cldf-bas.lua /size: 6622 b    last modification: 2023-12-21 09:44
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 variables     = interfaces.variables
35
36local ctx_flushnode = context.nuts.flush
37local ctx_sprint    = context.sprint
38local txtcatcodes   = tex.txtcatcodes
39
40local nuts          = nodes.nuts
41local tonode        = nuts.tonode
42local nodepool      = nuts.pool
43local new_rule      = nodepool.rule
44local new_glyph     = nodepool.glyph
45local new_latelua   = nodepool.latelua
46
47local setattrlist   = nuts.setattrlist
48
49local texgetcount   = tex.getcount
50local texsetcount   = tex.setcount
51
52local is_letter     = characters.is_letter
53
54-- a set of basic fast ones
55
56function context.setfontid(n)
57    -- isn't there a setter?
58    context("\\setfontid%i\\relax",n)
59end
60
61function context.char(k) -- used as escape too, so don't change to utf
62    if type(k) == "table" then
63        local n = #k
64        if n == 1 then
65            context([[\char%s\relax]],k[1])
66        elseif n > 0 then
67            context([[\char%s\relax]],concat(k,[[\relax\char]]))
68        end
69    else
70        if type(k) == "string" then
71            k = tonumber(k)
72        end
73        if type(k) == "number" then
74            context([[\char%s\relax]],k)
75        end
76    end
77end
78
79function context.safechar(c)
80    if characters.is_letter[c] then -- not yet loaded
81        ctx_sprint(c)
82    else
83        ctx_sprint(txtcatcodes,c)
84    end
85end
86
87function context.utfchar(k)
88    if type(k) == "string" then
89        k = tonumber(k)
90    end
91    if type(k) == "number" then
92        context(utfchar(k))
93    end
94end
95
96function context.rule(w,h,d,direction)
97    local rule
98    if type(w) == "table" then
99        rule = new_rule(w.width,w.height,w.depth,w.direction)
100    else
101        rule = new_rule(w,h,d,direction)
102    end
103    setattrlist(rule,true)
104    context(tonode(rule))
105 -- ctx_flushnode(tonode(rule))
106end
107
108function context.glyph(id,k)
109    if id then
110        if not k then
111            id, k = true, id
112        end
113        local glyph = new_glyph(id,k)
114        setattrlist(glyph,true)
115        context(tonode(glyph))
116     -- ctx_flushnode(tonode(glyph))
117    end
118end
119
120-- local function ctx_par  () context("\\par")   end
121-- local function ctx_space() context("\\space") end
122
123local ctx_par   = context.cs.par
124local ctx_space = context.cs.space
125
126context.par   = ctx_par
127context.space = ctx_space
128
129ctxcore.par   = ctx_par
130ctxcore.space = ctx_space
131
132-- local function ctx_bgroup() context("{") end
133-- local function ctx_egroup() context("}") end
134
135local ctx_bgroup = context.cs.bgroup
136local ctx_egroup = context.cs.egroup
137
138context.bgroup = ctx_bgroup
139context.egroup = ctx_egroup
140
141ctxcore.bgroup = ctx_bgroup
142ctxcore.egroup = ctx_egroup
143
144-- not yet used ... but will get variant at the tex end as well
145
146local function setboxregister(kind,n)
147    context(type(n) == "number" and [[\setbox%s\%s]] or [[\setbox\%s\%s]],n,kind)
148end
149
150function ctxcore.sethboxregister(n) setboxregister("hbox",n) end
151function ctxcore.setvboxregister(n) setboxregister("vbox",n) end
152function ctxcore.setvtopregister(n) setboxregister("vtop",n) end
153
154local function startboxregister(kind,n)
155    context(type(n) == "number" and [[\setbox%s\%s{]] or [[\setbox\%s\%s{]],n,kind)
156end
157
158function ctxcore.starthboxregister(n) startboxregister("hbox",n) end
159function ctxcore.startvboxregister(n) startboxregister("vbox",n) end
160function ctxcore.startvtopregister(n) startboxregister("vtop",n) end
161
162ctxcore.stophboxregister = ctx_egroup
163ctxcore.stopvboxregister = ctx_egroup
164ctxcore.stopvtopregister = ctx_egroup
165
166function ctxcore.flushboxregister(n)
167    context(type(n) == "number" and [[\box%s ]] or [[\box\%s]],n)
168end
169
170-- function ctxcore.beginhbox() context([[\hbox\bgroup]]) end
171-- function ctxcore.beginvbox() context([[\vbox\bgroup]]) end
172-- function ctxcore.beginvtop() context([[\vtop\bgroup]]) end
173
174local ctx_hbox = context.cs.hbox
175local ctx_vbox = context.cs.vbox
176local ctx_vtop = context.cs.vtop
177
178function ctxcore.beginhbox() ctx_hbox() ctx_bgroup() end
179function ctxcore.beginvbox() ctx_vbox() ctx_bgroup() end
180function ctxcore.beginvtop() ctx_vtop() ctx_bgroup() end
181
182ctxcore.endhbox = ctx_egroup -- \egroup
183ctxcore.endvbox = ctx_egroup -- \egroup
184ctxcore.endvtop = ctx_egroup -- \egroup
185
186local function allocate(name,what,cmd)
187    local a = format("c_syst_last_allocated_%s",what)
188    local n = texgetcount(a) + 1
189    if n <= texgetcount("c_syst_max_allocated_register") then
190        texsetcount(a,n)
191    end
192    context("\\global\\expandafter\\%sdef\\csname %s\\endcsname %s\\relax",cmd or what,name,n)
193    return n
194end
195
196context.registers = {
197    -- the number is available directly, the csname after the lua call
198    newdimen  = function(name) return allocate(name,"dimen") end,
199    newskip   = function(name) return allocate(name,"skip") end,
200    newcount  = function(name) return allocate(name,"count") end,
201    newmuskip = function(name) return allocate(name,"muskip") end,
202    newtoks   = function(name) return allocate(name,"toks") end,
203    newbox    = function(name) return allocate(name,"box","mathchar") end,
204    -- not really a register but kind of belongs here
205    newchar   = function(name,u) context([[\chardef\%s=%s\relax]],name,u) end,
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