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