1 /*
2 * Copyright (c) 2021-2025 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 "config/DrawingConfig.h"
19 #include "static_factory.h"
20
21 namespace OHOS {
22 namespace Rosen {
23 namespace Drawing {
Pen()24 Pen::Pen() noexcept
25 : width_(0),
26 miterLimit_(-1),
27 join_(Pen::JoinStyle::MITER_JOIN),
28 cap_(Pen::CapStyle::FLAT_CAP),
29 pathEffect_(nullptr),
30 brush_()
31 {}
32
Pen(const Color & c)33 Pen::Pen(const Color& c) noexcept : Pen()
34 {
35 brush_.SetColor(c);
36 }
37
Pen(int rgba)38 Pen::Pen(int rgba) noexcept : Pen()
39 {
40 brush_.SetColor(rgba);
41 }
42
GetColor() const43 Color Pen::GetColor() const
44 {
45 return brush_.GetColor();
46 }
47
SetColor(const Color & c)48 void Pen::SetColor(const Color& c)
49 {
50 brush_.SetColor(c);
51 }
52
SetColor(uint32_t c)53 void Pen::SetColor(uint32_t c)
54 {
55 brush_.SetColor(c);
56 }
57
SetARGB(int a,int r,int g,int b)58 void Pen::SetARGB(int a, int r, int g, int b)
59 {
60 return brush_.SetARGB(a, r, g, b);
61 }
62
GetColor4f()63 const Color4f& Pen::GetColor4f()
64 {
65 return brush_.GetColor4f();
66 }
67
GetColorSpace() const68 const std::shared_ptr<ColorSpace> Pen::GetColorSpace() const
69 {
70 return brush_.GetColorSpace();
71 }
72
GetColorSpacePtr() const73 const ColorSpace* Pen::GetColorSpacePtr() const
74 {
75 return brush_.GetColorSpacePtr();
76 }
77
SetColor(const Color4f & cf,std::shared_ptr<ColorSpace> s)78 void Pen::SetColor(const Color4f& cf, std::shared_ptr<ColorSpace> s)
79 {
80 brush_.SetColor(cf, s);
81 }
82
GetAlpha() const83 uint32_t Pen::GetAlpha() const
84 {
85 return brush_.GetAlpha();
86 }
87
GetAlphaF() const88 scalar Pen::GetAlphaF() const
89 {
90 return brush_.GetAlphaF();
91 }
92
GetRedF() const93 scalar Pen::GetRedF() const
94 {
95 return brush_.GetRedF();
96 }
97
GetGreenF() const98 scalar Pen::GetGreenF() const
99 {
100 return brush_.GetGreenF();
101 }
102
GetBlueF() const103 scalar Pen::GetBlueF() const
104 {
105 return brush_.GetBlueF();
106 }
107
SetAlpha(uint32_t a)108 void Pen::SetAlpha(uint32_t a)
109 {
110 return brush_.SetAlpha(a);
111 }
112
SetAlphaF(scalar a)113 void Pen::SetAlphaF(scalar a)
114 {
115 return brush_.SetAlphaF(a);
116 }
117
GetWidth() const118 scalar Pen::GetWidth() const
119 {
120 return width_;
121 }
122
SetWidth(scalar width)123 void Pen::SetWidth(scalar width)
124 {
125 width_ = width;
126 }
127
GetMiterLimit() const128 scalar Pen::GetMiterLimit() const
129 {
130 return miterLimit_;
131 }
132
SetMiterLimit(scalar limit)133 void Pen::SetMiterLimit(scalar limit)
134 {
135 miterLimit_ = limit;
136 }
137
GetCapStyle() const138 Pen::CapStyle Pen::GetCapStyle() const
139 {
140 return cap_;
141 }
142
SetCapStyle(CapStyle cs)143 void Pen::SetCapStyle(CapStyle cs)
144 {
145 cap_ = cs;
146 }
147
GetJoinStyle() const148 Pen::JoinStyle Pen::GetJoinStyle() const
149 {
150 return join_;
151 }
152
SetJoinStyle(JoinStyle js)153 void Pen::SetJoinStyle(JoinStyle js)
154 {
155 join_ = js;
156 }
157
GetBlendMode() const158 BlendMode Pen::GetBlendMode() const
159 {
160 return brush_.GetBlendMode();
161 }
162
SetBlendMode(BlendMode mode)163 void Pen::SetBlendMode(BlendMode mode)
164 {
165 return brush_.SetBlendMode(mode);
166 }
167
SetBlender(std::shared_ptr<Blender> blender)168 void Pen::SetBlender(std::shared_ptr<Blender> blender)
169 {
170 brush_.SetBlender(blender);
171 }
172
SetBlenderEnabled(bool blenderEnabled)173 void Pen::SetBlenderEnabled(bool blenderEnabled)
174 {
175 blenderEnabled_ = blenderEnabled;
176 }
177
GetBlender() const178 std::shared_ptr<Blender> Pen::GetBlender() const
179 {
180 return brush_.GetBlender();
181 }
182
GetBlenderPtr() const183 const Blender* Pen::GetBlenderPtr() const
184 {
185 return brush_.GetBlenderPtr();
186 }
187
IsAntiAlias() const188 bool Pen::IsAntiAlias() const
189 {
190 return brush_.IsAntiAlias();
191 }
192
SetAntiAlias(bool aa)193 void Pen::SetAntiAlias(bool aa)
194 {
195 brush_.SetAntiAlias(aa);
196 }
197
SetPathEffect(std::shared_ptr<PathEffect> e)198 void Pen::SetPathEffect(std::shared_ptr<PathEffect> e)
199 {
200 #ifdef DRAWING_DISABLE_API
201 if (DrawingConfig::IsDisabled(DrawingConfig::DrawingDisableFlag::DISABLE_PATH_EFFECT)) {
202 pathEffect_ = nullptr;
203 return;
204 }
205 #endif
206 pathEffect_ = e;
207 }
208
GetPathEffect() const209 std::shared_ptr<PathEffect> Pen::GetPathEffect() const
210 {
211 return pathEffect_;
212 }
213
GetPathEffectPtr() const214 const PathEffect* Pen::GetPathEffectPtr() const
215 {
216 return pathEffect_.get();
217 }
218
SetFilter(const Filter & filter)219 void Pen::SetFilter(const Filter& filter)
220 {
221 brush_.SetFilter(filter);
222 }
223
GetFilter() const224 const Filter& Pen::GetFilter() const
225 {
226 return brush_.GetFilter();
227 }
228
HasFilter() const229 bool Pen::HasFilter() const
230 {
231 return brush_.HasFilter();
232 }
233
SetShaderEffect(std::shared_ptr<ShaderEffect> e)234 void Pen::SetShaderEffect(std::shared_ptr<ShaderEffect> e)
235 {
236 brush_.SetShaderEffect(e);
237 }
238
GetShaderEffect() const239 std::shared_ptr<ShaderEffect> Pen::GetShaderEffect() const
240 {
241 return brush_.GetShaderEffect();
242 }
243
GetShaderEffectPtr() const244 const ShaderEffect* Pen::GetShaderEffectPtr() const
245 {
246 return brush_.GetShaderEffectPtr();
247 }
248
Reset()249 void Pen::Reset()
250 {
251 *this = Pen();
252 }
253
SetLooper(std::shared_ptr<BlurDrawLooper> blurDrawLooper)254 void Pen::SetLooper(std::shared_ptr<BlurDrawLooper> blurDrawLooper)
255 {
256 brush_.SetLooper(blurDrawLooper);
257 }
258
GetLooper() const259 std::shared_ptr<BlurDrawLooper> Pen::GetLooper() const
260 {
261 return brush_.GetLooper();
262 }
263
GetFillPath(const Path & src,Path & dst,const Rect * rect,const Matrix & matrix)264 bool Pen::GetFillPath(const Path& src, Path& dst, const Rect* rect, const Matrix& matrix)
265 {
266 return StaticFactory::GetFillPath(*this, src, dst, rect, matrix);
267 }
268
operator ==(const Pen & p1,const Pen & p2)269 bool operator==(const Pen& p1, const Pen& p2)
270 {
271 return p1.width_ == p2.width_ && p1.miterLimit_ == p2.miterLimit_ && p1.join_ == p2.join_ && p1.cap_ == p2.cap_ &&
272 p1.pathEffect_ == p2.pathEffect_ && p1.brush_ == p2.brush_;
273 }
274
operator !=(const Pen & p1,const Pen & p2)275 bool operator!=(const Pen& p1, const Pen& p2)
276 {
277 return !(p1 == p2);
278 }
279
Dump(std::string & out) const280 void Pen::Dump(std::string& out) const
281 {
282 out += "[brush";
283 brush_.Dump(out);
284 out += " width:" + std::to_string(width_) + " miterLimit:" + std::to_string(miterLimit_);
285 out += " joinStyle:" + std::to_string(static_cast<int>(join_));
286 out += " capStyle:" + std::to_string(static_cast<int>(cap_));
287 if (pathEffect_ != nullptr) {
288 out += " pathEffect:" + std::to_string(static_cast<int>(pathEffect_->GetType()));
289 }
290 out += " blenderEnabled:" + std::string(blenderEnabled_ ? "true" : "false");
291 out += ']';
292 }
293 } // namespace Drawing
294 } // namespace Rosen
295 } // namespace OHOS
296