1if not modules then modules = { } end modules ['font-imp-quality'] = {
2 version = 1.001,
3 comment = "companion to font-ini.mkiv and hand-ini.mkiv",
4 author = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
5 copyright = "PRAGMA ADE / ConTeXt Development Team",
6 license = "see context related readme files"
7}
8
9if not context then return end
10
11local next, type, tonumber = next, type, tonumber
12
13local fonts = fonts
14local utilities = utilities
15
16local handlers = fonts.handlers
17local otf = handlers.otf
18local afm = handlers.afm
19local registerotffeature = otf.features.register
20local registerafmfeature = afm.features.register
21
22local allocate = utilities.storage.allocate
23local getparameters = utilities.parsers.getparameters
24
25local implement = interfaces and interfaces.implement
26
27local trace_protrusion = false trackers.register("fonts.protrusion", function(v) trace_protrusion = v end)
28local trace_expansion = false trackers.register("fonts.expansion", function(v) trace_expansion = v end)
29
30local report_expansions = logs.reporter("fonts","expansions")
31local report_protrusions = logs.reporter("fonts","protrusions")
32
33
34
35
36
37local function get_class_and_vector(tfmdata,value,where)
38 local g_where = tfmdata.goodies and tfmdata.goodies[where]
39 local f_where = fonts[where]
40 local g_classes = g_where and g_where.classes
41 local f_classes = f_where and f_where.classes
42 local class = (g_classes and g_classes[value]) or (f_classes and f_classes[value])
43 if class then
44 local class_vector = class.vector
45 local g_vectors = g_where and g_where.vectors
46 local f_vectors = f_where and f_where.vectors
47 local vector = (g_vectors and g_vectors[class_vector]) or (f_vectors and f_vectors[class_vector])
48 return class, vector
49 end
50end
51
52
53
54
55
56local expansions = fonts.expansions or allocate()
57
58fonts.expansions = expansions
59
60local classes = expansions.classes or allocate()
61local vectors = expansions.vectors or allocate()
62
63expansions.classes = classes
64expansions.vectors = vectors
65
66classes.preset = {
67
68
69
70 factor = 1,
71}
72
73classes['quality'] = {
74
75
76
77 vector = 'default',
78 factor = 1,
79}
80
81vectors['default'] = {
82 [0x0041] = 0.5,
83 [0x0042] = 0.7,
84 [0x0043] = 0.7,
85 [0x0044] = 0.5,
86 [0x0045] = 0.7,
87 [0x0046] = 0.7,
88 [0x0047] = 0.5,
89 [0x0048] = 0.7,
90 [0x004B] = 0.7,
91 [0x004D] = 0.7,
92 [0x004E] = 0.7,
93 [0x004F] = 0.5,
94 [0x0050] = 0.7,
95 [0x0051] = 0.5,
96 [0x0052] = 0.7,
97 [0x0053] = 0.7,
98 [0x0055] = 0.7,
99 [0x0057] = 0.7,
100 [0x005A] = 0.7,
101 [0x0061] = 0.7,
102 [0x0062] = 0.7,
103 [0x0063] = 0.7,
104 [0x0064] = 0.7,
105 [0x0065] = 0.7,
106 [0x0067] = 0.7,
107 [0x0068] = 0.7,
108 [0x006B] = 0.7,
109 [0x006D] = 0.7,
110 [0x006E] = 0.7,
111 [0x006F] = 0.7,
112 [0x0070] = 0.7,
113 [0x0071] = 0.7,
114 [0x0073] = 0.7,
115 [0x0075] = 0.7,
116 [0x0077] = 0.7,
117 [0x007A] = 0.7,
118 [0x0032] = 0.7,
119 [0x0033] = 0.7,
120 [0x0036] = 0.7,
121 [0x0038] = 0.7,
122 [0x0039] = 0.7,
123}
124
125vectors['quality'] = vectors['default']
126
127
128
129
130local function initialize(tfmdata,value)
131 if value then
132 local class, vector = get_class_and_vector(tfmdata,value,"expansions")
133 if class then
134 if vector then
135
136
137
138 local factor = class.factor or 1
139 if trace_expansion then
140
141 report_expansions("setting class %a, vector %a, factor %a",value,class.vector,factor)
142 end
143 tfmdata.parameters.expansion = {
144
145
146
147 factor = factor,
148 class = class,
149 vector = vector,
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 elseif trace_expansion then
188 report_expansions("unknown vector %a in class %a",class.vector,value)
189 end
190 elseif trace_expansion then
191 report_expansions("unknown class %a",value)
192 end
193 end
194end
195
196local specification = {
197 name = "expansion",
198 description = "apply hz optimization",
199 initializers = {
200 base = initialize,
201 node = initialize,
202 }
203}
204
205registerotffeature(specification)
206registerafmfeature(specification)
207
208fonts.goodies.register("expansions", function(...) return fonts.goodies.report("expansions", trace_expansion, ...) end)
209
210implement {
211 name = "setupfontexpansion",
212 arguments = "2 strings",
213 actions = function(class,settings) getparameters(classes,class,'preset',settings) end
214}
215
216
217
218classes.math = {
219
220
221
222 vector = 'math',
223 factor = 1,
224}
225
226vectors.math = {
227 [0x002B] = { 4, 0 },
228 [0x2212] = { 4, 0 },
229 [0x003C] = { 8, 0 },
230 [0x003D] = { 8, 0 },
231 [0x003E] = { 8, 0 },
232 [0x002F] = { 2, 0 },
233 [0x0028] = { 2, 0 },
234 [0x0029] = { 2, 0 },
235 [0x005B] = { 2, 0 },
236 [0x005D] = { 2, 0 },
237}
238
239local function initialize(tfmdata,value)
240 if value then
241 local class, vector = get_class_and_vector(tfmdata,value,"expansions")
242 if class then
243 if vector then
244
245
246
247 local factor = class.factor or 1
248 if trace_expansion then
249
250 report_expansions("setting class %a, vector %a, factor %a",value,class.vector,factor)
251 end
252 tfmdata.parameters.expansion = {
253
254
255
256 factor = factor,
257 }
258
259 local characters = tfmdata.characters
260 for u, v in next, vector do
261 local chr = characters[u]
262 if type(v) == "table" then
263 local e = v[1]
264 local c = v[2] or 0
265 if e ~= 0 then
266 chr.expansion = e*factor
267 end
268 if c ~= 0 then
269 chr.compression = c*factor
270 end
271 elseif v ~= 0 then
272 chr.expansion = v*factor
273 end
274 end
275 elseif trace_expansion then
276 report_expansions("unknown vector %a in class %a",class.vector,value)
277 end
278 elseif trace_expansion then
279 report_expansions("unknown class %a",value)
280 end
281 end
282end
283
284registerotffeature {
285 name = "mathexpansion",
286 description = "apply hz optimization to math",
287 initializers = {
288 base = initialize,
289 node = initialize,
290 }
291}
292
293
294
295
296
297fonts.protrusions = allocate()
298local protrusions = fonts.protrusions
299
300protrusions.classes = allocate()
301protrusions.vectors = allocate()
302
303local classes = protrusions.classes
304local vectors = protrusions.vectors
305
306
307
308classes.preset = {
309 factor = 1,
310 left = 1,
311 right = 1,
312}
313
314classes['pure'] = { vector = 'pure', factor = 1 }
315classes['punctuation'] = { vector = 'punctuation', factor = 1 }
316classes['alpha'] = { vector = 'alpha', factor = 1 }
317classes['quality'] = { vector = 'quality', factor = 1 }
318
319vectors['pure'] = {
320
321 [0x002C] = { 0, 1 },
322 [0x002E] = { 0, 1 },
323 [0x003A] = { 0, 1 },
324 [0x003B] = { 0, 1 },
325 [0x002D] = { 0, 1 },
326 [0x00AD] = { 0, 1 },
327 [0x2013] = { 0, 0.50 },
328 [0x2014] = { 0, 0.33 },
329 [0x3001] = { 0, 1 },
330 [0x3002] = { 0, 1 },
331 [0x060C] = { 0, 1 },
332 [0x061B] = { 0, 1 },
333 [0x06D4] = { 0, 1 },
334
335}
336
337vectors['punctuation'] = {
338
339 [0x003F] = { 0, 0.20 },
340 [0x00BF] = { 0.20, 0 },
341 [0x0021] = { 0, 0.20 },
342 [0x00A1] = { 0.20, 0, },
343 [0x0028] = { 0.05, 0 },
344 [0x0029] = { 0, 0.05 },
345 [0x005B] = { 0.05, 0 },
346 [0x005D] = { 0, 0.05 },
347 [0x002C] = { 0, 0.70 },
348 [0x002E] = { 0, 0.70 },
349 [0x003A] = { 0, 0.50 },
350 [0x003B] = { 0, 0.50 },
351 [0x002D] = { 0, 0.70 },
352 [0x00AD] = { 0, 0.70 },
353 [0x2013] = { 0, 0.30 },
354 [0x2014] = { 0, 0.20 },
355 [0x060C] = { 0, 0.70 },
356 [0x061B] = { 0, 0.50 },
357 [0x06D4] = { 0, 0.70 },
358 [0x061F] = { 0, 0.20 },
359
360
361
362 [0x2039] = { 0.70, 0.70 },
363 [0x203A] = { 0.70, 0.70 },
364 [0x00AB] = { 0.50, 0.50 },
365 [0x00BB] = { 0.50, 0.50 },
366
367 [0x2018] = { 0.70, 0.70 },
368 [0x2019] = { 0, 0.70 },
369 [0x201A] = { 0.70, 0 },
370 [0x201B] = { 0.70, 0 },
371 [0x201C] = { 0.50, 0.50 },
372 [0x201D] = { 0, 0.50 },
373 [0x201E] = { 0.50, 0 },
374 [0x201F] = { 0.50, 0 },
375
376}
377
378vectors['alpha'] = {
379
380 [0x0041] = { .05, .05 },
381 [0x0046] = { 0, .05 },
382 [0x004A] = { .05, 0 },
383 [0x004B] = { 0, .05 },
384 [0x004C] = { 0, .05 },
385 [0x0054] = { .05, .05 },
386 [0x0056] = { .05, .05 },
387 [0x0057] = { .05, .05 },
388 [0x0058] = { .05, .05 },
389 [0x0059] = { .05, .05 },
390
391 [0x006B] = { 0, .05 },
392 [0x0072] = { 0, .05 },
393 [0x0074] = { 0, .05 },
394 [0x0076] = { .05, .05 },
395 [0x0077] = { .05, .05 },
396 [0x0078] = { .05, .05 },
397 [0x0079] = { .05, .05 },
398
399}
400
401vectors['quality'] = table.merged(
402 vectors['punctuation'],
403 vectors['alpha']
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
553local function initialize(tfmdata,value)
554 if value then
555
556
557
558
559
560 local class, vector = get_class_and_vector(tfmdata,value,"protrusions")
561 if class then
562 if vector then
563 local factor = class.factor or 1
564 local left = class.left or 1
565 local right = class.right or 1
566 if trace_protrusion then
567 report_protrusions("setting class %a, vector %a, factor %a, left %a, right %a",
568 value,class.vector,factor,left,right)
569 end
570 local data = characters.data
571 local lfactor = left * factor
572 local rfactor = right * factor
573 if trace_protrusion then
574 report_protrusions("left factor %0.3F, right factor %0.3F",lfactor,rfactor)
575 end
576 tfmdata.parameters.protrusion = {
577 factor = factor,
578 left = left,
579 right = right,
580 class = class,
581 vector = vector,
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 elseif trace_protrusion then
626 report_protrusions("unknown vector %a in class %a",class.vector,value)
627 end
628 elseif trace_protrusion then
629 report_protrusions("unknown class %a",value)
630 end
631
632 end
633end
634
635local specification = {
636 name = "protrusion",
637 description = "l/r margin character protrusion",
638 initializers = {
639 base = initialize,
640 node = initialize,
641 }
642}
643
644registerotffeature(specification)
645registerafmfeature(specification)
646
647fonts.goodies.register("protrusions", function(...) return fonts.goodies.report("protrusions", trace_protrusion, ...) end)
648
649implement {
650 name = "setupfontprotrusion",
651 arguments = "2 strings",
652 actions = function(class,settings) getparameters(classes,class,'preset',settings) end
653}
654
655local function initialize(tfmdata,value)
656 local properties = tfmdata.properties
657 local parameters = tfmdata.parameters
658 if properties then
659 value = tonumber(value)
660 if value then
661 if value < 0 then
662 value = 0
663 elseif value > 10 then
664 report_expansions("threshold for %a @ %p limited to 10 pct",properties.fontname,parameters.size)
665 value = 10
666 end
667 if value > 5 then
668 report_expansions("threshold for %a @ %p exceeds 5 pct",properties.fontname,parameters.size)
669 end
670 end
671 properties.threshold = value or nil
672 end
673end
674
675local specification = {
676 name = "threshold",
677 description = "threshold for quality features",
678 initializers = {
679 base = initialize,
680 node = initialize,
681 }
682}
683
684registerotffeature(specification)
685registerafmfeature(specification)
686
687
688
689local function addquality(id)
690 local tfmdata = fonts.hashes.identifiers[id]
691 if tfmdata then
692 local expansion = tfmdata.parameters.expansion
693 if expansion then
694 local class = expansion.class
695 local vector = expansion.vector
696 if class and vector then
697
698
699
700 local factor = class.factor or 1
701 local data = characters and characters.data
702 for i, chr in next, tfmdata.characters do
703 local v = vector[i]
704 if data and not v then
705 local d = data[i]
706 if d then
707 local s = d.shcode
708 if not s then
709
710 elseif type(s) == "table" then
711 v = ((vector[s[1]] or 0) + (vector[s[#s]] or 0)) / 2
712 else
713 v = vector[s] or 0
714 end
715 end
716 end
717
718 if v then
719 if type(v) == "table" then
720 local e = v[1]
721 local c = v[2] or 0
722 if e ~= 0 then
723 chr.expansion = e * factor * 1000
724 end
725 if c ~= 0 then
726 chr.compression = c * factor * 1000
727 end
728 elseif v == 0 then
729
730 else
731 chr.expansion = v * factor * 1000
732 end
733 elseif factor ~= 1 then
734 chr.expansion = factor * 1000
735 end
736 end
737 end
738 end
739
740 local protrusion = tfmdata.parameters.protrusion
741 if protrusion then
742 local class = protrusion.class
743 local vector = protrusion.vector
744 if class and vector then
745 local factor = class.factor or 1
746 local left = class.left or 1
747 local right = class.right or 1
748 local data = characters.data
749 local lfactor = left * factor
750 local rfactor = right * factor
751 for i, chr in next, tfmdata.characters do
752 local v = vector[i]
753 local pl = nil
754 local pr = nil
755 if v then
756 pl = v[1]
757 pr = v[2]
758 else
759 local d = data[i]
760 if d then
761 local s = d.shcode
762 if not s then
763
764 elseif type(s) == "table" then
765 local vl = vector[s[1]]
766 local vr = vector[s[#s]]
767 if vl then pl = vl[1] end
768 if vr then pr = vr[2] end
769 else
770 v = vector[s]
771 if v then
772 pl = v[1]
773 pr = v[2]
774 end
775 end
776 end
777 end
778 if pl and pl ~= 0 then
779 pl = pl * lfactor * chr.width
780 if trace_protrusion then
781 report_protrusions("left -> %0.3F %C ",pl,i)
782 end
783 chr.leftprotrusion = pl
784 end
785 if pr and pr ~= 0 then
786 pr = pr * rfactor * chr.width
787 chr.rightprotrusion = pr
788 if trace_protrusion then
789 report_protrusions("right -> %0.3F %C",pr,i)
790 end
791 end
792 end
793 end
794 end
795
796 if expansion or protrusion then
797 font.addquality(id,tfmdata)
798 end
799 end
800end
801
802callback.register("quality_font",addquality)
803 |