1 /***************************************************************************/
5 /* Auto-fitter warping algorithm (body). */
7 /* Copyright 2006, 2007, 2011 by */
8 /* David Turner, Robert Wilhelm, and Werner Lemberg. */
10 /* This file is part of the FreeType project, and may only be used, */
11 /* modified, and distributed under the terms of the FreeType project */
12 /* license, LICENSE.TXT. By continuing to use, modify, or distribute */
13 /* this file you indicate that you have read the license and */
14 /* understand and accept it fully. */
16 /***************************************************************************/
20 * The idea of the warping code is to slightly scale and shift a glyph
21 * within a single dimension so that as much of its segments are aligned
22 * (more or less) on the grid. To find out the optimal scaling and
23 * shifting value, various parameter combinations are tried and scored.
28 #ifdef AF_CONFIG_OPTION_USE_WARPER
30 /*************************************************************************/
32 /* The macro FT_COMPONENT is used in trace mode. It is an implicit */
33 /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
34 /* messages during execution. */
37 #define FT_COMPONENT trace_afwarp
40 /* The weights cover the range 0/64 - 63/64 of a pixel. Obviously, */
41 /* values around a half pixel (which means exactly between two grid */
42 /* lines) gets the worst weight. */
44 static const AF_WarpScore
45 af_warper_weights[64] =
47 35, 32, 30, 25, 20, 15, 12, 10, 5, 1, 0, 0, 0, 0, 0, 0,
48 0, 0, 0, 0, 0, 0, -1, -2, -5, -8,-10,-10,-20,-20,-30,-30,
50 -30,-30,-20,-20,-10,-10, -8, -5, -2, -1, 0, 0, 0, 0, 0, 0,
51 0, 0, 0, 0, 0, 0, 0, 1, 5, 10, 12, 15, 20, 25, 30, 32,
54 static const AF_WarpScore
55 af_warper_weights[64] =
57 30, 20, 10, 5, 4, 4, 3, 2, 1, 0, 0, 0, 0, 0, 0, 0,
58 0, 0, 0, 0, 0, 0, 0, -1, -2, -2, -5, -5,-10,-10,-15,-20,
60 -20,-15,-15,-10,-10, -5, -5, -2, -2, -1, 0, 0, 0, 0, 0, 0,
61 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 4, 5, 10, 20,
66 /* Score segments for a given `scale' and `delta' in the range */
67 /* `xx1' to `xx2', and store the best result in `warper'. If */
68 /* the new best score is equal to the old one, prefer the */
69 /* value with a smaller distortion (around `base_distort'). */
72 af_warper_compute_line_best( AF_Warper warper,
77 AF_WarpScore base_distort,
79 FT_UInt num_segments )
81 FT_Int idx_min, idx_max, idx0;
83 AF_WarpScore scores[65];
86 for ( nn = 0; nn < 65; nn++ )
89 idx0 = xx1 - warper->t1;
91 /* compute minimum and maximum indices */
93 FT_Pos xx1min = warper->x1min;
94 FT_Pos xx1max = warper->x1max;
98 if ( xx1min + w < warper->x2min )
99 xx1min = warper->x2min - w;
101 xx1max = warper->x1max;
102 if ( xx1max + w > warper->x2max )
103 xx1max = warper->x2max - w;
105 idx_min = xx1min - warper->t1;
106 idx_max = xx1max - warper->t1;
108 if ( idx_min < 0 || idx_min > idx_max || idx_max > 64 )
110 FT_TRACE5(( "invalid indices:\n"
111 " min=%d max=%d, xx1=%ld xx2=%ld,\n"
112 " x1min=%ld x1max=%ld, x2min=%ld x2max=%ld\n",
113 idx_min, idx_max, xx1, xx2,
114 warper->x1min, warper->x1max,
115 warper->x2min, warper->x2max ));
120 for ( nn = 0; nn < num_segments; nn++ )
122 FT_Pos len = segments[nn].max_coord - segments[nn].min_coord;
123 FT_Pos y0 = FT_MulFix( segments[nn].pos, scale ) + delta;
124 FT_Pos y = y0 + ( idx_min - idx0 );
128 /* score the length of the segments for the given range */
129 for ( idx = idx_min; idx <= idx_max; idx++, y++ )
130 scores[idx] += af_warper_weights[y & 63] * len;
133 /* find best score */
138 for ( idx = idx_min; idx <= idx_max; idx++ )
140 AF_WarpScore score = scores[idx];
141 AF_WarpScore distort = base_distort + ( idx - idx0 );
144 if ( score > warper->best_score ||
145 ( score == warper->best_score &&
146 distort < warper->best_distort ) )
148 warper->best_score = score;
149 warper->best_distort = distort;
150 warper->best_scale = scale;
151 warper->best_delta = delta + ( idx - idx0 );
158 /* Compute optimal scaling and delta values for a given glyph and */
162 af_warper_compute( AF_Warper warper,
174 FT_UInt nn, num_points, num_segments;
178 AF_WarpScore base_distort;
182 /* get original scaling transformation */
183 if ( dim == AF_DIMENSION_VERT )
185 org_scale = hints->y_scale;
186 org_delta = hints->y_delta;
190 org_scale = hints->x_scale;
191 org_delta = hints->x_delta;
194 warper->best_scale = org_scale;
195 warper->best_delta = org_delta;
196 warper->best_score = INT_MIN;
197 warper->best_distort = 0;
199 axis = &hints->axis[dim];
200 segments = axis->segments;
201 num_segments = axis->num_segments;
202 points = hints->points;
203 num_points = hints->num_points;
205 *a_scale = org_scale;
206 *a_delta = org_delta;
208 /* get X1 and X2, minimum and maximum in original coordinates */
209 if ( num_segments < 1 )
213 X1 = X2 = points[0].fx;
214 for ( nn = 1; nn < num_points; nn++ )
216 FT_Int X = points[nn].fx;
225 X1 = X2 = segments[0].pos;
226 for ( nn = 1; nn < num_segments; nn++ )
228 FT_Int X = segments[nn].pos;
241 warper->x1 = FT_MulFix( X1, org_scale ) + org_delta;
242 warper->x2 = FT_MulFix( X2, org_scale ) + org_delta;
244 warper->t1 = AF_WARPER_FLOOR( warper->x1 );
245 warper->t2 = AF_WARPER_CEIL( warper->x2 );
247 /* examine a half pixel wide range around the maximum coordinates */
248 warper->x1min = warper->x1 & ~31;
249 warper->x1max = warper->x1min + 32;
250 warper->x2min = warper->x2 & ~31;
251 warper->x2max = warper->x2min + 32;
253 if ( warper->x1max > warper->x2 )
254 warper->x1max = warper->x2;
256 if ( warper->x2min < warper->x1 )
257 warper->x2min = warper->x1;
259 warper->w0 = warper->x2 - warper->x1;
261 if ( warper->w0 <= 64 )
263 warper->x1max = warper->x1;
264 warper->x2min = warper->x2;
267 /* examine (at most) a pixel wide range around the natural width */
268 warper->wmin = warper->x2min - warper->x1max;
269 warper->wmax = warper->x2max - warper->x1min;
272 /* some heuristics to reduce the number of widths to be examined */
277 if ( warper->w0 <= 128 )
280 if ( warper->w0 <= 96 )
284 if ( warper->wmin < warper->w0 - margin )
285 warper->wmin = warper->w0 - margin;
287 if ( warper->wmax > warper->w0 + margin )
288 warper->wmax = warper->w0 + margin;
291 if ( warper->wmin < warper->w0 * 3 / 4 )
292 warper->wmin = warper->w0 * 3 / 4;
294 if ( warper->wmax > warper->w0 * 5 / 4 )
295 warper->wmax = warper->w0 * 5 / 4;
297 /* no scaling, just translation */
298 warper->wmin = warper->wmax = warper->w0;
301 for ( w = warper->wmin; w <= warper->wmax; w++ )
308 /* compute min and max positions for given width, */
309 /* assuring that they stay within the coordinate ranges */
312 if ( w >= warper->w0 )
314 xx1 -= w - warper->w0;
315 if ( xx1 < warper->x1min )
317 xx2 += warper->x1min - xx1;
323 xx1 -= w - warper->w0;
324 if ( xx1 > warper->x1max )
326 xx2 -= xx1 - warper->x1max;
331 if ( xx1 < warper->x1 )
332 base_distort = warper->x1 - xx1;
334 base_distort = xx1 - warper->x1;
336 if ( xx2 < warper->x2 )
337 base_distort += warper->x2 - xx2;
339 base_distort += xx2 - warper->x2;
341 /* give base distortion a greater weight while scoring */
344 new_scale = org_scale + FT_DivFix( w - warper->w0, X2 - X1 );
345 new_delta = xx1 - FT_MulFix( X1, new_scale );
347 af_warper_compute_line_best( warper, new_scale, new_delta, xx1, xx2,
349 segments, num_segments );
353 FT_Fixed best_scale = warper->best_scale;
354 FT_Pos best_delta = warper->best_delta;
357 hints->xmin_delta = FT_MulFix( X1, best_scale - org_scale )
359 hints->xmax_delta = FT_MulFix( X2, best_scale - org_scale )
362 *a_scale = best_scale;
363 *a_delta = best_delta;
367 #else /* !AF_CONFIG_OPTION_USE_WARPER */
369 /* ANSI C doesn't like empty source files */
370 typedef int _af_warp_dummy;
372 #endif /* !AF_CONFIG_OPTION_USE_WARPER */