spacing-spaces.tex /size: 9376 b    last modification: 2021-10-28 13:50
1% language=us runpath=texruns:manuals/spacing
2
3\environment spacing-style
4
5\startcomponent spacing-periods
6
7\startchapter[title=Spacing]
8
9\startsection[title=Spaces]
10
11When \TEX\ reads its input and transforms content it into a node list spaces are
12turned into glue nodes of subtype \quote {spaceskip} or \quote {xspaceskip}. In
13pseudo code, this is what happens:
14
15\starttyping
16if spacefactor >= 2000 and xspaceskip ~= 0 then
17    space   = xspaceskip_space
18    stretch = xspaceskip_stretch
19    shrink  = xspaceskip_shrink
20    subtype = xspaceskip
21else
22    if spaceskip == 0 then
23        space   = font_space
24        stretch = font_stretch
25        shrink  = font_shrink
26    else
27        space   = spaceskip_space
28        stretch = spaceskip_stretch
29        shrink  = spaceskip_shrink
30    end
31    if space_factor >= 2000
32        space = space + font_extraspace
33    end
34    stretch = stretch * space_factor
35    shrink  = shrink  * space_factor
36    subtype = spaceskip
37end
38insert_glue(space,stretch,shrink,subtype)
39\stoptyping
40
41We demonstrate the effects in a few examples. You can use \typ {\showmakeup [space]} to
42visualize spaces.
43
44\def\ShowSpaces#1#2#3%
45  {\blank
46   \start
47   \startsubsubsubject[title={case #1: \type{#2} and \type {#3}}]
48   \dontleavehmode
49   #2\relax
50   #3\relax
51   \showmakeup[space]%
52 % \definedfont[Serif*default]%
53   \nonfrenchspacing
54   \strut x\space x.\space x\crlf
55   \frenchspacing
56   \strut x\space x.\space x\par
57   \stop
58   \stopsubsubject
59   \blank}
60
61\ShowSpaces{1}{\spacefactor2000}{\xspaceskip100pt}
62
63\starttabulate[|l|l|]
64\HL
65\NC \type {\spacefactor} \NC $\geq 2000$ \NC \NR
66\NC \type {\xspaceskip}  \NC $\neq    0$ \NC \NR
67\HL
68\NC space   \NC xspaceskip_space   \NC \NR
69\NC stretch \NC xspaceskip_stretch \NC \NR
70\NC shrink  \NC xspaceskip_shrink  \NC \NR
71\NC subtype \NC xspaceskip         \NC \NR
72\HL
73\stoptabulate
74
75\ShowSpaces{2}{\spacefactor1000}{\spaceskip0pt}
76
77\starttabulate[|l|l|]
78\HL
79\NC \type {\spacefactor} \NC $\lt 2000$ \NC \NR
80\NC \type {\spaceskip}   \NC $\eq    0$ \NC \NR
81\HL
82\NC space   \NC font_space   \NC \NR
83\NC stretch \NC font_stretch \NC \NR
84\NC shrink  \NC font_shrink  \NC \NR
85\NC subtype \NC spaceskip    \NC \NR
86\HL
87\stoptabulate
88
89\ShowSpaces{3}{\spacefactor2000}{\spaceskip0pt}
90
91\starttabulate[|l|l|]
92\HL
93\NC \type {\spacefactor} \NC $\geq 2000$ \NC \NR
94\NC \type {\spaceskip}   \NC $\eq     0$ \NC \NR
95\HL
96\NC space   \NC font_space $+$ extraspace_font \NC \NR
97\NC stretch \NC font_stretch                   \NC \NR
98\NC shrink  \NC font_shrink                    \NC \NR
99\NC subtype \NC spaceskip                      \NC \NR
100\HL
101\stoptabulate
102
103\ShowSpaces{4}{\spacefactor1000}{\spaceskip100pt}
104
105\starttabulate[|l|l|]
106\HL
107\NC \type {\spacefactor} \NC $\lt  2000$ \NC \NR
108\NC \type {\spaceskip}   \NC $\neq    0$ \NC \NR
109\HL
110\NC space   \NC font_space                         \NC \NR
111\NC stretch \NC font_stretch $\times$ space_factor \NC \NR
112\NC shrink  \NC font_shrink  $\times$ space_factor \NC \NR
113\NC subtype \NC spaceskip                          \NC \NR
114\HL
115\stoptabulate
116
117\ShowSpaces{5}{\spacefactor2000}{\spaceskip100pt}
118
119\starttabulate[|l|l|]
120\HL
121\NC \type {\spacefactor} \NC $\geq 2000$ \NC \NR
122\NC \type {\spaceskip}   \NC $\neq    0$ \NC \NR
123\HL
124\NC space   \NC font_space   $+$ extraspace_font   \NC \NR
125\NC stretch \NC font_stretch $\times$ space_factor \NC \NR
126\NC shrink  \NC font_shrink  $\times$ space_factor \NC \NR
127\NC subtype \NC spaceskip                          \NC \NR
128\HL
129\stoptabulate
130
131The width of a space relates to the design of a font and therefore the t width of
132the space, its stretch and its shrink are taken from the font and scaled
133accordingly. Normally we take the space character in the font as reference.
134Traditional \TEX\ fonts don't have that character but \OPENTYPE\ fonts have. When
135there is no space character, in the case of a monospaced font we take the
136emwidth, otherwise we take half the emwidth. As a last resort we can take the
137average width of characters. And of even that fails we take half of the font
138units. But, as mentioned, modern fonts have a space.
139
140In the \CONTEXT\ font loader we use a stretch that is 1/2 of the width of a space
141and the shrink is 1/3 the width of a space. These values are familiar for those
142who come from traditional \TEX.
143
144As with many variables, you can overload these values when a font is loaded by
145setting the \type {spacing} feature. Here is how this is done:
146
147\startbuffer
148\definefontfeature
149  [morespace]
150  [spacing=0.50 plus 0.50 minus 0.250]
151\definefontfeature
152  [lessspace]
153  [spacing=0.25 plus 0.25 minus 0.125]
154\definefontfeature
155  [extramorespace]
156  [spacing=0.50 plus 0.50 minus 0.250 extra 2.00]
157\definefontfeature
158  [extralessspace]
159  [spacing=0.25 plus 0.25 minus 0.125 extra 2.00]
160
161\definedfont[Serif*default]
162    \inleft{\infofont default}
163    \samplefile{klein}\par
164    \blank
165\definedfont[Serif*default,morespace]
166    \inleft{\infofont morespace}
167    \samplefile{klein}\par
168    \blank
169\definedfont[Serif*default,extramorespace]
170    \inleft{\infofont extramorespace}
171    \samplefile{klein}\par
172    \blank
173\definedfont[Serif*default,lessspace]
174    \inleft{\infofont lessspace}
175    \samplefile{klein}\par
176    \blank
177\definedfont[Serif*default,extralessspace]
178    \inleft{\infofont extralessspace}
179    \samplefile{klein}\par
180    \blank
181\stopbuffer
182
183\typebuffer
184
185For demonstration purposes we use a somewhat excessive \type {extra}
186specification. By default the extra space is equal to the shrink.
187
188\blank \start
189    \showmakeup[space]
190    \getbuffer
191\stop \blank
192
193\stopsection
194
195\startsection[title=Expansion]
196
197Spaces become glue that can shrink or stretch. In the worst case words will come
198too close, or the gap will be large. Even worse is that this can lead to
199successive lines in a paragraph looking different with respect to spacing. A solution
200for this is to use font expansion, although the benefits are often less than
201some users want (you) to believe.
202
203This mechanism is enabled with \type {\setupalign}. There are two variants (\type
204{hz} and \type {fullhz}) and a reset (\type {nohz}). In \in {figure} [fig:expansion]
205we use the following font definition:
206
207\startbuffer
208\definefont[testfont][Normal*default,quality @ 9pt]
209\stopbuffer
210
211\typebuffer \getbuffer
212
213We use \type {\showfontexpansion} to view the effective expansion factors of each
214glyph. When \type {fullhz} is used fontkerns also can get expanded. Zero values
215are not shown. The font kern factors are shown below the character factors. They
216can be neglected and one can even wonder if they need a treatment especially
217because kerns are also used for relative positioning, accent anchoring and
218cursive attachments.
219
220\startplacefigure[reference=fig:expansion,title={The two expansion methods in action.}]
221    \startcombination[nx=3,ny=2,location=top]
222                                                           {\framed[foregroundstyle=\testfont,width=.3\textwidth,align={normal,tolerant,nohz}]  {\showfontexpansion\samplefile{ward}}}   {}
223                                                           {\framed[foregroundstyle=\testfont,width=.3\textwidth,align={normal,tolerant,fullhz}]{\showfontexpansion\samplefile{ward}}}   {}
224                                                           {\framed[foregroundstyle=\testfont,width=.3\textwidth,align={normal,tolerant,hz}]    {\showfontexpansion\samplefile{ward}}}   {}
225       {\scale[width=.3\textwidth]{\clip[nx=2,ny=4,x=2,y=3]{\framed[foregroundstyle=\testfont,width=.3\textwidth,align={normal,tolerant,nohz}]  {\showfontexpansion\samplefile{ward}}}}} {\tttf{nohz}}
226       {\scale[width=.3\textwidth]{\clip[nx=2,ny=4,x=2,y=3]{\framed[foregroundstyle=\testfont,width=.3\textwidth,align={normal,tolerant,fullhz}]{\showfontexpansion\samplefile{ward}}}}} {\tttf{fullhz}}
227       {\scale[width=.3\textwidth]{\clip[nx=2,ny=4,x=2,y=3]{\framed[foregroundstyle=\testfont,width=.3\textwidth,align={normal,tolerant,hz}]    {\showfontexpansion\samplefile{ward}}}}} {\tttf{hz}}
228    \stopcombination
229\stopplacefigure
230
231\stopsection
232
233\startsection[title=Looseness]
234
235The \type {\looseness} parameter can be used to let the par builder add more
236lines, but that condition is only met when the demand is reasonable. So we need
237stretch and often also tolerance to achieve it.
238
239\starttyping
240\looseness=1 ... text ... \par
241\stoptyping
242
243This setting is reset afterwards. Because \type {framed} does some grouping deep down,
244we need either to use it in there like this:
245
246\starttyping
247\framed
248  [align={normal,verytolerant,stretch},strut=no]
249  {\looseness1 ... \par}
250\stoptyping
251
252which is somewhat clumsy, or we can do:
253
254\starttyping
255\framed
256  [align={normal,verytolerant,stretch,2*more}]
257  {...}
258\stoptyping
259
260This is demonstrated in \in {figure} [fig:looseness].
261
262\startplacefigure[reference=fig:looseness,title={Looseness in action.}]
263    \dontcomplain
264    \startcombination[location=top,nx=3,ny=1]
265        {\framed[foregroundstyle=small,align={normal,verytolerant,stretch},       width=.3\textwidth]{\samplefile{sapolsky}\unskip}} {}
266        {\framed[foregroundstyle=small,align={normal,verytolerant,stretch,1*more},width=.3\textwidth]{\samplefile{sapolsky}\unskip}} {\type {1*more}}
267        {\framed[foregroundstyle=small,align={normal,verytolerant,stretch,2*more},width=.3\textwidth]{\samplefile{sapolsky}\unskip}} {\type {2*more}}
268    \stopcombination
269\stopplacefigure
270
271\stopsection
272
273\stopchapter
274
275\stopcomponent
276
277