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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
|
if not modules then modules = { } end modules ['font-ttf'] = {
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 version is different from previous in the sense that we no longer store
-- contours but keep points and contours (endpoints) separate for a while
-- because later on we need to apply deltas and that is easier on a list of
-- points.
-- The code is a bit messy. I looked at the ff code but it's messy too. It has
-- to do with the fact that we need to look at points on the curve and control
-- points in between. This also means that we start at point 2 and have to look
-- at point 1 when we're at the end. We still use a ps like storage with the
-- operator last in an entry. It's typical code that evolves stepwise till a
-- point of no comprehension.
-- For deltas we need a rather complex loop over points that can have holes and
-- be less than nofpoints and even can have duplicates and also the x and y value
-- lists can be shorter than etc. I need fonts in order to complete this simply
-- because I need to visualize in order to understand (what the standard tries
-- to explain).
-- 0 point then none applied
-- 1 points then applied to all
-- otherwise inferred deltas using nearest
-- if no lower point then use highest referenced point
-- if no higher point then use lowest referenced point
-- factor = (target-left)/(right-left)
-- delta = (1-factor)*left + factor * right
local next, type, unpack = next, type, unpack
local band, rshift = bit32.band, bit32.rshift
local sqrt, round = math.sqrt, math.round
local char, rep = string.char, string.rep
local concat = table.concat
local idiv = number.idiv
local setmetatableindex = table.setmetatableindex
local report = logs.reporter("otf reader","ttf")
local trace_deltas = false
local readers = fonts.handlers.otf.readers
local streamreader = readers.streamreader
local setposition = streamreader.setposition
local getposition = streamreader.getposition
local skipbytes = streamreader.skip
local readbyte = streamreader.readcardinal1 -- 8-bit unsigned integer
local readushort = streamreader.readcardinal2 -- 16-bit unsigned integer
local readulong = streamreader.readcardinal4 -- 24-bit unsigned integer
local readchar = streamreader.readinteger1 -- 8-bit signed integer
local readshort = streamreader.readinteger2 -- 16-bit signed integer
local read2dot14 = streamreader.read2dot14 -- 16-bit signed fixed number with the low 14 bits of fraction (2.14) (F2DOT14)
local readinteger = streamreader.readinteger1
local readcardinaltable = streamreader.readcardinaltable
local readintegertable = streamreader.readintegertable
directives.register("fonts.streamreader",function()
streamreader = utilities.streams
setposition = streamreader.setposition
getposition = streamreader.getposition
skipbytes = streamreader.skip
readbyte = streamreader.readcardinal1
readushort = streamreader.readcardinal2
readulong = streamreader.readcardinal4
readchar = streamreader.readinteger1
readshort = streamreader.readinteger2
read2dot14 = streamreader.read2dot14
readinteger = streamreader.readinteger1
readcardinaltable = streamreader.readcardinaltable
readintegertable = streamreader.readintegertable
end)
local short = 2
local ushort = 2
local ulong = 4
local helpers = readers.helpers
local gotodatatable = helpers.gotodatatable
local function mergecomposites(glyphs,shapes)
-- todo : deltas
local function merge(index,shape,components)
local contours = { }
local points = { }
local nofcontours = 0
local nofpoints = 0
local offset = 0
local deltas = shape.deltas
for i=1,#components do
local component = components[i]
local subindex = component.index
local subshape = shapes[subindex]
local subcontours = subshape.contours
local subpoints = subshape.points
if not subcontours then
local subcomponents = subshape.components
if subcomponents then
subcontours, subpoints = merge(subindex,subshape,subcomponents)
end
end
if subpoints then
local matrix = component.matrix
local xscale = matrix[1]
local xrotate = matrix[2]
local yrotate = matrix[3]
local yscale = matrix[4]
local xoffset = matrix[5]
local yoffset = matrix[6]
local count = #subpoints
if xscale == 1 and yscale == 1 and xrotate == 0 and yrotate == 0 then
for i=1,count do
local p = subpoints[i]
nofpoints = nofpoints + 1
points[nofpoints] = {
p[1] + xoffset,
p[2] + yoffset,
p[3]
}
end
else
for i=1,count do
local p = subpoints[i]
local x = p[1]
local y = p[2]
nofpoints = nofpoints + 1
points[nofpoints] = {
xscale * x + xrotate * y + xoffset,
yscale * y + yrotate * x + yoffset,
p[3]
}
end
end
local subcount = #subcontours
if subcount == 1 then
nofcontours = nofcontours + 1
contours[nofcontours] = offset + subcontours[1]
else
for i=1,#subcontours do
nofcontours = nofcontours + 1
contours[nofcontours] = offset + subcontours[i]
end
end
offset = offset + count
else
report("missing contours composite %s, component %s of %s, glyph %s",index,i,#components,subindex)
end
end
shape.points = points -- todo : phantom points
shape.contours = contours
shape.components = nil
return contours, points
end
-- for index=1,#glyphs do
for index=0,#glyphs-1 do
local shape = shapes[index]
if shape then
local components = shape.components
if components then
merge(index,shape,components)
end
end
end
end
local function readnothing(f)
return {
type = "nothing",
}
end
-- begin of converter
local function curveto(m_x,m_y,l_x,l_y,r_x,r_y) -- todo: inline this
return
l_x + 2/3 *(m_x-l_x), l_y + 2/3 *(m_y-l_y),
r_x + 2/3 *(m_x-r_x), r_y + 2/3 *(m_y-r_y),
r_x, r_y, "c"
end
-- We could omit the operator which saves some 10%:
--
-- #2=lineto #4=quadratic #6=cubic #3=moveto (with "m")
--
-- This is tricky ... something to do with phantom points .. however, the hvar
-- and vvar tables should take care of the width .. the test font doesn't have
-- those so here we go then (we need a flag for hvar).
--
-- h-advance left-side-bearing v-advance top-side-bearing
--
-- We had two loops (going backward) but can do it in one loop .. but maybe we
-- should only accept fonts with proper hvar tables.
local function applyaxis(glyph,shape,deltas,dowidth)
local points = shape.points
if points then
local nofpoints = #points
local h = nofpoints + 2 -- weird, the example font seems to have left first
local l = nofpoints + 1
----- v = nofpoints + 3
----- t = nofpoints + 4
local dw = 0
local dl = 0
for i=1,#deltas do
local deltaset = deltas[i]
local xvalues = deltaset.xvalues
local yvalues = deltaset.yvalues
local dpoints = deltaset.points
local factor = deltaset.factor
if dpoints then
-- todo: interpolate
local nofdpoints = #dpoints
for i=1,nofdpoints do
local d = dpoints[i]
local p = points[d]
if p then
if xvalues then
local x = xvalues[i]
if x and x ~= 0 then
p[1] = p[1] + factor * x
end
end
if yvalues then
local y = yvalues[i]
if y and y ~= 0 then
p[2] = p[2] + factor * y
end
end
elseif dowidth then
-- we've now ran into phantom points which is a bit fuzzy because:
-- are there gaps in there?
--
-- todo: move this outside the loop (when we can be sure of all 4 being there)
if d == h then
-- we have a phantom point hadvance
local x = xvalues[i]
if x then
dw = dw + factor * x
end
elseif d == l then
local x = xvalues[i]
if x then
dl = dl + factor * x
end
end
end
end
else
for i=1,nofpoints do
local p = points[i]
if xvalues then
local x = xvalues[i]
if x and x ~= 0 then
p[1] = p[1] + factor * x
end
end
if yvalues then
local y = yvalues[i]
if y and y ~= 0 then
p[2] = p[2] + factor * y
end
end
end
if dowidth then
local x = xvalues[h]
if x then
dw = dw + factor * x
end
local x = xvalues[l]
if x then
dl = dl + factor * x
end
end
end
end
-- for i=1,nofpoints do
-- local p = points[i]
-- p[1] = round(p[1])
-- p[2] = round(p[2])
-- end
if dowidth then
local width = glyph.width or 0
-- local lsb = glyph.lsb or 0
glyph.width = width + dw - dl
end
else
report("no points for glyph %a",glyph.name)
end
end
-- round or not ?
-- local quadratic = true -- both methods work, todo: install a directive
local quadratic = false
local function contours2outlines_normal(glyphs,shapes) -- maybe accept the bbox overhead
-- for index=1,#glyphs do
for index=0,#glyphs-1 do
local shape = shapes[index]
if shape then
local glyph = glyphs[index]
local contours = shape.contours
local points = shape.points
if contours then
local nofcontours = #contours
local segments = { }
local nofsegments = 0
glyph.segments = segments
if nofcontours > 0 then
local px = 0
local py = 0
local first = 1
for i=1,nofcontours do
local last = contours[i]
if last >= first then
local first_pt = points[first]
local first_on = first_pt[3]
-- todo no new tables but reuse lineto and quadratic
if first == last then
first_pt[3] = "m" -- "moveto"
nofsegments = nofsegments + 1
segments[nofsegments] = first_pt
else -- maybe also treat n == 2 special
local first_on = first_pt[3]
local last_pt = points[last]
local last_on = last_pt[3]
local start = 1
local control_pt = false
if first_on then
start = 2
else
if last_on then
first_pt = last_pt
else
first_pt = { (first_pt[1]+last_pt[1])/2, (first_pt[2]+last_pt[2])/2, false }
end
control_pt = first_pt
end
local x = first_pt[1]
local y = first_pt[2]
if not done then
xmin = x
ymin = y
xmax = x
ymax = y
done = true
end
nofsegments = nofsegments + 1
segments[nofsegments] = { x, y, "m" } -- "moveto"
if not quadratic then
px = x
py = y
end
local previous_pt = first_pt
for i=first,last do
local current_pt = points[i]
local current_on = current_pt[3]
local previous_on = previous_pt[3]
if previous_on then
if current_on then
-- both normal points
local x, y = current_pt[1], current_pt[2]
nofsegments = nofsegments + 1
segments[nofsegments] = { x, y, "l" } -- "lineto"
if not quadratic then
px, py = x, y
end
else
control_pt = current_pt
end
elseif current_on then
local x1 = control_pt[1]
local y1 = control_pt[2]
local x2 = current_pt[1]
local y2 = current_pt[2]
nofsegments = nofsegments + 1
if quadratic then
segments[nofsegments] = { x1, y1, x2, y2, "q" } -- "quadraticto"
else
x1, y1, x2, y2, px, py = curveto(x1, y1, px, py, x2, y2)
segments[nofsegments] = { x1, y1, x2, y2, px, py, "c" } -- "curveto"
end
control_pt = false
else
local x2 = (previous_pt[1]+current_pt[1])/2
local y2 = (previous_pt[2]+current_pt[2])/2
local x1 = control_pt[1]
local y1 = control_pt[2]
nofsegments = nofsegments + 1
if quadratic then
segments[nofsegments] = { x1, y1, x2, y2, "q" } -- "quadraticto"
else
x1, y1, x2, y2, px, py = curveto(x1, y1, px, py, x2, y2)
segments[nofsegments] = { x1, y1, x2, y2, px, py, "c" } -- "curveto"
end
control_pt = current_pt
end
previous_pt = current_pt
end
if first_pt == last_pt then
-- we're already done, probably a simple curve
else
nofsegments = nofsegments + 1
local x2 = first_pt[1]
local y2 = first_pt[2]
if not control_pt then
segments[nofsegments] = { x2, y2, "l" } -- "lineto"
elseif quadratic then
local x1 = control_pt[1]
local y1 = control_pt[2]
segments[nofsegments] = { x1, y1, x2, y2, "q" } -- "quadraticto"
else
local x1 = control_pt[1]
local y1 = control_pt[2]
x1, y1, x2, y2, px, py = curveto(x1, y1, px, py, x2, y2)
segments[nofsegments] = { x1, y1, x2, y2, px, py, "c" } -- "curveto"
-- px, py = x2, y2
end
end
end
end
first = last + 1
end
end
end
end
end
end
local function contours2outlines_shaped(glyphs,shapes,keepcurve)
-- for index=1,#glyphs do
for index=0,#glyphs-1 do
local shape = shapes[index]
if shape then
local glyph = glyphs[index]
local contours = shape.contours
local points = shape.points
if contours then
local nofcontours = #contours
local segments = keepcurve and { } or nil
local nofsegments = 0
if keepcurve then
glyph.segments = segments
end
if nofcontours > 0 then
local xmin, ymin, xmax, ymax, done = 0, 0, 0, 0, false
local px, py = 0, 0 -- we could use these in calculations which saves a copy
local first = 1
for i=1,nofcontours do
local last = contours[i]
if last >= first then
local first_pt = points[first]
local first_on = first_pt[3]
-- todo no new tables but reuse lineto and quadratic
if first == last then
-- this can influence the boundingbox
if keepcurve then
first_pt[3] = "m" -- "moveto"
nofsegments = nofsegments + 1
segments[nofsegments] = first_pt
end
else -- maybe also treat n == 2 special
local first_on = first_pt[3]
local last_pt = points[last]
local last_on = last_pt[3]
local start = 1
local control_pt = false
if first_on then
start = 2
else
if last_on then
first_pt = last_pt
else
first_pt = { (first_pt[1]+last_pt[1])/2, (first_pt[2]+last_pt[2])/2, false }
end
control_pt = first_pt
end
local x = first_pt[1]
local y = first_pt[2]
if not done then
xmin, ymin, xmax, ymax = x, y, x, y
done = true
else
if x < xmin then xmin = x elseif x > xmax then xmax = x end
if y < ymin then ymin = y elseif y > ymax then ymax = y end
end
if keepcurve then
nofsegments = nofsegments + 1
segments[nofsegments] = { x, y, "m" } -- "moveto"
end
if not quadratic then
px = x
py = y
end
local previous_pt = first_pt
for i=first,last do
local current_pt = points[i]
local current_on = current_pt[3]
local previous_on = previous_pt[3]
if previous_on then
if current_on then
-- both normal points
local x = current_pt[1]
local y = current_pt[2]
if x < xmin then xmin = x elseif x > xmax then xmax = x end
if y < ymin then ymin = y elseif y > ymax then ymax = y end
if keepcurve then
nofsegments = nofsegments + 1
segments[nofsegments] = { x, y, "l" } -- "lineto"
end
if not quadratic then
px = x
py = y
end
else
control_pt = current_pt
end
elseif current_on then
local x1 = control_pt[1]
local y1 = control_pt[2]
local x2 = current_pt[1]
local y2 = current_pt[2]
if quadratic then
if x1 < xmin then xmin = x1 elseif x1 > xmax then xmax = x1 end
if y1 < ymin then ymin = y1 elseif y1 > ymax then ymax = y1 end
if keepcurve then
nofsegments = nofsegments + 1
segments[nofsegments] = { x1, y1, x2, y2, "q" } -- "quadraticto"
end
else
x1, y1, x2, y2, px, py = curveto(x1, y1, px, py, x2, y2)
if x1 < xmin then xmin = x1 elseif x1 > xmax then xmax = x1 end
if y1 < ymin then ymin = y1 elseif y1 > ymax then ymax = y1 end
if x2 < xmin then xmin = x2 elseif x2 > xmax then xmax = x2 end
if y2 < ymin then ymin = y2 elseif y2 > ymax then ymax = y2 end
if px < xmin then xmin = px elseif px > xmax then xmax = px end
if py < ymin then ymin = py elseif py > ymax then ymax = py end
if keepcurve then
nofsegments = nofsegments + 1
segments[nofsegments] = { x1, y1, x2, y2, px, py, "c" } -- "curveto"
end
end
control_pt = false
else
local x2 = (previous_pt[1]+current_pt[1])/2
local y2 = (previous_pt[2]+current_pt[2])/2
local x1 = control_pt[1]
local y1 = control_pt[2]
if quadratic then
if x1 < xmin then xmin = x1 elseif x1 > xmax then xmax = x1 end
if y1 < ymin then ymin = y1 elseif y1 > ymax then ymax = y1 end
if keepcurve then
nofsegments = nofsegments + 1
segments[nofsegments] = { x1, y1, x2, y2, "q" } -- "quadraticto"
end
else
x1, y1, x2, y2, px, py = curveto(x1, y1, px, py, x2, y2)
if x1 < xmin then xmin = x1 elseif x1 > xmax then xmax = x1 end
if y1 < ymin then ymin = y1 elseif y1 > ymax then ymax = y1 end
if x2 < xmin then xmin = x2 elseif x2 > xmax then xmax = x2 end
if y2 < ymin then ymin = y2 elseif y2 > ymax then ymax = y2 end
if px < xmin then xmin = px elseif px > xmax then xmax = px end
if py < ymin then ymin = py elseif py > ymax then ymax = py end
if keepcurve then
nofsegments = nofsegments + 1
segments[nofsegments] = { x1, y1, x2, y2, px, py, "c" } -- "curveto"
end
end
control_pt = current_pt
end
previous_pt = current_pt
end
if first_pt == last_pt then
-- we're already done, probably a simple curve
elseif not control_pt then
if keepcurve then
nofsegments = nofsegments + 1
segments[nofsegments] = { first_pt[1], first_pt[2], "l" } -- "lineto"
end
else
local x1 = control_pt[1]
local y1 = control_pt[2]
local x2 = first_pt[1]
local y2 = first_pt[2]
if x1 < xmin then xmin = x1 elseif x1 > xmax then xmax = x1 end
if y1 < ymin then ymin = y1 elseif y1 > ymax then ymax = y1 end
if quadratic then
if keepcurve then
nofsegments = nofsegments + 1
segments[nofsegments] = { x1, y1, x2, y2, "q" } -- "quadraticto"
end
else
x1, y1, x2, y2, px, py = curveto(x1, y1, px, py, x2, y2)
if x2 < xmin then xmin = x2 elseif x2 > xmax then xmax = x2 end
if y2 < ymin then ymin = y2 elseif y2 > ymax then ymax = y2 end
if px < xmin then xmin = px elseif px > xmax then xmax = px end
if py < ymin then ymin = py elseif py > ymax then ymax = py end
if keepcurve then
nofsegments = nofsegments + 1
segments[nofsegments] = { x1, y1, x2, y2, px, py, "c" } -- "curveto"
end
-- px, py = x2, y2
end
end
end
end
first = last + 1
end
glyph.boundingbox = { round(xmin), round(ymin), round(xmax), round(ymax) }
end
end
end
end
end
-- optimize for zero
local c_zero = char(0)
local s_zero = char(0,0)
-- local shorthash = setmetatableindex(function(t,k)
-- t[k] = char(band(rshift(k,8),0xFF),band(k,0xFF)) return t[k]
-- end)
local function toushort(n)
return char(band(rshift(n,8),0xFF),band(n,0xFF))
-- return shorthash[n]
end
local function toshort(n)
if n < 0 then
n = n + 0x10000
end
return char(band(rshift(n,8),0xFF),band(n,0xFF))
-- return shorthash[n]
end
-- todo: we can reuse result, xpoints and ypoints
local chars = setmetatableindex(function(t,k)
for i=0,255 do local v = char(i) t[i] = v end return t[k]
end)
local function repackpoints(glyphs,shapes)
local noboundingbox = { 0, 0, 0, 0 }
local result = { } -- reused
local xpoints = { } -- reused
local ypoints = { } -- reused
for index=0,#glyphs-1 do
local shape = shapes[index]
if shape then
local r = 0
local glyph = glyphs[index]
local contours = shape.contours
local nofcontours = contours and #contours or 0
local boundingbox = glyph.boundingbox or noboundingbox
r = r + 1 result[r] = toshort(nofcontours)
r = r + 1 result[r] = toshort(boundingbox[1]) -- xmin
r = r + 1 result[r] = toshort(boundingbox[2]) -- ymin
r = r + 1 result[r] = toshort(boundingbox[3]) -- xmax
r = r + 1 result[r] = toshort(boundingbox[4]) -- ymax
if nofcontours > 0 then
for i=1,nofcontours do
r = r + 1 result[r] = toshort(contours[i]-1)
end
r = r + 1 result[r] = s_zero -- no instructions
local points = shape.points
local currentx = 0
local currenty = 0
-- local xpoints = { }
-- local ypoints = { }
local x = 0
local y = 0
local lastflag = nil
local nofflags = 0
for i=1,#points do
local pt = points[i]
local px = pt[1]
local py = pt[2]
local fl = pt[3] and 0x01 or 0x00
if px == currentx then
fl = fl + 0x10
else
local dx = round(px - currentx)
x = x + 1
if dx < -255 or dx > 255 then
xpoints[x] = toshort(dx)
elseif dx < 0 then
fl = fl + 0x02
-- xpoints[x] = char(-dx)
xpoints[x] = chars[-dx]
elseif dx > 0 then
fl = fl + 0x12
-- xpoints[x] = char(dx)
xpoints[x] = chars[dx]
else
fl = fl + 0x02
xpoints[x] = c_zero
end
end
if py == currenty then
fl = fl + 0x20
else
local dy = round(py - currenty)
y = y + 1
if dy < -255 or dy > 255 then
ypoints[y] = toshort(dy)
elseif dy < 0 then
fl = fl + 0x04
-- ypoints[y] = char(-dy)
ypoints[y] = chars[-dy]
elseif dy > 0 then
fl = fl + 0x24
-- ypoints[y] = char(dy)
ypoints[y] = chars[dy]
else
fl = fl + 0x04
ypoints[y] = c_zero
end
end
currentx = px
currenty = py
if lastflag == fl then
if nofflags == 255 then
-- This happens in koeieletters!
lastflag = lastflag + 0x08
r = r + 1 result[r] = char(lastflag,nofflags-1)
nofflags = 1
lastflag = fl
else
nofflags = nofflags + 1
end
else -- if > 255
if nofflags == 1 then
-- r = r + 1 result[r] = char(lastflag)
r = r + 1 result[r] = chars[lastflag]
elseif nofflags == 2 then
r = r + 1 result[r] = char(lastflag,lastflag)
elseif nofflags > 2 then
lastflag = lastflag + 0x08
r = r + 1 result[r] = char(lastflag,nofflags-1)
end
nofflags = 1
lastflag = fl
end
end
if nofflags == 1 then
-- r = r + 1 result[r] = char(lastflag)
r = r + 1 result[r] = chars[lastflag]
elseif nofflags == 2 then
r = r + 1 result[r] = char(lastflag,lastflag)
elseif nofflags > 2 then
lastflag = lastflag + 0x08
r = r + 1 result[r] = char(lastflag,nofflags-1)
end
-- r = r + 1 result[r] = concat(xpoints)
-- r = r + 1 result[r] = concat(ypoints)
r = r + 1 result[r] = concat(xpoints,"",1,x)
r = r + 1 result[r] = concat(ypoints,"",1,y)
end
-- can be helper or delegated to user
local stream = concat(result,"",1,r)
local length = #stream
local padding = idiv(length+3,4) * 4 - length
if padding > 0 then
-- stream = stream .. rep("\0",padding) -- can be a repeater
if padding == 1 then
padding = "\0"
elseif padding == 2 then
padding = "\0\0"
else
padding = "\0\0\0"
end
padding = stream .. padding
end
glyph.stream = stream
end
end
end
-- end of converter
local flags = { }
local function readglyph(f,nofcontours) -- read deltas here, saves space
local points = { }
-- local instructions = { }
local contours = { } -- readintegertable(f,nofcontours,short)
for i=1,nofcontours do
contours[i] = readshort(f) + 1
end
local nofpoints = contours[nofcontours]
local nofinstructions = readushort(f)
skipbytes(f,nofinstructions)
-- because flags can repeat we don't know the amount ... in fact this is
-- not that efficient (small files but more mem)
local i = 1
while i <= nofpoints do
local flag = readbyte(f)
flags[i] = flag
if band(flag,0x08) ~= 0 then
local n = readbyte(f)
if n == 1 then
i = i + 1
flags[i] = flag
else
for j=1,n do
i = i + 1
flags[i] = flag
end
end
end
i = i + 1
end
-- first come the x coordinates, and next the y coordinates and they
-- can be repeated
local x = 0
for i=1,nofpoints do
local flag = flags[i]
-- local short = band(flag,0x04) ~= 0
-- local same = band(flag,0x20) ~= 0
if band(flag,0x02) ~= 0 then
if band(flag,0x10) ~= 0 then
x = x + readbyte(f)
else
x = x - readbyte(f)
end
elseif band(flag,0x10) ~= 0 then
-- copy
else
x = x + readshort(f)
end
points[i] = { x, 0, band(flag,0x01) ~= 0 }
end
local y = 0
for i=1,nofpoints do
local flag = flags[i]
-- local short = band(flag,0x04) ~= 0
-- local same = band(flag,0x20) ~= 0
if band(flag,0x04) ~= 0 then
if band(flag,0x20) ~= 0 then
y = y + readbyte(f)
else
y = y - readbyte(f)
end
elseif band(flag,0x20) ~= 0 then
-- copy
else
y = y + readshort(f)
end
points[i][2] = y
end
return {
type = "glyph",
points = points,
contours = contours,
nofpoints = nofpoints,
}
end
local function readcomposite(f)
local components = { }
local nofcomponents = 0
local instructions = false
while true do
local flags = readushort(f)
local index = readushort(f)
----- f_words = band(flags,0x0001) ~= 0
local f_xyarg = band(flags,0x0002) ~= 0
----- f_round = band(flags,0x0006) ~= 0 -- 2 + 4
----- f_scale = band(flags,0x0008) ~= 0
----- f_reserved = band(flags,0x0010) ~= 0
----- f_more = band(flags,0x0020) ~= 0
----- f_xyscale = band(flags,0x0040) ~= 0
----- f_matrix = band(flags,0x0080) ~= 0
----- f_instruct = band(flags,0x0100) ~= 0
----- f_usemine = band(flags,0x0200) ~= 0
----- f_overlap = band(flags,0x0400) ~= 0
local f_offset = band(flags,0x0800) ~= 0
----- f_uoffset = band(flags,0x1000) ~= 0
local xscale = 1
local xrotate = 0
local yrotate = 0
local yscale = 1
local xoffset = 0
local yoffset = 0
local base = false
local reference = false
if f_xyarg then
if band(flags,0x0001) ~= 0 then -- f_words
xoffset = readshort(f)
yoffset = readshort(f)
else
xoffset = readchar(f) -- signed byte, stupid name
yoffset = readchar(f) -- signed byte, stupid name
end
else
if band(flags,0x0001) ~= 0 then -- f_words
base = readshort(f)
reference = readshort(f)
else
base = readchar(f) -- signed byte, stupid name
reference = readchar(f) -- signed byte, stupid name
end
end
if band(flags,0x0008) ~= 0 then -- f_scale
xscale = read2dot14(f)
yscale = xscale
if f_xyarg and f_offset then
xoffset = xoffset * xscale
yoffset = yoffset * yscale
end
elseif band(flags,0x0040) ~= 0 then -- f_xyscale
xscale = read2dot14(f)
yscale = read2dot14(f)
if f_xyarg and f_offset then
xoffset = xoffset * xscale
yoffset = yoffset * yscale
end
elseif band(flags,0x0080) ~= 0 then -- f_matrix
xscale = read2dot14(f)
xrotate = read2dot14(f)
yrotate = read2dot14(f)
yscale = read2dot14(f)
if f_xyarg and f_offset then
xoffset = xoffset * sqrt(xscale ^2 + xrotate^2)
yoffset = yoffset * sqrt(yrotate^2 + yscale ^2)
end
end
nofcomponents = nofcomponents + 1
components[nofcomponents] = {
index = index,
usemine = band(flags,0x0200) ~= 0, -- f_usemine
round = band(flags,0x0006) ~= 0, -- f_round,
base = base,
reference = reference,
matrix = { xscale, xrotate, yrotate, yscale, xoffset, yoffset },
}
if band(flags,0x0100) ~= 0 then
instructions = true
end
if band(flags,0x0020) == 0 then -- f_more
break
end
end
return {
type = "composite",
components = components,
}
end
-- function readers.cff(f,offset,glyphs,doshapes) -- false == no shapes (nil or true otherwise)
-- The glyf table depends on the loca table. We have one entry to much
-- in the locations table (the last one is a dummy) because we need to
-- calculate the size of a glyph blob from the delta, although we not
-- need it in our usage (yet). We can remove the locations table when
-- we're done (todo: cleanup finalizer).
function readers.loca(f,fontdata,specification)
if specification.glyphs then
local datatable = fontdata.tables.loca
if datatable then
-- locations are relative to the glypdata table (glyf)
local offset = fontdata.tables.glyf.offset
local format = fontdata.fontheader.indextolocformat
local profile = fontdata.maximumprofile
local nofglyphs = profile and profile.nofglyphs
local locations = { }
setposition(f,datatable.offset)
if format == 1 then
if not nofglyphs then
nofglyphs = idiv(datatable.length,4) - 1
end
for i=0,nofglyphs do
locations[i] = offset + readulong(f)
end
fontdata.nofglyphs = nofglyphs
else
if not nofglyphs then
nofglyphs = idiv(datatable.length,2) - 1
end
for i=0,nofglyphs do
locations[i] = offset + readushort(f) * 2
end
end
fontdata.nofglyphs = nofglyphs
fontdata.locations = locations
end
end
end
function readers.glyf(f,fontdata,specification) -- part goes to cff module
local tableoffset = gotodatatable(f,fontdata,"glyf",specification.glyphs)
if tableoffset then
local locations = fontdata.locations
if locations then
local glyphs = fontdata.glyphs
local nofglyphs = fontdata.nofglyphs
local filesize = fontdata.filesize
local nothing = { 0, 0, 0, 0 }
local shapes = { }
local loadshapes = specification.shapes or specification.instance or specification.streams
for index=0,nofglyphs-1 do
local location = locations[index]
local length = locations[index+1] - location
if location >= filesize then
report("discarding %s glyphs due to glyph location bug",nofglyphs-index+1)
fontdata.nofglyphs = index - 1
fontdata.badfont = true
break
elseif length > 0 then
setposition(f,location)
local nofcontours = readshort(f)
glyphs[index].boundingbox = {
readshort(f), -- xmin
readshort(f), -- ymin
readshort(f), -- xmax
readshort(f), -- ymax
}
if not loadshapes then
-- save space
elseif nofcontours == 0 then
shapes[index] = readnothing(f)
elseif nofcontours > 0 then
shapes[index] = readglyph(f,nofcontours)
else
shapes[index] = readcomposite(f,nofcontours)
end
else
if loadshapes then
shapes[index] = readnothing(f)
end
glyphs[index].boundingbox = nothing
end
end
if loadshapes then
if readers.gvar then
readers.gvar(f,fontdata,specification,glyphs,shapes)
end
mergecomposites(glyphs,shapes)
if specification.instance then
if specification.streams then
repackpoints(glyphs,shapes)
else
contours2outlines_shaped(glyphs,shapes,specification.shapes)
end
elseif specification.shapes then
if specification.streams then
repackpoints(glyphs,shapes)
else
contours2outlines_normal(glyphs,shapes)
end
elseif specification.streams then
repackpoints(glyphs,shapes)
end
end
end
end
end
-- gvar is a bit crazy format and one can really wonder if the bit-jugling obscurity
-- is still needed in these days .. cff is much nicer with these blends while the ttf
-- coding variant looks quite horrible
local function readtuplerecord(f,nofaxis)
local record = { }
for i=1,nofaxis do
record[i] = read2dot14(f)
end
return record
end
-- (1) the first is a real point the rest deltas
-- (2) points can be present more than once (multiple deltas then)
local function readpoints(f)
local count = readbyte(f)
if count == 0 then
-- second byte not used, deltas for all point numbers
return nil, 0 -- todo
else
if count < 128 then
-- no second byte, use count
elseif band(count,0x80) ~= 0 then
count = band(count,0x7F) * 256 + readbyte(f)
else
-- bad news
end
local points = { }
local p = 0
local n = 1 -- indices
while p < count do
local control = readbyte(f)
local runreader = band(control,0x80) ~= 0 and readushort or readbyte
local runlength = band(control,0x7F)
for i=1,runlength+1 do
n = n + runreader(f)
p = p + 1
points[p] = n
end
end
return points, p
end
end
local function readdeltas(f,nofpoints)
local deltas = { }
local p = 0
local z = 0
while nofpoints > 0 do
local control = readbyte(f)
if not control then
break
end
local allzero = band(control,0x80) ~= 0
local runlength = band(control,0x3F) + 1
if allzero then
z = z + runlength
else
local runreader = band(control,0x40) ~= 0 and readshort or readinteger
if z > 0 then
for i=1,z do
p = p + 1
deltas[p] = 0
end
z = 0
end
for i=1,runlength do
p = p + 1
deltas[p] = runreader(f)
end
end
nofpoints = nofpoints - runlength
end
-- saves space
-- if z > 0 then
-- for i=1,z do
-- p = p + 1
-- deltas[p] = 0
-- end
-- end
if p > 0 then
-- forget about trailing zeros
return deltas
else
-- forget about all zeros
end
end
local function readdeltas(f,nofpoints)
local deltas = { }
local p = 0
while nofpoints > 0 do
local control = readbyte(f)
if control then
local allzero = band(control,0x80) ~= 0
local runlength = band(control,0x3F) + 1
if allzero then
for i=1,runlength do
p = p + 1
deltas[p] = 0
end
else
local runreader = band(control,0x40) ~= 0 and readshort or readinteger
for i=1,runlength do
p = p + 1
deltas[p] = runreader(f)
end
end
nofpoints = nofpoints - runlength
else
-- it happens
break
end
end
-- saves space
if p > 0 then
return deltas
else
-- forget about all zeros
end
end
function readers.gvar(f,fontdata,specification,glyphdata,shapedata)
-- this is one of the messiest tables
local instance = specification.instance
if not instance then
return
end
local factors = specification.factors
if not factors then
return
end
local tableoffset = gotodatatable(f,fontdata,"gvar",specification.variable or specification.shapes)
if tableoffset then
local version = readulong(f) -- 1.0
local nofaxis = readushort(f)
local noftuples = readushort(f)
local tupleoffset = tableoffset + readulong(f)
local nofglyphs = readushort(f)
local flags = readushort(f)
local dataoffset = tableoffset + readulong(f)
local data = { }
local tuples = { }
local glyphdata = fontdata.glyphs
local dowidth = not fontdata.variabledata.hvarwidths
-- there is one more offset (so that one can calculate the size i suppose)
-- so we could test for overflows but we simply assume sane font files
if band(flags,0x0001) ~= 0 then
for i=1,nofglyphs+1 do
data[i] = dataoffset + readulong(f)
end
else
for i=1,nofglyphs+1 do
data[i] = dataoffset + 2*readushort(f)
end
end
--
if noftuples > 0 then
setposition(f,tupleoffset)
for i=1,noftuples do
tuples[i] = readtuplerecord(f,nofaxis)
end
end
local nextoffset = false
local startoffset = data[1]
for i=1,nofglyphs do -- hm one more cf spec
nextoffset = data[i+1]
local glyph = glyphdata[i-1]
local name = trace_deltas and glyph.name
if startoffset == nextoffset then
if name then
report("no deltas for glyph %a",name)
end
else
local shape = shapedata[i-1] -- todo 0
if not shape then
if name then
report("no shape for glyph %a",name)
end
else
lastoffset = startoffset
setposition(f,startoffset)
local flags = readushort(f)
local count = band(flags,0x0FFF)
local offset = startoffset + readushort(f) -- to serialized
local deltas = { }
local allpoints = (shape.nofpoints or 0) -- + 1
local shared = false
local nofshared = 0
if band(flags,0x8000) ~= 0 then -- has shared points
-- go to the packed stream (get them once)
local current = getposition(f)
setposition(f,offset)
shared, nofshared = readpoints(f)
offset = getposition(f)
setposition(f,current)
-- and back to the table
end
for j=1,count do
local size = readushort(f) -- check
local flags = readushort(f)
local index = band(flags,0x0FFF)
local haspeak = band(flags,0x8000) ~= 0
local intermediate = band(flags,0x4000) ~= 0
local private = band(flags,0x2000) ~= 0
local peak = nil
local start = nil
local stop = nil
local xvalues = nil
local yvalues = nil
local points = shared -- we default to shared
local nofpoints = nofshared -- we default to shared
-- local advance = 4
if haspeak then
peak = readtuplerecord(f,nofaxis)
-- advance = advance + 2*nofaxis
else
if index+1 > #tuples then
report("error, bad tuple index",index)
end
peak = tuples[index+1] -- hm, needs checking, only peak?
end
if intermediate then
start = readtuplerecord(f,nofaxis)
stop = readtuplerecord(f,nofaxis)
-- advance = advance + 4*nofaxis
end
-- get the deltas
if size > 0 then
local current = getposition(f)
-- goto the packed stream
setposition(f,offset)
if private then
points, nofpoints = readpoints(f)
end -- else
if nofpoints == 0 then
nofpoints = allpoints + 4
end
if nofpoints > 0 then
-- a nice test is to do only one
xvalues = readdeltas(f,nofpoints)
yvalues = readdeltas(f,nofpoints)
end
-- resync offset
offset = offset + size
-- back to the table
setposition(f,current)
end
if not xvalues and not yvalues then
points = nil
end
local s = 1
for i=1,nofaxis do
local f = factors[i]
local peak = peak and peak [i] or 0
-- local start = start and start[i] or 0
-- local stop = stop and stop [i] or 0
local start = start and start[i] or (peak < 0 and peak or 0)
local stop = stop and stop [i] or (peak > 0 and peak or 0)
-- do we really need these tests ... can't we assume sane values
if start > peak or peak > stop then
-- * 1
elseif start < 0 and stop > 0 and peak ~= 0 then
-- * 1
elseif peak == 0 then
-- * 1
elseif f < start or f > stop then
-- * 0
s = 0
break
elseif f < peak then
-- s = - s * (f - start) / (peak - start)
s = s * (f - start) / (peak - start)
elseif f > peak then
s = s * (stop - f) / (stop - peak)
else
-- * 1
end
end
if s == 0 then
if name then
report("no deltas applied for glyph %a",name)
end
else
deltas[#deltas+1] = {
factor = s,
points = points,
xvalues = xvalues,
yvalues = yvalues,
}
end
end
if shape.type == "glyph" then
applyaxis(glyph,shape,deltas,dowidth)
else
-- todo: args_are_xy_values mess .. i have to be really bored
-- and motivated to deal with it
shape.deltas = deltas
end
end
end
startoffset = nextoffset
end
end
end
|