Add a type to `Control' so that we can extend it.
[ttfautohint.git] / lib / tabytecode.c
blob33c14c9268f2be0ee7c384b95b9a7f999e2e7cb3
1 /* tabytecode.c */
3 /*
4 * Copyright (C) 2011-2014 by Werner Lemberg.
6 * This file is part of the ttfautohint library, and may only be used,
7 * modified, and distributed under the terms given in `COPYING'. By
8 * continuing to use, modify, or distribute this file you indicate that you
9 * have read `COPYING' and understand and accept it fully.
11 * The file `COPYING' mentioned in the previous paragraph is distributed
12 * with the ttfautohint library.
16 #include "ta.h"
18 #include <stdbool.h> /* for llrb.h */
20 #include "llrb.h" /* a red-black tree implementation */
21 #include "tahints.h"
24 #define DEBUGGING
27 #ifdef TA_DEBUG
28 int _ta_debug = 0;
29 int _ta_debug_global = 0;
30 int _ta_debug_disable_horz_hints;
31 int _ta_debug_disable_vert_hints;
32 int _ta_debug_disable_blue_hints;
33 void* _ta_debug_hints;
34 #endif
37 /* node structures for point hints */
39 typedef struct Node1 Node1;
40 struct Node1
42 LLRB_ENTRY(Node1) entry1;
43 FT_UShort point;
46 typedef struct Node2 Node2;
47 struct Node2
49 LLRB_ENTRY(Node2) entry2;
50 FT_UShort edge;
51 FT_UShort point;
54 typedef struct Node3 Node3;
55 struct Node3
57 LLRB_ENTRY(Node3) entry3;
58 FT_UShort before_edge;
59 FT_UShort after_edge;
60 FT_UShort point;
64 /* comparison functions for our red-black trees */
66 static int
67 node1cmp(Node1* e1,
68 Node1* e2)
70 /* sort by points */
71 return e1->point - e2->point;
74 static int
75 node2cmp(Node2* e1,
76 Node2* e2)
78 FT_Int delta;
81 /* sort by edges ... */
82 delta = (FT_Int)e1->edge - (FT_Int)e2->edge;
83 if (delta)
84 return delta;
86 /* ... then by points */
87 return (FT_Int)e1->point - (FT_Int)e2->point;
90 static int
91 node3cmp(Node3* e1,
92 Node3* e2)
94 FT_Int delta;
97 /* sort by `before' edges ... */
98 delta = (FT_Int)e1->before_edge - (FT_Int)e2->before_edge;
99 if (delta)
100 return delta;
102 /* ... then by `after' edges ... */
103 delta = (FT_Int)e1->after_edge - (FT_Int)e2->after_edge;
104 if (delta)
105 return delta;
107 /* ... then by points */
108 return (FT_Int)e1->point - (FT_Int)e2->point;
112 /* the red-black tree function bodies */
113 typedef struct ip_before_points ip_before_points;
114 typedef struct ip_after_points ip_after_points;
115 typedef struct ip_on_points ip_on_points;
116 typedef struct ip_between_points ip_between_points;
118 LLRB_HEAD(ip_before_points, Node1);
119 LLRB_HEAD(ip_after_points, Node1);
120 LLRB_HEAD(ip_on_points, Node2);
121 LLRB_HEAD(ip_between_points, Node3);
123 /* no trailing semicolon in the next four lines */
124 LLRB_GENERATE_STATIC(ip_before_points, Node1, entry1, node1cmp)
125 LLRB_GENERATE_STATIC(ip_after_points, Node1, entry1, node1cmp)
126 LLRB_GENERATE_STATIC(ip_on_points, Node2, entry2, node2cmp)
127 LLRB_GENERATE_STATIC(ip_between_points, Node3, entry3, node3cmp)
130 typedef struct Hints_Record_
132 FT_UInt size;
133 FT_UInt num_actions;
134 FT_Byte* buf;
135 FT_UInt buf_len;
136 } Hints_Record;
138 typedef struct Recorder_
140 SFNT* sfnt;
141 FONT* font;
142 GLYPH* glyph; /* the current glyph */
143 Hints_Record hints_record;
145 /* some segments can `wrap around' */
146 /* a contour's start point like 24-25-26-0-1-2 */
147 /* (there can be at most one such segment per contour); */
148 /* later on we append additional records */
149 /* to split them into 24-26 and 0-2 */
150 FT_UShort* wrap_around_segments;
151 FT_UShort num_wrap_around_segments;
153 FT_UShort num_stack_elements; /* the necessary stack depth so far */
155 /* data necessary for strong point interpolation */
156 ip_before_points ip_before_points_head;
157 ip_after_points ip_after_points_head;
158 ip_on_points ip_on_points_head;
159 ip_between_points ip_between_points_head;
161 FT_UShort num_strong_points;
162 FT_UShort num_segments;
163 } Recorder;
166 /* this is the bytecode of the `.ttfautohint' glyph */
168 FT_Byte ttfautohint_glyph_bytecode[7] =
171 /* increment `cvtl_is_subglyph' counter */
172 PUSHB_3,
173 cvtl_is_subglyph,
174 100,
175 cvtl_is_subglyph,
176 RCVT,
177 ADD,
178 WCVTP,
184 * convert array `args' into a sequence of NPUSHB, NPUSHW, PUSHB_X, and
185 * PUSHW_X instructions to be stored in `bufp' (the latter two instructions
186 * only if `optimize' is not set); if `need_words' is set, NPUSHW and
187 * PUSHW_X gets used
190 FT_Byte*
191 TA_build_push(FT_Byte* bufp,
192 FT_UInt* args,
193 FT_UInt num_args,
194 FT_Bool need_words,
195 FT_Bool optimize)
197 FT_UInt* arg = args;
198 FT_UInt i, j, nargs;
201 if (need_words)
203 for (i = 0; i < num_args; i += 255)
205 nargs = (num_args - i > 255) ? 255 : num_args - i;
207 if (optimize && nargs <= 8)
208 BCI(PUSHW_1 - 1 + nargs);
209 else
211 BCI(NPUSHW);
212 BCI(nargs);
214 for (j = 0; j < nargs; j++)
216 BCI(HIGH(*arg));
217 BCI(LOW(*arg));
218 arg++;
222 else
224 for (i = 0; i < num_args; i += 255)
226 nargs = (num_args - i > 255) ? 255 : num_args - i;
228 if (optimize && nargs <= 8)
229 BCI(PUSHB_1 - 1 + nargs);
230 else
232 BCI(NPUSHB);
233 BCI(nargs);
235 for (j = 0; j < nargs; j++)
237 BCI(*arg);
238 arg++;
243 return bufp;
248 * We optimize two common cases, replacing
250 * NPUSHB A ... NPUSHB B [... NPUSHB C] ... CALL
252 * with
254 * NPUSHB (A+B[+C]) ... CALL
256 * if possible
259 FT_Byte*
260 TA_optimize_push(FT_Byte* buf,
261 FT_Byte** pos)
263 FT_Byte sizes[3];
264 FT_Byte new_size1;
265 FT_Byte new_size2;
267 FT_UInt sum;
268 FT_UInt i;
269 FT_UInt pos_idx;
271 FT_Byte* p;
272 FT_Byte* bufp;
275 /* XXX improve handling of NPUSHW */
276 if (*(pos[0]) == NPUSHW
277 || *(pos[1]) == NPUSHW
278 || *(pos[2]) == NPUSHW)
279 return buf;
281 /* the point hints records block can be missing */
282 if (pos[0] == pos[1])
284 pos[1] = pos[2];
285 pos[2] = NULL;
288 /* there are at least two NPUSHB instructions */
289 /* (one of them directly at the start) */
290 sizes[0] = *(pos[0] + 1);
291 sizes[1] = *(pos[1] + 1);
292 sizes[2] = pos[2] ? *(pos[2] + 1) : 0;
294 sum = sizes[0] + sizes[1] + sizes[2];
296 if (sum > 2 * 0xFF)
297 return buf; /* nothing to do since we need three NPUSHB */
298 else if (!sizes[2] && (sum > 0xFF))
299 return buf; /* nothing to do since we need two NPUSHB */
301 if (sum > 0xFF)
303 /* reduce three NPUSHB to two */
304 new_size1 = 0xFF;
305 new_size2 = sum - 0xFF;
307 else
309 /* reduce two or three NPUSHB to one */
310 new_size1 = sum;
311 new_size2 = 0;
314 /* pack data */
315 p = buf;
316 bufp = buf;
317 pos_idx = 0;
319 if (new_size1 <= 8)
320 BCI(PUSHB_1 - 1 + new_size1);
321 else
323 BCI(NPUSHB);
324 BCI(new_size1);
326 for (i = 0; i < new_size1; i++)
328 if (p == pos[pos_idx])
330 pos_idx++;
331 p += 2; /* skip old NPUSHB */
333 *(bufp++) = *(p++);
336 if (new_size2)
338 if (new_size2 <= 8)
339 BCI(PUSHB_1 - 1 + new_size2);
340 else
342 BCI(NPUSHB);
343 BCI(new_size2);
345 for (i = 0; i < new_size2; i++)
347 if (p == pos[pos_idx])
349 pos_idx++;
350 p += 2;
352 *(bufp++) = *(p++);
356 BCI(CALL);
358 return bufp;
362 /* We add a subglyph for each composite glyph. */
363 /* Since subglyphs must contain at least one point, */
364 /* we have to adjust all point indices accordingly. */
365 /* Using the `pointsums' array of the `GLYPH' structure */
366 /* it is straightforward to do that: */
367 /* Assuming that point with index x is in the interval */
368 /* pointsums[n] <= x < pointsums[n + 1], */
369 /* the new point index is x + n. */
371 static FT_UInt
372 TA_adjust_point_index(Recorder* recorder,
373 FT_UInt idx)
375 FONT* font = recorder->font;
376 GLYPH* glyph = recorder->glyph;
377 FT_UShort i;
380 if (!glyph->num_components || !font->hint_composites)
381 return idx; /* not a composite glyph */
383 for (i = 0; i < glyph->num_pointsums; i++)
384 if (idx < glyph->pointsums[i])
385 break;
387 return idx + i;
391 /* we store the segments in the storage area; */
392 /* each segment record consists of the first and last point */
394 static FT_Byte*
395 TA_sfnt_build_glyph_segments(SFNT* sfnt,
396 Recorder* recorder,
397 FT_Byte* bufp,
398 FT_Bool optimize)
400 FONT* font = recorder->font;
401 TA_GlyphHints hints = &font->loader->hints;
402 TA_AxisHints axis = &hints->axis[TA_DIMENSION_VERT];
403 TA_Point points = hints->points;
404 TA_Segment segments = axis->segments;
405 TA_Segment seg;
406 TA_Segment seg_limit;
408 SFNT_Table* glyf_table = &font->tables[sfnt->glyf_idx];
409 glyf_Data* data = (glyf_Data*)glyf_table->data;
411 FT_UInt style_id = data->style_ids
412 [font->loader->metrics->style_class->style];
414 FT_Outline outline = font->loader->gloader->base.outline;
416 FT_UInt* args;
417 FT_UInt* arg;
418 FT_UInt num_args;
419 FT_UShort num_segments;
421 FT_Bool need_words = 0;
423 FT_Int n;
424 FT_UInt base;
425 FT_UShort num_packed_segments;
426 FT_UShort num_storage;
427 FT_UShort num_stack_elements;
428 FT_UShort num_twilight_points;
431 seg_limit = segments + axis->num_segments;
432 num_segments = axis->num_segments;
434 /* to pack the data in the bytecode more tightly, */
435 /* we store up to the first nine segments in nibbles if possible, */
436 /* using delta values */
437 base = 0;
438 num_packed_segments = 0;
439 for (seg = segments; seg < seg_limit; seg++)
441 FT_UInt first = seg->first - points;
442 FT_UInt last = seg->last - points;
445 first = TA_adjust_point_index(recorder, first);
446 last = TA_adjust_point_index(recorder, last);
448 if (first - base >= 16)
449 break;
450 if (first > last || last - first >= 16)
451 break;
452 if (num_packed_segments == 9)
453 break;
454 num_packed_segments++;
455 base = last;
458 /* also handle wrap-around segments */
459 num_segments += recorder->num_wrap_around_segments;
461 /* wrap-around segments are pushed with four arguments; */
462 /* a segment stored in nibbles needs only one byte instead of two */
463 num_args = num_packed_segments
464 + 2 * (num_segments - num_packed_segments)
465 + 2 * recorder->num_wrap_around_segments
466 + 3;
468 /* collect all arguments temporarily in an array (in reverse order) */
469 /* so that we can easily split into chunks of 255 args */
470 /* as needed by NPUSHB and NPUSHW, respectively */
471 args = (FT_UInt*)malloc(num_args * sizeof (FT_UInt));
472 if (!args)
473 return NULL;
475 arg = args + num_args - 1;
477 if (num_segments > 0xFF)
478 need_words = 1;
480 /* the number of packed segments is indicated by the function number */
481 if (recorder->glyph->num_components && font->hint_composites)
482 *(arg--) = bci_create_segments_composite_0 + num_packed_segments;
483 else
484 *(arg--) = bci_create_segments_0 + num_packed_segments;
486 *(arg--) = CVT_SCALING_VALUE_OFFSET(style_id);
487 *(arg--) = num_segments;
489 base = 0;
490 for (seg = segments; seg < segments + num_packed_segments; seg++)
492 FT_UInt first = seg->first - points;
493 FT_UInt last = seg->last - points;
494 FT_UInt low_nibble;
495 FT_UInt high_nibble;
498 first = TA_adjust_point_index(recorder, first);
499 last = TA_adjust_point_index(recorder, last);
501 low_nibble = first - base;
502 high_nibble = last - first;
504 *(arg--) = 16 * high_nibble + low_nibble;
506 base = last;
509 for (seg = segments + num_packed_segments; seg < seg_limit; seg++)
511 FT_UInt first = seg->first - points;
512 FT_UInt last = seg->last - points;
515 *(arg--) = TA_adjust_point_index(recorder, first);
516 *(arg--) = TA_adjust_point_index(recorder, last);
518 /* we push the last and first contour point */
519 /* as a third and fourth argument in wrap-around segments */
520 if (first > last)
522 for (n = 0; n < outline.n_contours; n++)
524 FT_UInt end = (FT_UInt)outline.contours[n];
527 if (first <= end)
529 *(arg--) = TA_adjust_point_index(recorder, end);
530 if (end > 0xFF)
531 need_words = 1;
533 if (n == 0)
534 *(arg--) = TA_adjust_point_index(recorder, 0);
535 else
536 *(arg--) = TA_adjust_point_index(recorder,
537 (FT_UInt)outline.contours[n - 1] + 1);
538 break;
543 if (last > 0xFF)
544 need_words = 1;
547 /* emit the second part of wrap-around segments as separate segments */
548 /* so that edges can easily link to them */
549 for (seg = segments; seg < seg_limit; seg++)
551 FT_UInt first = seg->first - points;
552 FT_UInt last = seg->last - points;
555 if (first > last)
557 for (n = 0; n < outline.n_contours; n++)
559 if (first <= (FT_UInt)outline.contours[n])
561 if (n == 0)
562 *(arg--) = TA_adjust_point_index(recorder, 0);
563 else
564 *(arg--) = TA_adjust_point_index(recorder,
565 (FT_UInt)outline.contours[n - 1] + 1);
566 break;
570 *(arg--) = TA_adjust_point_index(recorder, last);
574 /* with most fonts it is very rare */
575 /* that any of the pushed arguments is larger than 0xFF, */
576 /* thus we refrain from further optimizing this case */
577 bufp = TA_build_push(bufp, args, num_args, need_words, optimize);
579 BCI(CALL);
581 num_storage = sal_segment_offset + num_segments * 2;
582 if (num_storage > sfnt->max_storage)
583 sfnt->max_storage = num_storage;
585 num_twilight_points = num_segments * 2;
586 if (num_twilight_points > sfnt->max_twilight_points)
587 sfnt->max_twilight_points = num_twilight_points;
589 /* both this function and `TA_emit_hints_record' */
590 /* push data onto the stack */
591 num_stack_elements = ADDITIONAL_STACK_ELEMENTS
592 + recorder->num_stack_elements + num_args;
593 if (num_stack_elements > sfnt->max_stack_elements)
594 sfnt->max_stack_elements = num_stack_elements;
596 free(args);
598 return bufp;
602 static void
603 build_delta_exception(const Ctrl* ctrl,
604 FT_UInt** delta_args,
605 int* num_delta_args)
607 int offset;
608 int ppem;
609 int x_shift;
610 int y_shift;
613 ppem = ctrl->ppem - CONTROL_DELTA_PPEM_MIN;
615 if (ppem < 16)
616 offset = 0;
617 else if (ppem < 32)
618 offset = 1;
619 else
620 offset = 2;
622 ppem -= offset << 4;
625 * Using
627 * delta_shift = 3 ,
629 * the possible shift values in the instructions are indexed as follows:
631 * 0 -1px
632 * 1 -7/8px
633 * ...
634 * 7 -1/8px
635 * 8 1/8px
636 * ...
637 * 14 7/8px
638 * 15 1px
640 * (note that there is no index for a zero shift).
643 if (ctrl->x_shift < 0)
644 x_shift = ctrl->x_shift + 8;
645 else
646 x_shift = ctrl->x_shift + 7;
648 if (ctrl->y_shift < 0)
649 y_shift = ctrl->y_shift + 8;
650 else
651 y_shift = ctrl->y_shift + 7;
653 /* add point index and exception specification to appropriate stack */
654 if (ctrl->x_shift)
656 *(delta_args[offset] + num_delta_args[offset]++) =
657 (ppem << 4) + x_shift;
658 *(delta_args[offset] + num_delta_args[offset]++) =
659 ctrl->point_idx;
662 if (ctrl->y_shift)
664 offset += 3;
665 *(delta_args[offset] + num_delta_args[offset]++) =
666 (ppem << 4) + y_shift;
667 *(delta_args[offset] + num_delta_args[offset]++) =
668 ctrl->point_idx;
673 static FT_Byte*
674 TA_sfnt_build_delta_exceptions(SFNT* sfnt,
675 FONT* font,
676 FT_Long idx,
677 FT_Byte* bufp)
679 FT_Face face = font->loader->face;
681 int num_points;
682 int i;
684 FT_UShort num_stack_elements;
686 /* DELTAP[1-3] stacks for both x and y directions */
687 FT_UInt* delta_args[6] = {NULL, NULL, NULL, NULL, NULL, NULL};
688 int num_delta_args[6] = {0, 0, 0, 0, 0, 0};
689 FT_UInt* args = NULL;
691 FT_Bool need_words = 0;
692 FT_Bool need_word_counts = 0;
693 FT_Bool allocated = 0;
695 const Ctrl* ctrl;
698 num_points = font->loader->gloader->base.outline.n_points;
700 /* loop over all fitting control instructions */
701 for (;;)
703 ctrl = TA_control_get_ctrl(font);
705 if (!ctrl)
706 break;
708 /* check type */
709 if (!(ctrl->type == Control_Delta_before_IUP
710 || ctrl->type == Control_Delta_after_IUP))
711 break;
713 /* too large values of font and glyph indices in `ctrl' */
714 /* are handled by later calls of this function */
715 if (face->face_index < ctrl->font_idx
716 || idx < ctrl->glyph_idx)
717 break;
719 if (!allocated)
721 for (i = 0; i < 6; i++)
723 /* see the comment on allocating `ins_buf' in function */
724 /* `TA_sfnt_build_glyph_instructions' for more on the array sizes; */
725 /* we have to increase by 1 for the number of argument pairs */
726 delta_args[i] = (FT_UInt*)malloc((16 * 2 * num_points + 1)
727 * sizeof (FT_UInt));
728 if (!delta_args[i])
730 bufp = NULL;
731 goto Done;
735 allocated = 1;
738 /* since we walk sequentially over all glyphs (with points), */
739 /* and the control instruction entries have the same order, */
740 /* we don't need to test for equality of font and glyph indices: */
741 /* at this very point in the code we certainly have a hit */
742 build_delta_exception(ctrl, delta_args, num_delta_args);
744 if (ctrl->point_idx > 255)
745 need_words = 1;
747 TA_control_get_next(font);
750 /* nothing to do if no control instructions */
751 if (!allocated)
752 return bufp;
754 /* add number of argument pairs to the stacks */
755 for (i = 0; i < 6; i++)
757 if (num_delta_args[i])
759 int n = num_delta_args[i] >> 1;
762 if (n > 255)
763 need_word_counts = 1;
765 *(delta_args[i] + num_delta_args[i]) = n;
766 num_delta_args[i]++;
770 /* merge delta stacks into a single one */
771 if (need_words
772 || (!need_words && !need_word_counts))
774 FT_UInt num_args = 0;
777 for (i = 0; i < 6; i++)
779 FT_UInt* args_new;
780 FT_UInt num_args_new;
783 if (!num_delta_args[i])
784 continue;
786 num_args_new = num_args + num_delta_args[i];
787 args_new = (FT_UInt*)realloc(args, num_args_new * sizeof (FT_UInt));
788 if (!args_new)
790 bufp = NULL;
791 goto Done;
794 memcpy(args_new + num_args,
795 delta_args[i],
796 num_delta_args[i] * sizeof (FT_UInt));
798 args = args_new;
799 num_args = num_args_new;
802 num_stack_elements = num_args;
804 bufp = TA_build_push(bufp, args, num_args, need_words, 1);
806 else
808 num_stack_elements = 0;
810 /* stack elements are bytes, but counts need words */
811 for (i = 0; i < 6; i++)
813 int num_delta_arg;
816 if (!num_delta_args[i])
817 continue;
819 num_delta_arg = num_delta_args[i] - 1;
821 bufp = TA_build_push(bufp,
822 delta_args[i],
823 num_delta_arg,
824 need_words,
827 num_stack_elements += num_delta_arg + 1;
829 num_delta_arg >>= 1;
830 BCI(PUSHW_1);
831 BCI(HIGH(num_delta_arg));
832 BCI(LOW(num_delta_arg));
836 /* emit the DELTA opcodes */
837 if (num_delta_args[5])
838 BCI(DELTAP3);
839 if (num_delta_args[4])
840 BCI(DELTAP2);
841 if (num_delta_args[3])
842 BCI(DELTAP1);
844 if (num_delta_args[2] || num_delta_args[1] || num_delta_args[0])
845 BCI(SVTCA_x);
847 if (num_delta_args[2])
848 BCI(DELTAP3);
849 if (num_delta_args[1])
850 BCI(DELTAP2);
851 if (num_delta_args[0])
852 BCI(DELTAP1);
854 Done:
855 for (i = 0; i < 6; i++)
856 free(delta_args[i]);
857 free(args);
859 if (num_stack_elements > sfnt->max_stack_elements)
860 sfnt->max_stack_elements = num_stack_elements;
861 return bufp;
865 static FT_Byte*
866 TA_sfnt_build_glyph_scaler(SFNT* sfnt,
867 Recorder* recorder,
868 FT_Byte* bufp)
870 FONT* font = recorder->font;
871 FT_GlyphSlot glyph = sfnt->face->glyph;
872 FT_Vector* points = glyph->outline.points;
873 FT_Int num_contours = glyph->outline.n_contours;
875 FT_UInt* args;
876 FT_UInt* arg;
877 FT_UInt num_args;
879 FT_Bool need_words = 0;
880 FT_Int p, q;
881 FT_Int start, end;
882 FT_UShort num_storage;
883 FT_UShort num_stack_elements;
886 num_args = 2 * num_contours + 2;
888 /* collect all arguments temporarily in an array (in reverse order) */
889 /* so that we can easily split into chunks of 255 args */
890 /* as needed by NPUSHB and NPUSHW, respectively */
891 args = (FT_UInt*)malloc(num_args * sizeof (FT_UInt));
892 if (!args)
893 return NULL;
895 arg = args + num_args - 1;
897 if (num_args > 0xFF)
898 need_words = 1;
900 if (recorder->glyph->num_components && font->hint_composites)
901 *(arg--) = bci_scale_composite_glyph;
902 else
903 *(arg--) = bci_scale_glyph;
904 *(arg--) = num_contours;
906 start = 0;
907 end = 0;
909 for (p = 0; p < num_contours; p++)
911 FT_Int max = start;
912 FT_Int min = start;
915 end = glyph->outline.contours[p];
917 for (q = start; q <= end; q++)
919 if (points[q].y < points[min].y)
920 min = q;
921 if (points[q].y > points[max].y)
922 max = q;
925 if (min > max)
927 *(arg--) = TA_adjust_point_index(recorder, max);
928 *(arg--) = TA_adjust_point_index(recorder, min);
930 else
932 *(arg--) = TA_adjust_point_index(recorder, min);
933 *(arg--) = TA_adjust_point_index(recorder, max);
936 start = end + 1;
939 if (end > 0xFF)
940 need_words = 1;
942 /* with most fonts it is very rare */
943 /* that any of the pushed arguments is larger than 0xFF, */
944 /* thus we refrain from further optimizing this case */
945 bufp = TA_build_push(bufp, args, num_args, need_words, 1);
947 BCI(CALL);
949 num_storage = sal_segment_offset;
950 if (num_storage > sfnt->max_storage)
951 sfnt->max_storage = num_storage;
953 num_stack_elements = ADDITIONAL_STACK_ELEMENTS + num_args;
954 if (num_stack_elements > sfnt->max_stack_elements)
955 sfnt->max_stack_elements = num_stack_elements;
957 free(args);
959 return bufp;
963 static FT_Byte*
964 TA_font_build_subglyph_shifter(FONT* font,
965 FT_Byte* bufp)
967 FT_Face face = font->loader->face;
968 FT_GlyphSlot glyph = face->glyph;
970 TA_GlyphLoader gloader = font->loader->gloader;
972 TA_SubGlyph subglyphs = gloader->base.subglyphs;
973 TA_SubGlyph subglyph_limit = subglyphs + gloader->base.num_subglyphs;
974 TA_SubGlyph subglyph;
976 FT_Int curr_contour = 0;
979 for (subglyph = subglyphs; subglyph < subglyph_limit; subglyph++)
981 FT_Error error;
983 FT_UShort flags = subglyph->flags;
984 FT_Pos y_offset = subglyph->arg2;
986 FT_Int num_contours;
989 /* load subglyph to get the number of contours */
990 error = FT_Load_Glyph(face, subglyph->index, FT_LOAD_NO_SCALE);
991 if (error)
992 return NULL;
993 num_contours = glyph->outline.n_contours;
995 /* nothing to do if there is a point-to-point alignment */
996 if (!(flags & FT_SUBGLYPH_FLAG_ARGS_ARE_XY_VALUES))
997 goto End;
999 /* nothing to do if y offset is zero */
1000 if (!y_offset)
1001 goto End;
1003 /* nothing to do if there are no contours */
1004 if (!num_contours)
1005 goto End;
1007 /* note that calling `FT_Load_Glyph' without FT_LOAD_NO_RECURSE */
1008 /* ensures that composite subglyphs are represented as simple glyphs */
1010 if (num_contours > 0xFF
1011 || curr_contour > 0xFF)
1013 BCI(PUSHW_2);
1014 BCI(HIGH(curr_contour));
1015 BCI(LOW(curr_contour));
1016 BCI(HIGH(num_contours));
1017 BCI(LOW(num_contours));
1019 else
1021 BCI(PUSHB_2);
1022 BCI(curr_contour);
1023 BCI(num_contours);
1026 /* there are high chances that this value needs PUSHW, */
1027 /* thus we handle it separately */
1028 if (y_offset > 0xFF || y_offset < 0)
1030 BCI(PUSHW_1);
1031 BCI(HIGH(y_offset));
1032 BCI(LOW(y_offset));
1034 else
1036 BCI(PUSHB_1);
1037 BCI(y_offset);
1040 BCI(PUSHB_1);
1041 BCI(bci_shift_subglyph);
1042 BCI(CALL);
1044 End:
1045 curr_contour += num_contours;
1048 return bufp;
1053 * The four `ta_ip_*' actions in the `TA_hints_recorder' callback store its
1054 * data in four arrays (which are simple but waste a lot of memory). The
1055 * function below converts them into bytecode.
1057 * For `ta_ip_before' and `ta_ip_after', the collected points are emitted
1058 * together with the edge they correspond to.
1060 * For both `ta_ip_on' and `ta_ip_between', an outer loop is constructed to
1061 * loop over the edge or edge pairs, respectively, and each edge or edge
1062 * pair contains an inner loop to emit the correponding points.
1065 static void
1066 TA_build_point_hints(Recorder* recorder,
1067 TA_GlyphHints hints)
1069 TA_AxisHints axis = &hints->axis[TA_DIMENSION_VERT];
1070 TA_Segment segments = axis->segments;
1071 TA_Edge edges = axis->edges;
1073 FT_Byte* p = recorder->hints_record.buf;
1075 FT_UShort i;
1076 FT_UShort j;
1078 FT_UShort prev_edge;
1079 FT_UShort prev_before_edge;
1080 FT_UShort prev_after_edge;
1082 Node1* before_node;
1083 Node1* after_node;
1084 Node2* on_node;
1085 Node3* between_node;
1088 /* we store everything as 16bit numbers; */
1089 /* the function numbers (`ta_ip_before', etc.) */
1090 /* reflect the order in the TA_Action enumeration */
1092 /* ip_before_points */
1094 i = 0;
1095 for (before_node = LLRB_MIN(ip_before_points,
1096 &recorder->ip_before_points_head);
1097 before_node;
1098 before_node = LLRB_NEXT(ip_before_points,
1099 &recorder->ip_before_points_head,
1100 before_node))
1102 /* count points */
1103 i++;
1106 if (i)
1108 TA_Edge edge;
1111 recorder->hints_record.num_actions++;
1113 edge = edges;
1115 *(p++) = 0;
1116 *(p++) = (FT_Byte)ta_ip_before + ACTION_OFFSET;
1117 *(p++) = HIGH(edge->first - segments);
1118 *(p++) = LOW(edge->first - segments);
1119 *(p++) = HIGH(i);
1120 *(p++) = LOW(i);
1122 for (before_node = LLRB_MIN(ip_before_points,
1123 &recorder->ip_before_points_head);
1124 before_node;
1125 before_node = LLRB_NEXT(ip_before_points,
1126 &recorder->ip_before_points_head,
1127 before_node))
1129 FT_UInt point;
1132 point = TA_adjust_point_index(recorder, before_node->point);
1133 *(p++) = HIGH(point);
1134 *(p++) = LOW(point);
1138 /* ip_after_points */
1140 i = 0;
1141 for (after_node = LLRB_MIN(ip_after_points,
1142 &recorder->ip_after_points_head);
1143 after_node;
1144 after_node = LLRB_NEXT(ip_after_points,
1145 &recorder->ip_after_points_head,
1146 after_node))
1148 /* count points */
1149 i++;
1152 if (i)
1154 TA_Edge edge;
1157 recorder->hints_record.num_actions++;
1159 edge = edges + axis->num_edges - 1;
1161 *(p++) = 0;
1162 *(p++) = (FT_Byte)ta_ip_after + ACTION_OFFSET;
1163 *(p++) = HIGH(edge->first - segments);
1164 *(p++) = LOW(edge->first - segments);
1165 *(p++) = HIGH(i);
1166 *(p++) = LOW(i);
1168 for (after_node = LLRB_MIN(ip_after_points,
1169 &recorder->ip_after_points_head);
1170 after_node;
1171 after_node = LLRB_NEXT(ip_after_points,
1172 &recorder->ip_after_points_head,
1173 after_node))
1175 FT_UInt point;
1178 point = TA_adjust_point_index(recorder, after_node->point);
1179 *(p++) = HIGH(point);
1180 *(p++) = LOW(point);
1184 /* ip_on_point_array */
1186 prev_edge = 0xFFFF;
1187 i = 0;
1188 for (on_node = LLRB_MIN(ip_on_points,
1189 &recorder->ip_on_points_head);
1190 on_node;
1191 on_node = LLRB_NEXT(ip_on_points,
1192 &recorder->ip_on_points_head,
1193 on_node))
1195 /* count edges */
1196 if (on_node->edge != prev_edge)
1198 i++;
1199 prev_edge = on_node->edge;
1203 if (i)
1205 recorder->hints_record.num_actions++;
1207 *(p++) = 0;
1208 *(p++) = (FT_Byte)ta_ip_on + ACTION_OFFSET;
1209 *(p++) = HIGH(i);
1210 *(p++) = LOW(i);
1212 for (on_node = LLRB_MIN(ip_on_points,
1213 &recorder->ip_on_points_head);
1214 on_node;
1215 on_node = LLRB_NEXT(ip_on_points,
1216 &recorder->ip_on_points_head,
1217 on_node))
1219 Node2* edge_node;
1220 TA_Edge edge;
1223 edge = edges + on_node->edge;
1225 *(p++) = HIGH(edge->first - segments);
1226 *(p++) = LOW(edge->first - segments);
1228 /* save current position */
1229 edge_node = on_node;
1230 j = 0;
1231 for (;
1232 on_node;
1233 on_node = LLRB_NEXT(ip_on_points,
1234 &recorder->ip_on_points_head,
1235 on_node))
1237 /* count points on current edge */
1238 if (on_node->edge != edge_node->edge)
1239 break;
1240 j++;
1243 *(p++) = HIGH(j);
1244 *(p++) = LOW(j);
1246 /* restore current position */
1247 on_node = edge_node;
1248 for (;
1249 on_node;
1250 on_node = LLRB_NEXT(ip_on_points,
1251 &recorder->ip_on_points_head,
1252 on_node))
1254 FT_UInt point;
1257 if (on_node->edge != edge_node->edge)
1258 break;
1260 point = TA_adjust_point_index(recorder, on_node->point);
1261 *(p++) = HIGH(point);
1262 *(p++) = LOW(point);
1264 /* keep track of previous node */
1265 edge_node = on_node;
1268 /* reset loop iterator by one element, then continue */
1269 on_node = edge_node;
1273 /* ip_between_point_array */
1275 prev_before_edge = 0xFFFF;
1276 prev_after_edge = 0xFFFF;
1277 i = 0;
1278 for (between_node = LLRB_MIN(ip_between_points,
1279 &recorder->ip_between_points_head);
1280 between_node;
1281 between_node = LLRB_NEXT(ip_between_points,
1282 &recorder->ip_between_points_head,
1283 between_node))
1285 /* count `(before,after)' edge pairs */
1286 if (between_node->before_edge != prev_before_edge
1287 || between_node->after_edge != prev_after_edge)
1289 i++;
1290 prev_before_edge = between_node->before_edge;
1291 prev_after_edge = between_node->after_edge;
1295 if (i)
1297 recorder->hints_record.num_actions++;
1299 *(p++) = 0;
1300 *(p++) = (FT_Byte)ta_ip_between + ACTION_OFFSET;
1301 *(p++) = HIGH(i);
1302 *(p++) = LOW(i);
1304 for (between_node = LLRB_MIN(ip_between_points,
1305 &recorder->ip_between_points_head);
1306 between_node;
1307 between_node = LLRB_NEXT(ip_between_points,
1308 &recorder->ip_between_points_head,
1309 between_node))
1311 Node3* edge_pair_node;
1312 TA_Edge before;
1313 TA_Edge after;
1316 before = edges + between_node->before_edge;
1317 after = edges + between_node->after_edge;
1319 *(p++) = HIGH(after->first - segments);
1320 *(p++) = LOW(after->first - segments);
1321 *(p++) = HIGH(before->first - segments);
1322 *(p++) = LOW(before->first - segments);
1324 /* save current position */
1325 edge_pair_node = between_node;
1326 j = 0;
1327 for (;
1328 between_node;
1329 between_node = LLRB_NEXT(ip_between_points,
1330 &recorder->ip_between_points_head,
1331 between_node))
1333 /* count points associated with current edge pair */
1334 if (between_node->before_edge != edge_pair_node->before_edge
1335 || between_node->after_edge != edge_pair_node->after_edge)
1336 break;
1337 j++;
1340 *(p++) = HIGH(j);
1341 *(p++) = LOW(j);
1343 /* restore current position */
1344 between_node = edge_pair_node;
1345 for (;
1346 between_node;
1347 between_node = LLRB_NEXT(ip_between_points,
1348 &recorder->ip_between_points_head,
1349 between_node))
1351 FT_UInt point;
1354 if (between_node->before_edge != edge_pair_node->before_edge
1355 || between_node->after_edge != edge_pair_node->after_edge)
1356 break;
1358 point = TA_adjust_point_index(recorder, between_node->point);
1359 *(p++) = HIGH(point);
1360 *(p++) = LOW(point);
1362 /* keep track of previous node */
1363 edge_pair_node = between_node;
1366 /* reset loop iterator by one element, then continue */
1367 between_node = edge_pair_node;
1371 recorder->hints_record.buf = p;
1375 static FT_Bool
1376 TA_hints_record_is_different(Hints_Record* hints_records,
1377 FT_UInt num_hints_records,
1378 FT_Byte* start,
1379 FT_Byte* end)
1381 Hints_Record last_hints_record;
1384 if (!hints_records)
1385 return 1;
1387 /* we only need to compare with the last hints record */
1388 last_hints_record = hints_records[num_hints_records - 1];
1390 if ((FT_UInt)(end - start) != last_hints_record.buf_len)
1391 return 1;
1393 if (memcmp(start, last_hints_record.buf, last_hints_record.buf_len))
1394 return 1;
1396 return 0;
1400 static FT_Error
1401 TA_add_hints_record(Hints_Record** hints_records,
1402 FT_UInt* num_hints_records,
1403 FT_Byte* start,
1404 Hints_Record hints_record)
1406 Hints_Record* hints_records_new;
1407 FT_UInt buf_len;
1408 /* at this point, `hints_record.buf' still points into `ins_buf' */
1409 FT_Byte* end = hints_record.buf;
1412 buf_len = (FT_UInt)(end - start);
1414 /* now fill the structure completely */
1415 hints_record.buf_len = buf_len;
1416 hints_record.buf = (FT_Byte*)malloc(buf_len);
1417 if (!hints_record.buf)
1418 return FT_Err_Out_Of_Memory;
1420 memcpy(hints_record.buf, start, buf_len);
1422 (*num_hints_records)++;
1423 hints_records_new =
1424 (Hints_Record*)realloc(*hints_records, *num_hints_records
1425 * sizeof (Hints_Record));
1426 if (!hints_records_new)
1428 free(hints_record.buf);
1429 (*num_hints_records)--;
1430 return FT_Err_Out_Of_Memory;
1432 else
1433 *hints_records = hints_records_new;
1435 (*hints_records)[*num_hints_records - 1] = hints_record;
1437 return FT_Err_Ok;
1441 static FT_Byte*
1442 TA_emit_hints_record(Recorder* recorder,
1443 Hints_Record* hints_record,
1444 FT_Byte* bufp,
1445 FT_Bool optimize)
1447 FT_Byte* p;
1448 FT_Byte* endp;
1449 FT_Bool need_words = 0;
1451 FT_UInt i, j;
1452 FT_UInt num_arguments;
1453 FT_UInt num_args;
1456 /* check whether any argument is larger than 0xFF */
1457 endp = hints_record->buf + hints_record->buf_len;
1458 for (p = hints_record->buf; p < endp; p += 2)
1459 if (*p)
1461 need_words = 1;
1462 break;
1465 /* with most fonts it is very rare */
1466 /* that any of the pushed arguments is larger than 0xFF, */
1467 /* thus we refrain from further optimizing this case */
1469 num_arguments = hints_record->buf_len / 2;
1470 p = endp - 2;
1472 if (need_words)
1474 for (i = 0; i < num_arguments; i += 255)
1476 num_args = (num_arguments - i > 255) ? 255 : (num_arguments - i);
1478 if (optimize && num_args <= 8)
1479 BCI(PUSHW_1 - 1 + num_args);
1480 else
1482 BCI(NPUSHW);
1483 BCI(num_args);
1485 for (j = 0; j < num_args; j++)
1487 BCI(*p);
1488 BCI(*(p + 1));
1489 p -= 2;
1493 else
1495 /* we only need the lower bytes */
1496 p++;
1498 for (i = 0; i < num_arguments; i += 255)
1500 num_args = (num_arguments - i > 255) ? 255 : (num_arguments - i);
1502 if (optimize && num_args <= 8)
1503 BCI(PUSHB_1 - 1 + num_args);
1504 else
1506 BCI(NPUSHB);
1507 BCI(num_args);
1509 for (j = 0; j < num_args; j++)
1511 BCI(*p);
1512 p -= 2;
1517 /* collect stack depth data */
1518 if (num_arguments > recorder->num_stack_elements)
1519 recorder->num_stack_elements = num_arguments;
1521 return bufp;
1525 static FT_Byte*
1526 TA_emit_hints_records(Recorder* recorder,
1527 Hints_Record* hints_records,
1528 FT_UInt num_hints_records,
1529 FT_Byte* bufp,
1530 FT_Bool optimize)
1532 FT_UInt i;
1533 Hints_Record* hints_record;
1536 hints_record = hints_records;
1538 /* emit hints records in `if' clauses, */
1539 /* with the ppem size as the condition */
1540 for (i = 0; i < num_hints_records - 1; i++)
1542 BCI(MPPEM);
1543 if (hints_record->size > 0xFF)
1545 BCI(PUSHW_1);
1546 BCI(HIGH((hints_record + 1)->size));
1547 BCI(LOW((hints_record + 1)->size));
1549 else
1551 BCI(PUSHB_1);
1552 BCI((hints_record + 1)->size);
1554 BCI(LT);
1555 BCI(IF);
1556 bufp = TA_emit_hints_record(recorder, hints_record, bufp, optimize);
1557 BCI(ELSE);
1559 hints_record++;
1562 bufp = TA_emit_hints_record(recorder, hints_record, bufp, optimize);
1564 for (i = 0; i < num_hints_records - 1; i++)
1565 BCI(EIF);
1567 return bufp;
1571 static void
1572 TA_free_hints_records(Hints_Record* hints_records,
1573 FT_UInt num_hints_records)
1575 FT_UInt i;
1578 for (i = 0; i < num_hints_records; i++)
1579 free(hints_records[i].buf);
1581 free(hints_records);
1585 static FT_Byte*
1586 TA_hints_recorder_handle_segments(FT_Byte* bufp,
1587 TA_AxisHints axis,
1588 TA_Edge edge,
1589 FT_UShort* wraps)
1591 TA_Segment segments = axis->segments;
1592 TA_Segment seg;
1593 FT_UShort seg_idx;
1594 FT_UShort num_segs = 0;
1595 FT_UShort* wrap;
1598 seg_idx = edge->first - segments;
1600 /* we store everything as 16bit numbers */
1601 *(bufp++) = HIGH(seg_idx);
1602 *(bufp++) = LOW(seg_idx);
1604 /* wrap-around segments are stored as two segments */
1605 if (edge->first->first > edge->first->last)
1606 num_segs++;
1608 seg = edge->first->edge_next;
1609 while (seg != edge->first)
1611 num_segs++;
1613 if (seg->first > seg->last)
1614 num_segs++;
1616 seg = seg->edge_next;
1619 *(bufp++) = HIGH(num_segs);
1620 *(bufp++) = LOW(num_segs);
1622 if (edge->first->first > edge->first->last)
1624 /* emit second part of wrap-around segment; */
1625 /* the bytecode positions such segments after `normal' ones */
1626 wrap = wraps;
1627 for (;;)
1629 if (seg_idx == *wrap)
1630 break;
1631 wrap++;
1634 *(bufp++) = HIGH(axis->num_segments + (wrap - wraps));
1635 *(bufp++) = LOW(axis->num_segments + (wrap - wraps));
1638 seg = edge->first->edge_next;
1639 while (seg != edge->first)
1641 seg_idx = seg - segments;
1643 *(bufp++) = HIGH(seg_idx);
1644 *(bufp++) = LOW(seg_idx);
1646 if (seg->first > seg->last)
1648 wrap = wraps;
1649 for (;;)
1651 if (seg_idx == *wrap)
1652 break;
1653 wrap++;
1656 *(bufp++) = HIGH(axis->num_segments + (wrap - wraps));
1657 *(bufp++) = LOW(axis->num_segments + (wrap - wraps));
1660 seg = seg->edge_next;
1663 return bufp;
1667 static void
1668 TA_hints_recorder(TA_Action action,
1669 TA_GlyphHints hints,
1670 TA_Dimension dim,
1671 void* arg1,
1672 TA_Edge arg2,
1673 TA_Edge arg3,
1674 TA_Edge lower_bound,
1675 TA_Edge upper_bound)
1677 TA_AxisHints axis = &hints->axis[dim];
1678 TA_Edge edges = axis->edges;
1679 TA_Segment segments = axis->segments;
1680 TA_Point points = hints->points;
1682 Recorder* recorder = (Recorder*)hints->user;
1683 SFNT* sfnt = recorder->sfnt;
1684 FONT* font = recorder->font;
1685 FT_UShort* wraps = recorder->wrap_around_segments;
1686 FT_Byte* p = recorder->hints_record.buf;
1688 FT_UInt style = font->loader->metrics->style_class->style;
1691 if (dim == TA_DIMENSION_HORZ)
1692 return;
1694 /* we collect point hints for later processing */
1695 switch (action)
1697 case ta_ip_before:
1699 Node1* before_node;
1700 TA_Point point = (TA_Point)arg1;
1703 before_node = (Node1*)malloc(sizeof (Node1));
1704 if (!before_node)
1705 return;
1706 before_node->point = point - points;
1708 LLRB_INSERT(ip_before_points,
1709 &recorder->ip_before_points_head,
1710 before_node);
1712 return;
1714 case ta_ip_after:
1716 Node1* after_node;
1717 TA_Point point = (TA_Point)arg1;
1720 after_node = (Node1*)malloc(sizeof (Node1));
1721 if (!after_node)
1722 return;
1723 after_node->point = point - points;
1725 LLRB_INSERT(ip_after_points,
1726 &recorder->ip_after_points_head,
1727 after_node);
1729 return;
1731 case ta_ip_on:
1733 Node2* on_node;
1734 TA_Point point = (TA_Point)arg1;
1735 TA_Edge edge = arg2;
1738 on_node = (Node2*)malloc(sizeof (Node2));
1739 if (!on_node)
1740 return;
1741 on_node->edge = edge - edges;
1742 on_node->point = point - points;
1744 LLRB_INSERT(ip_on_points,
1745 &recorder->ip_on_points_head,
1746 on_node);
1748 return;
1750 case ta_ip_between:
1752 Node3* between_node;
1753 TA_Point point = (TA_Point)arg1;
1754 TA_Edge before = arg2;
1755 TA_Edge after = arg3;
1758 between_node = (Node3*)malloc(sizeof (Node3));
1759 if (!between_node)
1760 return;
1761 between_node->before_edge = before - edges;
1762 between_node->after_edge = after - edges;
1763 between_node->point = point - points;
1765 LLRB_INSERT(ip_between_points,
1766 &recorder->ip_between_points_head,
1767 between_node);
1769 return;
1771 case ta_bound:
1772 /* we ignore the BOUND action since we signal this information */
1773 /* with the proper function number */
1774 return;
1776 default:
1777 break;
1780 /* some enum values correspond to four or eight bytecode functions; */
1781 /* if the value is n, the function numbers are n, ..., n+7, */
1782 /* to be differentiated with flags */
1784 switch (action)
1786 case ta_link:
1788 TA_Edge base_edge = (TA_Edge)arg1;
1789 TA_Edge stem_edge = arg2;
1792 *(p++) = 0;
1793 *(p++) = (FT_Byte)action + ACTION_OFFSET
1794 + ((stem_edge->flags & TA_EDGE_SERIF) != 0)
1795 + 2 * ((base_edge->flags & TA_EDGE_ROUND) != 0);
1797 *(p++) = HIGH(base_edge->first - segments);
1798 *(p++) = LOW(base_edge->first - segments);
1799 *(p++) = HIGH(stem_edge->first - segments);
1800 *(p++) = LOW(stem_edge->first - segments);
1802 p = TA_hints_recorder_handle_segments(p, axis, stem_edge, wraps);
1804 break;
1806 case ta_anchor:
1808 TA_Edge edge = (TA_Edge)arg1;
1809 TA_Edge edge2 = arg2;
1812 *(p++) = 0;
1813 *(p++) = (FT_Byte)action + ACTION_OFFSET
1814 + ((edge2->flags & TA_EDGE_SERIF) != 0)
1815 + 2 * ((edge->flags & TA_EDGE_ROUND) != 0);
1817 *(p++) = HIGH(edge->first - segments);
1818 *(p++) = LOW(edge->first - segments);
1819 *(p++) = HIGH(edge2->first - segments);
1820 *(p++) = LOW(edge2->first - segments);
1822 p = TA_hints_recorder_handle_segments(p, axis, edge, wraps);
1824 break;
1826 case ta_adjust:
1828 TA_Edge edge = (TA_Edge)arg1;
1829 TA_Edge edge2 = arg2;
1830 TA_Edge edge_minus_one = lower_bound;
1833 *(p++) = 0;
1834 *(p++) = (FT_Byte)action + ACTION_OFFSET
1835 + ((edge2->flags & TA_EDGE_SERIF) != 0)
1836 + 2 * ((edge->flags & TA_EDGE_ROUND) != 0)
1837 + 4 * (edge_minus_one != NULL);
1839 *(p++) = HIGH(edge->first - segments);
1840 *(p++) = LOW(edge->first - segments);
1841 *(p++) = HIGH(edge2->first - segments);
1842 *(p++) = LOW(edge2->first - segments);
1844 if (edge_minus_one)
1846 *(p++) = HIGH(edge_minus_one->first - segments);
1847 *(p++) = LOW(edge_minus_one->first - segments);
1850 p = TA_hints_recorder_handle_segments(p, axis, edge, wraps);
1852 break;
1854 case ta_blue_anchor:
1856 TA_Edge edge = (TA_Edge)arg1;
1857 TA_Edge blue = arg2;
1860 *(p++) = 0;
1861 *(p++) = (FT_Byte)action + ACTION_OFFSET;
1863 *(p++) = HIGH(blue->first - segments);
1864 *(p++) = LOW(blue->first - segments);
1866 if (edge->best_blue_is_shoot)
1868 *(p++) = HIGH(CVT_BLUE_SHOOTS_OFFSET(style) + edge->best_blue_idx);
1869 *(p++) = LOW(CVT_BLUE_SHOOTS_OFFSET(style) + edge->best_blue_idx);
1871 else
1873 *(p++) = HIGH(CVT_BLUE_REFS_OFFSET(style) + edge->best_blue_idx);
1874 *(p++) = LOW(CVT_BLUE_REFS_OFFSET(style) + edge->best_blue_idx);
1877 *(p++) = HIGH(edge->first - segments);
1878 *(p++) = LOW(edge->first - segments);
1880 p = TA_hints_recorder_handle_segments(p, axis, edge, wraps);
1882 break;
1884 case ta_stem:
1886 TA_Edge edge = (TA_Edge)arg1;
1887 TA_Edge edge2 = arg2;
1888 TA_Edge edge_minus_one = lower_bound;
1891 *(p++) = 0;
1892 *(p++) = (FT_Byte)action + ACTION_OFFSET
1893 + ((edge2->flags & TA_EDGE_SERIF) != 0)
1894 + 2 * ((edge->flags & TA_EDGE_ROUND) != 0)
1895 + 4 * (edge_minus_one != NULL);
1897 *(p++) = HIGH(edge->first - segments);
1898 *(p++) = LOW(edge->first - segments);
1899 *(p++) = HIGH(edge2->first - segments);
1900 *(p++) = LOW(edge2->first - segments);
1902 if (edge_minus_one)
1904 *(p++) = HIGH(edge_minus_one->first - segments);
1905 *(p++) = LOW(edge_minus_one->first - segments);
1908 p = TA_hints_recorder_handle_segments(p, axis, edge, wraps);
1909 p = TA_hints_recorder_handle_segments(p, axis, edge2, wraps);
1911 break;
1913 case ta_blue:
1915 TA_Edge edge = (TA_Edge)arg1;
1918 *(p++) = 0;
1919 *(p++) = (FT_Byte)action + ACTION_OFFSET;
1921 if (edge->best_blue_is_shoot)
1923 *(p++) = HIGH(CVT_BLUE_SHOOTS_OFFSET(style) + edge->best_blue_idx);
1924 *(p++) = LOW(CVT_BLUE_SHOOTS_OFFSET(style) + edge->best_blue_idx);
1926 else
1928 *(p++) = HIGH(CVT_BLUE_REFS_OFFSET(style) + edge->best_blue_idx);
1929 *(p++) = LOW(CVT_BLUE_REFS_OFFSET(style) + edge->best_blue_idx);
1932 *(p++) = HIGH(edge->first - segments);
1933 *(p++) = LOW(edge->first - segments);
1935 p = TA_hints_recorder_handle_segments(p, axis, edge, wraps);
1937 break;
1939 case ta_serif:
1941 TA_Edge serif = (TA_Edge)arg1;
1942 TA_Edge base = serif->serif;
1945 *(p++) = 0;
1946 *(p++) = (FT_Byte)action + ACTION_OFFSET
1947 + (lower_bound != NULL)
1948 + 2 * (upper_bound != NULL);
1950 *(p++) = HIGH(serif->first - segments);
1951 *(p++) = LOW(serif->first - segments);
1952 *(p++) = HIGH(base->first - segments);
1953 *(p++) = LOW(base->first - segments);
1955 if (lower_bound)
1957 *(p++) = HIGH(lower_bound->first - segments);
1958 *(p++) = LOW(lower_bound->first - segments);
1960 if (upper_bound)
1962 *(p++) = HIGH(upper_bound->first - segments);
1963 *(p++) = LOW(upper_bound->first - segments);
1966 p = TA_hints_recorder_handle_segments(p, axis, serif, wraps);
1968 break;
1970 case ta_serif_anchor:
1971 case ta_serif_link2:
1973 TA_Edge edge = (TA_Edge)arg1;
1976 *(p++) = 0;
1977 *(p++) = (FT_Byte)action + ACTION_OFFSET
1978 + (lower_bound != NULL)
1979 + 2 * (upper_bound != NULL);
1981 *(p++) = HIGH(edge->first - segments);
1982 *(p++) = LOW(edge->first - segments);
1984 if (lower_bound)
1986 *(p++) = HIGH(lower_bound->first - segments);
1987 *(p++) = LOW(lower_bound->first - segments);
1989 if (upper_bound)
1991 *(p++) = HIGH(upper_bound->first - segments);
1992 *(p++) = LOW(upper_bound->first - segments);
1995 p = TA_hints_recorder_handle_segments(p, axis, edge, wraps);
1997 break;
1999 case ta_serif_link1:
2001 TA_Edge edge = (TA_Edge)arg1;
2002 TA_Edge before = arg2;
2003 TA_Edge after = arg3;
2006 *(p++) = 0;
2007 *(p++) = (FT_Byte)action + ACTION_OFFSET
2008 + (lower_bound != NULL)
2009 + 2 * (upper_bound != NULL);
2011 *(p++) = HIGH(before->first - segments);
2012 *(p++) = LOW(before->first - segments);
2013 *(p++) = HIGH(edge->first - segments);
2014 *(p++) = LOW(edge->first - segments);
2015 *(p++) = HIGH(after->first - segments);
2016 *(p++) = LOW(after->first - segments);
2018 if (lower_bound)
2020 *(p++) = HIGH(lower_bound->first - segments);
2021 *(p++) = LOW(lower_bound->first - segments);
2023 if (upper_bound)
2025 *(p++) = HIGH(upper_bound->first - segments);
2026 *(p++) = LOW(upper_bound->first - segments);
2029 p = TA_hints_recorder_handle_segments(p, axis, edge, wraps);
2031 break;
2033 default:
2034 /* there are more cases in the enumeration */
2035 /* which are handled with flags */
2036 break;
2039 recorder->hints_record.num_actions++;
2040 recorder->hints_record.buf = p;
2044 static FT_Error
2045 TA_init_recorder(Recorder* recorder,
2046 SFNT* sfnt,
2047 FONT* font,
2048 GLYPH* glyph,
2049 TA_GlyphHints hints)
2051 TA_AxisHints axis = &hints->axis[TA_DIMENSION_VERT];
2052 TA_Point points = hints->points;
2053 TA_Point point_limit = points + hints->num_points;
2054 TA_Point point;
2056 TA_Segment segments = axis->segments;
2057 TA_Segment seg_limit = segments + axis->num_segments;
2058 TA_Segment seg;
2060 FT_UShort num_strong_points = 0;
2061 FT_UShort* wrap_around_segment;
2063 recorder->sfnt = sfnt;
2064 recorder->font = font;
2065 recorder->glyph = glyph;
2066 recorder->num_segments = axis->num_segments;
2068 LLRB_INIT(&recorder->ip_before_points_head);
2069 LLRB_INIT(&recorder->ip_after_points_head);
2070 LLRB_INIT(&recorder->ip_on_points_head);
2071 LLRB_INIT(&recorder->ip_between_points_head);
2073 recorder->num_stack_elements = 0;
2075 /* no need to clean up allocated arrays in case of error; */
2076 /* this is handled later by `TA_free_recorder' */
2078 recorder->num_wrap_around_segments = 0;
2079 for (seg = segments; seg < seg_limit; seg++)
2080 if (seg->first > seg->last)
2081 recorder->num_wrap_around_segments++;
2083 recorder->wrap_around_segments =
2084 (FT_UShort*)malloc(recorder->num_wrap_around_segments
2085 * sizeof (FT_UShort));
2086 if (!recorder->wrap_around_segments)
2087 return FT_Err_Out_Of_Memory;
2089 wrap_around_segment = recorder->wrap_around_segments;
2090 for (seg = segments; seg < seg_limit; seg++)
2091 if (seg->first > seg->last)
2092 *(wrap_around_segment++) = seg - segments;
2094 /* get number of strong points */
2095 for (point = points; point < point_limit; point++)
2097 /* actually, we need to test `TA_FLAG_TOUCH_Y' also; */
2098 /* however, this value isn't known yet */
2099 /* (or rather, it can vary between different pixel sizes) */
2100 if (point->flags & TA_FLAG_WEAK_INTERPOLATION)
2101 continue;
2103 num_strong_points++;
2106 recorder->num_strong_points = num_strong_points;
2108 return FT_Err_Ok;
2112 static void
2113 TA_reset_recorder(Recorder* recorder,
2114 FT_Byte* bufp)
2116 recorder->hints_record.buf = bufp;
2117 recorder->hints_record.num_actions = 0;
2121 static void
2122 TA_rewind_recorder(Recorder* recorder,
2123 FT_Byte* bufp,
2124 FT_UInt size)
2126 Node1* before_node;
2127 Node1* after_node;
2128 Node2* on_node;
2129 Node3* between_node;
2131 Node1* next_before_node;
2132 Node1* next_after_node;
2133 Node2* next_on_node;
2134 Node3* next_between_node;
2137 TA_reset_recorder(recorder, bufp);
2139 recorder->hints_record.size = size;
2141 /* deallocate our red-black trees */
2143 for (before_node = LLRB_MIN(ip_before_points,
2144 &recorder->ip_before_points_head);
2145 before_node;
2146 before_node = next_before_node)
2148 next_before_node = LLRB_NEXT(ip_before_points,
2149 &recorder->ip_before_points_head,
2150 before_node);
2151 LLRB_REMOVE(ip_before_points,
2152 &recorder->ip_before_points_head,
2153 before_node);
2154 free(before_node);
2157 for (after_node = LLRB_MIN(ip_after_points,
2158 &recorder->ip_after_points_head);
2159 after_node;
2160 after_node = next_after_node)
2162 next_after_node = LLRB_NEXT(ip_after_points,
2163 &recorder->ip_after_points_head,
2164 after_node);
2165 LLRB_REMOVE(ip_after_points,
2166 &recorder->ip_after_points_head,
2167 after_node);
2168 free(after_node);
2171 for (on_node = LLRB_MIN(ip_on_points,
2172 &recorder->ip_on_points_head);
2173 on_node;
2174 on_node = next_on_node)
2176 next_on_node = LLRB_NEXT(ip_on_points,
2177 &recorder->ip_on_points_head,
2178 on_node);
2179 LLRB_REMOVE(ip_on_points,
2180 &recorder->ip_on_points_head,
2181 on_node);
2182 free(on_node);
2185 for (between_node = LLRB_MIN(ip_between_points,
2186 &recorder->ip_between_points_head);
2187 between_node;
2188 between_node = next_between_node)
2190 next_between_node = LLRB_NEXT(ip_between_points,
2191 &recorder->ip_between_points_head,
2192 between_node);
2193 LLRB_REMOVE(ip_between_points,
2194 &recorder->ip_between_points_head,
2195 between_node);
2196 free(between_node);
2201 static void
2202 TA_free_recorder(Recorder* recorder)
2204 free(recorder->wrap_around_segments);
2206 TA_rewind_recorder(recorder, NULL, 0);
2210 FT_Error
2211 TA_sfnt_build_glyph_instructions(SFNT* sfnt,
2212 FONT* font,
2213 FT_Long idx)
2215 FT_Face face = sfnt->face;
2216 FT_Error error;
2218 FT_Byte* ins_buf;
2219 FT_UInt ins_len;
2220 FT_Byte* bufp;
2221 FT_Byte* p;
2223 SFNT_Table* glyf_table = &font->tables[sfnt->glyf_idx];
2224 glyf_Data* data = (glyf_Data*)glyf_table->data;
2225 /* `idx' is never negative */
2226 GLYPH* glyph = &data->glyphs[idx];
2228 TA_GlyphHints hints;
2230 FT_UInt num_action_hints_records = 0;
2231 FT_UInt num_point_hints_records = 0;
2232 Hints_Record* action_hints_records = NULL;
2233 Hints_Record* point_hints_records = NULL;
2235 Recorder recorder;
2236 FT_UShort num_stack_elements;
2237 FT_Bool optimize = 0;
2239 FT_Int32 load_flags;
2240 FT_UInt size;
2242 FT_Byte* pos[3];
2244 #ifdef TA_DEBUG
2245 int _ta_debug_save;
2246 #endif
2249 /* XXX: right now, we abuse this flag to control */
2250 /* the global behaviour of the auto-hinter */
2251 load_flags = 1 << 29; /* vertical hinting only */
2252 if (!font->adjust_subglyphs)
2254 if (font->hint_composites)
2255 load_flags |= FT_LOAD_NO_SCALE;
2256 else
2257 load_flags |= FT_LOAD_NO_RECURSE;
2260 /* computing the segments is resolution independent, */
2261 /* thus the pixel size in this call is arbitrary -- */
2262 /* however, we avoid unnecessary debugging output */
2263 /* if we use the lowest value of the hinting range */
2264 error = FT_Set_Pixel_Sizes(face,
2265 font->hinting_range_min,
2266 font->hinting_range_min);
2267 if (error)
2268 return error;
2270 #ifdef TA_DEBUG
2271 /* temporarily disable some debugging output */
2272 /* to avoid getting the information twice */
2273 _ta_debug_save = _ta_debug;
2274 _ta_debug = 0;
2275 #endif
2277 ta_loader_register_hints_recorder(font->loader, NULL, NULL);
2278 error = ta_loader_load_glyph(font, face, (FT_UInt)idx, load_flags);
2280 #ifdef TA_DEBUG
2281 _ta_debug = _ta_debug_save;
2282 #endif
2284 if (error)
2285 return error;
2287 /* do nothing if we have an empty glyph */
2288 if (!face->glyph->outline.n_contours)
2289 return FT_Err_Ok;
2291 hints = &font->loader->hints;
2293 /* do nothing if the setup delivered the `none_dflt' style only */
2294 if (!hints->num_points)
2295 return FT_Err_Ok;
2298 * We allocate a buffer which is certainly large enough
2299 * to hold all of the created bytecode instructions;
2300 * later on it gets reallocated to its real size.
2302 * The value `1000' is a very rough guess, not tested well.
2304 * For delta exceptions, we have three DELTA commands,
2305 * covering 3*16 ppem values.
2306 * Since a point index can be larger than 255,
2307 * we assume two bytes everywhere for the necessary PUSH calls.
2308 * This value must be doubled for the other arguments of DELTA.
2309 * Additionally, we have both x and y deltas,
2310 * which need to be handled separately in the bytecode.
2311 * In summary, this is approx. 3*16 * 2*2 * 2 = 400 bytes per point,
2312 * adding some bytes for the necessary overhead.
2314 ins_len = hints->num_points
2315 * (1000 + ((font->control_data_head != NULL) ? 400 : 0));
2316 ins_buf = (FT_Byte*)malloc(ins_len);
2317 if (!ins_buf)
2318 return FT_Err_Out_Of_Memory;
2320 /* handle composite glyph */
2321 if (font->loader->gloader->base.num_subglyphs)
2323 bufp = TA_font_build_subglyph_shifter(font, ins_buf);
2324 if (!bufp)
2326 error = FT_Err_Out_Of_Memory;
2327 goto Err;
2330 goto Done1;
2333 /* only scale the glyph if the `none_dflt' style has been used */
2334 if (font->loader->metrics->style_class == &ta_none_dflt_style_class)
2336 /* since `TA_init_recorder' hasn't been called yet, */
2337 /* we manually initialize the `sfnt', `font', and `glyph' fields */
2338 recorder.sfnt = sfnt;
2339 recorder.font = font;
2340 recorder.glyph = glyph;
2342 bufp = TA_sfnt_build_glyph_scaler(sfnt, &recorder, ins_buf);
2343 if (!bufp)
2345 error = FT_Err_Out_Of_Memory;
2346 goto Err;
2349 goto Done1;
2352 error = TA_init_recorder(&recorder, sfnt, font, glyph, hints);
2353 if (error)
2354 goto Err;
2356 /* loop over a large range of pixel sizes */
2357 /* to find hints records which get pushed onto the bytecode stack */
2359 #ifdef DEBUGGING
2360 if (font->debug)
2362 int num_chars, i;
2363 char buf[256];
2366 (void)FT_Get_Glyph_Name(face, idx, buf, 256);
2368 num_chars = fprintf(stderr, "glyph %ld", idx);
2369 if (*buf)
2370 num_chars += fprintf(stderr, " (%s)", buf);
2371 fprintf(stderr, "\n");
2372 for (i = 0; i < num_chars; i++)
2373 putc('=', stderr);
2374 fprintf(stderr, "\n\n");
2377 #endif
2379 /* we temporarily use `ins_buf' to record the current glyph hints */
2380 ta_loader_register_hints_recorder(font->loader,
2381 TA_hints_recorder,
2382 (void*)&recorder);
2384 for (size = font->hinting_range_min;
2385 size <= font->hinting_range_max;
2386 size++)
2388 #ifdef DEBUGGING
2389 int have_dumps = 0;
2390 #endif
2393 TA_rewind_recorder(&recorder, ins_buf, size);
2395 error = FT_Set_Pixel_Sizes(face, size, size);
2396 if (error)
2397 goto Err;
2399 #ifdef DEBUGGING
2400 if (font->debug)
2402 int num_chars, i;
2405 num_chars = fprintf(stderr, "size %d\n", size);
2406 for (i = 0; i < num_chars - 1; i++)
2407 putc('-', stderr);
2408 fprintf(stderr, "\n\n");
2410 #endif
2412 /* calling `ta_loader_load_glyph' uses the */
2413 /* `TA_hints_recorder' function as a callback, */
2414 /* modifying `hints_record' */
2415 error = ta_loader_load_glyph(font, face, idx, load_flags);
2416 if (error)
2417 goto Err;
2419 if (TA_hints_record_is_different(action_hints_records,
2420 num_action_hints_records,
2421 ins_buf, recorder.hints_record.buf))
2423 #ifdef DEBUGGING
2424 if (font->debug)
2426 have_dumps = 1;
2428 ta_glyph_hints_dump_edges((TA_GlyphHints)_ta_debug_hints);
2429 ta_glyph_hints_dump_segments((TA_GlyphHints)_ta_debug_hints);
2430 ta_glyph_hints_dump_points((TA_GlyphHints)_ta_debug_hints);
2432 fprintf(stderr, "action hints record:\n");
2433 if (ins_buf == recorder.hints_record.buf)
2434 fprintf(stderr, " (none)");
2435 else
2437 fprintf(stderr, " ");
2438 for (p = ins_buf; p < recorder.hints_record.buf; p += 2)
2439 fprintf(stderr, " %2d", *p * 256 + *(p + 1));
2441 fprintf(stderr, "\n");
2443 #endif
2445 error = TA_add_hints_record(&action_hints_records,
2446 &num_action_hints_records,
2447 ins_buf, recorder.hints_record);
2448 if (error)
2449 goto Err;
2452 /* now handle point records */
2454 TA_reset_recorder(&recorder, ins_buf);
2456 /* use the point hints data collected in `TA_hints_recorder' */
2457 TA_build_point_hints(&recorder, hints);
2459 if (TA_hints_record_is_different(point_hints_records,
2460 num_point_hints_records,
2461 ins_buf, recorder.hints_record.buf))
2463 #ifdef DEBUGGING
2464 if (font->debug)
2466 if (!have_dumps)
2468 int num_chars, i;
2471 num_chars = fprintf(stderr, "size %d\n", size);
2472 for (i = 0; i < num_chars - 1; i++)
2473 putc('-', stderr);
2474 fprintf(stderr, "\n\n");
2476 ta_glyph_hints_dump_edges((TA_GlyphHints)_ta_debug_hints);
2477 ta_glyph_hints_dump_segments((TA_GlyphHints)_ta_debug_hints);
2478 ta_glyph_hints_dump_points((TA_GlyphHints)_ta_debug_hints);
2481 fprintf(stderr, "point hints record:\n");
2482 if (ins_buf == recorder.hints_record.buf)
2483 fprintf(stderr, " (none)");
2484 else
2486 fprintf(stderr, " ");
2487 for (p = ins_buf; p < recorder.hints_record.buf; p += 2)
2488 fprintf(stderr, " %2d", *p * 256 + *(p + 1));
2490 fprintf(stderr, "\n\n");
2492 #endif
2494 error = TA_add_hints_record(&point_hints_records,
2495 &num_point_hints_records,
2496 ins_buf, recorder.hints_record);
2497 if (error)
2498 goto Err;
2502 if (num_action_hints_records == 1 && !action_hints_records[0].num_actions)
2504 /* since we only have a single empty record we just scale the glyph */
2505 bufp = TA_sfnt_build_glyph_scaler(sfnt, &recorder, ins_buf);
2506 if (!bufp)
2508 error = FT_Err_Out_Of_Memory;
2509 goto Err;
2512 goto Done;
2515 /* if there is only a single record, */
2516 /* we do a global optimization later on */
2517 if (num_action_hints_records > 1)
2518 optimize = 1;
2520 /* store the hints records and handle stack depth */
2521 pos[0] = ins_buf;
2522 bufp = TA_emit_hints_records(&recorder,
2523 point_hints_records,
2524 num_point_hints_records,
2525 ins_buf,
2526 optimize);
2528 num_stack_elements = recorder.num_stack_elements;
2529 recorder.num_stack_elements = 0;
2531 pos[1] = bufp;
2532 bufp = TA_emit_hints_records(&recorder,
2533 action_hints_records,
2534 num_action_hints_records,
2535 bufp,
2536 optimize);
2538 recorder.num_stack_elements += num_stack_elements;
2540 pos[2] = bufp;
2541 bufp = TA_sfnt_build_glyph_segments(sfnt, &recorder, bufp, optimize);
2542 if (!bufp)
2544 error = FT_Err_Out_Of_Memory;
2545 goto Err;
2548 if (num_action_hints_records == 1)
2549 bufp = TA_optimize_push(ins_buf, pos);
2551 Done:
2552 TA_free_hints_records(action_hints_records, num_action_hints_records);
2553 TA_free_hints_records(point_hints_records, num_point_hints_records);
2554 TA_free_recorder(&recorder);
2556 Done1:
2557 /* handle delta exceptions */
2558 if (font->control_data_head)
2560 bufp = TA_sfnt_build_delta_exceptions(sfnt, font, idx, bufp);
2561 if (!bufp)
2563 error = FT_Err_Out_Of_Memory;
2564 goto Err;
2568 ins_len = bufp - ins_buf;
2570 if (ins_len > sfnt->max_instructions)
2571 sfnt->max_instructions = ins_len;
2573 glyph->ins_buf = (FT_Byte*)realloc(ins_buf, ins_len);
2574 glyph->ins_len = ins_len;
2576 return FT_Err_Ok;
2578 Err:
2579 TA_free_hints_records(action_hints_records, num_action_hints_records);
2580 TA_free_hints_records(point_hints_records, num_point_hints_records);
2581 TA_free_recorder(&recorder);
2582 free(ins_buf);
2584 return error;
2588 /* end of tabytecode.c */