fonts-tricks.tex /size: 11 Kb    last modification: 2021-10-28 13:50
1% language=us runpath=texruns:manuals/fonts
2
3\startcomponent fonts-tricks
4
5\environment fonts-environment
6
7\startchapter[title=Tricks][color=middleorange]
8
9\startsection[title=Introduction]
10
11In this chapter topics that don't fit in the previous chapters are
12collected.
13
14\stopsection
15
16\startsection[title=Extra characters]
17
18In one of our projects we need a small (high) minus and here we discuss
19one way of achieving this. An option is to define a new character
20and use a font feature to add it, as in:
21
22\starttyping
23\startluacode
24  local function addextraminus(tfmdata)
25    fonts.helpers.addprivate(tfmdata, "smallminus", ... ) -- see below
26    fonts.helpers.addprivate(tfmdata, "highminus",  ... ) -- see below
27  end
28  fonts.constructors.newfeatures.otf.register {
29    name        = "extraminus",
30    description = "extra minus symbols",
31    default     = true,
32    manipulators = {
33      base = addextraminus,
34      node = addextraminus,
35    }
36  }
37\stopluacode
38\stoptyping
39
40However, why make it an option when we can just add it to any font. So,
41we can end up with just:
42
43\typebuffer[extraminus] % already defined before loading fonts
44
45It is important to do this before fonts get loaded. Next we need to decide how to
46access these characters. A straightforward way is:
47
48\startbuffer
49\def\smallminus{\privatechar{smallminus}}
50\def\highminus {\privatechar{highminus}}
51\stopbuffer
52
53\typebuffer \getbuffer
54
55An alternative is:
56
57\starttyping
58\definemathcommand [smallminus] [ord] {\privatechar{smallminus}}
59\definemathcommand [highminus]  [ord] {\privatechar{highminus}}
60\stoptyping
61
62but that fails in text mode. So, we can consider:
63
64\starttyping
65\def\smallminus{\mathortext{\mathord{\privatechar{smallminus}}}{\privatechar{smallminus}}}
66\def\highminus {\mathortext{\mathord{\privatechar {highminus}}}{\privatechar {highminus}}}
67\stoptyping
68
69or more compact
70
71\starttyping
72\def\smallminus{\mathortext\mathord\firstofoneargument{\privatechar{smallminus}}}
73\def\highminus {\mathortext\mathord\firstofoneargument{\privatechar {highminus}}}
74\stoptyping
75
76What method you choose depends on usage. With the first, simple definitions we
77get this:
78
79\startbuffer
80[\getprivateslot{smallminus}] [\getprivatechar{smallminus}] [\smallminus] $[\smallminus]$\par
81[\getprivateslot {highminus}] [\getprivatechar {highminus}] [\highminus ] $[\highminus ]$\par
82\stopbuffer
83
84\typebuffer \getbuffer
85
86Because we are fully expandable we can even use this in:
87
88\startbuffer
89\bTABLE[aligncharacter=yes,alignmentcharacter={text->\smallminus}]
90  \bTR \bTD \mathminus100\smallminus00+ \eTD \eTR
91  \bTR \bTD           100\smallminus00  \eTD \eTR
92  \bTR \bTD \mathminus 99\smallminus00+ \eTD \eTR
93  \bTR \bTD \mathminus 99\smallminus00  \eTD \eTR
94  \bTR \bTD           100\highminus     \eTD \eTR
95\eTABLE
96\stopbuffer
97
98\typebuffer
99
100\startlinecorrection
101\getbuffer
102\stoplinecorrection
103
104With the other definitions the alignment would not work. If you need both then
105you can define different commands for text and math.
106
107\stopsection
108
109\startsection[title=Tabular numbers]
110
111Some font features are meant to be used in controlled situations. An example is
112the \type {tnum} feature that triggers so called \quote {tabular figures} meaning
113numbers that have equal widths. Its opposite feature is \type {lnum} or lining
114figures. We have two predefined \CONTEXT\ font features for this:
115
116\starttyping
117\definefontfeature [inlinenumbers]  [lnum=yes,tnum=no]
118\definefontfeature [tabularnumbers] [tnum=yes,lnum=no]
119\stoptyping
120
121Some mechanisms in \CONTEXT\ had better be aware of this and one of them is the
122alignment mechanism in tables.
123
124\startbuffer[demo]
125\startcombination[before=,after=]
126    \startcontent
127        \switchtobodyfont[pagella]\showglyphs%
128        \disabledirectives[typesetters.characteralign.autofont]%
129        \bTABLE[alignmentcharacter=.,aligncharacter=yes]
130            \bTR \bTD  11.111 \eTD \eTR
131            \bTR \bTD   2.2   \eTD \eTR
132            \bTR \bTD 444.444 \eTD \eTR
133        \eTABLE
134        \enabledirectives[typesetters.characteralign.autofont]%
135    \stopcontent
136    \startcaption
137        \small manual \type {tnum}
138    \stopcaption
139    \startcontent
140        \switchtobodyfont[pagella]\showglyphs%
141        \bTABLE[alignmentcharacter=.,aligncharacter=yes]
142            \bTR \bTD  11.111 \eTD \eTR
143            \bTR \bTD   2.2   \eTD \eTR
144            \bTR \bTD 444.444 \eTD \eTR
145        \eTABLE
146    \stopcontent
147    \startcaption
148        \small auto \type {tnum}
149    \stopcaption
150\stopcombination
151\stopbuffer
152
153In the following examples we demonstrate how the automatic font adaption makes
154sure that tabular figures are used. The table is defined as:
155
156\typebuffer[demo]
157
158\startbuffer
159\dontleavehmode\hbox to .3\textwidth \bgroup
160    \hss\getbuffer[demo]\hss
161\egroup
162\stopbuffer
163
164In \in {figure}[fig:tabularnumbers] we see what the features do. We use the \type
165{\feature} or \type {\addfeature} macro for this. For the moment this trickery is
166controlled by a directive (beware: enabling them is global).
167
168\startplacefigure[reference=fig:tabularnumbers,title={Tabular numbers}]
169    \startcombination[nx=3,ny=2]
170        {\getbuffer}                             {\tt\nohyphens default}
171        {\addfeature  [inlinenumbers]\getbuffer} {\tt\nohyphens inlinenumbers}
172        {\addfeature [tabularnumbers]\getbuffer} {\tt\nohyphens tabularnumbers}
173        {\addfeature[oldstylenumbers]\getbuffer} {\tt\nohyphens oldstylenumbers}
174        {\addfeature[oldstylenumbers]%
175         \addfeature  [inlinenumbers]\getbuffer} {\tt\nohyphens oldstylenumbers\par inlinenumbers}
176        {\addfeature[oldstylenumbers]%
177         \addfeature [tabularnumbers]\getbuffer} {\tt\nohyphens oldstylenumbers\par tabularnumbers}
178    \stopcombination
179\stopplacefigure
180
181\stopsection
182
183\startsection[title=Symbols]
184
185You can access glyphs by name but you need to know that name, for example:
186
187\startbuffer
188\definefontsynonym [bends] [file:manfnt.afm]
189
190\startsymbolset [Dangerous Bends]
191    \definesymbol [dbend]   [\resolvedglyphdirect{bends}{n:char_7e}]
192    \definesymbol [lhdbend] [\resolvedglyphdirect{bends}{n:char_7f}]
193\stopsymbolset
194
195\setupsymbolset [Dangerous Bends]
196
197Two dangerous bends: \symbol{dbend} and \symbol{lhdbend}.
198\stopbuffer
199
200\typebuffer
201
202You can best save the fonts you use that way in a place that doesn't get
203overwritten because names can change.
204
205\getbuffer
206
207\stopsection
208
209\startsection[title=Alternative styles]
210
211\startbuffer[demo]
212    \start
213        \getbuffer[setup]
214        \subject{[ {\myslanted myslanted} ] [ {\it it} ] [ {\slanted slanted} ] [ $x=1$ ]}
215        [ {\myslanted myslanted} ] [ {\it it} ] [ {\slanted slanted} ] [ $x=1$ ]
216        \typebuffer[setup]
217        \blackrule[width=\hsize,height=1pt,depth=0pt]
218    \stop
219\stopbuffer
220
221In section heads we want a nested style (e.g.\ italic) to adapt to the main font.
222The following definitions shows how you can influence that process. We use the
223following \type {demo} buffer as sample:
224
225\typebuffer[demo]
226
227\startbuffer
228\blackrule[width=\hsize,height=1pt,depth=0pt]
229
230\startbuffer[setup]
231\setuphead[subject][style=\tfb,before=,after=]
232\definealternativestyle [myslanted] [\it] []
233\stopbuffer
234
235\getbuffer[demo]
236
237\startbuffer[setup]
238\setuphead[subject][style=\tfb,before=,after=]
239\definealternativestyle [myslanted] [\it] [\bi]
240\stopbuffer
241
242\getbuffer[demo]
243
244\startbuffer[setup]
245\setuphead[subject][style=\tfb,before=,after=]
246\definealternativestyle [myslanted] [\it] [\tf]
247\stopbuffer
248
249\getbuffer[demo]
250
251\startbuffer[setup]
252\definealternativestyle [myslanted] [\normalitalicface]
253\setuphead[subject][style=bold,before=,after=]
254\stopbuffer
255
256\getbuffer[demo]
257
258\startbuffer[setup]
259\definealternativestyle [myslanted] [\normalitalicface]
260\setuphead[subject][style=\bfd,before=,after=]
261\stopbuffer
262
263\getbuffer[demo]
264
265\startbuffer[setup]
266\definealternativestyle [myslanted] [\slantedface]
267\setuphead[subject][style=boldface,before=,after=]
268\stopbuffer
269
270\getbuffer[demo]
271\stopbuffer
272
273You can influence the main method of operation with:
274
275\starttyping
276\setupalternativestyles[method=normal]
277\setupalternativestyles[method=auto]
278\stoptyping
279
280\startplacefigure[title={Alternative style methods.},reference=fig:alternativestyle]
281    \startcombination
282        {\setupalternativestyles[method=normal]\scale[width=.45\textwidth]{\framed[width=.85\textwidth,align=normal,frame=off,offset=overlay]{\getbuffer}}} {\type{method=normal}}
283        {\setupalternativestyles  [method=auto]\scale[width=.45\textwidth]{\framed[width=.85\textwidth,align=normal,frame=off,offset=overlay]{\getbuffer}}} {\type{method=auto}}
284    \stopcombination
285\stopplacefigure
286
287The result is shown in \in {figure} [fig:alternativestyle]. Relevant commands are:
288
289\starttyping
290\emphasistypeface
291\emphasisboldface
292
293\normaltypeface    \typeface
294\normalboldface    \boldface
295\normalslantedface \slantedface
296\normalitalicface  \italicface
297\swaptypeface      \swapface
298\stoptyping
299
300\stopsection
301
302\startsection[title={A virtual hack}]
303
304Here is some virtual trickery. A virtual font is just a font but instead of a
305character being a reference to a slot in a font (often via an index) it
306constructs a glyph from whatever characters, rules, displacements, inline \PDF\
307code, etc. We use them a lot deep down in \CONTEXT. The next example defines two
308characters represented by rules. This definition is about as minimalistic as
309reasonable and demonstrates how we can apply expansion (aka hz) to such a font.
310\footnote {You need \LUATEX\ 1.08 or later for this.} We store the font id (a
311number) in a macro. Watch how we don't refer to a glyph in a font. Because we
312don't specify its type as \type {virtual} we can leave out the \type {font}
313table. After all, we don't refer to real glyphs.
314
315\startbuffer
316\startluacode
317local d = 400000 -- just over 6pt
318local a = font.define {
319    characters = {
320        [string.byte("A")] =
321            {
322                width    = d,
323                depth    = d,
324                commands = { { "down", d }, { "rule", d, d } },
325                expansion_factor = 1000,
326            },
327        [string.byte("B")] =
328            {
329                width    = d,
330                height   = d,
331                commands = { { "rule", d, d } },
332                expansion_factor = 500,
333            },
334
335    },
336    parameters = {
337        space         = 2*d,
338        space_stretch = d,
339        space_shrink  = d,
340    },
341    stretch = 8,
342    shrink  = 8,
343    step    = 2,
344    name    = "foo"
345}
346
347tex.count.scratchcounter = a
348\stopluacode
349
350\edef\MyFontID{\the\scratchcounter}
351\stopbuffer
352
353\typebuffer \getbuffer
354
355% \enabletrackers[*expans*]
356
357\startbuffer
358\startoverlay
359  {\vbox \bgroup
360     \setuptolerance[verytolerant,stretch]
361     \setfontid\MyFontID
362     \dorecurse{5}{ABABABABABABABABABABABABABABABABA }
363   \egroup}
364  {\vbox \bgroup
365     \normaladjustspacing2
366     \middlegray
367     \setuptolerance[verytolerant,stretch]
368     \setfontid\MyFontID
369     \dorecurse{5}{ABABABABABABABABABABABABABABABABA }
370   \egroup}
371\stopoverlay
372\stopbuffer
373
374We test this with some rather low level code and show the result in \in {figure}
375[fig:virtualhack]. Of course you will never define a font this way if only
376because we don't set important parameters and this version is not generic in the
377sense that it scales well. You can find better examples elsewhere in the manual
378and in the distribution.
379
380\typebuffer
381
382\startplacefigure[title={A virtual hack.},reference=fig:virtualhack]
383    \getbuffer
384\stopplacefigure
385
386\stopsection
387
388\stopchapter
389
390\stopcomponent
391