1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
|
/* Copyright (C) 1997, 1998 Aladdin Enterprises. All rights reserved.
This file is part of Aladdin Ghostscript.
Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND. No author
or distributor accepts any responsibility for the consequences of using it,
or for whether it serves any particular purpose or works at all, unless he
or she says so in writing. Refer to the Aladdin Ghostscript Free Public
License (the "License") for full details.
Every copy of Aladdin Ghostscript must include a copy of the License,
normally in a plain ASCII text file named PUBLIC. The License grants you
the right to copy, modify and redistribute Aladdin Ghostscript, but only
under certain conditions described in the License. Among other things, the
License requires that the copyright notice and this notice be preserved on
all copies.
*/
/* CMap creation operator */
#include "ghost.h"
#include "oper.h"
#include "gsmatrix.h" /* for gxfont.h */
#include "gsstruct.h"
#include "gsutil.h" /* for bytes_compare */
#include "gxfcmap.h"
#include "gxfont.h"
#include "ialloc.h"
#include "idict.h"
#include "idparam.h"
#include "ifont.h" /* for zfont_mark_glyph_name */
#include "iname.h"
#include "store.h"
/* ---------------- Internal procedures ---------------- */
/* Free a code map tree in case of memory overflow. */
private void
free_code_map(gx_code_map * pcmap, gs_memory_t * mem)
{
if (pcmap->type == cmap_subtree) {
int i;
for (i = pcmap->byte_data.count1; i >= 0; --i)
free_code_map(&pcmap->data.subtree[i], mem);
gs_free_object(mem, pcmap->data.subtree, "free_code_map");
}
}
/* Convert a code map to internal form. */
private int
acquire_code_map(gx_code_map * pcmap, const ref * pref, int depth,
gs_cmap * root, gs_memory_t * mem)
{
pcmap->add_offset = 0;
pcmap->cmap = root;
pcmap->byte_data.font_index = 0;
switch (r_type(pref)) {
case t_null:
pcmap->type = cmap_glyph;
pcmap->data.glyph = gs_no_glyph;
return 0;
case t_name:
pcmap->type = cmap_glyph;
pcmap->data.glyph = name_index(pref);
return 0;
case t_integer:
if (pref->value.intval < 0 ||
pref->value.intval > gs_max_glyph - gs_min_cid_glyph
)
break;
pcmap->type = cmap_glyph;
pcmap->data.glyph = pref->value.intval + gs_min_cid_glyph;
return 0;
case t_string:
if (r_size(pref) < 1 || r_size(pref) > 4)
break;
pcmap->type = cmap_char_code;
pcmap->num_bytes1 = r_size(pref) - 1;
{
int i;
gs_char chr = 0;
for (i = 0; i < r_size(pref); ++i)
chr = (chr << 8) + pref->value.const_bytes[i];
pcmap->data.ccode = chr;
}
return 0;
default:
if (!r_is_array(pref) || r_size(pref) < 1 || r_size(pref) > 256)
break;
if (depth >= 4)
return_error(e_limitcheck);
{
uint size = r_size(pref);
uint count = 0;
ref_type rtype;
long prev_value;
long diff;
uint run_length;
ref rsub;
gx_code_map *subtree;
uint i, j;
/* Do a first pass to count non-null entries and find runs. */
for (rtype = t_null, i = 0; i < size; ++i) {
ref_type prev_type = rtype;
array_get(pref, (long)i, &rsub);
rtype = r_type(&rsub);
switch (rtype) {
case t_null:
continue;
case t_integer:
if (prev_type == t_integer) {
if (run_length == 1) {
diff = rsub.value.intval - prev_value;
if (!(diff & ~1L)) {
prev_value = rsub.value.intval;
run_length = 2;
continue;
}
} else if (rsub.value.intval - prev_value == diff) {
prev_value = rsub.value.intval;
++run_length;
continue;
}
}
prev_value = rsub.value.intval;
run_length = 1;
/* falls through */
default:
++count;
}
}
if (count == 0) /* all nulls */
count = 1;
subtree =
gs_alloc_struct_array(mem, count, gx_code_map,
&st_code_map_element,
"acquire_code_map");
if (subtree == 0)
return_error(e_VMerror);
pcmap->type = cmap_subtree;
pcmap->data.subtree = subtree;
/* Initialize a single undefined entry, in case count = 0 */
/* or we have to bail out with j = 0. */
subtree->first = subtree->last = 255;
subtree->type = cmap_glyph;
subtree->byte_data.font_index = 0;
subtree->data.glyph = gs_no_glyph;
/* Do the second pass to construct the tree. */
for (rtype = t_null, i = j = 0; i < size; ++i) {
ref_type prev_type = rtype;
gx_code_map *submap = &subtree[j];
int code;
array_get(pref, (long)i, &rsub);
rtype = r_type(&rsub);
switch (rtype) {
case t_null:
continue;
case t_integer:
if (prev_type == t_integer) {
if (submap[-1].first == submap[-1].last) {
diff = rsub.value.intval - prev_value;
if (!(diff & ~1L)) {
prev_value = rsub.value.intval;
submap[-1].add_offset = (uint)diff;
submap[-1].last++;
continue;
}
} else if (rsub.value.intval - prev_value == diff) {
prev_value = rsub.value.intval;
submap[-1].last++;
continue;
}
}
prev_value = rsub.value.intval;
/* falls through */
default:
code = acquire_code_map(submap, &rsub, depth + 1,
root, mem);
if (code < 0) { /* Release allocated elements. */
pcmap->byte_data.count1 = (j ? j - 1 : 0);
free_code_map(pcmap, mem);
return code;
}
submap->first = submap->last = (byte)i;
++j;
}
}
pcmap->byte_data.count1 = count - 1;
}
return 0;
}
return_error(e_rangecheck);
}
/* Acquire CIDSystemInfo. If missing, set Registry and Ordering to */
/* empty strings and Supplement to 0, and return 1. */
/* Note that this currently does not handle the array format. */
private int
acquire_cid_system_info(gs_cid_system_info * pcidsi, const ref * op)
{
ref *prcidsi;
ref *pregistry;
ref *pordering;
if (dict_find_string(op, "CIDSystemInfo", &prcidsi) <= 0) {
pcidsi->Registry.data = 0, pcidsi->Registry.size = 0;
pcidsi->Ordering.data = 0, pcidsi->Ordering.size = 0;
pcidsi->Supplement = 0;
return 1;
}
if (!r_has_type(prcidsi, t_dictionary))
return_error(e_typecheck);
if (dict_find_string(prcidsi, "Registry", &pregistry) <= 0 ||
dict_find_string(prcidsi, "Ordering", &pordering) <= 0
)
return_error(e_rangecheck);
check_read_type_only(*pregistry, t_string);
check_read_type_only(*pordering, t_string);
pcidsi->Registry.data = pregistry->value.const_bytes;
pcidsi->Registry.size = r_size(pregistry);
pcidsi->Ordering.data = pordering->value.const_bytes;
pcidsi->Ordering.size = r_size(pordering);
return dict_int_param(prcidsi, "Supplement", 0, max_int, -1,
&pcidsi->Supplement);
}
/* Check compatibility of CIDSystemInfo. */
private bool
bytes_eq(const gs_const_string *pcs1, const gs_const_string *pcs2)
{
return !bytes_compare(pcs1->data, pcs1->size,
pcs2->data, pcs2->size);
}
private bool
cid_system_info_compatible(const gs_cid_system_info * psi1,
const gs_cid_system_info * psi2)
{
return bytes_eq(&psi1->Registry, &psi2->Registry) &&
bytes_eq(&psi1->Ordering, &psi2->Ordering);
}
/* ---------------- (Semi-)public procedures ---------------- */
/* Get the CodeMap from a Type 0 font, and check the CIDSystemInfo of */
/* its subsidiary fonts. */
int
ztype0_get_cmap(const gs_cmap ** ppcmap, const ref * pfdepvector, const ref * op)
{
ref *prcmap;
ref *pcodemap;
const gs_cmap *pcmap;
int code;
ref rfdep;
gs_cid_system_info cidsi;
if (dict_find_string(op, "CMap", &prcmap) <= 0 ||
!r_has_type(prcmap, t_dictionary) ||
dict_find_string(prcmap, "CodeMap", &pcodemap) <= 0 ||
!r_has_stype(pcodemap, imemory, st_cmap)
)
return_error(e_invalidfont);
pcmap = r_ptr(pcodemap, gs_cmap);
/* Currently we only handle 1-element fonts. */
if (r_size(pfdepvector) != 1)
return_error(e_rangecheck);
array_get(pfdepvector, 0L, &rfdep);
code = acquire_cid_system_info(&cidsi, &rfdep);
if (code < 0)
return code;
if (code == 0 &&
!cid_system_info_compatible(&cidsi, &pcmap->CIDSystemInfo)
)
return_error(e_rangecheck);
*ppcmap = pcmap;
return 0;
}
/* ---------------- Operators ---------------- */
/* <CMap> .buildcmap <CMap> */
/*
* Create the internal form of a CMap. The initial CMap must be read-write
* and have an entry with key = CodeMap and value = null; the result is
* read-only and has a real CodeMap.
*/
private int
zbuildcmap(os_ptr op)
{
int code;
ref *pcodemaps;
ref *pcodemap;
gs_cmap *pcmap;
ref rdef, rnotdef, rcmap;
check_type(*op, t_dictionary);
check_dict_write(*op);
pcmap = ialloc_struct(gs_cmap, &st_cmap, "zbuildcmap(cmap)");
if (pcmap == 0) {
code = gs_note_error(e_VMerror);
goto fail;
}
if ((code = dict_uid_param(op, &pcmap->uid, 0, imemory)) < 0 ||
(code = dict_int_param(op, "WMode", 0, 1, 0, &pcmap->WMode)) < 0
)
goto fail;
if (dict_find_string(op, ".CodeMaps", &pcodemaps) <= 0 ||
!r_has_type(pcodemaps, t_array) ||
r_size(pcodemaps) != 2 ||
dict_find_string(op, "CodeMap", &pcodemap) <= 0 ||
!r_has_type(pcodemap, t_null)
) {
code = gs_note_error(e_rangecheck);
goto fail;
}
if ((code = acquire_cid_system_info(&pcmap->CIDSystemInfo, op)) < 0 ||
(array_get(pcodemaps, 0L, &rdef),
(code = acquire_code_map(&pcmap->def, &rdef, 0, pcmap, imemory)) < 0) ||
(array_get(pcodemaps, 1L, &rnotdef),
(code = acquire_code_map(&pcmap->notdef, &rnotdef, 0, pcmap, imemory)) < 0)
)
goto fail;
pcmap->mark_glyph = zfont_mark_glyph_name;
pcmap->mark_glyph_data = 0;
make_istruct_new(&rcmap, a_readonly, pcmap);
code = dict_put_string(op, "CodeMap", &rcmap);
if (code < 0)
goto fail;
return zreadonly(op);
fail:
ifree_object(pcmap, "zbuildcmap(cmap)");
return code;
}
/* ------ Initialization procedure ------ */
const op_def zfcmap_op_defs[] =
{
{"1.buildcmap", zbuildcmap},
op_def_end(0)
};
|