1 // Copyright 2016 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
7 #include "xfa/fxgraphics/cxfa_gepath.h"
8
9 #include "core/fxge/cfx_pathdata.h"
10 #include "third_party/base/ptr_util.h"
11
CXFA_GEPath()12 CXFA_GEPath::CXFA_GEPath() {}
13
~CXFA_GEPath()14 CXFA_GEPath::~CXFA_GEPath() {}
15
Clear()16 void CXFA_GEPath::Clear() {
17 data_.Clear();
18 }
19
Close()20 void CXFA_GEPath::Close() {
21 data_.ClosePath();
22 }
23
MoveTo(const CFX_PointF & point)24 void CXFA_GEPath::MoveTo(const CFX_PointF& point) {
25 data_.AppendPoint(point, FXPT_TYPE::MoveTo, false);
26 }
27
LineTo(const CFX_PointF & point)28 void CXFA_GEPath::LineTo(const CFX_PointF& point) {
29 data_.AppendPoint(point, FXPT_TYPE::LineTo, false);
30 }
31
BezierTo(const CFX_PointF & c1,const CFX_PointF & c2,const CFX_PointF & to)32 void CXFA_GEPath::BezierTo(const CFX_PointF& c1,
33 const CFX_PointF& c2,
34 const CFX_PointF& to) {
35 data_.AppendPoint(c1, FXPT_TYPE::BezierTo, false);
36 data_.AppendPoint(c2, FXPT_TYPE::BezierTo, false);
37 data_.AppendPoint(to, FXPT_TYPE::BezierTo, false);
38 }
39
ArcTo(const CFX_PointF & pos,const CFX_SizeF & size,float start_angle,float sweep_angle)40 void CXFA_GEPath::ArcTo(const CFX_PointF& pos,
41 const CFX_SizeF& size,
42 float start_angle,
43 float sweep_angle) {
44 CFX_SizeF new_size = size / 2.0f;
45 ArcToInternal(CFX_PointF(pos.x + new_size.width, pos.y + new_size.height),
46 new_size, start_angle, sweep_angle);
47 }
48
ArcToInternal(const CFX_PointF & pos,const CFX_SizeF & size,float start_angle,float sweep_angle)49 void CXFA_GEPath::ArcToInternal(const CFX_PointF& pos,
50 const CFX_SizeF& size,
51 float start_angle,
52 float sweep_angle) {
53 float x0 = cos(sweep_angle / 2);
54 float y0 = sin(sweep_angle / 2);
55 float tx = ((1.0f - x0) * 4) / (3 * 1.0f);
56 float ty = y0 - ((tx * x0) / y0);
57
58 CFX_PointF points[] = {CFX_PointF(x0 + tx, -ty), CFX_PointF(x0 + tx, ty)};
59 float sn = sin(start_angle + sweep_angle / 2);
60 float cs = cos(start_angle + sweep_angle / 2);
61
62 CFX_PointF bezier;
63 bezier.x = pos.x + (size.width * ((points[0].x * cs) - (points[0].y * sn)));
64 bezier.y = pos.y + (size.height * ((points[0].x * sn) + (points[0].y * cs)));
65 data_.AppendPoint(bezier, FXPT_TYPE::BezierTo, false);
66
67 bezier.x = pos.x + (size.width * ((points[1].x * cs) - (points[1].y * sn)));
68 bezier.y = pos.y + (size.height * ((points[1].x * sn) + (points[1].y * cs)));
69 data_.AppendPoint(bezier, FXPT_TYPE::BezierTo, false);
70
71 bezier.x = pos.x + (size.width * cos(start_angle + sweep_angle));
72 bezier.y = pos.y + (size.height * sin(start_angle + sweep_angle));
73 data_.AppendPoint(bezier, FXPT_TYPE::BezierTo, false);
74 }
75
AddLine(const CFX_PointF & p1,const CFX_PointF & p2)76 void CXFA_GEPath::AddLine(const CFX_PointF& p1, const CFX_PointF& p2) {
77 data_.AppendPoint(p1, FXPT_TYPE::MoveTo, false);
78 data_.AppendPoint(p2, FXPT_TYPE::LineTo, false);
79 }
80
AddRectangle(float left,float top,float width,float height)81 void CXFA_GEPath::AddRectangle(float left,
82 float top,
83 float width,
84 float height) {
85 data_.AppendRect(left, top, left + width, top + height);
86 }
87
AddEllipse(const CFX_RectF & rect)88 void CXFA_GEPath::AddEllipse(const CFX_RectF& rect) {
89 AddArc(rect.TopLeft(), rect.Size(), 0, FX_PI * 2);
90 }
91
AddArc(const CFX_PointF & original_pos,const CFX_SizeF & original_size,float start_angle,float sweep_angle)92 void CXFA_GEPath::AddArc(const CFX_PointF& original_pos,
93 const CFX_SizeF& original_size,
94 float start_angle,
95 float sweep_angle) {
96 if (sweep_angle == 0)
97 return;
98
99 const float bezier_arc_angle_epsilon = 0.01f;
100 while (start_angle > FX_PI * 2)
101 start_angle -= FX_PI * 2;
102 while (start_angle < 0)
103 start_angle += FX_PI * 2;
104 if (sweep_angle >= FX_PI * 2)
105 sweep_angle = FX_PI * 2;
106 if (sweep_angle <= -FX_PI * 2)
107 sweep_angle = -FX_PI * 2;
108
109 CFX_SizeF size = original_size / 2;
110 CFX_PointF pos(original_pos.x + size.width, original_pos.y + size.height);
111 data_.AppendPoint(pos + CFX_PointF(size.width * cos(start_angle),
112 size.height * sin(start_angle)),
113 FXPT_TYPE::MoveTo, false);
114
115 float total_sweep = 0;
116 float local_sweep = 0;
117 float prev_sweep = 0;
118 bool done = false;
119 do {
120 if (sweep_angle < 0) {
121 prev_sweep = total_sweep;
122 local_sweep = -FX_PI / 2;
123 total_sweep -= FX_PI / 2;
124 if (total_sweep <= sweep_angle + bezier_arc_angle_epsilon) {
125 local_sweep = sweep_angle - prev_sweep;
126 done = true;
127 }
128 } else {
129 prev_sweep = total_sweep;
130 local_sweep = FX_PI / 2;
131 total_sweep += FX_PI / 2;
132 if (total_sweep >= sweep_angle - bezier_arc_angle_epsilon) {
133 local_sweep = sweep_angle - prev_sweep;
134 done = true;
135 }
136 }
137
138 ArcToInternal(pos, size, start_angle, local_sweep);
139 start_angle += local_sweep;
140 } while (!done);
141 }
142
AddSubpath(CXFA_GEPath * path)143 void CXFA_GEPath::AddSubpath(CXFA_GEPath* path) {
144 if (!path)
145 return;
146 data_.Append(&path->data_, nullptr);
147 }
148
TransformBy(const CFX_Matrix & mt)149 void CXFA_GEPath::TransformBy(const CFX_Matrix& mt) {
150 data_.Transform(&mt);
151 }
152