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 | |
dsinclair | c777f48 | 2016-05-04 17:57:03 -0700 | [diff] [blame] | 25 | FWL_Error CFX_Path::MoveTo(FX_FLOAT x, FX_FLOAT y) { |
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 | 811b8a4 | 2016-03-17 08:59:42 -0400 | [diff] [blame] | 28 | m_generator->MoveTo(x, y); |
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 | |
dsinclair | c777f48 | 2016-05-04 17:57:03 -0700 | [diff] [blame] | 32 | FWL_Error CFX_Path::LineTo(FX_FLOAT x, FX_FLOAT y) { |
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 | 811b8a4 | 2016-03-17 08:59:42 -0400 | [diff] [blame] | 35 | m_generator->LineTo(x, y); |
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 | |
dsinclair | c777f48 | 2016-05-04 17:57:03 -0700 | [diff] [blame] | 39 | FWL_Error CFX_Path::BezierTo(FX_FLOAT ctrlX1, |
| 40 | FX_FLOAT ctrlY1, |
| 41 | FX_FLOAT ctrlX2, |
| 42 | FX_FLOAT ctrlY2, |
| 43 | FX_FLOAT toX, |
| 44 | FX_FLOAT toY) { |
Dan Sinclair | 811b8a4 | 2016-03-17 08:59:42 -0400 | [diff] [blame] | 45 | if (!m_generator) |
dsinclair | c777f48 | 2016-05-04 17:57:03 -0700 | [diff] [blame] | 46 | return FWL_Error::PropertyInvalid; |
Dan Sinclair | 811b8a4 | 2016-03-17 08:59:42 -0400 | [diff] [blame] | 47 | m_generator->BezierTo(ctrlX1, ctrlY1, ctrlX2, ctrlY2, toX, toY); |
dsinclair | c777f48 | 2016-05-04 17:57:03 -0700 | [diff] [blame] | 48 | return FWL_Error::Succeeded; |
Dan Sinclair | 811b8a4 | 2016-03-17 08:59:42 -0400 | [diff] [blame] | 49 | } |
| 50 | |
dsinclair | c777f48 | 2016-05-04 17:57:03 -0700 | [diff] [blame] | 51 | FWL_Error CFX_Path::ArcTo(FX_FLOAT left, |
| 52 | FX_FLOAT top, |
| 53 | FX_FLOAT width, |
| 54 | FX_FLOAT height, |
| 55 | FX_FLOAT startAngle, |
| 56 | FX_FLOAT sweepAngle) { |
Dan Sinclair | 811b8a4 | 2016-03-17 08:59:42 -0400 | [diff] [blame] | 57 | if (!m_generator) |
dsinclair | c777f48 | 2016-05-04 17:57:03 -0700 | [diff] [blame] | 58 | return FWL_Error::PropertyInvalid; |
Dan Sinclair | 811b8a4 | 2016-03-17 08:59:42 -0400 | [diff] [blame] | 59 | m_generator->ArcTo(left + width / 2, top + height / 2, width / 2, height / 2, |
| 60 | startAngle, sweepAngle); |
dsinclair | c777f48 | 2016-05-04 17:57:03 -0700 | [diff] [blame] | 61 | return FWL_Error::Succeeded; |
Dan Sinclair | 811b8a4 | 2016-03-17 08:59:42 -0400 | [diff] [blame] | 62 | } |
| 63 | |
dsinclair | c777f48 | 2016-05-04 17:57:03 -0700 | [diff] [blame] | 64 | FWL_Error CFX_Path::Close() { |
Dan Sinclair | 811b8a4 | 2016-03-17 08:59:42 -0400 | [diff] [blame] | 65 | if (!m_generator) |
dsinclair | c777f48 | 2016-05-04 17:57:03 -0700 | [diff] [blame] | 66 | return FWL_Error::PropertyInvalid; |
Dan Sinclair | 811b8a4 | 2016-03-17 08:59:42 -0400 | [diff] [blame] | 67 | m_generator->Close(); |
dsinclair | c777f48 | 2016-05-04 17:57:03 -0700 | [diff] [blame] | 68 | return FWL_Error::Succeeded; |
Dan Sinclair | 811b8a4 | 2016-03-17 08:59:42 -0400 | [diff] [blame] | 69 | } |
| 70 | |
dsinclair | c777f48 | 2016-05-04 17:57:03 -0700 | [diff] [blame] | 71 | FWL_Error CFX_Path::AddLine(FX_FLOAT x1, |
| 72 | FX_FLOAT y1, |
| 73 | FX_FLOAT x2, |
| 74 | FX_FLOAT y2) { |
Dan Sinclair | 811b8a4 | 2016-03-17 08:59:42 -0400 | [diff] [blame] | 75 | if (!m_generator) |
dsinclair | c777f48 | 2016-05-04 17:57:03 -0700 | [diff] [blame] | 76 | return FWL_Error::PropertyInvalid; |
Dan Sinclair | 811b8a4 | 2016-03-17 08:59:42 -0400 | [diff] [blame] | 77 | m_generator->AddLine(x1, y1, x2, y2); |
dsinclair | c777f48 | 2016-05-04 17:57:03 -0700 | [diff] [blame] | 78 | return FWL_Error::Succeeded; |
Dan Sinclair | 811b8a4 | 2016-03-17 08:59:42 -0400 | [diff] [blame] | 79 | } |
| 80 | |
dsinclair | c777f48 | 2016-05-04 17:57:03 -0700 | [diff] [blame] | 81 | FWL_Error CFX_Path::AddBezier(FX_FLOAT startX, |
| 82 | FX_FLOAT startY, |
| 83 | FX_FLOAT ctrlX1, |
| 84 | FX_FLOAT ctrlY1, |
| 85 | FX_FLOAT ctrlX2, |
| 86 | FX_FLOAT ctrlY2, |
| 87 | FX_FLOAT endX, |
| 88 | FX_FLOAT endY) { |
Dan Sinclair | 811b8a4 | 2016-03-17 08:59:42 -0400 | [diff] [blame] | 89 | if (!m_generator) |
dsinclair | c777f48 | 2016-05-04 17:57:03 -0700 | [diff] [blame] | 90 | return FWL_Error::PropertyInvalid; |
Dan Sinclair | 811b8a4 | 2016-03-17 08:59:42 -0400 | [diff] [blame] | 91 | m_generator->AddBezier(startX, startY, ctrlX1, ctrlY1, ctrlX2, ctrlY2, endX, |
| 92 | endY); |
dsinclair | c777f48 | 2016-05-04 17:57:03 -0700 | [diff] [blame] | 93 | return FWL_Error::Succeeded; |
Dan Sinclair | 811b8a4 | 2016-03-17 08:59:42 -0400 | [diff] [blame] | 94 | } |
| 95 | |
dsinclair | c777f48 | 2016-05-04 17:57:03 -0700 | [diff] [blame] | 96 | FWL_Error CFX_Path::AddRectangle(FX_FLOAT left, |
| 97 | FX_FLOAT top, |
| 98 | FX_FLOAT width, |
| 99 | FX_FLOAT height) { |
Dan Sinclair | 811b8a4 | 2016-03-17 08:59:42 -0400 | [diff] [blame] | 100 | if (!m_generator) |
dsinclair | c777f48 | 2016-05-04 17:57:03 -0700 | [diff] [blame] | 101 | return FWL_Error::PropertyInvalid; |
Dan Sinclair | 811b8a4 | 2016-03-17 08:59:42 -0400 | [diff] [blame] | 102 | m_generator->AddRectangle(left, top, left + width, top + height); |
dsinclair | c777f48 | 2016-05-04 17:57:03 -0700 | [diff] [blame] | 103 | return FWL_Error::Succeeded; |
Dan Sinclair | 811b8a4 | 2016-03-17 08:59:42 -0400 | [diff] [blame] | 104 | } |
| 105 | |
dsinclair | c777f48 | 2016-05-04 17:57:03 -0700 | [diff] [blame] | 106 | FWL_Error CFX_Path::AddEllipse(FX_FLOAT left, |
| 107 | FX_FLOAT top, |
| 108 | FX_FLOAT width, |
| 109 | FX_FLOAT height) { |
Dan Sinclair | 811b8a4 | 2016-03-17 08:59:42 -0400 | [diff] [blame] | 110 | if (!m_generator) |
dsinclair | c777f48 | 2016-05-04 17:57:03 -0700 | [diff] [blame] | 111 | return FWL_Error::PropertyInvalid; |
Dan Sinclair | 811b8a4 | 2016-03-17 08:59:42 -0400 | [diff] [blame] | 112 | m_generator->AddEllipse(left + width / 2, top + height / 2, width / 2, |
| 113 | height / 2); |
dsinclair | c777f48 | 2016-05-04 17:57:03 -0700 | [diff] [blame] | 114 | return FWL_Error::Succeeded; |
Dan Sinclair | 811b8a4 | 2016-03-17 08:59:42 -0400 | [diff] [blame] | 115 | } |
| 116 | |
dsinclair | c777f48 | 2016-05-04 17:57:03 -0700 | [diff] [blame] | 117 | FWL_Error CFX_Path::AddEllipse(const CFX_RectF& rect) { |
Dan Sinclair | 811b8a4 | 2016-03-17 08:59:42 -0400 | [diff] [blame] | 118 | if (!m_generator) |
dsinclair | c777f48 | 2016-05-04 17:57:03 -0700 | [diff] [blame] | 119 | return FWL_Error::PropertyInvalid; |
Dan Sinclair | 811b8a4 | 2016-03-17 08:59:42 -0400 | [diff] [blame] | 120 | m_generator->AddEllipse(rect.left + rect.Width() / 2, |
| 121 | rect.top + rect.Height() / 2, rect.Width() / 2, |
| 122 | rect.Height() / 2); |
dsinclair | c777f48 | 2016-05-04 17:57:03 -0700 | [diff] [blame] | 123 | return FWL_Error::Succeeded; |
Dan Sinclair | 811b8a4 | 2016-03-17 08:59:42 -0400 | [diff] [blame] | 124 | } |
| 125 | |
dsinclair | c777f48 | 2016-05-04 17:57:03 -0700 | [diff] [blame] | 126 | FWL_Error CFX_Path::AddArc(FX_FLOAT left, |
| 127 | FX_FLOAT top, |
| 128 | FX_FLOAT width, |
| 129 | FX_FLOAT height, |
| 130 | FX_FLOAT startAngle, |
| 131 | FX_FLOAT sweepAngle) { |
Dan Sinclair | 811b8a4 | 2016-03-17 08:59:42 -0400 | [diff] [blame] | 132 | if (!m_generator) |
dsinclair | c777f48 | 2016-05-04 17:57:03 -0700 | [diff] [blame] | 133 | return FWL_Error::PropertyInvalid; |
Dan Sinclair | 811b8a4 | 2016-03-17 08:59:42 -0400 | [diff] [blame] | 134 | m_generator->AddArc(left + width / 2, top + height / 2, width / 2, height / 2, |
| 135 | startAngle, sweepAngle); |
dsinclair | c777f48 | 2016-05-04 17:57:03 -0700 | [diff] [blame] | 136 | return FWL_Error::Succeeded; |
Dan Sinclair | 811b8a4 | 2016-03-17 08:59:42 -0400 | [diff] [blame] | 137 | } |
| 138 | |
dsinclair | c777f48 | 2016-05-04 17:57:03 -0700 | [diff] [blame] | 139 | FWL_Error CFX_Path::AddPie(FX_FLOAT left, |
| 140 | FX_FLOAT top, |
| 141 | FX_FLOAT width, |
| 142 | FX_FLOAT height, |
| 143 | FX_FLOAT startAngle, |
| 144 | FX_FLOAT sweepAngle) { |
Dan Sinclair | 811b8a4 | 2016-03-17 08:59:42 -0400 | [diff] [blame] | 145 | if (!m_generator) |
dsinclair | c777f48 | 2016-05-04 17:57:03 -0700 | [diff] [blame] | 146 | return FWL_Error::PropertyInvalid; |
Dan Sinclair | 811b8a4 | 2016-03-17 08:59:42 -0400 | [diff] [blame] | 147 | m_generator->AddPie(left + width / 2, top + height / 2, width / 2, height / 2, |
| 148 | startAngle, sweepAngle); |
dsinclair | c777f48 | 2016-05-04 17:57:03 -0700 | [diff] [blame] | 149 | return FWL_Error::Succeeded; |
Dan Sinclair | 811b8a4 | 2016-03-17 08:59:42 -0400 | [diff] [blame] | 150 | } |
| 151 | |
dsinclair | c777f48 | 2016-05-04 17:57:03 -0700 | [diff] [blame] | 152 | FWL_Error CFX_Path::AddSubpath(CFX_Path* path) { |
Dan Sinclair | 811b8a4 | 2016-03-17 08:59:42 -0400 | [diff] [blame] | 153 | if (!m_generator) |
dsinclair | c777f48 | 2016-05-04 17:57:03 -0700 | [diff] [blame] | 154 | return FWL_Error::PropertyInvalid; |
Dan Sinclair | 811b8a4 | 2016-03-17 08:59:42 -0400 | [diff] [blame] | 155 | m_generator->AddPathData(path->GetPathData()); |
dsinclair | c777f48 | 2016-05-04 17:57:03 -0700 | [diff] [blame] | 156 | return FWL_Error::Succeeded; |
Dan Sinclair | 811b8a4 | 2016-03-17 08:59:42 -0400 | [diff] [blame] | 157 | } |
| 158 | |
dsinclair | c777f48 | 2016-05-04 17:57:03 -0700 | [diff] [blame] | 159 | FWL_Error CFX_Path::Clear() { |
Dan Sinclair | 811b8a4 | 2016-03-17 08:59:42 -0400 | [diff] [blame] | 160 | if (!m_generator) |
dsinclair | c777f48 | 2016-05-04 17:57:03 -0700 | [diff] [blame] | 161 | return FWL_Error::PropertyInvalid; |
Dan Sinclair | 811b8a4 | 2016-03-17 08:59:42 -0400 | [diff] [blame] | 162 | m_generator->GetPathData()->SetPointCount(0); |
dsinclair | c777f48 | 2016-05-04 17:57:03 -0700 | [diff] [blame] | 163 | return FWL_Error::Succeeded; |
Dan Sinclair | 811b8a4 | 2016-03-17 08:59:42 -0400 | [diff] [blame] | 164 | } |
| 165 | |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 166 | bool CFX_Path::IsEmpty() const { |
Dan Sinclair | 811b8a4 | 2016-03-17 08:59:42 -0400 | [diff] [blame] | 167 | if (!m_generator) |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 168 | return false; |
dsinclair | c777f48 | 2016-05-04 17:57:03 -0700 | [diff] [blame] | 169 | if (m_generator->GetPathData()->GetPointCount() == 0) |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 170 | return true; |
| 171 | return false; |
Dan Sinclair | 811b8a4 | 2016-03-17 08:59:42 -0400 | [diff] [blame] | 172 | } |
| 173 | |
weili | 16fccc5 | 2016-08-09 10:33:10 -0700 | [diff] [blame] | 174 | CFX_PathData* CFX_Path::GetPathData() const { |
Dan Sinclair | 811b8a4 | 2016-03-17 08:59:42 -0400 | [diff] [blame] | 175 | if (!m_generator) |
| 176 | return nullptr; |
| 177 | return m_generator->GetPathData(); |
| 178 | } |