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
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
56
57function context.setfontid(n)
58
59 context("\\setfontid%i\\relax",n)
60end
61
62function context.char(k)
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
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
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
118 end
119end
120
121
122
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
134
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
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
172
173
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
184ctxcore.endvbox = ctx_egroup
185ctxcore.endvtop = ctx_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
205 newchar = texchardef,
206}
207
208function context.latelua(f)
209
210 local latelua = new_latelua(f)
211 setattrlist(latelua,true)
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
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 |