[NTG-context] Lua code and table

Hans Hagen j.hagen at xs4all.nl
Fri Feb 19 13:06:26 CET 2021


On 2/19/2021 12:42 PM, Fabrice Couvreur wrote:
> Hi Hans,Thanks again for the ideas to improve my code. However, I am new 
> to Lua and my level is far from sufficient to understand everything. Why 
> in no table do the crosses appear in the cells; which was my original 
> idea.Thank you.Fabrice
I get a cross (see attached).

Hans



-----------------------------------------------------------------
                                           Hans Hagen | PRAGMA ADE
               Ridderstraat 27 | 8061 GH Hasselt | The Netherlands
        tel: 038 477 53 69 | www.pragma-ade.nl | www.pragma-pod.nl
-----------------------------------------------------------------
-------------- next part --------------
\setupbodyfont[pagella]

\starttext

\definextable
  [MyTable]
  [align={middle,lohi},
   width=0.8cm,
   offset=0.8ex,
   bodyfont=9pt,
   framecolor=cyan]

\startluacode
    function document.MyTable(specification)

        local pattern    = specification.pattern
        local action     = specification.action
        local conversion = specification.conversion or "Characters"

        local n = #pattern

    --  context.startxtable { align = "middle,lohi", width = "0.8cm", offset = "0.8ex", bodyfont = "9pt", framecolor = "cyan" }
        context.startxtable { "MyTable" }
            context.startxrow()
                context.startxcell { frame = "off" }
                    context()
                context.stopxcell()
                for i=1,n do
                    context.startxcell()
                        context.convertnumber(conversion,i)
                    context.stopxcell()
                end
            context.stopxrow()
            for i=1,n do
                context.startxrow()
                    context.startxcell()
                        context.convertnumber(conversion,i)
                    context.stopxcell()
                for j=1,n do
                    context.startxcell { align = "middle" }
                        action(string.sub(pattern[i],j,j))
                    context.stopxcell()
                end
                context.stopxrow()
            end
        context.stopxtable()

    end
\stopluacode

\startuniqueMPgraphic{cross}{width,height,depth,color,line}
    draw image (
        draw (\MPvar{depth},\MPvar{height}) -- (\MPvar{width},0) ;
        draw (\MPvar{depth},0) -- (\MPvar{width},\MPvar{height}) ;
    )
        withpen pencircle scaled \MPvar{line}
        withcolor \MPvar{color} ;
\stopuniqueMPgraphic

\definesymbol
  [MyRedCross]
  [\uniqueMPgraphic{cross}{width=1em,height=1ex,depth=.5ex,color=red,line=.2ex}]

\definesymbol
  [MyGreenCross]
  [\uniqueMPgraphic{cross}{width=1em,height=1ex,depth=.5ex,color=green,line=.2ex}]

\startluacode
    document.MyTable {
        action = function(value)
            context(value == "X" and 1 or 0)
        end,
        conversion = "Characters",
        pattern = {
            ".XXX..XX",
            "X...XXX.",
            "X..X.XXX",
            "X.X.X..X",
            ".X.X.XX.",
            ".XX.X...",
            "XXX.X...",
            "X.XX...."
        }
    }
\stopluacode

\startluacode
    document.MyTable {
        action = function(value)
            context(value == "X" and 1 or 0)
        end,
        conversion = "Greek",
        pattern = {
            ".XXX..XX",
            "X...XXX.",
            "X..X.XXX",
            "X.X.X..X",
        }
    }
\stopluacode

\startluacode
    document.MyTable {
        action = function(value)
            if value == "X" then
                context.symbol { "MyRedCross" }
            end
        end,
        conversion = "Romannumerals",
        pattern = {
            ".XXX..XX",
            "X...XXX.",
            "X..X.XXX",
            "X.X.X..X",
            ".X.X.XX.",
            ".XX.X...",
            "XXX.X...",
            "X.XX...."
        }
    }
\stopluacode

\startluacode
    document.MyTable {
        action = function(value)
            context.symbol { value == "X" and "MyRedCross"  or "MyGreenCross" }
        end,
        conversion = "Romannumerals",
        pattern = {
            "X.X.X..X",
            ".X.X.XX.",
            ".XX.X...",
            "XXX.X...",
            "X.XX...."
        }
    }
\stopluacode

\stoptext
-------------- next part --------------
A non-text attachment was scrubbed...
Name: xx.pdf
Type: application/pdf
Size: 12946 bytes
Desc: not available
URL: <http://mailman.ntg.nl/pipermail/ntg-context/attachments/20210219/78feeef8/attachment-0001.pdf>


More information about the ntg-context mailing list