typo-txt.mkvi /size: 8008 b    last modification: 2020-07-01 14:35
1
%D \module
2
%D [ file=typo-txt,
3
%D version=2011.10.27,
4
%D title=\CONTEXT\ Typesetting Macros,
5
%D subtitle=Text Hacks,
6
%D author=Hans Hagen,
7
%D date=\currentdate,
8
%D copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
9
%C
10
%C This module is part of the \CONTEXT\ macro||package and is
11
%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
12
%C details.
13 14
%D This module replaces the by now rather old supp-fun module.
15 16
\writestatus
{
loading
}
{
ConTeXt
Typesetting
Macros
/
Text
Hacks
}
17 18
\unprotect
19 20
% registerctxluafile{typo-txt}{}
21 22
%D \macros
23
%D {normalizefontheight,normalizefontwidth,normalizedfontsize}
24
%D
25
%D Next we introduce some font manipulation macros. When we
26
%D want to typeset some text spread in a well defined area, it
27
%D can be considered bad practice to manipulate character and
28
%D word spacing. In such situations the next few macros can be
29
%D of help:
30
%D
31
%D \starttyping
32
%D \normalizefontheight \name {sample text} {height} {font}
33
%D \normalizefontwidth \name {sample text} {width} {font}
34
%D \stoptyping
35
%D
36
%D Consider for instance:
37
%D
38
%D \startbuffer[a]
39
%D \normalizefontheight \TempFont {X} {2\baselineskip} {Serif}
40
%D \stopbuffer
41
%D
42
%D \startbuffer[b]
43
%D \ruledhbox{\TempFont To Be Or Not To Be}
44
%D \stopbuffer
45
%D
46
%D \typebuffer[a,b] \getbuffer[a]
47
%D
48
%D This shows up as:
49
%D
50
%D \startlinecorrection
51
%D \ruledhbox{\getbuffer[b]}
52
%D \stoplinecorrection
53
%D
54
%D The horizontal counterpart is:
55
%D
56
%D \startbuffer[a]
57
%D \normalizefontwidth \TempFont {This Line Fits} {\hsize} {Serif}
58
%D \stopbuffer
59
%D
60
%D \startbuffer[b]
61
%D \ruledhbox{\TempFont This Line Fits}
62
%D \stopbuffer
63
%D
64
%D \typebuffer[a,b] \getbuffer[a]
65
%D
66
%D This gives:
67
%D
68
%D \startlinecorrection
69
%D \ruledhbox{\getbuffer[b]}
70
%D \stoplinecorrection
71
%D
72
%D The calculated font scale is avaliable in \type {\normalizedfontsize}.
73 74
\newbox
\b_typo_normalizers
75 76
\def
\typo_normalizers_size
#
axis
#
size
%
77
{
\dimexpr
\ifdim
#
1
\b_typo_normalizers
=
\zeropoint
78
\bodyfontsize
79
\else
80
\luaexpr{
\number
\dimexpr
10
pt
\relax
*
\number
\dimexpr
#
size
\relax
/
\number
#
axis
\b_typo_normalizers
}
\scaledpoint
81
\fi
\relax
}
82 83
\def
\typo_normalizers_font_at_size
#
axis
#
cs
#
text
#
size
#
font
% avoid overflow by using lua
84
{
\begingroup
85
\setbox
\b_typo_normalizers
\hbox
{
\definedfont
[
#
font
at
1
0
pt
]
\settrialtypesetting
#
text
}
%
86
\normalexpanded
{
\endgroup
\edef
\noexpand
\normalizedfontsize
{
\the
\typo_normalizers_size
{
#
axis
}
{
#
size
}
}
}
%
87
\definefont
[
\strippedcsname
#
cs
]
[
#
font
at
\normalizedfontsize
]
}
88 89
\unexpanded
\def
\typo_normalizers_text_at_size
#
axis
#
font
#
size
#
text
%
90
{
\dontleavehmode
91
\begingroup
92
\setbox
\b_typo_normalizers
\hbox
{
\definedfont
[
#
font
at
1
0
pt
]
\settrialtypesetting
#
text
}
%
93
\definedfont
[
#
font
at
\the
\typo_normalizers_size
{
#
axis
}
{
#
size
}
]
#
text
%
94
\endgroup
}
95 96
\def
\normalizedfontsize
{
\bodyfontsize
}
97 98
\unexpanded
\def
\normalizetextwidth
{
\typo_normalizers_text_at_size
\wd
}
99
\unexpanded
\def
\normalizetextheight
{
\typo_normalizers_text_at_size
\ht
}
100
\unexpanded
\def
\normalizetextdepth
{
\typo_normalizers_text_at_size
\dp
}
101
\unexpanded
\def
\normalizetextline
{
\typo_normalizers_text_at_size
\htdp
}
102 103
\unexpanded
\def
\normalizefontwidth
{
\typo_normalizers_font_at_size
\wd
}
104
\unexpanded
\def
\normalizefontheight
{
\typo_normalizers_font_at_size
\ht
}
105
\unexpanded
\def
\normalizefontdepth
{
\typo_normalizers_font_at_size
\dp
}
106
\unexpanded
\def
\normalizefontline
{
\typo_normalizers_font_at_size
\htdp
}
107 108
\unexpanded
\def
\widthspanningtext
#
text
#
size
#
specification
{
\hbox
{
\normalizefontwidth
\temp
{
#
text
}
{
#
size
}
{
#
specification
}
\temp
#
text
}
}
109
\unexpanded
\def
\heightspanningtext
#
text
#
size
#
specification
{
\hbox
{
\normalizefontheight
\temp
{
#
text
}
{
#
size
}
{
#
specification
}
\temp
#
text
}
}
110
\unexpanded
\def
\depthspanningtext
#
text
#
size
#
specification
{
\hbox
{
\normalizefontdepth
\temp
{
#
text
}
{
#
size
}
{
#
specification
}
\temp
#
text
}
}
111
\unexpanded
\def
\linespanningtext
#
text
#
size
#
specification
{
\hbox
{
\normalizefontline
\temp
{
#
text
}
{
#
size
}
{
#
specification
}
\temp
#
text
}
}
112 113
%D Traditionally we use UpperCasedNames for this kind of functionality.
114 115
\let
\NormalizeFontHeight
\normalizefontheight
116
\let
\NormalizeFontWidth
\normalizefontwidth
117
\let
\NormalizeTextHeight
\normalizetextheight
118
\let
\NormalizeTextWidth
\normalizetextwidth
119 120
\let
\WidthSpanningText
\widthspanningtext
121 122
\def
\TheNormalizedFontSize
{
\normalizedfontsize
}
123 124
%D \macros
125
%D {vulgarfraction}
126
%D
127
%D This code is moved from \type {cor-mis.mkiv}. We show three versions. First
128
%D the simple one using \type {\low} and \type {high}:
129
%D
130
%D \startbuffer
131
%D \def\vfrac#1#2%
132
%D {\hbox{\high{\tx#1\kern-.25em}/\low{\kern-.25em\tx#2}}}
133
%D
134
%D test \vfrac{1}{2} test \vfrac{123}{456} test
135
%D \stopbuffer
136
%D
137
%D \typebuffer {\showmakeup\getbuffer}
138
%D
139
%D A better way to handle the kerning is the following, here we kind of assume
140
%D that tye slash is symmetrical and has nearly zero width.
141
%D
142
%D \startbuffer
143
%D \def\vfract#1#2%
144
%D {\hbox{\high{\tx#1}\hbox to \zeropoint{\hss/\hss}\low{\tx#2}}}
145
%D \stopbuffer
146
%D
147
%D \typebuffer {\showmakeup\getbuffer}
148
%D
149
%D The third and best alternative is the following:
150
%D
151
%D {\showmakeup\getbuffer}\crlf\getbuffer
152
%D
153
%D This time we measure the height of the \type {/} and shift over the maximum
154
%D height and depths of this character and the fractional digits (we use 57 as
155
%D sample). Here we combine all methods in one macros.
156 157
\setnewconstant
\vulgarfractionmethod
\plusthree
158 159
\definehspace
[
\v!vulgarfraction
]
[
.
2
5
em
]
% [.15em]
160
\definesymbol
[
\v!vulgarfraction
]
[
/
]
% [\raise.2ex\hbox{/}]
161 162
\unexpanded
\def
\vulgarfraction
#
1
#
2
%
163
{
\dontleavehmode
164
\hbox
165
{
\def
\vulgarfraction
{
vulgarfraction
}
%
166
\ifcase
\vulgarfractionmethod
167
#
1
\symbol
[
\v!vulgarfraction
]
#
2
%
168
\or
169
\high
{
\tx
#
1
\kern
-
\hspaceamount
\empty
\v!vulgarfraction
}
%
170
\symbol
[
\vulgarfraction
]
%
171
\low
{
\kern
-
\hspaceamount
\empty
\v!vulgarfraction
\tx
#
2
}
%
172
\or
173
\high
{
\tx
#
1
}
%
174
\hbox
to
\zeropoint
{
\hss
\symbol
[
\v!vulgarfraction
]
\hss
}
%
175
\low
{
\tx
#
2
}
%
176
\or
177
\setbox
0
\hbox
{
\symbol
[
\vulgarfraction
]
}
%
178
\setbox
2
\hbox
{
\txx
5
7
}
%
179
\raise
\ht
0
\hbox
{
\lower
\ht
2
\hbox
{
\txx
#
1
}
}
%
180
\hbox
to
\zeropoint
{
\hss
\symbol
[
\v!vulgarfraction
]
\hss
}
%
181
\lower
\dp
0
\hbox
{
\raise
\dp
2
\hbox
{
\txx
#
2
}
}
%
182
\fi
}
}
183 184
\ifdefined
\vfrac
\else
\let
\vfrac
\vulgarfraction
\fi
185 186
%D \starttabulate[|l|l|]
187
%D \HL
188
%D \NC \bf method \NC \bf visualization \NC\NR
189
%D \HL
190
%D \NC 0 \NC \vulgarfractionmethod0 \vulgarfraction{1}{2} \NC\NR
191
%D \NC 1 \NC \vulgarfractionmethod1 \vulgarfraction{1}{2} \NC\NR
192
%D \NC 2 \NC \vulgarfractionmethod2 \vulgarfraction{1}{2} \NC\NR
193
%D \NC 3 \NC \vulgarfractionmethod3 \vulgarfraction{1}{2} \NC\NR
194
%D \HL
195
%D \stoptabulate
196 197
%D This is used in the beginners manual. One needs to set the font size to an
198
%D acceptable value for this to work.
199 200
\unexpanded
\def
\startnicelyfilledbox
201
{
\vbox
\bgroup
202
\forgetall
203
\dosingleempty
\dostartnicelyfilledbox
}
204 205
\def
\dostartnicelyfilledbox
[
#
1
]
%
206
{
\letdummyparameter
\c!width
\hsize
207
\letdummyparameter
\c!height
\vsize
208
\letdummyparameter
\c!offset
\exheight
% we obey to the outer exheight
209
\letdummyparameter
\c!strut
\v!yes
% we obey to the inner strut !
210
\getdummyparameters
[
#
1
]
%
211
\scratchoffset
\dummyparameter
\c!offset
\relax
212
\setbox
\scratchbox
\vbox
to
\dummyparameter
\c!height
\bgroup
213
\hsize
\dummyparameter
\c!width
\relax
214
\emergencystretch
1
0
\scratchoffset
215
\parfillskip
\zeropoint
216
\baselineskip
\zeropoint
plus
\onepoint
minus
\onepoint
217
\beginofshapebox
218
\leftskip
\scratchoffset
219
\rightskip
\scratchoffset
}
220 221
\unexpanded
\def
\stopnicelyfilledbox
222
{
\doifelse
{
\dummyparameter
\c!strut
}
\v!yes
223
{
\xdef
\doflushnicelyfilledbox
224
{
\ht
\shapebox
\the
\strutht
225
\dp
\shapebox
\the
\strutdp
226
\box
\shapebox
}
}
%
227
{
\gdef
\doflushnicelyfilledbox
228
{
\box
\shapebox
}
}
%
229
\endofshapebox
230
\doreshapebox
231
{
\doflushnicelyfilledbox
}
232
{
\penalty
\shapepenalty
}
233
{
\kern
\shapekern
}
234
{
\vfil
}
%
235
\kern
\scratchoffset
236
\vfilneg
237
\flushshapebox
238
\vfilneg
239
\kern
\scratchoffset
240
\egroup
241
\box
\scratchbox
242
\egroup
}
243 244
\protect
\endinput
245