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

\startcomponent fonts-tricks

\environment fonts-environment

\startchapter[title=Tricks][color=middleorange]

\startsection[title=Introduction]

In this chapter topics that don't fit in the previous chapters are
collected.

\stopsection

\startsection[title=Extra characters]

In one of our projects we need a small (high) minus and here we discuss
one way of achieving this. An option is to define a new character
and use a font feature to add it, as in:

\starttyping
\startluacode
  local function addextraminus(tfmdata)
    fonts.helpers.addprivate(tfmdata, "smallminus", ... ) -- see below
    fonts.helpers.addprivate(tfmdata, "highminus",  ... ) -- see below
  end
  fonts.constructors.newfeatures.otf.register {
    name        = "extraminus",
    description = "extra minus symbols",
    default     = true,
    manipulators = {
      base = addextraminus,
      node = addextraminus,
    }
  }
\stopluacode
\stoptyping

However, why make it an option when we can just add it to any font. So,
we can end up with just:

\typebuffer[extraminus] % already defined before loading fonts

It is important to do this before fonts get loaded. Next we need to decide how to
access these characters. A straightforward way is:

\startbuffer
\def\smallminus{\privatechar{smallminus}}
\def\highminus {\privatechar{highminus}}
\stopbuffer

\typebuffer \getbuffer

An alternative is:

\starttyping
\definemathcommand [smallminus] [ord] {\privatechar{smallminus}}
\definemathcommand [highminus]  [ord] {\privatechar{highminus}}
\stoptyping

but that fails in text mode. So, we can consider:

\starttyping
\def\smallminus{\mathortext{\mathord{\privatechar{smallminus}}}{\privatechar{smallminus}}}
\def\highminus {\mathortext{\mathord{\privatechar {highminus}}}{\privatechar {highminus}}}
\stoptyping

or more compact

\starttyping
\def\smallminus{\mathortext\mathord\firstofoneargument{\privatechar{smallminus}}}
\def\highminus {\mathortext\mathord\firstofoneargument{\privatechar {highminus}}}
\stoptyping

What method you choose depends on usage. With the first, simple definitions we
get this:

\startbuffer
[\getprivateslot{smallminus}] [\getprivatechar{smallminus}] [\smallminus] $[\smallminus]$\par
[\getprivateslot {highminus}] [\getprivatechar {highminus}] [\highminus ] $[\highminus ]$\par
\stopbuffer

\typebuffer \getbuffer

Because we are fully expandable we can even use this in:

\startbuffer
\bTABLE[aligncharacter=yes,alignmentcharacter={text->\smallminus}]
  \bTR \bTD \mathminus100\smallminus00+ \eTD \eTR
  \bTR \bTD           100\smallminus00  \eTD \eTR
  \bTR \bTD \mathminus 99\smallminus00+ \eTD \eTR
  \bTR \bTD \mathminus 99\smallminus00  \eTD \eTR
  \bTR \bTD           100\highminus     \eTD \eTR
\eTABLE
\stopbuffer

\typebuffer

\startlinecorrection
\getbuffer
\stoplinecorrection

With the other definitions the alignment would not work. If you need both then
you can define different commands for text and math.

\stopsection

\startsection[title=Tabular numbers]

Some font features are meant to be used in controlled situations. An example is
the \type {tnum} feature that triggers so called \quote {tabular figures} meaning
numbers that have equal widths. Its opposite feature is \type {lnum} or lining
figures. We have two predefined \CONTEXT\ font features for this:

\starttyping
\definefontfeature [inlinenumbers]  [lnum=yes,tnum=no]
\definefontfeature [tabularnumbers] [tnum=yes,lnum=no]
\stoptyping

Some mechanisms in \CONTEXT\ had better be aware of this and one of them is the
alignment mechanism in tables.

\startbuffer[demo]
\startcombination[before=,after=]
    \startcontent
        \switchtobodyfont[pagella]\showglyphs%
        \disabledirectives[typesetters.characteralign.autofont]%
        \bTABLE[alignmentcharacter=.,aligncharacter=yes]
            \bTR \bTD  11.111 \eTD \eTR
            \bTR \bTD   2.2   \eTD \eTR
            \bTR \bTD 444.444 \eTD \eTR
        \eTABLE
        \enabledirectives[typesetters.characteralign.autofont]%
    \stopcontent
    \startcaption
        \small manual \type {tnum}
    \stopcaption
    \startcontent
        \switchtobodyfont[pagella]\showglyphs%
        \bTABLE[alignmentcharacter=.,aligncharacter=yes]
            \bTR \bTD  11.111 \eTD \eTR
            \bTR \bTD   2.2   \eTD \eTR
            \bTR \bTD 444.444 \eTD \eTR
        \eTABLE
    \stopcontent
    \startcaption
        \small auto \type {tnum}
    \stopcaption
\stopcombination
\stopbuffer

In the following examples we demonstrate how the automatic font adaption makes
sure that tabular figures are used. The table is defined as:

\typebuffer[demo]

\startbuffer
\dontleavehmode\hbox to .3\textwidth \bgroup
    \hss\getbuffer[demo]\hss
\egroup
\stopbuffer

In \in {figure}[fig:tabularnumbers] we see what the features do. We use the \type
{\feature} or \type {\addfeature} macro for this. For the moment this trickery is
controlled by a directive (beware: enabling them is global).

\startplacefigure[reference=fig:tabularnumbers,title={Tabular numbers}]
    \startcombination[nx=3,ny=2]
        {\getbuffer}                             {\tt\nohyphens default}
        {\addfeature  [inlinenumbers]\getbuffer} {\tt\nohyphens inlinenumbers}
        {\addfeature [tabularnumbers]\getbuffer} {\tt\nohyphens tabularnumbers}
        {\addfeature[oldstylenumbers]\getbuffer} {\tt\nohyphens oldstylenumbers}
        {\addfeature[oldstylenumbers]%
         \addfeature  [inlinenumbers]\getbuffer} {\tt\nohyphens oldstylenumbers\par inlinenumbers}
        {\addfeature[oldstylenumbers]%
         \addfeature [tabularnumbers]\getbuffer} {\tt\nohyphens oldstylenumbers\par tabularnumbers}
    \stopcombination
\stopplacefigure

\stopsection

\startsection[title=Symbols]

You can access glyphs by name but you need to know that name, for example:

\startbuffer
\definefontsynonym [bends] [file:manfnt.afm]

\startsymbolset [Dangerous Bends]
    \definesymbol [dbend]   [\resolvedglyphdirect{bends}{n:char_7e}]
    \definesymbol [lhdbend] [\resolvedglyphdirect{bends}{n:char_7f}]
\stopsymbolset

\setupsymbolset [Dangerous Bends]

Two dangerous bends: \symbol{dbend} and \symbol{lhdbend}.
\stopbuffer

\typebuffer

You can best save the fonts you use that way in a place that doesn't get
overwritten because names can change.

\getbuffer

\stopsection

\startsection[title=Alternative styles]

\startbuffer[demo]
    \start
        \getbuffer[setup]
        \subject{[ {\myslanted myslanted} ] [ {\it it} ] [ {\slanted slanted} ] [ $x=1$ ]}
        [ {\myslanted myslanted} ] [ {\it it} ] [ {\slanted slanted} ] [ $x=1$ ]
        \typebuffer[setup]
        \blackrule[width=\hsize,height=1pt,depth=0pt]
    \stop
\stopbuffer

In section heads we want a nested style (e.g.\ italic) to adapt to the main font.
The following definitions shows how you can influence that process. We use the
following \type {demo} buffer as sample:

\typebuffer[demo]

\startbuffer
\blackrule[width=\hsize,height=1pt,depth=0pt]

\startbuffer[setup]
\setuphead[subject][style=\tfb,before=,after=]
\definealternativestyle [myslanted] [\it] []
\stopbuffer

\getbuffer[demo]

\startbuffer[setup]
\setuphead[subject][style=\tfb,before=,after=]
\definealternativestyle [myslanted] [\it] [\bi]
\stopbuffer

\getbuffer[demo]

\startbuffer[setup]
\setuphead[subject][style=\tfb,before=,after=]
\definealternativestyle [myslanted] [\it] [\tf]
\stopbuffer

\getbuffer[demo]

\startbuffer[setup]
\definealternativestyle [myslanted] [\normalitalicface]
\setuphead[subject][style=bold,before=,after=]
\stopbuffer

\getbuffer[demo]

\startbuffer[setup]
\definealternativestyle [myslanted] [\normalitalicface]
\setuphead[subject][style=\bfd,before=,after=]
\stopbuffer

\getbuffer[demo]

\startbuffer[setup]
\definealternativestyle [myslanted] [\slantedface]
\setuphead[subject][style=boldface,before=,after=]
\stopbuffer

\getbuffer[demo]
\stopbuffer

You can influence the main method of operation with:

\starttyping
\setupalternativestyles[method=normal]
\setupalternativestyles[method=auto]
\stoptyping

\startplacefigure[title={Alternative style methods.},reference=fig:alternativestyle]
    \startcombination
        {\setupalternativestyles[method=normal]\scale[width=.45\textwidth]{\framed[width=.85\textwidth,align=normal,frame=off,offset=overlay]{\getbuffer}}} {\type{method=normal}}
        {\setupalternativestyles  [method=auto]\scale[width=.45\textwidth]{\framed[width=.85\textwidth,align=normal,frame=off,offset=overlay]{\getbuffer}}} {\type{method=auto}}
    \stopcombination
\stopplacefigure

The result is shown in \in {figure} [fig:alternativestyle]. Relevant commands are:

\starttyping
\emphasistypeface
\emphasisboldface

\normaltypeface    \typeface
\normalboldface    \boldface
\normalslantedface \slantedface
\normalitalicface  \italicface
\swaptypeface      \swapface
\stoptyping

\stopsection

\startsection[title={A virtual hack}]

Here is some virtual trickery. A virtual font is just a font but instead of a
character being a reference to a slot in a font (often via an index) it
constructs a glyph from whatever characters, rules, displacements, inline \PDF\
code, etc. We use them a lot deep down in \CONTEXT. The next example defines two
characters represented by rules. This definition is about as minimalistic as
reasonable and demonstrates how we can apply expansion (aka hz) to such a font.
\footnote {You need \LUATEX\ 1.08 or later for this.} We store the font id (a
number) in a macro. Watch how we don't refer to a glyph in a font. Because we
don't specify its type as \type {virtual} we can leave out the \type {font}
table. After all, we don't refer to real glyphs.

\startbuffer
\startluacode
local d = 400000 -- just over 6pt
local a = font.define {
    characters = {
        [string.byte("A")] =
            {
                width    = d,
                depth    = d,
                commands = { { "down", d }, { "rule", d, d } },
                expansion_factor = 1000,
            },
        [string.byte("B")] =
            {
                width    = d,
                height   = d,
                commands = { { "rule", d, d } },
                expansion_factor = 500,
            },

    },
    parameters = {
        space         = 2*d,
        space_stretch = d,
        space_shrink  = d,
    },
    stretch = 8,
    shrink  = 8,
    step    = 2,
    name    = "foo"
}

tex.count.scratchcounter = a
\stopluacode

\edef\MyFontID{\the\scratchcounter}
\stopbuffer

\typebuffer \getbuffer

% \enabletrackers[*expans*]

\startbuffer
\startoverlay
  {\vbox \bgroup
     \setuptolerance[verytolerant,stretch]
     \setfontid\MyFontID
     \dorecurse{5}{ABABABABABABABABABABABABABABABABA }
   \egroup}
  {\vbox \bgroup
     \normaladjustspacing2
     \middlegray
     \setuptolerance[verytolerant,stretch]
     \setfontid\MyFontID
     \dorecurse{5}{ABABABABABABABABABABABABABABABABA }
   \egroup}
\stopoverlay
\stopbuffer

We test this with some rather low level code and show the result in \in {figure}
[fig:virtualhack]. Of course you will never define a font this way if only
because we don't set important parameters and this version is not generic in the
sense that it scales well. You can find better examples elsewhere in the manual
and in the distribution.

\typebuffer

\startplacefigure[title={A virtual hack.},reference=fig:virtualhack]
    \getbuffer
\stopplacefigure

\stopsection

\stopchapter

\stopcomponent