summaryrefslogtreecommitdiff
path: root/src/pdflib/pdflib/p_encoding.c
blob: af2479290b7add2c031b4a166a33d6bae68601c4 (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
/*---------------------------------------------------------------------------*
 |              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: p_encoding.c,v 1.1 2008/10/17 06:11:49 scuri Exp $
 *
 * PDFlib encoding handling routines
 *
 */

#include "p_intern.h"
#include "p_font.h"

void
pdf__encoding_set_char(PDF *p, const char *encoding, int slot,
                       const char *glyphname, int uv)
{
    int enc;
    pdc_encodingvector *ev;
    char given;

    if (!encoding || !*encoding)
        pdc_error(p->pdc, PDC_E_ILLARG_EMPTY, "encoding", 0, 0, 0);

    if (slot < 0 || slot > 255)
        pdc_error(p->pdc, PDC_E_ILLARG_INT,
            "slot", pdc_errprintf(p->pdc, "%d", slot), 0, 0);

    if (uv < 0 || uv >= PDC_NUM_BMPVAL)
        pdc_error(p->pdc, PDC_E_ILLARG_INT,
            "uv", pdc_errprintf(p->pdc, "%d", uv), 0, 0);

    if (!glyphname || !*glyphname)
    {
        if (uv == 0)
            pdc_error(p->pdc, PDF_E_ENC_GLYPHORCODE, 0, 0, 0, 0);
    }

    for (enc = (int) pdc_invalidenc + 1; enc < (int) pdc_firstvarenc; enc++)
    {
        if (!strcmp(encoding, pdc_get_fixed_encoding_name((pdc_encoding) enc)))
            pdc_error(p->pdc, PDF_E_ENC_CANTCHANGE, encoding, 0, 0, 0);
    }

    if (uv == 0)
    {
        given = 1;
        uv = (int) pdc_insert_glyphname(p->pdc, glyphname);
    }
    else if (!glyphname || !*glyphname)
    {
        given = 0;
        glyphname = pdc_insert_unicode(p->pdc, (pdc_ushort) uv);
    }
    else
    {
        const char *reg_glyphname;
        pdc_ushort reg_uv;
        int retval;

        given = 1;
        reg_glyphname = pdc_unicode2glyphname(p->pdc, (pdc_ushort) uv);
        if (reg_glyphname)
        {
            if (strcmp(reg_glyphname, glyphname) &&
                p->debug[(int) 'F'] == pdc_true)
            {
                pdc_warning(p->pdc, PDF_E_ENC_BADGLYPH,
                    glyphname,
                    pdc_errprintf(p->pdc, "%04X", uv),
                    reg_glyphname, 0);
            }

            /* We take the registered name */
        }
        else
        {
            retval = pdc_glyphname2unicode(p->pdc, glyphname);
            if (retval > -1)
            {
                reg_uv = (pdc_ushort) retval;
                if (reg_uv && reg_uv != (pdc_ushort) uv &&
                    p->debug[(int) 'F'] == pdc_true)
                {
                    pdc_error(p->pdc, PDF_E_ENC_BADUNICODE,
                        pdc_errprintf(p->pdc, "%04X", uv), glyphname,
                        pdc_errprintf(p->pdc, "%04X", reg_uv), 0);
                }
            }

            /* We register the new glyph name and unicode value */
            pdc_register_glyphname(p->pdc, glyphname, (pdc_ushort) uv,
                                   pdc_false);
        }
    }

    /* search for a registered encoding */
    enc = pdc_find_encoding(p->pdc, encoding);

    /* not found */
    if (enc == pdc_invalidenc)
    {
        ev = pdc_new_encoding(p->pdc, encoding);
        ev->flags |= PDC_ENC_USER;
        ev->flags |= PDC_ENC_SETNAMES;
        ev->flags |= PDC_ENC_ALLOCCHARS;

        enc = pdc_insert_encoding_vector(p->pdc, ev);
    }

    /* encoding vector */
    ev = pdc_get_encoding_vector(p->pdc, (pdc_encoding)enc);
    if (!(ev->flags & PDC_ENC_USER))
    {
	pdc_error(p->pdc, PDF_E_ENC_CANTCHANGE, encoding, 0, 0, 0);
    }
    else if (ev->flags & PDC_ENC_USED)
    {
	pdc_error(p->pdc, PDF_E_ENC_INUSE, encoding, 0, 0, 0);
    }

    /* Free character name */
    if (ev->chars[slot] != NULL)
        pdc_free(p->pdc, ev->chars[slot]);

    /* Saving */
    ev->codes[slot] = (pdc_ushort) uv;
    if (glyphname != NULL)
        ev->chars[slot] = pdc_strdup(p->pdc, glyphname);
    ev->given[slot] = given;

    pdc_encoding_logg_protocol(p->pdc, ev);
}

pdc_encoding
pdf_get_hypertextencoding_param(PDF *p, int *codepage)
{
    if (p->hypertextencoding == pdc_invalidenc)
    {
        p->hypertextencoding = pdf_get_hypertextencoding(p, "auto",
            &p->hypertextcodepage, pdc_true);

        if (p->hypertextencoding == pdc_invalidenc)
            pdc_error(p->pdc, -1, 0, 0, 0, 0);
    }

    if (codepage)
        *codepage = p->hypertextcodepage;

    return p->hypertextencoding;
}

pdc_encoding
pdf_get_hypertextencoding(PDF *p, const char *encoding, int *codepage,
                          pdc_bool verbose)
{
    pdc_encoding enc = pdc_invalidenc;

    *codepage = 0;

    if (!*encoding)
    {
        enc = pdc_unicode;
    }
    else
    {
        {
            enc = pdc_get_encoding(p->pdc, encoding, codepage, verbose);
            if (enc < 0 && enc != pdc_invalidenc && enc != pdc_unicode)
            {
                pdc_set_errmsg(p->pdc, PDF_E_ENC_BADHYPTEXTENC, encoding,
                               0, 0, 0);
                enc = pdc_invalidenc;
            }
        }
    }

    return enc;
}