fonts-compact.tex /size: 20 Kb    last modification: 2024-01-16 10:21
1
2% language=us runpath=texruns:manuals/fonts
3
4\continuewhenlmtxmode
5
6\startcomponent fonts-compact
7
8\environment fonts-environment
9
10\definetypeface
11  [narrowtt] [tt]
12  [mono] [modern-condensed] [default] [features=none]
13
14\startchapter[title=Compact][color=darkgray]
15
16\startsection[title=Introduction]
17
18The title of this chapter is somewhat misleading but in the end a lot boils down
19to efficient but flexible usage as offered in \CONTEXT\ \LMTX\ and \LUAMETATEX.
20This also means that what is discussed here does not apply to \MKIV.
21
22\stopsection
23
24\startsection[title=Compact mode]
25
26\startsubsection[title=What \TEX\ needs]
27
28A traditional \TEX\ approach to fonts is that you explicitly load a font
29that then gets an identifier which is used to trigger its usage. In plain \TEX\
30you find these lines:
31
32\startcolumns[n=3]
33\starttyping
34\font\tenrm     cmr10
35\font\preloaded cmr9
36\font\preloaded cmr8
37\font\sevenrm   cmr7
38\font\preloaded cmr6
39\font\fiverm    cmr5
40
41\font\teni      cmmi10
42\font\preloaded cmmi9
43\font\preloaded cmmi8
44\font\seveni    cmmi7
45\font\preloaded cmmi6
46\font\fivei     cmmi5
47
48\font\tensy     cmsy10
49\font\preloaded cmsy9
50\font\preloaded cmsy8
51\font\sevensy   cmsy7
52\font\preloaded cmsy6
53\font\fivesy    cmsy5
54
55\font\tenex     cmex10
56
57\font\preloaded cmss10
58\font\preloaded cmssq8
59
60\font\preloaded cmssi10
61\font\preloaded cmssqi8
62
63\font\tenbf     cmbx10
64\font\preloaded cmbx9
65\font\preloaded cmbx8
66\font\sevenbf   cmbx7
67\font\preloaded cmbx6
68\font\fivebf    cmbx5
69
70\font\tentt     cmtt10
71\font\preloaded cmtt9
72\font\preloaded cmtt8
73
74\font\preloaded cmsltt10
75
76\font\tensl     cmsl10
77\font\preloaded cmsl9
78\font\preloaded cmsl8
79
80\font\tenit     cmti10
81\font\preloaded cmti9
82\font\preloaded cmti8
83\font\preloaded cmti7
84\stoptyping
85\stopcolumns
86
87There are a few more later on in that file. Although users seldom see such low
88level definitions it shows a few interesting aspects. First of all, we see that
89there are different fonts for different sizes. These can be triggered with
90commands like \type {\tenbf}. We also see that some fonts get the name \type
91{\preloaded} but because later one we have:
92
93\starttyping
94\let\preloaded\undefined
95\stoptyping
96
97that command is no longer meaningful. The fonts are loaded but not accessible.
98However, when you define it again, the already loaded variant will be used which
99might save some runtime. Back to the sizes: all is centered around a ten point
100body font. The smaller sizes are for math and footnotes, the larger ones for
101section titles and such. The different sizes are optimized for that size and
102expected to be printed at that size. Of course looking at a document on a phone,
103epub device, high resolution monitor or beamer image, spoils that concept:
104instead of being optimized for some size it then becomes a variant with different
105properties. A seven point glyph at indeed seven point in a math superscript makes
106sense but when blown up to 16 point there is less need for a different shape
107unless one wants (for instance) to run the scripts narrower. The danger of ink
108filling shapes at small sizes is not present when we scale up.
109
110Anyway, the engine loads a font and when triggered it will use the dimensions
111that it provides: character properties like widths, heights, depths, italic
112corrections, kerns, ligatures, math variants and extensibles as well as font
113properties like spacing. If you load a ten point font at fifteen point the
114original gets scaled and a copy is used with these new properties. If you use
115400 sizes, you get 400 copies.
116
117\stopsubsection
118
119\startsubsection[title=How \LUATEX\ works]
120
121Because \LUATEX\ is a wide engine loading a font not only takes more time but
122using one also consumes more memory. In principle there should be no real
123difference in the amount of fonts loaded unless the macro package does a poor
124job. In what we call base mode, there is no difference with the traditional
125approach: \TEX\ needs the usual properties so, assuming that callbacks are used
126for font management, only these have to be passed to the engine. In what we call
127node mode in \CONTEXT\ rendering is delegated to \LUA. The engine is not involved
128in for instance ligature building and kerning.
129
130Where in a traditional engine small caps and old style variants are bound to
131specific fonts, using them involves loading a new instance. However, in \CONTEXT\
132we can avoid that by using dynamic features. This dynamic mechanism is part of
133node mode and is a convenient way to deal with small caps and oldstyle shapes. So
134here we can actually save some memory due to less instances but of course at the
135cost of some more complexity. But we still have dedicated instances for say eight
136point and ten point.
137
138\stopsubsection
139
140\startsubsection[title=What \LUAMETATEX\ can do]
141
142In this engine we can do scaling of glyphs on demand. This makes it possible to
143only define a ten point instance and scale for instance an eight point size from
144that instance. This can come at the cost of more runtime due to more calculations
145but in the end experiments demonstrated that runtime can become less. Memory
146consumption of course is less anyway.
147
148This scaling feature has quite some impact on the front|-|end because dimensions
149that relate to the current font might have to be scaled too. It is definitely
150something to take into account at the \LUA\ end. In addition to scaling we also
151have some other manipulations. Although these are taken into account in the
152frontend, it is the backend that eventually has to take care of it.
153
154\starttabulate
155\NC \type {\glyphscale } \NC scales the glyphs in two directions \NC \NR
156\NC \type {\glyphxscale} \NC scales the glyphs horizontally, aka extending \NC \NR
157\NC \type {\glyphyscale} \NC scales the glyphs vertically, aka squeezing \NC \NR
158\NC \type {\glyphslant } \NC transforms the glyphs such that it becomes oblique\NC \NR
159\NC \type {\glyphweight} \NC boldens the glyphs \NC \NR
160\stoptabulate
161
162Apart from slanting all these primitives result in different metrics. The weight is
163somewhat tricky because here we need some guesswork.
164
165When we use compact font mode in \CONTEXT\ \LMTX, the same font is loaded once
166and scaled on demand using the above control options. Of course this need some
167management, for instance because when setting the scale an already active scale
168might effectively get rescaled.
169
170% \unprotect
171% \meaning\font_scale_defined_x
172% \protect
173% \glyphscale\dimexpr\xtextface*\plushundred/\onepoint\relax
174% \gyphrescale\xtextface\relax
175
176\stopsubsection
177
178\stopsection
179
180\startsection[title=Design sizes]
181
182\startsubsection[title=Introduction]
183
184The first \TEX\ distributions had an important characteristic: the fonts that
185came with them were tuned for a certain size and we could speak of them having
186\quote {design sizes}. There were good reasons for this.
187
188\startitemize
189
190\startitem
191    The typeset result was supposed to be printed and we were not talking desktop
192    printers here, but printing presses. A book normally came in a ten point
193    design; after all, how many type does a typesetter has available in lead or
194    film.
195\stopitem
196
197\startitem
198    When math is involved there are two levels of script which means that there
199    are seven and five point sizes used too. At these smaller sizes some details of a
200    glyph have to be exaggerated and other bits and pieces have to become more
201    open in order not to become a blob of black ink.
202\stopitem
203
204\startitem
205    A section header or maybe a bit larger chapter title got its own size, often a
206    bit heavier and maybe running somewhat wider.
207\stopitem
208
209\startitem
210    Combined, this kind of usage results in a predictable way of presenting the
211    result to the user. Unless the reader uses a magnifying glass, what you see is
212    what you got.
213\stopitem
214
215\stopitemize
216
217Compare this to todays rendering and presentation. What is a ten point on a
218mobile phone, 1920x1080 14 inch laptop screen, a 27 inch 4K desktop monitor, or
219beamed on the wall presentation with a 3 meter diameter? And let's not bring
220these low res epub devices into the discussion. It is of course also a matter of
221taste but it is not uncommon to hear \TEX ies claim virtues of fonts and the
222rendering with \TEX\ (take expansion) while at the same time observing that they
223ignore aspects of typesetting that would really make their documents look nice.
224
225Where in the good old \TEX\ approach, rooted in traditional typesetting and
226printing, the concept of \quote {design size} made sense, I personally think that
227in today's rendering and usage it has no real meaning. A basic ten point font can
228show up in any size and is not adapted on-the-fly which in turn would demand
229on-the-fly typesetting with definitely a different look and feel due to different
230line breaks, and we don't even consider how scaling (bitmap or vector) images fit
231into such adaptations. It might work in browsers but not on more traditional
232designed and optimized published content.
233
234There is a sort of tradition that a font comes in \quote {normal}, \quote {bold},
235\quote {italic} and \quote {bolditalic} shapes. It can make sense to come up with
236a bit lighter and more heavy variant, but some designers can't make up their mind
237and come with a whole range, so we see \quote {thin}, \quote {demi-bold}, \quote
238{medium} and \quote {medium-bold}, and whatever fits the repertoire. The choice
239of what looks best is delegated to the user. In the past there definitely was a
240commercial aspect to this, after all each set has to be bought, and when it's not
241clear what to use you end up with dozens of fonts, of which most are never used.
242Today the drive probably comes from the possibilities that font design programs
243offer. And when variable fonts showed up we suddenly saw fonts come in many
244weights, again putting the burden of what looks best on users instead of what is
245intended on designers. As a side effect, the distinction between designs becomes
246vague and there is a danger that in the end users no longer really care what font
247they use because they can make it look like any other anyway.
248
249I like to argue that these \quote {new} fonts that come in what some call design
250sizes has little to do with design sizes in the traditional sense. The variants
251more reflect usage, like \quote {display} or \quote {heading}. As mentioned
252above, one can indeed consider them to be designed for (say) larger than default
253sizes, but then the word \quote {size} no longer hold.
254
255Say that in a traditional \TEX\ document one has the running text in ten point. A
256section header can use the twelve point and a chapter heading fourteen (\TEX\ has
257these 14.4pt, 17.3pt sizes). In print they really are like that but but when
258watched on a device the ten point can effectively be a eight point (epub) or
259twenty point (monitor) so there size has become meaningless because the font is
260not seen at the size it was designed for. When we choose a different one (say
261\quote {display}) we do so because of other properties than size. This means that
262instead of \quote {design size} we can better talk of \quote {usage}.
263
264The advantage of talking \quote {usage} is that we don't need to think size. In a
265traditional \TEX\ setup using design sizes you end up with (difficult) choices.
266If the document is in eleven point, do we scale down the twelve point or scale up
267the ten point, assuming that we have ten and twelve point designs optimized for
268the running text. And how about math when we go down to eight point in footnotes,
269do we really use five point script and three point script script or do we go for
270six point and five or maybe four point. Nowadays we can decide to just scale
271relative, and sticking to what was common in the days of lead typesetters with
272fixed sizes is not needed. Maybe a gradual evolution meant that we stuck to
273concepts a bit too much. So in todays \TEX\ setup we can decide to just define a
274basic setup (no design sizes at all) and do that multiple times depending on
275usage: kicking in a different set of shapes for \quote {display} and \quote
276{heading} when available. There is no reason to use a shape meant for display for
277a sixteen point running text because we scale the ten point and then use the
278display font at (say) twenty four point: it is usage and not scale that matters
279here. And an occasional ten point display font used at ten point in a ten point
280running text can have some use, but we make sure to trigger it with the right
281denomination: we mix a \quote {display} design into a \quote {regular} stream at
282the same size.
283
284It is of course also a matter of perception and taste but as with font expansion
285(aka hz) it could be annoying to see two different two's in $2^2$, something at
28610 points might look better at small sizes but worse as larger. In {figures}
287[fig:designsizes:1] \in {and} [fig:designsizes:2] you can see how it looks when
288we scale a design size beyond its intended size. Especially the second stylistic
289variant (meant for script script) can stand out.
290
291\def\SampleOne#1#2{\switchtobodyfont[#1]\scale[scale=#2000]{\im{2^{2^2}}}}
292
293\startbuffer
294\startcombination[6*3]
295    {\SampleOne{lucida} {1}} {}
296    {\SampleOne{lucida} {2}} {}
297    {\SampleOne{lucida} {3}} {}
298    {\SampleOne{lucida} {4}} {}
299    {\SampleOne{lucida} {5}} {}
300    {\SampleOne{lucida} {6}} {}
301    {\SampleOne{pagella}{1}} {}
302    {\SampleOne{pagella}{2}} {}
303    {\SampleOne{pagella}{3}} {}
304    {\SampleOne{pagella}{4}} {}
305    {\SampleOne{pagella}{5}} {}
306    {\SampleOne{pagella}{6}} {}
307    {\SampleOne{modern} {1}} {}
308    {\SampleOne{modern} {2}} {}
309    {\SampleOne{modern} {3}} {}
310    {\SampleOne{modern} {4}} {}
311    {\SampleOne{modern} {5}} {}
312    {\SampleOne{modern} {6}} {}
313\stopcombination
314\stopbuffer
315
316\startplacefigure[title={Design sizes beyond their design size (1).},reference=fig:designsizes:1]
317    \getbuffer
318\stopplacefigure
319
320\def\SampleOne#1#2{\switchtobodyfont[#1]\scale[scale=#2000]{\im{2\char\getmathcharone`2 \char\getmathchartwo`2}}}
321
322\startplacefigure[title={Design sizes beyond their design size (2).},reference=fig:designsizes:2]
323    \getbuffer
324\stopplacefigure
325
326With arbitrary scaling \quote {design size} no longer is a meaningful concept but
327(intended) usage might be; we could just drop the \quote {sizes} and stick to
328\quote {design} or maybe go for \quote {design variants} or simply \quote
329{usage}. So, that is what in \CONTEXT\ we will carry on. That still leaves the
330user the horrible task of figuring out all these weights, because in the worst
331case all the usage sets (3-5) combined with weights (3-8) will give us (9-40)
332setups to come up with and then choose from. Of course we can decide that the
333regular shape is what the designer came up with and that the rest is just
334marketing and bit of technical show-off. And if it's all too much work for a
335user, the regular shapes a good choice anyway. I wonder if readers really care
336that much, especially when they read on devices instead of proper high resolution
337print.
338
339\stopsubsection
340
341\startsubsection[title=A system]
342
343Mainly because, as shown above, \TEX\ shipped with Computer Modern in several
344design sizes the low level font system is designed to deal with that. This means
345that setups for \MKII\ look more complex than those for \MKIV\ and \LMTX,
346although deep down the same mechanism is active.
347
348Most fonts come with normal, bold, italic and bolditalic variants and for such a
349basic set of four, defining a typescript is relatively easy. If we don't need to
350access the specific fonts by name (like \typ {MyFontSerif}) we can just map
351(file)names onto \type {Serif} directly. If the \quote {select font} mechanism is
352used (\typ {\definefontfamily}) one doesn't even make typescripts but that
353assumes that we can reliably resolve fontnames.
354
355So how, in the perspective of the mentioned (usage specific) font variants does
356one set up a system. I will use Iwona as an example because that font comes in
357different variants. For that font we have the following choices:
358
359\startlines
360iwona      iwona-light      iwona-medium      iwona-heavy
361iwona-cond iwona-light-cond iwona-medium-cond iwona-heavy-cond
362\stoplines
363
364\startbuffer[iwona-use]
365\usebodyfont[iwona]
366\usebodyfont[iwona-heavy]
367\usebodyfont[iwona-cond]
368\stopbuffer
369
370Say that we know that we use, we can instruct \CONTEXT\ to preload these so that
371we don't get local definitions and loading when we use one of them grouped, as in
372a section title.
373
374\typebuffer[iwona-use]
375
376These names are a bit too bound to the fontname so we introduce some abstraction:
377
378\startbuffer[iwona-abstract]
379\aliasbodyfont[mainfont] [iwona]
380\aliasbodyfont[titlefont][iwona-heavy]
381\aliasbodyfont[notefont] [iwona-cond]
382\stopbuffer
383
384\typebuffer[iwona-abstract]
385
386\startbuffer[iwona-demo]
387\setupbodyfont[mainfont]
388\setuphead[chapter][style=\switchtobodyfont[titlefont]\tfd]
389\setuphead[section][style=\switchtobodyfont[titlefont]\tfc]
390\setupnotes[bodyfont={notefont,small}]
391
392\starttext
393
394\startchapter[title=Demo]
395\startsection[title=Demo]
396\samplefile {tufte} \footnote{\samplefile{tufte}}
397\stopsection
398\stopchapter
399
400\stoptext
401\stopbuffer
402
403\typebuffer[iwona-demo]
404
405We see the result in \in {figure} [fig:iwona:demo] and when that example is
406processed the log tells that quite some fonts got loaded: 18. The majority is
407traditional math fonts that get assembled into a pseudo \OPENTYPE\ font.
408
409\startplacefigure[default=page,title={An example font setup},reference=fig:iwona:demo]
410    \scale[width=\textwidth]{\typesetbuffer[iwona-use,iwona-abstract,iwona-demo]}
411\stopplacefigure
412
413The log also mentions: {\tt 28 instances, 24 shared in backend, 2 common vectors,
41422 common hashes, load time 0.615 seconds}. When we start the document with:
415
416\starttyping
417\enableexperiments[fonts.compact]
418\stoptyping
419
420We can see that less instances are loaded: {\tt 6 instances, 2 shared in backend,
4212 common vectors, 0 common hashes, load time 0.154 seconds}. We also save half a
422second. All this is because we now enabled scaling on-the-fly.
423
424\stopsubsection
425
426\startsubsection[title=Relations]
427
428There is a lot of history behind the font system. We started out with design
429sizes and features distributed over fonts. So, at the lowest level we had to
430handle for instance small caps and oldstyle via specific definitions. A tenpoint
431definition looked similar to a twelvepoint but with different font names. On top
432of that there were more general (and abstract) definitions as well as size
433specific mappings to larger or smaller variants of the fonts used at that size.
434
435With a single design size a setup is much simpler but it still uses that granular
436low level mechanism. In all of this, typescripts play an important role: they are
437recipes that relate symbolic names to files, and use these symbolic names in
438assemblies of fonts. Eventually a combination of serif, sans, mono and math is
439defined, even if most fonts don't have these four available. In that case we
440combine different designs.
441
442We could come up with a new system and it could even be a bit faster but we also
443need to keep compatibility in mind. It is not trivial to come up with something
444better (read: more \LMTX-ish) that is backward compatible. The default setup for
445a bodyfont is a rather safe one but not always the best for every body font.
446Without complicatign the existing mechanisms too much some extensions in for
447instance the bodyfont environment system were possible, for instance:
448
449% \setupbodyfontenvironment
450%   [metric]
451%   [
452%     small=0.7,
453%     interlinespace=1.4,
454%   ]
455
456\starttyping
457\definebodyfontenvironment[iwona]      [default][metric]
458\definebodyfontenvironment[iwona-heavy][default][metric]
459\definebodyfontenvironment[iwona-cond] [default][metric]
460\stoptyping
461
462where \type {metric} is a predefined setup and \type {default} indicates that all
463unset sizes use that one. You can define your own or adapt the predefined ones.
464
465\starttyping
466\setupbodyfontenvironment
467  [metric]
468  [small=0.7]
469\stoptyping
470
471or
472
473\starttyping
474\setupbodyfontenvironment
475  [metric]
476  [interlinespace=1.4]
477\stoptyping
478
479for a line height relative to the bodyfont size. Although this seldom happens
480you can be even mor granular. Say that you have
481
482\starttyping
483\definetypeface [DejavuA] [rm] [serif] [dejavu] [default]
484\definetypeface [DejavuA] [ss] [sans]  [dejavu] [default]
485\definetypeface [DejavuA] [tt] [mono]  [dejavu] [default]
486\definetypeface [DejavuA] [mm] [math]  [dejavu] [default]
487\stoptyping
488
489We can now make the sans a bit smaller:
490
491\starttyping
492\definebodyfontenvironment
493  [DejavuA] [ss] [default]
494  [text=0.95, ...]
495\stoptyping
496
497and a specific bodyfont size even more:
498
499\starttyping
500\definebodyfontenvironment
501  [DejavuA] [ss] [8pt]
502  [text=0.85, ...]
503\stoptyping
504
505Of course this kind of setups can best be achieved with an isolated, stepwise
506written environment file because testing it on a huge and versatile document
507source is not much fun.
508
509\stopsubsection
510
511% TODO: set tx to be some other bodyfont but with same interlinespace
512
513\stopsection
514
515\stopchapter
516
517\stopcomponent
518