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 #ifndef PEN_H
17 #define PEN_H
18 
19 #include "draw/brush.h"
20 #include "draw/color.h"
21 #include "effect/blur_draw_looper.h"
22 #include "effect/filter.h"
23 #include "effect/path_effect.h"
24 #include "utils/rect.h"
25 
26 namespace OHOS {
27 namespace Rosen {
28 namespace Drawing {
29 class DRAWING_API Pen {
30 public:
31     enum class JoinStyle {
32         MITER_JOIN,
33         ROUND_JOIN,
34         BEVEL_JOIN,
35         DEFAULT_JOIN = MITER_JOIN
36     };
37 
38     enum class CapStyle {
39         FLAT_CAP,
40         ROUND_CAP,
41         SQUARE_CAP,
42         DEFAULT_CAP = FLAT_CAP
43     };
44 
45     Pen() noexcept;
46     Pen(const Pen& p) noexcept = default;
47     Pen(const Color& c) noexcept;
48     Pen(int rgba) noexcept;
~Pen()49     ~Pen() {};
50 
51     /**
52      * @brief Get the Color of pen
53      *
54      * @return Color of pen
55      */
56     Color GetColor() const;
57 
58     /**
59      * @brief Set the Color of pen
60      *
61      * @param c color object to set
62      */
63     void SetColor(const Color& c);
64 
65     /**
66      * @brief Set the Color object
67      *
68      * @param c color uint32_t value to set
69      */
70     void SetColor(uint32_t c);
71 
72     /**
73      * @brief set ARGB of pen
74      *
75      * @param a alpha
76      * @param r red
77      * @param g green
78      * @param b blue
79      */
80     void SetARGB(int a, int r, int g, int b);
81 
82     /**
83      * @brief Get the Color 4f of pen
84      *
85      * @return Color4f
86      */
87     const Color4f& GetColor4f();
88 
89     /**
90      * @brief Get the Color Space object
91      *
92      * @return a shared pointer ColorSpace
93      */
94     const std::shared_ptr<ColorSpace> GetColorSpace() const;
95 
96     /**
97      * @brief Get a Color Space pointer
98      *
99      * @return a pointer ColorSpace
100      */
101     const ColorSpace* GetColorSpacePtr() const;
102 
103     /**
104      * @brief Set the Color and ColorSpace of pen
105      *
106      * @param cf color4f object
107      * @param s  a shared pointer to colorspace
108      */
109     void SetColor(const Color4f& cf, std::shared_ptr<ColorSpace> s);
110 
111     /**
112      * @brief Get the Alpha value of pen
113      *
114      * @return uint32_t of alpha
115      */
116     uint32_t GetAlpha() const;
117 
118     /**
119      * @brief Get the Alpha F object of pen
120      *
121      * @return value of alpha f
122      */
123     scalar GetAlphaF() const;
124 
125     /**
126      * @brief Set the Alpha value of pen
127      *
128      * @param a the alpha value to set
129      */
130     void SetAlpha(uint32_t a);
131 
132     /**
133      * @brief Set the Alpha F value of pen
134      *
135      * @param a the alpha f value
136      */
137     void SetAlphaF(scalar a);
138 
139     /**
140      * @brief Get the Width value of pen
141      *
142      * @return width value of pen
143      */
144     scalar GetWidth() const;
145 
146     /**
147      * @brief Set the Width value of pen
148      *
149      * @param width the value of width
150      */
151     void SetWidth(scalar width);
152 
153     /**
154      * @brief Get the Miter Limit of pen
155      *
156      * @return the value of miter limit
157      */
158     scalar GetMiterLimit() const;
159 
160     /**
161      * @brief Set the Miter Limit of pen
162      *
163      * @param limit the value of miter limit to set
164      */
165     void SetMiterLimit(scalar limit);
166 
167     /**
168      * @brief Get the Cap Style of pen
169      *
170      * @return CapStyle of pen
171      */
172     CapStyle GetCapStyle() const;
173 
174     /**
175      * @brief Set the Cap Style of pen
176      *
177      * @param cs the cap style object
178      */
179     void SetCapStyle(CapStyle cs);
180 
181     /**
182      * @brief Get the Join Style of pen
183      *
184      * @return JoinStyle of pen
185      */
186     JoinStyle GetJoinStyle() const;
187 
188     /**
189      * @brief Sets the geometry drawn at the corners of strokes.
190      *
191      * @param js join style object
192      */
193     void SetJoinStyle(JoinStyle js);
194 
195     /**
196      * @brief Get the Blend Mode of pen
197      *
198      * @return the value of BlendMode
199      */
200     BlendMode GetBlendMode() const;
201 
202     /**
203      * @brief Set the Blend Mode of pen
204      *
205      * @param mode the value of pen's blend mode
206      */
207     void SetBlendMode(BlendMode mode);
208 
209     /**
210      * @brief Sets the current blender, increasing its refcnt, and if a blender is already
211      *        present, decreasing that object's refcnt.
212      * @param blender  Blender used to set
213      */
214     void SetBlender(std::shared_ptr<Blender> blender);
215 
216     /**
217      * @brief Returns the user-supplied blend function, if one has been set.
218      * @return the Blender assigned to this Brush, otherwise nullptr
219      */
220     std::shared_ptr<Blender> GetBlender() const;
221 
222     /**
223      * @brief Sets the blenderEnabled flag, which determines whether the blender is used.
224      * @param blenderEnabled  whether the blender is used.
225      */
226     void SetBlenderEnabled(bool blenderEnabled);
227 
228     /**
229      * @brief Returns the blenderEnabled flag, which determines whether the blender is used.
230      * @return the blenderEnabled flag, whether the blender is used.
231      */
GetBlenderEnabled()232     bool GetBlenderEnabled() const { return blenderEnabled_; };
233 
234     /**
235      * @brief Returns the user-supplied blend function, if one has been set.
236      * @return the Blender assigned to this Brush, otherwise nullptr
237      */
238     const Blender* GetBlenderPtr() const;
239 
240     /**
241      * @brief Returns true if pixels on the active edges of Path may be drawn with partial transparency.
242      * @return antialiasing state
243      */
244     bool IsAntiAlias() const;
245 
246     /**
247      * @brief Requests, but does not require, that edge pixels draw opaque or with partial transparency.
248      * @param aa  setting for antialiasing
249      */
250     void SetAntiAlias(bool aa);
251 
252     /**
253      * @brief Sets PathEffect to pen. Pass nullptr to leave the path geometry unaltered.
254      * @param e replace Path with a modification when drawn
255      */
256     void SetPathEffect(std::shared_ptr<PathEffect> e);
257 
258     /**
259      * @brief Returns PathEffect if set, or nullptr.
260      * @return A shared pointer to PathEffect if previously set, nullptr otherwise
261      */
262     std::shared_ptr<PathEffect> GetPathEffect() const;
263 
264     /**
265      * @brief Returns PathEffect if set, or nullptr.
266      * @return A pointer to PathEffect if previously set, nullptr otherwise
267      */
268     const PathEffect* GetPathEffectPtr() const;
269 
270     /**
271      * @brief Set the Filter object with three property.
272      * ColorFilter, MaskFilter, and ImageFilter to apply to subsequent draw
273      * @param filter the Filter object to set
274      */
275     void SetFilter(const Filter& filter);
276     const Filter& GetFilter() const;
277     bool HasFilter() const;
278 
279     /**
280      * @brief Sets optional colors used when filling a path, such as a gradient. Sets SkShader to shader
281      * @param shader how geometry is filled with color; if nullptr, color is used instead
282      */
283     void SetShaderEffect(std::shared_ptr<ShaderEffect> e);
284 
285     /**
286      * @brief Returns optional colors used when filling a path, such as a gradient.
287      * @return A shared pointer to ShaderEffect
288      */
289     std::shared_ptr<ShaderEffect> GetShaderEffect() const;
290 
291     /**
292      * @brief Returns optional colors used when filling a path, such as a gradient.
293      * @return A pointer to ShaderEffect
294      */
295     const ShaderEffect* GetShaderEffectPtr() const;
296 
297     /**
298      * @brief Sets all contents to their initial values. This is equivalent to replacing
299      * Pen with the result of Pen().
300      */
301     void Reset();
302 
303     /**
304      * @brief Sets BlurDrawLooper, it will generate two draw operations, which may affect performance.
305      */
306     void SetLooper(std::shared_ptr<BlurDrawLooper> blurDrawLooper);
307 
308     /**
309      * @brief Gets BlurDrawLooper.
310      */
311     std::shared_ptr<BlurDrawLooper> GetLooper() const;
312 
313     /**
314      * @brief Returns the filled equivalent of the stroked path.
315      * @param src Path read to create a filled version
316      * @param dst resulting Path; may be the same as src
317      * @param rect optional limit passed to PathEffect
318      * @param matrix tranform passed to PathEffect
319      */
320     bool GetFillPath(const Path& src, Path& dst, const Rect* rect, const Matrix& matrix);
321 
322     friend DRAWING_API bool operator==(const Pen& p1, const Pen& p2);
323     friend DRAWING_API bool operator!=(const Pen& p1, const Pen& p2);
324 
325     void Dump(std::string& out) const;
326 
327 private:
328     scalar width_;
329     scalar miterLimit_;
330     JoinStyle join_;
331     CapStyle cap_;
332     std::shared_ptr<PathEffect> pathEffect_;
333 
334     Brush brush_;
335 
336     bool blenderEnabled_ = true;
337 };
338 } // namespace Drawing
339 } // namespace Rosen
340 } // namespace OHOS
341 #endif