This repository has been archived by the owner on Jan 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
otfl-font-otf.lua
2080 lines (1958 loc) · 77.7 KB
/
otfl-font-otf.lua
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
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
if not modules then modules = { } end modules ['font-otf'] = {
version = 1.001,
comment = "companion to font-ini.mkiv",
author = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
copyright = "PRAGMA ADE / ConTeXt Development Team",
license = "see context related readme files"
}
-- langs -> languages enz
-- anchor_classes vs kernclasses
-- modification/creationtime in subfont is runtime dus zinloos
-- to_table -> totable
-- ascent descent
-- more checking against low level calls of functions
local utf = unicode.utf8
local utfbyte = utf.byte
local format, gmatch, gsub, find, match, lower, strip = string.format, string.gmatch, string.gsub, string.find, string.match, string.lower, string.strip
local type, next, tonumber, tostring = type, next, tonumber, tostring
local abs = math.abs
local getn = table.getn
local lpegmatch = lpeg.match
local reversed, concat, remove = table.reversed, table.concat, table.remove
local ioflush = io.flush
local fastcopy, tohash, derivetable = table.fastcopy, table.tohash, table.derive
local allocate = utilities.storage.allocate
local registertracker = trackers.register
local registerdirective = directives.register
local starttiming = statistics.starttiming
local stoptiming = statistics.stoptiming
local elapsedtime = statistics.elapsedtime
local findbinfile = resolvers.findbinfile
local trace_private = false registertracker("otf.private", function(v) trace_private = v end)
local trace_loading = false registertracker("otf.loading", function(v) trace_loading = v end)
local trace_features = false registertracker("otf.features", function(v) trace_features = v end)
local trace_dynamics = false registertracker("otf.dynamics", function(v) trace_dynamics = v end)
local trace_sequences = false registertracker("otf.sequences", function(v) trace_sequences = v end)
local trace_markwidth = false registertracker("otf.markwidth", function(v) trace_markwidth = v end)
local trace_defining = false registertracker("fonts.defining", function(v) trace_defining = v end)
local report_otf = logs.reporter("fonts","otf loading")
local fonts = fonts
local otf = fonts.handlers.otf
otf.glists = { "gsub", "gpos" }
otf.version = 2.737 -- beware: also sync font-mis.lua
otf.cache = containers.define("fonts", "otf", otf.version, true)
local fontdata = fonts.hashes.identifiers
local chardata = characters and characters.data -- not used
local otffeatures = fonts.constructors.newfeatures("otf")
local registerotffeature = otffeatures.register
local enhancers = allocate()
otf.enhancers = enhancers
local patches = { }
enhancers.patches = patches
local definers = fonts.definers
local readers = fonts.readers
local constructors = fonts.constructors
local forceload = false
local cleanup = 0 -- mk: 0=885M 1=765M 2=735M (regular run 730M)
local usemetatables = false -- .4 slower on mk but 30 M less mem so we might change the default -- will be directive
local packdata = true
local syncspace = true
local forcenotdef = false
local wildcard = "*"
local default = "dflt"
local fontloaderfields = fontloader.fields
local mainfields = nil
local glyphfields = nil -- not used yet
registerdirective("fonts.otf.loader.cleanup", function(v) cleanup = tonumber(v) or (v and 1) or 0 end)
registerdirective("fonts.otf.loader.force", function(v) forceload = v end)
registerdirective("fonts.otf.loader.usemetatables", function(v) usemetatables = v end)
registerdirective("fonts.otf.loader.pack", function(v) packdata = v end)
registerdirective("fonts.otf.loader.syncspace", function(v) syncspace = v end)
registerdirective("fonts.otf.loader.forcenotdef", function(v) forcenotdef = v end)
local function load_featurefile(raw,featurefile)
if featurefile and featurefile ~= "" then
if trace_loading then
report_otf("featurefile: %s", featurefile)
end
fontloader.apply_featurefile(raw, featurefile)
end
end
local function showfeatureorder(rawdata,filename)
local sequences = rawdata.resources.sequences
if sequences and #sequences > 0 then
if trace_loading then
report_otf("font %s has %s sequences",filename,#sequences)
report_otf(" ")
end
for nos=1,#sequences do
local sequence = sequences[nos]
local typ = sequence.type or "no-type"
local name = sequence.name or "no-name"
local subtables = sequence.subtables or { "no-subtables" }
local features = sequence.features
if trace_loading then
report_otf("%3i %-15s %-20s [%s]",nos,name,typ,concat(subtables,","))
end
if features then
for feature, scripts in next, features do
local tt = { }
if type(scripts) == "table" then
for script, languages in next, scripts do
local ttt = { }
for language, _ in next, languages do
ttt[#ttt+1] = language
end
tt[#tt+1] = format("[%s: %s]",script,concat(ttt," "))
end
if trace_loading then
report_otf(" %s: %s",feature,concat(tt," "))
end
else
if trace_loading then
report_otf(" %s: %s",feature,tostring(scripts))
end
end
end
end
end
if trace_loading then
report_otf("\n")
end
elseif trace_loading then
report_otf("font %s has no sequences",filename)
end
end
--[[ldx--
<p>We start with a lot of tables and related functions.</p>
--ldx]]--
local valid_fields = table.tohash {
-- "anchor_classes",
"ascent",
-- "cache_version",
"cidinfo",
"copyright",
-- "creationtime",
"descent",
"design_range_bottom",
"design_range_top",
"design_size",
"encodingchanged",
"extrema_bound",
"familyname",
"fontname",
"fontname",
"fontstyle_id",
"fontstyle_name",
"fullname",
-- "glyphs",
"hasvmetrics",
-- "head_optimized_for_cleartype",
"horiz_base",
"issans",
"isserif",
"italicangle",
-- "kerns",
-- "lookups",
"macstyle",
-- "modificationtime",
"onlybitmaps",
"origname",
"os2_version",
"pfminfo",
-- "private",
"serifcheck",
"sfd_version",
-- "size",
"strokedfont",
"strokewidth",
-- "subfonts",
"table_version",
-- "tables",
-- "ttf_tab_saved",
"ttf_tables",
"uni_interp",
"uniqueid",
"units_per_em",
"upos",
"use_typo_metrics",
"uwidth",
-- "validation_state",
"version",
"vert_base",
"weight",
"weight_width_slope_only",
-- "xuid",
}
local ordered_enhancers = {
"prepare tables",
"prepare glyphs",
"prepare lookups",
"analyze glyphs",
"analyze math",
"prepare tounicode", -- maybe merge with prepare
"reorganize lookups",
"reorganize mark classes",
"reorganize anchor classes",
"reorganize glyph kerns",
"reorganize glyph lookups",
"reorganize glyph anchors",
"merge kern classes",
"reorganize features",
"reorganize subtables",
"check glyphs",
"check metadata",
"check extra features", -- after metadata
"add duplicates",
"check encoding",
"cleanup tables",
}
--[[ldx--
<p>Here we go.</p>
--ldx]]--
local actions = allocate()
local before = allocate()
local after = allocate()
patches.before = before
patches.after = after
local function enhance(name,data,filename,raw)
local enhancer = actions[name]
if enhancer then
if trace_loading then
report_otf("enhance: %s (%s)",name,filename)
ioflush()
end
enhancer(data,filename,raw)
elseif trace_loading then
-- report_otf("enhance: %s is undefined",name)
end
end
function enhancers.apply(data,filename,raw)
local basename = file.basename(lower(filename))
if trace_loading then
report_otf("start enhancing: %s",filename)
end
ioflush() -- we want instant messages
for e=1,#ordered_enhancers do
local enhancer = ordered_enhancers[e]
local b = before[enhancer]
if b then
for pattern, action in next, b do
if find(basename,pattern) then
action(data,filename,raw)
end
end
end
enhance(enhancer,data,filename,raw)
local a = after[enhancer]
if a then
for pattern, action in next, a do
if find(basename,pattern) then
action(data,filename,raw)
end
end
end
ioflush() -- we want instant messages
end
if trace_loading then
report_otf("stop enhancing")
end
ioflush() -- we want instant messages
end
-- patches.register("before","migrate metadata","cambria",function() end)
function patches.register(what,where,pattern,action)
local pw = patches[what]
if pw then
local ww = pw[where]
if ww then
ww[pattern] = action
else
pw[where] = { [pattern] = action}
end
end
end
function patches.report(fmt,...)
if trace_loading then
report_otf("patching: " ..fmt,...)
end
end
function enhancers.register(what,action) -- only already registered can be overloaded
actions[what] = action
end
function otf.load(filename,format,sub,featurefile)
local name = file.basename(file.removesuffix(filename))
local attr = lfs.attributes(filename)
local size = attr and attr.size or 0
local time = attr and attr.modification or 0
if featurefile then
name = name .. "@" .. file.removesuffix(file.basename(featurefile))
end
if sub == "" then
sub = false
end
local hash = name
if sub then
hash = hash .. "-" .. sub
end
hash = containers.cleanname(hash)
local featurefiles
if featurefile then
featurefiles = { }
for s in gmatch(featurefile,"[^,]+") do
local name = resolvers.findfile(file.addsuffix(s,'fea'),'fea') or ""
if name == "" then
report_otf("loading: no featurefile '%s'",s)
else
local attr = lfs.attributes(name)
featurefiles[#featurefiles+1] = {
name = name,
size = attr and attr.size or 0,
time = attr and attr.modification or 0,
}
end
end
if #featurefiles == 0 then
featurefiles = nil
end
end
local data = containers.read(otf.cache,hash)
local reload = not data or data.size ~= size or data.time ~= time
if forceload then
report_otf("loading: forced reload due to hard coded flag")
reload = true
end
if not reload then
local featuredata = data.featuredata
if featurefiles then
if not featuredata or #featuredata ~= #featurefiles then
reload = true
else
for i=1,#featurefiles do
local fi, fd = featurefiles[i], featuredata[i]
if fi.name ~= fd.name or fi.size ~= fd.size or fi.time ~= fd.time then
reload = true
break
end
end
end
elseif featuredata then
reload = true
end
if reload then
report_otf("loading: forced reload due to changed featurefile specification: %s",featurefile or "--")
end
end
if reload then
report_otf("loading: %s (hash: %s)",filename,hash)
local fontdata, messages
if sub then
fontdata, messages = fontloader.open(filename,sub)
else
fontdata, messages = fontloader.open(filename)
end
if fontdata then
mainfields = mainfields or (fontloaderfields and fontloaderfields(fontdata))
end
if trace_loading and messages and #messages > 0 then
if type(messages) == "string" then
report_otf("warning: %s",messages)
else
for m=1,#messages do
report_otf("warning: %s",tostring(messages[m]))
end
end
else
report_otf("font loaded okay")
end
if fontdata then
if featurefiles then
for i=1,#featurefiles do
load_featurefile(fontdata,featurefiles[i].name)
end
end
local unicodes = {
-- names to unicodes
}
local splitter = lpeg.splitter(" ",unicodes)
data = {
size = size,
time = time,
format = format,
featuredata = featurefiles,
resources = {
filename = resolvers.unresolve(filename), -- no shortcut
version = otf.version,
creator = "context mkiv",
unicodes = unicodes,
indices = {
-- index to unicodes
},
duplicates = {
-- alternative unicodes
},
variants = {
-- alternative unicodes (variants)
},
lookuptypes = {
},
},
metadata = {
-- raw metadata, not to be used
},
properties = {
-- normalized metadata
},
descriptions = {
},
goodies = {
},
helpers = {
tounicodelist = splitter,
tounicodetable = lpeg.Ct(splitter),
},
}
starttiming(data)
report_otf("file size: %s", size)
enhancers.apply(data,filename,fontdata)
if packdata then
if cleanup > 0 then
collectgarbage("collect")
--~ lua.collectgarbage()
end
enhance("pack",data,filename,nil)
end
report_otf("saving in cache: %s",filename)
data = containers.write(otf.cache, hash, data)
if cleanup > 1 then
collectgarbage("collect")
--~ lua.collectgarbage()
end
stoptiming(data)
if elapsedtime then -- not in generic
report_otf("preprocessing and caching took %s seconds",elapsedtime(data))
end
fontloader.close(fontdata) -- free memory
if cleanup > 3 then
collectgarbage("collect")
--~ lua.collectgarbage()
end
data = containers.read(otf.cache, hash) -- this frees the old table and load the sparse one
if cleanup > 2 then
collectgarbage("collect")
--~ lua.collectgarbage()
end
else
data = nil
report_otf("loading failed (file read error)")
end
end
if data then
if trace_defining then
report_otf("loading from cache: %s",hash)
end
enhance("unpack",data,filename,nil,false)
enhance("add dimensions",data,filename,nil,false)
if trace_sequences then
showfeatureorder(data,filename)
end
end
return data
end
local mt = {
__index = function(t,k) -- maybe set it
if k == "height" then
local ht = t.boundingbox[4]
return ht < 0 and 0 or ht
elseif k == "depth" then
local dp = -t.boundingbox[2]
return dp < 0 and 0 or dp
elseif k == "width" then
return 0
elseif k == "name" then -- or maybe uni*
return forcenotdef and ".notdef"
end
end
}
actions["prepare tables"] = function(data,filename,raw)
data.properties.hasitalics = false
end
actions["add dimensions"] = function(data,filename)
-- todo: forget about the width if it's the defaultwidth (saves mem)
-- we could also build the marks hash here (instead of storing it)
if data then
local descriptions = data.descriptions
local resources = data.resources
local defaultwidth = resources.defaultwidth or 0
local defaultheight = resources.defaultheight or 0
local defaultdepth = resources.defaultdepth or 0
if usemetatables then
for _, d in next, descriptions do
local wd = d.width
if not wd then
d.width = defaultwidth
elseif trace_markwidth and wd ~= 0 and d.class == "mark" then
report_otf("mark with width %s (%s) in %s",wd,d.name or "<noname>",file.basename(filename))
-- d.width = -wd
end
setmetatable(d,mt)
end
else
for _, d in next, descriptions do
local bb, wd = d.boundingbox, d.width
if not wd then
d.width = defaultwidth
elseif trace_markwidth and wd ~= 0 and d.class == "mark" then
report_otf("mark with width %s (%s) in %s",wd,d.name or "<noname>",file.basename(filename))
-- d.width = -wd
end
-- if forcenotdef and not d.name then
-- d.name = ".notdef"
-- end
if bb then
local ht, dp = bb[4], -bb[2]
if ht == 0 or ht < 0 then
-- not set
else
d.height = ht
end
if dp == 0 or dp < 0 then
-- not set
else
d.depth = dp
end
end
end
end
end
end
local function somecopy(old) -- fast one
if old then
local new = { }
if type(old) == "table" then
for k, v in next, old do
if k == "glyphs" then
-- skip
elseif type(v) == "table" then
new[k] = somecopy(v)
else
new[k] = v
end
end
else
for i=1,#mainfields do
local k = mainfields[i]
local v = old[k]
if k == "glyphs" then
-- skip
elseif type(v) == "table" then
new[k] = somecopy(v)
else
new[k] = v
end
end
end
return new
else
return { }
end
end
-- not setting hasitalics and class (when nil) during
-- table cronstruction can save some mem
actions["prepare glyphs"] = function(data,filename,raw)
local rawglyphs = raw.glyphs
local rawsubfonts = raw.subfonts
local rawcidinfo = raw.cidinfo
local criterium = constructors.privateoffset
local private = criterium
local resources = data.resources
local metadata = data.metadata
local properties = data.properties
local descriptions = data.descriptions
local unicodes = resources.unicodes -- name to unicode
local indices = resources.indices -- index to unicode
local duplicates = resources.duplicates
local variants = resources.variants
if rawsubfonts then
metadata.subfonts = { }
properties.cidinfo = rawcidinfo
if rawcidinfo.registry then
local cidmap = fonts.cid.getmap(rawcidinfo)
if cidmap then
rawcidinfo.usedname = cidmap.usedname
local nofnames, nofunicodes = 0, 0
local cidunicodes, cidnames = cidmap.unicodes, cidmap.names
for cidindex=1,#rawsubfonts do
local subfont = rawsubfonts[cidindex]
local cidglyphs = subfont.glyphs
metadata.subfonts[cidindex] = somecopy(subfont)
for index=0,subfont.glyphcnt-1 do -- we could take the previous glyphcnt instead of 0
local glyph = cidglyphs[index]
if glyph then
local unicode = glyph.unicode
local name = glyph.name or cidnames[index]
if not unicode or unicode == -1 or unicode >= criterium then
unicode = cidunicodes[index]
end
if not unicode or unicode == -1 or unicode >= criterium then
if not name then
name = format("u%06X",private)
end
unicode = private
unicodes[name] = private
if trace_private then
report_otf("enhance: glyph %s at index 0x%04X is moved to private unicode slot U+%05X",name,index,private)
end
private = private + 1
nofnames = nofnames + 1
else
if not name then
name = format("u%06X",unicode)
end
unicodes[name] = unicode
nofunicodes = nofunicodes + 1
end
indices[index] = unicode -- each index is unique (at least now)
local description = {
-- width = glyph.width,
boundingbox = glyph.boundingbox,
name = glyph.name or name or "unknown", -- uniXXXX
cidindex = cidindex,
index = index,
glyph = glyph,
}
descriptions[unicode] = description
else
-- report_otf("potential problem: glyph 0x%04X is used but empty",index)
end
end
end
if trace_loading then
report_otf("cid font remapped, %s unicode points, %s symbolic names, %s glyphs",nofunicodes, nofnames, nofunicodes+nofnames)
end
elseif trace_loading then
report_otf("unable to remap cid font, missing cid file for %s",filename)
end
elseif trace_loading then
report_otf("font %s has no glyphs",filename)
end
else
for index=0,raw.glyphcnt-1 do -- not raw.glyphmax-1 (as that will crash)
local glyph = rawglyphs[index]
if glyph then
local unicode = glyph.unicode
local name = glyph.name
if not unicode or unicode == -1 or unicode >= criterium then
unicode = private
unicodes[name] = private
if trace_private then
report_otf("enhance: glyph %s at index 0x%04X is moved to private unicode slot U+%05X",name,index,private)
end
private = private + 1
else
unicodes[name] = unicode
end
indices[index] = unicode
if not name then
name = format("u%06X",unicode)
end
descriptions[unicode] = {
-- width = glyph.width,
boundingbox = glyph.boundingbox,
name = name,
index = index,
glyph = glyph,
}
local altuni = glyph.altuni
if altuni then
local d
for i=1,#altuni do
local a = altuni[i]
local u = a.unicode
local v = a.variant
if v then
local vv = variants[v]
if vv then
vv[u] = unicode
else -- xits-math has some:
vv = { [u] = unicode }
variants[v] = vv
end
elseif d then
d[#d+1] = u
else
d = { u }
end
end
if d then
duplicates[unicode] = d
end
end
else
report_otf("potential problem: glyph 0x%04X is used but empty",index)
end
end
end
resources.private = private
end
-- the next one is still messy but will get better when we have
-- flattened map/enc tables in the font loader
actions["check encoding"] = function(data,filename,raw)
local descriptions = data.descriptions
local resources = data.resources
local properties = data.properties
local unicodes = resources.unicodes -- name to unicode
local indices = resources.indices -- index to unicodes
-- begin of messy (not needed when cidmap)
local mapdata = raw.map or { }
local unicodetoindex = mapdata and mapdata.map or { }
-- local encname = lower(data.enc_name or raw.enc_name or mapdata.enc_name or "")
local encname = lower(data.enc_name or mapdata.enc_name or "")
local criterium = 0xFFFF -- for instance cambria has a lot of mess up there
-- end of messy
if find(encname,"unicode") then -- unicodebmp, unicodefull, ...
if trace_loading then
report_otf("checking embedded unicode map '%s'",encname)
end
for unicode, index in next, unicodetoindex do -- altuni already covers this
if unicode <= criterium and not descriptions[unicode] then
local parent = indices[index] -- why nil?
if parent then
report_otf("weird, unicode U+%05X points to U+%05X with index 0x%04X",unicode,parent,index)
else
report_otf("weird, unicode U+%05X points to nowhere with index 0x%04X",unicode,index)
end
end
end
elseif properties.cidinfo then
report_otf("warning: no unicode map, used cidmap '%s'",properties.cidinfo.usedname or "?")
else
report_otf("warning: non unicode map '%s', only using glyph unicode data",encname or "whatever")
end
if mapdata then
mapdata.map = { } -- clear some memory
end
end
-- for the moment we assume that a fotn with lookups will not use
-- altuni so we stick to kerns only
actions["add duplicates"] = function(data,filename,raw)
local descriptions = data.descriptions
local resources = data.resources
local properties = data.properties
local unicodes = resources.unicodes -- name to unicode
local indices = resources.indices -- index to unicodes
local duplicates = resources.duplicates
for unicode, d in next, duplicates do
for i=1,#d do
local u = d[i]
if not descriptions[u] then
local description = descriptions[unicode]
local duplicate = table.copy(description) -- else packing problem
duplicate.comment = format("copy of U+%05X", unicode)
descriptions[u] = duplicate
local n = 0
for _, description in next, descriptions do
if kerns then
local kerns = description.kerns
for _, k in next, kerns do
local ku = k[unicode]
if ku then
k[u] = ku
n = n + 1
end
end
end
-- todo: lookups etc
end
if trace_loading then
report_otf("duplicating U+%05X to U+%05X with index 0x%04X (%s kerns)",unicode,u,description.index,n)
end
end
end
end
end
-- class : nil base mark ligature component (maybe we don't need it in description)
-- boundingbox: split into ht/dp takes more memory (larger tables and less sharing)
actions["analyze glyphs"] = function(data,filename,raw) -- maybe integrate this in the previous
local descriptions = data.descriptions
local resources = data.resources
local metadata = data.metadata
local properties = data.properties
local hasitalics = false
local widths = { }
local marks = { } -- always present (saves checking)
for unicode, description in next, descriptions do
local glyph = description.glyph
local italic = glyph.italic_correction
if not italic then
-- skip
elseif italic == 0 then
-- skip
else
description.italic = italic
hasitalics = true
end
local width = glyph.width
widths[width] = (widths[width] or 0) + 1
local class = glyph.class
if class then
if class == "mark" then
marks[unicode] = true
end
description.class = class
end
end
-- flag italic
properties.hasitalics = hasitalics
-- flag marks
resources.marks = marks
-- share most common width for cjk fonts
local wd, most = 0, 1
for k,v in next, widths do
if v > most then
wd, most = k, v
end
end
if most > 1000 then -- maybe 500
if trace_loading then
report_otf("most common width: %s (%s times), sharing (cjk font)",wd,most)
end
for unicode, description in next, descriptions do
if description.width == wd then
-- description.width = nil
else
description.width = description.glyph.width
end
end
resources.defaultwidth = wd
else
for unicode, description in next, descriptions do
description.width = description.glyph.width
end
end
end
actions["reorganize mark classes"] = function(data,filename,raw)
local mark_classes = raw.mark_classes
if mark_classes then
local resources = data.resources
local unicodes = resources.unicodes
local markclasses = { }
resources.markclasses = markclasses -- reversed
for name, class in next, mark_classes do
local t = { }
for s in gmatch(class,"[^ ]+") do
t[unicodes[s]] = true
end
markclasses[name] = t
end
end
end
actions["reorganize features"] = function(data,filename,raw) -- combine with other
local features = { }
data.resources.features = features
for k, what in next, otf.glists do
local dw = raw[what]
if dw then
local f = { }
features[what] = f
for i=1,#dw do
local d= dw[i]
local dfeatures = d.features
if dfeatures then
for i=1,#dfeatures do
local df = dfeatures[i]
local tag = strip(lower(df.tag))
local ft = f[tag]
if not ft then
ft = { }
f[tag] = ft
end
local dscripts = df.scripts
for i=1,#dscripts do
local d = dscripts[i]
local languages = d.langs
local script = strip(lower(d.script))
local fts = ft[script] if not fts then fts = {} ft[script] = fts end
for i=1,#languages do
fts[strip(lower(languages[i]))] = true
end
end
end
end
end
end
end
end
actions["reorganize anchor classes"] = function(data,filename,raw)
local resources = data.resources
local anchor_to_lookup = { }
local lookup_to_anchor = { }
resources.anchor_to_lookup = anchor_to_lookup
resources.lookup_to_anchor = lookup_to_anchor
local classes = raw.anchor_classes -- anchor classes not in final table
if classes then
for c=1,#classes do
local class = classes[c]
local anchor = class.name
local lookups = class.lookup
if type(lookups) ~= "table" then
lookups = { lookups }
end
local a = anchor_to_lookup[anchor]
if not a then
a = { }
anchor_to_lookup[anchor] = a
end
for l=1,#lookups do
local lookup = lookups[l]
local l = lookup_to_anchor[lookup]
if l then
l[anchor] = true
else
l = { [anchor] = true }
lookup_to_anchor[lookup] = l
end
a[lookup] = true
end
end
end
end
actions["prepare tounicode"] = function(data,filename,raw)
fonts.mappings.addtounicode(data,filename)
end
local g_directions = {
gsub_contextchain = 1,
gpos_contextchain = 1,
-- gsub_context = 1,
-- gpos_context = 1,