cldf-bas.lmt /size: 7208 b    last modification: 2025-02-21 11:03
1if not modules then modules = { } end modules ['cldf-bas'] = {
2    version   = 1.001,
3    comment   = "companion to cldf-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
9-- -- speedtest needed:
10--
11-- local flush, writer = context.getlogger()
12--
13-- trackers.register("context.trace",function(v)
14--     flush, writer = context.getlogger()
15-- end)
16--
17-- function context.bgroup()
18--     flush(ctxcatcodes,"{")
19-- end
20--
21-- function context.egroup()
22--     flush(ctxcatcodes,"}")
23-- end
24
25local tonumber      = tonumber
26local type          = type
27local format        = string.format
28local utfchar       = utf.char
29local concat        = table.concat
30
31local context       = context
32local ctxcore       = context.core
33
34local ctx_flushnode = context.nuts.flush
35local ctx_sprint    = context.sprint
36
37local txtcatcodes   <const> = tex.txtcatcodes
38
39local nuts          = nodes.nuts
40local tonode        = nuts.tonode
41local nodepool      = nuts.pool
42local new_rule      = nodepool.rule
43local new_glyph     = nodepool.glyph
44local new_latelua   = nodepool.latelua
45
46local setattrlist   = nuts.setattrlist
47
48local texgetcount   = tex.getcount
49local texsetcount   = tex.setcount
50local texchardef    = tex.chardef
51local expandmacro   = token.expandmacro
52
53local is_letter     = characters.is_letter
54
55-- a set of basic fast ones
56
57function context.setfontid(n)
58    -- isn't there a setter?
59    context("\\setfontid%i\\relax",n)
60end
61
62function context.char(k) -- used as escape too, so don't change to utf
63    if type(k) == "table" then
64        local n = #k
65        if n == 1 then
66            context([[\char%s\relax]],k[1])
67        elseif n > 0 then
68            context([[\char%s\relax]],concat(k,[[\relax\char]]))
69        end
70    else
71        if type(k) == "string" then
72            k = tonumber(k)
73        end
74        if type(k) == "number" then
75            context([[\char%s\relax]],k)
76        end
77    end
78end
79
80function context.safechar(c)
81    if characters.is_letter[c] then -- not yet loaded
82        ctx_sprint(c)
83    else
84        ctx_sprint(txtcatcodes,c)
85    end
86end
87
88function context.utfchar(k)
89    if type(k) == "string" then
90        k = tonumber(k)
91    end
92    if type(k) == "number" then
93        context(utfchar(k))
94    end
95end
96
97function context.rule(w,h,d,direction)
98    local rule
99    if type(w) == "table" then
100        rule = new_rule(w.width,w.height,w.depth,w.direction)
101    else
102        rule = new_rule(w,h,d,direction)
103    end
104    setattrlist(rule,true)
105    context(tonode(rule))
106 -- ctx_flushnode(tonode(rule))
107end
108
109function context.glyph(id,k)
110    if id then
111        if not k then
112            id, k = true, id
113        end
114        local glyph = new_glyph(id,k)
115        setattrlist(glyph,true)
116        context(tonode(glyph))
117     -- ctx_flushnode(tonode(glyph))
118    end
119end
120
121-- local function ctx_par  () context("\\par")   end
122-- local function ctx_space() context("\\space") end
123
124local ctx_par   = context.cs.par
125local ctx_space = context.cs.space
126
127context.par   = ctx_par
128context.space = ctx_space
129
130ctxcore.par   = ctx_par
131ctxcore.space = ctx_space
132
133-- local function ctx_bgroup() context("{") end
134-- local function ctx_egroup() context("}") end
135
136local ctx_bgroup = context.cs.bgroup
137local ctx_egroup = context.cs.egroup
138
139context.bgroup = ctx_bgroup
140context.egroup = ctx_egroup
141
142ctxcore.bgroup = ctx_bgroup
143ctxcore.egroup = ctx_egroup
144
145-- not yet used ... but will get variant at the tex end as well
146
147local function setboxregister(kind,n)
148    context(type(n) == "number" and [[\setbox%s\%s]] or [[\setbox\%s\%s]],n,kind)
149end
150
151function ctxcore.sethboxregister(n) setboxregister("hbox",n) end
152function ctxcore.setvboxregister(n) setboxregister("vbox",n) end
153function ctxcore.setvtopregister(n) setboxregister("vtop",n) end
154
155local function startboxregister(kind,n)
156    context(type(n) == "number" and [[\setbox%s\%s{]] or [[\setbox\%s\%s{]],n,kind)
157end
158
159function ctxcore.starthboxregister(n) startboxregister("hbox",n) end
160function ctxcore.startvboxregister(n) startboxregister("vbox",n) end
161function ctxcore.startvtopregister(n) startboxregister("vtop",n) end
162
163ctxcore.stophboxregister = ctx_egroup
164ctxcore.stopvboxregister = ctx_egroup
165ctxcore.stopvtopregister = ctx_egroup
166
167function ctxcore.flushboxregister(n)
168    context(type(n) == "number" and [[\box%s ]] or [[\box\%s]],n)
169end
170
171-- function ctxcore.beginhbox() context([[\hbox\bgroup]]) end
172-- function ctxcore.beginvbox() context([[\vbox\bgroup]]) end
173-- function ctxcore.beginvtop() context([[\vtop\bgroup]]) end
174
175local ctx_hbox = context.cs.hbox
176local ctx_vbox = context.cs.vbox
177local ctx_vtop = context.cs.vtop
178
179function ctxcore.beginhbox() ctx_hbox() ctx_bgroup() end
180function ctxcore.beginvbox() ctx_vbox() ctx_bgroup() end
181function ctxcore.beginvtop() ctx_vtop() ctx_bgroup() end
182
183ctxcore.endhbox = ctx_egroup -- \egroup
184ctxcore.endvbox = ctx_egroup -- \egroup
185ctxcore.endvtop = ctx_egroup -- \egroup
186
187local function allocate(name,what,cmd)
188    local a = format("c_syst_last_allocated_%s",what)
189    local n = texgetcount(a) + 1
190    if n <= texgetcount("c_syst_max_allocated_register") then
191        texsetcount(a,n)
192    end
193    context("\\global\\expandafter\\%sdef\\csname %s\\endcsname %s\\relax",cmd or what,name,n)
194    return n
195end
196
197context.registers = {
198    newdimen  = function(name) expandmacro("syst_new_dimen", true,name) return texgetcount("c_syst_last_allocated_dimen")  end,
199    newskip   = function(name) expandmacro("syst_new_skip",  true,name) return texgetcount("c_syst_last_allocated_skip" )  end,
200    newcount  = function(name) expandmacro("syst_new_count", true,name) return texgetcount("c_syst_last_allocated_count")  end,
201    newmuskip = function(name) expandmacro("syst_new_muskip",true,name) return texgetcount("c_syst_last_allocated_muskip") end,
202    newtoks   = function(name) expandmacro("syst_new_toks",  true,name) return texgetcount("c_syst_last_allocated_toks")   end,
203    newbox    = function(name) expandmacro("syst_new_box",   true,name) return texgetcount("c_syst_last_allocated_box")    end,
204    -- not really a register but kind of belongs here
205    newchar   = texchardef,
206}
207
208function context.latelua(f)
209    -- table check moved elsewhere
210    local latelua = new_latelua(f)
211    setattrlist(latelua,true) -- will become an option
212    ctx_flushnode(latelua,true)
213end
214
215do
216
217    local NC = ctxcore.NC
218    local BC = ctxcore.BC
219    local NR = ctxcore.NR
220
221    context.nc = setmetatable({ }, {
222        __call =
223            function(t,...)
224                NC()
225                return context(...)
226            end,
227        __index =
228            function(t,k)
229                NC()
230                return context[k]
231            end,
232        }
233    )
234
235    function context.bc(...)
236        BC()
237        return context(...)
238    end
239
240    function context.nr(...)
241        NC()
242        NR()
243    end
244
245end
246
247-- for the moment here:
248
249do
250
251    local texset = tex.set
252
253    local maxdimen <const> = tex.magicconstants.maxdimen
254
255    function tex.dontcomplain()
256        texset("hbadness",maxdimen)
257        texset("vbadness",maxdimen)
258        texset("hfuzz",   maxdimen)
259        texset("vfuzz",   maxdimen)
260    end
261
262end
263