meta-imp-txt.mkxl /size: 7867 b    last modification: 2023-12-21 09:44
1%D \module
2%D   [       file=meta-txt,
3%D        version=2000.07.06,
4%D          title=\METAPOST\ Graphics,
5%D       subtitle=Text Tricks,
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 See comments in the \MKIV\ module. This is an upgraded version for \LMTX,
15%D where we can so some things nicer.
16
17\unprotect
18
19%D Text in shapes. Here we use the option to prune a parshape when a new paragraph
20%D is seen. It's an experimental feature so I need to keep an eye on how it evolves.
21%D This time we combine \TEX, \METAPOST\ and \LUA, so we do't need a temporary file
22%D to communicate from \METAPOST\ to \TEX. We just store information in \LUA\ tables.
23
24\ifdefined\startshapetext \else
25    \writestatus{system}{something is wrong here}
26\fi
27
28%D Following:
29
30% \doifundefined{RotFont}{\definefont[RotFont][RegularBold*default]}
31%
32% \protected\def\getfollowtoken#1%
33%  {\hbox\bgroup
34%     \strut
35%     \ctxlua{mp.follow_text(#1)}%
36%   \egroup}
37
38\definefontfeature[mp:tp][liga=no]
39
40% if unknown RotPath  : path    RotPath  ; RotPath  := origin ; fi ;
41% if unknown TraceRot : boolean TraceRot ; TraceRot := false  ; fi ;
42% if unknown ExtraRot : numeric ExtraRot ; ExtraRot := 0      ; fi ;
43% if al < len[n]:
44%    RotPath := RotPath scaled ((len[n]+ExtraRot)/al) ;
45%    al := arclength RotPath ;
46% fi ;
47% if TraceRot :
48%    draw RotPath withpen pencircle scaled 1pt withcolor blue ;
49% fi ;
50% if TraceRot :
51%     draw boundingbox currentpicture withpen pencircle scaled .25pt withcolor blue ;
52% fi ;
53
54\protected\def\dofollowtokens#1#2%
55  {\dontleavehmode
56   \vpack\bgroup
57   \forgetall
58   \dontcomplain
59   \addff{mp:tp}%
60   \startMPcode
61      \includeMPgraphic{followtokens}
62      draw lmt_followtext [ path = RotPath, text = "#2", spread = #1 ] ;
63   \stopMPcode
64   \egroup}
65
66\protected\def\followtokens        {\dofollowtokens{true}}
67\protected\def\followtokenscentered{\dofollowtokens{false}}
68
69%D Fuzzy counters:
70
71\startuseMPgraphic{fuzzycount}
72    begingroup
73        save height, span, drift, d, cp ;
74        height := 3/ 5 * LineHeight ;
75        span   := 1/ 3 * height ;
76        drift  := 1/10 * height ;
77        pickup pencircle scaled (1/12 * height) ;
78        def d = (uniformdeviate drift) enddef ;
79        for i := 1 upto \MPvar{n} :
80            draw
81                if (i mod 5)=0 : ((-d - 4.5span,d) -- (d -0.5span,height - d))
82                else           : ((-d,          d) -- (d,         height - d)) fi
83                shifted (span*i,d-drift) ;
84        endfor;
85        picture cp ; cp := currentpicture ; % for readability
86        setbounds currentpicture to
87            (llcorner cp shifted (0,-ypart llcorner cp) --
88             lrcorner cp shifted (0,-ypart lrcorner cp) --
89             urcorner cp                                --
90             ulcorner cp                                -- cycle) ;
91    endgroup ;
92\stopuseMPgraphic
93
94\setupMPvariables
95  [fuzzycount]
96  [n=10]
97
98\protected\def\fuzzycount#1%
99  {\dontleavehmode\bgroup
100     \tx
101     \useMPgraphic{fuzzycount}{n=#1}%
102   \egroup}
103
104\defineconversion[fuzzy][\fuzzycount]
105
106%D English rules:
107
108\setupMPvariables
109  [EnglishRule]
110  [height=1ex,
111   width=\localhsize,
112   color=darkgray]
113
114\defineblank
115  [EnglishRule]
116  [medium]
117
118\startuniqueMPgraphic{EnglishRule}{height,width,color}
119    x1 = 0 ; x3 = \MPvar{width} ; x2 = x4 = .5x3 ;
120    y1 = y3 = 0 ; y2 = -y4 = \MPvar{height} / 2 ;
121    fill z1 .. z2 .. z3 & z3 .. z4 .. z1 & cycle withcolor \MPvar{color} ;
122\stopuniqueMPgraphic
123
124\protected\def\EnglishRule
125  {\startlinecorrection[EnglishRule]%
126     \setlocalhsize
127     \noindentation
128     \reuseMPgraphic{EnglishRule}%
129   \stoplinecorrection}
130
131%D Tight text (for old times sake): the following macro returns a tight bound
132%D character sequence.
133%D
134%D \useMPlibrary[txt]
135%D
136%D \startlinecorrection
137%D \TightText{\ss\bf 123}{0cm}{3cm}{red}
138%D \stoplinecorrection
139
140\protected\def\TightText#1#2#3#4%
141  {\hpack
142     {\startMPcode
143        picture p ; p := image (graphictext "#1" withfillcolor red) ;
144        draw p xsized #2 ysized #3 withcolor \MPcolor{#4} ;
145      \stopMPcode}}
146
147\protected\def\TightText#1#2#3#4%
148  {\dontleavehmode
149   \startMPcode
150      draw outlinetext.f ("#1") (\iftok{#4}\emptytoks \else withcolor \MPcolor{#4} \fi)
151        \iftok{#2}\emptytoks \else xsized #2 \fi
152        \iftok{#3}\emptytoks \else ysized  #3 \fi
153      ;
154   \stopMPcode}
155
156\protect
157
158\continueifinputfile{meta-imp-txt.mkxl}
159
160\setupbodyfont[pagella,10pt]
161
162% \useMPlibrary[txt]
163
164\starttext
165
166%D Shapes:
167
168\startuseMPgraphic{test 1}
169    path p ; p := fullcircle scaled 6cm ;
170
171    build_parshape(p,BodyFontSize/2,0,0,LineHeight,StrutHeight,StrutDepth,StrutHeight) ;
172
173    draw p withpen pencircle scaled 1pt ;
174\stopuseMPgraphic
175
176\startuseMPgraphic{test 2}
177    path p ; p := fullsquare rotated 45 scaled 5cm ;
178
179    build_parshape(p,BodyFontSize/2,0,0,LineHeight,StrutHeight,StrutDepth,StrutHeight) ;
180
181    draw p withpen pencircle scaled 1pt ;
182\stopuseMPgraphic
183
184\startuseMPgraphic{test 3}
185    numeric w, h ; w := h := 6cm ;
186    path p ; p := (.5w,h) -- (0,h) -- (0,0) -- (w,0) &
187        (w,0) .. (.75w,.5h) .. (w,h) & (w,h) -- cycle ;
188
189    build_parshape(p,BodyFontSize/2,0,0,LineHeight,StrutHeight,StrutDepth,StrutHeight) ;
190
191    draw p withpen pencircle scaled 1pt ;
192\stopuseMPgraphic
193
194\startuseMPgraphic{test 4}
195    numeric w, h, o, d ;
196
197    def shape = (o,o) -- (w-o,o) & (w-o,o) .. (.75w-o,.5h) ..
198        (w-2o,h-o) & (w-2o,h-o) -- (o,h-o) -- cycle
199    enddef ;
200
201    d := BodyFontSize/2;
202
203    w := h := 6cm ; o := d ; path p ; p := shape ;
204    w := h := 6cm ; o := 0 ; path q ; q := shape ;
205
206    build_parshape(p,q,d,d,LineHeight,StrutHeight,StrutDepth,StrutHeight) ;
207
208    draw q withpen pencircle scaled 1pt ;
209\stopuseMPgraphic
210
211\defineoverlay[test 1][\useMPgraphic{test 1}]
212\defineoverlay[test 2][\useMPgraphic{test 2}]
213\defineoverlay[test 3][\useMPgraphic{test 3}]
214\defineoverlay[test 4][\useMPgraphic{test 4}]
215
216\startbuffer
217\startshapetext[test 1,test 2,test 3,test 4]
218    \setupalign[verytolerant,stretch,normal]%
219    \samplefile{douglas} % Douglas R. Hofstadter
220\stopshapetext
221\startTEXpage[offset=10pt]
222    \startcombination[2*2]
223        {\framed[offset=overlay,frame=off,background=test 1]{\getshapetext}} {test 1}
224        {\framed[offset=overlay,frame=off,background=test 2]{\getshapetext}} {test 2}
225        {\framed[offset=overlay,frame=off,background=test 3]{\getshapetext}} {test 3}
226        {\framed[offset=overlay,frame=off,background=test 4]{\getshapetext}} {test 4}
227    \stopcombination
228\stopTEXpage
229\stopbuffer
230
231\getbuffer
232
233\startMPextensions
234    boolean trace_parshape ; trace_parshape := true ;
235\stopMPextensions
236
237\getbuffer
238
239%D Fuzzycount (maybe handy):
240
241\startitemize[fuzzy,nostopper]
242    \startitem This is real fuzzy. \stopitem
243    \startitem But it can be even more fuzzier. \stopitem
244    \startitem And how about this. \stopitem
245\stopitemize
246
247\EnglishRule
248
249\startitemize[continue,broad]
250    \startitem This is real fuzzy. \stopitem
251    \startitem But it can be even more fuzzier. \stopitem
252    \startitem And how about this. \stopitem
253\stopitemize
254
255%D Tight text (manual stuff):
256
257\ruledhbox{\TightText{oeps wat doet dit}{10cm}{1cm}{red}}   \blank
258\ruledhbox{\TightText{oeps wat doet dit}{10cm}{}   {green}} \blank
259\ruledhbox{\TightText{oeps wat doet dit}{}    {1cm}{blue}}  \blank
260\ruledhbox{\TightText{oeps wat doet dit}{}    {}   {}}      \blank
261
262%D Old fashioned:
263
264\startuseMPgraphic{followtokens}
265    path RotPath ; RotPath := reverse halfcircle xyscaled 3cm  ;
266    draw RotPath ;
267\stopuseMPgraphic
268
269\startTEXpage
270    \followtokens{{\hskip1em}some text{\hskip1em}}
271\stopTEXpage
272
273\startTEXpage
274    \followtokenscentered{{\hskip1em}some text{\hskip1em}}
275\stopTEXpage
276
277\stoptext
278