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.
18 #include <stdbool.h> /* for llrb.h */
20 #include "llrb.h" /* a red-black tree implementation */
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
;
37 /* node structures for point hints */
39 typedef struct Node1 Node1
;
42 LLRB_ENTRY(Node1
) entry1
;
46 typedef struct Node2 Node2
;
49 LLRB_ENTRY(Node2
) entry2
;
54 typedef struct Node3 Node3
;
57 LLRB_ENTRY(Node3
) entry3
;
58 FT_UShort before_edge
;
64 /* comparison functions for our red-black trees */
71 return e1
->point
- e2
->point
;
81 /* sort by edges ... */
82 delta
= (FT_Int
)e1
->edge
- (FT_Int
)e2
->edge
;
86 /* ... then by points */
87 return (FT_Int
)e1
->point
- (FT_Int
)e2
->point
;
97 /* sort by `before' edges ... */
98 delta
= (FT_Int
)e1
->before_edge
- (FT_Int
)e2
->before_edge
;
102 /* ... then by `after' edges ... */
103 delta
= (FT_Int
)e1
->after_edge
- (FT_Int
)e2
->after_edge
;
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_
138 typedef struct Recorder_
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
;
166 /* this is the bytecode of the `.ttfautohint' glyph */
168 FT_Byte ttfautohint_glyph_bytecode
[7] =
171 /* increment `cvtl_is_subglyph' counter */
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
191 TA_build_push(FT_Byte
* bufp
,
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
);
214 for (j
= 0; j
< nargs
; j
++)
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
);
235 for (j
= 0; j
< nargs
; j
++)
248 * We optimize two common cases, replacing
250 * NPUSHB A ... NPUSHB B [... NPUSHB C] ... CALL
254 * NPUSHB (A+B[+C]) ... CALL
260 TA_optimize_push(FT_Byte
* buf
,
275 /* XXX improve handling of NPUSHW */
276 if (*(pos
[0]) == NPUSHW
277 || *(pos
[1]) == NPUSHW
278 || *(pos
[2]) == NPUSHW
)
281 /* the point hints records block can be missing */
282 if (pos
[0] == pos
[1])
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];
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 */
303 /* reduce three NPUSHB to two */
305 new_size2
= sum
- 0xFF;
309 /* reduce two or three NPUSHB to one */
320 BCI(PUSHB_1
- 1 + new_size1
);
326 for (i
= 0; i
< new_size1
; i
++)
328 if (p
== pos
[pos_idx
])
331 p
+= 2; /* skip old NPUSHB */
339 BCI(PUSHB_1
- 1 + new_size2
);
345 for (i
= 0; i
< new_size2
; i
++)
347 if (p
== pos
[pos_idx
])
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. */
372 TA_adjust_point_index(Recorder
* recorder
,
375 FONT
* font
= recorder
->font
;
376 GLYPH
* glyph
= recorder
->glyph
;
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
])
391 /* we store the segments in the storage area; */
392 /* each segment record consists of the first and last point */
395 TA_sfnt_build_glyph_segments(SFNT
* sfnt
,
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
;
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
;
419 FT_UShort num_segments
;
421 FT_Bool need_words
= 0;
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 */
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)
450 if (first
> last
|| last
- first
>= 16)
452 if (num_packed_segments
== 9)
454 num_packed_segments
++;
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
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
));
475 arg
= args
+ num_args
- 1;
477 if (num_segments
> 0xFF)
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
;
484 *(arg
--) = bci_create_segments_0
+ num_packed_segments
;
486 *(arg
--) = CVT_SCALING_VALUE_OFFSET(style_id
);
487 *(arg
--) = num_segments
;
490 for (seg
= segments
; seg
< segments
+ num_packed_segments
; seg
++)
492 FT_UInt first
= seg
->first
- points
;
493 FT_UInt last
= seg
->last
- points
;
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
;
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 */
522 for (n
= 0; n
< outline
.n_contours
; n
++)
524 FT_UInt end
= (FT_UInt
)outline
.contours
[n
];
529 *(arg
--) = TA_adjust_point_index(recorder
, end
);
534 *(arg
--) = TA_adjust_point_index(recorder
, 0);
536 *(arg
--) = TA_adjust_point_index(recorder
,
537 (FT_UInt
)outline
.contours
[n
- 1] + 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
;
557 for (n
= 0; n
< outline
.n_contours
; n
++)
559 if (first
<= (FT_UInt
)outline
.contours
[n
])
562 *(arg
--) = TA_adjust_point_index(recorder
, 0);
564 *(arg
--) = TA_adjust_point_index(recorder
,
565 (FT_UInt
)outline
.contours
[n
- 1] + 1);
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
);
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
;
603 build_delta_exception(const Ctrl
* ctrl
,
604 FT_UInt
** delta_args
,
613 ppem
= ctrl
->ppem
- CONTROL_DELTA_PPEM_MIN
;
629 * the possible shift values in the instructions are indexed as follows:
640 * (note that there is no index for a zero shift).
643 if (ctrl
->x_shift
< 0)
644 x_shift
= ctrl
->x_shift
+ 8;
646 x_shift
= ctrl
->x_shift
+ 7;
648 if (ctrl
->y_shift
< 0)
649 y_shift
= ctrl
->y_shift
+ 8;
651 y_shift
= ctrl
->y_shift
+ 7;
653 /* add point index and exception specification to appropriate stack */
656 *(delta_args
[offset
] + num_delta_args
[offset
]++) =
657 (ppem
<< 4) + x_shift
;
658 *(delta_args
[offset
] + num_delta_args
[offset
]++) =
665 *(delta_args
[offset
] + num_delta_args
[offset
]++) =
666 (ppem
<< 4) + y_shift
;
667 *(delta_args
[offset
] + num_delta_args
[offset
]++) =
674 TA_sfnt_build_delta_exceptions(SFNT
* sfnt
,
679 FT_Face face
= font
->loader
->face
;
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;
698 num_points
= font
->loader
->gloader
->base
.outline
.n_points
;
700 /* loop over all fitting control instructions */
703 ctrl
= TA_control_get_ctrl(font
);
709 if (!(ctrl
->type
== Control_Delta_before_IUP
710 || ctrl
->type
== Control_Delta_after_IUP
))
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
)
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)
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)
747 TA_control_get_next(font
);
750 /* nothing to do if no control instructions */
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;
763 need_word_counts
= 1;
765 *(delta_args
[i
] + num_delta_args
[i
]) = n
;
770 /* merge delta stacks into a single one */
772 || (!need_words
&& !need_word_counts
))
774 FT_UInt num_args
= 0;
777 for (i
= 0; i
< 6; i
++)
780 FT_UInt num_args_new
;
783 if (!num_delta_args
[i
])
786 num_args_new
= num_args
+ num_delta_args
[i
];
787 args_new
= (FT_UInt
*)realloc(args
, num_args_new
* sizeof (FT_UInt
));
794 memcpy(args_new
+ num_args
,
796 num_delta_args
[i
] * sizeof (FT_UInt
));
799 num_args
= num_args_new
;
802 num_stack_elements
= num_args
;
804 bufp
= TA_build_push(bufp
, args
, num_args
, need_words
, 1);
808 num_stack_elements
= 0;
810 /* stack elements are bytes, but counts need words */
811 for (i
= 0; i
< 6; i
++)
816 if (!num_delta_args
[i
])
819 num_delta_arg
= num_delta_args
[i
] - 1;
821 bufp
= TA_build_push(bufp
,
827 num_stack_elements
+= num_delta_arg
+ 1;
831 BCI(HIGH(num_delta_arg
));
832 BCI(LOW(num_delta_arg
));
836 /* emit the DELTA opcodes */
837 if (num_delta_args
[5])
839 if (num_delta_args
[4])
841 if (num_delta_args
[3])
844 if (num_delta_args
[2] || num_delta_args
[1] || num_delta_args
[0])
847 if (num_delta_args
[2])
849 if (num_delta_args
[1])
851 if (num_delta_args
[0])
855 for (i
= 0; i
< 6; i
++)
859 if (num_stack_elements
> sfnt
->max_stack_elements
)
860 sfnt
->max_stack_elements
= num_stack_elements
;
866 TA_sfnt_build_glyph_scaler(SFNT
* sfnt
,
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
;
879 FT_Bool need_words
= 0;
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
));
895 arg
= args
+ num_args
- 1;
900 if (recorder
->glyph
->num_components
&& font
->hint_composites
)
901 *(arg
--) = bci_scale_composite_glyph
;
903 *(arg
--) = bci_scale_glyph
;
904 *(arg
--) = num_contours
;
909 for (p
= 0; p
< num_contours
; p
++)
915 end
= glyph
->outline
.contours
[p
];
917 for (q
= start
; q
<= end
; q
++)
919 if (points
[q
].y
< points
[min
].y
)
921 if (points
[q
].y
> points
[max
].y
)
927 *(arg
--) = TA_adjust_point_index(recorder
, max
);
928 *(arg
--) = TA_adjust_point_index(recorder
, min
);
932 *(arg
--) = TA_adjust_point_index(recorder
, min
);
933 *(arg
--) = TA_adjust_point_index(recorder
, max
);
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);
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
;
964 TA_font_build_subglyph_shifter(FONT
* font
,
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
++)
983 FT_UShort flags
= subglyph
->flags
;
984 FT_Pos y_offset
= subglyph
->arg2
;
989 /* load subglyph to get the number of contours */
990 error
= FT_Load_Glyph(face
, subglyph
->index
, FT_LOAD_NO_SCALE
);
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
))
999 /* nothing to do if y offset is zero */
1003 /* nothing to do if there are no contours */
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)
1014 BCI(HIGH(curr_contour
));
1015 BCI(LOW(curr_contour
));
1016 BCI(HIGH(num_contours
));
1017 BCI(LOW(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)
1031 BCI(HIGH(y_offset
));
1041 BCI(bci_shift_subglyph
);
1045 curr_contour
+= num_contours
;
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.
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
;
1078 FT_UShort prev_edge
;
1079 FT_UShort prev_before_edge
;
1080 FT_UShort prev_after_edge
;
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 */
1095 for (before_node
= LLRB_MIN(ip_before_points
,
1096 &recorder
->ip_before_points_head
);
1098 before_node
= LLRB_NEXT(ip_before_points
,
1099 &recorder
->ip_before_points_head
,
1111 recorder
->hints_record
.num_actions
++;
1116 *(p
++) = (FT_Byte
)ta_ip_before
+ ACTION_OFFSET
;
1117 *(p
++) = HIGH(edge
->first
- segments
);
1118 *(p
++) = LOW(edge
->first
- segments
);
1122 for (before_node
= LLRB_MIN(ip_before_points
,
1123 &recorder
->ip_before_points_head
);
1125 before_node
= LLRB_NEXT(ip_before_points
,
1126 &recorder
->ip_before_points_head
,
1132 point
= TA_adjust_point_index(recorder
, before_node
->point
);
1133 *(p
++) = HIGH(point
);
1134 *(p
++) = LOW(point
);
1138 /* ip_after_points */
1141 for (after_node
= LLRB_MIN(ip_after_points
,
1142 &recorder
->ip_after_points_head
);
1144 after_node
= LLRB_NEXT(ip_after_points
,
1145 &recorder
->ip_after_points_head
,
1157 recorder
->hints_record
.num_actions
++;
1159 edge
= edges
+ axis
->num_edges
- 1;
1162 *(p
++) = (FT_Byte
)ta_ip_after
+ ACTION_OFFSET
;
1163 *(p
++) = HIGH(edge
->first
- segments
);
1164 *(p
++) = LOW(edge
->first
- segments
);
1168 for (after_node
= LLRB_MIN(ip_after_points
,
1169 &recorder
->ip_after_points_head
);
1171 after_node
= LLRB_NEXT(ip_after_points
,
1172 &recorder
->ip_after_points_head
,
1178 point
= TA_adjust_point_index(recorder
, after_node
->point
);
1179 *(p
++) = HIGH(point
);
1180 *(p
++) = LOW(point
);
1184 /* ip_on_point_array */
1188 for (on_node
= LLRB_MIN(ip_on_points
,
1189 &recorder
->ip_on_points_head
);
1191 on_node
= LLRB_NEXT(ip_on_points
,
1192 &recorder
->ip_on_points_head
,
1196 if (on_node
->edge
!= prev_edge
)
1199 prev_edge
= on_node
->edge
;
1205 recorder
->hints_record
.num_actions
++;
1208 *(p
++) = (FT_Byte
)ta_ip_on
+ ACTION_OFFSET
;
1212 for (on_node
= LLRB_MIN(ip_on_points
,
1213 &recorder
->ip_on_points_head
);
1215 on_node
= LLRB_NEXT(ip_on_points
,
1216 &recorder
->ip_on_points_head
,
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
;
1233 on_node
= LLRB_NEXT(ip_on_points
,
1234 &recorder
->ip_on_points_head
,
1237 /* count points on current edge */
1238 if (on_node
->edge
!= edge_node
->edge
)
1246 /* restore current position */
1247 on_node
= edge_node
;
1250 on_node
= LLRB_NEXT(ip_on_points
,
1251 &recorder
->ip_on_points_head
,
1257 if (on_node
->edge
!= edge_node
->edge
)
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;
1278 for (between_node
= LLRB_MIN(ip_between_points
,
1279 &recorder
->ip_between_points_head
);
1281 between_node
= LLRB_NEXT(ip_between_points
,
1282 &recorder
->ip_between_points_head
,
1285 /* count `(before,after)' edge pairs */
1286 if (between_node
->before_edge
!= prev_before_edge
1287 || between_node
->after_edge
!= prev_after_edge
)
1290 prev_before_edge
= between_node
->before_edge
;
1291 prev_after_edge
= between_node
->after_edge
;
1297 recorder
->hints_record
.num_actions
++;
1300 *(p
++) = (FT_Byte
)ta_ip_between
+ ACTION_OFFSET
;
1304 for (between_node
= LLRB_MIN(ip_between_points
,
1305 &recorder
->ip_between_points_head
);
1307 between_node
= LLRB_NEXT(ip_between_points
,
1308 &recorder
->ip_between_points_head
,
1311 Node3
* edge_pair_node
;
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
;
1329 between_node
= LLRB_NEXT(ip_between_points
,
1330 &recorder
->ip_between_points_head
,
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
)
1343 /* restore current position */
1344 between_node
= edge_pair_node
;
1347 between_node
= LLRB_NEXT(ip_between_points
,
1348 &recorder
->ip_between_points_head
,
1354 if (between_node
->before_edge
!= edge_pair_node
->before_edge
1355 || between_node
->after_edge
!= edge_pair_node
->after_edge
)
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
;
1376 TA_hints_record_is_different(Hints_Record
* hints_records
,
1377 FT_UInt num_hints_records
,
1381 Hints_Record last_hints_record
;
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
)
1393 if (memcmp(start
, last_hints_record
.buf
, last_hints_record
.buf_len
))
1401 TA_add_hints_record(Hints_Record
** hints_records
,
1402 FT_UInt
* num_hints_records
,
1404 Hints_Record hints_record
)
1406 Hints_Record
* hints_records_new
;
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
)++;
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
;
1433 *hints_records
= hints_records_new
;
1435 (*hints_records
)[*num_hints_records
- 1] = hints_record
;
1442 TA_emit_hints_record(Recorder
* recorder
,
1443 Hints_Record
* hints_record
,
1449 FT_Bool need_words
= 0;
1452 FT_UInt num_arguments
;
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)
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;
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
);
1485 for (j
= 0; j
< num_args
; j
++)
1495 /* we only need the lower bytes */
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
);
1509 for (j
= 0; j
< num_args
; j
++)
1517 /* collect stack depth data */
1518 if (num_arguments
> recorder
->num_stack_elements
)
1519 recorder
->num_stack_elements
= num_arguments
;
1526 TA_emit_hints_records(Recorder
* recorder
,
1527 Hints_Record
* hints_records
,
1528 FT_UInt num_hints_records
,
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
++)
1543 if (hints_record
->size
> 0xFF)
1546 BCI(HIGH((hints_record
+ 1)->size
));
1547 BCI(LOW((hints_record
+ 1)->size
));
1552 BCI((hints_record
+ 1)->size
);
1556 bufp
= TA_emit_hints_record(recorder
, hints_record
, bufp
, optimize
);
1562 bufp
= TA_emit_hints_record(recorder
, hints_record
, bufp
, optimize
);
1564 for (i
= 0; i
< num_hints_records
- 1; i
++)
1572 TA_free_hints_records(Hints_Record
* hints_records
,
1573 FT_UInt num_hints_records
)
1578 for (i
= 0; i
< num_hints_records
; i
++)
1579 free(hints_records
[i
].buf
);
1581 free(hints_records
);
1586 TA_hints_recorder_handle_segments(FT_Byte
* bufp
,
1591 TA_Segment segments
= axis
->segments
;
1594 FT_UShort num_segs
= 0;
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
)
1608 seg
= edge
->first
->edge_next
;
1609 while (seg
!= edge
->first
)
1613 if (seg
->first
> seg
->last
)
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 */
1629 if (seg_idx
== *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
)
1651 if (seg_idx
== *wrap
)
1656 *(bufp
++) = HIGH(axis
->num_segments
+ (wrap
- wraps
));
1657 *(bufp
++) = LOW(axis
->num_segments
+ (wrap
- wraps
));
1660 seg
= seg
->edge_next
;
1668 TA_hints_recorder(TA_Action action
,
1669 TA_GlyphHints hints
,
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
)
1694 /* we collect point hints for later processing */
1700 TA_Point point
= (TA_Point
)arg1
;
1703 before_node
= (Node1
*)malloc(sizeof (Node1
));
1706 before_node
->point
= point
- points
;
1708 LLRB_INSERT(ip_before_points
,
1709 &recorder
->ip_before_points_head
,
1717 TA_Point point
= (TA_Point
)arg1
;
1720 after_node
= (Node1
*)malloc(sizeof (Node1
));
1723 after_node
->point
= point
- points
;
1725 LLRB_INSERT(ip_after_points
,
1726 &recorder
->ip_after_points_head
,
1734 TA_Point point
= (TA_Point
)arg1
;
1735 TA_Edge edge
= arg2
;
1738 on_node
= (Node2
*)malloc(sizeof (Node2
));
1741 on_node
->edge
= edge
- edges
;
1742 on_node
->point
= point
- points
;
1744 LLRB_INSERT(ip_on_points
,
1745 &recorder
->ip_on_points_head
,
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
));
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
,
1772 /* we ignore the BOUND action since we signal this information */
1773 /* with the proper function number */
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 */
1788 TA_Edge base_edge
= (TA_Edge
)arg1
;
1789 TA_Edge stem_edge
= arg2
;
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
);
1808 TA_Edge edge
= (TA_Edge
)arg1
;
1809 TA_Edge edge2
= arg2
;
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
);
1828 TA_Edge edge
= (TA_Edge
)arg1
;
1829 TA_Edge edge2
= arg2
;
1830 TA_Edge edge_minus_one
= lower_bound
;
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
);
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
);
1854 case ta_blue_anchor
:
1856 TA_Edge edge
= (TA_Edge
)arg1
;
1857 TA_Edge blue
= arg2
;
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
);
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
);
1886 TA_Edge edge
= (TA_Edge
)arg1
;
1887 TA_Edge edge2
= arg2
;
1888 TA_Edge edge_minus_one
= lower_bound
;
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
);
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
);
1915 TA_Edge edge
= (TA_Edge
)arg1
;
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
);
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
);
1941 TA_Edge serif
= (TA_Edge
)arg1
;
1942 TA_Edge base
= serif
->serif
;
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
);
1957 *(p
++) = HIGH(lower_bound
->first
- segments
);
1958 *(p
++) = LOW(lower_bound
->first
- segments
);
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
);
1970 case ta_serif_anchor
:
1971 case ta_serif_link2
:
1973 TA_Edge edge
= (TA_Edge
)arg1
;
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
);
1986 *(p
++) = HIGH(lower_bound
->first
- segments
);
1987 *(p
++) = LOW(lower_bound
->first
- segments
);
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
);
1999 case ta_serif_link1
:
2001 TA_Edge edge
= (TA_Edge
)arg1
;
2002 TA_Edge before
= arg2
;
2003 TA_Edge after
= arg3
;
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
);
2020 *(p
++) = HIGH(lower_bound
->first
- segments
);
2021 *(p
++) = LOW(lower_bound
->first
- segments
);
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
);
2034 /* there are more cases in the enumeration */
2035 /* which are handled with flags */
2039 recorder
->hints_record
.num_actions
++;
2040 recorder
->hints_record
.buf
= p
;
2045 TA_init_recorder(Recorder
* recorder
,
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
;
2056 TA_Segment segments
= axis
->segments
;
2057 TA_Segment seg_limit
= segments
+ axis
->num_segments
;
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
)
2103 num_strong_points
++;
2106 recorder
->num_strong_points
= num_strong_points
;
2113 TA_reset_recorder(Recorder
* recorder
,
2116 recorder
->hints_record
.buf
= bufp
;
2117 recorder
->hints_record
.num_actions
= 0;
2122 TA_rewind_recorder(Recorder
* recorder
,
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
);
2146 before_node
= next_before_node
)
2148 next_before_node
= LLRB_NEXT(ip_before_points
,
2149 &recorder
->ip_before_points_head
,
2151 LLRB_REMOVE(ip_before_points
,
2152 &recorder
->ip_before_points_head
,
2157 for (after_node
= LLRB_MIN(ip_after_points
,
2158 &recorder
->ip_after_points_head
);
2160 after_node
= next_after_node
)
2162 next_after_node
= LLRB_NEXT(ip_after_points
,
2163 &recorder
->ip_after_points_head
,
2165 LLRB_REMOVE(ip_after_points
,
2166 &recorder
->ip_after_points_head
,
2171 for (on_node
= LLRB_MIN(ip_on_points
,
2172 &recorder
->ip_on_points_head
);
2174 on_node
= next_on_node
)
2176 next_on_node
= LLRB_NEXT(ip_on_points
,
2177 &recorder
->ip_on_points_head
,
2179 LLRB_REMOVE(ip_on_points
,
2180 &recorder
->ip_on_points_head
,
2185 for (between_node
= LLRB_MIN(ip_between_points
,
2186 &recorder
->ip_between_points_head
);
2188 between_node
= next_between_node
)
2190 next_between_node
= LLRB_NEXT(ip_between_points
,
2191 &recorder
->ip_between_points_head
,
2193 LLRB_REMOVE(ip_between_points
,
2194 &recorder
->ip_between_points_head
,
2202 TA_free_recorder(Recorder
* recorder
)
2204 free(recorder
->wrap_around_segments
);
2206 TA_rewind_recorder(recorder
, NULL
, 0);
2211 TA_sfnt_build_glyph_instructions(SFNT
* sfnt
,
2215 FT_Face face
= sfnt
->face
;
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
;
2236 FT_UShort num_stack_elements
;
2237 FT_Bool optimize
= 0;
2239 FT_Int32 load_flags
;
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
;
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
);
2271 /* temporarily disable some debugging output */
2272 /* to avoid getting the information twice */
2273 _ta_debug_save
= _ta_debug
;
2277 ta_loader_register_hints_recorder(font
->loader
, NULL
, NULL
);
2278 error
= ta_loader_load_glyph(font
, face
, (FT_UInt
)idx
, load_flags
);
2281 _ta_debug
= _ta_debug_save
;
2287 /* do nothing if we have an empty glyph */
2288 if (!face
->glyph
->outline
.n_contours
)
2291 hints
= &font
->loader
->hints
;
2293 /* do nothing if the setup delivered the `none_dflt' style only */
2294 if (!hints
->num_points
)
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
);
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
);
2326 error
= FT_Err_Out_Of_Memory
;
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
);
2345 error
= FT_Err_Out_Of_Memory
;
2352 error
= TA_init_recorder(&recorder
, sfnt
, font
, glyph
, hints
);
2356 /* loop over a large range of pixel sizes */
2357 /* to find hints records which get pushed onto the bytecode stack */
2366 (void)FT_Get_Glyph_Name(face
, idx
, buf
, 256);
2368 num_chars
= fprintf(stderr
, "glyph %ld", idx
);
2370 num_chars
+= fprintf(stderr
, " (%s)", buf
);
2371 fprintf(stderr
, "\n");
2372 for (i
= 0; i
< num_chars
; i
++)
2374 fprintf(stderr
, "\n\n");
2379 /* we temporarily use `ins_buf' to record the current glyph hints */
2380 ta_loader_register_hints_recorder(font
->loader
,
2384 for (size
= font
->hinting_range_min
;
2385 size
<= font
->hinting_range_max
;
2393 TA_rewind_recorder(&recorder
, ins_buf
, size
);
2395 error
= FT_Set_Pixel_Sizes(face
, size
, size
);
2405 num_chars
= fprintf(stderr
, "size %d\n", size
);
2406 for (i
= 0; i
< num_chars
- 1; i
++)
2408 fprintf(stderr
, "\n\n");
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
);
2419 if (TA_hints_record_is_different(action_hints_records
,
2420 num_action_hints_records
,
2421 ins_buf
, recorder
.hints_record
.buf
))
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)");
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");
2445 error
= TA_add_hints_record(&action_hints_records
,
2446 &num_action_hints_records
,
2447 ins_buf
, recorder
.hints_record
);
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
))
2471 num_chars
= fprintf(stderr
, "size %d\n", size
);
2472 for (i
= 0; i
< num_chars
- 1; i
++)
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)");
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");
2494 error
= TA_add_hints_record(&point_hints_records
,
2495 &num_point_hints_records
,
2496 ins_buf
, recorder
.hints_record
);
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
);
2508 error
= FT_Err_Out_Of_Memory
;
2515 /* if there is only a single record, */
2516 /* we do a global optimization later on */
2517 if (num_action_hints_records
> 1)
2520 /* store the hints records and handle stack depth */
2522 bufp
= TA_emit_hints_records(&recorder
,
2523 point_hints_records
,
2524 num_point_hints_records
,
2528 num_stack_elements
= recorder
.num_stack_elements
;
2529 recorder
.num_stack_elements
= 0;
2532 bufp
= TA_emit_hints_records(&recorder
,
2533 action_hints_records
,
2534 num_action_hints_records
,
2538 recorder
.num_stack_elements
+= num_stack_elements
;
2541 bufp
= TA_sfnt_build_glyph_segments(sfnt
, &recorder
, bufp
, optimize
);
2544 error
= FT_Err_Out_Of_Memory
;
2548 if (num_action_hints_records
== 1)
2549 bufp
= TA_optimize_push(ins_buf
, pos
);
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
);
2557 /* handle delta exceptions */
2558 if (font
->control_data_head
)
2560 bufp
= TA_sfnt_build_delta_exceptions(sfnt
, font
, idx
, bufp
);
2563 error
= FT_Err_Out_Of_Memory
;
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
;
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
);
2588 /* end of tabytecode.c */