2

Normally we can integrate Metapost graphics in Context with the use of \startuseMPgraphic{id} and \useMPgraphic{id}. The idea is to define the graphics somewhere and use them somewhere else. However, I'm not able to get this kind of approach working when using lua to generate graphics. Is it even possible? Am I missing something here?

The following code snippet illustrates how I tried to solve this problem by wrapping the drawing to function, and then calling that function. This partially satisfies me, but when I try to scale the graphics width to \textwidth (not quite sure if I'm doing this part right...), there is clearly too much white space in the left side (see output). This is unacceptable for my purpose.

\startluacode

local function drawRow(row, row_num_total, row_num)
        local w = 1
        local d = 0.2
        row_num = row_num_total - row_num

        for i=1,#row do
            if row[i] == 1 then
                local x_shift = (i - 1) * (w + d)
                local y_shift = row_num * (w + d)

                context.metafun( "fill unitcircle " ..
                                 "shifted (%s, %s) " ..
                                 "withcolor red;", 
                                  x_shift , y_shift)
            end
        end
end

function drawMyLogo()
    local logo ={ 
            {1,1,1,1,1,1,1,0,1,1,1,1,1,0,1,0,0,0,0,0,1,},
            {0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,1,0,0,0,1,0,},
            {0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,1,0,1,0,0,},
            {0,0,0,1,0,0,0,0,1,1,1,1,1,0,0,0,0,1,0,0,0,},
            {0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,1,0,1,0,0,},
            {0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,1,0,0,0,1,0,},
            {0,0,0,1,0,0,0,0,1,1,1,1,1,0,1,0,0,0,0,0,1,},
    }

    context.metafun.start()
        for i=1,#logo do
            drawRow( logo[i], #logo, i )
        end
    context.metafun.stop()
end

\stopluacode


\starttext

\framed[width=\textwidth] {
    \scale[width=\textwidth]{
        \startluacode
            drawMyLogo()
        \stopluacode
    }
}

\stoptext

The cropped output (frame width is \textwidth): output

1 Answer 1

2

I'm not a ConTeXt user, but it looks as if you got simply caught by spurious spaces:

\startluacode

local function drawRow(row, row_num_total, row_num)
        local w = 1
        local d = 0.2
        row_num = row_num_total - row_num

        for i=1,#row do
            if row[i] == 1 then
                local x_shift = (i - 1) * (w + d)
                local y_shift = row_num * (w + d)

                context.metafun( "fill unitcircle " ..
                                 "shifted (%s, %s) " ..
                                 "withcolor red;",
                                  x_shift , y_shift)
            end
        end
end

function drawMyLogo()
    local logo ={
            {1,1,1,1,1,1,1,0,1,1,1,1,1,0,1,0,0,0,0,0,1,},
            {0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,1,0,0,0,1,0,},
            {0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,1,0,1,0,0,},
            {0,0,0,1,0,0,0,0,1,1,1,1,1,0,0,0,0,1,0,0,0,},
            {0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,1,0,1,0,0,},
            {0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,1,0,0,0,1,0,},
            {0,0,0,1,0,0,0,0,1,1,1,1,1,0,1,0,0,0,0,0,1,},
    }

    context.metafun.start()
        for i=1,#logo do
            drawRow( logo[i], #logo, i )
        end
    context.metafun.stop()
end

\stopluacode


\starttext

\framed[width=\textwidth] 
{% here
    \scale[width=0.3\textwidth]{% here
        \startluacode
            drawMyLogo()
        \stopluacode
    }
}

\framed[width=\textwidth] 
{% here
    \scale[width=\textwidth]{% here
        \startluacode
            drawMyLogo()
        \stopluacode
    }
}

\stoptext

enter image description here

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.