Debian Codegraph @

texk/web2c/luatexdir/pdf/pdfcolorstack.c

raw
1 /*
2
3 Copyright 2009-2011 Taco Hoekwater <[email protected]>
4
5 This file is part of LuaTeX.
6
7 LuaTeX is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2 of the License, or (at your
10 option) any later version.
11
12 LuaTeX is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15 License for more details.
16
17 You should have received a copy of the GNU General Public License along
18 with LuaTeX; if not, see <http://www.gnu.org/licenses/>.
19
20 */
21
22 #include "ptexlib.h"
23
24 /*tex
25
26 This module implements color stack support. The following array and
27 especially stack data structures are used.
28
29 \startitemize
30 \startitem
31 They automatically grow dynamically.
32 \stopitem
33 \startitem
34 The size never decreases.
35 \stopitem
36 \startitem
37 The variable with name ending in "size" contains the number how many
38 entries the data structure can hold.
39 \stopitem
40 \startitem
41 The variable with name ending in "used" contains the number of
42 actually used entries.
43 \stopitem
44 \startitem
45 Memory of strings in stack entries must be allocated and freed if the
46 stack is cleared.
47 \stopitem
48 \stopitemize
49
50 */
51
52 #define MAX_COLORSTACKS 32768
53
54 /*
55 The colorstack number is stored in two bytes (info field of the node);
56 condition (newcolorstack): |MAX_COLORSTACKS mod STACK_INCREMENT = 0|
57 */
58
59 #define COLOR_DEFAULT "0 g 0 G"
60
61 typedef struct {
62 char **page_stack;
63 char **form_stack;
64 char *page_current;
65 char *form_current;
66 char *form_init;
67 int page_size;
68 int form_size;
69 int page_used;
70 int form_used;
71 int literal_mode;
72 boolean page_start;
73 } colstack_type;
74
75 static colstack_type *colstacks = NULL;
76 static int colstacks_size = 0;
77 static int colstacks_used = 0;
78
79 /*tex
80
81 Initialization is done, if the color stacks are used, |init_colorstacks()| is
82 defined as macro to avoid unnecessary procedure calls.
83
84 */
85
86 #define init_colorstacks() if (colstacks_size == 0) colstacks_first_init();
87
88 static void colstacks_first_init(void)
89 {
90 colstacks_size = STACK_INCREMENT;
91 colstacks = xtalloc((unsigned) colstacks_size, colstack_type);
92 colstacks_used = 1;
93 colstacks[0].page_stack = NULL;
94 colstacks[0].form_stack = NULL;
95 colstacks[0].page_size = 0;
96 colstacks[0].form_size = 0;
97 colstacks[0].page_used = 0;
98 colstacks[0].form_used = 0;
99 colstacks[0].page_current = xstrdup(COLOR_DEFAULT);
100 colstacks[0].form_current = xstrdup(COLOR_DEFAULT);
101 colstacks[0].form_init = xstrdup(COLOR_DEFAULT);
102 colstacks[0].literal_mode = direct_always;
103 colstacks[0].page_start = true;
104 }
105
106 int colorstackused(void)
107 {
108 init_colorstacks();
109 return colstacks_used;
110 }
111
112 /*tex
113
114 A new color stack is setup with the given parameters. The stack number is
115 returned or -1 in case of error (no room).
116
117 */
118
119 int newcolorstack(const char *str, int literal_mode, boolean page_start)
120 {
121 colstack_type *colstack;
122 int colstack_num;
123 init_colorstacks();
124 /*tex Make room: */
125 if (colstacks_used == MAX_COLORSTACKS) {
126 return -1;
127 }
128 if (colstacks_used == colstacks_size) {
129 colstacks_size += STACK_INCREMENT;
130 /*tex
131 If |(MAX_COLORSTACKS mod STACK_INCREMENT = 0)| then we don't
132 need to check the case that size overruns |MAX_COLORSTACKS|.
133 */
134 colstacks = xreallocarray(colstacks, colstack_type, (unsigned) colstacks_size);
135 }
136 /*tex Claim new color stack. */
137 colstack_num = colstacks_used++;
138 colstack = &colstacks[colstack_num];
139 /*tex Configure the new color stack. */
140 colstack->page_stack = NULL;
141 colstack->form_stack = NULL;
142 colstack->page_size = 0;
143 colstack->page_used = 0;
144 colstack->form_size = 0;
145 colstack->form_used = 0;
146 colstack->literal_mode = literal_mode;
147 colstack->page_start = page_start;
148 colstack->page_current = NULL;
149 colstack->form_current = NULL;
150 colstack->form_init = NULL;
151 if (str) {
152 colstack->page_current = xstrdup(str);
153 colstack->form_current = xstrdup(str);
154 colstack->form_init = xstrdup(str);
155 }
156 return colstack_num;
157 }
158
159 #define get_colstack(n) (&colstacks[n])
160
161 /*tex Put a string on top of the string pool and updates |pool_ptr|. */
162
163 static void put_cstring_on_str_pool(char *str)
164 {
165 int save_selector = selector;
166 selector = new_string;
167 if (str == NULL || *str == 0) {
168 return;
169 }
170 tprint(str);
171 selector = save_selector;
172 }
173
174 static int colorstackset(int colstack_no, str_number s)
175 {
176 colstack_type *colstack = get_colstack(colstack_no);
177
178 if (global_shipping_mode == SHIPPING_PAGE) {
179 xfree(colstack->page_current);
180 colstack->page_current = makecstring(s);
181 } else {
182 xfree(colstack->form_current);
183 colstack->form_current = makecstring(s);
184 }
185 return colstack->literal_mode;
186 }
187
188 int colorstackcurrent(int colstack_no)
189 {
190 colstack_type *colstack = get_colstack(colstack_no);
191
192 if (global_shipping_mode == SHIPPING_PAGE) {
193 put_cstring_on_str_pool(colstack->page_current);
194 } else {
195 put_cstring_on_str_pool(colstack->form_current);
196 }
197 return colstack->literal_mode;
198 }
199
200 static int colorstackpush(int colstack_no, str_number s)
201 {
202 colstack_type *colstack = get_colstack(colstack_no);
203 char *str;
204 if (global_shipping_mode == SHIPPING_PAGE) {
205 if (colstack->page_used == colstack->page_size) {
206 colstack->page_size += STACK_INCREMENT;
207 xretalloc(colstack->page_stack, (unsigned) colstack->page_size, char *);
208 }
209 colstack->page_stack[colstack->page_used++] = colstack->page_current;
210 str = makecstring(s);
211 if (*str == 0) {
212 colstack->page_current = NULL;
213 } else {
214 colstack->page_current = xstrdup(str);
215 }
216 free(str);
217 } else {
218 if (colstack->form_used == colstack->form_size) {
219 colstack->form_size += STACK_INCREMENT;
220 xretalloc(colstack->form_stack, (unsigned) colstack->form_size, char *);
221 }
222 colstack->form_stack[colstack->form_used++] = colstack->form_current;
223 str = makecstring(s);
224 if (*str == 0) {
225 colstack->form_current = NULL;
226 } else {
227 colstack->form_current = xstrdup(str);
228 }
229 free(str);
230 }
231 return colstack->literal_mode;
232 }
233
234 int colorstackpop(int colstack_no)
235 {
236 colstack_type *colstack = get_colstack(colstack_no);
237 if (global_shipping_mode == SHIPPING_PAGE) {
238 if (colstack->page_used == 0) {
239 formatted_warning("pdf backend","pop empty color page stack %u",(unsigned int) colstack_no);
240 return colstack->literal_mode;
241 }
242 xfree(colstack->page_current);
243 colstack->page_current = colstack->page_stack[--colstack->page_used];
244 put_cstring_on_str_pool(colstack->page_current);
245 } else {
246 if (colstack->form_used == 0) {
247 formatted_warning("pdf backend","pop empty color form stack %u",(unsigned int) colstack_no);
248 return colstack->literal_mode;
249 }
250 xfree(colstack->form_current);
251 colstack->form_current = colstack->form_stack[--colstack->form_used];
252 put_cstring_on_str_pool(colstack->form_current);
253 }
254 return colstack->literal_mode;
255 }
256
257 void colorstackpagestart(void)
258 {
259 int i, j;
260 colstack_type *colstack;
261 if (global_shipping_mode == SHIPPING_PAGE) {
262 return;
263 }
264 for (i = 0; i < colstacks_used; i++) {
265 colstack = &colstacks[i];
266 for (j = 0; j < colstack->form_used; j++) {
267 xfree(colstack->form_stack[j]);
268 }
269 colstack->form_used = 0;
270 xfree(colstack->form_current);
271 if (colstack->form_init == NULL) {
272 colstack->form_current = NULL;
273 } else {
274 colstack->form_current = xstrdup(colstack->form_init);
275 }
276 }
277 }
278
279 int colorstackskippagestart(int colstack_no)
280 {
281 colstack_type *colstack = get_colstack(colstack_no);
282 if (!colstack->page_start) {
283 return 1;
284 }
285 if (colstack->page_current == NULL) {
286 return 0;
287 }
288 if (strcmp(COLOR_DEFAULT, colstack->page_current) == 0) {
289 return 2;
290 }
291 return 0;
292 }
293
294 void pdf_out_colorstack(PDF pdf, halfword p)
295 {
296 int old_setting;
297 str_number s;
298 int cmd = pdf_colorstack_cmd(p);
299 int stack_no = pdf_colorstack_stack(p);
300 int literal_mode = 0;
301 if (stack_no >= colorstackused()) {
302 formatted_warning("pdf backend","color stack %u is not initialized",(unsigned int) stack_no);
303 return;
304 }
305 switch (cmd) {
306 case colorstack_set:
307 case colorstack_push:
308 old_setting = selector;
309 selector = new_string;
310 show_token_list(token_link(pdf_colorstack_data(p)), null, -1);
311 selector = old_setting;
312 s = make_string();
313 if (cmd == colorstack_set)
314 literal_mode = colorstackset(stack_no, s);
315 else
316 literal_mode = colorstackpush(stack_no, s);
317 if (str_length(s) > 0)
318 pdf_literal(pdf, s, literal_mode, false);
319 flush_str(s);
320 return;
321 break;
322 case colorstack_pop:
323 literal_mode = colorstackpop(stack_no);
324 break;
325 case colorstack_current:
326 literal_mode = colorstackcurrent(stack_no);
327 break;
328 default:
329 break;
330 }
331 if (cur_length > 0) {
332 s = make_string();
333 pdf_literal(pdf, s, literal_mode, false);
334 flush_str(s);
335 }
336 }
337
338 void pdf_out_colorstack_startpage(PDF pdf)
339 {
340 int start_status;
341 int literal_mode;
342 str_number s;
343 int i = 0;
344 int max = colorstackused();
345 while (i < max) {
346 start_status = colorstackskippagestart(i);
347 if (start_status == 0) {
348 literal_mode = colorstackcurrent(i);
349 if (cur_length > 0) {
350 s = make_string();
351 pdf_literal(pdf, s, literal_mode, false);
352 flush_str(s);
353 }
354 }
355 i++;
356 }
357 }