spsdf.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /* Copyright (C) 1999, 2000 Aladdin Enterprises. All rights reserved.
  2. This software is provided AS-IS with no warranty, either express or
  3. implied.
  4. This software is distributed under license and may not be copied,
  5. modified or distributed except as expressly authorized under the terms
  6. of the license contained in the file LICENSE in this distribution.
  7. For more information about licensing, please refer to
  8. http://www.ghostscript.com/licensing/. For information on
  9. commercial licensing, go to http://www.artifex.com/licensing/ or
  10. contact Artifex Software, Inc., 101 Lucas Valley Road #110,
  11. San Rafael, CA 94903, U.S.A., +1(415)492-9861.
  12. */
  13. /* $Id: spsdf.c,v 1.7 2002/02/21 22:24:54 giles Exp $ */
  14. /* Common utilities for PostScript and PDF format printing */
  15. #include "stdio_.h" /* for stream.h */
  16. #include "string_.h"
  17. #include "gstypes.h"
  18. #include "gsmemory.h"
  19. #include "gserror.h"
  20. #include "gserrors.h"
  21. #include "spprint.h"
  22. #include "spsdf.h"
  23. #include "stream.h"
  24. #include "strimpl.h"
  25. #include "sa85x.h"
  26. #include "sstring.h"
  27. #include "scanchar.h"
  28. /*
  29. * Write a string in its shortest form ( () or <> ). Note that
  30. * this form is different depending on whether binary data are allowed.
  31. * Currently we don't support ASCII85 strings ( <~ ~> ).
  32. */
  33. void
  34. s_write_ps_string(stream * s, const byte * str, uint size, int print_ok)
  35. {
  36. uint added = 0;
  37. uint i;
  38. const stream_template *template;
  39. stream_AXE_state state;
  40. stream_state *st = NULL;
  41. if (print_ok & PRINT_BINARY_OK) {
  42. /* Only need to escape (, ), \, CR, EOL. */
  43. stream_putc(s, '(');
  44. for (i = 0; i < size; ++i) {
  45. byte ch = str[i];
  46. switch (ch) {
  47. case char_CR:
  48. stream_puts(s, "\\r");
  49. continue;
  50. case char_EOL:
  51. stream_puts(s, "\\n");
  52. continue;
  53. case '(':
  54. case ')':
  55. case '\\':
  56. stream_putc(s, '\\');
  57. }
  58. stream_putc(s, ch);
  59. }
  60. stream_putc(s, ')');
  61. return;
  62. }
  63. for (i = 0; i < size; ++i) {
  64. byte ch = str[i];
  65. if (ch == 0 || ch >= 127)
  66. added += 3;
  67. else if (strchr("()\\\n\r\t\b\f", ch) != 0)
  68. ++added;
  69. else if (ch < 32)
  70. added += 3;
  71. }
  72. if (added < size || (print_ok & PRINT_HEX_NOT_OK)) {
  73. /* More efficient, or mandatory, to represent as PostScript string. */
  74. template = &s_PSSE_template;
  75. stream_putc(s, '(');
  76. } else {
  77. /* More efficient, and permitted, to represent as hex string. */
  78. template = &s_AXE_template;
  79. st = (stream_state *) & state;
  80. s_AXE_init_inline(&state);
  81. stream_putc(s, '<');
  82. }
  83. {
  84. byte buf[100]; /* size is arbitrary */
  85. stream_cursor_read r;
  86. stream_cursor_write w;
  87. int status;
  88. r.ptr = str - 1;
  89. r.limit = r.ptr + size;
  90. w.limit = buf + sizeof(buf) - 1;
  91. do {
  92. /* One picky compiler complains if we initialize to buf - 1. */
  93. w.ptr = buf; w.ptr--;
  94. status = (*template->process) (st, &r, &w, true);
  95. stream_write(s, buf, (uint) (w.ptr + 1 - buf));
  96. }
  97. while (status == 1);
  98. }
  99. }
  100. /* Set up a write stream that just keeps track of the position. */
  101. int
  102. s_alloc_position_stream(stream ** ps, gs_memory_t * mem)
  103. {
  104. stream *s = *ps = s_alloc(mem, "s_alloc_position_stream");
  105. if (s == 0)
  106. return_error(gs_error_VMerror);
  107. swrite_position_only(s);
  108. return 0;
  109. }
  110. /* ---------------- Parameter printing ---------------- */
  111. private_st_printer_param_list();
  112. const param_printer_params_t param_printer_params_default = {
  113. param_printer_params_default_values
  114. };
  115. /* We'll implement the other printers later if we have to. */
  116. private param_proc_xmit_typed(param_print_typed);
  117. /*private param_proc_begin_xmit_collection(param_print_begin_collection); */
  118. /*private param_proc_end_xmit_collection(param_print_end_collection); */
  119. private const gs_param_list_procs printer_param_list_procs = {
  120. param_print_typed,
  121. NULL /* begin_collection */ ,
  122. NULL /* end_collection */ ,
  123. NULL /* get_next_key */ ,
  124. gs_param_request_default,
  125. gs_param_requested_default
  126. };
  127. int
  128. s_init_param_printer(printer_param_list_t *prlist,
  129. const param_printer_params_t * ppp, stream * s)
  130. {
  131. gs_param_list_init((gs_param_list *)prlist, &printer_param_list_procs,
  132. NULL);
  133. prlist->strm = s;
  134. prlist->params = *ppp;
  135. prlist->any = false;
  136. return 0;
  137. }
  138. int
  139. s_alloc_param_printer(gs_param_list ** pplist,
  140. const param_printer_params_t * ppp, stream * s,
  141. gs_memory_t * mem)
  142. {
  143. printer_param_list_t *prlist =
  144. gs_alloc_struct(mem, printer_param_list_t, &st_printer_param_list,
  145. "s_alloc_param_printer");
  146. int code;
  147. *pplist = (gs_param_list *)prlist;
  148. if (prlist == 0)
  149. return_error(gs_error_VMerror);
  150. code = s_init_param_printer(prlist, ppp, s);
  151. prlist->memory = mem;
  152. return code;
  153. }
  154. void
  155. s_release_param_printer(printer_param_list_t *prlist)
  156. {
  157. if (prlist) {
  158. if (prlist->any && prlist->params.suffix)
  159. stream_puts(prlist->strm, prlist->params.suffix);
  160. }
  161. }
  162. void
  163. s_free_param_printer(gs_param_list * plist)
  164. {
  165. if (plist) {
  166. printer_param_list_t *const prlist = (printer_param_list_t *) plist;
  167. s_release_param_printer(prlist);
  168. gs_free_object(prlist->memory, plist, "s_free_param_printer");
  169. }
  170. }
  171. private int
  172. param_print_typed(gs_param_list * plist, gs_param_name pkey,
  173. gs_param_typed_value * pvalue)
  174. {
  175. printer_param_list_t *const prlist = (printer_param_list_t *)plist;
  176. stream *s = prlist->strm;
  177. if (!prlist->any) {
  178. if (prlist->params.prefix)
  179. stream_puts(s, prlist->params.prefix);
  180. prlist->any = true;
  181. }
  182. if (prlist->params.item_prefix)
  183. stream_puts(s, prlist->params.item_prefix);
  184. pprints1(s, "/%s", pkey);
  185. switch (pvalue->type) {
  186. case gs_param_type_null:
  187. stream_puts(s, " null");
  188. break;
  189. case gs_param_type_bool:
  190. stream_puts(s, (pvalue->value.b ? " true" : " false"));
  191. break;
  192. case gs_param_type_int:
  193. pprintd1(s, " %d", pvalue->value.i);
  194. break;
  195. case gs_param_type_long:
  196. pprintld1(s, " %l", pvalue->value.l);
  197. break;
  198. case gs_param_type_float:
  199. pprintg1(s, " %g", pvalue->value.f);
  200. break;
  201. case gs_param_type_string:
  202. s_write_ps_string(s, pvalue->value.s.data, pvalue->value.s.size,
  203. prlist->params.print_ok);
  204. break;
  205. case gs_param_type_name:
  206. /****** SHOULD USE #-ESCAPES FOR PDF ******/
  207. stream_putc(s, '/');
  208. stream_write(s, pvalue->value.n.data, pvalue->value.n.size);
  209. break;
  210. case gs_param_type_int_array:
  211. {
  212. uint i;
  213. char sepr = (pvalue->value.ia.size <= 10 ? ' ' : '\n');
  214. stream_putc(s, '[');
  215. for (i = 0; i < pvalue->value.ia.size; ++i) {
  216. pprintd1(s, "%d", pvalue->value.ia.data[i]);
  217. stream_putc(s, sepr);
  218. }
  219. stream_putc(s, ']');
  220. }
  221. break;
  222. case gs_param_type_float_array:
  223. {
  224. uint i;
  225. char sepr = (pvalue->value.fa.size <= 10 ? ' ' : '\n');
  226. stream_putc(s, '[');
  227. for (i = 0; i < pvalue->value.fa.size; ++i) {
  228. pprintg1(s, "%g", pvalue->value.fa.data[i]);
  229. stream_putc(s, sepr);
  230. }
  231. stream_putc(s, ']');
  232. }
  233. break;
  234. /*case gs_param_type_string_array: */
  235. /*case gs_param_type_name_array: */
  236. default:
  237. return_error(gs_error_typecheck);
  238. }
  239. if (prlist->params.item_suffix)
  240. stream_puts(s, prlist->params.item_suffix);
  241. return 0;
  242. }