Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(74)

Side by Side Diff: xfa/fde/cfde_path.cpp

Issue 1896893003: Cleanup FDE interfaces. (Closed) Base URL: https://pdfium.googlesource.com/pdfium.git@master
Patch Set: Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « xfa/fde/cfde_path.h ('k') | xfa/fde/fde_gedevice.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 PDFium Authors. All rights reserved. 1 // Copyright 2014 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6 6
7 #include "xfa/fde/fde_geobject.h" 7 #include "xfa/fde/cfde_path.h"
8 8
9 #include "xfa/fde/fde_object.h" 9 #include "xfa/fde/fde_object.h"
10 10
11 IFDE_Path* IFDE_Path::Create() {
12 return new CFDE_Path;
13 }
14 FX_BOOL CFDE_Path::StartFigure() { 11 FX_BOOL CFDE_Path::StartFigure() {
15 return CloseFigure(); 12 return CloseFigure();
16 } 13 }
14
17 FX_BOOL CFDE_Path::CloseFigure() { 15 FX_BOOL CFDE_Path::CloseFigure() {
18 FX_PATHPOINT* pPoint = GetLastPoint(); 16 FX_PATHPOINT* pPoint = GetLastPoint();
19 if (pPoint) { 17 if (pPoint)
20 pPoint->m_Flag |= FXPT_CLOSEFIGURE; 18 pPoint->m_Flag |= FXPT_CLOSEFIGURE;
21 }
22 return TRUE; 19 return TRUE;
23 } 20 }
21
24 FX_PATHPOINT* CFDE_Path::GetLastPoint(int32_t iCount) const { 22 FX_PATHPOINT* CFDE_Path::GetLastPoint(int32_t iCount) const {
25 if (iCount < 1) { 23 if (iCount < 1)
26 return NULL; 24 return nullptr;
27 } 25
28 int32_t iPoints = m_Path.GetPointCount(); 26 int32_t iPoints = m_Path.GetPointCount();
29 if (iCount > iPoints) { 27 if (iCount > iPoints)
30 return NULL; 28 return nullptr;
31 }
32 return m_Path.GetPoints() + iPoints - iCount; 29 return m_Path.GetPoints() + iPoints - iCount;
33 } 30 }
31
34 FX_BOOL CFDE_Path::FigureClosed() const { 32 FX_BOOL CFDE_Path::FigureClosed() const {
35 FX_PATHPOINT* pPoint = GetLastPoint(); 33 FX_PATHPOINT* pPoint = GetLastPoint();
36 return pPoint ? (pPoint->m_Flag & FXPT_CLOSEFIGURE) : TRUE; 34 return pPoint ? (pPoint->m_Flag & FXPT_CLOSEFIGURE) : TRUE;
37 } 35 }
36
38 FX_PATHPOINT* CFDE_Path::AddPoints(int32_t iCount) { 37 FX_PATHPOINT* CFDE_Path::AddPoints(int32_t iCount) {
39 if (iCount < 1) { 38 if (iCount < 1)
40 return NULL; 39 return nullptr;
41 } 40
42 int32_t iPoints = m_Path.GetPointCount(); 41 int32_t iPoints = m_Path.GetPointCount();
43 m_Path.AddPointCount(iCount); 42 m_Path.AddPointCount(iCount);
44 return m_Path.GetPoints() + iPoints; 43 return m_Path.GetPoints() + iPoints;
45 } 44 }
45
46 void CFDE_Path::MoveTo(FX_FLOAT fx, FX_FLOAT fy) { 46 void CFDE_Path::MoveTo(FX_FLOAT fx, FX_FLOAT fy) {
47 FX_PATHPOINT* pPoint = AddPoints(1); 47 FX_PATHPOINT* pPoint = AddPoints(1);
48 pPoint->m_PointX = fx; 48 pPoint->m_PointX = fx;
49 pPoint->m_PointY = fy; 49 pPoint->m_PointY = fy;
50 pPoint->m_Flag = FXPT_MOVETO; 50 pPoint->m_Flag = FXPT_MOVETO;
51 } 51 }
52
52 void CFDE_Path::LineTo(FX_FLOAT fx, FX_FLOAT fy) { 53 void CFDE_Path::LineTo(FX_FLOAT fx, FX_FLOAT fy) {
53 FX_PATHPOINT* pPoint = AddPoints(1); 54 FX_PATHPOINT* pPoint = AddPoints(1);
54 pPoint->m_PointX = fx; 55 pPoint->m_PointX = fx;
55 pPoint->m_PointY = fy; 56 pPoint->m_PointY = fy;
56 pPoint->m_Flag = FXPT_LINETO; 57 pPoint->m_Flag = FXPT_LINETO;
57 } 58 }
59
58 void CFDE_Path::BezierTo(const CFX_PointF& p1, 60 void CFDE_Path::BezierTo(const CFX_PointF& p1,
59 const CFX_PointF& p2, 61 const CFX_PointF& p2,
60 const CFX_PointF& p3) { 62 const CFX_PointF& p3) {
61 FX_PATHPOINT* p = AddPoints(3); 63 FX_PATHPOINT* p = AddPoints(3);
62 p[0].m_PointX = p1.x; 64 p[0].m_PointX = p1.x;
63 p[0].m_PointY = p1.y; 65 p[0].m_PointY = p1.y;
64 p[0].m_Flag = FXPT_BEZIERTO; 66 p[0].m_Flag = FXPT_BEZIERTO;
65 p[1].m_PointX = p2.x; 67 p[1].m_PointX = p2.x;
66 p[1].m_PointY = p2.y; 68 p[1].m_PointY = p2.y;
67 p[1].m_Flag = FXPT_BEZIERTO; 69 p[1].m_Flag = FXPT_BEZIERTO;
68 p[2].m_PointX = p3.x; 70 p[2].m_PointX = p3.x;
69 p[2].m_PointY = p3.y; 71 p[2].m_PointY = p3.y;
70 p[2].m_Flag = FXPT_BEZIERTO; 72 p[2].m_Flag = FXPT_BEZIERTO;
71 } 73 }
74
72 void CFDE_Path::ArcTo(FX_BOOL bStart, 75 void CFDE_Path::ArcTo(FX_BOOL bStart,
73 const CFX_RectF& rect, 76 const CFX_RectF& rect,
74 FX_FLOAT startAngle, 77 FX_FLOAT startAngle,
75 FX_FLOAT endAngle) { 78 FX_FLOAT endAngle) {
76 FX_FLOAT rx = rect.width / 2; 79 FX_FLOAT rx = rect.width / 2;
77 FX_FLOAT ry = rect.height / 2; 80 FX_FLOAT ry = rect.height / 2;
78 FX_FLOAT cx = rect.left + rx; 81 FX_FLOAT cx = rect.left + rx;
79 FX_FLOAT cy = rect.top + ry; 82 FX_FLOAT cy = rect.top + ry;
80 FX_FLOAT alpha = 83 FX_FLOAT alpha =
81 FXSYS_atan2(rx * FXSYS_sin(startAngle), ry * FXSYS_cos(startAngle)); 84 FXSYS_atan2(rx * FXSYS_sin(startAngle), ry * FXSYS_cos(startAngle));
82 FX_FLOAT beta = 85 FX_FLOAT beta =
83 FXSYS_atan2(rx * FXSYS_sin(endAngle), ry * FXSYS_cos(endAngle)); 86 FXSYS_atan2(rx * FXSYS_sin(endAngle), ry * FXSYS_cos(endAngle));
84 if (FXSYS_fabs(beta - alpha) > FX_PI) { 87 if (FXSYS_fabs(beta - alpha) > FX_PI) {
85 if (beta > alpha) { 88 if (beta > alpha)
86 beta -= 2 * FX_PI; 89 beta -= 2 * FX_PI;
87 } else { 90 else
88 alpha -= 2 * FX_PI; 91 alpha -= 2 * FX_PI;
89 }
90 } 92 }
91 FX_FLOAT half_delta = (beta - alpha) / 2; 93 FX_FLOAT half_delta = (beta - alpha) / 2;
92 FX_FLOAT bcp = 4.0f / 3 * (1 - FXSYS_cos(half_delta)) / FXSYS_sin(half_delta); 94 FX_FLOAT bcp = 4.0f / 3 * (1 - FXSYS_cos(half_delta)) / FXSYS_sin(half_delta);
93 FX_FLOAT sin_alpha = FXSYS_sin(alpha); 95 FX_FLOAT sin_alpha = FXSYS_sin(alpha);
94 FX_FLOAT sin_beta = FXSYS_sin(beta); 96 FX_FLOAT sin_beta = FXSYS_sin(beta);
95 FX_FLOAT cos_alpha = FXSYS_cos(alpha); 97 FX_FLOAT cos_alpha = FXSYS_cos(alpha);
96 FX_FLOAT cos_beta = FXSYS_cos(beta); 98 FX_FLOAT cos_beta = FXSYS_cos(beta);
97 if (bStart) 99 if (bStart)
98 MoveTo(CFX_PointF(cx + rx * cos_alpha, cy + ry * sin_alpha)); 100 MoveTo(CFX_PointF(cx + rx * cos_alpha, cy + ry * sin_alpha));
99 101
100 BezierTo(CFX_PointF(cx + rx * (cos_alpha - bcp * sin_alpha), 102 BezierTo(CFX_PointF(cx + rx * (cos_alpha - bcp * sin_alpha),
101 cy + ry * (sin_alpha + bcp * cos_alpha)), 103 cy + ry * (sin_alpha + bcp * cos_alpha)),
102 CFX_PointF(cx + rx * (cos_beta + bcp * sin_beta), 104 CFX_PointF(cx + rx * (cos_beta + bcp * sin_beta),
103 cy + ry * (sin_beta - bcp * cos_beta)), 105 cy + ry * (sin_beta - bcp * cos_beta)),
104 CFX_PointF(cx + rx * cos_beta, cy + ry * sin_beta)); 106 CFX_PointF(cx + rx * cos_beta, cy + ry * sin_beta));
105 } 107 }
106 108
107 void CFDE_Path::AddBezier(const CFX_PointsF& points) { 109 void CFDE_Path::AddBezier(const CFX_PointsF& points) {
108 if (points.GetSize() != 4) { 110 if (points.GetSize() != 4)
109 return; 111 return;
110 } 112
111 const CFX_PointF* p = points.GetData(); 113 const CFX_PointF* p = points.GetData();
112 MoveTo(p[0]); 114 MoveTo(p[0]);
113 BezierTo(p[1], p[2], p[3]); 115 BezierTo(p[1], p[2], p[3]);
114 } 116 }
117
115 void CFDE_Path::AddBeziers(const CFX_PointsF& points) { 118 void CFDE_Path::AddBeziers(const CFX_PointsF& points) {
116 int32_t iCount = points.GetSize(); 119 int32_t iCount = points.GetSize();
117 if (iCount < 4) { 120 if (iCount < 4)
118 return; 121 return;
119 } 122
120 const CFX_PointF* p = points.GetData(); 123 const CFX_PointF* p = points.GetData();
121 const CFX_PointF* pEnd = p + iCount; 124 const CFX_PointF* pEnd = p + iCount;
122 MoveTo(p[0]); 125 MoveTo(p[0]);
123 for (++p; p <= pEnd - 3; p += 3) { 126 for (++p; p <= pEnd - 3; p += 3)
124 BezierTo(p[0], p[1], p[2]); 127 BezierTo(p[0], p[1], p[2]);
125 }
126 } 128 }
129
127 void CFDE_Path::GetCurveTangents(const CFX_PointsF& points, 130 void CFDE_Path::GetCurveTangents(const CFX_PointsF& points,
128 CFX_PointsF& tangents, 131 CFX_PointsF& tangents,
129 FX_BOOL bClosed, 132 FX_BOOL bClosed,
130 FX_FLOAT fTension) const { 133 FX_FLOAT fTension) const {
131 int32_t iCount = points.GetSize(); 134 int32_t iCount = points.GetSize();
132 tangents.SetSize(iCount); 135 tangents.SetSize(iCount);
133 if (iCount < 3) { 136 if (iCount < 3)
134 return; 137 return;
135 } 138
136 FX_FLOAT fCoefficient = fTension / 3.0f; 139 FX_FLOAT fCoefficient = fTension / 3.0f;
137 const CFX_PointF* pPoints = points.GetData(); 140 const CFX_PointF* pPoints = points.GetData();
138 CFX_PointF* pTangents = tangents.GetData(); 141 CFX_PointF* pTangents = tangents.GetData();
139 for (int32_t i = 0; i < iCount; ++i) { 142 for (int32_t i = 0; i < iCount; ++i) {
140 int32_t r = i + 1; 143 int32_t r = i + 1;
141 int32_t s = i - 1; 144 int32_t s = i - 1;
142 if (r >= iCount) { 145 if (r >= iCount)
143 r = bClosed ? (r - iCount) : (iCount - 1); 146 r = bClosed ? (r - iCount) : (iCount - 1);
144 } 147 if (s < 0)
145 if (s < 0) {
146 s = bClosed ? (s + iCount) : 0; 148 s = bClosed ? (s + iCount) : 0;
147 } 149
148 pTangents[i].x += (fCoefficient * (pPoints[r].x - pPoints[s].x)); 150 pTangents[i].x += (fCoefficient * (pPoints[r].x - pPoints[s].x));
149 pTangents[i].y += (fCoefficient * (pPoints[r].y - pPoints[s].y)); 151 pTangents[i].y += (fCoefficient * (pPoints[r].y - pPoints[s].y));
150 } 152 }
151 } 153 }
154
152 void CFDE_Path::AddCurve(const CFX_PointsF& points, 155 void CFDE_Path::AddCurve(const CFX_PointsF& points,
153 FX_BOOL bClosed, 156 FX_BOOL bClosed,
154 FX_FLOAT fTension) { 157 FX_FLOAT fTension) {
155 int32_t iLast = points.GetUpperBound(); 158 int32_t iLast = points.GetUpperBound();
156 if (iLast < 1) { 159 if (iLast < 1)
157 return; 160 return;
158 } 161
159 CFX_PointsF tangents; 162 CFX_PointsF tangents;
160 GetCurveTangents(points, tangents, bClosed, fTension); 163 GetCurveTangents(points, tangents, bClosed, fTension);
161 const CFX_PointF* pPoints = points.GetData(); 164 const CFX_PointF* pPoints = points.GetData();
162 CFX_PointF* pTangents = tangents.GetData(); 165 CFX_PointF* pTangents = tangents.GetData();
163 MoveTo(pPoints[0]); 166 MoveTo(pPoints[0]);
164 for (int32_t i = 0; i < iLast; ++i) { 167 for (int32_t i = 0; i < iLast; ++i) {
165 BezierTo(CFX_PointF(pPoints[i].x + pTangents[i].x, 168 BezierTo(CFX_PointF(pPoints[i].x + pTangents[i].x,
166 pPoints[i].y + pTangents[i].y), 169 pPoints[i].y + pTangents[i].y),
167 CFX_PointF(pPoints[i + 1].x - pTangents[i + 1].x, 170 CFX_PointF(pPoints[i + 1].x - pTangents[i + 1].x,
168 pPoints[i + 1].y - pTangents[i + 1].y), 171 pPoints[i + 1].y - pTangents[i + 1].y),
169 CFX_PointF(pPoints[i + 1].x, pPoints[i + 1].y)); 172 CFX_PointF(pPoints[i + 1].x, pPoints[i + 1].y));
170 } 173 }
171 if (bClosed) { 174 if (bClosed) {
172 BezierTo(CFX_PointF(pPoints[iLast].x + pTangents[iLast].x, 175 BezierTo(CFX_PointF(pPoints[iLast].x + pTangents[iLast].x,
173 pPoints[iLast].y + pTangents[iLast].y), 176 pPoints[iLast].y + pTangents[iLast].y),
174 CFX_PointF(pPoints[0].x - pTangents[0].x, 177 CFX_PointF(pPoints[0].x - pTangents[0].x,
175 pPoints[0].y - pTangents[0].y), 178 pPoints[0].y - pTangents[0].y),
176 CFX_PointF(pPoints[0].x, pPoints[0].y)); 179 CFX_PointF(pPoints[0].x, pPoints[0].y));
177 CloseFigure(); 180 CloseFigure();
178 } 181 }
179 } 182 }
183
180 void CFDE_Path::AddEllipse(const CFX_RectF& rect) { 184 void CFDE_Path::AddEllipse(const CFX_RectF& rect) {
181 FX_FLOAT fStartAngle = 0; 185 FX_FLOAT fStartAngle = 0;
182 FX_FLOAT fEndAngle = FX_PI / 2; 186 FX_FLOAT fEndAngle = FX_PI / 2;
183 for (int32_t i = 0; i < 4; ++i) { 187 for (int32_t i = 0; i < 4; ++i) {
184 ArcTo(i == 0, rect, fStartAngle, fEndAngle); 188 ArcTo(i == 0, rect, fStartAngle, fEndAngle);
185 fStartAngle += FX_PI / 2; 189 fStartAngle += FX_PI / 2;
186 fEndAngle += FX_PI / 2; 190 fEndAngle += FX_PI / 2;
187 } 191 }
188 CloseFigure(); 192 CloseFigure();
189 } 193 }
194
190 void CFDE_Path::AddLine(const CFX_PointF& pt1, const CFX_PointF& pt2) { 195 void CFDE_Path::AddLine(const CFX_PointF& pt1, const CFX_PointF& pt2) {
191 FX_PATHPOINT* pLast = GetLastPoint(); 196 FX_PATHPOINT* pLast = GetLastPoint();
192 if (pLast == NULL || FXSYS_fabs(pLast->m_PointX - pt1.x) > 0.001 || 197 if (!pLast || FXSYS_fabs(pLast->m_PointX - pt1.x) > 0.001 ||
193 FXSYS_fabs(pLast->m_PointY - pt1.y) > 0.001) { 198 FXSYS_fabs(pLast->m_PointY - pt1.y) > 0.001) {
194 MoveTo(pt1); 199 MoveTo(pt1);
195 } 200 }
196 LineTo(pt2); 201 LineTo(pt2);
197 } 202 }
198 void CFDE_Path::AddPath(const IFDE_Path* pSrc, FX_BOOL bConnect) { 203
204 void CFDE_Path::AddPath(const CFDE_Path* pSrc, FX_BOOL bConnect) {
199 CFDE_Path* pPath = (CFDE_Path*)pSrc; 205 CFDE_Path* pPath = (CFDE_Path*)pSrc;
200 if (pPath == NULL) { 206 if (!pPath)
201 return; 207 return;
202 } 208
203 int32_t iCount = pPath->m_Path.GetPointCount(); 209 int32_t iCount = pPath->m_Path.GetPointCount();
204 if (iCount < 1) { 210 if (iCount < 1)
205 return; 211 return;
206 } 212 if (bConnect)
207 if (bConnect) {
208 LineTo(pPath->m_Path.GetPointX(0), pPath->m_Path.GetPointY(0)); 213 LineTo(pPath->m_Path.GetPointX(0), pPath->m_Path.GetPointY(0));
209 } 214
210 m_Path.Append(&pPath->m_Path, NULL); 215 m_Path.Append(&pPath->m_Path, nullptr);
211 } 216 }
217
212 void CFDE_Path::AddPolygon(const CFX_PointsF& points) { 218 void CFDE_Path::AddPolygon(const CFX_PointsF& points) {
213 int32_t iCount = points.GetSize(); 219 int32_t iCount = points.GetSize();
214 if (iCount < 2) { 220 if (iCount < 2)
215 return; 221 return;
216 } 222
217 AddLines(points); 223 AddLines(points);
218 const CFX_PointF* p = points.GetData(); 224 const CFX_PointF* p = points.GetData();
219 if (FXSYS_fabs(p[0].x - p[iCount - 1].x) < 0.01f || 225 if (FXSYS_fabs(p[0].x - p[iCount - 1].x) < 0.01f ||
220 FXSYS_fabs(p[0].y - p[iCount - 1].y) < 0.01f) { 226 FXSYS_fabs(p[0].y - p[iCount - 1].y) < 0.01f) {
221 LineTo(p[0]); 227 LineTo(p[0]);
222 } 228 }
223 CloseFigure(); 229 CloseFigure();
224 } 230 }
231
225 void CFDE_Path::AddLines(const CFX_PointsF& points) { 232 void CFDE_Path::AddLines(const CFX_PointsF& points) {
226 int32_t iCount = points.GetSize(); 233 int32_t iCount = points.GetSize();
227 if (iCount < 2) { 234 if (iCount < 2)
228 return; 235 return;
229 } 236
230 const CFX_PointF* p = points.GetData(); 237 const CFX_PointF* p = points.GetData();
231 const CFX_PointF* pEnd = p + iCount; 238 const CFX_PointF* pEnd = p + iCount;
232 MoveTo(p[0]); 239 MoveTo(p[0]);
233 for (++p; p < pEnd; ++p) { 240 for (++p; p < pEnd; ++p)
234 LineTo(*p); 241 LineTo(*p);
235 }
236 } 242 }
243
237 void CFDE_Path::AddRectangle(const CFX_RectF& rect) { 244 void CFDE_Path::AddRectangle(const CFX_RectF& rect) {
238 MoveTo(rect.TopLeft()); 245 MoveTo(rect.TopLeft());
239 LineTo(rect.TopRight()); 246 LineTo(rect.TopRight());
240 LineTo(rect.BottomRight()); 247 LineTo(rect.BottomRight());
241 LineTo(rect.BottomLeft()); 248 LineTo(rect.BottomLeft());
242 CloseFigure(); 249 CloseFigure();
243 } 250 }
251
244 void CFDE_Path::GetBBox(CFX_RectF& bbox) const { 252 void CFDE_Path::GetBBox(CFX_RectF& bbox) const {
245 CFX_FloatRect rect = m_Path.GetBoundingBox(); 253 CFX_FloatRect rect = m_Path.GetBoundingBox();
246 bbox.Set(rect.left, rect.top, rect.Width(), rect.Height()); 254 bbox.Set(rect.left, rect.top, rect.Width(), rect.Height());
247 bbox.Normalize(); 255 bbox.Normalize();
248 } 256 }
257
249 void CFDE_Path::GetBBox(CFX_RectF& bbox, 258 void CFDE_Path::GetBBox(CFX_RectF& bbox,
250 FX_FLOAT fLineWidth, 259 FX_FLOAT fLineWidth,
251 FX_FLOAT fMiterLimit) const { 260 FX_FLOAT fMiterLimit) const {
252 CFX_FloatRect rect = m_Path.GetBoundingBox(fLineWidth, fMiterLimit); 261 CFX_FloatRect rect = m_Path.GetBoundingBox(fLineWidth, fMiterLimit);
253 bbox.Set(rect.left, rect.top, rect.Width(), rect.Height()); 262 bbox.Set(rect.left, rect.top, rect.Width(), rect.Height());
254 bbox.Normalize(); 263 bbox.Normalize();
255 } 264 }
OLDNEW
« no previous file with comments | « xfa/fde/cfde_path.h ('k') | xfa/fde/fde_gedevice.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698