2006-01-22 07:58:16 +01:00
|
|
|
/***************************************************************************/
|
|
|
|
/* */
|
|
|
|
/* afwarp.c */
|
|
|
|
/* */
|
|
|
|
/* Auto-fitter warping algorithm (body). */
|
|
|
|
/* */
|
2011-02-20 07:30:46 +01:00
|
|
|
/* Copyright 2006, 2007, 2011 by */
|
2006-01-22 07:58:16 +01:00
|
|
|
/* David Turner, Robert Wilhelm, and Werner Lemberg. */
|
|
|
|
/* */
|
|
|
|
/* This file is part of the FreeType project, and may only be used, */
|
|
|
|
/* modified, and distributed under the terms of the FreeType project */
|
|
|
|
/* license, LICENSE.TXT. By continuing to use, modify, or distribute */
|
|
|
|
/* this file you indicate that you have read the license and */
|
|
|
|
/* understand and accept it fully. */
|
|
|
|
/* */
|
|
|
|
/***************************************************************************/
|
|
|
|
|
|
|
|
|
2011-02-20 07:30:46 +01:00
|
|
|
/*
|
|
|
|
* The idea of the warping code is to slightly scale and shift a glyph
|
|
|
|
* within a single dimension so that as much of its segments are aligned
|
|
|
|
* (more or less) on the grid. To find out the optimal scaling and
|
|
|
|
* shifting value, various parameter combinations are tried and scored.
|
|
|
|
*/
|
|
|
|
|
2006-01-21 15:31:45 +01:00
|
|
|
#include "afwarp.h"
|
|
|
|
|
2011-03-02 03:52:36 +01:00
|
|
|
#ifdef AF_CONFIG_OPTION_USE_WARPER
|
2006-01-22 07:58:16 +01:00
|
|
|
|
2011-04-18 19:05:28 +02:00
|
|
|
/*************************************************************************/
|
|
|
|
/* */
|
|
|
|
/* The macro FT_COMPONENT is used in trace mode. It is an implicit */
|
|
|
|
/* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
|
|
|
|
/* messages during execution. */
|
|
|
|
/* */
|
|
|
|
#undef FT_COMPONENT
|
|
|
|
#define FT_COMPONENT trace_afwarp
|
|
|
|
|
|
|
|
|
2011-02-20 07:30:46 +01:00
|
|
|
/* The weights cover the range 0/64 - 63/64 of a pixel. Obviously, */
|
|
|
|
/* values around a half pixel (which means exactly between two grid */
|
|
|
|
/* lines) gets the worst weight. */
|
2006-01-21 15:31:45 +01:00
|
|
|
#if 1
|
2006-01-22 07:58:16 +01:00
|
|
|
static const AF_WarpScore
|
|
|
|
af_warper_weights[64] =
|
|
|
|
{
|
2007-01-26 16:05:41 +01:00
|
|
|
35, 32, 30, 25, 20, 15, 12, 10, 5, 1, 0, 0, 0, 0, 0, 0,
|
2006-01-22 07:58:16 +01:00
|
|
|
0, 0, 0, 0, 0, 0, -1, -2, -5, -8,-10,-10,-20,-20,-30,-30,
|
|
|
|
|
|
|
|
-30,-30,-20,-20,-10,-10, -8, -5, -2, -1, 0, 0, 0, 0, 0, 0,
|
2007-01-26 16:05:41 +01:00
|
|
|
0, 0, 0, 0, 0, 0, 0, 1, 5, 10, 12, 15, 20, 25, 30, 32,
|
2006-01-22 07:58:16 +01:00
|
|
|
};
|
2006-01-21 15:31:45 +01:00
|
|
|
#else
|
2006-01-22 07:58:16 +01:00
|
|
|
static const AF_WarpScore
|
|
|
|
af_warper_weights[64] =
|
|
|
|
{
|
|
|
|
30, 20, 10, 5, 4, 4, 3, 2, 1, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, -1, -2, -2, -5, -5,-10,-10,-15,-20,
|
|
|
|
|
|
|
|
-20,-15,-15,-10,-10, -5, -5, -2, -2, -1, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 4, 5, 10, 20,
|
|
|
|
};
|
2006-01-21 15:31:45 +01:00
|
|
|
#endif
|
|
|
|
|
2006-01-22 07:58:16 +01:00
|
|
|
|
2011-02-20 07:30:46 +01:00
|
|
|
/* Score segments for a given `scale' and `delta' in the range */
|
|
|
|
/* `xx1' to `xx2', and store the best result in `warper'. If */
|
|
|
|
/* the new best score is equal to the old one, prefer the */
|
|
|
|
/* value with a smaller distortion (around `base_distort'). */
|
|
|
|
|
2006-01-22 07:58:16 +01:00
|
|
|
static void
|
|
|
|
af_warper_compute_line_best( AF_Warper warper,
|
|
|
|
FT_Fixed scale,
|
|
|
|
FT_Pos delta,
|
|
|
|
FT_Pos xx1,
|
|
|
|
FT_Pos xx2,
|
|
|
|
AF_WarpScore base_distort,
|
|
|
|
AF_Segment segments,
|
|
|
|
FT_UInt num_segments )
|
2006-01-21 15:31:45 +01:00
|
|
|
{
|
2006-01-22 08:09:54 +01:00
|
|
|
FT_Int idx_min, idx_max, idx0;
|
2006-01-22 07:58:16 +01:00
|
|
|
FT_UInt nn;
|
2007-01-23 16:51:50 +01:00
|
|
|
AF_WarpScore scores[65];
|
2006-01-21 15:31:45 +01:00
|
|
|
|
|
|
|
|
2007-01-23 16:51:50 +01:00
|
|
|
for ( nn = 0; nn < 65; nn++ )
|
2006-01-22 07:58:16 +01:00
|
|
|
scores[nn] = 0;
|
2006-01-21 15:31:45 +01:00
|
|
|
|
2006-01-22 07:58:16 +01:00
|
|
|
idx0 = xx1 - warper->t1;
|
2006-01-21 15:31:45 +01:00
|
|
|
|
2006-01-22 07:58:16 +01:00
|
|
|
/* compute minimum and maximum indices */
|
2006-01-21 15:31:45 +01:00
|
|
|
{
|
2006-01-22 07:58:16 +01:00
|
|
|
FT_Pos xx1min = warper->x1min;
|
|
|
|
FT_Pos xx1max = warper->x1max;
|
|
|
|
FT_Pos w = xx2 - xx1;
|
|
|
|
|
|
|
|
|
|
|
|
if ( xx1min + w < warper->x2min )
|
2007-01-23 16:51:50 +01:00
|
|
|
xx1min = warper->x2min - w;
|
2006-01-22 07:58:16 +01:00
|
|
|
|
|
|
|
xx1max = warper->x1max;
|
|
|
|
if ( xx1max + w > warper->x2max )
|
2007-01-23 16:51:50 +01:00
|
|
|
xx1max = warper->x2max - w;
|
2006-01-22 07:58:16 +01:00
|
|
|
|
|
|
|
idx_min = xx1min - warper->t1;
|
|
|
|
idx_max = xx1max - warper->t1;
|
|
|
|
|
2007-01-23 16:51:50 +01:00
|
|
|
if ( idx_min < 0 || idx_min > idx_max || idx_max > 64 )
|
2006-01-22 07:58:16 +01:00
|
|
|
{
|
2011-04-18 19:05:28 +02:00
|
|
|
FT_TRACE5(( "invalid indices:\n"
|
|
|
|
" min=%d max=%d, xx1=%ld xx2=%ld,\n"
|
|
|
|
" x1min=%ld x1max=%ld, x2min=%ld x2max=%ld\n",
|
|
|
|
idx_min, idx_max, xx1, xx2,
|
|
|
|
warper->x1min, warper->x1max,
|
|
|
|
warper->x2min, warper->x2max ));
|
2006-01-22 07:58:16 +01:00
|
|
|
return;
|
|
|
|
}
|
2006-01-21 15:31:45 +01:00
|
|
|
}
|
|
|
|
|
2006-01-22 07:58:16 +01:00
|
|
|
for ( nn = 0; nn < num_segments; nn++ )
|
|
|
|
{
|
|
|
|
FT_Pos len = segments[nn].max_coord - segments[nn].min_coord;
|
|
|
|
FT_Pos y0 = FT_MulFix( segments[nn].pos, scale ) + delta;
|
|
|
|
FT_Pos y = y0 + ( idx_min - idx0 );
|
|
|
|
FT_Int idx;
|
2006-01-21 15:31:45 +01:00
|
|
|
|
|
|
|
|
2011-02-20 07:30:46 +01:00
|
|
|
/* score the length of the segments for the given range */
|
2006-01-22 07:58:16 +01:00
|
|
|
for ( idx = idx_min; idx <= idx_max; idx++, y++ )
|
|
|
|
scores[idx] += af_warper_weights[y & 63] * len;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* find best score */
|
2006-01-21 15:31:45 +01:00
|
|
|
{
|
2006-01-22 07:58:16 +01:00
|
|
|
FT_Int idx;
|
|
|
|
|
2006-01-21 15:31:45 +01:00
|
|
|
|
2006-01-22 07:58:16 +01:00
|
|
|
for ( idx = idx_min; idx <= idx_max; idx++ )
|
2006-01-21 15:31:45 +01:00
|
|
|
{
|
2006-01-22 07:58:16 +01:00
|
|
|
AF_WarpScore score = scores[idx];
|
|
|
|
AF_WarpScore distort = base_distort + ( idx - idx0 );
|
|
|
|
|
|
|
|
|
2011-02-20 07:30:46 +01:00
|
|
|
if ( score > warper->best_score ||
|
2006-01-22 07:58:16 +01:00
|
|
|
( score == warper->best_score &&
|
2011-02-20 07:30:46 +01:00
|
|
|
distort < warper->best_distort ) )
|
2006-01-22 07:58:16 +01:00
|
|
|
{
|
|
|
|
warper->best_score = score;
|
|
|
|
warper->best_distort = distort;
|
|
|
|
warper->best_scale = scale;
|
|
|
|
warper->best_delta = delta + ( idx - idx0 );
|
|
|
|
}
|
2006-01-21 15:31:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-01-22 07:58:16 +01:00
|
|
|
|
|
|
|
|
2011-02-20 07:30:46 +01:00
|
|
|
/* Compute optimal scaling and delta values for a given glyph and */
|
|
|
|
/* dimension. */
|
|
|
|
|
2006-01-22 07:58:16 +01:00
|
|
|
FT_LOCAL_DEF( void )
|
|
|
|
af_warper_compute( AF_Warper warper,
|
|
|
|
AF_GlyphHints hints,
|
|
|
|
AF_Dimension dim,
|
|
|
|
FT_Fixed *a_scale,
|
|
|
|
FT_Pos *a_delta )
|
2006-01-21 15:31:45 +01:00
|
|
|
{
|
2006-01-22 07:58:16 +01:00
|
|
|
AF_AxisHints axis;
|
|
|
|
AF_Point points;
|
2006-01-21 15:31:45 +01:00
|
|
|
|
2006-01-22 07:58:16 +01:00
|
|
|
FT_Fixed org_scale;
|
|
|
|
FT_Pos org_delta;
|
2006-01-21 15:31:45 +01:00
|
|
|
|
2006-01-22 07:58:16 +01:00
|
|
|
FT_UInt nn, num_points, num_segments;
|
|
|
|
FT_Int X1, X2;
|
|
|
|
FT_Int w;
|
2006-01-21 15:31:45 +01:00
|
|
|
|
2006-01-22 07:58:16 +01:00
|
|
|
AF_WarpScore base_distort;
|
|
|
|
AF_Segment segments;
|
2006-01-21 15:31:45 +01:00
|
|
|
|
2006-01-22 07:58:16 +01:00
|
|
|
|
|
|
|
/* get original scaling transformation */
|
|
|
|
if ( dim == AF_DIMENSION_VERT )
|
|
|
|
{
|
|
|
|
org_scale = hints->y_scale;
|
|
|
|
org_delta = hints->y_delta;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
org_scale = hints->x_scale;
|
|
|
|
org_delta = hints->x_delta;
|
|
|
|
}
|
|
|
|
|
|
|
|
warper->best_scale = org_scale;
|
|
|
|
warper->best_delta = org_delta;
|
2007-01-23 16:51:50 +01:00
|
|
|
warper->best_score = INT_MIN;
|
2006-01-22 07:58:16 +01:00
|
|
|
warper->best_distort = 0;
|
|
|
|
|
|
|
|
axis = &hints->axis[dim];
|
|
|
|
segments = axis->segments;
|
|
|
|
num_segments = axis->num_segments;
|
|
|
|
points = hints->points;
|
|
|
|
num_points = hints->num_points;
|
|
|
|
|
|
|
|
*a_scale = org_scale;
|
|
|
|
*a_delta = org_delta;
|
|
|
|
|
|
|
|
/* get X1 and X2, minimum and maximum in original coordinates */
|
2007-01-23 16:51:50 +01:00
|
|
|
if ( num_segments < 1 )
|
2006-01-22 07:58:16 +01:00
|
|
|
return;
|
2006-01-21 15:31:45 +01:00
|
|
|
|
|
|
|
#if 1
|
2006-01-22 07:58:16 +01:00
|
|
|
X1 = X2 = points[0].fx;
|
|
|
|
for ( nn = 1; nn < num_points; nn++ )
|
|
|
|
{
|
|
|
|
FT_Int X = points[nn].fx;
|
2006-01-21 15:31:45 +01:00
|
|
|
|
2006-01-22 07:58:16 +01:00
|
|
|
|
|
|
|
if ( X < X1 )
|
|
|
|
X1 = X;
|
|
|
|
if ( X > X2 )
|
|
|
|
X2 = X;
|
|
|
|
}
|
2006-01-21 15:31:45 +01:00
|
|
|
#else
|
2006-01-22 07:58:16 +01:00
|
|
|
X1 = X2 = segments[0].pos;
|
|
|
|
for ( nn = 1; nn < num_segments; nn++ )
|
|
|
|
{
|
|
|
|
FT_Int X = segments[nn].pos;
|
2006-01-21 15:31:45 +01:00
|
|
|
|
|
|
|
|
2006-01-22 07:58:16 +01:00
|
|
|
if ( X < X1 )
|
|
|
|
X1 = X;
|
|
|
|
if ( X > X2 )
|
|
|
|
X2 = X;
|
|
|
|
}
|
|
|
|
#endif
|
2006-01-21 15:31:45 +01:00
|
|
|
|
2006-01-22 07:58:16 +01:00
|
|
|
if ( X1 >= X2 )
|
|
|
|
return;
|
2006-01-21 15:31:45 +01:00
|
|
|
|
2006-01-22 07:58:16 +01:00
|
|
|
warper->x1 = FT_MulFix( X1, org_scale ) + org_delta;
|
|
|
|
warper->x2 = FT_MulFix( X2, org_scale ) + org_delta;
|
2006-01-21 15:31:45 +01:00
|
|
|
|
2006-01-22 07:58:16 +01:00
|
|
|
warper->t1 = AF_WARPER_FLOOR( warper->x1 );
|
|
|
|
warper->t2 = AF_WARPER_CEIL( warper->x2 );
|
2006-01-21 15:31:45 +01:00
|
|
|
|
2011-02-20 07:30:46 +01:00
|
|
|
/* examine a half pixel wide range around the maximum coordinates */
|
2006-01-22 07:58:16 +01:00
|
|
|
warper->x1min = warper->x1 & ~31;
|
|
|
|
warper->x1max = warper->x1min + 32;
|
|
|
|
warper->x2min = warper->x2 & ~31;
|
|
|
|
warper->x2max = warper->x2min + 32;
|
2006-01-21 15:31:45 +01:00
|
|
|
|
2006-01-22 07:58:16 +01:00
|
|
|
if ( warper->x1max > warper->x2 )
|
|
|
|
warper->x1max = warper->x2;
|
2006-01-21 15:31:45 +01:00
|
|
|
|
2006-01-22 07:58:16 +01:00
|
|
|
if ( warper->x2min < warper->x1 )
|
|
|
|
warper->x2min = warper->x1;
|
2006-01-21 15:31:45 +01:00
|
|
|
|
2006-01-22 07:58:16 +01:00
|
|
|
warper->w0 = warper->x2 - warper->x1;
|
2006-01-21 15:31:45 +01:00
|
|
|
|
2006-01-22 07:58:16 +01:00
|
|
|
if ( warper->w0 <= 64 )
|
|
|
|
{
|
|
|
|
warper->x1max = warper->x1;
|
|
|
|
warper->x2min = warper->x2;
|
|
|
|
}
|
2006-01-21 15:31:45 +01:00
|
|
|
|
2011-02-20 07:30:46 +01:00
|
|
|
/* examine (at most) a pixel wide range around the natural width */
|
2006-01-22 07:58:16 +01:00
|
|
|
warper->wmin = warper->x2min - warper->x1max;
|
|
|
|
warper->wmax = warper->x2max - warper->x1min;
|
2006-01-21 15:31:45 +01:00
|
|
|
|
2007-01-23 16:51:50 +01:00
|
|
|
#if 1
|
2011-02-20 07:30:46 +01:00
|
|
|
/* some heuristics to reduce the number of widths to be examined */
|
2007-01-23 16:51:50 +01:00
|
|
|
{
|
|
|
|
int margin = 16;
|
|
|
|
|
2007-01-25 12:50:00 +01:00
|
|
|
|
2007-01-23 16:51:50 +01:00
|
|
|
if ( warper->w0 <= 128 )
|
|
|
|
{
|
|
|
|
margin = 8;
|
|
|
|
if ( warper->w0 <= 96 )
|
|
|
|
margin = 4;
|
|
|
|
}
|
2006-01-21 15:31:45 +01:00
|
|
|
|
2007-01-23 16:51:50 +01:00
|
|
|
if ( warper->wmin < warper->w0 - margin )
|
|
|
|
warper->wmin = warper->w0 - margin;
|
|
|
|
|
|
|
|
if ( warper->wmax > warper->w0 + margin )
|
|
|
|
warper->wmax = warper->w0 + margin;
|
|
|
|
}
|
2006-01-21 15:31:45 +01:00
|
|
|
|
2006-01-22 07:58:16 +01:00
|
|
|
if ( warper->wmin < warper->w0 * 3 / 4 )
|
|
|
|
warper->wmin = warper->w0 * 3 / 4;
|
2006-01-21 15:31:45 +01:00
|
|
|
|
2006-01-22 07:58:16 +01:00
|
|
|
if ( warper->wmax > warper->w0 * 5 / 4 )
|
|
|
|
warper->wmax = warper->w0 * 5 / 4;
|
2007-01-23 16:51:50 +01:00
|
|
|
#else
|
|
|
|
/* no scaling, just translation */
|
|
|
|
warper->wmin = warper->wmax = warper->w0;
|
|
|
|
#endif
|
2006-01-21 15:31:45 +01:00
|
|
|
|
2006-01-22 07:58:16 +01:00
|
|
|
for ( w = warper->wmin; w <= warper->wmax; w++ )
|
2006-01-21 15:31:45 +01:00
|
|
|
{
|
2006-01-22 07:58:16 +01:00
|
|
|
FT_Fixed new_scale;
|
|
|
|
FT_Pos new_delta;
|
|
|
|
FT_Pos xx1, xx2;
|
|
|
|
|
|
|
|
|
2011-02-20 07:30:46 +01:00
|
|
|
/* compute min and max positions for given width, */
|
|
|
|
/* assuring that they stay within the coordinate ranges */
|
2006-01-22 07:58:16 +01:00
|
|
|
xx1 = warper->x1;
|
|
|
|
xx2 = warper->x2;
|
|
|
|
if ( w >= warper->w0 )
|
2006-01-21 15:31:45 +01:00
|
|
|
{
|
2006-01-22 07:58:16 +01:00
|
|
|
xx1 -= w - warper->w0;
|
|
|
|
if ( xx1 < warper->x1min )
|
|
|
|
{
|
|
|
|
xx2 += warper->x1min - xx1;
|
|
|
|
xx1 = warper->x1min;
|
|
|
|
}
|
2006-01-21 15:31:45 +01:00
|
|
|
}
|
2006-01-22 07:58:16 +01:00
|
|
|
else
|
2006-01-21 15:31:45 +01:00
|
|
|
{
|
2006-01-22 07:58:16 +01:00
|
|
|
xx1 -= w - warper->w0;
|
|
|
|
if ( xx1 > warper->x1max )
|
|
|
|
{
|
|
|
|
xx2 -= xx1 - warper->x1max;
|
|
|
|
xx1 = warper->x1max;
|
|
|
|
}
|
2006-01-21 15:31:45 +01:00
|
|
|
}
|
|
|
|
|
2006-01-22 07:58:16 +01:00
|
|
|
if ( xx1 < warper->x1 )
|
|
|
|
base_distort = warper->x1 - xx1;
|
|
|
|
else
|
|
|
|
base_distort = xx1 - warper->x1;
|
2006-01-21 15:31:45 +01:00
|
|
|
|
2006-01-22 07:58:16 +01:00
|
|
|
if ( xx2 < warper->x2 )
|
|
|
|
base_distort += warper->x2 - xx2;
|
|
|
|
else
|
|
|
|
base_distort += xx2 - warper->x2;
|
2006-01-21 15:31:45 +01:00
|
|
|
|
2011-02-20 07:30:46 +01:00
|
|
|
/* give base distortion a greater weight while scoring */
|
2006-01-22 07:58:16 +01:00
|
|
|
base_distort *= 10;
|
2006-01-21 15:31:45 +01:00
|
|
|
|
2006-01-22 07:58:16 +01:00
|
|
|
new_scale = org_scale + FT_DivFix( w - warper->w0, X2 - X1 );
|
|
|
|
new_delta = xx1 - FT_MulFix( X1, new_scale );
|
|
|
|
|
|
|
|
af_warper_compute_line_best( warper, new_scale, new_delta, xx1, xx2,
|
|
|
|
base_distort,
|
|
|
|
segments, num_segments );
|
|
|
|
}
|
2006-01-21 15:31:45 +01:00
|
|
|
|
2007-01-26 16:05:41 +01:00
|
|
|
{
|
|
|
|
FT_Fixed best_scale = warper->best_scale;
|
|
|
|
FT_Pos best_delta = warper->best_delta;
|
2012-01-16 18:00:24 +01:00
|
|
|
|
2007-01-26 17:08:49 +01:00
|
|
|
|
|
|
|
hints->xmin_delta = FT_MulFix( X1, best_scale - org_scale )
|
|
|
|
+ best_delta;
|
|
|
|
hints->xmax_delta = FT_MulFix( X2, best_scale - org_scale )
|
|
|
|
+ best_delta;
|
2007-01-26 16:05:41 +01:00
|
|
|
|
|
|
|
*a_scale = best_scale;
|
|
|
|
*a_delta = best_delta;
|
|
|
|
}
|
2006-01-21 15:31:45 +01:00
|
|
|
}
|
|
|
|
|
2011-03-02 03:52:36 +01:00
|
|
|
#else /* !AF_CONFIG_OPTION_USE_WARPER */
|
2006-01-27 13:11:22 +01:00
|
|
|
|
2011-03-02 03:52:36 +01:00
|
|
|
/* ANSI C doesn't like empty source files */
|
|
|
|
typedef int _af_warp_dummy;
|
2006-01-27 13:11:22 +01:00
|
|
|
|
2011-03-02 03:52:36 +01:00
|
|
|
#endif /* !AF_CONFIG_OPTION_USE_WARPER */
|
2006-01-21 15:31:45 +01:00
|
|
|
|
2006-01-22 07:58:16 +01:00
|
|
|
/* END */
|