4 * Copyright (C) 2011-2016 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 /* originally file `afhints.c' (2011-Mar-28) from FreeType */
18 /* heavily modified 2011 by Werner Lemberg <wl@gnu.org> */
27 /* get new segment for given axis */
30 ta_axis_hints_new_segment(TA_AxisHints axis
,
33 FT_Error error
= FT_Err_Ok
;
34 TA_Segment segment
= NULL
;
37 if (axis
->num_segments
< TA_SEGMENTS_EMBEDDED
)
39 if (axis
->segments
== NULL
)
41 axis
->segments
= axis
->embedded
.segments
;
42 axis
->max_segments
= TA_SEGMENTS_EMBEDDED
;
45 else if (axis
->num_segments
>= axis
->max_segments
)
47 TA_Segment segments_new
;
49 FT_Int old_max
= axis
->max_segments
;
50 FT_Int new_max
= old_max
;
51 FT_Int big_max
= (FT_Int
)(FT_INT_MAX
/ sizeof (*segment
));
54 if (old_max
>= big_max
)
56 error
= FT_Err_Out_Of_Memory
;
60 new_max
+= (new_max
>> 2) + 4;
65 if (axis
->segments
== axis
->embedded
.segments
)
67 axis
->segments
= (TA_Segment
)malloc(
68 (size_t)new_max
* sizeof (TA_SegmentRec
));
70 return FT_Err_Out_Of_Memory
;
72 memcpy(axis
->segments
, axis
->embedded
.segments
,
73 sizeof (axis
->embedded
.segments
));
77 segments_new
= (TA_Segment
)realloc(
79 (size_t)new_max
* sizeof (TA_SegmentRec
));
81 return FT_Err_Out_Of_Memory
;
82 axis
->segments
= segments_new
;
85 axis
->max_segments
= new_max
;
88 segment
= axis
->segments
+ axis
->num_segments
++;
96 /* get new edge for given axis, direction, and position, */
97 /* without initializing the edge itself */
100 ta_axis_hints_new_edge(TA_AxisHints axis
,
103 FT_Bool top_to_bottom_hinting
,
106 FT_Error error
= FT_Err_Ok
;
111 if (axis
->num_edges
< TA_EDGES_EMBEDDED
)
113 if (axis
->edges
== NULL
)
115 axis
->edges
= axis
->embedded
.edges
;
116 axis
->max_edges
= TA_EDGES_EMBEDDED
;
119 else if (axis
->num_edges
>= axis
->max_edges
)
123 FT_Int old_max
= axis
->max_edges
;
124 FT_Int new_max
= old_max
;
125 FT_Int big_max
= (FT_Int
)(FT_INT_MAX
/ sizeof (*edge
));
128 if (old_max
>= big_max
)
130 error
= FT_Err_Out_Of_Memory
;
134 new_max
+= (new_max
>> 2) + 4;
135 if (new_max
< old_max
136 || new_max
> big_max
)
139 if (axis
->edges
== axis
->embedded
.edges
)
141 axis
->edges
= (TA_Edge
)malloc((size_t)new_max
* sizeof (TA_EdgeRec
));
143 return FT_Err_Out_Of_Memory
;
145 memcpy(axis
->edges
, axis
->embedded
.edges
,
146 sizeof (axis
->embedded
.edges
));
150 edges_new
= (TA_Edge
)realloc(axis
->edges
,
151 (size_t)new_max
* sizeof (TA_EdgeRec
));
153 return FT_Err_Out_Of_Memory
;
154 axis
->edges
= edges_new
;
157 axis
->max_edges
= new_max
;
161 edge
= edges
+ axis
->num_edges
;
165 if (top_to_bottom_hinting
? (edge
[-1].fpos
> fpos
)
166 : (edge
[-1].fpos
< fpos
))
169 /* we want the edge with same position and minor direction */
170 /* to appear before those in the major one in the list */
171 if (edge
[-1].fpos
== fpos
172 && dir
== axis
->major_dir
)
195 _ta_message(const char* format
,
201 va_start(ap
, format
);
202 vfprintf(stderr
, format
, ap
);
208 ta_dir_str(TA_Direction dir
)
235 #define TA_INDEX_NUM(ptr, base) \
236 (int)((ptr) ? ((ptr) - (base)) \
241 ta_print_idx(char* p
,
251 sprintf(p
, "%d", idx
);
258 ta_get_segment_index(TA_GlyphHints hints
,
262 TA_AxisHints axis
= &hints
->axis
[dimension
];
263 TA_Point point
= hints
->points
+ point_idx
;
264 TA_Segment segments
= axis
->segments
;
265 TA_Segment limit
= segments
+ axis
->num_segments
;
269 for (segment
= segments
; segment
< limit
; segment
++)
271 if (segment
->first
<= segment
->last
)
273 if (point
>= segment
->first
&& point
<= segment
->last
)
278 TA_Point p
= segment
->first
;
286 if (p
== segment
->last
)
295 if (segment
== limit
)
298 return (int)(segment
- segments
);
303 ta_get_edge_index(TA_GlyphHints hints
,
307 TA_AxisHints axis
= &hints
->axis
[dimension
];
308 TA_Edge edges
= axis
->edges
;
309 TA_Segment segment
= axis
->segments
+ segment_idx
;
312 return segment_idx
== -1 ? -1 : TA_INDEX_NUM(segment
->edge
, edges
);
317 ta_glyph_hints_dump_points(TA_GlyphHints hints
)
319 TA_Point points
= hints
->points
;
320 TA_Point limit
= points
+ hints
->num_points
;
321 TA_Point
* contour
= hints
->contours
;
322 TA_Point
* climit
= contour
+ hints
->num_contours
;
326 TA_LOG(("Table of points:\n"));
328 if (hints
->num_points
)
329 TA_LOG((" index hedge hseg flags"
330 " xorg yorg xscale yscale xfit yfit"));
332 TA_LOG((" (none)\n"));
334 for (point
= points
; point
< limit
; point
++)
336 int point_idx
= TA_INDEX_NUM(point
, points
);
337 int segment_idx_1
= ta_get_segment_index(hints
, point_idx
, 1);
339 char buf1
[16], buf2
[16];
342 /* insert extra newline at the beginning of a contour */
343 if (contour
< climit
&& *contour
== point
)
349 /* we don't show vertical edges since they are never used */
350 TA_LOG((" %5d %5s %5s %s "
351 " %5d %5d %7.2f %7.2f %7.2f %7.2f\n",
354 ta_get_edge_index(hints
, segment_idx_1
, 1)),
355 ta_print_idx(buf2
, segment_idx_1
),
356 (point
->flags
& TA_FLAG_WEAK_INTERPOLATION
) ? "weak" : " -- ",
370 ta_edge_flags_to_string(FT_Byte flags
)
372 static char temp
[32];
376 if (flags
& TA_EDGE_ROUND
)
378 memcpy(temp
+ pos
, "round", 5);
381 if (flags
& TA_EDGE_SERIF
)
385 memcpy(temp
+ pos
, "serif", 5);
397 /* dump the array of linked segments */
400 ta_glyph_hints_dump_segments(TA_GlyphHints hints
)
405 for (dimension
= TA_DEBUG_STARTDIM
;
406 dimension
>= TA_DEBUG_ENDDIM
;
409 TA_AxisHints axis
= &hints
->axis
[dimension
];
410 TA_Point points
= hints
->points
;
411 TA_Edge edges
= axis
->edges
;
412 TA_Segment segments
= axis
->segments
;
413 TA_Segment limit
= segments
+ axis
->num_segments
;
416 char buf1
[16], buf2
[16], buf3
[16];
419 TA_LOG(("Table of %s segments:\n",
420 dimension
== TA_DIMENSION_HORZ
? "vertical"
422 if (axis
->num_segments
)
423 TA_LOG((" index pos dir from to"
425 " height extra flags\n"));
427 TA_LOG((" (none)\n"));
429 for (seg
= segments
; seg
< limit
; seg
++)
430 TA_LOG((" %5d %5.2g %5s %4d %4d"
433 TA_INDEX_NUM(seg
, segments
),
434 dimension
== TA_DIMENSION_HORZ
? (int)seg
->first
->ox
/ 64.0
435 : (int)seg
->first
->oy
/ 64.0,
436 ta_dir_str((TA_Direction
)seg
->dir
),
437 TA_INDEX_NUM(seg
->first
, points
),
438 TA_INDEX_NUM(seg
->last
, points
),
440 ta_print_idx(buf1
, TA_INDEX_NUM(seg
->link
, segments
)),
441 ta_print_idx(buf2
, TA_INDEX_NUM(seg
->serif
, segments
)),
442 ta_print_idx(buf3
, TA_INDEX_NUM(seg
->edge
, edges
)),
445 seg
->height
- (seg
->max_coord
- seg
->min_coord
),
446 ta_edge_flags_to_string(seg
->flags
)));
452 /* dump the array of linked edges */
455 ta_glyph_hints_dump_edges(TA_GlyphHints hints
)
460 for (dimension
= TA_DEBUG_STARTDIM
;
461 dimension
>= TA_DEBUG_ENDDIM
;
464 TA_AxisHints axis
= &hints
->axis
[dimension
];
465 TA_Edge edges
= axis
->edges
;
466 TA_Edge limit
= edges
+ axis
->num_edges
;
469 char buf1
[16], buf2
[16];
472 /* note that TA_DIMENSION_HORZ corresponds to _vertical_ edges */
473 /* since they have a constant X coordinate */
474 TA_LOG(("Table of %s edges:\n",
475 dimension
== TA_DIMENSION_HORZ
? "vertical"
478 TA_LOG((" index pos dir link serif"
479 " blue opos pos flags\n"));
481 TA_LOG((" (none)\n"));
483 for (edge
= edges
; edge
< limit
; edge
++)
484 TA_LOG((" %5d %5.2g %5s %4s %5s"
485 " %c %5.2f %5.2f %11s\n",
486 TA_INDEX_NUM(edge
, edges
),
487 (int)edge
->opos
/ 64.0,
488 ta_dir_str((TA_Direction
)edge
->dir
),
489 ta_print_idx(buf1
, TA_INDEX_NUM(edge
->link
, edges
)),
490 ta_print_idx(buf2
, TA_INDEX_NUM(edge
->serif
, edges
)),
492 edge
->blue_edge
? 'y' : 'n',
495 ta_edge_flags_to_string(edge
->flags
)));
500 #endif /* TA_DEBUG */
503 /* compute the direction value of a given vector */
506 ta_direction_compute(FT_Pos dx
,
509 FT_Pos ll
, ss
; /* long and short arm lengths */
510 TA_Direction dir
; /* candidate direction */
544 /* return no direction if arm lengths do not differ enough */
545 /* (value 14 is heuristic, corresponding to approx. 4.1 degrees); */
546 /* the long arm is never negative */
547 if (ll
<= 14 * TA_ABS(ss
))
555 ta_glyph_hints_init(TA_GlyphHints hints
)
557 /* no need to initialize the embedded items */
558 memset(hints
, 0, sizeof (*hints
) - sizeof (hints
->embedded
));
563 ta_glyph_hints_done(TA_GlyphHints hints
)
571 /* we don't need to free the segment and edge buffers */
572 /* since they are really within the hints->points array */
573 for (dim
= 0; dim
< TA_DIMENSION_MAX
; dim
++)
575 TA_AxisHints axis
= &hints
->axis
[dim
];
578 axis
->num_segments
= 0;
579 axis
->max_segments
= 0;
580 if (axis
->segments
!= axis
->embedded
.segments
)
582 free(axis
->segments
);
583 axis
->segments
= NULL
;
588 if (axis
->edges
!= axis
->embedded
.edges
)
595 if (hints
->contours
!= hints
->embedded
.contours
)
597 free(hints
->contours
);
598 hints
->contours
= NULL
;
600 hints
->max_contours
= 0;
601 hints
->num_contours
= 0;
603 if (hints
->points
!= hints
->embedded
.points
)
606 hints
->points
= NULL
;
608 hints
->max_points
= 0;
609 hints
->num_points
= 0;
616 ta_glyph_hints_rescale(TA_GlyphHints hints
,
617 TA_StyleMetrics metrics
)
619 hints
->metrics
= metrics
;
620 hints
->scaler_flags
= metrics
->scaler
.flags
;
624 /* from FreeType's ftcalc.c */
627 ta_corner_is_flat(FT_Pos in_x
,
635 FT_Pos d_in
, d_out
, d_corner
;
660 return (d_in
+ d_out
- d_corner
) < (d_corner
>> 4);
664 /* recompute all TA_Point in TA_GlyphHints */
665 /* from the definitions in a source outline */
668 ta_glyph_hints_reload(TA_GlyphHints hints
,
671 FT_Error error
= FT_Err_Ok
;
673 FT_UInt old_max
, new_max
;
675 FT_Fixed x_scale
= hints
->x_scale
;
676 FT_Fixed y_scale
= hints
->y_scale
;
677 FT_Pos x_delta
= hints
->x_delta
;
678 FT_Pos y_delta
= hints
->y_delta
;
681 hints
->num_points
= 0;
682 hints
->num_contours
= 0;
684 hints
->axis
[0].num_segments
= 0;
685 hints
->axis
[0].num_edges
= 0;
686 hints
->axis
[1].num_segments
= 0;
687 hints
->axis
[1].num_edges
= 0;
689 /* first of all, reallocate the contours array if necessary */
690 new_max
= (FT_UInt
)outline
->n_contours
;
691 old_max
= (FT_UInt
)hints
->max_contours
;
693 if (new_max
<= TA_CONTOURS_EMBEDDED
)
695 if (hints
->contours
== NULL
)
697 hints
->contours
= hints
->embedded
.contours
;
698 hints
->max_contours
= TA_CONTOURS_EMBEDDED
;
701 else if (new_max
> old_max
)
703 TA_Point
* contours_new
;
706 if (hints
->contours
== hints
->embedded
.contours
)
707 hints
->contours
= NULL
;
709 new_max
= (new_max
+ 3) & ~3U; /* round up to a multiple of 4 */
711 contours_new
= (TA_Point
*)realloc(hints
->contours
,
712 new_max
* sizeof (TA_Point
));
714 return FT_Err_Out_Of_Memory
;
716 hints
->contours
= contours_new
;
717 hints
->max_contours
= (FT_Int
)new_max
;
720 /* reallocate the points arrays if necessary -- we reserve */
721 /* two additional point positions, used to hint metrics appropriately */
722 new_max
= (FT_UInt
)(outline
->n_points
+ 2);
723 old_max
= (FT_UInt
)hints
->max_points
;
725 if (new_max
<= TA_POINTS_EMBEDDED
)
727 if (hints
->points
== NULL
)
729 hints
->points
= hints
->embedded
.points
;
730 hints
->max_points
= TA_POINTS_EMBEDDED
;
733 else if (new_max
> old_max
)
738 if (hints
->points
== hints
->embedded
.points
)
739 hints
->points
= NULL
;
741 new_max
= (new_max
+ 2 + 7) & ~7U; /* round up to a multiple of 8 */
743 points_new
= (TA_Point
)realloc(hints
->points
,
744 new_max
* sizeof (TA_PointRec
));
746 return FT_Err_Out_Of_Memory
;
748 hints
->points
= points_new
;
749 hints
->max_points
= (FT_Int
)new_max
;
752 hints
->num_points
= outline
->n_points
;
753 hints
->num_contours
= outline
->n_contours
;
755 /* we can't rely on the value of `FT_Outline.flags' to know the fill */
756 /* direction used for a glyph, given that some fonts are broken */
757 /* (e.g. the Arphic ones); we thus recompute it each time we need to */
759 hints
->axis
[TA_DIMENSION_HORZ
].major_dir
= TA_DIR_UP
;
760 hints
->axis
[TA_DIMENSION_VERT
].major_dir
= TA_DIR_LEFT
;
762 if (FT_Outline_Get_Orientation(outline
) == FT_ORIENTATION_POSTSCRIPT
)
764 hints
->axis
[TA_DIMENSION_HORZ
].major_dir
= TA_DIR_DOWN
;
765 hints
->axis
[TA_DIMENSION_VERT
].major_dir
= TA_DIR_RIGHT
;
768 hints
->x_scale
= x_scale
;
769 hints
->y_scale
= y_scale
;
770 hints
->x_delta
= x_delta
;
771 hints
->y_delta
= y_delta
;
773 hints
->xmin_delta
= 0;
774 hints
->xmax_delta
= 0;
776 points
= hints
->points
;
777 if (hints
->num_points
== 0)
782 TA_Point point_limit
= points
+ hints
->num_points
;
785 /* compute coordinates & Bezier flags, next and prev */
787 FT_Vector
* vec
= outline
->points
;
788 char* tag
= outline
->tags
;
790 TA_Point end
= points
+ outline
->contours
[0];
793 FT_Int contour_index
= 0;
796 for (point
= points
; point
< point_limit
; point
++, vec
++, tag
++)
798 point
->in_dir
= (FT_Char
)TA_DIR_NONE
;
799 point
->out_dir
= (FT_Char
)TA_DIR_NONE
;
801 point
->fx
= (FT_Short
)vec
->x
;
802 point
->fy
= (FT_Short
)vec
->y
;
803 point
->ox
= point
->x
= FT_MulFix(vec
->x
, x_scale
) + x_delta
;
804 point
->oy
= point
->y
= FT_MulFix(vec
->y
, y_scale
) + y_delta
;
806 switch (FT_CURVE_TAG(*tag
))
808 case FT_CURVE_TAG_CONIC
:
809 point
->flags
= TA_FLAG_CONIC
;
811 case FT_CURVE_TAG_CUBIC
:
812 point
->flags
= TA_FLAG_CUBIC
;
815 point
->flags
= TA_FLAG_NONE
;
824 if (++contour_index
< outline
->n_contours
)
826 end
= points
+ outline
->contours
[contour_index
];
833 /* set up the contours array */
835 TA_Point
* contour
= hints
->contours
;
836 TA_Point
* contour_limit
= contour
+ hints
->num_contours
;
838 short* end
= outline
->contours
;
842 for (; contour
< contour_limit
; contour
++, end
++)
844 contour
[0] = points
+ idx
;
845 idx
= (short)(end
[0] + 1);
851 * Compute directions of `in' and `out' vectors.
853 * Note that distances between points that are very near to each
854 * other are accumulated. In other words, the auto-hinter
855 * prepends the small vectors between near points to the first
856 * non-near vector. All intermediate points are tagged as
857 * weak; the directions are adjusted also to be equal to the
861 /* value 20 in `near_limit' is heuristic */
862 FT_UInt units_per_em
= hints
->metrics
->scaler
.face
->units_per_EM
;
863 FT_Int near_limit
= 20 * units_per_em
/ 2048;
864 FT_Int near_limit2
= 2 * near_limit
- 1;
867 TA_Point
* contour_limit
= hints
->contours
+ hints
->num_contours
;
870 for (contour
= hints
->contours
; contour
< contour_limit
; contour
++)
872 TA_Point first
= *contour
;
873 TA_Point next
, prev
, curr
;
878 /* since the first point of a contour could be part of a */
879 /* series of near points, go backwards to find the first */
880 /* non-near point and adjust `first' */
885 while (prev
!= first
)
887 out_x
= point
->fx
- prev
->fx
;
888 out_y
= point
->fy
- prev
->fy
;
891 * We use Taxicab metrics to measure the vector length.
893 * Note that the accumulated distances so far could have the
894 * opposite direction of the distance measured here. For this
895 * reason we use `near_limit2' for the comparison to get a
896 * non-near point even in the worst case.
898 if (TA_ABS(out_x
) + TA_ABS(out_y
) >= near_limit2
)
905 /* adjust first point */
908 /* now loop over all points of the contour to get */
909 /* `in' and `out' vector directions */
914 * We abuse the `u' and `v' fields to store index deltas to the
915 * next and previous non-near point, respectively.
917 * To avoid problems with not having non-near points, we point to
918 * `first' by default as the next non-near point.
920 curr
->u
= (FT_Pos
)(first
- curr
);
929 TA_Direction out_dir
;
935 out_x
+= next
->fx
- point
->fx
;
936 out_y
+= next
->fy
- point
->fy
;
938 if (TA_ABS(out_x
) + TA_ABS(out_y
) < near_limit
)
940 next
->flags
|= TA_FLAG_WEAK_INTERPOLATION
;
944 curr
->u
= (FT_Pos
)(next
- curr
);
947 out_dir
= ta_direction_compute(out_x
, out_y
);
949 /* adjust directions for all points inbetween; */
950 /* the loop also updates position of `curr' */
951 curr
->out_dir
= (FT_Char
)out_dir
;
952 for (curr
= curr
->next
; curr
!= next
; curr
= curr
->next
)
954 curr
->in_dir
= (FT_Char
)out_dir
;
955 curr
->out_dir
= (FT_Char
)out_dir
;
957 next
->in_dir
= (FT_Char
)out_dir
;
959 curr
->u
= (FT_Pos
)(first
- curr
);
965 } while (next
!= first
);
969 * The next step is to `simplify' an outline's topology so that we
970 * can identify local extrema more reliably: A series of
971 * non-horizontal or non-vertical vectors pointing into the same
972 * quadrant are handled as a single, long vector. From a
973 * topological point of the view, the intermediate points are of no
974 * interest and thus tagged as weak.
977 for (point
= points
; point
< point_limit
; point
++)
979 if (point
->flags
& TA_FLAG_WEAK_INTERPOLATION
)
982 if (point
->in_dir
== TA_DIR_NONE
983 && point
->out_dir
== TA_DIR_NONE
)
985 /* check whether both vectors point into the same quadrant */
990 TA_Point next_u
= point
+ point
->u
;
991 TA_Point prev_v
= point
+ point
->v
;
994 in_x
= point
->fx
- prev_v
->fx
;
995 in_y
= point
->fy
- prev_v
->fy
;
997 out_x
= next_u
->fx
- point
->fx
;
998 out_y
= next_u
->fy
- point
->fy
;
1000 if ((in_x
^ out_x
) >= 0 && (in_y
^ out_y
) >= 0)
1002 /* yes, so tag current point as weak */
1003 /* and update index deltas */
1005 point
->flags
|= TA_FLAG_WEAK_INTERPOLATION
;
1007 prev_v
->u
= (FT_Pos
)(next_u
- prev_v
);
1008 next_u
->v
= -prev_v
->u
;
1014 * Finally, check for remaining weak points. Everything else not
1015 * collected in edges so far is then implicitly classified as strong
1019 for (point
= points
; point
< point_limit
; point
++)
1021 if (point
->flags
& TA_FLAG_WEAK_INTERPOLATION
)
1024 if (point
->flags
& TA_FLAG_CONTROL
)
1026 /* control points are always weak */
1028 point
->flags
|= TA_FLAG_WEAK_INTERPOLATION
;
1030 else if (point
->out_dir
== point
->in_dir
)
1032 if (point
->out_dir
!= TA_DIR_NONE
)
1034 /* current point lies on a horizontal or */
1035 /* vertical segment (but doesn't start or end it) */
1040 TA_Point next_u
= point
+ point
->u
;
1041 TA_Point prev_v
= point
+ point
->v
;
1044 if (ta_corner_is_flat(point
->fx
- prev_v
->fx
,
1045 point
->fy
- prev_v
->fy
,
1046 next_u
->fx
- point
->fx
,
1047 next_u
->fy
- point
->fy
))
1049 /* either the `in' or the `out' vector is much more */
1050 /* dominant than the other one, so tag current point */
1051 /* as weak and update index deltas */
1053 prev_v
->u
= (FT_Pos
)(next_u
- prev_v
);
1054 next_u
->v
= -prev_v
->u
;
1060 else if (point
->in_dir
== -point
->out_dir
)
1062 /* current point forms a spike */
1069 /* change some directions at the user's request */
1070 /* to make ttfautohint insert one-point segments */
1071 /* or remove points from segments */
1080 /* `globals' is not set up while initializing metrics, */
1081 /* so exit early in this case */
1082 if (!hints
->metrics
->globals
)
1085 font
= hints
->metrics
->globals
->font
;
1087 /* start conditions are set with `TA_control_segment_dir_collect' */
1088 while (TA_control_segment_dir_get_next(font
, &idx
, &dir
,
1089 &left_offset
, &right_offset
))
1091 TA_Point point
= &points
[idx
];
1094 point
->out_dir
= dir
;
1095 if (dir
== TA_DIR_NONE
)
1096 point
->flags
|= TA_FLAG_WEAK_INTERPOLATION
;
1098 point
->flags
&= ~TA_FLAG_WEAK_INTERPOLATION
;
1099 point
->left_offset
= (FT_Short
)left_offset
;
1100 point
->right_offset
= (FT_Short
)right_offset
;
1109 /* store the hinted outline in an FT_Outline structure */
1112 ta_glyph_hints_save(TA_GlyphHints hints
,
1113 FT_Outline
* outline
)
1115 TA_Point point
= hints
->points
;
1116 TA_Point limit
= point
+ hints
->num_points
;
1118 FT_Vector
* vec
= outline
->points
;
1119 char* tag
= outline
->tags
;
1122 for (; point
< limit
; point
++, vec
++, tag
++)
1127 if (point
->flags
& TA_FLAG_CONIC
)
1128 tag
[0] = FT_CURVE_TAG_CONIC
;
1129 else if (point
->flags
& TA_FLAG_CUBIC
)
1130 tag
[0] = FT_CURVE_TAG_CUBIC
;
1132 tag
[0] = FT_CURVE_TAG_ON
;
1137 /****************************************************************
1139 * EDGE POINT GRID-FITTING
1141 ****************************************************************/
1144 /* align all points of an edge to the same coordinate value, */
1145 /* either horizontally or vertically */
1148 ta_glyph_hints_align_edge_points(TA_GlyphHints hints
,
1151 TA_AxisHints axis
= &hints
->axis
[dim
];
1152 TA_Segment segments
= axis
->segments
;
1153 TA_Segment segment_limit
= segments
+ axis
->num_segments
;
1157 if (dim
== TA_DIMENSION_HORZ
)
1159 for (seg
= segments
; seg
< segment_limit
; seg
++)
1161 TA_Edge edge
= seg
->edge
;
1162 TA_Point point
, first
, last
;
1173 point
->x
= edge
->pos
;
1174 point
->flags
|= TA_FLAG_TOUCH_X
;
1179 point
= point
->next
;
1185 for (seg
= segments
; seg
< segment_limit
; seg
++)
1187 TA_Edge edge
= seg
->edge
;
1188 TA_Point point
, first
, last
;
1199 point
->y
= edge
->pos
;
1200 point
->flags
|= TA_FLAG_TOUCH_Y
;
1205 point
= point
->next
;
1212 /****************************************************************
1214 * STRONG POINT INTERPOLATION
1216 ****************************************************************/
1219 /* hint the strong points -- */
1220 /* this is equivalent to the TrueType `IP' hinting instruction */
1223 ta_glyph_hints_align_strong_points(TA_GlyphHints hints
,
1226 TA_Point points
= hints
->points
;
1227 TA_Point point_limit
= points
+ hints
->num_points
;
1229 TA_AxisHints axis
= &hints
->axis
[dim
];
1231 TA_Edge edges
= axis
->edges
;
1232 TA_Edge edge_limit
= edges
+ axis
->num_edges
;
1234 FT_UShort touch_flag
;
1237 if (dim
== TA_DIMENSION_HORZ
)
1238 touch_flag
= TA_FLAG_TOUCH_X
;
1240 touch_flag
= TA_FLAG_TOUCH_Y
;
1242 if (edges
< edge_limit
)
1248 for (point
= points
; point
< point_limit
; point
++)
1250 FT_Pos u
, ou
, fu
; /* point position */
1254 if (point
->flags
& touch_flag
)
1257 /* if this point is candidate to weak interpolation, we */
1258 /* interpolate it after all strong points have been processed */
1260 if ((point
->flags
& TA_FLAG_WEAK_INTERPOLATION
))
1263 if (dim
== TA_DIMENSION_VERT
)
1276 /* is the point before the first edge? */
1278 delta
= edge
->fpos
- u
;
1281 u
= edge
->pos
- (edge
->opos
- ou
);
1283 if (hints
->recorder
)
1284 hints
->recorder(ta_ip_before
, hints
, dim
,
1285 point
, NULL
, NULL
, NULL
, NULL
);
1290 /* is the point after the last edge? */
1291 edge
= edge_limit
- 1;
1292 delta
= u
- edge
->fpos
;
1295 u
= edge
->pos
+ (ou
- edge
->opos
);
1297 if (hints
->recorder
)
1298 hints
->recorder(ta_ip_after
, hints
, dim
,
1299 point
, NULL
, NULL
, NULL
, NULL
);
1305 FT_PtrDist min
, max
, mid
;
1309 /* find enclosing edges */
1311 max
= edge_limit
- edges
;
1313 /* for a small number of edges, a linear search is better */
1319 for (nn
= 0; nn
< max
; nn
++)
1320 if (edges
[nn
].fpos
>= u
)
1323 if (edges
[nn
].fpos
== u
)
1327 if (hints
->recorder
)
1328 hints
->recorder(ta_ip_on
, hints
, dim
,
1329 point
, &edges
[nn
], NULL
, NULL
, NULL
);
1338 mid
= (max
+ min
) >> 1;
1348 /* we are on the edge */
1351 if (hints
->recorder
)
1352 hints
->recorder(ta_ip_on
, hints
, dim
,
1353 point
, edge
, NULL
, NULL
, NULL
);
1359 /* point is not on an edge */
1361 TA_Edge before
= edges
+ min
- 1;
1362 TA_Edge after
= edges
+ min
+ 0;
1365 /* assert(before && after && before != after) */
1366 if (before
->scale
== 0)
1367 before
->scale
= FT_DivFix(after
->pos
- before
->pos
,
1368 after
->fpos
- before
->fpos
);
1370 u
= before
->pos
+ FT_MulFix(fu
- before
->fpos
,
1373 if (hints
->recorder
)
1374 hints
->recorder(ta_ip_between
, hints
, dim
,
1375 point
, before
, after
, NULL
, NULL
);
1380 /* save the point position */
1381 if (dim
== TA_DIMENSION_HORZ
)
1386 point
->flags
|= touch_flag
;
1392 /****************************************************************
1394 * WEAK POINT INTERPOLATION
1396 ****************************************************************/
1399 /* shift the original coordinates of all points between `p1' and */
1400 /* `p2' to get hinted coordinates, using the same difference as */
1401 /* given by `ref' */
1404 ta_iup_shift(TA_Point p1
,
1409 FT_Pos delta
= ref
->u
- ref
->v
;
1415 for (p
= p1
; p
< ref
; p
++)
1416 p
->u
= p
->v
+ delta
;
1418 for (p
= ref
+ 1; p
<= p2
; p
++)
1419 p
->u
= p
->v
+ delta
;
1423 /* interpolate the original coordinates of all points between `p1' and */
1424 /* `p2' to get hinted coordinates, using `ref1' and `ref2' as the */
1425 /* reference points; the `u' and `v' members are the current and */
1426 /* original coordinate values, respectively. */
1428 /* details can be found in the TrueType bytecode specification */
1431 ta_iup_interp(TA_Point p1
,
1437 FT_Pos u
, v1
, v2
, u1
, u2
, d1
, d2
;
1443 if (ref1
->v
> ref2
->v
)
1457 if (u1
== u2
|| v1
== v2
)
1459 for (p
= p1
; p
<= p2
; p
++)
1475 FT_Fixed scale
= FT_DivFix(u2
- u1
, v2
- v1
);
1478 for (p
= p1
; p
<= p2
; p
++)
1487 u
= u1
+ FT_MulFix(u
- v1
, scale
);
1495 /* hint the weak points -- */
1496 /* this is equivalent to the TrueType `IUP' hinting instruction */
1499 ta_glyph_hints_align_weak_points(TA_GlyphHints hints
,
1502 TA_Point points
= hints
->points
;
1503 TA_Point point_limit
= points
+ hints
->num_points
;
1505 TA_Point
* contour
= hints
->contours
;
1506 TA_Point
* contour_limit
= contour
+ hints
->num_contours
;
1508 FT_UShort touch_flag
;
1511 TA_Point first_point
;
1514 /* pass 1: move segment points to edge positions */
1516 if (dim
== TA_DIMENSION_HORZ
)
1518 touch_flag
= TA_FLAG_TOUCH_X
;
1520 for (point
= points
; point
< point_limit
; point
++)
1522 point
->u
= point
->x
;
1523 point
->v
= point
->ox
;
1528 touch_flag
= TA_FLAG_TOUCH_Y
;
1530 for (point
= points
; point
< point_limit
; point
++)
1532 point
->u
= point
->y
;
1533 point
->v
= point
->oy
;
1537 for (; contour
< contour_limit
; contour
++)
1539 TA_Point first_touched
, last_touched
;
1543 end_point
= point
->prev
;
1544 first_point
= point
;
1546 /* find first touched point */
1549 if (point
> end_point
) /* no touched point in contour */
1552 if (point
->flags
& touch_flag
)
1558 first_touched
= point
;
1562 /* skip any touched neighbours */
1563 while (point
< end_point
1564 && (point
[1].flags
& touch_flag
) != 0)
1567 last_touched
= point
;
1569 /* find the next touched point, if any */
1573 if (point
> end_point
)
1576 if ((point
->flags
& touch_flag
) != 0)
1582 /* interpolate between last_touched and point */
1583 ta_iup_interp(last_touched
+ 1, point
- 1,
1584 last_touched
, point
);
1588 /* special case: only one point was touched */
1589 if (last_touched
== first_touched
)
1590 ta_iup_shift(first_point
, end_point
, first_touched
);
1592 else /* interpolate the last part */
1594 if (last_touched
< end_point
)
1595 ta_iup_interp(last_touched
+ 1, end_point
,
1596 last_touched
, first_touched
);
1598 if (first_touched
> points
)
1599 ta_iup_interp(first_point
, first_touched
- 1,
1600 last_touched
, first_touched
);
1607 /* now save the interpolated values back to x/y */
1608 if (dim
== TA_DIMENSION_HORZ
)
1610 for (point
= points
; point
< point_limit
; point
++)
1611 point
->x
= point
->u
;
1615 for (point
= points
; point
< point_limit
; point
++)
1616 point
->y
= point
->u
;
1621 #ifdef TA_CONFIG_OPTION_USE_WARPER
1623 /* apply (small) warp scale and warp delta for given dimension */
1626 ta_glyph_hints_scale_dim(TA_GlyphHints hints
,
1631 TA_Point points
= hints
->points
;
1632 TA_Point points_limit
= points
+ hints
->num_points
;
1636 if (dim
== TA_DIMENSION_HORZ
)
1638 for (point
= points
; point
< points_limit
; point
++)
1639 point
->x
= FT_MulFix(point
->fx
, scale
) + delta
;
1643 for (point
= points
; point
< points_limit
; point
++)
1644 point
->y
= FT_MulFix(point
->fy
, scale
) + delta
;
1648 #endif /* TA_CONFIG_OPTION_USE_WARPER */
1650 /* end of tahints.c */