blob: e4fb727092a6d797c129b0e171758a75e370d50e [file] [log] [blame]
Dan Sinclair811b8a42016-03-17 08:59:42 -04001// 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
dsinclair74a34fc2016-09-29 16:41:42 -07009#include "core/fxge/cfx_pathdata.h"
Dan Sinclair811b8a42016-03-17 08:59:42 -040010#include "xfa/fxgraphics/cfx_path_generator.h"
11
weili16fccc52016-08-09 10:33:10 -070012CFX_Path::CFX_Path() {}
Dan Sinclair811b8a42016-03-17 08:59:42 -040013
dsinclairc777f482016-05-04 17:57:03 -070014FWL_Error CFX_Path::Create() {
Dan Sinclair811b8a42016-03-17 08:59:42 -040015 if (m_generator)
dsinclairc777f482016-05-04 17:57:03 -070016 return FWL_Error::PropertyInvalid;
Dan Sinclair811b8a42016-03-17 08:59:42 -040017
weili16fccc52016-08-09 10:33:10 -070018 m_generator.reset(new CFX_PathGenerator());
dsinclairc777f482016-05-04 17:57:03 -070019 return FWL_Error::Succeeded;
Dan Sinclair811b8a42016-03-17 08:59:42 -040020}
21
weili16fccc52016-08-09 10:33:10 -070022CFX_Path::~CFX_Path() {}
Dan Sinclair811b8a42016-03-17 08:59:42 -040023
dsinclairc777f482016-05-04 17:57:03 -070024FWL_Error CFX_Path::MoveTo(FX_FLOAT x, FX_FLOAT y) {
Dan Sinclair811b8a42016-03-17 08:59:42 -040025 if (!m_generator)
dsinclairc777f482016-05-04 17:57:03 -070026 return FWL_Error::PropertyInvalid;
Dan Sinclair811b8a42016-03-17 08:59:42 -040027 m_generator->MoveTo(x, y);
dsinclairc777f482016-05-04 17:57:03 -070028 return FWL_Error::Succeeded;
Dan Sinclair811b8a42016-03-17 08:59:42 -040029}
30
dsinclairc777f482016-05-04 17:57:03 -070031FWL_Error CFX_Path::LineTo(FX_FLOAT x, FX_FLOAT y) {
Dan Sinclair811b8a42016-03-17 08:59:42 -040032 if (!m_generator)
dsinclairc777f482016-05-04 17:57:03 -070033 return FWL_Error::PropertyInvalid;
Dan Sinclair811b8a42016-03-17 08:59:42 -040034 m_generator->LineTo(x, y);
dsinclairc777f482016-05-04 17:57:03 -070035 return FWL_Error::Succeeded;
Dan Sinclair811b8a42016-03-17 08:59:42 -040036}
37
dsinclairc777f482016-05-04 17:57:03 -070038FWL_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 Sinclair811b8a42016-03-17 08:59:42 -040044 if (!m_generator)
dsinclairc777f482016-05-04 17:57:03 -070045 return FWL_Error::PropertyInvalid;
Dan Sinclair811b8a42016-03-17 08:59:42 -040046 m_generator->BezierTo(ctrlX1, ctrlY1, ctrlX2, ctrlY2, toX, toY);
dsinclairc777f482016-05-04 17:57:03 -070047 return FWL_Error::Succeeded;
Dan Sinclair811b8a42016-03-17 08:59:42 -040048}
49
dsinclairc777f482016-05-04 17:57:03 -070050FWL_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 Sinclair811b8a42016-03-17 08:59:42 -040056 if (!m_generator)
dsinclairc777f482016-05-04 17:57:03 -070057 return FWL_Error::PropertyInvalid;
Dan Sinclair811b8a42016-03-17 08:59:42 -040058 m_generator->ArcTo(left + width / 2, top + height / 2, width / 2, height / 2,
59 startAngle, sweepAngle);
dsinclairc777f482016-05-04 17:57:03 -070060 return FWL_Error::Succeeded;
Dan Sinclair811b8a42016-03-17 08:59:42 -040061}
62
dsinclairc777f482016-05-04 17:57:03 -070063FWL_Error CFX_Path::Close() {
Dan Sinclair811b8a42016-03-17 08:59:42 -040064 if (!m_generator)
dsinclairc777f482016-05-04 17:57:03 -070065 return FWL_Error::PropertyInvalid;
Dan Sinclair811b8a42016-03-17 08:59:42 -040066 m_generator->Close();
dsinclairc777f482016-05-04 17:57:03 -070067 return FWL_Error::Succeeded;
Dan Sinclair811b8a42016-03-17 08:59:42 -040068}
69
dsinclairc777f482016-05-04 17:57:03 -070070FWL_Error CFX_Path::AddLine(FX_FLOAT x1,
71 FX_FLOAT y1,
72 FX_FLOAT x2,
73 FX_FLOAT y2) {
Dan Sinclair811b8a42016-03-17 08:59:42 -040074 if (!m_generator)
dsinclairc777f482016-05-04 17:57:03 -070075 return FWL_Error::PropertyInvalid;
Dan Sinclair811b8a42016-03-17 08:59:42 -040076 m_generator->AddLine(x1, y1, x2, y2);
dsinclairc777f482016-05-04 17:57:03 -070077 return FWL_Error::Succeeded;
Dan Sinclair811b8a42016-03-17 08:59:42 -040078}
79
dsinclairc777f482016-05-04 17:57:03 -070080FWL_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 Sinclair811b8a42016-03-17 08:59:42 -040088 if (!m_generator)
dsinclairc777f482016-05-04 17:57:03 -070089 return FWL_Error::PropertyInvalid;
Dan Sinclair811b8a42016-03-17 08:59:42 -040090 m_generator->AddBezier(startX, startY, ctrlX1, ctrlY1, ctrlX2, ctrlY2, endX,
91 endY);
dsinclairc777f482016-05-04 17:57:03 -070092 return FWL_Error::Succeeded;
Dan Sinclair811b8a42016-03-17 08:59:42 -040093}
94
dsinclairc777f482016-05-04 17:57:03 -070095FWL_Error CFX_Path::AddRectangle(FX_FLOAT left,
96 FX_FLOAT top,
97 FX_FLOAT width,
98 FX_FLOAT height) {
Dan Sinclair811b8a42016-03-17 08:59:42 -040099 if (!m_generator)
dsinclairc777f482016-05-04 17:57:03 -0700100 return FWL_Error::PropertyInvalid;
Dan Sinclair811b8a42016-03-17 08:59:42 -0400101 m_generator->AddRectangle(left, top, left + width, top + height);
dsinclairc777f482016-05-04 17:57:03 -0700102 return FWL_Error::Succeeded;
Dan Sinclair811b8a42016-03-17 08:59:42 -0400103}
104
dsinclairc777f482016-05-04 17:57:03 -0700105FWL_Error CFX_Path::AddEllipse(FX_FLOAT left,
106 FX_FLOAT top,
107 FX_FLOAT width,
108 FX_FLOAT height) {
Dan Sinclair811b8a42016-03-17 08:59:42 -0400109 if (!m_generator)
dsinclairc777f482016-05-04 17:57:03 -0700110 return FWL_Error::PropertyInvalid;
Dan Sinclair811b8a42016-03-17 08:59:42 -0400111 m_generator->AddEllipse(left + width / 2, top + height / 2, width / 2,
112 height / 2);
dsinclairc777f482016-05-04 17:57:03 -0700113 return FWL_Error::Succeeded;
Dan Sinclair811b8a42016-03-17 08:59:42 -0400114}
115
dsinclairc777f482016-05-04 17:57:03 -0700116FWL_Error CFX_Path::AddEllipse(const CFX_RectF& rect) {
Dan Sinclair811b8a42016-03-17 08:59:42 -0400117 if (!m_generator)
dsinclairc777f482016-05-04 17:57:03 -0700118 return FWL_Error::PropertyInvalid;
Dan Sinclair811b8a42016-03-17 08:59:42 -0400119 m_generator->AddEllipse(rect.left + rect.Width() / 2,
120 rect.top + rect.Height() / 2, rect.Width() / 2,
121 rect.Height() / 2);
dsinclairc777f482016-05-04 17:57:03 -0700122 return FWL_Error::Succeeded;
Dan Sinclair811b8a42016-03-17 08:59:42 -0400123}
124
dsinclairc777f482016-05-04 17:57:03 -0700125FWL_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 Sinclair811b8a42016-03-17 08:59:42 -0400131 if (!m_generator)
dsinclairc777f482016-05-04 17:57:03 -0700132 return FWL_Error::PropertyInvalid;
Dan Sinclair811b8a42016-03-17 08:59:42 -0400133 m_generator->AddArc(left + width / 2, top + height / 2, width / 2, height / 2,
134 startAngle, sweepAngle);
dsinclairc777f482016-05-04 17:57:03 -0700135 return FWL_Error::Succeeded;
Dan Sinclair811b8a42016-03-17 08:59:42 -0400136}
137
dsinclairc777f482016-05-04 17:57:03 -0700138FWL_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 Sinclair811b8a42016-03-17 08:59:42 -0400144 if (!m_generator)
dsinclairc777f482016-05-04 17:57:03 -0700145 return FWL_Error::PropertyInvalid;
Dan Sinclair811b8a42016-03-17 08:59:42 -0400146 m_generator->AddPie(left + width / 2, top + height / 2, width / 2, height / 2,
147 startAngle, sweepAngle);
dsinclairc777f482016-05-04 17:57:03 -0700148 return FWL_Error::Succeeded;
Dan Sinclair811b8a42016-03-17 08:59:42 -0400149}
150
dsinclairc777f482016-05-04 17:57:03 -0700151FWL_Error CFX_Path::AddSubpath(CFX_Path* path) {
Dan Sinclair811b8a42016-03-17 08:59:42 -0400152 if (!m_generator)
dsinclairc777f482016-05-04 17:57:03 -0700153 return FWL_Error::PropertyInvalid;
Dan Sinclair811b8a42016-03-17 08:59:42 -0400154 m_generator->AddPathData(path->GetPathData());
dsinclairc777f482016-05-04 17:57:03 -0700155 return FWL_Error::Succeeded;
Dan Sinclair811b8a42016-03-17 08:59:42 -0400156}
157
dsinclairc777f482016-05-04 17:57:03 -0700158FWL_Error CFX_Path::Clear() {
Dan Sinclair811b8a42016-03-17 08:59:42 -0400159 if (!m_generator)
dsinclairc777f482016-05-04 17:57:03 -0700160 return FWL_Error::PropertyInvalid;
Dan Sinclair811b8a42016-03-17 08:59:42 -0400161 m_generator->GetPathData()->SetPointCount(0);
dsinclairc777f482016-05-04 17:57:03 -0700162 return FWL_Error::Succeeded;
Dan Sinclair811b8a42016-03-17 08:59:42 -0400163}
164
weili16fccc52016-08-09 10:33:10 -0700165FX_BOOL CFX_Path::IsEmpty() const {
Dan Sinclair811b8a42016-03-17 08:59:42 -0400166 if (!m_generator)
dsinclairc777f482016-05-04 17:57:03 -0700167 return FALSE;
168 if (m_generator->GetPathData()->GetPointCount() == 0)
Dan Sinclair811b8a42016-03-17 08:59:42 -0400169 return TRUE;
Dan Sinclair811b8a42016-03-17 08:59:42 -0400170 return FALSE;
171}
172
weili16fccc52016-08-09 10:33:10 -0700173CFX_PathData* CFX_Path::GetPathData() const {
Dan Sinclair811b8a42016-03-17 08:59:42 -0400174 if (!m_generator)
175 return nullptr;
176 return m_generator->GetPathData();
177}