| 1 |
%%% textpath.mp |
| 2 |
%%% Copyright 2007 Stephan Hennig <[email protected]> |
| 3 |
% |
| 4 |
% This work may be distributed and/or modified under the conditions of |
| 5 |
% the LaTeX Project Public License, either version 1.3 of this license |
| 6 |
% or (at your option) any later version. The latest version of this |
| 7 |
% license is in http://www.latex-project.org/lppl.txt |
| 8 |
% |
| 9 |
|
| 10 |
if known textpath_fileversion: endinput fi; |
| 11 |
string textpath_fileversion; |
| 12 |
textpath_fileversion := "v1.6 (2007/02/11)"; |
| 13 |
message "Loading textpath " & textpath_fileversion; |
| 14 |
|
| 15 |
|
| 16 |
%%% Global variables. |
| 17 |
|
| 18 |
%%% Global variable. |
| 19 |
%%% Character coordinates that lay outside path boundaries after |
| 20 |
%%% transformation are clipped to the boundaries. |
| 21 |
%%% Variable textpathClip determines if such characters should be |
| 22 |
%%% output at path boundaries, thereby acting as a visual indicator |
| 23 |
%%% that something went wrong, or if text outside path boundaries |
| 24 |
%%% should be omitted. Variable textpathClip can be either one (omit) |
| 25 |
%%% or zero (don't omit). |
| 26 |
newinternal textpathClip; |
| 27 |
textpathClip := 0; |
| 28 |
|
| 29 |
%%% Global variable. |
| 30 |
%%% Variable textpathLetterSpace determines the amount of space that |
| 31 |
%%% is additionally inserted between all characters. This can be |
| 32 |
%%% useful, when drawing text along a concacve shape or in general |
| 33 |
%%% along paths with a high curvature. Variable textpathLetterSpace |
| 34 |
%%% can contain positive or negative values. |
| 35 |
newinternal textpathLetterSpace; |
| 36 |
textpathLetterSpace := 0bp; |
| 37 |
|
| 38 |
%%% Global variable. |
| 39 |
newinternal textpathRotation; |
| 40 |
textpathRotation := 0; |
| 41 |
|
| 42 |
%%% Global variable. |
| 43 |
newinternal textpathAbsRotation; |
| 44 |
textpathAbsRotation := 0; |
| 45 |
|
| 46 |
newinternal textpathRepeat; |
| 47 |
textpathRepeat := 1; |
| 48 |
|
| 49 |
newinternal textpathStretch; |
| 50 |
textpathStretch := 1; |
| 51 |
|
| 52 |
newinternal textpathHSpace; |
| 53 |
textpathHSpace := 0; |
| 54 |
|
| 55 |
newinternal textpathShift; |
| 56 |
textpathShift := 0; |
| 57 |
|
| 58 |
newinternal textpathSoulLetterSpace; |
| 59 |
textpathSoulLetterSpace := 10bp; |
| 60 |
|
| 61 |
newinternal textpathFancyStrokes; |
| 62 |
textpathFancyStrokes := 1; |
| 63 |
|
| 64 |
newinternal textpathStrokePrecision; |
| 65 |
textpathStrokePrecision := 10; |
| 66 |
|
| 67 |
newinternal textpathCureSqrt; |
| 68 |
textpathCureSqrt := 1; |
| 69 |
|
| 70 |
newinternal textpathAutoScale; |
| 71 |
textpathAutoScale := 0; |
| 72 |
|
| 73 |
newinternal textpathDraft; |
| 74 |
textpathDraft := 0; |
| 75 |
|
| 76 |
newinternal textpathDebug; |
| 77 |
textpathDebug := 1; |
| 78 |
|
| 79 |
pair textpathHookCoord, textpathHookPoint; |
| 80 |
newinternal textpathHookRot; |
| 81 |
textpathHookCoord := origin; |
| 82 |
textpathHookPoint := origin; |
| 83 |
textpathHookRot := 0; |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
%%% User Macros. |
| 88 |
|
| 89 |
%%% User macro. |
| 90 |
%%% Set text along a path. |
| 91 |
%%% Parameters: |
| 92 |
%%% * a text string t that is fed into TeX through latexmp, |
| 93 |
%%% * a path p the text should follow. |
| 94 |
%%% * a numeric variable j that controls justification: |
| 95 |
%%% j=0: text is left aligned on the path, |
| 96 |
%%% j=0.5: text is centered on the path, |
| 97 |
%%% j=1: text is right aligned on the path, |
| 98 |
%%% The general rule is the character at fraction j of the total |
| 99 |
%%% text width is transformed to the point at fraction j of the total |
| 100 |
%%% path width. j should be from the interval [0, 1]. |
| 101 |
%%% Return value: |
| 102 |
%%% * a picture variable containing text following a path. |
| 103 |
def textpath(expr t, p, j)= |
| 104 |
textpathFont("", t, p, j) |
| 105 |
enddef; |
| 106 |
|
| 107 |
|
| 108 |
%%% User macro. |
| 109 |
%%% Set text in arbitrary font along a path. |
| 110 |
%%% Parameters: |
| 111 |
%%% * a string f that is a valid LaTeX font selection command, e.g., \itshape, |
| 112 |
%%% * a text string t that is fed into TeX through latexmp, |
| 113 |
%%% * a path p the text should follow. |
| 114 |
%%% * a numeric variable j that controls justification: |
| 115 |
%%% j=0: text is left aligned on the path, |
| 116 |
%%% j=0.5: text is centered on the path, |
| 117 |
%%% j=1: text is right aligned on the path, |
| 118 |
%%% The general rule is the character at fraction j of the total |
| 119 |
%%% text width is transformed to the point at fraction j of the total |
| 120 |
%%% path width. j should be from the interval [0, 1]. |
| 121 |
%%% Return value: |
| 122 |
%%% * a picture variable containing text following a path. |
| 123 |
vardef textpathFont(expr f, t, p, j)= |
| 124 |
save pre, post; |
| 125 |
string pre, post; |
| 126 |
pre := f & "\spaced{"; |
| 127 |
post := "}"; |
| 128 |
textpathToPic(strToPic(pre & t & post), p, j) |
| 129 |
enddef; |
| 130 |
|
| 131 |
|
| 132 |
%%% User macro. |
| 133 |
%%% Set text along a path. |
| 134 |
%%% Parameters: |
| 135 |
%%% * a text string t that is fed into TeX through latexmp, |
| 136 |
%%% * a path p the text should follow. |
| 137 |
%%% * a numeric variable j that controls justification: |
| 138 |
%%% j=0: text is left aligned on the path, |
| 139 |
%%% j=0.5: text is centered on the path, |
| 140 |
%%% j=1: text is right aligned on the path, |
| 141 |
%%% The general rule is the character at fraction j of the total |
| 142 |
%%% text width is transformed to the point at fraction j of the total |
| 143 |
%%% path width. j should be from the interval [0, 1]. |
| 144 |
%%% Return value: |
| 145 |
%%% * a picture variable containing text following a path. |
| 146 |
vardef textpathRaw(expr t, p, j)= |
| 147 |
save pre, post; |
| 148 |
string pre, post; |
| 149 |
pre := "\rule[-30bp]{0pt}{30bp}"; |
| 150 |
post := ""; |
| 151 |
textpathRawToPic(strToPic(pre & t & post), p, j) |
| 152 |
enddef; |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
%%% Internal Macros. |
| 157 |
|
| 158 |
%%% Internal macro. |
| 159 |
%%% Convert a string into a picture using TeX via latexmp package. |
| 160 |
%%% Parameters: |
| 161 |
%%% * a string. |
| 162 |
%%% Return value: |
| 163 |
%%% * a picture variable. |
| 164 |
vardef strToPic(expr s)= |
| 165 |
interim labeloffset := 0bp; |
| 166 |
thelabel.urt(textext(s), origin) |
| 167 |
enddef; |
| 168 |
|
| 169 |
|
| 170 |
%%% Internal macro. |
| 171 |
%%% Computes the length of a textual picture, thereby correcting |
| 172 |
%%% for textpathSoulLetterSpace and textpathLetterSpace |
| 173 |
vardef getPictureWidth(expr textpic, scale)= |
| 174 |
save lenpic, k, w; |
| 175 |
numeric lenpic, k, w; |
| 176 |
lenpic := 0; |
| 177 |
k := 0; |
| 178 |
for item within textpic: |
| 179 |
if textual item: |
| 180 |
w := xpart lrcorner ( item shifted (scale*k*(textpathLetterSpace-textpathSoulLetterSpace), 0bp) ); |
| 181 |
if w > lenpic: |
| 182 |
lenpic := w; |
| 183 |
fi |
| 184 |
k := k + 1; |
| 185 |
fi |
| 186 |
endfor |
| 187 |
lenpic |
| 188 |
enddef; |
| 189 |
|
| 190 |
|
| 191 |
%%% Internal macro. |
| 192 |
%%% Does pre-processing for macros textpath(Raw)ToPic, such as |
| 193 |
%%% calculating variables startOffset, autoScale, ltextpathHSpace etc. |
| 194 |
def preprocessTextPic(expr rawMode, atextpic, p, j)= |
| 195 |
save textpic, pic, lenp, lenpic, startOffset, ltextpathHSpace, autoScale, overfullPath; |
| 196 |
interim bboxmargin := 0bp; |
| 197 |
numeric lenp, lenpic, startOffset, ltextpathHSpace, autoScale; |
| 198 |
picture textpic, pic; |
| 199 |
boolean overfullPath; |
| 200 |
|
| 201 |
textpic := atextpic; |
| 202 |
pic := nullpicture; |
| 203 |
%%% Compute path length. |
| 204 |
lenp := arclength(p); |
| 205 |
%%% Compute picture length, i.e., correct plain picture dimensions |
| 206 |
%%% for soul and textpath letter spacing. |
| 207 |
if rawMode:%%% Called from textpathRawToPic. |
| 208 |
lenpic := xpart (lrcorner textpic - llcorner textpic); |
| 209 |
else:%%% Called from textpathToPic. |
| 210 |
lenpic := getPictureWidth(textpic, 1);%%% Get corrected picture with scale = 1. |
| 211 |
fi |
| 212 |
%%% Compute inter-copy space (for textpathRepeat > 1). |
| 213 |
if textpathStretch = 0: |
| 214 |
ltextpathHSpace := textpathHSpace; |
| 215 |
else: |
| 216 |
ltextpathHSpace := lenp/textpathRepeat - lenpic; |
| 217 |
fi |
| 218 |
%%% Compute startOffset as j * remaining space. |
| 219 |
startOffset := j * (lenp - textpathRepeat*lenpic - (textpathRepeat-1)*ltextpathHSpace); |
| 220 |
%%% By default, assume path isn't overfull. |
| 221 |
overfullPath := false; |
| 222 |
autoScale := 1; |
| 223 |
%%% Overfull path? |
| 224 |
if startOffset < 0: |
| 225 |
overfullPath := true; |
| 226 |
if textpathAutoScale <> 0:%%% Calculate scaling factor. |
| 227 |
if textpathStretch <> 0: |
| 228 |
ltextpathHSpace := 0;%%% No inter-copy space when stretching allowed. |
| 229 |
fi |
| 230 |
autoScale := lenp/(textpathRepeat*lenpic + (textpathRepeat-1)*ltextpathHSpace1); |
| 231 |
startOffset := 0; |
| 232 |
if textpathDraft = 0: |
| 233 |
textpic := atextpic scaled autoScale; |
| 234 |
else: |
| 235 |
textpic := atextpic xscaled autoScale;%%% Don't scale y in draft mode. |
| 236 |
fi |
| 237 |
lenpic := getPictureWidth(textpic, autoScale); |
| 238 |
%%% Compute inter-copy space (for textpathRepeat > 1). |
| 239 |
if textpathStretch = 0: |
| 240 |
ltextpathHSpace := textpathHSpace*autoScale; |
| 241 |
else: |
| 242 |
ltextpathHSpace := lenp/textpathRepeat - lenpic;%%% Should be equal to zero. |
| 243 |
fi |
| 244 |
%%% Finally, output a warning. |
| 245 |
message "Package textpath warning: Overfull path! Scaled to " & decimal autoScale & "."; |
| 246 |
else:%%% No scaling. |
| 247 |
autoScale := 1; |
| 248 |
if textpathClip = 0:%%% Warn the user about overfull paths. |
| 249 |
message "Package textpath warning: Overfull path! Not clipped."; |
| 250 |
else: |
| 251 |
message "Package textpath warning: Overfull path! Clipped."; |
| 252 |
fi |
| 253 |
fi |
| 254 |
fi |
| 255 |
enddef; |
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
%%% Internal macro. |
| 260 |
def transformSimpleItem(expr rawMode, item, p)= |
| 261 |
%%% bitem is a character or straight rule to transform. |
| 262 |
%%% banker is its original anchor position. |
| 263 |
if rawMode: |
| 264 |
bitem := item; |
| 265 |
banker := (xpart center bitem, autoScale*30bp);%%% Why 30bp? |
| 266 |
else: |
| 267 |
bitem := item shifted (k*autoScale*(textpathLetterSpace-textpathSoulLetterSpace), 0bp); |
| 268 |
banker := (xpart center bitem, ypart item); |
| 269 |
fi |
| 270 |
%%% bx is the anchor position of a first item copy. |
| 271 |
bx := (xpart banker) + startOffset; |
| 272 |
%%% Shift anchor to origin and raise baseline. |
| 273 |
bitem := bitem shifted -banker shifted (0bp, textpathShift); |
| 274 |
%%% Iterate over all copies of an item. |
| 275 |
for i=1 upto textpathRepeat: |
| 276 |
%%% cx is the anchor position of an item copy. |
| 277 |
cx := (i-1)*(lenpic + ltextpathHSpace) + bx; |
| 278 |
%%% Draw items with valid positions or if no clipping. |
| 279 |
if ((cx>=0) and (cx<=lenp)) or (textpathClip=0): |
| 280 |
%%% Compute path time value. |
| 281 |
tb := arctime cx of p; |
| 282 |
%%% Compute rotation angle. |
| 283 |
if textpathAbsRotation=0: |
| 284 |
db := textpathRotation + angle direction tb of p; |
| 285 |
else: |
| 286 |
db := textpathRotation; |
| 287 |
fi |
| 288 |
%%% Draw item's bounding box? |
| 289 |
if odd (textpathDebug div 2): |
| 290 |
addto pic doublepath bbox bitem rotated db shifted point tb of p withpen currentpen; |
| 291 |
fi |
| 292 |
%%% Draw item? |
| 293 |
if odd (textpathDebug div 1): |
| 294 |
%%% In draft mode fill bounding box if path is overfull. |
| 295 |
if overfullPath and (textpathDraft<>0): |
| 296 |
addto pic contour bbox bitem rotated db shifted point tb of p; |
| 297 |
else: |
| 298 |
addto pic also bitem rotated db shifted point tb of p; |
| 299 |
fi |
| 300 |
fi |
| 301 |
%%% Draw item's anchor point? |
| 302 |
if odd (textpathDebug div 4): |
| 303 |
addto pic contour fullcircle scaled 3bp shifted point tb of p; |
| 304 |
fi |
| 305 |
fi |
| 306 |
endfor |
| 307 |
%%% Save top right corner of supported square root glyphs in variable textpathHookCoord. |
| 308 |
if rawMode and (textpathCureSqrt <> 0): |
| 309 |
if ((fontpart item="cmex10") and ((ASCII textpart item>=112) and (ASCII textpart item<=116))) or |
| 310 |
((fontpart item="cmsy5") and (ASCII textpart item=112)) or |
| 311 |
((fontpart item="cmsy6") and (ASCII textpart item=112)) or |
| 312 |
((fontpart item="cmsy7") and (ASCII textpart item=112)) or |
| 313 |
((fontpart item="cmsy8") and (ASCII textpart item=112)) or |
| 314 |
((fontpart item="cmsy9") and (ASCII textpart item=112)) or |
| 315 |
((fontpart item="cmsy10") and (ASCII textpart item=112)) or |
| 316 |
((fontpart item="cmbsy10") and (ASCII textpart item=112)) or |
| 317 |
((fontpart item="fourier-mex") and (ASCII textpart item=114)) or |
| 318 |
((fontpart item="fourier-ms") and (ASCII textpart item=112)) or |
| 319 |
((fontpart item="MnSymbolE5") and (ASCII textpart item=186)) or |
| 320 |
((fontpart item="MnSymbolE5") and (ASCII textpart item=189)) or |
| 321 |
((fontpart item="MnSymbolE10") and (ASCII textpart item=186)):%%% \sqrt glyph? |
| 322 |
textpathHookCoord := urcorner item shifted (-xpart center item, -autoScale*30bp + textpathShift); |
| 323 |
textpathHookRot := db; |
| 324 |
textpathHookPoint := point tb of p; |
| 325 |
% addto pic contour fullcircle scaled 2bp shifted textpathHookCoord rotated db shifted point tb of p withcolor green; |
| 326 |
else: |
| 327 |
textpathHookCoord := origin; |
| 328 |
fi |
| 329 |
fi |
| 330 |
enddef; |
| 331 |
|
| 332 |
|
| 333 |
%%% Internal macro. |
| 334 |
def transformStrokedItem(expr rawMode, item, p)= |
| 335 |
if textpathFancyStrokes = 0: |
| 336 |
transformSimpleItem(true, item, p); |
| 337 |
else: |
| 338 |
pp := pathpart item; |
| 339 |
for i=1 upto textpathRepeat: |
| 340 |
if (textpathHookCoord=origin) or (textpathCureSqrt=0): |
| 341 |
banker := (0, ypart point 0 of pp - autoScale*30bp + textpathShift); |
| 342 |
bx := (xpart point 0 of pp) + startOffset; |
| 343 |
dpp := angle direction 0 of pp; |
| 344 |
cx := (i-1)*(lenpic + ltextpathHSpace) + bx; |
| 345 |
tb := arctime cx of p; |
| 346 |
if textpathAbsRotation=0: |
| 347 |
db := textpathRotation + angle direction tb of p; |
| 348 |
else: |
| 349 |
db := textpathRotation; |
| 350 |
fi |
| 351 |
np := banker rotated db shifted point tb of p%{dir (dpp+db)} |
| 352 |
elseif textpathCureSqrt=1: |
| 353 |
np := textpathHookCoord shifted (dir(-90)*xpart point 0 of makepath penpart item); |
| 354 |
np := np rotated textpathHookRot shifted textpathHookPoint%{dir textpathHookRot} |
| 355 |
fi |
| 356 |
for h=1 upto textpathStrokePrecision: |
| 357 |
hide( |
| 358 |
banker := (0, ypart point (h/textpathStrokePrecision) of pp - autoScale*30bp + textpathShift); |
| 359 |
bx := (xpart point (h/textpathStrokePrecision) of pp) + startOffset; |
| 360 |
cx := (i-1)*(lenpic + ltextpathHSpace) + bx; |
| 361 |
dpp := angle direction (h/textpathStrokePrecision) of pp; |
| 362 |
tb := arctime cx of p; |
| 363 |
if textpathAbsRotation=0: |
| 364 |
db := textpathRotation + angle direction tb of p; |
| 365 |
else: |
| 366 |
db := textpathRotation; |
| 367 |
fi |
| 368 |
) |
| 369 |
..banker rotated db shifted point tb of p%{dir (dpp+db)} |
| 370 |
endfor; |
| 371 |
if odd (textpathDebug div 2): |
| 372 |
addto pic doublepath bbox np withpen currentpen; |
| 373 |
fi |
| 374 |
if odd (textpathDebug div 1): |
| 375 |
addto pic doublepath np withpen penpart item; |
| 376 |
fi |
| 377 |
if odd (textpathDebug div 4): |
| 378 |
%%% Doesn't work. |
| 379 |
% addto pic contour fullcircle scaled 3bp shifted point tb of p; |
| 380 |
fi |
| 381 |
endfor |
| 382 |
fi |
| 383 |
enddef; |
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
%%% Internal working macro. |
| 389 |
%%% Set text from picture variable along a path with justification j. |
| 390 |
%%% Parameters: |
| 391 |
%%% * a picture that contains the text that has to be transformed onto a path, |
| 392 |
%%% * a path p the text should follow, |
| 393 |
%%% * a numeric variable that controls justification, |
| 394 |
%%% Return value: |
| 395 |
%%% * a picture variable containing text following a path. |
| 396 |
vardef textpathToPic(expr atextpic, p, j)= |
| 397 |
save bitem, banker, k, bx, cx, tb, db; |
| 398 |
numeric k, bx, cx, tb, db; |
| 399 |
picture bitem; |
| 400 |
pair banker; |
| 401 |
|
| 402 |
%%% Calculate variables such as startOffset, autoScale, ltextpathHSpace etc. |
| 403 |
preprocessTextPic(false, atextpic, p, j); |
| 404 |
%%% |
| 405 |
%%% Now iterate over all picture elements. |
| 406 |
%%% |
| 407 |
k := 0; |
| 408 |
for item within textpic: |
| 409 |
if textual item:%%% Process textual elements. |
| 410 |
transformSimpleItem(false, item, p); |
| 411 |
k := k + 1; |
| 412 |
else: |
| 413 |
message "Package textpath warning: Unsupported picture element found!"; |
| 414 |
fi |
| 415 |
endfor |
| 416 |
%%% Return pic. |
| 417 |
pic |
| 418 |
enddef; |
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
%%% Internal working macro. |
| 423 |
%%% Set text from picture variable along a path with justification j. |
| 424 |
%%% Parameters: |
| 425 |
%%% * a picture that contains the text that has to be transformed onto a path, |
| 426 |
%%% * a path p the text should follow, |
| 427 |
%%% * a numeric variable that controls justification, |
| 428 |
%%% Return value: |
| 429 |
%%% * a picture variable containing text following a path. |
| 430 |
vardef textpathRawToPic(expr atextpic, p, j)= |
| 431 |
save bitem, banker, bx, cx, tb, db, pp, np, dpp; |
| 432 |
numeric bx, cx, tb, db, dpp; |
| 433 |
picture bitem; |
| 434 |
pair banker; |
| 435 |
path pp, np; |
| 436 |
|
| 437 |
%%% Calculate variables such as startOffset, autoScale, ltextpathHSpace etc. |
| 438 |
preprocessTextPic(true, atextpic, p, j); |
| 439 |
%%% Reset last sqrt glyph's top right bounding box corner. |
| 440 |
textpathHookCoord := origin; |
| 441 |
%%% |
| 442 |
%%% Now iterate over all picture elements. |
| 443 |
%%% |
| 444 |
for item within textpic: |
| 445 |
if textual item:%%% Process textual elements. |
| 446 |
transformSimpleItem(true, item, p); |
| 447 |
elseif stroked item:%%% Process stroked elements. |
| 448 |
transformStrokedItem(true, item, p); |
| 449 |
else: |
| 450 |
message "Package textpath warning: Unsupported picture element found!"; |
| 451 |
fi |
| 452 |
endfor |
| 453 |
pic |
| 454 |
enddef; |