evenmore-expansion.tex /size: 4610 b    last modification: 2021-10-28 13:50
1% language=us runpath=texruns:manuals/evenmore
2
3\environment evenmore-style
4
5\startcomponent evenmore-expansion
6
7\startchapter[title=Expansion]
8
9Character expansion was introduced in \PDFTEX\ a couple of decades ago. It is a
10mechanism that scales glyphs horizontally in order to reduce excessive whitespace
11that is needed to properly justify a paragraph. I must admit that I never use it
12myself but there are users who do. Although this mechanism evolved a bit, and in
13\LUATEX\ is implemented a bit different, the basics remained the same. If you
14have no clue what this is about, you can just quite reading here.
15
16A font can be set up to expand characters by a certain amount: they can shrink or
17stretch. This is driven by three parameters: \type {step}, \type {stretch} and
18\type {shrink}. The values are in thousands because \TEX\ has no float quantity.
19Originally these values were percentages of the width of a glyph, later they
20became related to the em width but in \LUATEX\ we went back to the former
21definition.
22
23In \CONTEXT\ \MKIV\ we have an interface that works as follows:
24
25\startbuffer
26\startluacode
27    local classes = fonts.expansions.classes
28
29    classes.qualitya = {
30        vector  = "default",
31        factor  = 1,
32        stretch = 4,
33        shrink  = 2,
34        step    = .5,
35    }
36
37    classes.qualityb = {
38        vector  = "default",
39        factor  = 1,
40        stretch = 8,
41        shrink  = 4,
42        step    = .5,
43    }
44
45\stopluacode
46\stopbuffer
47
48\typebuffer[option=TEX] \getbuffer
49
50The default vector looks like this:
51
52\starttyping[option=LUA]
53vectors['default'] = {
54    [0x0041] = 0.5, -- A
55    [0x0042] = 0.7, -- B
56    -- and some more
57}
58\stoptyping
59
60The values that we pass to the engine are stretch 40, shrink 20, and step 5 for
61\type {qualitya} and stretch 80, shrink 40, and step 5 for \type {qualityb}, so
62we multiply by 10. In the engine the step is limited to 100, the stretch to 1000
63and the shrink to 500. But these extremes produce quite bad results.
64
65The expansion class is set with the \type {expansion} feature, as in:
66
67\startbuffer
68\definefontfeature [basea] [default] [expansion=qualitya]
69\definefontfeature [baseb] [default] [expansion=qualityb]
70
71\definefont [FontA] [Serif*basea]
72\definefont [FontB] [Serif*baseb]
73
74\stopbuffer
75
76\typebuffer[option=TEX] \getbuffer
77
78\startbuffer[sample]
79    \setupalign[verytolerant,stretch,hz] % hz triggers expansion
80    \dorecurse {30} {%
81        {\FontB \darkred test me #1,} \FontA \dorecurse{#1}{test ##1, }%
82    }\par
83\stopbuffer
84
85In \in {figure} [hz:1] we see this in action, using the following code:
86
87\typebuffer[sample][option=TEX]
88
89\startplacefigure[reference=hz:1]
90    \getbuffer[sample]
91\stopplacefigure
92
93There is one drawback with this method, although so far I never heard a user
94complain, which can be an indication of how this mechanism is used: you cannot
95mix fonts with different step, stretch and|/|or shrink. As we just did this in
96the example, this statement is not really true in \LUAMETATEX: there we only need
97to keep the step the same. This is compatible in the sense that otherwise we
98would quit the run, so at least we carry on: the smallest stretch and shrink is
99taken. But, we do issue a warning (once) because there can be side effects! This
100is not that pretty a solution anyway because it depends on what font is used
101first.
102
103It is for this reason that we have another option: in \CONTEXT\ \LMTX\ you can
104define a specific expansion:
105
106\startbuffer
107\defineexpansion
108  [myexpansion]
109  [step=1, % default
110   stretch=50,
111   shrink=20]
112\stopbuffer
113
114\typebuffer[option=TEX] \getbuffer
115
116There is no need to have a different step than~1. In \PDFTEX\ instances are
117created per step used, but in \LUATEX\ this is more fluid. There is no gain in
118using different steps. We just keep it for compatibility reasons. This specific
119expansion is enables with:
120
121\starttyping[option=TEX]
122\setexpansion[myexpansion]
123\stoptyping
124
125and the result is shown in \in {figure} [hz:2]. This time the set expansion wins
126over the one set in the font. All fonts that have the expansion feature set are
127treated the same. By using this method you can locally have different values.
128
129\startplacefigure[reference=hz:2]
130    \setexpansion[myexpansion]
131    \getbuffer[sample]
132\stopplacefigure
133
134Deep down we use some new primitives:
135
136\starttyping[option=TEX]
137\adjustspacingstep
138\adjustspacingstretch
139\adjustspacingshrink
140\stoptyping
141
142The step is limited to 100 (10\percent) and the stretch and shrink to 500
143(50\percent) and the stretch to 1000 (100\percent) but these extremes are only
144useful for examples.
145
146\stopchapter
147
148\stopcomponent
149