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-otn.lua
2712 lines (2544 loc) · 120 KB
/
otfl-font-otn.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-otn'] = {
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"
}
-- this is still somewhat preliminary and it will get better in due time;
-- much functionality could only be implemented thanks to the husayni font
-- of Idris Samawi Hamid to who we dedicate this module.
-- in retrospect it always looks easy but believe it or not, it took a lot
-- of work to get proper open type support done: buggy fonts, fuzzy specs,
-- special made testfonts, many skype sessions between taco, idris and me,
-- torture tests etc etc ... unfortunately the code does not show how much
-- time it took ...
-- todo:
--
-- kerning is probably not yet ok for latin around dics nodes
-- extension infrastructure (for usage out of context)
-- sorting features according to vendors/renderers
-- alternative loop quitters
-- check cursive and r2l
-- find out where ignore-mark-classes went
-- default features (per language, script)
-- handle positions (we need example fonts)
-- handle gpos_single (we might want an extra width field in glyph nodes because adding kerns might interfere)
-- mark (to mark) code is still not what it should be (too messy but we need some more extreem husayni tests)
--[[ldx--
<p>This module is a bit more split up that I'd like but since we also want to test
with plain <l n='tex'/> it has to be so. This module is part of <l n='context'/>
and discussion about improvements and functionality mostly happens on the
<l n='context'/> mailing list.</p>
<p>The specification of OpenType is kind of vague. Apart from a lack of a proper
free specifications there's also the problem that Microsoft and Adobe
may have their own interpretation of how and in what order to apply features.
In general the Microsoft website has more detailed specifications and is a
better reference. There is also some information in the FontForge help files.</p>
<p>Because there is so much possible, fonts might contain bugs and/or be made to
work with certain rederers. These may evolve over time which may have the side
effect that suddenly fonts behave differently.</p>
<p>After a lot of experiments (mostly by Taco, me and Idris) we're now at yet another
implementation. Of course all errors are mine and of course the code can be
improved. There are quite some optimizations going on here and processing speed
is currently acceptable. Not all functions are implemented yet, often because I
lack the fonts for testing. Many scripts are not yet supported either, but I will
look into them as soon as <l n='context'/> users ask for it.</p>
<p>Because there are different interpretations possible, I will extend the code
with more (configureable) variants. I can also add hooks for users so that they can
write their own extensions.</p>
<p>Glyphs are indexed not by unicode but in their own way. This is because there is no
relationship with unicode at all, apart from the fact that a font might cover certain
ranges of characters. One character can have multiple shapes. However, at the
<l n='tex'/> end we use unicode so and all extra glyphs are mapped into a private
space. This is needed because we need to access them and <l n='tex'/> has to include
then in the output eventually.</p>
<p>The raw table as it coms from <l n='fontforge'/> gets reorganized in to fit out needs.
In <l n='context'/> that table is packed (similar tables are shared) and cached on disk
so that successive runs can use the optimized table (after loading the table is
unpacked). The flattening code used later is a prelude to an even more compact table
format (and as such it keeps evolving).</p>
<p>This module is sparsely documented because it is a moving target. The table format
of the reader changes and we experiment a lot with different methods for supporting
features.</p>
<p>As with the <l n='afm'/> code, we may decide to store more information in the
<l n='otf'/> table.</p>
<p>Incrementing the version number will force a re-cache. We jump the number by one
when there's a fix in the <l n='fontforge'/> library or <l n='lua'/> code that
results in different tables.</p>
--ldx]]--
-- action handler chainproc chainmore comment
--
-- gsub_single ok ok ok
-- gsub_multiple ok ok not implemented yet
-- gsub_alternate ok ok not implemented yet
-- gsub_ligature ok ok ok
-- gsub_context ok --
-- gsub_contextchain ok --
-- gsub_reversecontextchain ok --
-- chainsub -- ok
-- reversesub -- ok
-- gpos_mark2base ok ok
-- gpos_mark2ligature ok ok
-- gpos_mark2mark ok ok
-- gpos_cursive ok untested
-- gpos_single ok ok
-- gpos_pair ok ok
-- gpos_context ok --
-- gpos_contextchain ok --
--
-- todo: contextpos and contextsub and class stuff
--
-- actions:
--
-- handler : actions triggered by lookup
-- chainproc : actions triggered by contextual lookup
-- chainmore : multiple substitutions triggered by contextual lookup (e.g. fij -> f + ij)
--
-- remark: the 'not implemented yet' variants will be done when we have fonts that use them
-- remark: we need to check what to do with discretionaries
-- We used to have independent hashes for lookups but as the tags are unique
-- we now use only one hash. If needed we can have multiple again but in that
-- case I will probably prefix (i.e. rename) the lookups in the cached font file.
local concat, insert, remove = table.concat, table.insert, table.remove
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 lpegmatch = lpeg.match
local random = math.random
local logs, trackers, nodes, attributes = logs, trackers, nodes, attributes
local registertracker = trackers.register
local fonts = fonts
local otf = fonts.handlers.otf
local trace_lookups = false registertracker("otf.lookups", function(v) trace_lookups = v end)
local trace_singles = false registertracker("otf.singles", function(v) trace_singles = v end)
local trace_multiples = false registertracker("otf.multiples", function(v) trace_multiples = v end)
local trace_alternatives = false registertracker("otf.alternatives", function(v) trace_alternatives = v end)
local trace_ligatures = false registertracker("otf.ligatures", function(v) trace_ligatures = v end)
local trace_contexts = false registertracker("otf.contexts", function(v) trace_contexts = v end)
local trace_marks = false registertracker("otf.marks", function(v) trace_marks = v end)
local trace_kerns = false registertracker("otf.kerns", function(v) trace_kerns = v end)
local trace_cursive = false registertracker("otf.cursive", function(v) trace_cursive = v end)
local trace_preparing = false registertracker("otf.preparing", function(v) trace_preparing = v end)
local trace_bugs = false registertracker("otf.bugs", function(v) trace_bugs = v end)
local trace_details = false registertracker("otf.details", function(v) trace_details = v end)
local trace_applied = false registertracker("otf.applied", function(v) trace_applied = v end)
local trace_steps = false registertracker("otf.steps", function(v) trace_steps = v end)
local trace_skips = false registertracker("otf.skips", function(v) trace_skips = v end)
local trace_directions = false registertracker("otf.directions", function(v) trace_directions = v end)
local report_direct = logs.reporter("fonts","otf direct")
local report_subchain = logs.reporter("fonts","otf subchain")
local report_chain = logs.reporter("fonts","otf chain")
local report_process = logs.reporter("fonts","otf process")
local report_prepare = logs.reporter("fonts","otf prepare")
registertracker("otf.verbose_chain", function(v) otf.setcontextchain(v and "verbose") end)
registertracker("otf.normal_chain", function(v) otf.setcontextchain(v and "normal") end)
registertracker("otf.replacements", "otf.singles,otf.multiples,otf.alternatives,otf.ligatures")
registertracker("otf.positions","otf.marks,otf.kerns,otf.cursive")
registertracker("otf.actions","otf.replacements,otf.positions")
registertracker("otf.injections","nodes.injections")
registertracker("*otf.sample","otf.steps,otf.actions,otf.analyzing")
local insert_node_after = node.insert_after
local delete_node = nodes.delete
local copy_node = node.copy
local find_node_tail = node.tail or node.slide
local set_attribute = node.set_attribute
local has_attribute = node.has_attribute
local flush_node_list = node.flush_list
local setmetatableindex = table.setmetatableindex
local zwnj = 0x200C
local zwj = 0x200D
local wildcard = "*"
local default = "dflt"
local nodecodes = nodes.nodecodes
local whatcodes = nodes.whatcodes
local glyphcodes = nodes.glyphcodes
local glyph_code = nodecodes.glyph
local glue_code = nodecodes.glue
local disc_code = nodecodes.disc
local whatsit_code = nodecodes.whatsit
local dir_code = whatcodes.dir
local localpar_code = whatcodes.localpar
local ligature_code = glyphcodes.ligature
local privateattribute = attributes.private
-- Something is messed up: we have two mark / ligature indices, one at the injection
-- end and one here ... this is bases in KE's patches but there is something fishy
-- there as I'm pretty sure that for husayni we need some connection (as it's much
-- more complex than an average font) but I need proper examples of all cases, not
-- of only some.
local state = privateattribute('state')
local markbase = privateattribute('markbase')
local markmark = privateattribute('markmark')
local markdone = privateattribute('markdone') -- assigned at the injection end
local cursbase = privateattribute('cursbase')
local curscurs = privateattribute('curscurs')
local cursdone = privateattribute('cursdone')
local kernpair = privateattribute('kernpair')
local ligacomp = privateattribute('ligacomp') -- assigned here (ideally it should be combined)
local injections = nodes.injections
local setmark = injections.setmark
local setcursive = injections.setcursive
local setkern = injections.setkern
local setpair = injections.setpair
local markonce = true
local cursonce = true
local kernonce = true
local fonthashes = fonts.hashes
local fontdata = fonthashes.identifiers
local otffeatures = fonts.constructors.newfeatures("otf")
local registerotffeature = otffeatures.register
local onetimemessage = fonts.loggers.onetimemessage
otf.defaultnodealternate = "none" -- first last
-- we share some vars here, after all, we have no nested lookups and
-- less code
local tfmdata = false
local characters = false
local descriptions = false
local resources = false
local marks = false
local currentfont = false
local lookuptable = false
local anchorlookups = false
local lookuptypes = false
local handlers = { }
local rlmode = 0
local featurevalue = false
-- we cannot optimize with "start = first_glyph(head)" because then we don't
-- know which rlmode we're in which messes up cursive handling later on
--
-- head is always a whatsit so we can safely assume that head is not changed
-- we use this for special testing and documentation
local checkstep = (nodes and nodes.tracers and nodes.tracers.steppers.check) or function() end
local registerstep = (nodes and nodes.tracers and nodes.tracers.steppers.register) or function() end
local registermessage = (nodes and nodes.tracers and nodes.tracers.steppers.message) or function() end
local function logprocess(...)
if trace_steps then
registermessage(...)
end
report_direct(...)
end
local function logwarning(...)
report_direct(...)
end
local function gref(n)
if type(n) == "number" then
local description = descriptions[n]
local name = description and description.name
if name then
return format("U+%05X (%s)",n,name)
else
return format("U+%05X",n)
end
elseif not n then
return "<error in tracing>"
else
local num, nam = { }, { }
for i=1,#n do
local ni = n[i]
if tonumber(ni) then -- later we will start at 2
local di = descriptions[ni]
num[i] = format("U+%05X",ni)
nam[i] = di and di.name or "?"
end
end
return format("%s (%s)",concat(num," "), concat(nam," "))
end
end
local function cref(kind,chainname,chainlookupname,lookupname,index)
if index then
return format("feature %s, chain %s, sub %s, lookup %s, index %s",kind,chainname,chainlookupname,lookupname,index)
elseif lookupname then
return format("feature %s, chain %s, sub %s, lookup %s",kind,chainname or "?",chainlookupname or "?",lookupname)
elseif chainlookupname then
return format("feature %s, chain %s, sub %s",kind,chainname or "?",chainlookupname)
elseif chainname then
return format("feature %s, chain %s",kind,chainname)
else
return format("feature %s",kind)
end
end
local function pref(kind,lookupname)
return format("feature %s, lookup %s",kind,lookupname)
end
-- we can assume that languages that use marks are not hyphenated
-- we can also assume that at most one discretionary is present
local function markstoligature(kind,lookupname,start,stop,char)
local n = copy_node(start)
local keep = start
local current
current, start = insert_node_after(start,start,n)
local snext = stop.next
current.next = snext
if snext then
snext.prev = current
end
start.prev, stop.next = nil, nil
current.char, current.subtype, current.components = char, ligature_code, start
return keep
end
local function toligature(kind,lookupname,start,stop,char,markflag,discfound) -- brr head
if start == stop then
start.char = char
return start
elseif discfound then
-- print("start->stop",nodes.tosequence(start,stop))
local components = start.components
if components then
flush_node_list(components)
start.components = nil
end
local lignode = copy_node(start)
lignode.font = start.font
lignode.char = char
lignode.subtype = ligature_code
local next = stop.next
local prev = start.prev
stop.next = nil
start.prev = nil
lignode.components = start
-- print("lignode",nodes.tosequence(lignode))
-- print("components",nodes.tosequence(lignode.components))
prev.next = lignode
if next then
next.prev = lignode
end
lignode.next = next
lignode.prev = prev
-- print("start->end",nodes.tosequence(start))
return lignode
else
-- start is the ligature
local deletemarks = markflag ~= "mark"
local n = copy_node(start)
local current
current, start = insert_node_after(start,start,n)
local snext = stop.next
current.next = snext
if snext then
snext.prev = current
end
start.prev = nil
stop.next = nil
current.char = char
current.subtype = ligature_code
current.components = start
local head = current
-- this is messy ... we should get rid of the components eventually
local i = 0 -- is index of base
while start do
if not marks[start.char] then
i = i + 1
elseif not deletemarks then -- quite fishy
set_attribute(start,ligacomp,i)
if trace_marks then
logwarning("%s: keep mark %s, gets index %s",pref(kind,lookupname),gref(start.char),i)
end
head, current = insert_node_after(head,current,copy_node(start))
end
start = start.next
end
start = current.next
while start and start.id == glyph_code do
if marks[start.char] then
set_attribute(start,ligacomp,i)
if trace_marks then
logwarning("%s: keep mark %s, gets index %s",pref(kind,lookupname),gref(start.char),i)
end
else
break
end
start = start.next
end
--
-- we do need components in funny kerning mode but maybe I can better reconstruct then
-- as we do have the font components info available; removing components makes the
-- previous code much simpler
--
-- flush_node_list(head.components)
return head
end
end
function handlers.gsub_single(start,kind,lookupname,replacement)
if trace_singles then
logprocess("%s: replacing %s by single %s",pref(kind,lookupname),gref(start.char),gref(replacement))
end
start.char = replacement
return start, true
end
local function get_alternative_glyph(start,alternatives,value)
-- needs checking: (global value, brrr)
local choice = nil
local n = #alternatives
local char = start.char
--
if value == "random" then
local r = random(1,n)
value, choice = format("random, choice %s",r), alternatives[r]
elseif value == "first" then
value, choice = format("first, choice %s",1), alternatives[1]
elseif value == "last" then
value, choice = format("last, choice %s",n), alternatives[n]
else
value = tonumber(value)
if type(value) ~= "number" then
value, choice = "default, choice 1", alternatives[1]
elseif value > n then
local defaultalt = otf.defaultnodealternate
if defaultalt == "first" then
value, choice = format("no %s variants, taking %s",value,n), alternatives[n]
elseif defaultalt == "last" then
value, choice = format("no %s variants, taking %s",value,1), alternatives[1]
else
value, choice = format("no %s variants, ignoring",value), false
end
elseif value == 0 then
value, choice = format("choice %s (no change)",value), char
elseif value < 1 then
value, choice = format("no %s variants, taking %s",value,1), alternatives[1]
else
value, choice = format("choice %s",value), alternatives[value]
end
end
return choice
end
local function multiple_glyphs(start,multiple) -- marks ?
local nofmultiples = #multiple
if nofmultiples > 0 then
start.char = multiple[1]
if nofmultiples > 1 then
local sn = start.next
for k=2,nofmultiples do -- todo: use insert_node
local n = copy_node(start)
n.char = multiple[k]
n.next = sn
n.prev = start
if sn then
sn.prev = n
end
start.next = n
start = n
end
end
return start, true
else
if trace_multiples then
logprocess("no multiple for %s",gref(start.char))
end
return start, false
end
end
function handlers.gsub_alternate(start,kind,lookupname,alternative,sequence)
local value = featurevalue == true and tfmdata.shared.features[kind] or featurevalue
local choice = get_alternative_glyph(start,alternative,value)
if choice then
if trace_alternatives then
logprocess("%s: replacing %s by alternative %s (%s)",pref(kind,lookupname),gref(char),gref(choice),choice)
end
start.char = choice
else
if trace_alternatives then
logwarning("%s: no variant %s for %s",pref(kind,lookupname),tostring(value),gref(char))
end
end
return start, true
end
function handlers.gsub_multiple(start,kind,lookupname,multiple)
if trace_multiples then
logprocess("%s: replacing %s by multiple %s",pref(kind,lookupname),gref(start.char),gref(multiple))
end
return multiple_glyphs(start,multiple)
end
function handlers.gsub_ligature(start,kind,lookupname,ligature,sequence)
local s, stop, discfound = start.next, nil, false
local startchar = start.char
if marks[startchar] then
while s do
local id = s.id
if id == glyph_code and s.subtype<256 and s.font == currentfont then
local lg = ligature[s.char]
if lg then
stop = s
ligature = lg
s = s.next
else
break
end
else
break
end
end
if stop then
local lig = ligature.ligature
if lig then
if trace_ligatures then
local stopchar = stop.char
start = markstoligature(kind,lookupname,start,stop,lig)
logprocess("%s: replacing %s upto %s by ligature %s",pref(kind,lookupname),gref(startchar),gref(stopchar),gref(start.char))
else
start = markstoligature(kind,lookupname,start,stop,lig)
end
return start, true
else
-- ok, goto next lookup
end
end
else
local skipmark = sequence.flags[1]
while s do
local id = s.id
if id == glyph_code and s.subtype<256 then
if s.font == currentfont then
local char = s.char
if skipmark and marks[char] then
s = s.next
else
local lg = ligature[char]
if lg then
stop = s
ligature = lg
s = s.next
else
break
end
end
else
break
end
elseif id == disc_code then
discfound = true
s = s.next
else
break
end
end
if stop then
local lig = ligature.ligature
if lig then
if trace_ligatures then
local stopchar = stop.char
start = toligature(kind,lookupname,start,stop,lig,skipmark,discfound)
logprocess("%s: replacing %s upto %s by ligature %s",pref(kind,lookupname),gref(startchar),gref(stopchar),gref(start.char))
else
start = toligature(kind,lookupname,start,stop,lig,skipmark,discfound)
end
return start, true
else
-- ok, goto next lookup
end
end
end
return start, false
end
--[[ldx--
<p>We get hits on a mark, but we're not sure if the it has to be applied so
we need to explicitly test for basechar, baselig and basemark entries.</p>
--ldx]]--
function handlers.gpos_mark2base(start,kind,lookupname,markanchors,sequence)
local markchar = start.char
if marks[markchar] then
local base = start.prev -- [glyph] [start=mark]
if base and base.id == glyph_code and base.subtype<256 and base.font == currentfont then
local basechar = base.char
if marks[basechar] then
while true do
base = base.prev
if base and base.id == glyph_code and base.subtype<256 and base.font == currentfont then
basechar = base.char
if not marks[basechar] then
break
end
else
if trace_bugs then
logwarning("%s: no base for mark %s",pref(kind,lookupname),gref(markchar))
end
return start, false
end
end
end
local baseanchors = descriptions[basechar]
if baseanchors then
baseanchors = baseanchors.anchors
end
if baseanchors then
local baseanchors = baseanchors['basechar']
if baseanchors then
local al = anchorlookups[lookupname]
for anchor,ba in next, baseanchors do
if al[anchor] then
local ma = markanchors[anchor]
if ma then
local dx, dy, bound = setmark(start,base,tfmdata.parameters.factor,rlmode,ba,ma)
if trace_marks then
logprocess("%s, anchor %s, bound %s: anchoring mark %s to basechar %s => (%s,%s)",
pref(kind,lookupname),anchor,bound,gref(markchar),gref(basechar),dx,dy)
end
return start, true
end
end
end
if trace_bugs then
logwarning("%s, no matching anchors for mark %s and base %s",pref(kind,lookupname),gref(markchar),gref(basechar))
end
end
else -- if trace_bugs then
-- logwarning("%s: char %s is missing in font",pref(kind,lookupname),gref(basechar))
onetimemessage(currentfont,basechar,"no base anchors",report_fonts)
end
elseif trace_bugs then
logwarning("%s: prev node is no char",pref(kind,lookupname))
end
elseif trace_bugs then
logwarning("%s: mark %s is no mark",pref(kind,lookupname),gref(markchar))
end
return start, false
end
function handlers.gpos_mark2ligature(start,kind,lookupname,markanchors,sequence)
-- check chainpos variant
local markchar = start.char
if marks[markchar] then
local base = start.prev -- [glyph] [optional marks] [start=mark]
if base and base.id == glyph_code and base.subtype<256 and base.font == currentfont then
local basechar = base.char
if marks[basechar] then
while true do
base = base.prev
if base and base.id == glyph_code and base.subtype<256 and base.font == currentfont then
basechar = base.char
if not marks[basechar] then
break
end
else
if trace_bugs then
logwarning("%s: no base for mark %s",pref(kind,lookupname),gref(markchar))
end
return start, false
end
end
end
local index = has_attribute(start,ligacomp)
local baseanchors = descriptions[basechar]
if baseanchors then
baseanchors = baseanchors.anchors
if baseanchors then
local baseanchors = baseanchors['baselig']
if baseanchors then
local al = anchorlookups[lookupname]
for anchor,ba in next, baseanchors do
if al[anchor] then
local ma = markanchors[anchor]
if ma then
ba = ba[index]
if ba then
local dx, dy, bound = setmark(start,base,tfmdata.parameters.factor,rlmode,ba,ma) -- index
if trace_marks then
logprocess("%s, anchor %s, index %s, bound %s: anchoring mark %s to baselig %s at index %s => (%s,%s)",
pref(kind,lookupname),anchor,index,bound,gref(markchar),gref(basechar),index,dx,dy)
end
return start, true
end
end
end
end
if trace_bugs then
logwarning("%s: no matching anchors for mark %s and baselig %s",pref(kind,lookupname),gref(markchar),gref(basechar))
end
end
end
else -- if trace_bugs then
-- logwarning("%s: char %s is missing in font",pref(kind,lookupname),gref(basechar))
onetimemessage(currentfont,basechar,"no base anchors",report_fonts)
end
elseif trace_bugs then
logwarning("%s: prev node is no char",pref(kind,lookupname))
end
elseif trace_bugs then
logwarning("%s: mark %s is no mark",pref(kind,lookupname),gref(markchar))
end
return start, false
end
function handlers.gpos_mark2mark(start,kind,lookupname,markanchors,sequence)
local markchar = start.char
if marks[markchar] then
local base = start.prev -- [glyph] [basemark] [start=mark]
-- while base and has_attribute(base,ligacomp) and has_attribute(base,ligacomp) ~= has_attribute(start,ligacomp) do
-- base = base.prev -- KE: prevents mkmk for marks on different components of a ligature
-- end
local slc = has_attribute(start,ligacomp)
if slc then -- a rather messy loop ... needs checking with husayni
while base do
local blc = has_attribute(base,ligacomp)
if blc and blc ~= slc then
base = base.prev
else
break
end
end
end
if base and base.id == glyph_code and base.subtype<256 and base.font == currentfont then -- subtype test can go
local basechar = base.char
local baseanchors = descriptions[basechar]
if baseanchors then
baseanchors = baseanchors.anchors
if baseanchors then
baseanchors = baseanchors['basemark']
if baseanchors then
local al = anchorlookups[lookupname]
for anchor,ba in next, baseanchors do
if al[anchor] then
local ma = markanchors[anchor]
if ma then
local dx, dy, bound = setmark(start,base,tfmdata.parameters.factor,rlmode,ba,ma)
if trace_marks then
logprocess("%s, anchor %s, bound %s: anchoring mark %s to basemark %s => (%s,%s)",
pref(kind,lookupname),anchor,bound,gref(markchar),gref(basechar),dx,dy)
end
return start,true
end
end
end
if trace_bugs then
logwarning("%s: no matching anchors for mark %s and basemark %s",pref(kind,lookupname),gref(markchar),gref(basechar))
end
end
end
else -- if trace_bugs then
-- logwarning("%s: char %s is missing in font",pref(kind,lookupname),gref(basechar))
onetimemessage(currentfont,basechar,"no base anchors",report_fonts)
end
elseif trace_bugs then
logwarning("%s: prev node is no mark",pref(kind,lookupname))
end
elseif trace_bugs then
logwarning("%s: mark %s is no mark",pref(kind,lookupname),gref(markchar))
end
return start,false
end
function handlers.gpos_cursive(start,kind,lookupname,exitanchors,sequence) -- to be checked
local alreadydone = cursonce and has_attribute(start,cursbase)
if not alreadydone then
local done = false
local startchar = start.char
if marks[startchar] then
if trace_cursive then
logprocess("%s: ignoring cursive for mark %s",pref(kind,lookupname),gref(startchar))
end
else
local nxt = start.next
while not done and nxt and nxt.id == glyph_code and nxt.subtype<256 and nxt.font == currentfont do
local nextchar = nxt.char
if marks[nextchar] then
-- should not happen (maybe warning)
nxt = nxt.next
else
local entryanchors = descriptions[nextchar]
if entryanchors then
entryanchors = entryanchors.anchors
if entryanchors then
entryanchors = entryanchors['centry']
if entryanchors then
local al = anchorlookups[lookupname]
for anchor, entry in next, entryanchors do
if al[anchor] then
local exit = exitanchors[anchor]
if exit then
local dx, dy, bound = setcursive(start,nxt,tfmdata.parameters.factor,rlmode,exit,entry,characters[startchar],characters[nextchar])
if trace_cursive then
logprocess("%s: moving %s to %s cursive (%s,%s) using anchor %s and bound %s in rlmode %s",pref(kind,lookupname),gref(startchar),gref(nextchar),dx,dy,anchor,bound,rlmode)
end
done = true
break
end
end
end
end
end
else -- if trace_bugs then
-- logwarning("%s: char %s is missing in font",pref(kind,lookupname),gref(startchar))
onetimemessage(currentfont,startchar,"no entry anchors",report_fonts)
end
break
end
end
end
return start, done
else
if trace_cursive and trace_details then
logprocess("%s, cursive %s is already done",pref(kind,lookupname),gref(start.char),alreadydone)
end
return start, false
end
end
function handlers.gpos_single(start,kind,lookupname,kerns,sequence)
local startchar = start.char
local dx, dy, w, h = setpair(start,tfmdata.parameters.factor,rlmode,sequence.flags[4],kerns,characters[startchar])
if trace_kerns then
logprocess("%s: shifting single %s by (%s,%s) and correction (%s,%s)",pref(kind,lookupname),gref(startchar),dx,dy,w,h)
end
return start, false
end
function handlers.gpos_pair(start,kind,lookupname,kerns,sequence)
-- todo: kerns in disc nodes: pre, post, replace -> loop over disc too
-- todo: kerns in components of ligatures
local snext = start.next
if not snext then
return start, false
else
local prev, done = start, false
local factor = tfmdata.parameters.factor
local lookuptype = lookuptypes[lookupname]
while snext and snext.id == glyph_code and snext.subtype<256 and snext.font == currentfont do
local nextchar = snext.char
local krn = kerns[nextchar]
if not krn and marks[nextchar] then
prev = snext
snext = snext.next
else
local krn = kerns[nextchar]
if not krn then
-- skip
elseif type(krn) == "table" then
if lookuptype == "pair" then -- probably not needed
local a, b = krn[2], krn[3]
if a and #a > 0 then
local startchar = start.char
local x, y, w, h = setpair(start,factor,rlmode,sequence.flags[4],a,characters[startchar])
if trace_kerns then
logprocess("%s: shifting first of pair %s and %s by (%s,%s) and correction (%s,%s)",pref(kind,lookupname),gref(startchar),gref(nextchar),x,y,w,h)
end
end
if b and #b > 0 then
local startchar = start.char
local x, y, w, h = setpair(snext,factor,rlmode,sequence.flags[4],b,characters[nextchar])
if trace_kerns then
logprocess("%s: shifting second of pair %s and %s by (%s,%s) and correction (%s,%s)",pref(kind,lookupname),gref(startchar),gref(nextchar),x,y,w,h)
end
end
else -- wrong ... position has different entries
report_process("%s: check this out (old kern stuff)",pref(kind,lookupname))
-- local a, b = krn[2], krn[6]
-- if a and a ~= 0 then
-- local k = setkern(snext,factor,rlmode,a)
-- if trace_kerns then
-- logprocess("%s: inserting first kern %s between %s and %s",pref(kind,lookupname),k,gref(prev.char),gref(nextchar))
-- end
-- end
-- if b and b ~= 0 then
-- logwarning("%s: ignoring second kern xoff %s",pref(kind,lookupname),b*factor)
-- end
end
done = true
elseif krn ~= 0 then
local k = setkern(snext,factor,rlmode,krn)
if trace_kerns then
logprocess("%s: inserting kern %s between %s and %s",pref(kind,lookupname),k,gref(prev.char),gref(nextchar))
end
done = true
end
break
end
end
return start, done
end
end
--[[ldx--
<p>I will implement multiple chain replacements once I run into a font that uses
it. It's not that complex to handle.</p>
--ldx]]--
local chainmores = { }
local chainprocs = { }
local function logprocess(...)
if trace_steps then
registermessage(...)
end
report_subchain(...)
end
local logwarning = report_subchain
local function logprocess(...)
if trace_steps then
registermessage(...)
end
report_chain(...)
end
local logwarning = report_chain
-- We could share functions but that would lead to extra function calls with many
-- arguments, redundant tests and confusing messages.
function chainprocs.chainsub(start,stop,kind,chainname,currentcontext,lookuphash,lookuplist,chainlookupname)
logwarning("%s: a direct call to chainsub cannot happen",cref(kind,chainname,chainlookupname))
return start, false
end
function chainmores.chainsub(start,stop,kind,chainname,currentcontext,lookuphash,lookuplist,chainlookupname,n)
logprocess("%s: a direct call to chainsub cannot happen",cref(kind,chainname,chainlookupname))
return start, false
end
-- The reversesub is a special case, which is why we need to store the replacements
-- in a bit weird way. There is no lookup and the replacement comes from the lookup
-- itself. It is meant mostly for dealing with Urdu.
function chainprocs.reversesub(start,stop,kind,chainname,currentcontext,lookuphash,replacements)
local char = start.char
local replacement = replacements[char]
if replacement then
if trace_singles then
logprocess("%s: single reverse replacement of %s by %s",cref(kind,chainname),gref(char),gref(replacement))
end
start.char = replacement
return start, true
else
return start, false
end
end
--[[ldx--
<p>This chain stuff is somewhat tricky since we can have a sequence of actions to be
applied: single, alternate, multiple or ligature where ligature can be an invalid
one in the sense that it will replace multiple by one but not neccessary one that
looks like the combination (i.e. it is the counterpart of multiple then). For
example, the following is valid:</p>
<typing>
<line>xxxabcdexxx [single a->A][multiple b->BCD][ligature cde->E] xxxABCDExxx</line>
</typing>
<p>Therefore we we don't really do the replacement here already unless we have the
single lookup case. The efficiency of the replacements can be improved by deleting
as less as needed but that would also make the code even more messy.</p>
--ldx]]--
local function delete_till_stop(start,stop,ignoremarks) -- keeps start
local n = 1
if start == stop then
-- done
elseif ignoremarks then
repeat -- start x x m x x stop => start m
local next = start.next
if not marks[next.char] then
delete_node(start,next)
end
n = n + 1
until next == stop
else -- start x x x stop => start
repeat
local next = start.next
delete_node(start,next)
n = n + 1
until next == stop
end