Dan Sinclair | 811b8a4 | 2016-03-17 08:59:42 -0400 | [diff] [blame] | 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/cfx_path.h" |
| 8 | |
dsinclair | 74a34fc | 2016-09-29 16:41:42 -0700 | [diff] [blame] | 9 | #include "core/fxge/cfx_pathdata.h" |
tsepez | a9caab9 | 2016-12-14 05:57:10 -0800 | [diff] [blame] | 10 | #include "third_party/base/ptr_util.h" |
Dan Sinclair | 811b8a4 | 2016-03-17 08:59:42 -0400 | [diff] [blame] | 11 | #include "xfa/fxgraphics/cfx_path_generator.h" |
| 12 | |
weili | 16fccc5 | 2016-08-09 10:33:10 -0700 | [diff] [blame] | 13 | CFX_Path::CFX_Path() {} |
Dan Sinclair | 811b8a4 | 2016-03-17 08:59:42 -0400 | [diff] [blame] | 14 | |
dsinclair | c777f48 | 2016-05-04 17:57:03 -0700 | [diff] [blame] | 15 | FWL_Error CFX_Path::Create() { |
Dan Sinclair | 811b8a4 | 2016-03-17 08:59:42 -0400 | [diff] [blame] | 16 | if (m_generator) |
dsinclair | c777f48 | 2016-05-04 17:57:03 -0700 | [diff] [blame] | 17 | return FWL_Error::PropertyInvalid; |
Dan Sinclair | 811b8a4 | 2016-03-17 08:59:42 -0400 | [diff] [blame] | 18 | |
tsepez | a9caab9 | 2016-12-14 05:57:10 -0800 | [diff] [blame] | 19 | m_generator = pdfium::MakeUnique<CFX_PathGenerator>(); |
dsinclair | c777f48 | 2016-05-04 17:57:03 -0700 | [diff] [blame] | 20 | return FWL_Error::Succeeded; |
Dan Sinclair | 811b8a4 | 2016-03-17 08:59:42 -0400 | [diff] [blame] | 21 | } |
| 22 | |
weili | 16fccc5 | 2016-08-09 10:33:10 -0700 | [diff] [blame] | 23 | CFX_Path::~CFX_Path() {} |
Dan Sinclair | 811b8a4 | 2016-03-17 08:59:42 -0400 | [diff] [blame] | 24 | |
dan sinclair | b147e07 | 2017-02-22 19:56:15 -0500 | [diff] [blame] | 25 | FWL_Error CFX_Path::MoveTo(const CFX_PointF& point) { |
Dan Sinclair | 811b8a4 | 2016-03-17 08:59:42 -0400 | [diff] [blame] | 26 | if (!m_generator) |
dsinclair | c777f48 | 2016-05-04 17:57:03 -0700 | [diff] [blame] | 27 | return FWL_Error::PropertyInvalid; |
dan sinclair | b147e07 | 2017-02-22 19:56:15 -0500 | [diff] [blame] | 28 | m_generator->MoveTo(point); |
dsinclair | c777f48 | 2016-05-04 17:57:03 -0700 | [diff] [blame] | 29 | return FWL_Error::Succeeded; |
Dan Sinclair | 811b8a4 | 2016-03-17 08:59:42 -0400 | [diff] [blame] | 30 | } |
| 31 | |
dan sinclair | b147e07 | 2017-02-22 19:56:15 -0500 | [diff] [blame] | 32 | FWL_Error CFX_Path::LineTo(const CFX_PointF& point) { |
Dan Sinclair | 811b8a4 | 2016-03-17 08:59:42 -0400 | [diff] [blame] | 33 | if (!m_generator) |
dsinclair | c777f48 | 2016-05-04 17:57:03 -0700 | [diff] [blame] | 34 | return FWL_Error::PropertyInvalid; |
dan sinclair | b147e07 | 2017-02-22 19:56:15 -0500 | [diff] [blame] | 35 | m_generator->LineTo(point); |
dsinclair | c777f48 | 2016-05-04 17:57:03 -0700 | [diff] [blame] | 36 | return FWL_Error::Succeeded; |
Dan Sinclair | 811b8a4 | 2016-03-17 08:59:42 -0400 | [diff] [blame] | 37 | } |
| 38 | |
dan sinclair | b147e07 | 2017-02-22 19:56:15 -0500 | [diff] [blame] | 39 | FWL_Error CFX_Path::BezierTo(const CFX_PointF& c1, |
| 40 | const CFX_PointF& c2, |
| 41 | const CFX_PointF& to) { |
Dan Sinclair | 811b8a4 | 2016-03-17 08:59:42 -0400 | [diff] [blame] | 42 | if (!m_generator) |
dsinclair | c777f48 | 2016-05-04 17:57:03 -0700 | [diff] [blame] | 43 | return FWL_Error::PropertyInvalid; |
dan sinclair | b147e07 | 2017-02-22 19:56:15 -0500 | [diff] [blame] | 44 | m_generator->BezierTo(c1, c2, to); |
dsinclair | c777f48 | 2016-05-04 17:57:03 -0700 | [diff] [blame] | 45 | return FWL_Error::Succeeded; |
Dan Sinclair | 811b8a4 | 2016-03-17 08:59:42 -0400 | [diff] [blame] | 46 | } |
| 47 | |
dan sinclair | b147e07 | 2017-02-22 19:56:15 -0500 | [diff] [blame] | 48 | FWL_Error CFX_Path::ArcTo(const CFX_PointF& pos, |
| 49 | const CFX_SizeF& size, |
dsinclair | c777f48 | 2016-05-04 17:57:03 -0700 | [diff] [blame] | 50 | FX_FLOAT startAngle, |
| 51 | FX_FLOAT sweepAngle) { |
Dan Sinclair | 811b8a4 | 2016-03-17 08:59:42 -0400 | [diff] [blame] | 52 | if (!m_generator) |
dsinclair | c777f48 | 2016-05-04 17:57:03 -0700 | [diff] [blame] | 53 | return FWL_Error::PropertyInvalid; |
dan sinclair | b147e07 | 2017-02-22 19:56:15 -0500 | [diff] [blame] | 54 | CFX_SizeF newSize = size / 2.0f; |
| 55 | m_generator->ArcTo(CFX_PointF(pos.x + newSize.width, pos.y + newSize.height), |
| 56 | newSize, startAngle, sweepAngle); |
dsinclair | c777f48 | 2016-05-04 17:57:03 -0700 | [diff] [blame] | 57 | return FWL_Error::Succeeded; |
Dan Sinclair | 811b8a4 | 2016-03-17 08:59:42 -0400 | [diff] [blame] | 58 | } |
| 59 | |
dsinclair | c777f48 | 2016-05-04 17:57:03 -0700 | [diff] [blame] | 60 | FWL_Error CFX_Path::Close() { |
Dan Sinclair | 811b8a4 | 2016-03-17 08:59:42 -0400 | [diff] [blame] | 61 | if (!m_generator) |
dsinclair | c777f48 | 2016-05-04 17:57:03 -0700 | [diff] [blame] | 62 | return FWL_Error::PropertyInvalid; |
Dan Sinclair | 811b8a4 | 2016-03-17 08:59:42 -0400 | [diff] [blame] | 63 | m_generator->Close(); |
dsinclair | c777f48 | 2016-05-04 17:57:03 -0700 | [diff] [blame] | 64 | return FWL_Error::Succeeded; |
Dan Sinclair | 811b8a4 | 2016-03-17 08:59:42 -0400 | [diff] [blame] | 65 | } |
| 66 | |
dan sinclair | b147e07 | 2017-02-22 19:56:15 -0500 | [diff] [blame] | 67 | FWL_Error CFX_Path::AddLine(const CFX_PointF& p1, const CFX_PointF& p2) { |
Dan Sinclair | 811b8a4 | 2016-03-17 08:59:42 -0400 | [diff] [blame] | 68 | if (!m_generator) |
dsinclair | c777f48 | 2016-05-04 17:57:03 -0700 | [diff] [blame] | 69 | return FWL_Error::PropertyInvalid; |
dan sinclair | b147e07 | 2017-02-22 19:56:15 -0500 | [diff] [blame] | 70 | m_generator->AddLine(p1, p2); |
dsinclair | c777f48 | 2016-05-04 17:57:03 -0700 | [diff] [blame] | 71 | return FWL_Error::Succeeded; |
Dan Sinclair | 811b8a4 | 2016-03-17 08:59:42 -0400 | [diff] [blame] | 72 | } |
| 73 | |
dan sinclair | b147e07 | 2017-02-22 19:56:15 -0500 | [diff] [blame] | 74 | FWL_Error CFX_Path::AddBezier(const CFX_PointF& p1, |
| 75 | const CFX_PointF& c1, |
| 76 | const CFX_PointF& c2, |
| 77 | const CFX_PointF& p2) { |
Dan Sinclair | 811b8a4 | 2016-03-17 08:59:42 -0400 | [diff] [blame] | 78 | if (!m_generator) |
dsinclair | c777f48 | 2016-05-04 17:57:03 -0700 | [diff] [blame] | 79 | return FWL_Error::PropertyInvalid; |
dan sinclair | b147e07 | 2017-02-22 19:56:15 -0500 | [diff] [blame] | 80 | m_generator->AddBezier(p1, c1, c2, p2); |
dsinclair | c777f48 | 2016-05-04 17:57:03 -0700 | [diff] [blame] | 81 | return FWL_Error::Succeeded; |
Dan Sinclair | 811b8a4 | 2016-03-17 08:59:42 -0400 | [diff] [blame] | 82 | } |
| 83 | |
dsinclair | c777f48 | 2016-05-04 17:57:03 -0700 | [diff] [blame] | 84 | FWL_Error CFX_Path::AddRectangle(FX_FLOAT left, |
| 85 | FX_FLOAT top, |
| 86 | FX_FLOAT width, |
| 87 | FX_FLOAT height) { |
Dan Sinclair | 811b8a4 | 2016-03-17 08:59:42 -0400 | [diff] [blame] | 88 | if (!m_generator) |
dsinclair | c777f48 | 2016-05-04 17:57:03 -0700 | [diff] [blame] | 89 | return FWL_Error::PropertyInvalid; |
Dan Sinclair | 811b8a4 | 2016-03-17 08:59:42 -0400 | [diff] [blame] | 90 | m_generator->AddRectangle(left, top, left + width, top + height); |
dsinclair | c777f48 | 2016-05-04 17:57:03 -0700 | [diff] [blame] | 91 | return FWL_Error::Succeeded; |
Dan Sinclair | 811b8a4 | 2016-03-17 08:59:42 -0400 | [diff] [blame] | 92 | } |
| 93 | |
dan sinclair | b147e07 | 2017-02-22 19:56:15 -0500 | [diff] [blame] | 94 | FWL_Error CFX_Path::AddEllipse(const CFX_PointF& pos, const CFX_SizeF& size) { |
Dan Sinclair | 811b8a4 | 2016-03-17 08:59:42 -0400 | [diff] [blame] | 95 | if (!m_generator) |
dsinclair | c777f48 | 2016-05-04 17:57:03 -0700 | [diff] [blame] | 96 | return FWL_Error::PropertyInvalid; |
dan sinclair | b147e07 | 2017-02-22 19:56:15 -0500 | [diff] [blame] | 97 | CFX_SizeF newSize = size / 2.0f; |
| 98 | m_generator->AddEllipse( |
| 99 | CFX_PointF(pos.x + newSize.width, pos.y + newSize.height), newSize); |
dsinclair | c777f48 | 2016-05-04 17:57:03 -0700 | [diff] [blame] | 100 | return FWL_Error::Succeeded; |
Dan Sinclair | 811b8a4 | 2016-03-17 08:59:42 -0400 | [diff] [blame] | 101 | } |
| 102 | |
dsinclair | c777f48 | 2016-05-04 17:57:03 -0700 | [diff] [blame] | 103 | FWL_Error CFX_Path::AddEllipse(const CFX_RectF& rect) { |
Dan Sinclair | 811b8a4 | 2016-03-17 08:59:42 -0400 | [diff] [blame] | 104 | if (!m_generator) |
dsinclair | c777f48 | 2016-05-04 17:57:03 -0700 | [diff] [blame] | 105 | return FWL_Error::PropertyInvalid; |
dan sinclair | b147e07 | 2017-02-22 19:56:15 -0500 | [diff] [blame] | 106 | m_generator->AddEllipse( |
| 107 | CFX_PointF(rect.left + rect.Width() / 2, rect.top + rect.Height() / 2), |
| 108 | CFX_SizeF(rect.Width() / 2, rect.Height() / 2)); |
dsinclair | c777f48 | 2016-05-04 17:57:03 -0700 | [diff] [blame] | 109 | return FWL_Error::Succeeded; |
Dan Sinclair | 811b8a4 | 2016-03-17 08:59:42 -0400 | [diff] [blame] | 110 | } |
| 111 | |
dan sinclair | b147e07 | 2017-02-22 19:56:15 -0500 | [diff] [blame] | 112 | FWL_Error CFX_Path::AddArc(const CFX_PointF& pos, |
| 113 | const CFX_SizeF& size, |
dsinclair | c777f48 | 2016-05-04 17:57:03 -0700 | [diff] [blame] | 114 | FX_FLOAT startAngle, |
| 115 | FX_FLOAT sweepAngle) { |
Dan Sinclair | 811b8a4 | 2016-03-17 08:59:42 -0400 | [diff] [blame] | 116 | if (!m_generator) |
dsinclair | c777f48 | 2016-05-04 17:57:03 -0700 | [diff] [blame] | 117 | return FWL_Error::PropertyInvalid; |
dan sinclair | b147e07 | 2017-02-22 19:56:15 -0500 | [diff] [blame] | 118 | CFX_SizeF newSize = size / 2; |
| 119 | m_generator->AddArc(CFX_PointF(pos.x + newSize.width, pos.y + newSize.height), |
| 120 | newSize, startAngle, sweepAngle); |
dsinclair | c777f48 | 2016-05-04 17:57:03 -0700 | [diff] [blame] | 121 | return FWL_Error::Succeeded; |
Dan Sinclair | 811b8a4 | 2016-03-17 08:59:42 -0400 | [diff] [blame] | 122 | } |
| 123 | |
dan sinclair | b147e07 | 2017-02-22 19:56:15 -0500 | [diff] [blame] | 124 | FWL_Error CFX_Path::AddPie(const CFX_PointF& pos, |
| 125 | const CFX_SizeF& size, |
dsinclair | c777f48 | 2016-05-04 17:57:03 -0700 | [diff] [blame] | 126 | FX_FLOAT startAngle, |
| 127 | FX_FLOAT sweepAngle) { |
Dan Sinclair | 811b8a4 | 2016-03-17 08:59:42 -0400 | [diff] [blame] | 128 | if (!m_generator) |
dsinclair | c777f48 | 2016-05-04 17:57:03 -0700 | [diff] [blame] | 129 | return FWL_Error::PropertyInvalid; |
dan sinclair | b147e07 | 2017-02-22 19:56:15 -0500 | [diff] [blame] | 130 | CFX_SizeF newSize = size / 2; |
| 131 | m_generator->AddPie(CFX_PointF(pos.x + newSize.width, pos.y + newSize.height), |
| 132 | newSize, startAngle, sweepAngle); |
dsinclair | c777f48 | 2016-05-04 17:57:03 -0700 | [diff] [blame] | 133 | return FWL_Error::Succeeded; |
Dan Sinclair | 811b8a4 | 2016-03-17 08:59:42 -0400 | [diff] [blame] | 134 | } |
| 135 | |
dsinclair | c777f48 | 2016-05-04 17:57:03 -0700 | [diff] [blame] | 136 | FWL_Error CFX_Path::AddSubpath(CFX_Path* path) { |
Dan Sinclair | 811b8a4 | 2016-03-17 08:59:42 -0400 | [diff] [blame] | 137 | if (!m_generator) |
dsinclair | c777f48 | 2016-05-04 17:57:03 -0700 | [diff] [blame] | 138 | return FWL_Error::PropertyInvalid; |
Dan Sinclair | 811b8a4 | 2016-03-17 08:59:42 -0400 | [diff] [blame] | 139 | m_generator->AddPathData(path->GetPathData()); |
dsinclair | c777f48 | 2016-05-04 17:57:03 -0700 | [diff] [blame] | 140 | return FWL_Error::Succeeded; |
Dan Sinclair | 811b8a4 | 2016-03-17 08:59:42 -0400 | [diff] [blame] | 141 | } |
| 142 | |
dsinclair | c777f48 | 2016-05-04 17:57:03 -0700 | [diff] [blame] | 143 | FWL_Error CFX_Path::Clear() { |
Dan Sinclair | 811b8a4 | 2016-03-17 08:59:42 -0400 | [diff] [blame] | 144 | if (!m_generator) |
dsinclair | c777f48 | 2016-05-04 17:57:03 -0700 | [diff] [blame] | 145 | return FWL_Error::PropertyInvalid; |
Dan Sinclair | e460232 | 2017-02-15 11:07:32 -0500 | [diff] [blame] | 146 | m_generator->GetPathData()->Clear(); |
dsinclair | c777f48 | 2016-05-04 17:57:03 -0700 | [diff] [blame] | 147 | return FWL_Error::Succeeded; |
Dan Sinclair | 811b8a4 | 2016-03-17 08:59:42 -0400 | [diff] [blame] | 148 | } |
| 149 | |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 150 | bool CFX_Path::IsEmpty() const { |
Dan Sinclair | 811b8a4 | 2016-03-17 08:59:42 -0400 | [diff] [blame] | 151 | if (!m_generator) |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 152 | return false; |
Dan Sinclair | e460232 | 2017-02-15 11:07:32 -0500 | [diff] [blame] | 153 | if (m_generator->GetPathData()->GetPoints().empty()) |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 154 | return true; |
| 155 | return false; |
Dan Sinclair | 811b8a4 | 2016-03-17 08:59:42 -0400 | [diff] [blame] | 156 | } |
| 157 | |
weili | 16fccc5 | 2016-08-09 10:33:10 -0700 | [diff] [blame] | 158 | CFX_PathData* CFX_Path::GetPathData() const { |
Dan Sinclair | 811b8a4 | 2016-03-17 08:59:42 -0400 | [diff] [blame] | 159 | if (!m_generator) |
| 160 | return nullptr; |
| 161 | return m_generator->GetPathData(); |
| 162 | } |