summaryrefslogtreecommitdiff
path: root/tex/context/base/typo-txt.mkvi
blob: 7562fe70cd049ecc35c8261aecfddad4070d25c2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
%D \module
%D   [       file=typo-txt,
%D        version=2011.10.27,
%D          title=\CONTEXT\ Typesetting Macros,
%D       subtitle=Text Hacks,
%D         author=Hans Hagen,
%D           date=\currentdate,
%D      copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
%C
%C This module is part of the \CONTEXT\ macro||package and is
%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
%C details.

%D This module replaces the by now rather old supp-fun module.

\writestatus{loading}{ConTeXt Typesetting Macros / Text Hacks}

\unprotect

% registerctxluafile{typo-txt}{1.001}

%D \macros
%D   {normalizefontheight,normalizefontwidth,normalizedfontsize}
%D
%D Next we introduce some font manipulation macros. When we
%D want to typeset some text spread in a well defined area, it
%D can be considered bad practice to manipulate character and
%D word spacing. In such situations the next few macros can be
%D of help:
%D
%D \starttyping
%D \normalizefontheight \name {sample text} {height} {font}
%D \normalizefontwidth  \name {sample text} {width}  {font}
%D \stoptyping
%D
%D Consider for instance:
%D
%D \startbuffer[a]
%D \normalizefontheight \TempFont {X} {2\baselineskip} {Serif}
%D \stopbuffer
%D
%D \startbuffer[b]
%D \ruledhbox{\TempFont To Be Or Not To Be}
%D \stopbuffer
%D
%D \typebuffer[a,b] \getbuffer[a]
%D
%D This shows up as:
%D
%D \startlinecorrection
%D \ruledhbox{\getbuffer[b]}
%D \stoplinecorrection
%D
%D The horizontal counterpart is:
%D
%D \startbuffer[a]
%D \normalizefontwidth \TempFont {This Line Fits} {\hsize} {Serif}
%D \stopbuffer
%D
%D \startbuffer[b]
%D \ruledhbox{\TempFont This Line Fits}
%D \stopbuffer
%D
%D \typebuffer[a,b] \getbuffer[a]
%D
%D This gives:
%D
%D \startlinecorrection
%D \ruledhbox{\getbuffer[b]}
%D \stoplinecorrection
%D
%D The calculated font scale is avaliable in \type {\normalizedfontsize}.

\newbox\b_typo_normalizers

\def\typo_normalizers_size#axis#size%
  {\dimexpr\ifdim#1\b_typo_normalizers=\zeropoint
     \bodyfontsize
   \else
     \luaexpr{\number\dimexpr10pt\relax*\number\dimexpr#size\relax/\number#axis\b_typo_normalizers}\scaledpoint
   \fi\relax}

\def\typo_normalizers_font_at_size#axis#cs#text#size#font% avoid overflow by using lua
  {\begingroup
   \setbox\b_typo_normalizers\hbox{\definedfont[#font at 10pt]\settrialtypesetting#text}%
   \normalexpanded{\endgroup\edef\noexpand\normalizedfontsize{\the\typo_normalizers_size{#axis}{#size}}}%
   \definefont[\strippedcsname#cs][#font at \normalizedfontsize]}

\unexpanded\def\typo_normalizers_text_at_size#axis#font#size#text%
  {\dontleavehmode
   \begingroup
   \setbox\b_typo_normalizers\hbox{\definedfont[#font at 10pt]\settrialtypesetting#text}%
   \definedfont[#font at \the\typo_normalizers_size{#axis}{#size}]#text%
   \endgroup}

\def\normalizedfontsize{\bodyfontsize}

\unexpanded\def\normalizetextwidth {\typo_normalizers_text_at_size\wd}
\unexpanded\def\normalizetextheight{\typo_normalizers_text_at_size\ht}
\unexpanded\def\normalizetextdepth {\typo_normalizers_text_at_size\dp}
\unexpanded\def\normalizetextline  {\typo_normalizers_text_at_size\htdp}

\unexpanded\def\normalizefontwidth {\typo_normalizers_font_at_size\wd}
\unexpanded\def\normalizefontheight{\typo_normalizers_font_at_size\ht}
\unexpanded\def\normalizefontdepth {\typo_normalizers_font_at_size\dp}
\unexpanded\def\normalizefontline  {\typo_normalizers_font_at_size\htdp}

\unexpanded\def\widthspanningtext #text#size#specification{\hbox{\normalizefontwidth \temp{#text}{#size}{#specification}\temp#text}}
\unexpanded\def\heightspanningtext#text#size#specification{\hbox{\normalizefontheight\temp{#text}{#size}{#specification}\temp#text}}
\unexpanded\def\depthspanningtext #text#size#specification{\hbox{\normalizefontdepth \temp{#text}{#size}{#specification}\temp#text}}
\unexpanded\def\linespanningtext  #text#size#specification{\hbox{\normalizefontline  \temp{#text}{#size}{#specification}\temp#text}}

%D Traditionally we use UpperCasedNames for this kind of functionality.

\let\NormalizeFontHeight   \normalizefontheight
\let\NormalizeFontWidth    \normalizefontwidth
\let\NormalizeTextHeight   \normalizetextheight
\let\NormalizeTextWidth    \normalizetextwidth

\let\WidthSpanningText     \widthspanningtext

\def\TheNormalizedFontSize{\normalizedfontsize}

%D \macros
%D   {vulgarfraction}
%D
%D This code is moved from \type {cor-mis.mkiv}. We show three versions. First
%D the simple one using \type {\low} and \type {high}:
%D
%D \startbuffer
%D \def\vfrac#1#2%
%D   {\hbox{\high{\tx#1\kern-.25em}/\low{\kern-.25em\tx#2}}}
%D
%D test \vfrac{1}{2} test \vfrac{123}{456} test
%D \stopbuffer
%D
%D \typebuffer {\showmakeup\getbuffer}
%D
%D A better way to handle the kerning is the following, here we kind of assume
%D that tye slash is symmetrical and has nearly zero width.
%D
%D \startbuffer
%D \def\vfract#1#2%
%D   {\hbox{\high{\tx#1}\hbox to \zeropoint{\hss/\hss}\low{\tx#2}}}
%D \stopbuffer
%D
%D \typebuffer {\showmakeup\getbuffer}
%D
%D The third and best alternative is the following:
%D
%D {\showmakeup\getbuffer}\crlf\getbuffer
%D
%D This time we measure the height of the \type {/} and shift over the maximum
%D height and depths of this character and the fractional digits (we use 57 as
%D sample). Here we combine all methods in one macros.

\setnewconstant\vulgarfractionmethod\plusthree

\definehspace[\v!vulgarfraction][.25em] % [.15em]
\definesymbol[\v!vulgarfraction][/]     % [\raise.2ex\hbox{/}]

\unexpanded\def\vulgarfraction#1#2%
  {\dontleavehmode
   \hbox
     {\def\vulgarfraction{vulgarfraction}%
      \ifcase\vulgarfractionmethod
        #1\symbol[\v!vulgarfraction]#2%
      \or
        \high{\tx#1\kern-\hspaceamount\empty\v!vulgarfraction}%
        \symbol[\vulgarfraction]%
        \low {\kern-\hspaceamount\empty\v!vulgarfraction\tx#2}%
      \or
        \high{\tx#1}%
        \hbox to \zeropoint{\hss\symbol[\v!vulgarfraction]\hss}%
        \low{\tx#2}%
      \or
        \setbox0\hbox{\symbol[\vulgarfraction]}%
        \setbox2\hbox{\txx57}%
        \raise\ht0\hbox{\lower\ht2\hbox{\txx#1}}%
        \hbox to \zeropoint{\hss\symbol[\v!vulgarfraction]\hss}%
        \lower\dp0\hbox{\raise\dp2\hbox{\txx#2}}%
      \fi}}

\ifdefined\vfrac \else \let\vfrac\vulgarfraction \fi

%D \starttabulate[|l|l|]
%D \HL
%D \NC \bf method \NC \bf visualization \NC\NR
%D \HL
%D \NC 0 \NC \vulgarfractionmethod0 \vulgarfraction{1}{2} \NC\NR
%D \NC 1 \NC \vulgarfractionmethod1 \vulgarfraction{1}{2} \NC\NR
%D \NC 2 \NC \vulgarfractionmethod2 \vulgarfraction{1}{2} \NC\NR
%D \NC 3 \NC \vulgarfractionmethod3 \vulgarfraction{1}{2} \NC\NR
%D \HL
%D \stoptabulate

%D This is used in the beginners manual. One needs to set the font size to an
%D acceptable value for this to work.

\unexpanded\def\startnicelyfilledbox
  {\vbox\bgroup
   \forgetall
   \dosingleempty\dostartnicelyfilledbox}

\def\dostartnicelyfilledbox[#1]%
  {\letdummyparameter\c!width \hsize
   \letdummyparameter\c!height\vsize
   \letdummyparameter\c!offset\exheight % we obey to the outer exheight
   \letdummyparameter\c!strut \v!yes    % we obey to the inner strut !
   \getdummyparameters[#1]%
   \scratchoffset\dummyparameter\c!offset\relax
   \setbox\scratchbox\vbox to \dummyparameter\c!height \bgroup
   \hsize\dummyparameter\c!width\relax
   \emergencystretch10\scratchoffset
   \parfillskip\zeropoint
   \baselineskip\zeropoint plus \onepoint minus \onepoint
   \beginofshapebox
   \leftskip \scratchoffset
   \rightskip\scratchoffset}

\unexpanded\def\stopnicelyfilledbox
  {\doifelse{\dummyparameter\c!strut}\v!yes
     {\xdef\doflushnicelyfilledbox
        {\ht\shapebox\the\strutht
         \dp\shapebox\the\strutdp
         \box\shapebox}}%
     {\gdef\doflushnicelyfilledbox
        {\box\shapebox}}%
   \endofshapebox
   \doreshapebox
     {\doflushnicelyfilledbox}
     {\penalty\shapepenalty}
     {\kern\shapekern}
     {\vfil}%
   \kern\scratchoffset
   \vfilneg
   \flushshapebox
   \vfilneg
   \kern\scratchoffset
   \egroup
   \box\scratchbox
   \egroup}

\protect \endinput