~ubuntu-branches/ubuntu/jaunty/ghostscript/jaunty-updates

« back to all changes in this revision

Viewing changes to src/gsfcmap.c

  • Committer: Bazaar Package Importer
  • Author(s): Till Kamppeter
  • Date: 2009-01-20 16:40:45 UTC
  • mfrom: (1.1.10 upstream)
  • Revision ID: james.westby@ubuntu.com-20090120164045-lnfhi0n30o5lwhwa
Tags: 8.64.dfsg.1~svn9377-0ubuntu1
* New upstream release (SVN rev 9377)
   o Fixes many bugs concerning PDF rendering, to make the PDF printing
     workflow correctly working.
   o Fixes long-standing bugs in many drivers, like input paper tray and
     duplex options not working for the built-in PCL 4, 5, 5c, 5e, and
     6/XL drivers, PDF input not working for bjc600, bjc800, and cups
     output devices, several options not working and uninitialized
     memory with cups output device.
   o Merged nearly all patches of the Ubuntu and Debian packages upstream.
   o Fixes LP: #317810, LP: #314439, LP: #314018.
* debian/patches/03_libpaper_support.dpatch,
  debian/patches/11_gs-cjk_font_glyph_handling_fix.dpatch,
  debian/patches/12_gs-cjk_vertical_writing_metrics_fix.dpatch,
  debian/patches/13_gs-cjk_cjkps_examples.dpatch,
  debian/patches/20_bbox_segv_fix.dpatch,
  debian/patches/21_brother_7x0_gdi_fix.dpatch,
  debian/patches/22_epsn_margin_workaround.dpatch,
  debian/patches/24_gs_man_fix.dpatch,
  debian/patches/25_toolbin_insecure_tmp_usage_fix.dpatch,
  debian/patches/26_assorted_script_fixes.dpatch,
  debian/patches/29_gs_css_fix.dpatch,
  debian/patches/30_ps2pdf_man_improvement.dpatch,
  debian/patches/31_fix-gc-sigbus.dpatch,
  debian/patches/34_ftbfs-on-hurd-fix.dpatch,
  debian/patches/35_disable_libcairo.dpatch,
  debian/patches/38_pxl-duplex.dpatch,
  debian/patches/39_pxl-resolution.dpatch,
  debian/patches/42_gs-init-ps-delaybind-fix.dpatch,
  debian/patches/45_bjc600-bjc800-pdf-input.dpatch,
  debian/patches/48_cups-output-device-pdf-duplex-uninitialized-memory-fix.dpatch,
  debian/patches/50_lips4-floating-point-exception.dpatch,
  debian/patches/52_cups-device-logging.dpatch,
  debian/patches/55_pcl-input-slot-fix.dpatch,
  debian/patches/57_pxl-input-slot-fix.dpatch,
  debian/patches/60_pxl-cups-driver-pdf.dpatch,
  debian/patches/62_onebitcmyk-pdf.dpatch,
  debian/patches/65_too-big-temp-files-1.dpatch,
  debian/patches/67_too-big-temp-files-2.dpatch,
  debian/patches/70_take-into-account-data-in-stream-buffer-before-refill.dpatch:
  Removed, applied upstream.
* debian/patches/01_docdir_fix_for_debian.dpatch,
  debian/patches/02_gs_man_fix_debian.dpatch,
  debian/patches/01_docdir-fix-for-debian.dpatch,
  debian/patches/02_docdir-fix-for-debian.dpatch: Renamed patches to
  make merging with Debian easier.
* debian/patches/32_improve-handling-of-media-size-changes-from-gv.dpatch, 
  debian/patches/33_bad-params-to-xinitimage-on-large-bitmaps.dpatch:
  regenerated for new source directory structure.
* debian/rules: Corrected paths to remove cidfmap (it is in Resource/Init/
  in GS 8.64) and to install headers (source paths are psi/ and base/ now).
* debian/rules: Remove all fontmaps, as DeFoMa replaces them.
* debian/local/pdftoraster/pdftoraster.c,
  debian/local/pdftoraster/pdftoraster.convs, debian/rules: Removed
  added pdftoraster filter and use the one which comes with Ghostscript.
* debian/ghostscript.links: s/8.63/8.64/

Show diffs side-by-side

added added

removed removed

Lines of Context:
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: gsfcmap.c 8250 2007-09-25 13:31:24Z giles $ */
15
 
/* CMap character decoding */
16
 
#include "memory_.h"
17
 
#include "string_.h"
18
 
#include "gx.h"
19
 
#include "gserrors.h"
20
 
#include "gsstruct.h"
21
 
#include "gsutil.h"             /* for gs_next_ids */
22
 
#include "gxfcmap.h"
23
 
 
24
 
typedef struct gs_cmap_identity_s {
25
 
    GS_CMAP_COMMON;
26
 
    int num_bytes;
27
 
    int varying_bytes;
28
 
    int code;                   /* 0 or num_bytes */
29
 
} gs_cmap_identity_t;
30
 
 
31
 
/* GC descriptors */
32
 
public_st_cmap();
33
 
gs_private_st_suffix_add0_local(st_cmap_identity, gs_cmap_identity_t,
34
 
                                "gs_cmap_identity_t", cmap_ptrs, cmap_data,
35
 
                                st_cmap);
36
 
 
37
 
/* ---------------- Client procedures ---------------- */
38
 
 
39
 
    /* ------ Initialization/creation ------ */
40
 
 
41
 
/*
42
 
 * Create an Identity CMap.
43
 
 */
44
 
static uint
45
 
get_integer_bytes(const byte *src, int count)
46
 
{
47
 
    uint v = 0;
48
 
    int i;
49
 
 
50
 
    for (i = 0; i < count; ++i)
51
 
        v = (v << 8) + src[i];
52
 
    return v;
53
 
}
54
 
static int
55
 
identity_decode_next(const gs_cmap_t *pcmap, const gs_const_string *str,
56
 
                     uint *pindex, uint *pfidx,
57
 
                     gs_char *pchr, gs_glyph *pglyph)
58
 
{
59
 
    const gs_cmap_identity_t *const pcimap =
60
 
        (const gs_cmap_identity_t *)pcmap;
61
 
    int num_bytes = pcimap->num_bytes;
62
 
    uint value;
63
 
 
64
 
    if (str->size < *pindex + num_bytes) {
65
 
        *pglyph = gs_no_glyph;
66
 
        return (*pindex == str->size ? 2 : -1);
67
 
    }
68
 
    value = get_integer_bytes(str->data + *pindex, num_bytes);
69
 
    *pglyph = gs_min_cid_glyph + value;
70
 
    *pchr = value;
71
 
    *pindex += num_bytes;
72
 
    *pfidx = 0;
73
 
    return pcimap->code;
74
 
}
75
 
static int
76
 
identity_next_range(gs_cmap_ranges_enum_t *penum)
77
 
{
78
 
    if (penum->index == 0) {
79
 
        const gs_cmap_identity_t *const pcimap =
80
 
            (const gs_cmap_identity_t *)penum->cmap;
81
 
 
82
 
        memset(penum->range.first, 0, pcimap->num_bytes);
83
 
        memset(penum->range.last, 0xff, pcimap->num_bytes);
84
 
        penum->range.size = pcimap->num_bytes;
85
 
        penum->index = 1;
86
 
        return 0;
87
 
    }
88
 
    return 1;
89
 
}
90
 
static const gs_cmap_ranges_enum_procs_t identity_range_procs = {
91
 
    identity_next_range
92
 
};
93
 
static void
94
 
identity_enum_ranges(const gs_cmap_t *pcmap, gs_cmap_ranges_enum_t *pre)
95
 
{
96
 
    gs_cmap_ranges_enum_setup(pre, pcmap, &identity_range_procs);
97
 
}
98
 
static int
99
 
identity_next_lookup(gs_cmap_lookups_enum_t *penum)
100
 
{
101
 
    if (penum->index[0] == 0) {
102
 
        const gs_cmap_identity_t *const pcimap =
103
 
            (const gs_cmap_identity_t *)penum->cmap;
104
 
        int num_bytes = pcimap->num_bytes;
105
 
 
106
 
        memset(penum->entry.key[0], 0, num_bytes);
107
 
        memset(penum->entry.key[1], 0xff, num_bytes);
108
 
        memset(penum->entry.key[1], 0, num_bytes - pcimap->varying_bytes);
109
 
        penum->entry.key_size = num_bytes;
110
 
        penum->entry.key_is_range = true;
111
 
        penum->entry.value_type =
112
 
            (pcimap->code ? CODE_VALUE_CHARS : CODE_VALUE_CID);
113
 
        penum->entry.value.size = num_bytes;
114
 
        penum->entry.font_index = 0;
115
 
        penum->index[0] = 1;
116
 
        return 0;
117
 
    }
118
 
    return 1;
119
 
}
120
 
static int
121
 
no_next_lookup(gs_cmap_lookups_enum_t *penum)
122
 
{
123
 
    return 1;
124
 
}
125
 
static int
126
 
identity_next_entry(gs_cmap_lookups_enum_t *penum)
127
 
{
128
 
    const gs_cmap_identity_t *const pcimap =
129
 
        (const gs_cmap_identity_t *)penum->cmap;
130
 
    int num_bytes = pcimap->num_bytes;
131
 
    int i = num_bytes - pcimap->varying_bytes;
132
 
 
133
 
    memcpy(penum->temp_value, penum->entry.key[0], num_bytes);
134
 
    memcpy(penum->entry.key[0], penum->entry.key[1], i);
135
 
    while (--i >= 0)
136
 
        if (++(penum->entry.key[1][i]) != 0) {
137
 
            penum->entry.value.data = penum->temp_value;
138
 
            return 0;
139
 
        }
140
 
    return 1;
141
 
}
142
 
 
143
 
static const gs_cmap_lookups_enum_procs_t identity_lookup_procs = {
144
 
    identity_next_lookup, identity_next_entry
145
 
};
146
 
const gs_cmap_lookups_enum_procs_t gs_cmap_no_lookups_procs = {
147
 
    no_next_lookup, 0
148
 
};
149
 
static void
150
 
identity_enum_lookups(const gs_cmap_t *pcmap, int which,
151
 
                      gs_cmap_lookups_enum_t *pre)
152
 
{
153
 
    gs_cmap_lookups_enum_setup(pre, pcmap,
154
 
                               (which ? &gs_cmap_no_lookups_procs :
155
 
                                &identity_lookup_procs));
156
 
}
157
 
static bool
158
 
identity_is_identity(const gs_cmap_t *pcmap, int font_index_only)
159
 
{
160
 
    return true;
161
 
}
162
 
 
163
 
static const gs_cmap_procs_t identity_procs = {
164
 
    identity_decode_next, identity_enum_ranges, identity_enum_lookups, identity_is_identity
165
 
};
166
 
 
167
 
static int
168
 
gs_cmap_identity_alloc(gs_cmap_t **ppcmap, int num_bytes, int varying_bytes,
169
 
                       int return_code, const char *cmap_name, int wmode,
170
 
                       gs_memory_t *mem)
171
 
{
172
 
    /*
173
 
     * We could allow any value of num_bytes between 1 and
174
 
     * min(MAX_CMAP_CODE_SIZE, 4), but if num_bytes != 2, we can't name
175
 
     * the result "Identity-[HV]".
176
 
     */
177
 
    static const gs_cid_system_info_t identity_cidsi = {
178
 
        { (const byte *)"Adobe", 5 },
179
 
        { (const byte *)"Identity", 8 },
180
 
        0
181
 
    };
182
 
    int code;
183
 
    gs_cmap_identity_t *pcimap;
184
 
 
185
 
    if (num_bytes != 2)
186
 
        return_error(gs_error_rangecheck);
187
 
    code = gs_cmap_alloc(ppcmap, &st_cmap_identity, wmode,
188
 
                         (const byte *)cmap_name, strlen(cmap_name),
189
 
                         &identity_cidsi, 1, &identity_procs, mem);
190
 
    if (code < 0)
191
 
        return code;
192
 
    pcimap = (gs_cmap_identity_t *)*ppcmap;
193
 
    pcimap->num_bytes = num_bytes;
194
 
    pcimap->varying_bytes = varying_bytes;
195
 
    pcimap->code = return_code;
196
 
    return 0;
197
 
}
198
 
int
199
 
gs_cmap_create_identity(gs_cmap_t **ppcmap, int num_bytes, int wmode,
200
 
                        gs_memory_t *mem)
201
 
{
202
 
    return gs_cmap_identity_alloc(ppcmap, num_bytes, num_bytes, 0,
203
 
                                  (wmode ? "Identity-V" : "Identity-H"),
204
 
                                  wmode, mem);
205
 
}
206
 
int
207
 
gs_cmap_create_char_identity(gs_cmap_t **ppcmap, int num_bytes, int wmode,
208
 
                             gs_memory_t *mem)
209
 
{
210
 
    return gs_cmap_identity_alloc(ppcmap, num_bytes, 1, num_bytes,
211
 
                                  (wmode ? "Identity-BF-V" : "Identity-BF-H"),
212
 
                                  wmode, mem);
213
 
}
214
 
 
215
 
    /* ------ Check identity ------ */
216
 
 
217
 
/*
218
 
 * Check for identity CMap. Uses a fast check for special cases.
219
 
 */
220
 
int
221
 
gs_cmap_is_identity(const gs_cmap_t *pcmap, int font_index_only)
222
 
{
223
 
    return pcmap->procs->is_identity(pcmap, font_index_only);
224
 
}
225
 
 
226
 
    /* ------ Decoding ------ */
227
 
 
228
 
/*
229
 
 * Decode and map a character from a string using a CMap.
230
 
 * See gsfcmap.h for details.
231
 
 */
232
 
int
233
 
gs_cmap_decode_next(const gs_cmap_t *pcmap, const gs_const_string *str,
234
 
                    uint *pindex, uint *pfidx,
235
 
                    gs_char *pchr, gs_glyph *pglyph)
236
 
{
237
 
    return pcmap->procs->decode_next(pcmap, str, pindex, pfidx, pchr, pglyph);
238
 
}
239
 
 
240
 
    /* ------ Enumeration ------ */
241
 
 
242
 
/*
243
 
 * Initialize the enumeration of the code space ranges, and enumerate
244
 
 * the next range.  See gxfcmap.h for details.
245
 
 */
246
 
void
247
 
gs_cmap_ranges_enum_init(const gs_cmap_t *pcmap, gs_cmap_ranges_enum_t *penum)
248
 
{
249
 
    pcmap->procs->enum_ranges(pcmap, penum);
250
 
}
251
 
int
252
 
gs_cmap_enum_next_range(gs_cmap_ranges_enum_t *penum)
253
 
{
254
 
    return penum->procs->next_range(penum);
255
 
}
256
 
 
257
 
/*
258
 
 * Initialize the enumeration of the lookups, and enumerate the next
259
 
 * the next lookup or entry.  See gxfcmap.h for details.
260
 
 */
261
 
void
262
 
gs_cmap_lookups_enum_init(const gs_cmap_t *pcmap, int which,
263
 
                          gs_cmap_lookups_enum_t *penum)
264
 
{
265
 
    pcmap->procs->enum_lookups(pcmap, which, penum);
266
 
}
267
 
int
268
 
gs_cmap_enum_next_lookup(gs_cmap_lookups_enum_t *penum)
269
 
{
270
 
    return penum->procs->next_lookup(penum);
271
 
}
272
 
int
273
 
gs_cmap_enum_next_entry(gs_cmap_lookups_enum_t *penum)
274
 
{
275
 
    return penum->procs->next_entry(penum);
276
 
}
277
 
 
278
 
/* ---------------- Implementation procedures ---------------- */
279
 
 
280
 
    /* ------ Initialization/creation ------ */
281
 
 
282
 
/*
283
 
 * Initialize a just-allocated CMap, to ensure that all pointers are clean
284
 
 * for the GC.  Note that this only initializes the common part.
285
 
 */
286
 
void
287
 
gs_cmap_init(const gs_memory_t *mem, gs_cmap_t *pcmap, int num_fonts)
288
 
{
289
 
    memset(pcmap, 0, sizeof(*pcmap));
290
 
    /* We reserve a range of IDs for pdfwrite needs,
291
 
       to allow an identification of submaps for a particular subfont.
292
 
     */
293
 
    pcmap->id = gs_next_ids(mem, num_fonts);
294
 
    pcmap->num_fonts = num_fonts;
295
 
    uid_set_invalid(&pcmap->uid);
296
 
}
297
 
 
298
 
/*
299
 
 * Allocate and initialize (the common part of) a CMap.
300
 
 */
301
 
int
302
 
gs_cmap_alloc(gs_cmap_t **ppcmap, const gs_memory_struct_type_t *pstype,
303
 
              int wmode, const byte *map_name, uint name_size,
304
 
              const gs_cid_system_info_t *pcidsi_in, int num_fonts,
305
 
              const gs_cmap_procs_t *procs, gs_memory_t *mem)
306
 
{
307
 
    gs_cmap_t *pcmap =
308
 
        gs_alloc_struct(mem, gs_cmap_t, pstype, "gs_cmap_alloc(CMap)");
309
 
    gs_cid_system_info_t *pcidsi =
310
 
        gs_alloc_struct_array(mem, num_fonts, gs_cid_system_info_t,
311
 
                              &st_cid_system_info_element,
312
 
                              "gs_cmap_alloc(CIDSystemInfo)");
313
 
 
314
 
    if (pcmap == 0 || pcidsi == 0) {
315
 
        gs_free_object(mem, pcidsi, "gs_cmap_alloc(CIDSystemInfo)");
316
 
        gs_free_object(mem, pcmap, "gs_cmap_alloc(CMap)");
317
 
        return_error(gs_error_VMerror);
318
 
    }
319
 
    gs_cmap_init(mem, pcmap, num_fonts);        /* id, uid, num_fonts */
320
 
    pcmap->CMapType = 1;
321
 
    pcmap->CMapName.data = map_name;
322
 
    pcmap->CMapName.size = name_size;
323
 
    if (pcidsi_in)
324
 
        memcpy(pcidsi, pcidsi_in, sizeof(*pcidsi) * num_fonts);
325
 
    else
326
 
        memset(pcidsi, 0, sizeof(*pcidsi) * num_fonts);
327
 
    pcmap->CIDSystemInfo = pcidsi;
328
 
    pcmap->CMapVersion = 1.0;
329
 
    /* uid = 0, UIDOffset = 0 */
330
 
    pcmap->WMode = wmode;
331
 
    /* from_Unicode = 0 */
332
 
    /* not glyph_name, glyph_name_data */
333
 
    pcmap->procs = procs;
334
 
    *ppcmap = pcmap;
335
 
    return 0;
336
 
}
337
 
 
338
 
/*
339
 
 * Initialize an enumerator with convenient defaults (index = 0).
340
 
 */
341
 
void
342
 
gs_cmap_ranges_enum_setup(gs_cmap_ranges_enum_t *penum,
343
 
                          const gs_cmap_t *pcmap,
344
 
                          const gs_cmap_ranges_enum_procs_t *procs)
345
 
{
346
 
    penum->cmap = pcmap;
347
 
    penum->procs = procs;
348
 
    penum->index = 0;
349
 
}
350
 
void
351
 
gs_cmap_lookups_enum_setup(gs_cmap_lookups_enum_t *penum,
352
 
                           const gs_cmap_t *pcmap,
353
 
                           const gs_cmap_lookups_enum_procs_t *procs)
354
 
{
355
 
    penum->cmap = pcmap;
356
 
    penum->procs = procs;
357
 
    penum->index[0] = penum->index[1] = 0;
358
 
}
359
 
 
360
 
/* 
361
 
 * For a random CMap, compute whether it is identity.
362
 
 * It is not applicable to gs_cmap_ToUnicode_t due to
363
 
 * different sizes of domain keys and range values.
364
 
 * Note we reject CMaps with Registry=Artifex
365
 
 * to force embedding special instandard CMaps,
366
 
 * which are not commonly in use yet.
367
 
 */
368
 
bool
369
 
gs_cmap_compute_identity(const gs_cmap_t *pcmap, int font_index_only)
370
 
{
371
 
    const int which = 0;
372
 
    gs_cmap_lookups_enum_t lenum;
373
 
    int code;
374
 
 
375
 
    if (!bytes_compare(pcmap->CIDSystemInfo->Registry.data, pcmap->CIDSystemInfo->Registry.size,
376
 
                    (const byte *)"Artifex", 7))
377
 
        return false;
378
 
    for (gs_cmap_lookups_enum_init(pcmap, which, &lenum);
379
 
         (code = gs_cmap_enum_next_lookup(&lenum)) == 0; ) {
380
 
        if (font_index_only >= 0 && lenum.entry.font_index != font_index_only)
381
 
            continue;
382
 
        if (font_index_only < 0 && lenum.entry.font_index > 0)
383
 
            return false;
384
 
        while (gs_cmap_enum_next_entry(&lenum) == 0) {
385
 
            switch (lenum.entry.value_type) {
386
 
            case CODE_VALUE_CID:
387
 
                break;
388
 
            case CODE_VALUE_CHARS:
389
 
                return false; /* Not implemented yet. */
390
 
            case CODE_VALUE_GLYPH:
391
 
                return false;
392
 
            default :
393
 
                return false; /* Must not happen. */
394
 
            }
395
 
            if (lenum.entry.key_size != lenum.entry.value.size)
396
 
                return false;
397
 
            if (memcmp(lenum.entry.key[0], lenum.entry.value.data, 
398
 
                lenum.entry.key_size))
399
 
                return false;
400
 
        }
401
 
    }
402
 
    return true;
403
 
}
404
 
 
405
 
/* ================= ToUnicode CMap ========================= */
406
 
 
407
 
/*
408
 
 * This kind of CMaps keeps character a mapping from a random
409
 
 * PS encoding to Unicode, being defined in PDF reference, "ToUnicode CMaps".
410
 
 * It represents ranges in a closure data, without using 
411
 
 * gx_cmap_lookup_range_t. A special function gs_cmap_ToUnicode_set
412
 
 * allows to write code pairs into the closure data.
413
 
 */
414
 
 
415
 
static const int gs_cmap_ToUnicode_code_bytes = 2;
416
 
 
417
 
typedef struct gs_cmap_ToUnicode_s {
418
 
    GS_CMAP_COMMON;
419
 
    int num_codes;
420
 
    int key_size;
421
 
    bool is_identity;
422
 
} gs_cmap_ToUnicode_t;
423
 
 
424
 
gs_private_st_suffix_add0(st_cmap_ToUnicode, gs_cmap_ToUnicode_t,
425
 
    "gs_cmap_ToUnicode_t", cmap_ToUnicode_enum_ptrs, cmap_ToUnicode_reloc_ptrs,
426
 
    st_cmap);
427
 
 
428
 
static int
429
 
gs_cmap_ToUnicode_next_range(gs_cmap_ranges_enum_t *penum)
430
 
{   const gs_cmap_ToUnicode_t *cmap = (gs_cmap_ToUnicode_t *)penum->cmap;
431
 
    if (penum->index == 0) {
432
 
        memset(penum->range.first, 0, cmap->key_size);
433
 
        memset(penum->range.last, 0xff, cmap->key_size);
434
 
        penum->range.size = cmap->key_size; 
435
 
        penum->index = 1;
436
 
        return 0;
437
 
    }
438
 
    return 1;
439
 
}
440
 
 
441
 
static const gs_cmap_ranges_enum_procs_t gs_cmap_ToUnicode_range_procs = {
442
 
    gs_cmap_ToUnicode_next_range
443
 
};
444
 
 
445
 
static int
446
 
gs_cmap_ToUnicode_decode_next(const gs_cmap_t *pcmap, const gs_const_string *str,
447
 
                     uint *pindex, uint *pfidx,
448
 
                     gs_char *pchr, gs_glyph *pglyph)
449
 
{
450
 
    return_error(gs_error_unregistered);
451
 
}
452
 
 
453
 
static void
454
 
gs_cmap_ToUnicode_enum_ranges(const gs_cmap_t *pcmap, gs_cmap_ranges_enum_t *pre)
455
 
{
456
 
    gs_cmap_ranges_enum_setup(pre, pcmap, &gs_cmap_ToUnicode_range_procs);
457
 
}
458
 
 
459
 
static int
460
 
gs_cmap_ToUnicode_next_lookup(gs_cmap_lookups_enum_t *penum)
461
 
{   const gs_cmap_ToUnicode_t *cmap = (gs_cmap_ToUnicode_t *)penum->cmap;
462
 
    
463
 
    if (penum->index[0]++ > 0)
464
 
        return 1;
465
 
    penum->entry.value.data = penum->temp_value;
466
 
    penum->entry.value.size = gs_cmap_ToUnicode_code_bytes;
467
 
    penum->index[1] = 0;
468
 
    penum->entry.key_is_range = true;
469
 
    penum->entry.value_type = CODE_VALUE_CHARS;
470
 
    penum->entry.key_size = cmap->key_size;
471
 
    penum->entry.value.size = gs_cmap_ToUnicode_code_bytes;
472
 
    penum->entry.font_index = 0;
473
 
    return 0;
474
 
}
475
 
 
476
 
static int
477
 
gs_cmap_ToUnicode_next_entry(gs_cmap_lookups_enum_t *penum)
478
 
{   const gs_cmap_ToUnicode_t *cmap = (gs_cmap_ToUnicode_t *)penum->cmap;
479
 
    const uchar *map = cmap->glyph_name_data;
480
 
    const int num_codes = cmap->num_codes;
481
 
    uint index = penum->index[1], i, j;
482
 
    uchar c0, c1, c2;
483
 
 
484
 
    /* Warning : this hardcodes gs_cmap_ToUnicode_num_code_bytes = 2 */
485
 
    for (i = index; i < num_codes; i++)
486
 
        if (map[i + i + 0] != 0 || map[i + i + 1] != 0)
487
 
            break;
488
 
    if (i >= num_codes)
489
 
        return 1;
490
 
    c0 = map[i + i + 0];
491
 
    c1 = map[i + i + 1];
492
 
    for (j = i + 1, c2 = c1 + 1; j < num_codes; j++, c2++) {
493
 
        /* Due to PDF spec, *bfrange boundaries may differ 
494
 
           in the last byte only. */
495
 
        if (j % 256 == 0)
496
 
            break;
497
 
        if ((uchar)c2 == 0)
498
 
            break;
499
 
        if (map[j + j + 0] != c0 || map[j + j + 1] != c2)
500
 
            break;
501
 
    }
502
 
    penum->index[1] = j;
503
 
    penum->entry.key[0][0] = (uchar)(i >> 8);
504
 
    penum->entry.key[0][cmap->key_size - 1] = (uchar)(i & 0xFF);
505
 
    penum->entry.key[1][0] = (uchar)(j >> 8);
506
 
    penum->entry.key[1][cmap->key_size - 1] = (uchar)((j - 1) & 0xFF);
507
 
    memcpy(penum->temp_value, map + i * gs_cmap_ToUnicode_code_bytes, 
508
 
                        gs_cmap_ToUnicode_code_bytes);
509
 
    return 0;
510
 
}
511
 
 
512
 
static const gs_cmap_lookups_enum_procs_t gs_cmap_ToUnicode_lookup_procs = {
513
 
    gs_cmap_ToUnicode_next_lookup, gs_cmap_ToUnicode_next_entry
514
 
};
515
 
 
516
 
static void
517
 
gs_cmap_ToUnicode_enum_lookups(const gs_cmap_t *pcmap, int which,
518
 
                      gs_cmap_lookups_enum_t *pre)
519
 
{
520
 
    gs_cmap_lookups_enum_setup(pre, pcmap,
521
 
                               (which ? &gs_cmap_no_lookups_procs : /* fixme */
522
 
                                &gs_cmap_ToUnicode_lookup_procs));
523
 
}
524
 
 
525
 
static bool
526
 
gs_cmap_ToUnicode_is_identity(const gs_cmap_t *pcmap, int font_index_only)
527
 
{   const gs_cmap_ToUnicode_t *cmap = (gs_cmap_ToUnicode_t *)pcmap;
528
 
    return cmap->is_identity;
529
 
}
530
 
 
531
 
static const gs_cmap_procs_t gs_cmap_ToUnicode_procs = {
532
 
    gs_cmap_ToUnicode_decode_next,
533
 
    gs_cmap_ToUnicode_enum_ranges,
534
 
    gs_cmap_ToUnicode_enum_lookups,
535
 
    gs_cmap_ToUnicode_is_identity
536
 
};
537
 
 
538
 
/*
539
 
 * Allocate and initialize a ToUnicode CMap.
540
 
 */
541
 
int
542
 
gs_cmap_ToUnicode_alloc(gs_memory_t *mem, int id, int num_codes, int key_size, gs_cmap_t **ppcmap)
543
 
{   int code;
544
 
    uchar *map, *cmap_name = NULL;
545
 
    gs_cmap_ToUnicode_t *cmap;
546
 
    int name_len = 0;
547
 
#   if 0
548
 
        /* We don't write a CMap name to ToUnicode CMaps,
549
 
         * becsue (1) there is no conventional method for
550
 
         * generating them, and (2) Acrobat Reader ignores them.
551
 
         * But we'd like to keep this code until beta-testing completes,
552
 
         * and we ensure that other viewers do not need the names.
553
 
         */
554
 
        char sid[10], *pref = "aux-";
555
 
        int sid_len, pref_len = strlen(pref);
556
 
    
557
 
        sprintf(sid, "%d", id);
558
 
        sid_len = strlen(sid);
559
 
        name_len = pref_len + sid_len;
560
 
        cmap_name = gs_alloc_string(mem, name_len, "gs_cmap_ToUnicode_alloc");
561
 
        if (cmap_name == 0)
562
 
            return_error(gs_error_VMerror);
563
 
        memcpy(cmap_name, pref, pref_len);
564
 
        memcpy(cmap_name + pref_len, sid, sid_len);
565
 
#   endif
566
 
    code = gs_cmap_alloc(ppcmap, &st_cmap_ToUnicode,
567
 
              0, cmap_name, name_len, NULL, 0, &gs_cmap_ToUnicode_procs, mem);
568
 
    if (code < 0)
569
 
        return code;
570
 
    map = (uchar *)gs_alloc_bytes(mem, num_codes * gs_cmap_ToUnicode_code_bytes, 
571
 
                                  "gs_cmap_ToUnicode_alloc");
572
 
    if (map == NULL)
573
 
        return_error(gs_error_VMerror);
574
 
    memset(map, 0, num_codes * gs_cmap_ToUnicode_code_bytes);
575
 
    cmap = (gs_cmap_ToUnicode_t *)*ppcmap;
576
 
    cmap->glyph_name_data = map;
577
 
    cmap->CMapType = 2;
578
 
    cmap->num_fonts = 1;
579
 
    cmap->key_size = key_size;
580
 
    cmap->num_codes = num_codes;
581
 
    cmap->ToUnicode = true;
582
 
    cmap->is_identity = true;
583
 
    return 0;
584
 
}
585
 
 
586
 
/*
587
 
 * Write a code pair to ToUnicode CMap.
588
 
 */
589
 
void
590
 
gs_cmap_ToUnicode_add_pair(gs_cmap_t *pcmap, int code0, int code1)
591
 
{   gs_cmap_ToUnicode_t *cmap = (gs_cmap_ToUnicode_t *)pcmap;
592
 
    uchar *map = pcmap->glyph_name_data;
593
 
    const int num_codes = ((gs_cmap_ToUnicode_t *)pcmap)->num_codes;
594
 
    
595
 
    if (code0 >= num_codes)
596
 
        return; /* must not happen. */
597
 
    map[code0 * gs_cmap_ToUnicode_code_bytes + 0] = (uchar)(code1 >> 8);
598
 
    map[code0 * gs_cmap_ToUnicode_code_bytes + 1] = (uchar)(code1 & 0xFF);
599
 
    cmap->is_identity &= (code0 == code1);
600
 
}