typo-txt.mklx /size: 8100 b    last modification: 2025-02-21 11:03
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  {\todimension{%
77     \ifdim{#axis\b_typo_normalizers}=\zeropoint
78       \bodyfontsize
79     \else % \tenpoint
80       \luaexpr{\number\dimexpr10pt\relax*\number\dimexpr#size\relax/\number#axis\b_typo_normalizers}\scaledpoint
81     \fi}}
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 10pt]\settrialtypesetting#text}%
86   \normalexpanded{\endgroup\enforced\edef\noexpand\normalizedfontsize{\typo_normalizers_size{#axis}{#size}}}%
87   \definefont[\strippedcsname#cs][#font at \normalizedfontsize]}
88
89\protected\def\typo_normalizers_text_at_size#axis#font#size#text%
90  {\dontleavehmode
91   \begingroup
92   \setbox\b_typo_normalizers\hbox{\definedfont[#font at 10pt]\settrialtypesetting#text}%
93   \definedfont[#font at \typo_normalizers_size{#axis}{#size}]#text%
94   \endgroup}
95
96\mutable\def\normalizedfontsize{\bodyfontsize}
97
98\permanent\protected\def\normalizetextwidth {\typo_normalizers_text_at_size\wd}
99\permanent\protected\def\normalizetextheight{\typo_normalizers_text_at_size\ht}
100\permanent\protected\def\normalizetextdepth {\typo_normalizers_text_at_size\dp}
101\permanent\protected\def\normalizetextline  {\typo_normalizers_text_at_size\htdp}
102
103\permanent\protected\def\normalizefontwidth {\typo_normalizers_font_at_size\wd}
104\permanent\protected\def\normalizefontheight{\typo_normalizers_font_at_size\ht}
105\permanent\protected\def\normalizefontdepth {\typo_normalizers_font_at_size\dp}
106\permanent\protected\def\normalizefontline  {\typo_normalizers_font_at_size\htdp}
107
108\permanent\protected\def\widthspanningtext #text#size#specification{\hbox{\normalizefontwidth \temp{#text}{#size}{#specification}\temp#text}}
109\permanent\protected\def\heightspanningtext#text#size#specification{\hbox{\normalizefontheight\temp{#text}{#size}{#specification}\temp#text}}
110\permanent\protected\def\depthspanningtext #text#size#specification{\hbox{\normalizefontdepth \temp{#text}{#size}{#specification}\temp#text}}
111\permanent\protected\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\aliased\let\NormalizeFontHeight   \normalizefontheight
116\aliased\let\NormalizeFontWidth    \normalizefontwidth
117\aliased\let\NormalizeTextHeight   \normalizetextheight
118\aliased\let\NormalizeTextWidth    \normalizetextwidth
119
120\aliased\let\WidthSpanningText     \widthspanningtext
121
122\permanent\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][.25em] % [.15em]
160\definesymbol[\v!vulgarfraction][/]     % [\raise.2ex\hbox{/}]
161
162\permanent\protected\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        \setbox0\hbox{\symbol[\vulgarfraction]}%
178        \setbox2\hbox{\txx57}%
179        \raise\ht0\hbox{\lower\ht2\hbox{\txx#1}}%
180        \hbox to \zeropoint{\hss\symbol[\v!vulgarfraction]\hss}%
181        \lower\dp0\hbox{\raise\dp2\hbox{\txx#2}}%
182      \fi}}
183
184\ifdefined\vfrac \else \let\vfrac\vulgarfraction \fi % \aliased ?
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\permanent\tolerant\protected\def\startnicelyfilledbox[#S#1]%
201  {\vbox\bgroup
202   \forgetall
203   \letdummyparameter\c!width \hsize
204   \letdummyparameter\c!height\vsize
205   \letdummyparameter\c!offset\exheight % we obey to the outer exheight
206   \letdummyparameter\c!strut \v!yes    % we obey to the inner strut !
207   \getdummyparameters[#1]%
208   \scratchoffset{\dummyparameter\c!offset}%
209   \setbox\scratchbox\vbox to {\dummyparameter\c!height} \bgroup
210   \hsize{\dummyparameter\c!width}%
211   \emergencystretch10\scratchoffset
212   \parfillskip\zeroskip
213   \parfillleftskip\zeroskip
214   \leftskip \scratchoffset
215   \rightskip\scratchoffset}
216
217\permanent\protected\def\stopnicelyfilledbox
218  {\egroup
219   \vbox to {\dummyparameter\c!height} \bgroup
220     \ifcstok{\dummyparameter\c!strut}\v!yes
221       \dorecurse{\boxlines\scratchbox}%
222         {\boxlineht\scratchbox\recurselevel\strutht
223          \boxlinedp\scratchbox\recurselevel\strutdp
224          \boxline\scratchbox\recurselevel
225          \vss}%
226     \else
227       \dorecurse{\boxlines\scratchbox}%
228         {\boxline\scratchbox\recurselevel
229          \vss}%
230     \fi
231     \unskip
232   \egroup
233   \egroup}
234
235\protect \endinput
236