Debian Codegraph @

texk/dvipdfm-x/spc_color.c

raw
1 /* This is dvipdfmx, an eXtended version of dvipdfm by Mark A. Wicks.
2
3 Copyright (C) 2002-2020 by Jin-Hwan Cho and Shunsaku Hirata,
4 the dvipdfmx project team.
5
6 Copyright (C) 1998, 1999 by Mark A. Wicks <[email protected]>
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
21 */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include "system.h"
28 #include "mem.h"
29 #include "error.h"
30 #include "dpxutil.h"
31
32 #include "pdfdev.h"
33 #include "pdfcolor.h"
34
35 #include "specials.h"
36
37 #include "spc_util.h"
38 #include "spc_color.h"
39
40
41 /* Color stack is actually placed into pdfcolor.c.
42 * The reason why we need to place stack there is
43 * that we must reinstall color after grestore and
44 * other operations that can change current color
45 * implicitely.
46 */
47
48 static int
49 spc_handler_color_push (struct spc_env *spe, struct spc_arg *args)
50 {
51 int error;
52 pdf_color colorspec;
53
54 error = spc_util_read_colorspec(spe, &colorspec, args, 1);
55 if (!error) {
56 pdf_color_push(&colorspec, &colorspec);
57 }
58
59 return error;
60 }
61
62 static int
63 spc_handler_color_pop (struct spc_env *spe, struct spc_arg *args)
64 {
65 pdf_color_pop();
66
67 return 0;
68 }
69
70 /* Invoked by the special command "color rgb .625 0 0".
71 * DVIPS clears the color stack, and then saves and sets the given color.
72 */
73 static int
74 spc_handler_color_default (struct spc_env *spe, struct spc_arg *args)
75 {
76 int error;
77 pdf_color colorspec;
78
79 error = spc_util_read_colorspec(spe, &colorspec, args, 1);
80 if (!error) {
81 pdf_color_clear_stack();
82 pdf_color_set(&colorspec, &colorspec);
83 }
84
85 return error;
86 }
87
88
89 /* This is from color special? */
90 #include "pdfdoc.h"
91
92 static int
93 spc_handler_background (struct spc_env *spe, struct spc_arg *args)
94 {
95 int error;
96 pdf_color colorspec;
97
98 error = spc_util_read_colorspec(spe, &colorspec, args, 1);
99 if (!error)
100 pdf_doc_set_bgcolor(&colorspec);
101
102 return error;
103 }
104
105
106 #ifndef ISBLANK
107 #define ISBLANK(c) ((c) == ' ' || (c) == '\t' || (c) == '\v')
108 #endif
109 static void
110 skip_blank (const char **pp, const char *endptr)
111 {
112 const char *p = *pp;
113 for ( ; p < endptr && ISBLANK(*p); p++);
114 *pp = p;
115 }
116
117 int
118 spc_color_check_special (const char *buf, int len)
119 {
120 int r = 0;
121 const char *p, *endptr;
122 char *q;
123
124 p = buf;
125 endptr = p + len;
126
127 skip_blank(&p, endptr);
128 q = parse_c_ident(&p, endptr);
129 if (!q)
130 return 0;
131 else if (!strcmp(q, "color"))
132 r = 1;
133 else if (!strcmp(q, "background")) {
134 r = 1;
135 }
136 RELEASE(q);
137
138 return r;
139 }
140
141 int
142 spc_color_setup_handler (struct spc_handler *sph,
143 struct spc_env *spe, struct spc_arg *ap)
144 {
145 const char *p;
146 char *q;
147
148 ASSERT(sph && spe && ap);
149
150 skip_blank(&ap->curptr, ap->endptr);
151 q = parse_c_ident(&ap->curptr, ap->endptr);
152 if (!q)
153 return -1;
154 skip_blank(&ap->curptr, ap->endptr);
155
156 if (!strcmp(q, "background")) {
157 ap->command = "background";
158 sph->exec = &spc_handler_background;
159 RELEASE(q);
160 } else if (!strcmp(q, "color")) { /* color */
161 RELEASE(q);
162 p = ap->curptr;
163
164 q = parse_c_ident(&p, ap->endptr);
165 if (!q)
166 return -1;
167 else if (!strcmp(q, "push")) {
168 ap->command = "push";
169 sph->exec = &spc_handler_color_push;
170 ap->curptr = p;
171 } else if (!strcmp(q, "pop")) {
172 ap->command = "pop";
173 sph->exec = &spc_handler_color_pop;
174 ap->curptr = p;
175 } else { /* cmyk, rgb, ... */
176 ap->command = "";
177 sph->exec = &spc_handler_color_default;
178 }
179 RELEASE(q);
180 } else {
181 spc_warn(spe, "Not color/background special?");
182 RELEASE(q);
183 return -1;
184 }
185
186 skip_blank(&ap->curptr, ap->endptr);
187 return 0;
188 }
189