| 1 |
|
| 2 |
if known loadHershey: expandafter endinput fi |
| 3 |
|
| 4 |
% this default is for Debian with ‘hershey-fonts-data’ installed. |
| 5 |
newinternal string hersheyFonts; |
| 6 |
hersheyFonts := "/usr/share/hershey-fonts/"; |
| 7 |
|
| 8 |
vardef loadHershey @# = |
| 9 |
if known hershey.loaded.@# : |
| 10 |
message("Hershey font "&str(@#)&" already known: skipping rereading."); |
| 11 |
else: |
| 12 |
hershey.loaded.font := 1; |
| 13 |
doLoadHershey.@# ; |
| 14 |
fi |
| 15 |
enddef; |
| 16 |
vardef doLoadHershey suffix font = |
| 17 |
% create the variable hershey.<fontname> |
| 18 |
save var; string var; var := "hershey." & str font & "."; |
| 19 |
scantokens ("path "&var)[][]; |
| 20 |
% variables used for reading the file |
| 21 |
save file, line; |
| 22 |
string file, line; |
| 23 |
file := hersheyFonts & str font & ".jhf"; |
| 24 |
save pnum, lnum, wd, i, x, y, first; |
| 25 |
boolean first; lnum := 0; |
| 26 |
% read the font line by line |
| 27 |
forever : |
| 28 |
line := readfrom file; |
| 29 |
exitif line = EOF; |
| 30 |
lnum := lnum + 1; pnum := 1; first := true; |
| 31 |
% the line starts with a width, followed by min/max x coordinates, |
| 32 |
% followed by the data describing the glyph, in pairs of coordinates, |
| 33 |
% every coördinate encoded as a single character. |
| 34 |
wd := scantokens (substring(5,8) of line) - 1; |
| 35 |
if wd > 0 : |
| 36 |
line := substring (8, infinity) of line; |
| 37 |
x := ASCII(line) - 82; line := substring(1, infinity) of line; |
| 38 |
y := ASCII(line) - 82; line := substring(1, infinity) of line; |
| 39 |
scantokens(var)[lnum].lft := x; |
| 40 |
scantokens(var)[lnum].rt := y; |
| 41 |
%message char(lnum+31) & " (" & decimal(lnum) & "): (" & decimal x & ", " & decimal y & ")"; |
| 42 |
scantokens(var)[lnum][pnum] := fi |
| 43 |
for i = 1 upto wd : |
| 44 |
hide( % 82 is ASCII("R") |
| 45 |
x := ASCII(line) - 82; line := substring(1, infinity) of line; |
| 46 |
y := 82 - ASCII(line); line := substring(1, infinity) of line;) |
| 47 |
if (x,y) = (-50,0) : |
| 48 |
% the special string " R" separates paths |
| 49 |
; pnum := pnum+1; first := true; |
| 50 |
scantokens(var)[lnum][pnum] := |
| 51 |
else : |
| 52 |
if first : hide(first := false) |
| 53 |
else : -- fi (x,y) |
| 54 |
fi |
| 55 |
endfor; |
| 56 |
endfor |
| 57 |
closefrom file; |
| 58 |
enddef; |
| 59 |
|
| 60 |
vardef drawHershey @# (expr s) text t = |
| 61 |
if numeric s: drawHersheyChar @# (s) t; |
| 62 |
else : drawHersheyString @# (s) t; fi |
| 63 |
enddef; |
| 64 |
vardef drawHersheyChar @# (expr c) text t = |
| 65 |
save i; i := 1; forever : |
| 66 |
exitif unknown hershey.@#[c][i]; |
| 67 |
draw hershey.@#[c][i] t; |
| 68 |
i := i + 1; |
| 69 |
endfor |
| 70 |
enddef; |
| 71 |
vardef drawHersheyString @# (expr s) text t = |
| 72 |
save c, ss, sh; string ss; ss := s; sh := 0; |
| 73 |
forever : |
| 74 |
c := ASCII(ss) - 31; |
| 75 |
sh := sh + hershey.@#[c].rt; |
| 76 |
% NB. -9 is the baseline of the hershey fonts; |
| 77 |
% we put the baseline on x=0 instead. |
| 78 |
drawHersheyChar @# (c) shifted (sh, 9) t; |
| 79 |
sh := sh - hershey.@#[c].lft; |
| 80 |
ss := substring(1, infinity) of ss; |
| 81 |
exitif length(ss) = 0; |
| 82 |
endfor |
| 83 |
enddef; |
| 84 |
|