summaryrefslogtreecommitdiff
path: root/tex/context/base/mkiv/meta-imp-txt.mkiv
blob: b2a6d6d1dd82edad21d02d93f4277b0e6588e70c (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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
%D \module
%D   [       file=meta-txt,
%D        version=2000.07.06,
%D          title=\METAPOST\ Graphics,
%D       subtitle=Text Tricks,
%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 In this library some handy text manipulations are
%D defined. Some can and will be improved as soon as the
%D \TEX||\METAPOST\ interface is stable. Some of the
%D solutions may look weird, which is entirely my fault,
%D since I implemented them in the process of getting grip
%D on this kind of manipulations. Undoubtly better
%D \METAPOST\ code is possible, but my way of learning
%D this kind of trickery happens to be by \quote {trial
%D and error} and \quote {look and feel} (as well as
%D identifying tricks in Hobby's code).

% textext ipv btex ... etex

% we need a proper prefix here

\unprotect

\definesystemvariable {sh}  % ShapedText .. todo: commandhandler

\startMPextensions
    loadmodule "text" ;
\stopMPextensions

%%%%%%%

% \def\newchar#1{\chardef#1=0 }

\ifdefined\MPtoks       \else \newtoks\MPtoks        \fi
\ifdefined\MPbox        \else \newbox \MPbox         \fi

\ifdefined\parwidth     \else \newdimen\parwidth     \fi
\ifdefined\parheight    \else \newdimen\parheight    \fi
\ifdefined\parvoffset   \else \newdimen\parvoffset   \fi
\ifdefined\parhoffset   \else \newdimen\parhoffset   \fi
\ifdefined\parlines     \else \newcount\parlines     \fi
\ifdefined\partoks      \else \newtoks \partoks      \fi
\ifdefined\shapetextbox \else \newbox  \shapetextbox \fi
                              \newif   \ifparseries
\ifdefined\parfirst     \else \chardef \parfirst=0   \fi

\unexpanded\def\startshapetext[#1]%
  {\global\newcounter\currentshapetext
   \global\setbox\shapetextbox\vbox\bgroup
     \expanded{\switchtobodyfont[\@@shbodyfont]}%
     \dontcomplain
     \hsize\parwidth
     \setuptolerance[\v!verytolerant,\v!stretch]%
     \!!counta\zerocount
     \!!toksa\emptytoks
     \def\docommand##1%
       {\setbox\scratchbox\hbox{\useMPgraphic{##1}}%
        \global\chardef\parfirst\zerocount
        \getMPdata % \readlocfile{\MPdatafile}{}{}%
        \setshapecharacteristics
        \advance\!!counta by \parlines
        \expandafter\appendtoks\the\partoks\to\!!toksa}%
     \processcommalist[#1]\docommand
     \global\parseriestrue
     \xdef\totalparlines{\the\!!counta}%
     \global\partoks\!!toksa
    %\ifx\partoks\emptytoks\else % safeguard
       \expanded{\parshape \the\!!counta \the\!!toksa}%
    %\fi
     \setshapecharacteristics % extra dummy
     \ifparseries\def\par{\endgraf\adaptparshape}\fi
     \EveryPar{\begstrut}}

\unexpanded\def\stopshapetext
  {\endstrut
  %\removebottomthings
   \egroup
   \global\newcounter\currentshapetext
   \getshapecharacteristics}

\unexpanded\def\adaptparshape
  {\def\docommand##1%
     {\ifcase\!!counta
        \expandafter\appendtoks\space##1 \to\!!toksa
      \else
        \advance\!!counta \minusone
      \fi}%
   \!!counta\prevgraf
   \doglobal\decrement(\totalparlines,\!!counta)%
   \multiply\!!counta \plustwo
   \!!toksa\emptytoks
   \expanded{\processseparatedlist[\the\partoks][\space]}\docommand
   \global\partoks\!!toksa
  %\ifx\partoks\emptytoks\else % safeguard
     \expanded{\parshape\totalparlines\the\partoks}%
 }%\fi}

\unexpanded\def\getshapecharacteristics
  {\doglobal\increment\currentshapetext
   \doifelsedefined{parlines:\currentshapetext}
     {\global\parlines         \getvalue{parlines:\currentshapetext}%
      \global\chardef\parfirst \getvalue{parfirst:\currentshapetext}%
      \global\parvoffset       \getvalue{parvoffset:\currentshapetext}%
      \global\parhoffset       \getvalue{parhoffset:\currentshapetext}%
      \global\parwidth         \getvalue{parwidth:\currentshapetext}%
      \global\parheight        \getvalue{parheight:\currentshapetext}}
     {\global\parlines         \plusone
      \global\chardef\parfirst \zerocount
      \global\parvoffset       \zeropoint
      \global\parhoffset       \zeropoint
      \global\parwidth         \hsize
      \global\parheight        \vsize}}

\unexpanded\def\setshapecharacteristics
  {\doglobal\increment\currentshapetext
   \setxvalue{parlines:\currentshapetext  }{\the\parlines}%
   \setxvalue{parfirst:\currentshapetext  }{\the\parfirst}%
   \setxvalue{parvoffset:\currentshapetext}{\the\parvoffset}%
   \setxvalue{parhoffset:\currentshapetext}{\the\parhoffset}%
   \setxvalue{parwidth:\currentshapetext  }{\the\parwidth}%
   \setxvalue{parheight:\currentshapetext }{\the\parheight}}

\unexpanded\def\getshapetext % option: unvbox
  {\vbox\bgroup
   \forgetall
   \dontcomplain
   \setbox\scratchbox\vbox to \parheight
     {\expanded{\switchtobodyfont[\@@shbodyfont]}% evt strutheight en
      \splittopskip\strutheight               % lineheight opslaan
      \vskip\parvoffset                       % scheelt switch en
      \ifcase\parfirst\or\vskip\lineheight\fi % is ook veiliger
      \hskip\parhoffset
      \hbox{\vsplit\shapetextbox to \parlines\lineheight}}%
   \wd\scratchbox\parwidth
   \ht\scratchbox\parheight
   \dp\scratchbox\zeropoint
   \box\scratchbox
   \getshapecharacteristics
   \egroup}

\unexpanded\def\setupshapetexts
  {\dodoubleempty\getparameters[\??sh]}

\setupshapetexts
  [\c!bodyfont=]

\doifundefined{RotFont}{\definefont[RotFont][RegularBold*default]}

% \startuseMPgraphic{followtokens}
%   % we default to nothing
% \stopuseMPgraphic

% \unexpanded\def\processfollowingtoken#1% strut toegevoegd
%   {\appendtoks#1\to\MPtoks
%    \setbox\MPbox=\hbox{\RotFont\setstrut\strut\the\MPtoks}%
%    \startMPdrawing
%      n := n + 1 ; len[n] := \the\wd\MPbox ;
%    \stopMPdrawing
%    \startMPdrawing[-]
%    % pic[n] := textext{\RotFont\setstrut\strut#1} ; % btex \RotFont\setstrut\strut#1 etex ;
%      pic[n] := btex \RotFont\setstrut\strut#1 etex ;
%      pic[n] := pic[n] shifted - llcorner pic[n] ;
%    \stopMPdrawing}
%
% \unexpanded\def\dofollowtokens#1#2%
%   {\vbox\bgroup
%    \forgetall
%    \dontcomplain
%    \doifundefined{RotFont}{\definefont[RotFont][RegularBold*default]}%
%    \MPtoks\emptytoks
%    \resetMPdrawing
%    \startMPdrawing
%      \includeMPgraphic{followtokens} ;
%      picture pic[] ; numeric len[], n ; n := 0 ;
%    \stopMPdrawing
%    \handletokens#2\with\processfollowingtoken
%    \startMPdrawing
%      if unknown RotPath  : path    RotPath  ; RotPath  := origin ; fi ;
%      if unknown RotColor : color   RotColor ; RotColor := black  ; fi ;
%      if unknown TraceRot : boolean TraceRot ; TraceRot := false  ; fi ;
%      if unknown ExtraRot : numeric ExtraRot ; ExtraRot := 0      ; fi ;
%      numeric al, at, pl, pc, wid, pos ; pair ap, ad ;
%      al := arclength RotPath ;
%      if al=0 :
%        al := len[n] + ExtraRot ;
%        RotPath := origin -- (al,0) ;
%      fi ;
%      if al<len[n]:
%        RotPath := RotPath scaled ((len[n]+ExtraRot)/al) ;
%        al := arclength RotPath ;
%      fi ;
%      if \number#1 = 1 :
%        pl := (al-len[n])/(if n>1 : (n-1) else : 1 fi) ;
%        pc := 0 ;
%      else : % centered / MP
%        pl := 0 ;
%        pc := arclength RotPath/2 - len[n]/2 ;
%      fi ;
%      if TraceRot :
%        draw RotPath withpen pencircle scaled 1pt withcolor blue ;
%      fi ;
%      for i=1 upto n :
%        wid := abs(xpart urcorner pic[i] - xpart llcorner pic[i]) ;
%        pos := len[i]-wid/2 + (i-1)*pl + pc ;
%        at := arctime   pos of RotPath ;
%        ap := point     at  of RotPath ;
%        ad := direction at  of RotPath ;
%        draw pic[i] shifted (-wid/2,0) rotated(angle(ad)) shifted ap
%          withcolor RotColor ;
%        if TraceRot :
%          draw boundingbox
%             pic[i] shifted (-wid/2,0) rotated(angle(ad)) shifted ap
%             withpen pencircle scaled .25pt withcolor red ;
%          draw ap
%             withpen pencircle scaled .50pt withcolor green ;
%        fi ;
%      endfor ;
%    \stopMPdrawing
%    \MPdrawingdonetrue
%    \getMPdrawing
%    \resetMPdrawing
%    \egroup}

\unexpanded\def\getfollowtoken#1%
 {\hbox\bgroup
    \strut
    \ctxlua{mp.follow_text(#1)}%
  \egroup}

\definefontfeature[mp:tp][liga=no]

\startMPdefinitions
    def mfun_follow_draw (expr alternative) =
        if unknown RotPath  : path    RotPath  ; RotPath  := origin ; fi ;
      % if unknown RotColor : color   RotColor ; RotColor := black  ; fi ;
        if unknown TraceRot : boolean TraceRot ; TraceRot := false  ; fi ;
        if unknown ExtraRot : numeric ExtraRot ; ExtraRot := 0      ; fi ;
        picture pic[] ;
        numeric len[] ; len[0] := 0 ;
        numeric n ; n := lua.mp.follow_size() ;
        for i=1 upto n :
            pic[i] := lua.mp.follow_slot(i) ;
            pic[i] := pic[i] shifted - llcorner pic[i] ;
            len[i] := len[i-1] + lua.mp.follow_width(i) ;
        endfor ;
        numeric al, at, pl, pc, wid, pos ; pair ap, ad ;
        al := arclength RotPath ;
        if al = 0 :
            al := len[n] + ExtraRot ;
            RotPath := origin -- (al,0) ;
        fi ;
        if al < len[n]:
           RotPath := RotPath scaled ((len[n]+ExtraRot)/al) ;
           al := arclength RotPath ;
        fi ;
        if alternative = 1 :
           pl := (al-len[n])/(if n>1 : (n-1) else : 1 fi) ;
           pc := 0 ;
        else : % centered / MP
           pl := 0 ;
           pc := arclength RotPath/2 - len[n]/2 ;
        fi ;
        if TraceRot :
           draw RotPath withpen pencircle scaled 1pt withcolor blue ;
        fi ;
        for i=1 upto n :
          % wid := abs(xpart urcorner pic[i] - xpart llcorner pic[i]) ;
            wid := lua.mp.follow_width(i) ;
            pos := len[i]-wid/2 + (i-1)*pl + pc ;
            at := arctime   pos of RotPath ;
            ap := point     at  of RotPath ;
            ad := direction at  of RotPath ;
            if mfun_trial_run :
                % skip (ok, somewhat inefficient as we can consider a
                % dedicated store and textext variant (todo)
            else :
                pic[i] := pic[i] shifted (-wid/2,0) rotated(angle(ad)) shifted ap ;
                draw pic[i] ; % withcolor RotColor ;
                if TraceRot :
                    draw boundingbox pic[i] withpen pencircle scaled .25pt withcolor red ;
                    draw ap withpen pencircle scaled .50pt withcolor green ;
                fi ;
            fi ;
        endfor ;
        if TraceRot :
            draw boundingbox currentpicture withpen pencircle scaled .25pt withcolor blue ;
        fi ;
    enddef ;
\stopMPdefinitions

\startluacode
    local nodecodes = nodes.nodecodes

    local visible_code = {
        [nodecodes.glyph] = true,
        [nodecodes.glue]  = true,
        [nodecodes.hlist] = true,
        [nodecodes.vlist] = true,
        [nodecodes.rule]  = true,
    }

    local disc_code = nodecodes.disc
    local kern_code = nodecodes.kern

    local c_userkern = nodes.kerncodes.userkern
    local a_fontkern = attributes.private("fontkern")

    local n = nil
    local s = 0

    function mp.follow_reset()
        r = nil
        s = 0
    end

    function mp.follow_initialize(b)
        if not r then
            local l = tex.takebox(b).list
            n = { }
            s = 0
            while l do
                local c = l
                l = l.next
                local id = c.id
                if visible_code[id] then
                    s = s + 1
                    n[s] = c
                    c.prev = nil
                    c.next = nil
                elseif id == kern_code then
                    if c.subtype == c_userkern and not c[a_fontkern] then
                        s = s + 1
                        n[s] = c
                        c.prev = nil
                    else
                        n[s].next = c
                        c.prev = n[s]
                    end
                    c.next = nil
                elseif id == disc_code then
                    local r = c.replace
                    while r do
                        s = s + 1
                        n[s] = r
                        r = r.next
                        r.prev = nil
                        r.next = nil
                    end
                end
            end
        end
    end

    function mp.follow_size()
        mp.print(s)
    end

    function mp.follow_slot(i)
        mp.print('textext("\\getfollowtoken{' .. i .. '}")')
    end

    function mp.follow_text(s)
        context(n[s])
    end

    function mp.follow_width(i)
        mp.print(number.topoints(n[i].width))
    end
\stopluacode

\unexpanded\def\dofollowtokens#1#2%
  {\vbox\bgroup
   \forgetall
   \dontcomplain
   \setbox\scratchbox\hbox{\addff{mp:tp}#2}%
   \ctxlua{mp.follow_initialize(\number\scratchbox)}%
   \stopluacode
   \startMPcode
     \includeMPgraphic{followtokens} ;
     mfun_follow_draw(\number#1) ;
   \stopMPcode
   \ctxlua{mp.follow_reset()}%
   \egroup}

\unexpanded\def\followtokens        {\dofollowtokens1}
\unexpanded\def\followtokenscentered{\dofollowtokens0}

% stretched variant:
%
% \followtokens
%   {This is just a dummy text, kerned by T{\kern
%    -.1667em\lower .5ex\hbox {E}}{\kern -.125emX} and typeset
%    in a circle using {\setMFPfont M}{\setMFPfont
%    E}{\setMFPfont T}{\setMFPfont A}{\setMFPfont
%    P}{\setMFPfont O}{\setMFPfont S}{\setMFPfont T}.\quad}

% centered variant:
%
% \def\followtokengraphicscale#1{%%
%   \startuseMPgraphic {followtokens}
%     path RotPath; RotPath :=  reverse halfcircle scaled #1 ;
%     draw RotPath ;
%     setbounds currentpicture to boundingbox fullcircle scaled 12cm ;
%   \stopuseMPgraphic}
%
% \startoverlay
%   {\followtokengraphicscale{12cm}%%
%    \followtokenscentered{There was question on the list about this kind of graphics.}}
%   {\followtokengraphicscale{10cm}%%
%    \followtokenscentered{And Marco patched followingtokens to handle a centered text.}}
%   {\followtokengraphicscale{8cm}%%
%    \followtokenscentered{That ended up as variant branch in the main macro.}}
%   {\followtokengraphicscale{6cm}%%
%    \followtokenscentered{So now we have two commands.}}
% \stopoverlay

% \followtokengraphicscale{6cm}
% \followtokens{Hans Hagen uses {\darkred\TeX}, {\darkgreen\Lua}, {\darkblue \MetaPost} and friends.}

\startuseMPgraphic{fuzzycount}
   begingroup
   save height, span, drift, d, cp ;
   height := 3/ 5 * \baselinedistance ;
   span   := 1/ 3 * height ;
   drift  := 1/10 * height ;
   pickup pencircle scaled (1/12 * height) ;
   def d = (uniformdeviate drift) enddef ;
   for i := 1 upto \MPvar{n} :
     draw
       if (i mod 5)=0 : ((-d-4.5span,d)--(+d-0.5span,height-d))
       else           : ((-d,+d)--(+d,height-d)) fi
       shifted (span*i,d-drift) ;
   endfor;
   picture cp ; cp := currentpicture ; % for readability
   setbounds currentpicture to
     (llcorner cp shifted (0,-ypart llcorner cp) --
      lrcorner cp shifted (0,-ypart lrcorner cp) --
      urcorner cp -- ulcorner cp -- cycle) ;
   endgroup ;
\stopuseMPgraphic

\setupMPvariables
  [fuzzycount]
  [n=10]

\unexpanded\def\fuzzycount#1%
  {{\tx\useMPgraphic{fuzzycount}{n=#1}}}

\defineconversion[fuzzy][\fuzzycount]

%%%%%%%

\setupMPvariables
  [EnglishRule]
  [height=1ex,
   width=\the\localhsize, % without \the, problems in non e-tex
   color=darkgray]

\defineblank
  [EnglishRule]
  [medium]

\startuniqueMPgraphic{EnglishRule}{height,width,color}
    height = \MPvar{height} ;
    x1 = 0 ; x3 = \MPvar{width} ; x2 = x4 = .5x3 ;
    y1 = y3 = 0 ; y2 = -y4 = height/2 ;
    fill z1..z2..z3 & z3..z4..z1 & cycle withcolor \MPvar{color} ;
\stopuniqueMPgraphic

\unexpanded\def\EnglishRule
  {\startlinecorrection[EnglishRule]
   \setlocalhsize \noindent \reuseMPgraphic{EnglishRule}
   \stoplinecorrection}

%D The following macro returns a tight bound character
%D sequence.
%D
%D \useMPlibrary[txt]
%D
%D \startlinecorrection
%D \TightText{\ss\bf 123}{0cm}{3cm}{red}
%D \stoplinecorrection

\unexpanded\def\TightText#1#2#3#4%
  {\hbox % \ruledhbox
     {\startMPcode
        picture p ; p := image (graphictext "#1" withfillcolor red) ;
        draw p xsized #2 ysized #3 withcolor \MPcolor{#4} ;
      \stopMPcode}}

\protect \endinput