summaryrefslogtreecommitdiff
path: root/doc/context/presentations/context/2021/context-2021-compactfonts.tex
blob: 7d2d3e4828bf368a3311734699c257bcc3f43892 (plain)
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
% language=us

\usemodule[present-boring,abbreviations-logos]


\definehighlight[nb][style=bold,color=middlegray,define=no]

\definecolor[maincolor][g=.3]

\startdocument
  [title={COMPACT FONTS},
   banner={a different approach to scaling},
   location={context\enspace {\bf 2021}\enspace meeting}]

\starttitle[title=How \TEX\ uses fonts]

\startitemize
    \startitem
        In a \TEX\ engine there is a 1-to-1 mapping from characters in the input
        to a slot in a font.
    \stopitem
    \startitem
        Combinations of characters can be mapped onto one shape: ligatures.
    \stopitem
    \startitem
        Hyphenation is (also) controlled by the input encoding of a font which
        imposes some limitations.
    \stopitem
    \startitem
        In the typeset result characters (glyphs) can be (re)positioned relatively: kerns.
    \stopitem
    \startitem
        This all happens in a very interwoven way (e.g. inside the par builder).
    \stopitem
\stopitemize

\stoptitle

\starttitle[title=How traditional \TEX\ sees fonts]

\startitemize
    \startitem
        A font is just a (binary) blob of data with information where characters
        have a width, height, depth and italic correction.
    \stopitem
    \startitem
        The engine only needs the dimensions and when the same font is used with a
        different scale a copy with scaled dimensions is used.
    \stopitem
    \startitem
        There can be recipes for ligatures and (more complex) so called math extensible
        shapes (like arrows, wide accents, fences and radicals)
    \stopitem
    \startitem
        There are pairwise specification for kerning.
    \stopitem
    \startitem
        A handful of font parameters is provided in order to control for instance spacing.
    \stopitem
    \startitem
        Virtual fonts are just normal fonts but they have recipes for the backend to
        construct shapes: \TEX\ uses the normal metric file, the backend also the virtual
        specification file.
    \stopitem
\stopitemize

\blank[2*big]

An example of a definition and usage:

\starttyping
\font\cs somename [somescale]  test {\cs test} test
\stoptyping

\stoptitle

\starttitle[title=\UNICODE\ engines]

\startitemize
    \startitem
        There are no fundamental differences between a traditional engine and an
        \UNICODE\ aware one with respect to what \TEX\ needs from fonts:
        dimensions.
    \stopitem
    \startitem
        There can be more than 256 characters in a font.
    \stopitem
    \startitem
        Some mechanisms have to be present for applying font features (like ligaturing and
        kerning).
    \stopitem
    \startitem
        In \LUATEX\ hyphenation, ligaturing and kerning are clearly separate steps.
    \stopitem
    \startitem
        In \LUATEX\ callbacks can do lots of things with fonts and characters.
    \stopitem
    \startitem
        Math fonts are handled differently and there is more info that comes from the font.
    \stopitem
    \startitem
        In \LUATEX\ features like expansion are implemented differently, i.e.\ no
        copies of fonts are made and applied expansion travels with the glyphs.
    \stopitem
\stopitemize

\stoptitle

\starttitle[title=Overhead]

\startitemize
    \startitem
        In all engines scales copies are used. For traditional \TEX\ a copy is cheap, but a
        font that supports more of \UNICODE\ cna be quite large.
    \stopitem
    \startitem
        Different feature sets in principle make for a different font (this can be different
        per macro package).
    \stopitem
    \startitem
        Every math scale needs three font instances: text, script and scriptscript.
    \stopitem
    \startitem
        In \CONTEXT\ lots of sharing takes place and a lot of low level
        optimizations happens (memory, performance). We always made sure (already
        in \MKII) that the font system was not the bottleneck.
    \stopitem
    \startitem
        In most cases font definitions are dynamic, and we delay initialization
        as much as possible.
    \stopitem
    \startitem
        In \MKIV\ font features can be dynamic which saves a lot of memory at the
        cost of some extra processing overhead.
    \stopitem
\stopitemize

\stoptitle

\starttitle[title=Design sizes]

\startitemize
    \startitem
        A macro package's font handling is complicated by design sizes. The low level
        code can look complex too.
    \stopitem
    \startitem
        There are hardly any fonts that come in design sizes (basically only Computer
        Modern) so we need to support that.
    \stopitem
    \startitem
        Design sizes make sense for math fonts where very small characters and
        symbols are used in scripts.
    \stopitem
    \startitem
        In \OPENTYPE\ fonts smaller math design sizes are part of the font (that
        then gets loaded three times with different \type {ssty} settings).
    \stopitem
\stopitemize

\startlinecorrection
\dontleavehmode\scale[frame=on,height=5ex]{$\textstyle         2$}\quad
\dontleavehmode\scale[frame=on,height=5ex]{$\scriptstyle       2$}\quad
\dontleavehmode\scale[frame=on,height=5ex]{$\scriptscriptstyle 2$}\quad\quad
\dontleavehmode\scale[frame=on,height=2ex]{$\textstyle         2$}\quad
\dontleavehmode\scale[frame=on,height=2ex]{$\scriptstyle       2$}\quad
\dontleavehmode\scale[frame=on,height=2ex]{$\scriptscriptstyle 2$}\quad\quad
\dontleavehmode\scale[frame=on,width=4em]{\colored[r=.6,a=1,t=.5]{$\textstyle         2$}}\hskip-4em
\dontleavehmode\scale[frame=on,width=4em]{\colored[g=.6,a=1,t=.5]{$\scriptstyle       2$}}\hskip-4em
\dontleavehmode\scale[frame=on,width=4em]{\colored[b=.6,a=1,t=.5]{$\scriptscriptstyle 2$}}\quad\quad
\dontleavehmode{$\textstyle         2$}\quad
\dontleavehmode{$\scriptstyle       2$}\quad
\dontleavehmode{$\scriptscriptstyle 2$}\quad
\stoplinecorrection

\stoptitle

\starttitle[title=The system]

\startitemize
    \startitem
        Each bodyfont size (often a few are defined) has regular, bold, italic,
        bold italic, slanted, bold slanted, etc. There can be unscaled instances
        (design size) or scaled ones.
    \stopitem
    \startitem
        Within a bodyfont size we have size variants: smaller {\tx x} and {\tx xx},
        larger {\tfa a}, {\tfb b}, {\tfc c} and {\tfd d}.
    \stopitem
    \startitem
        In \MKII\ we inherit smallcap and oldstyle setups but in \MKIV\ one can either
        do that dynamic or define an additional bodyfont instance.
    \stopitem
    \startitem
        A document can use a mix of fonts mixed setup, so there is a namespace
        used per family.
    \stopitem
    \startitem
        In \OPENTYPE\ symbols are more naturally part of a font, as are emoji and
        colored shapes.
    \stopitem
    \startitem
        Lack of variants can result in artificially slanted of boldened fonts (more
        advanced in \MKIV). Variable fonts are not special, but demand some work
        when loading and embedding.
    \stopitem
    \startitem
        Runtime virtual fonts are part of \MKIV\ as are fallback shapes.
    \stopitem
    \startitem
        Languages and scripts can introduce an extra dimension to operate on.
    \stopitem
    \startitem
        Math is greatly simplified by the fact that families are part of a font.
    \stopitem
    \startitem
        Bold math fonts are provided by the boldening feature.
    \stopitem
\stopitemize

\stoptitle

\starttitle[title={Practice}]

\startitemize
    \startitem
        Due to optimizations the actual number of loaded instances is normally
        small but there are exceptions.
    \stopitem
    \startitem
        The \LUAMETATEX\ manual has 158 instances most of which are math (six per
        used bodyfont for math plus a regular: Lucida, Pagella, Latin Modern,
        Dejavu, Cambria).
    \stopitem
    \startitem
        The final \PDF\ file has 21 embedded fonts (subsets).
    \stopitem
    \startitem
        The fact that different sizes each have an instance and that for math
        we need three per size plus one for r2l, while the only difference
        is scale, makes one think of other solutions.
    \stopitem
    \startitem
        Using duplicates of huge \CJK\ fonts makes it even more noticeable.
    \stopitem
\stopitemize

But there is a way out!

\stoptitle

\starttitle[title=Glyph scaling]

We can scale glyphs in three ways:

\startbuffer
\definedfont[lmroman10-regular*default]%
test {\glyphxscale 2500 test} test
test {\glyphyscale 2500 test} test
test {\glyphscale  2500 test} test
\stopbuffer

\typebuffer {\getbuffer}

We do need to honor grouping:

\startbuffer
\definedfont[lmroman10-regular*default]%
e{\glyphxscale 2500 ff}icient
ef{\glyphxscale 2500 f}icient
ef{\glyphxscale 2500 fi}cient
e{\glyphxscale 2500 ffi}cient
\stopbuffer

\typebuffer {\getbuffer}

These scales can also be part of a font definition so we can do with less
instances.

\stoptitle

\starttitle[title=Implications]

\startitemize
    \startitem
        The text processing part of the engine has to scale on the fly (e.g.\
        when dimensions are needed in paragraph building and (box) packing.
    \stopitem
    \startitem
        Font processing (features) already distinguishes per instance but now also
        has to differentiate by (upto three) scales (we use the same font id
        for scaled instances).
    \stopitem
    \startitem
        The math machinery has to check for smaller sizes and also scale
        dimensions on the fly.
    \stopitem
    \startitem
        For math that means two times scaling: the main scale (like glyph scale) plus
        the relative scaling of script and scriptscript.
    \stopitem
    \startitem
        When they are used font dimensions and math parameters are to be scaled as are
        rules in some math extensibles.
    \stopitem
    \startitem
        This all has to be done efficiently so that there is no significant drop in
        performance.
    \stopitem
    \startitem
        But quite a bit less memory is used and loading time has become less too.
    \stopitem
\stopitemize

Some day this will become default (which means a sentimental process of dropping
ancient code):

\starttyping
\enableexperiments[fonts.compact]
\stoptyping

\stoptitle

\starttitle[title=Details]

The tricks shown on this page and following pages have been in place and tested
for a while now. Because \TEX\ uses integers a scale value of 1000 means 1.000,
as in other mechanisms:

\startbuffer
\definedfont[lmroman10-regular*default]%
test {\glyphscale 2500 test} test
\stopbuffer

\typebuffer {\getbuffer}

% \glyphtextscale
% \glyphscriptscale
% \glyphscriptscriptscale

\page

The font definition mechanism supports horizontal and vertical scaling:

\startbuffer
\definefont[FooA][Serif*default @ 12pt 1800 500]
\definefont[FooB][Serif*default @ 12pt 0.85 0.4]
\definefont[FooC][Serif*default @ 12pt]

There is also a new command:

\definetweakedfont[runwider] [xscale=1.5]
\definetweakedfont[runtaller][yscale=2.5,xscale=.8,yoffset=-.2ex]

{\FooA test test \runwider test test \runtaller test test}\par
{\FooB test test \runwider test test \runtaller test test}\par
{\FooC test test \runwider test test \runtaller test test}\par
\stopbuffer

\typebuffer

\getbuffer

\page

Tweaking also works in math:

\startbuffer
\definetweakedfont[squeezed][xscale=0.9]
\definetweakedfont[squoozed][xscale=0.7]

\startlines
$a = b^2 + \sqrt{c}$
{\squeezed $a = b^2 + \sqrt{c}$}
{\squoozed $a = b^2 + \sqrt{c}$}
{$\squoozed a = b^2 + \sqrt{c}$}
{$\scriptstyle a = b^2 + \sqrt{c}$}
\stoplines
\stopbuffer

\typebuffer

\getbuffer

\page

\startbuffer
\startcombination[3*1]
    {\bTABLE
        \bTR \bTD foo \eTD \bTD[style=\squeezed] $x = 1$ \eTD \eTR
        \bTR \bTD oof \eTD \bTD[style=\squeezed] $x = 2$ \eTD \eTR
     \eTABLE} {local}
    {\bTABLE[style=\squeezed]
        \bTR \bTD $x = 1$ \eTD \bTD  $x = 3$ \eTD \eTR
        \bTR \bTD $x = 2$ \eTD \bTD  $x = 4$ \eTD \eTR
     \eTABLE} {global}
    {\bTABLE[style=\squeezed\squeezed\squeezed\squeezed]
        \bTR \bTD $x = 1$ \eTD \bTD  $x = 3$ \eTD \eTR
        \bTR \bTD $x = 2$ \eTD \bTD  $x = 4$ \eTD \eTR
     \eTABLE} {multiple}
\stopcombination
\stopbuffer

\typebuffer

\startlinecorrection
\getbuffer
\stoplinecorrection

\page

Tweaking can be combined with styles:

\startbuffer
\definetweakedfont[MyLargerFontA][scale=2000,style=bold]
test {\MyLargerFontA test} test
\stopbuffer

\typebuffer

\getbuffer

\page

We can use negative scale values:

\startbuffer
\bTABLE[align=middle]
  \bTR
    \bTD a{\glyphxscale  1000 \glyphyscale  1000 bc}d \eTD
    \bTD a{\glyphxscale  1000 \glyphyscale -1000 bc}d \eTD
    \bTD a{\glyphxscale -1000 \glyphyscale -1000 bc}d \eTD
    \bTD a{\glyphxscale -1000 \glyphyscale  1000 bc}d \eTD
  \eTR
  \bTR
    \bTD \tttf +1000 +1000 \eTD
    \bTD \tttf +1000 -1000 \eTD
    \bTD \tttf -1000 -1000 \eTD
    \bTD \tttf -1000 +1000 \eTD
  \eTR
\eTABLE
\stopbuffer

\typebuffer

\startlinecorrection
\getbuffer
\stoplinecorrection

\page

There are more glyph properties:

\startbuffer
\ruledhbox{
    \ruledhbox{\glyph yoffset 1ex options 0 123} % left curly brace
    \ruledhbox{\glyph xoffset .5em yoffset 1ex options "C0 123}
    \ruledhbox{oeps{\glyphyoffset 1ex \glyphxscale 800
                    \glyphyscale\glyphxscale oeps}oeps}
}
\stopbuffer

\typebuffer \scale[width=\textwidth]{\getbuffer}

\page

Glyph scales are internal counter values, like any other:

\startbuffer
\samplefile{jojomayer}
{\glyphyoffset .8ex
 \glyphxscale 700 \glyphyscale\glyphxscale
 \samplefile{jojomayer}}
{\glyphyscale\numexpr3*\glyphxscale/2\relax
 \samplefile{jojomayer}}
{\glyphyoffset -.2ex
 \glyphxscale 500 \glyphyscale\glyphxscale
 \samplefile{jojomayer}}
\samplefile{jojomayer}
\stopbuffer

\typebuffer

\getbuffer

\page

There is a helper for real (float) scales:

\startbuffer
1200 : \the\numericscale1200
1.20 : \the\numericscale1.200
\stopbuffer

\typebuffer

These lines produce the same integer:

\startlines\tttf
\getbuffer
\stoplines

\page

You can do this but it's not always predictable (depends on outer scales too):

\startbuffer[definition]
\def\UnKernedTeX
  {T%
   {\glyph xoffset -.2ex yoffset -.4ex `E}%
   {\glyph xoffset -.4ex options "60 `X}}
\stopbuffer

\startbuffer[example]
We use \UnKernedTeX\ and {\bf \UnKernedTeX} and {\bs \UnKernedTeX}:
the slanted version could use some more left shifting of the E.
\stopbuffer

\typebuffer[definition,example]

\startnarrower
    \getbuffer[definition,example]
\stopnarrower

Marks and cursive features can interfere, so this is an alternative:

\startbuffer[definition]
\def\UnKernedTeX
  {T\glyph left .2ex raise -.4ex `E\glyph left .2ex `X\relax}
\stopbuffer

\typebuffer[definition]

\startnarrower
    \getbuffer[definition,example]
\stopnarrower

\page

Yet another glyph option:

\starttyping
\multiply\glyphscale by 2 <{\darkgray \glyph                      `M}>
\multiply\glyphscale by 2 <{\darkgray \glyph raise 3pt            `M}>
\multiply\glyphscale by 2 <{\darkgray \glyph raise -3pt           `M}>
\multiply\glyphscale by 2 <{\darkgray \glyph left  3pt            `M}>
\multiply\glyphscale by 2 <{\darkgray \glyph           right  2pt `M}>
\multiply\glyphscale by 2 <{\darkgray \glyph left  3pt right  2pt `M}>
\multiply\glyphscale by 2 <{\darkgray \glyph left -3pt            `M}>
\multiply\glyphscale by 2 <{\darkgray \glyph           right -2pt `M}>
\multiply\glyphscale by 2 <{\darkgray \glyph left -3pt right -2pt `M}>
\stoptyping

\startlinecorrection
\bTABLE[align=middle,width=.33\textwidth]
    \bTR
        \bTD \showglyphs \multiply\glyphscale by 2 <{\darkgray \glyph                      `M}>\eTD
        \bTD \showglyphs \multiply\glyphscale by 2 <{\darkgray \glyph raise 3pt            `M}>\eTD
        \bTD \showglyphs \multiply\glyphscale by 2 <{\darkgray \glyph raise -3pt           `M}>\eTD
    \eTR
    \bTR[frame=off]
        \bTD \tttf            \eTD
        \bTD \tttf raise  3pt \eTD
        \bTD \tttf raise -3pt \eTD
    \eTR
    \bTR
        \bTD \showglyphs \multiply\glyphscale by 2 <{\darkgray \glyph left  3pt            `M}>\eTD
        \bTD \showglyphs \multiply\glyphscale by 2 <{\darkgray \glyph           right  2pt `M}>\eTD
        \bTD \showglyphs \multiply\glyphscale by 2 <{\darkgray \glyph left  3pt right  2pt `M}>\eTD
    \eTR
    \bTR[frame=off]
        \bTD \tttf left  3pt           \eTD
        \bTD \tttf           right  2pt\eTD
        \bTD \tttf left  3pt right  2pt\eTD
    \eTR
    \bTR
        \bTD \showglyphs \multiply\glyphscale by 2 <{\darkgray \glyph left -3pt            `M}>\eTD
        \bTD \showglyphs \multiply\glyphscale by 2 <{\darkgray \glyph           right -2pt `M}>\eTD
        \bTD \showglyphs \multiply\glyphscale by 2 <{\darkgray \glyph left -3pt right -2pt `M}>\eTD
    \eTR
    \bTR[frame=off]
        \bTD \tttf left -3pt            \eTD
        \bTD \tttf           right -2pt \eTD
        \bTD \tttf left -3pt right -2pt \eTD
    \eTR
\eTABLE
\stoplinecorrection

\page

A larger example:

\startbuffer
\glyphscale 4000
\vl\glyph                        `M\vl\quad
\vl\glyph raise  .2em            `M\vl\quad
\vl\glyph left   .3em            `M\vl\quad
\vl\glyph             right  .2em`M\vl\quad
\vl\glyph left  -.2em right -.2em`M\vl\quad
\vl\glyph raise -.2em right  .4em`M\vl
\stopbuffer

\typebuffer

{\getbuffer}

\stoptitle

\stopdocument