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