Debian Codegraph @

texk/web2c/luatexdir/luafontloader/src/luafflib.c

raw
1 /* luafflib.c
2
3 Copyright 2007-2010 Taco Hoekwater <[email protected]>
4
5 This file is part of LuaTeX.
6
7 LuaTeX is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2 of the License, or (at your
10 option) any later version.
11
12 LuaTeX is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15 License for more details.
16
17 You should have received a copy of the GNU General Public License along
18 with LuaTeX; if not, see <http://www.gnu.org/licenses/>. */
19
20 /**
21 * @desc Support interface for fontforge 20070607
22 * @version 1.0
23 * @author Taco Hoekwater
24 */
25
26 #include "pfaedit.h"
27 #include "ustring.h"
28 #include "lib/lib.h" /* web2c's lib, for recorder_record_input */
29
30 #include "ffdummies.h"
31 #include "splinefont.h"
32
33 #define FONT_METATABLE "fontloader.splinefont"
34 #define FONT_SUBFONT_METATABLE "fontloader.splinefont.subfont"
35 #define FONT_GLYPHS_METATABLE "fontloader.splinefont.glyphs"
36 #define FONT_GLYPH_METATABLE "fontloader.splinefont.glyph"
37
38 #define LUA_OTF_VERSION "0.5"
39
40 extern void normal_error(const char *t, const char *p);
41 extern void formatted_error(const char *t, const char *fmt, ...);
42
43 static const char *possub_type_enum[] = {
44 "null", "position", "pair", "substitution",
45 "alternate", "multiple", "ligature", "lcaret",
46 "kerning", "vkerning", "anchors", "contextpos",
47 "contextsub", "chainpos", "chainsub", "reversesub",
48 "max", "kernback", "vkernback", NULL
49 };
50
51 #define LAST_POSSUB_TYPE_ENUM 18
52
53 #define eight_nulls() NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
54
55 static const char *otf_lookup_type_enum[] = {
56 "gsub_start", "gsub_single", "gsub_multiple", "gsub_alternate",
57 "gsub_ligature", "gsub_context", "gsub_contextchain", NULL,
58 "gsub_reversecontextchain", NULL, NULL, NULL, NULL, NULL, NULL, NULL, /*0x00F */
59 eight_nulls(), eight_nulls(),
60 eight_nulls(), eight_nulls(),
61 eight_nulls(), eight_nulls(),
62 eight_nulls(), eight_nulls(),
63 eight_nulls(), eight_nulls(),
64 eight_nulls(), eight_nulls(),
65 eight_nulls(), eight_nulls(),
66 eight_nulls(), eight_nulls(),
67 eight_nulls(), eight_nulls(),
68 eight_nulls(), eight_nulls(),
69 eight_nulls(), eight_nulls(),
70 eight_nulls(), eight_nulls(),
71 eight_nulls(), eight_nulls(),
72 eight_nulls(), eight_nulls(),
73 eight_nulls(), eight_nulls(),
74 "gpos_start", "gpos_single", "gpos_pair", "gpos_cursive",
75 "gpos_mark2base", "gpos_mark2ligature", "gpos_mark2mark", "gpos_context",
76 "gpos_contextchain", NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 0x10F */
77 eight_nulls(), eight_nulls(),
78 eight_nulls(), eight_nulls(),
79 eight_nulls(), eight_nulls(),
80 eight_nulls(), eight_nulls(),
81 eight_nulls(), eight_nulls(),
82 eight_nulls(), eight_nulls(),
83 eight_nulls(), eight_nulls(),
84 eight_nulls(), eight_nulls(),
85 eight_nulls(), eight_nulls(),
86 eight_nulls(), eight_nulls(),
87 eight_nulls(), eight_nulls(),
88 eight_nulls(), eight_nulls(),
89 eight_nulls(), eight_nulls(),
90 eight_nulls(), eight_nulls(),
91 eight_nulls(), eight_nulls(),
92 };
93
94 static const char *anchor_type_enum[] = {
95 "mark", "basechar", "baselig", "basemark", "centry", "cexit", "max", NULL
96 };
97
98 #define MAX_ANCHOR_TYPE 7
99
100 static const char *anchorclass_type_enum[] = {
101 "mark", "mkmk", "curs", "mklg", NULL
102 };
103
104 static const char *glyph_class_enum[] = {
105 "automatic", "none", "base", "ligature", "mark", "component", NULL
106 };
107
108 static const char *ttfnames_enum[ttf_namemax] = {
109 "copyright", "family", "subfamily", "uniqueid",
110 "fullname", "version", "postscriptname", "trademark",
111 "manufacturer", "designer", "descriptor", "venderurl",
112 "designerurl", "license", "licenseurl", "idontknow",
113 "preffamilyname", "prefmodifiers", "compatfull", "sampletext",
114 "cidfindfontname", "wwsfamily", "wwssubfamily"
115 };
116
117 static const char *fpossub_format_enum[] = {
118 "glyphs", "class", "coverage", "reversecoverage", NULL
119 };
120
121 static const char *tex_type_enum[4] = { "unset", "text", "math", "mathext" };
122
123 /* has an offset of 1, ui_none = 0. */
124 static const char *uni_interp_enum[9] = {
125 "unset", "none", "adobe", "greek", "japanese",
126 "trad_chinese", "simp_chinese", "korean", "ams"
127 };
128
129 #define check_isfont(L,b) (SplineFont **)luaL_checkudata(L,b,FONT_METATABLE)
130 #define check_isglyph(L,b) (struct splinechar **)luaL_checkudata(L,b,FONT_GLYPH_METATABLE)
131
132 /* forward declarations */
133 static void handle_generic_pst(lua_State * L, struct generic_pst *pst);
134 static void handle_generic_fpst(lua_State * L, struct generic_fpst *fpst);
135 static void handle_kernclass(lua_State * L, struct kernclass *kerns, const char *name);
136 static void handle_splinefont(lua_State * L, struct splinefont *sf);
137 static void handle_kernpair(lua_State * L, struct kernpair *kp);
138 static void handle_liglist(lua_State * L, struct liglist *ligofme);
139 static void handle_anchorpoint(lua_State * L, struct anchorpoint *anchor);
140
141 static int is_userdata(lua_State *L, int b, const char *utype)
142 {
143 if (lua_type(L,b) == LUA_TUSERDATA) {
144 lua_getmetatable(L, b);
145 luaL_getmetatable(L, utype);
146 if (lua_compare(L, -2, -1, LUA_OPEQ)) {
147 lua_pop(L,2);
148 return 1;
149 }
150 lua_pop(L,2);
151 }
152 return 0;
153 }
154
155 static void lua_ff_pushfont(lua_State * L, SplineFont * sf)
156 {
157 SplineFont **a;
158 if (sf == NULL) {
159 lua_pushnil(L);
160 } else {
161 a = lua_newuserdata(L, sizeof(SplineFont *));
162 *a = sf;
163 luaL_getmetatable(L, FONT_METATABLE);
164 lua_setmetatable(L, -2);
165 }
166 return;
167 }
168
169 static void lua_ff_pushsubfont(lua_State * L, SplineFont * sf)
170 {
171 SplineFont **a;
172 if (sf == NULL) {
173 lua_pushnil(L);
174 } else {
175 a = lua_newuserdata(L, sizeof(SplineFont *));
176 *a = sf;
177 luaL_getmetatable(L, FONT_SUBFONT_METATABLE);
178 lua_setmetatable(L, -2);
179 }
180 return;
181 }
182
183 static void lua_ff_pushglyph(lua_State * L, struct splinechar *sc)
184 {
185 struct splinechar **a;
186 if (sc == NULL) {
187 lua_pushnil(L);
188 } else {
189 a = lua_newuserdata(L, sizeof(struct splinechar *));
190 *a = sc;
191 luaL_getmetatable(L, FONT_GLYPH_METATABLE);
192 lua_setmetatable(L, -2);
193 }
194 return;
195 }
196
197
198 static int ff_open(lua_State * L)
199 {
200 SplineFont *sf;
201 const char *fontname;
202 FILE *l;
203 char s[511];
204 size_t len;
205 int args, i;
206 int openflags = 1 + 4;
207 fontname = luaL_checkstring(L, 1);
208 /* test fontname for existance */
209 if ((l = fopen(fontname, "r"))) {
210 recorder_record_input(fontname);
211 fclose(l);
212 } else {
213 lua_pushnil(L);
214 lua_pushfstring(L, "font loading failed for %s (read error)\n", fontname);
215 return 2;
216 }
217 args = lua_gettop(L);
218 if (args >= 2 && (lua_type(L, 2) == LUA_TSTRING)) {
219 len = strlen(fontname);
220 if (*(fontname + len) != ')') {
221 /* possibly fails for embedded parens in the font name */
222 if (len == 0) {
223 snprintf(s, 511, "%s", fontname);
224 } else {
225 snprintf(s, 511, "%s(%s)", fontname, lua_tolstring(L, 2, &len));
226 }
227 }
228 } else {
229 snprintf(s, 511, "%s", fontname);
230 }
231 if (strlen(s) > 0) {
232 gww_error_count = 0;
233 sf = ReadSplineFont((char *) s, openflags);
234 if (sf == NULL) {
235 lua_pushnil(L);
236 lua_pushfstring(L, "font loading failed for %s\n", s);
237 if (gww_error_count > 0) {
238 for (i = 0; i < gww_error_count; i++) {
239 lua_pushstring(L, gww_errors[i]);
240 lua_concat(L, 2);
241 }
242 gwwv_errors_free();
243 }
244 } else {
245 FVAppend(_FontViewCreate(sf));
246 lua_ff_pushfont(L, sf);
247 if (gww_error_count > 0) {
248 lua_newtable(L);
249 for (i = 0; i < gww_error_count; i++) {
250 lua_pushstring(L, gww_errors[i]);
251 lua_rawseti(L, -2, (i + 1));
252 }
253 gwwv_errors_free();
254 } else {
255 lua_pushnil(L);
256 }
257 }
258 } else {
259 lua_pushnil(L);
260 lua_pushfstring(L, "font loading failed: empty string given\n", fontname);
261 }
262 return 2;
263 }
264
265 static int ff_close(lua_State * L)
266 {
267 SplineFont **sf;
268 /*fputs("ff_close called",stderr); */
269 sf = check_isfont(L, 1);
270 if (*sf != NULL) {
271 if ((*sf)->fv) { /* condition might be improved */
272 FontViewClose((*sf)->fv);
273 } else {
274 EncMapFree((*sf)->map);
275 SplineFontFree(*sf);
276 }
277 *sf = NULL;
278 }
279 return 0;
280 }
281
282 /*
283 static int notdef_loc(SplineFont * sf)
284 {
285 int k;
286 int l = -1;
287 for (k = 0; k < sf->glyphcnt; k++) {
288 if (sf->glyphs[k]) {
289 if (strcmp(sf->glyphs[k]->name, ".notdef") == 0) {
290 l = k;
291 }
292 }
293 }
294 if (l == -1)
295 l = sf->glyphcnt;
296 return l;
297 }
298 */
299
300 static int notdef_loc(SplineFont * sf)
301 {
302 int k;
303 for (k = 0; k < sf->glyphcnt; k++) {
304 if (sf->glyphs[k]) {
305 if (strcmp(sf->glyphs[k]->name, ".notdef") == 0) {
306 return k ;
307 }
308 }
309 }
310 return -1;
311 }
312
313 static int ff_apply_featurefile(lua_State * L)
314 {
315 SplineFont **sf;
316 char *fname;
317 sf = check_isfont(L, 1);
318 fname = xstrdup(luaL_checkstring(L, 2));
319 SFApplyFeatureFilename(*sf, fname);
320 recorder_record_input(fname);
321 free(fname);
322 if (gww_error_count > 0) {
323 int i;
324 lua_newtable(L);
325 for (i = 0; i < gww_error_count; i++) {
326 lua_pushstring(L, gww_errors[i]);
327 lua_rawseti(L, -2, (i + 1));
328 }
329 gwwv_errors_free();
330 } else {
331 lua_pushnil(L);
332 }
333 return 1;
334 }
335
336 static int ff_apply_afmfile(lua_State * L)
337 {
338 SplineFont **sf;
339 const char *fname;
340 sf = check_isfont(L, 1);
341 fname = luaL_checkstring(L, 2);
342 CheckAfmOfPostscript(*sf, fname, (*sf)->map);
343 if (gww_error_count > 0) {
344 int i;
345 lua_newtable(L);
346 for (i = 0; i < gww_error_count; i++) {
347 lua_pushstring(L, gww_errors[i]);
348 lua_rawseti(L, -2, (i + 1));
349 }
350 gwwv_errors_free();
351 } else {
352 lua_pushnil(L);
353 }
354 return 1;
355 }
356
357 static void dump_intfield(lua_State * L, const char *name, long int field)
358 {
359 lua_checkstack(L, 2);
360 lua_pushstring(L, name);
361 lua_pushinteger(L, field);
362 lua_rawset(L, -3);
363 }
364
365 static void dump_uintfield(lua_State * L, const char *name, unsigned int field)
366 {
367 lua_checkstack(L, 2);
368 lua_pushstring(L, name);
369 lua_pushinteger(L, field);
370 lua_rawset(L, -3);
371 }
372
373 static void dump_realfield(lua_State * L, const char *name, real field)
374 {
375 lua_checkstack(L, 2);
376 lua_pushstring(L, name);
377 lua_pushnumber(L, field);
378 lua_rawset(L, -3);
379 }
380
381 #define dump_cond_intfield(a,b,c) if ((c)!=0) { dump_intfield ((a),(b),(c)); }
382
383 static void dump_stringfield(lua_State * L, const char *name, const char *field)
384 {
385 lua_checkstack(L, 2);
386 lua_pushstring(L, name);
387 lua_pushstring(L, field);
388 lua_rawset(L, -3);
389 }
390
391 static void dump_char_ref(lua_State * L, struct splinechar *spchar)
392 {
393 lua_checkstack(L, 2);
394 lua_pushstring(L, "char");
395 lua_pushstring(L, spchar->name);
396 lua_rawset(L, -3);
397 }
398
399 static void dump_lstringfield(lua_State * L, const char *name, char *field, int len)
400 {
401 lua_checkstack(L, 2);
402 lua_pushstring(L, name);
403 lua_pushlstring(L, field, len);
404 lua_rawset(L, -3);
405 }
406
407 static void dump_enumfield(lua_State * L, const char *name, int fid, const char **fields)
408 {
409 lua_checkstack(L, 2);
410 lua_pushstring(L, name);
411 lua_pushstring(L, fields[fid]);
412 lua_rawset(L, -3);
413 }
414
415 static void dump_floatfield(lua_State * L, const char *name, double field)
416 {
417 lua_checkstack(L, 2);
418 lua_pushstring(L, name);
419 lua_pushnumber(L, field);
420 lua_rawset(L, -3);
421 }
422
423 static char tag_string[5] = { 0 };
424
425 static char *make_tag_string(unsigned int field)
426 {
427 tag_string[0] = (field & 0xFF000000) >> 24;
428 tag_string[1] = (field & 0x00FF0000) >> 16;
429 tag_string[2] = (field & 0x0000FF00) >> 8;
430 tag_string[3] = (field & 0x000000FF);
431 return (char *) tag_string;
432 }
433
434 static void dump_tag(lua_State * L, const char *name, unsigned int field)
435 {
436 lua_checkstack(L, 2);
437 lua_pushstring(L, name);
438 lua_pushlstring(L, make_tag_string(field), 4);
439 lua_rawset(L, -3);
440 }
441
442 static void dump_subtable_name(lua_State * L, const char *name, struct lookup_subtable *s)
443 {
444 /* this is likely a backref */
445 if (s == NULL)
446 return;
447 lua_checkstack(L, 2);
448 if (s->next == NULL) {
449 dump_stringfield(L, name, s->subtable_name);
450 } else {
451 /* can this really happen ? */
452 int i = 0;
453 lua_newtable(L);
454 while (s != NULL) {
455 lua_pushstring(L, s->subtable_name);
456 lua_rawseti(L, -2, ++i);
457 s = s->next;
458 }
459 lua_setfield(L, -2, name);
460 }
461 }
462
463 #define NESTED_TABLE(a,b,c) { \
464 int k = 1;\
465 next = b; \
466 while (next != NULL) { \
467 lua_checkstack(L,2); \
468 lua_pushinteger(L,k); k++; \
469 lua_createtable(L,0,c); \
470 a(L, next); \
471 lua_rawset(L,-3); \
472 next = next->next; \
473 } \
474 }
475
476 #define NESTED_TABLE_SF(a,b,c,d) { \
477 int k = 1; \
478 next = b; \
479 while (next != NULL) { \
480 lua_checkstack(L,2); \
481 lua_pushinteger(L,k); k++; \
482 lua_createtable(L,0,d); \
483 if (a(L, next, c)) \
484 lua_rawset(L,-3); \
485 else { \
486 lua_pop(L,2); \
487 } \
488 next = next->next; \
489 } \
490 }
491
492 static void do_handle_scriptlanglist(lua_State * L, struct scriptlanglist *sl)
493 {
494 int k;
495 dump_tag(L, "script", sl->script);
496
497 lua_checkstack(L, 3);
498 lua_newtable(L);
499 for (k = 0; k < MAX_LANG; k++) {
500 if (sl->langs[k] != 0) {
501 lua_pushinteger(L, (k + 1));
502 lua_pushstring(L, make_tag_string(sl->langs[k]));
503 lua_rawset(L, -3);
504 }
505 }
506
507 if (sl->lang_cnt >= MAX_LANG) {
508 for (k = MAX_LANG; k < sl->lang_cnt; k++) {
509 lua_pushinteger(L, (k + 1));
510 lua_pushstring(L, make_tag_string(sl->morelangs[k - MAX_LANG]));
511 lua_rawset(L, -3);
512 }
513 }
514 lua_setfield(L, -2, "langs");
515 }
516
517 static void handle_scriptlanglist(lua_State * L, struct scriptlanglist *sll)
518 {
519 struct scriptlanglist *next;
520 NESTED_TABLE(do_handle_scriptlanglist, sll, 4);
521 }
522
523 static void
524 do_handle_featurescriptlanglist(lua_State * L,
525 struct featurescriptlanglist *features)
526 {
527 dump_tag(L, "tag", features->featuretag);
528 lua_newtable(L);
529 handle_scriptlanglist(L, features->scripts);
530 lua_setfield(L, -2, "scripts");
531 }
532
533 static void
534 handle_featurescriptlanglist(lua_State * L,
535 struct featurescriptlanglist *features)
536 {
537 struct featurescriptlanglist *next;
538 NESTED_TABLE(do_handle_featurescriptlanglist, features, 3);
539 }
540
541 static void do_handle_lookup_subtable(lua_State * L, struct lookup_subtable *subtable)
542 {
543
544 dump_stringfield(L, "name", subtable->subtable_name);
545 dump_stringfield(L, "suffix", subtable->suffix);
546
547 /* struct otlookup *lookup; *//* this is the parent */
548
549 /* dump_intfield (L,"unused", subtable->unused); */
550 /* The next one is true if there is no fpst, false otherwise */
551 /*
552 dump_intfield (L,"per_glyph_pst_or_kern",subtable->per_glyph_pst_or_kern);
553 */
554 dump_cond_intfield(L, "anchor_classes", subtable->anchor_classes);
555 dump_cond_intfield(L, "vertical_kerning", subtable->vertical_kerning);
556
557 if (subtable->kc != NULL) {
558 lua_newtable(L);
559 handle_kernclass(L, subtable->kc, subtable->subtable_name);
560 lua_setfield(L, -2, "kernclass");
561 }
562 #if 0
563 if (subtable->fpst != NULL) {
564 /* lua_newtable(L); */
565 handle_generic_fpst(L, subtable->fpst);
566 /* lua_setfield(L,-2, "fpst"); */
567 }
568 #endif
569
570 /* int subtable_offset; *//* used by OTF file generation */
571 /* int32 *extra_subtables; *//* used by OTF file generation */
572 }
573
574 static void handle_lookup_subtable(lua_State * L, struct lookup_subtable *subtable)
575 {
576 struct lookup_subtable *next;
577 NESTED_TABLE(do_handle_lookup_subtable, subtable, 2);
578 }
579
580 static int do_handle_lookup(lua_State * L, struct otlookup *lookup, SplineFont * sf)
581 {
582 int mc;
583
584 dump_enumfield(L, "type", lookup->lookup_type, otf_lookup_type_enum);
585
586 lua_newtable(L);
587 if (lookup->lookup_flags & pst_r2l) {
588 lua_pushstring(L, "r2l");
589 lua_pushboolean(L, 1);
590 lua_rawset(L, -3);
591 }
592 if (lookup->lookup_flags & pst_ignorebaseglyphs) {
593 lua_pushstring(L, "ignorebaseglyphs");
594 lua_pushboolean(L, 1);
595 lua_rawset(L, -3);
596 }
597 if (lookup->lookup_flags & pst_ignoreligatures) {
598 lua_pushstring(L, "ignoreligatures");
599 lua_pushboolean(L, 1);
600 lua_rawset(L, -3);
601 }
602 if (lookup->lookup_flags & pst_ignorecombiningmarks) {
603 lua_pushstring(L, "ignorecombiningmarks");
604 lua_pushboolean(L, 1);
605 lua_rawset(L, -3);
606 }
607 mc = (lookup->lookup_flags >> 8);
608 if (mc > 0 && mc < sf->mark_class_cnt && sf->mark_class_names[mc] != NULL) {
609 lua_pushstring(L, "mark_class");
610 lua_pushstring(L, sf->mark_class_names[mc]);
611 lua_rawset(L, -3);
612 }
613 lua_setfield(L, -2, "flags");
614
615 dump_stringfield(L, "name", lookup->lookup_name);
616
617 if (lookup->features != NULL) {
618 lua_newtable(L);
619 handle_featurescriptlanglist(L, lookup->features);
620 lua_setfield(L, -2, "features");
621 }
622
623 if (lookup->subtables != NULL) {
624 lua_newtable(L);
625 handle_lookup_subtable(L, lookup->subtables);
626 lua_setfield(L, -2, "subtables");
627 }
628
629 /* dump_intfield (L,"unused", lookup->unused); */
630 /* dump_intfield (L,"empty", lookup->empty); */
631 /* dump_intfield (L,"store_in_afm", lookup->store_in_afm); */
632 /* dump_intfield (L,"needs_extension", lookup->needs_extension); */
633 /* dump_intfield (L,"temporary_kern", lookup->temporary_kern); */
634 /* dump_intfield (L,"def_lang_checked", lookup->def_lang_checked); */
635 /* dump_intfield (L,"def_lang_found", lookup->def_lang_found); */
636 /* dump_intfield (L,"ticked", lookup->ticked); */
637 /* dump_intfield (L,"subcnt", lookup->subcnt); */
638 /* dump_intfield (L,"lookup_index", lookup->lookup_index); *//* identical to array index */
639 /* dump_intfield (L,"lookup_offset", lookup->lookup_offset); */
640 /* dump_intfield (L,"lookup_length", lookup->lookup_length); */
641 /* dump_stringfield(L,"tempname", lookup->tempname); */
642 return 1;
643 }
644
645 static void handle_lookup(lua_State * L, struct otlookup *lookup, SplineFont * sf)
646 {
647 struct otlookup *next;
648 NESTED_TABLE_SF(do_handle_lookup, lookup, sf, 18); /* 18 is a guess */
649 }
650
651 static void do_handle_kernpair(lua_State * L, struct kernpair *kp)
652 {
653
654 if (kp->sc != NULL)
655 dump_char_ref(L, kp->sc);
656 dump_intfield(L, "off", kp->off);
657 /* uint16 kcid; *//* temporary value */
658 dump_subtable_name(L, "lookup", kp->subtable);
659 }
660
661 static void handle_kernpair(lua_State * L, struct kernpair *kp)
662 {
663 struct kernpair *next;
664 NESTED_TABLE(do_handle_kernpair, kp, 4);
665 }
666
667 static void handle_splinecharlist(lua_State * L, struct splinecharlist *scl)
668 {
669
670 struct splinecharlist *next = scl;
671 int k = 1;
672 lua_checkstack(L, 10);
673 while (next != NULL) {
674 if (next->sc != NULL) {
675 lua_pushinteger(L, k);
676 k++;
677 lua_pushstring(L, next->sc->name);
678 lua_rawset(L, -3);
679 }
680 next = next->next;
681 }
682 }
683
684 /* vs is the "variation selector" a unicode codepoint which modifieds */
685 /* the code point before it. If vs is -1 then unienc is just an */
686 /* alternate encoding (greek Alpha and latin A), but if vs is one */
687 /* of unicode's variation selectors then this glyph is somehow a */
688 /* variant shape. The specifics depend on the selector and script */
689 /* fid is currently unused, but may, someday, be used to do ttcs */
690 /* NOTE: GlyphInfo displays vs==-1 as vs==0, and fixes things up */
691
692 static int handle_altuni(lua_State * L, struct altuni *au)
693 {
694 struct altuni *next = au;
695 int i = 0;
696 int k = 1;
697 lua_checkstack(L, 3);
698 while (next != NULL) {
699 if (next->unienc<0x10FFFF) {
700 lua_newtable(L);
701 dump_intfield(L, "unicode", next->unienc);
702 i++;
703 if (next->vs != -1) {
704 dump_intfield(L, "variant", next->vs);
705 /* dump_intfield(L, "fid", next->fid); */
706 }
707 lua_rawseti(L, -2, k++);
708 } else {
709 printf("ignoring variant %i %i\n",next->unienc,next->vs);
710 }
711 next = next->next;
712 }
713 return i;
714 }
715
716 #define interesting_vr(a) (((a)->xoff!=0) || ((a)->yoff!=0) || ((a)->h_adv_off!=0) || ((a)->v_adv_off!=0))
717
718 static void handle_vr(lua_State * L, struct vr *pos)
719 {
720
721 dump_cond_intfield(L, "x", pos->xoff);
722 dump_cond_intfield(L, "y", pos->yoff);
723 dump_cond_intfield(L, "h", pos->h_adv_off);
724 dump_cond_intfield(L, "v", pos->v_adv_off);
725
726 }
727
728 static void do_handle_generic_pst(lua_State * L, struct generic_pst *pst)
729 {
730 int k;
731 if (pst->type > LAST_POSSUB_TYPE_ENUM) {
732 dump_tag(L, "type", pst->type);
733 } else {
734 dump_enumfield(L, "type", pst->type, possub_type_enum);
735 }
736 /* unsigned int ticked: 1; */
737 /* unsigned int temporary: 1; *//* Used in afm ligature closure */
738 /* struct lookup_subtable *subtable; *//* handled by caller */
739
740 lua_checkstack(L, 4);
741 lua_pushstring(L, "specification");
742 lua_createtable(L, 0, 4);
743 if (pst->type == pst_position) {
744 handle_vr(L, &pst->u.pos);
745 } else if (pst->type == pst_pair) {
746 dump_stringfield(L, "paired", pst->u.pair.paired);
747 if (pst->u.pair.vr != NULL) {
748 lua_pushstring(L, "offsets");
749 lua_createtable(L, 2, 0);
750 if (interesting_vr(pst->u.pair.vr)) {
751 lua_createtable(L, 0, 4);
752 handle_vr(L, pst->u.pair.vr);
753 lua_rawseti(L, -2, 1);
754 }
755 if (interesting_vr(pst->u.pair.vr + 1)) {
756 lua_createtable(L, 0, 4);
757 handle_vr(L, pst->u.pair.vr + 1);
758 lua_rawseti(L, -2, 2);
759 }
760 lua_rawset(L, -3);
761 }
762 } else if (pst->type == pst_substitution) {
763 dump_stringfield(L, "variant", pst->u.subs.variant);
764 } else if (pst->type == pst_alternate) {
765 dump_stringfield(L, "components", pst->u.mult.components);
766 } else if (pst->type == pst_multiple) {
767 dump_stringfield(L, "components", pst->u.alt.components);
768 } else if (pst->type == pst_ligature) {
769 dump_stringfield(L, "components", pst->u.lig.components);
770 if (pst->u.lig.lig != NULL) {
771 dump_char_ref(L, pst->u.lig.lig);
772 }
773 } else if (pst->type == pst_lcaret) {
774 for (k = 0; k < pst->u.lcaret.cnt; k++) {
775 lua_pushinteger(L, (k + 1));
776 lua_pushinteger(L, pst->u.lcaret.carets[k]);
777 lua_rawset(L, -3);
778 }
779 }
780 lua_rawset(L, -3);
781 }
782
783 static void handle_generic_pst(lua_State * L, struct generic_pst *pst)
784 {
785 struct generic_pst *next;
786 int k;
787 int l = 1;
788 next = pst;
789 /* most likely everything arrives in proper order. But to prevent
790 * surprises, better do this is the proper way
791 */
792 while (next != NULL) {
793 if (next->subtable != NULL && next->subtable->subtable_name != NULL) {
794 lua_checkstack(L, 3); /* just in case */
795 lua_getfield(L, -1, next->subtable->subtable_name);
796 if (!lua_istable(L, -1)) {
797 lua_pop(L, 1);
798 lua_newtable(L);
799 lua_setfield(L, -2, next->subtable->subtable_name);
800 lua_getfield(L, -1, next->subtable->subtable_name);
801 }
802 k = lua_rawlen(L, -1) + 1;
803 lua_pushinteger(L, k);
804 lua_createtable(L, 0, 4);
805 do_handle_generic_pst(L, next);
806 lua_rawset(L, -3);
807 next = next->next;
808 lua_pop(L, 1); /* pop the subtable */
809 } else {
810 /* Found a pst without subtable, or without subtable name */
811 lua_pushinteger(L, l);
812 l++;
813 lua_createtable(L, 0, 4);
814 do_handle_generic_pst(L, next);
815 lua_rawset(L, -3);
816 next = next->next;
817 }
818 }
819 }
820
821 static void do_handle_liglist(lua_State * L, struct liglist *ligofme)
822 {
823 lua_checkstack(L, 2);
824 if (ligofme->lig != NULL) {
825 lua_createtable(L, 0, 6);
826 handle_generic_pst(L, ligofme->lig);
827 lua_setfield(L, -2, "lig");
828 }
829 dump_char_ref(L, ligofme->first);
830 if (ligofme->components != NULL) {
831 lua_newtable(L);
832 handle_splinecharlist(L, ligofme->components);
833 lua_setfield(L, -2, "components");
834 }
835 dump_intfield(L, "ccnt", ligofme->ccnt);
836 }
837
838
839 static void handle_liglist(lua_State * L, struct liglist *ligofme)
840 {
841 struct liglist *next;
842 NESTED_TABLE(do_handle_liglist, ligofme, 3);
843 }
844
845 static void do_handle_anchorpoint(lua_State * L, struct anchorpoint *anchor)
846 {
847
848 if (anchor->anchor == NULL) {
849 return;
850 }
851 if (anchor->type >= 0 && anchor->type <= MAX_ANCHOR_TYPE) {
852 lua_pushstring(L, anchor_type_enum[anchor->type]);
853 } else {
854 lua_pushstring(L, "Anchorpoint has an unknown type!");
855 lua_error(L);
856 }
857 /* unsigned int selected: 1; */
858 /* unsigned int ticked: 1; */
859
860 lua_rawget(L, -2);
861 if (!lua_istable(L, -1)) {
862 /* create the table first */
863 lua_pop(L, 1);
864 lua_pushstring(L, anchor_type_enum[anchor->type]);
865 lua_pushvalue(L, -1);
866 lua_newtable(L);
867 lua_rawset(L, -4);
868 lua_rawget(L, -2);
869 }
870 /* now the 'type' table is top of stack */
871 if (anchor->type == at_baselig) {
872 lua_pushstring(L, anchor->anchor->name);
873 lua_rawget(L, -2);
874 if (!lua_istable(L, -1)) {
875 /* create the table first */
876 lua_pop(L, 1);
877 lua_pushstring(L, anchor->anchor->name);
878 lua_pushvalue(L, -1);
879 lua_newtable(L);
880 lua_rawset(L, -4);
881 lua_rawget(L, -2);
882 }
883 lua_newtable(L);
884 dump_intfield(L, "x", anchor->me.x);
885 dump_intfield(L, "y", anchor->me.y);
886 if (anchor->has_ttf_pt)
887 dump_intfield(L, "ttf_pt_index", anchor->ttf_pt_index);
888 dump_intfield(L, "lig_index", anchor->lig_index);
889 lua_rawseti(L, -2, (anchor->lig_index + 1));
890 lua_pop(L, 1);
891 } else {
892 lua_pushstring(L, anchor->anchor->name);
893 lua_newtable(L);
894 dump_intfield(L, "x", anchor->me.x);
895 dump_intfield(L, "y", anchor->me.y);
896 if (anchor->has_ttf_pt)
897 dump_intfield(L, "ttf_pt_index", anchor->ttf_pt_index);
898 dump_intfield(L, "lig_index", anchor->lig_index);
899 lua_rawset(L, -3);
900 }
901 lua_pop(L, 1);
902 }
903
904 static void handle_anchorpoint(lua_State * L, struct anchorpoint *anchor)
905 {
906 struct anchorpoint *next;
907 next = anchor;
908 while (next != NULL) {
909 do_handle_anchorpoint(L, next);
910 next = next->next;
911 }
912 }
913
914 static void handle_glyphvariants(lua_State * L, struct glyphvariants *vars)
915 {
916 int i;
917 dump_stringfield(L, "variants", vars->variants);
918 dump_intfield(L, "italic_correction", vars->italic_correction);
919 lua_newtable(L);
920 for (i = 0; i < vars->part_cnt; i++) {
921 lua_newtable(L);
922 dump_stringfield(L, "component", vars->parts[i].component);
923 dump_intfield(L, "extender", vars->parts[i].is_extender);
924 dump_intfield(L, "start", vars->parts[i].startConnectorLength);
925 dump_intfield(L, "end", vars->parts[i].endConnectorLength);
926 dump_intfield(L, "advance", vars->parts[i].fullAdvance);
927 lua_rawseti(L, -2, (i + 1));
928 }
929 lua_setfield(L, -2, "parts");
930 }
931
932 static void handle_mathkernvertex(lua_State * L, struct mathkernvertex *mkv)
933 {
934 int i;
935 for (i = 0; i < mkv->cnt; i++) {
936 lua_newtable(L);
937 dump_intfield(L, "height", mkv->mkd[i].height);
938 dump_intfield(L, "kern", mkv->mkd[i].kern);
939 lua_rawseti(L, -2, (i + 1));
940 }
941 }
942
943 static void handle_mathkern(lua_State * L, struct mathkern *mk)
944 {
945 lua_newtable(L);
946 handle_mathkernvertex(L, &(mk->top_right));
947 lua_setfield(L, -2, "top_right");
948 lua_newtable(L);
949 handle_mathkernvertex(L, &(mk->top_left));
950 lua_setfield(L, -2, "top_left");
951 lua_newtable(L);
952 handle_mathkernvertex(L, &(mk->bottom_right));
953 lua_setfield(L, -2, "bottom_right");
954 lua_newtable(L);
955 handle_mathkernvertex(L, &(mk->bottom_left));
956 lua_setfield(L, -2, "bottom_left");
957 }
958
959 static void handle_splinechar(lua_State * L, struct splinechar *glyph, int hasvmetrics)
960 {
961 DBounds bb;
962 if (glyph->xmax == 0 && glyph->ymax == 0 && glyph->xmin == 0
963 && glyph->ymin == 0) {
964 SplineCharFindBounds(glyph, &bb);
965 glyph->xmin = bb.minx;
966 glyph->ymin = bb.miny;
967 glyph->xmax = bb.maxx;
968 glyph->ymax = bb.maxy;
969 }
970 dump_stringfield(L, "name", glyph->name);
971 dump_intfield(L, "unicode", glyph->unicodeenc);
972 lua_createtable(L, 4, 0);
973 lua_pushinteger(L, 1);
974 lua_pushinteger(L, glyph->xmin);
975 lua_rawset(L, -3);
976 lua_pushinteger(L, 2);
977 lua_pushinteger(L, glyph->ymin);
978 lua_rawset(L, -3);
979 lua_pushinteger(L, 3);
980 lua_pushinteger(L, glyph->xmax);
981 lua_rawset(L, -3);
982 lua_pushinteger(L, 4);
983 lua_pushinteger(L, glyph->ymax);
984 lua_rawset(L, -3);
985 lua_setfield(L, -2, "boundingbox");
986 if (hasvmetrics) {
987 dump_intfield(L, "vwidth", glyph->vwidth);
988 if (glyph->tsb != 0)
989 dump_intfield(L, "tsidebearing", glyph->tsb);
990 }
991 dump_intfield(L, "width", glyph->width);
992
993 if (glyph->lsidebearing != glyph->xmin) {
994 dump_cond_intfield(L, "lsidebearing", glyph->lsidebearing);
995 }
996 /* dump_intfield(L,"ttf_glyph", glyph->ttf_glyph); */
997
998 /* Layer layers[2]; *//* TH Not used */
999 /* int layer_cnt; *//* TH Not used */
1000 /* StemInfo *hstem; *//* TH Not used */
1001 /* StemInfo *vstem; *//* TH Not used */
1002 /* DStemInfo *dstem; *//* TH Not used */
1003
1004 /* MinimumDistance *md; *//* TH Not used */
1005 /* struct charviewbase *views; *//* TH Not used */
1006 /* struct charinfo *charinfo; *//* TH ? (charinfo.c) */
1007 /* struct splinefont *parent; *//* TH Not used */
1008
1009 if (glyph->glyph_class > 0) {
1010 dump_enumfield(L, "class", glyph->glyph_class, glyph_class_enum);
1011 }
1012 /* TH: internal fontforge stuff
1013 dump_intfield(L,"changed", glyph->changed);
1014 dump_intfield(L,"changedsincelasthinted", glyph->changedsincelasthinted);
1015 dump_intfield(L,"manualhints", glyph->manualhints);
1016 dump_intfield(L,"ticked", glyph->ticked);
1017 dump_intfield(L,"changed_since_autosave", glyph->changed_since_autosave);
1018 dump_intfield(L,"widthset", glyph->widthset);
1019 dump_intfield(L,"vconflicts", glyph->vconflicts);
1020 dump_intfield(L,"hconflicts", glyph->hconflicts);
1021 dump_intfield(L,"searcherdummy", glyph->searcherdummy);
1022 dump_intfield(L,"changed_since_search", glyph->changed_since_search);
1023 dump_intfield(L,"wasopen", glyph->wasopen);
1024 dump_intfield(L,"namechanged", glyph->namechanged);
1025 dump_intfield(L,"blended", glyph->blended);
1026 dump_intfield(L,"ticked2", glyph->ticked2);
1027 dump_intfield(L,"unused_so_far", glyph->unused_so_far);
1028 dump_intfield(L,"numberpointsbackards", glyph->numberpointsbackards);
1029 dump_intfield(L,"instructions_out_of_date", glyph->instructions_out_of_date);
1030 dump_intfield(L,"complained_about_ptnums", glyph->complained_about_ptnums);
1031 unsigned int vs_open: 1;
1032 unsigned int unlink_rm_ovrlp_save_undo: 1;
1033 unsigned int inspiro: 1;
1034 unsigned int lig_caret_cnt_fixed: 1;
1035
1036
1037 uint8 *ttf_instrs;
1038 int16 ttf_instrs_len;
1039 int16 countermask_cnt;
1040 HintMask *countermasks;
1041 */
1042
1043 if (glyph->kerns != NULL) {
1044 lua_newtable(L);
1045 handle_kernpair(L, glyph->kerns);
1046 lua_setfield(L, -2, "kerns");
1047 }
1048 if (glyph->vkerns != NULL) {
1049 lua_newtable(L);
1050 handle_kernpair(L, glyph->vkerns);
1051 lua_setfield(L, -2, "vkerns");
1052 }
1053
1054 if (glyph->dependents != NULL) {
1055 lua_newtable(L);
1056 handle_splinecharlist(L, glyph->dependents);
1057 lua_setfield(L, -2, "dependents");
1058
1059 }
1060 if (glyph->possub != NULL) {
1061 lua_newtable(L);
1062 handle_generic_pst(L, glyph->possub);
1063 lua_setfield(L, -2, "lookups");
1064 }
1065
1066 if (glyph->ligofme != NULL) {
1067 lua_newtable(L);
1068 handle_liglist(L, glyph->ligofme);
1069 lua_setfield(L, -2, "ligatures");
1070 }
1071
1072 if (glyph->comment != NULL)
1073 dump_stringfield(L, "comment", glyph->comment);
1074
1075 /* Color color; *//* dont care */
1076
1077 if (glyph->anchor != NULL) {
1078 lua_newtable(L);
1079 handle_anchorpoint(L, glyph->anchor);
1080 lua_setfield(L, -2, "anchors");
1081 }
1082
1083 if (glyph->altuni != NULL) {
1084 int i;
1085 lua_newtable(L);
1086 i = handle_altuni(L, glyph->altuni);
1087 if (i>0) {
1088 lua_setfield(L, -2, "altuni");
1089 } else {
1090 lua_pop(L,1);
1091 }
1092 }
1093
1094 if (glyph->tex_height != TEX_UNDEF)
1095 dump_intfield(L, "tex_height", glyph->tex_height);
1096 if (glyph->tex_depth != TEX_UNDEF)
1097 dump_intfield(L, "tex_depth", glyph->tex_depth);
1098
1099 dump_cond_intfield(L, "is_extended_shape", glyph->is_extended_shape);
1100 if (glyph->italic_correction != TEX_UNDEF)
1101 dump_intfield(L, "italic_correction", glyph->italic_correction);
1102 if (glyph->top_accent_horiz != TEX_UNDEF)
1103 dump_intfield(L, "top_accent", glyph->top_accent_horiz);
1104
1105 if (glyph->vert_variants != NULL) {
1106 lua_newtable(L);
1107 handle_glyphvariants(L, glyph->vert_variants);
1108 lua_setfield(L, -2, "vert_variants");
1109 }
1110 if (glyph->horiz_variants != NULL) {
1111 lua_newtable(L);
1112 handle_glyphvariants(L, glyph->horiz_variants);
1113 lua_setfield(L, -2, "horiz_variants");
1114 }
1115 if (glyph->mathkern != NULL) {
1116 lua_newtable(L);
1117 handle_mathkern(L, glyph->mathkern);
1118 lua_setfield(L, -2, "mathkern");
1119 }
1120 }
1121
1122 const char *panose_values_0[] = {
1123 "Any", "No Fit", "Text and Display", "Script", "Decorative", "Pictorial"
1124 };
1125
1126 const char *panose_values_1[] = {
1127 "Any", "No Fit", "Cove", "Obtuse Cove", "Square Cove",
1128 "Obtuse Square Cove",
1129 "Square", "Thin", "Bone", "Exaggerated", "Triangle", "Normal Sans",
1130 "Obtuse Sans", "Perp Sans", "Flared", "Rounded"
1131 };
1132
1133 const char *panose_values_2[] = {
1134 "Any", "No Fit", "Very Light", "Light", "Thin", "Book",
1135 "Medium", "Demi", "Bold", "Heavy", "Black", "Nord"
1136 };
1137
1138 const char *panose_values_3[] = {
1139 "Any", "No Fit", "Old Style", "Modern", "Even Width",
1140 "Expanded", "Condensed", "Very Expanded", "Very Condensed", "Monospaced"
1141 };
1142
1143 const char *panose_values_4[] = {
1144 "Any", "No Fit", "None", "Very Low", "Low", "Medium Low",
1145 "Medium", "Medium High", "High", "Very High"
1146 };
1147
1148 const char *panose_values_5[] = {
1149 "Any", "No Fit", "Gradual/Diagonal", "Gradual/Transitional",
1150 "Gradual/Vertical",
1151 "Gradual/Horizontal", "Rapid/Vertical", "Rapid/Horizontal",
1152 "Instant/Vertical"
1153 };
1154
1155 const char *panose_values_6[] = {
1156 "Any", "No Fit", "Straight Arms/Horizontal", "Straight Arms/Wedge",
1157 "Straight Arms/Vertical",
1158 "Straight Arms/Single Serif", "Straight Arms/Double Serif",
1159 "Non-Straight Arms/Horizontal",
1160 "Non-Straight Arms/Wedge", "Non-Straight Arms/Vertical",
1161 "Non-Straight Arms/Single Serif",
1162 "Non-Straight Arms/Double Serif"
1163 };
1164
1165 const char *panose_values_7[] = {
1166 "Any", "No Fit", "Normal/Contact", "Normal/Weighted", "Normal/Boxed",
1167 "Normal/Flattened",
1168 "Normal/Rounded", "Normal/Off Center", "Normal/Square", "Oblique/Contact",
1169 "Oblique/Weighted",
1170 "Oblique/Boxed", "Oblique/Flattened", "Oblique/Rounded",
1171 "Oblique/Off Center", "Oblique/Square"
1172 };
1173
1174 const char *panose_values_8[] = {
1175 "Any", "No Fit", "Standard/Trimmed", "Standard/Pointed",
1176 "Standard/Serifed", "High/Trimmed",
1177 "High/Pointed", "High/Serifed", "Constant/Trimmed", "Constant/Pointed",
1178 "Constant/Serifed",
1179 "Low/Trimmed", "Low/Pointed", "Low/Serifed"
1180 };
1181
1182 const char *panose_values_9[] = {
1183 "Any", "No Fit", "Constant/Small", "Constant/Standard",
1184 "Constant/Large", "Ducking/Small", "Ducking/Standard", "Ducking/Large"
1185 };
1186
1187 #define panose_values_0_max 5
1188 #define panose_values_1_max 15
1189 #define panose_values_2_max 11
1190 #define panose_values_3_max 9
1191 #define panose_values_4_max 9
1192 #define panose_values_5_max 8
1193 #define panose_values_6_max 11
1194 #define panose_values_7_max 15
1195 #define panose_values_8_max 13
1196 #define panose_values_9_max 7
1197
1198 #define fix_range(a,b) (b<0 ? 0 : (b>a ? 0 : b))
1199
1200 static void handle_pfminfo(lua_State * L, struct pfminfo pfm)
1201 {
1202
1203 dump_intfield(L, "pfmset", pfm.pfmset);
1204 dump_intfield(L, "winascent_add", pfm.winascent_add);
1205 dump_intfield(L, "windescent_add", pfm.windescent_add);
1206 dump_intfield(L, "hheadascent_add", pfm.hheadascent_add);
1207 dump_intfield(L, "hheaddescent_add", pfm.hheaddescent_add);
1208 dump_intfield(L, "typoascent_add", pfm.typoascent_add);
1209 dump_intfield(L, "typodescent_add", pfm.typodescent_add);
1210 dump_intfield(L, "subsuper_set", pfm.subsuper_set);
1211 dump_intfield(L, "panose_set", pfm.panose_set);
1212 dump_intfield(L, "hheadset", pfm.hheadset);
1213 dump_intfield(L, "vheadset", pfm.vheadset);
1214 dump_intfield(L, "pfmfamily", pfm.pfmfamily);
1215 dump_intfield(L, "weight", pfm.weight);
1216 dump_intfield(L, "width", pfm.width);
1217 dump_intfield(L, "avgwidth", pfm.avgwidth);
1218 dump_intfield(L, "firstchar", pfm.firstchar);
1219 dump_intfield(L, "lastchar", pfm.lastchar);
1220 lua_createtable(L, 0, 10);
1221
1222 dump_enumfield(L, "familytype", fix_range(panose_values_0_max, pfm.panose[0]), panose_values_0);
1223 dump_enumfield(L, "serifstyle", fix_range(panose_values_1_max, pfm.panose[1]), panose_values_1);
1224 dump_enumfield(L, "weight", fix_range(panose_values_2_max, pfm.panose[2]), panose_values_2);
1225 dump_enumfield(L, "proportion", fix_range(panose_values_3_max, pfm.panose[3]), panose_values_3);
1226 dump_enumfield(L, "contrast", fix_range(panose_values_4_max, pfm.panose[4]), panose_values_4);
1227 dump_enumfield(L, "strokevariation", fix_range(panose_values_5_max, pfm.panose[5]), panose_values_5);
1228 dump_enumfield(L, "armstyle", fix_range(panose_values_6_max, pfm.panose[6]), panose_values_6);
1229 dump_enumfield(L, "letterform", fix_range(panose_values_7_max, pfm.panose[7]), panose_values_7);
1230 dump_enumfield(L, "midline", fix_range(panose_values_8_max, pfm.panose[8]), panose_values_8);
1231 dump_enumfield(L, "xheight", fix_range(panose_values_9_max, pfm.panose[9]), panose_values_9);
1232 lua_setfield(L, -2, "panose");
1233
1234 dump_intfield(L, "fstype", pfm.fstype);
1235 dump_intfield(L, "linegap", pfm.linegap);
1236 dump_intfield(L, "vlinegap", pfm.vlinegap);
1237 dump_intfield(L, "hhead_ascent", pfm.hhead_ascent);
1238 dump_intfield(L, "hhead_descent", pfm.hhead_descent);
1239 dump_intfield(L, "os2_typoascent", pfm.os2_typoascent);
1240 dump_intfield(L, "os2_typodescent", pfm.os2_typodescent);
1241 dump_intfield(L, "os2_typolinegap", pfm.os2_typolinegap);
1242 dump_intfield(L, "os2_winascent", pfm.os2_winascent);
1243 dump_intfield(L, "os2_windescent", pfm.os2_windescent);
1244 dump_intfield(L, "os2_subxsize", pfm.os2_subxsize);
1245 dump_intfield(L, "os2_subysize", pfm.os2_subysize);
1246 dump_intfield(L, "os2_subxoff", pfm.os2_subxoff);
1247 dump_intfield(L, "os2_subyoff", pfm.os2_subyoff);
1248 dump_intfield(L, "os2_supxsize", pfm.os2_supxsize);
1249 dump_intfield(L, "os2_supysize", pfm.os2_supysize);
1250 dump_intfield(L, "os2_supxoff", pfm.os2_supxoff);
1251 dump_intfield(L, "os2_supyoff", pfm.os2_supyoff);
1252 dump_intfield(L, "os2_strikeysize", pfm.os2_strikeysize);
1253 dump_intfield(L, "os2_strikeypos", pfm.os2_strikeypos);
1254 dump_lstringfield(L, "os2_vendor", pfm.os2_vendor, 4);
1255 dump_intfield(L, "os2_family_class", pfm.os2_family_class);
1256 dump_intfield(L, "os2_xheight", pfm.os2_xheight);
1257 dump_intfield(L, "os2_capheight", pfm.os2_capheight);
1258 dump_intfield(L, "os2_defaultchar", pfm.os2_defaultchar);
1259 dump_intfield(L, "os2_breakchar", pfm.os2_breakchar);
1260 if (pfm.hascodepages) {
1261 lua_newtable(L);
1262 lua_pushinteger(L, pfm.codepages[0]);
1263 lua_rawseti(L, -2, 1);
1264 lua_pushinteger(L, pfm.codepages[1]);
1265 lua_rawseti(L, -2, 2);
1266 lua_setfield(L, -2, "codepages");
1267 }
1268 if (pfm.hasunicoderanges) {
1269 lua_newtable(L);
1270 lua_pushinteger(L, pfm.unicoderanges[0]);
1271 lua_rawseti(L, -2, 1);
1272 lua_pushinteger(L, pfm.unicoderanges[1]);
1273 lua_rawseti(L, -2, 2);
1274 lua_pushinteger(L, pfm.unicoderanges[2]);
1275 lua_rawseti(L, -2, 3);
1276 lua_pushinteger(L, pfm.unicoderanges[3]);
1277 lua_rawseti(L, -2, 4);
1278 lua_setfield(L, -2, "unicoderanges");
1279 }
1280 }
1281
1282
1283 static char *do_handle_enc(lua_State * L, struct enc *enc)
1284 {
1285 int i;
1286 char *ret = enc->enc_name;
1287 dump_stringfield(L, "enc_name", enc->enc_name);
1288 dump_intfield(L, "char_cnt", enc->char_cnt);
1289
1290 lua_checkstack(L, 4);
1291 if (enc->char_cnt && enc->unicode != NULL) {
1292 lua_createtable(L, enc->char_cnt, 1);
1293 for (i = 0; i < enc->char_cnt; i++) {
1294 lua_pushinteger(L, i);
1295 lua_pushinteger(L, enc->unicode[i]);
1296 lua_rawset(L, -3);
1297 }
1298 lua_setfield(L, -2, "unicode");
1299 }
1300
1301 if (enc->char_cnt && enc->psnames != NULL) {
1302 lua_createtable(L, enc->char_cnt, 1);
1303 for (i = 0; i < enc->char_cnt; i++) {
1304 lua_pushinteger(L, i);
1305 lua_pushstring(L, enc->psnames[i]);
1306 lua_rawset(L, -3);
1307 }
1308 lua_setfield(L, -2, "psnames");
1309 }
1310 dump_intfield(L, "builtin", enc->builtin);
1311 dump_intfield(L, "hidden", enc->hidden);
1312 dump_intfield(L, "only_1byte", enc->only_1byte);
1313 dump_intfield(L, "has_1byte", enc->has_1byte);
1314 dump_intfield(L, "has_2byte", enc->has_2byte);
1315 dump_cond_intfield(L, "is_unicodebmp", enc->is_unicodebmp);
1316 dump_cond_intfield(L, "is_unicodefull", enc->is_unicodefull);
1317 dump_cond_intfield(L, "is_custom", enc->is_custom);
1318 dump_cond_intfield(L, "is_original", enc->is_original);
1319 dump_cond_intfield(L, "is_compact", enc->is_compact);
1320 dump_cond_intfield(L, "is_japanese", enc->is_japanese);
1321 dump_cond_intfield(L, "is_korean", enc->is_korean);
1322 dump_cond_intfield(L, "is_tradchinese", enc->is_tradchinese);
1323 dump_cond_intfield(L, "is_simplechinese", enc->is_simplechinese);
1324
1325 if (enc->iso_2022_escape_len > 0) {
1326 dump_lstringfield(L, "iso_2022_escape", enc->iso_2022_escape, enc->iso_2022_escape_len);
1327 }
1328 dump_intfield(L, "low_page", enc->low_page);
1329 dump_intfield(L, "high_page", enc->high_page);
1330
1331 dump_stringfield(L, "iconv_name", enc->iconv_name);
1332
1333 dump_intfield(L, "char_max", enc->char_max);
1334 return ret;
1335 }
1336
1337 #if 0 /* unused */
1338 static void handle_enc(lua_State * L, struct enc *enc)
1339 {
1340 struct enc *next;
1341 NESTED_TABLE(do_handle_enc, enc, 24);
1342 }
1343 #endif
1344
1345 static void handle_encmap(lua_State * L, struct encmap *map, int notdef_loc)
1346 {
1347 int i;
1348 dump_intfield(L, "enccount", map->enccount);
1349 dump_intfield(L, "encmax", map->encmax);
1350 dump_intfield(L, "backmax", map->backmax);
1351 /*dump_intfield(L,"ticked", map->ticked) ; */
1352 if (map->remap != NULL) {
1353 lua_newtable(L);
1354 dump_intfield(L, "firstenc", map->remap->firstenc);
1355 dump_intfield(L, "lastenc", map->remap->lastenc);
1356 dump_intfield(L, "infont", map->remap->infont);
1357 lua_setfield(L, -2, "remap");
1358 }
1359 lua_checkstack(L, 4);
1360 if (map->encmax > 0 && map->map != NULL) {
1361 lua_createtable(L, map->encmax, 1);
1362 for (i = 0; i < map->encmax; i++) {
1363 if (map->map[i] != -1) {
1364 int l = map->map[i];
1365 lua_pushinteger(L, i);
1366 /*
1367 if (l < notdef_loc)
1368 lua_pushinteger(L, (l + 1));
1369 else
1370 */
1371 lua_pushinteger(L, l);
1372 lua_rawset(L, -3);
1373 }
1374 }
1375 lua_setfield(L, -2, "map");
1376 }
1377
1378 if (map->backmax > 0 && map->backmap != NULL) {
1379 lua_newtable(L);
1380 for (i = 0; i < map->backmax; i++) {
1381 if (map->backmap[i] != -1) {
1382 /*
1383 if (i < notdef_loc)
1384 lua_pushinteger(L, (i + 1));
1385 else
1386 */
1387 lua_pushinteger(L, i);
1388 lua_pushinteger(L, map->backmap[i]);
1389 lua_rawset(L, -3);
1390 }
1391 }
1392 lua_setfield(L, -2, "backmap");
1393 }
1394
1395 if (map->enc != NULL) {
1396 char *encname;
1397 lua_newtable(L);
1398 encname = do_handle_enc(L, map->enc);
1399 lua_setfield(L, -2, "enc");
1400 lua_pushstring(L, encname);
1401 lua_setfield(L, -2, "enc_name");
1402 }
1403 }
1404
1405 static void handle_psdict(lua_State * L, struct psdict *private)
1406 {
1407 int k;
1408 if (private->keys != NULL && private->values != NULL) {
1409 for (k = 0; k < private->next; k++) {
1410 lua_pushstring(L, private->keys[k]);
1411 lua_pushstring(L, private->values[k]);
1412 lua_rawset(L, -3);
1413 }
1414 }
1415 }
1416
1417 static void do_handle_ttflangname(lua_State * L, struct ttflangname *names)
1418 {
1419 int k;
1420 dump_stringfield(L, "lang", MSLangString(names->lang));
1421 lua_checkstack(L, 4);
1422 lua_createtable(L, 0, ttf_namemax);
1423 for (k = 0; k < ttf_namemax; k++) {
1424 lua_pushstring(L, ttfnames_enum[k]);
1425 lua_pushstring(L, names->names[k]);
1426 lua_rawset(L, -3);
1427 }
1428 lua_setfield(L, -2, "names");
1429 }
1430
1431 static void handle_ttflangname(lua_State * L, struct ttflangname *names)
1432 {
1433 struct ttflangname *next;
1434 NESTED_TABLE(do_handle_ttflangname, names, 2);
1435 }
1436
1437 static void do_handle_anchorclass(lua_State * L, struct anchorclass *anchor)
1438 {
1439 dump_stringfield(L, "name", anchor->name);
1440 dump_subtable_name(L, "lookup", anchor->subtable);
1441 dump_enumfield(L, "type", anchor->type, anchorclass_type_enum);
1442 /* uint8 has_base; */
1443 /* uint8 processed, has_mark, matches, ac_num; */
1444 /* uint8 ticked; */
1445 }
1446
1447 static void handle_anchorclass(lua_State * L, struct anchorclass *anchor)
1448 {
1449 struct anchorclass *next;
1450 NESTED_TABLE(do_handle_anchorclass, anchor, 10);
1451 }
1452
1453 static void do_handle_ttf_table(lua_State * L, struct ttf_table *ttf_tab)
1454 {
1455
1456 dump_tag(L, "tag", ttf_tab->tag);
1457 dump_intfield(L, "len", ttf_tab->len);
1458 dump_intfield(L, "maxlen", ttf_tab->maxlen);
1459 dump_lstringfield(L, "data", (char *) ttf_tab->data, ttf_tab->len);
1460 }
1461
1462 static void handle_ttf_table(lua_State * L, struct ttf_table *ttf_tab)
1463 {
1464 struct ttf_table *next;
1465 NESTED_TABLE(do_handle_ttf_table, ttf_tab, 4);
1466 }
1467
1468 static int do_handle_kernclass(lua_State * L, struct kernclass *kerns, const char *name)
1469 {
1470 int k;
1471 int match = 0;
1472 if (name) {
1473 struct lookup_subtable *s = kerns->subtable;
1474 while (s != NULL) {
1475 if (strcmp(s->subtable_name,name)==0) {
1476 match = 1;
1477 break;
1478 }
1479 s = s->next;
1480 }
1481 } else {
1482 match = 1;
1483 }
1484 if (!match) {
1485 return 0;
1486 }
1487 lua_checkstack(L, 4);
1488 lua_createtable(L, kerns->first_cnt, 1);
1489 for (k = 0; k < kerns->first_cnt; k++) {
1490 lua_pushinteger(L, (k + 1));
1491 lua_pushstring(L, kerns->firsts[k]);
1492 lua_rawset(L, -3);
1493 }
1494 lua_setfield(L, -2, "firsts");
1495
1496 lua_createtable(L, kerns->second_cnt, 1);
1497 for (k = 0; k < kerns->second_cnt; k++) {
1498 lua_pushinteger(L, (k + 1));
1499 lua_pushstring(L, kerns->seconds[k]);
1500 lua_rawset(L, -3);
1501 }
1502 lua_setfield(L, -2, "seconds");
1503
1504 if (!name) {
1505 dump_subtable_name(L, "lookup", kerns->subtable);
1506 }
1507 lua_createtable(L, kerns->second_cnt * kerns->first_cnt, 1);
1508 for (k = 0; k < (kerns->second_cnt * kerns->first_cnt); k++) {
1509 if (kerns->offsets[k] != 0) {
1510 lua_pushinteger(L, (k + 1));
1511 lua_pushinteger(L, kerns->offsets[k]);
1512 lua_rawset(L, -3);
1513 }
1514 }
1515 lua_setfield(L, -2, "offsets");
1516 return 1;
1517 }
1518
1519 static void handle_kernclass(lua_State * L, struct kernclass *kerns, const char *name)
1520 {
1521 struct kernclass *next;
1522 NESTED_TABLE_SF(do_handle_kernclass, kerns, name, 8);
1523 }
1524
1525
1526 #define DUMP_NUMBER_ARRAY(s,cnt,item) { \
1527 if (cnt>0 && item != NULL) { \
1528 int kk; \
1529 lua_newtable(L); \
1530 for (kk=0;kk<cnt;kk++) { \
1531 lua_pushinteger(L,(kk+1)); \
1532 lua_pushinteger(L,item[kk]); \
1533 lua_rawset(L,-3); } \
1534 lua_setfield(L,-2,s); } }
1535
1536
1537 #define DUMP_STRING_ARRAY(s,cnt,item) { \
1538 if (cnt>0 && item!=NULL) { \
1539 int kk; \
1540 lua_newtable(L); \
1541 for (kk=0;kk<cnt;kk++) { \
1542 lua_pushinteger(L,(kk+1)); \
1543 lua_pushstring(L,item[kk]); \
1544 lua_rawset(L,-3); } \
1545 lua_setfield(L,-2,s); } }
1546
1547 #define DUMP_EXACT_STRING_ARRAY(s,cnt,item) { \
1548 if (cnt>0 && item!=NULL) { \
1549 int kk; \
1550 lua_newtable(L); \
1551 for (kk=0;kk<cnt;kk++) { \
1552 lua_pushinteger(L,(kk)); \
1553 lua_pushstring(L,item[kk]); \
1554 lua_rawset(L,-3); } \
1555 lua_setfield(L,-2,s); } }
1556
1557 static void handle_fpst_rule(lua_State * L, struct fpst_rule *rule, int format)
1558 {
1559 int k;
1560
1561 if (format == pst_glyphs) {
1562
1563 lua_newtable(L);
1564 dump_stringfield(L, "names", rule->u.glyph.names);
1565 dump_stringfield(L, "back", rule->u.glyph.back);
1566 dump_stringfield(L, "fore", rule->u.glyph.fore);
1567 lua_setfield(L, -2, fpossub_format_enum[format]);
1568
1569 } else if (format == pst_class) {
1570
1571 lua_newtable(L);
1572 DUMP_NUMBER_ARRAY("current", rule->u.class.ncnt,
1573 rule->u.class.nclasses);
1574 DUMP_NUMBER_ARRAY("before", rule->u.class.bcnt, rule->u.class.bclasses);
1575 DUMP_NUMBER_ARRAY("after", rule->u.class.fcnt, rule->u.class.fclasses);
1576 #if 0
1577 DUMP_NUMBER_ARRAY("allclasses", 0, rule->u.class.allclasses);
1578 #endif
1579 lua_setfield(L, -2, fpossub_format_enum[format]);
1580
1581 } else if (format == pst_coverage) {
1582
1583 lua_newtable(L);
1584 DUMP_STRING_ARRAY("current", rule->u.coverage.ncnt,
1585 rule->u.coverage.ncovers);
1586 DUMP_STRING_ARRAY("before", rule->u.coverage.bcnt,
1587 rule->u.coverage.bcovers);
1588 DUMP_STRING_ARRAY("after", rule->u.coverage.fcnt,
1589 rule->u.coverage.fcovers);
1590 lua_setfield(L, -2, fpossub_format_enum[format]);
1591
1592 } else if (format == pst_reversecoverage) {
1593
1594 lua_newtable(L);
1595 DUMP_STRING_ARRAY("current", rule->u.rcoverage.always1,
1596 rule->u.rcoverage.ncovers);
1597 DUMP_STRING_ARRAY("before", rule->u.rcoverage.bcnt,
1598 rule->u.rcoverage.bcovers);
1599 DUMP_STRING_ARRAY("after", rule->u.rcoverage.fcnt,
1600 rule->u.rcoverage.fcovers);
1601 dump_stringfield(L, "replacements", rule->u.rcoverage.replacements);
1602 lua_setfield(L, -2, fpossub_format_enum[format]);
1603 } else {
1604 fprintf(stderr, "handle_fpst_rule(): Unknown rule format: %d\n",
1605 format);
1606 }
1607
1608 if (rule->lookup_cnt > 0) {
1609 lua_newtable(L);
1610 for (k = 0; k < rule->lookup_cnt; k++) {
1611 lua_pushinteger(L, (rule->lookups[k].seq + 1));
1612 if (rule->lookups[k].lookup != NULL) {
1613 lua_pushstring(L, rule->lookups[k].lookup->lookup_name);
1614 } else {
1615 lua_pushnil(L);
1616 }
1617 lua_rawset(L, -3);
1618 }
1619 lua_setfield(L, -2, "lookups");
1620 } else {
1621 /*
1622 fprintf(stderr,"handle_fpst_rule(): No lookups?\n");
1623 */
1624 }
1625 }
1626
1627 static void do_handle_generic_fpst(lua_State * L, struct generic_fpst *fpst)
1628 {
1629 int k;
1630
1631 if (fpst->type > LAST_POSSUB_TYPE_ENUM) {
1632 dump_intfield(L, "type", fpst->type);
1633 } else {
1634 dump_enumfield(L, "type", fpst->type, possub_type_enum);
1635 }
1636 dump_enumfield(L, "format", fpst->format, fpossub_format_enum);
1637
1638 if (fpst->format == pst_class) {
1639 DUMP_EXACT_STRING_ARRAY("current_class", fpst->nccnt, fpst->nclass);
1640 DUMP_EXACT_STRING_ARRAY("before_class", fpst->bccnt, fpst->bclass);
1641 DUMP_EXACT_STRING_ARRAY("after_class", fpst->fccnt, fpst->fclass);
1642 } else {
1643 DUMP_STRING_ARRAY("current_class", fpst->nccnt, fpst->nclass);
1644 DUMP_STRING_ARRAY("before_class", fpst->bccnt, fpst->bclass);
1645 DUMP_STRING_ARRAY("after_class", fpst->fccnt, fpst->fclass);
1646 }
1647
1648 lua_checkstack(L, 4);
1649 if (fpst->rule_cnt > 0) {
1650 lua_createtable(L, fpst->rule_cnt, 1);
1651 for (k = 0; k < fpst->rule_cnt; k++) {
1652 lua_pushinteger(L, (k + 1));
1653 lua_newtable(L);
1654 handle_fpst_rule(L, &(fpst->rules[k]), fpst->format);
1655 lua_rawset(L, -3);
1656 }
1657 lua_setfield(L, -2, "rules");
1658 }
1659 /*dump_intfield (L,"ticked", fpst->ticked); */
1660 }
1661
1662 static void handle_generic_fpst(lua_State * L, struct generic_fpst *fpst)
1663 {
1664 struct generic_fpst *next;
1665 int k = 1;
1666 lua_checkstack(L, 3);
1667 if (fpst->subtable != NULL && fpst->subtable->subtable_name != NULL) {
1668 lua_pushstring(L, fpst->subtable->subtable_name);
1669 } else {
1670 lua_pushinteger(L, k);
1671 k++;
1672 }
1673 lua_createtable(L, 0, 10);
1674 do_handle_generic_fpst(L, fpst);
1675 lua_rawset(L, -3);
1676 next = fpst->next;
1677 while (next != NULL) {
1678 lua_checkstack(L, 3);
1679 if (next->subtable != NULL && next->subtable->subtable_name != NULL) {
1680 lua_pushstring(L, next->subtable->subtable_name);
1681 } else {
1682 lua_pushinteger(L, k);
1683 k++;
1684 }
1685 lua_createtable(L, 0, 10);
1686 do_handle_generic_fpst(L, next);
1687 lua_rawset(L, -3);
1688 next = next->next;
1689 }
1690 }
1691
1692 static void do_handle_otfname(lua_State * L, struct otfname *oname)
1693 {
1694 dump_intfield(L, "lang", oname->lang);
1695 dump_stringfield(L, "name", oname->name);
1696 }
1697
1698 static void handle_otfname(lua_State * L, struct otfname *oname)
1699 {
1700 struct otfname *next;
1701 NESTED_TABLE(do_handle_otfname, oname, 2);
1702 }
1703
1704 static void handle_MATH(lua_State * L, struct MATH *MATH)
1705 {
1706 dump_intfield(L, "ScriptPercentScaleDown", MATH->ScriptPercentScaleDown);
1707 dump_intfield(L, "ScriptScriptPercentScaleDown", MATH->ScriptScriptPercentScaleDown);
1708 dump_intfield(L, "DelimitedSubFormulaMinHeight", MATH->DelimitedSubFormulaMinHeight);
1709 dump_intfield(L, "DisplayOperatorMinHeight", MATH->DisplayOperatorMinHeight);
1710 dump_intfield(L, "MathLeading", MATH->MathLeading);
1711 dump_intfield(L, "AxisHeight", MATH->AxisHeight);
1712 dump_intfield(L, "AccentBaseHeight", MATH->AccentBaseHeight);
1713 dump_intfield(L, "FlattenedAccentBaseHeight", MATH->FlattenedAccentBaseHeight);
1714 dump_intfield(L, "SubscriptShiftDown", MATH->SubscriptShiftDown);
1715 dump_intfield(L, "SubscriptTopMax", MATH->SubscriptTopMax);
1716 dump_intfield(L, "SubscriptBaselineDropMin", MATH->SubscriptBaselineDropMin);
1717 dump_intfield(L, "SuperscriptShiftUp", MATH->SuperscriptShiftUp);
1718 dump_intfield(L, "SuperscriptShiftUpCramped", MATH->SuperscriptShiftUpCramped);
1719 dump_intfield(L, "SuperscriptBottomMin", MATH->SuperscriptBottomMin);
1720 dump_intfield(L, "SuperscriptBaselineDropMax", MATH->SuperscriptBaselineDropMax);
1721 dump_intfield(L, "SubSuperscriptGapMin", MATH->SubSuperscriptGapMin);
1722 dump_intfield(L, "SuperscriptBottomMaxWithSubscript", MATH->SuperscriptBottomMaxWithSubscript);
1723 dump_intfield(L, "SpaceAfterScript", MATH->SpaceAfterScript);
1724 dump_intfield(L, "UpperLimitGapMin", MATH->UpperLimitGapMin);
1725 dump_intfield(L, "UpperLimitBaselineRiseMin", MATH->UpperLimitBaselineRiseMin);
1726 dump_intfield(L, "LowerLimitGapMin", MATH->LowerLimitGapMin);
1727 dump_intfield(L, "LowerLimitBaselineDropMin", MATH->LowerLimitBaselineDropMin);
1728 dump_intfield(L, "StackTopShiftUp", MATH->StackTopShiftUp);
1729 dump_intfield(L, "StackTopDisplayStyleShiftUp", MATH->StackTopDisplayStyleShiftUp);
1730 dump_intfield(L, "StackBottomShiftDown", MATH->StackBottomShiftDown);
1731 dump_intfield(L, "StackBottomDisplayStyleShiftDown", MATH->StackBottomDisplayStyleShiftDown);
1732 dump_intfield(L, "StackGapMin", MATH->StackGapMin);
1733 dump_intfield(L, "StackDisplayStyleGapMin", MATH->StackDisplayStyleGapMin);
1734 dump_intfield(L, "StretchStackTopShiftUp", MATH->StretchStackTopShiftUp);
1735 dump_intfield(L, "StretchStackBottomShiftDown", MATH->StretchStackBottomShiftDown);
1736 dump_intfield(L, "StretchStackGapAboveMin", MATH->StretchStackGapAboveMin);
1737 dump_intfield(L, "StretchStackGapBelowMin", MATH->StretchStackGapBelowMin);
1738 dump_intfield(L, "FractionNumeratorShiftUp", MATH->FractionNumeratorShiftUp);
1739 dump_intfield(L, "FractionNumeratorDisplayStyleShiftUp", MATH->FractionNumeratorDisplayStyleShiftUp);
1740 dump_intfield(L, "FractionDenominatorShiftDown", MATH->FractionDenominatorShiftDown);
1741 dump_intfield(L, "FractionDenominatorDisplayStyleShiftDown", MATH->FractionDenominatorDisplayStyleShiftDown);
1742 dump_intfield(L, "FractionNumeratorGapMin", MATH->FractionNumeratorGapMin);
1743 dump_intfield(L, "FractionNumeratorDisplayStyleGapMin", MATH->FractionNumeratorDisplayStyleGapMin);
1744 dump_intfield(L, "FractionRuleThickness", MATH->FractionRuleThickness);
1745 dump_intfield(L, "FractionDenominatorGapMin", MATH->FractionDenominatorGapMin);
1746 dump_intfield(L, "FractionDenominatorDisplayStyleGapMin", MATH->FractionDenominatorDisplayStyleGapMin);
1747 dump_intfield(L, "SkewedFractionHorizontalGap", MATH->SkewedFractionHorizontalGap);
1748 dump_intfield(L, "SkewedFractionVerticalGap", MATH->SkewedFractionVerticalGap);
1749 dump_intfield(L, "OverbarVerticalGap", MATH->OverbarVerticalGap);
1750 dump_intfield(L, "OverbarRuleThickness", MATH->OverbarRuleThickness);
1751 dump_intfield(L, "OverbarExtraAscender", MATH->OverbarExtraAscender);
1752 dump_intfield(L, "UnderbarVerticalGap", MATH->UnderbarVerticalGap);
1753 dump_intfield(L, "UnderbarRuleThickness", MATH->UnderbarRuleThickness);
1754 dump_intfield(L, "UnderbarExtraDescender", MATH->UnderbarExtraDescender);
1755 dump_intfield(L, "RadicalVerticalGap", MATH->RadicalVerticalGap);
1756 dump_intfield(L, "RadicalDisplayStyleVerticalGap", MATH->RadicalDisplayStyleVerticalGap);
1757 dump_intfield(L, "RadicalRuleThickness", MATH->RadicalRuleThickness);
1758 dump_intfield(L, "RadicalExtraAscender", MATH->RadicalExtraAscender);
1759 dump_intfield(L, "RadicalKernBeforeDegree", MATH->RadicalKernBeforeDegree);
1760 dump_intfield(L, "RadicalKernAfterDegree", MATH->RadicalKernAfterDegree);
1761 dump_intfield(L, "RadicalDegreeBottomRaisePercent", MATH->RadicalDegreeBottomRaisePercent);
1762 dump_intfield(L, "MinConnectorOverlap", MATH->MinConnectorOverlap);
1763 }
1764
1765 /* the handling of BASE is untested, no font */
1766 static void handle_baselangextent(lua_State * L, struct baselangextent *ble);
1767
1768 static void do_handle_baselangextent(lua_State * L, struct baselangextent *ble)
1769 {
1770 dump_tag(L, "tag", ble->lang);
1771 dump_intfield(L, "ascent", ble->ascent);
1772 dump_intfield(L, "descent", ble->descent);
1773 lua_newtable(L);
1774 handle_baselangextent(L, ble->features);
1775 lua_setfield(L, -2, "features");
1776 }
1777
1778 static void handle_baselangextent(lua_State * L, struct baselangextent *ble)
1779 {
1780 struct baselangextent *next;
1781 NESTED_TABLE(do_handle_baselangextent, ble, 4);
1782 }
1783
1784 static void handle_base(lua_State * L, struct Base *Base)
1785 {
1786 int i;
1787 struct basescript *next = Base->scripts;
1788 lua_newtable(L);
1789 for (i = 0; i < Base->baseline_cnt; i++) {
1790 lua_pushstring(L, make_tag_string(Base->baseline_tags[i]));
1791 lua_rawseti(L, -2, (i + 1));
1792 }
1793 lua_setfield(L, -2, "tags");
1794 if (next != NULL) {
1795 lua_newtable(L);
1796 while (next != NULL) {
1797 lua_pushstring(L, make_tag_string(next->script));
1798 lua_newtable(L);
1799 dump_intfield(L, "default_baseline", (next->def_baseline + 1));
1800 lua_newtable(L);
1801 for (i = 0; i < Base->baseline_cnt; i++) {
1802 if (next->baseline_pos != NULL) /* default omitted */
1803 lua_pushinteger(L, next->baseline_pos[i]);
1804 else
1805 lua_pushinteger(L, 0);
1806 lua_rawseti(L, -2, (i + 1));
1807 }
1808 lua_setfield(L, -2, "baseline");
1809 lua_newtable(L);
1810 handle_baselangextent(L, next->langs);
1811 lua_setfield(L, -2, "lang");
1812 lua_rawset(L, -3);
1813 next = next->next;
1814 }
1815 lua_setfield(L, -2, "scripts");
1816 }
1817 }
1818
1819 static void handle_axismap(lua_State * L, struct axismap *am)
1820 {
1821 int i;
1822 lua_checkstack(L, 3);
1823 lua_newtable(L);
1824 for (i = 0; i < am->points; i++) {
1825 lua_pushinteger(L, am->blends[i]);
1826 lua_rawseti(L, -2, (i + 1));
1827 }
1828 lua_setfield(L, -2, "blends");
1829 lua_newtable(L);
1830 for (i = 0; i < am->points; i++) {
1831 lua_pushinteger(L, am->designs[i]);
1832 lua_rawseti(L, -2, (i + 1));
1833 }
1834 lua_setfield(L, -2, "designs");
1835 dump_realfield(L, "min", am->min);
1836 dump_realfield(L, "def", am->def);
1837 dump_realfield(L, "max", am->max);
1838 }
1839
1840 static void handle_mmset(lua_State * L, struct mmset *mm)
1841 {
1842 int i;
1843 lua_newtable(L);
1844 for (i = 0; i < mm->axis_count; i++) {
1845 lua_pushstring(L, mm->axes[i]);
1846 lua_rawseti(L, -2, (i + 1));
1847 }
1848 lua_setfield(L, -2, "axes");
1849
1850 dump_intfield(L, "instance_count", mm->instance_count);
1851 /* SplineFont *normal; *//* this is the parent */
1852 if (mm->instance_count > 0) {
1853 lua_newtable(L);
1854 for (i = 0; i < mm->instance_count * mm->axis_count; i++) {
1855 lua_pushinteger(L, mm->positions[i]);
1856 lua_rawseti(L, -2, (i + 1));
1857 }
1858 lua_setfield(L, -2, "positions");
1859
1860 /* better not to do this */
1861 #if 0
1862 {
1863 struct mmset *mmsave;
1864 lua_newtable(L);
1865 for (i = 0; i < mm->instance_count; i++) {
1866 lua_checkstack(L, 20);
1867 lua_createtable(L, 0, 60);
1868 mmsave = mm->instances[i]->mm;
1869 mm->instances[i]->mm = NULL;
1870 handle_splinefont(L, mm->instances[i]);
1871 mm->instances[i]->mm = mmsave;
1872 lua_rawseti(L, -2, (i + 1));
1873 }
1874 lua_setfield(L, -2, "instances");
1875 }
1876 #endif
1877
1878 lua_newtable(L);
1879 for (i = 0; i < mm->instance_count; i++) {
1880 lua_pushinteger(L, mm->defweights[i]);
1881 lua_rawseti(L, -2, (i + 1));
1882 }
1883 lua_setfield(L, -2, "defweights");
1884 }
1885
1886 if (mm->axismaps != NULL) {
1887 lua_newtable(L);
1888 for (i = 0; i < mm->axis_count; i++) {
1889 lua_newtable(L);
1890 handle_axismap(L, &(mm->axismaps[i]));
1891 lua_rawseti(L, -2, (i + 1));
1892 }
1893 lua_setfield(L, -2, "axismaps");
1894 }
1895 dump_stringfield(L, "cdv", mm->cdv);
1896 dump_stringfield(L, "ndv", mm->ndv);
1897 }
1898
1899 static void handle_splinefont(lua_State * L, struct splinefont *sf)
1900 {
1901 int k;
1902 int fix_notdef = 0; /* obsolete */
1903 int l = -1;
1904
1905 dump_stringfield(L, "table_version", LUA_OTF_VERSION);
1906 dump_stringfield(L, "fontname", sf->fontname);
1907 dump_stringfield(L, "fullname", sf->fullname);
1908 dump_stringfield(L, "familyname", sf->familyname);
1909 dump_stringfield(L, "weight", sf->weight);
1910 dump_stringfield(L, "copyright", sf->copyright);
1911 dump_stringfield(L, "filename", sf->filename);
1912 /* dump_stringfield(L,"defbasefilename", sf->defbasefilename); */
1913 dump_stringfield(L, "version", sf->version);
1914 dump_floatfield(L, "italicangle", sf->italicangle);
1915 dump_floatfield(L, "upos", sf->upos);
1916 dump_floatfield(L, "uwidth", sf->uwidth);
1917 dump_intfield(L, "ascent", sf->ascent);
1918 dump_intfield(L, "descent", sf->descent);
1919 dump_intfield(L, "notdef_loc",notdef_loc(sf));
1920 if (sf->uniqueid!=0) {
1921 dump_intfield(L, "uniqueid", sf->uniqueid);
1922 }
1923
1924 if (sf->glyphcnt > 0) {
1925 dump_intfield(L, "glyphcnt", sf->glyphmax - sf->glyphmin + 1);
1926 } else {
1927 dump_intfield(L, "glyphcnt", 0);
1928 }
1929
1930 if (sf->names != NULL) {
1931 /*
1932 this is not the best way to determine it but for now it will do; otherwise
1933 we need to mess with the ff library
1934 */
1935 lua_pushstring(L, "truetype");
1936 lua_pushboolean(L, 1);
1937 lua_rawset(L, -3);
1938 }
1939
1940 dump_intfield(L, "glyphmax", sf->glyphmax - 1);
1941 dump_intfield(L, "glyphmin", sf->glyphmin);
1942 dump_intfield(L, "units_per_em", sf->units_per_em);
1943
1944 if (sf->possub != NULL) {
1945 lua_newtable(L);
1946 handle_generic_fpst(L, sf->possub);
1947 lua_setfield(L, -2, "lookups");
1948 }
1949
1950 lua_checkstack(L, 4);
1951 lua_createtable(L, sf->glyphcnt, 0);
1952
1953 /* This after-the-fact type discovery is not brilliant,
1954 I should really add a 'format' key in the structure */
1955
1956 /*
1957 if ((sf->origname != NULL) &&
1958 (strmatch(sf->origname + strlen(sf->origname) - 4, ".pfa") == 0 ||
1959 strmatch(sf->origname + strlen(sf->origname) - 4, ".pfb") == 0)) {
1960 fix_notdef = 1;
1961 }
1962 */
1963
1964 if (fix_notdef) {
1965
1966 /* this is obsolete */
1967
1968 /* some code to ensure that the .notdef ends up in slot 0
1969 (this will actually be enforced by the CFF writer) */
1970 for (k = 0; k < sf->glyphcnt; k++) {
1971 if (sf->glyphs[k]) {
1972 if (strcmp(sf->glyphs[k]->name, ".notdef") == 0) {
1973 l = k;
1974 }
1975 }
1976 }
1977 if (l == -1) { /* fake a .notdef at the end */
1978 l = sf->glyphcnt;
1979 }
1980 for (k = 0; k < l; k++) {
1981 if (sf->glyphs[k]) {
1982 lua_pushinteger(L, (k + 1));
1983 lua_createtable(L, 0, 12);
1984 handle_splinechar(L, sf->glyphs[k], sf->hasvmetrics);
1985 lua_rawset(L, -3);
1986 }
1987 }
1988 if (sf->glyphs != NULL && l < sf->glyphcnt) {
1989 lua_pushinteger(L, 0);
1990 if (sf->glyphs[l]) {
1991 lua_createtable(L, 0, 12);
1992 handle_splinechar(L, sf->glyphs[l], sf->hasvmetrics);
1993 } else {
1994 lua_createtable(L, 0, 0);
1995 }
1996 lua_rawset(L, -3);
1997 }
1998 }
1999 if ((l + 1) < sf->glyphcnt) {
2000 for (k = (l + 1); k < sf->glyphcnt; k++) {
2001 if (sf->glyphs[k]) {
2002 lua_pushinteger(L, k);
2003 lua_createtable(L, 0, 12);
2004 handle_splinechar(L, sf->glyphs[k], sf->hasvmetrics);
2005 lua_rawset(L, -3);
2006 }
2007 }
2008 }
2009 lua_setfield(L, -2, "glyphs");
2010
2011 /* dump_intfield(L,"changed", sf->changed); */
2012 dump_intfield(L, "hasvmetrics", sf->hasvmetrics);
2013 dump_intfield(L, "onlybitmaps", sf->onlybitmaps);
2014 dump_intfield(L, "serifcheck", sf->serifcheck);
2015 dump_intfield(L, "isserif", sf->isserif);
2016 dump_intfield(L, "issans", sf->issans);
2017 dump_intfield(L, "encodingchanged", sf->encodingchanged);
2018 dump_intfield(L, "strokedfont", sf->strokedfont);
2019 dump_intfield(L, "use_typo_metrics", sf->use_typo_metrics);
2020 dump_intfield(L, "weight_width_slope_only", sf->weight_width_slope_only);
2021 dump_intfield(L, "head_optimized_for_cleartype",
2022 sf->head_optimized_for_cleartype);
2023
2024 dump_enumfield(L, "uni_interp", (sf->uni_interp + 1), uni_interp_enum);
2025
2026 if (sf->map != NULL) {
2027 lua_newtable(L);
2028 handle_encmap(L, sf->map, l);
2029 lua_setfield(L, -2, "map");
2030 }
2031
2032 dump_stringfield(L, "origname", sf->origname); /* new */
2033
2034 if (sf->private != NULL) {
2035 lua_newtable(L);
2036 handle_psdict(L, sf->private);
2037 lua_setfield(L, -2, "private");
2038 }
2039
2040 dump_stringfield(L, "xuid", sf->xuid);
2041
2042 lua_createtable(L, 0, 40);
2043 handle_pfminfo(L, sf->pfminfo);
2044 lua_setfield(L, -2, "pfminfo");
2045
2046 if (sf->names != NULL) {
2047 lua_newtable(L);
2048 handle_ttflangname(L, sf->names);
2049 lua_setfield(L, -2, "names");
2050 }
2051
2052 lua_createtable(L, 0, 4);
2053 dump_stringfield(L, "registry", sf->cidregistry);
2054 dump_stringfield(L, "ordering", sf->ordering);
2055 dump_intfield(L, "version", sf->cidversion);
2056 dump_intfield(L, "supplement", sf->supplement);
2057 lua_setfield(L, -2, "cidinfo");
2058
2059 /* SplineFont *cidmaster *//* parent in a subfont */
2060 if (sf->subfontcnt > 0) {
2061 lua_createtable(L, sf->subfontcnt, 0);
2062 for (k = 0; k < sf->subfontcnt; k++) {
2063 lua_checkstack(L, 10);
2064 lua_newtable(L);
2065 handle_splinefont(L, sf->subfonts[k]);
2066 lua_rawseti(L, -2, (k + 1));
2067 }
2068 lua_setfield(L, -2, "subfonts");
2069 }
2070
2071 dump_stringfield(L, "comments", sf->comments);
2072 dump_stringfield(L, "fontlog", sf->fontlog);
2073
2074 if (sf->cvt_names != NULL) {
2075 lua_newtable(L);
2076 for (k = 0; sf->cvt_names[k] != END_CVT_NAMES; ++k) {
2077 lua_pushstring(L, sf->cvt_names[k]);
2078 lua_rawseti(L, -2, (k + 1));
2079 }
2080 lua_setfield(L, -2, "cvt_names");
2081 }
2082
2083 if (sf->ttf_tables != NULL) {
2084 lua_newtable(L);
2085 handle_ttf_table(L, sf->ttf_tables);
2086 lua_setfield(L, -2, "ttf_tables");
2087 }
2088
2089 if (sf->ttf_tab_saved != NULL) {
2090 lua_newtable(L);
2091 handle_ttf_table(L, sf->ttf_tab_saved);
2092 lua_setfield(L, -2, "ttf_tab_saved");
2093 }
2094
2095 if (sf->texdata.type != tex_unset) {
2096 lua_newtable(L);
2097 dump_enumfield(L, "type", sf->texdata.type, tex_type_enum);
2098 lua_newtable(L);
2099 for (k = 0; k < 22; k++) {
2100 lua_pushinteger(L, k);
2101 lua_pushinteger(L, sf->texdata.params[k]);
2102 lua_rawset(L, -3);
2103 }
2104 lua_setfield(L, -2, "params");
2105 lua_setfield(L, -2, "texdata");
2106 }
2107 if (sf->anchor != NULL) {
2108 lua_newtable(L);
2109 handle_anchorclass(L, sf->anchor);
2110 lua_setfield(L, -2, "anchor_classes");
2111 }
2112 if (sf->kerns != NULL) {
2113 lua_newtable(L);
2114 handle_kernclass(L, sf->kerns, NULL);
2115 lua_setfield(L, -2, "kerns");
2116 }
2117 if (sf->vkerns != NULL) {
2118 lua_newtable(L);
2119 handle_kernclass(L, sf->vkerns, NULL);
2120 lua_setfield(L, -2, "vkerns");
2121 }
2122 if (sf->gsub_lookups != NULL) {
2123 lua_newtable(L);
2124 handle_lookup(L, sf->gsub_lookups, sf);
2125 lua_setfield(L, -2, "gsub");
2126 }
2127 if (sf->gpos_lookups != NULL) {
2128 lua_newtable(L);
2129 handle_lookup(L, sf->gpos_lookups, sf);
2130 lua_setfield(L, -2, "gpos");
2131 }
2132
2133 if (sf->mm != NULL) {
2134 lua_newtable(L);
2135 handle_mmset(L, sf->mm);
2136 lua_setfield(L, -2, "mm");
2137 }
2138 dump_stringfield(L, "chosenname", sf->chosenname);
2139
2140 if (sf->macstyle!=-1) {
2141 dump_intfield(L, "macstyle", sf->macstyle);
2142 }
2143 dump_stringfield(L, "fondname", sf->fondname);
2144
2145 dump_intfield(L, "design_size", sf->design_size);
2146 dump_intfield(L, "fontstyle_id", sf->fontstyle_id);
2147
2148 if (sf->fontstyle_name != NULL) {
2149 lua_newtable(L);
2150 handle_otfname(L, sf->fontstyle_name);
2151 lua_setfield(L, -2, "fontstyle_name");
2152 }
2153
2154 dump_intfield(L, "design_range_bottom", sf->design_range_bottom);
2155 dump_intfield(L, "design_range_top", sf->design_range_top);
2156 dump_floatfield(L, "strokewidth", sf->strokewidth);
2157
2158 if (sf->mark_class_cnt > 0) {
2159 lua_newtable(L);
2160 for (k = 0; k < sf->mark_class_cnt; k++) {
2161 if (sf->mark_class_names[k] != NULL) {
2162 lua_pushstring(L, sf->mark_class_names[k]);
2163 lua_pushstring(L, sf->mark_classes[k]);
2164 lua_rawset(L, -3);
2165 }
2166 }
2167 lua_setfield(L, -2, "mark_classes");
2168 }
2169
2170 dump_uintfield(L, "creationtime", sf->creationtime);
2171 dump_uintfield(L, "modificationtime", sf->modificationtime);
2172
2173 dump_intfield(L, "os2_version", sf->os2_version);
2174 dump_intfield(L, "sfd_version", sf->sfd_version);
2175
2176 if (sf->MATH != NULL) {
2177 lua_newtable(L);
2178 handle_MATH(L, sf->MATH);
2179 lua_setfield(L, -2, "math");
2180 }
2181
2182 if (sf->loadvalidation_state != 0) {
2183 int val, st;
2184 lua_newtable(L);
2185 val = 1;
2186 st = sf->loadvalidation_state;
2187 if (st & lvs_bad_ps_fontname) {
2188 lua_pushliteral(L, "bad_ps_fontname");
2189 lua_rawseti(L, -2, val++);
2190 }
2191 if (st & lvs_bad_glyph_table) {
2192 lua_pushliteral(L, "bad_glyph_table");
2193 lua_rawseti(L, -2, val++);
2194 }
2195 if (st & lvs_bad_cff_table) {
2196 lua_pushliteral(L, "bad_cff_table");
2197 lua_rawseti(L, -2, val++);
2198 }
2199 if (st & lvs_bad_metrics_table) {
2200 lua_pushliteral(L, "bad_metrics_table");
2201 lua_rawseti(L, -2, val++);
2202 }
2203 if (st & lvs_bad_cmap_table) {
2204 lua_pushliteral(L, "bad_cmap_table");
2205 lua_rawseti(L, -2, val++);
2206 }
2207 if (st & lvs_bad_bitmaps_table) {
2208 lua_pushliteral(L, "bad_bitmaps_table");
2209 lua_rawseti(L, -2, val++);
2210 }
2211 if (st & lvs_bad_gx_table) {
2212 lua_pushliteral(L, "bad_gx_table");
2213 lua_rawseti(L, -2, val++);
2214 }
2215 if (st & lvs_bad_ot_table) {
2216 lua_pushliteral(L, "bad_ot_table");
2217 lua_rawseti(L, -2, val++);
2218 }
2219 if (st & lvs_bad_os2_version) {
2220 lua_pushliteral(L, "bad_os2_version");
2221 lua_rawseti(L, -2, val++);
2222 }
2223 if (st & lvs_bad_sfnt_header) {
2224 lua_pushliteral(L, "bad_sfnt_header");
2225 lua_rawseti(L, -2, val++);
2226 }
2227 lua_setfield(L, -2, "validation_state");
2228 }
2229
2230 if (sf->horiz_base != NULL) {
2231 lua_newtable(L);
2232 handle_base(L, sf->horiz_base);
2233 lua_setfield(L, -2, "horiz_base");
2234 }
2235 if (sf->vert_base != NULL) {
2236 lua_newtable(L);
2237 handle_base(L, sf->vert_base);
2238 lua_setfield(L, -2, "vert_base");
2239 }
2240 dump_intfield(L, "extrema_bound", sf->extrema_bound);
2241 }
2242
2243 static int ff_make_table(lua_State * L)
2244 {
2245 SplineFont *sf;
2246 sf = *(check_isfont(L, 1));
2247 if (sf == NULL) {
2248 lua_pushboolean(L, 0);
2249 } else {
2250 lua_createtable(L, 0, 60);
2251 handle_splinefont(L, sf);
2252 }
2253 return 1;
2254 }
2255
2256 static void do_ff_info(lua_State * L, SplineFont * sf)
2257 {
2258 lua_newtable(L);
2259 dump_stringfield(L, "familyname", sf->familyname);
2260 dump_stringfield(L, "fontname", sf->fontname);
2261 dump_stringfield(L, "fullname", sf->fullname);
2262 dump_floatfield(L, "italicangle", sf->italicangle);
2263 dump_stringfield(L, "version", sf->version);
2264 dump_stringfield(L, "weight", sf->weight);
2265
2266 dump_intfield(L, "units_per_em", sf->units_per_em);
2267
2268 /*dump_intfield(L, "design_range_bottom", sf->design_range_bottom);*/
2269 /*dump_intfield(L, "design_range_top", sf->design_range_top);*/
2270 /*dump_intfield(L, "design_size", sf->design_size);*/
2271
2272 lua_createtable(L, 0, 40);
2273 handle_pfminfo(L, sf->pfminfo);
2274 lua_setfield(L, -2, "pfminfo");
2275
2276 /* Do we need this ? */
2277
2278 if (sf->names != NULL) {
2279 lua_newtable(L);
2280 handle_ttflangname(L, sf->names);
2281 lua_setfield(L, -2, "names");
2282 }
2283 }
2284
2285 typedef enum {
2286 FK_table_version = 0,
2287 FK_fontname,
2288 FK_fullname,
2289 FK_familyname,
2290 FK_weight,
2291 FK_copyright,
2292 FK_filename,
2293 FK_version,
2294 FK_italicangle,
2295 FK_upos,
2296 FK_uwidth,
2297 FK_ascent,
2298 FK_descent,
2299 FK_uniqueid,
2300 FK_glyphcnt,
2301 FK_glyphmax,
2302 FK_glyphmin,
2303 FK_units_per_em,
2304 FK_lookups,
2305 FK_glyphs,
2306 FK_hasvmetrics,
2307 FK_onlybitmaps,
2308 FK_serifcheck,
2309 FK_isserif,
2310 FK_issans,
2311 FK_encodingchanged,
2312 FK_strokedfont,
2313 FK_use_typo_metrics,
2314 FK_weight_width_slope_only,
2315 FK_head_optimized_for_cleartype,
2316 FK_uni_interp,
2317 FK_map,
2318 FK_origname,
2319 FK_private,
2320 FK_xuid,
2321 FK_pfminfo,
2322 FK_names,
2323 FK_cidinfo,
2324 FK_subfonts,
2325 FK_comments,
2326 FK_fontlog,
2327 FK_cvt_names,
2328 FK_ttf_tables,
2329 FK_ttf_tab_saved,
2330 FK_texdata,
2331 FK_anchor_classes,
2332 FK_kerns,
2333 FK_vkerns,
2334 FK_gsub,
2335 FK_gpos,
2336 /* FK_sm, */ /* AAT is not supported anymore */
2337 FK_features,
2338 FK_mm,
2339 FK_chosenname,
2340 FK_macstyle,
2341 FK_fondname,
2342 FK_design_size,
2343 FK_fontstyle_id,
2344 FK_fontstyle_name,
2345 FK_design_range_bottom,
2346 FK_design_range_top,
2347 FK_strokewidth,
2348 FK_mark_classes,
2349 FK_creationtime,
2350 FK_modificationtime,
2351 FK_os2_version,
2352 FK_sfd_version,
2353 FK_math,
2354 FK_validation_state,
2355 FK_horiz_base,
2356 FK_vert_base,
2357 FK_extrema_bound,
2358 FK_notdef_loc,
2359 } font_key_values;
2360
2361 const char *font_keys[] = {
2362 "table_version",
2363 "fontname",
2364 "fullname",
2365 "familyname",
2366 "weight",
2367 "copyright",
2368 "filename",
2369 "version",
2370 "italicangle",
2371 "upos",
2372 "uwidth",
2373 "ascent",
2374 "descent",
2375 "uniqueid",
2376 "glyphcnt",
2377 "glyphmax",
2378 "glyphmin",
2379 "units_per_em",
2380 "lookups",
2381 "glyphs",
2382 "hasvmetrics",
2383 "onlybitmaps",
2384 "serifcheck",
2385 "isserif",
2386 "issans",
2387 "encodingchanged",
2388 "strokedfont",
2389 "use_typo_metrics",
2390 "weight_width_slope_only",
2391 "head_optimized_for_cleartype",
2392 "uni_interp",
2393 "map",
2394 "origname",
2395 "private",
2396 "xuid",
2397 "pfminfo",
2398 "names",
2399 "cidinfo",
2400 "subfonts",
2401 "comments",
2402 "fontlog",
2403 "cvt_names",
2404 "ttf_tables",
2405 "ttf_tab_saved",
2406 "texdata",
2407 "anchor_classes",
2408 "kerns",
2409 "vkerns",
2410 "gsub",
2411 "gpos",
2412 "features",
2413 "mm",
2414 "chosenname",
2415 "macstyle",
2416 "fondname",
2417 "design_size",
2418 "fontstyle_id",
2419 "fontstyle_name",
2420 "design_range_bottom",
2421 "design_range_top",
2422 "strokewidth",
2423 "mark_classes",
2424 "creationtime",
2425 "modificationtime",
2426 "os2_version",
2427 "sfd_version",
2428 "math",
2429 "validation_state",
2430 "horiz_base",
2431 "vert_base",
2432 "extrema_bound",
2433 "notdef_loc",
2434 NULL
2435 };
2436
2437 typedef enum {
2438 GK_name = 0,
2439 GK_unicode,
2440 GK_boundingbox,
2441 GK_vwidth,
2442 GK_width,
2443 GK_lsidebearing,
2444 GK_class,
2445 GK_kerns,
2446 GK_vkerns,
2447 GK_dependents,
2448 GK_lookups,
2449 GK_ligatures,
2450 GK_comment,
2451 GK_anchors,
2452 GK_altuni,
2453 GK_tex_height,
2454 GK_tex_depth,
2455 GK_is_extended_shape,
2456 GK_italic_correction,
2457 GK_top_accent,
2458 GK_vert_variants,
2459 GK_horiz_variants,
2460 GK_mathkern,
2461 } font_glyph_key_values;
2462
2463 const char *font_glyph_keys[] = {
2464 "name",
2465 "unicode",
2466 "boundingbox",
2467 "vwidth",
2468 "width",
2469 "lsidebearing",
2470 "class",
2471 "kerns",
2472 "vkerns",
2473 "dependents",
2474 "lookups",
2475 "ligatures",
2476 "comment",
2477 "anchors",
2478 "altuni",
2479 "tex_height",
2480 "tex_depth",
2481 "is_extended_shape",
2482 "italic_correction",
2483 "top_accent",
2484 "vert_variants",
2485 "horiz_variants",
2486 "mathkern",
2487 NULL
2488 };
2489
2490 static int ff_fields(lua_State * L)
2491 {
2492 int i;
2493 const char **fields = NULL;
2494 if (is_userdata(L, 1, FONT_METATABLE) ||
2495 is_userdata(L, 1, FONT_SUBFONT_METATABLE)) {
2496 fields = font_keys;
2497 } else if (is_userdata(L, 1, FONT_GLYPH_METATABLE)) {
2498 fields = font_glyph_keys;
2499 }
2500 if (fields != NULL) {
2501 lua_newtable(L);
2502 for (i = 0; fields[i] != NULL; i++) {
2503 lua_pushstring(L, fields[i]);
2504 lua_rawseti(L, -2, (i + 1));
2505 }
2506 } else {
2507 lua_pushnil(L);
2508 }
2509 return 1;
2510 }
2511
2512 static int ff_glyphs_index(lua_State * L)
2513 {
2514
2515 SplineFont *sf;
2516 int gid = 0;
2517 int l = -1;
2518 int fix_notdef = 0; /* obsolete */
2519 lua_pushstring(L, "__sf");
2520 lua_rawget(L, 1);
2521 /* sf = *check_isfont(L, -1); */
2522 if (!(is_userdata(L, -1, FONT_METATABLE) ||
2523 is_userdata(L, -1, FONT_SUBFONT_METATABLE))) {
2524 return luaL_error(L, "fontloader.__index: expected a (sub)font userdata object\n");
2525 }
2526 sf = *((SplineFont **)lua_touserdata(L, -1));
2527
2528 lua_pop(L, 1);
2529 gid = luaL_checkinteger(L, 2);
2530 /* if (gid < sf->glyphmin || gid >= sf->glyphmax) {*/
2531 if (gid < sf->glyphmin || gid > sf->glyphmax) {
2532 return luaL_error(L, "fontloader.glyphs.__index: index is invalid\n");
2533 }
2534 /* This after-the-fact type discovery is not brilliant,
2535 I should really add a 'format' key in the structure */
2536 /*
2537 if ((sf->origname != NULL) &&
2538 (strmatch(sf->origname + strlen(sf->origname) - 4, ".pfa") == 0 ||
2539 strmatch(sf->origname + strlen(sf->origname) - 4, ".pfb") == 0)) {
2540 fix_notdef = 1;
2541 }
2542 */
2543 /* some code to ensure that the .notdef ends up in slot 0
2544 (this will actually be enforced by the CFF writer) */
2545 if (fix_notdef) {
2546 /* this is obsolete */
2547 l = notdef_loc(sf);
2548 /* now l is the .notdef location, adjust gid if needed */
2549 if (l == sf->glyphcnt) {
2550 /* no .notdef at all, will be created at zero */
2551 if (gid == 0) {
2552 /* we ask for .notdef but .notdef was added at end */
2553 gid = l;
2554 } else {
2555 /* f.glyphs[gid] == sf->glyphs[gid-1] */
2556 gid--;
2557 }
2558 } else if (l != 0) {
2559 if (gid == 0) {
2560 gid = l;
2561 } else if (gid < l) {
2562 gid--;
2563 }
2564 }
2565 }
2566
2567 if (sf->glyphs[gid] && sf->glyphs[gid] != (struct splinechar *)-1) {
2568 lua_ff_pushglyph(L, sf->glyphs[gid]);
2569 } else {
2570 lua_pushnil(L);
2571 }
2572 return 1;
2573 }
2574
2575 static int ff_glyph_index(lua_State * L)
2576 {
2577 struct splinechar *glyph;
2578 int key;
2579 glyph = *check_isglyph(L, 1);
2580 if (glyph == NULL) {
2581 return luaL_error(L, "fontloader.glyph.__index: glyph is nonexistent\n");
2582 }
2583 if (lua_type(L, 2) != LUA_TSTRING) { /* 1 == 'font' */
2584 return luaL_error(L, "fontloader.glyph.__index: can only be indexed by string\n");
2585 }
2586 key = luaL_checkoption(L, 2, NULL, font_glyph_keys);
2587 switch (key) {
2588 case GK_name:
2589 lua_pushstring(L, glyph->name);
2590 break;
2591 case GK_unicode:
2592 lua_pushinteger(L, glyph->unicodeenc);
2593 break;
2594 case GK_boundingbox:
2595 if (glyph->xmax == 0 && glyph->ymax == 0 && glyph->xmin == 0 && glyph->ymin == 0) {
2596 DBounds bb;
2597 SplineCharFindBounds(glyph, &bb);
2598 glyph->xmin = bb.minx;
2599 glyph->ymin = bb.miny;
2600 glyph->xmax = bb.maxx;
2601 glyph->ymax = bb.maxy;
2602 }
2603 lua_createtable(L, 4, 0);
2604 lua_pushinteger(L, 1);
2605 lua_pushinteger(L, glyph->xmin);
2606 lua_rawset(L, -3);
2607 lua_pushinteger(L, 2);
2608 lua_pushinteger(L, glyph->ymin);
2609 lua_rawset(L, -3);
2610 lua_pushinteger(L, 3);
2611 lua_pushinteger(L, glyph->xmax);
2612 lua_rawset(L, -3);
2613 lua_pushinteger(L, 4);
2614 lua_pushinteger(L, glyph->ymax);
2615 lua_rawset(L, -3);
2616 break;
2617 case GK_vwidth:
2618 lua_pushinteger(L, glyph->vwidth);
2619 break;
2620 case GK_width:
2621 lua_pushinteger(L, glyph->width);
2622 break;
2623 case GK_lsidebearing:
2624 lua_pushinteger(L, glyph->lsidebearing);
2625 break;
2626 case GK_class:
2627 if (glyph->glyph_class > 0) {
2628 lua_pushstring(L, glyph_class_enum[glyph->glyph_class]);
2629 } else {
2630 lua_pushnil(L);
2631 }
2632 break;
2633 case GK_kerns:
2634 if (glyph->kerns != NULL) {
2635 lua_newtable(L);
2636 handle_kernpair(L, glyph->kerns);
2637 } else {
2638 lua_pushnil(L);
2639 }
2640 break;
2641 case GK_vkerns:
2642 if (glyph->vkerns != NULL) {
2643 lua_newtable(L);
2644 handle_kernpair(L, glyph->vkerns);
2645 } else {
2646 lua_pushnil(L);
2647 }
2648 break;
2649 case GK_dependents:
2650 if (glyph->dependents != NULL) {
2651 lua_newtable(L);
2652 handle_splinecharlist(L, glyph->dependents);
2653 } else {
2654 lua_pushnil(L);
2655 }
2656 break;
2657 case GK_lookups:
2658 if (glyph->possub != NULL) {
2659 lua_newtable(L);
2660 handle_generic_pst(L, glyph->possub);
2661 } else {
2662 lua_pushnil(L);
2663 }
2664 break;
2665 case GK_ligatures:
2666 if (glyph->ligofme != NULL) {
2667 lua_newtable(L);
2668 handle_liglist(L, glyph->ligofme);
2669 } else {
2670 lua_pushnil(L);
2671 }
2672 break;
2673 case GK_comment:
2674 lua_pushstring(L, glyph->comment);
2675 break;
2676 case GK_anchors:
2677 if (glyph->anchor != NULL) {
2678 lua_newtable(L);
2679 handle_anchorpoint(L, glyph->anchor);
2680 } else {
2681 lua_pushnil(L);
2682 }
2683 break;
2684 case GK_altuni:
2685 if (glyph->altuni != NULL) {
2686 lua_newtable(L);
2687 handle_altuni(L, glyph->altuni);
2688 } else {
2689 lua_pushnil(L);
2690 }
2691 break;
2692 case GK_tex_height:
2693 if (glyph->tex_height != TEX_UNDEF) {
2694 lua_pushinteger(L, glyph->tex_height);
2695 } else {
2696 lua_pushnil(L);
2697 }
2698 break;
2699 case GK_tex_depth:
2700 if (glyph->tex_height != TEX_UNDEF) {
2701 lua_pushinteger(L, glyph->tex_depth);
2702 } else {
2703 lua_pushnil(L);
2704 }
2705 break;
2706 case GK_is_extended_shape:
2707 lua_pushinteger(L, glyph->is_extended_shape);
2708 break;
2709 case GK_italic_correction:
2710 if (glyph->italic_correction != TEX_UNDEF) {
2711 lua_pushinteger(L, glyph->italic_correction);
2712 } else {
2713 lua_pushnil(L);
2714 }
2715 break;
2716 case GK_top_accent:
2717 if (glyph->top_accent_horiz != TEX_UNDEF) {
2718 lua_pushinteger(L, glyph->top_accent_horiz);
2719 } else {
2720 lua_pushnil(L);
2721 }
2722 break;
2723 case GK_vert_variants:
2724 if (glyph->vert_variants != NULL) {
2725 lua_newtable(L);
2726 handle_glyphvariants(L, glyph->vert_variants);
2727 } else {
2728 lua_pushnil(L);
2729 }
2730 break;
2731 case GK_horiz_variants:
2732 if (glyph->horiz_variants != NULL) {
2733 lua_newtable(L);
2734 handle_glyphvariants(L, glyph->horiz_variants);
2735 } else {
2736 lua_pushnil(L);
2737 }
2738 break;
2739 case GK_mathkern:
2740 if (glyph->mathkern != NULL) {
2741 lua_newtable(L);
2742 handle_mathkern(L, glyph->mathkern);
2743 } else {
2744 lua_pushnil(L);
2745 }
2746 break;
2747 default:
2748 lua_pushnil(L);
2749 }
2750 return 1;
2751 }
2752
2753 static int ff_index(lua_State * L)
2754 {
2755 SplineFont *sf;
2756 int k, key;
2757 /* sf = *check_isfont(L, 1); */
2758 if (!(is_userdata(L, 1, FONT_METATABLE) || is_userdata(L, 1, FONT_SUBFONT_METATABLE))) {
2759 return luaL_error(L, "fontloader.__index: expected a (sub)font userdata object\n");
2760 }
2761 sf = *((SplineFont **)lua_touserdata(L, 1));
2762 if (sf == NULL) {
2763 lua_pushnil(L);
2764 return 1;
2765 }
2766 if (lua_type(L, 2) != LUA_TSTRING) { /* 1 == 'font' */
2767 return luaL_error(L, "fontloader.__index: can only be indexed by string\n");
2768 }
2769 key = luaL_checkoption(L, 2, NULL, font_keys);
2770 switch (key) {
2771 case FK_table_version:
2772 lua_pushstring(L, LUA_OTF_VERSION);
2773 break;
2774 case FK_fontname:
2775 lua_pushstring(L, sf->fontname);
2776 break;
2777 case FK_fullname:
2778 lua_pushstring(L, sf->fullname);
2779 break;
2780 case FK_familyname:
2781 lua_pushstring(L, sf->familyname);
2782 break;
2783 case FK_weight:
2784 lua_pushstring(L, sf->weight);
2785 break;
2786 case FK_copyright:
2787 lua_pushstring(L, sf->copyright);
2788 break;
2789 case FK_filename:
2790 lua_pushstring(L, sf->filename);
2791 break;
2792 case FK_version:
2793 lua_pushstring(L, sf->version);
2794 break;
2795 case FK_italicangle:
2796 lua_pushinteger(L, sf->italicangle);
2797 break;
2798 case FK_upos:
2799 lua_pushinteger(L, sf->upos);
2800 break;
2801 case FK_uwidth:
2802 lua_pushinteger(L, sf->uwidth);
2803 break;
2804 case FK_ascent:
2805 lua_pushinteger(L, sf->ascent);
2806 break;
2807 case FK_descent:
2808 lua_pushinteger(L, sf->descent);
2809 break;
2810 case FK_uniqueid:
2811 lua_pushinteger(L, sf->uniqueid);
2812 break;
2813 case FK_glyphcnt:
2814 if (sf->glyphcnt > 0) {
2815 lua_pushinteger(L, sf->glyphmax - sf->glyphmin + 1);
2816 } else {
2817 lua_pushinteger(L, 0);
2818 }
2819 break;
2820 case FK_glyphmax:
2821 lua_pushinteger(L, sf->glyphmax - 1);
2822 break;
2823 case FK_glyphmin:
2824 lua_pushinteger(L, sf->glyphmin);
2825 break;
2826 case FK_units_per_em:
2827 lua_pushinteger(L, sf->units_per_em);
2828 break;
2829 case FK_lookups:
2830 if (sf->possub != NULL) {
2831 lua_newtable(L);
2832 handle_generic_fpst(L, sf->possub);
2833 } else {
2834 lua_pushnil(L);
2835 }
2836 break;
2837 case FK_glyphs:
2838 lua_newtable(L); /* the virtual glyph table */
2839 lua_pushstring(L, "__sf");
2840 lua_pushvalue(L, 1); /* that is our font */
2841 lua_rawset(L, -3);
2842 luaL_getmetatable(L, FONT_GLYPHS_METATABLE);
2843 lua_setmetatable(L, -2); /* assign the metatable */
2844 break;
2845 case FK_hasvmetrics:
2846 lua_pushinteger(L, sf->hasvmetrics);
2847 break;
2848 case FK_onlybitmaps:
2849 lua_pushinteger(L, sf->onlybitmaps);
2850 break;
2851 case FK_serifcheck:
2852 lua_pushinteger(L, sf->serifcheck);
2853 break;
2854 case FK_isserif:
2855 lua_pushinteger(L, sf->isserif);
2856 break;
2857 case FK_issans:
2858 lua_pushinteger(L, sf->issans);
2859 break;
2860 case FK_encodingchanged:
2861 lua_pushinteger(L, sf->encodingchanged);
2862 break;
2863 case FK_strokedfont:
2864 lua_pushinteger(L, sf->strokedfont);
2865 break;
2866 case FK_use_typo_metrics:
2867 lua_pushinteger(L, sf->use_typo_metrics);
2868 break;
2869 case FK_weight_width_slope_only:
2870 lua_pushinteger(L, sf->weight_width_slope_only);
2871 break;
2872 case FK_head_optimized_for_cleartype:
2873 lua_pushinteger(L, sf->head_optimized_for_cleartype);
2874 break;
2875 case FK_uni_interp:
2876 lua_pushstring(L, uni_interp_enum[(sf->uni_interp + 1)]);
2877 break;
2878 case FK_map:
2879 if (sf->map != NULL) {
2880 lua_newtable(L);
2881 handle_encmap(L, sf->map, 0); /* notdef_loc(sf)); */
2882 } else {
2883 lua_pushnil(L);
2884 }
2885 break;
2886 case FK_origname:
2887 lua_pushstring(L, sf->origname);
2888 break;
2889 case FK_private:
2890 if (sf->private != NULL) {
2891 lua_newtable(L);
2892 handle_psdict(L, sf->private);
2893 } else {
2894 lua_pushnil(L);
2895 }
2896 break;
2897 case FK_xuid:
2898 lua_pushstring(L, sf->xuid);
2899 break;
2900 case FK_pfminfo:
2901 lua_createtable(L, 0, 40);
2902 handle_pfminfo(L, sf->pfminfo);
2903 break;
2904 case FK_names:
2905 if (sf->names != NULL) {
2906 lua_newtable(L);
2907 handle_ttflangname(L, sf->names);
2908 } else {
2909 lua_pushnil(L);
2910 }
2911 break;
2912 case FK_cidinfo:
2913 lua_createtable(L, 0, 4);
2914 dump_stringfield(L, "registry", sf->cidregistry);
2915 dump_stringfield(L, "ordering", sf->ordering);
2916 dump_intfield(L, "version", sf->cidversion);
2917 dump_intfield(L, "supplement", sf->supplement);
2918 break;
2919 case FK_subfonts:
2920 if (sf->subfontcnt > 0) {
2921 lua_createtable(L, sf->subfontcnt, 0);
2922 for (k = 0; k < sf->subfontcnt; k++) {
2923 lua_ff_pushsubfont(L, sf->subfonts[k]);
2924 lua_rawseti(L, -2, (k + 1));
2925 }
2926 } else {
2927 lua_pushnil(L);
2928 }
2929 break;
2930 case FK_comments:
2931 lua_pushstring(L, sf->comments);
2932 break;
2933 case FK_fontlog:
2934 lua_pushstring(L, sf->fontlog);
2935 break;
2936 case FK_cvt_names:
2937 if (sf->cvt_names != NULL) {
2938 lua_newtable(L);
2939 for (k = 0; sf->cvt_names[k] != END_CVT_NAMES; ++k) {
2940 lua_pushstring(L, sf->cvt_names[k]);
2941 lua_rawseti(L, -2, (k + 1));
2942 }
2943 } else {
2944 lua_pushnil(L);
2945 }
2946 break;
2947 case FK_ttf_tables:
2948 if (sf->ttf_tables != NULL) {
2949 lua_newtable(L);
2950 handle_ttf_table(L, sf->ttf_tables);
2951 } else {
2952 lua_pushnil(L);
2953 }
2954 break;
2955 case FK_ttf_tab_saved:
2956 if (sf->ttf_tab_saved != NULL) {
2957 lua_newtable(L);
2958 handle_ttf_table(L, sf->ttf_tab_saved);
2959 } else {
2960 lua_pushnil(L);
2961 }
2962 break;
2963 case FK_texdata:
2964 if (sf->texdata.type != tex_unset) {
2965 lua_newtable(L);
2966 dump_enumfield(L, "type", sf->texdata.type, tex_type_enum);
2967 lua_newtable(L);
2968 for (k = 0; k < 22; k++) {
2969 lua_pushinteger(L, k);
2970 lua_pushinteger(L, sf->texdata.params[k]);
2971 lua_rawset(L, -3);
2972 }
2973 lua_setfield(L, -2, "params");
2974 } else {
2975 lua_pushnil(L);
2976 }
2977 break;
2978 case FK_anchor_classes:
2979 if (sf->anchor != NULL) {
2980 lua_newtable(L);
2981 handle_anchorclass(L, sf->anchor);
2982 } else {
2983 lua_pushnil(L);
2984 }
2985 break;
2986 case FK_kerns:
2987 if (sf->kerns != NULL) {
2988 lua_newtable(L);
2989 handle_kernclass(L, sf->kerns, NULL);
2990 } else {
2991 lua_pushnil(L);
2992 }
2993 break;
2994 case FK_vkerns:
2995 if (sf->vkerns != NULL) {
2996 lua_newtable(L);
2997 handle_kernclass(L, sf->vkerns, NULL);
2998 } else {
2999 lua_pushnil(L);
3000 }
3001 break;
3002 case FK_gsub:
3003 if (sf->gsub_lookups != NULL) {
3004 lua_newtable(L);
3005 handle_lookup(L, sf->gsub_lookups, sf);
3006 } else {
3007 lua_pushnil(L);
3008 }
3009 break;
3010 case FK_gpos:
3011 if (sf->gpos_lookups != NULL) {
3012 lua_newtable(L);
3013 handle_lookup(L, sf->gpos_lookups, sf);
3014 } else {
3015 lua_pushnil(L);
3016 }
3017 break;
3018 case FK_mm:
3019 if (sf->mm != NULL) {
3020 lua_newtable(L);
3021 handle_mmset(L, sf->mm);
3022 } else {
3023 lua_pushnil(L);
3024 }
3025 break;
3026 case FK_chosenname:
3027 lua_pushstring(L, sf->chosenname);
3028 break;
3029 case FK_macstyle:
3030 lua_pushinteger(L, sf->macstyle);
3031 break;
3032 case FK_fondname:
3033 lua_pushstring(L, sf->fondname);
3034 break;
3035 case FK_design_size:
3036 lua_pushinteger(L, sf->design_size);
3037 break;
3038 case FK_fontstyle_id:
3039 lua_pushinteger(L, sf->fontstyle_id);
3040 break;
3041 case FK_fontstyle_name:
3042 if (sf->fontstyle_name != NULL) {
3043 lua_newtable(L);
3044 handle_otfname(L, sf->fontstyle_name);
3045 } else {
3046 lua_pushnil(L);
3047 }
3048 break;
3049 case FK_design_range_bottom:
3050 lua_pushinteger(L, sf->design_range_bottom);
3051 break;
3052 case FK_design_range_top:
3053 lua_pushinteger(L, sf->design_range_top);
3054 break;
3055 case FK_strokewidth:
3056 lua_pushinteger(L, sf->strokewidth);
3057 break;
3058 case FK_mark_classes:
3059 if (sf->mark_class_cnt > 0) {
3060 lua_newtable(L);
3061 for (k = 0; k < sf->mark_class_cnt; k++) {
3062 if (sf->mark_class_names[k] != NULL) {
3063 lua_pushstring(L, sf->mark_class_names[k]);
3064 lua_pushstring(L, sf->mark_classes[k]);
3065 lua_rawset(L, -3);
3066 }
3067 }
3068 } else {
3069 lua_pushnil(L);
3070 }
3071 break;
3072 case FK_creationtime:
3073 lua_pushinteger(L, sf->creationtime);
3074 break;
3075 case FK_modificationtime:
3076 lua_pushinteger(L, sf->modificationtime);
3077 break;
3078 case FK_os2_version:
3079 lua_pushinteger(L, sf->os2_version);
3080 break;
3081 case FK_sfd_version:
3082 lua_pushinteger(L, sf->sfd_version);
3083 break;
3084 case FK_math:
3085 if (sf->MATH != NULL) {
3086 lua_newtable(L);
3087 handle_MATH(L, sf->MATH);
3088 } else {
3089 lua_pushnil(L);
3090 }
3091 break;
3092 case FK_validation_state:
3093 if (sf->loadvalidation_state != 0) {
3094 int val, st;
3095 lua_newtable(L);
3096 val = 1;
3097 st = sf->loadvalidation_state;
3098 if (st & lvs_bad_ps_fontname) {
3099 lua_pushliteral(L, "bad_ps_fontname");
3100 lua_rawseti(L, -2, val++);
3101 }
3102 if (st & lvs_bad_glyph_table) {
3103 lua_pushliteral(L, "bad_glyph_table");
3104 lua_rawseti(L, -2, val++);
3105 }
3106 if (st & lvs_bad_cff_table) {
3107 lua_pushliteral(L, "bad_cff_table");
3108 lua_rawseti(L, -2, val++);
3109 }
3110 if (st & lvs_bad_metrics_table) {
3111 lua_pushliteral(L, "bad_metrics_table");
3112 lua_rawseti(L, -2, val++);
3113 }
3114 if (st & lvs_bad_cmap_table) {
3115 lua_pushliteral(L, "bad_cmap_table");
3116 lua_rawseti(L, -2, val++);
3117 }
3118 if (st & lvs_bad_bitmaps_table) {
3119 lua_pushliteral(L, "bad_bitmaps_table");
3120 lua_rawseti(L, -2, val++);
3121 }
3122 if (st & lvs_bad_gx_table) {
3123 lua_pushliteral(L, "bad_gx_table");
3124 lua_rawseti(L, -2, val++);
3125 }
3126 if (st & lvs_bad_ot_table) {
3127 lua_pushliteral(L, "bad_ot_table");
3128 lua_rawseti(L, -2, val++);
3129 }
3130 if (st & lvs_bad_os2_version) {
3131 lua_pushliteral(L, "bad_os2_version");
3132 lua_rawseti(L, -2, val++);
3133 }
3134 if (st & lvs_bad_sfnt_header) {
3135 lua_pushliteral(L, "bad_sfnt_header");
3136 lua_rawseti(L, -2, val++);
3137 }
3138 } else {
3139 lua_pushnil(L);
3140 }
3141 break;
3142 case FK_horiz_base:
3143 if (sf->horiz_base != NULL) {
3144 lua_newtable(L);
3145 handle_base(L, sf->horiz_base);
3146 } else {
3147 lua_pushnil(L);
3148 }
3149 break;
3150 case FK_vert_base:
3151 if (sf->vert_base != NULL) {
3152 lua_newtable(L);
3153 handle_base(L, sf->vert_base);
3154 } else {
3155 lua_pushnil(L);
3156 }
3157 break;
3158 case FK_extrema_bound:
3159 lua_pushinteger(L, sf->extrema_bound);
3160 break;
3161 case FK_notdef_loc:
3162 lua_pushinteger(L, notdef_loc(sf));
3163 break;
3164 default:
3165 /* can't actually happen, |luaL_checkoption| raises an error instead */
3166 lua_pushnil(L);
3167 break;
3168 }
3169 return 1;
3170 }
3171
3172 static int ff_info(lua_State * L)
3173 {
3174 SplineFont *sf;
3175 FILE *l;
3176 int i;
3177 const char *fontname;
3178 char *fontnamecopy;
3179 int openflags = 1;
3180 fontname = luaL_checkstring(L, 1);
3181 if (!strlen(fontname)) {
3182 lua_pushnil(L);
3183 lua_pushfstring(L, "font loading failed: empty string given\n", fontname);
3184 return 2;
3185 }
3186 /* test fontname for existance */
3187 if ((l = fopen(fontname, "r"))) {
3188 recorder_record_input(fontname);
3189 fclose(l);
3190 } else {
3191 lua_pushnil(L);
3192 lua_pushfstring(L, "font loading failed for %s (read error)\n", fontname);
3193 return 2;
3194 }
3195 gww_error_count = 0;
3196 fontnamecopy = xstrdup(fontname);
3197 sf = ReadSplineFontInfo(fontnamecopy, openflags);
3198 free(fontnamecopy);
3199 if (gww_error_count > 0)
3200 gwwv_errors_free();
3201
3202 if (sf == NULL) {
3203 lua_pushnil(L);
3204 lua_pushfstring(L, "font loading failed for %s\n", fontname);
3205 return 2;
3206 } else {
3207 if (sf->next != NULL) {
3208 SplineFont *sf_next;
3209 i = 1;
3210 lua_newtable(L);
3211 while (sf) {
3212 do_ff_info(L, sf);
3213 lua_rawseti(L, -2, i);
3214 i++;
3215 sf_next = sf->next;
3216 EncMapFree(sf->map);
3217 SplineFontFree(sf);
3218 sf = sf_next;
3219 }
3220 } else {
3221 do_ff_info(L, sf);
3222 EncMapFree(sf->map);
3223 SplineFontFree(sf);
3224 }
3225 }
3226 return 1;
3227 }
3228
3229 static void ff_do_cff(SplineFont * sf, char *filename, unsigned char **buf,
3230 int *bufsiz)
3231 {
3232 FILE *f;
3233 int32 *bsizes = NULL;
3234 int flags = ps_flag_nocffsugar + ps_flag_nohints;
3235 EncMap *map;
3236
3237 map = EncMap1to1(sf->glyphcnt);
3238
3239 if (WriteTTFFont
3240 (filename, sf, ff_cff, bsizes, bf_none, flags, map, ly_fore)) {
3241 /* success */
3242 f = fopen(filename, "rb");
3243 recorder_record_input(filename);
3244 readbinfile(f, buf, bufsiz);
3245 /*fprintf(stdout,"\n%s => CFF, size: %d\n", sf->filename, *bufsiz); */
3246 fclose(f);
3247 return;
3248 }
3249 formatted_error("fontloader","%s to CFF conversion failed", sf->filename);
3250 }
3251
3252 /* exported for writecff.c */
3253
3254 int ff_createcff(char *file, unsigned char **buf, int *bufsiz)
3255 {
3256 SplineFont *sf;
3257 int k;
3258 char s[] = "tempfile.cff";
3259 int openflags = 1;
3260 int notdefpos = 0;
3261 sf = ReadSplineFont(file, openflags);
3262 if (sf) {
3263 /* this is not the best way. nicer to have no temp file at all */
3264 ff_do_cff(sf, s, buf, bufsiz);
3265 for (k = 0; k < sf->glyphcnt; k++) {
3266 if (sf->glyphs[k] && strcmp(sf->glyphs[k]->name, ".notdef") == 0) {
3267 notdefpos = k;
3268 break;
3269 }
3270 }
3271 remove(s);
3272 EncMapFree(sf->map);
3273 SplineFontFree(sf);
3274 }
3275 return notdefpos;
3276 }
3277
3278 int ff_get_ttc_index(char *ffname, char *psname)
3279 {
3280 SplineFont *sf;
3281 int i = 0;
3282 int openflags = 1;
3283 int index = -1;
3284
3285 sf = ReadSplineFontInfo((char *) ffname, openflags);
3286 if (sf == NULL) {
3287 normal_error("fontloader","font loading failed unexpectedly");
3288 }
3289 while (sf != NULL) {
3290 if (strcmp(sf->fontname, psname) == 0) {
3291 index = i;
3292 }
3293 i++;
3294 sf = sf->next;
3295 }
3296 if (index>=0)
3297 return (i-index-1);
3298 return -1;
3299 }
3300
3301 static struct luaL_Reg fllib[] = {
3302 {"open", ff_open},
3303 {"info", ff_info},
3304 {"close", ff_close},
3305 {"fields", ff_fields},
3306 {"apply_afmfile", ff_apply_afmfile},
3307 {"apply_featurefile", ff_apply_featurefile},
3308 {"to_table", ff_make_table},
3309 {NULL, NULL}
3310 };
3311
3312 static const struct luaL_Reg fflib_m[] = {
3313 {"__gc", ff_close}, /* doesnt work yet! */
3314 {"__index", ff_index},
3315 {NULL, NULL} /* sentinel */
3316 };
3317
3318 int luaopen_ff(lua_State * L)
3319 {
3320 static char coord_sep_string[] = ",";
3321 static char SaveTablesPref_string[] = "VORG,JSTF,acnt,bsln,fdsc,fmtx,hsty,just,trak,Zapf,LINO";
3322 InitSimpleStuff();
3323 setlocale(LC_ALL, "C"); /* undo whatever InitSimpleStuff has caused */
3324 coord_sep = coord_sep_string;
3325 FF_SetUiInterface(&luaui_interface);
3326 default_encoding = FindOrMakeEncoding("ISO8859-1");
3327 SaveTablesPref = SaveTablesPref_string;
3328 luaL_newmetatable(L, FONT_METATABLE);
3329 luaL_openlib(L, NULL, fflib_m, 0);
3330
3331 /* virtual subfont table */
3332 luaL_newmetatable(L, FONT_SUBFONT_METATABLE);
3333 lua_pushstring(L, "__index");
3334 lua_pushcfunction(L, ff_index);
3335 lua_rawset(L, -3);
3336 lua_pop(L, 1);
3337 /* virtual glyphs table */
3338 luaL_newmetatable(L, FONT_GLYPHS_METATABLE);
3339 lua_pushstring(L, "__index");
3340 lua_pushcfunction(L, ff_glyphs_index);
3341 lua_rawset(L, -3);
3342 lua_pop(L, 1);
3343 /* virtual glyph table */
3344 luaL_newmetatable(L, FONT_GLYPH_METATABLE);
3345 lua_pushstring(L, "__index");
3346 lua_pushcfunction(L, ff_glyph_index);
3347 lua_rawset(L, -3);
3348 lua_pop(L, 1);
3349
3350 luaL_openlib(L, "fontloader", fllib, 0);
3351
3352 return 1;
3353 }