fonts-methods.tex /size: 10 Kb    last modification: 2021-10-28 13:50
1% language=us runpath=texruns:manuals/fonts
2
3\startcomponent fonts-methods
4
5\environment fonts-environment
6
7\startchapter[title=Methods][color=darkyellow]
8
9\startsection[title=Introduction]
10
11A font definition looks as follows:
12
13\starttyping
14\definefont
15  [MyFont]
16  [namepart method specification size]
17\stoptyping
18
19For example:
20
21\starttyping
22\definefont
23  [MyFont]
24  [Bold*default at 12.3pt]
25\stoptyping
26
27We have already discussed the namepart and size in a previous chapter and here
28we will focus on the method. The method is represented by a character and although
29we currently only have a few methods there can be many more.
30
31\stopsection
32
33\startsection[title=: (direct features)]
34
35This one is seldom used, but those coming from another macro package to \CONTEXT\
36might use it as first attempt to defining a font.
37
38\starttyping
39\definefont
40  [MyFont]
41  [Bold:+kern;+liga; at 12.3pt]
42\stoptyping
43
44This is the \XETEX\ way of defining fonts. A \type {+} means as much as
45\quotation {turn on this feature} so you can guess what the minus sign does.
46Alternatively you can use a key/value approach with semicolons as separator. If
47no value is given the value \type {yes} is assumed.
48
49\starttyping
50\definefont
51  [MyFont]
52  [Bold:kern=yes;liga=yes; at 12.3pt]
53\stoptyping
54
55When we started supporting \XETEX\ we ran into issues with already present
56features of \CONTEXT\ as the \XETEX\ syntax also has some more obscure properties
57using slashes and brackets for signalling a file or name lookup. As in \CONTEXT\
58we prefer a more symbolic approach anyway, it never was a real issue.
59
60\stopsection
61
62\startsection[title=* (symbolic features)]
63
64The most natural way to associate a set of features with a font instance
65is the following:
66
67\starttyping
68\definefont
69  [MyFont]
70  [Bold*default at 12.3pt]
71\stoptyping
72
73This will use the featureset named \type {default} and this one is defined in
74\type {font-pre.mkiv} which might be worth looking at.
75
76\starttyping
77\definefontfeature
78  [always]
79  [mode=auto,
80   script=auto,
81   kern=yes,
82   mark=yes,
83   mkmk=yes,
84   curs=yes]
85
86\definefontfeature
87  [default]
88  [always]
89  [liga=yes,
90   tlig=yes,
91   trep=yes] % texligatures=yes,texquotes=yes
92
93\definefontfeature
94  [smallcaps]
95  [always]
96  [smcp=yes,
97   tlig=yes,
98   trep=yes] % texligatures=yes,texquotes=yes
99
100\definefontfeature
101  [oldstyle]
102  [always]
103  [onum=yes,
104   liga=yes,
105   tlig=yes,
106   trep=yes] % texligatures=yes,texquotes=yes
107
108\definefontfeature % == default unless redefined
109  [ligatures]
110  [always]
111  [liga=yes,
112   tlig=yes,
113   trep=yes]
114
115\definefontfeature % can be used for type1 fonts
116  [complete]
117  [always]
118  [compose=yes,
119   liga=yes,
120   tlig=yes,
121   trep=yes]
122
123\definefontfeature
124  [none]
125  [mode=none,
126   features=no]
127\stoptyping
128
129These definitions show that you can construct feature sets on top of existing
130ones, but keep in mind that they are defined instantly, so any change in the
131parent is not reflected in its kids.
132
133In a font definition you can specify more than one set:
134
135\starttyping
136\definefont
137  [MyFont]
138  [Bold*always,oldstyle at 12.3pt]
139\stoptyping
140
141\stopsection
142
143\startsection[title=@ (virtual features)]
144
145This method is somewhat special as it demands knowledge of the internals
146of the \CONTEXT\ font code. Much of it is still experimental but it is a
147nice playground. A good example of its usage can be found in the file
148\type {m-punk.mkiv} where we create a font out of \METAPOST\ graphics.
149
150Another example is virtual math. As in the beginning of \LUATEX\ and \MKIV\
151there were only a few \OPENTYPE\ math fonts, and as I wanted to get rid
152of the old mechanisms, it was decided to virtualize the math fonts. For
153instance a Latin Modern Roman 10 point math font can be defined as follows:
154
155\starttyping
156\definefontsynonym
157  [LMMathRoman10-Regular]
158  [LMMath10-Regular@lmroman10-math]
159\stoptyping
160
161The \type {lmroman10-math} refers to a virtual definition and in this case
162it is one using a built|-|in constructor and therefore we use a goodies
163file to specify the font. That file looks as follows:
164
165\starttyping
166return {
167  name = "lm-math",
168  version = "1.00",
169  comment = "Goodies that complement latin modern math.",
170  author = "Hans Hagen",
171  copyright = "ConTeXt development team",
172  mathematics = {
173    ...
174    virtuals = {
175      ...
176      ["lmroman10-math"] = ten,
177      ...
178    },
179    ...
180  }
181}
182\stoptyping
183
184Here \type {ten} is a previously defined table:
185
186\startnarrowtyping
187local ten = {
188  { name = "lmroman10-regular.otf", features = "virtualmath", main = true },
189  { name = "rm-lmr10.tfm", vector = "tex-mr-missing" } ,
190  { name = "lmmi10.tfm", vector = "tex-mi", skewchar = 0x7F },
191  { name = "lmmi10.tfm", vector = "tex-it", skewchar = 0x7F },
192  { name = "lmsy10.tfm", vector = "tex-sy", skewchar = 0x30, parameters = true } ,
193  { name = "lmex10.tfm", vector = "tex-ex", extension = true } ,
194  { name = "msam10.tfm", vector = "tex-ma" },
195  { name = "msbm10.tfm", vector = "tex-mb" },
196  { name = "stmary10.afm", vector = "tex-mc" },
197  { name = "lmroman10-bold.otf", vector = "tex-bf" } ,
198  { name = "lmmib10.tfm", vector = "tex-bi", skewchar = 0x7F } ,
199  { name = "lmsans10-regular.otf", vector = "tex-ss", optional = true },
200  { name = "lmmono10-regular.otf", vector = "tex-tt", optional = true },
201  { name = "eufm10.tfm", vector = "tex-fraktur", optional = true },
202  { name = "eufb10.tfm", vector = "tex-fraktur-bold", optional = true },
203}
204\stopnarrowtyping
205
206This says as much as: take \type {lmroman10-regular.otf} as starting point
207and overload slots with ones found in the following fonts. The vectors are
208predefined as they are shared with other font sets like \type {px} and
209\type {tx}.
210
211In due time more virtual methods might end up in \CONTEXT\ because they are
212a convenient way to extend or manipulate fonts.
213
214\stopsection
215
216\startsection[title=Lua fonts]
217
218You can define a font in \LUA. In the process you can use all kind of helper
219functions that \CONTEXT\ provides. Here is an example:
220
221\typefile{fonts-demo-rule.lua}
222
223This code is stored in \type {fonts-demo-rule.lua} and we can load that font
224in the usual way, by specifying a filename:
225
226\startbuffer
227\definefont
228  [MyRuleFont]
229  [file:fonts-demo-rule.lua*default sa 1]
230\stopbuffer
231
232\typebuffer
233
234\getbuffer
235
236{\MyRuleFont So when we use it we get text typeset where all vowels are replaced
237by rules. The actualtext injection (in theory) makes it possible to cut and paste
238the text from the \PDF\ document but while writing this (mid 2016) a {\maincolor
239\type {mupdf}} based viewer couldn't handle it and {\maincolor \type {acrobat}}
240had problems with spaces.}
241
242\startbuffer
243\definefontfeature
244  [myrulefont]
245  [default]
246  [original=file:texgyrepagella-regular.otf]
247\definefont
248  [MyRuleFont]
249  [file:fonts-demo-rule.lua*myrulefont]
250\stopbuffer
251
252\typebuffer
253
254\getbuffer
255
256{\showfontkerns \MyRuleFont The previous code demonstrates how we can pass a
257fontname to be used as base to the generator. In case you wonder how features
258behave with such fonts: as you can see here, font kerns are indeed injected.
259Compared to {\maincolor \type {Dejavu}}, the {\maincolor \type {Pagella}} font
260has quite some more kerns.}
261
262\startbuffer
263\definefontfeature
264  [myrulefont]
265  [default]
266  [original=file:texgyrepagella-regular.otf,
267   option=line]
268\definefont
269  [MyRuleFont]
270  [file:fonts-demo-rule.lua*myrulefont]
271\stopbuffer
272
273\typebuffer
274
275\getbuffer
276
277{\MyRuleFont Here we show how the passed {\maincolor \type {option}} is handled.
278Because we no longer have a relationship with the height and depth, the real text
279is a bit harder to guess.}
280
281\stopsection
282
283\startsection[title=Old fuzzy fonts]
284
285Most natural is to use \OPENTYPE\ or \TYPEONE\ fonts. In the case of \TYPEONE\ a
286matching pair of \type {afm} and \type {pfb} files is needed. However, there can
287be situations where there is only a \type{tfm} and \type {pfb} file (or not even
288that: just a bitmap file).
289
290I will not show specimen here, simply because I don't have (nor want to have) the
291fonts needed in my development and production environments. The implementation
292was tested with a specific czech computer modern font.
293
294In a traditional (8 bit) setup we have an \type {tfm} file, a \type {pfb} file
295and a \type {enc file}. The order of the characters in the \type {tfm} file
296directly relates to the input encoding. The \type {enc} file relates that order
297to the order in the \type {pfb} file. The mapping from input encoding to font
298shape encoding happens via glyph names. In the \type {map} file we tell what
299\type {pfb} file to use with what \type {enc} file.
300
301However, in the case of the \type {csr.tfm} and \type {csr.pfb} file it looks
302like in practice the \type {enc} file is not used, probably because in the \type
303{pfb} file the standard encoding matches the order in the \type {tfm} file. This
304is of course a rather dangerous assumption, especially if information lacks to
305check it.
306
307The next example definitions demonstrate several paths to go from \UNICODE\ input
308(source file) to rendered shapes. As this is mostly meant for generic usage we
309use the low level definition code (\CONTEXT\ users are not supposed to use that
310method).
311
312\starttyping
313\font\foo=file:csr10.tfm:reencode=auto;mode=node;liga=yes;kern=yes
314\stoptyping
315
316This is the easiest way. We use the \type {tfm} file for dimensions, ligatures
317and kerns. The \type {auto} option will use the \type {pfb} file to identify the
318right mapping. We enable ligatures and kerns and we use node mode. This indicates
319that we're dealing with a pseudo \OPENTYPE\ setup here. You can provide a \type
320{pfb} file with the \type {pfbfile} feature in case the name differes from the
321\type {tfm} file.
322
323\starttyping
324\font\foo=file:csr10.tfm:reencode=csr.enc;mode=node;liga=yes;kern=yes
325\stoptyping
326
327Now we use the \type {enc} file for the encoding vector but we still need the
328\type {pfb} file for mapping that onto the right shape. You probably can best use
329\type {auto} instead.
330
331\starttyping
332\font\foo=file:csr10.tfm:reencode=csr.enc;bitmap=yes;mode=node;liga=yes;kern=yes
333\stoptyping
334
335Here we force bitmap shapes. This is a bit tricky as a different code path is
336followed in the backend. Unless the situation is too confusing, a proper \type
337{ToUnicode} is included in the output, so that cut and paste works all right,
338given that the viewer is able to deal with it (always use \ACROBAT\ as
339reference).
340
341Why do we need modes and|/|or to simulate \OPENTYPE\ behaviour? Indeed it seldom
342makes sense with \type {tfm} files but in this particular case teh font has a
343quote cheat.
344
345\starttyping
346\startluacode
347    fonts.handlers.otf.addfeature {
348        name = "czechdqcheat",
349        type = "substitution",
350        data = {
351            quotedblright = "csquotedblright",
352        },
353    }
354\stopluacode
355\stoptyping
356
357We could make this a language specific feature but as this font is not meant for
358other languages it makes no sense to do so. This feature is enabled with:
359
360\starttyping
361czechdqcheat=yes
362\stoptyping
363
364This will replace one quote by another with different side bearings. Of course a
365properly bounded quote with proper kerning makes much more sense. A test case is:
366
367\starttyping
368\quotedblleft  X\quotedblright
369\quotedblright X\quotedblleft
370\stoptyping
371
372\stopsection
373
374\stopchapter
375
376\stopcomponent
377