summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/metapost/textpath/textpath.mp
blob: 03df35cd8f092e6340301b2285f94ee67eb0643f (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
%%% Global variables.

%%% Global variable.
%%% Character coordinates that lay outside path boundaries after
%%% transformation are clipped to the boundaries.
%%% Variable textpathClip determines if such characters should be
%%% output at path boundaries, thereby acting as a visual indicator
%%% that something went wrong, or if text outside path boundaries
%%% should be omitted. Variable textpathClip can be either one (omit)
%%% or zero (don't omit).
newinternal textpathClip;
textpathClip := 1;

%%% Global variable.
%%% Variable textpathLetterSpace determines the amount of space that
%%% is additionally inserted between all characters.  This can be
%%% useful, when drawing text along a concacve shape or in general
%%% along paths with a high curvature.  Variable textpathLetterSpace
%%% can contain positive or negative values.
newinternal textpathLetterSpace;
textpathLetterSpace := 0bp;

%%% Global variable.
newinternal textpathRotation;
textpathRotation := 0;

%%% Global variable.
newinternal textpathAbsRotation;
textpathAbsRotation := 0;

newinternal textpathRepeat;
textpathRepeat := 1;

newinternal textpathStretch;
textpathStretch := 1;

newinternal textpathHSpace;
textpathHSpace := 0;

newinternal textpathShift;
textpathShift := 0;

newinternal textpathSoulLetterSpace;
textpathSoulLetterSpace := 10bp;

newinternal textpathFancyStrokes;
textpathFancyStrokes := 1;

newinternal textpathStrokePrecision;
textpathStrokePrecision := 10;

newinternal textpathCureSqrt;
textpathCureSqrt := 1;

newinternal textpathDebug;
textpathDebug := 0;

pair textpathHookCoord, textpathHookPoint;
newinternal textpathHookRoot;
textpathHookCoord := origin;
textpathHookPoint := origin;
textpathHookRot := 0;



%%% User Macros.

%%% User macro.
%%% Set text along a path.
%%% Parameters:
%%%  * a text string t that is fed into TeX through latexmp,
%%%  * a path p the text should follow.
%%%  * a numeric variable j that controls justification:
%%%    j=0:   text is left aligned on the path,
%%%    j=0.5: text is centered on the path,
%%%    j=1:   text is right aligned on the path,
%%%    The general rule is the character at fraction j of the total
%%%    text width is transformed to the point at fraction j of the total
%%%    path width.  j should be from the interval [0, 1].
%%% Return value:
%%%  * a picture variable containing text following a path.
def textpath(expr t, p, j)=
  textpathFont("", t, p, j)
enddef;


%%% User macro.
%%% Set text in arbitrary font along a path.
%%% Parameters:
%%%  * a string f that is a valid LaTeX font selection command, e.g., \itshape,
%%%  * a text string t that is fed into TeX through latexmp,
%%%  * a path p the text should follow.
%%%  * a numeric variable j that controls justification:
%%%    j=0:   text is left aligned on the path,
%%%    j=0.5: text is centered on the path,
%%%    j=1:   text is right aligned on the path,
%%%    The general rule is the character at fraction j of the total
%%%    text width is transformed to the point at fraction j of the total
%%%    path width.  j should be from the interval [0, 1].
%%% Return value:
%%%  * a picture variable containing text following a path.
vardef textpathFont(expr f, t, p, j)=
save pre, post;
string pre, post;
  pre := f & "\spaced{";
  post := "}";
  textpathPic(strToPic(pre & t & post), p, j, "")
enddef;


%%% User macro.
%%% Set text along a path.
%%% Parameters:
%%%  * a text string t that is fed into TeX through latexmp,
%%%  * a path p the text should follow.
%%%  * a numeric variable j that controls justification:
%%%    j=0:   text is left aligned on the path,
%%%    j=0.5: text is centered on the path,
%%%    j=1:   text is right aligned on the path,
%%%    The general rule is the character at fraction j of the total
%%%    text width is transformed to the point at fraction j of the total
%%%    path width.  j should be from the interval [0, 1].
%%% Return value:
%%%  * a picture variable containing text following a path.
vardef textpathRaw(expr t, p, j)=
save temp, pic, pre, post;
numeric temp;
picture pic;
string pre, post;
  pre := "\rule[-30bp]{0pt}{30bp}";
  post := "";
  temp := textpathSoulLetterSpace;
  textpathSoulLetterSpace := 0bp;
  pic := textpathPicRaw(strToPic(pre & t & post), p, j, "");
  textpathSoulLetterSpace := temp;
  pic
enddef;



%%% Internal Macros.

%%% Internal macro.
%%% Convert a string into a picture using TeX via latexmp package.
%%% Parameters:
%%%  * a string.
%%% Return value:
%%%  * a picture variable.
vardef strToPic(expr s)=
save loffset, pic;
numeric loffset;
picture pic;
  loffset := labeloffset;
  labeloffset := 0bp;
  pic := thelabel.urt(textext(s), origin);
  labeloffset := loffset;
  pic
enddef;


%%% Internal working macro.
%%% Set text from picture variable along a path with justification j.
%%% Parameters:
%%%  * a picture that contains the text that has to be transformed onto a path,
%%%  * a path p the text should follow,
%%%  * a numeric variable that controls justification,
%%%  * a string: "debug" indicates debug mode.
%%% Return value:
%%%  * a picture variable containing text following a path.
vardef textpathPic(expr textpic, p, j, debug)=
save tb, db, pic, item, bitem, banker, k, i, lenp, lenpic, bx, cx, ltextpathHSpace;
numeric tb, db, k, i, lenp, lenpic, ltextpathHSpace, bx, cx;
picture pic, item, bitem;
pair banker;
  pic := nullpicture;
  lenp := arclength(p);
  lenpic := 0;
  k := 0;
  for item within textpic:
    if textual item:
      bitem := item shifted (-k*textpathSoulLetterSpace, 0bp) shifted (k*textpathLetterSpace, 0bp);
      x := xpart lrcorner bitem;
      if x > lenpic:
        lenpic := x;
      fi
      k := k + 1;
    fi
  endfor
  if textpathStretch=1:
    ltextpathHSpace := lenp/textpathRepeat - lenpic;
  else:
    ltextpathHSpace := textpathHSpace;
  fi
  k := 0;
  for item within textpic:
    if textual item:
      bitem := item shifted (-k*textpathSoulLetterSpace, 0bp) shifted (k*textpathLetterSpace, textpathShift);
      banker := (xpart center bitem, ypart item);
      if textpathStretch=1:
        bx := (xpart banker) + j*ltextpathHSpace;
      else:
        bx := (xpart banker) + j*(lenp - textpathRepeat*lenpic - (textpathRepeat-1)*ltextpathHSpace);
      fi
      bitem := bitem shifted -banker;
      for i:= 1 upto textpathRepeat:
        cx := (i-1)*(lenpic + ltextpathHSpace) + bx;
        if (textpathClip=0) or ((cx>=0) and (cx<=lenp)):
          tb := arctime cx of p;
          if textpathAbsRotation=0:
            db := textpathRotation + angle direction tb of p;
          else:
            db := textpathRotation;
          fi
          if not (debug="debug"):
            addto pic also bitem rotated db shifted point tb of p;
          else:
            addto pic contour fullcircle scaled 2bp shifted point tb of p;
          fi
        fi
      endfor
      k := k + 1;
    else:
      show "Warning!  Unsupported picture element found!";
    fi
  endfor
  pic
enddef;



%%% Internal working macro.
%%% Set text from picture variable along a path with justification j.
%%% Parameters:
%%%  * a picture that contains the text that has to be transformed onto a path,
%%%  * a path p the text should follow,
%%%  * a numeric variable that controls justification,
%%%  * a string: "debug" indicates debug mode.
%%% Return value:
%%%  * a picture variable containing text following a path.
vardef textpathPicRaw(expr textpic, p, j, debug)=
save tb, db, pic, item, bitem, banker, i, lenp, lenpic, bx, cx, ltextpathHSpace, pp, np, dpp;
numeric tb, db, dpp, i, h, lenp, lenpic, ltextpathHSpace, bx, cx;
picture pic, item, bitem;
pair banker;
path pp, np;
  show textpic;
  pic := nullpicture;
  lenp := arclength(p);
  lenpic := xpart (lrcorner textpic - llcorner textpic);
  if textpathStretch=1:
    ltextpathHSpace := lenp/textpathRepeat - lenpic;
  else:
    ltextpathHSpace := textpathHSpace;
  fi
  for item within textpic:
    if textual item:
%       show "t=" & textpart item & " ASCII=" & decimal ASCII textpart item & " font=" & fontpart item;
      bitem := item shifted (0, textpathShift);
      banker := (xpart center bitem, 30bp);
      if textpathStretch=1:
        bx := (xpart banker) + j*ltextpathHSpace;
      else:
        bx := (xpart banker) + j*(lenp - textpathRepeat*lenpic - (textpathRepeat-1)*ltextpathHSpace);
      fi
      bitem := bitem shifted -banker;
      for i:=1 upto textpathRepeat:
        cx := (i-1)*(lenpic + ltextpathHSpace) + bx;
        if (textpathClip=0) or ((cx>=0) and (cx<=lenp)):
          tb := arctime cx of p;
          if textpathAbsRotation=0:
            db := textpathRotation + angle direction tb of p;
          else:
            db := textpathRotation;
          fi
          addto pic also bitem rotated db shifted point tb of p;
        fi
      endfor
      if ((fontpart item="cmex10")      and ((ASCII textpart item>=112) and (ASCII textpart item<=116))) or
         ((fontpart item="cmsy5")       and (ASCII textpart item=112)) or
         ((fontpart item="cmsy6")       and (ASCII textpart item=112)) or
         ((fontpart item="cmsy7")       and (ASCII textpart item=112)) or
         ((fontpart item="cmsy8")       and (ASCII textpart item=112)) or
         ((fontpart item="cmsy9")       and (ASCII textpart item=112)) or
         ((fontpart item="cmsy10")      and (ASCII textpart item=112)) or
         ((fontpart item="cmbsy10")     and (ASCII textpart item=112)) or
         ((fontpart item="fourier-mex") and (ASCII textpart item=114)) or
         ((fontpart item="fourier-ms")  and (ASCII textpart item=112)) or
         ((fontpart item="MnSymbolE5")  and (ASCII textpart item=186)) or
         ((fontpart item="MnSymbolE5")  and (ASCII textpart item=189)) or
         ((fontpart item="MnSymbolE10")  and (ASCII textpart item=186)):
        textpathHookCoord := urcorner item shifted (-xpart center item, -30bp + textpathShift);
        textpathHookRot := db;
        textpathHookPoint := point tb of p;
%         addto pic contour fullcircle scaled 2bp shifted textpathHookCoord rotated db shifted point tb of p withcolor green;
      else:
        textpathHookCoord := origin;
      fi
    elseif stroked item:
      if textpathFancyStrokes=0:
        bitem := item shifted (0, textpathShift);
        banker := (xpart center bitem, 30bp);
        if textpathStretch=1:
          bx := (xpart banker) + j*ltextpathHSpace;
        else:
          bx := (xpart banker) + j*(lenp - textpathRepeat*lenpic - (textpathRepeat-1)*ltextpathHSpace);
        fi
        bitem := bitem shifted -banker;
        for i:=1 upto textpathRepeat:
          cx := (i-1)*(lenpic + ltextpathHSpace) + bx;
          if (textpathClip=0) or ((cx>=0) and (cx<=lenp)):
            tb := arctime cx of p;
            if textpathAbsRotation=0:
              db := textpathRotation + angle direction tb of p;
            else:
              db := textpathRotation;
            fi
            addto pic also bitem rotated db shifted point tb of p;
          fi
        endfor
      else:
        pp := pathpart item;
        for i=1 upto textpathRepeat:
          if (textpathHookCoord=origin) or (textpathCureSqrt=0):
            banker := (0, ypart point 0 of pp - 30bp + textpathShift);
            if textpathStretch=1:
              bx := (xpart point 0 of pp) + j*ltextpathHSpace;
            else:
              bx := (xpart point 0 of pp) + j*(lenp - textpathRepeat*lenpic - (textpathRepeat-1)*ltextpathHSpace);
            fi
            dpp := angle direction 0 of pp;
            cx := (i-1)*(lenpic + ltextpathHSpace) + bx;
            tb := arctime cx of p;
            if textpathAbsRotation=0:
              db := textpathRotation + angle direction tb of p;
            else:
              db := textpathRotation;
            fi
            np := banker rotated db shifted point tb of p%{dir (dpp+db)}
          elseif textpathCureSqrt=1:
            np := textpathHookCoord shifted (dir(-90)*xpart point 0 of makepath penpart item);
            np := np rotated textpathHookRot shifted textpathHookPoint%{dir textpathHookRot}
          fi
          for h:=1 upto textpathStrokePrecision:
            hide(
            banker := (0, ypart point (h/textpathStrokePrecision) of pp - 30bp + textpathShift);
            if textpathStretch=1:
              bx := (xpart point (h/textpathStrokePrecision) of pp) + j*ltextpathHSpace;
            else:
              bx := (xpart point (h/textpathStrokePrecision) of pp) + j*(lenp - textpathRepeat*lenpic - (textpathRepeat-1)*ltextpathHSpace);
            fi
            cx := (i-1)*(lenpic + ltextpathHSpace) + bx;
            dpp := angle direction (h/textpathStrokePrecision) of pp;
            tb := arctime cx of p;
            if textpathAbsRotation=0:
              db := textpathRotation + angle direction tb of p;
            else:
              db := textpathRotation;
            fi
            )
            ..banker rotated db shifted point tb of p%{dir (dpp+db)}
          endfor;
        addto pic doublepath np withpen penpart item;
        endfor
      fi
    else:
      show "Warning!  Unsupported picture element found!";
    fi
  endfor
  pic
enddef;