1
/***************************************************************************/
5
/* FreeType bbox computation (body). */
7
/* Copyright 1996-2001, 2002, 2004 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
/* use 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. */
92
/* y2 :: The coordinate of the control point. */
94
/* y3 :: The end coordinate. */
97
/* min :: The address of the current minimum. */
99
/* max :: The address of the current maximum. */
102
BBox_Conic_Check( FT_Pos y1,
108
if ( y1 <= y3 && y2 == y1 ) /* flat arc */
113
if ( y2 >= y1 && y2 <= y3 ) /* ascending arc */
118
if ( y2 >= y3 && y2 <= y1 ) /* descending arc */
127
y1 = y3 = y1 - FT_MulDiv( y2 - y1, y2 - y1, y1 - 2*y2 + y3 );
130
if ( y1 < *min ) *min = y1;
131
if ( y3 > *max ) *max = y3;
135
/*************************************************************************/
141
/* This function is used as a `conic_to' emitter during */
142
/* FT_Raster_Decompose(). It checks a conic Bezier curve with the */
143
/* current bounding box, and computes its extrema if necessary to */
147
/* control :: A pointer to a control point. */
149
/* to :: A pointer to the destination vector. */
152
/* user :: The address of the current walk context. */
155
/* Always 0. Needed for the interface only. */
158
/* In the case of a non-monotonous arc, we compute directly the */
159
/* extremum coordinates, as it is sufficiently fast. */
162
BBox_Conic_To( FT_Vector* control,
166
/* we don't need to check `to' since it is always an `on' point, thus */
167
/* within the bbox */
169
if ( CHECK_X( control, user->bbox ) )
170
BBox_Conic_Check( user->last.x,
176
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 accuracy. */
200
/* p1 :: The start coordinate. */
202
/* p2 :: The coordinate of the first control point. */
204
/* p3 :: The coordinate of the second control point. */
206
/* p4 :: The end coordinate. */
209
/* min :: The address of the current minimum. */
211
/* max :: The address of the current maximum. */
217
BBox_Cubic_Check( FT_Pos p1,
224
FT_Pos stack[32*3 + 1], *arc;
244
if ( y1 == y2 && y1 == y3 ) /* flat */
249
if ( y2 >= y1 && y2 <= y4 && y3 >= y1 && y3 <= y4 ) /* ascending */
254
if ( y2 >= y4 && y2 <= y1 && y3 >= y4 && y3 <= y1 ) /* descending */
263
/* unknown direction -- split the arc in two */
265
arc[1] = y1 = ( y1 + y2 ) / 2;
266
arc[5] = y4 = ( y4 + y3 ) / 2;
267
y2 = ( y2 + y3 ) / 2;
268
arc[2] = y1 = ( y1 + y2 ) / 2;
269
arc[4] = y4 = ( y4 + y2 ) / 2;
270
arc[3] = ( y1 + y4 ) / 2;
276
if ( y1 < *min ) *min = y1;
277
if ( y4 > *max ) *max = y4;
282
} while ( arc >= stack );
288
test_cubic_extrema( FT_Pos y1,
296
/* FT_Pos a = y4 - 3*y3 + 3*y2 - y1; */
297
FT_Pos b = y3 - 2*y2 + y1;
308
/* P(x) = a*x^3 + 3b*x^2 + 3c*x + d , */
310
/* dP/dx = 3a*x^2 + 6b*x + 3c . */
312
/* However, we also have */
316
/* which implies by subtraction that */
318
/* P(u) = b*u^2 + 2c*u + d . */
320
if ( u > 0 && u < 0x10000L )
322
uu = FT_MulFix( u, u );
323
y = d + FT_MulFix( c, 2*u ) + FT_MulFix( b, uu );
325
if ( y < *min ) *min = y;
326
if ( y > *max ) *max = y;
332
BBox_Cubic_Check( FT_Pos y1,
339
/* always compare first and last points */
340
if ( y1 < *min ) *min = y1;
341
else if ( y1 > *max ) *max = y1;
343
if ( y4 < *min ) *min = y4;
344
else if ( y4 > *max ) *max = y4;
346
/* now, try to see if there are split points here */
349
/* flat or ascending arc test */
350
if ( y1 <= y2 && y2 <= y4 && y1 <= y3 && y3 <= y4 )
355
/* descending arc test */
356
if ( y1 >= y2 && y2 >= y4 && y1 >= y3 && y3 >= y4 )
360
/* There are some split points. Find them. */
362
FT_Pos a = y4 - 3*y3 + 3*y2 - y1;
363
FT_Pos b = y3 - 2*y2 + y1;
369
/* We need to solve `ax^2+2bx+c' here, without floating points! */
370
/* The trick is to normalize to a different representation in order */
371
/* to use our 16.16 fixed point routines. */
373
/* We compute FT_MulFix(b,b) and FT_MulFix(a,c) after normalization. */
374
/* These values must fit into a single 16.16 value. */
376
/* We normalize a, b, and c to `8.16' fixed float values to ensure */
377
/* that its product is held in a `16.16' value. */
384
/* The following computation is based on the fact that for */
385
/* any value `y', if `n' is the position of the most */
386
/* significant bit of `abs(y)' (starting from 0 for the */
387
/* least significant bit), then `y' is in the range */
391
/* We want to shift `a', `b', and `c' concurrently in order */
392
/* to ensure that they all fit in 8.16 values, which maps */
393
/* to the integer range `-2^23..2^23-1'. */
395
/* Necessarily, we need to shift `a', `b', and `c' so that */
396
/* the most significant bit of its absolute values is at */
397
/* _most_ at position 23. */
399
/* We begin by computing `t1' as the bitwise `OR' of the */
400
/* absolute values of `a', `b', `c'. */
402
t1 = (FT_ULong)( ( a >= 0 ) ? a : -a );
403
t2 = (FT_ULong)( ( b >= 0 ) ? b : -b );
405
t2 = (FT_ULong)( ( c >= 0 ) ? c : -c );
408
/* Now we can be sure that the most significant bit of `t1' */
409
/* is the most significant bit of either `a', `b', or `c', */
410
/* depending on the greatest integer range of the particular */
413
/* Next, we compute the `shift', by shifting `t1' as many */
414
/* times as necessary to move its MSB to position 23. This */
415
/* corresponds to a value of `t1' that is in the range */
416
/* 0x40_0000..0x7F_FFFF. */
418
/* Finally, we shift `a', `b', and `c' by the same amount. */
419
/* This ensures that all values are now in the range */
420
/* -2^23..2^23, i.e., they are now expressed as 8.16 */
421
/* fixed-float numbers. This also means that we are using */
422
/* 24 bits of precision to compute the zeros, independently */
423
/* of the range of the original polynomial coefficients. */
425
/* This algorithm should ensure reasonably accurate values */
426
/* for the zeros. Note that they are only expressed with */
427
/* 16 bits when computing the extrema (the zeros need to */
428
/* be in 0..1 exclusive to be considered part of the arc). */
430
if ( t1 == 0 ) /* all coefficients are 0! */
433
if ( t1 > 0x7FFFFFUL )
440
} while ( t1 > 0x7FFFFFUL );
442
/* this loses some bits of precision, but we use 24 of them */
443
/* for the computation anyway */
448
else if ( t1 < 0x400000UL )
455
} while ( t1 < 0x400000UL );
468
t = - FT_DivFix( c, b ) / 2;
469
test_cubic_extrema( y1, y2, y3, y4, t, min, max );
474
/* solve the equation now */
475
d = FT_MulFix( b, b ) - FT_MulFix( a, c );
481
/* there is a single split point at -b/a */
482
t = - FT_DivFix( b, a );
483
test_cubic_extrema( y1, y2, y3, y4, t, min, max );
487
/* there are two solutions; we need to filter them */
488
d = FT_SqrtFixed( (FT_Int32)d );
489
t = - FT_DivFix( b - d, a );
490
test_cubic_extrema( y1, y2, y3, y4, t, min, max );
492
t = - FT_DivFix( b + d, a );
493
test_cubic_extrema( y1, y2, y3, y4, t, min, max );
502
/*************************************************************************/
508
/* This function is used as a `cubic_to' emitter during */
509
/* FT_Raster_Decompose(). It checks a cubic Bezier curve with the */
510
/* current bounding box, and computes its extrema if necessary to */
514
/* control1 :: A pointer to the first control point. */
516
/* control2 :: A pointer to the second control point. */
518
/* to :: A pointer to the destination vector. */
521
/* user :: The address of the current walk context. */
524
/* Always 0. Needed for the interface only. */
527
/* In the case of a non-monotonous arc, we don't compute directly */
528
/* extremum coordinates, we subdivide instead. */
531
BBox_Cubic_To( FT_Vector* control1,
536
/* we don't need to check `to' since it is always an `on' point, thus */
537
/* within the bbox */
539
if ( CHECK_X( control1, user->bbox ) ||
540
CHECK_X( control2, user->bbox ) )
541
BBox_Cubic_Check( user->last.x,
548
if ( CHECK_Y( control1, user->bbox ) ||
549
CHECK_Y( control2, user->bbox ) )
550
BBox_Cubic_Check( user->last.y,
563
/* documentation is in ftbbox.h */
565
FT_EXPORT_DEF( FT_Error )
566
FT_Outline_Get_BBox( FT_Outline* outline,
576
return FT_Err_Invalid_Argument;
579
return FT_Err_Invalid_Outline;
581
/* if outline is empty, return (0,0,0,0) */
582
if ( outline->n_points == 0 || outline->n_contours <= 0 )
584
abbox->xMin = abbox->xMax = 0;
585
abbox->yMin = abbox->yMax = 0;
589
/* We compute the control box as well as the bounding box of */
590
/* all `on' points in the outline. Then, if the two boxes */
591
/* coincide, we exit immediately. */
593
vec = outline->points;
594
bbox.xMin = bbox.xMax = cbox.xMin = cbox.xMax = vec->x;
595
bbox.yMin = bbox.yMax = cbox.yMin = cbox.yMax = vec->y;
598
for ( n = 1; n < outline->n_points; n++ )
604
/* update control box */
605
if ( x < cbox.xMin ) cbox.xMin = x;
606
if ( x > cbox.xMax ) cbox.xMax = x;
608
if ( y < cbox.yMin ) cbox.yMin = y;
609
if ( y > cbox.yMax ) cbox.yMax = y;
611
if ( FT_CURVE_TAG( outline->tags[n] ) == FT_CURVE_TAG_ON )
613
/* update bbox for `on' points only */
614
if ( x < bbox.xMin ) bbox.xMin = x;
615
if ( x > bbox.xMax ) bbox.xMax = x;
617
if ( y < bbox.yMin ) bbox.yMin = y;
618
if ( y > bbox.yMax ) bbox.yMax = y;
624
/* test two boxes for equality */
625
if ( cbox.xMin < bbox.xMin || cbox.xMax > bbox.xMax ||
626
cbox.yMin < bbox.yMin || cbox.yMax > bbox.yMax )
628
/* the two boxes are different, now walk over the outline to */
629
/* get the Bezier arc extrema. */
631
static const FT_Outline_Funcs bbox_interface =
633
(FT_Outline_MoveTo_Func) BBox_Move_To,
634
(FT_Outline_LineTo_Func) BBox_Move_To,
635
(FT_Outline_ConicTo_Func)BBox_Conic_To,
636
(FT_Outline_CubicTo_Func)BBox_Cubic_To,
646
error = FT_Outline_Decompose( outline, &bbox_interface, &user );