Debian Codegraph @

texk/web2c/luatexdir/font/subfont.c

raw
1 /*
2
3 Copyright 2005-2006 Han The Thanh <[email protected]>
4 Copyright 2006-2008 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 #include <string.h>
25
26 static struct avl_table *sfd_tree = NULL;
27
28 static unsigned char *sfd_buffer = NULL;
29 static int sfd_size = 0;
30 static int sfd_curbyte = 0;
31
32 #define SFD_BUF_SIZE SMALL_BUF_SIZE
33
34 #define sfd_close() xfclose(sfd_file, cur_file_name)
35 #define sfd_open(a) (sfd_file = fopen((char *)(a), FOPEN_RBIN_MODE))
36
37 #define sfd_read_file() readbinfile(sfd_file,&sfd_buffer,&sfd_size)
38 #define sfd_getchar() sfd_buffer[sfd_curbyte++]
39 #define sfd_eof() (sfd_curbyte>=sfd_size)
40
41 static FILE *sfd_file;
42 static char sfd_line[SFD_BUF_SIZE];
43
44 static subfont_entry *new_subfont_entry(void)
45 {
46 int i;
47 subfont_entry *subfont;
48 subfont = xtalloc(1, subfont_entry);
49 subfont->infix = NULL;
50 for (i = 0; i < 256; ++i) {
51 /*tex Unassigned: */
52 subfont->charcodes[i] = -1;
53 }
54 subfont->next = NULL;
55 return subfont;
56 }
57
58 static sfd_entry *new_sfd_entry(void)
59 {
60 sfd_entry *sfd;
61 sfd = xtalloc(1, sfd_entry);
62 sfd->name = NULL;
63 sfd->subfont = NULL;
64 return sfd;
65 }
66
67 static void destroy_sfd_entry(void *pa, void *pb)
68 {
69 subfont_entry *p, *q;
70 sfd_entry *sfd;
71 sfd = (sfd_entry *) pa;
72 (void) pb;
73 p = sfd->subfont;
74 while (p != NULL) {
75 q = p->next;
76 xfree(p->infix);
77 xfree(p);
78 p = q;
79 }
80 xfree(sfd->name);
81 }
82
83 static int comp_sfd_entry(const void *pa, const void *pb, void *p)
84 {
85 (void) p;
86 return strcmp(((const sfd_entry *) pa)->name, ((const sfd_entry *) pb)->name);
87 }
88
89 void sfd_free(void)
90 {
91 if (sfd_tree != NULL)
92 avl_destroy(sfd_tree, destroy_sfd_entry);
93 }
94
95 static void sfd_getline(boolean expect_eof)
96 {
97 char *p;
98 char c;
99 restart:
100 if (sfd_eof()) {
101 if (expect_eof) {
102 if (*sfd_line == '#')
103 *sfd_line = 10;
104 return;
105 } else
106 normal_error("sub font","unexpected end of file");
107 }
108 p = sfd_line;
109 do {
110 c = (char) sfd_getchar();
111 append_char_to_buf(c, p, sfd_line, SFD_BUF_SIZE);
112 } while (c != 10 && !sfd_eof());
113 append_eol(p, sfd_line, SFD_BUF_SIZE);
114 if (p - sfd_line < 2 || *sfd_line == '#')
115 goto restart;
116 }
117
118 static sfd_entry *read_sfd(char *sfd_name)
119 {
120 void **aa;
121 sfd_entry *sfd, tmp_sfd;
122 subfont_entry *sf;
123 char buf[SMALL_BUF_SIZE], *p;
124 long int i, j, k;
125 int n;
126 int callback_id = 0;
127 int file_opened = 0;
128 /*tex Check whether this |sfd| has been read: */
129 tmp_sfd.name = sfd_name;
130 if (sfd_tree == NULL) {
131 sfd_tree = avl_create(comp_sfd_entry, NULL, &avl_xallocator);
132 assert(sfd_tree != NULL);
133 }
134 sfd = (sfd_entry *) avl_find(sfd_tree, &tmp_sfd);
135 if (sfd != NULL)
136 return sfd;
137 xfree(sfd_buffer);
138 sfd_curbyte = 0;
139 sfd_size = 0;
140 cur_file_name = luatex_find_file(sfd_name, find_sfd_file_callback);
141 if (cur_file_name) {
142 callback_id = callback_defined(read_sfd_file_callback);
143 if (callback_id > 0) {
144 if (!(run_callback(callback_id, "S->bSd", cur_file_name, &file_opened, &sfd_buffer, &sfd_size) &&
145 file_opened && sfd_size > 0)) {
146 formatted_warning("ttf font","cannot open SFD file for reading '%s'", cur_file_name);
147 cur_file_name = NULL;
148 return NULL;
149 }
150 } else {
151 if (!sfd_open(cur_file_name)) {
152 formatted_warning("ttf font", "cannot open SFD file for reading '%s'", cur_file_name);
153 cur_file_name = NULL;
154 return NULL;
155 }
156 sfd_read_file();
157 sfd_close();
158 }
159 }
160 tex_printf("{");
161 tex_printf("%s", cur_file_name);
162 sfd = new_sfd_entry();
163 sfd->name = xstrdup(sfd_name);
164 while (!sfd_eof()) {
165 sfd_getline(true);
166 if (*sfd_line == 10) {
167 /*tex An empty line indicates |EOF|. */
168 break;
169 }
170 sf = new_subfont_entry();
171 sf->next = sfd->subfont;
172 sfd->subfont = sf;
173 sscanf(sfd_line, "%s %n", buf, &n);
174 sf->infix = xstrdup(buf);
175 /*tex Skip to the next word: */
176 p = sfd_line + n;
177 k = 0;
178 read_ranges:
179 for (;;) {
180 if (*p == '\\') {
181 /*tex Continue on the next line: */
182 sfd_getline(false);
183 p = sfd_line;
184 goto read_ranges;
185 } else if (*p == 0) {
186 /*tex End of subfont. */
187 break;
188 }
189 if (sscanf(p, " %li %n", &i, &n) == 0)
190 formatted_error("sub font","invalid token: %s", p);
191 p += n;
192 if (*p == ':') {
193 /*tex Variant: offset */
194 k = i;
195 p++;
196 } else if (*p == '_') {
197 /*tex Variant: range */
198 if (sscanf(p + 1, " %li %n", &j, &n) == 0)
199 formatted_error("sub font","invalid token: %s", p);
200 if (i > j || k + (j - i) > 255)
201 formatted_error("sub font","invalid range: %s", p);
202 while (i <= j)
203 sf->charcodes[k++] = i++;
204 p += n + 1;
205 } else {
206 /*tex Variant: codepoint */
207 sf->charcodes[k++] = i;
208 }
209 }
210 }
211 tex_printf("}");
212 aa = avl_probe(sfd_tree, sfd);
213 assert(aa != NULL);
214 return sfd;
215 }
216
217 boolean handle_subfont_fm(fm_entry * fm, int mode)
218 {
219 size_t l;
220 char *p, *q, *r;
221 sfd_entry *sfd;
222 subfont_entry *sf;
223 fm_entry *fm2;
224 char buf[SMALL_BUF_SIZE];
225 assert(fm->tfm_name != NULL);
226 p = fm->tfm_name;
227 /*tex Search for the first |@|. */
228 q = strchr(p, '@');
229 if (q == NULL)
230 return false;
231 /*tex Search for the second |@|: */
232 r = strchr(q + 1, '@');
233 if (r == NULL)
234 return false;
235 /*tex Check if prefix or sfd name is empty or the second |@| is not the last char yet. */
236 if (q <= p || r <= q + 1 || r - p != (int) strlen(p) - 1) {
237 return false;
238 }
239 /*tex The length of sfd name: */
240 l = (size_t) (r - (q + 1));
241 strncpy(buf, q + 1, l);
242 buf[l] = 0;
243 check_buf(strlen(buf) + 4, SMALL_BUF_SIZE);
244 strcat(buf, ".sfd");
245 sfd = read_sfd(buf);
246 if (sfd == NULL)
247 return false;
248 /*tex At this point we know fm is a subfont. */
249 set_subfont(fm);
250 xfree(fm->ps_name);
251 /*tex Set default values for |Pid| and |Eid|: */
252 if (fm->pid == -1) {
253 fm->pid = 3;
254 fm->eid = 1;
255 }
256 /*tex Calculate the length of base tfm name (prefix). */
257 l = (size_t) (q - p);
258 for (sf = sfd->subfont; sf != NULL; sf = sf->next) {
259 strncpy(buf, p, l);
260 buf[l] = 0;
261 strcat(buf, sf->infix);
262 fm2 = new_fm_entry();
263 fm2->tfm_name = xstrdup(buf);
264 fm2->ff_name = xstrdup(fm->ff_name);
265 fm2->type = fm->type;
266 fm2->pid = fm->pid;
267 fm2->eid = fm->eid;
268 fm2->subfont = sf;
269 /*tex Try to insert the entry. */
270 if (avl_do_entry(fm2, mode) != 0) {
271 /*tex And delete it if we failed. */
272 delete_fm_entry(fm2);
273 }
274 }
275 delete_fm_entry(fm);
276 return true;
277 }