| 1 |
%% |
| 2 |
%% naive paragraph formatting for metapost. You get better results using |
| 3 |
%% btex .. etex, since that will give you kerning, ligatures, and the |
| 4 |
%% famous TeX paragraph breaking algorithm. This one is OK for me, though, |
| 5 |
%% |
| 6 |
%% Copyright 2001,2003 Patrick TJ McPhee |
| 7 |
%% everyone is welcome to use this code for any purpose, to modify it, and |
| 8 |
%% to copy it in whole or in part for use in other macro sets, with the |
| 9 |
%% conditions that this copyright notice be preserved with any significant |
| 10 |
%% portion of the code, and that modifications to this file be clearly |
| 11 |
%% marked. |
| 12 |
|
| 13 |
|
| 14 |
% arguments |
| 15 |
% s - text to format |
| 16 |
% w - maximum width |
| 17 |
% b = baseline skip |
| 18 |
|
| 19 |
vardef breaktowidth(expr s, w, b) = |
| 20 |
save p; |
| 21 |
picture p; |
| 22 |
|
| 23 |
p := s infont defaultfont scaled defaultscale; |
| 24 |
% if the string is too long to fit within bounds, we break it up on |
| 25 |
% word boundaries. |
| 26 |
if xpart(lrcorner p) > w: |
| 27 |
save q, gq, offs, len, goodlen, gl, ls, sp; |
| 28 |
|
| 29 |
picture q, gq; |
| 30 |
numeric offs, len, goodlen, ls, gl; |
| 31 |
string sp; |
| 32 |
|
| 33 |
p := nullpicture; |
| 34 |
offs := 0; % offset of current substring within s |
| 35 |
len := 1; % most recent character examined |
| 36 |
goodlen := 0; % most recent reasonable break point |
| 37 |
ls := length(s); |
| 38 |
|
| 39 |
forever: |
| 40 |
if len < ls: |
| 41 |
sp := substring(len, len+1) of s; |
| 42 |
else: |
| 43 |
sp := " "; |
| 44 |
fi; |
| 45 |
|
| 46 |
if (sp = " ") or (sp = "-") or (sp = "/"): |
| 47 |
|
| 48 |
if sp <>" ": |
| 49 |
len := len + 1; |
| 50 |
fi; |
| 51 |
|
| 52 |
% test to see if this is within the needed width |
| 53 |
q := (substring(offs, len) of s) infont defaultfont scaled defaultscale; |
| 54 |
|
| 55 |
|
| 56 |
if xpart (lrcorner q) > w: |
| 57 |
% if there's no good break point, build up until we |
| 58 |
% get something that fits -- can't use goodlen as the |
| 59 |
% array index as it is reset to 0 after the loop ends |
| 60 |
if goodlen <= offs: |
| 61 |
for gl = (len-1) downto (offs+1): |
| 62 |
goodlen := gl; |
| 63 |
gq := (substring(offs, goodlen) of s) infont defaultfont scaled defaultscale; |
| 64 |
exitunless xpart (lrcorner gq) > w; |
| 65 |
endfor; |
| 66 |
fi; |
| 67 |
|
| 68 |
|
| 69 |
p := image(draw p shifted (0, b); |
| 70 |
draw gq;); |
| 71 |
offs := goodlen; |
| 72 |
len := offs + 1; |
| 73 |
|
| 74 |
% we still fit, so save the current position |
| 75 |
else: |
| 76 |
if sp <> " ": |
| 77 |
goodlen := len; |
| 78 |
else: |
| 79 |
goodlen := len+1; |
| 80 |
fi; |
| 81 |
gq := q; |
| 82 |
fi; |
| 83 |
fi; |
| 84 |
|
| 85 |
exitunless len < ls; |
| 86 |
|
| 87 |
len := len + 1; |
| 88 |
endfor; |
| 89 |
|
| 90 |
if goodlen < ls: |
| 91 |
gq := (substring(offs, ls) of s) infont defaultfont scaled defaultscale; |
| 92 |
fi; |
| 93 |
|
| 94 |
if goodlen <> ls: |
| 95 |
p := image(draw p shifted (0, b); |
| 96 |
draw gq;); |
| 97 |
fi; |
| 98 |
fi; |
| 99 |
p |
| 100 |
enddef; |
| 101 |
|