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 | |
npm | 660de3c | 2016-08-08 08:18:29 -0700 | [diff] [blame^] | 9 | #include "core/fxge/include/cfx_pathdata.h" |
Dan Sinclair | 811b8a4 | 2016-03-17 08:59:42 -0400 | [diff] [blame] | 10 | #include "xfa/fxgraphics/cfx_path_generator.h" |
| 11 | |
| 12 | CFX_Path::CFX_Path() { |
| 13 | m_generator = nullptr; |
| 14 | } |
| 15 | |
dsinclair | c777f48 | 2016-05-04 17:57:03 -0700 | [diff] [blame] | 16 | FWL_Error CFX_Path::Create() { |
Dan Sinclair | 811b8a4 | 2016-03-17 08:59:42 -0400 | [diff] [blame] | 17 | if (m_generator) |
dsinclair | c777f48 | 2016-05-04 17:57:03 -0700 | [diff] [blame] | 18 | return FWL_Error::PropertyInvalid; |
Dan Sinclair | 811b8a4 | 2016-03-17 08:59:42 -0400 | [diff] [blame] | 19 | |
| 20 | m_generator = new CFX_PathGenerator; |
| 21 | m_generator->Create(); |
dsinclair | c777f48 | 2016-05-04 17:57:03 -0700 | [diff] [blame] | 22 | return FWL_Error::Succeeded; |
Dan Sinclair | 811b8a4 | 2016-03-17 08:59:42 -0400 | [diff] [blame] | 23 | } |
| 24 | |
| 25 | CFX_Path::~CFX_Path() { |
| 26 | delete m_generator; |
| 27 | } |
| 28 | |
dsinclair | c777f48 | 2016-05-04 17:57:03 -0700 | [diff] [blame] | 29 | FWL_Error CFX_Path::MoveTo(FX_FLOAT x, FX_FLOAT y) { |
Dan Sinclair | 811b8a4 | 2016-03-17 08:59:42 -0400 | [diff] [blame] | 30 | if (!m_generator) |
dsinclair | c777f48 | 2016-05-04 17:57:03 -0700 | [diff] [blame] | 31 | return FWL_Error::PropertyInvalid; |
Dan Sinclair | 811b8a4 | 2016-03-17 08:59:42 -0400 | [diff] [blame] | 32 | m_generator->MoveTo(x, y); |
dsinclair | c777f48 | 2016-05-04 17:57:03 -0700 | [diff] [blame] | 33 | return FWL_Error::Succeeded; |
Dan Sinclair | 811b8a4 | 2016-03-17 08:59:42 -0400 | [diff] [blame] | 34 | } |
| 35 | |
dsinclair | c777f48 | 2016-05-04 17:57:03 -0700 | [diff] [blame] | 36 | FWL_Error CFX_Path::LineTo(FX_FLOAT x, FX_FLOAT y) { |
Dan Sinclair | 811b8a4 | 2016-03-17 08:59:42 -0400 | [diff] [blame] | 37 | if (!m_generator) |
dsinclair | c777f48 | 2016-05-04 17:57:03 -0700 | [diff] [blame] | 38 | return FWL_Error::PropertyInvalid; |
Dan Sinclair | 811b8a4 | 2016-03-17 08:59:42 -0400 | [diff] [blame] | 39 | m_generator->LineTo(x, y); |
dsinclair | c777f48 | 2016-05-04 17:57:03 -0700 | [diff] [blame] | 40 | return FWL_Error::Succeeded; |
Dan Sinclair | 811b8a4 | 2016-03-17 08:59:42 -0400 | [diff] [blame] | 41 | } |
| 42 | |
dsinclair | c777f48 | 2016-05-04 17:57:03 -0700 | [diff] [blame] | 43 | FWL_Error CFX_Path::BezierTo(FX_FLOAT ctrlX1, |
| 44 | FX_FLOAT ctrlY1, |
| 45 | FX_FLOAT ctrlX2, |
| 46 | FX_FLOAT ctrlY2, |
| 47 | FX_FLOAT toX, |
| 48 | FX_FLOAT toY) { |
Dan Sinclair | 811b8a4 | 2016-03-17 08:59:42 -0400 | [diff] [blame] | 49 | if (!m_generator) |
dsinclair | c777f48 | 2016-05-04 17:57:03 -0700 | [diff] [blame] | 50 | return FWL_Error::PropertyInvalid; |
Dan Sinclair | 811b8a4 | 2016-03-17 08:59:42 -0400 | [diff] [blame] | 51 | m_generator->BezierTo(ctrlX1, ctrlY1, ctrlX2, ctrlY2, toX, toY); |
dsinclair | c777f48 | 2016-05-04 17:57:03 -0700 | [diff] [blame] | 52 | return FWL_Error::Succeeded; |
Dan Sinclair | 811b8a4 | 2016-03-17 08:59:42 -0400 | [diff] [blame] | 53 | } |
| 54 | |
dsinclair | c777f48 | 2016-05-04 17:57:03 -0700 | [diff] [blame] | 55 | FWL_Error CFX_Path::ArcTo(FX_FLOAT left, |
| 56 | FX_FLOAT top, |
| 57 | FX_FLOAT width, |
| 58 | FX_FLOAT height, |
| 59 | FX_FLOAT startAngle, |
| 60 | FX_FLOAT sweepAngle) { |
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->ArcTo(left + width / 2, top + height / 2, width / 2, height / 2, |
| 64 | startAngle, sweepAngle); |
dsinclair | c777f48 | 2016-05-04 17:57:03 -0700 | [diff] [blame] | 65 | return FWL_Error::Succeeded; |
Dan Sinclair | 811b8a4 | 2016-03-17 08:59:42 -0400 | [diff] [blame] | 66 | } |
| 67 | |
dsinclair | c777f48 | 2016-05-04 17:57:03 -0700 | [diff] [blame] | 68 | FWL_Error CFX_Path::Close() { |
Dan Sinclair | 811b8a4 | 2016-03-17 08:59:42 -0400 | [diff] [blame] | 69 | if (!m_generator) |
dsinclair | c777f48 | 2016-05-04 17:57:03 -0700 | [diff] [blame] | 70 | return FWL_Error::PropertyInvalid; |
Dan Sinclair | 811b8a4 | 2016-03-17 08:59:42 -0400 | [diff] [blame] | 71 | m_generator->Close(); |
dsinclair | c777f48 | 2016-05-04 17:57:03 -0700 | [diff] [blame] | 72 | return FWL_Error::Succeeded; |
Dan Sinclair | 811b8a4 | 2016-03-17 08:59:42 -0400 | [diff] [blame] | 73 | } |
| 74 | |
dsinclair | c777f48 | 2016-05-04 17:57:03 -0700 | [diff] [blame] | 75 | FWL_Error CFX_Path::AddLine(FX_FLOAT x1, |
| 76 | FX_FLOAT y1, |
| 77 | FX_FLOAT x2, |
| 78 | FX_FLOAT y2) { |
Dan Sinclair | 811b8a4 | 2016-03-17 08:59:42 -0400 | [diff] [blame] | 79 | if (!m_generator) |
dsinclair | c777f48 | 2016-05-04 17:57:03 -0700 | [diff] [blame] | 80 | return FWL_Error::PropertyInvalid; |
Dan Sinclair | 811b8a4 | 2016-03-17 08:59:42 -0400 | [diff] [blame] | 81 | m_generator->AddLine(x1, y1, x2, y2); |
dsinclair | c777f48 | 2016-05-04 17:57:03 -0700 | [diff] [blame] | 82 | return FWL_Error::Succeeded; |
Dan Sinclair | 811b8a4 | 2016-03-17 08:59:42 -0400 | [diff] [blame] | 83 | } |
| 84 | |
dsinclair | c777f48 | 2016-05-04 17:57:03 -0700 | [diff] [blame] | 85 | FWL_Error CFX_Path::AddBezier(FX_FLOAT startX, |
| 86 | FX_FLOAT startY, |
| 87 | FX_FLOAT ctrlX1, |
| 88 | FX_FLOAT ctrlY1, |
| 89 | FX_FLOAT ctrlX2, |
| 90 | FX_FLOAT ctrlY2, |
| 91 | FX_FLOAT endX, |
| 92 | FX_FLOAT endY) { |
Dan Sinclair | 811b8a4 | 2016-03-17 08:59:42 -0400 | [diff] [blame] | 93 | if (!m_generator) |
dsinclair | c777f48 | 2016-05-04 17:57:03 -0700 | [diff] [blame] | 94 | return FWL_Error::PropertyInvalid; |
Dan Sinclair | 811b8a4 | 2016-03-17 08:59:42 -0400 | [diff] [blame] | 95 | m_generator->AddBezier(startX, startY, ctrlX1, ctrlY1, ctrlX2, ctrlY2, endX, |
| 96 | endY); |
dsinclair | c777f48 | 2016-05-04 17:57:03 -0700 | [diff] [blame] | 97 | return FWL_Error::Succeeded; |
Dan Sinclair | 811b8a4 | 2016-03-17 08:59:42 -0400 | [diff] [blame] | 98 | } |
| 99 | |
dsinclair | c777f48 | 2016-05-04 17:57:03 -0700 | [diff] [blame] | 100 | FWL_Error CFX_Path::AddRectangle(FX_FLOAT left, |
| 101 | FX_FLOAT top, |
| 102 | FX_FLOAT width, |
| 103 | FX_FLOAT height) { |
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 | 811b8a4 | 2016-03-17 08:59:42 -0400 | [diff] [blame] | 106 | m_generator->AddRectangle(left, top, left + width, top + height); |
dsinclair | c777f48 | 2016-05-04 17:57:03 -0700 | [diff] [blame] | 107 | return FWL_Error::Succeeded; |
Dan Sinclair | 811b8a4 | 2016-03-17 08:59:42 -0400 | [diff] [blame] | 108 | } |
| 109 | |
dsinclair | c777f48 | 2016-05-04 17:57:03 -0700 | [diff] [blame] | 110 | FWL_Error CFX_Path::AddEllipse(FX_FLOAT left, |
| 111 | FX_FLOAT top, |
| 112 | FX_FLOAT width, |
| 113 | FX_FLOAT height) { |
Dan Sinclair | 811b8a4 | 2016-03-17 08:59:42 -0400 | [diff] [blame] | 114 | if (!m_generator) |
dsinclair | c777f48 | 2016-05-04 17:57:03 -0700 | [diff] [blame] | 115 | return FWL_Error::PropertyInvalid; |
Dan Sinclair | 811b8a4 | 2016-03-17 08:59:42 -0400 | [diff] [blame] | 116 | m_generator->AddEllipse(left + width / 2, top + height / 2, width / 2, |
| 117 | height / 2); |
dsinclair | c777f48 | 2016-05-04 17:57:03 -0700 | [diff] [blame] | 118 | return FWL_Error::Succeeded; |
Dan Sinclair | 811b8a4 | 2016-03-17 08:59:42 -0400 | [diff] [blame] | 119 | } |
| 120 | |
dsinclair | c777f48 | 2016-05-04 17:57:03 -0700 | [diff] [blame] | 121 | FWL_Error CFX_Path::AddEllipse(const CFX_RectF& rect) { |
Dan Sinclair | 811b8a4 | 2016-03-17 08:59:42 -0400 | [diff] [blame] | 122 | if (!m_generator) |
dsinclair | c777f48 | 2016-05-04 17:57:03 -0700 | [diff] [blame] | 123 | return FWL_Error::PropertyInvalid; |
Dan Sinclair | 811b8a4 | 2016-03-17 08:59:42 -0400 | [diff] [blame] | 124 | m_generator->AddEllipse(rect.left + rect.Width() / 2, |
| 125 | rect.top + rect.Height() / 2, rect.Width() / 2, |
| 126 | rect.Height() / 2); |
dsinclair | c777f48 | 2016-05-04 17:57:03 -0700 | [diff] [blame] | 127 | return FWL_Error::Succeeded; |
Dan Sinclair | 811b8a4 | 2016-03-17 08:59:42 -0400 | [diff] [blame] | 128 | } |
| 129 | |
dsinclair | c777f48 | 2016-05-04 17:57:03 -0700 | [diff] [blame] | 130 | FWL_Error CFX_Path::AddArc(FX_FLOAT left, |
| 131 | FX_FLOAT top, |
| 132 | FX_FLOAT width, |
| 133 | FX_FLOAT height, |
| 134 | FX_FLOAT startAngle, |
| 135 | FX_FLOAT sweepAngle) { |
Dan Sinclair | 811b8a4 | 2016-03-17 08:59:42 -0400 | [diff] [blame] | 136 | if (!m_generator) |
dsinclair | c777f48 | 2016-05-04 17:57:03 -0700 | [diff] [blame] | 137 | return FWL_Error::PropertyInvalid; |
Dan Sinclair | 811b8a4 | 2016-03-17 08:59:42 -0400 | [diff] [blame] | 138 | m_generator->AddArc(left + width / 2, top + height / 2, width / 2, height / 2, |
| 139 | startAngle, sweepAngle); |
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::AddPie(FX_FLOAT left, |
| 144 | FX_FLOAT top, |
| 145 | FX_FLOAT width, |
| 146 | FX_FLOAT height, |
| 147 | FX_FLOAT startAngle, |
| 148 | FX_FLOAT sweepAngle) { |
Dan Sinclair | 811b8a4 | 2016-03-17 08:59:42 -0400 | [diff] [blame] | 149 | if (!m_generator) |
dsinclair | c777f48 | 2016-05-04 17:57:03 -0700 | [diff] [blame] | 150 | return FWL_Error::PropertyInvalid; |
Dan Sinclair | 811b8a4 | 2016-03-17 08:59:42 -0400 | [diff] [blame] | 151 | m_generator->AddPie(left + width / 2, top + height / 2, width / 2, height / 2, |
| 152 | startAngle, sweepAngle); |
dsinclair | c777f48 | 2016-05-04 17:57:03 -0700 | [diff] [blame] | 153 | return FWL_Error::Succeeded; |
Dan Sinclair | 811b8a4 | 2016-03-17 08:59:42 -0400 | [diff] [blame] | 154 | } |
| 155 | |
dsinclair | c777f48 | 2016-05-04 17:57:03 -0700 | [diff] [blame] | 156 | FWL_Error CFX_Path::AddSubpath(CFX_Path* path) { |
Dan Sinclair | 811b8a4 | 2016-03-17 08:59:42 -0400 | [diff] [blame] | 157 | if (!m_generator) |
dsinclair | c777f48 | 2016-05-04 17:57:03 -0700 | [diff] [blame] | 158 | return FWL_Error::PropertyInvalid; |
Dan Sinclair | 811b8a4 | 2016-03-17 08:59:42 -0400 | [diff] [blame] | 159 | m_generator->AddPathData(path->GetPathData()); |
dsinclair | c777f48 | 2016-05-04 17:57:03 -0700 | [diff] [blame] | 160 | return FWL_Error::Succeeded; |
Dan Sinclair | 811b8a4 | 2016-03-17 08:59:42 -0400 | [diff] [blame] | 161 | } |
| 162 | |
dsinclair | c777f48 | 2016-05-04 17:57:03 -0700 | [diff] [blame] | 163 | FWL_Error CFX_Path::Clear() { |
Dan Sinclair | 811b8a4 | 2016-03-17 08:59:42 -0400 | [diff] [blame] | 164 | if (!m_generator) |
dsinclair | c777f48 | 2016-05-04 17:57:03 -0700 | [diff] [blame] | 165 | return FWL_Error::PropertyInvalid; |
Dan Sinclair | 811b8a4 | 2016-03-17 08:59:42 -0400 | [diff] [blame] | 166 | m_generator->GetPathData()->SetPointCount(0); |
dsinclair | c777f48 | 2016-05-04 17:57:03 -0700 | [diff] [blame] | 167 | return FWL_Error::Succeeded; |
Dan Sinclair | 811b8a4 | 2016-03-17 08:59:42 -0400 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | FX_BOOL CFX_Path::IsEmpty() { |
| 171 | if (!m_generator) |
dsinclair | c777f48 | 2016-05-04 17:57:03 -0700 | [diff] [blame] | 172 | return FALSE; |
| 173 | if (m_generator->GetPathData()->GetPointCount() == 0) |
Dan Sinclair | 811b8a4 | 2016-03-17 08:59:42 -0400 | [diff] [blame] | 174 | return TRUE; |
Dan Sinclair | 811b8a4 | 2016-03-17 08:59:42 -0400 | [diff] [blame] | 175 | return FALSE; |
| 176 | } |
| 177 | |
| 178 | CFX_PathData* CFX_Path::GetPathData() { |
| 179 | if (!m_generator) |
| 180 | return nullptr; |
| 181 | return m_generator->GetPathData(); |
| 182 | } |