xref: /aosp_15_r20/external/pdfium/third_party/agg23/agg_curves.cpp (revision 3ac0a46f773bac49fa9476ec2b1cf3f8da5ec3a4)
1 
2 //----------------------------------------------------------------------------
3 // XYQ: 2006-01-22 Copied from AGG project.
4 // TODO: This file uses intensive floating point operations, so it's NOT suitable
5 // for platforms like Symbian OS. We need to change to FIX format.
6 //----------------------------------------------------------------------------
7 //----------------------------------------------------------------------------
8 // Anti-Grain Geometry - Version 2.3
9 // Copyright (C) 2002-2005 Maxim Shemanarev (http://www.antigrain.com)
10 //
11 // Permission to copy, use, modify, sell and distribute this software
12 // is granted provided this copyright notice appears in all copies.
13 // This software is provided "as is" without express or implied
14 // warranty, and with no claim as to its suitability for any purpose.
15 //
16 //----------------------------------------------------------------------------
17 // Contact: [email protected]
18 //          [email protected]
19 //          http://www.antigrain.com
20 //----------------------------------------------------------------------------
21 
22 #include "agg_curves.h"
23 #include "agg_math.h"
24 
25 namespace pdfium
26 {
27 namespace agg
28 {
29 const float curve_collinearity_epsilon              = 1e-30f;
30 enum curve_recursion_limit_e { curve_recursion_limit = 16 };
init(float x1,float y1,float x2,float y2,float x3,float y3,float x4,float y4)31 void curve4_div::init(float x1, float y1,
32                       float x2, float y2,
33                       float x3, float y3,
34                       float x4, float y4)
35 {
36     m_points.remove_all();
37     m_distance_tolerance_square = 1.0f / 4;
38     m_distance_tolerance_manhattan = 1.0f * 4;
39     bezier(x1, y1, x2, y2, x3, y3, x4, y4);
40     m_count = 0;
41 }
recursive_bezier(float x1,float y1,float x2,float y2,float x3,float y3,float x4,float y4,unsigned level)42 void curve4_div::recursive_bezier(float x1, float y1,
43                                   float x2, float y2,
44                                   float x3, float y3,
45                                   float x4, float y4,
46                                   unsigned level)
47 {
48     if(level > curve_recursion_limit) {
49         return;
50     }
51     float x12   = (x1 + x2) / 2;
52     float y12   = (y1 + y2) / 2;
53     float x23   = (x2 + x3) / 2;
54     float y23   = (y2 + y3) / 2;
55     float x34   = (x3 + x4) / 2;
56     float y34   = (y3 + y4) / 2;
57     float x123  = (x12 + x23) / 2;
58     float y123  = (y12 + y23) / 2;
59     float x234  = (x23 + x34) / 2;
60     float y234  = (y23 + y34) / 2;
61     float x1234 = (x123 + x234) / 2;
62     float y1234 = (y123 + y234) / 2;
63     float dx = x4 - x1;
64     float dy = y4 - y1;
65     float d2 = fabs(((x2 - x4) * dy) - ((y2 - y4) * dx));
66     float d3 = fabs(((x3 - x4) * dy) - ((y3 - y4) * dx));
67     switch((int(d2 > curve_collinearity_epsilon) << 1) +
68             int(d3 > curve_collinearity_epsilon)) {
69         case 0:
70           if (fabs(x1 + x3 - x2 - x2) + fabs(y1 + y3 - y2 - y2) +
71                   fabs(x2 + x4 - x3 - x3) + fabs(y2 + y4 - y3 - y3) <=
72               m_distance_tolerance_manhattan) {
73             m_points.add(point_type(x1234, y1234, path_flags_jr));
74             return;
75             }
76             break;
77         case 1:
78           if ((d3 * d3) <=
79               (m_distance_tolerance_square * ((dx * dx) + (dy * dy)))) {
80                 m_points.add(point_type(x23, y23, path_flags_jr));
81                 return;
82             }
83             break;
84         case 2:
85           if ((d2 * d2) <=
86               (m_distance_tolerance_square * ((dx * dx) + (dy * dy)))) {
87                 m_points.add(point_type(x23, y23, path_flags_jr));
88                 return;
89             }
90             break;
91         case 3:
92           if (((d2 + d3) * (d2 + d3)) <=
93               (m_distance_tolerance_square * ((dx * dx) + (dy * dy)))) {
94                 m_points.add(point_type(x23, y23, path_flags_jr));
95                 return;
96             }
97             break;
98     }
99     recursive_bezier(x1, y1, x12, y12, x123, y123, x1234, y1234, level + 1);
100     recursive_bezier(x1234, y1234, x234, y234, x34, y34, x4, y4, level + 1);
101 }
bezier(float x1,float y1,float x2,float y2,float x3,float y3,float x4,float y4)102 void curve4_div::bezier(float x1, float y1,
103                         float x2, float y2,
104                         float x3, float y3,
105                         float x4, float y4)
106 {
107     m_points.add(point_type(x1, y1));
108     recursive_bezier(x1, y1, x2, y2, x3, y3, x4, y4, 0);
109     m_points.add(point_type(x4, y4));
110 }
111 }
112 }  // namespace pdfium
113