4 * Copyright (C) 2011-2012 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.
21 #define MISSING (FT_UInt)~0
23 /* a simple macro to emit bytecode instructions */
24 #define BCI(code) *(bufp++) = (code)
26 /* we increase the stack depth by this amount */
27 #define ADDITIONAL_STACK_ELEMENTS 20
30 /* #define DEBUGGING */
35 int _ta_debug_disable_horz_hints
;
36 int _ta_debug_disable_vert_hints
;
37 int _ta_debug_disable_blue_hints
;
38 void* _ta_debug_hints
;
42 typedef struct Hints_Record_
50 typedef struct Recorder_
53 GLYPH
* glyph
; /* the current glyph */
54 Hints_Record hints_record
;
56 /* some segments can `wrap around' */
57 /* a contour's start point like 24-25-26-0-1-2 */
58 /* (there can be at most one such segment per contour); */
59 /* later on we append additional records */
60 /* to split them into 24-26 and 0-2 */
61 FT_UInt
* wrap_around_segments
;
62 FT_UInt num_wrap_around_segments
;
64 FT_UShort num_stack_elements
; /* the necessary stack depth so far */
66 /* data necessary for strong point interpolation */
67 FT_UInt
* ip_before_points
;
68 FT_UInt
* ip_after_points
;
69 FT_UInt
* ip_on_point_array
;
70 FT_UInt
* ip_between_point_array
;
72 FT_UInt num_strong_points
;
77 /* We add a subglyph for each composite glyph. */
78 /* Since subglyphs must contain at least one point, */
79 /* we have to adjust all point indices accordingly. */
80 /* Using the `pointsums' array of the `GLYPH' structure */
81 /* it is straightforward to do that: */
82 /* Assuming that point with index x is in the interval */
83 /* pointsums[n] <= x < pointsums[n + 1], */
84 /* the new point index is x + n. */
87 TA_adjust_point_index(Recorder
* recorder
,
90 GLYPH
* glyph
= recorder
->glyph
;
94 if (!glyph
->num_components
)
95 return idx
; /* not a composite glyph */
97 for (i
= 0; i
< glyph
->num_pointsums
; i
++)
98 if (idx
< glyph
->pointsums
[i
])
105 /* we store the segments in the storage area; */
106 /* each segment record consists of the first and last point */
109 TA_sfnt_build_glyph_segments(SFNT
* sfnt
,
113 FONT
* font
= recorder
->font
;
114 TA_GlyphHints hints
= &font
->loader
->hints
;
115 TA_AxisHints axis
= &hints
->axis
[TA_DIMENSION_VERT
];
116 TA_Point points
= hints
->points
;
117 TA_Segment segments
= axis
->segments
;
119 TA_Segment seg_limit
;
121 FT_Outline outline
= font
->loader
->gloader
->base
.outline
;
127 FT_UInt num_segments
;
129 FT_Bool need_words
= 0;
134 FT_UInt num_packed_segments
;
136 FT_UInt num_stack_elements
;
137 FT_UInt num_twilight_points
;
140 seg_limit
= segments
+ axis
->num_segments
;
141 num_segments
= axis
->num_segments
;
143 /* to pack the data in the bytecode more tightly, */
144 /* we store up to the first nine segments in nibbles if possible, */
145 /* using delta values */
147 num_packed_segments
= 0;
148 for (seg
= segments
; seg
< seg_limit
; seg
++)
150 FT_UInt first
= seg
->first
- points
;
151 FT_UInt last
= seg
->last
- points
;
154 first
= TA_adjust_point_index(recorder
, first
);
155 last
= TA_adjust_point_index(recorder
, last
);
157 if (first
- base
>= 16)
159 if (first
> last
|| last
- first
>= 16)
161 if (num_packed_segments
== 9)
163 num_packed_segments
++;
167 /* also handle wrap-around segments */
168 num_segments
+= recorder
->num_wrap_around_segments
;
170 /* wrap-around segments are pushed with four arguments; */
171 /* a segment stored in nibbles needs only one byte instead of two */
172 num_args
= num_packed_segments
173 + 2 * (num_segments
- num_packed_segments
)
174 + 2 * recorder
->num_wrap_around_segments
177 /* collect all arguments temporarily in an array (in reverse order) */
178 /* so that we can easily split into chunks of 255 args */
179 /* as needed by NPUSHB and NPUSHW, respectively */
180 args
= (FT_UInt
*)malloc(num_args
* sizeof (FT_UInt
));
184 arg
= args
+ num_args
- 1;
186 if (num_segments
> 0xFF)
189 /* the number of packed segments is indicated by the function number */
190 if (recorder
->glyph
->num_components
)
191 *(arg
--) = bci_create_segments_composite_0
+ num_packed_segments
;
193 *(arg
--) = bci_create_segments_0
+ num_packed_segments
;
194 *(arg
--) = num_segments
;
197 for (seg
= segments
; seg
< segments
+ num_packed_segments
; seg
++)
199 FT_UInt first
= seg
->first
- points
;
200 FT_UInt last
= seg
->last
- points
;
205 first
= TA_adjust_point_index(recorder
, first
);
206 last
= TA_adjust_point_index(recorder
, last
);
208 low_nibble
= first
- base
;
209 high_nibble
= last
- first
;
211 *(arg
--) = 16 * high_nibble
+ low_nibble
;
219 for (seg
= segments
+ num_packed_segments
; seg
< seg_limit
; seg
++)
221 FT_UInt first
= seg
->first
- points
;
222 FT_UInt last
= seg
->last
- points
;
225 *(arg
--) = TA_adjust_point_index(recorder
, first
);
226 *(arg
--) = TA_adjust_point_index(recorder
, last
);
228 /* we push the last and first contour point */
229 /* as a third and fourth argument in wrap-around segments */
232 for (n
= 0; n
< outline
.n_contours
; n
++)
234 FT_UInt end
= (FT_UInt
)outline
.contours
[n
];
239 *(arg
--) = TA_adjust_point_index(recorder
, end
);
244 *(arg
--) = TA_adjust_point_index(recorder
, 0);
246 *(arg
--) = TA_adjust_point_index(recorder
,
247 (FT_UInt
)outline
.contours
[n
- 1] + 1);
257 /* emit the second part of wrap-around segments as separate segments */
258 /* so that edges can easily link to them */
259 for (seg
= segments
; seg
< seg_limit
; seg
++)
261 FT_UInt first
= seg
->first
- points
;
262 FT_UInt last
= seg
->last
- points
;
267 for (n
= 0; n
< outline
.n_contours
; n
++)
269 if (first
<= (FT_UInt
)outline
.contours
[n
])
272 *(arg
--) = TA_adjust_point_index(recorder
, 0);
274 *(arg
--) = TA_adjust_point_index(recorder
,
275 (FT_UInt
)outline
.contours
[n
- 1] + 1);
280 *(arg
--) = TA_adjust_point_index(recorder
, last
);
283 /* with most fonts it is very rare */
284 /* that any of the pushed arguments is larger than 0xFF, */
285 /* thus we refrain from further optimizing this case */
291 for (i
= 0; i
< num_args
; i
+= 255)
293 nargs
= (num_args
- i
> 255) ? 255 : num_args
- i
;
297 for (j
= 0; j
< nargs
; j
++)
307 for (i
= 0; i
< num_args
; i
+= 255)
309 nargs
= (num_args
- i
> 255) ? 255 : num_args
- i
;
313 for (j
= 0; j
< nargs
; j
++)
323 num_storage
= sal_segment_offset
+ num_segments
* 2;
324 if (num_storage
> sfnt
->max_storage
)
325 sfnt
->max_storage
= num_storage
;
327 num_twilight_points
= num_segments
* 2;
328 if (num_twilight_points
> sfnt
->max_twilight_points
)
329 sfnt
->max_twilight_points
= num_twilight_points
;
331 /* both this function and `TA_emit_hints_record' */
332 /* push data onto the stack */
333 num_stack_elements
= ADDITIONAL_STACK_ELEMENTS
334 + recorder
->num_stack_elements
+ num_args
;
335 if (num_stack_elements
> sfnt
->max_stack_elements
)
336 sfnt
->max_stack_elements
= num_stack_elements
;
345 TA_sfnt_build_glyph_scaler(SFNT
* sfnt
,
349 FT_GlyphSlot glyph
= sfnt
->face
->glyph
;
350 FT_Vector
* points
= glyph
->outline
.points
;
351 FT_Int num_contours
= glyph
->outline
.n_contours
;
358 FT_Bool need_words
= 0;
362 FT_UInt num_stack_elements
;
365 num_args
= 2 * num_contours
+ 2;
367 /* collect all arguments temporarily in an array (in reverse order) */
368 /* so that we can easily split into chunks of 255 args */
369 /* as needed by NPUSHB and NPUSHW, respectively */
370 args
= (FT_UInt
*)malloc(num_args
* sizeof (FT_UInt
));
374 arg
= args
+ num_args
- 1;
379 if (recorder
->glyph
->num_components
)
380 *(arg
--) = bci_scale_composite_glyph
;
382 *(arg
--) = bci_scale_glyph
;
383 *(arg
--) = num_contours
;
388 for (p
= 0; p
< num_contours
; p
++)
394 end
= glyph
->outline
.contours
[p
];
396 for (q
= start
; q
<= end
; q
++)
398 if (points
[q
].y
< points
[min
].y
)
400 if (points
[q
].y
> points
[max
].y
)
406 *(arg
--) = TA_adjust_point_index(recorder
, max
);
407 *(arg
--) = TA_adjust_point_index(recorder
, min
);
411 *(arg
--) = TA_adjust_point_index(recorder
, min
);
412 *(arg
--) = TA_adjust_point_index(recorder
, max
);
421 /* with most fonts it is very rare */
422 /* that any of the pushed arguments is larger than 0xFF, */
423 /* thus we refrain from further optimizing this case */
429 for (i
= 0; i
< num_args
; i
+= 255)
431 nargs
= (num_args
- i
> 255) ? 255 : num_args
- i
;
435 for (j
= 0; j
< nargs
; j
++)
445 for (i
= 0; i
< num_args
; i
+= 255)
447 nargs
= (num_args
- i
> 255) ? 255 : num_args
- i
;
451 for (j
= 0; j
< nargs
; j
++)
461 num_stack_elements
= ADDITIONAL_STACK_ELEMENTS
+ num_args
;
462 if (num_stack_elements
> sfnt
->max_stack_elements
)
463 sfnt
->max_stack_elements
= num_stack_elements
;
472 TA_font_build_subglyph_shifter(FONT
* font
,
475 FT_Face face
= font
->loader
->face
;
476 FT_GlyphSlot glyph
= face
->glyph
;
478 TA_GlyphLoader gloader
= font
->loader
->gloader
;
480 TA_SubGlyph subglyphs
= gloader
->base
.subglyphs
;
481 TA_SubGlyph subglyph_limit
= subglyphs
+ gloader
->base
.num_subglyphs
;
482 TA_SubGlyph subglyph
;
484 FT_Int curr_contour
= 0;
487 for (subglyph
= subglyphs
; subglyph
< subglyph_limit
; subglyph
++)
491 FT_UShort flags
= subglyph
->flags
;
492 FT_Pos y_offset
= subglyph
->arg2
;
497 /* load subglyph to get the number of contours */
498 error
= FT_Load_Glyph(face
, subglyph
->index
, FT_LOAD_NO_SCALE
);
501 num_contours
= glyph
->outline
.n_contours
;
503 /* nothing to do if there is a point-to-point alignment */
504 if (!(flags
& FT_SUBGLYPH_FLAG_ARGS_ARE_XY_VALUES
))
507 /* nothing to do if y offset is zero */
511 /* nothing to do if there are no contours */
515 /* note that calling `FT_Load_Glyph' without FT_LOAD_NO_RECURSE */
516 /* ensures that composite subglyphs are represented as simple glyphs */
518 if (num_contours
> 0xFF
519 || curr_contour
> 0xFF)
522 BCI(HIGH(curr_contour
));
523 BCI(LOW(curr_contour
));
524 BCI(HIGH(num_contours
));
525 BCI(LOW(num_contours
));
534 /* there are high chances that this value needs PUSHW, */
535 /* thus we handle it separately */
536 if (y_offset
> 0xFF || y_offset
< 0)
549 BCI(bci_shift_subglyph
);
553 curr_contour
+= num_contours
;
561 * The four `ta_ip_*' actions in the `TA_hints_recorder' callback store its
562 * data in four arrays (which are simple but waste a lot of memory). The
563 * function below converts them into bytecode.
565 * For `ta_ip_before' and `ta_ip_after', the collected points are emitted
566 * together with the edge they correspond to.
568 * For both `ta_ip_on' and `ta_ip_between', an outer loop is constructed to
569 * loop over the edge or edge pairs, respectively, and each edge or edge
570 * pair contains an inner loop to emit the correponding points.
574 TA_build_point_hints(Recorder
* recorder
,
577 TA_AxisHints axis
= &hints
->axis
[TA_DIMENSION_VERT
];
578 TA_Segment segments
= axis
->segments
;
579 TA_Edge edges
= axis
->edges
;
585 FT_Byte
* p
= recorder
->hints_record
.buf
;
586 FT_UInt num_edges
= axis
->num_edges
;
587 FT_UInt num_strong_points
= recorder
->num_strong_points
;
601 /* we store everything as 16bit numbers; */
602 /* the function numbers (`ta_ip_before', etc.) */
603 /* reflect the order in the TA_Action enumeration */
605 /* ip_before_points */
608 ip
= recorder
->ip_before_points
;
609 ip_limit
= ip
+ num_strong_points
;
610 for (; ip
< ip_limit
; ip
++)
620 recorder
->hints_record
.num_actions
++;
625 *(p
++) = (FT_Byte
)ta_ip_before
+ ACTION_OFFSET
;
626 *(p
++) = HIGH(edge
->first
- segments
);
627 *(p
++) = LOW(edge
->first
- segments
);
631 ip
= recorder
->ip_before_points
;
633 for (; ip
< ip_limit
; ip
++)
635 FT_UInt point
= TA_adjust_point_index(recorder
, *ip
);
638 *(p
++) = HIGH(point
);
643 /* ip_after_points */
646 ip
= recorder
->ip_after_points
;
647 ip_limit
= ip
+ num_strong_points
;
648 for (; ip
< ip_limit
; ip
++)
658 recorder
->hints_record
.num_actions
++;
660 edge
= edges
+ axis
->num_edges
- 1;
663 *(p
++) = (FT_Byte
)ta_ip_after
+ ACTION_OFFSET
;
664 *(p
++) = HIGH(edge
->first
- segments
);
665 *(p
++) = LOW(edge
->first
- segments
);
669 ip
= recorder
->ip_after_points
;
671 for (; ip
< ip_limit
; ip
++)
673 FT_UInt point
= TA_adjust_point_index(recorder
, *ip
);
676 *(p
++) = HIGH(point
);
681 /* ip_on_point_array */
684 ip
= recorder
->ip_on_point_array
;
685 ip_limit
= ip
+ num_edges
* num_strong_points
;
686 for (; ip
< ip_limit
; ip
+= num_strong_points
)
692 recorder
->hints_record
.num_actions
++;
695 *(p
++) = (FT_Byte
)ta_ip_on
+ ACTION_OFFSET
;
700 ip
= recorder
->ip_on_point_array
;
701 ip_limit
= ip
+ num_edges
* num_strong_points
;
702 for (; ip
< ip_limit
; ip
+= num_strong_points
, i
++)
709 *(p
++) = HIGH(edge
->first
- segments
);
710 *(p
++) = LOW(edge
->first
- segments
);
714 iq_limit
= iq
+ num_strong_points
;
715 for (; iq
< iq_limit
; iq
++)
728 for (; iq
< iq_limit
; iq
++)
730 FT_UInt point
= TA_adjust_point_index(recorder
, *iq
);
733 *(p
++) = HIGH(point
);
739 /* ip_between_point_array */
742 ip
= recorder
->ip_between_point_array
;
743 ip_limit
= ip
+ num_edges
* num_edges
* num_strong_points
;
744 for (; ip
< ip_limit
; ip
+= num_strong_points
)
750 recorder
->hints_record
.num_actions
++;
753 *(p
++) = (FT_Byte
)ta_ip_between
+ ACTION_OFFSET
;
758 ip
= recorder
->ip_between_point_array
;
759 ip_limit
= ip
+ num_edges
* num_edges
* num_strong_points
;
762 ip
+= num_edges
* num_strong_points
, i
++)
768 iq_limit
= iq
+ num_edges
* num_strong_points
;
769 for (; iq
< iq_limit
; iq
+= num_strong_points
, j
++)
776 *(p
++) = HIGH(after
->first
- segments
);
777 *(p
++) = LOW(after
->first
- segments
);
778 *(p
++) = HIGH(before
->first
- segments
);
779 *(p
++) = LOW(before
->first
- segments
);
783 ir_limit
= ir
+ num_strong_points
;
784 for (; ir
< ir_limit
; ir
++)
797 for (; ir
< ir_limit
; ir
++)
799 FT_UInt point
= TA_adjust_point_index(recorder
, *ir
);
802 *(p
++) = HIGH(point
);
809 recorder
->hints_record
.buf
= p
;
814 TA_hints_record_is_different(Hints_Record
* hints_records
,
815 FT_UInt num_hints_records
,
819 Hints_Record last_hints_record
;
825 /* we only need to compare with the last hints record */
826 last_hints_record
= hints_records
[num_hints_records
- 1];
828 if ((FT_UInt
)(end
- start
) != last_hints_record
.buf_len
)
831 if (memcmp(start
, last_hints_record
.buf
, last_hints_record
.buf_len
))
839 TA_add_hints_record(Hints_Record
** hints_records
,
840 FT_UInt
* num_hints_records
,
842 Hints_Record hints_record
)
844 Hints_Record
* hints_records_new
;
846 /* at this point, `hints_record.buf' still points into `ins_buf' */
847 FT_Byte
* end
= hints_record
.buf
;
850 buf_len
= (FT_UInt
)(end
- start
);
852 /* now fill the structure completely */
853 hints_record
.buf_len
= buf_len
;
854 hints_record
.buf
= (FT_Byte
*)malloc(buf_len
);
855 if (!hints_record
.buf
)
856 return FT_Err_Out_Of_Memory
;
858 memcpy(hints_record
.buf
, start
, buf_len
);
860 (*num_hints_records
)++;
862 (Hints_Record
*)realloc(*hints_records
, *num_hints_records
863 * sizeof (Hints_Record
));
864 if (!hints_records_new
)
866 free(hints_record
.buf
);
867 (*num_hints_records
)--;
868 return FT_Err_Out_Of_Memory
;
871 *hints_records
= hints_records_new
;
873 (*hints_records
)[*num_hints_records
- 1] = hints_record
;
880 TA_emit_hints_record(Recorder
* recorder
,
881 Hints_Record
* hints_record
,
886 FT_Bool need_words
= 0;
889 FT_UInt num_arguments
;
893 /* check whether any argument is larger than 0xFF */
894 endp
= hints_record
->buf
+ hints_record
->buf_len
;
895 for (p
= hints_record
->buf
; p
< endp
; p
+= 2)
899 /* with most fonts it is very rare */
900 /* that any of the pushed arguments is larger than 0xFF, */
901 /* thus we refrain from further optimizing this case */
903 num_arguments
= hints_record
->buf_len
/ 2;
908 for (i
= 0; i
< num_arguments
; i
+= 255)
910 num_args
= (num_arguments
- i
> 255) ? 255 : (num_arguments
- i
);
914 for (j
= 0; j
< num_args
; j
++)
924 /* we only need the lower bytes */
927 for (i
= 0; i
< num_arguments
; i
+= 255)
929 num_args
= (num_arguments
- i
> 255) ? 255 : (num_arguments
- i
);
933 for (j
= 0; j
< num_args
; j
++)
941 /* collect stack depth data */
942 if (num_arguments
> recorder
->num_stack_elements
)
943 recorder
->num_stack_elements
= num_arguments
;
950 TA_emit_hints_records(Recorder
* recorder
,
951 Hints_Record
* hints_records
,
952 FT_UInt num_hints_records
,
956 Hints_Record
* hints_record
;
959 hints_record
= hints_records
;
961 for (i
= 0; i
< num_hints_records
- 1; i
++)
964 if (hints_record
->size
> 0xFF)
967 BCI(HIGH((hints_record
+ 1)->size
));
968 BCI(LOW((hints_record
+ 1)->size
));
973 BCI((hints_record
+ 1)->size
);
977 bufp
= TA_emit_hints_record(recorder
, hints_record
, bufp
);
983 bufp
= TA_emit_hints_record(recorder
, hints_record
, bufp
);
985 for (i
= 0; i
< num_hints_records
- 1; i
++)
993 TA_free_hints_records(Hints_Record
* hints_records
,
994 FT_UInt num_hints_records
)
999 for (i
= 0; i
< num_hints_records
; i
++)
1000 free(hints_records
[i
].buf
);
1002 free(hints_records
);
1007 TA_hints_recorder_handle_segments(FT_Byte
* bufp
,
1012 TA_Segment segments
= axis
->segments
;
1015 FT_UInt num_segs
= 0;
1019 seg_idx
= edge
->first
- segments
;
1021 /* we store everything as 16bit numbers */
1022 *(bufp
++) = HIGH(seg_idx
);
1023 *(bufp
++) = LOW(seg_idx
);
1025 /* wrap-around segments are stored as two segments */
1026 if (edge
->first
->first
> edge
->first
->last
)
1029 seg
= edge
->first
->edge_next
;
1030 while (seg
!= edge
->first
)
1034 if (seg
->first
> seg
->last
)
1037 seg
= seg
->edge_next
;
1040 *(bufp
++) = HIGH(num_segs
);
1041 *(bufp
++) = LOW(num_segs
);
1043 if (edge
->first
->first
> edge
->first
->last
)
1045 /* emit second part of wrap-around segment; */
1046 /* the bytecode positions such segments after `normal' ones */
1050 if (seg_idx
== *wrap
)
1055 *(bufp
++) = HIGH(axis
->num_segments
+ (wrap
- wraps
));
1056 *(bufp
++) = LOW(axis
->num_segments
+ (wrap
- wraps
));
1059 seg
= edge
->first
->edge_next
;
1060 while (seg
!= edge
->first
)
1062 seg_idx
= seg
- segments
;
1064 *(bufp
++) = HIGH(seg_idx
);
1065 *(bufp
++) = LOW(seg_idx
);
1067 if (seg
->first
> seg
->last
)
1072 if (seg_idx
== *wrap
)
1077 *(bufp
++) = HIGH(axis
->num_segments
+ (wrap
- wraps
));
1078 *(bufp
++) = LOW(axis
->num_segments
+ (wrap
- wraps
));
1081 seg
= seg
->edge_next
;
1089 TA_hints_recorder(TA_Action action
,
1090 TA_GlyphHints hints
,
1095 TA_Edge lower_bound
,
1096 TA_Edge upper_bound
)
1098 TA_AxisHints axis
= &hints
->axis
[dim
];
1099 TA_Edge edges
= axis
->edges
;
1100 TA_Segment segments
= axis
->segments
;
1101 TA_Point points
= hints
->points
;
1103 Recorder
* recorder
= (Recorder
*)hints
->user
;
1104 FONT
* font
= recorder
->font
;
1105 FT_UInt
* wraps
= recorder
->wrap_around_segments
;
1106 FT_Byte
* p
= recorder
->hints_record
.buf
;
1112 if (dim
== TA_DIMENSION_HORZ
)
1115 /* we collect point hints for later processing */
1120 TA_Point point
= (TA_Point
)arg1
;
1123 ip
= recorder
->ip_before_points
;
1124 limit
= ip
+ recorder
->num_strong_points
;
1125 for (; ip
< limit
; ip
++)
1129 *ip
= point
- points
;
1138 TA_Point point
= (TA_Point
)arg1
;
1141 ip
= recorder
->ip_after_points
;
1142 limit
= ip
+ recorder
->num_strong_points
;
1143 for (; ip
< limit
; ip
++)
1147 *ip
= point
- points
;
1156 TA_Point point
= (TA_Point
)arg1
;
1157 TA_Edge edge
= arg2
;
1160 ip
= recorder
->ip_on_point_array
1161 + recorder
->num_strong_points
1163 limit
= ip
+ recorder
->num_strong_points
;
1164 for (; ip
< limit
; ip
++)
1168 *ip
= point
- points
;
1177 TA_Point point
= (TA_Point
)arg1
;
1178 TA_Edge before
= arg2
;
1179 TA_Edge after
= arg3
;
1182 /* note that `recorder->num_segments' has been used for allocation, */
1183 /* but `axis->num_edges' is used for accessing this array */
1184 ip
= recorder
->ip_between_point_array
1185 + recorder
->num_strong_points
* axis
->num_edges
1187 + recorder
->num_strong_points
1189 limit
= ip
+ recorder
->num_strong_points
;
1190 for (; ip
< limit
; ip
++)
1194 *ip
= point
- points
;
1202 /* we ignore the BOUND action since we signal this information */
1203 /* with the proper function number */
1210 /* some enum values correspond to four or eight bytecode functions; */
1211 /* if the value is n, the function numbers are n, ..., n+7, */
1212 /* to be differentiated with flags */
1218 TA_Edge base_edge
= (TA_Edge
)arg1
;
1219 TA_Edge stem_edge
= arg2
;
1223 *(p
++) = (FT_Byte
)action
+ ACTION_OFFSET
1224 + ((stem_edge
->flags
& TA_EDGE_SERIF
) != 0)
1225 + 2 * ((base_edge
->flags
& TA_EDGE_ROUND
) != 0);
1227 *(p
++) = HIGH(base_edge
->first
- segments
);
1228 *(p
++) = LOW(base_edge
->first
- segments
);
1229 *(p
++) = HIGH(stem_edge
->first
- segments
);
1230 *(p
++) = LOW(stem_edge
->first
- segments
);
1232 p
= TA_hints_recorder_handle_segments(p
, axis
, stem_edge
, wraps
);
1238 TA_Edge edge
= (TA_Edge
)arg1
;
1239 TA_Edge edge2
= arg2
;
1243 *(p
++) = (FT_Byte
)action
+ ACTION_OFFSET
1244 + ((edge2
->flags
& TA_EDGE_SERIF
) != 0)
1245 + 2 * ((edge
->flags
& TA_EDGE_ROUND
) != 0);
1247 *(p
++) = HIGH(edge
->first
- segments
);
1248 *(p
++) = LOW(edge
->first
- segments
);
1249 *(p
++) = HIGH(edge2
->first
- segments
);
1250 *(p
++) = LOW(edge2
->first
- segments
);
1252 p
= TA_hints_recorder_handle_segments(p
, axis
, edge
, wraps
);
1258 TA_Edge edge
= (TA_Edge
)arg1
;
1259 TA_Edge edge2
= arg2
;
1260 TA_Edge edge_minus_one
= lower_bound
;
1264 *(p
++) = (FT_Byte
)action
+ ACTION_OFFSET
1265 + ((edge2
->flags
& TA_EDGE_SERIF
) != 0)
1266 + 2 * ((edge
->flags
& TA_EDGE_ROUND
) != 0)
1267 + 4 * (edge_minus_one
!= NULL
);
1269 *(p
++) = HIGH(edge
->first
- segments
);
1270 *(p
++) = LOW(edge
->first
- segments
);
1271 *(p
++) = HIGH(edge2
->first
- segments
);
1272 *(p
++) = LOW(edge2
->first
- segments
);
1276 *(p
++) = HIGH(edge_minus_one
->first
- segments
);
1277 *(p
++) = LOW(edge_minus_one
->first
- segments
);
1280 p
= TA_hints_recorder_handle_segments(p
, axis
, edge
, wraps
);
1284 case ta_blue_anchor
:
1286 TA_Edge edge
= (TA_Edge
)arg1
;
1287 TA_Edge blue
= arg2
;
1291 *(p
++) = (FT_Byte
)action
+ ACTION_OFFSET
;
1293 *(p
++) = HIGH(blue
->first
- segments
);
1294 *(p
++) = LOW(blue
->first
- segments
);
1296 if (edge
->best_blue_is_shoot
)
1298 *(p
++) = HIGH(CVT_BLUE_SHOOTS_OFFSET(font
) + edge
->best_blue_idx
);
1299 *(p
++) = LOW(CVT_BLUE_SHOOTS_OFFSET(font
) + edge
->best_blue_idx
);
1303 *(p
++) = HIGH(CVT_BLUE_REFS_OFFSET(font
) + edge
->best_blue_idx
);
1304 *(p
++) = LOW(CVT_BLUE_REFS_OFFSET(font
) + edge
->best_blue_idx
);
1307 *(p
++) = HIGH(edge
->first
- segments
);
1308 *(p
++) = LOW(edge
->first
- segments
);
1310 p
= TA_hints_recorder_handle_segments(p
, axis
, edge
, wraps
);
1316 TA_Edge edge
= (TA_Edge
)arg1
;
1317 TA_Edge edge2
= arg2
;
1318 TA_Edge edge_minus_one
= lower_bound
;
1322 *(p
++) = (FT_Byte
)action
+ ACTION_OFFSET
1323 + ((edge2
->flags
& TA_EDGE_SERIF
) != 0)
1324 + 2 * ((edge
->flags
& TA_EDGE_ROUND
) != 0)
1325 + 4 * (edge_minus_one
!= NULL
);
1327 *(p
++) = HIGH(edge
->first
- segments
);
1328 *(p
++) = LOW(edge
->first
- segments
);
1329 *(p
++) = HIGH(edge2
->first
- segments
);
1330 *(p
++) = LOW(edge2
->first
- segments
);
1334 *(p
++) = HIGH(edge_minus_one
->first
- segments
);
1335 *(p
++) = LOW(edge_minus_one
->first
- segments
);
1338 p
= TA_hints_recorder_handle_segments(p
, axis
, edge
, wraps
);
1339 p
= TA_hints_recorder_handle_segments(p
, axis
, edge2
, wraps
);
1345 TA_Edge edge
= (TA_Edge
)arg1
;
1349 *(p
++) = (FT_Byte
)action
+ ACTION_OFFSET
;
1351 if (edge
->best_blue_is_shoot
)
1353 *(p
++) = HIGH(CVT_BLUE_SHOOTS_OFFSET(font
) + edge
->best_blue_idx
);
1354 *(p
++) = LOW(CVT_BLUE_SHOOTS_OFFSET(font
) + edge
->best_blue_idx
);
1358 *(p
++) = HIGH(CVT_BLUE_REFS_OFFSET(font
) + edge
->best_blue_idx
);
1359 *(p
++) = LOW(CVT_BLUE_REFS_OFFSET(font
) + edge
->best_blue_idx
);
1362 *(p
++) = HIGH(edge
->first
- segments
);
1363 *(p
++) = LOW(edge
->first
- segments
);
1365 p
= TA_hints_recorder_handle_segments(p
, axis
, edge
, wraps
);
1371 TA_Edge serif
= (TA_Edge
)arg1
;
1372 TA_Edge base
= serif
->serif
;
1376 *(p
++) = (FT_Byte
)action
+ ACTION_OFFSET
1377 + (lower_bound
!= NULL
)
1378 + 2 * (upper_bound
!= NULL
);
1380 *(p
++) = HIGH(serif
->first
- segments
);
1381 *(p
++) = LOW(serif
->first
- segments
);
1382 *(p
++) = HIGH(base
->first
- segments
);
1383 *(p
++) = LOW(base
->first
- segments
);
1387 *(p
++) = HIGH(lower_bound
->first
- segments
);
1388 *(p
++) = LOW(lower_bound
->first
- segments
);
1392 *(p
++) = HIGH(upper_bound
->first
- segments
);
1393 *(p
++) = LOW(upper_bound
->first
- segments
);
1396 p
= TA_hints_recorder_handle_segments(p
, axis
, serif
, wraps
);
1400 case ta_serif_anchor
:
1401 case ta_serif_link2
:
1403 TA_Edge edge
= (TA_Edge
)arg1
;
1407 *(p
++) = (FT_Byte
)action
+ ACTION_OFFSET
1408 + (lower_bound
!= NULL
)
1409 + 2 * (upper_bound
!= NULL
);
1411 *(p
++) = HIGH(edge
->first
- segments
);
1412 *(p
++) = LOW(edge
->first
- segments
);
1416 *(p
++) = HIGH(lower_bound
->first
- segments
);
1417 *(p
++) = LOW(lower_bound
->first
- segments
);
1421 *(p
++) = HIGH(upper_bound
->first
- segments
);
1422 *(p
++) = LOW(upper_bound
->first
- segments
);
1425 p
= TA_hints_recorder_handle_segments(p
, axis
, edge
, wraps
);
1429 case ta_serif_link1
:
1431 TA_Edge edge
= (TA_Edge
)arg1
;
1432 TA_Edge before
= arg2
;
1433 TA_Edge after
= arg3
;
1437 *(p
++) = (FT_Byte
)action
+ ACTION_OFFSET
1438 + (lower_bound
!= NULL
)
1439 + 2 * (upper_bound
!= NULL
);
1441 *(p
++) = HIGH(before
->first
- segments
);
1442 *(p
++) = LOW(before
->first
- segments
);
1443 *(p
++) = HIGH(edge
->first
- segments
);
1444 *(p
++) = LOW(edge
->first
- segments
);
1445 *(p
++) = HIGH(after
->first
- segments
);
1446 *(p
++) = LOW(after
->first
- segments
);
1450 *(p
++) = HIGH(lower_bound
->first
- segments
);
1451 *(p
++) = LOW(lower_bound
->first
- segments
);
1455 *(p
++) = HIGH(upper_bound
->first
- segments
);
1456 *(p
++) = LOW(upper_bound
->first
- segments
);
1459 p
= TA_hints_recorder_handle_segments(p
, axis
, edge
, wraps
);
1464 /* there are more cases in the enumeration */
1465 /* which are handled with flags */
1469 recorder
->hints_record
.num_actions
++;
1470 recorder
->hints_record
.buf
= p
;
1475 TA_init_recorder(Recorder
* recorder
,
1478 TA_GlyphHints hints
)
1480 TA_AxisHints axis
= &hints
->axis
[TA_DIMENSION_VERT
];
1481 TA_Point points
= hints
->points
;
1482 TA_Point point_limit
= points
+ hints
->num_points
;
1485 TA_Segment segments
= axis
->segments
;
1486 TA_Segment seg_limit
= segments
+ axis
->num_segments
;
1489 FT_UInt num_strong_points
= 0;
1490 FT_UInt
* wrap_around_segment
;
1492 recorder
->font
= font
;
1493 recorder
->glyph
= glyph
;
1494 recorder
->num_segments
= axis
->num_segments
;
1496 recorder
->ip_before_points
= NULL
;
1497 recorder
->ip_after_points
= NULL
;
1498 recorder
->ip_on_point_array
= NULL
;
1499 recorder
->ip_between_point_array
= NULL
;
1501 recorder
->num_stack_elements
= 0;
1503 /* no need to clean up allocated arrays in case of error; */
1504 /* this is handled later by `TA_free_recorder' */
1506 recorder
->num_wrap_around_segments
= 0;
1507 for (seg
= segments
; seg
< seg_limit
; seg
++)
1508 if (seg
->first
> seg
->last
)
1509 recorder
->num_wrap_around_segments
++;
1511 recorder
->wrap_around_segments
=
1512 (FT_UInt
*)malloc(recorder
->num_wrap_around_segments
* sizeof (FT_UInt
));
1513 if (!recorder
->wrap_around_segments
)
1514 return FT_Err_Out_Of_Memory
;
1516 wrap_around_segment
= recorder
->wrap_around_segments
;
1517 for (seg
= segments
; seg
< seg_limit
; seg
++)
1518 if (seg
->first
> seg
->last
)
1519 *(wrap_around_segment
++) = seg
- segments
;
1521 /* get number of strong points */
1522 for (point
= points
; point
< point_limit
; point
++)
1524 /* actually, we need to test `TA_FLAG_TOUCH_Y' also; */
1525 /* however, this value isn't known yet */
1526 /* (or rather, it can vary between different pixel sizes) */
1527 if (point
->flags
& TA_FLAG_WEAK_INTERPOLATION
)
1530 num_strong_points
++;
1533 recorder
->num_strong_points
= num_strong_points
;
1535 recorder
->ip_before_points
=
1536 (FT_UInt
*)malloc(num_strong_points
* sizeof (FT_UInt
));
1537 if (!recorder
->ip_before_points
)
1538 return FT_Err_Out_Of_Memory
;
1540 recorder
->ip_after_points
=
1541 (FT_UInt
*)malloc(num_strong_points
* sizeof (FT_UInt
));
1542 if (!recorder
->ip_after_points
)
1543 return FT_Err_Out_Of_Memory
;
1545 /* actually, we need `hints->num_edges' for the array sizes; */
1546 /* however, this value isn't known yet */
1547 /* (or rather, it can vary between different pixel sizes) */
1548 recorder
->ip_on_point_array
=
1549 (FT_UInt
*)malloc(axis
->num_segments
1550 * num_strong_points
* sizeof (FT_UInt
));
1551 if (!recorder
->ip_on_point_array
)
1552 return FT_Err_Out_Of_Memory
;
1554 recorder
->ip_between_point_array
=
1555 (FT_UInt
*)malloc(axis
->num_segments
* axis
->num_segments
1556 * num_strong_points
* sizeof (FT_UInt
));
1557 if (!recorder
->ip_between_point_array
)
1558 return FT_Err_Out_Of_Memory
;
1565 TA_reset_recorder(Recorder
* recorder
,
1568 recorder
->hints_record
.buf
= bufp
;
1569 recorder
->hints_record
.num_actions
= 0;
1574 TA_rewind_recorder(Recorder
* recorder
,
1578 TA_reset_recorder(recorder
, bufp
);
1580 recorder
->hints_record
.size
= size
;
1582 /* We later check with MISSING (which expands to 0xFF bytes) */
1584 memset(recorder
->ip_before_points
, 0xFF,
1585 recorder
->num_strong_points
* sizeof (FT_UInt
));
1586 memset(recorder
->ip_after_points
, 0xFF,
1587 recorder
->num_strong_points
* sizeof (FT_UInt
));
1589 memset(recorder
->ip_on_point_array
, 0xFF,
1590 recorder
->num_segments
1591 * recorder
->num_strong_points
* sizeof (FT_UInt
));
1592 memset(recorder
->ip_between_point_array
, 0xFF,
1593 recorder
->num_segments
* recorder
->num_segments
1594 * recorder
->num_strong_points
* sizeof (FT_UInt
));
1599 TA_free_recorder(Recorder
* recorder
)
1601 free(recorder
->wrap_around_segments
);
1603 free(recorder
->ip_before_points
);
1604 free(recorder
->ip_after_points
);
1605 free(recorder
->ip_on_point_array
);
1606 free(recorder
->ip_between_point_array
);
1611 TA_sfnt_build_glyph_instructions(SFNT
* sfnt
,
1615 FT_Face face
= sfnt
->face
;
1623 SFNT_Table
* glyf_table
= &font
->tables
[sfnt
->glyf_idx
];
1624 glyf_Data
* data
= (glyf_Data
*)glyf_table
->data
;
1625 /* `idx' is never negative */
1626 GLYPH
* glyph
= &data
->glyphs
[idx
];
1628 TA_GlyphHints hints
;
1630 FT_UInt num_action_hints_records
;
1631 FT_UInt num_point_hints_records
;
1632 Hints_Record
* action_hints_records
;
1633 Hints_Record
* point_hints_records
;
1636 FT_UInt num_stack_elements
;
1638 FT_Int32 load_flags
;
1642 /* XXX: right now, we abuse this flag to control */
1643 /* the global behaviour of the auto-hinter */
1644 load_flags
= font
->fallback_script
<< 30;
1645 load_flags
|= 1 << 28; /* vertical hinting only */
1646 if (font
->increase_x_height
)
1647 load_flags
|= 1 << 29;
1648 if (!font
->pre_hinting
)
1649 load_flags
|= FT_LOAD_NO_SCALE
;
1651 /* computing the segments is resolution independent, */
1652 /* thus the pixel size in this call is arbitrary */
1653 error
= FT_Set_Pixel_Sizes(face
, 20, 20);
1657 ta_loader_register_hints_recorder(font
->loader
, NULL
, NULL
);
1658 error
= ta_loader_load_glyph(font
->loader
, face
, (FT_UInt
)idx
, load_flags
);
1662 /* do nothing if we have an empty glyph */
1663 if (!face
->glyph
->outline
.n_contours
)
1666 hints
= &font
->loader
->hints
;
1668 /* do nothing if the setup delivered the dummy module only */
1669 if (!hints
->num_points
)
1672 /* we allocate a buffer which is certainly large enough */
1673 /* to hold all of the created bytecode instructions; */
1674 /* later on it gets reallocated to its real size */
1675 ins_len
= hints
->num_points
* 1000;
1676 ins_buf
= (FT_Byte
*)malloc(ins_len
);
1678 return FT_Err_Out_Of_Memory
;
1680 /* initialize array with an invalid bytecode */
1681 /* so that we can easily find the array length at reallocation time */
1682 memset(ins_buf
, INS_A0
, ins_len
);
1684 /* handle composite glyph */
1685 if (font
->loader
->gloader
->base
.num_subglyphs
)
1687 bufp
= TA_font_build_subglyph_shifter(font
, ins_buf
);
1690 error
= FT_Err_Out_Of_Memory
;
1697 /* only scale the glyph if the dummy hinter has been used */
1698 if (font
->loader
->metrics
->clazz
== &ta_dummy_script_class
)
1700 /* since `TA_init_recorder' hasn't been called yet, */
1701 /* we manually initialize the `glyph' field */
1702 recorder
.glyph
= glyph
;
1704 bufp
= TA_sfnt_build_glyph_scaler(sfnt
, &recorder
, ins_buf
);
1707 error
= FT_Err_Out_Of_Memory
;
1714 error
= TA_init_recorder(&recorder
, font
, glyph
, hints
);
1718 /* loop over a large range of pixel sizes */
1719 /* to find hints records which get pushed onto the bytecode stack */
1726 num_chars
= fprintf(stderr
, "glyph %ld\n", idx
);
1727 for (i
= 0; i
< num_chars
- 1; i
++)
1729 fprintf(stderr
, "\n\n");
1734 /* we temporarily use `ins_buf' to record the current glyph hints */
1735 ta_loader_register_hints_recorder(font
->loader
,
1739 num_action_hints_records
= 0;
1740 num_point_hints_records
= 0;
1741 action_hints_records
= NULL
;
1742 point_hints_records
= NULL
;
1744 for (size
= font
->hinting_range_min
;
1745 size
<= font
->hinting_range_max
;
1753 TA_rewind_recorder(&recorder
, ins_buf
, size
);
1755 error
= FT_Set_Pixel_Sizes(face
, size
, size
);
1759 /* calling `ta_loader_load_glyph' uses the */
1760 /* `TA_hints_recorder' function as a callback, */
1761 /* modifying `hints_record' */
1762 error
= ta_loader_load_glyph(font
->loader
, face
, idx
, load_flags
);
1766 if (TA_hints_record_is_different(action_hints_records
,
1767 num_action_hints_records
,
1768 ins_buf
, recorder
.hints_record
.buf
))
1774 fprintf(stderr
, " size %d:\n", size
);
1776 ta_glyph_hints_dump_edges(_ta_debug_hints
);
1777 ta_glyph_hints_dump_segments(_ta_debug_hints
);
1778 ta_glyph_hints_dump_points(_ta_debug_hints
);
1780 fprintf(stderr
, " action hints record:\n");
1781 for (p
= ins_buf
; p
< recorder
.hints_record
.buf
; p
+= 2)
1782 fprintf(stderr
, " %2d", *p
* 256 + *(p
+ 1));
1783 fprintf(stderr
, "\n");
1787 error
= TA_add_hints_record(&action_hints_records
,
1788 &num_action_hints_records
,
1789 ins_buf
, recorder
.hints_record
);
1794 /* now handle point records */
1796 TA_reset_recorder(&recorder
, ins_buf
);
1798 /* use the point hints data collected in `TA_hints_recorder' */
1799 TA_build_point_hints(&recorder
, hints
);
1801 if (TA_hints_record_is_different(point_hints_records
,
1802 num_point_hints_records
,
1803 ins_buf
, recorder
.hints_record
.buf
))
1809 fprintf(stderr
, " size %d:\n", size
);
1811 ta_glyph_hints_dump_edges(_ta_debug_hints
);
1812 ta_glyph_hints_dump_segments(_ta_debug_hints
);
1813 ta_glyph_hints_dump_points(_ta_debug_hints
);
1816 fprintf(stderr
, " point hints record:\n");
1817 for (p
= ins_buf
; p
< recorder
.hints_record
.buf
; p
+= 2)
1818 fprintf(stderr
, " %2d", *p
* 256 + *(p
+ 1));
1819 fprintf(stderr
, "\n");
1823 error
= TA_add_hints_record(&point_hints_records
,
1824 &num_point_hints_records
,
1825 ins_buf
, recorder
.hints_record
);
1831 if (num_action_hints_records
== 1 && !action_hints_records
[0].num_actions
)
1833 /* since we only have a single empty record we just scale the glyph */
1834 bufp
= TA_sfnt_build_glyph_scaler(sfnt
, &recorder
, ins_buf
);
1837 error
= FT_Err_Out_Of_Memory
;
1841 /* clear the rest of the temporarily used part of `ins_buf' */
1843 while (*p
!= INS_A0
)
1849 /* store the hints records and handle stack depth */
1850 bufp
= TA_emit_hints_records(&recorder
,
1851 point_hints_records
,
1852 num_point_hints_records
,
1854 num_stack_elements
= recorder
.num_stack_elements
;
1855 recorder
.num_stack_elements
= 0;
1856 bufp
= TA_emit_hints_records(&recorder
,
1857 action_hints_records
,
1858 num_action_hints_records
,
1860 recorder
.num_stack_elements
+= num_stack_elements
;
1862 bufp
= TA_sfnt_build_glyph_segments(sfnt
, &recorder
, bufp
);
1865 error
= FT_Err_Out_Of_Memory
;
1869 /* XXX do nothing if we have NPUSHW in the data */
1870 if (num_action_hints_records
== 1
1871 && !memchr(ins_buf
, NPUSHW
, bufp
- ins_buf
))
1874 * we optimize two common cases, replacing
1876 * NPUSHB A ... NPUSHB B [... NPUSHB C] ... CALL
1880 * NPUSHB (A+B[+C]) ... CALL
1894 /* there are at least two NPUSHB instructions */
1895 /* (one of them directly at the start) */
1897 sizes
[0] = *(pos
[0] + 1);
1898 pos
[1] = (FT_Byte
*)memchr(pos
[0] + 1 + sizes
[0], NPUSHB
, bufp
- ins_buf
);
1899 sizes
[1] = *(pos
[1] + 1);
1900 pos
[2] = (FT_Byte
*)memchr(pos
[1] + 1 + sizes
[1], NPUSHB
, bufp
- ins_buf
);
1901 sizes
[2] = pos
[2] ? *(pos
[2] + 1) : 0;
1903 sum
= sizes
[0] + sizes
[1] + sizes
[2];
1906 goto Done2
; /* nothing to do since we need three NPUSHB */
1907 else if (!sizes
[2] && (sum
> 0xFF))
1908 goto Done2
; /* nothing to do since we need two NPUSHB */
1912 /* reduce three NPUSHB to two */
1914 new_size2
= sum
- 0xFF;
1918 /* reduce two or three NPUSHB to one */
1930 for (i
= 0; i
< new_size1
; i
++)
1932 if (p
== pos
[pos_idx
])
1935 p
+= 2; /* skip old NPUSHB */
1944 for (i
= 0; i
< new_size2
; i
++)
1946 if (p
== pos
[pos_idx
])
1959 /* clear the rest of the temporarily used part of `ins_buf' */
1961 while (*p
!= INS_A0
)
1965 TA_free_hints_records(action_hints_records
, num_action_hints_records
);
1966 TA_free_recorder(&recorder
);
1968 /* we are done, so reallocate the instruction array to its real size */
1969 if (*bufp
== INS_A0
)
1971 /* search backwards */
1972 while (*bufp
== INS_A0
)
1978 /* search forwards */
1979 while (*bufp
!= INS_A0
)
1984 ins_len
= bufp
- ins_buf
;
1986 if (ins_len
> sfnt
->max_instructions
)
1987 sfnt
->max_instructions
= ins_len
;
1989 glyph
->ins_buf
= (FT_Byte
*)realloc(ins_buf
, ins_len
);
1990 glyph
->ins_len
= ins_len
;
1995 TA_free_hints_records(action_hints_records
, num_action_hints_records
);
1996 TA_free_recorder(&recorder
);
2003 /* end of tabytecode.c */