1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
|
if not modules then modules = { } end modules ['font-def'] = {
version = 1.001,
comment = "companion to font-ini.tex",
author = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
copyright = "PRAGMA ADE / ConTeXt Development Team",
license = "see context related readme files"
}
-- check reuse of lmroman1o-regular vs lmr10
--[[ldx--
<p>Here we deal with defining fonts. We do so by intercepting the
default loader that only handles <l n='tfm'/>.</p>
--ldx]]--
fonts = fonts or { }
fonts.define = fonts.define or { }
fonts.tfm = fonts.tfm or { }
fonts.vf = fonts.vf or { }
fonts.used = fonts.used or { }
fonts.tfm.version = 1.01
fonts.tfm.cache = containers.define("fonts", "tfm", fonts.tfm.version, false) -- better in font-tfm
--[[ldx--
<p>Choosing a font by name and specififying its size is only part of the
game. In order to prevent complex commands, <l n='xetex'/> introduced
a method to pass feature information as part of the font name. At the
risk of introducing nasty parsing and compatinility problems, this
syntax was expanded over time.</p>
<p>For the sake of users who have defined fonts using that syntax, we
will support it, but we will provide additional methods as well.
Normally users will not use this direct way, but use a more abstract
interface.</p>
--ldx]]--
--~ name, kind, features = fonts.features.split_xetex("blabla / B : + lnum ; foo = bar ; - whatever ; whow ; + hans ; test = yes")
fonts.define.method = 3 -- 1: tfm 2: tfm and if not then afm 3: afm and if not then tfm
fonts.define.auto_afm = true
fonts.define.auto_otf = true
fonts.define.specify = fonts.define.specify or { }
fonts.define.splitsymbols = ""
fonts.define.methods = fonts.define.methods or { }
fonts.tfm.fonts = fonts.tfm.fonts or { }
fonts.tfm.readers = fonts.tfm.readers or { }
fonts.tfm.internalized = fonts.tfm.internalized or { } -- internal tex numbers
fonts.tfm.id = fonts.tfm.id or { } -- font data, maybe use just fonts.ids (faster lookup)
fonts.tfm.readers.sequence = { 'otf', 'ttf', 'afm', 'tfm' }
--[[ldx--
<p>We hardly gain anything when we cache the final (pre scaled)
<l n='tfm'/> table. But it can be handy for debugging.</p>
--ldx]]--
fonts.version = 1.05
fonts.cache = containers.define("fonts", "def", fonts.version, false)
--[[ldx--
<p>We can prefix a font specification by <type>name:</type> or
<type>file:</type>. The first case will result in a lookup in the
synonym table.</p>
<typing>
[ name: | file: ] identifier [ separator [ specification ] ]
</typing>
<p>The following function split the font specification into components
and prepares a table that will move along as we proceed.</p>
--ldx]]--
function fonts.define.analyze(name, size, id)
name = name or 'unknown'
local specification = name
local lookup, rest = specification:match("^(.-):(.+)$")
local sub = ""
if lookup == 'file' or lookup == 'name' then
name = rest
else
lookup = 'file'
end
local font, method, detail = name:match("^(.-)(["..fonts.define.splitsymbols.."])(.+)$")
if method and detail then
name = font
else
method, detail = "", ""
end
local mainfont, subfont = name:match("^(.*-)(%(.*-)(%)$")
if mainfont and subfont then
name, sub = mainfont, subfont
end
size = size or (65536*10)
return {
lookup = lookup, -- forced type
specification = specification, -- full specification
size = size, -- size in scaled points or -1000*n
name = name, -- font or filename
sub = sub, -- subfont (eg in ttc)
method = method, -- specification method
detail = detail, -- specification
resolved = "", -- resolved font name
forced = "", -- forced loader
id = id, -- font id
features = { }, -- preprocessed features
-- hash = nil
-- filename = nil,
-- encoding = nil,
-- format = nil,
}
end
function fonts.define.register_split(symbol,action)
fonts.define.splitsymbols = fonts.define.splitsymbols .. "%" .. symbol
fonts.define.specify[symbol] = action
end
--[[ldx--
<p>A unique hash value is generated by:</p>
--ldx]]--
function fonts.tfm.hash_features(specification)
if specification.features then
local t = { }
local normal = specification.features.normal
if not table.is_empty(normal) then
for _, v in pairs(table.sortedkeys(normal)) do
if v ~= "number" then
t[#t+1] = v .. '=' .. tostring(normal[v])
end
end
end
local vtf = specification.features.vtf
if not table.is_empty(vtf) then
for _, v in pairs(table.sortedkeys(vtf)) do
t[#t+1] = v .. '=' .. tostring(vtf[v])
end
end
if next(t) then
return table.concat(t,"+")
end
end
return "unknown"
end
--~ function fonts.tfm.hash_instance(specification)
--~ if not specification.hash then
--~ specification.hash = fonts.tfm.hash_features(specification)
--~ end
--~ return specification.hash .. ' @ ' .. tostring(specification.size)
--~ end
fonts.designsizes = { }
--[[ldx--
<p>In principle we can share tfm tables when we are in node for a font, but then
we need to define a font switch as an id/attr switch which is no fun, so in that
case users can best use dynamic features ... so, we will not use that speedup. Okay,
when we get rid of base mode we can optimize even further by sharing, but then we
loose our testcases for <l n='luatex'/>.</p>
--ldx]]--
function fonts.tfm.hash_instance(specification,force)
local hash, size = specification.hash, specification.size
if force or not hash then
hash = fonts.tfm.hash_features(specification)
specification.hash = hash
end
if size < 1000 and fonts.designsizes[hash] then
size = fonts.tfm.scaled(size, fonts.designsizes[hash])
specification.size = size
end
return hash .. ' @ ' .. tostring(size)
end
--[[ldx--
<p>We can resolve the filename using the next function:</p>
--ldx]]--
function fonts.define.resolve(specification)
if specification.lookup == 'name' then
specification.resolved, specification.sub = fonts.names.resolve(specification.name,specification.sub)
if specification.resolved then
specification.forced = file.extname(specification.resolved)
specification.name = file.removesuffix(specification.resolved)
end
elseif specification.lookup == 'file' then
specification.forced = file.extname(specification.name)
specification.name = file.removesuffix(specification.name)
end
if specification.forced == "" then
specification.forced = nil
end
specification.hash = specification.name .. ' @ ' .. fonts.tfm.hash_features(specification)
if specification.sub and specification.sub ~= "" then
specification.hash = specification.sub .. ' @ ' .. specification.hash
end
return specification
end
--[[ldx--
<p>The main read function either uses a forced reader (as determined by
a lookup) or tries to resolve the name using the list of readers.</p>
<p>We need to cache when possible. We do cache raw tfm data (from <l
n='tfm'/>, <l n='afm'/> or <l n='otf'/>). After that we can cache based
on specificstion (name) and size, that is, <l n='tex'/> only needs a number
for an already loaded fonts. However, it may make sense to cache fonts
before they're scaled as well (store <l n='tfm'/>'s with applied methods
and features). However, there may be a relation between the size and
features (esp in virtual fonts) so let's not do that now.</p>
<p>Watch out, here we do load a font, but we don't prepare the
specification yet.</p>
--ldx]]--
function fonts.tfm.read(specification)
garbagecollector.push()
input.starttiming(fonts)
local hash = fonts.tfm.hash_instance(specification)
local tfmtable = fonts.tfm.fonts[hash] -- hashes by size !
if not tfmtable then
if specification.forced and specification.forced ~= "" then
tfmtable = fonts.tfm.readers[specification.forced](specification)
if not tfmtable then
logs.error("define font",string.format("forced type %s of %s not found",specification.forced,specification.name))
end
else
for _, reader in ipairs(fonts.tfm.readers.sequence) do
if fonts.tfm.readers[reader] then -- not really needed
if fonts.trace then
logs.report("define font",string.format("trying type %s for %s with file %s",reader,specification.name,specification.filename or "unknown"))
end
tfmtable = fonts.tfm.readers[reader](specification)
if tfmtable then break end
end
end
end
if tfmtable then
if tfmtable.filename and fonts.dontembed[tfmtable.filename] then
tfmtable.embedding = "no"
else
tfmtable.embedding = "subset"
end
fonts.tfm.fonts[hash] = tfmtable
fonts.designsizes[specification.hash] = tfmtable.designsize -- we only know this for sure after loading once
--~ tfmtable.mode = specification.features.normal.mode or "base"
end
end
input.stoptiming(fonts)
garbagecollector.pop()
if not tfmtable then
logs.error("define font",string.format("font with name %s is not found",specification.name))
end
return tfmtable
end
--[[ldx--
<p>For virtual fonts we need a slightly different approach:</p>
--ldx]]--
function fonts.tfm.read_and_define(name,size) -- no id
local specification = fonts.define.analyze(name,size,nil)
if specification.method and fonts.define.specify[specification.method] then
specification = fonts.define.specify[specification.method](specification)
end
specification = fonts.define.resolve(specification)
local hash = fonts.tfm.hash_instance(specification)
local id = fonts.tfm.internalized[hash]
if not id then
local fontdata = fonts.tfm.read(specification)
if fontdata then
if not fonts.tfm.internalized[hash] then
id = font.define(fontdata)
fonts.tfm.id[id] = fontdata
fonts.tfm.internalized[hash] = id
if fonts.trace then
logs.report("define font", string.format("loading at 1 id %s, hash: %s",id,hash))
end
else
id = fonts.tfm.internalized[hash]
end
else
id = 0 -- signal
end
end
return fonts.tfm.id[id], id
end
--[[ldx--
<p>A naive callback could be the following:</p>
<code>
callback.register('define_font', function(name,size,id)
return fonts.define.read(fonts.define.resolve(fonts.define.analyze(name,size,id)))
end)
</code>
--ldx]]--
--[[ldx--
<p>Next follow the readers. This code was written while <l n='luatex'/>
evolved. Each one has its own way of dealing with its format.</p>
--ldx]]--
function fonts.tfm.readers.opentype(specification,suffix,what)
if fonts.define.auto_otf then
local fullname, tfmtable = nil, nil
fullname = input.findbinfile(texmf.instance,specification.name,suffix) or ""
if fullname == "" then
local fb = fonts.names.old_to_new[specification.name]
if fb then
fullname = input.findbinfile(texmf.instance,fb,suffix) or ""
end
end
if fullname == "" then
local fb = fonts.names.new_to_old[specification.name]
if fb then
fullname = input.findbinfile(texmf.instance,fb,suffix) or ""
end
end
if fullname ~= "" then
specification.filename, specification.format = fullname, what -- hm, so we do set the filename, then
tfmtable = fonts.tfm.read_from_open_type(specification) -- we need to do it for all matches / todo
end
return tfmtable
else
return nil
end
end
function fonts.tfm.readers.otf(specification) return fonts.tfm.readers.opentype(specification,"otf","opentype") end
function fonts.tfm.readers.ttf(specification) return fonts.tfm.readers.opentype(specification,"ttf","truetype") end
function fonts.tfm.readers.ttc(specification) return fonts.tfm.readers.opentype(specification,"ttf","truetype") end -- !!
function fonts.tfm.readers.afm(specification,method)
local fullname, tfmtable = nil, nil
method = method or fonts.define.method
if method == 2 then
fullname = input.findbinfile(texmf.instance,specification.name,"ofm") or ""
if fullname == "" then
tfmtable = fonts.tfm.read_from_afm(specification)
else -- redundant
specification.filename = fullname
tfmtable = fonts.tfm.read_from_tfm(specification)
end
elseif method == 3 then -- maybe also findbinfile here
if fonts.define.auto_afm then
tfmtable = fonts.tfm.read_from_afm(specification)
end
elseif method == 4 then -- maybe also findbinfile here
tfmtable = fonts.tfm.read_from_afm(specification)
end
return tfmtable
end
function fonts.tfm.readers.tfm(specification)
local fullname, tfmtable = nil, nil
tfmtable = fonts.tfm.read_from_tfm(specification)
return tfmtable
end
--[[ldx--
<p>So far we haven't really dealt with features (or whatever we want
to pass along with the font definition. We distinguish the following
situations:</p>
situations:</p>
<code>
name:xetex like specs
name@virtual font spec
name*context specification
</code>
<p>Of course one can always define more.</p>
--ldx]]--
function fonts.define.specify.predefined(specification)
if specification.detail ~= "" and fonts.define.methods[specification.detail] then
specification.features.vtf = { preset = specification.detail }
end
return specification
end
fonts.define.register_split("@", fonts.define.specify.predefined)
function fonts.define.specify.colonized(specification) -- xetex mode
local list = { }
if specification.detail and specification.detail ~= "" then
local expanded_features = { }
local function expand(features)
for _,v in pairs(features:split(";")) do
expanded_features[#expanded_features+1] = v
end
end
expand(specification.detail)
for _,v in pairs(expanded_features) do
local a, b = v:match("^%s*(%S+)%s*=%s*(%S+)%s*$")
if a and b then
list[a] = b:is_boolean()
if type(list[a]) == "nil" then
list[a] = b
end
else
local a, b = v:match("^%s*([%+%-]?)%s*(%S+)%s*$")
if a and b then
list[b] = a ~= "-"
end
end
end
end
specification.features.normal = list
return specification
end
function fonts.tfm.make(specification)
local fvm = fonts.define.methods[specification.features.vtf.preset]
if fvm then
return fvm(specification)
else
return nil
end
end
fonts.define.register_split(":", fonts.define.specify.colonized)
fonts.define.specify.context_setups = fonts.define.specify.context_setups or { }
fonts.define.specify.context_numbers = fonts.define.specify.context_numbers or { }
fonts.define.specify.synonyms = fonts.define.specify.synonyms or { }
input.storage.register(false,"fonts/setups" , fonts.define.specify.context_setups , "fonts.define.specify.context_setups" )
input.storage.register(false,"fonts/numbers", fonts.define.specify.context_numbers, "fonts.define.specify.context_numbers")
--~ local t = aux.settings_to_hash(features)
--~ for k,v in pairs(t) do
--~ k = synonyms[k] or k
--~ t[k] = v:is_boolean()
--~ if type(t[k]) == "nil" then
--~ t[k] = v
--~ end
--~ end
fonts.triggers = fonts.triggers or { }
function fonts.define.specify.preset_context(name,parent,features)
if features == "" then
if parent:find("=") then
features = parent
parent = ""
end
end
local fds = fonts.define.specify
local setups, numbers, synonyms = fds.context_setups, fds.context_numbers, fds.synonyms
local number = (setups[name] and setups[name].number) or 0
local t = (features == "" and { }) or fonts.otf.meanings.normalize(aux.settings_to_hash(features))
-- todo: synonyms, and not otf bound
if parent ~= "" then
for p in parent:gmatch("[^, ]+") do
local s = setups[p]
if s then
for k,v in pairs(s) do
if t[k] == nil then
t[k] = v
end
end
end
end
end
-- these are auto set so in order to prevent redundant definitions
-- we need to preset them (we hash the features and adding a default
-- setting during initialization may result in a different hash)
local default = fonts.otf.features.default
for k,v in pairs(fonts.triggers) do
if type(t[v]) == "nil" then
local vv = default[v]
if vv then t[v] = vv end
end
end
-- sparse 'm so that we get a better hash and less test (experimental
-- optimization)
local tt = { }
for k,v in pairs(t) do
if v then tt[k] = v end
end
-- needed for dynamic features
if number == 0 then
numbers[#numbers+1] = name
tt.number = #numbers
else
tt.number = number
end
setups[name] = tt
end
--~ function fonts.define.specify.context_number(name)
--~ local s = fonts.define.specify.context_setups[name]
--~ return (s and s.number) or -1
--~ end
do
-- here we clone features according to languages
local default = 0
local setups = fonts.define.specify.context_setups
local numbers = fonts.define.specify.context_numbers
function fonts.define.specify.context_number(name)
local t = setups[name]
if not t then
return default
elseif t.auto then
local lng = tonumber(tex.language)
local tag = name .. ":" .. lng
local s = setups[tag]
if s then
return s.number or default
else
local script, language = languages.association(lng)
if t.script ~= script or t.language ~= language then
local s = table.fastcopy(t)
local n = #numbers + 1
setups[tag] = s
numbers[n] = tag
s.number = n
s.script = script
s.language = language
return n
else
setups[tag] = t
return t.number or default
end
end
else
return t.number or default
end
end
end
function fonts.define.specify.context_tostring(name,kind,separator,yes,no,strict,omit)
return aux.hash_to_string(table.merged(fonts[kind].features.default or {},fonts.define.specify.context_setups[name] or {}),separator,yes,no,strict,omit)
end
function fonts.define.specify.split_context(features)
if fonts.define.specify.context_setups[features] then
return fonts.define.specify.context_setups[features]
else -- ? ? ?
return fonts.define.specify.preset_context("***",features)
end
end
function fonts.define.specify.starred(features)
if features.detail and features.detail ~= "" then
features.features.normal = fonts.define.specify.split_context(features.detail)
else
features.features.normal = { }
end
return features
end
fonts.define.register_split('*',fonts.define.specify.starred)
--[[ldx--
<p>We need to check for default features. For this we provide
a helper function.</p>
--ldx]]--
function fonts.define.check(features,defaults) -- nb adapts features !
local done = false
if table.is_empty(features) then
features, done = table.fastcopy(defaults), true
else
for k,v in pairs(defaults) do
if features[k] == nil then
features[k], done = v, true
end
end
end
return features, done -- done signals a change
end
--[[ldx--
<p>So far the specifyers. Now comes the real definer. Here we cache
based on id's. Here we also intercept the virtual font handler. Since
it evolved stepwise I may rewrite this bit (combine code).</p>
In the previously defined reader (the one resulting in a <l n='tfm'/>
table) we cached the (scaled) instances. Here we cache them again, but
this time based on id. We could combine this in one cache but this does
not gain much. By the way, passing id's back to in the callback was
introduced later in the development.</p>
--ldx]]--
function fonts.define.read(name,size,id)
local specification = fonts.define.analyze(name,size,id)
if specification.method and fonts.define.specify[specification.method] then
specification = fonts.define.specify[specification.method](specification)
end
specification = fonts.define.resolve(specification)
local hash = fonts.tfm.hash_instance(specification)
if true then
--~ local fontdata = containers.read(fonts.cache(),hash) -- for tracing purposes
end
local fontdata = fonts.tfm.internalized[hash] -- id
if not fontdata then
if specification.features.vtf and specification.features.vtf.preset then
fontdata = fonts.tfm.make(specification)
else
fontdata = fonts.tfm.read(specification)
if fontdata then
fonts.tfm.check_virtual_id(fontdata)
end
end
if true then
--~ fontdata = containers.write(fonts.cache(),hash,fontdata) -- for tracing purposes
end
if not fonts.tfm.internalized[hash] then
fonts.tfm.id[id] = fontdata
fonts.tfm.internalized[hash] = id
if fonts.trace then
logs.report("define font", string.format("loading at 2 id %s, hash: %s",id,hash))
end
else
fontdata = fonts.tfm.internalized[hash]
end
end
if not fontdata then
logs.error("define font", string.format("unknown font %s, loading aborted",specification.name))
elseif fonts.trace and type(fontdata) == "table" then
logs.report("define font",string.format("using %s font with id %s, n:%s s:%s b:%s e:%s p:%s f:%s",
fontdata.type or "unknown",
id or "?",
fontdata.name or "?",
fontdata.size or "default",
fontdata.encodingbytes or "?",
fontdata.encodingname or "unicode",
fontdata.fullname or "?",
file.basename(fontdata.filename or "?")))
end
return fontdata
end
--~ table.insert(fonts.tfm.readers.sequence,1,'vtf')
--~ function fonts.tfm.readers.vtf(specification)
--~ if specification.features.vtf and specification.features.vtf.preset then
--~ return fonts.tfm.make(specification)
--~ else
--~ return nil
--~ end
--~ end
function fonts.vf.find(name)
name = file.removesuffix(file.basename(name))
if fonts.tfm.resolve_vf then
local format = fonts.logger.format(name)
if format == 'tfm' or format == 'ofm' then
if fonts.trace then
logs.report("define font",string.format("locating vf for %s",name))
end
return input.findbinfile(texmf.instance,name,"ovf")
else
if fonts.trace then
logs.report("define font",string.format("vf for %s is already taken care of",name))
end
return nil -- ""
end
else
if fonts.trace then
logs.report("define font",string.format("locating vf for %s",name))
end
return input.findbinfile(texmf.instance,name,"ovf")
end
end
--[[ldx--
<p>We overload both the <l n='tfm'/> and <l n='vf'/> readers.</p>
--ldx]]--
callback.register('define_font' , fonts.define.read)
callback.register('find_vf_file', fonts.vf.find ) -- not that relevant any more
|