在 Context 中集成 lua 生成的 Metapost 图形

在 Context 中集成 lua 生成的 Metapost 图形

通常,我们可以使用 和 将 Metapost 图形集成到 Context 中\startuseMPgraphic{id}\useMPgraphic{id}这个想法是在某处定义图形并在其他地方使用它们。但是,当使用 lua 生成图形时,我无法使这种方法发挥作用。这可能吗?我这里遗漏了什么吗?

以下代码片段说明了我如何通过将绘图包装到函数中,然后调用该函数来解决此问题。这部分满足了我的要求,但当我尝试将图形宽度缩放到\textwidth(不太确定我是否正确执行了这一部分...)时,左侧的空白明显太多(参见输出)。这对于我的目的是不可接受的。

\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

裁剪后的输出(框架宽度为 \textwidth): 输出

答案1

我不是 ConTeXt 用户,但看起来你只是被虚假的空间所困扰:

\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

在此处输入图片描述

相关内容