Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
cfx_path.cpp
Go to the documentation of this file.
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 "core/fxge/cfx_path.h"
8
9#include <math.h>
10
11#include <algorithm>
12#include <array>
13#include <iterator>
14
15#include "core/fxcrt/check_op.h"
16#include "core/fxcrt/fx_system.h"
17
18namespace {
19
20bool IsRectPreTransform(const std::vector<CFX_Path::Point>& points) {
21 if (points.size() != 5 && points.size() != 4)
22 return false;
23
24 if (points.size() == 5 && points[0].m_Point != points[4].m_Point)
25 return false;
26
27 if (points[0].m_Point == points[2].m_Point ||
28 points[1].m_Point == points[3].m_Point) {
29 return false;
30 }
31
32 for (size_t i = 1; i < points.size(); ++i) {
33 if (points[i].m_Type != CFX_Path::Point::Type::kLine)
34 return false;
35 }
36 return true;
37}
38
39bool XYBothNotEqual(const CFX_PointF& p1, const CFX_PointF& p2) {
40 return p1.x != p2.x && p1.y != p2.y;
41}
42
43bool IsRectImpl(const std::vector<CFX_Path::Point>& points) {
44 if (!IsRectPreTransform(points))
45 return false;
46
47 for (int i = 1; i < 4; i++) {
48 if (XYBothNotEqual(points[i].m_Point, points[i - 1].m_Point))
49 return false;
50 }
51
52 if (XYBothNotEqual(points[0].m_Point, points[3].m_Point))
53 return false;
54
55 return true;
56}
57
58CFX_FloatRect CreateRectFromPoints(const CFX_PointF& p1, const CFX_PointF& p2) {
59 CFX_FloatRect rect(p1.x, p1.y, p2.x, p2.y);
60 rect.Normalize();
61 return rect;
62}
63
64bool PathPointsNeedNormalization(const std::vector<CFX_Path::Point>& points) {
65 return points.size() > 5;
66}
67
68std::vector<CFX_Path::Point> GetNormalizedPoints(
69 const std::vector<CFX_Path::Point>& points) {
70 DCHECK(PathPointsNeedNormalization(points));
71
72 if (points[0].m_Point != points.back().m_Point)
73 return {};
74
75 std::vector<CFX_Path::Point> normalized;
76 normalized.reserve(6);
77 normalized.push_back(points[0]);
78 for (auto it = points.begin() + 1; it != points.end(); ++it) {
79 // Exactly 5 points left. Stop normalizing and take what is left.
80 if (normalized.size() + std::distance(it, points.end()) == 5) {
81 std::copy(it, points.end(), std::back_inserter(normalized));
82 break;
83 }
84
85 // If the line does not move, skip this point.
86 const auto& point = *it;
87 if (point.m_Type == CFX_Path::Point::Type::kLine && !point.m_CloseFigure &&
88 !normalized.back().m_CloseFigure &&
89 point.m_Point == normalized.back().m_Point) {
90 continue;
91 }
92
93 normalized.push_back(point);
94
95 // Too many points. Not considered as a rectangle.
96 if (normalized.size() > 5)
97 return {};
98 }
99
100 DCHECK_EQ(5u, normalized.size());
101 return normalized;
102}
103
104void UpdateLineEndPoints(CFX_FloatRect* rect,
105 const CFX_PointF& start_pos,
106 const CFX_PointF& end_pos,
107 float hw) {
108 if (start_pos.x == end_pos.x) {
109 if (start_pos.y == end_pos.y) {
110 rect->UpdateRect(end_pos + CFX_PointF(hw, hw));
111 rect->UpdateRect(end_pos - CFX_PointF(hw, hw));
112 return;
113 }
114
115 float point_y;
116 if (end_pos.y < start_pos.y)
117 point_y = end_pos.y - hw;
118 else
119 point_y = end_pos.y + hw;
120
121 rect->UpdateRect(CFX_PointF(end_pos.x + hw, point_y));
122 rect->UpdateRect(CFX_PointF(end_pos.x - hw, point_y));
123 return;
124 }
125
126 if (start_pos.y == end_pos.y) {
127 float point_x;
128 if (end_pos.x < start_pos.x)
129 point_x = end_pos.x - hw;
130 else
131 point_x = end_pos.x + hw;
132
133 rect->UpdateRect(CFX_PointF(point_x, end_pos.y + hw));
134 rect->UpdateRect(CFX_PointF(point_x, end_pos.y - hw));
135 return;
136 }
137
138 CFX_PointF diff = end_pos - start_pos;
139 float ll = hypotf(diff.x, diff.y);
140 float mx = end_pos.x + hw * diff.x / ll;
141 float my = end_pos.y + hw * diff.y / ll;
142 float dx1 = hw * diff.y / ll;
143 float dy1 = hw * diff.x / ll;
144 rect->UpdateRect(CFX_PointF(mx - dx1, my + dy1));
145 rect->UpdateRect(CFX_PointF(mx + dx1, my - dy1));
146}
147
148void UpdateLineJoinPoints(CFX_FloatRect* rect,
149 const CFX_PointF& start_pos,
150 const CFX_PointF& mid_pos,
151 const CFX_PointF& end_pos,
152 float half_width,
153 float miter_limit) {
154 float start_k = 0;
155 float start_c = 0;
156 float end_k = 0;
157 float end_c = 0;
158 float start_len = 0;
159 float start_dc = 0;
160 float end_len = 0;
161 float end_dc = 0;
162 float one_twentieth = 1.0f / 20;
163
164 bool bStartVert = fabs(start_pos.x - mid_pos.x) < one_twentieth;
165 bool bEndVert = fabs(mid_pos.x - end_pos.x) < one_twentieth;
166 if (bStartVert && bEndVert) {
167 int start_dir = mid_pos.y > start_pos.y ? 1 : -1;
168 float point_y = mid_pos.y + half_width * start_dir;
169 rect->UpdateRect(CFX_PointF(mid_pos.x + half_width, point_y));
170 rect->UpdateRect(CFX_PointF(mid_pos.x - half_width, point_y));
171 return;
172 }
173
174 if (!bStartVert) {
175 CFX_PointF start_to_mid = start_pos - mid_pos;
176 start_k = (mid_pos.y - start_pos.y) / (mid_pos.x - start_pos.x);
177 start_c = mid_pos.y - (start_k * mid_pos.x);
178 start_len = hypotf(start_to_mid.x, start_to_mid.y);
179 start_dc = fabsf(half_width * start_len / start_to_mid.x);
180 }
181 if (!bEndVert) {
182 CFX_PointF end_to_mid = end_pos - mid_pos;
183 end_k = end_to_mid.y / end_to_mid.x;
184 end_c = mid_pos.y - (end_k * mid_pos.x);
185 end_len = hypotf(end_to_mid.x, end_to_mid.y);
186 end_dc = fabs(half_width * end_len / end_to_mid.x);
187 }
188 if (bStartVert) {
189 CFX_PointF outside(start_pos.x, 0);
190 if (end_pos.x < start_pos.x)
191 outside.x += half_width;
192 else
193 outside.x -= half_width;
194
195 if (start_pos.y < (end_k * start_pos.x) + end_c)
196 outside.y = (end_k * outside.x) + end_c + end_dc;
197 else
198 outside.y = (end_k * outside.x) + end_c - end_dc;
199
200 rect->UpdateRect(outside);
201 return;
202 }
203
204 if (bEndVert) {
205 CFX_PointF outside(end_pos.x, 0);
206 if (start_pos.x < end_pos.x)
207 outside.x += half_width;
208 else
209 outside.x -= half_width;
210
211 if (end_pos.y < (start_k * end_pos.x) + start_c)
212 outside.y = (start_k * outside.x) + start_c + start_dc;
213 else
214 outside.y = (start_k * outside.x) + start_c - start_dc;
215
216 rect->UpdateRect(outside);
217 return;
218 }
219
220 if (fabs(start_k - end_k) < one_twentieth) {
221 int start_dir = mid_pos.x > start_pos.x ? 1 : -1;
222 int end_dir = end_pos.x > mid_pos.x ? 1 : -1;
223 if (start_dir == end_dir)
224 UpdateLineEndPoints(rect, mid_pos, end_pos, half_width);
225 else
226 UpdateLineEndPoints(rect, start_pos, mid_pos, half_width);
227 return;
228 }
229
230 float start_outside_c = start_c;
231 if (end_pos.y < (start_k * end_pos.x) + start_c)
232 start_outside_c += start_dc;
233 else
234 start_outside_c -= start_dc;
235
236 float end_outside_c = end_c;
237 if (start_pos.y < (end_k * start_pos.x) + end_c)
238 end_outside_c += end_dc;
239 else
240 end_outside_c -= end_dc;
241
242 float join_x = (end_outside_c - start_outside_c) / (start_k - end_k);
243 float join_y = start_k * join_x + start_outside_c;
244 rect->UpdateRect(CFX_PointF(join_x, join_y));
245}
246
247} // namespace
248
249CFX_Path::Point::Point() = default;
250
251CFX_Path::Point::Point(const CFX_PointF& point, Type type, bool close)
252 : m_Point(point), m_Type(type), m_CloseFigure(close) {}
253
254CFX_Path::Point::Point(const Point& other) = default;
255
256CFX_Path::Point::~Point() = default;
257
258CFX_Path::CFX_Path() = default;
259
260CFX_Path::CFX_Path(const CFX_Path& src) = default;
261
262CFX_Path::CFX_Path(CFX_Path&& src) noexcept = default;
263
264CFX_Path::~CFX_Path() = default;
265
267 m_Points.clear();
268}
269
271 if (m_Points.empty())
272 return;
273 m_Points.back().m_CloseFigure = true;
274}
275
276void CFX_Path::Append(const CFX_Path& src, const CFX_Matrix* matrix) {
277 if (src.m_Points.empty())
278 return;
279
280 size_t cur_size = m_Points.size();
281 m_Points.insert(m_Points.end(), src.m_Points.begin(), src.m_Points.end());
282
283 if (!matrix)
284 return;
285
286 for (size_t i = cur_size; i < m_Points.size(); i++)
287 m_Points[i].m_Point = matrix->Transform(m_Points[i].m_Point);
288}
289
290void CFX_Path::AppendPoint(const CFX_PointF& point, Point::Type type) {
291 m_Points.emplace_back(point, type, /*close=*/false);
292}
293
295 m_Points.emplace_back(point, type, /*close=*/true);
296}
297
298void CFX_Path::AppendLine(const CFX_PointF& pt1, const CFX_PointF& pt2) {
299 if (m_Points.empty() || fabs(m_Points.back().m_Point.x - pt1.x) > 0.001 ||
300 fabs(m_Points.back().m_Point.y - pt1.y) > 0.001) {
302 }
304}
305
307 return AppendRect(rect.left, rect.bottom, rect.right, rect.top);
308}
309
310void CFX_Path::AppendRect(float left, float bottom, float right, float top) {
311 CFX_PointF left_bottom(left, bottom);
312 CFX_PointF left_top(left, top);
313 CFX_PointF right_top(right, top);
314 CFX_PointF right_bottom(right, bottom);
315
316 AppendLine(left_bottom, left_top);
317 AppendLine(left_top, right_top);
318 AppendLine(right_top, right_bottom);
319 AppendLine(right_bottom, left_bottom);
321}
322
324 if (m_Points.empty())
325 return CFX_FloatRect();
326
327 CFX_FloatRect rect(m_Points[0].m_Point);
328 for (size_t i = 1; i < m_Points.size(); ++i)
329 rect.UpdateRect(m_Points[i].m_Point);
330 return rect;
331}
332
334 float miter_limit) const {
335 CFX_FloatRect rect(100000.0f, 100000.0f, -100000.0f, -100000.0f);
336 size_t iPoint = 0;
337 float half_width = line_width;
338 size_t iStartPoint = 0;
339 size_t iEndPoint = 0;
340 size_t iMiddlePoint = 0;
341 bool bJoin;
342 while (iPoint < m_Points.size()) {
343 if (m_Points[iPoint].m_Type == CFX_Path::Point::Type::kMove) {
344 if (iPoint + 1 == m_Points.size()) {
345 if (m_Points[iPoint].m_CloseFigure) {
346 // Update `rect` right away since this is the final point to be drawn.
347 rect.UpdateRect(m_Points[iPoint].m_Point);
348 }
349 break;
350 }
351
352 iStartPoint = iPoint + 1;
353 iEndPoint = iPoint;
354 bJoin = false;
355 } else {
356 if (m_Points[iPoint].IsTypeAndOpen(CFX_Path::Point::Type::kBezier)) {
357 // Callers are responsible for adding Beziers in sets of 3.
358 CHECK_LT(iPoint + 2, m_Points.size());
359 DCHECK_EQ(m_Points[iPoint + 1].m_Type, CFX_Path::Point::Type::kBezier);
360 DCHECK_EQ(m_Points[iPoint + 2].m_Type, CFX_Path::Point::Type::kBezier);
361 rect.UpdateRect(m_Points[iPoint].m_Point);
362 rect.UpdateRect(m_Points[iPoint + 1].m_Point);
363 iPoint += 2;
364 }
365 if (iPoint + 1 == m_Points.size() ||
366 m_Points[iPoint + 1].m_Type == CFX_Path::Point::Type::kMove) {
367 iStartPoint = iPoint - 1;
368 iEndPoint = iPoint;
369 bJoin = false;
370 } else {
371 iStartPoint = iPoint - 1;
372 iMiddlePoint = iPoint;
373 iEndPoint = iPoint + 1;
374 bJoin = true;
375 }
376 }
377 CHECK_LT(iStartPoint, m_Points.size());
378 CHECK_LT(iEndPoint, m_Points.size());
379 if (bJoin) {
380 CHECK_LT(iMiddlePoint, m_Points.size());
381 UpdateLineJoinPoints(
382 &rect, m_Points[iStartPoint].m_Point, m_Points[iMiddlePoint].m_Point,
383 m_Points[iEndPoint].m_Point, half_width, miter_limit);
384 } else {
385 UpdateLineEndPoints(&rect, m_Points[iStartPoint].m_Point,
386 m_Points[iEndPoint].m_Point, half_width);
387 }
388 ++iPoint;
389 }
390 return rect;
391}
392
393void CFX_Path::Transform(const CFX_Matrix& matrix) {
394 for (auto& point : m_Points)
395 point.m_Point = matrix.Transform(point.m_Point);
396}
397
398bool CFX_Path::IsRect() const {
399 if (PathPointsNeedNormalization(m_Points))
400 return IsRectImpl(GetNormalizedPoints(m_Points));
401 return IsRectImpl(m_Points);
402}
403
404std::optional<CFX_FloatRect> CFX_Path::GetRect(const CFX_Matrix* matrix) const {
405 bool do_normalize = PathPointsNeedNormalization(m_Points);
406 std::vector<Point> normalized;
407 if (do_normalize)
408 normalized = GetNormalizedPoints(m_Points);
409 const std::vector<Point>& path_points = do_normalize ? normalized : m_Points;
410
411 if (!matrix) {
412 if (!IsRectImpl(path_points))
413 return std::nullopt;
414
415 return CreateRectFromPoints(path_points[0].m_Point, path_points[2].m_Point);
416 }
417
418 if (!IsRectPreTransform(path_points))
419 return std::nullopt;
420
421 std::array<CFX_PointF, 5> points;
422 for (size_t i = 0; i < path_points.size(); ++i) {
423 points[i] = matrix->Transform(path_points[i].m_Point);
424
425 if (i == 0)
426 continue;
427 if (XYBothNotEqual(points[i], points[i - 1]))
428 return std::nullopt;
429 }
430
431 if (XYBothNotEqual(points[0], points[3]))
432 return std::nullopt;
433
434 return CreateRectFromPoints(points[0], points[2]);
435}
436
437CFX_RetainablePath::CFX_RetainablePath() = default;
438
439// Note: can't default the copy constructor since Retainable<> has a deleted
440// copy constructor (as it should). Instead, we want the default Retainable<>
441// constructor to be invoked so as to create a copy with a ref-count of 1 as
442// of the time it is created, then populate the remainder of the members from
443// the |src| object.
444CFX_RetainablePath::CFX_RetainablePath(const CFX_RetainablePath& src)
445 : CFX_Path(src) {}
446
447CFX_RetainablePath::~CFX_RetainablePath() = default;
448
449RetainPtr<CFX_RetainablePath> CFX_RetainablePath::Clone() const {
450 return pdfium::MakeRetain<CFX_RetainablePath>(*this);
451}
#define DCHECK
Definition check.h:33
#define CHECK_LT(x, y)
Definition check_op.h:12
#define DCHECK_EQ(x, y)
Definition check_op.h:17
constexpr CFX_FloatRect(float l, float b, float r, float t)
constexpr CFX_FloatRect()=default
void UpdateRect(const CFX_PointF &point)
CFX_PointF Transform(const CFX_PointF &point) const
Point(const CFX_PointF &point, Type type, bool close)
Definition cfx_path.cpp:251
bool m_CloseFigure
Definition cfx_path.h:35
Point(const Point &other)
CFX_FloatRect GetBoundingBox() const
Definition cfx_path.cpp:323
void Transform(const CFX_Matrix &matrix)
Definition cfx_path.cpp:393
void Clear()
Definition cfx_path.cpp:266
void AppendRect(float left, float bottom, float right, float top)
Definition cfx_path.cpp:310
CFX_Path(CFX_Path &&src) noexcept
void Append(const CFX_Path &src, const CFX_Matrix *matrix)
Definition cfx_path.cpp:276
CFX_FloatRect GetBoundingBoxForStrokePath(float line_width, float miter_limit) const
Definition cfx_path.cpp:333
void AppendFloatRect(const CFX_FloatRect &rect)
Definition cfx_path.cpp:306
void AppendPointAndClose(const CFX_PointF &point, Point::Type type)
Definition cfx_path.cpp:294
void AppendLine(const CFX_PointF &pt1, const CFX_PointF &pt2)
Definition cfx_path.cpp:298
CFX_Path(const CFX_Path &src)
void ClosePath()
Definition cfx_path.cpp:270
std::optional< CFX_FloatRect > GetRect(const CFX_Matrix *matrix) const
Definition cfx_path.cpp:404
void AppendPoint(const CFX_PointF &point, Point::Type type)
Definition cfx_path.cpp:290
bool IsRect() const
Definition cfx_path.cpp:398
~CFX_RetainablePath() override
RetainPtr< CFX_RetainablePath > Clone() const
Definition cfx_path.cpp:449
CFX_PTemplate< float > CFX_PointF