Debian Codegraph @

texk/dvipdfm-x/spc_util.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 "pdfdoc.h"
33 #include "pdfdev.h"
34 #include "pdfparse.h"
35 #include "pdfcolor.h"
36 #include "pdfdraw.h"
37 #include "pdfresource.h"
38
39 #include "specials.h"
40
41 #include "spc_util.h"
42
43
44 #ifndef ISBLANK
45 #define ISBLANK(c) ((c) == ' ' || (c) == '\t')
46 #endif
47 static void
48 skip_blank (const char **pp, const char *endptr)
49 {
50 const char *p = *pp;
51 for ( ; p < endptr && ISBLANK(*p); p++);
52 *pp = p;
53 }
54
55
56 /* From pdfcolor.c */
57 static int pdf_color_namedcolor (pdf_color *color, const char *colorname);
58
59 int
60 spc_util_read_numbers (double *values, int num_values, struct spc_arg *args)
61 {
62 int count;
63 char *q;
64
65 skip_blank(&args->curptr, args->endptr);
66 for (count = 0;
67 count < num_values &&
68 args->curptr < args->endptr; ) {
69 q = parse_float_decimal(&args->curptr, args->endptr);
70 if (!q)
71 break;
72 else {
73 values[count] = atof(q);
74 RELEASE(q);
75 skip_blank(&args->curptr, args->endptr);
76 count++;
77 }
78 }
79
80 return count;
81 }
82
83 static void
84 rgb_color_from_hsv (pdf_color *color, double h, double s, double v)
85 {
86 double r, g, b;
87 ASSERT( color );
88 r = g = b = v;
89 if (s != 0.0) {
90 double h6, f, v1, v2, v3;
91 int i;
92 h6 = h * 6; /* 360 / 60 */
93 i = (int) h6;
94 f = h6 - i;
95 v1 = v * (1 - s);
96 v2 = v * (1 - s * f);
97 v3 = v * (1 - s * (1 - f));
98 switch (i) {
99 case 0: r = v ; g = v3; b = v1; break;
100 case 1: r = v2; g = v ; b = v1; break;
101 case 2: r = v1; g = v ; b = v3; break;
102 case 3: r = v1; g = v2; b = v ; break;
103 case 4: r = v3; g = v1; b = v ; break;
104 case 5: r = v ; g = v1; b = v2; break;
105 case 6: r = v ; g = v1; b = v2; break;
106 }
107 }
108 pdf_color_rgbcolor(color, r, g, b);
109 }
110
111 static int
112 spc_read_color_color (struct spc_env *spe, pdf_color *colorspec, struct spc_arg *ap)
113 {
114 char *q;
115 double cv[4];
116 int nc;
117 int error = 0;
118
119 q = parse_c_ident(&ap->curptr, ap->endptr);
120 if (!q) {
121 spc_warn(spe, "No valid color specified?");
122 return -1;
123 }
124 skip_blank(&ap->curptr, ap->endptr);
125
126 if (!strcmp(q, "rgb")) { /* Handle rgb color */
127 nc = spc_util_read_numbers(cv, 3, ap);
128 if (nc != 3) {
129 spc_warn(spe, "Invalid value for RGB color specification.");
130 error = -1;
131 } else {
132 pdf_color_rgbcolor(colorspec, cv[0], cv[1], cv[2]);
133 }
134 } else if (!strcmp(q, "cmyk")) { /* Handle cmyk color */
135 nc = spc_util_read_numbers(cv, 4, ap);
136 if (nc != 4) {
137 spc_warn(spe, "Invalid value for CMYK color specification.");
138 error = -1;
139 } else {
140 pdf_color_cmykcolor(colorspec, cv[0], cv[1], cv[2], cv[3]);
141 }
142 } else if (!strcmp(q, "gray")) { /* Handle gray */
143 nc = spc_util_read_numbers(cv, 1, ap);
144 if (nc != 1) {
145 spc_warn(spe, "Invalid value for gray color specification.");
146 error = -1;
147 } else {
148 pdf_color_graycolor(colorspec, cv[0]);
149 }
150 } else if (!strcmp(q, "spot")) { /* Handle spot colors */
151 char *color_name = parse_c_ident(&ap->curptr, ap->endptr);
152 if (!color_name) {
153 spc_warn(spe, "No valid spot color name specified?");
154 return -1;
155 }
156 skip_blank(&ap->curptr, ap->endptr);
157 nc = spc_util_read_numbers(cv, 1, ap);
158 if (nc != 1) {
159 spc_warn(spe, "Invalid value for spot color specification.");
160 error = -1;
161 free(color_name);
162 } else {
163 pdf_color_spotcolor(colorspec, color_name, cv[0]);
164 }
165 } else if (!strcmp(q, "hsb")) {
166 nc = spc_util_read_numbers(cv, 3, ap);
167 if (nc != 3) {
168 spc_warn(spe, "Invalid value for HSB color specification.");
169 error = -1;
170 } else {
171 rgb_color_from_hsv(colorspec, cv[0], cv[1], cv[2]);
172 spc_warn(spe, "HSB color converted to RGB: hsb: <%g, %g, %g> ==> rgb: <%g, %g, %g>",
173 cv[0], cv[1], cv[2],
174 colorspec->values[0], colorspec->values[1], colorspec->values[2]);
175 }
176 } else { /* Must be a "named" color */
177 error = pdf_color_namedcolor(colorspec, q);
178 if (error)
179 spc_warn(spe, "Unrecognized color name: %s", q);
180 }
181 RELEASE(q);
182
183 return error;
184 }
185
186 /* Argument for this is PDF_Number or PDF_Array.
187 * But we ignore that since we don't want to add
188 * dependency to pdfxxx and @foo can not be
189 * allowed for color specification. "pdf" here
190 * means pdf: special syntax.
191 */
192 static int
193 spc_read_color_pdf (struct spc_env *spe, pdf_color *colorspec, struct spc_arg *ap)
194 {
195 double cv[PDF_COLOR_COMPONENT_MAX]; /* dvipdfmx limit */
196 int nc = -1, type = PDF_COLORSPACE_TYPE_INVALID, isarry = 0;
197 int error = 0;
198 char *q;
199
200 skip_blank(&ap->curptr, ap->endptr);
201 if (ap->curptr[0] == '@') {
202 char *ident = NULL;
203 int res_id = -1;
204 char *name = NULL;
205
206 ident = parse_opt_ident(&ap->curptr, ap->endptr);
207 skip_blank(&ap->curptr, ap->endptr);
208 res_id = pdf_findresource("ColorSpace", ident);
209 {
210 pdf_obj *cspace, *csname;
211
212 cspace = spc_lookup_object(ident);
213 if (!cspace || !PDF_OBJ_ARRAYTYPE(cspace)) {
214 WARN("Couldn't find ColorSpace resource for \"@%s\"... (or not an array object?)", ident);
215 RELEASE(ident);
216 return -1;
217 }
218 csname = pdf_get_array(cspace, 0);
219 if (!csname || !PDF_OBJ_NAMETYPE(csname)) {
220 WARN("Invalid ColorSpace resource for \"@%s\" found...", ident);
221 RELEASE(ident);
222 return -1;
223 }
224
225 name = pdf_name_value(csname);
226 if (!strcmp(name, "Separation")) {
227 type = PDF_COLORSPACE_TYPE_SEPARATION;
228 } else if (!strcmp(name, "CalGray")) {
229 type = PDF_COLORSPACE_TYPE_CALGRAY;
230 nc = 1;
231 } else if (!strcmp(name, "CalRGB")) {
232 type = PDF_COLORSPACE_TYPE_CALRGB;
233 nc = 3;
234 } else if (!strcmp(name, "LAB")) {
235 type = PDF_COLORSPACE_TYPE_LAB;
236 nc = 3;
237 } else if (!strcmp(name, "ICCBased")) {
238 type = PDF_COLORSPACE_TYPE_ICCBASED;
239 nc = -1;
240 } else if (!strcmp(name, "DeviceN")) {
241 type = PDF_COLORSPACE_TYPE_DEVICEN;
242 nc = -1;
243 } else if (!strcmp(name, "Indexed")) {
244 type = PDF_COLORSPACE_TYPE_INDEXED;
245 nc = 1;
246 } else if (!strcmp(name, "Pattern")) {
247 type = PDF_COLORSPACE_TYPE_PATTERN;
248 nc = -1;
249 } else {
250 WARN("Specified object \"%s\" not a ColorSpace???", ident);
251 return -1;
252 }
253 if (res_id < 0) {
254 res_id = pdf_defineresource("ColorSpace", ident, pdf_link_obj(cspace), 0);
255 }
256 RELEASE(ident);
257 }
258
259 colorspec->res_id = res_id;
260 colorspec->type = type;
261 if (ap->curptr[0] == '[') {
262 ap->curptr++; skip_blank(&ap->curptr, ap->endptr);
263 isarry = 1;
264 }
265 if (nc > 0) {
266 int n;
267 n = spc_util_read_numbers(cv, nc, ap);
268 if (n != nc) {
269 WARN("Wrong number of color components...");
270 return -1;
271 }
272 } else {
273 nc = spc_util_read_numbers(cv, PDF_COLOR_COMPONENT_MAX, ap);
274 }
275 colorspec->num_components = nc;
276 while (nc-- > 0) {
277 colorspec->values[nc] = cv[nc];
278 }
279 if (type == PDF_COLORSPACE_TYPE_PATTERN) {
280 /* reference appears */
281 skip_blank(&ap->curptr, ap->endptr);
282 if (ap->curptr[0] != '@') {
283 WARN("An object reference expected but not found for Pattern!");
284 return -1;
285 }
286 ident = parse_opt_ident(&ap->curptr, ap->endptr);
287 res_id = pdf_findresource("Pattern", ident);
288 if (res_id < 0) {
289 pdf_obj *pattern = spc_lookup_object(ident);
290 /* Skip checking. /Type entry is optional... */
291 res_id = pdf_defineresource("Pattern", ident, pdf_link_obj(pattern), 0);
292 }
293 colorspec->pattern_id = res_id;
294 RELEASE(ident);
295 }
296 if (isarry) {
297 skip_blank(&ap->curptr, ap->endptr);
298 if (ap->curptr >= ap->endptr || ap->curptr[0] != ']') {
299 spc_warn(spe, "Unbalanced '[' and ']' or wrong number of color components in color specification?");
300 error = -1;
301 } else {
302 ap->curptr++;
303 }
304 }
305 } else if (ap->curptr[0] == '/') { /* /DeviceGray... */
306 pdf_obj *csname;
307 csname = parse_pdf_name(&ap->curptr, ap->endptr);
308 if (!csname) {
309 WARN("Failed to read a name object while parsing colorspecification...");
310 return -1;
311 } else {
312 char *name = pdf_name_value(csname);
313 if (!strcmp(name, "DeviceGray")) {
314 type = PDF_COLORSPACE_TYPE_DEVICEGRAY;
315 nc = 1;
316 } else if (!strcmp(name, "DeviceRGB")) {
317 type = PDF_COLORSPACE_TYPE_DEVICERGB;
318 nc = 3;
319 } else if (!strcmp(name, "DeviceCMYK")) {
320 type = PDF_COLORSPACE_TYPE_DEVICECMYK;
321 nc = 4;
322 } else if (!strcmp(name, "Pattern")) {
323 type = PDF_COLORSPACE_TYPE_PATTERN;
324 nc = 0;
325 } else {
326 WARN("Unknown ColorSpace name specified: %s", name);
327 return -1;
328 }
329 pdf_release_obj(csname);
330 skip_blank(&ap->curptr, ap->endptr);
331 }
332 colorspec->res_id = -1;
333 colorspec->type = type;
334 if (ap->curptr[0] == '[') {
335 ap->curptr++; skip_blank(&ap->curptr, ap->endptr);
336 isarry = 1;
337 }
338 {
339 int n = spc_util_read_numbers(cv, nc, ap);
340 if (n != nc) {
341 WARN("Wrong number of color components for this ColorSpace...");
342 return -1;
343 }
344 }
345 colorspec->num_components = nc;
346 while (nc-- > 0) {
347 colorspec->values[nc] = cv[nc];
348 }
349 if (type == PDF_COLORSPACE_TYPE_PATTERN) {
350 char *ident = NULL;
351 int res_id = -1;
352 /* reference appears */
353 skip_blank(&ap->curptr, ap->endptr);
354 if (ap->curptr[0] != '@') {
355 WARN("An object reference expected but not found for Pattern!");
356 return -1;
357 }
358 ident = parse_opt_ident(&ap->curptr, ap->endptr);
359 res_id = pdf_findresource("Pattern", ident);
360 if (res_id < 0) {
361 pdf_obj *pattern = spc_lookup_object(ident);
362 /* Skip checking. /Type entry is optional... */
363 res_id = pdf_defineresource("Pattern", ident, pdf_link_obj(pattern), 0);
364 }
365 colorspec->pattern_id = res_id;
366 RELEASE(ident);
367 }
368 if (isarry) {
369 skip_blank(&ap->curptr, ap->endptr);
370 if (ap->curptr >= ap->endptr || ap->curptr[0] != ']') {
371 spc_warn(spe, "Unbalanced '[' and ']' or wrong number of color components in color specification?");
372 error = -1;
373 } else {
374 ap->curptr++;
375 }
376 }
377 } else {
378 if (ap->curptr[0] == '[') {
379 ap->curptr++; skip_blank(&ap->curptr, ap->endptr);
380 isarry = 1;
381 }
382 nc = spc_util_read_numbers(cv, 4, ap);
383 switch (nc) {
384 case 1:
385 pdf_color_graycolor(colorspec, cv[0]);
386 break;
387 case 3:
388 pdf_color_rgbcolor (colorspec, cv[0], cv[1], cv[2]);
389 break;
390 case 4:
391 pdf_color_cmykcolor(colorspec, cv[0], cv[1], cv[2], cv[3]);
392 break;
393 default:
394 /* Try to read the color names defined in dvipsname.def */
395 q = parse_c_ident(&ap->curptr, ap->endptr);
396 if (!q) {
397 spc_warn(spe, "No valid color specified?");
398 return -1;
399 }
400 error = pdf_color_namedcolor(colorspec, q);
401 if (error)
402 spc_warn(spe, "Unrecognized color name: %s, keep the current color", q);
403 RELEASE(q);
404 break;
405 }
406 if (isarry) {
407 skip_blank(&ap->curptr, ap->endptr);
408 if (ap->curptr >= ap->endptr || ap->curptr[0] != ']') {
409 spc_warn(spe, "Unbalanced '[' and ']' in color specification.");
410 error = -1;
411 } else {
412 ap->curptr++;
413 }
414 }
415 }
416
417 return error;
418 }
419
420 /* This is for reading *single* color specification. */
421 int
422 spc_util_read_colorspec (struct spc_env *spe, pdf_color *colorspec, struct spc_arg *ap, int syntax)
423 {
424 ASSERT(colorspec && spe && ap);
425
426 skip_blank(&ap->curptr, ap->endptr);
427 if (ap->curptr >= ap->endptr) {
428 return -1;
429 }
430
431 pdf_color_black(colorspec); /* As initialization... */
432 if (syntax)
433 return spc_read_color_color(spe, colorspec, ap);
434 else
435 return spc_read_color_pdf(spe, colorspec, ap);
436 }
437
438 int
439 spc_util_read_pdfcolor (struct spc_env *spe, pdf_color *colorspec, struct spc_arg *ap, pdf_color *defaultcolor)
440 {
441 int error = 0;
442
443 ASSERT(colorspec && spe && ap);
444
445 skip_blank(&ap->curptr, ap->endptr);
446 if (ap->curptr >= ap->endptr) {
447 return -1;
448 }
449 error = spc_read_color_pdf(spe, colorspec, ap);
450 if (error < 0 && defaultcolor) {
451 pdf_color_copycolor(colorspec, defaultcolor);
452 error = 0;
453 }
454 return error;
455 }
456
457 /* This need to allow 'true' prefix for unit and
458 * length value must be divided by current magnification.
459 */
460 static int
461 spc_util_read_length (struct spc_env *spe, double *vp /* ret. */, struct spc_arg *ap)
462 {
463 char *q;
464 double v, u = 1.0;
465 const char *ukeys[] = {
466 #define K_UNIT__PT 0
467 #define K_UNIT__IN 1
468 #define K_UNIT__CM 2
469 #define K_UNIT__MM 3
470 #define K_UNIT__BP 4
471 #define K_UNIT__PC 5
472 #define K_UNIT__DD 6
473 #define K_UNIT__CC 7
474 #define K_UNIT__SP 8
475 "pt", "in", "cm", "mm", "bp", "pc", "dd", "cc", "sp", NULL
476 };
477 int k, error = 0;
478
479 q = parse_float_decimal(&ap->curptr, ap->endptr);
480 if (!q)
481 return -1;
482
483 v = atof(q);
484 RELEASE(q);
485
486 skip_white(&ap->curptr, ap->endptr);
487 q = parse_c_ident(&ap->curptr, ap->endptr);
488 if (q) {
489 char *qq = q;
490 if (strlen(q) >= strlen("true") &&
491 !memcmp(q, "true", strlen("true"))) {
492 u /= spe->mag != 0.0 ? spe->mag : 1.0; /* inverse magnify */
493 q += strlen("true");
494
495 if (!*q) {
496 RELEASE(qq);
497 skip_white(&ap->curptr, ap->endptr);
498 qq = q = parse_c_ident(&ap->curptr, ap->endptr);
499 }
500 }
501
502 if (q) {
503 for (k = 0; ukeys[k] && strcmp(ukeys[k], q); k++);
504 switch (k) {
505 case K_UNIT__PT: u *= 72.0 / 72.27; break;
506 case K_UNIT__IN: u *= 72.0; break;
507 case K_UNIT__CM: u *= 72.0 / 2.54 ; break;
508 case K_UNIT__MM: u *= 72.0 / 25.4 ; break;
509 case K_UNIT__BP: u *= 1.0 ; break;
510 case K_UNIT__PC: u *= 12.0 * 72.0 / 72.27 ; break;
511 case K_UNIT__DD: u *= 1238.0 / 1157.0 * 72.0 / 72.27 ; break;
512 case K_UNIT__CC: u *= 12.0 * 1238.0 / 1157.0 * 72.0 / 72.27 ; break;
513 case K_UNIT__SP: u *= 72.0 / (72.27 * 65536) ; break;
514 default:
515 spc_warn(spe, "Unknown unit of measure: %s", q);
516 error = -1;
517 break;
518 }
519 RELEASE(qq);
520 }
521 else {
522 spc_warn(spe, "Missing unit of measure after \"true\"");
523 error = -1;
524 }
525 }
526
527 *vp = v * u;
528 return error;
529 }
530
531
532 /*
533 * Compute a transformation matrix
534 * transformations are applied in the following
535 * order: scaling, rotate, displacement.
536 */
537 static void
538 make_transmatrix (pdf_tmatrix *M,
539 double xoffset, double yoffset,
540 double xscale, double yscale,
541 double rotate)
542 {
543 double c, s;
544
545 c = cos(rotate);
546 s = sin(rotate);
547
548 M->a = xscale * c; M->b = xscale * s;
549 M->c = -yscale * s; M->d = yscale * c;
550 M->e = xoffset; M->f = yoffset;
551 }
552
553 static int
554 spc_read_dimtrns_dvips (struct spc_env *spe,
555 transform_info *t, struct spc_arg *ap)
556 {
557 static const char *_dtkeys[] = {
558 #define K_TRN__HOFFSET 0
559 #define K_TRN__VOFFSET 1
560 "hoffset", "voffset",
561 #define K_DIM__HSIZE 2
562 #define K_DIM__VSIZE 3
563 "hsize", "vsize",
564 #define K_TRN__HSCALE 4
565 #define K_TRN__VSCALE 5
566 "hscale", "vscale",
567 #define K_TRN__ANGLE 6
568 "angle",
569 #define K__CLIP 7
570 "clip",
571 #define K_DIM__LLX 8
572 #define K_DIM__LLY 9
573 #define K_DIM__URX 10
574 #define K_DIM__URY 11
575 "llx", "lly", "urx", "ury",
576 #define K_DIM__RWI 12
577 #define K_DIM__RHI 13
578 "rwi", "rhi",
579 NULL
580 };
581 double xoffset, yoffset, xscale, yscale, rotate;
582 int error = 0;
583
584 xoffset = yoffset = rotate = 0.0; xscale = yscale = 1.0;
585
586 skip_blank(&ap->curptr, ap->endptr);
587 while (!error && ap->curptr < ap->endptr) {
588 char *kp, *vp;
589 int k;
590
591 kp = parse_c_ident(&ap->curptr, ap->endptr);
592 if (!kp)
593 break;
594
595 for (k = 0; _dtkeys[k] && strcmp(kp, _dtkeys[k]); k++);
596 if (!_dtkeys[k]) {
597 spc_warn(spe, "Unrecognized dimension/transformation key: %s", kp);
598 error = -1;
599 RELEASE(kp);
600 break;
601 }
602
603 skip_blank(&ap->curptr, ap->endptr);
604 if (k == K__CLIP) {
605 t->flags |= INFO_DO_CLIP;
606 RELEASE(kp);
607 continue; /* not key-value */
608 }
609
610 if (ap->curptr < ap->endptr && ap->curptr[0] == '=') {
611 ap->curptr++;
612 skip_blank(&ap->curptr, ap->endptr);
613 }
614
615 vp = NULL;
616 if (ap->curptr[0] == '\'' || ap->curptr[0] == '\"') {
617 char qchr = ap->curptr[0];
618 ap->curptr++;
619 skip_blank(&ap->curptr, ap->endptr);
620 vp = parse_float_decimal(&ap->curptr, ap->endptr);
621 skip_blank(&ap->curptr, ap->endptr);
622 if (vp && qchr != ap->curptr[0]) {
623 spc_warn(spe, "Syntax error in dimension/transformation specification.");
624 error = -1;
625 RELEASE(vp); vp = NULL;
626 }
627 ap->curptr++;
628 } else {
629 vp = parse_float_decimal(&ap->curptr, ap->endptr);
630 }
631 if (!error && !vp) {
632 spc_warn(spe, "Missing value for dimension/transformation: %s", kp);
633 error = -1;
634 }
635 RELEASE(kp);
636 if (!vp || error) {
637 break;
638 }
639
640 switch (k) {
641 case K_TRN__HOFFSET:
642 xoffset = atof(vp);
643 break;
644 case K_TRN__VOFFSET:
645 yoffset = atof(vp);
646 break;
647 case K_DIM__HSIZE:
648 t->width = atof(vp);
649 t->flags |= INFO_HAS_WIDTH;
650 break;
651 case K_DIM__VSIZE:
652 t->height = atof(vp);
653 t->flags |= INFO_HAS_HEIGHT;
654 break;
655 case K_TRN__HSCALE:
656 xscale = atof(vp) / 100.0;
657 break;
658 case K_TRN__VSCALE:
659 yscale = atof(vp) / 100.0;
660 break;
661 case K_TRN__ANGLE:
662 rotate = M_PI * atof(vp) / 180.0;
663 break;
664 case K_DIM__LLX:
665 t->bbox.llx = atof(vp);
666 t->flags |= INFO_HAS_USER_BBOX;
667 break;
668 case K_DIM__LLY:
669 t->bbox.lly = atof(vp);
670 t->flags |= INFO_HAS_USER_BBOX;
671 break;
672 case K_DIM__URX:
673 t->bbox.urx = atof(vp);
674 t->flags |= INFO_HAS_USER_BBOX;
675 break;
676 case K_DIM__URY:
677 t->bbox.ury = atof(vp);
678 t->flags |= INFO_HAS_USER_BBOX;
679 break;
680 case K_DIM__RWI:
681 t->width = atof(vp) / 10.0;
682 t->flags |= INFO_HAS_WIDTH;
683 break;
684 case K_DIM__RHI:
685 t->height = atof(vp) / 10.0;
686 t->flags |= INFO_HAS_HEIGHT;
687 break;
688 }
689 skip_blank(&ap->curptr, ap->endptr);
690 RELEASE(vp);
691 }
692 make_transmatrix(&(t->matrix), xoffset, yoffset, xscale, yscale, rotate);
693
694 return error;
695 }
696
697 /* "page" and "pagebox" are not dimension nor transformation nor
698 * something acceptable to put into here.
699 * PLEASE DONT ADD HERE!
700 */
701 static int
702 spc_read_dimtrns_pdfm (struct spc_env *spe,
703 transform_info *p, struct spc_arg *ap)
704 {
705 int has_scale, has_xscale, has_yscale, has_rotate, has_matrix;
706 const char *_dtkeys[] = {
707 #define K_DIM__WIDTH 0
708 #define K_DIM__HEIGHT 1
709 #define K_DIM__DEPTH 2
710 "width", "height", "depth",
711 #define K_TRN__SCALE 3
712 #define K_TRN__XSCALE 4
713 #define K_TRN__YSCALE 5
714 #define K_TRN__ROTATE 6
715 "scale", "xscale", "yscale", "rotate", /* See "Dvipdfmx User's Manual", p.5 */
716 #define K_TRN__BBOX 7
717 "bbox", /* See "Dvipdfmx User's Manual", p.5 */
718 #define K_TRN__MATRIX 8
719 "matrix",
720 #undef K__CLIP
721 #define K__CLIP 9
722 "clip",
723 #define K__HIDE 10
724 "hide",
725 NULL
726 };
727 double xscale, yscale, rotate;
728 int error = 0;
729
730 has_xscale = has_yscale = has_scale = has_rotate = has_matrix = 0;
731 xscale = yscale = 1.0; rotate = 0.0;
732 p->flags |= INFO_DO_CLIP; /* default: do clipping */
733 p->flags &= ~INFO_DO_HIDE; /* default: do clipping */
734
735 skip_blank(&ap->curptr, ap->endptr);
736
737 while (!error && ap->curptr < ap->endptr) {
738 char *kp, *vp;
739 int k;
740
741 kp = parse_c_ident(&ap->curptr, ap->endptr);
742 if (!kp)
743 break;
744
745 skip_blank(&ap->curptr, ap->endptr);
746 for (k = 0; _dtkeys[k] && strcmp(_dtkeys[k], kp); k++);
747 switch (k) {
748 case K_DIM__WIDTH:
749 error = spc_util_read_length(spe, &p->width , ap);
750 p->flags |= INFO_HAS_WIDTH;
751 break;
752 case K_DIM__HEIGHT:
753 error = spc_util_read_length(spe, &p->height, ap);
754 p->flags |= INFO_HAS_HEIGHT;
755 break;
756 case K_DIM__DEPTH:
757 error = spc_util_read_length(spe, &p->depth , ap);
758 p->flags |= INFO_HAS_HEIGHT;
759 break;
760 case K_TRN__SCALE:
761 vp = parse_float_decimal(&ap->curptr, ap->endptr);
762 if (!vp)
763 error = -1;
764 else {
765 xscale = yscale = atof(vp);
766 has_scale = 1;
767 RELEASE(vp);
768 }
769 break;
770 case K_TRN__XSCALE:
771 vp = parse_float_decimal(&ap->curptr, ap->endptr);
772 if (!vp)
773 error = -1;
774 else {
775 xscale = atof(vp);
776 has_xscale = 1;
777 RELEASE(vp);
778 }
779 break;
780 case K_TRN__YSCALE:
781 vp = parse_float_decimal(&ap->curptr, ap->endptr);
782 if (!vp)
783 error = -1;
784 else {
785 yscale = atof(vp);
786 has_yscale = 1;
787 RELEASE(vp);
788 }
789 break;
790 case K_TRN__ROTATE:
791 vp = parse_float_decimal(&ap->curptr, ap->endptr);
792 if (!vp)
793 error = -1;
794 else {
795 rotate = M_PI * atof(vp) / 180.0;
796 has_rotate = 1;
797 RELEASE(vp);
798 }
799 break;
800 case K_TRN__BBOX:
801 {
802 double v[4];
803 if (spc_util_read_numbers(v, 4, ap) != 4)
804 error = -1;
805 else {
806 p->bbox.llx = v[0];
807 p->bbox.lly = v[1];
808 p->bbox.urx = v[2];
809 p->bbox.ury = v[3];
810 p->flags |= INFO_HAS_USER_BBOX;
811 }
812 }
813 break;
814 case K_TRN__MATRIX:
815 {
816 double v[6];
817 if (spc_util_read_numbers(v, 6, ap) != 6)
818 error = -1;
819 else {
820 pdf_setmatrix(&(p->matrix), v[0], v[1], v[2], v[3], v[4], v[5]);
821 has_matrix = 1;
822 }
823 }
824 break;
825 case K__CLIP:
826 vp = parse_float_decimal(&ap->curptr, ap->endptr);
827 if (!vp)
828 error = -1;
829 else {
830 if (atof(vp))
831 p->flags |= INFO_DO_CLIP;
832 else
833 p->flags &= ~INFO_DO_CLIP;
834 RELEASE(vp);
835 }
836 break;
837 case K__HIDE:
838 p->flags |= INFO_DO_HIDE;
839 break;
840
841 default:
842 error = -1;
843 break;
844 }
845 if (error)
846 spc_warn(spe, "Unrecognized key or invalid value for " \
847 "dimension/transformation: %s", kp);
848 else
849 skip_blank(&ap->curptr, ap->endptr);
850 RELEASE(kp);
851 }
852
853 if (!error) {
854 /* Check consistency */
855 if (has_xscale && (p->flags & INFO_HAS_WIDTH)) {
856 spc_warn(spe, "Can't supply both width and xscale. Ignore xscale.");
857 xscale = 1.0;
858 } else if (has_yscale &&
859 (p->flags & INFO_HAS_HEIGHT)) {
860 spc_warn(spe, "Can't supply both height/depth and yscale. Ignore yscale.");
861 yscale = 1.0;
862 } else if (has_scale &&
863 (has_xscale || has_yscale)) {
864 spc_warn(spe, "Can't supply overall scale along with axis scales.");
865 error = -1;
866 } else if (has_matrix &&
867 (has_scale || has_xscale || has_yscale || has_rotate)) {
868 spc_warn(spe, "Can't supply transform matrix along with scales or rotate. Ignore scales and rotate.");
869 }
870 }
871
872 if (!has_matrix) {
873 make_transmatrix(&(p->matrix), 0.0, 0.0, xscale, yscale, rotate);
874 }
875
876 if (!(p->flags & INFO_HAS_USER_BBOX)) {
877 p->flags &= ~INFO_DO_CLIP; /* no clipping needed */
878 }
879
880 return error;
881 }
882
883 int
884 spc_util_read_dimtrns (struct spc_env *spe,
885 transform_info *ti, struct spc_arg *args, int syntax)
886 {
887 if (!ti || !spe || !args)
888 return -1;
889
890 if (syntax) {
891 return spc_read_dimtrns_dvips(spe, ti, args);
892 } else {
893 return spc_read_dimtrns_pdfm (spe, ti, args);
894 }
895 }
896
897 int
898 spc_util_read_blahblah (struct spc_env *spe,
899 transform_info *p, int *page_no,
900 enum pdf_page_boundary *bbox_type,
901 char **page_name,
902 struct spc_arg *ap)
903 {
904 int has_scale, has_xscale, has_yscale, has_rotate, has_matrix;
905 const char *_dtkeys[] = {
906 "width", "height", "depth",
907 "scale", "xscale", "yscale", "rotate",
908 "bbox",
909 "matrix",
910 "clip",
911 "hide",
912 #define K__PAGE 11
913 "page",
914 #define K__PAGEBOX 12
915 "pagebox",
916 #define K__NAMED 13
917 "named",
918 NULL
919 };
920 double xscale, yscale, rotate;
921 int error = 0;
922
923 has_xscale = has_yscale = has_scale = has_rotate = has_matrix = 0;
924 xscale = yscale = 1.0; rotate = 0.0;
925 p->flags |= INFO_DO_CLIP; /* default: do clipping */
926 p->flags &= ~INFO_DO_HIDE; /* default: do clipping */
927
928 skip_blank(&ap->curptr, ap->endptr);
929
930 while (!error && ap->curptr < ap->endptr) {
931 char *kp, *vp;
932 int k;
933
934 kp = parse_c_ident(&ap->curptr, ap->endptr);
935 if (!kp)
936 break;
937
938 skip_blank(&ap->curptr, ap->endptr);
939 for (k = 0; _dtkeys[k] && strcmp(_dtkeys[k], kp); k++);
940 switch (k) {
941 case K_DIM__WIDTH:
942 error = spc_util_read_length(spe, &p->width , ap);
943 p->flags |= INFO_HAS_WIDTH;
944 break;
945 case K_DIM__HEIGHT:
946 error = spc_util_read_length(spe, &p->height, ap);
947 p->flags |= INFO_HAS_HEIGHT;
948 break;
949 case K_DIM__DEPTH:
950 error = spc_util_read_length(spe, &p->depth , ap);
951 p->flags |= INFO_HAS_HEIGHT;
952 break;
953 case K_TRN__SCALE:
954 vp = parse_float_decimal(&ap->curptr, ap->endptr);
955 if (!vp)
956 error = -1;
957 else {
958 xscale = yscale = atof(vp);
959 has_scale = 1;
960 RELEASE(vp);
961 }
962 break;
963 case K_TRN__XSCALE:
964 vp = parse_float_decimal(&ap->curptr, ap->endptr);
965 if (!vp)
966 error = -1;
967 else {
968 xscale = atof(vp);
969 has_xscale = 1;
970 RELEASE(vp);
971 }
972 break;
973 case K_TRN__YSCALE:
974 vp = parse_float_decimal(&ap->curptr, ap->endptr);
975 if (!vp)
976 error = -1;
977 else {
978 yscale = atof(vp);
979 has_yscale = 1;
980 RELEASE(vp);
981 }
982 break;
983 case K_TRN__ROTATE:
984 vp = parse_float_decimal(&ap->curptr, ap->endptr);
985 if (!vp)
986 error = -1;
987 else {
988 rotate = M_PI * atof(vp) / 180.0;
989 has_rotate = 1;
990 RELEASE(vp);
991 }
992 break;
993 case K_TRN__BBOX:
994 {
995 double v[4];
996 if (spc_util_read_numbers(v, 4, ap) != 4)
997 error = -1;
998 else {
999 p->bbox.llx = v[0];
1000 p->bbox.lly = v[1];
1001 p->bbox.urx = v[2];
1002 p->bbox.ury = v[3];
1003 p->flags |= INFO_HAS_USER_BBOX;
1004 }
1005 }
1006 break;
1007 case K_TRN__MATRIX:
1008 {
1009 double v[6];
1010 if (spc_util_read_numbers(v, 6, ap) != 6)
1011 error = -1;
1012 else {
1013 pdf_setmatrix(&(p->matrix), v[0], v[1], v[2], v[3], v[4], v[5]);
1014 has_matrix = 1;
1015 }
1016 }
1017 break;
1018 case K__CLIP:
1019 vp = parse_float_decimal(&ap->curptr, ap->endptr);
1020 if (!vp)
1021 error = -1;
1022 else {
1023 if (atof(vp))
1024 p->flags |= INFO_DO_CLIP;
1025 else
1026 p->flags &= ~INFO_DO_CLIP;
1027 RELEASE(vp);
1028 }
1029 break;
1030
1031 case K__PAGE:
1032 {
1033 double page;
1034 if (page_no && spc_util_read_numbers(&page, 1, ap) == 1)
1035 *page_no = (int) page;
1036 else
1037 error = -1;
1038 }
1039 break;
1040 case K__HIDE:
1041 p->flags |= INFO_DO_HIDE;
1042 break;
1043 case K__PAGEBOX:
1044 {
1045 char *q;
1046 q = parse_c_ident (&ap->curptr, ap->endptr);
1047 if (q) {
1048 if (bbox_type) {
1049 if (strcasecmp(q, "cropbox") == 0) *bbox_type = pdf_page_boundary_cropbox;
1050 else if (strcasecmp(q, "mediabox") == 0) *bbox_type = pdf_page_boundary_mediabox;
1051 else if (strcasecmp(q, "artbox") == 0) *bbox_type = pdf_page_boundary_artbox;
1052 else if (strcasecmp(q, "trimbox") == 0) *bbox_type = pdf_page_boundary_trimbox;
1053 else if (strcasecmp(q, "bleedbox") == 0) *bbox_type = pdf_page_boundary_bleedbox;
1054 }
1055 RELEASE(q);
1056 } else if (bbox_type) {
1057 *bbox_type = pdf_page_boundary__auto;
1058 }
1059 }
1060 break;
1061 case K__NAMED:
1062 {
1063 *page_name = parse_c_string (&ap->curptr, ap->endptr);
1064 }
1065 break;
1066
1067 default:
1068 error = -1;
1069 break;
1070 }
1071 if (error)
1072 spc_warn(spe, "Unrecognized key or invalid value for " \
1073 "dimension/transformation: %s", kp);
1074 else
1075 skip_blank(&ap->curptr, ap->endptr);
1076 RELEASE(kp);
1077 }
1078
1079 if (!error) {
1080 /* Check consistency */
1081 if (has_xscale && (p->flags & INFO_HAS_WIDTH)) {
1082 spc_warn(spe, "Can't supply both width and xscale. Ignore xscale.");
1083 xscale = 1.0;
1084 } else if (has_yscale &&
1085 (p->flags & INFO_HAS_HEIGHT)) {
1086 spc_warn(spe, "Can't supply both height/depth and yscale. Ignore yscale.");
1087 yscale = 1.0;
1088 } else if (has_scale &&
1089 (has_xscale || has_yscale)) {
1090 spc_warn(spe, "Can't supply overall scale along with axis scales.");
1091 error = -1;
1092 } else if (has_matrix &&
1093 (has_scale || has_xscale || has_yscale || has_rotate)) {
1094 spc_warn(spe, "Can't supply transform matrix along with scales or rotate. Ignore scales and rotate.");
1095 }
1096 }
1097
1098 if (!has_matrix) {
1099 make_transmatrix(&(p->matrix), 0.0, 0.0, xscale, yscale, rotate);
1100 }
1101
1102 if (!(p->flags & INFO_HAS_USER_BBOX)) {
1103 p->flags &= ~INFO_DO_CLIP; /* no clipping needed */
1104 }
1105
1106 return error;
1107 }
1108
1109 /* Color names */
1110 #ifdef rgb
1111 #undef rgb
1112 #endif
1113 #ifdef cmyk
1114 #undef cmyk
1115 #endif
1116 #define gray(g) {-1, PDF_COLORSPACE_TYPE_GRAY, 1, NULL, {g}, -1}
1117 #define rgb8(r,g,b) {-1, PDF_COLORSPACE_TYPE_RGB, 3, NULL, {((r)/255.0), ((g)/255.0), ((b)/255.0), 0.0}, -1}
1118 #define cmyk(c,m,y,k) {-1, PDF_COLORSPACE_TYPE_CMYK, 4, NULL, {(c), (m), (y), (k)}, -1}
1119
1120 static struct colordef_
1121 {
1122 const char *key;
1123 pdf_color color;
1124 } colordefs[] = {
1125 {"GreenYellow", cmyk(0.15, 0.00, 0.69, 0.00)},
1126 {"Yellow", cmyk(0.00, 0.00, 1.00, 0.00)},
1127 {"Goldenrod", cmyk(0.00, 0.10, 0.84, 0.00)},
1128 {"Dandelion", cmyk(0.00, 0.29, 0.84, 0.00)},
1129 {"Apricot", cmyk(0.00, 0.32, 0.52, 0.00)},
1130 {"Peach", cmyk(0.00, 0.50, 0.70, 0.00)},
1131 {"Melon", cmyk(0.00, 0.46, 0.50, 0.00)},
1132 {"YellowOrange", cmyk(0.00, 0.42, 1.00, 0.00)},
1133 {"Orange", cmyk(0.00, 0.61, 0.87, 0.00)},
1134 {"BurntOrange", cmyk(0.00, 0.51, 1.00, 0.00)},
1135 {"Bittersweet", cmyk(0.00, 0.75, 1.00, 0.24)},
1136 {"RedOrange", cmyk(0.00, 0.77, 0.87, 0.00)},
1137 {"Mahogany", cmyk(0.00, 0.85, 0.87, 0.35)},
1138 {"Maroon", cmyk(0.00, 0.87, 0.68, 0.32)},
1139 {"BrickRed", cmyk(0.00, 0.89, 0.94, 0.28)},
1140 {"Red", cmyk(0.00, 1.00, 1.00, 0.00)},
1141 {"OrangeRed", cmyk(0.00, 1.00, 0.50, 0.00)},
1142 {"RubineRed", cmyk(0.00, 1.00, 0.13, 0.00)},
1143 {"WildStrawberry", cmyk(0.00, 0.96, 0.39, 0.00)},
1144 {"Salmon", cmyk(0.00, 0.53, 0.38, 0.00)},
1145 {"CarnationPink", cmyk(0.00, 0.63, 0.00, 0.00)},
1146 {"Magenta", cmyk(0.00, 1.00, 0.00, 0.00)},
1147 {"VioletRed", cmyk(0.00, 0.81, 0.00, 0.00)},
1148 {"Rhodamine", cmyk(0.00, 0.82, 0.00, 0.00)},
1149 {"Mulberry", cmyk(0.34, 0.90, 0.00, 0.02)},
1150 {"RedViolet", cmyk(0.07, 0.90, 0.00, 0.34)},
1151 {"Fuchsia", cmyk(0.47, 0.91, 0.00, 0.08)},
1152 {"Lavender", cmyk(0.00, 0.48, 0.00, 0.00)},
1153 {"Thistle", cmyk(0.12, 0.59, 0.00, 0.00)},
1154 {"Orchid", cmyk(0.32, 0.64, 0.00, 0.00)},
1155 {"DarkOrchid", cmyk(0.40, 0.80, 0.20, 0.00)},
1156 {"Purple", cmyk(0.45, 0.86, 0.00, 0.00)},
1157 {"Plum", cmyk(0.50, 1.00, 0.00, 0.00)},
1158 {"Violet", cmyk(0.79, 0.88, 0.00, 0.00)},
1159 {"RoyalPurple", cmyk(0.75, 0.90, 0.00, 0.00)},
1160 {"BlueViolet", cmyk(0.86, 0.91, 0.00, 0.04)},
1161 {"Periwinkle", cmyk(0.57, 0.55, 0.00, 0.00)},
1162 {"CadetBlue", cmyk(0.62, 0.57, 0.23, 0.00)},
1163 {"CornflowerBlue", cmyk(0.65, 0.13, 0.00, 0.00)},
1164 {"MidnightBlue", cmyk(0.98, 0.13, 0.00, 0.43)},
1165 {"NavyBlue", cmyk(0.94, 0.54, 0.00, 0.00)},
1166 {"RoyalBlue", cmyk(1.00, 0.50, 0.00, 0.00)},
1167 {"Blue", cmyk(1.00, 1.00, 0.00, 0.00)},
1168 {"Cerulean", cmyk(0.94, 0.11, 0.00, 0.00)},
1169 {"Cyan", cmyk(1.00, 0.00, 0.00, 0.00)},
1170 {"ProcessBlue", cmyk(0.96, 0.00, 0.00, 0.00)},
1171 {"SkyBlue", cmyk(0.62, 0.00, 0.12, 0.00)},
1172 {"Turquoise", cmyk(0.85, 0.00, 0.20, 0.00)},
1173 {"TealBlue", cmyk(0.86, 0.00, 0.34, 0.02)},
1174 {"Aquamarine", cmyk(0.82, 0.00, 0.30, 0.00)},
1175 {"BlueGreen", cmyk(0.85, 0.00, 0.33, 0.00)},
1176 {"Emerald", cmyk(1.00, 0.00, 0.50, 0.00)},
1177 {"JungleGreen", cmyk(0.99, 0.00, 0.52, 0.00)},
1178 {"SeaGreen", cmyk(0.69, 0.00, 0.50, 0.00)},
1179 {"Green", cmyk(1.00, 0.00, 1.00, 0.00)},
1180 {"ForestGreen", cmyk(0.91, 0.00, 0.88, 0.12)},
1181 {"PineGreen", cmyk(0.92, 0.00, 0.59, 0.25)},
1182 {"LimeGreen", cmyk(0.50, 0.00, 1.00, 0.00)},
1183 {"YellowGreen", cmyk(0.44, 0.00, 0.74, 0.00)},
1184 {"SpringGreen", cmyk(0.26, 0.00, 0.76, 0.00)},
1185 {"OliveGreen", cmyk(0.64, 0.00, 0.95, 0.40)},
1186 {"RawSienna", cmyk(0.00, 0.72, 1.00, 0.45)},
1187 {"Sepia", cmyk(0.00, 0.83, 1.00, 0.70)},
1188 {"Brown", cmyk(0.00, 0.81, 1.00, 0.60)},
1189 {"Tan", cmyk(0.14, 0.42, 0.56, 0.00)},
1190 /* Adobe Reader 7 and 8 had problem when gray and cmyk black colors
1191 * are mixed. No problem with Previewer.app.
1192 * It happens when \usepackage[dvipdfm]{graphicx} and then called
1193 * \usepackage{color} without dvipdfm option. */
1194 {"Gray", gray(0.5)},
1195 {"Black", gray(0.0)},
1196 {"White", gray(1.0)},
1197 {NULL}
1198 };
1199
1200
1201 static int
1202 pdf_color_namedcolor (pdf_color *color, const char *name)
1203 {
1204 int i;
1205 for (i = 0; colordefs[i].key; i++) {
1206 if (!strcmp(colordefs[i].key, name)) {
1207 pdf_color_copycolor(color, &colordefs[i].color);
1208 return 0;
1209 }
1210 }
1211 return -1;
1212 }