node-ini.lmt /size: 8343 b    last modification: 2024-01-16 09:02
1if not modules then modules = { } end modules ['node-ini'] = {
2    version   = 1.001,
3    comment   = "companion to node-ini.mkiv",
4    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
5    copyright = "PRAGMA ADE / ConTeXt Development Team",
6    license   = "see context related readme files"
7}
8
9local next, type, tostring = next, type, tostring
10local gsub = string.gsub
11local concat, remove = table.concat, table.remove
12local sortedhash, sortedkeys, swapped = table.sortedhash, table.sortedkeys, table.swapped
13
14-- Access to nodes is what gives LuaTeX its power. Here we implement a few helper
15-- functions. These functions are rather optimized.
16
17nodes                = nodes or { }
18local nodes          = nodes
19nodes.handlers       = nodes.handlers or { }
20
21local mark           = utilities.storage.mark
22local allocate       = utilities.storage.allocate
23local formatcolumns  = utilities.formatters.formatcolumns
24
25local getsubtypes    = node.subtypes
26----- getvalues      = node.values
27
28local listcodes      = mark(getsubtypes("list"))
29local rulecodes      = mark(getsubtypes("rule"))
30local dircodes       = mark(getsubtypes("dir"))
31local markcodes      = mark(getsubtypes("mark"))
32local glyphcodes     = mark(getsubtypes("glyph"))
33local disccodes      = mark(getsubtypes("disc"))
34local gluecodes      = mark(getsubtypes("glue"))
35local boundarycodes  = mark(getsubtypes("boundary"))
36local penaltycodes   = mark(getsubtypes("penalty"))
37local kerncodes      = mark(getsubtypes("kern"))
38local mathcodes      = mark(getsubtypes("math"))
39local noadcodes      = mark(getsubtypes("noad"))
40local radicalcodes   = mark(getsubtypes("radical"))
41local accentcodes    = mark(getsubtypes("accent"))
42local fencecodes     = mark(getsubtypes("fence"))
43local choicecodes    = mark(getsubtypes("choice"))
44----- fractioncodes  = mark(getsubtypes("fraction"))
45local parcodes       = mark(getsubtypes("par"))
46local attributecodes = mark(getsubtypes("attribute"))
47
48local function simplified(t)
49    local r = { }
50    for k, v in next, t do
51        r[k] = gsub(v,"_","")
52    end
53    return r
54end
55
56-- if environment.initex then
57--     local texintegerdef = tex.integerdef
58--     for i=1,#nodecodes do
59--         texintegerdef(nodecodes[i] .. "nodecode",i,"immutable")
60--     end
61-- end
62
63-- local noadoptions = allocate {
64--     set      =        0x08,
65--     unused_1 = 0x00 + 0x08,
66--     unused_2 = 0x01 + 0x08,
67--     axis     = 0x02 + 0x08,
68--     no_axis  = 0x04 + 0x08,
69--     exact    = 0x10 + 0x08,
70--     left     = 0x11 + 0x08,
71--     middle   = 0x12 + 0x08,
72--     right    = 0x14 + 0x08,
73-- }
74
75local nodecodes = simplified(node.types())
76
77gluecodes      = allocate(swapped(gluecodes,gluecodes))
78dircodes       = allocate(swapped(dircodes,dircodes))
79markcodes      = allocate(swapped(markcodes,markcodes))
80boundarycodes  = allocate(swapped(boundarycodes,boundarycodes))
81noadcodes      = allocate(swapped(noadcodes,noadcodes))
82radicalcodes   = allocate(swapped(radicalcodes,radicalcodes))
83nodecodes      = allocate(swapped(nodecodes,nodecodes))
84listcodes      = allocate(swapped(listcodes,listcodes))
85glyphcodes     = allocate(swapped(glyphcodes,glyphcodes))
86kerncodes      = allocate(swapped(kerncodes,kerncodes))
87penaltycodes   = allocate(swapped(penaltycodes,penaltycodes))
88mathcodes      = allocate(swapped(mathcodes,mathcodes))
89disccodes      = allocate(swapped(disccodes,disccodes))
90accentcodes    = allocate(swapped(accentcodes,accentcodes))
91fencecodes     = allocate(swapped(fencecodes,fencecodes))
92choicecodes    = allocate(swapped(choicecodes,choicecodes))
93parcodes       = allocate(swapped(parcodes,parcodes))
94attributecodes = allocate(swapped(attributecodes,attributecodes))
95rulecodes      = allocate(swapped(rulecodes,rulecodes))
96
97nodes.gluecodes      = gluecodes
98nodes.dircodes       = dircodes
99nodes.markcodes      = markcodes
100nodes.boundarycodes  = boundarycodes
101nodes.noadcodes      = noadcodes
102nodes.listcodes      = listcodes
103nodes.glyphcodes     = glyphcodes
104nodes.kerncodes      = kerncodes
105nodes.penaltycodes   = penaltycodes
106nodes.mathcodes      = mathcodes
107nodes.disccodes      = disccodes
108nodes.accentcodes    = accentcodes
109nodes.radicalcodes   = radicalcodes
110nodes.fencecodes     = fencecodes
111nodes.choicecodes    = choicecodes
112nodes.parcodes       = parcodes
113nodes.attributecodes = attributecodes
114nodes.rulecodes      = rulecodes
115
116nodes.nodecodes      = nodecodes
117
118-- these are now in tex namespace but we keep them for old times sake
119
120nodes.fillvalues      = tex.fillcodes
121nodes.fillcodes       = tex.fillcodes
122nodes.dirvalues       = tex.directioncodes
123nodes.directionvalues = tex.directioncodes
124nodes.mathvalues      = tex.mathparametercodes
125
126-- we will transition to more verbose subtypes (after other math is done)
127
128noadcodes.ord      = noadcodes.ord   or noadcodes.ordinary
129noadcodes.operator = noadcodes.op    or noadcodes.operator
130noadcodes.bin      = noadcodes.bin   or noadcodes.binary
131noadcodes.rel      = noadcodes.rel   or noadcodes.relation
132noadcodes.punct    = noadcodes.punct or noadcodes.punctuation
133noadcodes.rad      = noadcodes.rad   or noadcodes.radical
134noadcodes.frac     = noadcodes.frac  or noadcodes.fraction
135noadcodes.acc      = noadcodes.acc   or noadcodes.accent
136
137-- so for now:
138
139noadcodes.ordinary    = noadcodes.ordinary    or noadcodes.ord
140noadcodes.operator    = noadcodes.operator    or noadcodes.op
141noadcodes.binary      = noadcodes.binary      or noadcodes.bin
142noadcodes.relation    = noadcodes.relation    or noadcodes.rel
143noadcodes.punctuation = noadcodes.punctuation or noadcodes.punct
144noadcodes.radical     = noadcodes.radical     or noadcodes.rad
145noadcodes.fraction    = noadcodes.fraction    or noadcodes.frac
146noadcodes.accent      = noadcodes.accent      or noadcodes.acc
147
148local subtypes = allocate {
149    glue      = gluecodes,
150    dir       = dircodes,
151    mark      = markcodes,
152    boundary  = boundarycodes,
153    noad      = noadcodes,
154    glyph     = glyphcodes,
155    kern      = kerncodes,
156    penalty   = penaltycodes,
157    math      = mathcodes,
158    disc      = disccodes,
159    accent    = accentcodes,
160    radical   = radicalcodes,
161    fence     = fencecodes,
162    choice    = choicecodes,
163    par       = parcodes,
164    attribute = attributecodes,
165    rule      = rulecodes,
166
167    vlist     = listcodes,
168    hlist     = listcodes,
169
170 -- list      = listcodes,
171
172 -- parameter = parametercodes,
173 -- user      = usercodes,
174}
175
176for k, v in table.sortedhash(subtypes) do
177    local i = nodecodes[k]
178    if i and not subtypes[i] then
179        subtypes[i] = v
180    end
181end
182
183nodes.subtypes = subtypes
184
185-- a few more friendly aliases:
186
187nodes.skipcodes            = gluecodes
188nodes.directioncodes       = dircodes
189nodes.discretionarycodes   = disccodes
190
191glyphcodes.glyph           = glyphcodes.character
192
193gluecodes.parfillrightskip = gluecodes.parfillrightskip or gluecodes.parfillskip
194gluecodes.parfillskip      = gluecodes.parfillskip      or gluecodes.parfillrightskip
195
196listcodes.row              = listcodes.alignment
197listcodes.column           = listcodes.alignment
198
199kerncodes.kerning          = kerncodes.fontkern
200kerncodes.italiccorrection = kerncodes.italiccorrection or 1 -- new
201
202-- We use the real node code numbers.
203
204local texsetintegervalue = tex.setintegervalue
205
206for i=0,nodecodes.glyph do
207    texsetintegervalue(nodecodes[i] .. "nodecode",i,"immutable")
208end
209texsetintegervalue("tempnodecode",nodecodes.temp,"immutable") -- can happen in tables
210
211for i=0,#gluecodes do
212    texsetintegervalue(gluecodes[i] .. "subtypecode",i,"immutable")
213end
214
215nodes.specialskipcodes = {
216    [gluecodes.leftskip]                             = true,
217    [gluecodes.rightskip]                            = true,
218    [gluecodes.lefthangskip]                         = true,
219    [gluecodes.righthangskip]                        = true,
220    [gluecodes.parfillleftskip  or parfillskip_code] = true,
221    [gluecodes.parfillrightskip or parfillskip_code] = true,
222    [gluecodes.indentskip]                           = true,
223    [gluecodes.correctionskip]                       = true,
224}
225
226table.setmetatableindex(listcodes,function(t,k)
227    local v
228    if type(k) == "string" then
229        v = mathematics.classes[k] + 0x100
230    else
231        local i = k - 0x100
232        v = mathematics.classes[i] or mathematics.classnames[i]
233    end
234    if not v then
235        v = listcodes.unknown
236    end
237    t[k] = v
238    return v
239end)
240