freetype-commit
[Top][All Lists]

## [freetype2] master 2ea511e: [smooth] Simplify cubic Bézier flattening.

 From: Alexei Podtelezhnikov Subject: [freetype2] master 2ea511e: [smooth] Simplify cubic Bézier flattening. Date: Mon, 29 Apr 2019 22:50:45 -0400 (EDT)

```branch: master
Author: Alexei Podtelezhnikov <address@hidden>
Commit: Alexei Podtelezhnikov <address@hidden>

[smooth] Simplify cubic Bézier flattening.

The previous implementation is correct but it is too complex.
The revised algorithm is based on the fact that each split moves
the control points closer to the trisection points on the chord.
The corresponding distances are good surrogates for the curve
deviation from the straight line.

This cubic flattening algorithm is somewhat similar to the conic
algorithm based the distance from the control point to the middle of
the chord.  The cubic distances, however, decrease less predictably
but are easy enough to calculate on each step.

* src/smooth/ftgrays.c (gray_render_cubic): Replace the split
condition.
---
ChangeLog            | 18 ++++++++++++++++++
src/smooth/ftgrays.c | 49 +++++++------------------------------------------
2 files changed, 25 insertions(+), 42 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index dd9f48c..1efd28a 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,21 @@
+2019-04-29  Alexei Podtelezhnikov  <address@hidden>
+
+       [smooth] Simplify cubic Bézier flattening.
+
+       The previous implementation is correct but it is too complex.
+       The revised algorithm is based on the fact that each split moves
+       the control points closer to the trisection points on the chord.
+       The corresponding distances are good surrogates for the curve
+       deviation from the straight line.
+
+       This cubic flattening algorithm is somewhat similar to the conic
+       algorithm based the distance from the control point to the middle of
+       the chord.  The cubic distances, however, decrease less predictably
+       but are easy enough to calculate on each step.
+
+       * src/smooth/ftgrays.c (gray_render_cubic): Replace the split
+       condition.
+
2019-04-26  Alexei Podtelezhnikov  <address@hidden>

[smooth] Bithacks and cosmetics.
diff --git a/src/smooth/ftgrays.c b/src/smooth/ftgrays.c
index b421fc8..72ab546 100644
--- a/src/smooth/ftgrays.c
+++ b/src/smooth/ftgrays.c
@@ -1093,9 +1093,6 @@ typedef ptrdiff_t  FT_PtrDist;
{
FT_Vector   bez_stack[16 * 3 + 1];  /* enough to accommodate bisections */
FT_Vector*  arc = bez_stack;
-    TPos        dx, dy, dx_, dy_;
-    TPos        dx1, dy1, dx2, dy2;
-    TPos        L, s, s_limit;

arc.x = UPSCALE( to->x );
@@ -1124,45 +1121,13 @@ typedef ptrdiff_t  FT_PtrDist;

for (;;)
{
-      /* Decide whether to split or draw. See `Rapid Termination          */
-      /* Evaluation for Recursive Subdivision of Bezier Curves' by Thomas */
-      /* F. Hain, at                                                      */
-      /*
*/
-
-      /* dx and dy are x and y components of the P0-P3 chord vector. */
-      dx = dx_ = arc.x - arc.x;
-      dy = dy_ = arc.y - arc.y;
-
-      L = FT_HYPOT( dx_, dy_ );
-
-      /* Avoid possible arithmetic overflow below by splitting. */
-      if ( L > 32767 )
-        goto Split;
-
-      /* Max deviation may be as much as (s/L) * 3/4 (if Hain's v = 1). */
-      s_limit = L * (TPos)( ONE_PIXEL / 6 );
-
-      /* s is L * the perpendicular distance from P1 to the line P0-P3. */
-      dx1 = arc.x - arc.x;
-      dy1 = arc.y - arc.y;
-      s = FT_ABS( SUB_LONG( MUL_LONG( dy, dx1 ), MUL_LONG( dx, dy1 ) ) );
-
-      if ( s > s_limit )
-        goto Split;
-
-      /* s is L * the perpendicular distance from P2 to the line P0-P3. */
-      dx2 = arc.x - arc.x;
-      dy2 = arc.y - arc.y;
-      s = FT_ABS( SUB_LONG( MUL_LONG( dy, dx2 ), MUL_LONG( dx, dy2 ) ) );
-
-      if ( s > s_limit )
-        goto Split;
-
-      /* Split super curvy segments where the off points are so far
-         from the chord that the angles P0-P1-P3 or P0-P2-P3 become
-         acute as detected by appropriate dot products. */
-      if ( dx1 * ( dx1 - dx ) + dy1 * ( dy1 - dy ) > 0 ||
-           dx2 * ( dx2 - dx ) + dy2 * ( dy2 - dy ) > 0 )
+      /* with each split, control points quickly converge towards  */
+      /* chord trisection points and the vanishing distances below */
+      /* indicate when the segment is flat enough to draw          */
+      if ( FT_ABS( 2 * arc.x - 3 * arc.x + arc.x ) > ONE_PIXEL / 2 ||
+           FT_ABS( 2 * arc.y - 3 * arc.y + arc.y ) > ONE_PIXEL / 2 ||
+           FT_ABS( arc.x - 3 * arc.x + 2 * arc.x ) > ONE_PIXEL / 2 ||
+           FT_ABS( arc.y - 3 * arc.y + 2 * arc.y ) > ONE_PIXEL / 2 )
goto Split;

gray_render_line( RAS_VAR_ arc.x, arc.y );

```

reply via email to