Debian Codegraph @

texk/web2c/luatexdir/font/luafont.c

raw
1 /*
2
3 Copyright 2006-2011 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
22 #include "ptexlib.h"
23 #include "lua/luatex-api.h"
24
25 #define noVERBOSE
26
27 /*tex
28
29 Todo: make these keys.
30
31 */
32
33 const char *font_type_strings[] = {
34 "unknown", "virtual", "real", NULL
35 };
36
37 const char *font_writingmode_strings[] = {
38 "unknown", "horizontal", "vertical", NULL
39 };
40
41 const char *font_identity_strings[] = {
42 "unknown", "horizontal", "vertical", NULL
43 };
44
45 const char *font_format_strings[] = {
46 "unknown", "type1", "type3", "truetype", "opentype", NULL
47 };
48
49 const char *font_embedding_strings[] = {
50 "unknown", "no", "subset", "full", NULL
51 };
52
53 const char *ligature_type_strings[] = {
54 "=:", "=:|", "|=:", "|=:|", "", "=:|>", "|=:>", "|=:|>", "", "", "", "|=:|>>", NULL
55 };
56
57 const char *MATH_param_names[] = {
58 "nil",
59 "ScriptPercentScaleDown",
60 "ScriptScriptPercentScaleDown",
61 "DelimitedSubFormulaMinHeight",
62 "DisplayOperatorMinHeight",
63 "MathLeading",
64 "AxisHeight",
65 "AccentBaseHeight",
66 "FlattenedAccentBaseHeight",
67 "SubscriptShiftDown",
68 "SubscriptTopMax",
69 "SubscriptBaselineDropMin",
70 "SuperscriptShiftUp",
71 "SuperscriptShiftUpCramped",
72 "SuperscriptBottomMin",
73 "SuperscriptBaselineDropMax",
74 "SubSuperscriptGapMin",
75 "SuperscriptBottomMaxWithSubscript",
76 "SpaceAfterScript",
77 "UpperLimitGapMin",
78 "UpperLimitBaselineRiseMin",
79 "LowerLimitGapMin",
80 "LowerLimitBaselineDropMin",
81 "StackTopShiftUp",
82 "StackTopDisplayStyleShiftUp",
83 "StackBottomShiftDown",
84 "StackBottomDisplayStyleShiftDown",
85 "StackGapMin",
86 "StackDisplayStyleGapMin",
87 "StretchStackTopShiftUp",
88 "StretchStackBottomShiftDown",
89 "StretchStackGapAboveMin",
90 "StretchStackGapBelowMin",
91 "FractionNumeratorShiftUp",
92 "FractionNumeratorDisplayStyleShiftUp",
93 "FractionDenominatorShiftDown",
94 "FractionDenominatorDisplayStyleShiftDown",
95 "FractionNumeratorGapMin",
96 "FractionNumeratorDisplayStyleGapMin",
97 "FractionRuleThickness",
98 "FractionDenominatorGapMin",
99 "FractionDenominatorDisplayStyleGapMin",
100 "SkewedFractionHorizontalGap",
101 "SkewedFractionVerticalGap",
102 "OverbarVerticalGap",
103 "OverbarRuleThickness",
104 "OverbarExtraAscender",
105 "UnderbarVerticalGap",
106 "UnderbarRuleThickness",
107 "UnderbarExtraDescender",
108 "RadicalVerticalGap",
109 "RadicalDisplayStyleVerticalGap",
110 "RadicalRuleThickness",
111 "RadicalExtraAscender",
112 "RadicalKernBeforeDegree",
113 "RadicalKernAfterDegree",
114 "RadicalDegreeBottomRaisePercent",
115 "MinConnectorOverlap",
116 "SubscriptShiftDownWithSuperscript",
117 "FractionDelimiterSize",
118 "FractionDelimiterDisplayStyleSize",
119 "NoLimitSubFactor",
120 "NoLimitSupFactor",
121 NULL,
122 };
123
124 int ff_checkoption (lua_State *L, int narg, const char *def, const char *const lst[]);
125
126 int ff_checkoption (lua_State *L, int narg, const char *def, const char *const lst[]) {
127 const char *name = (def) ? luaL_optstring(L, narg, def) : luaL_checkstring(L, narg);
128 int i;
129 for (i=0; lst[i]; i++)
130 if (strcmp(lst[i], name) == 0)
131 return i;
132 return -1;
133 }
134
135 #define dump_intfield(L,n,c) \
136 lua_push_string_by_name(L,n); \
137 lua_pushinteger(L, c); \
138 lua_rawset(L, -3); \
139
140 #define dump_stringfield(L,n,c) \
141 lua_push_string_by_name(L,n); \
142 lua_pushstring(L, c); \
143 lua_rawset(L, -3);
144
145 #define dump_booleanfield(L,n,c) \
146 lua_push_string_by_name(L,n); \
147 lua_pushboolean(L, c); \
148 lua_rawset(L, -3);
149
150 static void dump_math_kerns(lua_State * L, charinfo * co, int l, int id)
151 {
152 int i;
153 for (i = 0; i < l; i++) {
154 lua_newtable(L);
155 if (id==top_left_kern) {
156 dump_intfield(L, height, co->top_left_math_kern_array[(2*i)]);
157 dump_intfield(L, kern, co->top_left_math_kern_array[(2*i)+1]);
158 } else if (id==top_right_kern) {
159 dump_intfield(L, height, co->top_right_math_kern_array[(2*i)]);
160 dump_intfield(L, kern, co->top_right_math_kern_array[(2*i)+1]);
161 } else if (id==bottom_right_kern) {
162 dump_intfield(L, height, co->bottom_right_math_kern_array[(2*i)]);
163 dump_intfield(L, kern, co->bottom_right_math_kern_array[(2*i)+1]);
164 } else if (id==bottom_left_kern) {
165 dump_intfield(L, height, co->bottom_left_math_kern_array[(2*i)]);
166 dump_intfield(L, kern, co->bottom_left_math_kern_array[(2*i)+1]);
167 }
168 lua_rawseti(L, -2, (i + 1));
169 }
170 }
171
172 #define set_packet_number(fw) { \
173 fw = *(vfp++); \
174 fw = fw * 256 + *(vfp++); \
175 fw = fw * 256 + *(vfp++); \
176 fw = fw * 256 + *(vfp++); \
177 }
178
179 #define set_packet_scaled(fw) { \
180 fw = *(vfp++); \
181 if (fw > 127) { \
182 fw = fw - 256; \
183 } \
184 fw = fw * 256 + *(vfp++); \
185 fw = fw * 256 + *(vfp++); \
186 fw = fw * 256 + *(vfp++); \
187 }
188
189 static void font_commands_to_lua(lua_State * L, internal_font_number f, charinfo * co)
190 {
191 eight_bits *vfp = get_charinfo_packets(co);
192 if (vfp != NULL) {
193 int cmd;
194 int i = 1;
195 lua_push_string_by_name(L,commands);
196 lua_newtable(L);
197 while ((cmd = *(vfp++)) != packet_end_code) {
198 switch (cmd) {
199 case packet_font_code:
200 {
201 halfword f ;
202 set_packet_number(f);
203 lua_createtable(L, 2, 0);
204 lua_push_string_by_name(L,font);
205 lua_rawseti(L, -2, 1);
206 lua_pushinteger(L, f);
207 lua_rawseti(L, -2, 2);
208 lua_rawseti(L, -2, i++);
209 }
210 break;
211 case packet_push_code:
212 {
213 lua_createtable(L, 1, 0);
214 lua_push_string_by_name(L,push);
215 lua_rawseti(L, -2, 1);
216 lua_rawseti(L, -2, i++);
217 }
218 break;
219 case packet_pop_code:
220 {
221 lua_createtable(L, 1, 0);
222 lua_push_string_by_name(L,pop);
223 lua_rawseti(L, -2, 1);
224 lua_rawseti(L, -2, i++);
225 }
226 break;
227 case packet_char_code:
228 {
229 halfword c ;
230 set_packet_number(c);
231 lua_createtable(L, 2, 0);
232 lua_push_string_by_name(L,char);
233 lua_rawseti(L, -2, 1);
234 lua_pushinteger(L, c);
235 lua_rawseti(L, -2, 2);
236 lua_rawseti(L, -2, i++);
237 }
238 break;
239 case packet_rule_code:
240 {
241 halfword h, w ;
242 set_packet_scaled(h);
243 set_packet_scaled(w);
244 lua_createtable(L, 3, 0);
245 lua_push_string_by_name(L,rule);
246 lua_rawseti(L, -2, 1);
247 lua_pushinteger(L, h);
248 lua_rawseti(L, -2, 2);
249 lua_pushinteger(L, w);
250 lua_rawseti(L, -2, 3);
251 lua_rawseti(L, -2, i++);
252 }
253 break;
254 case packet_right_code:
255 {
256 halfword r ;
257 set_packet_scaled(r);
258 lua_createtable(L, 1, 0);
259 lua_push_string_by_name(L,right);
260 lua_rawseti(L, -2, 1);
261 lua_pushinteger(L, r);
262 lua_rawseti(L, -2, 2);
263 lua_rawseti(L, -2, i++);
264 }
265 break;
266 case packet_down_code:
267 {
268 halfword d ;
269 set_packet_scaled(d);
270 lua_createtable(L, 1, 0);
271 lua_push_string_by_name(L,down);
272 lua_rawseti(L, -2, 1);
273 lua_pushinteger(L, d);
274 lua_rawseti(L, -2, 2);
275 lua_rawseti(L, -2, i++);
276 }
277 break;
278 case packet_pdf_code:
279 {
280 halfword m, l;
281 set_packet_number(m);
282 set_packet_number(l);
283 lua_createtable(L, 3, 0);
284 lua_push_string_by_name(L,pdf);
285 lua_rawseti(L, -2, 1);
286 lua_pushinteger(L, m);
287 lua_rawseti(L, -2, 2);
288 lua_pushstring(L,"<pdf data>");
289 lua_rawseti(L, -2, 3);
290 lua_rawseti(L, -2, i++);
291 vfp += l;
292 }
293 break;
294 case packet_pdf_mode:
295 {
296 halfword m;
297 set_packet_number(m);
298 lua_createtable(L, 2, 0);
299 lua_push_string_by_name(L,mode);
300 lua_rawseti(L, -2, 1);
301 lua_pushinteger(L, m);
302 lua_rawseti(L, -2, 2);
303 lua_rawseti(L, -2, i++);
304 }
305 break;
306 case packet_special_code:
307 {
308 halfword l;
309 set_packet_number(l);
310 lua_createtable(L, 2, 0);
311 lua_push_string_by_name(L,special);
312 lua_rawseti(L, -2, 1);
313 lua_pushstring(L,"<special data>");
314 lua_rawseti(L, -2, 2);
315 lua_rawseti(L, -2, i++);
316 vfp += l;
317 }
318 break;
319 case packet_lua_code:
320 {
321 halfword n;
322 set_packet_number(n);
323 lua_createtable(L, 2, 0);
324 lua_push_string_by_name(L,lua);
325 lua_rawseti(L, -2, 1);
326 lua_pushstring(L,"<lua data>");
327 lua_rawseti(L, -2, 2);
328 lua_rawseti(L, -2, i++);
329 }
330 break;
331 case packet_image_code:
332 {
333 halfword n;
334 set_packet_number(n);
335 lua_createtable(L, 2, 0);
336 lua_push_string_by_name(L,image);
337 lua_rawseti(L, -2, 1);
338 lua_pushstring(L,"<image data>");
339 lua_rawseti(L, -2, 2);
340 lua_rawseti(L, -2, i++);
341 }
342 break;
343 case packet_node_code:
344 {
345 halfword n;
346 set_packet_number(n);
347 lua_createtable(L, 2, 0);
348 lua_push_string_by_name(L,node);
349 lua_rawseti(L, -2, 1);
350 lua_pushstring(L,"<node data>");
351 lua_rawseti(L, -2, 2);
352 lua_rawseti(L, -2, i++);
353 }
354 break;
355 case packet_nop_code:
356 {
357 lua_createtable(L, 1, 0);
358 lua_push_string_by_name(L,nop);
359 lua_rawseti(L, -2, 1);
360 lua_rawseti(L, -2, i++);
361 }
362 break;
363 case packet_scale_code:
364 break;
365 default:
366 break;
367 }
368 }
369 lua_rawset(L, -3);
370 }
371 }
372
373 static void font_char_to_lua(lua_State * L, internal_font_number f, charinfo * co)
374 {
375 liginfo *l;
376 kerninfo *ki;
377 lua_createtable(L, 0, 10);
378 dump_intfield(L,width,get_charinfo_width(co));
379 dump_intfield(L,height,get_charinfo_height(co));
380 dump_intfield(L,depth,get_charinfo_depth(co));
381 if (get_charinfo_italic(co) != 0) {
382 dump_intfield(L,italic,get_charinfo_italic(co));
383 }
384 if (get_charinfo_vert_italic(co) != 0) {
385 dump_intfield(L,vert_italic,get_charinfo_vert_italic(co));
386 }
387 if (get_charinfo_top_accent(co) !=0 && get_charinfo_top_accent(co) != INT_MIN) {
388 dump_intfield(L,top_accent,get_charinfo_top_accent(co));
389 }
390 if (get_charinfo_bot_accent(co) != 0 && get_charinfo_bot_accent(co) != INT_MIN) {
391 dump_intfield(L,bot_accent,get_charinfo_bot_accent(co));
392 }
393 if (get_charinfo_ef(co) != 1000) {
394 dump_intfield(L,expansion_factor,get_charinfo_ef(co));
395 }
396 if (get_charinfo_lp(co) != 0) {
397 dump_intfield(L,left_protruding,get_charinfo_lp(co));
398 }
399 if (get_charinfo_rp(co) != 0) {
400 dump_intfield(L,right_protruding,get_charinfo_rp(co));
401 }
402 if (font_encodingbytes(f) == 2) {
403 dump_intfield(L,index,get_charinfo_index(co));
404 }
405 if (get_charinfo_name(co) != NULL) {
406 dump_stringfield(L,name,get_charinfo_name(co));
407 }
408 if (get_charinfo_tounicode(co) != NULL) {
409 dump_stringfield(L,tounicode,get_charinfo_tounicode(co));
410 }
411 if (get_charinfo_tag(co) == list_tag) {
412 dump_intfield(L,next,get_charinfo_remainder(co));
413 }
414 if (get_charinfo_used(co)) {
415 dump_booleanfield(L,used,(get_charinfo_used(co) ? true : false));
416 }
417 if (get_charinfo_tag(co) == ext_tag) {
418 extinfo *h;
419 h = get_charinfo_hor_variants(co);
420 if (h != NULL) {
421 int i = 1;
422 lua_push_string_by_name(L,horiz_variants);
423 lua_newtable(L);
424 while (h != NULL) {
425 lua_createtable(L, 0, 5);
426 dump_intfield(L, glyph, h->glyph);
427 dump_intfield(L, extender, h->extender);
428 dump_intfield(L, start, h->start_overlap);
429 dump_intfield(L, end, h->end_overlap);
430 dump_intfield(L, advance, h->advance);
431 lua_rawseti(L, -2, i);
432 i++;
433 h = h->next;
434 }
435 lua_rawset(L, -3);
436 }
437 h = get_charinfo_vert_variants(co);
438 if (h != NULL) {
439 int i = 1;
440 lua_push_string_by_name(L,vert_variants);
441 lua_newtable(L);
442 while (h != NULL) {
443 lua_createtable(L, 0, 5);
444 dump_intfield(L, glyph, h->glyph);
445 dump_intfield(L, extender, h->extender);
446 dump_intfield(L, start, h->start_overlap);
447 dump_intfield(L, end, h->end_overlap);
448 dump_intfield(L, advance, h->advance);
449 lua_rawseti(L, -2, i);
450 i++;
451 h = h->next;
452 }
453 lua_rawset(L, -3);
454 }
455 }
456 ki = get_charinfo_kerns(co);
457 if (ki != NULL) {
458 int i;
459 lua_push_string_by_name(L,kerns);
460 lua_createtable(L, 10, 1);
461 for (i = 0; !kern_end(ki[i]); i++) {
462 if (kern_disabled(ki[i])) {
463 /*tex Skip like in lookup. */
464 } else {
465 lua_rawgeti(L, -1, kern_char(ki[i]));
466 if (lua_type(L,-1) == LUA_TNIL) {
467 lua_pop(L,1);
468 if (kern_char(ki[i]) == right_boundarychar) {
469 lua_push_string_by_name(L,right_boundary);
470 } else {
471 lua_pushinteger(L, kern_char(ki[i]));
472 }
473 lua_pushinteger(L, kern_kern(ki[i]));
474 lua_rawset(L, -3);
475 } else {
476 /*tex The first one wins. */
477 lua_pop(L,1);
478 }
479 }
480 }
481 lua_rawset(L, -3);
482 }
483 l = get_charinfo_ligatures(co);
484 if (l != NULL) {
485 int i;
486 lua_push_string_by_name(L,ligatures);
487 lua_createtable(L, 10, 1);
488 for (i = 0; !lig_end(l[i]); i++) {
489 if (lig_char(l[i]) == right_boundarychar) {
490 lua_push_string_by_name(L,right_boundary);
491 } else {
492 lua_pushinteger(L, lig_char(l[i]));
493 }
494 lua_createtable(L, 0, 2);
495 lua_push_string_by_name(L,type);
496 lua_pushinteger(L, lig_type(l[i]));
497 lua_rawset(L, -3);
498 lua_push_string_by_name(L,char);
499 lua_pushinteger(L, lig_replacement(l[i]));
500 lua_rawset(L, -3);
501 lua_rawset(L, -3);
502 }
503 lua_rawset(L, -3);
504 }
505 lua_push_string_by_name(L,mathkern);
506 lua_newtable(L);
507 {
508 int i, j;
509 i = get_charinfo_math_kerns(co, top_right_kern);
510 j = 0;
511 if (i > 0) {
512 j++;
513 lua_push_string_by_name(L,top_right);
514 lua_newtable(L);
515 dump_math_kerns(L, co, i, top_right_kern);
516 lua_rawset(L, -3);
517 }
518 i = get_charinfo_math_kerns(co, top_left_kern);
519 if (i > 0) {
520 j++;
521 lua_push_string_by_name(L,top_left);
522 lua_newtable(L);
523 dump_math_kerns(L, co, i, top_left_kern);
524 lua_rawset(L, -3);
525 }
526 i = get_charinfo_math_kerns(co, bottom_right_kern);
527 if (i > 0) {
528 j++;
529 lua_push_string_by_name(L,bottom_right);
530 lua_newtable(L);
531 dump_math_kerns(L, co, i, bottom_right_kern);
532 lua_rawset(L, -3);
533 }
534 i = get_charinfo_math_kerns(co, bottom_left_kern);
535 if (i > 0) {
536 j++;
537 lua_push_string_by_name(L,bottom_left);
538 lua_newtable(L);
539 dump_math_kerns(L, co, i, bottom_left_kern);
540 lua_rawset(L, -3);
541 }
542 if (j > 0)
543 lua_rawset(L, -3);
544 else
545 lua_pop(L, 2);
546 }
547 font_commands_to_lua(L,f,co);
548 }
549
550 static void write_lua_parameters(lua_State * L, int f)
551 {
552 int k;
553 lua_push_string_by_name(L,parameters);
554 lua_newtable(L);
555 for (k = 1; k <= font_params(f); k++) {
556 switch (k) {
557 case slant_code:
558 dump_intfield(L,slant,font_param(f, k));
559 break;
560 case space_code:
561 dump_intfield(L,space,font_param(f, k));
562 break;
563 case space_stretch_code:
564 dump_intfield(L,space_stretch,font_param(f, k));
565 break;
566 case space_shrink_code:
567 dump_intfield(L,space_shrink,font_param(f, k));
568 break;
569 case x_height_code:
570 dump_intfield(L,x_height,font_param(f, k));
571 break;
572 case quad_code:
573 dump_intfield(L,quad,font_param(f, k));
574 break;
575 case extra_space_code:
576 dump_intfield(L,extra_space,font_param(f, k));
577 break;
578 default:
579 lua_pushinteger(L, font_param(f, k));
580 lua_rawseti(L, -2, k);
581 }
582 }
583 lua_rawset(L, -3);
584 }
585
586 int font_parameters_to_lua(lua_State * L, int f)
587 {
588 int k;
589 lua_newtable(L);
590 for (k = 1; k <= font_params(f); k++) {
591 switch (k) {
592 case slant_code:
593 dump_intfield(L,slant,font_param(f, k));
594 break;
595 case space_code:
596 dump_intfield(L,space,font_param(f, k));
597 break;
598 case space_stretch_code:
599 dump_intfield(L,space_stretch,font_param(f, k));
600 break;
601 case space_shrink_code:
602 dump_intfield(L,space_shrink,font_param(f, k));
603 break;
604 case x_height_code:
605 dump_intfield(L,x_height,font_param(f, k));
606 break;
607 case quad_code:
608 dump_intfield(L,quad,font_param(f, k));
609 break;
610 case extra_space_code:
611 dump_intfield(L,extra_space,font_param(f, k));
612 break;
613 default:
614 lua_pushinteger(L, font_param(f, k));
615 lua_rawseti(L, -2, k);
616 }
617 }
618 return 1;
619 }
620
621 static void write_lua_math_parameters(lua_State * L, int f)
622 {
623 int k;
624 lua_push_string_by_name(L,MathConstants);
625 lua_newtable(L);
626 for (k = 1; k <= font_math_params(f); k++) {
627 lua_pushinteger(L, font_math_param(f, k));
628 if (k <= MATH_param_max) {
629 lua_setfield(L, -2, MATH_param_names[k]);
630 } else {
631 lua_rawseti(L, -2, k);
632 }
633 }
634 lua_rawset(L, -3);
635 }
636
637 int font_to_lua(lua_State * L, int f, int usecache)
638 {
639 int k;
640 charinfo *co;
641 if (usecache && font_cache_id(f) > 0) {
642 /*tex Fetch the table from the registry if it was saved there by |font_from_lua|. */
643 lua_rawgeti(L, LUA_REGISTRYINDEX, font_cache_id(f));
644 return 1;
645 }
646 lua_newtable(L);
647 lua_push_string_by_name(L,name);
648 lua_pushstring(L, font_name(f));
649 lua_rawset(L, -3);
650 if (font_area(f) != NULL) {
651 dump_stringfield(L,area,font_area(f));
652 }
653 if (font_filename(f) != NULL) {
654 dump_stringfield(L,filename,font_filename(f));
655 }
656 if (font_fullname(f) != NULL) {
657 dump_stringfield(L,fullname,font_fullname(f));
658 }
659 if (font_psname(f) != NULL) {
660 dump_stringfield(L,psname,font_psname(f));
661 }
662 if (font_encodingname(f) != NULL) {
663 dump_stringfield(L,encodingname,font_encodingname(f));
664 }
665 dump_booleanfield(L,used,(font_used(f) ? true : false));
666 dump_stringfield(L,type,font_type_strings[font_type(f)]);
667 dump_stringfield(L,format,font_format_strings[font_format(f)]);
668 dump_stringfield(L,writingmode,font_writingmode_strings[font_writingmode(f)]);
669 dump_stringfield(L,identity,font_identity_strings[font_identity(f)]);
670 dump_stringfield(L,embedding,font_embedding_strings[font_embedding(f)]);
671 dump_intfield(L,streamprovider,font_streamprovider(f));
672 dump_intfield(L,units_per_em,font_units_per_em(f));
673 dump_intfield(L,size,font_size(f));
674 dump_intfield(L,designsize,font_dsize(f));
675 dump_intfield(L,checksum,font_checksum(f));
676 dump_intfield(L,slant,font_slant(f));
677 dump_intfield(L,extend,font_extend(f));
678 dump_intfield(L,squeeze,font_squeeze(f));
679 dump_intfield(L,mode,font_mode(f));
680 dump_intfield(L,width,font_width(f));
681 dump_intfield(L,direction,font_natural_dir(f));
682 dump_intfield(L,encodingbytes,font_encodingbytes(f));
683 dump_intfield(L,subfont,font_subfont(f));
684 dump_booleanfield(L,oldmath,font_oldmath(f));
685 dump_intfield(L,tounicode,font_tounicode(f));
686 /*tex The next one is read only: */
687 if (font_max_shrink(f) != 0) {
688 dump_intfield(L,shrink,font_max_shrink(f));
689 }
690 if (font_max_stretch(f) != 0) {
691 dump_intfield(L,stretch,font_max_stretch(f));
692 }
693 if (font_step(f) != 0) {
694 dump_intfield(L,step,font_step(f));
695 }
696 if (pdf_font_attr(f) != 0) {
697 char *s = makecstring(pdf_font_attr(f));
698 dump_stringfield(L,attributes,s);
699 free(s);
700 }
701 /*tex Parameters: */
702 write_lua_parameters(L, f);
703 write_lua_math_parameters(L, f);
704 /*tex Characters: */
705 lua_push_string_by_name(L,characters);
706 lua_createtable(L, charinfo_size(f), 0);
707 if (has_left_boundary(f)) {
708 co = get_charinfo(f, left_boundarychar);
709 lua_push_string_by_name(L,left_boundary);
710 font_char_to_lua(L, f, co);
711 lua_rawset(L, -3);
712 }
713 if (has_right_boundary(f)) {
714 co = get_charinfo(f, right_boundarychar);
715 lua_push_string_by_name(L,right_boundary);
716 font_char_to_lua(L, f, co);
717 lua_rawset(L, -3);
718 }
719 for (k = font_bc(f); k <= font_ec(f); k++) {
720 if (quick_char_exists(f, k)) {
721 lua_pushinteger(L, k);
722 co = get_charinfo(f, k);
723 font_char_to_lua(L, f, co);
724 lua_rawset(L, -3);
725 }
726 }
727 lua_rawset(L, -3);
728 return 1;
729 }
730
731 #define count_hash_items(L,name,n) \
732 n = 0; \
733 lua_key_rawgeti(name); \
734 if (lua_type(L, -1) == LUA_TTABLE) { \
735 lua_pushnil(L); \
736 while (lua_next(L, -2) != 0) { \
737 n++; \
738 lua_pop(L, 1); \
739 } \
740 } \
741 if (n) { \
742 /*tex Keep the table on stack. */ \
743 } else{ \
744 lua_pop(L, 1); \
745 }
746
747 #define streq(a,b) (strcmp(a,b)==0)
748
749 #define append_packet(k) { *(cp++) = (eight_bits) (k); }
750
751 #define do_store_four(l) { \
752 append_packet((l & 0xFF000000) >> 24); \
753 append_packet((l & 0x00FF0000) >> 16); \
754 append_packet((l & 0x0000FF00) >> 8); \
755 append_packet((l & 0x000000FF)); \
756 }
757
758 static void append_float(eight_bits ** cpp, float a)
759 {
760 unsigned int i;
761 eight_bits *cp = *cpp;
762 union U {
763 float a;
764 eight_bits b[sizeof(float)];
765 } u;
766 u.a = a;
767 for (i = 0; i < sizeof(float); i++)
768 append_packet(u.b[i]);
769 *cpp = cp;
770 }
771
772 static int n_enum_field(lua_State * L, int name_index, int dflt, const char **values)
773 {
774 int k, t;
775 const char *s;
776 int i = dflt;
777 /*tex Fetch the string pointer: */
778 lua_rawgeti(L, LUA_REGISTRYINDEX, name_index);
779 lua_rawget(L, -2);
780 t = lua_type(L,-1);
781 if (t == LUA_TNUMBER) {
782 i = (int) lua_tointeger(L, -1);
783 } else if (t == LUA_TSTRING) {
784 s = lua_tostring(L, -1);
785 k = 0;
786 while (values[k] != NULL) {
787 if (strcmp(values[k], s) == 0) {
788 i = k;
789 break;
790 }
791 k++;
792 }
793 }
794 lua_pop(L, 1);
795 return i;
796 }
797
798 static int n_boolean_field(lua_State * L, int name_index, int dflt)
799 {
800 int i = dflt;
801 /*tex Fetch the string pointer: */
802 lua_rawgeti(L, LUA_REGISTRYINDEX, name_index);
803 lua_rawget(L, -2);
804 if (lua_isboolean(L, -1)) {
805 i = lua_toboolean(L, -1);
806 }
807 lua_pop(L, 1);
808 return i;
809 }
810
811 static char *n_string_field_copy(lua_State * L, int name_index, const char *dflt)
812 {
813 char *i;
814 /*tex Fetch the string pointer: */
815 lua_rawgeti(L, LUA_REGISTRYINDEX, name_index);
816 lua_rawget(L, -2);
817 if (lua_type(L,-1) == LUA_TSTRING) {
818 i = xstrdup(lua_tostring(L, -1));
819 } else if (dflt == NULL) {
820 i = NULL;
821 } else {
822 i = xstrdup(dflt);
823 }
824 lua_pop(L, 1);
825 return i;
826 }
827
828 static const char *n_string_field(lua_State * L, int name_index)
829 {
830 /*tex Fetch the string pointer: */
831 lua_rawgeti(L, LUA_REGISTRYINDEX, name_index);
832 lua_rawget(L, -2);
833 return lua_tostring(L,-1);
834 }
835
836 static int n_some_field(lua_State * L, int name_index)
837 {
838 /*tex Fetch the string pointer: */
839 lua_rawgeti(L, LUA_REGISTRYINDEX, name_index);
840 lua_rawget(L, -2);
841 return lua_type(L,-1);
842 }
843
844 static int count_char_packet_bytes(lua_State * L)
845 {
846 register int i;
847 register int ts;
848 register int l = 0;
849 int ff = 0;
850 for (i = 1; i <= (int) lua_rawlen(L, -1); i++) {
851 lua_rawgeti(L, -1, i);
852 if (lua_istable(L, -1)) {
853 lua_rawgeti(L, -1, 1);
854 if (lua_type(L,-1) == LUA_TSTRING) {
855 const char *s = lua_tostring(L, -1);
856 if (lua_key_eq(s, font)) {
857 l += 5;
858 ff = 1;
859 } else if (lua_key_eq(s, char)) {
860 if (ff == 0) {
861 l += 5;
862 }
863 l += 5;
864 ff = 1;
865 } else if (lua_key_eq(s, slot)) {
866 l += 10;
867 } else if (lua_key_eq(s, comment) || lua_key_eq(s, nop)) {
868 ;
869 } else if (lua_key_eq(s, push) || lua_key_eq(s, pop)) {
870 l++;
871 } else if (lua_key_eq(s, rule)) {
872 l += 9;
873 } else if (lua_key_eq(s, right) || lua_key_eq(s, node) || lua_key_eq(s, down) || lua_key_eq(s, image) || lua_key_eq(s, lua)) {
874 l += 5;
875 } else if (lua_key_eq(s, scale)) {
876 l += sizeof(float) + 1;
877 } else if (lua_key_eq(s, pdf)) {
878 size_t len;
879 l += 5;
880 ts = lua_rawlen(L, -2);
881 lua_rawgeti(L, -2, 2);
882 if (ts == 3) {
883 if (lua_type(L,-1) == LUA_TSTRING) {
884 /*tex There is no need to do something. */
885 } else if (lua_type(L,-1) == LUA_TNUMBER) {
886 /*tex There is no need to do something. */
887 } else {
888 normal_error("vf command","invalid packet pdf literal category");
889 }
890 lua_rawgeti(L, -3, 3);
891 }
892 if (lua_type(L,-1) == LUA_TSTRING) {
893 (void) lua_tolstring(L, -1, &len);
894 if (len > 0) {
895 l = (int) (l + 5 + (int) len);
896 }
897 } else {
898 normal_error("vf command","invalid packet pdf literal");
899 }
900 lua_pop(L, ts == 3 ? 2 : 1);
901 } else if (lua_key_eq(s, special)) {
902 size_t len;
903 lua_rawgeti(L, -2, 2);
904 if (lua_type(L,-1) == LUA_TSTRING) {
905 (void) lua_tolstring(L, -1, &len);
906 if (len > 0) {
907 l = (int) (l + 5 + (int) len);
908 }
909 } else {
910 normal_error("vf command","invalid packet special");
911 }
912 lua_pop(L, 1);
913 } else {
914 normal_error("vf command","unknown packet command");
915 }
916 } else {
917 normal_error("vf command","no packet command");
918 }
919 /*tex Pop the command name: */
920 lua_pop(L, 1);
921 }
922 /*tex Pop this item: */
923 lua_pop(L, 1);
924 }
925 return l;
926 }
927
928 static scaled sp_to_dvi(halfword sp, halfword atsize)
929 {
930 double result, mult;
931 mult = (double) (atsize / 65536.0);
932 result = (double) (sp * 16.0);
933 return floor(result / mult);
934 }
935
936 static void read_char_packets(lua_State * L, int *l_fonts, charinfo * co, internal_font_number f, int atsize)
937 {
938 int i, n, m;
939 size_t l;
940 int cmd;
941 const char *s;
942 eight_bits *cpackets, *cp;
943 int ff = 0;
944 int sf = 0;
945 int ts = 0;
946 int max_f = 0;
947 int pc = count_char_packet_bytes(L);
948 if (pc <= 0)
949 return;
950 if (l_fonts != NULL) {
951 while (l_fonts[(max_f + 1)] != 0) {
952 max_f++;
953 }
954 }
955 cp = cpackets = xmalloc((unsigned) (pc + 1));
956 for (i = 1; i <= (int) lua_rawlen(L, -1); i++) {
957 lua_rawgeti(L, -1, i);
958 if (lua_istable(L, -1)) {
959 /*tex fetch the command code */
960 lua_rawgeti(L, -1, 1);
961 if (lua_type(L,-1) == LUA_TSTRING) {
962 s = lua_tostring(L, -1);
963 cmd = 0;
964 if (lua_key_eq(s, font)) {
965 if (l_fonts == NULL) {
966 normal_error("vf command","no font table found");
967 }
968 cmd = packet_font_code;
969 } else if (lua_key_eq(s, char)) {
970 if (l_fonts == NULL) {
971 normal_error("vf command","no font table found");
972 }
973 cmd = packet_char_code;
974 if (ff == 0) {
975 append_packet(packet_font_code);
976 ff = l_fonts[1];
977 do_store_four(ff);
978 }
979 } else if (lua_key_eq(s, slot)) {
980 if (l_fonts == NULL) {
981 normal_error("vf command","no font table found");
982 }
983 /*tex we could be sparse but no real reason */
984 cmd = packet_nop_code;
985 lua_rawgeti(L, -2, 2);
986 n = (int) lua_roundnumber(L, -1);
987 if (n == 0) {
988 sf = f;
989 } else {
990 sf = (n > max_f ? l_fonts[1] : l_fonts[n]);
991 }
992 lua_rawgeti(L, -3, 3);
993 n = (int) lua_roundnumber(L, -1);
994 lua_pop(L, 2);
995 append_packet(packet_font_code);
996 do_store_four(sf);
997 append_packet(packet_char_code);
998 do_store_four(n);
999 } else if (lua_key_eq(s, comment) || lua_key_eq(s, nop)) {
1000 cmd = packet_nop_code;
1001 } else if (lua_key_eq(s, node)) {
1002 cmd = packet_node_code;
1003 } else if (lua_key_eq(s, push)) {
1004 cmd = packet_push_code;
1005 } else if (lua_key_eq(s, pop)) {
1006 cmd = packet_pop_code;
1007 } else if (lua_key_eq(s, rule)) {
1008 cmd = packet_rule_code;
1009 } else if (lua_key_eq(s, right)) {
1010 cmd = packet_right_code;
1011 } else if (lua_key_eq(s, down)) {
1012 cmd = packet_down_code;
1013 } else if (lua_key_eq(s, pdf)) {
1014 cmd = packet_pdf_code;
1015 } else if (lua_key_eq(s, special)) {
1016 cmd = packet_special_code;
1017 } else if (lua_key_eq(s, image)) {
1018 cmd = packet_image_code;
1019 } else if (lua_key_eq(s, scale)) {
1020 cmd = packet_scale_code;
1021 } else if (lua_key_eq(s, lua)) {
1022 cmd = packet_lua_code;
1023 }
1024 switch (cmd) {
1025 case packet_push_code:
1026 case packet_pop_code:
1027 append_packet(cmd);
1028 break;
1029 case packet_font_code:
1030 append_packet(cmd);
1031 lua_rawgeti(L, -2, 2);
1032 n = (int) lua_roundnumber(L, -1);
1033 if (n == 0) {
1034 ff = n;
1035 } else {
1036 ff = (n > max_f ? l_fonts[1] : l_fonts[n]);
1037 }
1038 do_store_four(ff);
1039 lua_pop(L, 1);
1040 break;
1041 case packet_node_code:
1042 append_packet(cmd);
1043 lua_rawgeti(L, -2, 2);
1044 n = copy_node_list(nodelist_from_lua(L,-1));
1045 do_store_four(n);
1046 lua_pop(L, 1);
1047 break;
1048 case packet_char_code:
1049 append_packet(cmd);
1050 lua_rawgeti(L, -2, 2);
1051 n = (int) lua_roundnumber(L, -1);
1052 do_store_four(n);
1053 lua_pop(L, 1);
1054 break;
1055 case packet_right_code:
1056 case packet_down_code:
1057 append_packet(cmd);
1058 lua_rawgeti(L, -2, 2);
1059 n = (int) lua_roundnumber(L, -1);
1060 do_store_four(sp_to_dvi(n, atsize));
1061 lua_pop(L, 1);
1062 break;
1063 case packet_rule_code:
1064 append_packet(cmd);
1065 lua_rawgeti(L, -2, 2);
1066 n = (int) lua_roundnumber(L, -1);
1067 do_store_four(sp_to_dvi(n, atsize));
1068 lua_rawgeti(L, -3, 3);
1069 n = (int) lua_roundnumber(L, -1);
1070 do_store_four(sp_to_dvi(n, atsize));
1071 lua_pop(L, 2);
1072 break;
1073 case packet_pdf_code:
1074 ts = (int) lua_rawlen(L, -2);
1075 lua_rawgeti(L, -2, 2);
1076 if (ts == 3) {
1077 /*tex mode on stack */
1078 s = lua_tostring(L, -1);
1079 if (lua_type(L, -1) == LUA_TSTRING) {
1080 /*tex |<pdf> <mode> <direct|page|text|raw|origin>| */
1081 if (lua_key_eq(s, mode)) {
1082 cmd = packet_pdf_mode;
1083 lua_rawgeti(L, -3, 3);
1084 /*tex mode on stack */
1085 s = lua_tostring(L, -1);
1086 }
1087 } else {
1088 /*tex |<pdf> <direct|page|text|raw|origin> <string>| */
1089 }
1090 if (lua_type(L, -1) == LUA_TSTRING) {
1091 if (lua_key_eq(s, direct)) {
1092 n = direct_always;
1093 } else if (lua_key_eq(s, page)) {
1094 n = direct_page;
1095 } else if (lua_key_eq(s, text)) {
1096 n = direct_text;
1097 } else if (lua_key_eq(s, font)) {
1098 n = direct_font;
1099 } else if (lua_key_eq(s, raw)) {
1100 n = direct_raw;
1101 } else if (lua_key_eq(s, origin)) {
1102 n = set_origin;
1103 } else {
1104 n = set_origin ;
1105 }
1106 } else {
1107 n = (int) lua_roundnumber(L, -1);
1108 if (n < set_origin || n >= scan_special) {
1109 n = set_origin ;
1110 }
1111 }
1112 if (cmd == packet_pdf_code) {
1113 /*tex string on stack */
1114 lua_rawgeti(L, -3, 3);
1115 }
1116 } else {
1117 n = set_origin;
1118 }
1119 append_packet(cmd);
1120 do_store_four(n);
1121 if (cmd == packet_pdf_code) {
1122 s = luaL_checklstring(L, -1, &l);
1123 do_store_four(l);
1124 if (l > 0) {
1125 m = (int) l;
1126 while (m > 0) {
1127 n = *s++;
1128 m--;
1129 append_packet(n);
1130 }
1131 }
1132 }
1133 lua_pop(L,ts == 3 ? 2 : 1);
1134 break;
1135 case packet_special_code:
1136 append_packet(cmd);
1137 lua_rawgeti(L, -2, 2);
1138 s = luaL_checklstring(L, -1, &l);
1139 if (l > 0) {
1140 do_store_four(l);
1141 m = (int) l;
1142 while (m > 0) {
1143 n = *s++;
1144 m--;
1145 append_packet(n);
1146 }
1147 }
1148 lua_pop(L, 1);
1149 break;
1150 case packet_lua_code:
1151 append_packet(cmd);
1152 lua_rawgeti(L, -2, 2);
1153 n = luaL_ref(L, LUA_REGISTRYINDEX);
1154 do_store_four(n);
1155 break;
1156 case packet_image_code:
1157 append_packet(cmd);
1158 lua_rawgeti(L, -2, 2);
1159 if (lua_istable(L, -1)) {
1160 lua_getglobal(L, "img");
1161 /*tex Just in case one accidentely wipes |img|: */
1162 if (lua_type(L,-1) != LUA_TTABLE) {
1163 normal_error("vf command","no img table found");
1164 }
1165 lua_pushstring(L, "new");
1166 lua_gettable(L, -2);
1167 /*tex or the |new| entry: */
1168 if (lua_type(L,-1) != LUA_TFUNCTION) {
1169 normal_error("vf command","no img.new function found");
1170 }
1171 lua_insert(L, -3);
1172 lua_pop(L, 1);
1173 lua_call(L, 1, 1);
1174 }
1175 luaL_checkudata(L, -1, TYPE_IMG);
1176 n = luaL_ref(L, LUA_REGISTRYINDEX);
1177 do_store_four(n);
1178 break;
1179 case packet_nop_code:
1180 break;
1181 case packet_scale_code:
1182 append_packet(cmd);
1183 lua_rawgeti(L, -2, 2);
1184 append_float(&cp, (float) luaL_checknumber(L, -1));
1185 lua_pop(L, 1);
1186 break;
1187 default:
1188 normal_error("vf command","invalid packet code");
1189 }
1190 }
1191 /*tex Command code: */
1192 lua_pop(L, 1);
1193 } else {
1194 normal_error("vf command","commands has to be a table");
1195 }
1196 /*tex Command table: */
1197 lua_pop(L, 1);
1198 }
1199 append_packet(packet_end_code);
1200 set_charinfo_packets(co, cpackets);
1201 return;
1202 }
1203
1204 static void read_lua_cidinfo(lua_State * L, int f)
1205 {
1206 int i;
1207 char *s;
1208 lua_key_rawgeti(cidinfo);
1209 if (lua_istable(L, -1)) {
1210 i = lua_numeric_field_by_index(L,lua_key_index(version), 0);
1211 set_font_cidversion(f, i);
1212 i = lua_numeric_field_by_index(L,lua_key_index(supplement), 0);
1213 set_font_cidsupplement(f, i);
1214 s = n_string_field_copy(L, lua_key_index(registry), "Adobe");
1215 set_font_cidregistry(f, s);
1216 s = n_string_field_copy(L, lua_key_index(ordering), "Identity");
1217 set_font_cidordering(f, s);
1218 }
1219 lua_pop(L, 1);
1220 }
1221
1222 static void read_lua_parameters(lua_State * L, int f)
1223 {
1224 int i, n, t;
1225 const char *s;
1226 lua_key_rawgeti(parameters);
1227 if (lua_istable(L, -1)) {
1228 /*tex The number of parameters is the |max(IntegerKeys(L)),7)| */
1229 n = 7;
1230 lua_pushnil(L);
1231 while (lua_next(L, -2) != 0) {
1232 if (lua_type(L, -2) == LUA_TNUMBER) {
1233 i = (int) lua_tointeger(L, -2);
1234 if (i > n)
1235 n = i;
1236 }
1237 lua_pop(L, 1);
1238 }
1239 if (n > 7)
1240 set_font_params(f, n);
1241 /*tex Sometimes it is handy to have all integer keys: */
1242 for (i = 1; i <= 7; i++) {
1243 lua_rawgeti(L, -1, i);
1244 if (lua_type(L, -1) == LUA_TNUMBER) {
1245 n = lua_roundnumber(L, -1);
1246 set_font_param(f, i, n);
1247 }
1248 lua_pop(L, 1);
1249 }
1250 lua_pushnil(L);
1251 while (lua_next(L, -2) != 0) {
1252 t = lua_type(L,-2);
1253 if (t == LUA_TNUMBER) {
1254 i = (int) lua_tointeger(L, -2);
1255 if (i >= 8) {
1256 if (lua_type(L,-1) == LUA_TNUMBER) {
1257 n = lua_roundnumber(L, -1);
1258 } else {
1259 n = 0;
1260 }
1261 set_font_param(f, i, n);
1262 }
1263 } else if (t == LUA_TSTRING) {
1264 s = lua_tostring(L, -2);
1265 if (lua_type(L,-1) == LUA_TNUMBER) {
1266 n = lua_roundnumber(L, -1);
1267 } else {
1268 n = 0;
1269 }
1270 if (lua_key_eq(s, slant)) {
1271 set_font_param(f, slant_code, n);
1272 } else if (lua_key_eq(s, space)) {
1273 set_font_param(f, space_code, n);
1274 } else if (lua_key_eq(s, space_stretch)) {
1275 set_font_param(f, space_stretch_code, n);
1276 } else if (lua_key_eq(s, space_shrink)) {
1277 set_font_param(f, space_shrink_code, n);
1278 } else if (lua_key_eq(s, x_height)) {
1279 set_font_param(f, x_height_code, n);
1280 } else if (lua_key_eq(s, quad)) {
1281 set_font_param(f, quad_code, n);
1282 } else if (lua_key_eq(s, extra_space)) {
1283 set_font_param(f, extra_space_code, n);
1284 }
1285 }
1286 lua_pop(L, 1);
1287 }
1288 }
1289 lua_pop(L, 1);
1290
1291 }
1292
1293 static void read_lua_math_parameters(lua_State * L, int f)
1294 {
1295 int i = 0, n = 0, t;
1296 lua_key_rawgeti(MathConstants);
1297 if (lua_istable(L, -1)) {
1298 lua_pushnil(L);
1299 while (lua_next(L, -2) != 0) {
1300 t = lua_type(L,-2);
1301 if (t == LUA_TNUMBER) {
1302 i = (int) lua_tointeger(L, -2);
1303 } else if (t == LUA_TSTRING) {
1304 i = ff_checkoption(L, -2, NULL, MATH_param_names);
1305 }
1306 n = (int) lua_roundnumber(L, -1);
1307 if (i > 0) {
1308 set_font_math_param(f, i, n);
1309 }
1310 lua_pop(L, 1);
1311 }
1312 set_font_oldmath(f,false);
1313 } else {
1314 set_font_oldmath(f,true);
1315 }
1316 lua_pop(L, 1);
1317 }
1318
1319 #define MIN_INF -0x7FFFFFFF
1320
1321 static void store_math_kerns(lua_State * L, int index, charinfo * co, int id)
1322 {
1323 int l, k;
1324 scaled ht, krn;
1325 lua_key_direct_rawgeti(index);
1326 if (lua_istable(L, -1) && ((k = (int) lua_rawlen(L, -1)) > 0)) {
1327 for (l = 0; l < k; l++) {
1328 lua_rawgeti(L, -1, (l + 1));
1329 if (lua_istable(L, -1)) {
1330 ht = (scaled) lua_numeric_field_by_index(L, lua_key_index(height), MIN_INF);
1331 krn = (scaled) lua_numeric_field_by_index(L, lua_key_index(kern), MIN_INF);
1332 if (krn > MIN_INF && ht > MIN_INF)
1333 add_charinfo_math_kern(co, id, ht, krn);
1334 }
1335 lua_pop(L, 1);
1336 }
1337 }
1338 lua_pop(L, 1);
1339 }
1340
1341 static void font_char_from_lua(lua_State * L, internal_font_number f, int i, int *l_fonts, boolean has_math)
1342 {
1343 int k, r, t, lt, u, n;
1344 charinfo *co;
1345 kerninfo *ckerns;
1346 liginfo *cligs;
1347 scaled j;
1348 const char *s;
1349 /*tex The number of ligature table items: */
1350 int nl = 0;
1351 /*tex The number of kern table items: */
1352 int nk = 0;
1353 int ctr = 0;
1354 int atsize = font_size(f);
1355 if (lua_istable(L, -1)) {
1356 co = get_charinfo(f, i);
1357 set_charinfo_tag(co, 0);
1358 j = lua_numeric_field_by_index(L, lua_key_index(width), 0);
1359 set_charinfo_width(co, j);
1360 j = lua_numeric_field_by_index(L, lua_key_index(height), 0);
1361 set_charinfo_height(co, j);
1362 j = lua_numeric_field_by_index(L, lua_key_index(depth), 0);
1363 set_charinfo_depth(co, j);
1364 j = lua_numeric_field_by_index(L, lua_key_index(italic), 0);
1365 set_charinfo_italic(co, j);
1366 j = lua_numeric_field_by_index(L, lua_key_index(vert_italic), 0);
1367 set_charinfo_vert_italic(co, j);
1368 j = lua_numeric_field_by_index(L, lua_key_index(index), 0);
1369 set_charinfo_index(co, j);
1370 j = lua_numeric_field_by_index(L, lua_key_index(expansion_factor), 1000);
1371 set_charinfo_ef(co, j);
1372 j = lua_numeric_field_by_index(L, lua_key_index(left_protruding), 0);
1373 set_charinfo_lp(co, j);
1374 j = lua_numeric_field_by_index(L, lua_key_index(right_protruding), 0);
1375 set_charinfo_rp(co, j);
1376 k = n_boolean_field(L, lua_key_index(used), 0);
1377 set_charinfo_used(co, k);
1378 s = n_string_field(L, lua_key_index(name));
1379 if (s != NULL)
1380 set_charinfo_name(co, xstrdup(s));
1381 else
1382 set_charinfo_name(co, NULL);
1383 /*tex |n_string_field| leaves a value on stack*/
1384 lua_pop(L,1);
1385 u = n_some_field(L,lua_key_index(tounicode));
1386 if (u == LUA_TNUMBER) {
1387 u = lua_tointeger(L,-1);
1388 if (u < 0) {
1389 set_charinfo_tounicode(co, NULL);
1390 } else if (u < 0xD7FF || (u > 0xDFFF && u <= 0xFFFF)) {
1391 char *s = malloc(5);
1392 sprintf(s,"%04X",(unsigned int) u);
1393 set_charinfo_tounicode(co,s);
1394 } else {
1395 char *s = malloc(11);
1396 u = u - 0x10000;
1397 sprintf(s,"%04X%04X",(unsigned int) (floor(u/1024)+0xD800),(unsigned int) (u%1024+0xDC00));
1398 set_charinfo_tounicode(co,s);
1399 }
1400 } else if (u == LUA_TTABLE) {
1401 n = lua_rawlen(L,-1);
1402 u = 0;
1403 for (k = 1; k <= n; k++) {
1404 lua_rawgeti(L, -1, k);
1405 if (lua_type(L,-1) == LUA_TNUMBER) {
1406 u = lua_tointeger(L,-1);
1407 } else {
1408 lua_pop(L, 1);
1409 break;
1410 }
1411 if (u < 0) {
1412 u = -1;
1413 lua_pop(L, 1);
1414 break;
1415 } else if (u < 0xD7FF || (u > 0xDFFF && u <= 0xFFFF)) {
1416 u = u + 4;
1417 } else {
1418 u = u + 8;
1419 }
1420 lua_pop(L, 1);
1421 }
1422 if (u>0) {
1423 char *s = malloc(u+1);
1424 char *t = s ;
1425 for (k = 1; k <= n; k++) {
1426 lua_rawgeti(L, -1, k);
1427 u = lua_tointeger(L,-1);
1428 if (u < 0xD7FF || (u > 0xDFFF && u <= 0xFFFF)) {
1429 sprintf(t,"%04X",(unsigned int) u);
1430 t += 4;
1431 } else {
1432 u = u - 0x10000;
1433 sprintf(t,"%04X%04X",(unsigned int) (floor(u/1024)+0xD800),(unsigned int) (u%1024+0xDC00));
1434 t += 8;
1435 }
1436 lua_pop(L, 1);
1437 }
1438 set_charinfo_tounicode(co,s);
1439 } else {
1440 set_charinfo_tounicode(co, NULL);
1441 }
1442 } else if (u == LUA_TSTRING) {
1443 s = lua_tostring(L,-1);
1444 set_charinfo_tounicode(co, xstrdup(s));
1445 } else {
1446 set_charinfo_tounicode(co, NULL);
1447 }
1448 lua_pop(L,1);
1449 if (has_math) {
1450 j = lua_numeric_field_by_index(L, lua_key_index(top_accent), INT_MIN);
1451 set_charinfo_top_accent(co, j);
1452 j = lua_numeric_field_by_index(L, lua_key_index(bot_accent), INT_MIN);
1453 set_charinfo_bot_accent(co, j);
1454 k = lua_numeric_field_by_index(L, lua_key_index(next), -1);
1455 if (k >= 0) {
1456 set_charinfo_tag(co, list_tag);
1457 set_charinfo_remainder(co, k);
1458 }
1459 lua_key_rawgeti(extensible);
1460 if (lua_istable(L, -1)) {
1461 int top, bot, mid, rep;
1462 top = lua_numeric_field_by_index(L, lua_key_index(top), 0);
1463 bot = lua_numeric_field_by_index(L, lua_key_index(bot), 0);
1464 mid = lua_numeric_field_by_index(L, lua_key_index(mid), 0);
1465 rep = lua_numeric_field_by_index(L, lua_key_index(rep), 0);
1466 if (top != 0 || bot != 0 || mid != 0 || rep != 0) {
1467 set_charinfo_tag(co, ext_tag);
1468 set_charinfo_extensible(co, top, bot, mid, rep);
1469 } else {
1470 formatted_warning("font", "lua-loaded font %s char U+%X has an invalid extensible field", font_name(f), (int) i);
1471 }
1472 }
1473 lua_pop(L, 1);
1474 lua_key_rawgeti(horiz_variants);
1475 if (lua_istable(L, -1)) {
1476 int glyph, startconnect, endconnect, advance, extender;
1477 extinfo *h;
1478 set_charinfo_tag(co, ext_tag);
1479 set_charinfo_hor_variants(co, NULL);
1480 for (k = 1;; k++) {
1481 lua_rawgeti(L, -1, k);
1482 if (lua_istable(L, -1)) {
1483 glyph = lua_numeric_field_by_index(L, lua_key_index(glyph), 0);
1484 extender = lua_numeric_field_by_index(L, lua_key_index(extender), 0);
1485 startconnect = lua_numeric_field_by_index(L, lua_key_index(start), 0);
1486 endconnect = lua_numeric_field_by_index(L, lua_key_index(end), 0);
1487 advance = lua_numeric_field_by_index(L, lua_key_index(advance), 0);
1488 h = new_variant(glyph, startconnect, endconnect, advance, extender);
1489 add_charinfo_hor_variant(co, h);
1490 lua_pop(L, 1);
1491 } else {
1492 lua_pop(L, 1);
1493 break;
1494 }
1495 }
1496 }
1497 lua_pop(L, 1);
1498 lua_key_rawgeti(vert_variants);
1499 if (lua_istable(L, -1)) {
1500 int glyph, startconnect, endconnect, advance, extender;
1501 extinfo *h;
1502 set_charinfo_tag(co, ext_tag);
1503 set_charinfo_vert_variants(co, NULL);
1504 for (k = 1;; k++) {
1505 lua_rawgeti(L, -1, k);
1506 if (lua_istable(L, -1)) {
1507 glyph = lua_numeric_field_by_index(L, lua_key_index(glyph), 0);
1508 extender = lua_numeric_field_by_index(L, lua_key_index(extender), 0);
1509 startconnect = lua_numeric_field_by_index(L, lua_key_index(start), 0);
1510 endconnect = lua_numeric_field_by_index(L, lua_key_index(end), 0);
1511 advance = lua_numeric_field_by_index(L, lua_key_index(advance), 0);
1512 h = new_variant(glyph, startconnect, endconnect, advance, extender);
1513 add_charinfo_vert_variant(co, h);
1514 lua_pop(L, 1);
1515 } else {
1516 lua_pop(L, 1);
1517 break;
1518 }
1519 }
1520 }
1521 lua_pop(L, 1);
1522 /*tex
1523 Here is a complete example:
1524
1525 \starttyping
1526 mathkern = {
1527 bottom_left = { { height = 420, kern = 80 }, { height = 520, kern = 4 } },
1528 bottom_right = { { height = 0, kern = 48 } },
1529 top_left = { { height = 620, kern = 0 }, { height = 720, kern = -80 } },
1530 top_right = { { height = 676, kern = 115 }, { height = 776, kern = 45 } },
1531 }
1532 \stoptyping
1533
1534 */
1535 lua_key_rawgeti(mathkern);
1536 if (lua_istable(L, -1)) {
1537 store_math_kerns(L,lua_key_index(top_left), co, top_left_kern);
1538 store_math_kerns(L,lua_key_index(top_right), co, top_right_kern);
1539 store_math_kerns(L,lua_key_index(bottom_right), co, bottom_right_kern);
1540 store_math_kerns(L,lua_key_index(bottom_left), co, bottom_left_kern);
1541 }
1542 lua_pop(L, 1);
1543 }
1544 /*tex end of |has_math| */
1545 count_hash_items(L, kerns, nk);
1546 if (nk > 0) {
1547 /*tex The kerns table is still on stack. */
1548 ckerns = xcalloc((unsigned) (nk + 1), sizeof(kerninfo));
1549 ctr = 0;
1550 /*tex Traverse the hash. */
1551 lua_pushnil(L);
1552 while (lua_next(L, -2) != 0) {
1553 k = non_boundarychar;
1554 lt = lua_type(L,-2);
1555 if (lt == LUA_TNUMBER) {
1556 /*tex Adjacent char: */
1557 k = (int) lua_tointeger(L, -2);
1558 if (k < 0)
1559 k = non_boundarychar;
1560 } else if (lt == LUA_TSTRING) {
1561 s = lua_tostring(L, -2);
1562 if (lua_key_eq(s, right_boundary)) {
1563 k = right_boundarychar;
1564 if (!has_right_boundary(f))
1565 set_right_boundary(f, get_charinfo(f, right_boundarychar));
1566 }
1567 }
1568 j = lua_roundnumber(L, -1);
1569 if (k != non_boundarychar) {
1570 set_kern_item(ckerns[ctr], k, j);
1571 ctr++;
1572 } else {
1573 formatted_warning("font", "lua-loaded font %s char U+%X has an invalid kern field", font_name(f), (int) i);
1574 }
1575 lua_pop(L, 1);
1576 }
1577 /*tex A guard against empty tables. */
1578 if (ctr > 0) {
1579 set_kern_item(ckerns[ctr], end_kern, 0);
1580 set_charinfo_kerns(co, ckerns);
1581 } else {
1582 formatted_warning("font", "lua-loaded font %s char U+%X has an invalid kerns field", font_name(f), (int) i);
1583 }
1584 lua_pop(L, 1);
1585 }
1586 /*tex Packet commands. */
1587 lua_key_rawgeti(commands);
1588 if (lua_istable(L, -1)) {
1589 lua_pushnil(L);
1590 if (lua_next(L, -2) != 0) {
1591 lua_pop(L, 2);
1592 read_char_packets(L, (int *) l_fonts, co, f, atsize);
1593 }
1594 }
1595 lua_pop(L, 1);
1596 /*tex The ligatures. */
1597 count_hash_items(L, ligatures, nl);
1598 if (nl > 0) {
1599 /*tex The ligatures table still on stack. */
1600 cligs = xcalloc((unsigned) (nl + 1), sizeof(liginfo));
1601 ctr = 0;
1602 /*tex Traverse the hash. */
1603 lua_pushnil(L);
1604 while (lua_next(L, -2) != 0) {
1605 k = non_boundarychar;
1606 lt = lua_type(L,-2);
1607 if (lt == LUA_TNUMBER) {
1608 /*tex Adjacent char: */
1609 k = (int) lua_tointeger(L, -2);
1610 if (k < 0) {
1611 k = non_boundarychar;
1612 }
1613 } else if (lt == LUA_TSTRING) {
1614 s = lua_tostring(L, -2);
1615 if (lua_key_eq(s, right_boundary)) {
1616 k = right_boundarychar;
1617 if (!has_right_boundary(f))
1618 set_right_boundary(f, get_charinfo(f, right_boundarychar));
1619 }
1620 }
1621 r = -1;
1622 if (lua_istable(L, -1)) {
1623 /*tex Ligature: */
1624 r = lua_numeric_field_by_index(L, lua_key_index(char), -1);
1625 }
1626 if (r != -1 && k != non_boundarychar) {
1627 t = n_enum_field(L, lua_key_index(type), 0, ligature_type_strings);
1628 set_ligature_item(cligs[ctr], (char) ((t * 2) + 1), k, r);
1629 ctr++;
1630 } else {
1631 formatted_warning("font", "lua-loaded font %s char U+%X has an invalid ligature field", font_name(f), (int) i);
1632 }
1633 /*tex The iterator value: */
1634 lua_pop(L, 1);
1635 }
1636 /*tex A guard against empty tables. */
1637 if (ctr > 0) {
1638 set_ligature_item(cligs[ctr], 0, end_ligature, 0);
1639 set_charinfo_ligatures(co, cligs);
1640 } else {
1641 formatted_warning("font", "lua-loaded font %s char U+%X has an invalid ligatures field", font_name(f), (int) i);
1642 }
1643 /*tex The ligatures table. */
1644 lua_pop(L, 1);
1645 }
1646 }
1647 }
1648
1649 /*tex
1650
1651 The caller has to fix the state of the lua stack when there is an error!
1652
1653 */
1654
1655 int font_from_lua(lua_State * L, int f)
1656 {
1657 int i, n, r, t, lt;
1658 int s_top;
1659 int bc;
1660 int ec;
1661 char *s;
1662 const char *ss;
1663 int *l_fonts = NULL;
1664 int save_ref ;
1665 boolean no_math = false;
1666 /*tex Will we save a cache of the \LUA\ table? */
1667 save_ref = 1;
1668 ss = NULL;
1669 ss = n_string_field(L, lua_key_index(cache));
1670 if (lua_key_eq(ss, no))
1671 save_ref = -1;
1672 else if (lua_key_eq(ss, renew))
1673 save_ref = 0;
1674 /*tex |n_string_field| leaves a value on stack. */
1675 lua_pop(L,1);
1676 /*tex The table is at stack |index -1| */
1677 s = n_string_field_copy(L,lua_key_index(area), "");
1678 set_font_area(f, s);
1679 s = n_string_field_copy(L, lua_key_index(filename), NULL);
1680 set_font_filename(f, s);
1681 s = n_string_field_copy(L, lua_key_index(encodingname), NULL);
1682 set_font_encodingname(f, s);
1683 s = n_string_field_copy(L, lua_key_index(name), NULL);
1684 set_font_name(f, s);
1685 s = n_string_field_copy(L, lua_key_index(fullname), font_name(f));
1686 set_font_fullname(f, s);
1687 if (s == NULL) {
1688 formatted_error("font","lua-loaded font '%d' has no name!", f);
1689 return false;
1690 }
1691 s = n_string_field_copy(L, lua_key_index(psname), NULL);
1692 set_font_psname(f, s);
1693 i = lua_numeric_field_by_index(L,lua_key_index(units_per_em), 0);
1694 set_font_units_per_em(f, i);
1695 i = lua_numeric_field_by_index(L,lua_key_index(designsize), 655360);
1696 set_font_dsize(f, i);
1697 i = lua_numeric_field_by_index(L,lua_key_index(size), font_dsize(f));
1698 set_font_size(f, i);
1699 set_font_checksum(f, (unsigned)(lua_unsigned_numeric_field_by_index(L,lua_key_index(checksum), 0))) ;
1700 i = lua_numeric_field_by_index(L,lua_key_index(direction), 0);
1701 set_font_natural_dir(f, i);
1702 i = lua_numeric_field_by_index(L,lua_key_index(encodingbytes), 0);
1703 set_font_encodingbytes(f, (char) i);
1704 i = lua_numeric_field_by_index(L,lua_key_index(streamprovider), 0);
1705 set_font_streamprovider(f, (char) i);
1706 i = n_boolean_field(L,lua_key_index(oldmath), 0);
1707 set_font_oldmath(f, i);
1708 i = lua_numeric_field_by_index(L,lua_key_index(tounicode), 0);
1709 set_font_tounicode(f, (char) i);
1710 i = lua_numeric_field_by_index(L,lua_key_index(slant), 0);
1711 if (i < FONT_SLANT_MIN)
1712 i = FONT_SLANT_MIN;
1713 if (i > FONT_SLANT_MAX)
1714 i = FONT_SLANT_MAX;
1715 set_font_slant(f, i);
1716 i = lua_numeric_field_by_index(L,lua_key_index(extend), 1000);
1717 if (i < FONT_EXTEND_MIN)
1718 i = FONT_EXTEND_MIN;
1719 if (i > FONT_EXTEND_MAX)
1720 i = FONT_EXTEND_MAX;
1721 set_font_extend(f, i);
1722 i = lua_numeric_field_by_index(L,lua_key_index(squeeze), 1000);
1723 if (i < FONT_SQUEEZE_MIN)
1724 i = FONT_SQUEEZE_MIN;
1725 if (i > FONT_SQUEEZE_MAX)
1726 i = FONT_SQUEEZE_MAX;
1727 set_font_squeeze(f, i);
1728 i = lua_numeric_field_by_index(L,lua_key_index(width), 0);
1729 if (i < FONT_WIDTH_MIN)
1730 i = FONT_WIDTH_MIN;
1731 if (i > FONT_WIDTH_MAX)
1732 i = FONT_WIDTH_MAX;
1733 set_font_width(f, i);
1734 i = lua_numeric_field_by_index(L,lua_key_index(mode), 0);
1735 if (i < FONT_MODE_MIN)
1736 i = FONT_MODE_MIN;
1737 if (i > FONT_MODE_MAX)
1738 i = FONT_MODE_MAX;
1739 set_font_mode(f, i);
1740 i = lua_numeric_field_by_index(L,lua_key_index(hyphenchar), default_hyphen_char_par);
1741 set_hyphen_char(f, i);
1742 i = lua_numeric_field_by_index(L,lua_key_index(skewchar), default_skew_char_par);
1743 set_skew_char(f, i);
1744 i = n_boolean_field(L, lua_key_index(used), 0);
1745 set_font_used(f, (char) i);
1746 s = n_string_field_copy(L, lua_key_index(attributes), NULL);
1747 if (s != NULL && strlen(s) > 0) {
1748 i = maketexstring(s);
1749 set_pdf_font_attr(f, i);
1750 }
1751 free(s);
1752 i = n_enum_field(L, lua_key_index(type), unknown_font_type, font_type_strings);
1753 set_font_type(f, i);
1754 i = n_enum_field(L, lua_key_index(format), unknown_format, font_format_strings);
1755 set_font_format(f, i);
1756 i = n_enum_field(L, lua_key_index(writingmode), unknown_writingmode, font_writingmode_strings);
1757 set_font_writingmode(f, i);
1758 i = n_enum_field(L, lua_key_index(identity), unknown_identity, font_identity_strings);
1759 set_font_identity(f, i);
1760 i = n_enum_field(L, lua_key_index(embedding), unknown_embedding, font_embedding_strings);
1761 set_font_embedding(f, i);
1762 if (font_encodingbytes(f) == 0 && (font_format(f) == opentype_format || font_format(f) == truetype_format)) {
1763 set_font_encodingbytes(f, 2);
1764 }
1765 i = lua_numeric_field_by_index(L,lua_key_index(subfont), 0);
1766 set_font_subfont(f,i);
1767 /*tex Now fetch the base fonts, if needed. */
1768 count_hash_items(L, fonts, n);
1769 if (n > 0) {
1770 /*tex The font table still on stack. */
1771 l_fonts = xmalloc((unsigned) ((unsigned) (n + 2) * sizeof(int)));
1772 memset(l_fonts, 0, (size_t) ((unsigned) (n + 2) * sizeof(int)));
1773 for (i = 1; i <= n; i++) {
1774 lua_rawgeti(L, -1, i);
1775 if (lua_istable(L, -1)) {
1776 lua_key_rawgeti(id);
1777 if (lua_isnumber(L, -1)) {
1778 l_fonts[i] = (int) lua_tointeger(L, -1);
1779 if (l_fonts[i] == 0) {
1780 l_fonts[i] = (int) f;
1781 }
1782 /*tex Pop id and entry. */
1783 lua_pop(L, 2);
1784 continue;
1785 }
1786 /*tex Pop id. */
1787 lua_pop(L, 1);
1788 };
1789 ss = NULL;
1790 if (lua_istable(L, -1)) {
1791 ss = n_string_field(L, lua_key_index(name));
1792 /*tex The string is anchored. */
1793 lua_pop(L,1);
1794 }
1795 if (ss != NULL) {
1796 t = lua_numeric_field_by_index(L, lua_key_index(size), -1000);
1797 /*tex The stack is messed up, otherwise this explicit resizing would not be needed! */
1798 s_top = lua_gettop(L);
1799 if (strcmp(font_name(f), ss) == 0)
1800 l_fonts[i] = f;
1801 else
1802 l_fonts[i] = find_font_id(ss, t);
1803 lua_settop(L, s_top);
1804 } else {
1805 formatted_error("font","invalid local font at index %i in lua-loaded font '%s' (1)",i,font_name(f));
1806 }
1807 /*tex Pop the list entry. */
1808 lua_pop(L, 1);
1809 }
1810 /*tex Pop the font table. */
1811 lua_pop(L, 1);
1812 } else if (font_type(f) == virtual_font_type) {
1813 /*tex
1814 We no longer do this check but instead create an entry. This permits
1815 (valid) tricks.
1816 */
1817 /*
1818 formatted_error("font","invalid local fonts in lua-loaded font '%s' (2)", font_name(f));
1819 */
1820 } else {
1821 l_fonts = xmalloc(3 * sizeof(int));
1822 l_fonts[0] = 0;
1823 l_fonts[1] = f;
1824 l_fonts[2] = 0;
1825 }
1826 /*tex The parameters. */
1827 no_math = n_boolean_field(L, lua_key_index(nomath), 0);
1828 read_lua_parameters(L, f);
1829 if (!no_math) {
1830 read_lua_math_parameters(L, f);
1831 if (n_boolean_field(L, lua_key_index(oldmath), 0)) {
1832 set_font_oldmath(f,true);
1833 }
1834
1835 } else {
1836 set_font_oldmath(f,true);
1837 }
1838 read_lua_cidinfo(L, f);
1839 /*tex The characters. */
1840 lua_key_rawgeti(characters);
1841 if (lua_istable(L, -1)) {
1842 /*tex Find the array size values; |num| holds the number of characters to add. */
1843 int num = 0;
1844 ec = 0;
1845 bc = -1;
1846 /*tex The first key: */
1847 lua_pushnil(L);
1848 while (lua_next(L, -2) != 0) {
1849 if (lua_isnumber(L, -2)) {
1850 i = (int) lua_tointeger(L, -2);
1851 if (i >= 0) {
1852 if (lua_istable(L, -1)) {
1853 num++;
1854 if (i > ec)
1855 ec = i;
1856 if (bc < 0)
1857 bc = i;
1858 if (bc >= 0 && i < bc)
1859 bc = i;
1860 }
1861 }
1862 }
1863 lua_pop(L, 1);
1864 }
1865 if (bc != -1) {
1866 int fstep;
1867 font_malloc_charinfo(f, num);
1868 set_font_bc(f, bc);
1869 set_font_ec(f, ec);
1870 /*tex The first key: */
1871 lua_pushnil(L);
1872 while (lua_next(L, -2) != 0) {
1873 lt = lua_type(L,-2);
1874 if (lt == LUA_TNUMBER) {
1875 i = (int) lua_tointeger(L, -2);
1876 if (i >= 0) {
1877 font_char_from_lua(L, f, i, l_fonts, !no_math);
1878 }
1879 } else if (lt == LUA_TSTRING) {
1880 const char *ss1 = lua_tostring(L, -2);
1881 if (lua_key_eq(ss1, left_boundary)) {
1882 font_char_from_lua(L, f, left_boundarychar, l_fonts, !no_math);
1883 } else if (lua_key_eq(ss1, right_boundary)) {
1884 font_char_from_lua(L, f, right_boundarychar, l_fonts, !no_math);
1885 }
1886 }
1887 lua_pop(L, 1);
1888 }
1889 lua_pop(L, 1);
1890 /*tex
1891
1892 Handle font expansion last: the |copy_font| routine is called eventually,
1893 and that needs to know |bc| and |ec|. We permits virtual fonts to use
1894 expansion as one can always turn it off.
1895
1896 */
1897 fstep = lua_numeric_field_by_index(L, lua_key_index(step), 0);
1898 if (fstep < 0)
1899 fstep = 0;
1900 if (fstep > 100)
1901 fstep = 100;
1902 if (fstep != 0) {
1903 int fshrink = lua_numeric_field_by_index(L, lua_key_index(shrink), 0);
1904 int fstretch= lua_numeric_field_by_index(L, lua_key_index(stretch), 0);
1905 if (fshrink < 0)
1906 fshrink = 0;
1907 if (fshrink > 500)
1908 fshrink = 500;
1909 fshrink -= (fshrink % fstep);
1910 if (fshrink < 0)
1911 fshrink = 0;
1912 if (fstretch < 0)
1913 fstretch = 0;
1914 if (fstretch > 1000)
1915 fstretch = 1000;
1916 fstretch -= (fstretch % fstep);
1917 if (fstretch < 0)
1918 fstretch = 0;
1919 set_expand_params(f, fstretch, fshrink, fstep);
1920 }
1921
1922 } else {
1923 formatted_warning("font","lua-loaded font '%d' with name '%s' has no characters", f, font_name(f));
1924 }
1925 if (save_ref > 0) {
1926 /*tex This pops the table. */
1927 r = luaL_ref(L, LUA_REGISTRYINDEX);
1928 set_font_cache_id(f, r);
1929 } else {
1930 lua_pop(L, 1);
1931 set_font_cache_id(f, save_ref);
1932 }
1933 } else {
1934 formatted_warning("font","lua-loaded font '%d' with name '%s' has no character table", f, font_name(f));
1935 }
1936 if (l_fonts != NULL)
1937 free(l_fonts);
1938 return true;
1939 }
1940
1941 int characters_from_lua(lua_State * L, int f)
1942 {
1943 int i, n, t, lt;
1944 int *l_fonts = NULL;
1945 int s_top;
1946 const char *ss;
1947 boolean no_math = false;
1948 /*tex Speedup: */
1949 no_math = n_boolean_field(L, lua_key_index(nomath), 0);
1950 /*tex Type: */
1951 i = n_enum_field(L, lua_key_index(type), font_type(f), font_type_strings);
1952 set_font_type(f, i);
1953 /*tex Fonts: */
1954 count_hash_items(L, fonts, n);
1955 if (n > 0) {
1956 /*tex The font table still on stack. */
1957 l_fonts = xmalloc((unsigned) ((unsigned) (n + 2) * sizeof(int)));
1958 memset(l_fonts, 0, (size_t) ((unsigned) (n + 2) * sizeof(int)));
1959 for (i = 1; i <= n; i++) {
1960 lua_rawgeti(L, -1, i);
1961 if (lua_istable(L, -1)) {
1962 lua_key_rawgeti(id);
1963 if (lua_isnumber(L, -1)) {
1964 l_fonts[i] = (int) lua_tointeger(L, -1);
1965 if (l_fonts[i] == 0) {
1966 l_fonts[i] = (int) f;
1967 }
1968 /*tex Pop id and entry. */
1969 lua_pop(L, 2);
1970 continue;
1971 }
1972 /*tex Pop id. */
1973 lua_pop(L, 1);
1974 };
1975 ss = NULL;
1976 if (lua_istable(L, -1)) {
1977 ss = n_string_field(L, lua_key_index(name));
1978 /* string is anchored */
1979 lua_pop(L,1);
1980 }
1981 if (ss != NULL) {
1982 t = lua_numeric_field_by_index(L, lua_key_index(size), -1000);
1983 /*tex the stack is messed up, otherwise this explicit resizing would not be needed! */
1984 s_top = lua_gettop(L);
1985 if (strcmp(font_name(f), ss) == 0)
1986 l_fonts[i] = f;
1987 else
1988 l_fonts[i] = find_font_id(ss, t);
1989 lua_settop(L, s_top);
1990 } else {
1991 formatted_error("font","invalid local font in lua-loaded font '%s' (3)", font_name(f));
1992 }
1993 /*tex Pop list entry. */
1994 lua_pop(L, 1);
1995 }
1996 /*tex Pop font table. */
1997 lua_pop(L, 1);
1998 } else if (font_type(f) == virtual_font_type) {
1999 formatted_error("font","invalid local fonts in lua-loaded font '%s' (4)", font_name(f));
2000 } else {
2001 l_fonts = xmalloc(3 * sizeof(int));
2002 l_fonts[0] = 0;
2003 l_fonts[1] = f;
2004 l_fonts[2] = 0;
2005 }
2006 /*tex The characters. */
2007 lua_key_rawgeti(characters);
2008 if (lua_istable(L, -1)) {
2009 /*tex Find the array size values; |num| has the amount. */
2010 int num = 0;
2011 int todo = 0;
2012 int bc = font_bc(f);
2013 int ec = font_ec(f);
2014 /*tex First key: */
2015 lua_pushnil(L);
2016 while (lua_next(L, -2) != 0) {
2017 if (lua_isnumber(L, -2)) {
2018 i = (int) lua_tointeger(L, -2);
2019 if (i >= 0) {
2020 if (lua_istable(L, -1)) {
2021 todo++;
2022 if (! quick_char_exists(f,i)) {
2023 num++;
2024 if (i > ec)
2025 ec = i;
2026 if (bc < 0)
2027 bc = i;
2028 if (bc >= 0 && i < bc)
2029 bc = i;
2030 }
2031 }
2032 }
2033 }
2034 lua_pop(L, 1);
2035 }
2036 if (todo > 0) {
2037 font_malloc_charinfo(f, num);
2038 set_font_bc(f, bc);
2039 set_font_ec(f, ec);
2040 /*tex First key: */
2041 lua_pushnil(L);
2042 while (lua_next(L, -2) != 0) {
2043 lt = lua_type(L,-2);
2044 if (lt == LUA_TNUMBER) {
2045 i = (int) lua_tointeger(L, -2);
2046 if (i >= 0) {
2047 if (quick_char_exists(f,i)) {
2048 charinfo *co = char_info(f, i);
2049 set_charinfo_name(co, NULL);
2050 set_charinfo_tounicode(co, NULL);
2051 set_charinfo_packets(co, NULL);
2052 set_charinfo_ligatures(co, NULL);
2053 set_charinfo_kerns(co, NULL);
2054 set_charinfo_vert_variants(co, NULL);
2055 set_charinfo_hor_variants(co, NULL);
2056 }
2057 font_char_from_lua(L, f, i, l_fonts, !no_math);
2058 }
2059 }
2060 lua_pop(L, 1);
2061 }
2062 lua_pop(L, 1);
2063 }
2064 }
2065 if (l_fonts != NULL)
2066 free(l_fonts);
2067 return true;
2068 }
2069
2070 /*tex Ligaturing starts here */
2071
2072 static void nesting_append(halfword nest1, halfword newn)
2073 {
2074 halfword tail = tlink(nest1);
2075 if (tail == null) {
2076 couple_nodes(nest1, newn);
2077 } else {
2078 couple_nodes(tail, newn);
2079 }
2080 tlink(nest1) = newn;
2081 }
2082
2083 static void nesting_prepend(halfword nest1, halfword newn)
2084 {
2085 halfword head = vlink(nest1);
2086 couple_nodes(nest1, newn);
2087 if (head == null) {
2088 tlink(nest1) = newn;
2089 } else {
2090 couple_nodes(newn, head);
2091 }
2092 }
2093
2094 static void nesting_prepend_list(halfword nest1, halfword newn)
2095 {
2096 halfword head = vlink(nest1);
2097 couple_nodes(nest1, newn);
2098 if (head == null) {
2099 tlink(nest1) = tail_of_list(newn);
2100 } else {
2101 halfword tail = tail_of_list(newn);
2102 couple_nodes(tail, head);
2103 }
2104 }
2105
2106 static int test_ligature(liginfo * lig, halfword left, halfword right)
2107 {
2108 if (type(left) != glyph_node)
2109 return 0;
2110 if (font(left) != font(right))
2111 return 0;
2112 if (is_ghost(left) || is_ghost(right))
2113 return 0;
2114 *lig = get_ligature(font(left), character(left), character(right));
2115 if (is_valid_ligature(*lig)) {
2116 return 1;
2117 }
2118 return 0;
2119 }
2120
2121 static int try_ligature(halfword * frst, halfword fwd)
2122 {
2123 halfword cur = *frst;
2124 liginfo lig;
2125 if (test_ligature(&lig, cur, fwd)) {
2126 int move_after = (lig_type(lig) & 0x0C) >> 2;
2127 int keep_right = ((lig_type(lig) & 0x01) != 0);
2128 int keep_left = ((lig_type(lig) & 0x02) != 0);
2129 halfword newgl = raw_glyph_node();
2130 font(newgl) = font(cur);
2131 character(newgl) = lig_replacement(lig);
2132 set_is_ligature(newgl);
2133 /*tex
2134 Below might not be correct in contrived border case. but we use it
2135 only for debugging.
2136 */
2137 if (character(cur) < 0) {
2138 set_is_leftboundary(newgl);
2139 }
2140 if (character(fwd) < 0) {
2141 set_is_rightboundary(newgl);
2142 }
2143 if (character(cur) < 0) {
2144 if (character(fwd) < 0) {
2145 build_attribute_list(newgl);
2146 } else {
2147 add_node_attr_ref(node_attr(fwd));
2148 node_attr(newgl) = node_attr(fwd);
2149 }
2150 } else {
2151 add_node_attr_ref(node_attr(cur));
2152 node_attr(newgl) = node_attr(cur);
2153 }
2154 /*tex
2155 Maybe if this ligature is consists of another ligature we should add
2156 it's |lig_ptr| to the new glyphs |lig_ptr| (and cleanup the no longer
2157 needed node). This has a very low priority, so low that it might
2158 never happen.
2159 */
2160 /*tex Left side: */
2161 if (keep_left) {
2162 halfword new_first = copy_node(cur);
2163 lig_ptr(newgl) = new_first;
2164 couple_nodes(cur, newgl);
2165 if (move_after) {
2166 move_after--;
2167 cur = newgl;
2168 }
2169 } else {
2170 halfword prev = alink(cur);
2171 uncouple_node(cur);
2172 lig_ptr(newgl) = cur;
2173 couple_nodes(prev, newgl);
2174 cur = newgl; /* as cur has disappeared */
2175 }
2176 /*tex Right side: */
2177 if (keep_right) {
2178 halfword new_second = copy_node(fwd);
2179 /*tex This is correct, because we {\em know} |lig_ptr| points to {\em one} node. */
2180 couple_nodes(lig_ptr(newgl), new_second);
2181 couple_nodes(newgl, fwd);
2182 if (move_after) {
2183 move_after--;
2184 cur = fwd;
2185 }
2186 } else {
2187 halfword next = vlink(fwd);
2188 uncouple_node(fwd);
2189 /*tex This works because we {\em know} |lig_ptr| points to {\em one} node. */
2190 couple_nodes(lig_ptr(newgl), fwd);
2191 if (next != null) {
2192 couple_nodes(newgl, next);
2193 }
2194 }
2195 /*tex Check and return. */
2196 *frst = cur;
2197 return 1;
2198 }
2199 return 0;
2200 }
2201
2202 /*tex
2203
2204 There shouldn't be any ligatures here - we only add them at the end of
2205 |xxx_break| in a \.{DISC-1 - DISC-2} situation and we stop processing
2206 \.{DISC-1} (we continue with \.{DISC-1}'s |post_| and |no_break|.
2207
2208 */
2209
2210 static halfword handle_lig_nest(halfword root, halfword cur)
2211 {
2212 if (cur == null)
2213 return root;
2214 while (vlink(cur) != null) {
2215 halfword fwd = vlink(cur);
2216 if (type(cur) == glyph_node && type(fwd) == glyph_node &&
2217 font(cur) == font(fwd) && try_ligature(&cur, fwd)) {
2218 continue;
2219 }
2220 cur = vlink(cur);
2221 }
2222 tlink(root) = cur;
2223 return root;
2224 }
2225
2226 /*tex
2227 Because \LATEX\ dev wants to support more complex select sequences we get a
2228 compatibility issue. For that reason we now can block select discs in so
2229 called \quote {base} mode (the traditional \TEX\ font handling). By
2230 completely blocking select discs (which actually were just implemented for
2231 the rare case of having multiple hyphenation points in a three character
2232 ligature) we simplify handling and possible side effects of patches in the
2233 line break routines (which then becomes more complex). Because the \CONTEXT\
2234 font handler doesn't inject select discretionaries we don't expect side
2235 effects there. The control parameter |\discretionaryligaturemode| defaults to
2236 the value 0. A value of 1 reflects \quotation {lazy ligaturing} i.e.\ |f-f-i|
2237 becomes |f-fi|, and a value of 2 turns |f-f-i| into |ff-i|. This mechanism is
2238 backport of \LUAMETATEX\ where select discs had been abandoned already and
2239 where lazy ligatures is one of the possible glyph properties.
2240 */
2241
2242 static halfword handle_lig_word(halfword cur)
2243 {
2244 halfword right = null;
2245 halfword last = null;
2246 if (type(cur) == boundary_node) {
2247 halfword prev = alink(cur);
2248 halfword fwd = vlink(cur);
2249 /*tex There is no need to uncouple |cur|, it is freed. */
2250 flush_node(cur);
2251 if (fwd == null) {
2252 vlink(prev) = fwd;
2253 return prev;
2254 }
2255 couple_nodes(prev, fwd);
2256 if (type(fwd) != glyph_node)
2257 return prev;
2258 cur = fwd;
2259 } else if (has_left_boundary(font(cur))) {
2260 halfword prev = alink(cur);
2261 halfword p = new_glyph(font(cur), left_boundarychar);
2262 couple_nodes(prev, p);
2263 couple_nodes(p, cur);
2264 cur = p;
2265 }
2266 if (has_right_boundary(font(cur))) {
2267 right = new_glyph(font(cur), right_boundarychar);
2268 }
2269 while (1) {
2270 /*tex A glyph followed by \unknown */
2271 if (type(cur) == glyph_node) {
2272 halfword fwd = vlink(cur);
2273 if (fwd == null) {
2274 /*tex The last character of a paragraph. */
2275 if (right == null)
2276 break;
2277 /*tex |par| prohibits the use of |couple_nodes| here. */
2278 try_couple_nodes(cur, right);
2279 right = null;
2280 continue;
2281 }
2282 if (type(fwd) == glyph_node) {
2283 /*tex a glyph followed by a glyph */
2284 if (font(cur) != font(fwd))
2285 break;
2286 if (try_ligature(&cur, fwd))
2287 continue;
2288 } else if (type(fwd) == disc_node) {
2289 /*tex a glyph followed by a disc */
2290 halfword pre = vlink_pre_break(fwd);
2291 halfword nob = vlink_no_break(fwd);
2292 halfword next, tail;
2293 liginfo lig;
2294 /*tex Check on: |a{b?}{?}{?}| and |a+b=>B| : |{B?}{?}{a?}| */
2295 /*tex Check on: |a{?}{?}{b?}| and |a+b=>B| : |{a?}{?}{B?}| */
2296 if ((pre != null && type(pre) == glyph_node && test_ligature(&lig, cur, pre))
2297 || (nob != null && type(nob) == glyph_node && test_ligature(&lig, cur, nob))) {
2298 /*tex Move |cur| from before disc to skipped part */
2299 halfword prev = alink(cur);
2300 uncouple_node(cur);
2301 couple_nodes(prev, fwd);
2302 nesting_prepend(no_break(fwd), cur);
2303 /*tex Now ligature the |pre_break|. */
2304 nesting_prepend(pre_break(fwd), copy_node(cur));
2305 /*tex As we have removed cur, we need to start again. */
2306 cur = prev;
2307 }
2308 /*tex Check on: |a{?}{?}{}b| and |a+b=>B| : |{a?}{?b}{B}|. */
2309 next = vlink(fwd);
2310 if (nob == null && next != null && type(next) == glyph_node && test_ligature(&lig, cur, next)) {
2311 /*tex Move |cur| from before |disc| to |no_break| part. */
2312 halfword prev = alink(cur);
2313 uncouple_node(cur);
2314 couple_nodes(prev, fwd);
2315 /*tex We {\em know} it's empty. */
2316 couple_nodes(no_break(fwd), cur);
2317 /*tex Now copy |cur| the |pre_break|. */
2318 nesting_prepend(pre_break(fwd), copy_node(cur));
2319 /*tex Move next from after disc to |no_break| part. */
2320 tail = vlink(next);
2321 uncouple_node(next);
2322 try_couple_nodes(fwd, tail);
2323 /*tex We {\em know} this works. */
2324 couple_nodes(cur, next);
2325 /*tex Make sure the list is correct. */
2326 tlink(no_break(fwd)) = next;
2327 /*tex Now copy next to the |post_break|. */
2328 nesting_append(post_break(fwd), copy_node(next));
2329 /*tex As we have removed cur, we need to start again. */
2330 cur = prev;
2331 }
2332 /*tex We are finished with the |pre_break|. */
2333 handle_lig_nest(pre_break(fwd), vlink_pre_break(fwd));
2334 } else if (type(fwd) == boundary_node) {
2335 halfword next = vlink(fwd);
2336 try_couple_nodes(cur, next);
2337 flush_node(fwd);
2338 if (right != null) {
2339 /*tex Shame, didn't need it. */
2340 flush_node(right);
2341 /*tex No need to reset |right|, we're going to leave the loop anyway. */
2342 }
2343 break;
2344 } else {
2345 /*tex Is something unknown. */
2346 if (right == null)
2347 break;
2348 couple_nodes(cur, right);
2349 couple_nodes(right, fwd);
2350 right = null;
2351 continue;
2352 }
2353 /*tex A discretionary followed by \unknown */
2354 } else if (type(cur) == disc_node) {
2355 /*tex If |{?}{x}{?}| or |{?}{?}{y}| then: */
2356 if (vlink_no_break(cur) != null || vlink_post_break(cur) != null) {
2357 halfword prev = 0;
2358 halfword fwd;
2359 liginfo lig;
2360 if (subtype(cur) == select_disc) {
2361 prev = alink(cur);
2362 if (vlink_post_break(cur) != null)
2363 handle_lig_nest(post_break(prev), vlink_post_break(prev));
2364 if (vlink_no_break(cur) != null)
2365 handle_lig_nest(no_break(prev), vlink_no_break(prev));
2366 }
2367 if (vlink_post_break(cur) != null)
2368 handle_lig_nest(post_break(cur), vlink_post_break(cur));
2369 if (vlink_no_break(cur) != null)
2370 handle_lig_nest(no_break(cur), vlink_no_break(cur));
2371 while ((fwd = vlink(cur)) != null) {
2372 halfword nob, pst, next;
2373 if (type(fwd) != glyph_node)
2374 break;
2375 if (subtype(cur) != select_disc) {
2376 nob = tlink_no_break(cur);
2377 pst = tlink_post_break(cur);
2378 if ((nob == null || !test_ligature(&lig, nob, fwd)) &&
2379 (pst == null || !test_ligature(&lig, pst, fwd)))
2380 break;
2381 nesting_append(no_break(cur), copy_node(fwd));
2382 handle_lig_nest(no_break(cur), nob);
2383 } else {
2384 int dobreak = 0;
2385 nob = tlink_no_break(prev);
2386 pst = tlink_post_break(prev);
2387 if ((nob == null || !test_ligature(&lig, nob, fwd)) &&
2388 (pst == null || !test_ligature(&lig, pst, fwd)))
2389 dobreak = 1;
2390 if (!dobreak) {
2391 nesting_append(no_break(prev), copy_node(fwd));
2392 handle_lig_nest(no_break(prev), nob);
2393 nesting_append(post_break(prev), copy_node(fwd));
2394 handle_lig_nest(post_break(prev), pst);
2395 }
2396 dobreak = 0;
2397 nob = tlink_no_break(cur);
2398 pst = tlink_post_break(cur);
2399 if ((nob == null || !test_ligature(&lig, nob, fwd)) &&
2400 (pst == null || !test_ligature(&lig, pst, fwd)))
2401 dobreak = 1;
2402 if (!dobreak) {
2403 nesting_append(no_break(cur), copy_node(fwd));
2404 handle_lig_nest(no_break(cur), nob);
2405 }
2406 if (dobreak)
2407 break;
2408 }
2409 next = vlink(fwd);
2410 uncouple_node(fwd);
2411 try_couple_nodes(cur, next);
2412 nesting_append(post_break(cur), fwd);
2413 handle_lig_nest(post_break(cur), pst);
2414 }
2415 if (fwd != null && type(fwd) == disc_node) {
2416 halfword next = vlink(fwd);
2417 if (vlink_no_break(fwd) == null
2418 && vlink_post_break(fwd) == null
2419 && next != null
2420 && type(next) == glyph_node
2421 && ((tlink_post_break(cur) != null && test_ligature(&lig, tlink_post_break(cur), next)) ||
2422 (tlink_no_break (cur) != null && test_ligature(&lig, tlink_no_break (cur), next)))) {
2423 /*tex Optionally building an |init_disc| followed by a |select_disc|: |{a-}{b}{AB} {-}{}{} c| */
2424 halfword last1 = vlink(next), tail;
2425 uncouple_node(next);
2426 try_couple_nodes(fwd, last1);
2427 /* the modes are ported from luametatex (not one-to-one, so they need checking) */
2428 if (discretionary_ligature_mode_par == 1) {
2429 /*tex f-f-i -> f-fi */
2430 halfword tail = tlink_no_break(cur);
2431 nesting_append(no_break(cur), copy_node(next));
2432 handle_lig_nest(no_break(cur), tail);
2433 tail = tlink_post_break(cur);
2434 nesting_append(post_break(cur), next);
2435 handle_lig_nest(post_break(cur), tail);
2436 try_couple_nodes(alink(fwd), vlink(fwd));
2437 flush_node(fwd);
2438 } else if (discretionary_ligature_mode_par == 2) {
2439 /*tex f-f-i -> ff-i : |{a-}{b}{AB} {-}{c}{}| => |{AB-}{c}{ABc}| */
2440 nesting_append(post_break(fwd), copy_node(next));
2441 if (vlink_no_break(cur)) {
2442 halfword tail;
2443 nesting_prepend_list(no_break(fwd), copy_node_list(vlink_no_break(cur)));
2444 tail = tlink_no_break(fwd);
2445 nesting_append(no_break(fwd), next);
2446 handle_lig_nest(no_break(fwd), tail);
2447 nesting_prepend_list(pre_break(fwd), copy_node_list(vlink_no_break(cur)));
2448 }
2449 try_couple_nodes(alink(cur), vlink(cur));
2450 flush_node(cur);
2451 cur = fwd;
2452 } else {
2453 /*tex |{a-}{b}{AB} {-}{c}{}| */
2454 nesting_append(post_break(fwd), copy_node(next));
2455 /*tex |{a-}{b}{AB} {-}{c}{-}| */
2456 if (vlink_no_break(cur) != null) {
2457 nesting_prepend(no_break(fwd), copy_node(vlink_pre_break(fwd)));
2458 }
2459 /*tex |{a-}{b}{AB} {b-}{c}{-}| */
2460 if (vlink_post_break(cur) != null)
2461 nesting_prepend_list(pre_break(fwd), copy_node_list(vlink_post_break(cur)));
2462 /*tex |{a-}{b}{AB} {b-}{c}{AB-}| */
2463 if (vlink_no_break(cur) != null) {
2464 nesting_prepend_list(no_break(fwd), copy_node_list(vlink_no_break(cur)));
2465 }
2466 /*tex |{a-}{b}{ABC} {b-}{c}{AB-}| */
2467 tail = tlink_no_break(cur);
2468 nesting_append(no_break(cur), copy_node(next));
2469 handle_lig_nest(no_break(cur), tail);
2470 /*tex |{a-}{BC}{ABC} {b-}{c}{AB-}| */
2471 tail = tlink_post_break(cur);
2472 nesting_append(post_break(cur), next);
2473 handle_lig_nest(post_break(cur), tail);
2474 /*tex Set the subtypes: */
2475 subtype(cur) = init_disc;
2476 subtype(fwd) = select_disc;
2477 }
2478
2479 }
2480 }
2481 }
2482
2483 } else {
2484 /*tex We have glyph nor disc. */
2485 return last;
2486 }
2487 /*tex Goto the next node, where |\par| allows |vlink(cur)| to be NULL. */
2488 last = cur;
2489 cur = vlink(cur);
2490 }
2491 return cur;
2492 }
2493
2494 /*tex The return value is the new tail, head should be a dummy: */
2495
2496 halfword handle_ligaturing(halfword head, halfword tail)
2497 {
2498 /*tex A trick to allow explicit |node==null| tests. */
2499 halfword save_tail1 = null;
2500 halfword cur, prev;
2501 if (vlink(head) == null)
2502 return tail;
2503 if (tail != null) {
2504 save_tail1 = vlink(tail);
2505 vlink(tail) = null;
2506 }
2507 if (fix_node_lists) {
2508 fix_node_list(head);
2509 }
2510 prev = head;
2511 cur = vlink(prev);
2512 while (cur != null) {
2513 if (type(cur) == glyph_node || (type(cur) == boundary_node)) {
2514 cur = handle_lig_word(cur);
2515 }
2516 prev = cur;
2517 cur = vlink(cur);
2518 }
2519 if (prev == null) {
2520 prev = tail;
2521 }
2522 if (tail != null) {
2523 try_couple_nodes(prev, save_tail1);
2524 }
2525 return prev;
2526 }
2527
2528
2529 /*tex Kerning starts here: */
2530
2531 static void add_kern_before(halfword left, halfword right)
2532 {
2533 if ((!is_rightghost(right)) &&
2534 font(left) == font(right) && has_kern(font(left), character(left))) {
2535 int k = raw_get_kern(font(left), character(left), character(right));
2536 if (k != 0) {
2537 halfword kern = new_kern(k);
2538 halfword prev = alink(right);
2539 couple_nodes(prev, kern);
2540 couple_nodes(kern, right);
2541 /*tex Update the attribute list (inherit from left): */
2542 delete_attribute_ref(node_attr(kern));
2543 add_node_attr_ref(node_attr(left));
2544 node_attr(kern) = node_attr(left);
2545 }
2546 }
2547 }
2548
2549 static void add_kern_after(halfword left, halfword right, halfword aft)
2550 {
2551 if ((!is_rightghost(right)) &&
2552 font(left) == font(right) && has_kern(font(left), character(left))) {
2553 int k = raw_get_kern(font(left), character(left), character(right));
2554 if (k != 0) {
2555 halfword kern = new_kern(k);
2556 halfword next = vlink(aft);
2557 couple_nodes(aft, kern);
2558 try_couple_nodes(kern, next);
2559 /*tex Update the attribute list (inherit from left == aft): */
2560 delete_attribute_ref(node_attr(kern));
2561 add_node_attr_ref(node_attr(aft));
2562 node_attr(kern) = node_attr(aft);
2563 }
2564 }
2565 }
2566
2567 static void do_handle_kerning(halfword root, halfword init_left, halfword init_right)
2568 {
2569 halfword cur = vlink(root);
2570 halfword left = null;
2571 if (cur == null) {
2572 if (init_left != null && init_right != null) {
2573 add_kern_after(init_left, init_right, root);
2574 tlink(root) = vlink(root);
2575 }
2576 return;
2577 }
2578 if (type(cur) == glyph_node) {
2579 set_is_glyph(cur);
2580 if (init_left != null)
2581 add_kern_before(init_left, cur);
2582 left = cur;
2583 }
2584 while ((cur = vlink(cur)) != null) {
2585 if (type(cur) == glyph_node) {
2586 set_is_glyph(cur);
2587 if (left != null) {
2588 add_kern_before(left, cur);
2589 if (character(left) < 0 || is_ghost(left)) {
2590 halfword prev = alink(left);
2591 couple_nodes(prev, cur);
2592 flush_node(left);
2593 }
2594 }
2595 left = cur;
2596 } else {
2597 if (type(cur) == disc_node) {
2598 halfword right = type(vlink(cur)) == glyph_node ? vlink(cur) : null;
2599 do_handle_kerning(pre_break(cur), left, null);
2600 if (vlink_pre_break(cur) != null)
2601 tlink_pre_break(cur) = tail_of_list(vlink_pre_break(cur));
2602 do_handle_kerning(post_break(cur), null, right);
2603 if (vlink_post_break(cur) != null)
2604 tlink_post_break(cur) = tail_of_list(vlink_post_break(cur));
2605 do_handle_kerning(no_break(cur), left, right);
2606 if (vlink_no_break(cur) != null)
2607 tlink_no_break(cur) = tail_of_list(vlink_no_break(cur));
2608 }
2609 if (left != null) {
2610 if (character(left) < 0 || is_ghost(left)) {
2611 halfword prev = alink(left);
2612 couple_nodes(prev, cur);
2613 flush_node(left);
2614 }
2615 left = null;
2616 }
2617 }
2618 }
2619 if (left != null) {
2620 if (init_right != null)
2621 add_kern_after(left, init_right, left);
2622 if (character(left) < 0 || is_ghost(left)) {
2623 halfword prev = alink(left);
2624 halfword next = vlink(left);
2625 if (next != null) {
2626 couple_nodes(prev, next);
2627 tlink(root) = next;
2628 } else if (prev != root) {
2629 vlink(prev) = null;
2630 tlink(root) = prev;
2631 } else {
2632 vlink(root) = null;
2633 tlink(root) = null;
2634 }
2635 flush_node(left);
2636 }
2637 }
2638 }
2639
2640 halfword handle_kerning(halfword head, halfword tail)
2641 {
2642 halfword save_link = null;
2643 if (tail == null) {
2644 tlink(head) = null;
2645 do_handle_kerning(head, null, null);
2646 } else {
2647 save_link = vlink(tail);
2648 vlink(tail) = null;
2649 tlink(head) = tail;
2650 do_handle_kerning(head, null, null);
2651 tail = tlink(head);
2652 if (valid_node(save_link)) {
2653 try_couple_nodes(tail, save_link);
2654 }
2655 }
2656 return tail;
2657 }
2658
2659 /*tex The ligaturing and kerning \LUA\ interface: */
2660
2661 static void run_lua_ligkern_callback(halfword head, halfword tail, int callback_id)
2662 {
2663 int i;
2664 int top = lua_gettop(Luas);
2665 if (!get_callback(Luas, callback_id)) {
2666 lua_settop(Luas, top);
2667 }
2668 nodelist_to_lua(Luas, head);
2669 nodelist_to_lua(Luas, tail);
2670 if ((i=lua_pcall(Luas, 2, 0, 0)) != 0) {
2671 formatted_warning("ligkern","error: %s",lua_tostring(Luas, -1));
2672 lua_settop(Luas, top);
2673 luatex_error(Luas, (i == LUA_ERRRUN ? 0 : 1));
2674 }
2675 if (fix_node_lists) {
2676 fix_node_list(head);
2677 }
2678 lua_settop(Luas, top);
2679 }
2680
2681 halfword new_ligkern(halfword head, halfword tail)
2682 {
2683 int callback_id = 0;
2684 if (! head)
2685 return null;
2686 if (vlink(head) == null)
2687 return tail;
2688 callback_id = callback_defined(ligaturing_callback);
2689 if (callback_id > 0) {
2690 halfword save_tail = null;
2691 if (tail) {
2692 save_tail = vlink(tail);
2693 vlink(tail) = null;
2694 }
2695 run_lua_ligkern_callback(head, tail, callback_id);
2696 tail = tail_of_list(head);
2697 if (save_tail) {
2698 try_couple_nodes(tail, save_tail);
2699 }
2700 tail = tail_of_list(head);
2701 } else if (callback_id == 0) {
2702 tail = handle_ligaturing(head, tail);
2703 }
2704 callback_id = callback_defined(kerning_callback);
2705 if (callback_id > 0) {
2706 run_lua_ligkern_callback(head, tail, callback_id);
2707 tail = tail_of_list(head);
2708 } else if (callback_id == 0) {
2709 halfword nest1 = new_node(nesting_node, 1);
2710 halfword cur = vlink(head);
2711 halfword aft = vlink(tail);
2712 couple_nodes(nest1, cur);
2713 tlink(nest1) = tail;
2714 vlink(tail) = null;
2715 do_handle_kerning(nest1, null, null);
2716 couple_nodes(head, vlink(nest1));
2717 tail = tlink(nest1);
2718 try_couple_nodes(tail, aft);
2719 flush_node(nest1);
2720 }
2721 return tail;
2722 }