7#include "core/fxge/cfx_path.h"
14#include "core/fxcrt/fx_system.h"
15#include "third_party/base/check_op.h"
19bool IsRectPreTransform(
const std::vector<CFX_Path::Point>& points) {
20 if (points.size() != 5 && points.size() != 4)
23 if (points.size() == 5 && points[0].m_Point != points[4].m_Point)
26 if (points[0].m_Point == points[2].m_Point ||
27 points[1].m_Point == points[3].m_Point) {
31 for (size_t i = 1; i < points.size(); ++i) {
38bool XYBothNotEqual(
const CFX_PointF& p1,
const CFX_PointF& p2) {
39 return p1.x != p2.x && p1.y != p2.y;
42bool IsRectImpl(
const std::vector<CFX_Path::Point>& points) {
43 if (!IsRectPreTransform(points))
46 for (
int i = 1; i < 4; i++) {
47 if (XYBothNotEqual(points[i].m_Point, points[i - 1].m_Point))
51 if (XYBothNotEqual(points[0].m_Point, points[3].m_Point))
57CFX_FloatRect CreateRectFromPoints(
const CFX_PointF& p1,
const CFX_PointF& p2) {
63bool PathPointsNeedNormalization(
const std::vector<CFX_Path::Point>& points) {
64 return points.size() > 5;
67std::vector<CFX_Path::Point> GetNormalizedPoints(
68 const std::vector<CFX_Path::Point>& points) {
69 DCHECK(PathPointsNeedNormalization(points));
71 if (points[0].m_Point != points.back().m_Point)
74 std::vector<CFX_Path::Point> normalized;
75 normalized.reserve(6);
76 normalized.push_back(points[0]);
77 for (
auto it = points.begin() + 1; it != points.end(); ++it) {
79 if (normalized.size() +
std::distance(it, points.end()) == 5) {
80 std::copy(it, points.end(),
std::back_inserter(normalized));
85 const auto& point = *it;
87 !normalized.back().m_CloseFigure &&
88 point.m_Point == normalized.back().m_Point) {
92 normalized.push_back(point);
95 if (normalized.size() > 5)
99 DCHECK_EQ(5u, normalized.size());
104 const CFX_PointF& start_pos,
105 const CFX_PointF& end_pos,
107 if (start_pos.x == end_pos.x) {
108 if (start_pos.y == end_pos.y) {
115 if (end_pos.y < start_pos.y)
116 point_y = end_pos.y - hw;
118 point_y = end_pos.y + hw;
125 if (start_pos.y == end_pos.y) {
127 if (end_pos.x < start_pos.x)
128 point_x = end_pos.x - hw;
130 point_x = end_pos.x + hw;
137 CFX_PointF diff = end_pos - start_pos;
138 float ll = FXSYS_sqrt2(diff.x, diff.y);
139 float mx = end_pos.x + hw * diff.x / ll;
140 float my = end_pos.y + hw * diff.y / ll;
141 float dx1 = hw * diff.y / ll;
142 float dy1 = hw * diff.x / ll;
148 const CFX_PointF& start_pos,
149 const CFX_PointF& mid_pos,
150 const CFX_PointF& end_pos,
161 float one_twentieth = 1.0f / 20;
163 bool bStartVert = fabs(start_pos.x - mid_pos.x) < one_twentieth;
164 bool bEndVert = fabs(mid_pos.x - end_pos.x) < one_twentieth;
165 if (bStartVert && bEndVert) {
166 int start_dir = mid_pos.y > start_pos.y ? 1 : -1;
167 float point_y = mid_pos.y + half_width * start_dir;
174 CFX_PointF start_to_mid = start_pos - mid_pos;
175 start_k = (mid_pos.y - start_pos.y) / (mid_pos.x - start_pos.x);
176 start_c = mid_pos.y - (start_k * mid_pos.x);
177 start_len = FXSYS_sqrt2(start_to_mid.x, start_to_mid.y);
178 start_dc = fabsf(half_width * start_len / start_to_mid.x);
181 CFX_PointF end_to_mid = end_pos - mid_pos;
182 end_k = end_to_mid.y / end_to_mid.x;
183 end_c = mid_pos.y - (end_k * mid_pos.x);
184 end_len = FXSYS_sqrt2(end_to_mid.x, end_to_mid.y);
185 end_dc = fabs(half_width * end_len / end_to_mid.x);
188 CFX_PointF outside(start_pos.x, 0);
189 if (end_pos.x < start_pos.x)
190 outside.x += half_width;
192 outside.x -= half_width;
194 if (start_pos.y < (end_k * start_pos.x) + end_c)
195 outside.y = (end_k * outside.x) + end_c + end_dc;
197 outside.y = (end_k * outside.x) + end_c - end_dc;
204 CFX_PointF outside(end_pos.x, 0);
205 if (start_pos.x < end_pos.x)
206 outside.x += half_width;
208 outside.x -= half_width;
210 if (end_pos.y < (start_k * end_pos.x) + start_c)
211 outside.y = (start_k * outside.x) + start_c + start_dc;
213 outside.y = (start_k * outside.x) + start_c - start_dc;
219 if (fabs(start_k - end_k) < one_twentieth) {
220 int start_dir = mid_pos.x > start_pos.x ? 1 : -1;
221 int end_dir = end_pos.x > mid_pos.x ? 1 : -1;
222 if (start_dir == end_dir)
223 UpdateLineEndPoints(rect, mid_pos, end_pos, half_width);
225 UpdateLineEndPoints(rect, start_pos, mid_pos, half_width);
229 float start_outside_c = start_c;
230 if (end_pos.y < (start_k * end_pos.x) + start_c)
231 start_outside_c += start_dc;
233 start_outside_c -= start_dc;
235 float end_outside_c = end_c;
236 if (start_pos.y < (end_k * start_pos.x) + end_c)
237 end_outside_c += end_dc;
239 end_outside_c -= end_dc;
241 float join_x = (end_outside_c - start_outside_c) / (start_k - end_k);
242 float join_y = start_k * join_x + start_outside_c;
270 if (m_Points.empty())
272 m_Points.back().m_CloseFigure =
true;
276 if (src.m_Points.empty())
279 size_t cur_size = m_Points.size();
280 m_Points.insert(m_Points.end(), src.m_Points.begin(), src.m_Points.end());
285 for (size_t i = cur_size; i < m_Points.size(); i++)
286 m_Points[i].m_Point = matrix->Transform(m_Points[i].m_Point);
290 m_Points.emplace_back(point, type,
false);
294 m_Points.emplace_back(point, type,
true);
298 if (m_Points.empty() || fabs(m_Points.back().m_Point.x - pt1.x) > 0.001 ||
299 fabs(m_Points.back().m_Point.y - pt1.y) > 0.001) {
310 CFX_PointF left_bottom(left, bottom);
311 CFX_PointF left_top(left, top);
312 CFX_PointF right_top(right, top);
313 CFX_PointF right_bottom(right, bottom);
323 if (m_Points.empty())
327 for (size_t i = 1; i < m_Points.size(); ++i)
328 rect.UpdateRect(m_Points[i].m_Point);
333 float miter_limit)
const {
336 float half_width = line_width;
337 size_t iStartPoint = 0;
338 size_t iEndPoint = 0;
339 size_t iMiddlePoint = 0;
341 while (iPoint < m_Points.size()) {
342 if (m_Points[iPoint].m_Type == CFX_Path::Point::Type::kMove) {
343 if (iPoint + 1 == m_Points.size()) {
344 if (m_Points[iPoint].m_CloseFigure) {
346 rect.UpdateRect(m_Points[iPoint].m_Point);
351 iStartPoint = iPoint + 1;
355 if (m_Points[iPoint].IsTypeAndOpen(CFX_Path::Point::Type::kBezier)) {
357 CHECK_LT(iPoint + 2, m_Points.size());
358 DCHECK_EQ(m_Points[iPoint + 1].m_Type, CFX_Path::Point::Type::kBezier);
359 DCHECK_EQ(m_Points[iPoint + 2].m_Type, CFX_Path::Point::Type::kBezier);
360 rect.UpdateRect(m_Points[iPoint].m_Point);
361 rect.UpdateRect(m_Points[iPoint + 1].m_Point);
364 if (iPoint + 1 == m_Points.size() ||
365 m_Points[iPoint + 1].m_Type == CFX_Path::Point::Type::kMove) {
366 iStartPoint = iPoint - 1;
370 iStartPoint = iPoint - 1;
371 iMiddlePoint = iPoint;
372 iEndPoint = iPoint + 1;
376 CHECK_LT(iStartPoint, m_Points.size());
377 CHECK_LT(iEndPoint, m_Points.size());
379 CHECK_LT(iMiddlePoint, m_Points.size());
380 UpdateLineJoinPoints(
381 &rect, m_Points[iStartPoint].m_Point, m_Points[iMiddlePoint].m_Point,
382 m_Points[iEndPoint].m_Point, half_width, miter_limit);
384 UpdateLineEndPoints(&rect, m_Points[iStartPoint].m_Point,
385 m_Points[iEndPoint].m_Point, half_width);
393 for (
auto& point : m_Points)
394 point.m_Point = matrix.Transform(point.m_Point);
398 if (PathPointsNeedNormalization(m_Points))
399 return IsRectImpl(GetNormalizedPoints(m_Points));
400 return IsRectImpl(m_Points);
405 bool do_normalize = PathPointsNeedNormalization(m_Points);
406 std::vector<Point> normalized;
408 normalized = GetNormalizedPoints(m_Points);
409 const std::vector<Point>& path_points = do_normalize ? normalized : m_Points;
412 if (!IsRectImpl(path_points))
413 return absl::nullopt;
415 return CreateRectFromPoints(path_points[0].m_Point, path_points[2].m_Point);
418 if (!IsRectPreTransform(path_points))
419 return absl::nullopt;
421 CFX_PointF points[5];
422 for (size_t i = 0; i < path_points.size(); ++i) {
427 if (XYBothNotEqual(points[i], points[i - 1]))
428 return absl::nullopt;
431 if (XYBothNotEqual(points[0], points[3]))
432 return absl::nullopt;
434 return CreateRectFromPoints(points[0], points[2]);
437CFX_RetainablePath::CFX_RetainablePath() =
default;
444CFX_RetainablePath::CFX_RetainablePath(
const CFX_RetainablePath& src)
450 return pdfium::MakeRetain<CFX_RetainablePath>(*
this);
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)
Point(const Point &other)
CFX_FloatRect GetBoundingBox() const
void Transform(const CFX_Matrix &matrix)
void AppendRect(float left, float bottom, float right, float top)
CFX_Path(CFX_Path &&src) noexcept
void Append(const CFX_Path &src, const CFX_Matrix *matrix)
absl::optional< CFX_FloatRect > GetRect(const CFX_Matrix *matrix) const
CFX_FloatRect GetBoundingBoxForStrokePath(float line_width, float miter_limit) const
void AppendFloatRect(const CFX_FloatRect &rect)
void AppendPointAndClose(const CFX_PointF &point, Point::Type type)
void AppendLine(const CFX_PointF &pt1, const CFX_PointF &pt2)
CFX_Path(const CFX_Path &src)
void AppendPoint(const CFX_PointF &point, Point::Type type)
~CFX_RetainablePath() override
RetainPtr< CFX_RetainablePath > Clone() const