hybrid-italics.tex /size: 14 Kb    last modification: 2023-12-21 09:43
1% language=us
2
3% \enabletrackers[typesetters.italics]
4
5% We need to make sure that this works ok, also in future versions
6% so we add some more (re)settings than needed.
7
8\startcomponent hybrid-italics
9
10\environment hybrid-environment
11
12\ctxcommand{pushitaliccorrection()}
13
14\startchapter[title={Italic correction}]
15
16\component hybrid-note
17
18\startsection [title={Introduction}]
19
20During the 2011 \CONTEXT\ conference there were presentations by Thomas Schmitz
21and Jano Kula where they demonstrated advanced rendering of document source
22encoded in \XML. When looking at the examples on screen using many fonts I
23realized that (also given my own workflows) it was about time to look into
24automated italic correction in the perspective of \MKIV.
25
26In the Lucida Math project it already became clear that italics in \OPENTYPE\
27math fonts are to be ignored. And, as in regular \OPENTYPE\ fonts italic
28correction is basically non|-|existent some alternative approach is needed there
29as well. In \CONTEXT\ you can already for quite a while enable the \type {itlc}
30feature which adds italic correction to shapes using some heuristics. However, in
31\TEX\ this kind of correction is never applied automatically but is triggered by
32the \type {\/} command. Commands like \type {\em} deal with italic correction
33automatically but otherwise you need to take care of it yourself. In a time when
34you not always have control over the source code or when you are coding in a
35format that has no provisions for it (for instance \XML) some further automatism
36makes sense. You might even wonder if explicit corrections still make sense.
37
38In this chapter we discuss an alternative approach in \MKIV. This is a typical
39example of an experimental feature that might need further discussion (probably
40at a next conference). One of our mottos is that the document source should be as
41clean as possible and this is one way to go.
42
43\stopsection
44
45\startsection [title={Some preparations}]
46
47Adding italic correction to a font is easy: you just add the right feature
48directive. You can do this for all italic (or oblique) fonts in one go:
49
50\starttyping
51\definefontfeature[default][default][itlc=yes]
52\stoptyping
53
54At some point this might become the default in \CONTEXT. After that the \type
55{\/} command can do the job, but as mentioned, we don't really want to do this
56each time it's needed. If you never plan to use that command you can disable
57\TEX's built|-|in mechanism completely by setting the \type {textitalics}
58parameter.
59
60\starttyping
61\definefontfeature[default][default][itlc=yes,textitalics=yes]
62\stoptyping
63
64It even makes sense then to redefine the the \type {\/} command:
65
66\starttyping
67\let\/=/
68\stoptyping
69
70so that we have a nice escape in tune with the other escapes.
71
72\stopsection
73
74\startsection [title={Controlling correction}]
75
76In the following examples we will use Cambria as an example as it shows the
77effect rather prominently.
78
79\startbuffer[setups]
80\definefontfeature[itclyes][default][itlc=yes,textitalics=no]
81
82\definefont[itlcregular]   [name:cambria*itclyes sa 4]
83\definefont[itlcbold]      [name:cambriabold*itclyes sa 4]
84\definefont[itlcitalic]    [name:cambriaitalic*itclyes sa 4]
85\definefont[itlcbolditalic][name:cambriabolditalic*itclyes sa 4]
86\stopbuffer
87
88\getbuffer[setups]
89
90\let\/=\italiccorrection % to be sure
91
92\defineframed[ITLCexampleA][frame=off,foregroundcolor=white,background=color,backgroundcolor=red]
93\defineframed[ITLCexampleB][frame=off,foregroundcolor=white,background=color,backgroundcolor=blue]
94\defineframed[ITLCexampleC][frame=off,foregroundcolor=white,background=color,backgroundcolor=green]
95\defineframed[ITLCexampleD][frame=off,foregroundcolor=white,background=color,backgroundcolor=yellow]
96\defineframed[ITLCexampleE][frame=off,foregroundcolor=white,background=color,backgroundcolor=gray]
97
98We start with a simple case: just an emphasized word in a small
99line:
100
101\starttyping
102\setupitaliccorrection[none]\tf test {\it test} test
103\setupitaliccorrection[none]\tf test {\it test\/} test}
104\setupitaliccorrection[text]\tf test {\it test} test}
105\stoptyping
106
107Decorated for the purpose of this demonstration this comes out as follows:
108
109\startlinecorrection[blank] \vbox \bgroup \offinterlineskip
110    \ITLCexampleA{\setupitaliccorrection[none]\itlcregular test {\itlcitalic test} test}   \par
111    \ITLCexampleB{\setupitaliccorrection[none]\itlcregular test {\itlcitalic test\/} test} \par
112    \ITLCexampleC{\setupitaliccorrection[text]\itlcregular test {\itlcitalic test} test}   \par
113\egroup \stoplinecorrection
114
115In the first line no correction is applied. The second line shows \TEX\ in action
116and the third line demonstrates the automatically applied correction. The
117explicit directive in the second lines of course gives most control but is also a
118no|-|go when you have lots of them.
119
120Actually, \TEX\ is clever enough to ignore multiple corrections: it will only
121apply one after a glyph.
122
123\starttyping
124\setupitaliccorrection[none]\tf test {\it test} test}
125\setupitaliccorrection[none]\tf test {\it test\/} test}
126\setupitaliccorrection[none]\tf test {\it test\/\/\/\/} test}
127\stoptyping
128
129So we get this:
130
131\startlinecorrection[blank] \vbox \bgroup \offinterlineskip
132    \ITLCexampleA{\setupitaliccorrection[none]\itlcregular test {\itlcitalic test} test} \par
133    \ITLCexampleB{\setupitaliccorrection[none]\itlcregular test {\itlcitalic test\/} test} \par
134    \ITLCexampleC{\setupitaliccorrection[none]\itlcregular test {\itlcitalic test\/\/\/\/} test} \par
135\egroup \stoplinecorrection
136
137It can be argued that in a decent usage of \CONTEXT\ you will never switch to
138another font this way. Instead you will do this:
139
140\starttyping
141\definehighlight[important][style=\it]
142
143test \important{test} test
144\stoptyping
145
146However, this will not correct at all, so in fact you have to use an environment
147that takes care of automatically adding the \type {\/} at the end. Quite from the
148start the \type {\em} command does this, with the added benefit of dealing with
149bold and nested emphasizing.
150
151Which brings us to cases where you don't want to apply correction, like:
152
153\starttyping
154\setupitaliccorrection[none]\tf test {\it test}{\bi test}
155\setupitaliccorrection[none]\tf test {\it test\/}{\bi test}
156\setupitaliccorrection[text]\tf test {\it test}{\bi test}
157\stoptyping
158
159Now we get:
160
161\startlinecorrection[blank] \vbox \bgroup \offinterlineskip
162    \ITLCexampleA{\setupitaliccorrection[none]\itlcregular test {\itlcitalic test}{\itlcbolditalic test}}   \par
163    \ITLCexampleB{\setupitaliccorrection[none]\itlcregular test {\itlcitalic test\/}{\itlcbolditalic test}} \par
164    \ITLCexampleC{\setupitaliccorrection[text]\itlcregular test {\itlcitalic test}{\itlcbolditalic test}}   \par
165\egroup \stoplinecorrection
166
167A variant on this is:
168
169\starttyping
170\setupitaliccorrection[none]\tf test {\it test   \bi test}
171\setupitaliccorrection[none]\tf test {\it test\/ \bi test}
172\setupitaliccorrection[text]\tf test {\it test   \bi test}
173\stoptyping
174
175which gives:
176
177\startlinecorrection[blank] \vbox \bgroup \offinterlineskip
178    \ITLCexampleA{\setupitaliccorrection[none]\itlcregular test {\itlcitalic test   \itlcbolditalic test}} \par
179    \ITLCexampleB{\setupitaliccorrection[none]\itlcregular test {\itlcitalic test\/ \itlcbolditalic test}} \par
180    \ITLCexampleC{\setupitaliccorrection[text]\itlcregular test {\itlcitalic test   \itlcbolditalic test}} \par
181\egroup \stoplinecorrection
182
183So, no italic correction is added between italic shapes of different fonts.
184Ideally we should have some inter|-|character kerning, but that is currently
185beyond this mechanism.
186
187What does the \type {text} mean in the setup command? The following table tells
188what keywords can be passed:
189
190\starttabulate
191\NC \type {text}   \NC only apply correction to running text \NC \NR
192\NC \type {always} \NC also apply correction to end end of a list \NC \NR
193\NC \type {global} \NC enable this mechanism globally (more efficient) \NC \NR
194\NC \type {none}   \NC disable this mechanism \NC \NR
195\stoptabulate
196
197The difference between \type {text} and \type {always} is best demonstrated with
198an example:
199
200\starttyping
201\setupitaliccorrection[none]\tf   test {\it test}}
202\setupitaliccorrection[always]\tf test {\it test}}
203\setupitaliccorrection[text]\tf   test {\it test}}
204\stoptyping
205
206This gives:
207
208\startlinecorrection[blank] \vbox \bgroup \offinterlineskip
209    \ITLCexampleA{\setupitaliccorrection[none]\itlcregular   test {\itlcitalic test}} \par
210    \ITLCexampleD{\setupitaliccorrection[always]\itlcregular test {\itlcitalic test}} \par
211    \ITLCexampleC{\setupitaliccorrection[text]\itlcregular   test {\itlcitalic test}} \par
212\egroup \stoplinecorrection
213
214The \type {always} option will flush pending corrections at a boundary, like the
215edge of a box (or line). Contrary to \TEX's italic corrections, the \MKIV\
216variants are glue and they will disappear whenever \TEX\ likes to get rid of
217glue, for instance at line breaks. \footnote {There is some room for improvement
218here, for instance we can take penalties into account.}
219
220While writing this, we're still talking of an experimental setup so there might
221be extensions or changes to this mechanism. \footnote {For instance, I'm
222considering extending this mechanism to provide kerning between fonts, something
223for a rainy afternoon.}
224
225As it's just a guess you can influence the amount of automatic correction by
226specifying a factor. We show an exmaple of this.
227
228\startbuffer
229\definefontfeature[itclyes]       [default][itlc=yes,textitalics=delay]
230\definefontfeature[itclyesten]    [default][itlc=10, textitalics=delay]
231\definefontfeature[itclyeshundred][default][itlc=100,textitalics=delay]
232
233\definefont[itlcitalicyes]      [name:cambriaitalic*itclyes        sa 4]
234\definefont[itlcitalicten]      [name:cambriaitalic*itclyesten     sa 4]
235\definefont[itlcitalichundred]  [name:cambriaitalic*itclyeshundred sa 4]
236\stopbuffer
237
238\typebuffer \getbuffer
239
240We show all three variants:
241
242\starttyping
243\setupitaliccorrection[text]\itlcregular test {\itlcitalicyes     test} test\par
244\setupitaliccorrection[text]\itlcregular test {\itlcitalicten     test} test\par
245\setupitaliccorrection[text]\itlcregular test {\itlcitalichundred test} test\par
246\stoptyping
247
248This becomes:
249
250\startlinecorrection[blank] \vbox \bgroup \offinterlineskip
251    \ITLCexampleA{\setupitaliccorrection[text]\itlcregular test {\itlcitalicyes     test} test}\par
252    \ITLCexampleB{\setupitaliccorrection[text]\itlcregular test {\itlcitalicten     test} test}\par
253    \ITLCexampleC{\setupitaliccorrection[text]\itlcregular test {\itlcitalichundred test} test}\par
254\egroup \stoplinecorrection
255
256\stopsection
257
258\startsection [title={Saving resources}]
259
260You can keep track of what gets done by enabling a tracker:
261
262\starttyping
263\enabletrackers[typesetters.italics]
264\stoptyping
265
266You will notice that there are occasional reports about correction being
267inserted, ignored and removed. As node lists are parsed there is some extra
268overhead, but not that much. The \TEX\ solution (using \type {\/}) is quit
269efficient because that command directly injects a kern without too much analysis.
270You can gain some efficiency for the automated variant by using the \type
271{global} option:
272
273\starttyping
274\setupitaliccorrection[always,global]
275\stoptyping
276
277Also, you can disable \TEX's mechanism effectively by not passing the italic
278information to the font machinery at all:
279
280\starttyping
281\definefontfeature[italics][default][itlc=yes,textitalics=yes]
282\stoptyping
283
284The \type {itlc} feature will tag the font for italic corrections but the \type
285{textitalics} option will make sure that this information is not passed to the
286\TEX\ font handler but kept private.
287
288As adding the italic corrections to a font takes memory and a little bit of extra
289load time, we can delay this process till it is really needed.
290
291\starttyping
292\definefontfeature[italics][default][itlc=yes,textitalics=delay]
293\stoptyping
294
295In this case the correction will be calculated when needed and cached for later
296usage. At some point this might become the default \CONTEXT\ behaviour.
297
298\stopsection
299
300\startsection [title={Math}]
301
302Italic correction in math plays a role when dealing with traditional \TEX\ fonts,
303where glyph dimensions can have a special meaning. However, in \OPENTYPE\ math
304the correction is mostly ignored. You can disable it altogether and let an
305alternative mechanism deal with it. This mechanism is still somewhat experimental
306but is controlled as follows:
307
308\starttyping
309\switchtobodyfont[xits]
310\setupmathematics[italics=no] test $a;b;a; b; f;$ test}
311\setupmathematics[italics=1]  test $a;b;a; b; f;$ test}
312\setupmathematics[italics=2]  test $a;b;a; b; f;$ test}
313\setupmathematics[italics=3]  test $a;b;a; b; f;$ test}
314\setupmathematics[italics=4]  test $a;b;a; b; f;$ test}
315\stoptyping
316
317This gives:
318
319\start \switchtobodyfont[xits,42pt] \stop
320
321\startlinecorrection[blank] \vbox \bgroup \offinterlineskip \enabletrackers[math.italics]
322    \ITLCexampleE{\switchtobodyfont[xits,42pt]\setupmathematics[italics=no] test $a;b;a; b; f;$ test}\par
323    \ITLCexampleA{\switchtobodyfont[xits,42pt]\setupmathematics[italics=1]  test $a;b;a; b; f;$ test}\par
324    \ITLCexampleB{\switchtobodyfont[xits,42pt]\setupmathematics[italics=2]  test $a;b;a; b; f;$ test}\par
325    \ITLCexampleC{\switchtobodyfont[xits,42pt]\setupmathematics[italics=3]  test $a;b;a; b; f;$ test}\par
326    \ITLCexampleD{\switchtobodyfont[xits,42pt]\setupmathematics[italics=4]  test $a;b;a; b; f;$ test}\par
327\egroup \stoplinecorrection \disabletrackers[math.italics]
328
329The actual rendering can depend on the settings in the goodies file, for
330instance:
331
332\starttyping
333local italics = {
334    defaultfactor = 0.025,
335    disableengine = true, % feature: mathitalics=yes
336    corrections   = {
337     -- [0x1D44E] = 0.99,    -- a (fraction of quad)
338     -- [0x1D44F] = 100,     -- b (font points)
339        [0x1D453] = -0.0375, -- f
340    }
341}
342
343return {
344    name = "xits-math",
345    version = "1.00",
346    comment = "Goodies that complement xits (by Khaled Hosny).",
347    author = "Hans Hagen",
348    copyright = "ConTeXt development team",
349    mathematics = {
350        italics = {
351            ["xits-math"] = italics,
352        },
353    }
354}
355\stoptyping
356
357Corrections can be specified in the font's units or as a fraction (smaller than
3581) in which case it will be multiplied by \type {1em}. You can set the font
359feature \type {mathitalics} to \type {yes} to inhibit the engine's built|-|in
360mechanism completely and rely on the alternative approach but as users will
361seldom define math feature sets themselves, there is also the possibility to
362disable the engine in the goodies file.
363
364The process can be watched by setting a tracker:
365
366\starttyping
367\enabletrackers[math.italics]
368\stoptyping
369
370\stopsection
371
372\stopchapter
373
374\ctxcommand{popitaliccorrection()}
375
376\disabletrackers[typesetters.italics]
377
378\stopcomponent
379