Blame


1 61f5c35c 2004-05-15 devnull #include <u.h>
2 61f5c35c 2004-05-15 devnull #include <libc.h>
3 61f5c35c 2004-05-15 devnull #include <bio.h>
4 61f5c35c 2004-05-15 devnull #include <stdio.h>
5 61f5c35c 2004-05-15 devnull #include "../common/common.h"
6 61f5c35c 2004-05-15 devnull #include "ps_include.h"
7 61f5c35c 2004-05-15 devnull
8 61f5c35c 2004-05-15 devnull extern int curpostfontid;
9 61f5c35c 2004-05-15 devnull extern int curfontsize;
10 61f5c35c 2004-05-15 devnull
11 61f5c35c 2004-05-15 devnull typedef struct {long start, end;} Section;
12 61f5c35c 2004-05-15 devnull static char *buf;
13 61f5c35c 2004-05-15 devnull
14 e8fb1d3e 2004-05-17 devnull static void
15 e8fb1d3e 2004-05-17 devnull copy(Biobuf *fin, Biobuf *fout, Section *s) {
16 61f5c35c 2004-05-15 devnull if (s->end <= s->start)
17 61f5c35c 2004-05-15 devnull return;
18 61f5c35c 2004-05-15 devnull Bseek(fin, s->start, 0);
19 61f5c35c 2004-05-15 devnull while (Bseek(fin, 0L, 1) < s->end && (buf=Brdline(fin, '\n')) != NULL){
20 61f5c35c 2004-05-15 devnull /*
21 61f5c35c 2004-05-15 devnull * We have to be careful here, because % can legitimately appear
22 61f5c35c 2004-05-15 devnull * in Ascii85 encodings, and must not be elided.
23 61f5c35c 2004-05-15 devnull * The goal here is to make any DSC comments impotent without
24 61f5c35c 2004-05-15 devnull * actually changing the behavior of the Postscript.
25 61f5c35c 2004-05-15 devnull * Since stripping ``comments'' breaks Ascii85, we can instead just
26 61f5c35c 2004-05-15 devnull * indent comments a space, which turns DSC comments into non-DSC comments
27 61f5c35c 2004-05-15 devnull * and has no effect on binary encodings, which are whitespace-blind.
28 61f5c35c 2004-05-15 devnull */
29 61f5c35c 2004-05-15 devnull if(buf[0] == '%')
30 61f5c35c 2004-05-15 devnull Bputc(fout, ' ');
31 61f5c35c 2004-05-15 devnull Bwrite(fout, buf, Blinelen(fin));
32 61f5c35c 2004-05-15 devnull }
33 61f5c35c 2004-05-15 devnull }
34 61f5c35c 2004-05-15 devnull
35 61f5c35c 2004-05-15 devnull /*
36 61f5c35c 2004-05-15 devnull *
37 61f5c35c 2004-05-15 devnull * Reads a PostScript file (*fin), and uses structuring comments to locate the
38 61f5c35c 2004-05-15 devnull * prologue, trailer, global definitions, and the requested page. After the whole
39 61f5c35c 2004-05-15 devnull * file is scanned, the special ps_include PostScript definitions are copied to
40 61f5c35c 2004-05-15 devnull * *fout, followed by the prologue, global definitions, the requested page, and
41 61f5c35c 2004-05-15 devnull * the trailer. Before returning the initial environment (saved in PS_head) is
42 61f5c35c 2004-05-15 devnull * restored.
43 61f5c35c 2004-05-15 devnull *
44 61f5c35c 2004-05-15 devnull * By default we assume the picture is 8.5 by 11 inches, but the BoundingBox
45 61f5c35c 2004-05-15 devnull * comment, if found, takes precedence.
46 61f5c35c 2004-05-15 devnull *
47 61f5c35c 2004-05-15 devnull */
48 61f5c35c 2004-05-15 devnull /* *fin, *fout; /* input and output files */
49 61f5c35c 2004-05-15 devnull /* page_no; /* physical page number from *fin */
50 61f5c35c 2004-05-15 devnull /* whiteout; /* erase picture area */
51 61f5c35c 2004-05-15 devnull /* outline; /* draw a box around it and */
52 61f5c35c 2004-05-15 devnull /* scaleboth; /* scale both dimensions - if not zero */
53 61f5c35c 2004-05-15 devnull /* cx, cy; /* center of the picture and */
54 61f5c35c 2004-05-15 devnull /* sx, sy; /* its size - in current coordinates */
55 61f5c35c 2004-05-15 devnull /* ax, ay; /* left-right, up-down adjustment */
56 61f5c35c 2004-05-15 devnull /* rot; /* rotation - in clockwise degrees */
57 61f5c35c 2004-05-15 devnull
58 61f5c35c 2004-05-15 devnull void
59 e8fb1d3e 2004-05-17 devnull ps_include(Biobuf *fin, Biobuf *fout, int page_no, int whiteout,
60 61f5c35c 2004-05-15 devnull int outline, int scaleboth, double cx, double cy, double sx, double sy,
61 61f5c35c 2004-05-15 devnull double ax, double ay, double rot) {
62 61f5c35c 2004-05-15 devnull char **strp;
63 61f5c35c 2004-05-15 devnull int foundpage = 0; /* found the page when non zero */
64 61f5c35c 2004-05-15 devnull int foundpbox = 0; /* found the page bounding box */
65 61f5c35c 2004-05-15 devnull int nglobal = 0; /* number of global defs so far */
66 61f5c35c 2004-05-15 devnull int maxglobal = 0; /* and the number we've got room for */
67 61f5c35c 2004-05-15 devnull Section prolog, page, trailer; /* prologue, page, and trailer offsets */
68 e8fb1d3e 2004-05-17 devnull Section *global = 0; /* offsets for all global definitions */
69 61f5c35c 2004-05-15 devnull double llx, lly; /* lower left and */
70 61f5c35c 2004-05-15 devnull double urx, ury; /* upper right corners - default coords */
71 61f5c35c 2004-05-15 devnull double w = whiteout != 0; /* mostly for the var() macro */
72 61f5c35c 2004-05-15 devnull double o = outline != 0;
73 61f5c35c 2004-05-15 devnull double s = scaleboth != 0;
74 61f5c35c 2004-05-15 devnull int i; /* loop index */
75 61f5c35c 2004-05-15 devnull
76 61f5c35c 2004-05-15 devnull #define has(word) (strncmp(buf, word, strlen(word)) == 0)
77 61f5c35c 2004-05-15 devnull #define grab(n) ((Section *)(nglobal \
78 61f5c35c 2004-05-15 devnull ? realloc((char *)global, n*sizeof(Section)) \
79 61f5c35c 2004-05-15 devnull : calloc(n, sizeof(Section))))
80 61f5c35c 2004-05-15 devnull
81 61f5c35c 2004-05-15 devnull llx = lly = 0; /* default BoundingBox - 8.5x11 inches */
82 61f5c35c 2004-05-15 devnull urx = 72 * 8.5;
83 61f5c35c 2004-05-15 devnull ury = 72 * 11.0;
84 61f5c35c 2004-05-15 devnull
85 61f5c35c 2004-05-15 devnull /* section boundaries and bounding box */
86 61f5c35c 2004-05-15 devnull
87 61f5c35c 2004-05-15 devnull prolog.start = prolog.end = 0;
88 61f5c35c 2004-05-15 devnull page.start = page.end = 0;
89 61f5c35c 2004-05-15 devnull trailer.start = 0;
90 61f5c35c 2004-05-15 devnull Bseek(fin, 0L, 0);
91 61f5c35c 2004-05-15 devnull
92 61f5c35c 2004-05-15 devnull while ((buf=Brdline(fin, '\n')) != NULL) {
93 61f5c35c 2004-05-15 devnull buf[Blinelen(fin)-1] = '\0';
94 61f5c35c 2004-05-15 devnull if (!has("%%"))
95 61f5c35c 2004-05-15 devnull continue;
96 61f5c35c 2004-05-15 devnull else if (has("%%Page: ")) {
97 61f5c35c 2004-05-15 devnull if (!foundpage)
98 61f5c35c 2004-05-15 devnull page.start = Bseek(fin, 0L, 1);
99 61f5c35c 2004-05-15 devnull sscanf(buf, "%*s %*s %d", &i);
100 61f5c35c 2004-05-15 devnull if (i == page_no)
101 61f5c35c 2004-05-15 devnull foundpage = 1;
102 61f5c35c 2004-05-15 devnull else if (foundpage && page.end <= page.start)
103 61f5c35c 2004-05-15 devnull page.end = Bseek(fin, 0L, 1);
104 61f5c35c 2004-05-15 devnull } else if (has("%%EndPage: ")) {
105 61f5c35c 2004-05-15 devnull sscanf(buf, "%*s %*s %d", &i);
106 61f5c35c 2004-05-15 devnull if (i == page_no) {
107 61f5c35c 2004-05-15 devnull foundpage = 1;
108 61f5c35c 2004-05-15 devnull page.end = Bseek(fin, 0L, 1);
109 61f5c35c 2004-05-15 devnull }
110 61f5c35c 2004-05-15 devnull if (!foundpage)
111 61f5c35c 2004-05-15 devnull page.start = Bseek(fin, 0L, 1);
112 61f5c35c 2004-05-15 devnull } else if (has("%%PageBoundingBox: ")) {
113 61f5c35c 2004-05-15 devnull if (i == page_no) {
114 61f5c35c 2004-05-15 devnull foundpbox = 1;
115 61f5c35c 2004-05-15 devnull sscanf(buf, "%*s %lf %lf %lf %lf",
116 61f5c35c 2004-05-15 devnull &llx, &lly, &urx, &ury);
117 61f5c35c 2004-05-15 devnull }
118 61f5c35c 2004-05-15 devnull } else if (has("%%BoundingBox: ")) {
119 61f5c35c 2004-05-15 devnull if (!foundpbox)
120 61f5c35c 2004-05-15 devnull sscanf(buf,"%*s %lf %lf %lf %lf",
121 61f5c35c 2004-05-15 devnull &llx, &lly, &urx, &ury);
122 61f5c35c 2004-05-15 devnull } else if (has("%%EndProlog") || has("%%EndSetup") || has("%%EndDocumentSetup"))
123 61f5c35c 2004-05-15 devnull prolog.end = page.start = Bseek(fin, 0L, 1);
124 61f5c35c 2004-05-15 devnull else if (has("%%Trailer"))
125 61f5c35c 2004-05-15 devnull trailer.start = Bseek(fin, 0L, 1);
126 61f5c35c 2004-05-15 devnull else if (has("%%BeginGlobal")) {
127 61f5c35c 2004-05-15 devnull if (page.end <= page.start) {
128 61f5c35c 2004-05-15 devnull if (nglobal >= maxglobal) {
129 61f5c35c 2004-05-15 devnull maxglobal += 20;
130 61f5c35c 2004-05-15 devnull global = grab(maxglobal);
131 61f5c35c 2004-05-15 devnull }
132 61f5c35c 2004-05-15 devnull global[nglobal].start = Bseek(fin, 0L, 1);
133 61f5c35c 2004-05-15 devnull }
134 61f5c35c 2004-05-15 devnull } else if (has("%%EndGlobal"))
135 61f5c35c 2004-05-15 devnull if (page.end <= page.start)
136 61f5c35c 2004-05-15 devnull global[nglobal++].end = Bseek(fin, 0L, 1);
137 61f5c35c 2004-05-15 devnull }
138 61f5c35c 2004-05-15 devnull Bseek(fin, 0L, 2);
139 61f5c35c 2004-05-15 devnull if (trailer.start == 0)
140 61f5c35c 2004-05-15 devnull trailer.start = Bseek(fin, 0L, 1);
141 61f5c35c 2004-05-15 devnull trailer.end = Bseek(fin, 0L, 1);
142 61f5c35c 2004-05-15 devnull
143 61f5c35c 2004-05-15 devnull if (page.end <= page.start)
144 61f5c35c 2004-05-15 devnull page.end = trailer.start;
145 61f5c35c 2004-05-15 devnull
146 61f5c35c 2004-05-15 devnull /*
147 61f5c35c 2004-05-15 devnull fprint(2, "prolog=(%d,%d)\n", prolog.start, prolog.end);
148 61f5c35c 2004-05-15 devnull fprint(2, "page=(%d,%d)\n", page.start, page.end);
149 61f5c35c 2004-05-15 devnull for(i = 0; i < nglobal; i++)
150 61f5c35c 2004-05-15 devnull fprint(2, "global[%d]=(%d,%d)\n", i, global[i].start, global[i].end);
151 61f5c35c 2004-05-15 devnull fprint(2, "trailer=(%d,%d)\n", trailer.start, trailer.end);
152 61f5c35c 2004-05-15 devnull */
153 61f5c35c 2004-05-15 devnull
154 61f5c35c 2004-05-15 devnull /* all output here */
155 61f5c35c 2004-05-15 devnull for (strp = PS_head; *strp != NULL; strp++)
156 61f5c35c 2004-05-15 devnull Bwrite(fout, *strp, strlen(*strp));
157 61f5c35c 2004-05-15 devnull
158 61f5c35c 2004-05-15 devnull Bprint(fout, "/llx %g def\n", llx);
159 61f5c35c 2004-05-15 devnull Bprint(fout, "/lly %g def\n", lly);
160 61f5c35c 2004-05-15 devnull Bprint(fout, "/urx %g def\n", urx);
161 61f5c35c 2004-05-15 devnull Bprint(fout, "/ury %g def\n", ury);
162 61f5c35c 2004-05-15 devnull Bprint(fout, "/w %g def\n", w);
163 61f5c35c 2004-05-15 devnull Bprint(fout, "/o %g def\n", o);
164 61f5c35c 2004-05-15 devnull Bprint(fout, "/s %g def\n", s);
165 61f5c35c 2004-05-15 devnull Bprint(fout, "/cx %g def\n", cx);
166 61f5c35c 2004-05-15 devnull Bprint(fout, "/cy %g def\n", cy);
167 61f5c35c 2004-05-15 devnull Bprint(fout, "/sx %g def\n", sx);
168 61f5c35c 2004-05-15 devnull Bprint(fout, "/sy %g def\n", sy);
169 61f5c35c 2004-05-15 devnull Bprint(fout, "/ax %g def\n", ax);
170 61f5c35c 2004-05-15 devnull Bprint(fout, "/ay %g def\n", ay);
171 61f5c35c 2004-05-15 devnull Bprint(fout, "/rot %g def\n", rot);
172 61f5c35c 2004-05-15 devnull
173 61f5c35c 2004-05-15 devnull for (strp = PS_setup; *strp != NULL; strp++)
174 61f5c35c 2004-05-15 devnull Bwrite(fout, *strp, strlen(*strp));
175 61f5c35c 2004-05-15 devnull
176 61f5c35c 2004-05-15 devnull copy(fin, fout, &prolog);
177 61f5c35c 2004-05-15 devnull for(i = 0; i < nglobal; i++)
178 61f5c35c 2004-05-15 devnull copy(fin, fout, &global[i]);
179 61f5c35c 2004-05-15 devnull copy(fin, fout, &page);
180 61f5c35c 2004-05-15 devnull copy(fin, fout, &trailer);
181 61f5c35c 2004-05-15 devnull for (strp = PS_tail; *strp != NULL; strp++)
182 61f5c35c 2004-05-15 devnull Bwrite(fout, *strp, strlen(*strp));
183 61f5c35c 2004-05-15 devnull
184 61f5c35c 2004-05-15 devnull if(nglobal)
185 61f5c35c 2004-05-15 devnull free(global);
186 61f5c35c 2004-05-15 devnull
187 61f5c35c 2004-05-15 devnull /* force the program to reestablish its state */
188 61f5c35c 2004-05-15 devnull curpostfontid = -1;
189 61f5c35c 2004-05-15 devnull curfontsize = -1;
190 61f5c35c 2004-05-15 devnull }