1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "draw/pen.h"
17 
18 #include "static_factory.h"
19 
20 namespace OHOS {
21 namespace Rosen {
22 namespace Drawing {
Pen()23 Pen::Pen() noexcept
24     : width_(0),
25       miterLimit_(-1),
26       join_(Pen::JoinStyle::MITER_JOIN),
27       cap_(Pen::CapStyle::FLAT_CAP),
28       pathEffect_(nullptr),
29       brush_()
30 {}
31 
Pen(const Color & c)32 Pen::Pen(const Color& c) noexcept : Pen()
33 {
34     brush_.SetColor(c);
35 }
36 
Pen(int rgba)37 Pen::Pen(int rgba) noexcept : Pen()
38 {
39     brush_.SetColor(rgba);
40 }
41 
GetColor() const42 Color Pen::GetColor() const
43 {
44     return brush_.GetColor();
45 }
46 
SetColor(const Color & c)47 void Pen::SetColor(const Color& c)
48 {
49     brush_.SetColor(c);
50 }
51 
SetColor(uint32_t c)52 void Pen::SetColor(uint32_t c)
53 {
54     brush_.SetColor(c);
55 }
56 
SetARGB(int a,int r,int g,int b)57 void Pen::SetARGB(int a, int r, int g, int b)
58 {
59     return brush_.SetARGB(a, r, g, b);
60 }
61 
GetColor4f()62 const Color4f& Pen::GetColor4f()
63 {
64     return brush_.GetColor4f();
65 }
66 
GetColorSpace() const67 const std::shared_ptr<ColorSpace> Pen::GetColorSpace() const
68 {
69     return brush_.GetColorSpace();
70 }
71 
GetColorSpacePtr() const72 const ColorSpace* Pen::GetColorSpacePtr() const
73 {
74     return brush_.GetColorSpacePtr();
75 }
76 
SetColor(const Color4f & cf,std::shared_ptr<ColorSpace> s)77 void Pen::SetColor(const Color4f& cf, std::shared_ptr<ColorSpace> s)
78 {
79     brush_.SetColor(cf, s);
80 }
81 
GetAlpha() const82 uint32_t Pen::GetAlpha() const
83 {
84     return brush_.GetAlpha();
85 }
86 
GetAlphaF() const87 scalar Pen::GetAlphaF() const
88 {
89     return brush_.GetAlphaF();
90 }
91 
SetAlpha(uint32_t a)92 void Pen::SetAlpha(uint32_t a)
93 {
94     return brush_.SetAlpha(a);
95 }
96 
SetAlphaF(scalar a)97 void Pen::SetAlphaF(scalar a)
98 {
99     return brush_.SetAlphaF(a);
100 }
101 
GetWidth() const102 scalar Pen::GetWidth() const
103 {
104     return width_;
105 }
106 
SetWidth(scalar width)107 void Pen::SetWidth(scalar width)
108 {
109     width_ = width;
110 }
111 
GetMiterLimit() const112 scalar Pen::GetMiterLimit() const
113 {
114     return miterLimit_;
115 }
116 
SetMiterLimit(scalar limit)117 void Pen::SetMiterLimit(scalar limit)
118 {
119     miterLimit_ = limit;
120 }
121 
GetCapStyle() const122 Pen::CapStyle Pen::GetCapStyle() const
123 {
124     return cap_;
125 }
126 
SetCapStyle(CapStyle cs)127 void Pen::SetCapStyle(CapStyle cs)
128 {
129     cap_ = cs;
130 }
131 
GetJoinStyle() const132 Pen::JoinStyle Pen::GetJoinStyle() const
133 {
134     return join_;
135 }
136 
SetJoinStyle(JoinStyle js)137 void Pen::SetJoinStyle(JoinStyle js)
138 {
139     join_ = js;
140 }
141 
GetBlendMode() const142 BlendMode Pen::GetBlendMode() const
143 {
144     return brush_.GetBlendMode();
145 }
146 
SetBlendMode(BlendMode mode)147 void Pen::SetBlendMode(BlendMode mode)
148 {
149     return brush_.SetBlendMode(mode);
150 }
151 
SetBlender(std::shared_ptr<Blender> blender)152 void Pen::SetBlender(std::shared_ptr<Blender> blender)
153 {
154     brush_.SetBlender(blender);
155 }
156 
SetBlenderEnabled(bool blenderEnabled)157 void Pen::SetBlenderEnabled(bool blenderEnabled)
158 {
159     blenderEnabled_ = blenderEnabled;
160 }
161 
GetBlender() const162 std::shared_ptr<Blender> Pen::GetBlender() const
163 {
164     return brush_.GetBlender();
165 }
166 
GetBlenderPtr() const167 const Blender* Pen::GetBlenderPtr() const
168 {
169     return brush_.GetBlenderPtr();
170 }
171 
IsAntiAlias() const172 bool Pen::IsAntiAlias() const
173 {
174     return brush_.IsAntiAlias();
175 }
176 
SetAntiAlias(bool aa)177 void Pen::SetAntiAlias(bool aa)
178 {
179     brush_.SetAntiAlias(aa);
180 }
181 
SetPathEffect(std::shared_ptr<PathEffect> e)182 void Pen::SetPathEffect(std::shared_ptr<PathEffect> e)
183 {
184     pathEffect_ = e;
185 }
186 
GetPathEffect() const187 std::shared_ptr<PathEffect> Pen::GetPathEffect() const
188 {
189     return pathEffect_;
190 }
191 
GetPathEffectPtr() const192 const PathEffect* Pen::GetPathEffectPtr() const
193 {
194     return pathEffect_.get();
195 }
196 
SetFilter(const Filter & filter)197 void Pen::SetFilter(const Filter& filter)
198 {
199     brush_.SetFilter(filter);
200 }
201 
GetFilter() const202 const Filter& Pen::GetFilter() const
203 {
204     return brush_.GetFilter();
205 }
206 
HasFilter() const207 bool Pen::HasFilter() const
208 {
209     return brush_.HasFilter();
210 }
211 
SetShaderEffect(std::shared_ptr<ShaderEffect> e)212 void Pen::SetShaderEffect(std::shared_ptr<ShaderEffect> e)
213 {
214     brush_.SetShaderEffect(e);
215 }
216 
GetShaderEffect() const217 std::shared_ptr<ShaderEffect> Pen::GetShaderEffect() const
218 {
219     return brush_.GetShaderEffect();
220 }
221 
GetShaderEffectPtr() const222 const ShaderEffect* Pen::GetShaderEffectPtr() const
223 {
224     return brush_.GetShaderEffectPtr();
225 }
226 
Reset()227 void Pen::Reset()
228 {
229     *this = Pen();
230 }
231 
SetLooper(std::shared_ptr<BlurDrawLooper> blurDrawLooper)232 void Pen::SetLooper(std::shared_ptr<BlurDrawLooper> blurDrawLooper)
233 {
234     brush_.SetLooper(blurDrawLooper);
235 }
236 
GetLooper() const237 std::shared_ptr<BlurDrawLooper> Pen::GetLooper() const
238 {
239     return brush_.GetLooper();
240 }
241 
GetFillPath(const Path & src,Path & dst,const Rect * rect,const Matrix & matrix)242 bool Pen::GetFillPath(const Path& src, Path& dst, const Rect* rect, const Matrix& matrix)
243 {
244     return StaticFactory::GetFillPath(*this, src, dst, rect, matrix);
245 }
246 
operator ==(const Pen & p1,const Pen & p2)247 bool operator==(const Pen& p1, const Pen& p2)
248 {
249     return p1.width_ == p2.width_ && p1.miterLimit_ == p2.miterLimit_ && p1.join_ == p2.join_ && p1.cap_ == p2.cap_ &&
250         p1.pathEffect_ == p2.pathEffect_ && p1.brush_ == p2.brush_;
251 }
252 
operator !=(const Pen & p1,const Pen & p2)253 bool operator!=(const Pen& p1, const Pen& p2)
254 {
255     return !(p1 == p2);
256 }
257 
Dump(std::string & out) const258 void Pen::Dump(std::string& out) const
259 {
260     out += "[brush";
261     brush_.Dump(out);
262     out += " width:" + std::to_string(width_) + " miterLimit:" + std::to_string(miterLimit_);
263     out += " joinStyle:" + std::to_string(static_cast<int>(join_));
264     out += " capStyle:" + std::to_string(static_cast<int>(cap_));
265     if (pathEffect_ != nullptr) {
266         out += " pathEffect:" + std::to_string(static_cast<int>(pathEffect_->GetType()));
267     }
268     out += " blenderEnabled:" + std::string(blenderEnabled_ ? "true" : "false");
269     out += ']';
270 }
271 } // namespace Drawing
272 } // namespace Rosen
273 } // namespace OHOS
274