blob: 3072df24752380764bfe86b054e82a615e6ff4d0 [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
dan sinclairb147e072017-02-22 19:56:15 -050025FWL_Error CFX_Path::MoveTo(const CFX_PointF& point) {
Dan Sinclair811b8a42016-03-17 08:59:42 -040026 if (!m_generator)
dsinclairc777f482016-05-04 17:57:03 -070027 return FWL_Error::PropertyInvalid;
dan sinclairb147e072017-02-22 19:56:15 -050028 m_generator->MoveTo(point);
dsinclairc777f482016-05-04 17:57:03 -070029 return FWL_Error::Succeeded;
Dan Sinclair811b8a42016-03-17 08:59:42 -040030}
31
dan sinclairb147e072017-02-22 19:56:15 -050032FWL_Error CFX_Path::LineTo(const CFX_PointF& point) {
Dan Sinclair811b8a42016-03-17 08:59:42 -040033 if (!m_generator)
dsinclairc777f482016-05-04 17:57:03 -070034 return FWL_Error::PropertyInvalid;
dan sinclairb147e072017-02-22 19:56:15 -050035 m_generator->LineTo(point);
dsinclairc777f482016-05-04 17:57:03 -070036 return FWL_Error::Succeeded;
Dan Sinclair811b8a42016-03-17 08:59:42 -040037}
38
dan sinclairb147e072017-02-22 19:56:15 -050039FWL_Error CFX_Path::BezierTo(const CFX_PointF& c1,
40 const CFX_PointF& c2,
41 const CFX_PointF& to) {
Dan Sinclair811b8a42016-03-17 08:59:42 -040042 if (!m_generator)
dsinclairc777f482016-05-04 17:57:03 -070043 return FWL_Error::PropertyInvalid;
dan sinclairb147e072017-02-22 19:56:15 -050044 m_generator->BezierTo(c1, c2, to);
dsinclairc777f482016-05-04 17:57:03 -070045 return FWL_Error::Succeeded;
Dan Sinclair811b8a42016-03-17 08:59:42 -040046}
47
dan sinclairb147e072017-02-22 19:56:15 -050048FWL_Error CFX_Path::ArcTo(const CFX_PointF& pos,
49 const CFX_SizeF& size,
dsinclairc777f482016-05-04 17:57:03 -070050 FX_FLOAT startAngle,
51 FX_FLOAT sweepAngle) {
Dan Sinclair811b8a42016-03-17 08:59:42 -040052 if (!m_generator)
dsinclairc777f482016-05-04 17:57:03 -070053 return FWL_Error::PropertyInvalid;
dan sinclairb147e072017-02-22 19:56:15 -050054 CFX_SizeF newSize = size / 2.0f;
55 m_generator->ArcTo(CFX_PointF(pos.x + newSize.width, pos.y + newSize.height),
56 newSize, startAngle, sweepAngle);
dsinclairc777f482016-05-04 17:57:03 -070057 return FWL_Error::Succeeded;
Dan Sinclair811b8a42016-03-17 08:59:42 -040058}
59
dsinclairc777f482016-05-04 17:57:03 -070060FWL_Error CFX_Path::Close() {
Dan Sinclair811b8a42016-03-17 08:59:42 -040061 if (!m_generator)
dsinclairc777f482016-05-04 17:57:03 -070062 return FWL_Error::PropertyInvalid;
Dan Sinclair811b8a42016-03-17 08:59:42 -040063 m_generator->Close();
dsinclairc777f482016-05-04 17:57:03 -070064 return FWL_Error::Succeeded;
Dan Sinclair811b8a42016-03-17 08:59:42 -040065}
66
dan sinclairb147e072017-02-22 19:56:15 -050067FWL_Error CFX_Path::AddLine(const CFX_PointF& p1, const CFX_PointF& p2) {
Dan Sinclair811b8a42016-03-17 08:59:42 -040068 if (!m_generator)
dsinclairc777f482016-05-04 17:57:03 -070069 return FWL_Error::PropertyInvalid;
dan sinclairb147e072017-02-22 19:56:15 -050070 m_generator->AddLine(p1, p2);
dsinclairc777f482016-05-04 17:57:03 -070071 return FWL_Error::Succeeded;
Dan Sinclair811b8a42016-03-17 08:59:42 -040072}
73
dan sinclairb147e072017-02-22 19:56:15 -050074FWL_Error CFX_Path::AddBezier(const CFX_PointF& p1,
75 const CFX_PointF& c1,
76 const CFX_PointF& c2,
77 const CFX_PointF& p2) {
Dan Sinclair811b8a42016-03-17 08:59:42 -040078 if (!m_generator)
dsinclairc777f482016-05-04 17:57:03 -070079 return FWL_Error::PropertyInvalid;
dan sinclairb147e072017-02-22 19:56:15 -050080 m_generator->AddBezier(p1, c1, c2, p2);
dsinclairc777f482016-05-04 17:57:03 -070081 return FWL_Error::Succeeded;
Dan Sinclair811b8a42016-03-17 08:59:42 -040082}
83
dsinclairc777f482016-05-04 17:57:03 -070084FWL_Error CFX_Path::AddRectangle(FX_FLOAT left,
85 FX_FLOAT top,
86 FX_FLOAT width,
87 FX_FLOAT height) {
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->AddRectangle(left, top, left + width, top + height);
dsinclairc777f482016-05-04 17:57:03 -070091 return FWL_Error::Succeeded;
Dan Sinclair811b8a42016-03-17 08:59:42 -040092}
93
dan sinclairb147e072017-02-22 19:56:15 -050094FWL_Error CFX_Path::AddEllipse(const CFX_PointF& pos, const CFX_SizeF& size) {
Dan Sinclair811b8a42016-03-17 08:59:42 -040095 if (!m_generator)
dsinclairc777f482016-05-04 17:57:03 -070096 return FWL_Error::PropertyInvalid;
dan sinclairb147e072017-02-22 19:56:15 -050097 CFX_SizeF newSize = size / 2.0f;
98 m_generator->AddEllipse(
99 CFX_PointF(pos.x + newSize.width, pos.y + newSize.height), newSize);
dsinclairc777f482016-05-04 17:57:03 -0700100 return FWL_Error::Succeeded;
Dan Sinclair811b8a42016-03-17 08:59:42 -0400101}
102
dsinclairc777f482016-05-04 17:57:03 -0700103FWL_Error CFX_Path::AddEllipse(const CFX_RectF& rect) {
Dan Sinclair811b8a42016-03-17 08:59:42 -0400104 if (!m_generator)
dsinclairc777f482016-05-04 17:57:03 -0700105 return FWL_Error::PropertyInvalid;
dan sinclairb147e072017-02-22 19:56:15 -0500106 m_generator->AddEllipse(
107 CFX_PointF(rect.left + rect.Width() / 2, rect.top + rect.Height() / 2),
108 CFX_SizeF(rect.Width() / 2, rect.Height() / 2));
dsinclairc777f482016-05-04 17:57:03 -0700109 return FWL_Error::Succeeded;
Dan Sinclair811b8a42016-03-17 08:59:42 -0400110}
111
dan sinclairb147e072017-02-22 19:56:15 -0500112FWL_Error CFX_Path::AddArc(const CFX_PointF& pos,
113 const CFX_SizeF& size,
dsinclairc777f482016-05-04 17:57:03 -0700114 FX_FLOAT startAngle,
115 FX_FLOAT sweepAngle) {
Dan Sinclair811b8a42016-03-17 08:59:42 -0400116 if (!m_generator)
dsinclairc777f482016-05-04 17:57:03 -0700117 return FWL_Error::PropertyInvalid;
dan sinclairb147e072017-02-22 19:56:15 -0500118 CFX_SizeF newSize = size / 2;
119 m_generator->AddArc(CFX_PointF(pos.x + newSize.width, pos.y + newSize.height),
120 newSize, startAngle, sweepAngle);
dsinclairc777f482016-05-04 17:57:03 -0700121 return FWL_Error::Succeeded;
Dan Sinclair811b8a42016-03-17 08:59:42 -0400122}
123
dan sinclairb147e072017-02-22 19:56:15 -0500124FWL_Error CFX_Path::AddPie(const CFX_PointF& pos,
125 const CFX_SizeF& size,
dsinclairc777f482016-05-04 17:57:03 -0700126 FX_FLOAT startAngle,
127 FX_FLOAT sweepAngle) {
Dan Sinclair811b8a42016-03-17 08:59:42 -0400128 if (!m_generator)
dsinclairc777f482016-05-04 17:57:03 -0700129 return FWL_Error::PropertyInvalid;
dan sinclairb147e072017-02-22 19:56:15 -0500130 CFX_SizeF newSize = size / 2;
131 m_generator->AddPie(CFX_PointF(pos.x + newSize.width, pos.y + newSize.height),
132 newSize, startAngle, sweepAngle);
dsinclairc777f482016-05-04 17:57:03 -0700133 return FWL_Error::Succeeded;
Dan Sinclair811b8a42016-03-17 08:59:42 -0400134}
135
dsinclairc777f482016-05-04 17:57:03 -0700136FWL_Error CFX_Path::AddSubpath(CFX_Path* path) {
Dan Sinclair811b8a42016-03-17 08:59:42 -0400137 if (!m_generator)
dsinclairc777f482016-05-04 17:57:03 -0700138 return FWL_Error::PropertyInvalid;
Dan Sinclair811b8a42016-03-17 08:59:42 -0400139 m_generator->AddPathData(path->GetPathData());
dsinclairc777f482016-05-04 17:57:03 -0700140 return FWL_Error::Succeeded;
Dan Sinclair811b8a42016-03-17 08:59:42 -0400141}
142
dsinclairc777f482016-05-04 17:57:03 -0700143FWL_Error CFX_Path::Clear() {
Dan Sinclair811b8a42016-03-17 08:59:42 -0400144 if (!m_generator)
dsinclairc777f482016-05-04 17:57:03 -0700145 return FWL_Error::PropertyInvalid;
Dan Sinclaire4602322017-02-15 11:07:32 -0500146 m_generator->GetPathData()->Clear();
dsinclairc777f482016-05-04 17:57:03 -0700147 return FWL_Error::Succeeded;
Dan Sinclair811b8a42016-03-17 08:59:42 -0400148}
149
tsepezd19e9122016-11-02 15:43:18 -0700150bool CFX_Path::IsEmpty() const {
Dan Sinclair811b8a42016-03-17 08:59:42 -0400151 if (!m_generator)
tsepezd19e9122016-11-02 15:43:18 -0700152 return false;
Dan Sinclaire4602322017-02-15 11:07:32 -0500153 if (m_generator->GetPathData()->GetPoints().empty())
tsepezd19e9122016-11-02 15:43:18 -0700154 return true;
155 return false;
Dan Sinclair811b8a42016-03-17 08:59:42 -0400156}
157
weili16fccc52016-08-09 10:33:10 -0700158CFX_PathData* CFX_Path::GetPathData() const {
Dan Sinclair811b8a42016-03-17 08:59:42 -0400159 if (!m_generator)
160 return nullptr;
161 return m_generator->GetPathData();
162}