Debian Codegraph @

texmf-dist/doc/generic/stretchy/Documentation/internals.tex

raw
1 %% S. Lurp --- STRETCHY Docs --- MIT License
2 Stretchy provides various macros for creating your own stretchy symbols.
3 These all require knowledge regarding the usage of PDF path painting operators.
4 For an in-depth explanation on PDFs and the usage of pdf\TeX{} primitives, you may consult my article
5 \url{https://github.com/Shlurp/pdfTeX-explanation/blob/master/pdftex-explanation.pdf}{here}.
6
7 \subsection{Stretchy Coordinates}
8
9 All Stretchy painting commands should utilize the Stretchy coordinate system.
10 This is accessed and manipulated via the following macros:
11 \blist
12 \item \macro\strty@p;
13 \item \macro\strty@pd;
14 \item \macro\strty@trans;
15 \item \macro\strty@setpttrans.
16 \elist
17 Essentially, all these macros do is apply a transformation to the coordinates provided.
18 That is, if you specify a line from $(x_0,y_0)$ to $(x_1,y_1)$, Stretchy will transform these coordinates
19 according to the Current Stretchy Transformation (CST) $\CST$, and draw a line from $\CST(x_0,y_0)$ to
20 $\CST(x_1,y_1)$.
21 The idea is similar for cubic B\'ezier curves (the start, end, and control points are transformed via $T$).
22
23 In order to facilitate this, you must pass the coordinates to \macro\strty@p.
24 That is, instead of doing something like
25
26 \blisting
27 0 0 m
28 10 0 l
29 S
30 \elisting
31
32 You should do
33 \blisting
34 \strty@p{0}{0} m
35 \strty@p{10}{0} l
36 S
37 \elisting
38
39 The CST is specified by \macro\strty@trans, which is a macro accepting $2$ parameters and must expand to
40 two groups.
41 For example,
42 \blisting
43 \def\strty@trans#1#2{{-#2}{#1}}
44 \elisting
45 will rotate all points by $90$ degrees.
46
47 Stretchy provides some useful macros for basic arithmetic operations.
48 \blist
49 \item \macro\strty@nopt{} computes a dimension expression, and expands to the result without the trailing
50 {\tt pt}.
51 For example \inlinecode|\strty@nopt{1pt+2pt}| will expand to {\tt3}.
52 \item \macro\strty@add{} accepts two parameters (numbers), and expands to their sum.
53 \item \macro\strty@mult{} accepts two parameters (numbers), and expands to their product.
54 \elist
55
56 The definitions of \macro\strty@add{} and \macro\strty@mult{} are simply
57 \listfile{strty-utils.tex}[9-10]
58
59 The definition of \macro\strty@setpttrans{} is simply
60 \listfile{strty-utils.tex}[61-63]
61 that is, it simply multiplies each component by \macro\strty@ptm, which is defined to be {\tt.996264} (the
62 ratio between \TeX{} and PDF pts).
63 Let us define this transformation to be $\PTT$.
64
65 \macro\strty@pd{} is a macro accepting four parameters:
66 \getmacrousage{\strty@pd {<x>}{<y>}{<dx>}{<dy>}}
67 it transforms the point $(x,y)$ to $\CST(x,y)+(dx,dy)$ where $dx,dy$ are dimensions.
68 This is useful e.g. in \macro\sqrtyrsqint, where the rounded edges are a set dimension, and thus the vertical
69 edges must be offset by a set dimension.
70
71 \subsection{Stretchy Utilities}
72
73 In {\tt strty-utils.tex}, Stretchy defines some useful utilities.
74 Most of these are internal to Stretchy or were discussed previously, but we take the time to discuss one:
75 \macro\strty@scalebox.
76 This accepts two parameters:
77 \getmacrousage{\strty@scalebox {<scale>}{<material>}}
78 and scales {\it material} by {\it scale}.
79 For example \inlinecode|\strty@scalebox{2}{\stretchylogo}| will produce
80
81 \medskip
82 \centerline{\strty@scalebox{2}{\stretchylogo}}
83
84 \subsection{Repeated Symbols}
85
86 In {\tt strty-repeatedsyms.tex}, Stretchy defines all the repeated symbols (listed above) as well as some
87 useful auxillary macros.
88
89 \macroexp{\strty@circle {x}{y}{r}}
90 This expands to PDF code for drawing a circle centered at $(x,y)$ (dimensions, not affected by the CST) with a
91 radius of $r$ (not a dimension).
92 The axis points of the circle end up being
93 \blist
94 \item right: $\CST(r,0)+(x,y)$;
95 \item left: $\CST(-r,0)+(x,y)$;
96 \item top: $\CST(0,r)+(x,y)$;
97 \item bottom: $\CST(0,-r)+(x,y)$.
98 \elist
99
100 In order to draw the circle, Stretchy draws four cubic B\'ezier curves.
101 The value \macro\strty@cd{} determines the distance of the control points from the axis points of the circle.
102 \emacroexp
103
104 \macroexp{\strty@repeatedsum {<name>}{<symbol>}{<kerning>}}
105 This defines a macro of name {\it name} which accepts a single parameter $N$, and paints the symbol
106 {\it symbol} $N$ times with {\it kerning} placed between subsequent symbols.
107 For example,
108 \listfile{strty-repeatedsyms.tex}[61-62]
109 defines the two variants of \macro\strtyint{} and \macro\strtyintlimits{} (one for display math and the other
110 for textstyle math).
111 This defines the macros \macro\strty@dint@sym{} and \macro\strty@tint@sym.
112 For example, \inlinecode|\strty@dint@sym{5}| paints
113 $$ \strty@dint@sym{5} $$
114 \emacroexp
115
116 \macroexp{\strty@extensible {<name>}{<bg code>}{<fg code>}{<symbol>}{<dx>}{<dy>}{<kerning>}}
117 This defines a macro of name {\it name} which accepts a single parameter $N$ and paints the symbol
118 {\it symbol} $N$ times with {\it kerning} placed between subsequent symbols.
119 {\it bg code} is PDF code placed before painting the symbols, and {\it fg code} is PDF code placed after
120 painting the symbols.
121
122 If the resulting width of painting $N$ {\it symbol}s is $w$, then we define $\CST$ to be
123 $$ \CST\colon(x,y) \mapsto \left({{x(w+dx)}\over{2}}\,,\,y\cdot dy\right) $$
124 that is, $\CST$ stretches the $x$-axis by a factor of ${{w+dx}\over2}$ and the $y$-axis by a factor of $dy$.
125
126 For example, \macro\strty@dsqint@sym{} is the symbol for \macro\strtysqint{} in display math, and is defined
127 like so:
128 \listfile{strty-repeatedsyms.tex}[124-134]
129 \emacroexp
130
131 \macroexp{\strty@createoplims \<macro>{nolim sup kern}{nolim sub kern}{lim sup kern}{lim sub kern}}\hfil\break
132 This defines two macros, \macro\macro@nolim{} and \macro\macro@lim{} which accept three arguments each, $N$,
133 {\it sup} and {\it sub}.
134 These then pass $N$ to \macro\macro{} (which is a repeating macro, e.g. defined by \macro\strty@extensible),
135 and place {\it sup} and {\it sub} in the super- and subscripts, with kerning according to the parameters given.
136
137 For example, the definition of \macro\strtyint{} and \macro\strtyintlimits{} is
138 \listfile{strty-repeatedsyms.tex}[61-80]
139 \benum
140 \item lines 61 and 62 define the symbols;
141 \item lines 63 and 64 define the kerning of the super and subscripts;
142 \item the rest define the actual macros.
143 \eenum
144 \emacroexp
145
146 \subsection{Stretched Symbols}
147
148 Stretchy provides the following auxillary macros for creating stretched symbols.
149
150 \macroexp{\strty@hstretch {<name>}{<left>}{<right>}{<code>}}
151 This defines a macro named {\it name} which accepts a parameter $w$, and creates a symbol of width $w$.
152 This symbol consists of (from left to right):
153 \benum
154 \item the material {\it left};
155 \item the code {\it code};
156 \item the material {\it right}.
157 \eenum
158 The CST is set to be the composition of $\PTT$ with
159 $$ (x,y)\mapsto((w-w_l-w_r)\cdot x+w_l\,,\,y) $$
160 where $w_l,w_r$ are the widths of {\it left} and {\it right}, respectively.
161 That is, $(0,0)$ is mapped to $(w_l,0)$, $(1,0)$ is mapped to $(w-w_r,0)$, and $(0,1)$ is mapped to $(0,1)$.
162 So the left side of {\it code} maps to $w_l$ (the edge of {\it left}), and the right side to $w-w_r$ (the edge
163 of {\it right}).
164 \emacroexp
165
166 \macroexp{\strty@vstretch {<name>}{<top>}{<bottom>}{<code>}}
167 This defines a macro named {\it name} which accepts a parameter $h$, and creates a symbol of height $h$.
168 This symbol consists of (from top to bottom):
169 \benum
170 \item the material {\it top};
171 \item the code {\it code};
172 \item the material {\it bottom}.
173 \eenum
174 The CST is set to be the composition of $\PTT$ with
175 $$ (x,y)\mapsto(x\,,\,(d-h_t-h_b)\cdot y+h_b) $$
176 where $h_t,h_b$ are the heights of {\it top} and {\it bottom}, respectively.
177 That is, $(0,0)$ is mapped to $(0,h_b)$, $(1,0)$ is mapped to $(1,0)$, and $(0,1)$ is mapped to $(0,d-h_t)$.
178 So the top of {\it code} maps to $d-h_t$ (the bottom of {\it top}), and the bottom to $h_b$ (the top
179 of {\it bottom}).
180 \emacroexp
181
182 \macroexp{\strty@hvstretch {<name>}{<tl>}{<tr>}{<bl>}{<br>}{<top code>}{<bot code>}{<mid code>}}
183 This defines a\hfil\break macro named {\it name} which accepts two parameters $w,h$.
184 It creates a symbol of width $w$ and height $h$ of the form:
185 $$ \pmatrix{\hbox{\it tl}&\hbox{\it top code}&\hbox{\it tr}\cr
186 &\hbox{\it mid code}\cr
187 \hbox{\it bl}&\hbox{\it bot code}&\hbox{\it br}} $$
188 The transformations for each code are as follows:
189 \blist
190 \item {\it top code}: let $w_{\it tl},w_{\it tr}$ be the widths of {\it tl} and {\it tr} respectively.
191 Then $\CST$ is the composition of $\PTT$ with
192 $$ (x,y) \mapsto ((w-w_{\it tl}-w_{\it tr})\cdot x+w_{\it tl}\,,\,y) $$
193 that is, the left side of {\it top code} maps to $w_{\it tl}$, and the right side to $w-w_{\it tr}$.
194 \item {\it bot code}: let $w_{\it bl},w_{\it br}$ be the widths of {\it bl} and {\it br} respectively.
195 Then $\CST$ is the composition of $\PTT$ with
196 $$ (x,y) \mapsto ((w-w_{\it bl}-w_{\it br})\cdot x+w_{\it bl}\,,\,y) $$
197 that is, the left side of {\it top code} maps to $w_{\it bl}$, and the right side to $w-w_{\it br}$.
198 \item {\it mid code}: let $h_t,h_b$ be the heights of the top and bottom materials, respectively.
199 Then $\CST$ is the composition of $\PTT$ with
200 $$ (x,y) \mapsto (w\cdot x\,,\,(h-h_t-h_b)\cdot y+h_b) $$
201 so
202 \blist
203 \item $(0,0)$ (the bottom left of {\it mid code}) maps to $(0,h_b)$ (the top left of the bottom
204 material);
205 \item $(1,0)$ (the bottom right) maps to $(w,h_b)$ (the top right of the bottom material);
206 \item $(0,1)$ (the top left) maps to $(0,h-h_t)$ (the bottom left of the top material);
207 \item $(1,1)$ (the top right) maps to $(w,h-h_t)$ (the bottom right of the top material).
208 \elist
209 \elist
210 \emacroexp
211
212 To create a stretched symbol, there are four steps:
213 \benum
214 \item precisely measure the dimensions of the glyph you're making stretchy;
215 \item crop the glyph where you want (either using form XObjects, or clipping paths);
216 \item use \macro\strty@XXstretch{} (${\tt XX}\in\{{\tt h},{\tt v},{\tt hv}\}$) on the cropped sections of
217 the glyph, as well as the code to connect them.
218 This creates a stretchable symbol;
219 \item define macro to get the material (super-, sub-script, and main material) and compute the dimensions
220 to stretch the symbol to.
221 \eenum
222
223 For example, we can measure (textstyle) $\prod$ to get the (rough) dimensions of the strokes
224
225 \bigskip
226 \def\mbscale{7}
227 \def\mbstrokewd{0}
228 \centerline{\measurebox{\mbnodp{$\prod$}}{%
229 \measureh{b=.5\mbht, e=10pt, stroke=.01, color=blue, dash=.1, label=(top stroke top), noprint, legend}%
230 \measureh{b=.5\mbht, e=9.6pt, stroke=.01, color=blue, dash=.1, label=(top stroke bot), noprint, legend}%
231 \measurew{b=1.59pt, e=2.56pt, stroke=.01, color=red, dash=.1, label=(left stroke), noprint, legend}%
232 \measurew{b=\mbwd-2.574pt, e=\mbwd, stroke=.01, color=red, dash=.1, label=(right stroke left),
233 noprint, legend, y=.5\mbht-1pt}%
234 \measurew{e=\mbwd, b=\mbwd-1.604pt, stroke=.01, color=red, dash=.1, label=(right stroke right),
235 noprint, legend, y=.5\mbht-1pt}%
236 }}
237
238 Now we can create our own stretched \macro\prod, with the following code:
239
240 \blisting
241 \bgroup
242 \setbox0=\hbox{\mbnodp{$\prod$}} % product with no depth
243 \setbox1=\vbox to.5\ht0{\copy0\vss}
244 \setbox2=\vbox to.5\ht0{\vss\copy0}
245 \setbox3=\hbox to.5\wd0{\copy1\hss} % tl
246 \setbox4=\hbox to.5\wd0{\hss\copy1} % tr
247 \setbox5=\hbox to.5\wd0{\copy2\hss} % bl
248 \setbox6=\hbox to.5\wd0{\hss\copy2} % br
249 \pdfxform3 \xdef\prodtl{\pdfrefxform\the\pdflastxform}
250 \pdfxform4 \xdef\prodtr{\pdfrefxform\the\pdflastxform}
251 \pdfxform5 \xdef\prodbl{\pdfrefxform\the\pdflastxform}
252 \pdfxform6 \xdef\prodbr{\pdfrefxform\the\pdflastxform}
253 \egroup
254
255 \strty@hvstretch{stretchedprod}{\prodtl}{\prodtr}{\prodbl}{\prodbr}%
256 { % top connecting horizontal line
257 \strty@p{0}{5} m
258 \strty@p{1}{5} l
259 \strty@p{1}{4.6} l
260 \strty@p{0}{4.6} l
261 h
262 f
263 }%
264 {}%
265 {% connecting vertical lines
266 \strty@pd{0}{0}{1.59pt}{0pt} m
267 \strty@pd{0}{1}{1.59pt}{0pt} l
268 \strty@pd{0}{1}{2.56pt}{0pt} l
269 \strty@pd{0}{0}{2.56pt}{0pt} l
270 h
271 f
272 \strty@pd{1}{0}{-2.574pt}{0pt} m
273 \strty@pd{1}{1}{-2.574pt}{0pt} l
274 \strty@pd{1}{1}{-1.604pt}{0pt} l
275 \strty@pd{1}{0}{-1.604pt}{0pt} l
276 h
277 f
278 }
279 \elisting
280
281 \bgroup
282 \setbox0=\hbox{\mbnodp{$\prod$}} % product with no depth
283 \setbox1=\vbox to.5\ht0{\copy0\vss}
284 \setbox2=\vbox to.5\ht0{\vss\copy0}
285 \setbox3=\hbox to.5\wd0{\copy1\hss} % tl
286 \setbox4=\hbox to.5\wd0{\hss\copy1} % tr
287 \setbox5=\hbox to.5\wd0{\copy2\hss} % bl
288 \setbox6=\hbox to.5\wd0{\hss\copy2} % br
289 \pdfxform3 \xdef\prodtl{\pdfrefxform\the\pdflastxform}
290 \pdfxform4 \xdef\prodtr{\pdfrefxform\the\pdflastxform}
291 \pdfxform5 \xdef\prodbl{\pdfrefxform\the\pdflastxform}
292 \pdfxform6 \xdef\prodbr{\pdfrefxform\the\pdflastxform}
293 \egroup
294
295 \strty@hvstretch{stretchedprod}{\prodtl}{\prodtr}{\prodbl}{\prodbr}%
296 { % top connecting horizontal line
297 \strty@p{0}{5} m
298 \strty@p{1}{5} l
299 \strty@p{1}{4.6} l
300 \strty@p{0}{4.6} l
301 h
302 f
303 }%
304 {}%
305 {% connecting vertical lines
306 \strty@pd{0}{0}{1.59pt}{0pt} m
307 \strty@pd{0}{1}{1.59pt}{0pt} l
308 \strty@pd{0}{1}{2.56pt}{0pt} l
309 \strty@pd{0}{0}{2.56pt}{0pt} l
310 h
311 f
312 \strty@pd{1}{0}{-2.574pt}{0pt} m
313 \strty@pd{1}{1}{-2.574pt}{0pt} l
314 \strty@pd{1}{1}{-1.604pt}{0pt} l
315 \strty@pd{1}{0}{-1.604pt}{0pt} l
316 h
317 f
318 }
319
320 Doing then \inlinecode|\stretchedprod{20pt}{20pt}|, will give, for example:
321
322 \centerline{\stretchedprod{20pt}{20pt}}
323
324 The rest of the code to get the dimensions of the limits and main material and pass to \macro\stretchedprod is
325 standard.
326