summaryrefslogtreecommitdiff
path: root/doc/context/sources/general/fonts/fonts/fonts-methods.tex
blob: eac86d69487eb3bc84166ba5e6a83ba2030e2a42 (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
% language=uk

\startcomponent fonts-methods

\environment fonts-environment

\startchapter[title=Methods][color=darkyellow]

\startsection[title=Introduction]

A font definition looks as follows:

\starttyping
\definefont
  [MyFont]
  [namepart method specification size]
\stoptyping

For example:

\starttyping
\definefont
  [MyFont]
  [Bold*default at 12.3pt]
\stoptyping

We have already discussed the namepart and size in a previous chapter and here
we will focus on the method. The method is represented by a character and although
we currently only have a few methods there can be many more.

\stopsection

\startsection[title=: (direct features)]

This one is seldom used, but those coming from another macro package to \CONTEXT\
might use it as first attempt to defining a font.

\starttyping
\definefont
  [MyFont]
  [Bold:+kern;+liga; at 12.3pt]
\stoptyping

This is the \XETEX\ way of defining fonts. A \type {+} means as much as
\quotation {turn on this feature} so you can guess what the minus sign does.
Alternatively you can use a key/value approach with semicolons as separator. If
no value is given the value \type {yes} is assumed.

\starttyping
\definefont
  [MyFont]
  [Bold:kern=yes;liga=yes; at 12.3pt]
\stoptyping

When we started supporting \XETEX\ we ran into issues with already present
features of \CONTEXT\ as the \XETEX\ syntax also has some more obscure properties
using slashes and brackets for signalling a file or name lookup. As in \CONTEXT\
we prefer a more symbolic approach anyway, it never was a real issue.

\stopsection

\startsection[title=* (symbolic features)]

The most natural way to associate a set of features with a font instance
is the following:

\starttyping
\definefont
  [MyFont]
  [Bold*default at 12.3pt]
\stoptyping

This will use the featureset named \type {default} and this one is defined in
\type {font-pre.mkiv} which might be worth looking at.

\starttyping
\definefontfeature
  [always]
  [mode=auto,
   script=auto,
   kern=yes,
   mark=yes,
   mkmk=yes,
   curs=yes]

\definefontfeature
  [default]
  [always]
  [liga=yes,
   tlig=yes,
   trep=yes] % texligatures=yes,texquotes=yes

\definefontfeature
  [smallcaps]
  [always]
  [smcp=yes,
   tlig=yes,
   trep=yes] % texligatures=yes,texquotes=yes

\definefontfeature
  [oldstyle]
  [always]
  [onum=yes,
   liga=yes,
   tlig=yes,
   trep=yes] % texligatures=yes,texquotes=yes

\definefontfeature % == default unless redefined
  [ligatures]
  [always]
  [liga=yes,
   tlig=yes,
   trep=yes]

\definefontfeature % can be used for type1 fonts
  [complete]
  [always]
  [compose=yes,
   liga=yes,
   tlig=yes,
   trep=yes]

\definefontfeature
  [none]
  [mode=none,
   features=no]
\stoptyping

These definitions show that you can construct feature sets on top of existing
ones, but keep in mind that they are defined instantly, so any change in the
parent is not reflected in its kids.

In a font definition you can specify more than one set:

\starttyping
\definefont
  [MyFont]
  [Bold*always,oldstyle at 12.3pt]
\stoptyping

\stopsection

\startsection[title=@ (virtual features)]

This method is somewhat special as it demands knowledge of the internals
of the \CONTEXT\ font code. Much of it is still experimental but it is a
nice playground. A good example of its usage can be found in the file
\type {m-punk.mkiv} where we create a font out of \METAPOST\ graphics.

Another example is virtual math. As in the beginning of \LUATEX\ and \MKIV\
there were only a few \OPENTYPE\ math fonts, and as I wanted to get rid
of the old mechanisms, it was decided to virtualize the math fonts. For
instance a Latin Modern Roman 10 point math font can be defined as follows:

\starttyping
\definefontsynonym
  [LMMathRoman10-Regular]
  [LMMath10-Regular@lmroman10-math]
\stoptyping

The \type {lmroman10-math} refers to a virtual definition and in this case
it is one using a built|-|in constructor and therefore we use a goodies
file to specify the font. That file looks as follows:

\starttyping
return {
  name = "lm-math",
  version = "1.00",
  comment = "Goodies that complement latin modern math.",
  author = "Hans Hagen",
  copyright = "ConTeXt development team",
  mathematics = {
    ...
    virtuals = {
      ...
      ["lmroman10-math"] = ten,
      ...
    },
    ...
  }
}
\stoptyping

Here \type {ten} is a previously defined table:

\startnarrowtyping
local ten = {
  { name = "lmroman10-regular.otf", features = "virtualmath", main = true },
  { name = "rm-lmr10.tfm", vector = "tex-mr-missing" } ,
  { name = "lmmi10.tfm", vector = "tex-mi", skewchar = 0x7F },
  { name = "lmmi10.tfm", vector = "tex-it", skewchar = 0x7F },
  { name = "lmsy10.tfm", vector = "tex-sy", skewchar = 0x30, parameters = true } ,
  { name = "lmex10.tfm", vector = "tex-ex", extension = true } ,
  { name = "msam10.tfm", vector = "tex-ma" },
  { name = "msbm10.tfm", vector = "tex-mb" },
  { name = "stmary10.afm", vector = "tex-mc" },
  { name = "lmroman10-bold.otf", vector = "tex-bf" } ,
  { name = "lmmib10.tfm", vector = "tex-bi", skewchar = 0x7F } ,
  { name = "lmsans10-regular.otf", vector = "tex-ss", optional = true },
  { name = "lmmono10-regular.otf", vector = "tex-tt", optional = true },
  { name = "eufm10.tfm", vector = "tex-fraktur", optional = true },
  { name = "eufb10.tfm", vector = "tex-fraktur-bold", optional = true },
}
\stopnarrowtyping

This says as much as: take \type {lmroman10-regular.otf} as starting point
and overload slots with ones found in the following fonts. The vectors are
predefined as they are shared with other font sets like \type {px} and
\type {tx}.

In due time more virtual methods might end up in \CONTEXT\ because they are
a convenient way to extend or manipulate fonts.

\stopsection

\startsection[title=Lua fonts]

You can define a font in \LUA. In the process you can use all kind of helper
functions that \CONTEXT\ provides. Here is an example:

\typefile{fonts-demo-rule.lua}

This code is stored in \type {fonts-demo-rule.lua} and we can load that font
in the usual way, by specifying a filename:

\startbuffer
\definefont
  [MyRuleFont]
  [file:fonts-demo-rule.lua*default sa 1]
\stopbuffer

\typebuffer

\getbuffer

{\MyRuleFont So when we use it we get text typeset where all vowels are replaced
by rules. The actualtext injection (in theory) makes it possible to cut and paste
the text from the \PDF\ document but while writing this (mid 2016) a {\maincolor
\type {mupdf}} based viewer couldn't handle it and {\maincolor \type {acrobat}}
had problems with spaces.}

\startbuffer
\definefontfeature
  [myrulefont]
  [default]
  [original=file:texgyrepagella-regular.otf]
\definefont
  [MyRuleFont]
  [file:fonts-demo-rule.lua*myrulefont]
\stopbuffer

\typebuffer

\getbuffer

{\showfontkerns \MyRuleFont The previous code demonstrates how we can pass a
fontname to be used as base to the generator. In case you wonder how features
behave with such fonts: as you can see here, font kerns are indeed injected.
Compared to {\maincolor \type {Dejavu}}, the {\maincolor \type {Pagella}} font
has quite some more kerns.}

\startbuffer
\definefontfeature
  [myrulefont]
  [default]
  [original=file:texgyrepagella-regular.otf,
   option=line]
\definefont
  [MyRuleFont]
  [file:fonts-demo-rule.lua*myrulefont]
\stopbuffer

\typebuffer

\getbuffer

{\MyRuleFont Here we show how the passed {\maincolor \type {option}} is handled.
Because we no longer have a relationship with the height and depth, the real text
is a bit harder to guess.}

\stopsection

\startsection[title=Old fuzzy fonts]

Most natural is to use \OPENTYPE\ or \TYPEONE\ fonts. In the case of \TYPEONE\ a
matching pair of \type {afm} and \type {pfb} files is needed. However, there can
be situations where there is only a \type{tfm} and \type {pfb} file (or not even
that: just a bitmap file).

I will not show specimen here, simply because I don't have (nor want to have) the
fonts needed in my development and production environments. The implementation
was tested with a specific czech computer modern font.

In a traditional (8 bit) setup we have an \type {tfm} file, a \type {pfb} file
and a \type {enc file}. The order of the characters in the \type {tfm} file
directly relates to the input encoding. The \type {enc} file relates that order
to the order in the \type {pfb} file. The mapping from input encoding to font
shape encoding happens via glyph names. In the \type {map} file we tell what
\type {pfb} file to use with what \type {enc} file.

However, in the case of the \type {csr.tfm} and \type {csr.pfb} file it looks
like in practice the \type {enc} file is not used, probably because in the \type
{pfb} file the standard encoding matches the order in the \type {tfm} file. This
is of course a rather dangerous assumption, especially if information lacks to
check it.

The next example definitions demonstrate several paths to go from \UNICODE\ input
(source file) to rendered shapes. As this is mostly meant for generic usage we
use the low level definition code (\CONTEXT\ users are not supposed to use that
method).

\starttyping
\font\foo=file:csr10.tfm:reencode=auto;mode=node;liga=yes;kern=yes
\stoptyping

This is the easiest way. We use the \type {tfm} file for dimensions, ligatures
and kerns. The \type {auto} option will use the \type {pfb} file to identify the
right mapping. We enable ligatures and kerns and we use node mode. This indicates
that we're dealing with a pseudo \OPENTYPE\ setup here. You can provide a \type
{pfb} file with the \type {pfbfile} feature in case the name differes from the
\type {tfm} file.

\starttyping
\font\foo=file:csr10.tfm:reencode=csr.enc;mode=node;liga=yes;kern=yes
\stoptyping

Now we use the \type {enc} file for the encoding vector but we still need the
\type {pfb} file for mapping that onto the right shape. You probably can best use
\type {auto} instead.

\starttyping
\font\foo=file:csr10.tfm:reencode=csr.enc;bitmap=yes;mode=node;liga=yes;kern=yes
\stoptyping

Here we force bitmap shapes. This is a bit tricky as a different code path is
followed in the backend. Unless the situation is too confusing, a proper \type
{ToUnicode} is included in the output, so that cut and paste works all right,
given that the viewer is able to deal with it (always use \ACROBAT\ as
reference).

Why do we need modes and|/|or to simulate \OPENTYPE\ behaviour? Indeed it seldom
makes sense with \type {tfm} files but in this particular case teh font has a
quote cheat.

\starttyping
\startluacode
    fonts.handlers.otf.addfeature {
        name = "czechdqcheat",
        type = "substitution",
        data = {
            quotedblright = "csquotedblright",
        },
    }
\stopluacode
\stoptyping

We could make this a language specific feature but as this font is not meant for
other languages it makes no sense to do so. This feature is enabled with:

\starttyping
czechdqcheat=yes
\stoptyping

This will replace one quote by another with different side bearings. Of course a
properly bounded quote with proper kerning makes much more sense. A test case is:

\starttyping
\quotedblleft  X\quotedblright
\quotedblright X\quotedblleft
\stoptyping

\stopsection

\stopchapter

\stopcomponent