summaryrefslogtreecommitdiff
path: root/Build/source/texk/web2c/pdftexdir/writeenc.c
blob: 5547e3f7cb00aa8b21f69f0957002bc33b8fca9b (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
/*
Copyright (c) 1996-2005 Han The Thanh, <thanh@pdftex.org>

This file is part of pdfTeX.

pdfTeX is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

pdfTeX is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with pdfTeX; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

$Id: writeenc.c,v 1.3 2005/12/26 14:20:13 hahe Exp hahe $

*/

#include "ptexlib.h"

/**********************************************************************/
/* All encoding entries go into AVL tree for fast search by name. */

struct avl_table *fe_tree = NULL;

/* AVL sort fe_entry into fe_tree by name */

static int comp_fe_entry(const void *pa, const void *pb, void *p)
{
    return strcmp(((const fe_entry *) pa)->name, ((const fe_entry *) pb)->name);
}

fe_entry *new_fe_entry()
{
    fe_entry *fe;
    fe = xtalloc(1, fe_entry);
    fe->name = NULL;
    fe->fe_objnum = 0;
    fe->glyph_names = NULL;     /* encoding file not yet read in */
    fe->tx_tree = NULL;
    return fe;
}

fe_entry *lookup_fe_entry(char *s)
{
    fe_entry fe;
    assert(s != NULL);
    fe.name = s;
    if (fe_tree == NULL) {
        fe_tree = avl_create(comp_fe_entry, NULL, &avl_xallocator);
        assert(fe_tree != NULL);
    }
    return (fe_entry *) avl_find(fe_tree, &fe);
}

void register_fe_entry(fe_entry * fe)
{
    void **aa;
    if (fe_tree == NULL) {
        fe_tree = avl_create(comp_fe_entry, NULL, &avl_xallocator);
        assert(fe_tree != NULL);
    }
    assert(fe != NULL);
    assert(fe->name != NULL);
    assert(lookup_fe_entry(fe->name) == NULL);  /* encoding not yet registered */
    aa = avl_probe(fe_tree, fe);
    assert(aa != NULL);
}

fe_entry *get_fe_entry(char *s)
{
    fe_entry *fe;
    if ((fe = lookup_fe_entry(s)) == NULL) {
        fe = new_fe_entry();
        fe->name = s;
        fe->glyph_names = load_enc_file(s);
        register_fe_entry(fe);
    }
    return fe;
}

/**********************************************************************/

void epdf_write_enc(char **glyph_names, integer fe_objnum)
{
    int i, i_old;
    assert(glyph_names != NULL);
    assert(fe_objnum != 0);
    pdfbegindict(fe_objnum, 1);
    pdf_puts("/Type /Encoding\n");
    pdf_printf("/Differences [");
    for (i = 0, i_old = -2; i < 256; i++)
        if (glyph_names[i] != notdef) {
            if (i == i_old + 1) /* no gap */
                pdf_printf("/%s", glyph_names[i]);
            else {
                if (i_old == -2)
                    pdf_printf("%i/%s", i, glyph_names[i]);
                else
                    pdf_printf(" %i/%s", i, glyph_names[i]);
            }
            i_old = i;
        }
    pdf_puts("]\n");
    pdfenddict();
}

void write_enc(char **glyph_names, struct avl_table *tx_tree, integer fe_objnum)
{
    int i_old, *p;
    struct avl_traverser t;
    assert(glyph_names != NULL);
    assert(tx_tree != NULL);
    assert(fe_objnum != 0);
    pdfbegindict(fe_objnum, 1);
    pdf_puts("/Type /Encoding\n");
    pdf_printf("/Differences [");
    avl_t_init(&t, tx_tree);
    for (i_old = -2, p = (int *) avl_t_first(&t, tx_tree); p != NULL;
         p = (int *) avl_t_next(&t)) {
        if (*p == i_old + 1)    /* no gap */
            pdf_printf("/%s", glyph_names[*p]);
        else {
            if (i_old == -2)
                pdf_printf("%i/%s", *p, glyph_names[*p]);
            else
                pdf_printf(" %i/%s", *p, glyph_names[*p]);
        }
        i_old = *p;
    }
    pdf_puts("]\n");
    pdfenddict();
}

void write_fontencoding(fe_entry * fe)
{
    assert(fe != NULL);
    write_enc(fe->glyph_names, fe->tx_tree, fe->fe_objnum);
}

void write_fontencodings()
{
    fe_entry *fe;
    struct avl_traverser t;
    if (fe_tree == NULL)
        return;
    avl_t_init(&t, fe_tree);
    for (fe = (fe_entry *) avl_t_first(&t, fe_tree); fe != NULL;
         fe = (fe_entry *) avl_t_next(&t))
        if (fe->fe_objnum != 0)
            write_fontencoding(fe);
}

/**********************************************************************/
/* cleaning up... */

static void destroy_fe_entry(void *pa, void *pb)
{
    fe_entry *p;
    int i;
    p = (fe_entry *) pa;
    xfree(p->name);
    if (p->glyph_names != NULL)
        for (i = 0; i < 256; i++)
            if (p->glyph_names[i] != notdef)
                xfree(p->glyph_names[i]);
    xfree(p->glyph_names);
    xfree(p);
}

void enc_free()
{
    if (fe_tree != NULL)
        avl_destroy(fe_tree, destroy_fe_entry);
    fe_tree = NULL;
}

/**********************************************************************/