typo-shp.mkxl /size: 7669 b    last modification: 2023-12-21 09:44
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  \the\dimexpr
114                    \wd\b_spac_shapes+\shapedparagraphparameter\c!distance
115                   +\shapedparagraphparameter\c!hoffset
116                \relax
117          right \zeropoint
118          copy  \the\numexpr
119                   \noflines
120                  +\shapedparagraphparameter\c!lines
121                \relax
122          left  \zeropoint
123          right \zeropoint
124        % inspect
125          done
126      \relax
127      \pushparagraphtweak{shift}%
128      \dontleavehmode
129      \vtop
130         xoffset \the\dimexpr
131          -\shapedparagraphparameter\c!distance
132          -\wd\b_spac_shapes
133         \relax
134         yoffset \the\dimexpr
135           \shapedparagraphparameter\c!voffset
136          +\strutht
137         \relax
138         {\smashbox\b_spac_shapes
139          \box\b_spac_shapes}}}
140
141\def\spac_shapes_lines
142  {\normalexpanded
143     {\endgroup
144      \clf_setparagraphshape
145          left  \shapedparagraphparameter\c!width
146          right \zeropoint
147          copy  \shapedparagraphparameter\c!lines
148          left  \zeropoint
149          right \zeropoint
150        % inspect
151          done
152      \relax
153      \pushparagraphtweak{shift}}}
154
155\permanent\protected\tolerant\def\startshapedparagraph[#S#1]% no grouping
156  {\begingroup
157   \lettonothing\currentshapedparagraph
158   \setupshapedparagraph[#1]%
159   \setlocalhsize
160   \ifempty{\shapedparagraphparameter\c!text}\else
161     \global\setbox\b_spac_shapes\vpack\bgroup
162       \forgetall
163       \shapedparagraphparameter\c!text
164     \egroup
165   \fi
166   \ifvoid\b_spac_shapes
167     \ifnum\shapedparagraphparameter\c!lines>\zerocount
168       \spac_shapes_lines
169     \else
170       \spac_shapes_list
171     \fi
172   \else
173     \spac_shapes_text
174   \fi
175   \ignorespaces}
176
177\permanent\protected\def\stopshapedparagraph
178  {\popparagraphtweak}
179
180%D As it is not much code we now put it here:
181
182\newbox    \shapetextbox
183\newinteger\shapetextindex
184
185\permanent\protected\def\startshapetext[#S#1]%
186  {\begingroup
187   \global\shapetextindex\zerocount
188   \global\setbox\shapetextbox\vbox\bgroup
189     \setparagraphmetashape[#1]%
190     \forgetall
191     \dontcomplain
192     \setuptolerance[\v!verytolerant,\v!stretch]% default
193   % \setuplayout[\c!grid=\v!yes]% goes wrong, we need a local one
194     \pushparagraphtweak{shift}}
195
196\permanent\protected\def\stopshapetext
197  {\popparagraphtweak
198   \egroup
199   \endgroup}
200
201% \permanent\protected\def\getshapetext
202%   {\vbox\bgroup
203%    \forgetall
204%    \dontcomplain
205%    \global\advanceby\shapetextindex\plusone
206%    \scratchcounter\getshapeparameter{lines}\relax
207%    \ifnum\scratchcounter>\zerocount
208%      \scratchwidth  \getshapeparameter{width}\scaledpoint\relax
209%      \scratchheight \getshapeparameter{height}\scaledpoint\relax
210%      \setbox\scratchbox\vpack to \scratchheight
211%        {\splittopskip\strutheight
212%         \vskip\dimexpr\getshapeparameter{voffset}\scaledpoint\relax
213%         \ifcase\numexpr\getshapeparameter{first}\relax\else
214%           \vskip\lineheight
215%         \fi
216%         \hskip\dimexpr\getshapeparameter{hoffset}\scaledpoint\relax
217%         \hpack{\vsplit\shapetextbox to \scratchcounter\lineheight}}%
218%      \wd\scratchbox\scratchwidth
219%      \ht\scratchbox\scratchheight
220%      \dp\scratchbox\zeropoint
221%      \box\scratchbox
222%    \else
223%       % what now
224%    \fi
225%    \egroup}
226
227\permanent\protected\def\getshapetext
228  {\vbox\bgroup
229   \forgetall
230   \dontcomplain
231   \global\advanceby\shapetextindex\plusone
232   \scratchcounter\getshapeparameter{lines}\relax
233   \ifnum\scratchcounter>\zerocount
234     \scratchwidth  \getshapeparameter{width}\scaledpoint\relax
235     \scratchheight \getshapeparameter{height}\scaledpoint\relax
236     \scratchhoffset\dimexpr\getshapeparameter{hoffset}\scaledpoint\relax
237     \scratchvoffset\dimexpr\getshapeparameter{voffset}\scaledpoint\relax
238     \setbox\scratchbox\vpack to \scratchheight
239       {\splittopskip\strutheight
240        \vskip\scratchvoffset
241        \ifcase\numexpr\getshapeparameter{first}\relax\else
242          \vskip\lineheight
243        \fi
244        \hskip\scratchhoffset
245        \hpack{\vsplit\shapetextbox to \scratchcounter\lineheight}}%
246     \wd\scratchbox\dimexpr\scratchwidth +\scratchhoffset\relax
247     \ht\scratchbox\dimexpr\scratchheight+\scratchvoffset\relax
248     \dp\scratchbox\strutdp % zeropoint
249     \box\scratchbox
250   \else
251      % what now
252   \fi
253   \egroup}
254
255\protect
256