Debian Codegraph @

texk/xdvipsk/luamap.c

raw
1 /*
2 * Loads a opentype font charcodes mapping file from Luatex.
3 */
4 #include "xdvips.h" /* The copyright notice in that file is included too! */
5 #ifdef KPATHSEA
6 #include <kpathsea/c-pathmx.h>
7 #include <kpathsea/concatn.h>
8 #include <kpathsea/variable.h>
9 #else
10 /* Be sure we have `isascii'. */
11 #ifndef WIN32
12 #if !(defined(HAVE_DECL_ISASCII) && HAVE_DECL_ISASCII)
13 #define isascii(c) (((c) & ~0x7f) == 0)
14 #endif
15 #endif
16 #endif
17 /*
18 * The external declarations:
19 */
20 #include "protos.h"
21 #include "mem.h"
22 #include "error.h"
23 #include "dpxfile.h"
24 #include "glyphmap.h"
25 #include "lua.h"
26 #include "lauxlib.h"
27 #include "lualib.h"
28 #include "unicode.h"
29
30 #define LUAMAP_CACHE_ALLOC_SIZE 16u
31 #define LUAMAP_DEBUG_STR "LuaMap"
32
33 struct LuaMap {
34 char *name;
35 luamaptype *luamap;
36 };
37
38 struct LuaMap_cache {
39 int num;
40 int max;
41 struct LuaMap **luamaps;
42 };
43
44 static struct LuaMap_cache *__cache = NULL;
45
46 #define CHECK_ID(n) do {\
47 if (! __cache)\
48 ERROR("%s: LuaMap cache not initialized.", LUAMAP_DEBUG_STR);\
49 if ((n) < 0 || (n) >= __cache->num)\
50 ERROR("Invalid LuaMap ID %d", (n));\
51 } while (0)
52
53 static const long hextable[] = {
54 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
55 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
56 -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, -1, 10, 11, 12, 13, 14, 15, -1,
57 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
58 -1, -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
59 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
60 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
61 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
62 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
63 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
64 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
65 };
66
67 static long hexdec(unsigned const char *hex) {
68 long ret = 0;
69 while (*hex && ret >= 0) {
70 ret = (ret << 4) | hextable[*hex++];
71 }
72 return ret;
73 }
74
75 static struct LuaMap *LuaMap_new()
76 {
77 struct LuaMap *map = NEW(1, struct LuaMap);
78 map->name = NULL;
79 map->luamap = NULL;
80 return map;
81 }
82
83 static int LuaMap_cache_add(const char* luamap_name, struct LuaMap** map_ptr)
84 {
85 int id = -1;
86 struct LuaMap* map = NULL;
87
88 assert(map_ptr);
89
90 if (__cache->num >= __cache->max)
91 {
92 __cache->max += LUAMAP_CACHE_ALLOC_SIZE;
93 __cache->luamaps = RENEW(__cache->luamaps, __cache->max, struct LuaMap*);
94 }
95 id = __cache->num;
96 (__cache->num)++;
97 map = __cache->luamaps[id] = LuaMap_new();
98
99 map->name = NEW(strlen(luamap_name) + 1, char);
100 strcpy(map->name, luamap_name);
101
102 *map_ptr = map;
103 return id;
104 }
105
106 static int LuaMap_parse(struct LuaMap *map, FILE *fp)
107 {
108 luamaptype *lmp;
109 int k, m, n, charcode, cid;
110 unsigned short gid;
111 char strbuf[512], str[5];
112 unsigned int *tounicode;
113 char seps[] = ",";
114 char *token;
115
116 while (fgets(strbuf, 512, fp) != NULL) {
117 token = strtok(strbuf, seps);
118 m = 0;
119 while (token != NULL) {
120 switch (m) {
121 case 0:
122 charcode = atoi(token);
123 break;
124 case 1:
125 gid = atoi(token);
126 break;
127 case 2:
128 if (token[strlen(token) - 1] == '\n')
129 token[strlen(token) - 1] = 0;
130 if (strcmp(token, "nil") == 0) {
131 n = 0;
132 tounicode = NEW(1, unsigned int);
133 }
134 else {
135 n = strlen(token) / 4;
136 if (strlen(token) % 4)
137 {
138 n++;
139 PRINTF_PR("Error: wrong format of the unicode %s in the map %s.\n", token, map->name);
140 }
141 tounicode = NEW(n, unsigned int);
142 for (k = 0; k < n; k++) {
143 memset(str, 0, 5);
144 strncpy(str, &token[k * 4], 4);
145 tounicode[k] = hexdec(str);
146 }
147 }
148 lmp = NEW(1, luamaptype);
149 lmp->charcode = (Otf_Enc_Type == enc_gid)? gid : charcode;
150 lmp->gid = gid;
151 lmp->tu_count = n;
152 lmp->tounicode = tounicode;
153 HASH_ADD_INT(map->luamap, gid, lmp);
154 break;
155 default:
156 break;
157 }
158 if (m < 2 ) {
159 token = strtok(NULL, seps);
160 m++;
161 }
162 else
163 break;
164 }
165 }
166
167 return 0;
168 }
169
170 static boolean is_private(unsigned long ucode)
171 {
172 return (((ucode >= 0xE000) && (ucode < 0xF900)) || /* Private Use Areas */
173 ((ucode >= 0xF0000) && (ucode < 0xFFFFE)) ||
174 ((ucode >= 0x100000) && (ucode < 0x10FFFE)));
175 }
176
177 unsigned int *uni32_2_utf16(unsigned long ucode, int *p_ucnt)
178 {
179 unsigned int *tounicode = NULL;
180 int ucnt = 0;
181
182 assert(p_ucnt);
183
184 if (ucode <= 0x10FFFF)
185 {
186 if (ucode >= 0x10000L)
187 {
188 ucnt = 2;
189 tounicode = NEW(ucnt, unsigned int);
190 assert(tounicode);
191 tounicode[0] = UTF32toUTF16HS(ucode);
192 tounicode[1] = UTF32toUTF16LS(ucode);
193 }
194 else
195 {
196 ucnt = 1;
197 tounicode = NEW(ucnt, unsigned int);
198 assert(tounicode);
199 tounicode[0] = ucode;
200 }
201 }
202
203 *p_ucnt = ucnt;
204 return (tounicode);
205 }
206
207 unsigned long *utf16_2_uni32(const unsigned int *tounicode, int ucnt, int *p_uni32_cnt)
208 {
209 unsigned long *uni32_arr = NULL;
210
211 assert(p_uni32_cnt);
212 assert(tounicode);
213
214 uni32_arr = NEW(ucnt, unsigned long);
215 assert(uni32_arr);
216 unsigned long uni32;
217 int ii, jj;
218
219 jj = 0;
220 for (ii = 0; ii < ucnt; ii++)
221 {
222 if ((tounicode[ii] & 0xFC00) == 0xD800)
223 uni32 = (tounicode[ii] & 0x3FF) << 10;
224 else if ((tounicode[ii] & 0xFC00) == 0xDC00)
225 {
226 uni32 = (uni32 | (tounicode[ii] & 0x3FF)) + 0x10000;
227 uni32_arr[jj++] = uni32;
228 }
229 else
230 uni32_arr[jj++] = tounicode[ii];
231 }
232
233 *p_uni32_cnt = jj;
234
235 return(uni32_arr);
236 }
237
238 void add_glyph_tounicode(
239 struct LuaMap *map,
240 FT_Face ft_face,
241 FT_UInt gid,
242 FT_ULong ucode,
243 boolean overwrite
244 )
245 {
246 unsigned int *tounicode = NULL;
247 char glyph_name[GLYPH_NAME_LEN + 1];
248 unsigned long *uni32_arr;
249 int ucnt, uni32_cnt;
250 int igid, ii;
251 luamaptype *lmp;
252 static char map_line[ENC_BUF_SIZE + 1];
253 FT_Error ft_error = FT_Err_Ok;
254
255 ft_error = FT_Get_Glyph_Name(ft_face, gid, glyph_name, GLYPH_NAME_LEN);
256 if (ft_error)
257 glyph_name[0] = '\0';
258
259 if (is_private(ucode) && glyph_name[0])
260 {
261 tounicode = get_glname_tounicode(glyph_name, map->name, "otf:", FALSE, &ucnt, NULL);
262 if (tounicode)
263 {
264 uni32_arr = utf16_2_uni32(tounicode, ucnt, &uni32_cnt);
265 if (uni32_arr)
266 {
267 for (ii = 0; ii < uni32_cnt; ii++)
268 if (is_private(uni32_arr[ii]))
269 {
270 RELEASE(tounicode);
271 tounicode = NULL;
272 break;
273 }
274 RELEASE(uni32_arr);
275 }
276 }
277 }
278
279 lmp = NULL;
280 igid = gid;
281 HASH_FIND_INT(map->luamap, &igid, lmp);
282 if (lmp)
283 {
284 if (overwrite)
285 {
286 RELEASE(lmp->tounicode);
287 }
288 else
289 lmp = NULL;
290 }
291 else
292 if (Otf_Enc_Type == enc_gid) /* no new entries for charcode encoded .dvi's */
293 {
294 lmp = NEW(1, luamaptype);
295 assert(lmp);
296 lmp->charcode = lmp->gid = gid;
297 HASH_ADD_INT(map->luamap, gid, lmp);
298 }
299
300 if (lmp)
301 {
302 if (tounicode)
303 {
304 lmp->tounicode = tounicode;
305 lmp->tu_count = ucnt;
306 }
307 else
308 {
309 lmp->tounicode = uni32_2_utf16(ucode, &ucnt);
310 lmp->tu_count = ucnt;
311 }
312
313 if (glyph_name[0])
314 {
315 strcpy(map_line, "load_touni({['otf:");
316 strncat(map_line, map->name, ENC_BUF_SIZE - strlen(map_line) - 20);
317 map_line[ENC_BUF_SIZE] = '\0';
318 strlwr(map_line);
319 strcat(map_line, "/");
320 strncat(map_line, glyph_name, ENC_BUF_SIZE - strlen(map_line) - 20);
321 map_line[ENC_BUF_SIZE] = '\0';
322 strcat(map_line, "'] = {");
323 for (ii = 0; ii < ucnt; ii++)
324 {
325 if (strlen(map_line) >= ENC_BUF_SIZE - 20)
326 break;
327 sprintf(map_line + strlen(map_line), "0x%04X, ", lmp->tounicode[ii]);
328 }
329 strcat(map_line, "}}, false, \'\')");
330 if (luaL_dostring(L, map_line))
331 {
332 PRINTF_PR("Error: Lua script %.500s execution error: %.500s.\n", map_line, lua_tostring(L, -1));
333 lua_pop(L, 1);
334 }
335 }
336 }
337 }
338
339 /* iterates through all unicode values of the font encoding and fills LuaMap */
340 static void LuaMap_ftget(struct LuaMap *map, FT_Face ft_face)
341 {
342 FT_UInt gid;
343 FT_ULong ucode;
344
345 gid = 0;
346 ucode = FT_Get_First_Char(ft_face, &gid);
347 while (TRUE)
348 {
349 add_glyph_tounicode(map, ft_face, gid, ucode, TRUE);
350
351 if (gid == 0)
352 break;
353 ucode = FT_Get_Next_Char(ft_face, ucode, &gid);
354 }
355
356 /* adding unicodeless gid's */
357 for (gid = 0; gid < ft_face->num_glyphs; gid++)
358 add_glyph_tounicode(map, ft_face, gid, 0x100000 + (gid & 0x7FFF), FALSE);
359 }
360
361 static void LuaMap_release(struct LuaMap *map)
362 {
363 luamaptype *current, *tmp;
364
365 if (map->name)
366 RELEASE(map->name);
367 HASH_ITER(hh, map->luamap, current, tmp) {
368 RELEASE(current->tounicode);
369 HASH_DEL(map->luamap, current); /* delete it (users advances to next) */
370 RELEASE(current); /* free it */
371 }
372 RELEASE(map);
373 }
374
375
376 void
377 LuaMap_cache_init (void)
378 {
379 static unsigned char range_min[2] = {0x00, 0x00};
380 static unsigned char range_max[2] = {0xff, 0xff};
381
382 if (__cache)
383 ERROR("%s: Already initialized.", LUAMAP_DEBUG_STR);
384
385 __cache = NEW(1, struct LuaMap_cache);
386
387 __cache->max = LUAMAP_CACHE_ALLOC_SIZE;
388 __cache->luamaps = NEW(__cache->max, struct LuaMap *);
389 __cache->num = 0;
390 }
391
392 luamaptype *
393 LuaMap_cache_get (int id)
394 {
395 CHECK_ID(id);
396 return __cache->luamaps[id]->luamap;
397 }
398
399 int
400 LuaMap_cache_find (const char *luamap_name)
401 {
402 int n, id = 0;
403 char strbuf[500], *p;
404 FILE *fp = NULL;
405 struct LuaMap* map = NULL;
406 resfont_ref* rfnt_ref = NULL;
407 struct resfont *rfnt = NULL;
408 FT_Error ft_error = FT_Err_Ok;
409 FT_Face ft_face = NULL;
410 char *otf_name = NULL;
411
412 if (!__cache)
413 LuaMap_cache_init();
414 ASSERT(__cache);
415
416 for (id = 0; id < __cache->num; id++) {
417 char *name = NULL;
418 /* CMapName may be undefined when processing usecmap. */
419 name = __cache->luamaps[id]->name;
420 if (name && strcmp(luamap_name, name) == 0) {
421 return id;
422 }
423 }
424
425 id = -1;
426 if (Otf_Enc_Type == enc_charcode)
427 {
428 if ((fulliname == 0) || (strlen(fulliname) == 0))
429 return -1;
430 strcpy(strbuf, fulliname);
431 p = strbuf + strlen(strbuf) - 1;
432 while (p > strbuf) {
433 if ((*p == '\\') || (*p == '/')) {
434 *(p + 1) = 0;
435 break;
436 }
437 p--;
438 }
439 if (p == strbuf)
440 *p = 0;
441 strcat(strbuf, OTFMAPFILEDIR);
442 strcat(strbuf, luamap_name);
443 strcat(strbuf, OTFENCDFILEEXT);
444 fp = DPXFOPEN(strbuf, DPX_RES_TYPE_TEXT);
445 if (fp)
446 {
447 id = LuaMap_cache_add(luamap_name, &map);
448
449 if (id >= 0)
450 if (LuaMap_parse(map, fp) < 0)
451 ERROR("%s: Parsing CMap file failed.", LUAMAP_DEBUG_STR);
452
453 DPXFCLOSE(fp);
454 }
455 }
456 if ((Otf_Enc_Type == enc_gid) || (charcode_otf_g2u && (id >= 0))) /* no new map for charcode encoded .dvi's */
457 {
458 rfnt_ref = lookup_v(luamap_name);
459 if (rfnt_ref)
460 {
461 rfnt = rfnt_ref->resfont_ptr;
462 if (rfnt && rfnt->Fontfile)
463 {
464 fp = lookup_font_file(rfnt->Fontfile, &otf_name, NULL);
465 if (fp)
466 fclose(fp);
467 if (otf_name)
468 {
469 ft_error = FT_New_Face(ft_lib, otf_name, rfnt->index, &ft_face);
470 if (!ft_error)
471 {
472 ft_error = FT_Select_Charmap(ft_face, FT_ENCODING_UNICODE);
473 /*
474 if (ft_error)
475 ft_error = FT_Select_Charmap(ft_face, FT_ENCODING_MS_UNICODE);
476 if (ft_error)
477 ft_error = FT_Select_Charmap(ft_face, FT_ENCODING_APPLE_UNICODE);
478 */
479 if (!ft_error)
480 {
481 if (id < 0) /* if (Otf_Enc_Type == enc_charcode) branch skipped, the map is not yet present */
482 id = LuaMap_cache_add(luamap_name, &map);
483
484 if (id >= 0)
485 LuaMap_ftget(map, ft_face);
486 }
487 else
488 WARN("%s: Opentype font charmap selection error.", LUAMAP_DEBUG_STR); /* TODO: ft_error evaluation */
489
490 FT_Done_Face(ft_face);
491 }
492 else
493 WARN("%s: Unable to open font %s", LUAMAP_DEBUG_STR, otf_name); /* TODO: ft_error evaluation */
494 free(otf_name);
495 otf_name = NULL;
496 }
497 else
498 WARN("%s: Unable to expand font file name %s", LUAMAP_DEBUG_STR, rfnt->Fontfile);
499 }
500 else
501 WARN("%s: Font object error: %s", LUAMAP_DEBUG_STR, luamap_name);
502 }
503 else
504 WARN("%s: Font object not found: %s", LUAMAP_DEBUG_STR, luamap_name);
505 }
506
507 return id;
508 }
509
510 void
511 LuaMap_cache_close (void)
512 {
513 if (__cache) {
514 int id;
515 for (id = 0; id < __cache->num; id++) {
516 LuaMap_release(__cache->luamaps[id]);
517 }
518 RELEASE(__cache->luamaps);
519 RELEASE(__cache);
520 __cache = NULL;
521 }
522 }