1 /*
2  * Copyright (c) 2023 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/paint.h"
17 
18 namespace OHOS {
19 namespace Rosen {
20 namespace Drawing {
Paint()21 Paint::Paint() noexcept
22     : filter_() {}
23 
Paint(const Paint & other)24 Paint::Paint(const Paint& other) noexcept
25 {
26     antiAlias_ = other.antiAlias_;
27     color_ = other.color_;
28     blendMode_ = other.blendMode_;
29     style_ = other.style_;
30     width_ = other.width_;
31     miterLimit_ = other.miterLimit_;
32     cap_ = other.cap_;
33     join_ = other.join_;
34     if (other.hasFilter_) {
35         filter_ = other.filter_;
36         hasFilter_ = true;
37     } else {
38         filter_.Reset();
39         hasFilter_ = false;
40     }
41     colorSpace_ = other.colorSpace_;
42     shaderEffect_ = other.shaderEffect_;
43     pathEffect_ = other.pathEffect_;
44     blender_ = other.blender_;
45     blenderEnabled_ = other.blenderEnabled_;
46     blurDrawLooper_ = other.blurDrawLooper_;
47 }
48 
Paint(const Color & c,std::shared_ptr<ColorSpace> colorSpace)49 Paint::Paint(const Color& c, std::shared_ptr<ColorSpace> colorSpace) noexcept
50     : color_(c), filter_(), colorSpace_(colorSpace) {}
51 
operator =(const Paint & other)52 Paint& Paint::operator=(const Paint& other)
53 {
54     antiAlias_ = other.antiAlias_;
55     color_ = other.color_;
56     blendMode_ = other.blendMode_;
57     style_ = other.style_;
58     width_ = other.width_;
59     miterLimit_ = other.miterLimit_;
60     cap_ = other.cap_;
61     join_ = other.join_;
62     if (other.hasFilter_) {
63         filter_ = other.filter_;
64         hasFilter_ = true;
65     } else {
66         filter_.Reset();
67         hasFilter_ = false;
68     }
69     colorSpace_ = other.colorSpace_;
70     shaderEffect_ = other.shaderEffect_;
71     pathEffect_ = other.pathEffect_;
72     blender_ = other.blender_;
73     blenderEnabled_ = other.blenderEnabled_;
74     blurDrawLooper_ = other.blurDrawLooper_;
75     return *this;
76 }
77 
CanCombinePaint(const Paint & pen,const Paint & brush)78 bool Paint::CanCombinePaint(const Paint& pen, const Paint& brush)
79 {
80     return pen.antiAlias_ == brush.antiAlias_ &&
81         pen.color_ == brush.color_ &&
82         pen.blendMode_ == brush.blendMode_ &&
83         pen.hasFilter_ == brush.hasFilter_ &&
84         pen.filter_ == brush.filter_ &&
85         pen.colorSpace_ == brush.colorSpace_ &&
86         pen.shaderEffect_ == brush.shaderEffect_ &&
87         pen.blender_ == brush.blender_ &&
88         pen.blenderEnabled_ == brush.blenderEnabled_ &&
89         pen.blurDrawLooper_ == brush.blurDrawLooper_;
90 }
91 
AttachBrush(const Brush & brush)92 void Paint::AttachBrush(const Brush& brush)
93 {
94     antiAlias_ = brush.IsAntiAlias();
95     color_ = brush.GetColor();
96     blendMode_ = brush.GetBlendMode();
97     style_ = PaintStyle::PAINT_FILL;
98     if (brush.HasFilter()) {
99         filter_ = brush.GetFilter();
100         hasFilter_ = true;
101     } else {
102         filter_.Reset();
103         hasFilter_ = false;
104     }
105     colorSpace_ = brush.GetColorSpace();
106     shaderEffect_ = brush.GetShaderEffect();
107     blender_ = brush.GetBlender();
108     blenderEnabled_ = brush.GetBlenderEnabled();
109     blurDrawLooper_ = brush.GetLooper();
110 }
111 
AttachPen(const Pen & pen)112 void Paint::AttachPen(const Pen& pen)
113 {
114     antiAlias_ = pen.IsAntiAlias();
115     color_ = pen.GetColor();
116     blendMode_ = pen.GetBlendMode();
117     style_ = PaintStyle::PAINT_STROKE;
118     width_ = pen.GetWidth();
119     miterLimit_ = pen.GetMiterLimit();
120     cap_ = pen.GetCapStyle();
121     join_ = pen.GetJoinStyle();
122     if (pen.HasFilter()) {
123         filter_ = pen.GetFilter();
124         hasFilter_ = true;
125     } else {
126         filter_.Reset();
127         hasFilter_ = false;
128     }
129     colorSpace_ = pen.GetColorSpace();
130     shaderEffect_ = pen.GetShaderEffect();
131     pathEffect_ = pen.GetPathEffect();
132     blender_ = pen.GetBlender();
133     blenderEnabled_ = pen.GetBlenderEnabled();
134     blurDrawLooper_ = pen.GetLooper();
135 }
136 
SetStyle(const PaintStyle & style)137 void Paint::SetStyle(const PaintStyle& style)
138 {
139     style_ = style;
140 }
141 
HasStrokeStyle() const142 bool Paint::HasStrokeStyle() const
143 {
144     return style_ == PaintStyle::PAINT_FILL_STROKE || style_ == PaintStyle::PAINT_STROKE;
145 }
146 
SetColor(const Color & c)147 void Paint::SetColor(const Color& c)
148 {
149     color_ = c;
150 }
151 
SetARGB(int a,int r,int g,int b)152 void Paint::SetARGB(int a, int r, int g, int b)
153 {
154     color_.SetRgb(r, g, b, a);
155 }
156 
SetColor(const Color4f & cf,std::shared_ptr<ColorSpace> s)157 void Paint::SetColor(const Color4f& cf, std::shared_ptr<ColorSpace> s)
158 {
159     color_.SetRgbF(cf.redF_, cf.greenF_, cf.blueF_, cf.alphaF_);
160     colorSpace_ = s;
161 }
162 
SetAlpha(uint32_t a)163 void Paint::SetAlpha(uint32_t a)
164 {
165     color_.SetAlpha(a);
166 }
167 
SetAlphaF(scalar a)168 void Paint::SetAlphaF(scalar a)
169 {
170     color_.SetAlphaF(a);
171 }
172 
SetWidth(scalar width)173 void Paint::SetWidth(scalar width)
174 {
175     width_ = width;
176 }
177 
SetMiterLimit(scalar limit)178 void Paint::SetMiterLimit(scalar limit)
179 {
180     miterLimit_ = limit;
181 }
182 
SetCapStyle(Pen::CapStyle cs)183 void Paint::SetCapStyle(Pen::CapStyle cs)
184 {
185     cap_ = cs;
186 }
187 
SetJoinStyle(Pen::JoinStyle js)188 void Paint::SetJoinStyle(Pen::JoinStyle js)
189 {
190     join_ = js;
191 }
192 
SetBlendMode(BlendMode mode)193 void Paint::SetBlendMode(BlendMode mode)
194 {
195     blendMode_ = mode;
196 }
197 
SetFilter(const Filter & filter)198 void Paint::SetFilter(const Filter& filter)
199 {
200     filter_ = filter;
201     hasFilter_ = true;
202 }
203 
SetShaderEffect(std::shared_ptr<ShaderEffect> e)204 void Paint::SetShaderEffect(std::shared_ptr<ShaderEffect> e)
205 {
206     shaderEffect_ = e;
207 }
208 
SetPathEffect(std::shared_ptr<PathEffect> e)209 void Paint::SetPathEffect(std::shared_ptr<PathEffect> e)
210 {
211     pathEffect_ = e;
212 }
213 
SetBlender(std::shared_ptr<Blender> blender)214 void Paint::SetBlender(std::shared_ptr<Blender> blender)
215 {
216     blender_ = blender;
217 }
218 
SetBlenderEnabled(bool blenderEnabled)219 void Paint::SetBlenderEnabled(bool blenderEnabled)
220 {
221     blenderEnabled_ = blenderEnabled;
222 }
223 
SetLooper(std::shared_ptr<BlurDrawLooper> blurDrawLooper)224 void Paint::SetLooper(std::shared_ptr<BlurDrawLooper> blurDrawLooper)
225 {
226     blurDrawLooper_ = blurDrawLooper;
227 }
228 
GetLooper() const229 std::shared_ptr<BlurDrawLooper> Paint::GetLooper() const
230 {
231     return blurDrawLooper_;
232 }
233 
SetAntiAlias(bool aa)234 void Paint::SetAntiAlias(bool aa)
235 {
236     antiAlias_ = aa;
237 }
238 
Reset()239 void Paint::Reset()
240 {
241     antiAlias_ = false;
242     color_ = Color::COLOR_BLACK;
243     blendMode_ = BlendMode::SRC_OVER;
244     style_ = PaintStyle::PAINT_NONE;
245     width_ = 0;
246     miterLimit_ = DEFAULT_MITER_VAL;
247     join_ = Pen::JoinStyle::DEFAULT_JOIN;
248     cap_ = Pen::CapStyle::DEFAULT_CAP;
249 
250     hasFilter_ = false;
251     filter_.Reset();
252 
253     colorSpace_ = nullptr;
254     shaderEffect_ = nullptr;
255     pathEffect_ = nullptr;
256     blurDrawLooper_ = nullptr;
257 }
258 
Disable()259 void Paint::Disable()
260 {
261     style_ = PaintStyle::PAINT_NONE;
262     hasFilter_ = false;
263 }
264 
operator ==(const Paint & p1,const Paint & p2)265 bool operator==(const Paint& p1, const Paint& p2)
266 {
267     return p1.antiAlias_ == p2.antiAlias_ &&
268         p1.color_ == p2.color_ &&
269         p1.blendMode_ == p2.blendMode_ &&
270         p1.style_ == p2.style_ &&
271         IsScalarAlmostEqual(p1.width_, p2.width_) &&
272         IsScalarAlmostEqual(p1.miterLimit_, p2.miterLimit_) &&
273         p1.join_ == p2.join_ &&
274         p1.cap_ == p2.cap_ &&
275         p1.filter_ == p2.filter_ &&
276         p1.colorSpace_ == p2.colorSpace_ &&
277         p1.shaderEffect_ == p2.shaderEffect_ &&
278         p1.pathEffect_ == p2.pathEffect_ &&
279         p1.blender_ == p2.blender_ &&
280         p1.blenderEnabled_ == p2.blenderEnabled_ &&
281         p1.blurDrawLooper_ == p2.blurDrawLooper_;
282 }
283 
operator !=(const Paint & p1,const Paint & p2)284 bool operator!=(const Paint& p1, const Paint& p2)
285 {
286     return p1.antiAlias_ != p2.antiAlias_ ||
287         p1.color_ != p2.color_ ||
288         p1.blendMode_ != p2.blendMode_ ||
289         p1.style_ != p2.style_ ||
290         !IsScalarAlmostEqual(p1.width_, p2.width_) ||
291         !IsScalarAlmostEqual(p1.miterLimit_, p2.miterLimit_) ||
292         p1.join_ != p2.join_ ||
293         p1.cap_ != p2.cap_ ||
294         p1.filter_ != p2.filter_ ||
295         p1.colorSpace_ != p2.colorSpace_ ||
296         p1.shaderEffect_ != p2.shaderEffect_ ||
297         p1.pathEffect_ != p2.pathEffect_ ||
298         p1.blender_ != p2.blender_ ||
299         p1.blenderEnabled_ != p2.blenderEnabled_ ||
300         p1.blurDrawLooper_ != p2.blurDrawLooper_;
301 }
302 } // namespace Drawing
303 } // namespace Rosen
304 } // namespace OHOS