cldf-bas.lua /size: 6623 b    last modification: 2020-07-01 14:35
1
if
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 25
local
tonumber
=
tonumber
26
local
type
=
type
27
local
format
=
string
.
format
28
local
utfchar
=
utf
.
char
29
local
concat
=
table
.
concat
30 31
local
context
=
context
32
local
ctxcore
=
context
.
core
33 34
local
variables
=
interfaces
.
variables
35 36
local
ctx_flushnode
=
context
.
nuts
.
flush
37
local
ctx_sprint
=
context
.
sprint
38
local
txtcatcodes
=
tex
.
txtcatcodes
39 40
local
nuts
=
nodes
.
nuts
41
local
tonode
=
nuts
.
tonode
42
local
nodepool
=
nuts
.
pool
43
local
new_rule
=
nodepool
.
rule
44
local
new_glyph
=
nodepool
.
glyph
45
local
new_latelua
=
nodepool
.
latelua
46 47
local
setattrlist
=
nuts
.
setattrlist
48 49
local
texgetcount
=
tex
.
getcount
50
local
texsetcount
=
tex
.
setcount
51 52
local
is_letter
=
characters
.
is_letter
53 54
-- a set of basic fast ones
55 56
function
context
.
setfontid
(
n
)
57
-- isn't there a setter?
58
context
(
"
\\setfontid%i\\relax
"
,
n
)
59
end
60 61
function
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
77
end
78 79
function
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
85
end
86 87
function
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
94
end
95 96
function
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))
106
end
107 108
function
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
118
end
119 120
-- local function ctx_par () context("\\par") end
121
-- local function ctx_space() context("\\space") end
122 123
local
ctx_par
=
context
.
cs
.
par
124
local
ctx_space
=
context
.
cs
.
space
125 126
context
.
par
=
ctx_par
127
context
.
space
=
ctx_space
128 129
ctxcore
.
par
=
ctx_par
130
ctxcore
.
space
=
ctx_space
131 132
-- local function ctx_bgroup() context("{") end
133
-- local function ctx_egroup() context("}") end
134 135
local
ctx_bgroup
=
context
.
cs
.
bgroup
136
local
ctx_egroup
=
context
.
cs
.
egroup
137 138
context
.
bgroup
=
ctx_bgroup
139
context
.
egroup
=
ctx_egroup
140 141
ctxcore
.
bgroup
=
ctx_bgroup
142
ctxcore
.
egroup
=
ctx_egroup
143 144
-- not yet used ... but will get variant at the tex end as well
145 146
local
function
setboxregister
(
kind
,
n
)
147
context
(
type
(
n
)
=
=
"
number
"
and
[[
\setbox%s\%s
]]
or
[[
\setbox\%s\%s
]]
,
n
,
kind
)
148
end
149 150
function
ctxcore
.
sethboxregister
(
n
)
setboxregister
(
"
hbox
"
,
n
)
end
151
function
ctxcore
.
setvboxregister
(
n
)
setboxregister
(
"
vbox
"
,
n
)
end
152
function
ctxcore
.
setvtopregister
(
n
)
setboxregister
(
"
vtop
"
,
n
)
end
153 154
local
function
startboxregister
(
kind
,
n
)
155
context
(
type
(
n
)
=
=
"
number
"
and
[[
\setbox%s\%s{
]]
or
[[
\setbox\%s\%s{
]]
,
n
,
kind
)
156
end
157 158
function
ctxcore
.
starthboxregister
(
n
)
startboxregister
(
"
hbox
"
,
n
)
end
159
function
ctxcore
.
startvboxregister
(
n
)
startboxregister
(
"
vbox
"
,
n
)
end
160
function
ctxcore
.
startvtopregister
(
n
)
startboxregister
(
"
vtop
"
,
n
)
end
161 162
ctxcore
.
stophboxregister
=
ctx_egroup
163
ctxcore
.
stopvboxregister
=
ctx_egroup
164
ctxcore
.
stopvtopregister
=
ctx_egroup
165 166
function
ctxcore
.
flushboxregister
(
n
)
167
context
(
type
(
n
)
=
=
"
number
"
and
[[
\box%s
]]
or
[[
\box\%s
]]
,
n
)
168
end
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 174
local
ctx_hbox
=
context
.
cs
.
hbox
175
local
ctx_vbox
=
context
.
cs
.
vbox
176
local
ctx_vtop
=
context
.
cs
.
vtop
177 178
function
ctxcore
.
beginhbox
(
)
ctx_hbox
(
)
ctx_bgroup
(
)
end
179
function
ctxcore
.
beginvbox
(
)
ctx_vbox
(
)
ctx_bgroup
(
)
end
180
function
ctxcore
.
beginvtop
(
)
ctx_vtop
(
)
ctx_bgroup
(
)
end
181 182
ctxcore
.
endhbox
=
ctx_egroup
-- \egroup
183
ctxcore
.
endvbox
=
ctx_egroup
-- \egroup
184
ctxcore
.
endvtop
=
ctx_egroup
-- \egroup
185 186
local
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
194
end
195 196
context
.
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 208
function
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
)
213
end
214 215
do
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 245
end
246 247