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