1 /***************************************************************************/
5 /* FreeType bbox computation (body). */
7 /* Copyright 1996-2001 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 /***************************************************************************/
19 /*************************************************************************/
21 /* This component has a _single_ role: to compute exact outline bounding */
24 /*************************************************************************/
31 #include FT_INTERNAL_CALC_H
34 typedef struct TBBox_Rec_
42 /*************************************************************************/
48 /* This function is used as a `move_to' and `line_to' emitter during */
49 /* FT_Outline_Decompose(). It simply records the destination point */
50 /* in `user->last'; no further computations are necessary since we */
51 /* the cbox as the starting bbox which must be refined. */
54 /* to :: A pointer to the destination vector. */
57 /* user :: A pointer to the current walk context. */
60 /* Always 0. Needed for the interface only. */
63 BBox_Move_To( FT_Vector* to,
72 #define CHECK_X( p, bbox ) \
73 ( p->x < bbox.xMin || p->x > bbox.xMax )
75 #define CHECK_Y( p, bbox ) \
76 ( p->y < bbox.yMin || p->y > bbox.yMax )
79 /*************************************************************************/
82 /* BBox_Conic_Check */
85 /* Finds the extrema of a 1-dimensional conic Bezier curve and update */
86 /* a bounding range. This version uses direct computation, as it */
87 /* doesn't need square roots. */
90 /* y1 :: The start coordinate. */
91 /* y2 :: The coordinate of the control point. */
92 /* y3 :: The end coordinate. */
95 /* min :: The address of the current minimum. */
96 /* max :: The address of the current maximum. */
99 BBox_Conic_Check( FT_Pos y1,
107 if ( y2 == y1 ) /* Flat arc */
112 if ( y2 >= y1 && y2 <= y3 ) /* Ascending arc */
117 if ( y2 >= y3 && y2 <= y1 ) /* Descending arc */
126 y1 = y3 = y1 - FT_MulDiv( y2 - y1, y2 - y1, y1 - 2*y2 + y3 );
129 if ( y1 < *min ) *min = y1;
130 if ( y3 > *max ) *max = y3;
134 /*************************************************************************/
140 /* This function is used as a `conic_to' emitter during */
141 /* FT_Raster_Decompose(). It checks a conic Bezier curve with the */
142 /* current bounding box, and computes its extrema if necessary to */
146 /* control :: A pointer to a control point. */
147 /* to :: A pointer to the destination vector. */
150 /* user :: The address of the current walk context. */
153 /* Always 0. Needed for the interface only. */
156 /* In the case of a non-monotonous arc, we compute directly the */
157 /* extremum coordinates, as it is sufficiently fast. */
160 BBox_Conic_To( FT_Vector* control,
164 /* we don't need to check `to' since it is always an `on' point, thus */
165 /* within the bbox */
167 if ( CHECK_X( control, user->bbox ) )
169 BBox_Conic_Check( user->last.x,
175 if ( CHECK_Y( control, user->bbox ) )
177 BBox_Conic_Check( user->last.y,
189 /*************************************************************************/
192 /* BBox_Cubic_Check */
195 /* Finds the extrema of a 1-dimensional cubic Bezier curve and */
196 /* updates a bounding range. This version uses splitting because we */
197 /* don't want to use square roots and extra accuracies. */
200 /* p1 :: The start coordinate. */
201 /* p2 :: The coordinate of the first control point. */
202 /* p3 :: The coordinate of the second control point. */
203 /* p4 :: The end coordinate. */
206 /* min :: The address of the current minimum. */
207 /* max :: The address of the current maximum. */
211 BBox_Cubic_Check( FT_Pos p1,
218 FT_Pos stack[32*3 + 1], *arc;
238 if ( y1 == y2 && y1 == y3 ) /* Flat */
243 if ( y2 >= y1 && y2 <= y4 && y3 >= y1 && y3 <= y4 ) /* Ascending */
248 if ( y2 >= y4 && y2 <= y1 && y3 >= y4 && y3 <= y1 ) /* Descending */
257 /* Unknown direction -- split the arc in two */
259 arc[1] = y1 = ( y1 + y2 ) / 2;
260 arc[5] = y4 = ( y4 + y3 ) / 2;
261 y2 = ( y2 + y3 ) / 2;
262 arc[2] = y1 = ( y1 + y2 ) / 2;
263 arc[4] = y4 = ( y4 + y2 ) / 2;
264 arc[3] = ( y1 + y4 ) / 2;
270 if ( y1 < *min ) *min = y1;
271 if ( y4 > *max ) *max = y4;
276 } while ( arc >= stack );
281 test_cubic_extrema( FT_Pos y1,
289 /* FT_Pos a = y4 - 3*y3 + 3*y2 - y1; */
290 FT_Pos b = y3 - 2*y2 + y1;
301 /* a*x^3 + 3b*x^2 + 3c*x + d . */
303 /* However, we also have */
307 /* which implies that */
309 /* P(u) = b*u^2 + 2c*u + d */
311 if ( u > 0 && u < 0x10000L )
313 uu = FT_MulFix( u, u );
314 y = d + FT_MulFix( c, 2*u ) + FT_MulFix( b, uu );
316 if ( y < *min ) *min = y;
317 if ( y > *max ) *max = y;
323 BBox_Cubic_Check( FT_Pos y1,
330 /* always compare first and last points */
331 if ( y1 < *min ) *min = y1;
332 else if ( y1 > *max ) *max = y1;
334 if ( y4 < *min ) *min = y4;
335 else if ( y4 > *max ) *max = y4;
337 /* now, try to see if there are split points here */
340 /* flat or ascending arc test */
341 if ( y1 <= y2 && y2 <= y4 && y1 <= y3 && y3 <= y4 )
346 /* descending arc test */
347 if ( y1 >= y2 && y2 >= y4 && y1 >= y3 && y3 >= y4 )
351 /* There are some split points. Find them. */
353 FT_Pos a = y4 - 3*y3 + 3*y2 - y1;
354 FT_Pos b = y3 - 2*y2 + y1;
360 /* We need to solve "ax^2+2bx+c" here, without floating points! */
361 /* The trick is to normalize to a different representation in order */
362 /* to use our 16.16 fixed point routines. */
364 /* We compute FT_MulFix(b,b) and FT_MulFix(a,c) after the */
365 /* the normalization. These values must fit into a single 16.16 */
368 /* We normalize a, b, and c to "8.16" fixed float values to ensure */
369 /* that their product is held in a "16.16" value. */
376 /* Technical explanation of what's happening there. */
378 /* The following computation is based on the fact that for */
379 /* any value "y", if "n" is the position of the most */
380 /* significant bit of "abs(y)" (starting from 0 for the */
381 /* least significant bit), then y is in the range */
385 /* We want to shift "a", "b" and "c" concurrently in order */
386 /* to ensure that they all fit in 8.16 values, which maps */
387 /* to the integer range "-2^23..2^23-1". */
389 /* Necessarily, we need to shift "a", "b" and "c" so that */
390 /* the most significant bit of their absolute values is at */
391 /* _most_ at position 23. */
393 /* We begin by computing "t1" as the bitwise "or" of the */
394 /* absolute values of "a", "b", "c". */
396 t1 = (FT_ULong)((a >= 0) ? a : -a );
397 t2 = (FT_ULong)((b >= 0) ? b : -b );
399 t2 = (FT_ULong)((c >= 0) ? c : -c );
402 /* Now, the most significant bit of "t1" is sure to be the */
403 /* msb of one of "a", "b", "c", depending on which one is */
404 /* expressed in the greatest integer range. */
406 /* We now compute the "shift", by shifting "t1" as many */
407 /* times as necessary to move its msb to position 23. */
409 /* This corresponds to a value of t1 that is in the range */
410 /* 0x40_0000..0x7F_FFFF. */
412 /* Finally, we shift "a", "b" and "c" by the same amount. */
413 /* This ensures that all values are now in the range */
414 /* -2^23..2^23, i.e. that they are now expressed as 8.16 */
415 /* fixed float numbers. */
417 /* This also means that we are using 24 bits of precision */
418 /* to compute the zeros, independently of the range of */
419 /* the original polynom coefficients. */
421 /* This should ensure reasonably accurate values for the */
422 /* zeros. Note that the latter are only expressed with */
423 /* 16 bits when computing the extrema (the zeros need to */
424 /* be in 0..1 exclusive to be considered part of the arc). */
426 if ( t1 == 0 ) /* all coefficients are 0! */
429 if ( t1 > 0x7FFFFFUL )
435 } while ( t1 > 0x7FFFFFUL );
437 /* losing some bits of precision, but we use 24 of them */
438 /* for the computation anyway. */
443 else if ( t1 < 0x400000UL )
449 } while ( t1 < 0x400000UL );
462 t = - FT_DivFix( c, b ) / 2;
463 test_cubic_extrema( y1, y2, y3, y4, t, min, max );
468 /* solve the equation now */
469 d = FT_MulFix( b, b ) - FT_MulFix( a, c );
475 /* there is a single split point at -b/a */
476 t = - FT_DivFix( b, a );
477 test_cubic_extrema( y1, y2, y3, y4, t, min, max );
481 /* there are two solutions; we need to filter them though */
482 d = FT_SqrtFixed( (FT_Int32)d );
483 t = - FT_DivFix( b - d, a );
484 test_cubic_extrema( y1, y2, y3, y4, t, min, max );
486 t = - FT_DivFix( b + d, a );
487 test_cubic_extrema( y1, y2, y3, y4, t, min, max );
496 /*************************************************************************/
502 /* This function is used as a `cubic_to' emitter during */
503 /* FT_Raster_Decompose(). It checks a cubic Bezier curve with the */
504 /* current bounding box, and computes its extrema if necessary to */
508 /* control1 :: A pointer to the first control point. */
509 /* control2 :: A pointer to the second control point. */
510 /* to :: A pointer to the destination vector. */
513 /* user :: The address of the current walk context. */
516 /* Always 0. Needed for the interface only. */
519 /* In the case of a non-monotonous arc, we don't compute directly */
520 /* extremum coordinates, we subdivise instead. */
523 BBox_Cubic_To( FT_Vector* control1,
528 /* we don't need to check `to' since it is always an `on' point, thus */
529 /* within the bbox */
531 if ( CHECK_X( control1, user->bbox ) ||
532 CHECK_X( control2, user->bbox ) )
534 BBox_Cubic_Check( user->last.x,
541 if ( CHECK_Y( control1, user->bbox ) ||
542 CHECK_Y( control2, user->bbox ) )
544 BBox_Cubic_Check( user->last.y,
557 /* documentation is in ftbbox.h */
559 FT_EXPORT_DEF( FT_Error )
560 FT_Outline_Get_BBox( FT_Outline* outline,
570 return FT_Err_Invalid_Argument;
573 return FT_Err_Invalid_Outline;
575 /* if outline is empty, return (0,0,0,0) */
576 if ( outline->n_points == 0 || outline->n_contours <= 0 )
578 abbox->xMin = abbox->xMax = 0;
579 abbox->yMin = abbox->yMax = 0;
583 /* We compute the control box as well as the bounding box of */
584 /* all `on' points in the outline. Then, if the two boxes */
585 /* coincide, we exit immediately. */
587 vec = outline->points;
588 bbox.xMin = bbox.xMax = cbox.xMin = cbox.xMax = vec->x;
589 bbox.yMin = bbox.yMax = cbox.yMin = cbox.yMax = vec->y;
592 for ( n = 1; n < outline->n_points; n++ )
598 /* update control box */
599 if ( x < cbox.xMin ) cbox.xMin = x;
600 if ( x > cbox.xMax ) cbox.xMax = x;
602 if ( y < cbox.yMin ) cbox.yMin = y;
603 if ( y > cbox.yMax ) cbox.yMax = y;
605 if ( FT_CURVE_TAG( outline->tags[n] ) == FT_Curve_Tag_On )
607 /* update bbox for `on' points only */
608 if ( x < bbox.xMin ) bbox.xMin = x;
609 if ( x > bbox.xMax ) bbox.xMax = x;
611 if ( y < bbox.yMin ) bbox.yMin = y;
612 if ( y > bbox.yMax ) bbox.yMax = y;
618 /* test two boxes for equality */
619 if ( cbox.xMin < bbox.xMin || cbox.xMax > bbox.xMax ||
620 cbox.yMin < bbox.yMin || cbox.yMax > bbox.yMax )
622 /* the two boxes are different, now walk over the outline to */
623 /* get the Bezier arc extrema. */
625 static const FT_Outline_Funcs interface =
627 (FT_Outline_MoveTo_Func) BBox_Move_To,
628 (FT_Outline_LineTo_Func) BBox_Move_To,
629 (FT_Outline_ConicTo_Func)BBox_Conic_To,
630 (FT_Outline_CubicTo_Func)BBox_Cubic_To,
640 error = FT_Outline_Decompose( outline, &interface, &user );