fonts-lookups.tex /size: 15 Kb    last modification: 2021-10-28 13:50
1% language=us runpath=texruns:manuals/fonts
2
3\startcomponent fonts-lookups
4
5\environment fonts-environment
6
7\startchapter[title=Lookups][color=darkblue]
8
9\startsection[title=Introduction]
10
11In traditional \TEX\ a font is defined by referring to its filename. A
12definition looks like this:
13
14\starttyping
15\font \MyFontA = lmr10
16\font \MyFontB = lmr10 at 20pt
17\font \MyFontC = lmr10 scaled 1500
18\stoptyping
19
20The first definition defines the command \type {MyFontA} as a reference to the
21font stored in the file \type {lmx10}. No scaling takes place so the natural size
22is taken. This so called designsize is in no way standardized. Just look at these
23three specimen:
24
25\startlines
26{\definedfont[file:dejavuserif*default]Design Size (Dejavu)}
27{\definedfont[file:cambria*default]Design Size (Cambria)}
28{\definedfont[file:lmroman10-regular*default]Design Size (Latin Modern)}
29\stoplines
30
31The designsize is normally 10 point, but as there is no real reference for this a
32designer decides how to translate this into a visual representation. As a
33consequence the \type {20pt} in the second line of the example definitions only
34means that the font is scaled to (normally) twice the designsize. The third line
35scaled by a factor \type {1.5} and the reason for using a value thousand times
36larger is that \TEX's numbers are integers.
37
38The next three lines are typical for Latin Modern (derived from Computer Modern)
39because this family comes in different design sizes.
40
41\starttyping
42\font \MyFontD = lmr12
43\font \MyFontE = lmr12 at 20pt
44\font \MyFontF = lmr12 scaled 1500
45\stoptyping
46
47Because the designsize is part of the font metrics the second line (\type
48{\MyFontE}) is of similar size as \type {\MyFontB} although the 12~point variant
49is visually better suited for scaling up.
50
51These definitions refer to files, but what file? What gets loaded is the file
52with name \type {name.tfm}. Eventually for embedding in the (let's assume \PDF)
53file the outlines are taken from \type {name.pfb}. At that stage, when present, a
54\type {name.vf} is consulted in order to resolve characters that are combinations
55of others (potentially from other \type {pfb} files). The mapping from \type
56{name.tfm} to \type {name.pfb} filename happens in the so called map file. This
57means that one can also refer to another file, for instance \type {name.ttf}.
58
59All this logic is hard coded in the engine and because the virtual font mechanism
60was introduced later without extending the \type {tfm} format, it can be hard at
61times to figure out issues when a (maybe obsolete) virtual file is present (this
62can be the case if you have generated the \type {tfm} file from an \type {afm}
63file that comes with the \type {pfb} file when you buy one.
64
65But, in \LUATEX\ we no longer use traditional fonts and as a consequence we have
66more options open. Before we move on to them, we mention yet another definition:
67
68\starttyping
69\font \MyFontG = lmr12 sa 1.2
70\stoptyping
71
72This method is not part of \TEX\ but is provided by \CONTEXT, \MKII\ as well as
73\MKIV. It means as much as \quotation {scale this font to 1.2 times the
74bodyfontsize}. As this involves parsing the specification, it does not work
75as advertised here, but the next definition works okay:
76
77\starttyping
78\definefont[MyFontG][lmr12 sa 1.2]
79\stoptyping
80
81This indicates that we already had a parser for font specifications on board
82which in turn made it relatively easy to do even more parsing, for instance for
83font features as introduced in \XETEX\ and \LUATEX.
84
85\stopsection
86
87\startsection[title=Specifications]
88
89In \LUATEX\ we intercept the font loader. We do so for several reasons.
90
91\startitemize[packed]
92
93\startitem We want to make decisions on what file to load, this is needed when
94for instance there are files with the same name but different properties. \stopitem
95
96\startitem We want to be able to lookup by file, by name, and by more abstract specification.
97In doing so, we want to be as tolerant as possible. \stopitem
98
99\startitem We want to support several scaling methods, as discussed in the previous section. \stopitem
100
101\startitem We want to implement several strategies for passing features and defining non
102standard approaches. \stopitem
103
104\stopitemize
105
106The formal specification of a font is as follows:
107
108\starttyping
109\definefont[PublicReference][filename]
110\definefont[PublicReference][filename at dimension]
111\definefont[PublicReference][filename scaled number]
112\stoptyping
113
114We already had that extended to:
115
116\starttyping
117\definefont[PublicReference][filename]
118\definefont[PublicReference][filename at dimension]
119\definefont[PublicReference][filename scaled number]
120\definefont[PublicReference][filename sa number]
121\stoptyping
122
123So let's generalize that to:
124
125\starttyping
126\definefont[PublicReference][filename scaling]
127\stoptyping
128
129And in \MKIV\ we now have:
130
131\starttyping
132\definefont[PublicReference][filename*featurenames scaling]
133\definefont[PublicReference][filename:featurespecication scaling]
134\definefont[PublicReference][filename@virtualconstructor scaling]
135\stoptyping
136
137The second variant is seldom used and is only provided because some users
138have fonts defined in the \XETEX\ way. Users are advised not to use this
139method. The last method is special in the sense that it's used to define
140fonts that are constructed using the built in virtual font constructors. This
141method is for instance used for defining virtual math fonts.
142
143The first method is what we use most. It is really important not to forget the
144feature specification. A rather safe bet is \type {*default}. In a next chapter
145we will discuss the difference between these two; here we focus on the name part.
146
147The \type {filename} is in fact a symbolic name. In \CONTEXT\ we have always used
148an indirect reference to fonts. Look at this:
149
150\starttyping
151\definefont[TitleFont][SerifBold*default sa 2]
152\stoptyping
153
154A reference like \type {SerifBold} makes it possible to define styles independent
155of the chosen font family. This reference eventually gets resolved to a real
156name and there can be a chain of references.
157
158Font definitions can be grouped into a larger setup using typescripts. In that
159case, we can set the features for a regular, italic, bold and bolditalic for the
160whole set but when a fontname has a specific feature associated (as in the
161previous examples) that one takes precedence.
162
163so far we talked about fonts being files, but in practice a lookup happens by
164file as well as by name as known to the system. In the next section this will be
165explained in more detail.
166
167\stopsection
168
169\startsection[title=File]
170
171You can force a file lookup with:
172
173\starttyping
174\definefont[TitleFont][file:somefilename*default sa 2]
175\stoptyping
176
177If you use more symbolic names you can use the \type {file:} prefix in the
178mapping:
179
180\starttyping
181\definefontsynonym[SerifBold][file:somefile]
182\definefont[TitleFont][SerifBold*default sa 2]
183\stoptyping
184
185In projects that are supposed to run for a long time I always use the file based
186lookup, because filenames tend to be rather stable. Also, as the lookup happens
187in the \TEX\ directory structure, file lookups will rely on the general file
188search routines. This has the benefit that case is ignored. When no match is found
189the lookup will also use the font name database. Spaces and special characters are
190ignored.
191
192The name alone is not enough as there can be similar filenames with different
193suffixes. Therefore the lookup will happen in the order \ctxcommand {
194concatcommalist { list = fonts.readers.sequence, separator = ", ", last = " and
195", command = "type" } }. You can force a lookup by being more explicit, like:
196
197\starttyping
198\definefont[TitleFont][file:somefilename.ttf*default sa 1]
199\stoptyping
200
201\stopsection
202
203\startsection[title=Name]
204
205Say that we want to use a Dejavu font and that instead of filenames we want to
206use its given name. The best way to find out what is available is to call for a
207list:
208
209\starttyping
210mtxrun --script font --list --all dejavu
211\stoptyping
212
213This produces the following list:
214
215\startnarrowtyping
216dejavusans                     dejavusans                     dejavusans.ttf
217dejavusansbold                 dejavusansbold                 dejavusans-bold.ttf
218dejavusansboldoblique          dejavusansboldoblique          dejavusans-boldoblique.ttf
219dejavusanscondensed            dejavusanscondensed            dejavusanscondensed.ttf
220dejavusanscondensedbold        dejavusanscondensedbold        dejavusanscondensed-bold.ttf
221dejavusanscondensedboldoblique dejavusanscondensedboldoblique dejavusanscondensed-boldoblique.ttf
222dejavusanscondensednormal      dejavusanscondensed            dejavusanscondensed.ttf
223dejavusanscondensedoblique     dejavusanscondensedoblique     dejavusanscondensed-oblique.ttf
224dejavusansextralight           dejavusansextralight           dejavusans-extralight.ttf
225dejavusanslight                dejavusansextralight           dejavusans-extralight.ttf
226dejavusansmono                 dejavusansmono                 dejavusansmono.ttf
227dejavusansmonobold             dejavusansmonobold             dejavusansmono-bold.ttf
228dejavusansmonoboldoblique      dejavusansmonoboldoblique      dejavusansmono-boldoblique.ttf
229dejavusansmononormal           dejavusansmonooblique          dejavusansmono-oblique.ttf
230dejavusansmonooblique          dejavusansmonooblique          dejavusansmono-oblique.ttf
231dejavusansnormal               dejavusans                     dejavusans.ttf
232dejavusansoblique              dejavusansoblique              dejavusans-oblique.ttf
233dejavuserif                    dejavuserif                    dejavuserif.ttf
234dejavuserifbold                dejavuserifbold                dejavuserif-bold.ttf
235dejavuserifbolditalic          dejavuserifbolditalic          dejavuserif-bolditalic.ttf
236dejavuserifcondensed           dejavuserifcondensed           dejavuserifcondensed.ttf
237dejavuserifcondensedbold       dejavuserifcondensedbold       dejavuserifcondensed-bold.ttf
238dejavuserifcondensedbolditalic dejavuserifcondensedbolditalic dejavuserifcondensed-bolditalic.ttf
239dejavuserifcondenseditalic     dejavuserifcondenseditalic     dejavuserifcondensed-italic.ttf
240dejavuserifcondensednormal     dejavuserifcondensed           dejavuserifcondensed.ttf
241dejavuserifitalic              dejavuserifitalic              dejavuserif-italic.ttf
242dejavuserifnormal              dejavuserif                    dejavuserif.ttf
243\stopnarrowtyping
244
245The first two columns mention the names that we can use to access a font. These
246are normalized names in the sense that we only kept letters and numbers. The next three
247definitions are equivalent:
248
249\starttyping
250\definefont[TitleFont][name:dejavuserif*default sa 1]
251\definefont[TitleFont][name:dejavuserifnormal*default sa 1]
252\definefont[TitleFont][name:dejavuserif.ttf*default sa 1]
253\stoptyping
254
255In the list you see two names that all point to \type {dejavusans-extralight.ttf}:
256
257\starttyping
258dejavusansextralight
259dejavusanslight
260\stoptyping
261
262There are some heuristics built into \CONTEXT\ and we do some cleanup as well.
263For instance we interpret \type {ital} as \type {italic}. In a font there is
264sometimes information about the weight and we look at those properties as well.
265Unfortunately font names (even within a collection) are often rather inconsistent
266so you still need to know what you're looking for. The more explicit you are, the
267less change of problems.
268
269\stopsection
270
271\startsection[title=Spec]
272
273There is often some logic in naming fonts but it's not robust and really depends on
274how consistent a font designer or typefoundry has been. In \CONTEXT\ we can
275access names by using a normalized scheme.
276
277\starttyping
278name-weight-style-width-variant
279\stoptyping
280
281The following values are valid:
282
283\starttabulate[|Bl|Tp|]
284\NC weight  \NC \ctxcommand { concatcommalist { list = fonts.names.knownweights  } } \NC \NR
285\NC style   \NC \ctxcommand { concatcommalist { list = fonts.names.knownstyles   } } \NC \NR
286\NC width   \NC \ctxcommand { concatcommalist { list = fonts.names.knownwidths   } } \NC \NR
287\NC variant \NC \ctxcommand { concatcommalist { list = fonts.names.knownvariants } } \NC \NR
288\stoptabulate
289
290The four specifiers are optional but the more you provide, the better the match. Let's
291give an example:
292
293\starttyping
294mtxrun --script fonts --list --spec dejavu
295\stoptyping
296
297This reports:
298
299\startnarrowtyping
300dejavuserifcondensed normal normal normal normal dejavuserifcondensed dejavuserifcondensed.ttf
301dejavuserif          normal normal normal normal dejavuserif          dejavuserif.ttf
302dejavusansmono       normal normal normal normal dejavusansmono       dejavusansmono.ttf
303dejavusanscondensed  normal normal normal normal dejavusanscondensed  dejavusanscondensed.ttf
304dejavusans           normal normal normal normal dejavusans           dejavusans.ttf
305\stopnarrowtyping
306
307We can be more specific, for instance:
308
309\starttyping
310mtxrun --script fonts --list --spec dejavu-bold
311\stoptyping
312
313\startnarrowtyping
314dejavuserif    bold normal normal normal dejavuserifbold    dejavuserif-bold.ttf
315dejavusansmono bold normal normal normal dejavusansmonobold dejavusansmono-bold.ttf
316dejavusans     bold normal normal normal dejavusansbold     dejavusans-bold.ttf
317\stopnarrowtyping
318
319We add another specifier:
320
321\starttyping
322mtxrun --script fonts --list --spec dejavu-bold-italic
323\stoptyping
324
325\startnarrowtyping
326dejavuserif    bold italic normal normal dejavuserifbolditalic     dejavuserif-bolditalic.ttf
327dejavusansmono bold italic normal normal dejavusansmonoboldoblique dejavusansmono-boldoblique.ttf
328dejavusans     bold italic normal normal dejavusansboldoblique     dejavusans-boldoblique.ttf
329\stopnarrowtyping
330
331As the first hit is used we need to be more specific with respect to the
332name, so lets do that in an example definition:
333
334\starttyping
335\definefont[TitleFont][spec:dejavuserif-bold-italic*default sa 1]
336\stoptyping
337
338Watch the prefix \type {spec}. Wolfgang Schusters \type {simplefonts} module
339nowadays uses this method to define sets of fonts based on a name only
340specification. Of course that works best if a fontset has well defined
341properties.
342
343\stopsection
344
345\startsection[title=Selectfont]
346
347The selectfont interface by Wolfgang Schuster can be used to define fonts
348by name. For a long term project workflow you probably want to use filenames
349but for average use names do well:
350
351\starttyping
352\definefontfamily [mainface] [serif] [DejaVu Serif]
353\definefontfamily [mainface] [sans]  [DejaVu Sans]
354\definefontfamily [mainface] [mono]  [DejaVu Sans Mono] [features=none]
355\definefontfamily [mainface] [math]  [Dejavu Math]
356\stoptyping
357
358This setup is triggered in the usual way:
359
360\starttyping
361\setupbodyfont[mainface]
362\stoptyping
363
364When you combine different designs you may need to apply a relative scale:
365
366\starttyping
367\definefontfamily [mainface] [math]  [XITS Math] [rscale=1.1]
368\stoptyping
369
370Some fonts come in designsizes, like Latin Modern:
371
372\starttyping
373\definefontfamily
374  [mainface]
375  [serif]
376  [Latin Modern Roman]
377  [designsize=auto]
378\stoptyping
379
380You can define fallbacks, for example:
381
382\starttyping
383\definefallbackfamily
384  [mainface]
385  [serif]
386  [DejaVu Serif]
387  [range=cyrillic]
388
389\definefontfamily [mainface] [serif] [TeX Gyre Pagella]
390\stoptyping
391
392Here Pagella is used with missing characters taken from Dejavu. The ranges
393are defined with:
394
395\starttyping
396\definefontfamilypreset
397  [range:cyrillic]
398  [range={cyrillic,
399          cyrillicextendeda,
400          cyrillicextendedb,
401          cyrillicsupplement}]
402\stoptyping
403
404For more details you can consult the wiki and the source file \type {font-sel.mkvi}.
405
406\stopsection
407
408\stopchapter
409
410\stopcomponent
411