summaryrefslogtreecommitdiffstats
path: root/src/autofit/aflatin.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/autofit/aflatin.c')
-rw-r--r--src/autofit/aflatin.c468
1 files changed, 391 insertions, 77 deletions
diff --git a/src/autofit/aflatin.c b/src/autofit/aflatin.c
index 9c9f370..db7aaff 100644
--- a/src/autofit/aflatin.c
+++ b/src/autofit/aflatin.c
@@ -4,7 +4,7 @@
/* */
/* Auto-fitter hinting routines for latin writing system (body). */
/* */
-/* Copyright 2003-2015 by */
+/* Copyright 2003-2016 by */
/* David Turner, Robert Wilhelm, and Werner Lemberg. */
/* */
/* This file is part of the FreeType project, and may only be used, */
@@ -95,7 +95,7 @@
const char* p;
#ifdef FT_DEBUG_LEVEL_TRACE
- FT_ULong ch;
+ FT_ULong ch = 0;
#endif
p = script_class->standard_charstring;
@@ -1076,7 +1076,7 @@
FT_UInt ppem;
- scaled = FT_MulFix( blue->shoot.org, scaler->y_scale );
+ scaled = FT_MulFix( blue->shoot.org, scale );
ppem = metrics->root.scaler.face->size->metrics.x_ppem;
limit = metrics->root.globals->increase_x_height;
threshold = 40;
@@ -1123,8 +1123,6 @@
if ( dist == 0 )
{
- scale = new_scale;
-
FT_TRACE5((
"af_latin_metrics_scale_dim:"
" x height alignment (style `%s'):\n"
@@ -1132,9 +1130,11 @@
" vertical scaling changed from %.4f to %.4f (by %d%%)\n"
"\n",
af_style_names[metrics->root.style_class->style],
- axis->org_scale / 65536.0,
scale / 65536.0,
+ new_scale / 65536.0,
( fitted - scaled ) * 100 / scaled ));
+
+ scale = new_scale;
}
#ifdef FT_DEBUG_LEVEL_TRACE
else
@@ -1436,19 +1436,36 @@
/* do each contour separately */
for ( ; contour < contour_limit; contour++ )
{
- AF_Point point = contour[0];
- AF_Point last = point->prev;
- int on_edge = 0;
- FT_Pos min_pos = 32000; /* minimum segment pos != min_coord */
- FT_Pos max_pos = -32000; /* maximum segment pos != max_coord */
- FT_Pos min_on_pos = 32000;
- FT_Pos max_on_pos = -32000;
- FT_Bool passed;
+ AF_Point point = contour[0];
+ AF_Point last = point->prev;
+ int on_edge = 0;
+
+ /* we call values measured along a segment (point->v) */
+ /* `coordinates', and values orthogonal to it (point->u) */
+ /* `positions' */
+ FT_Pos min_pos = 32000;
+ FT_Pos max_pos = -32000;
+ FT_Pos min_coord = 32000;
+ FT_Pos max_coord = -32000;
+ FT_UShort min_flags = AF_FLAG_NONE;
+ FT_UShort max_flags = AF_FLAG_NONE;
+ FT_Pos min_on_coord = 32000;
+ FT_Pos max_on_coord = -32000;
+
+ FT_Bool passed;
+
+ AF_Segment prev_segment = NULL;
+
+ FT_Pos prev_min_pos = min_pos;
+ FT_Pos prev_max_pos = max_pos;
+ FT_Pos prev_min_coord = min_coord;
+ FT_Pos prev_max_coord = max_coord;
+ FT_UShort prev_min_flags = min_flags;
+ FT_UShort prev_max_flags = max_flags;
+ FT_Pos prev_min_on_coord = min_on_coord;
+ FT_Pos prev_max_on_coord = max_on_coord;
- if ( point == last ) /* skip singletons -- just in case */
- continue;
-
if ( FT_ABS( last->out_dir ) == major_dir &&
FT_ABS( point->out_dir ) == major_dir )
{
@@ -1478,51 +1495,182 @@
if ( on_edge )
{
+ /* get minimum and maximum position */
u = point->u;
if ( u < min_pos )
min_pos = u;
if ( u > max_pos )
max_pos = u;
- /* get minimum and maximum coordinate of on points */
+ /* get minimum and maximum coordinate together with flags */
+ v = point->v;
+ if ( v < min_coord )
+ {
+ min_coord = v;
+ min_flags = point->flags;
+ }
+ if ( v > max_coord )
+ {
+ max_coord = v;
+ max_flags = point->flags;
+ }
+
+ /* get minimum and maximum coordinate of `on' points */
if ( !( point->flags & AF_FLAG_CONTROL ) )
{
v = point->v;
- if ( v < min_on_pos )
- min_on_pos = v;
- if ( v > max_on_pos )
- max_on_pos = v;
+ if ( v < min_on_coord )
+ min_on_coord = v;
+ if ( v > max_on_coord )
+ max_on_coord = v;
}
if ( point->out_dir != segment_dir || point == last )
{
- /* we are just leaving an edge; record a new segment! */
- segment->last = point;
- segment->pos = (FT_Short)( ( min_pos + max_pos ) >> 1 );
-
- /* a segment is round if either its first or last point */
- /* is a control point, and the length of the on points */
- /* inbetween doesn't exceed a heuristic limit */
- if ( ( segment->first->flags | point->flags ) & AF_FLAG_CONTROL &&
- ( max_on_pos - min_on_pos ) < flat_threshold )
- segment->flags |= AF_EDGE_ROUND;
+ /* check whether the new segment's start point is identical to */
+ /* the previous segment's end point; for example, this might */
+ /* happen for spikes */
- /* compute segment size */
- min_pos = max_pos = point->v;
+ if ( !prev_segment || segment->first != prev_segment->last )
+ {
+ /* points are different: we are just leaving an edge, thus */
+ /* record a new segment */
+
+ segment->last = point;
+ segment->pos = (FT_Short)( ( min_pos + max_pos ) >> 1 );
+ segment->delta = (FT_Short)( ( max_pos - min_pos ) >> 1 );
+
+ /* a segment is round if either its first or last point */
+ /* is a control point, and the length of the on points */
+ /* inbetween doesn't exceed a heuristic limit */
+ if ( ( min_flags | max_flags ) & AF_FLAG_CONTROL &&
+ ( max_on_coord - min_on_coord ) < flat_threshold )
+ segment->flags |= AF_EDGE_ROUND;
+
+ segment->min_coord = (FT_Short)min_coord;
+ segment->max_coord = (FT_Short)max_coord;
+ segment->height = segment->max_coord - segment->min_coord;
+
+ prev_segment = segment;
+ prev_min_pos = min_pos;
+ prev_max_pos = max_pos;
+ prev_min_coord = min_coord;
+ prev_max_coord = max_coord;
+ prev_min_flags = min_flags;
+ prev_max_flags = max_flags;
+ prev_min_on_coord = min_on_coord;
+ prev_max_on_coord = max_on_coord;
+ }
+ else
+ {
+ /* points are the same: we don't create a new segment but */
+ /* merge the current segment with the previous one */
+
+ if ( prev_segment->last->in_dir == point->in_dir )
+ {
+ /* we have identical directions (this can happen for */
+ /* degenerate outlines that move zig-zag along the main */
+ /* axis without changing the coordinate value of the other */
+ /* axis, and where the segments have just been merged): */
+ /* unify segments */
+
+ /* update constraints */
+
+ if ( prev_min_pos < min_pos )
+ min_pos = prev_min_pos;
+ if ( prev_max_pos > max_pos )
+ max_pos = prev_max_pos;
+
+ if ( prev_min_coord < min_coord )
+ {
+ min_coord = prev_min_coord;
+ min_flags = prev_min_flags;
+ }
+ if ( prev_max_coord > max_coord )
+ {
+ max_coord = prev_max_coord;
+ max_flags = prev_max_flags;
+ }
- v = segment->first->v;
- if ( v < min_pos )
- min_pos = v;
- if ( v > max_pos )
- max_pos = v;
+ if ( prev_min_on_coord < min_on_coord )
+ min_on_coord = prev_min_on_coord;
+ if ( prev_max_on_coord > max_on_coord )
+ max_on_coord = prev_max_on_coord;
+
+ prev_segment->last = point;
+ prev_segment->pos = (FT_Short)( ( min_pos +
+ max_pos ) >> 1 );
+
+ if ( ( min_flags | max_flags ) & AF_FLAG_CONTROL &&
+ ( max_on_coord - min_on_coord ) < flat_threshold )
+ prev_segment->flags |= AF_EDGE_ROUND;
+ else
+ prev_segment->flags &= ~AF_EDGE_ROUND;
+
+ prev_segment->min_coord = (FT_Short)min_coord;
+ prev_segment->max_coord = (FT_Short)max_coord;
+ prev_segment->height = prev_segment->max_coord -
+ prev_segment->min_coord;
+ }
+ else
+ {
+ /* we have different directions; use the properties of the */
+ /* longer segment and discard the other one */
- segment->min_coord = (FT_Short)min_pos;
- segment->max_coord = (FT_Short)max_pos;
- segment->height = (FT_Short)( segment->max_coord -
- segment->min_coord );
+ if ( FT_ABS( prev_max_coord - prev_min_coord ) >
+ FT_ABS( max_coord - min_coord ) )
+ {
+ /* discard current segment */
+
+ if ( min_pos < prev_min_pos )
+ prev_min_pos = min_pos;
+ if ( max_pos > prev_max_pos )
+ prev_max_pos = max_pos;
+
+ prev_segment->last = point;
+ prev_segment->pos = (FT_Short)( ( prev_min_pos +
+ prev_max_pos ) >> 1 );
+ }
+ else
+ {
+ /* discard previous segment */
+
+ if ( prev_min_pos < min_pos )
+ min_pos = prev_min_pos;
+ if ( prev_max_pos > max_pos )
+ max_pos = prev_max_pos;
+
+ segment->last = point;
+ segment->pos = (FT_Short)( ( min_pos + max_pos ) >> 1 );
+
+ if ( ( min_flags | max_flags ) & AF_FLAG_CONTROL &&
+ ( max_on_coord - min_on_coord ) < flat_threshold )
+ segment->flags |= AF_EDGE_ROUND;
+
+ segment->min_coord = (FT_Short)min_coord;
+ segment->max_coord = (FT_Short)max_coord;
+ segment->height = segment->max_coord -
+ segment->min_coord;
+
+ *prev_segment = *segment;
+
+ prev_min_pos = min_pos;
+ prev_max_pos = max_pos;
+ prev_min_coord = min_coord;
+ prev_max_coord = max_coord;
+ prev_min_flags = min_flags;
+ prev_max_flags = max_flags;
+ prev_min_on_coord = min_on_coord;
+ prev_max_on_coord = max_on_coord;
+ }
+ }
+
+ axis->num_segments--;
+ }
on_edge = 0;
segment = NULL;
+
/* fall through */
}
}
@@ -1535,7 +1683,12 @@
passed = 1;
}
- if ( !on_edge && FT_ABS( point->out_dir ) == major_dir )
+ /* if we are not on an edge, check whether the major direction */
+ /* coincides with the current point's `out' direction, or */
+ /* whether we have a single-point contour */
+ if ( !on_edge &&
+ ( FT_ABS( point->out_dir ) == major_dir ||
+ point == point->prev ) )
{
/* this is the start of a new segment! */
segment_dir = (AF_Direction)point->out_dir;
@@ -1551,17 +1704,42 @@
segment->first = point;
segment->last = point;
- min_pos = max_pos = point->u;
+ /* `af_axis_hints_new_segment' reallocates memory, */
+ /* thus we have to refresh the `prev_segment' pointer */
+ if ( prev_segment )
+ prev_segment = segment - 1;
+
+ min_pos = max_pos = point->u;
+ min_coord = max_coord = point->v;
+ min_flags = max_flags = point->flags;
if ( point->flags & AF_FLAG_CONTROL )
{
- min_on_pos = 32000;
- max_on_pos = -32000;
+ min_on_coord = 32000;
+ max_on_coord = -32000;
}
else
- min_on_pos = max_on_pos = point->v;
+ min_on_coord = max_on_coord = point->v;
on_edge = 1;
+
+ if ( point == point->prev )
+ {
+ /* we have a one-point segment: this is a one-point */
+ /* contour with `in' and `out' direction set to */
+ /* AF_DIR_NONE */
+ segment->pos = (FT_Short)min_pos;
+
+ if (point->flags & AF_FLAG_CONTROL)
+ segment->flags |= AF_EDGE_ROUND;
+
+ segment->min_coord = (FT_Short)point->v;
+ segment->max_coord = (FT_Short)point->v;
+ segment->height = 0;
+
+ on_edge = 0;
+ segment = NULL;
+ }
}
point = point->next;
@@ -1773,6 +1951,12 @@
FT_Memory memory = hints->memory;
AF_LatinAxis laxis = &((AF_LatinMetrics)hints->metrics)->axis[dim];
+ AF_StyleClass style_class = hints->metrics->style_class;
+ AF_ScriptClass script_class = AF_SCRIPT_CLASSES_GET
+ [style_class->script];
+
+ FT_Bool top_to_bottom_hinting = 0;
+
AF_Segment segments = axis->segments;
AF_Segment segment_limit = segments + axis->num_segments;
AF_Segment seg;
@@ -1783,6 +1967,7 @@
FT_Fixed scale;
FT_Pos edge_distance_threshold;
FT_Pos segment_length_threshold;
+ FT_Pos segment_width_threshold;
axis->num_edges = 0;
@@ -1795,15 +1980,24 @@
: AF_DIR_RIGHT;
#endif
+ if ( dim == AF_DIMENSION_VERT )
+ top_to_bottom_hinting = script_class->top_to_bottom_hinting;
+
/*
* We ignore all segments that are less than 1 pixel in length
* to avoid many problems with serif fonts. We compute the
* corresponding threshold in font units.
*/
if ( dim == AF_DIMENSION_HORZ )
- segment_length_threshold = FT_DivFix( 64, hints->y_scale );
+ segment_length_threshold = FT_DivFix( 64, hints->y_scale );
else
- segment_length_threshold = 0;
+ segment_length_threshold = 0;
+
+ /*
+ * Similarly, we ignore segments that have a width delta
+ * larger than 0.5px (i.e., a width larger than 1px).
+ */
+ segment_width_threshold = FT_DivFix( 32, scale );
/*********************************************************************/
/* */
@@ -1836,7 +2030,11 @@
FT_Int ee;
- if ( seg->height < segment_length_threshold )
+ /* ignore too short segments, too wide ones, and, in this loop, */
+ /* one-point segments without a direction */
+ if ( seg->height < segment_length_threshold ||
+ seg->delta > segment_width_threshold ||
+ seg->dir == AF_DIR_NONE )
continue;
/* A special case for serif edges: If they are smaller than */
@@ -1872,6 +2070,7 @@
/* sort according to the position */
error = af_axis_hints_new_edge( axis, seg->pos,
(AF_Direction)seg->dir,
+ top_to_bottom_hinting,
memory, &edge );
if ( error )
goto Exit;
@@ -1897,6 +2096,44 @@
}
}
+ /* we loop again over all segments to catch one-point segments */
+ /* without a direction: if possible, link them to existing edges */
+ for ( seg = segments; seg < segment_limit; seg++ )
+ {
+ AF_Edge found = NULL;
+ FT_Int ee;
+
+
+ if ( seg->dir != AF_DIR_NONE )
+ continue;
+
+ /* look for an edge corresponding to the segment */
+ for ( ee = 0; ee < axis->num_edges; ee++ )
+ {
+ AF_Edge edge = axis->edges + ee;
+ FT_Pos dist;
+
+
+ dist = seg->pos - edge->fpos;
+ if ( dist < 0 )
+ dist = -dist;
+
+ if ( dist < edge_distance_threshold )
+ {
+ found = edge;
+ break;
+ }
+ }
+
+ /* one-point segments without a match are ignored */
+ if ( found )
+ {
+ seg->edge_next = found->first;
+ found->last->edge_next = seg;
+ found->last = seg;
+ }
+ }
+
/******************************************************************/
/* */
@@ -2337,6 +2574,7 @@
af_latin_compute_stem_width( AF_GlyphHints hints,
AF_Dimension dim,
FT_Pos width,
+ FT_Pos base_delta,
FT_UInt base_flags,
FT_UInt stem_flags )
{
@@ -2414,7 +2652,39 @@
dist += delta;
}
else
- dist = ( dist + 32 ) & ~63;
+ {
+ /* A stem's end position depends on two values: the start */
+ /* position and the stem length. The former gets usually */
+ /* rounded to the grid, while the latter gets rounded also if it */
+ /* exceeds a certain length (see below in this function). This */
+ /* `double rounding' can lead to a great difference to the */
+ /* original, unhinted position; this normally doesn't matter for */
+ /* large PPEM values, but for small sizes it can easily make */
+ /* outlines collide. For this reason, we adjust the stem length */
+ /* by a small amount depending on the PPEM value in case the */
+ /* former and latter rounding both point into the same */
+ /* direction. */
+
+ FT_Pos bdelta = 0;
+
+
+ if ( ( ( width > 0 ) && ( base_delta > 0 ) ) ||
+ ( ( width < 0 ) && ( base_delta < 0 ) ) )
+ {
+ FT_UInt ppem = metrics->root.scaler.face->size->metrics.x_ppem;
+
+
+ if ( ppem < 10 )
+ bdelta = base_delta;
+ else if ( ppem < 30 )
+ bdelta = ( base_delta * (FT_Pos)( 30 - ppem ) ) / 20;
+
+ if ( bdelta < 0 )
+ bdelta = -bdelta;
+ }
+
+ dist = ( dist - bdelta + 32 ) & ~63;
+ }
}
}
else
@@ -2503,11 +2773,17 @@
AF_Edge base_edge,
AF_Edge stem_edge )
{
- FT_Pos dist = stem_edge->opos - base_edge->opos;
+ FT_Pos dist, base_delta;
+ FT_Pos fitted_width;
+
- FT_Pos fitted_width = af_latin_compute_stem_width( hints, dim, dist,
- base_edge->flags,
- stem_edge->flags );
+ dist = stem_edge->opos - base_edge->opos;
+ base_delta = base_edge->pos - base_edge->opos;
+
+ fitted_width = af_latin_compute_stem_width( hints, dim,
+ dist, base_delta,
+ base_edge->flags,
+ stem_edge->flags );
stem_edge->pos = base_edge->pos + fitted_width;
@@ -2558,8 +2834,14 @@
AF_Edge anchor = NULL;
FT_Int has_serifs = 0;
+ AF_StyleClass style_class = hints->metrics->style_class;
+ AF_ScriptClass script_class = AF_SCRIPT_CLASSES_GET
+ [style_class->script];
+
+ FT_Bool top_to_bottom_hinting = 0;
+
#ifdef FT_DEBUG_LEVEL_TRACE
- FT_UInt num_actions = 0;
+ FT_UInt num_actions = 0;
#endif
@@ -2567,6 +2849,9 @@
dim == AF_DIMENSION_VERT ? "horizontal" : "vertical",
af_style_names[hints->metrics->style_class->style] ));
+ if ( dim == AF_DIMENSION_VERT )
+ top_to_bottom_hinting = script_class->top_to_bottom_hinting;
+
/* we begin by aligning all stems relative to the blue zone */
/* if needed -- that's only for horizontal edges */
@@ -2702,7 +2987,8 @@
org_len = edge2->opos - edge->opos;
- cur_len = af_latin_compute_stem_width( hints, dim, org_len,
+ cur_len = af_latin_compute_stem_width( hints, dim,
+ org_len, 0,
edge->flags,
edge2->flags );
@@ -2771,7 +3057,8 @@
org_len = edge2->opos - edge->opos;
org_center = org_pos + ( org_len >> 1 );
- cur_len = af_latin_compute_stem_width( hints, dim, org_len,
+ cur_len = af_latin_compute_stem_width( hints, dim,
+ org_len, 0,
edge->flags,
edge2->flags );
@@ -2831,7 +3118,8 @@
org_len = edge2->opos - edge->opos;
org_center = org_pos + ( org_len >> 1 );
- cur_len = af_latin_compute_stem_width( hints, dim, org_len,
+ cur_len = af_latin_compute_stem_width( hints, dim,
+ org_len, 0,
edge->flags,
edge2->flags );
@@ -2862,16 +3150,25 @@
edge->flags |= AF_EDGE_DONE;
edge2->flags |= AF_EDGE_DONE;
- if ( edge > edges && edge->pos < edge[-1].pos )
+ if ( edge > edges &&
+ ( top_to_bottom_hinting ? ( edge->pos > edge[-1].pos )
+ : ( edge->pos < edge[-1].pos ) ) )
{
+ /* don't move if stem would (almost) disappear otherwise; */
+ /* the ad-hoc value 16 corresponds to 1/4px */
+ if ( edge->link && FT_ABS( edge->link->pos - edge[-1].pos ) > 16 )
+ {
#ifdef FT_DEBUG_LEVEL_TRACE
- FT_TRACE5(( " BOUND: edge %d (pos=%.2f) moved to %.2f\n",
- edge - edges, edge->pos / 64.0, edge[-1].pos / 64.0 ));
+ FT_TRACE5(( " BOUND: edge %d (pos=%.2f) moved to %.2f\n",
+ edge - edges,
+ edge->pos / 64.0,
+ edge[-1].pos / 64.0 ));
- num_actions++;
+ num_actions++;
#endif
- edge->pos = edge[-1].pos;
+ edge->pos = edge[-1].pos;
+ }
}
}
}
@@ -3023,29 +3320,46 @@
#endif
edge->flags |= AF_EDGE_DONE;
- if ( edge > edges && edge->pos < edge[-1].pos )
+ if ( edge > edges &&
+ ( top_to_bottom_hinting ? ( edge->pos > edge[-1].pos )
+ : ( edge->pos < edge[-1].pos ) ) )
{
+ /* don't move if stem would (almost) disappear otherwise; */
+ /* the ad-hoc value 16 corresponds to 1/4px */
+ if ( edge->link && FT_ABS( edge->link->pos - edge[-1].pos ) > 16 )
+ {
#ifdef FT_DEBUG_LEVEL_TRACE
- FT_TRACE5(( " BOUND: edge %d (pos=%.2f) moved to %.2f\n",
- edge - edges, edge->pos / 64.0, edge[-1].pos / 64.0 ));
+ FT_TRACE5(( " BOUND: edge %d (pos=%.2f) moved to %.2f\n",
+ edge - edges,
+ edge->pos / 64.0,
+ edge[-1].pos / 64.0 ));
- num_actions++;
+ num_actions++;
#endif
- edge->pos = edge[-1].pos;
+ edge->pos = edge[-1].pos;
+ }
}
- if ( edge + 1 < edge_limit &&
- edge[1].flags & AF_EDGE_DONE &&
- edge->pos > edge[1].pos )
+ if ( edge + 1 < edge_limit &&
+ edge[1].flags & AF_EDGE_DONE &&
+ ( top_to_bottom_hinting ? ( edge->pos < edge[1].pos )
+ : ( edge->pos > edge[1].pos ) ) )
{
+ /* don't move if stem would (almost) disappear otherwise; */
+ /* the ad-hoc value 16 corresponds to 1/4px */
+ if ( edge->link && FT_ABS( edge->link->pos - edge[-1].pos ) > 16 )
+ {
#ifdef FT_DEBUG_LEVEL_TRACE
- FT_TRACE5(( " BOUND: edge %d (pos=%.2f) moved to %.2f\n",
- edge - edges, edge->pos / 64.0, edge[1].pos / 64.0 ));
+ FT_TRACE5(( " BOUND: edge %d (pos=%.2f) moved to %.2f\n",
+ edge - edges,
+ edge->pos / 64.0,
+ edge[1].pos / 64.0 ));
- num_actions++;
+ num_actions++;
#endif
- edge->pos = edge[1].pos;
+ edge->pos = edge[1].pos;
+ }
}
}
}