summaryrefslogtreecommitdiff
path: root/tex/context/base/mkxl/typo-shp.mkxl
blob: 12d3c0115a8c0808bf9aaa08690dc5e13fa941d0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
%D \module
%D   [       file=typo-shp,
%D        version=2021.02.27, % was meta-txt / meta-imp-txt / 2000.07.06
%D          title=\CONTEXT\ Typesetting Macros,
%D       subtitle=Paragraph Shapes,
%D         author=Hans Hagen,
%D           date=\currentdate,
%D      copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
%C
%C This module is part of the \CONTEXT\ macro||package and is
%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
%C details.

%D I finally decided to move some of the code written in 2000 for the
%D \METAFUN\ manual to its own core module. Of course it got adapted
%D to the way \LMTX\ does things.

\writestatus{loading}{ConTeXt Spacing Macros / Paragraph Shapes}

\registerctxluafile{typo-shp}{autosuffix}

\unprotect

%D Something new (experimental and evolving):
%D
%D \starttyping
%D \parshape
%D      3
%D      options 1 % repeat
%D      0cm 10cm 2cm 8cm 4cm 6cm
%D      lots of text
%D \stoptyping
%D
%D \starttyping
%D \parshape 4 5mm 125mm 0mm 120mm 5mm 125mm 0mm 120mm
%D \pushparagraphtweak {repeat}
%D     verse line 1\crlf
%D     verse line 2\crlf
%D     verse line 3\crlf
%D     verse line 4\par
%D     etc
%D \popparagraphtweak
%D \stoptyping

%D But we wrap this in a more abstract interface:

\installcorenamespace {parshapes}
\installcorenamespace {shapedparagraph}

\installbasicnativeautosetuphandler \??shapedparagraph {shapedparagraph}

\setupshapedparagraph
  [\c!method=,
   \c!list=,
   \c!lines=\zerocount,
   \c!mp=,
   \c!repeat=,
   \c!hoffset=\zeropoint,
   \c!voffset=\zeropoint,
   \c!distance=\zeropoint]

\permanent\protected\lettonothing\stopparagraphshape

\permanent\protected\def\startparagraphshape[#1]#2\stopparagraphshape
  {\gdefcsname\??parshapes#1\endcsname{#2}} % global

\permanent\protected\def\rawparagraphshape#1%
  {\begincsname\??parshapes#1\endcsname}

\permanent\protected\def\setparagraphshape[#1]%
  {\ifcsname\??parshapes#1\endcsname
     \expandafter\clf_setparagraphshape\lastnamedcs done\relax
   \fi}

\def\spac_shapes_calculate#1% called locally in \LUA
  {\startMPcalculation \includeMPgraphic{#1} \stopMPcalculation}

% \permanent\protected\tolerant\def\startshapedparagraph[#1]% no grouping
%   {\begingroup
%    \lettonothing\currentshapedparagraph
%    \setupshapedparagraph[#1]%
%    \edef\p_mp    {\shapedparagraphparameter\c!mp}%
%    \edef\p_repeat{\shapedparagraphparameter\c!repeat}%
%    \setlocalhsize
%    \normalexpanded
%      {\endgroup
%       \ifempty\p_mp
%         \setparagraphshape[\shapedparagraphparameter\c!list]%
%       \else
%         \setparagraphmetashape[\shapedparagraphparameter\c!mp][\ifx\p_repeat\v!yes repeat\fi]%
%       \fi
%       \pushparagraphtweak{\shapedparagraphparameter\c!method}\relax}}

\newbox\b_spac_shapes

\def\spac_shapes_list
  {\edef\p_mp    {\shapedparagraphparameter\c!mp}%
   \edef\p_repeat{\shapedparagraphparameter\c!repeat}%
   \normalexpanded
     {\endgroup
      \ifempty\p_mp
        \setparagraphshape[\shapedparagraphparameter\c!list]%
      \else
        \setparagraphmetashape[\p_mp][\ifx\p_repeat\v!yes repeat\fi]%
      \fi
      \pushparagraphtweak{\shapedparagraphparameter\c!method}\relax}}

\def\spac_shapes_text
  {\getnoflines{\dimexpr\htdp\b_spac_shapes+\shapedparagraphparameter\c!voffset}%
   \normalexpanded
     {\endgroup
      \clf_setparagraphshape
          left  \the\dimexpr
                    \wd\b_spac_shapes+\shapedparagraphparameter\c!distance
                   +\shapedparagraphparameter\c!hoffset
                \relax
          right \zeropoint
          copy  \the\numexpr
                   \noflines
                  +\shapedparagraphparameter\c!lines
                \relax
          left  \zeropoint
          right \zeropoint
        % inspect
          done
      \relax
      \pushparagraphtweak{shift}%
      \dontleavehmode
      \vtop
         xoffset \the\dimexpr
          -\shapedparagraphparameter\c!distance
          -\wd\b_spac_shapes
         \relax
         yoffset \the\dimexpr
           \shapedparagraphparameter\c!voffset
          +\strutht
         \relax
         {\smashbox\b_spac_shapes
          \box\b_spac_shapes}}}

\def\spac_shapes_lines
  {\normalexpanded
     {\endgroup
      \clf_setparagraphshape
          left  \shapedparagraphparameter\c!width
          right \zeropoint
          copy  \shapedparagraphparameter\c!lines
          left  \zeropoint
          right \zeropoint
        % inspect
          done
      \relax
      \pushparagraphtweak{shift}}}

\permanent\protected\tolerant\def\startshapedparagraph[#1]% no grouping
  {\begingroup
   \lettonothing\currentshapedparagraph
   \setupshapedparagraph[#1]%
   \setlocalhsize
   \doifsomething{\shapedparagraphparameter\c!text}
     {\global\setbox\b_spac_shapes\vpack\bgroup
      \forgetall
      \shapedparagraphparameter\c!text
      \egroup}%
   \ifvoid\b_spac_shapes
     \ifnum\shapedparagraphparameter\c!lines>\zerocount
       \spac_shapes_lines
     \else
       \spac_shapes_list
     \fi
   \else
     \spac_shapes_text
   \fi
   \ignorespaces}

\permanent\protected\def\stopshapedparagraph
  {\popparagraphtweak}

%D As it is not much code we now put it here:

\newbox    \shapetextbox
\newinteger\shapetextindex

\permanent\protected\def\startshapetext[#1]%
  {\begingroup
   \global\shapetextindex\zerocount
   \global\setbox\shapetextbox\vbox\bgroup
     \setparagraphmetashape[#1]%
     \forgetall
     \dontcomplain
     \setuptolerance[\v!verytolerant,\v!stretch]% default
   % \setuplayout[\c!grid=\v!yes]% goes wrong, we need a local one
     \pushparagraphtweak{shift}}

\permanent\protected\def\stopshapetext
  {\popparagraphtweak
   \egroup
   \endgroup}

% \permanent\protected\def\getshapetext
%   {\vbox\bgroup
%    \forgetall
%    \dontcomplain
%    \global\advanceby\shapetextindex\plusone
%    \scratchcounter\getshapeparameter{lines}\relax
%    \ifnum\scratchcounter>\zerocount
%      \scratchwidth  \getshapeparameter{width}\scaledpoint\relax
%      \scratchheight \getshapeparameter{height}\scaledpoint\relax
%      \setbox\scratchbox\vpack to \scratchheight
%        {\splittopskip\strutheight
%         \vskip\dimexpr\getshapeparameter{voffset}\scaledpoint\relax
%         \ifcase\numexpr\getshapeparameter{first}\relax\else
%           \vskip\lineheight
%         \fi
%         \hskip\dimexpr\getshapeparameter{hoffset}\scaledpoint\relax
%         \hpack{\vsplit\shapetextbox to \scratchcounter\lineheight}}%
%      \wd\scratchbox\scratchwidth
%      \ht\scratchbox\scratchheight
%      \dp\scratchbox\zeropoint
%      \box\scratchbox
%    \else
%       % what now
%    \fi
%    \egroup}

\permanent\protected\def\getshapetext
  {\vbox\bgroup
   \forgetall
   \dontcomplain
   \global\advanceby\shapetextindex\plusone
   \scratchcounter\getshapeparameter{lines}\relax
   \ifnum\scratchcounter>\zerocount
     \scratchwidth  \getshapeparameter{width}\scaledpoint\relax
     \scratchheight \getshapeparameter{height}\scaledpoint\relax
     \scratchhoffset\dimexpr\getshapeparameter{hoffset}\scaledpoint\relax
     \scratchvoffset\dimexpr\getshapeparameter{voffset}\scaledpoint\relax
     \setbox\scratchbox\vpack to \scratchheight
       {\splittopskip\strutheight
        \vskip\scratchvoffset
        \ifcase\numexpr\getshapeparameter{first}\relax\else
          \vskip\lineheight
        \fi
        \hskip\scratchhoffset
        \hpack{\vsplit\shapetextbox to \scratchcounter\lineheight}}%
     \wd\scratchbox\dimexpr\scratchwidth +\scratchhoffset\relax
     \ht\scratchbox\dimexpr\scratchheight+\scratchvoffset\relax
     \dp\scratchbox\strutdp % zeropoint
     \box\scratchbox
   \else
      % what now
   \fi
   \egroup}

\protect