misc/libfreetype/src/autofit/afwarp.c
author koda
Mon, 25 Apr 2011 01:46:54 +0200
changeset 5172 88f2e05288ba
permissions -rw-r--r--
aaand let's add freetype as well while we are at it other smaller changes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5172
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
     1
/***************************************************************************/
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
     2
/*                                                                         */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
     3
/*  afwarp.c                                                               */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
     4
/*                                                                         */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
     5
/*    Auto-fitter warping algorithm (body).                                */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
     6
/*                                                                         */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
     7
/*  Copyright 2006, 2007, 2011 by                                          */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
     8
/*  David Turner, Robert Wilhelm, and Werner Lemberg.                      */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
     9
/*                                                                         */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    10
/*  This file is part of the FreeType project, and may only be used,       */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    11
/*  modified, and distributed under the terms of the FreeType project      */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    12
/*  license, LICENSE.TXT.  By continuing to use, modify, or distribute     */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    13
/*  this file you indicate that you have read the license and              */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    14
/*  understand and accept it fully.                                        */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    15
/*                                                                         */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    16
/***************************************************************************/
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    17
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    18
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    19
  /*
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    20
   *  The idea of the warping code is to slightly scale and shift a glyph
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    21
   *  within a single dimension so that as much of its segments are aligned
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    22
   *  (more or less) on the grid.  To find out the optimal scaling and
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    23
   *  shifting value, various parameter combinations are tried and scored.
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    24
   */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    25
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    26
#include "afwarp.h"
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    27
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    28
#ifdef AF_CONFIG_OPTION_USE_WARPER
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    29
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    30
  /*************************************************************************/
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    31
  /*                                                                       */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    32
  /* The macro FT_COMPONENT is used in trace mode.  It is an implicit      */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    33
  /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log  */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    34
  /* messages during execution.                                            */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    35
  /*                                                                       */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    36
#undef  FT_COMPONENT
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    37
#define FT_COMPONENT  trace_afwarp
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    38
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    39
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    40
  /* The weights cover the range 0/64 - 63/64 of a pixel.  Obviously, */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    41
  /* values around a half pixel (which means exactly between two grid */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    42
  /* lines) gets the worst weight.                                    */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    43
#if 1
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    44
  static const AF_WarpScore
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    45
  af_warper_weights[64] =
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    46
  {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    47
    35, 32, 30, 25, 20, 15, 12, 10,  5,  1,  0,  0,  0,  0,  0,  0,
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    48
     0,  0,  0,  0,  0,  0, -1, -2, -5, -8,-10,-10,-20,-20,-30,-30,
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    49
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    50
   -30,-30,-20,-20,-10,-10, -8, -5, -2, -1,  0,  0,  0,  0,  0,  0,
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    51
     0,  0,  0,  0,  0,  0,  0,  1,  5, 10, 12, 15, 20, 25, 30, 32,
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    52
  };
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    53
#else
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    54
  static const AF_WarpScore
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    55
  af_warper_weights[64] =
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    56
  {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    57
    30, 20, 10,  5,  4,  4,  3,  2,  1,  0,  0,  0,  0,  0,  0,  0,
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    58
     0,  0,  0,  0,  0,  0,  0, -1, -2, -2, -5, -5,-10,-10,-15,-20,
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    59
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    60
   -20,-15,-15,-10,-10, -5, -5, -2, -2, -1,  0,  0,  0,  0,  0,  0,
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    61
     0,  0,  0,  0,  0,  0,  0,  0,  1,  2,  3,  4,  4,  5, 10, 20,
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    62
  };
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    63
#endif
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    64
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    65
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    66
  /* Score segments for a given `scale' and `delta' in the range */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    67
  /* `xx1' to `xx2', and store the best result in `warper'.  If  */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    68
  /* the new best score is equal to the old one, prefer the      */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    69
  /* value with a smaller distortion (around `base_distort').    */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    70
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    71
  static void
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    72
  af_warper_compute_line_best( AF_Warper     warper,
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    73
                               FT_Fixed      scale,
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    74
                               FT_Pos        delta,
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    75
                               FT_Pos        xx1,
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    76
                               FT_Pos        xx2,
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    77
                               AF_WarpScore  base_distort,
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    78
                               AF_Segment    segments,
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    79
                               FT_UInt       num_segments )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    80
  {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    81
    FT_Int        idx_min, idx_max, idx0;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    82
    FT_UInt       nn;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    83
    AF_WarpScore  scores[65];
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    84
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    85
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    86
    for ( nn = 0; nn < 65; nn++ )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    87
      scores[nn] = 0;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    88
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    89
    idx0 = xx1 - warper->t1;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    90
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    91
    /* compute minimum and maximum indices */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    92
    {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    93
      FT_Pos  xx1min = warper->x1min;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    94
      FT_Pos  xx1max = warper->x1max;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    95
      FT_Pos  w      = xx2 - xx1;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    96
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    97
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    98
      if ( xx1min + w < warper->x2min )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    99
        xx1min = warper->x2min - w;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   100
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   101
      xx1max = warper->x1max;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   102
      if ( xx1max + w > warper->x2max )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   103
        xx1max = warper->x2max - w;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   104
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   105
      idx_min = xx1min - warper->t1;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   106
      idx_max = xx1max - warper->t1;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   107
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   108
      if ( idx_min < 0 || idx_min > idx_max || idx_max > 64 )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   109
      {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   110
        FT_TRACE5(( "invalid indices:\n"
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   111
                    "  min=%d max=%d, xx1=%ld xx2=%ld,\n"
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   112
                    "  x1min=%ld x1max=%ld, x2min=%ld x2max=%ld\n",
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   113
                    idx_min, idx_max, xx1, xx2,
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   114
                    warper->x1min, warper->x1max,
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   115
                    warper->x2min, warper->x2max ));
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   116
        return;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   117
      }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   118
    }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   119
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   120
    for ( nn = 0; nn < num_segments; nn++ )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   121
    {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   122
      FT_Pos  len = segments[nn].max_coord - segments[nn].min_coord;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   123
      FT_Pos  y0  = FT_MulFix( segments[nn].pos, scale ) + delta;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   124
      FT_Pos  y   = y0 + ( idx_min - idx0 );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   125
      FT_Int  idx;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   126
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   127
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   128
      /* score the length of the segments for the given range */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   129
      for ( idx = idx_min; idx <= idx_max; idx++, y++ )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   130
        scores[idx] += af_warper_weights[y & 63] * len;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   131
    }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   132
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   133
    /* find best score */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   134
    {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   135
      FT_Int  idx;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   136
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   137
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   138
      for ( idx = idx_min; idx <= idx_max; idx++ )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   139
      {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   140
        AF_WarpScore  score = scores[idx];
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   141
        AF_WarpScore  distort = base_distort + ( idx - idx0 );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   142
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   143
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   144
        if ( score > warper->best_score         ||
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   145
             ( score == warper->best_score    &&
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   146
               distort < warper->best_distort ) )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   147
        {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   148
          warper->best_score   = score;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   149
          warper->best_distort = distort;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   150
          warper->best_scale   = scale;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   151
          warper->best_delta   = delta + ( idx - idx0 );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   152
        }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   153
      }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   154
    }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   155
  }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   156
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   157
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   158
  /* Compute optimal scaling and delta values for a given glyph and */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   159
  /* dimension.                                                     */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   160
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   161
  FT_LOCAL_DEF( void )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   162
  af_warper_compute( AF_Warper      warper,
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   163
                     AF_GlyphHints  hints,
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   164
                     AF_Dimension   dim,
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   165
                     FT_Fixed      *a_scale,
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   166
                     FT_Pos        *a_delta )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   167
  {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   168
    AF_AxisHints  axis;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   169
    AF_Point      points;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   170
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   171
    FT_Fixed      org_scale;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   172
    FT_Pos        org_delta;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   173
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   174
    FT_UInt       nn, num_points, num_segments;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   175
    FT_Int        X1, X2;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   176
    FT_Int        w;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   177
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   178
    AF_WarpScore  base_distort;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   179
    AF_Segment    segments;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   180
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   181
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   182
    /* get original scaling transformation */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   183
    if ( dim == AF_DIMENSION_VERT )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   184
    {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   185
      org_scale = hints->y_scale;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   186
      org_delta = hints->y_delta;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   187
    }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   188
    else
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   189
    {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   190
      org_scale = hints->x_scale;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   191
      org_delta = hints->x_delta;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   192
    }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   193
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   194
    warper->best_scale   = org_scale;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   195
    warper->best_delta   = org_delta;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   196
    warper->best_score   = INT_MIN;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   197
    warper->best_distort = 0;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   198
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   199
    axis         = &hints->axis[dim];
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   200
    segments     = axis->segments;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   201
    num_segments = axis->num_segments;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   202
    points       = hints->points;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   203
    num_points   = hints->num_points;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   204
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   205
    *a_scale = org_scale;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   206
    *a_delta = org_delta;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   207
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   208
    /* get X1 and X2, minimum and maximum in original coordinates */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   209
    if ( num_segments < 1 )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   210
      return;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   211
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   212
#if 1
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   213
    X1 = X2 = points[0].fx;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   214
    for ( nn = 1; nn < num_points; nn++ )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   215
    {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   216
      FT_Int  X = points[nn].fx;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   217
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   218
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   219
      if ( X < X1 )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   220
        X1 = X;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   221
      if ( X > X2 )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   222
        X2 = X;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   223
    }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   224
#else
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   225
    X1 = X2 = segments[0].pos;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   226
    for ( nn = 1; nn < num_segments; nn++ )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   227
    {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   228
      FT_Int  X = segments[nn].pos;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   229
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   230
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   231
      if ( X < X1 )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   232
        X1 = X;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   233
      if ( X > X2 )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   234
        X2 = X;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   235
    }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   236
#endif
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   237
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   238
    if ( X1 >= X2 )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   239
      return;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   240
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   241
    warper->x1 = FT_MulFix( X1, org_scale ) + org_delta;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   242
    warper->x2 = FT_MulFix( X2, org_scale ) + org_delta;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   243
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   244
    warper->t1 = AF_WARPER_FLOOR( warper->x1 );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   245
    warper->t2 = AF_WARPER_CEIL( warper->x2 );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   246
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   247
    /* examine a half pixel wide range around the maximum coordinates */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   248
    warper->x1min = warper->x1 & ~31;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   249
    warper->x1max = warper->x1min + 32;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   250
    warper->x2min = warper->x2 & ~31;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   251
    warper->x2max = warper->x2min + 32;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   252
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   253
    if ( warper->x1max > warper->x2 )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   254
      warper->x1max = warper->x2;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   255
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   256
    if ( warper->x2min < warper->x1 )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   257
      warper->x2min = warper->x1;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   258
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   259
    warper->w0 = warper->x2 - warper->x1;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   260
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   261
    if ( warper->w0 <= 64 )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   262
    {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   263
      warper->x1max = warper->x1;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   264
      warper->x2min = warper->x2;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   265
    }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   266
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   267
    /* examine (at most) a pixel wide range around the natural width */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   268
    warper->wmin = warper->x2min - warper->x1max;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   269
    warper->wmax = warper->x2max - warper->x1min;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   270
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   271
#if 1
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   272
    /* some heuristics to reduce the number of widths to be examined */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   273
    {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   274
      int  margin = 16;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   275
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   276
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   277
      if ( warper->w0 <= 128 )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   278
      {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   279
         margin = 8;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   280
         if ( warper->w0 <= 96 )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   281
           margin = 4;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   282
      }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   283
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   284
      if ( warper->wmin < warper->w0 - margin )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   285
        warper->wmin = warper->w0 - margin;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   286
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   287
      if ( warper->wmax > warper->w0 + margin )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   288
        warper->wmax = warper->w0 + margin;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   289
    }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   290
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   291
    if ( warper->wmin < warper->w0 * 3 / 4 )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   292
      warper->wmin = warper->w0 * 3 / 4;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   293
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   294
    if ( warper->wmax > warper->w0 * 5 / 4 )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   295
      warper->wmax = warper->w0 * 5 / 4;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   296
#else
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   297
    /* no scaling, just translation */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   298
    warper->wmin = warper->wmax = warper->w0;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   299
#endif
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   300
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   301
    for ( w = warper->wmin; w <= warper->wmax; w++ )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   302
    {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   303
      FT_Fixed  new_scale;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   304
      FT_Pos    new_delta;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   305
      FT_Pos    xx1, xx2;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   306
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   307
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   308
      /* compute min and max positions for given width,       */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   309
      /* assuring that they stay within the coordinate ranges */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   310
      xx1 = warper->x1;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   311
      xx2 = warper->x2;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   312
      if ( w >= warper->w0 )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   313
      {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   314
        xx1 -= w - warper->w0;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   315
        if ( xx1 < warper->x1min )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   316
        {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   317
          xx2 += warper->x1min - xx1;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   318
          xx1  = warper->x1min;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   319
        }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   320
      }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   321
      else
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   322
      {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   323
        xx1 -= w - warper->w0;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   324
        if ( xx1 > warper->x1max )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   325
        {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   326
          xx2 -= xx1 - warper->x1max;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   327
          xx1  = warper->x1max;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   328
        }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   329
      }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   330
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   331
      if ( xx1 < warper->x1 )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   332
        base_distort = warper->x1 - xx1;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   333
      else
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   334
        base_distort = xx1 - warper->x1;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   335
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   336
      if ( xx2 < warper->x2 )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   337
        base_distort += warper->x2 - xx2;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   338
      else
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   339
        base_distort += xx2 - warper->x2;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   340
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   341
      /* give base distortion a greater weight while scoring */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   342
      base_distort *= 10;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   343
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   344
      new_scale = org_scale + FT_DivFix( w - warper->w0, X2 - X1 );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   345
      new_delta = xx1 - FT_MulFix( X1, new_scale );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   346
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   347
      af_warper_compute_line_best( warper, new_scale, new_delta, xx1, xx2,
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   348
                                   base_distort,
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   349
                                   segments, num_segments );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   350
    }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   351
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   352
    {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   353
      FT_Fixed  best_scale = warper->best_scale;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   354
      FT_Pos    best_delta = warper->best_delta;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   355
     
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   356
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   357
      hints->xmin_delta = FT_MulFix( X1, best_scale - org_scale )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   358
                          + best_delta;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   359
      hints->xmax_delta = FT_MulFix( X2, best_scale - org_scale )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   360
                          + best_delta;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   361
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   362
      *a_scale = best_scale;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   363
      *a_delta = best_delta;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   364
    }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   365
  }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   366
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   367
#else /* !AF_CONFIG_OPTION_USE_WARPER */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   368
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   369
  /* ANSI C doesn't like empty source files */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   370
  typedef int  _af_warp_dummy;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   371
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   372
#endif /* !AF_CONFIG_OPTION_USE_WARPER */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   373
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   374
/* END */