blob: 3288631f15a366f4c5235f583ad9cd1ebcce86a0 [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"
tsepeza9caab92016-12-14 05:57:10 -080010#include "third_party/base/ptr_util.h"
Dan Sinclair811b8a42016-03-17 08:59:42 -040011#include "xfa/fxgraphics/cfx_path_generator.h"
12
weili16fccc52016-08-09 10:33:10 -070013CFX_Path::CFX_Path() {}
Dan Sinclair811b8a42016-03-17 08:59:42 -040014
dsinclairc777f482016-05-04 17:57:03 -070015FWL_Error CFX_Path::Create() {
Dan Sinclair811b8a42016-03-17 08:59:42 -040016 if (m_generator)
dsinclairc777f482016-05-04 17:57:03 -070017 return FWL_Error::PropertyInvalid;
Dan Sinclair811b8a42016-03-17 08:59:42 -040018
tsepeza9caab92016-12-14 05:57:10 -080019 m_generator = pdfium::MakeUnique<CFX_PathGenerator>();
dsinclairc777f482016-05-04 17:57:03 -070020 return FWL_Error::Succeeded;
Dan Sinclair811b8a42016-03-17 08:59:42 -040021}
22
weili16fccc52016-08-09 10:33:10 -070023CFX_Path::~CFX_Path() {}
Dan Sinclair811b8a42016-03-17 08:59:42 -040024
dsinclairc777f482016-05-04 17:57:03 -070025FWL_Error CFX_Path::MoveTo(FX_FLOAT x, FX_FLOAT y) {
Dan Sinclair811b8a42016-03-17 08:59:42 -040026 if (!m_generator)
dsinclairc777f482016-05-04 17:57:03 -070027 return FWL_Error::PropertyInvalid;
Dan Sinclair811b8a42016-03-17 08:59:42 -040028 m_generator->MoveTo(x, y);
dsinclairc777f482016-05-04 17:57:03 -070029 return FWL_Error::Succeeded;
Dan Sinclair811b8a42016-03-17 08:59:42 -040030}
31
dsinclairc777f482016-05-04 17:57:03 -070032FWL_Error CFX_Path::LineTo(FX_FLOAT x, FX_FLOAT y) {
Dan Sinclair811b8a42016-03-17 08:59:42 -040033 if (!m_generator)
dsinclairc777f482016-05-04 17:57:03 -070034 return FWL_Error::PropertyInvalid;
Dan Sinclair811b8a42016-03-17 08:59:42 -040035 m_generator->LineTo(x, y);
dsinclairc777f482016-05-04 17:57:03 -070036 return FWL_Error::Succeeded;
Dan Sinclair811b8a42016-03-17 08:59:42 -040037}
38
dsinclairc777f482016-05-04 17:57:03 -070039FWL_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 Sinclair811b8a42016-03-17 08:59:42 -040045 if (!m_generator)
dsinclairc777f482016-05-04 17:57:03 -070046 return FWL_Error::PropertyInvalid;
Dan Sinclair811b8a42016-03-17 08:59:42 -040047 m_generator->BezierTo(ctrlX1, ctrlY1, ctrlX2, ctrlY2, toX, toY);
dsinclairc777f482016-05-04 17:57:03 -070048 return FWL_Error::Succeeded;
Dan Sinclair811b8a42016-03-17 08:59:42 -040049}
50
dsinclairc777f482016-05-04 17:57:03 -070051FWL_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 Sinclair811b8a42016-03-17 08:59:42 -040057 if (!m_generator)
dsinclairc777f482016-05-04 17:57:03 -070058 return FWL_Error::PropertyInvalid;
Dan Sinclair811b8a42016-03-17 08:59:42 -040059 m_generator->ArcTo(left + width / 2, top + height / 2, width / 2, height / 2,
60 startAngle, sweepAngle);
dsinclairc777f482016-05-04 17:57:03 -070061 return FWL_Error::Succeeded;
Dan Sinclair811b8a42016-03-17 08:59:42 -040062}
63
dsinclairc777f482016-05-04 17:57:03 -070064FWL_Error CFX_Path::Close() {
Dan Sinclair811b8a42016-03-17 08:59:42 -040065 if (!m_generator)
dsinclairc777f482016-05-04 17:57:03 -070066 return FWL_Error::PropertyInvalid;
Dan Sinclair811b8a42016-03-17 08:59:42 -040067 m_generator->Close();
dsinclairc777f482016-05-04 17:57:03 -070068 return FWL_Error::Succeeded;
Dan Sinclair811b8a42016-03-17 08:59:42 -040069}
70
dsinclairc777f482016-05-04 17:57:03 -070071FWL_Error CFX_Path::AddLine(FX_FLOAT x1,
72 FX_FLOAT y1,
73 FX_FLOAT x2,
74 FX_FLOAT y2) {
Dan Sinclair811b8a42016-03-17 08:59:42 -040075 if (!m_generator)
dsinclairc777f482016-05-04 17:57:03 -070076 return FWL_Error::PropertyInvalid;
Dan Sinclair811b8a42016-03-17 08:59:42 -040077 m_generator->AddLine(x1, y1, x2, y2);
dsinclairc777f482016-05-04 17:57:03 -070078 return FWL_Error::Succeeded;
Dan Sinclair811b8a42016-03-17 08:59:42 -040079}
80
dsinclairc777f482016-05-04 17:57:03 -070081FWL_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 Sinclair811b8a42016-03-17 08:59:42 -040089 if (!m_generator)
dsinclairc777f482016-05-04 17:57:03 -070090 return FWL_Error::PropertyInvalid;
Dan Sinclair811b8a42016-03-17 08:59:42 -040091 m_generator->AddBezier(startX, startY, ctrlX1, ctrlY1, ctrlX2, ctrlY2, endX,
92 endY);
dsinclairc777f482016-05-04 17:57:03 -070093 return FWL_Error::Succeeded;
Dan Sinclair811b8a42016-03-17 08:59:42 -040094}
95
dsinclairc777f482016-05-04 17:57:03 -070096FWL_Error CFX_Path::AddRectangle(FX_FLOAT left,
97 FX_FLOAT top,
98 FX_FLOAT width,
99 FX_FLOAT height) {
Dan Sinclair811b8a42016-03-17 08:59:42 -0400100 if (!m_generator)
dsinclairc777f482016-05-04 17:57:03 -0700101 return FWL_Error::PropertyInvalid;
Dan Sinclair811b8a42016-03-17 08:59:42 -0400102 m_generator->AddRectangle(left, top, left + width, top + height);
dsinclairc777f482016-05-04 17:57:03 -0700103 return FWL_Error::Succeeded;
Dan Sinclair811b8a42016-03-17 08:59:42 -0400104}
105
dsinclairc777f482016-05-04 17:57:03 -0700106FWL_Error CFX_Path::AddEllipse(FX_FLOAT left,
107 FX_FLOAT top,
108 FX_FLOAT width,
109 FX_FLOAT height) {
Dan Sinclair811b8a42016-03-17 08:59:42 -0400110 if (!m_generator)
dsinclairc777f482016-05-04 17:57:03 -0700111 return FWL_Error::PropertyInvalid;
Dan Sinclair811b8a42016-03-17 08:59:42 -0400112 m_generator->AddEllipse(left + width / 2, top + height / 2, width / 2,
113 height / 2);
dsinclairc777f482016-05-04 17:57:03 -0700114 return FWL_Error::Succeeded;
Dan Sinclair811b8a42016-03-17 08:59:42 -0400115}
116
dsinclairc777f482016-05-04 17:57:03 -0700117FWL_Error CFX_Path::AddEllipse(const CFX_RectF& rect) {
Dan Sinclair811b8a42016-03-17 08:59:42 -0400118 if (!m_generator)
dsinclairc777f482016-05-04 17:57:03 -0700119 return FWL_Error::PropertyInvalid;
Dan Sinclair811b8a42016-03-17 08:59:42 -0400120 m_generator->AddEllipse(rect.left + rect.Width() / 2,
121 rect.top + rect.Height() / 2, rect.Width() / 2,
122 rect.Height() / 2);
dsinclairc777f482016-05-04 17:57:03 -0700123 return FWL_Error::Succeeded;
Dan Sinclair811b8a42016-03-17 08:59:42 -0400124}
125
dsinclairc777f482016-05-04 17:57:03 -0700126FWL_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 Sinclair811b8a42016-03-17 08:59:42 -0400132 if (!m_generator)
dsinclairc777f482016-05-04 17:57:03 -0700133 return FWL_Error::PropertyInvalid;
Dan Sinclair811b8a42016-03-17 08:59:42 -0400134 m_generator->AddArc(left + width / 2, top + height / 2, width / 2, height / 2,
135 startAngle, sweepAngle);
dsinclairc777f482016-05-04 17:57:03 -0700136 return FWL_Error::Succeeded;
Dan Sinclair811b8a42016-03-17 08:59:42 -0400137}
138
dsinclairc777f482016-05-04 17:57:03 -0700139FWL_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 Sinclair811b8a42016-03-17 08:59:42 -0400145 if (!m_generator)
dsinclairc777f482016-05-04 17:57:03 -0700146 return FWL_Error::PropertyInvalid;
Dan Sinclair811b8a42016-03-17 08:59:42 -0400147 m_generator->AddPie(left + width / 2, top + height / 2, width / 2, height / 2,
148 startAngle, sweepAngle);
dsinclairc777f482016-05-04 17:57:03 -0700149 return FWL_Error::Succeeded;
Dan Sinclair811b8a42016-03-17 08:59:42 -0400150}
151
dsinclairc777f482016-05-04 17:57:03 -0700152FWL_Error CFX_Path::AddSubpath(CFX_Path* path) {
Dan Sinclair811b8a42016-03-17 08:59:42 -0400153 if (!m_generator)
dsinclairc777f482016-05-04 17:57:03 -0700154 return FWL_Error::PropertyInvalid;
Dan Sinclair811b8a42016-03-17 08:59:42 -0400155 m_generator->AddPathData(path->GetPathData());
dsinclairc777f482016-05-04 17:57:03 -0700156 return FWL_Error::Succeeded;
Dan Sinclair811b8a42016-03-17 08:59:42 -0400157}
158
dsinclairc777f482016-05-04 17:57:03 -0700159FWL_Error CFX_Path::Clear() {
Dan Sinclair811b8a42016-03-17 08:59:42 -0400160 if (!m_generator)
dsinclairc777f482016-05-04 17:57:03 -0700161 return FWL_Error::PropertyInvalid;
Dan Sinclair811b8a42016-03-17 08:59:42 -0400162 m_generator->GetPathData()->SetPointCount(0);
dsinclairc777f482016-05-04 17:57:03 -0700163 return FWL_Error::Succeeded;
Dan Sinclair811b8a42016-03-17 08:59:42 -0400164}
165
tsepezd19e9122016-11-02 15:43:18 -0700166bool CFX_Path::IsEmpty() const {
Dan Sinclair811b8a42016-03-17 08:59:42 -0400167 if (!m_generator)
tsepezd19e9122016-11-02 15:43:18 -0700168 return false;
dsinclairc777f482016-05-04 17:57:03 -0700169 if (m_generator->GetPathData()->GetPointCount() == 0)
tsepezd19e9122016-11-02 15:43:18 -0700170 return true;
171 return false;
Dan Sinclair811b8a42016-03-17 08:59:42 -0400172}
173
weili16fccc52016-08-09 10:33:10 -0700174CFX_PathData* CFX_Path::GetPathData() const {
Dan Sinclair811b8a42016-03-17 08:59:42 -0400175 if (!m_generator)
176 return nullptr;
177 return m_generator->GetPathData();
178}