typo-shp.mkxl /size: 6818 b    last modification: 2025-02-21 11:03
1%D \module
2%D   [       file=typo-shp,
3%D        version=2021.02.27, % was meta-txt / meta-imp-txt / 2000.07.06
4%D          title=\CONTEXT\ Typesetting Macros,
5%D       subtitle=Paragraph Shapes,
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 I finally decided to move some of the code written in 2000 for the
15%D \METAFUN\ manual to its own core module. Of course it got adapted
16%D to the way \LMTX\ does things.
17
18\writestatus{loading}{ConTeXt Spacing Macros / Paragraph Shapes}
19
20\registerctxluafile{typo-shp}{autosuffix}
21
22\unprotect
23
24%D Something new (experimental and evolving):
25%D
26%D \starttyping
27%D \parshape
28%D      3
29%D      options 1 % repeat
30%D      0cm 10cm 2cm 8cm 4cm 6cm
31%D      lots of text
32%D \stoptyping
33%D
34%D \starttyping
35%D \parshape 4 5mm 125mm 0mm 120mm 5mm 125mm 0mm 120mm
36%D \pushparagraphtweak {repeat}
37%D     verse line 1\crlf
38%D     verse line 2\crlf
39%D     verse line 3\crlf
40%D     verse line 4\par
41%D     etc
42%D \popparagraphtweak
43%D \stoptyping
44
45%D But we wrap this in a more abstract interface:
46
47\installcorenamespace {parshapes}
48\installcorenamespace {shapedparagraph}
49
50\installbasicnativeautosetuphandler \??shapedparagraph {shapedparagraph}
51
52\setupshapedparagraph
53  [\c!method=,
54   \c!list=,
55   \c!lines=\zerocount,
56   \c!mp=,
57   \c!repeat=,
58   \c!hoffset=\zeropoint,
59   \c!voffset=\zeropoint,
60   \c!distance=\zeropoint]
61
62\permanent\protected\lettonothing\stopparagraphshape
63
64\permanent\protected\def\startparagraphshape[#1]#2\stopparagraphshape
65  {\gdefcsname\??parshapes#1\endcsname{#2}} % global
66
67\permanent\protected\def\rawparagraphshape#1%
68  {\begincsname\??parshapes#1\endcsname}
69
70\permanent\protected\def\setparagraphshape[#1]%
71  {\ifcsname\??parshapes#1\endcsname
72     \expandafter\clf_setparagraphshape\lastnamedcs done\relax
73   \fi}
74
75\def\spac_shapes_calculate#1% called locally in \LUA
76  {\startMPcalculation \includeMPgraphic{#1} \stopMPcalculation}
77
78% \permanent\protected\tolerant\def\startshapedparagraph[#1]% no grouping
79%   {\begingroup
80%    \lettonothing\currentshapedparagraph
81%    \setupshapedparagraph[#1]%
82%    \edef\p_mp    {\shapedparagraphparameter\c!mp}%
83%    \edef\p_repeat{\shapedparagraphparameter\c!repeat}%
84%    \setlocalhsize
85%    \normalexpanded
86%      {\endgroup
87%       \ifempty\p_mp
88%         \setparagraphshape[\shapedparagraphparameter\c!list]%
89%       \else
90%         \setparagraphmetashape[\shapedparagraphparameter\c!mp][\ifx\p_repeat\v!yes repeat\fi]%
91%       \fi
92%       \pushparagraphtweak{\shapedparagraphparameter\c!method}\relax}}
93
94\newbox\b_spac_shapes
95
96\def\spac_shapes_list
97  {\edef\p_mp    {\shapedparagraphparameter\c!mp}%
98   \edef\p_repeat{\shapedparagraphparameter\c!repeat}%
99   \normalexpanded
100     {\endgroup
101      \ifempty\p_mp
102        \setparagraphshape[\shapedparagraphparameter\c!list]%
103      \else
104        \setparagraphmetashape[\p_mp][\ifx\p_repeat\v!yes repeat\fi]%
105      \fi
106      \pushparagraphtweak{\shapedparagraphparameter\c!method}\relax}}
107
108\def\spac_shapes_text
109  {\getnoflines{\dimexpr\htdp\b_spac_shapes+(\shapedparagraphparameter\c!voffset)}%
110   \normalexpanded
111     {\endgroup
112      \clf_setparagraphshape
113          left  \todimension{% expanded
114                    \wd\b_spac_shapes
115                   +(\shapedparagraphparameter\c!distance)%
116                   +(\shapedparagraphparameter\c!hoffset)%
117                }%
118          right \zeropoint
119          copy  \tointeger{% expanded
120                   \noflines
121                  +(\shapedparagraphparameter\c!lines)%
122                }%
123          left  \zeropoint
124          right \zeropoint
125        % inspect
126          done
127      \relax
128      \pushparagraphtweak{shift}%
129      \dontleavehmode
130      \vtop
131         \s!xoffset \todimension{% expanded
132          \zeropoint % otherwise error
133          -(\shapedparagraphparameter\c!distance)%
134          -\wd\b_spac_shapes
135         }%
136         \s!yoffset \todimension{% expanded
137           (\shapedparagraphparameter\c!voffset)%
138          +\strutht
139         }%
140         {\smashbox\b_spac_shapes
141          \box\b_spac_shapes}}}
142
143\def\spac_shapes_lines
144  {\normalexpanded
145     {\endgroup
146      \clf_setparagraphshape
147          left  \todimension{\shapedparagraphparameter\c!width}% expanded
148          right \zeropoint
149          copy  \tointeger{\shapedparagraphparameter\c!lines}% expanded
150          left  \zeropoint
151          right \zeropoint
152        % inspect
153          done
154      \relax
155      \pushparagraphtweak{shift}}}
156
157\permanent\protected\tolerant\def\startshapedparagraph[#S#1]% no grouping
158  {\begingroup
159   \lettonothing\currentshapedparagraph
160   \setupshapedparagraph[#1]%
161   \setlocalhsize
162   \ifempty{\shapedparagraphparameter\c!text}\else
163     \global\setbox\b_spac_shapes\vpack\bgroup
164       \forgetall
165       \shapedparagraphparameter\c!text
166     \egroup
167   \fi
168   \ifvoid\b_spac_shapes
169     \ifnum{\shapedparagraphparameter\c!lines}>\zerocount
170       \spac_shapes_lines
171     \else
172       \spac_shapes_list
173     \fi
174   \else
175     \spac_shapes_text
176   \fi
177   \ignorespaces}
178
179\permanent\protected\def\stopshapedparagraph
180  {\popparagraphtweak}
181
182%D As it is not much code we now put it here:
183
184\newbox    \shapetextbox
185\newinteger\shapetextindex
186
187\permanent\protected\def\startshapetext[#S#1]%
188  {\begingroup
189   \global\shapetextindex\zerocount
190   \global\setbox\shapetextbox\vbox\bgroup
191     \setparagraphmetashape[#1]%
192     \forgetall
193     \dontcomplain
194     \setuptolerance[\v!verytolerant,\v!stretch]% default
195   % \setuplayout[\c!grid=\v!yes]% goes wrong, we need a local one
196     \pushparagraphtweak{shift}}
197
198\permanent\protected\def\stopshapetext
199  {\popparagraphtweak
200   \egroup
201   \endgroup}
202
203\permanent\protected\def\getshapetext
204  {\vbox\bgroup
205   \forgetall
206   \dontcomplain
207   \global\advanceby\shapetextindex\plusone
208   \scratchcounter\getshapeparameter{lines}\relax
209   \ifnum\scratchcounter>\zerocount
210     \scratchwidth  {\getshapeparameter{width}\scaledpoint}%
211     \scratchheight {\getshapeparameter{height}\scaledpoint}%
212     \scratchhoffset{\getshapeparameter{hoffset}\scaledpoint}%
213     \scratchvoffset{\getshapeparameter{voffset}\scaledpoint}%
214     \setbox\scratchbox\vpack to \scratchheight
215       {\splittopskip\strutheight
216        \vskip\scratchvoffset
217        \ifcase{\getshapeparameter{first}}\else
218          \vskip\lineheight
219        \fi
220        \hskip\scratchhoffset
221        \hpack{\vsplit\shapetextbox to \scratchcounter\lineheight}}%
222     \wd\scratchbox{\scratchwidth +\scratchhoffset}%
223     \ht\scratchbox{\scratchheight+\scratchvoffset}%
224     \dp\scratchbox\strutdp % zeropoint
225     \box\scratchbox
226   \else
227      % what now
228   \fi
229   \egroup}
230
231\protect
232