1 /* Copyright (C) 2001-2006 Artifex Software, Inc.
2    All Rights Reserved.
3 
4    This software is provided AS-IS with no warranty, either express or
5    implied.
6 
7    This software is distributed under license and may not be copied, modified
8    or distributed except as expressly authorized under the terms of that
9    license.  Refer to licensing information at http://www.artifex.com/
10    or contact Artifex Software, Inc.,  7 Mt. Lassen Drive - Suite A-134,
11    San Rafael, CA  94903, U.S.A., +1(415)492-9861, for further information.
12 */
13 
14 /* $Id: gdevpsfm.c 9607 2009-03-31 14:30:14Z ken $ */
15 /* Write a CMap */
16 #include "gx.h"
17 #include "gserrors.h"
18 #include "gxfcmap.h"
19 #include "stream.h"
20 #include "spprint.h"
21 #include "spsdf.h"
22 #include "gdevpsf.h"
23 
24 /* ---------------- Utilities ---------------- */
25 
26 typedef struct cmap_operators_s {
27     const char *beginchar;
28     const char *endchar;
29     const char *beginrange;
30     const char *endrange;
31 } cmap_operators_t;
32 static const cmap_operators_t
33   cmap_cid_operators = {
34     "begincidchar\n", "endcidchar\n",
35     "begincidrange\n", "endcidrange\n"
36   },
37   cmap_notdef_operators = {
38     "beginnotdefchar\n", "endnotdefchar\n",
39     "beginnotdefrange\n", "endnotdefrange\n"
40   };
41 
42 /* Write a gs_string with a prefix. */
43 static void
pput_string_entry(stream * s,const char * prefix,const gs_const_string * pstr)44 pput_string_entry(stream *s, const char *prefix, const gs_const_string *pstr)
45 {
46     stream_puts(s, prefix);
47     stream_write(s, pstr->data, pstr->size);
48 }
49 
50 /* Write a hex string. */
51 static void
pput_hex(stream * s,const byte * pcid,int size)52 pput_hex(stream *s, const byte *pcid, int size)
53 {
54     int i;
55     static const char *const hex_digits = "0123456789abcdef";
56 
57     for (i = 0; i < size; ++i) {
58 	stream_putc(s, hex_digits[pcid[i] >> 4]);
59 	stream_putc(s, hex_digits[pcid[i] & 0xf]);
60     }
61 }
62 
63 /* Write a list of code space ranges. */
64 static void
cmap_put_ranges(stream * s,const gx_code_space_range_t * pcsr,int count)65 cmap_put_ranges(stream *s, const gx_code_space_range_t *pcsr, int count)
66 {
67     int i;
68 
69     pprintd1(s, "%d begincodespacerange\n", count);
70     for (i = 0; i < count; ++i, ++pcsr) {
71 	stream_puts(s, "<");
72 	pput_hex(s, pcsr->first, pcsr->size);
73 	stream_puts(s, "><");
74 	pput_hex(s, pcsr->last, pcsr->size);
75 	stream_puts(s, ">\n");
76     }
77     stream_puts(s, "endcodespacerange\n");
78 }
79 
80 /* Write one CIDSystemInfo dictionary. */
81 static void
cmap_put_system_info(stream * s,const gs_cid_system_info_t * pcidsi)82 cmap_put_system_info(stream *s, const gs_cid_system_info_t *pcidsi)
83 {
84     if (cid_system_info_is_null(pcidsi)) {
85 	stream_puts(s, " null ");
86     } else {
87 	stream_puts(s, " 3 dict dup begin\n");
88 	stream_puts(s, "/Registry ");
89 	s_write_ps_string(s, pcidsi->Registry.data, pcidsi->Registry.size, 0);
90 	stream_puts(s, " def\n/Ordering ");
91 	s_write_ps_string(s, pcidsi->Ordering.data, pcidsi->Ordering.size, 0);
92 	pprintd1(s, " def\n/Supplement %d def\nend ", pcidsi->Supplement);
93     }
94 }
95 
96 /* Write one code map. */
97 static int
cmap_put_code_map(const gs_memory_t * mem,stream * s,int which,const gs_cmap_t * pcmap,const cmap_operators_t * pcmo,psf_put_name_chars_proc_t put_name_chars,int font_index_only)98 cmap_put_code_map(const gs_memory_t *mem,
99 		  stream *s, int which, const gs_cmap_t *pcmap,
100 		  const cmap_operators_t *pcmo,
101 		  psf_put_name_chars_proc_t put_name_chars,
102 		  int font_index_only)
103 {
104     /* For simplicity, produce one entry for each lookup range. */
105     gs_cmap_lookups_enum_t lenum;
106     int font_index = (pcmap->num_fonts <= 1 ? 0 : -1);
107     int code;
108 
109     for (gs_cmap_lookups_enum_init(pcmap, which, &lenum);
110 	 (code = gs_cmap_enum_next_lookup(&lenum)) == 0; ) {
111 	gs_cmap_lookups_enum_t counter;
112 	int num_entries = 0;
113 	int gi;
114 
115 	if (font_index_only >= 0 && lenum.entry.font_index != font_index_only)
116 	    continue;
117 	if (font_index_only < 0 && lenum.entry.font_index != font_index) {
118 	    pprintd1(s, "%d usefont\n", lenum.entry.font_index);
119 	    font_index = lenum.entry.font_index;
120 	}
121 	/* Count the number of entries in this lookup range. */
122 	counter = lenum;
123 	while (gs_cmap_enum_next_entry(&counter) == 0)
124 	    ++num_entries;
125 	for (gi = 0; gi < num_entries; gi += 100) {
126 	    int i = gi, ni = min(i + 100, num_entries);
127 	    const char *end;
128 
129 	    pprintd1(s, "%d ", ni - i);
130 	    if (lenum.entry.key_is_range) {
131 		if (lenum.entry.value_type == CODE_VALUE_CID || lenum.entry.value_type == CODE_VALUE_NOTDEF) {
132 		    stream_puts(s, pcmo->beginrange);
133 		    end = pcmo->endrange;
134 		} else {	/* must be def, not notdef */
135 		    stream_puts(s, "beginbfrange\n");
136 		    end = "endbfrange\n";
137 		}
138 	    } else {
139 		if (lenum.entry.value_type == CODE_VALUE_CID || lenum.entry.value_type == CODE_VALUE_NOTDEF) {
140 		    stream_puts(s, pcmo->beginchar);
141 		    end = pcmo->endchar;
142 		} else {	/* must be def, not notdef */
143 		    stream_puts(s, "beginbfchar\n");
144 		    end = "endbfchar\n";
145 		}
146 	    }
147 	    for (; i < ni; ++i) {
148 		int j;
149 		long value;
150 		int value_size;
151 
152 		DISCARD(gs_cmap_enum_next_entry(&lenum)); /* can't fail */
153 		value_size = lenum.entry.value.size;
154 		for (j = 0; j <= lenum.entry.key_is_range; ++j) {
155 		    stream_putc(s, '<');
156 		    pput_hex(s, lenum.entry.key[j], lenum.entry.key_size);
157 		    stream_putc(s, '>');
158 		}
159 		for (j = 0, value = 0; j < value_size; ++j)
160 		    value = (value << 8) + lenum.entry.value.data[j];
161 		switch (lenum.entry.value_type) {
162 		case CODE_VALUE_CID:
163 		case CODE_VALUE_NOTDEF:
164 		    pprintld1(s, "%ld", value);
165 		    break;
166 		case CODE_VALUE_CHARS:
167 		    stream_putc(s, '<');
168 		    pput_hex(s, lenum.entry.value.data, value_size);
169 		    stream_putc(s, '>');
170 		    break;
171 		case CODE_VALUE_GLYPH: {
172 		    gs_const_string str;
173 		    int code = pcmap->glyph_name(mem, (gs_glyph)value, &str,
174 						 pcmap->glyph_name_data);
175 
176 		    if (code < 0)
177 			return code;
178 		    stream_putc(s, '/');
179 		    code = put_name_chars(s, str.data, str.size);
180 		    if (code < 0)
181 			return code;
182 		}
183 		    break;
184 		default:	/* not possible */
185 		    return_error(gs_error_unregistered);
186 		}
187 		stream_putc(s, '\n');
188 	    }
189 	    stream_puts(s, end);
190 	}
191     }
192     return code;
193 }
194 
195 /* ---------------- Main program ---------------- */
196 
197 /* Write a CMap in its standard (source) format. */
198 int
psf_write_cmap(const gs_memory_t * mem,stream * s,const gs_cmap_t * pcmap,psf_put_name_chars_proc_t put_name_chars,const gs_const_string * alt_cmap_name,int font_index_only)199 psf_write_cmap(const gs_memory_t *mem,
200 	       stream *s, const gs_cmap_t *pcmap,
201 	       psf_put_name_chars_proc_t put_name_chars,
202 	       const gs_const_string *alt_cmap_name, int font_index_only)
203 {
204     const gs_const_string *const cmap_name =
205 	(alt_cmap_name ? alt_cmap_name : &pcmap->CMapName);
206     const gs_cid_system_info_t *const pcidsi = pcmap->CIDSystemInfo;
207 
208     switch (pcmap->CMapType) {
209     case 0: case 1: case 2:
210 	break;
211     default:
212 	return_error(gs_error_rangecheck);
213     }
214 
215     /* Write the header. */
216 
217     if (!pcmap->ToUnicode) {
218 	stream_puts(s, "%!PS-Adobe-3.0 Resource-CMap\n");
219 	stream_puts(s, "%%DocumentNeededResources: ProcSet (CIDInit)\n");
220 	stream_puts(s, "%%IncludeResource: ProcSet (CIDInit)\n");
221 	pput_string_entry(s, "%%BeginResource: CMap (", cmap_name);
222 	pput_string_entry(s, ")\n%%Title: (", cmap_name);
223 	pput_string_entry(s, " ", &pcidsi->Registry);
224 	pput_string_entry(s, " ", &pcidsi->Ordering);
225 	pprintd1(s, " %d)\n", pcidsi->Supplement);
226 	pprintg1(s, "%%%%Version: %g\n", pcmap->CMapVersion);
227     }
228     stream_puts(s, "/CIDInit /ProcSet findresource begin\n");
229     stream_puts(s, "12 dict begin\nbegincmap\n");
230 
231     /* Write the fixed entries. */
232 
233     pprintd1(s, "/CMapType %d def\n", pcmap->CMapType);
234     stream_puts(s, "/CMapName/");
235     put_name_chars(s, cmap_name->data, cmap_name->size);
236     stream_puts(s, " def\n");
237     if (!pcmap->ToUnicode) {
238 	pprintg1(s, "/CMapVersion %g def\n", pcmap->CMapVersion);
239 	stream_puts(s, "/CIDSystemInfo");
240 	if (font_index_only >= 0 && font_index_only < pcmap->num_fonts) {
241 	    cmap_put_system_info(s, pcidsi + font_index_only);
242 	} else if (pcmap->num_fonts == 1) {
243 	    cmap_put_system_info(s, pcidsi);
244 	} else {
245 	    int i;
246 
247 	    pprintd1(s, " %d array\n", pcmap->num_fonts);
248 	    for (i = 0; i < pcmap->num_fonts; ++i) {
249 		pprintd1(s, "dup %d", i);
250 		cmap_put_system_info(s, pcidsi + i);
251 		stream_puts(s, "put\n");
252 	    }
253 	}
254 	stream_puts(s, " def\n");
255 	if (uid_is_XUID(&pcmap->uid)) {
256 	    uint i, n = uid_XUID_size(&pcmap->uid);
257 	    const long *values = uid_XUID_values(&pcmap->uid);
258 
259 	    stream_puts(s, "/XUID [");
260 	    for (i = 0; i < n; ++i)
261 		pprintld1(s, " %ld", values[i]);
262 	    stream_puts(s, "] def\n");
263 	}
264 	pprintld1(s, "/UIDOffset %ld def\n", pcmap->UIDOffset);
265 	pprintd1(s, "/WMode %d def\n", pcmap->WMode);
266     }
267 
268     /* Write the code space ranges. */
269 
270     {
271 	gs_cmap_ranges_enum_t renum;
272 #define MAX_RANGES 100
273 	gx_code_space_range_t ranges[MAX_RANGES];
274 	int code, count = 0;
275 
276 	for (gs_cmap_ranges_enum_init(pcmap, &renum);
277 	     (code = gs_cmap_enum_next_range(&renum)) == 0; ) {
278 	    if (count == MAX_RANGES) {
279 		cmap_put_ranges(s, ranges, count);
280 		count = 0;
281 	    }
282 	    ranges[count++] = renum.range;
283 	}
284 	if (code < 0)
285 	    return code;
286 	if (count)
287 	    cmap_put_ranges(s, ranges, count);
288 #undef MAX_RANGES
289     }
290 
291     /* Write the code and notdef data. */
292 
293     {
294 	int code;
295 
296 	code = cmap_put_code_map(mem, s, 1, pcmap, &cmap_notdef_operators,
297 			         put_name_chars, font_index_only);
298 	if (code < 0)
299 	    return code;
300 	code = cmap_put_code_map(mem, s, 0, pcmap, &cmap_cid_operators,
301 			         put_name_chars, font_index_only);
302 	if (code < 0)
303 	    return code;
304     }
305 
306     /* Write the trailer. */
307 
308     stream_puts(s, "endcmap\n");
309     stream_puts(s, "CMapName currentdict /CMap defineresource pop\nend end\n");
310     if (!pcmap->ToUnicode) {
311 	stream_puts(s, "%%EndResource\n");
312 	stream_puts(s, "%%EOF\n");
313     }
314 
315     return 0;
316 }
317 
318