typo-txt.mklx /size: 8078 b    last modification: 2023-12-21 09:44
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 want to typeset some
26%D text spread in a well defined area, it can be considered bad practice to
27%D manipulate character and word spacing. In such situations the next few macros can
28%D be of help:
29%D
30%D \starttyping
31%D \normalizefontheight \name {sample text} {height} {font}
32%D \normalizefontwidth  \name {sample text} {width}  {font}
33%D \stoptyping
34%D
35%D Consider for instance:
36%D
37%D \startbuffer[a]
38%D \normalizefontheight \TempFont {X} {2\baselineskip} {Serif}
39%D \stopbuffer
40%D
41%D \startbuffer[b]
42%D \ruledhbox{\TempFont To Be Or Not To Be}
43%D \stopbuffer
44%D
45%D \typebuffer[a,b] \getbuffer[a]
46%D
47%D This shows up as:
48%D
49%D \startlinecorrection
50%D \ruledhbox{\getbuffer[b]}
51%D \stoplinecorrection
52%D
53%D The horizontal counterpart is:
54%D
55%D \startbuffer[a]
56%D \normalizefontwidth \TempFont {This Line Fits} {\hsize} {Serif}
57%D \stopbuffer
58%D
59%D \startbuffer[b]
60%D \ruledhbox{\TempFont This Line Fits}
61%D \stopbuffer
62%D
63%D \typebuffer[a,b] \getbuffer[a]
64%D
65%D This gives:
66%D
67%D \startlinecorrection
68%D \ruledhbox{\getbuffer[b]}
69%D \stoplinecorrection
70%D
71%D The calculated font scale is avaliable in \type {\normalizedfontsize}.
72
73\newbox\b_typo_normalizers
74
75\def\typo_normalizers_size#axis#size%
76  {\dimexpr\ifdim#1\b_typo_normalizers=\zeropoint
77     \bodyfontsize
78   \else
79     \luaexpr{\number\dimexpr10pt\relax*\number\dimexpr#size\relax/\number#axis\b_typo_normalizers}\scaledpoint
80   \fi\relax}
81
82\def\typo_normalizers_font_at_size#axis#cs#text#size#font% avoid overflow by using lua
83  {\begingroup
84   \setbox\b_typo_normalizers\hbox{\definedfont[#font at 10pt]\settrialtypesetting#text}%
85   \normalexpanded{\endgroup\enforced\edef\noexpand\normalizedfontsize{\the\typo_normalizers_size{#axis}{#size}}}%
86   \definefont[\strippedcsname#cs][#font at \normalizedfontsize]}
87
88\protected\def\typo_normalizers_text_at_size#axis#font#size#text%
89  {\dontleavehmode
90   \begingroup
91   \setbox\b_typo_normalizers\hbox{\definedfont[#font at 10pt]\settrialtypesetting#text}%
92   \definedfont[#font at \the\typo_normalizers_size{#axis}{#size}]#text%
93   \endgroup}
94
95\mutable\def\normalizedfontsize{\bodyfontsize}
96
97\permanent\protected\def\normalizetextwidth {\typo_normalizers_text_at_size\wd}
98\permanent\protected\def\normalizetextheight{\typo_normalizers_text_at_size\ht}
99\permanent\protected\def\normalizetextdepth {\typo_normalizers_text_at_size\dp}
100\permanent\protected\def\normalizetextline  {\typo_normalizers_text_at_size\htdp}
101
102\permanent\protected\def\normalizefontwidth {\typo_normalizers_font_at_size\wd}
103\permanent\protected\def\normalizefontheight{\typo_normalizers_font_at_size\ht}
104\permanent\protected\def\normalizefontdepth {\typo_normalizers_font_at_size\dp}
105\permanent\protected\def\normalizefontline  {\typo_normalizers_font_at_size\htdp}
106
107\permanent\protected\def\widthspanningtext #text#size#specification{\hbox{\normalizefontwidth \temp{#text}{#size}{#specification}\temp#text}}
108\permanent\protected\def\heightspanningtext#text#size#specification{\hbox{\normalizefontheight\temp{#text}{#size}{#specification}\temp#text}}
109\permanent\protected\def\depthspanningtext #text#size#specification{\hbox{\normalizefontdepth \temp{#text}{#size}{#specification}\temp#text}}
110\permanent\protected\def\linespanningtext  #text#size#specification{\hbox{\normalizefontline  \temp{#text}{#size}{#specification}\temp#text}}
111
112%D Traditionally we use UpperCasedNames for this kind of functionality.
113
114\aliased\let\NormalizeFontHeight   \normalizefontheight
115\aliased\let\NormalizeFontWidth    \normalizefontwidth
116\aliased\let\NormalizeTextHeight   \normalizetextheight
117\aliased\let\NormalizeTextWidth    \normalizetextwidth
118
119\aliased\let\WidthSpanningText     \widthspanningtext
120
121\permanent\def\TheNormalizedFontSize{\normalizedfontsize}
122
123%D \macros
124%D   {vulgarfraction}
125%D
126%D This code is moved from \type {cor-mis.mkiv}. We show three versions. First
127%D the simple one using \type {\low} and \type {high}:
128%D
129%D \startbuffer
130%D \def\vfrac#1#2%
131%D   {\hbox{\high{\tx#1\kern-.25em}/\low{\kern-.25em\tx#2}}}
132%D
133%D test \vfrac{1}{2} test \vfrac{123}{456} test
134%D \stopbuffer
135%D
136%D \typebuffer {\showmakeup\getbuffer}
137%D
138%D A better way to handle the kerning is the following, here we kind of assume
139%D that tye slash is symmetrical and has nearly zero width.
140%D
141%D \startbuffer
142%D \def\vfract#1#2%
143%D   {\hbox{\high{\tx#1}\hbox to \zeropoint{\hss/\hss}\low{\tx#2}}}
144%D \stopbuffer
145%D
146%D \typebuffer {\showmakeup\getbuffer}
147%D
148%D The third and best alternative is the following:
149%D
150%D {\showmakeup\getbuffer}\crlf\getbuffer
151%D
152%D This time we measure the height of the \type {/} and shift over the maximum
153%D height and depths of this character and the fractional digits (we use 57 as
154%D sample). Here we combine all methods in one macros.
155
156\setnewconstant\vulgarfractionmethod\plusthree
157
158\definehspace[\v!vulgarfraction][.25em] % [.15em]
159\definesymbol[\v!vulgarfraction][/]     % [\raise.2ex\hbox{/}]
160
161\permanent\protected\def\vulgarfraction#1#2%
162  {\dontleavehmode
163   \hbox
164     {\def\vulgarfraction{vulgarfraction}%
165      \ifcase\vulgarfractionmethod
166        #1\symbol[\v!vulgarfraction]#2%
167      \or
168        \high{\tx#1\kern-\hspaceamount\empty\v!vulgarfraction}%
169        \symbol[\vulgarfraction]%
170        \low {\kern-\hspaceamount\empty\v!vulgarfraction\tx#2}%
171      \or
172        \high{\tx#1}%
173        \hbox to \zeropoint{\hss\symbol[\v!vulgarfraction]\hss}%
174        \low{\tx#2}%
175      \or
176        \setbox0\hbox{\symbol[\vulgarfraction]}%
177        \setbox2\hbox{\txx57}%
178        \raise\ht0\hbox{\lower\ht2\hbox{\txx#1}}%
179        \hbox to \zeropoint{\hss\symbol[\v!vulgarfraction]\hss}%
180        \lower\dp0\hbox{\raise\dp2\hbox{\txx#2}}%
181      \fi}}
182
183\ifdefined\vfrac \else \let\vfrac\vulgarfraction \fi % \aliased ?
184
185%D \starttabulate[|l|l|]
186%D \HL
187%D \NC \bf method \NC \bf visualization \NC\NR
188%D \HL
189%D \NC 0 \NC \vulgarfractionmethod0 \vulgarfraction{1}{2} \NC\NR
190%D \NC 1 \NC \vulgarfractionmethod1 \vulgarfraction{1}{2} \NC\NR
191%D \NC 2 \NC \vulgarfractionmethod2 \vulgarfraction{1}{2} \NC\NR
192%D \NC 3 \NC \vulgarfractionmethod3 \vulgarfraction{1}{2} \NC\NR
193%D \HL
194%D \stoptabulate
195
196%D This is used in the beginners manual. One needs to set the font size to an
197%D acceptable value for this to work.
198
199\permanent\tolerant\protected\def\startnicelyfilledbox[#S#1]%
200  {\vbox\bgroup
201   \forgetall
202   \letdummyparameter\c!width \hsize
203   \letdummyparameter\c!height\vsize
204   \letdummyparameter\c!offset\exheight % we obey to the outer exheight
205   \letdummyparameter\c!strut \v!yes    % we obey to the inner strut !
206   \getdummyparameters[#1]%
207   \scratchoffset\dummyparameter\c!offset\relax
208   \setbox\scratchbox\vbox to \dummyparameter\c!height \bgroup
209   \hsize\dummyparameter\c!width\relax
210   \emergencystretch10\scratchoffset
211   \parfillskip\zeroskip
212   \parfillleftskip\zeroskip
213   \leftskip \scratchoffset
214   \rightskip\scratchoffset}
215
216\permanent\protected\def\stopnicelyfilledbox
217  {\egroup
218   \vbox to \dummyparameter\c!height \bgroup
219     \ifcstok{\dummyparameter\c!strut}\v!yes
220       \dorecurse{\boxlines\scratchbox}%
221         {\boxlineht\scratchbox\recurselevel\strutht
222          \boxlinedp\scratchbox\recurselevel\strutdp
223          \boxline\scratchbox\recurselevel
224          \vss}%
225     \else
226       \dorecurse{\boxlines\scratchbox}%
227         {\boxline\scratchbox\recurselevel
228          \vss}%
229     \fi
230     \unskip
231   \egroup
232   \egroup}
233
234\protect \endinput
235