3 This is dvipdfmx, an eXtended version of dvipdfm by Mark A. Wicks.
5 Copyright (C) 2002-2012 by Jin-Hwan Cho and Shunsaku Hirata,
6 the dvipdfmx project team.
8 Copyright (C) 1998, 1999 by Mark A. Wicks <mwicks@kettering.edu>
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
42 static const char *pst_const_null = "null";
43 static const char *pst_const_mark = "mark";
45 static const char *pst_const_true = "true";
46 static const char *pst_const_false = "false";
49 typedef char * pst_null;
50 typedef struct { char value; } pst_boolean;
51 typedef struct { long value; } pst_integer;
52 typedef struct { double value; } pst_real;
53 typedef struct { char *value; } pst_name;
62 static pst_boolean *pst_boolean_new (char value) ;
63 static void pst_boolean_release (pst_boolean *obj);
64 static long pst_boolean_IV (pst_boolean *obj);
65 static double pst_boolean_RV (pst_boolean *obj);
66 static unsigned char *pst_boolean_SV (pst_boolean *obj);
67 static long pst_boolean_length (pst_boolean *obj);
68 static void *pst_boolean_data_ptr(pst_boolean *obj);
71 static pst_integer *pst_integer_new (long value) ;
72 static void pst_integer_release (pst_integer *obj);
73 static long pst_integer_IV (pst_integer *obj);
74 static double pst_integer_RV (pst_integer *obj);
75 static unsigned char *pst_integer_SV (pst_integer *obj);
76 static unsigned int pst_integer_length (pst_integer *obj);
77 static void *pst_integer_data_ptr(pst_integer *obj);
79 static pst_real *pst_real_new (double value) ;
80 static void pst_real_release (pst_real *obj);
81 static long pst_real_IV (pst_real *obj);
82 static double pst_real_RV (pst_real *obj);
83 static unsigned char *pst_real_SV (pst_real *obj);
84 static void *pst_real_data_ptr (pst_real *obj);
85 static unsigned int pst_real_length (pst_real *obj);
88 static pst_name *pst_name_new (const char *name) ;
89 static void pst_name_release (pst_name *obj);
90 static long pst_name_IV (pst_name *obj);
91 static double pst_name_RV (pst_name *obj);
92 static unsigned char *pst_name_SV (pst_name *obj);
93 static void *pst_name_data_ptr (pst_name *obj);
94 static unsigned int pst_name_length (pst_name *obj);
97 static pst_string *pst_string_parse_literal (unsigned char **inbuf, unsigned char *inbufend);
98 static pst_string *pst_string_parse_hex (unsigned char **inbuf, unsigned char *inbufend);
100 static pst_string *pst_string_new (unsigned char *str, unsigned int len);
101 static void pst_string_release (pst_string *obj) ;
102 static long pst_string_IV (pst_string *obj) ;
103 static double pst_string_RV (pst_string *obj) ;
104 static unsigned char *pst_string_SV (pst_string *obj) ;
105 static void *pst_string_data_ptr (pst_string *obj) ;
106 static unsigned int pst_string_length (pst_string *obj) ;
109 #define TYPE_ERROR() ERROR("Operation not defined for this type of object.")
112 pst_new_obj (pst_type type, void *data)
116 obj = NEW(1, struct pst_obj);
128 q = NEW(strlen(pst_const_mark)+1, char);
129 strcpy(q, pst_const_mark);
130 return pst_new_obj(PST_TYPE_MARK, (void *)q);
134 pst_release_obj (pst_obj *obj)
138 case PST_TYPE_BOOLEAN: pst_boolean_release(obj->data); break;
139 case PST_TYPE_INTEGER: pst_integer_release(obj->data); break;
140 case PST_TYPE_REAL: pst_real_release(obj->data); break;
141 case PST_TYPE_NAME: pst_name_release(obj->data); break;
142 case PST_TYPE_STRING: pst_string_release(obj->data); break;
145 case PST_TYPE_UNKNOWN:
150 ERROR("Unrecognized object type: %d", obj->type);
156 pst_type_of (pst_obj *obj)
163 pst_length_of (pst_obj *obj)
169 case PST_TYPE_BOOLEAN: len = pst_boolean_length(obj->data); break;
170 case PST_TYPE_INTEGER: len = pst_integer_length(obj->data); break;
171 case PST_TYPE_REAL: len = pst_real_length(obj->data); break;
172 case PST_TYPE_NAME: len = pst_name_length(obj->data); break;
173 case PST_TYPE_STRING: len = pst_string_length(obj->data); break;
178 case PST_TYPE_UNKNOWN:
179 len = strlen(obj->data);
182 ERROR("Unrecognized object type: %d", obj->type);
189 pst_getIV (pst_obj *obj)
195 case PST_TYPE_BOOLEAN: iv = pst_boolean_IV(obj->data); break;
196 case PST_TYPE_INTEGER: iv = pst_integer_IV(obj->data); break;
197 case PST_TYPE_REAL: iv = pst_real_IV(obj->data); break;
198 case PST_TYPE_NAME: iv = pst_name_IV(obj->data); break;
199 case PST_TYPE_STRING: iv = pst_string_IV(obj->data); break;
204 case PST_TYPE_UNKNOWN:
205 ERROR("Cannot convert object of type UNKNOWN to integer value.");
208 ERROR("Unrecognized object type: %d", obj->type);
215 pst_getRV (pst_obj *obj)
221 case PST_TYPE_BOOLEAN: rv = pst_boolean_RV(obj->data); break;
222 case PST_TYPE_INTEGER: rv = pst_integer_RV(obj->data); break;
223 case PST_TYPE_REAL: rv = pst_real_RV(obj->data); break;
224 case PST_TYPE_NAME: rv = pst_name_RV(obj->data); break;
225 case PST_TYPE_STRING: rv = pst_string_RV(obj->data); break;
230 case PST_TYPE_UNKNOWN:
231 ERROR("Cannot convert object of type UNKNOWN to real value.");
234 ERROR("Unrecognized object type: %d", obj->type);
240 /* Length can be obtained by pst_length_of(). */
242 pst_getSV (pst_obj *obj)
244 unsigned char *sv = NULL;
248 case PST_TYPE_BOOLEAN: sv = pst_boolean_SV(obj->data); break;
249 case PST_TYPE_INTEGER: sv = pst_integer_SV(obj->data); break;
250 case PST_TYPE_REAL: sv = pst_real_SV(obj->data); break;
251 case PST_TYPE_NAME: sv = pst_name_SV(obj->data); break;
252 case PST_TYPE_STRING: sv = pst_string_SV(obj->data); break;
257 case PST_TYPE_UNKNOWN:
261 len = strlen((char *) obj->data);
263 sv = NEW(len+1, unsigned char);
264 memcpy(sv, obj->data, len);
272 ERROR("Unrecognized object type: %d", obj->type);
279 pst_data_ptr (pst_obj *obj)
285 case PST_TYPE_BOOLEAN: p = pst_boolean_data_ptr(obj->data); break;
286 case PST_TYPE_INTEGER: p = pst_integer_data_ptr(obj->data); break;
287 case PST_TYPE_REAL: p = pst_real_data_ptr(obj->data); break;
288 case PST_TYPE_NAME: p = pst_name_data_ptr(obj->data); break;
289 case PST_TYPE_STRING: p = pst_string_data_ptr(obj->data); break;
294 case PST_TYPE_UNKNOWN:
298 ERROR("Unrecognized object type: %d", obj->type);
306 pst_boolean_new (char value)
309 obj = NEW(1, pst_boolean);
315 pst_boolean_release (pst_boolean *obj)
322 pst_boolean_IV (pst_boolean *obj)
325 return (long) obj->value;
329 pst_boolean_RV (pst_boolean *obj)
332 return (double) obj->value;
335 static unsigned char *
336 pst_boolean_SV (pst_boolean *obj)
343 str = NEW(5, unsigned char);
344 memcpy(str, "true", 4);
347 str = NEW(6, unsigned char);
348 memcpy(str, "false", 5);
356 pst_boolean_length (pst_boolean *obj)
363 pst_boolean_data_ptr (pst_boolean *obj)
366 return (void*) &(obj->value);
370 pst_parse_boolean (unsigned char **inbuf, unsigned char *inbufend)
372 if (*inbuf + 4 <= inbufend &&
373 memcmp(*inbuf, "true", 4) == 0 &&
374 PST_TOKEN_END(*inbuf + 4, inbufend)) {
376 return pst_new_obj(PST_TYPE_BOOLEAN, pst_boolean_new(1));
377 } else if (*inbuf + 5 <= inbufend &&
378 memcmp(*inbuf, "false", 5) == 0 &&
379 PST_TOKEN_END(*inbuf + 5, inbufend)) {
381 return pst_new_obj(PST_TYPE_BOOLEAN, pst_boolean_new(0));
389 pst_parse_null (unsigned char **inbuf, unsigned char *inbufend)
391 if (*inbuf + 4 <= inbufend &&
392 memcmp(*inbuf, "null", 4) == 0 &&
393 PST_TOKEN_END(*inbuf+4, inbufend)) {
397 q = NEW(strlen(pst_const_null)+1, char);
398 strcpy(q, pst_const_null);
399 return pst_new_obj(PST_TYPE_NULL, (void*)q);
406 pst_integer_new (long value)
409 obj = NEW(1, pst_integer);
415 pst_integer_release (pst_integer *obj)
422 pst_integer_IV (pst_integer *obj)
425 return (long) obj->value;
429 pst_integer_RV (pst_integer *obj)
432 return (double) obj->value;
435 static unsigned char *
436 pst_integer_SV (pst_integer *obj)
440 char fmt_buf[PST_MAX_DIGITS+5];
444 len = sprintf(fmt_buf, "%ld", obj->value);
446 value = NEW(len, char);
447 strcpy(value, fmt_buf);
449 return (unsigned char *) value;
453 pst_integer_data_ptr (pst_integer *obj)
456 return (void*) &(obj->value);
460 pst_integer_length (pst_integer *obj)
468 pst_real_new (double value)
472 obj = NEW(1, pst_real);
479 pst_real_release (pst_real *obj)
486 pst_real_IV (pst_real *obj)
489 return (long) obj->value;
493 pst_real_RV (pst_real *obj)
496 return (double) obj->value;
499 static unsigned char *
500 pst_real_SV (pst_real *obj)
504 char fmt_buf[PST_MAX_DIGITS+5];
508 len = sprintf(fmt_buf, "%.5g", obj->value);
510 value = NEW(len, char);
511 strcpy(value, fmt_buf);
513 return (unsigned char *) value;
517 pst_real_data_ptr (pst_real *obj)
521 return (void*) &(obj->value);
525 pst_real_length (pst_real *obj)
531 /* NOTE: the input buffer must be null-terminated, i.e., *inbufend == 0 */
532 /* leading white-space is ignored */
534 pst_parse_number (unsigned char **inbuf, unsigned char *inbufend)
541 lval = strtol((char *) *inbuf, (char **) (void *) &cur, 10);
542 if (errno || *cur == '.' || *cur == 'e' || *cur == 'E') {
545 dval = strtod((char *) *inbuf, (char **) (void *) &cur);
546 if (!errno && PST_TOKEN_END(cur, inbufend)) {
548 return pst_new_obj(PST_TYPE_REAL, pst_real_new(dval));
550 } else if (cur != *inbuf && PST_TOKEN_END(cur, inbufend)) {
553 return pst_new_obj(PST_TYPE_INTEGER, pst_integer_new(lval));
554 } else if (lval >= 2 && lval <= 36 && *cur == '#' && isalnum(*++cur) &&
555 /* strtod allows leading "0x" for hex numbers, but we don't */
556 (lval != 16 || (cur[1] != 'x' && cur[1] != 'X'))) {
557 /* integer with radix */
558 /* Can the base have a (plus) sign? I think yes. */
560 lval = strtol((char *) cur, (char **) (void *) &cur, lval);
561 if (!errno && PST_TOKEN_END(cur, inbufend)) {
563 return pst_new_obj(PST_TYPE_INTEGER, pst_integer_new(lval));
573 * \0 is not allowed for name object.
577 pst_name_new (const char *name)
581 obj = NEW(1, pst_name);
582 obj->value = NEW(strlen(name)+1, char);
583 strcpy(obj->value, name);
589 pst_name_release (pst_name *obj)
599 pst_name_is_valid (const char *name)
601 static const char *valid_chars =
602 "!\"#$&'*+,-.0123456789:;=?@ABCDEFGHIJKLMNOPQRSTUVWXYZ\\^_`abcdefghijklmnopqrstuvwxyz|~";
603 if (strspn(name, valid_chars) == strlen(name))
610 pst_name_encode (const char *name)
612 char *encoded_name, *p;
617 if (len > PST_NAME_LEN_MAX) {
618 WARN("Input string too long for name object. String will be truncated.");
619 len = PST_NAME_LEN_MAX;
622 p = encoded_name = NEW(3*len+1, char);
623 for (i = 0; i < len; i++) {
625 if (c < '!' || c > '~' ||
626 c == '#' || is_delim(c) || is_space(c)) {
640 pst_parse_name (unsigned char **inbuf, unsigned char *inbufend) /* / is required */
642 unsigned char wbuf[PST_NAME_LEN_MAX+1];
643 unsigned char c, *p = wbuf, *cur = *inbuf;
650 while (!PST_TOKEN_END(cur, inbufend)) {
654 if (cur + 2 >= inbufend) {
655 WARN("Premature end of input name string.");
658 val = getxpair(&cur);
660 WARN("Invalid char for name object. (ignored)");
663 c = (unsigned char) val;
665 if (len < PST_NAME_LEN_MAX)
671 if (len > PST_NAME_LEN_MAX)
672 WARN("String too long for name object. Output will be truncated.");
675 return pst_new_obj(PST_TYPE_NAME, pst_name_new((char *)wbuf));
679 pst_name_IV (pst_name *obj)
686 pst_name_RV (pst_name *obj)
692 static unsigned char *
693 pst_name_SV (pst_name *obj)
697 value = NEW(strlen(obj->value)+1, char);
698 strcpy(value, obj->value);
700 return (unsigned char *) value;
704 pst_name_data_ptr (pst_name *obj)
711 pst_name_length (pst_name *obj)
714 return strlen(obj->value);
721 * TODO: ascii85 string <~ .... ~>
724 pst_string_new (unsigned char *str, unsigned int len)
727 obj = NEW(1, pst_string);
731 obj->value = NEW(len, unsigned char);
733 memcpy(obj->value, str, len);
739 pst_string_release (pst_string *obj)
748 pst_parse_string (unsigned char **inbuf, unsigned char *inbufend)
750 if (*inbuf + 2 >= inbufend) {
752 } else if (**inbuf == '(')
753 return pst_new_obj(PST_TYPE_STRING, pst_string_parse_literal(inbuf, inbufend));
754 else if (**inbuf == '<' && *(*inbuf+1) == '~')
755 ERROR("ASCII85 string not supported yet.");
756 else if (**inbuf == '<')
757 return pst_new_obj(PST_TYPE_STRING, pst_string_parse_hex(inbuf, inbufend));
762 pst_string_parse_literal (unsigned char **inbuf, unsigned char *inbufend)
764 unsigned char wbuf[PST_STRING_LEN_MAX];
765 unsigned char *cur = *inbuf, c = 0;
766 long len = 0, balance = 1;
768 if (cur + 2 > inbufend || *cur != '(')
772 while (cur < inbufend && len < PST_STRING_LEN_MAX && balance > 0) {
777 unsigned char unescaped, valid;
778 unescaped = esctouc(&cur, inbufend, &valid);
780 wbuf[len++] = unescaped;
793 * An end-of-line marker (\n, \r or \r\n), not preceeded by a backslash,
794 * must be converted to single \n.
797 if (cur < inbufend && *cur == '\n')
809 return pst_string_new(wbuf, len);
813 pst_string_parse_hex (unsigned char **inbuf, unsigned char *inbufend)
815 unsigned char wbuf[PST_STRING_LEN_MAX];
816 unsigned char *cur = *inbuf;
817 unsigned long len = 0;
819 if (cur + 2 > inbufend || *cur != '<' ||
820 (*cur == '<' && *(cur+1) == '<'))
824 /* PDF Reference does not specify how to treat invalid char */
825 while (cur < inbufend && len < PST_STRING_LEN_MAX) {
827 skip_white_spaces(&cur, inbufend);
832 WARN("Invalid char for hex string <%x> treated as <0>.", *(cur-1));
835 skip_white_spaces(&cur, inbufend);
838 /* 0 is appended if final hex digit is missing */
839 lo = (cur < inbufend) ? xtoi(*(cur++)) : 0;
841 WARN("Invalid char for hex string <%x> treated as <0>.", *(cur-1));
844 wbuf[len++] = (hi << 4) | lo;
850 return pst_string_new(wbuf, len);
854 pst_string_IV (pst_string *obj)
856 return (long) pst_string_RV(obj);
860 pst_string_RV (pst_string *obj)
863 unsigned char *p, *end;
868 end = p + obj->length;
869 nobj = pst_parse_number(&p, end);
870 if (nobj == NULL || p != end)
871 ERROR("Cound not convert string to real value.");
872 rv = pst_getRV(nobj);
873 pst_release_obj(nobj);
878 static unsigned char *
879 pst_string_SV (pst_string *obj)
881 unsigned char *str = NULL;
883 str = NEW(obj->length + 1, unsigned char);
884 memcpy(str, obj->value, obj->length);
885 str[obj->length] = '\0';
890 pst_string_data_ptr (pst_string *obj)
897 pst_string_length (pst_string *obj)