1 // Copyright 2016 The PDFium Authors
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/fgas/graphics/cfgas_gepath.h"
8
9 #include <math.h>
10
11 #include "core/fxcrt/fx_system.h"
12 #include "core/fxge/cfx_path.h"
13
14 CFGAS_GEPath::CFGAS_GEPath() = default;
15
16 CFGAS_GEPath::~CFGAS_GEPath() = default;
17
Clear()18 void CFGAS_GEPath::Clear() {
19 path_.Clear();
20 }
21
Close()22 void CFGAS_GEPath::Close() {
23 path_.ClosePath();
24 }
25
MoveTo(const CFX_PointF & point)26 void CFGAS_GEPath::MoveTo(const CFX_PointF& point) {
27 path_.AppendPoint(point, CFX_Path::Point::Type::kMove);
28 }
29
LineTo(const CFX_PointF & point)30 void CFGAS_GEPath::LineTo(const CFX_PointF& point) {
31 path_.AppendPoint(point, CFX_Path::Point::Type::kLine);
32 }
33
BezierTo(const CFX_PointF & c1,const CFX_PointF & c2,const CFX_PointF & to)34 void CFGAS_GEPath::BezierTo(const CFX_PointF& c1,
35 const CFX_PointF& c2,
36 const CFX_PointF& to) {
37 path_.AppendPoint(c1, CFX_Path::Point::Type::kBezier);
38 path_.AppendPoint(c2, CFX_Path::Point::Type::kBezier);
39 path_.AppendPoint(to, CFX_Path::Point::Type::kBezier);
40 }
41
ArcTo(const CFX_PointF & pos,const CFX_SizeF & size,float start_angle,float sweep_angle)42 void CFGAS_GEPath::ArcTo(const CFX_PointF& pos,
43 const CFX_SizeF& size,
44 float start_angle,
45 float sweep_angle) {
46 CFX_SizeF new_size = size / 2.0f;
47 ArcToInternal(CFX_PointF(pos.x + new_size.width, pos.y + new_size.height),
48 new_size, start_angle, sweep_angle);
49 }
50
ArcToInternal(const CFX_PointF & pos,const CFX_SizeF & size,float start_angle,float sweep_angle)51 void CFGAS_GEPath::ArcToInternal(const CFX_PointF& pos,
52 const CFX_SizeF& size,
53 float start_angle,
54 float sweep_angle) {
55 float x0 = cos(sweep_angle / 2);
56 float y0 = sin(sweep_angle / 2);
57 float tx = ((1.0f - x0) * 4) / (3 * 1.0f);
58 float ty = y0 - ((tx * x0) / y0);
59
60 CFX_PointF points[] = {CFX_PointF(x0 + tx, -ty), CFX_PointF(x0 + tx, ty)};
61 float sn = sin(start_angle + sweep_angle / 2);
62 float cs = cos(start_angle + sweep_angle / 2);
63
64 CFX_PointF bezier;
65 bezier.x = pos.x + (size.width * ((points[0].x * cs) - (points[0].y * sn)));
66 bezier.y = pos.y + (size.height * ((points[0].x * sn) + (points[0].y * cs)));
67 path_.AppendPoint(bezier, CFX_Path::Point::Type::kBezier);
68
69 bezier.x = pos.x + (size.width * ((points[1].x * cs) - (points[1].y * sn)));
70 bezier.y = pos.y + (size.height * ((points[1].x * sn) + (points[1].y * cs)));
71 path_.AppendPoint(bezier, CFX_Path::Point::Type::kBezier);
72
73 bezier.x = pos.x + (size.width * cos(start_angle + sweep_angle));
74 bezier.y = pos.y + (size.height * sin(start_angle + sweep_angle));
75 path_.AppendPoint(bezier, CFX_Path::Point::Type::kBezier);
76 }
77
AddLine(const CFX_PointF & p1,const CFX_PointF & p2)78 void CFGAS_GEPath::AddLine(const CFX_PointF& p1, const CFX_PointF& p2) {
79 path_.AppendPoint(p1, CFX_Path::Point::Type::kMove);
80 path_.AppendPoint(p2, CFX_Path::Point::Type::kLine);
81 }
82
AddRectangle(float left,float top,float width,float height)83 void CFGAS_GEPath::AddRectangle(float left,
84 float top,
85 float width,
86 float height) {
87 path_.AppendRect(left, top, left + width, top + height);
88 }
89
AddEllipse(const CFX_RectF & rect)90 void CFGAS_GEPath::AddEllipse(const CFX_RectF& rect) {
91 AddArc(rect.TopLeft(), rect.Size(), 0, FXSYS_PI * 2);
92 }
93
AddArc(const CFX_PointF & original_pos,const CFX_SizeF & original_size,float start_angle,float sweep_angle)94 void CFGAS_GEPath::AddArc(const CFX_PointF& original_pos,
95 const CFX_SizeF& original_size,
96 float start_angle,
97 float sweep_angle) {
98 if (sweep_angle == 0)
99 return;
100
101 const float bezier_arc_angle_epsilon = 0.01f;
102 while (start_angle > FXSYS_PI * 2)
103 start_angle -= FXSYS_PI * 2;
104 while (start_angle < 0)
105 start_angle += FXSYS_PI * 2;
106 if (sweep_angle >= FXSYS_PI * 2)
107 sweep_angle = FXSYS_PI * 2;
108 if (sweep_angle <= -FXSYS_PI * 2)
109 sweep_angle = -FXSYS_PI * 2;
110
111 CFX_SizeF size = original_size / 2;
112 CFX_PointF pos(original_pos.x + size.width, original_pos.y + size.height);
113 path_.AppendPoint(pos + CFX_PointF(size.width * cos(start_angle),
114 size.height * sin(start_angle)),
115 CFX_Path::Point::Type::kMove);
116
117 float total_sweep = 0;
118 float local_sweep = 0;
119 float prev_sweep = 0;
120 bool done = false;
121 do {
122 if (sweep_angle < 0) {
123 prev_sweep = total_sweep;
124 local_sweep = -FXSYS_PI / 2;
125 total_sweep -= FXSYS_PI / 2;
126 if (total_sweep <= sweep_angle + bezier_arc_angle_epsilon) {
127 local_sweep = sweep_angle - prev_sweep;
128 done = true;
129 }
130 } else {
131 prev_sweep = total_sweep;
132 local_sweep = FXSYS_PI / 2;
133 total_sweep += FXSYS_PI / 2;
134 if (total_sweep >= sweep_angle - bezier_arc_angle_epsilon) {
135 local_sweep = sweep_angle - prev_sweep;
136 done = true;
137 }
138 }
139
140 ArcToInternal(pos, size, start_angle, local_sweep);
141 start_angle += local_sweep;
142 } while (!done);
143 }
144
AddSubpath(const CFGAS_GEPath & path)145 void CFGAS_GEPath::AddSubpath(const CFGAS_GEPath& path) {
146 path_.Append(path.path_, nullptr);
147 }
148
TransformBy(const CFX_Matrix & mt)149 void CFGAS_GEPath::TransformBy(const CFX_Matrix& mt) {
150 path_.Transform(mt);
151 }
152