Debian Codegraph @

texk/xdvipsk/color.c

raw
1 /*
2 * This is a set of routines for dvips that are used to process color
3 * commands in the TeX file (passed by \special commands). This was
4 * orignally written by J. Hafner, E. Blanz and M. Flickner of IBM
5 * Research, Almaden Research Center. Contact [email protected].
6 * And then it was completely rewritten by Tomas Rokicki to:
7 *
8 * - Be easier on the memory allocator (malloc/free)
9 * - Use less memory overall (by a great deal) and be faster
10 * - Work with the -C, -a, and other options
11 * - Be more adaptable to other drivers and previewers.
12 *
13 * The motivating idea: we want to be able to process 5,000 page
14 * documents using lots of colors on each page on a 640K IBM PC
15 * relatively quickly.
16 */
17 #ifndef XDVIPSK
18 #include "dvips.h" /* The copyright notice in that file is included too! */
19 #else
20 #include "xdvips.h" /* The copyright notice in that file is included too! */
21 #endif /* XDVIPSK */
22 #include <stdio.h>
23 /*
24 * The external declarations:
25 */
26 #include "protos.h"
27
28 /*
29 * Here we set some limits on some color stuff.
30 */
31 #define COLORHASH (89)
32 #define MAXCOLORLEN (120) /* maximum color length for background */
33 #define INITCOLORLEN (10000) /* initial stack size in chars */
34 #define OUTBUFSIZ (100) /* base limit on special length */
35 /*
36 * This is where we store the color information for a particular page.
37 * If we free all of these, we free all of the allocated color
38 * stuff; we do no allocations (but one) other than when we allocate
39 * these.
40 */
41 static struct colorpage {
42 struct colorpage *next;
43 integer boploc; /* we use the bop loc as a page indicator */
44 char *bg;
45 char *colordat;
46 } *colorhash[COLORHASH];
47 static char *cstack, *csp, *cend, *bg;
48 /*
49 * This routine sends a color command out. If the command is a
50 * single `word' or starts with a double quote, we send it out
51 * exactly as is (except for any double quote.) If the command
52 * is a word followed by arguments, we send out the arguments and
53 * then the word prefixed by "TeXcolor".
54 */
55 static void
56 colorcmdout(char *s)
57 {
58 char *p;
59 char tempword[OUTBUFSIZ];
60 integer templen;
61
62 while (*s && *s <= ' ')
63 s++;
64 if (*s == '"') {
65 cmdout(s+1);
66 return;
67 }
68 for (p=s; *p && *p > ' '; p++);
69 for (; *p && *p <= ' '; p++);
70 if (*p == 0) {
71 cmdout(s);
72 return;
73 }
74 cmdout(p);
75
76 strcpy(tempword, "TeXcolor");
77 templen = strlen("TeXcolor") + strlen(s) + 2; /* 2 is slop, just in case */
78 if (templen >= OUTBUFSIZ) {
79 /* PostScript names get truncated to around 128 characters anyway,
80 so there's no reason to allow longer here. */
81 sprintf(errbuf, "! TeXcolor special name longer than %d characters",
82 OUTBUFSIZ);
83 error (errbuf);
84 }
85
86 for (p=tempword + strlen(tempword); *s && *s > ' '; p++, s++)
87 *p = *s;
88 *p = 0;
89 cmdout(tempword);
90 return;
91 }
92 /*
93 * For a new dvi file, call this. Frees all allocated memory.
94 */
95 #define DEFAULTCOLOR "Black"
96 void initcolor(void) {
97 int i;
98 struct colorpage *p, *q;
99
100 for (i=0; i<COLORHASH; i++) {
101 for (p=colorhash[i]; p; p = q) {
102 q = p->next;
103 free(p->colordat);
104 free(p);
105 }
106 colorhash[i] = 0;
107 }
108 cstack = (char *)mymalloc(INITCOLORLEN);
109 strcpy(cstack, "\n");
110 strcat(cstack, DEFAULTCOLOR);
111 csp = cstack + strlen(cstack);
112 cend = cstack + INITCOLORLEN - 3; /* for conservativeness */
113 bg = 0;
114 }
115 /*
116 * This takes a call from predospecial to set the background color for
117 * the current page. It is saved in stackdepth and backed down the
118 * stack during popcolors.
119 */
120 void
121 background(char *bkgrnd)
122 {
123 if (bkgrnd && *bkgrnd) {
124 if (strlen(bkgrnd) > MAXCOLORLEN)
125 error(" color name too long; ignored");
126 else
127 strcpy(bg, bkgrnd);
128 }
129 }
130 /*
131 * This routine puts a call from \special for color on the colorstack
132 * and sets the color in the PostScript.
133 */
134 void
135 pushcolor(char *p, Boolean outtops)
136 {
137 while (strlen(p) + csp > cend) {
138 int newlen = 3 * (cend - cstack);
139 char *newcs = (char *)mymalloc(newlen);
140 strcpy(newcs, cstack);
141 csp = newcs + (csp - cstack);
142 cend = newcs + newlen - 3;
143 cstack = newcs;
144 }
145 *csp++ = '\n';
146 strcpy(csp, p);
147 csp += strlen(p);
148 if (outtops) {
149 colorcmdout(p);
150 }
151 }
152 /*
153 * This routine removes a color from the colorstack and resets the color
154 * in the PostScript to the previous color.
155 */
156 void
157 popcolor(Boolean outtops)
158 {
159 char *p = csp - 1;
160
161 while (p >= cstack && *p != '\n')
162 p--;
163 if (p == cstack)
164 return; /* We don't pop the last color as that is global */
165 *p = 0;
166 csp = p;
167 for (p--; p >= cstack && *p != '\n'; p--);
168 p++;
169 if ( outtops ) {
170 colorcmdout(p);
171 }
172 }
173 /*
174 * This routine clears the colorstack, pushes a new color onto the stack
175 * (this is now the root or global color).
176 */
177 void
178 resetcolorstack(char * p, int outtops)
179 {
180 #ifdef XDVIPSK
181 char buf[1024];
182 #endif /* XDVIPSK */
183 char *q = csp - 1;
184
185 while (q > cstack && *q != '\n')
186 q--;
187 if (q > cstack && outtops == 0) {
188 #ifdef SHORTINT
189 fprintf(stderr, "You've mistakenly made a global color change ");
190 fprintf_str(stderr, "to %s within nested colors\n", p);
191 fprintf(stderr, "on page %ld. Will try to recover.\n", pagenum);
192 #ifdef XDVIPSK
193 sprintf(buf, "You've mistakenly made a global color change to %s within nested colors on page %d. Will try to recover.", p, pagenum);
194 writelogrecord(buf);
195 #endif /* XDVIPSK */
196 #else /* ~SHORTINT */
197 fprintf(stderr, "You've mistakenly made a global color change ");
198 fprintf_str(stderr, "to %s within nested colors\n", p);
199 fprintf(stderr, "on page %d. Will try to recover.\n", pagenum);
200 #ifdef XDVIPSK
201 sprintf(buf, "You've mistakenly made a global color change to %s within nested colors on page %d. Will try to recover.", p, pagenum);
202 writelogrecord(buf);
203 #endif /* XDVIPSK */
204 #endif /* ~SHORTINT */
205 }
206 csp = cstack;
207 *csp = 0;
208 pushcolor(p, outtops);
209 }
210 /*
211 * This routine is a bit magic. It looks up the page in the current
212 * hash table. If the page is already entered in the hash table, then
213 * it restores the color to what that page had, and sets the last
214 * color. This will occur if this is not the first time that this
215 * page has been encountered.
216 *
217 * If, on the other hand, this is the first time that the page has
218 * been encountered, then it will create a new hash entry and copy the
219 * current color information into it. Since we can only encounter a
220 * new page after having just finished scanning the previous page,
221 * this is safe.
222 */
223 void
224 bopcolor(int outtops)
225 {
226 integer pageloc = ftell(dvifile);
227 int h = pageloc % COLORHASH;
228 struct colorpage *p = colorhash[h];
229
230 while (p) {
231 if (p->boploc == pageloc)
232 break;
233 else
234 p = p->next;
235 }
236 if (p) {
237 strcpy(cstack, p->colordat);
238 csp = cstack + strlen(cstack);
239 bg = p->bg;
240 if (outtops && strcmp(bg, "White")!=0 && bg[0]) {
241 cmdout("gsave");
242 colorcmdout(bg);
243 cmdout("clippath fill grestore");
244 }
245 } else {
246 p = (struct colorpage *)mymalloc((integer)
247 (sizeof(struct colorpage)));
248 p->colordat = mymalloc(strlen(cstack) + MAXCOLORLEN + 2);
249 p->next = colorhash[h];
250 p->boploc = pageloc;
251 strcpy(p->colordat, cstack);
252 p->bg = p->colordat + strlen(cstack) + 1;
253 if (bg)
254 strcpy(p->bg, bg);
255 else
256 *(p->bg) = 0;
257 bg = p->bg;
258 colorhash[h] = p;
259 }
260 if (outtops) {
261 char *pp = csp - 1;
262 while (pp >= cstack && *pp != '\n')
263 pp--;
264 pp++;
265 if (strcmp(pp, DEFAULTCOLOR)!=0) {
266 colorcmdout(pp);
267 }
268 }
269 }