1
/* Copyright (C) 2001-2006 Artifex Software, Inc.
4
This software is provided AS-IS with no warranty, either express or
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.
14
/* $Id: gdevpsfm.c 8250 2007-09-25 13:31:24Z giles $ */
25
/* ---------------- Utilities ---------------- */
27
typedef struct cmap_operators_s {
28
const char *beginchar;
30
const char *beginrange;
33
static const cmap_operators_t
34
cmap_cid_operators = {
35
"begincidchar\n", "endcidchar\n",
36
"begincidrange\n", "endcidrange\n"
38
cmap_notdef_operators = {
39
"beginnotdefchar\n", "endnotdefchar\n",
40
"beginnotdefrange\n", "endnotdefrange\n"
43
/* Write a gs_string with a prefix. */
45
pput_string_entry(stream *s, const char *prefix, const gs_const_string *pstr)
47
stream_puts(s, prefix);
48
stream_write(s, pstr->data, pstr->size);
51
/* Write a hex string. */
53
pput_hex(stream *s, const byte *pcid, int size)
56
static const char *const hex_digits = "0123456789abcdef";
58
for (i = 0; i < size; ++i) {
59
stream_putc(s, hex_digits[pcid[i] >> 4]);
60
stream_putc(s, hex_digits[pcid[i] & 0xf]);
64
/* Write a list of code space ranges. */
66
cmap_put_ranges(stream *s, const gx_code_space_range_t *pcsr, int count)
70
pprintd1(s, "%d begincodespacerange\n", count);
71
for (i = 0; i < count; ++i, ++pcsr) {
73
pput_hex(s, pcsr->first, pcsr->size);
75
pput_hex(s, pcsr->last, pcsr->size);
76
stream_puts(s, ">\n");
78
stream_puts(s, "endcodespacerange\n");
81
/* Write one CIDSystemInfo dictionary. */
83
cmap_put_system_info(stream *s, const gs_cid_system_info_t *pcidsi)
85
if (cid_system_info_is_null(pcidsi)) {
86
stream_puts(s, " null ");
88
stream_puts(s, " 3 dict dup begin\n");
89
stream_puts(s, "/Registry ");
90
s_write_ps_string(s, pcidsi->Registry.data, pcidsi->Registry.size, 0);
91
stream_puts(s, " def\n/Ordering ");
92
s_write_ps_string(s, pcidsi->Ordering.data, pcidsi->Ordering.size, 0);
93
pprintd1(s, " def\n/Supplement %d def\nend ", pcidsi->Supplement);
97
/* Write one code map. */
99
cmap_put_code_map(const gs_memory_t *mem,
100
stream *s, int which, const gs_cmap_t *pcmap,
101
const cmap_operators_t *pcmo,
102
psf_put_name_chars_proc_t put_name_chars,
105
/* For simplicity, produce one entry for each lookup range. */
106
gs_cmap_lookups_enum_t lenum;
107
int font_index = (pcmap->num_fonts <= 1 ? 0 : -1);
110
for (gs_cmap_lookups_enum_init(pcmap, which, &lenum);
111
(code = gs_cmap_enum_next_lookup(&lenum)) == 0; ) {
112
gs_cmap_lookups_enum_t counter;
116
if (font_index_only >= 0 && lenum.entry.font_index != font_index_only)
118
if (font_index_only < 0 && lenum.entry.font_index != font_index) {
119
pprintd1(s, "%d usefont\n", lenum.entry.font_index);
120
font_index = lenum.entry.font_index;
122
/* Count the number of entries in this lookup range. */
124
while (gs_cmap_enum_next_entry(&counter) == 0)
126
for (gi = 0; gi < num_entries; gi += 100) {
127
int i = gi, ni = min(i + 100, num_entries);
130
pprintd1(s, "%d ", ni - i);
131
if (lenum.entry.key_is_range) {
132
if (lenum.entry.value_type == CODE_VALUE_CID || lenum.entry.value_type == CODE_VALUE_NOTDEF) {
133
stream_puts(s, pcmo->beginrange);
134
end = pcmo->endrange;
135
} else { /* must be def, not notdef */
136
stream_puts(s, "beginbfrange\n");
137
end = "endbfrange\n";
140
if (lenum.entry.value_type == CODE_VALUE_CID || lenum.entry.value_type == CODE_VALUE_NOTDEF) {
141
stream_puts(s, pcmo->beginchar);
143
} else { /* must be def, not notdef */
144
stream_puts(s, "beginbfchar\n");
148
for (; i < ni; ++i) {
153
DISCARD(gs_cmap_enum_next_entry(&lenum)); /* can't fail */
154
value_size = lenum.entry.value.size;
155
for (j = 0; j <= lenum.entry.key_is_range; ++j) {
157
pput_hex(s, lenum.entry.key[j], lenum.entry.key_size);
160
for (j = 0, value = 0; j < value_size; ++j)
161
value = (value << 8) + lenum.entry.value.data[j];
162
switch (lenum.entry.value_type) {
164
case CODE_VALUE_NOTDEF:
165
pprintld1(s, "%ld", value);
167
case CODE_VALUE_CHARS:
169
pput_hex(s, lenum.entry.value.data, value_size);
172
case CODE_VALUE_GLYPH: {
174
int code = pcmap->glyph_name(mem, (gs_glyph)value, &str,
175
pcmap->glyph_name_data);
180
code = put_name_chars(s, str.data, str.size);
185
default: /* not possible */
186
return_error(gs_error_unregistered);
188
stream_putc(s, '\n');
196
/* ---------------- Main program ---------------- */
198
/* Write a CMap in its standard (source) format. */
200
psf_write_cmap(const gs_memory_t *mem,
201
stream *s, const gs_cmap_t *pcmap,
202
psf_put_name_chars_proc_t put_name_chars,
203
const gs_const_string *alt_cmap_name, int font_index_only)
205
const gs_const_string *const cmap_name =
206
(alt_cmap_name ? alt_cmap_name : &pcmap->CMapName);
207
const gs_cid_system_info_t *const pcidsi = pcmap->CIDSystemInfo;
209
switch (pcmap->CMapType) {
210
case 0: case 1: case 2:
213
return_error(gs_error_rangecheck);
216
/* Write the header. */
218
if (!pcmap->ToUnicode) {
219
stream_puts(s, "%!PS-Adobe-3.0 Resource-CMap\n");
220
stream_puts(s, "%%DocumentNeededResources: ProcSet (CIDInit)\n");
221
stream_puts(s, "%%IncludeResource: ProcSet (CIDInit)\n");
222
pput_string_entry(s, "%%BeginResource: CMap (", cmap_name);
223
pput_string_entry(s, ")\n%%Title: (", cmap_name);
224
pput_string_entry(s, " ", &pcidsi->Registry);
225
pput_string_entry(s, " ", &pcidsi->Ordering);
226
pprintd1(s, " %d)\n", pcidsi->Supplement);
227
pprintg1(s, "%%%%Version: %g\n", pcmap->CMapVersion);
229
stream_puts(s, "/CIDInit /ProcSet findresource begin\n");
230
stream_puts(s, "12 dict begin\nbegincmap\n");
232
/* Write the fixed entries. */
234
pprintd1(s, "/CMapType %d def\n", pcmap->CMapType);
235
stream_puts(s, "/CMapName/");
236
put_name_chars(s, cmap_name->data, cmap_name->size);
237
stream_puts(s, " def\n");
238
if (!pcmap->ToUnicode) {
239
pprintg1(s, "/CMapVersion %g def\n", pcmap->CMapVersion);
240
stream_puts(s, "/CIDSystemInfo");
241
if (font_index_only >= 0 && font_index_only < pcmap->num_fonts) {
242
cmap_put_system_info(s, pcidsi + font_index_only);
243
} else if (pcmap->num_fonts == 1) {
244
cmap_put_system_info(s, pcidsi);
248
pprintd1(s, " %d array\n", pcmap->num_fonts);
249
for (i = 0; i < pcmap->num_fonts; ++i) {
250
pprintd1(s, "dup %d", i);
251
cmap_put_system_info(s, pcidsi + i);
252
stream_puts(s, "put\n");
255
stream_puts(s, " def\n");
256
if (uid_is_XUID(&pcmap->uid)) {
257
uint i, n = uid_XUID_size(&pcmap->uid);
258
const long *values = uid_XUID_values(&pcmap->uid);
260
stream_puts(s, "/XUID [");
261
for (i = 0; i < n; ++i)
262
pprintld1(s, " %ld", values[i]);
263
stream_puts(s, "] def\n");
265
pprintld1(s, "/UIDOffset %ld def\n", pcmap->UIDOffset);
266
pprintd1(s, "/WMode %d def\n", pcmap->WMode);
269
/* Write the code space ranges. */
272
gs_cmap_ranges_enum_t renum;
273
#define MAX_RANGES 100
274
gx_code_space_range_t ranges[MAX_RANGES];
277
for (gs_cmap_ranges_enum_init(pcmap, &renum);
278
(code = gs_cmap_enum_next_range(&renum)) == 0; ) {
279
if (count == MAX_RANGES) {
280
cmap_put_ranges(s, ranges, count);
283
ranges[count++] = renum.range;
288
cmap_put_ranges(s, ranges, count);
292
/* Write the code and notdef data. */
297
code = cmap_put_code_map(mem, s, 1, pcmap, &cmap_notdef_operators,
298
put_name_chars, font_index_only);
301
code = cmap_put_code_map(mem, s, 0, pcmap, &cmap_cid_operators,
302
put_name_chars, font_index_only);
307
/* Write the trailer. */
309
stream_puts(s, "endcmap\n");
310
stream_puts(s, "CMapName currentdict /CMap defineresource pop\nend end\n");
311
if (!pcmap->ToUnicode) {
312
stream_puts(s, "%%EndResource\n");
313
stream_puts(s, "%%EOF\n");