ps: use a dictionary in selectfont_cached
[neatpost.git] / ps.c
blob98bf722d0775aea0002ef23f41ee9f5ed6e2f117
1 #include "post.h"
3 void ps_pagebeg(int n)
5 out("%%%%Page: %d %d\n", n, n);
6 out("/saveobj save def\n");
7 out("mark\n");
8 out("%d pagesetup\n", n);
11 void ps_pageend(int n)
13 out("cleartomark\n");
14 out("showpage\n");
15 out("saveobj restore\n");
18 void ps_trailer(int pages, char *fonts)
20 out("%%%%Trailer\n");
21 out("done\n");
22 out("%%%%DocumentFonts: %s\n", fonts);
23 out("%%%%Pages: %d\n", pages);
24 out("%%%%EOF\n");
27 static char *prolog =
28 "/setup {\n"
29 " counttomark 2 idiv {def} repeat pop\n"
30 " /scaling 72 resolution div def\n"
31 " linewidth setlinewidth\n"
32 " 1 setlinecap\n"
33 " 0 pagesize 1 get translate\n"
34 " scaling scaling scale\n"
35 " 0 0 moveto\n"
36 "} def\n"
37 "\n"
38 "/pagesetup {\n"
39 " /page exch def\n"
40 " currentdict /pagedict known currentdict page known and {\n"
41 " page load pagedict exch get cvx exec\n"
42 " } if\n"
43 "} def\n"
44 "\n"
45 "/w {neg moveto show} bind def\n"
46 "/m {neg moveto} bind def\n"
47 "/g {neg moveto {glyphshow} forall} bind def\n"
48 "/rgb {255 div 3 1 roll 255 div 3 1 roll 255 div 3 1 roll setrgbcolor} bind def\n"
49 "/done {/lastpage where {pop lastpage} if} def\n"
50 "\n"
51 "% caching fonts, as selectfont is supposed to be doing\n"
52 "/fncache 16 dict def\n"
53 "/selectfont_append { fncache exch dup findfont put } bind def\n"
54 "/selectfont_cached {\n"
55 " exch dup fncache exch known not { dup selectfont_append } if\n"
56 " fncache exch get exch scalefont setfont\n"
57 "} bind def\n"
58 "/f {\n"
59 " exch dup 3 1 roll scaling div selectfont_cached\n"
60 " linewidth mul scaling 10 mul div setlinewidth\n"
61 "} bind def\n"
62 "\n"
63 "/savedmatrix matrix def\n"
64 "/drawl {\n"
65 " neg lineto\n"
66 "} bind def\n"
67 "/drawe {\n"
68 " savedmatrix currentmatrix pop scale\n"
69 " .5 0 rmoveto currentpoint .5 0 rmoveto .5 0 360 arc\n"
70 " savedmatrix setmatrix\n"
71 "} bind def\n"
72 "/drawa {\n"
73 " /dy2 exch def\n"
74 " /dx2 exch def\n"
75 " /dy1 exch def\n"
76 " /dx1 exch def\n"
77 " currentpoint dy1 neg add exch dx1 add exch\n"
78 " dx1 dx1 mul dy1 dy1 mul add sqrt\n"
79 " dy1 dx1 neg atan\n"
80 " dy2 neg dx2 atan\n"
81 " arc\n"
82 "} bind def\n"
83 "/draws {\n"
84 " /y2 exch def\n"
85 " /x2 exch def\n"
86 " /y1 exch def\n"
87 " /x1 exch def\n"
88 " /y0 exch def\n"
89 " /x0 exch def\n"
90 " x0 5 x1 mul add 6 div\n"
91 " y0 5 y1 mul add -6 div\n"
92 " x2 5 x1 mul add 6 div\n"
93 " y2 5 y1 mul add -6 div\n"
94 " x1 x2 add 2 div\n"
95 " y1 y2 add -2 div\n"
96 " curveto\n"
97 "} bind def\n";
99 void ps_header(int pagewidth, int pageheight, int linewidth)
101 out("%%!PS-Adobe-2.0\n");
102 out("%%%%Version: 1.0\n");
103 out("%%%%Creator: neatroff - http://litcave.rudi.ir/\n");
104 out("%%%%DocumentFonts: (atend)\n");
105 out("%%%%Pages: (atend)\n");
106 out("%%%%EndComments\n");
108 out("%%%%BeginProlog\n");
109 out("/resolution %d def\n", dev_res);
110 out("/pagesize [%d %d] def\n", (pagewidth * 720 + 127) / 254,
111 (pageheight * 720 + 127) / 254);
112 out("/linewidth %d.%02d def\n\n", linewidth / 100, linewidth % 100);
113 out("%s", prolog);
114 out("%%%%EndProlog\n");
115 out("%%%%BeginSetup\n");
116 out("<< /PageSize pagesize /ImagingBBox null >> setpagedevice\n");
117 out("mark\n");
118 out("setup\n");
119 out("%%%%EndSetup\n");