gdevpsfm.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /* Copyright (C) 2000 Aladdin Enterprises. All rights reserved.
  2. This file is part of AFPL Ghostscript.
  3. AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND. No author or
  4. distributor accepts any responsibility for the consequences of using it, or
  5. for whether it serves any particular purpose or works at all, unless he or
  6. she says so in writing. Refer to the Aladdin Free Public License (the
  7. "License") for full details.
  8. Every copy of AFPL Ghostscript must include a copy of the License, normally
  9. in a plain ASCII text file named PUBLIC. The License grants you the right
  10. to copy, modify and redistribute AFPL Ghostscript, but only under certain
  11. conditions described in the License. Among other things, the License
  12. requires that the copyright notice and this notice be preserved on all
  13. copies.
  14. */
  15. /*$Id: gdevpsfm.c,v 1.5 2001/04/01 00:35:26 raph Exp $ */
  16. /* Write a CMap */
  17. #include "gx.h"
  18. #include "gserrors.h"
  19. #include "gxfcmap.h"
  20. #include "stream.h"
  21. #include "spprint.h"
  22. #include "spsdf.h"
  23. #include "gdevpsf.h"
  24. /* ---------------- Utilities ---------------- */
  25. typedef struct cmap_operators_s {
  26. const char *beginchar;
  27. const char *endchar;
  28. const char *beginrange;
  29. const char *endrange;
  30. } cmap_operators_t;
  31. private const cmap_operators_t
  32. cmap_cid_operators = {
  33. "begincidchar\n", "endcidchar\n",
  34. "begincidrange\n", "endcidrange\n"
  35. },
  36. cmap_notdef_operators = {
  37. "beginnotdefchar\n", "endnotdefchar\n",
  38. "beginnotdefrange\n", "endnotdefrange\n"
  39. };
  40. /* Write a gs_string with a prefix. */
  41. private void
  42. pput_string_entry(stream *s, const char *prefix, const gs_const_string *pstr)
  43. {
  44. stream_puts(s, prefix);
  45. stream_write(s, pstr->data, pstr->size);
  46. }
  47. /* Write a hex string. */
  48. private void
  49. pput_hex(stream *s, const byte *pcid, int size)
  50. {
  51. int i;
  52. static const char *const hex_digits = "0123456789abcdef";
  53. for (i = 0; i < size; ++i) {
  54. stream_putc(s, hex_digits[pcid[i] >> 4]);
  55. stream_putc(s, hex_digits[pcid[i] & 0xf]);
  56. }
  57. }
  58. /* Write one CIDSystemInfo dictionary. */
  59. private void
  60. cmap_put_system_info(stream *s, const gs_cid_system_info_t *pcidsi)
  61. {
  62. if (cid_system_info_is_null(pcidsi)) {
  63. stream_puts(s, " null ");
  64. } else {
  65. stream_puts(s, " 3 dict dup begin\n");
  66. stream_puts(s, "/Registry ");
  67. s_write_ps_string(s, pcidsi->Registry.data, pcidsi->Registry.size, 0);
  68. stream_puts(s, " def\n/Ordering ");
  69. s_write_ps_string(s, pcidsi->Ordering.data, pcidsi->Ordering.size, 0);
  70. pprintd1(s, " def\n/Supplement %d def\nend ", pcidsi->Supplement);
  71. }
  72. }
  73. /* Write one code map. */
  74. private int
  75. cmap_put_code_map(stream *s, const gx_code_map_t *pccmap,
  76. const gs_cmap_t *pcmap, const cmap_operators_t *pcmo,
  77. psf_put_name_chars_proc_t put_name_chars, int *pfont_index)
  78. {
  79. /* For simplicity, produce one entry for each lookup range. */
  80. const gx_code_lookup_range_t *pclr = pccmap->lookup;
  81. int nl = pccmap->num_lookup;
  82. for (; nl > 0; ++pclr, --nl) {
  83. const byte *pkey = pclr->keys.data;
  84. const byte *pvalue = pclr->values.data;
  85. int gi;
  86. if (pclr->font_index != *pfont_index) {
  87. pprintd1(s, "%d usefont\n", pclr->font_index);
  88. *pfont_index = pclr->font_index;
  89. }
  90. for (gi = 0; gi < pclr->num_keys; gi += 100) {
  91. int i = gi, ni = min(i + 100, pclr->num_keys);
  92. const char *end;
  93. pprintd1(s, "%d ", ni - i);
  94. if (pclr->key_is_range) {
  95. if (pclr->value_type == CODE_VALUE_CID) {
  96. stream_puts(s, pcmo->beginrange);
  97. end = pcmo->endrange;
  98. } else { /* must be def, not notdef */
  99. stream_puts(s, "beginbfrange\n");
  100. end = "endbfrange\n";
  101. }
  102. } else {
  103. if (pclr->value_type == CODE_VALUE_CID) {
  104. stream_puts(s, pcmo->beginchar);
  105. end = pcmo->endchar;
  106. } else { /* must be def, not notdef */
  107. stream_puts(s, "beginbfchar\n");
  108. end = "endbfchar\n";
  109. }
  110. }
  111. for (; i < ni; ++i) {
  112. int j;
  113. long value;
  114. for (j = 0; j <= pclr->key_is_range; ++j) {
  115. stream_putc(s, '<');
  116. pput_hex(s, pclr->key_prefix, pclr->key_prefix_size);
  117. pput_hex(s, pkey, pclr->key_size);
  118. stream_putc(s, '>');
  119. pkey += pclr->key_size;
  120. }
  121. for (j = 0, value = 0; j < pclr->value_size; ++j)
  122. value = (value << 8) + *pvalue++;
  123. switch (pclr->value_type) {
  124. case CODE_VALUE_CID:
  125. pprintld1(s, "%ld", value);
  126. break;
  127. case CODE_VALUE_CHARS:
  128. stream_putc(s, '<');
  129. pput_hex(s, pvalue - pclr->value_size, pclr->value_size);
  130. stream_putc(s, '>');
  131. break;
  132. case CODE_VALUE_GLYPH: {
  133. gs_const_string str;
  134. int code = pcmap->glyph_name((gs_glyph)value, &str,
  135. pcmap->glyph_name_data);
  136. if (code < 0)
  137. return code;
  138. stream_putc(s, '/');
  139. code = put_name_chars(s, str.data, str.size);
  140. if (code < 0)
  141. return code;
  142. }
  143. break;
  144. default: /* not possible */
  145. return_error(gs_error_rangecheck);
  146. }
  147. stream_putc(s, '\n');
  148. }
  149. stream_puts(s, end);
  150. }
  151. }
  152. return 0;
  153. }
  154. /* ---------------- Main program ---------------- */
  155. /* Write a CMap in its standard (source) format. */
  156. int
  157. psf_write_cmap(stream *s, const gs_cmap_t *pcmap,
  158. psf_put_name_chars_proc_t put_name_chars,
  159. const gs_const_string *alt_cmap_name)
  160. {
  161. const gs_const_string *const cmap_name =
  162. (alt_cmap_name ? alt_cmap_name : &pcmap->CMapName);
  163. const gs_cid_system_info_t *const pcidsi = pcmap->CIDSystemInfo;
  164. switch (pcmap->CMapType) {
  165. case 0: case 1:
  166. break;
  167. default:
  168. return_error(gs_error_rangecheck);
  169. }
  170. /* Write the header. */
  171. stream_puts(s, "%!PS-Adobe-3.0 Resource-CMap\n");
  172. stream_puts(s, "%%DocumentNeededResources: ProcSet (CIDInit)\n");
  173. stream_puts(s, "%%IncludeResource: ProcSet (CIDInit)\n");
  174. pput_string_entry(s, "%%BeginResource: CMap (", cmap_name);
  175. pput_string_entry(s, ")\n%%Title: (", cmap_name);
  176. pput_string_entry(s, " ", &pcidsi->Registry);
  177. pput_string_entry(s, " ", &pcidsi->Ordering);
  178. pprintd1(s, " %d)\n", pcidsi->Supplement);
  179. pprintg1(s, "%%%%Version: %g\n", pcmap->CMapVersion);
  180. stream_puts(s, "/CIDInit /ProcSet findresource begin\n");
  181. stream_puts(s, "12 dict begin\nbegincmap\n");
  182. /* Write the fixed entries. */
  183. pprintd1(s, "/CMapType %d def\n", pcmap->CMapType);
  184. stream_puts(s, "/CMapName/");
  185. put_name_chars(s, cmap_name->data, cmap_name->size);
  186. stream_puts(s, " def\n/CIDSystemInfo");
  187. if (pcmap->num_fonts == 1) {
  188. cmap_put_system_info(s, pcidsi);
  189. } else {
  190. int i;
  191. pprintd1(s, " %d array\n", pcmap->num_fonts);
  192. for (i = 0; i < pcmap->num_fonts; ++i) {
  193. pprintd1(s, "dup %d", i);
  194. cmap_put_system_info(s, pcidsi + i);
  195. stream_puts(s, "put\n");
  196. }
  197. }
  198. pprintg1(s, "def\n/CMapVersion %g def\n", pcmap->CMapVersion);
  199. if (uid_is_XUID(&pcmap->uid)) {
  200. uint i, n = uid_XUID_size(&pcmap->uid);
  201. const long *values = uid_XUID_values(&pcmap->uid);
  202. stream_puts(s, "/XUID [");
  203. for (i = 0; i < n; ++i)
  204. pprintld1(s, " %ld", values[i]);
  205. stream_puts(s, "] def\n");
  206. }
  207. pprintld1(s, "/UIDOffset %ld def\n", pcmap->UIDOffset);
  208. pprintd1(s, "/WMode %d def\n", pcmap->WMode);
  209. /* Write the code space ranges. */
  210. {
  211. const gx_code_space_range_t *pcsr = pcmap->code_space.ranges;
  212. int gi;
  213. for (gi = 0; gi < pcmap->code_space.num_ranges; gi += 100) {
  214. int i = gi, ni = min(i + 100, pcmap->code_space.num_ranges);
  215. pprintd1(s, "%d begincodespacerange\n", ni - i);
  216. for (; i < ni; ++i, ++pcsr) {
  217. stream_puts(s, "<");
  218. pput_hex(s, pcsr->first, pcsr->size);
  219. stream_puts(s, "><");
  220. pput_hex(s, pcsr->last, pcsr->size);
  221. stream_puts(s, ">\n");
  222. }
  223. stream_puts(s, "endcodespacerange\n");
  224. }
  225. }
  226. /* Write the code and notdef data. */
  227. {
  228. int font_index = (pcmap->num_fonts <= 1 ? 0 : -1);
  229. cmap_put_code_map(s, &pcmap->notdef, pcmap, &cmap_notdef_operators,
  230. put_name_chars, &font_index);
  231. cmap_put_code_map(s, &pcmap->def, pcmap, &cmap_cid_operators,
  232. put_name_chars, &font_index);
  233. }
  234. /* Write the trailer. */
  235. stream_puts(s, "endcmap\n");
  236. stream_puts(s, "CMapName currentdict /CMap defineresource pop\nend end\n");
  237. stream_puts(s, "%%EndResource\n");
  238. stream_puts(s, "%%EOF\n");
  239. return 0;
  240. }