ftbbox.c 21.0 KB
Newer Older
M
Ming, Bai 已提交
1 2 3 4 5 6
/***************************************************************************/
/*                                                                         */
/*  ftbbox.c                                                               */
/*                                                                         */
/*    FreeType bbox computation (body).                                    */
/*                                                                         */
7
/*  Copyright 1996-2015 by                                                 */
M
Ming, Bai 已提交
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
/*  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.                                        */
/*                                                                         */
/***************************************************************************/


  /*************************************************************************/
  /*                                                                       */
  /* This component has a _single_ role: to compute exact outline bounding */
  /* boxes.                                                                */
  /*                                                                       */
  /*************************************************************************/


#include <ft2build.h>
G
Grissiom 已提交
28 29
#include FT_INTERNAL_DEBUG_H

M
Ming, Bai 已提交
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
#include FT_BBOX_H
#include FT_IMAGE_H
#include FT_OUTLINE_H
#include FT_INTERNAL_CALC_H
#include FT_INTERNAL_OBJECTS_H


  typedef struct  TBBox_Rec_
  {
    FT_Vector  last;
    FT_BBox    bbox;

  } TBBox_Rec;


G
Grissiom 已提交
45 46 47 48 49 50 51 52 53 54
#define FT_UPDATE_BBOX( p, bbox ) \
  FT_BEGIN_STMNT                  \
    if ( p->x < bbox.xMin )       \
      bbox.xMin = p->x;           \
    if ( p->x > bbox.xMax )       \
      bbox.xMax = p->x;           \
    if ( p->y < bbox.yMin )       \
      bbox.yMin = p->y;           \
    if ( p->y > bbox.yMax )       \
      bbox.yMax = p->y;           \
G
Grissiom 已提交
55 56
  FT_END_STMNT

G
Grissiom 已提交
57
#define CHECK_X( p, bbox )                         \
G
Grissiom 已提交
58 59
          ( p->x < bbox.xMin || p->x > bbox.xMax )

G
Grissiom 已提交
60
#define CHECK_Y( p, bbox )                         \
G
Grissiom 已提交
61 62 63
          ( p->y < bbox.yMin || p->y > bbox.yMax )


M
Ming, Bai 已提交
64 65 66 67 68 69
  /*************************************************************************/
  /*                                                                       */
  /* <Function>                                                            */
  /*    BBox_Move_To                                                       */
  /*                                                                       */
  /* <Description>                                                         */
G
Grissiom 已提交
70
  /*    This function is used as a `move_to' emitter during                */
M
Ming, Bai 已提交
71
  /*    FT_Outline_Decompose().  It simply records the destination point   */
G
Grissiom 已提交
72 73
  /*    in `user->last'. We also update bbox in case contour starts with   */
  /*    an implicit `on' point.                                            */
M
Ming, Bai 已提交
74 75 76 77 78 79 80 81 82 83 84 85 86 87
  /*                                                                       */
  /* <Input>                                                               */
  /*    to   :: A pointer to the destination vector.                       */
  /*                                                                       */
  /* <InOut>                                                               */
  /*    user :: A pointer to the current walk context.                     */
  /*                                                                       */
  /* <Return>                                                              */
  /*    Always 0.  Needed for the interface only.                          */
  /*                                                                       */
  static int
  BBox_Move_To( FT_Vector*  to,
                TBBox_Rec*  user )
  {
G
Grissiom 已提交
88 89
    FT_UPDATE_BBOX( to, user->bbox );

M
Ming, Bai 已提交
90 91 92 93 94 95
    user->last = *to;

    return 0;
  }


G
Grissiom 已提交
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
  /*************************************************************************/
  /*                                                                       */
  /* <Function>                                                            */
  /*    BBox_Line_To                                                       */
  /*                                                                       */
  /* <Description>                                                         */
  /*    This function is used as a `line_to' emitter during                */
  /*    FT_Outline_Decompose().  It simply records the destination point   */
  /*    in `user->last'; no further computations are necessary because     */
  /*    bbox already contains both explicit ends of the line segment.      */
  /*                                                                       */
  /* <Input>                                                               */
  /*    to   :: A pointer to the destination vector.                       */
  /*                                                                       */
  /* <InOut>                                                               */
  /*    user :: A pointer to the current walk context.                     */
  /*                                                                       */
  /* <Return>                                                              */
  /*    Always 0.  Needed for the interface only.                          */
  /*                                                                       */
  static int
  BBox_Line_To( FT_Vector*  to,
                TBBox_Rec*  user )
  {
    user->last = *to;
M
Ming, Bai 已提交
121

G
Grissiom 已提交
122 123
    return 0;
  }
M
Ming, Bai 已提交
124 125 126 127 128 129 130 131


  /*************************************************************************/
  /*                                                                       */
  /* <Function>                                                            */
  /*    BBox_Conic_Check                                                   */
  /*                                                                       */
  /* <Description>                                                         */
G
Grissiom 已提交
132
  /*    Find the extrema of a 1-dimensional conic Bezier curve and update  */
M
Ming, Bai 已提交
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
  /*    a bounding range.  This version uses direct computation, as it     */
  /*    doesn't need square roots.                                         */
  /*                                                                       */
  /* <Input>                                                               */
  /*    y1  :: The start coordinate.                                       */
  /*                                                                       */
  /*    y2  :: The coordinate of the control point.                        */
  /*                                                                       */
  /*    y3  :: The end coordinate.                                         */
  /*                                                                       */
  /* <InOut>                                                               */
  /*    min :: The address of the current minimum.                         */
  /*                                                                       */
  /*    max :: The address of the current maximum.                         */
  /*                                                                       */
  static void
  BBox_Conic_Check( FT_Pos   y1,
                    FT_Pos   y2,
                    FT_Pos   y3,
                    FT_Pos*  min,
                    FT_Pos*  max )
  {
G
Grissiom 已提交
155 156 157 158 159 160 161 162 163 164 165 166 167
    /* This function is only called when a control off-point is outside */
    /* the bbox that contains all on-points.  It finds a local extremum */
    /* within the segment, equal to (y1*y3 - y2*y2)/(y1 - 2*y2 + y3).   */
    /* Or, offsetting from y2, we get                                   */

    y1 -= y2;
    y3 -= y2;
    y2 += FT_MulDiv( y1, y3, y1 + y3 );

    if ( y2 < *min )
      *min = y2;
    if ( y2 > *max )
      *max = y2;
M
Ming, Bai 已提交
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
  }


  /*************************************************************************/
  /*                                                                       */
  /* <Function>                                                            */
  /*    BBox_Conic_To                                                      */
  /*                                                                       */
  /* <Description>                                                         */
  /*    This function is used as a `conic_to' emitter during               */
  /*    FT_Outline_Decompose().  It checks a conic Bezier curve with the   */
  /*    current bounding box, and computes its extrema if necessary to     */
  /*    update it.                                                         */
  /*                                                                       */
  /* <Input>                                                               */
  /*    control :: A pointer to a control point.                           */
  /*                                                                       */
  /*    to      :: A pointer to the destination vector.                    */
  /*                                                                       */
  /* <InOut>                                                               */
  /*    user    :: The address of the current walk context.                */
  /*                                                                       */
  /* <Return>                                                              */
  /*    Always 0.  Needed for the interface only.                          */
  /*                                                                       */
  /* <Note>                                                                */
  /*    In the case of a non-monotonous arc, we compute directly the       */
  /*    extremum coordinates, as it is sufficiently fast.                  */
  /*                                                                       */
  static int
  BBox_Conic_To( FT_Vector*  control,
                 FT_Vector*  to,
                 TBBox_Rec*  user )
  {
G
Grissiom 已提交
202 203
    /* in case `to' is implicit and not included in bbox yet */
    FT_UPDATE_BBOX( to, user->bbox );
M
Ming, Bai 已提交
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230

    if ( CHECK_X( control, user->bbox ) )
      BBox_Conic_Check( user->last.x,
                        control->x,
                        to->x,
                        &user->bbox.xMin,
                        &user->bbox.xMax );

    if ( CHECK_Y( control, user->bbox ) )
      BBox_Conic_Check( user->last.y,
                        control->y,
                        to->y,
                        &user->bbox.yMin,
                        &user->bbox.yMax );

    user->last = *to;

    return 0;
  }


  /*************************************************************************/
  /*                                                                       */
  /* <Function>                                                            */
  /*    BBox_Cubic_Check                                                   */
  /*                                                                       */
  /* <Description>                                                         */
G
Grissiom 已提交
231 232 233
  /*    Find the extrema of a 1-dimensional cubic Bezier curve and         */
  /*    update a bounding range.  This version uses iterative splitting    */
  /*    because it is faster than the exact solution with square roots.    */
M
Ming, Bai 已提交
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
  /*                                                                       */
  /* <Input>                                                               */
  /*    p1  :: The start coordinate.                                       */
  /*                                                                       */
  /*    p2  :: The coordinate of the first control point.                  */
  /*                                                                       */
  /*    p3  :: The coordinate of the second control point.                 */
  /*                                                                       */
  /*    p4  :: The end coordinate.                                         */
  /*                                                                       */
  /* <InOut>                                                               */
  /*    min :: The address of the current minimum.                         */
  /*                                                                       */
  /*    max :: The address of the current maximum.                         */
  /*                                                                       */
G
Grissiom 已提交
249 250 251 252 253
  static FT_Pos
  cubic_peak( FT_Pos  q1,
              FT_Pos  q2,
              FT_Pos  q3,
              FT_Pos  q4 )
M
Ming, Bai 已提交
254
  {
G
Grissiom 已提交
255 256 257
    FT_Pos  peak = 0;
    FT_Int  shift;

258

G
Grissiom 已提交
259 260 261 262 263 264 265 266 267
    /* This function finds a peak of a cubic segment if it is above 0    */
    /* using iterative bisection of the segment, or returns 0.           */
    /* The fixed-point arithmetic of bisection is inherently stable      */
    /* but may loose accuracy in the two lowest bits.  To compensate,    */
    /* we upscale the segment if there is room.  Large values may need   */
    /* to be downscaled to avoid overflows during bisection.             */
    /* It is called with either q2 or q3 positive, which is necessary    */
    /* for the peak to exist and avoids undefined FT_MSB.                */

268 269 270 271
    shift = 27 - FT_MSB( (FT_UInt32)( FT_ABS( q1 ) |
                                      FT_ABS( q2 ) |
                                      FT_ABS( q3 ) |
                                      FT_ABS( q4 ) ) );
G
Grissiom 已提交
272 273

    if ( shift > 0 )
M
Ming, Bai 已提交
274
    {
G
Grissiom 已提交
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
      /* upscaling too much just wastes time */
      if ( shift > 2 )
        shift = 2;

      q1 <<=  shift;
      q2 <<=  shift;
      q3 <<=  shift;
      q4 <<=  shift;
    }
    else
    {
      q1 >>= -shift;
      q2 >>= -shift;
      q3 >>= -shift;
      q4 >>= -shift;
    }
M
Ming, Bai 已提交
291

G
Grissiom 已提交
292 293 294 295 296 297
    /* for a peak to exist above 0, the cubic segment must have */
    /* at least one of its control off-points above 0.          */
    while ( q2 > 0 || q3 > 0 )
    {
      /* determine which half contains the maximum and split */
      if ( q1 + q2 > q3 + q4 ) /* first half */
M
Ming, Bai 已提交
298
      {
G
Grissiom 已提交
299 300 301 302 303 304 305 306
        q4 = q4 + q3;
        q3 = q3 + q2;
        q2 = q2 + q1;
        q4 = q4 + q3;
        q3 = q3 + q2;
        q4 = ( q4 + q3 ) / 8;
        q3 = q3 / 4;
        q2 = q2 / 2;
M
Ming, Bai 已提交
307
      }
G
Grissiom 已提交
308
      else                     /* second half */
M
Ming, Bai 已提交
309
      {
G
Grissiom 已提交
310 311 312 313 314 315 316 317
        q1 = q1 + q2;
        q2 = q2 + q3;
        q3 = q3 + q4;
        q1 = q1 + q2;
        q2 = q2 + q3;
        q1 = ( q1 + q2 ) / 8;
        q2 = q2 / 4;
        q3 = q3 / 2;
M
Ming, Bai 已提交
318
      }
G
Grissiom 已提交
319 320 321

      /* check whether either end reached the maximum */
      if ( q1 == q2 && q1 >= q3 )
M
Ming, Bai 已提交
322
      {
G
Grissiom 已提交
323 324
        peak = q1;
        break;
M
Ming, Bai 已提交
325
      }
G
Grissiom 已提交
326 327 328 329 330 331
      if ( q3 == q4 && q2 <= q4 )
      {
        peak = q4;
        break;
      }
    }
M
Ming, Bai 已提交
332

G
Grissiom 已提交
333 334 335 336
    if ( shift > 0 )
      peak >>=  shift;
    else
      peak <<= -shift;
M
Ming, Bai 已提交
337

G
Grissiom 已提交
338
    return peak;
M
Ming, Bai 已提交
339 340 341 342
  }


  static void
G
Grissiom 已提交
343 344 345 346
  BBox_Cubic_Check( FT_Pos   p1,
                    FT_Pos   p2,
                    FT_Pos   p3,
                    FT_Pos   p4,
M
Ming, Bai 已提交
347 348 349
                    FT_Pos*  min,
                    FT_Pos*  max )
  {
G
Grissiom 已提交
350 351 352 353
    /* This function is only called when a control off-point is outside  */
    /* the bbox that contains all on-points.  So at least one of the     */
    /* conditions below holds and cubic_peak is called with at least one */
    /* non-zero argument.                                                */
M
Ming, Bai 已提交
354

G
Grissiom 已提交
355 356
    if ( p2 > *max || p3 > *max )
      *max += cubic_peak( p1 - *max, p2 - *max, p3 - *max, p4 - *max );
M
Ming, Bai 已提交
357

G
Grissiom 已提交
358 359 360
    /* now flip the signs to update the minimum */
    if ( p2 < *min || p3 < *min )
      *min -= cubic_peak( *min - p1, *min - p2, *min - p3, *min - p4 );
M
Ming, Bai 已提交
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
  }


  /*************************************************************************/
  /*                                                                       */
  /* <Function>                                                            */
  /*    BBox_Cubic_To                                                      */
  /*                                                                       */
  /* <Description>                                                         */
  /*    This function is used as a `cubic_to' emitter during               */
  /*    FT_Outline_Decompose().  It checks a cubic Bezier curve with the   */
  /*    current bounding box, and computes its extrema if necessary to     */
  /*    update it.                                                         */
  /*                                                                       */
  /* <Input>                                                               */
  /*    control1 :: A pointer to the first control point.                  */
  /*                                                                       */
  /*    control2 :: A pointer to the second control point.                 */
  /*                                                                       */
  /*    to       :: A pointer to the destination vector.                   */
  /*                                                                       */
  /* <InOut>                                                               */
  /*    user     :: The address of the current walk context.               */
  /*                                                                       */
  /* <Return>                                                              */
  /*    Always 0.  Needed for the interface only.                          */
  /*                                                                       */
  /* <Note>                                                                */
  /*    In the case of a non-monotonous arc, we don't compute directly     */
  /*    extremum coordinates, we subdivide instead.                        */
  /*                                                                       */
  static int
  BBox_Cubic_To( FT_Vector*  control1,
                 FT_Vector*  control2,
                 FT_Vector*  to,
                 TBBox_Rec*  user )
  {
G
Grissiom 已提交
398 399 400
    /* We don't need to check `to' since it is always an on-point,    */
    /* thus within the bbox.  Only segments with an off-point outside */
    /* the bbox can possibly reach new extreme values.                */
M
Ming, Bai 已提交
401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424

    if ( CHECK_X( control1, user->bbox ) ||
         CHECK_X( control2, user->bbox ) )
      BBox_Cubic_Check( user->last.x,
                        control1->x,
                        control2->x,
                        to->x,
                        &user->bbox.xMin,
                        &user->bbox.xMax );

    if ( CHECK_Y( control1, user->bbox ) ||
         CHECK_Y( control2, user->bbox ) )
      BBox_Cubic_Check( user->last.y,
                        control1->y,
                        control2->y,
                        to->y,
                        &user->bbox.yMin,
                        &user->bbox.yMax );

    user->last = *to;

    return 0;
  }

G
Grissiom 已提交
425 426

  FT_DEFINE_OUTLINE_FUNCS(bbox_interface,
M
Ming, Bai 已提交
427
    (FT_Outline_MoveTo_Func) BBox_Move_To,
G
Grissiom 已提交
428
    (FT_Outline_LineTo_Func) BBox_Line_To,
M
Ming, Bai 已提交
429 430 431 432 433
    (FT_Outline_ConicTo_Func)BBox_Conic_To,
    (FT_Outline_CubicTo_Func)BBox_Cubic_To,
    0, 0
  )

G
Grissiom 已提交
434

M
Ming, Bai 已提交
435 436 437 438 439 440
  /* documentation is in ftbbox.h */

  FT_EXPORT_DEF( FT_Error )
  FT_Outline_Get_BBox( FT_Outline*  outline,
                       FT_BBox     *abbox )
  {
G
Grissiom 已提交
441 442 443 444
    FT_BBox     cbox = {  0x7FFFFFFFL,  0x7FFFFFFFL,
                         -0x7FFFFFFFL, -0x7FFFFFFFL };
    FT_BBox     bbox = {  0x7FFFFFFFL,  0x7FFFFFFFL,
                         -0x7FFFFFFFL, -0x7FFFFFFFL };
M
Ming, Bai 已提交
445 446 447 448 449
    FT_Vector*  vec;
    FT_UShort   n;


    if ( !abbox )
G
Grissiom 已提交
450
      return FT_THROW( Invalid_Argument );
M
Ming, Bai 已提交
451 452

    if ( !outline )
G
Grissiom 已提交
453
      return FT_THROW( Invalid_Outline );
M
Ming, Bai 已提交
454 455 456 457 458 459 460 461 462 463 464 465 466 467 468

    /* if outline is empty, return (0,0,0,0) */
    if ( outline->n_points == 0 || outline->n_contours <= 0 )
    {
      abbox->xMin = abbox->xMax = 0;
      abbox->yMin = abbox->yMax = 0;
      return 0;
    }

    /* We compute the control box as well as the bounding box of  */
    /* all `on' points in the outline.  Then, if the two boxes    */
    /* coincide, we exit immediately.                             */

    vec = outline->points;

G
Grissiom 已提交
469
    for ( n = 0; n < outline->n_points; n++ )
M
Ming, Bai 已提交
470
    {
G
Grissiom 已提交
471
      FT_UPDATE_BBOX( vec, cbox);
M
Ming, Bai 已提交
472 473

      if ( FT_CURVE_TAG( outline->tags[n] ) == FT_CURVE_TAG_ON )
G
Grissiom 已提交
474
        FT_UPDATE_BBOX( vec, bbox);
M
Ming, Bai 已提交
475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509

      vec++;
    }

    /* test two boxes for equality */
    if ( cbox.xMin < bbox.xMin || cbox.xMax > bbox.xMax ||
         cbox.yMin < bbox.yMin || cbox.yMax > bbox.yMax )
    {
      /* the two boxes are different, now walk over the outline to */
      /* get the Bezier arc extrema.                               */

      FT_Error   error;
      TBBox_Rec  user;

#ifdef FT_CONFIG_OPTION_PIC
      FT_Outline_Funcs bbox_interface;
      Init_Class_bbox_interface(&bbox_interface);
#endif

      user.bbox = bbox;

      error = FT_Outline_Decompose( outline, &bbox_interface, &user );
      if ( error )
        return error;

      *abbox = user.bbox;
    }
    else
      *abbox = bbox;

    return FT_Err_Ok;
  }


/* END */