1 /***************************************************************************/
5 /* FreeType bbox computation (body). */
7 /* Copyright 1996-2001, 2002, 2004, 2006, 2010 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
32 #include FT_INTERNAL_OBJECTS_H
35 typedef struct TBBox_Rec_
43 /*************************************************************************/
49 /* This function is used as a `move_to' and `line_to' emitter during */
50 /* FT_Outline_Decompose(). It simply records the destination point */
51 /* in `user->last'; no further computations are necessary since we */
52 /* use the cbox as the starting bbox which must be refined. */
55 /* to :: A pointer to the destination vector. */
58 /* user :: A pointer to the current walk context. */
61 /* Always 0. Needed for the interface only. */
64 BBox_Move_To( FT_Vector* to,
73 #define CHECK_X( p, bbox ) \
74 ( p->x < bbox.xMin || p->x > bbox.xMax )
76 #define CHECK_Y( p, bbox ) \
77 ( p->y < bbox.yMin || p->y > bbox.yMax )
80 /*************************************************************************/
83 /* BBox_Conic_Check */
86 /* Finds the extrema of a 1-dimensional conic Bezier curve and update */
87 /* a bounding range. This version uses direct computation, as it */
88 /* doesn't need square roots. */
91 /* y1 :: The start coordinate. */
93 /* y2 :: The coordinate of the control point. */
95 /* y3 :: The end coordinate. */
98 /* min :: The address of the current minimum. */
100 /* max :: The address of the current maximum. */
103 BBox_Conic_Check( FT_Pos y1,
109 if ( y1 <= y3 && y2 == y1 ) /* flat arc */
114 if ( y2 >= y1 && y2 <= y3 ) /* ascending arc */
119 if ( y2 >= y3 && y2 <= y1 ) /* descending arc */
128 y1 = y3 = y1 - FT_MulDiv( y2 - y1, y2 - y1, y1 - 2*y2 + y3 );
131 if ( y1 < *min ) *min = y1;
132 if ( y3 > *max ) *max = y3;
136 /*************************************************************************/
142 /* This function is used as a `conic_to' emitter during */
143 /* FT_Outline_Decompose(). It checks a conic Bezier curve with the */
144 /* current bounding box, and computes its extrema if necessary to */
148 /* control :: A pointer to a control point. */
150 /* to :: A pointer to the destination vector. */
153 /* user :: The address of the current walk context. */
156 /* Always 0. Needed for the interface only. */
159 /* In the case of a non-monotonous arc, we compute directly the */
160 /* extremum coordinates, as it is sufficiently fast. */
163 BBox_Conic_To( FT_Vector* control,
167 /* we don't need to check `to' since it is always an `on' point, thus */
168 /* within the bbox */
170 if ( CHECK_X( control, user->bbox ) )
171 BBox_Conic_Check( user->last.x,
177 if ( CHECK_Y( control, user->bbox ) )
178 BBox_Conic_Check( user->last.y,
190 /*************************************************************************/
193 /* BBox_Cubic_Check */
196 /* Finds the extrema of a 1-dimensional cubic Bezier curve and */
197 /* updates a bounding range. This version uses splitting because we */
198 /* don't want to use square roots and extra accuracy. */
201 /* p1 :: The start coordinate. */
203 /* p2 :: The coordinate of the first control point. */
205 /* p3 :: The coordinate of the second control point. */
207 /* p4 :: The end coordinate. */
210 /* min :: The address of the current minimum. */
212 /* max :: The address of the current maximum. */
218 BBox_Cubic_Check( FT_Pos p1,
225 FT_Pos stack[32*3 + 1], *arc;
245 if ( y1 == y2 && y1 == y3 ) /* flat */
250 if ( y2 >= y1 && y2 <= y4 && y3 >= y1 && y3 <= y4 ) /* ascending */
255 if ( y2 >= y4 && y2 <= y1 && y3 >= y4 && y3 <= y1 ) /* descending */
264 /* unknown direction -- split the arc in two */
266 arc[1] = y1 = ( y1 + y2 ) / 2;
267 arc[5] = y4 = ( y4 + y3 ) / 2;
268 y2 = ( y2 + y3 ) / 2;
269 arc[2] = y1 = ( y1 + y2 ) / 2;
270 arc[4] = y4 = ( y4 + y2 ) / 2;
271 arc[3] = ( y1 + y4 ) / 2;
277 if ( y1 < *min ) *min = y1;
278 if ( y4 > *max ) *max = y4;
283 } while ( arc >= stack );
289 test_cubic_extrema( FT_Pos y1,
297 /* FT_Pos a = y4 - 3*y3 + 3*y2 - y1; */
298 FT_Pos b = y3 - 2*y2 + y1;
307 /* The polynomial is */
309 /* P(x) = a*x^3 + 3b*x^2 + 3c*x + d , */
311 /* dP/dx = 3a*x^2 + 6b*x + 3c . */
313 /* However, we also have */
317 /* which implies by subtraction that */
319 /* P(u) = b*u^2 + 2c*u + d . */
321 if ( u > 0 && u < 0x10000L )
323 uu = FT_MulFix( u, u );
324 y = d + FT_MulFix( c, 2*u ) + FT_MulFix( b, uu );
326 if ( y < *min ) *min = y;
327 if ( y > *max ) *max = y;
333 BBox_Cubic_Check( FT_Pos y1,
340 /* always compare first and last points */
341 if ( y1 < *min ) *min = y1;
342 else if ( y1 > *max ) *max = y1;
344 if ( y4 < *min ) *min = y4;
345 else if ( y4 > *max ) *max = y4;
347 /* now, try to see if there are split points here */
350 /* flat or ascending arc test */
351 if ( y1 <= y2 && y2 <= y4 && y1 <= y3 && y3 <= y4 )
356 /* descending arc test */
357 if ( y1 >= y2 && y2 >= y4 && y1 >= y3 && y3 >= y4 )
361 /* There are some split points. Find them. */
363 FT_Pos a = y4 - 3*y3 + 3*y2 - y1;
364 FT_Pos b = y3 - 2*y2 + y1;
370 /* We need to solve `ax^2+2bx+c' here, without floating points! */
371 /* The trick is to normalize to a different representation in order */
372 /* to use our 16.16 fixed point routines. */
374 /* We compute FT_MulFix(b,b) and FT_MulFix(a,c) after normalization. */
375 /* These values must fit into a single 16.16 value. */
377 /* We normalize a, b, and c to `8.16' fixed float values to ensure */
378 /* that its product is held in a `16.16' value. */
385 /* The following computation is based on the fact that for */
386 /* any value `y', if `n' is the position of the most */
387 /* significant bit of `abs(y)' (starting from 0 for the */
388 /* least significant bit), then `y' is in the range */
392 /* We want to shift `a', `b', and `c' concurrently in order */
393 /* to ensure that they all fit in 8.16 values, which maps */
394 /* to the integer range `-2^23..2^23-1'. */
396 /* Necessarily, we need to shift `a', `b', and `c' so that */
397 /* the most significant bit of its absolute values is at */
398 /* _most_ at position 23. */
400 /* We begin by computing `t1' as the bitwise `OR' of the */
401 /* absolute values of `a', `b', `c'. */
403 t1 = (FT_ULong)( ( a >= 0 ) ? a : -a );
404 t2 = (FT_ULong)( ( b >= 0 ) ? b : -b );
406 t2 = (FT_ULong)( ( c >= 0 ) ? c : -c );
409 /* Now we can be sure that the most significant bit of `t1' */
410 /* is the most significant bit of either `a', `b', or `c', */
411 /* depending on the greatest integer range of the particular */
414 /* Next, we compute the `shift', by shifting `t1' as many */
415 /* times as necessary to move its MSB to position 23. This */
416 /* corresponds to a value of `t1' that is in the range */
417 /* 0x40_0000..0x7F_FFFF. */
419 /* Finally, we shift `a', `b', and `c' by the same amount. */
420 /* This ensures that all values are now in the range */
421 /* -2^23..2^23, i.e., they are now expressed as 8.16 */
422 /* fixed-float numbers. This also means that we are using */
423 /* 24 bits of precision to compute the zeros, independently */
424 /* of the range of the original polynomial coefficients. */
426 /* This algorithm should ensure reasonably accurate values */
427 /* for the zeros. Note that they are only expressed with */
428 /* 16 bits when computing the extrema (the zeros need to */
429 /* be in 0..1 exclusive to be considered part of the arc). */
431 if ( t1 == 0 ) /* all coefficients are 0! */
434 if ( t1 > 0x7FFFFFUL )
441 } while ( t1 > 0x7FFFFFUL );
443 /* this loses some bits of precision, but we use 24 of them */
444 /* for the computation anyway */
449 else if ( t1 < 0x400000UL )
456 } while ( t1 < 0x400000UL );
469 t = - FT_DivFix( c, b ) / 2;
470 test_cubic_extrema( y1, y2, y3, y4, t, min, max );
475 /* solve the equation now */
476 d = FT_MulFix( b, b ) - FT_MulFix( a, c );
482 /* there is a single split point at -b/a */
483 t = - FT_DivFix( b, a );
484 test_cubic_extrema( y1, y2, y3, y4, t, min, max );
488 /* there are two solutions; we need to filter them */
489 d = FT_SqrtFixed( (FT_Int32)d );
490 t = - FT_DivFix( b - d, a );
491 test_cubic_extrema( y1, y2, y3, y4, t, min, max );
493 t = - FT_DivFix( b + d, a );
494 test_cubic_extrema( y1, y2, y3, y4, t, min, max );
503 /*************************************************************************/
509 /* This function is used as a `cubic_to' emitter during */
510 /* FT_Outline_Decompose(). It checks a cubic Bezier curve with the */
511 /* current bounding box, and computes its extrema if necessary to */
515 /* control1 :: A pointer to the first control point. */
517 /* control2 :: A pointer to the second control point. */
519 /* to :: A pointer to the destination vector. */
522 /* user :: The address of the current walk context. */
525 /* Always 0. Needed for the interface only. */
528 /* In the case of a non-monotonous arc, we don't compute directly */
529 /* extremum coordinates, we subdivide instead. */
532 BBox_Cubic_To( FT_Vector* control1,
537 /* we don't need to check `to' since it is always an `on' point, thus */
538 /* within the bbox */
540 if ( CHECK_X( control1, user->bbox ) ||
541 CHECK_X( control2, user->bbox ) )
542 BBox_Cubic_Check( user->last.x,
549 if ( CHECK_Y( control1, user->bbox ) ||
550 CHECK_Y( control2, user->bbox ) )
551 BBox_Cubic_Check( user->last.y,
563 FT_DEFINE_OUTLINE_FUNCS(bbox_interface,
564 (FT_Outline_MoveTo_Func) BBox_Move_To,
565 (FT_Outline_LineTo_Func) BBox_Move_To,
566 (FT_Outline_ConicTo_Func)BBox_Conic_To,
567 (FT_Outline_CubicTo_Func)BBox_Cubic_To,
571 /* documentation is in ftbbox.h */
573 FT_EXPORT_DEF( FT_Error )
574 FT_Outline_Get_BBox( FT_Outline* outline,
584 return FT_Err_Invalid_Argument;
587 return FT_Err_Invalid_Outline;
589 /* if outline is empty, return (0,0,0,0) */
590 if ( outline->n_points == 0 || outline->n_contours <= 0 )
592 abbox->xMin = abbox->xMax = 0;
593 abbox->yMin = abbox->yMax = 0;
597 /* We compute the control box as well as the bounding box of */
598 /* all `on' points in the outline. Then, if the two boxes */
599 /* coincide, we exit immediately. */
601 vec = outline->points;
602 bbox.xMin = bbox.xMax = cbox.xMin = cbox.xMax = vec->x;
603 bbox.yMin = bbox.yMax = cbox.yMin = cbox.yMax = vec->y;
606 for ( n = 1; n < outline->n_points; n++ )
612 /* update control box */
613 if ( x < cbox.xMin ) cbox.xMin = x;
614 if ( x > cbox.xMax ) cbox.xMax = x;
616 if ( y < cbox.yMin ) cbox.yMin = y;
617 if ( y > cbox.yMax ) cbox.yMax = y;
619 if ( FT_CURVE_TAG( outline->tags[n] ) == FT_CURVE_TAG_ON )
621 /* update bbox for `on' points only */
622 if ( x < bbox.xMin ) bbox.xMin = x;
623 if ( x > bbox.xMax ) bbox.xMax = x;
625 if ( y < bbox.yMin ) bbox.yMin = y;
626 if ( y > bbox.yMax ) bbox.yMax = y;
632 /* test two boxes for equality */
633 if ( cbox.xMin < bbox.xMin || cbox.xMax > bbox.xMax ||
634 cbox.yMin < bbox.yMin || cbox.yMax > bbox.yMax )
636 /* the two boxes are different, now walk over the outline to */
637 /* get the Bezier arc extrema. */
642 #ifdef FT_CONFIG_OPTION_PIC
643 FT_Outline_Funcs bbox_interface;
644 Init_Class_bbox_interface(&bbox_interface);
649 error = FT_Outline_Decompose( outline, &bbox_interface, &user );