Debian Codegraph @

texk/web2c/luatexdir/font/writeenc.c

raw
1 /*
2
3 Copyright 1996-2006 Han The Thanh <[email protected]>
4 Copyright 2006-2011 Taco Hoekwater <[email protected]>
5
6 This file is part of LuaTeX.
7
8 LuaTeX is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2 of the License, or (at your
11 option) any later version.
12
13 LuaTeX is distributed in the hope that it will be useful, but WITHOUT
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
16 License for more details.
17
18 You should have received a copy of the GNU General Public License along
19 with LuaTeX; if not, see <http://www.gnu.org/licenses/>.
20
21 */
22
23 #include "ptexlib.h"
24
25 /*tex
26
27 All encoding entries go into AVL tree for fast search by name.
28
29 */
30
31 struct avl_table *fe_tree = NULL;
32
33 /* The AVL sort |fe_entry| into |fe_tree| by name. */
34
35 static int comp_fe_entry(const void *pa, const void *pb, void *p)
36 {
37 (void) p;
38 return strcmp(((const fe_entry *) pa)->name, ((const fe_entry *) pb)->name);
39 }
40
41 static fe_entry *new_fe_entry(void)
42 {
43 fe_entry *fe;
44 fe = xtalloc(1, fe_entry);
45 fe->name = NULL;
46 fe->fe_objnum = 0;
47 /*tex The encoding file is not yet read in. */
48 fe->glyph_names = NULL;
49 fe->tx_tree = NULL;
50 return fe;
51 }
52
53 static fe_entry *lookup_fe_entry(char *s)
54 {
55 fe_entry fe;
56 assert(s != NULL);
57 fe.name = s;
58 if (fe_tree == NULL) {
59 fe_tree = avl_create(comp_fe_entry, NULL, &avl_xallocator);
60 assert(fe_tree != NULL);
61 }
62 return (fe_entry *) avl_find(fe_tree, &fe);
63 }
64
65 static void register_fe_entry(fe_entry * fe)
66 {
67 void **aa;
68 if (fe_tree == NULL) {
69 fe_tree = avl_create(comp_fe_entry, NULL, &avl_xallocator);
70 assert(fe_tree != NULL);
71 }
72 assert(fe != NULL);
73 assert(fe->name != NULL);
74 /*tex The encoding is not yet registered. */
75 assert(lookup_fe_entry(fe->name) == NULL);
76 aa = avl_probe(fe_tree, fe);
77 assert(aa != NULL);
78 }
79
80 fe_entry *get_fe_entry(char *s)
81 {
82 fe_entry *fe;
83 char **gl;
84 if ((fe = lookup_fe_entry(s)) == NULL && (gl = load_enc_file(s)) != NULL) {
85 fe = new_fe_entry();
86 fe->name = s;
87 fe->glyph_names = gl;
88 register_fe_entry(fe);
89 }
90 return fe;
91 }
92
93 static void write_enc(PDF pdf, char **glyph_names, struct avl_table *tx_tree, int fe_objnum)
94 {
95 int i_old, *p;
96 struct avl_traverser t;
97 assert(glyph_names != NULL);
98 assert(tx_tree != NULL);
99 assert(fe_objnum != 0);
100 pdf_begin_obj(pdf, fe_objnum, OBJSTM_ALWAYS);
101 pdf_begin_dict(pdf);
102 pdf_dict_add_name(pdf, "Type", "Encoding");
103 pdf_add_name(pdf, "Differences");
104 pdf_begin_array(pdf);
105 avl_t_init(&t, tx_tree);
106 for (i_old = -2, p = (int *) avl_t_first(&t, tx_tree); p != NULL;
107 p = (int *) avl_t_next(&t)) {
108 if (*p == i_old + 1) {
109 pdf_add_name(pdf, glyph_names[*p]);
110 } else {
111 pdf_add_int(pdf, *p);
112 pdf_add_name(pdf, glyph_names[*p]);
113 }
114 i_old = *p;
115 }
116 pdf_end_array(pdf);
117 pdf_end_dict(pdf);
118 pdf_end_obj(pdf);
119 }
120
121 static void write_fontencoding(PDF pdf, fe_entry * fe)
122 {
123 assert(fe != NULL);
124 write_enc(pdf, fe->glyph_names, fe->tx_tree, fe->fe_objnum);
125 }
126
127 void write_fontencodings(PDF pdf)
128 {
129 fe_entry *fe;
130 struct avl_traverser t;
131 if (fe_tree == NULL)
132 return;
133 avl_t_init(&t, fe_tree);
134 for (fe = (fe_entry *) avl_t_first(&t, fe_tree); fe != NULL;
135 fe = (fe_entry *) avl_t_next(&t))
136 if (fe->fe_objnum != 0)
137 write_fontencoding(pdf, fe);
138 }
139
140 /*tex Cleaning up \unknown */
141
142 static void destroy_fe_entry(void *pa, void *pb)
143 {
144 fe_entry *p;
145 int i;
146 (void) pb;
147 p = (fe_entry *) pa;
148 xfree(p->name);
149 if (p->glyph_names != NULL)
150 for (i = 0; i < 256; i++)
151 if (p->glyph_names[i] != notdef)
152 xfree(p->glyph_names[i]);
153 xfree(p->glyph_names);
154 if (p->tx_tree != NULL)
155 avl_destroy(p->tx_tree,NULL);
156 xfree(p);
157 }
158
159 void enc_free(void)
160 {
161 if (fe_tree != NULL)
162 avl_destroy(fe_tree, destroy_fe_entry);
163 fe_tree = NULL;
164 }