font-hsh.lua /size: 10 Kb    last modification: 2020-07-01 14:35
1
if
not
modules
then
modules
=
{
}
end
modules
[
'
font-hsh
'
]
=
{
2
version
=
1
.
001
,
3
comment
=
"
companion to font-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
local
rawget
=
rawget
10 11
local
setmetatableindex
=
table
.
setmetatableindex
12
local
currentfont
=
font
and
font
.
current
-- used in the web service
13
local
allocate
=
utilities
.
storage
.
allocate
14 15
local
fonts
=
fonts
16
local
hashes
=
fonts
.
hashes
or
allocate
(
)
17
fonts
.
hashes
=
hashes
18 19
-- todo: autoallocate ... just create on the fly .. use constructors.keys (problem: plurals)
20 21
local
identifiers
=
hashes
.
identifiers
or
allocate
(
)
22
local
characters
=
hashes
.
characters
or
allocate
(
)
-- chardata
23
local
descriptions
=
hashes
.
descriptions
or
allocate
(
)
24
local
parameters
=
hashes
.
parameters
or
allocate
(
)
25
local
mathparameters
=
hashes
.
mathparameters
or
allocate
(
)
26
local
properties
=
hashes
.
properties
or
allocate
(
)
27
local
resources
=
hashes
.
resources
or
allocate
(
)
28
local
spacings
=
hashes
.
spacings
or
allocate
(
)
29
local
spaces
=
hashes
.
spaces
or
allocate
(
)
30
local
quads
=
hashes
.
quads
or
allocate
(
)
-- maybe also spacedata
31
local
xheights
=
hashes
.
xheights
or
allocate
(
)
32
local
csnames
=
hashes
.
csnames
or
allocate
(
)
-- namedata
33
local
features
=
hashes
.
features
or
allocate
(
)
34
local
marks
=
hashes
.
marks
or
allocate
(
)
35
local
classes
=
hashes
.
classes
or
allocate
(
)
36
local
italics
=
hashes
.
italics
or
allocate
(
)
37
local
lastmathids
=
hashes
.
lastmathids
or
allocate
(
)
38
local
dynamics
=
hashes
.
dynamics
or
allocate
(
)
39
local
unicodes
=
hashes
.
unicodes
or
allocate
(
)
40
local
originals
=
hashes
.
originals
or
allocate
(
)
41
local
modes
=
hashes
.
modes
or
allocate
(
)
42
local
variants
=
hashes
.
variants
or
allocate
(
)
43 44
hashes
.
characters
=
characters
45
hashes
.
descriptions
=
descriptions
46
hashes
.
parameters
=
parameters
47
hashes
.
mathparameters
=
mathparameters
48
hashes
.
properties
=
properties
49
hashes
.
resources
=
resources
50
hashes
.
spacings
=
spacings
51
hashes
.
spaces
=
spaces
52
hashes
.
quads
=
quads
hashes
.
emwidths
=
quads
53
hashes
.
xheights
=
xheights
hashes
.
exheights
=
xheights
54
hashes
.
csnames
=
csnames
55
hashes
.
features
=
features
56
hashes
.
marks
=
marks
57
hashes
.
classes
=
classes
58
hashes
.
italics
=
italics
59
hashes
.
lastmathids
=
lastmathids
60
hashes
.
dynamics
=
dynamics
61
hashes
.
unicodes
=
unicodes
62
hashes
.
originals
=
originals
63
hashes
.
modes
=
modes
64
hashes
.
variants
=
variants
65 66
local
nodepool
=
nodes
and
nodes
.
pool
67
local
dummyglyph
=
nodepool
and
nodepool
.
register
(
nodepool
.
glyph
(
)
)
68 69
local
nulldata
=
allocate
{
70
name
=
"
nullfont
"
,
71
characters
=
{
}
,
72
descriptions
=
{
}
,
73
properties
=
{
74
designsize
=
786432
,
75
}
,
76
parameters
=
{
-- lmromanregular @ 12pt
77
slantperpoint
=
0
,
78
spacing
=
{
79
width
=
256377
,
80
stretch
=
128188
,
81
shrink
=
85459
,
82
extra
=
85459
,
83
}
,
84
quad
=
786432
,
85
xheight
=
338952
,
86
-- compatibility:
87
slant
=
0
,
-- 1
88
space
=
256377
,
-- 2
89
space_stretch
=
128188
,
-- 3
90
space_shrink
=
85459
,
-- 4
91
x_height
=
338952
,
-- 5
92
quad
=
786432
,
-- 6
93
extra_space
=
85459
,
-- 7
94
size
=
786432
,
95
}
,
96
}
97 98
fonts
.
nulldata
=
nulldata
99 100
fonts
.
constructors
.
enhanceparameters
(
nulldata
.
parameters
)
-- official copies for us
101 102
setmetatableindex
(
identifiers
,
function
(
t
,
k
)
103
return
k
=
=
true
and
identifiers
[
currentfont
(
)
]
or
nulldata
104
end
)
105 106
if
font
then
107 108
-- to be used
109 110
local
define
=
font
.
define
111
local
setfont
=
font
.
setfont
112
local
frozen
=
font
.
frozen
113 114
function
fonts
.
reserveid
(
fontdata
)
115
return
define
(
fontdata
or
nulldata
)
116
end
117 118
function
fonts
.
enhanceid
(
id
,
fontdata
)
119
if
not
frozen
(
id
)
then
120
setfont
(
id
,
fontdata
)
121
end
122
end
123 124
end
125 126
setmetatableindex
(
characters
,
function
(
t
,
k
)
127
if
k
=
=
true
then
128
return
characters
[
currentfont
(
)
]
129
else
130
local
characters
=
identifiers
[
k
]
.
characters
131
t
[
k
]
=
characters
132
return
characters
133
end
134
end
)
135 136
setmetatableindex
(
descriptions
,
function
(
t
,
k
)
137
if
k
=
=
true
then
138
return
descriptions
[
currentfont
(
)
]
139
else
140
local
descriptions
=
identifiers
[
k
]
.
descriptions
141
t
[
k
]
=
descriptions
142
return
descriptions
143
end
144
end
)
145 146
setmetatableindex
(
parameters
,
function
(
t
,
k
)
147
if
k
=
=
true
then
148
return
parameters
[
currentfont
(
)
]
149
else
150
local
parameters
=
identifiers
[
k
]
.
parameters
151
t
[
k
]
=
parameters
152
return
parameters
153
end
154
end
)
155 156
setmetatableindex
(
mathparameters
,
function
(
t
,
k
)
157
if
k
=
=
true
then
158
return
mathparameters
[
currentfont
(
)
]
159
else
160
local
mathparameters
=
identifiers
[
k
]
.
mathparameters
161
t
[
k
]
=
mathparameters
162
return
mathparameters
163
end
164
end
)
165 166
setmetatableindex
(
properties
,
function
(
t
,
k
)
167
if
k
=
=
true
then
168
return
properties
[
currentfont
(
)
]
169
else
170
local
properties
=
identifiers
[
k
]
.
properties
171
t
[
k
]
=
properties
172
return
properties
173
end
174
end
)
175 176
setmetatableindex
(
resources
,
function
(
t
,
k
)
177
if
k
=
=
true
then
178
return
resources
[
currentfont
(
)
]
179
else
180
local
shared
=
identifiers
[
k
]
.
shared
181
local
rawdata
=
shared
and
shared
.
rawdata
182
local
resources
=
rawdata
and
rawdata
.
resources
183
t
[
k
]
=
resources
or
false
-- better than resolving each time
184
return
resources
185
end
186
end
)
187 188
setmetatableindex
(
features
,
function
(
t
,
k
)
189
if
k
=
=
true
then
190
return
features
[
currentfont
(
)
]
191
else
192
local
shared
=
identifiers
[
k
]
.
shared
193
local
features
=
shared
and
shared
.
features
or
{
}
194
t
[
k
]
=
features
195
return
features
196
end
197
end
)
198 199
local
nospacing
=
{
200
width
=
0
,
201
stretch
=
0
,
202
shrink
=
0
,
203
extra
=
0
,
204
}
205 206
setmetatableindex
(
spacings
,
function
(
t
,
k
)
207
if
k
=
=
true
then
208
return
spacings
[
currentfont
(
)
]
209
else
210
local
parameters
=
parameters
[
k
]
211
local
spacing
=
parameters
and
parameters
.
spacing
or
nospacing
212
t
[
k
]
=
spacing
213
return
spacing
214
end
215
end
)
216 217
setmetatableindex
(
spaces
,
function
(
t
,
k
)
218
if
k
=
=
true
then
219
return
spaces
[
currentfont
(
)
]
220
else
221
local
space
=
spacings
[
k
]
.
width
222
t
[
k
]
=
space
223
return
space
224
end
225
end
)
226 227
setmetatableindex
(
marks
,
function
(
t
,
k
)
228
if
k
=
=
true
then
229
return
marks
[
currentfont
(
)
]
230
else
231
local
resources
=
identifiers
[
k
]
.
resources
or
{
}
232
local
marks
=
resources
.
marks
or
{
}
233
t
[
k
]
=
marks
234
return
marks
235
end
236
end
)
237 238
setmetatableindex
(
classes
,
function
(
t
,
k
)
239
if
k
=
=
true
then
240
return
classes
[
currentfont
(
)
]
241
else
242
local
resources
=
identifiers
[
k
]
.
resources
or
{
}
243
local
classes
=
resources
.
classes
or
{
}
244
t
[
k
]
=
classes
245
return
classes
246
end
247
end
)
248 249
setmetatableindex
(
quads
,
function
(
t
,
k
)
250
if
k
=
=
true
then
251
return
quads
[
currentfont
(
)
]
252
else
253
local
parameters
=
rawget
(
parameters
,
k
)
254
local
quad
255
if
parameters
then
256
quad
=
parameters
.
quad
257
elseif
dummyglyph
then
258
dummyglyph
.
font
=
k
259
dummyglyph
.
char
=
0x2014
-- emdash
260
quad
=
dummyglyph
.
width
-- dirty trick
261
end
262
if
not
quad
or
quad
=
=
0
then
263
quad
=
655360
-- lm 10pt
264
end
265
t
[
k
]
=
quad
266
return
quad
267
end
268
end
)
269 270
setmetatableindex
(
xheights
,
function
(
t
,
k
)
271
if
k
=
=
true
then
272
return
xheights
[
currentfont
(
)
]
273
else
274
local
parameters
=
rawget
(
parameters
,
k
)
275
local
xheight
276
if
parameters
then
277
xheight
=
parameters
.
xheight
278
elseif
dummyglyph
then
279
dummyglyph
.
font
=
k
280
dummyglyph
.
char
=
0x78
-- x
281
xheight
=
dummyglyph
.
height
-- dirty trick
282
end
283
if
not
xheight
or
xheight
=
=
0
then
284
xheight
=
282460
-- lm 10pt
285
end
286
t
[
k
]
=
xheight
287
return
xheight
288
end
289
end
)
290 291
setmetatableindex
(
italics
,
function
(
t
,
k
)
-- is test !
292
if
k
=
=
true
then
293
return
italics
[
currentfont
(
)
]
294
else
295
local
properties
=
identifiers
[
k
]
.
properties
296
local
hasitalics
=
properties
and
properties
.
hasitalics
297
if
hasitalics
then
298
hasitalics
=
characters
[
k
]
-- convenient return
299
else
300
hasitalics
=
false
301
end
302
t
[
k
]
=
hasitalics
303
return
hasitalics
304
end
305
end
)
306 307
setmetatableindex
(
dynamics
,
function
(
t
,
k
)
308
if
k
=
=
true
then
309
return
dynamics
[
currentfont
(
)
]
310
else
311
local
shared
=
identifiers
[
k
]
.
shared
312
local
dynamics
=
shared
and
shared
.
dynamics
or
false
313
t
[
k
]
=
dynamics
314
return
dynamics
315
end
316
end
)
317 318
setmetatableindex
(
unicodes
,
function
(
t
,
k
)
-- always a unicode
319
if
k
=
=
true
then
320
return
unicodes
[
currentfont
(
)
]
321
else
322
local
resources
=
resources
[
k
]
323
local
unicodes
=
resources
and
resources
.
unicodes
or
{
}
324
t
[
k
]
=
unicodes
325
return
unicodes
326
end
327
end
)
328 329
setmetatableindex
(
originals
,
function
(
t
,
k
)
-- always a unicode
330
if
k
=
=
true
then
331
return
originals
[
currentfont
(
)
]
332
else
333
local
resolved
=
{
}
334
setmetatableindex
(
resolved
,
function
(
t
,
name
)
335
local
u
=
unicodes
[
k
]
[
name
]
336
local
d
=
u
and
descriptions
[
k
]
[
u
]
337
local
v
=
d
and
d
.
unicode
or
u
or
0
-- so we return notdef (at least for the moment)
338
t
[
name
]
=
u
339
return
v
340
end
)
341
t
[
k
]
=
resolved
342
return
resolved
343
end
344
end
)
345 346
setmetatableindex
(
modes
,
function
(
t
,
k
)
347
if
k
=
=
true
then
348
return
modes
[
currentfont
(
)
]
349
else
350
local
mode
=
properties
[
k
]
.
mode
or
"
base
"
351
t
[
k
]
=
mode
352
return
mode
353
end
354
end
)
355 356
setmetatableindex
(
variants
,
function
(
t
,
k
)
357
if
k
=
=
true
then
358
return
variants
[
currentfont
(
)
]
359
else
360
local
resources
=
resources
[
k
]
361
if
resources
then
362
local
variants
=
resources
.
variants
363
if
variants
and
next
(
variants
)
then
364
t
[
k
]
=
variants
365
return
variants
366
end
367
end
368
t
[
k
]
=
false
369
return
false
370
end
371
end
)
372 373
if
font
then
374 375
function
font
.
getfont
(
id
)
376
return
identifiers
[
id
]
377
end
378 379
end
380 381
-- font.setfont = currentfont -- bah, no native 'setfont' as name
382