summaryrefslogtreecommitdiff
path: root/src/pdflib/pdcore/pc_chartabs.c
blob: 3799d4518f55905dba06f3d95d2ad2b03fb9c601 (plain)
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
/*---------------------------------------------------------------------------*
 |              PDFlib - A library for generating PDF on the fly             |
 +---------------------------------------------------------------------------+
 | Copyright (c) 1997-2006 Thomas Merz and PDFlib GmbH. All rights reserved. |
 +---------------------------------------------------------------------------+
 |                                                                           |
 |    This software is subject to the PDFlib license. It is NOT in the       |
 |    public domain. Extended versions and commercial licenses are           |
 |    available, please check http://www.pdflib.com.                         |
 |                                                                           |
 *---------------------------------------------------------------------------*/

/* $Id: pc_chartabs.c,v 1.1 2008/10/17 06:10:43 scuri Exp $
 *
 * PDFlib routines for converting glyph or character names to Unicode
 * and vice versa
 *
 */

#define PC_CHARTABS_C

#include "pc_util.h"
#include "pc_chartabs.h"
#include "pc_ctype.h"


/* ---------------- general character search functions ------------------- */

/*
 * Binary search for list of codes in a pdc_glyph_tab array sorted by glyphname
 */
int
pdc_glyphname2codelist(const char *glyphname, const pdc_glyph_tab *glyphtab,
                       int tabsize, pdc_ushort *codelist)
{
    int lo = 0;
    int hi = glyphname ? tabsize : lo;
    int nv = 0;

    while (lo < hi)
    {
        int i = (lo + hi) / 2;
        int cmp = strcmp(glyphname, glyphtab[i].name);

        if (cmp == 0)
        {
            for (; i >= 1; i--)
            {
                if (strcmp(glyphname, glyphtab[i-1].name))
                    break;
            }
            for (; i < tabsize; i++)
            {
                if (strcmp(glyphname, glyphtab[i].name))
                    break;
                codelist[nv] = glyphtab[i].code;
                nv++;
            }
            return nv;
        }

        if (cmp < 0)
            hi = i;
        else
            lo = i + 1;
    }

    return nv;
}

/*
 * Binary search for code in a pdc_glyph_tab array sorted by glyphname
 */
int
pdc_glyphname2code(const char *glyphname, const pdc_glyph_tab *glyphtab,
                   int tabsize)
{
    int lo = 0;
    int hi = glyphname ? tabsize : lo;

    while (lo < hi)
    {
        int i = (lo + hi) / 2;
        int cmp = strcmp(glyphname, glyphtab[i].name);

        if (cmp == 0)
            return (int) glyphtab[i].code;

        if (cmp < 0)
            hi = i;
        else
            lo = i + 1;
    }

    return -1;
}

/*
 * Binary search for glyphname in a pdc_glyph_tab array sorted by code
 */
const char *
pdc_code2glyphname(pdc_ushort code, const pdc_glyph_tab *glyphtab, int tabsize)
{
    int lo = 0;
    int hi = tabsize;

    while (lo < hi)
    {
        int i = (lo + hi) / 2;

        if (code == glyphtab[i].code)
            return glyphtab[i].name;

        if (code < glyphtab[i].code)
            hi = i;
        else
            lo = i + 1;
    }

    return NULL;
}

/*
 * Binary search for list of codes in a pdc_code_map array sorted by source code
 */
int
pdc_code2codelist(pdc_core *pdc, pdc_ushort code,
                  const pdc_code_map *codemap, int tabsize,
                  pdc_ushort *codelist, int listsize)
{
    int lo = 0;
    int hi = tabsize;
    int nv = 0;

    while (lo < hi)
    {
        int i = (lo + hi) / 2;

        if (codemap[i].src == code)
        {
            for (; i >= 1; i--)
            {
                if (codemap[i-1].src != code)
                    break;
            }

            for (; i < tabsize; i++)
            {
                if (codemap[i].src != code)
                    break;

                if (nv >= listsize)
                    pdc_error(pdc, PDC_E_CONV_LIST_MEMOVERFLOW, 0, 0, 0, 0);

                codelist[nv] = codemap[i].dst;
                nv++;
            }

            return nv;
        }
        if (codemap[i].src > code)
            hi = i;
        else
            lo = i + 1;
    }

    return nv;
}

/*
 * Binary search for glyphname in a pdc_glyph_tab array sorted by glyphname
 * to get the static pointer for the glyphname.
 */
const char *
pdc_glyphname2glyphname(const char *glyphname,
                        const pdc_glyph_tab *glyphtab, int tabsize)
{
    int lo = 0;
    int hi = tabsize;

    while (lo < hi)
    {
        int i = (lo + hi) / 2;
        int cmp = strcmp(glyphname, glyphtab[i].name);

        if (cmp == 0)
            return glyphtab[i].name;

        if (cmp < 0)
            hi = i;
        else
            lo = i + 1;
    }

    return NULL;
}


/* ---------------- special character search functions ------------------- */

/*
 * Returns the Unicode value of a glyph name in Adobe Glyph List 1.2'.
 * If the name is not contained in AGL, -1 will be returned.
 */
int
pdc_adobe2unicode(const char *glyphname)
{
    return pdc_glyphname2code(glyphname, tab_agl2uni,
                             (sizeof (tab_agl2uni)) / (sizeof (pdc_glyph_tab)));
}

/*
 * Returns the name in AGL 1.2' or ZapfDingbats font,
 * which corresponds to the supplied Unicode value.
 * If the value doesn't have a corresponding glyph name,
 * NULL will be returned.
 * For control codes ".notdef" will be returned.
 * But this is not compatibel with AGL 2.0!
 */
const char *
pdc_unicode2adobe(pdc_ushort uv)
{
    const char *glyphname;

    /* AGL 1.2' glyphname */
    glyphname = pdc_code2glyphname(uv, tab_uni2agl,
                            (sizeof tab_uni2agl) / (sizeof (pdc_glyph_tab)));
    if (glyphname != NULL)
        return glyphname;

    /* C0 and C1 control characters.
     * They have never a graphical representation but are defined.
     */
    if (uv < PDC_UNICODE_SPACE ||
        (uv >= PDC_UNICODE_DELETE && uv < PDC_UNICODE_NBSP))
        return glyph__notdef;

    return NULL;
}

const char *
pdc_get_notdef_glyphname(void)
{
    return (char *) glyph__notdef;
}

/*
 * Returns the Unicode value of a ZapfDingbats glyph name.
 * If the name is not contained in the ZapfDingbats list
 * -1 will be returned.
 */
int
pdc_zadb2unicode(const char *glyphname)
{
    return pdc_glyphname2code(glyphname, tab_zadb2uni,
                          (sizeof (tab_zadb2uni)) / (sizeof (pdc_glyph_tab)));
}

/*
 * Returns the glyph name in the ZapfDingbats font which corresponds
 * to the supplied Unicode value. If the value doesn't have a
 * corresponding glyph name NULL will be returned.
 */
const char *
pdc_unicode2zadb(pdc_ushort uv)
{
    return pdc_code2glyphname(uv, tab_uni2zadb,
                             (sizeof tab_uni2zadb) / (sizeof (pdc_glyph_tab)));
}

/*
 * Returns the Unicode values of a glyph name in Adobe Glyph List 2.0
 * which is not contained in AGL-1.2'.
 *
 * The function presupposes that uvlist is an array of PDC_MAX_UVLIST.
 *
 * Return value is the number of Unicodes.
 */
int
pdc_newadobe2unicodelist(const char *glyphname, pdc_ushort *uvlist)
{
    return pdc_glyphname2codelist(glyphname, tab_diffagl2uni,
                           (sizeof tab_diffagl2uni) / (sizeof (pdc_glyph_tab)),
                           uvlist);
}

/*
 * Returns the glyph name in Adobe Glyph List 2.0
 * which is not contained in AGL-1.2' corresponding
 * to the supplied Unicode value. Ambiguous Unicode
 * values or glyph names are not supported!
 * If the value doesn't have a corresponding glyph name
 * NULL will be returned.
 */
const char *
pdc_unicode2newadobe(pdc_ushort uv)
{
    return pdc_code2glyphname(uv, tab_uni2diffagl,
                         (sizeof tab_uni2diffagl) / (sizeof (pdc_glyph_tab)));
}

/*
 * Returns the glyph name in Adobe Glyph List 2.0
 * which is not contained in AGL-1.2' and which matches
 * the supplied glyph name.
 * If no match is found NULL will be returned.
 */
const char *
pdc_get_newadobe_glyphname(const char *glyphname)
{
    return pdc_glyphname2glyphname(glyphname, tab_diffagl2uni,
                         (sizeof tab_diffagl2uni) / (sizeof (pdc_glyph_tab)));
}


/*
 * Returns the alternative Unicode value of a double-mapped
 * AGL-1.2 glyph name. If the name is not double-mapped,
 * -1 will be returned.
 */
int
pdc_glyphname2altunicode(const char *glyphname)
{
    return pdc_glyphname2code(glyphname, tab_double_mappping,
                   (sizeof (tab_double_mappping)) / (sizeof (pdc_glyph_tab)));
}

/*
 * Returns true if a character name is contained in pc_standard_latin_charset.
 * Otherwise false will be returned.
 */
pdc_bool
pdc_is_std_charname(const char *glyphname)
{
    int lo = 0;
    int hi = ((sizeof pc_standard_latin_charset) / (sizeof (char *)));

    if (glyphname)
    {
        while (lo < hi)
        {
            int i = (lo + hi) / 2;
            int cmp = strcmp(glyphname, pc_standard_latin_charset[i]);

            if (cmp == 0)
                return pdc_true;

            if (cmp < 0)
                hi = i;
            else
                lo = i + 1;
        }
    }

    return pdc_false;
}



/* -------------- special character mapping for Type1 fonts --------------- */

/*
 * Deletes a bit in a bit mask. The bit indicates that
 * the respective glyph name of AGL 2.0 is not available
 * in a PostScript font. The glyph name is used to avoid
 * ambiguities (see comment in pc_chartabs.h)
 *
 */

#define PDC_BIT_NBSP         (1L<<0)
#define PDC_BIT_SHY          (1L<<1)
#define PDC_BIT_MODMACRON    (1L<<2)
#define PDC_BIT_CAPDELTA     (1L<<3)
#define PDC_BIT_CAPOMEGA     (1L<<4)
#define PDC_BIT_DIVSLASH     (1L<<5)
#define PDC_BIT_BULLETOP     (1L<<6)
#define PDC_BIT_SMALLMU      (1L<<7)

void
pdc_delete_missingglyph_bit(pdc_ushort uv, pdc_ulong *bmask)
{
    switch(uv)
    {
        case PDC_UNICODE_NBSP:
        *bmask &= ~PDC_BIT_NBSP;
        return;

        case PDC_UNICODE_SHY:
        *bmask &= ~PDC_BIT_SHY;
        return;

        case PDC_UNICODE_MODMACRON:
        *bmask &= ~PDC_BIT_MODMACRON;
        return;

        case PDC_UNICODE_CAPDELTA:
        *bmask &= ~PDC_BIT_CAPDELTA;
        return;

        case PDC_UNICODE_CAPOMEGA:
        *bmask &= ~PDC_BIT_CAPOMEGA;
        return;

        case PDC_UNICODE_DIVSLASH:
        *bmask &= ~PDC_BIT_DIVSLASH;
        return;

        case PDC_UNICODE_BULLETOP:
        *bmask &= ~PDC_BIT_BULLETOP;
        return;

        case PDC_UNICODE_SMALLMU:
        *bmask &= ~PDC_BIT_SMALLMU;
        return;

        default:
        return;
    }
}

/*
 * Returnes an alternative Unicode value and/or glyph name for an
 * AGL 2.0 glyph name which is not available in a PostScript font.
 *
 */

pdc_ushort
pdc_get_alter_glyphname(pdc_ushort uv, pdc_ulong bmask, char **glyphname)
{
    switch(uv)
    {
        case PDC_UNICODE_NBSP:
        if (bmask & PDC_BIT_NBSP)
        {
            if (glyphname)
                *glyphname = (char *) glyph_space;
            return PDC_UNICODE_SPACE;
        }
        break;

        case PDC_UNICODE_SHY:
        if (bmask & PDC_BIT_SHY)
        {
            if (glyphname)
                *glyphname = (char *) glyph_hyphen;
            return PDC_UNICODE_HYPHEN;
        }
        break;

        case PDC_UNICODE_MODMACRON:
        if (bmask & PDC_BIT_MODMACRON)
        {
            if (glyphname)
                *glyphname = (char *) glyph_macron;
            return PDC_UNICODE_MACRON;
        }
        break;

        case PDC_UNICODE_CAPDELTA:
        if (bmask & PDC_BIT_CAPDELTA)
        {
            if (glyphname)
                *glyphname = (char *) glyph_Delta;
            return PDC_UNICODE_INCREMENT;
        }
        break;

        case PDC_UNICODE_CAPOMEGA:
        if (bmask & PDC_BIT_CAPOMEGA)
        {
            if (glyphname)
                *glyphname = (char *) glyph_Omega;
            return PDC_UNICODE_OHMSIGN;
        }
        break;

        case PDC_UNICODE_DIVSLASH:
        if (bmask & PDC_BIT_DIVSLASH)
        {
            if (glyphname)
                *glyphname = (char *) glyph_fraction;
            return PDC_UNICODE_FRACSLASH;
        }

        case PDC_UNICODE_BULLETOP:
        if (bmask & PDC_BIT_BULLETOP)
        {
            if (glyphname)
                *glyphname = (char *) glyph_periodcentered;
            return PDC_UNICODE_MIDDLEDOT;
        }

        case PDC_UNICODE_SMALLMU:
        if (bmask & PDC_BIT_SMALLMU)
        {
            if (glyphname)
                *glyphname = (char *) glyph_mu;
            return PDC_UNICODE_MICRO;
        }

        default:
        if (glyphname)
        {
            if (*glyphname == NULL)
                *glyphname = (char *) pdc_get_notdef_glyphname();
            return 0;
        }
    }

    return uv;
}

/*
 * Returns the Unicode value for a given string Unicode expression:
 *
 *    - Byte 1...255 -> U0001...U00FF
 *    - U+XXXXX
 *    - 0xXXXXX
 *    - HTML character reference without frame syntax &...;
 *
 * If no conversion is possible -1 will be returned.
 */
int
pdc_string2unicode(pdc_core *pdc, const char *text, int i_flags,
                   const pdc_keyconn *keyconn, pdc_bool verbose)
{
    int iz = PDC_KEY_NOTFOUND, usv = -1;
    pdc_bool seterr = pdc_false;
    int flags = PDC_INT_UNSIGNED;
    int i = 0;

    (void) verbose;

    /* single byte as Unicode value */
    if (strlen(text) == 1)
    {
        char c = text[0];
        usv = (pdc_byte) c;
    }
    else
    {
        /* keyword */
        if (keyconn)
        {
            if (i_flags & PDC_INT_CASESENS)
                iz = pdc_get_keycode(text, keyconn);
            else
                iz = pdc_get_keycode_ci(text, keyconn);
        }
        if (iz != PDC_KEY_NOTFOUND)
        {
            usv = iz;
        }
        else
        {
            /* Unicode value */
            if (!pdc_strincmp(text, "U+", 2))
            {
                flags |= PDC_INT_HEXADEC;
                i = 2;
            }
            if (!pdc_str2integer(&text[i], flags, &iz))
            {
                    seterr = pdc_true;
            }
            else if (iz >= PDC_NUM_UNIVAL ||
                     (iz >= PDC_UNICODE_MINHIGHSUR &&
                      iz <= PDC_UNICODE_MAXLOWSUR))
            {
                seterr = pdc_true;
            }
            else
            {
                usv = iz;
            }
        }
    }

    if (seterr)
    {
        pdc_set_errmsg(pdc, PDC_E_CONV_ILLUTF32, &text[i], 0, 0, 0);
        if (verbose)
            pdc_error(pdc, -1, 0, 0, 0, 0);
    }

    return usv;
}

/*
 * Returns true if Unicode character is a character relevant for line breaking
 *
 */
pdc_bool
pdc_is_linebreaking_relchar(pdc_ushort uv)
{
    switch (uv)
    {
        case PDC_UNICODE_HT:
        case PDC_UNICODE_LF:
        case PDC_UNICODE_VT:
        case PDC_UNICODE_FF:
        case PDC_UNICODE_CR:
        case PDC_UNICODE_NEL:
        case PDC_UNICODE_SHY:
        case PDC_UNICODE_LS:
        case PDC_UNICODE_PS:
        return pdc_true;
    }

    return pdc_false;
}