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