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 #ifndef PAINT_H
17 #define PAINT_H
18 
19 #include "draw/brush.h"
20 #include "draw/pen.h"
21 
22 namespace OHOS {
23 namespace Rosen {
24 namespace Drawing {
25 class DRAWING_API Paint {
26 public:
27     constexpr static scalar DEFAULT_MITER_VAL = 4.0f;
28 
29     Paint() noexcept;
30     Paint(const Paint& other) noexcept;
31     Paint(const Color& c, std::shared_ptr<ColorSpace> colorSpace = nullptr) noexcept;
32     Paint& operator=(const Paint& other);
~Paint()33     ~Paint() {};
34 
35     static bool CanCombinePaint(const Paint& pen, const Paint& brush);
36     void AttachBrush(const Brush& brush);
37     void AttachPen(const Pen& pen);
38 
39     enum PaintStyle : uint8_t {
40         PAINT_NONE = 0x00,
41         PAINT_FILL = 0x01,
42         PAINT_STROKE = 0x02,
43         PAINT_FILL_STROKE = 0x03
44     };
45 
46     void SetStyle(const PaintStyle& style);
GetStyle()47     PaintStyle GetStyle() const { return style_; }
IsValid()48     bool IsValid() const { return style_ != PaintStyle::PAINT_NONE; }
49     bool HasStrokeStyle() const;
50 
51     void SetColor(const Color& c);
52     void SetARGB(int a, int r, int g, int b);
53     void SetColor(const Color4f& cf, std::shared_ptr<ColorSpace> colorSpace = nullptr);
GetColor()54     const Color& GetColor() const { return color_; }
GetColor4f()55     const Color4f& GetColor4f() { return color_.GetColor4f(); }
GetColorSpace()56     const std::shared_ptr<ColorSpace> GetColorSpace() const { return colorSpace_; }
GetColorSpacePtr()57     const ColorSpace* GetColorSpacePtr() const { return colorSpace_.get(); }
58 
59     void SetAlpha(uint32_t a);
60     void SetAlphaF(scalar a);
GetAlpha()61     inline uint32_t GetAlpha() const { return color_.GetAlpha(); }
GetAlphaF()62     inline scalar GetAlphaF() const { return color_.GetAlphaF(); }
63 
64     void SetWidth(scalar width);
GetWidth()65     scalar GetWidth() const { return width_; }
66 
67     void SetMiterLimit(scalar limit);
GetMiterLimit()68     scalar GetMiterLimit() const { return miterLimit_; }
69 
70     void SetCapStyle(Pen::CapStyle cs);
GetCapStyle()71     Pen::CapStyle GetCapStyle() const { return cap_; }
72 
73     void SetJoinStyle(Pen::JoinStyle js);
GetJoinStyle()74     Pen::JoinStyle GetJoinStyle() const { return join_; }
75 
76     void SetBlendMode(BlendMode mode);
GetBlendMode()77     BlendMode GetBlendMode() const { return blendMode_; }
78 
79     void SetFilter(const Filter& filter);
GetFilter()80     const Filter GetFilter() const { return filter_; }
HasFilter()81     bool HasFilter() const { return hasFilter_; }
82 
83     void SetShaderEffect(std::shared_ptr<ShaderEffect> e);
GetShaderEffect()84     const std::shared_ptr<ShaderEffect> GetShaderEffect() const { return shaderEffect_; }
GetShaderEffectPtr()85     const ShaderEffect* GetShaderEffectPtr() const { return shaderEffect_.get(); }
86 
87     void SetPathEffect(std::shared_ptr<PathEffect> e);
GetPathEffect()88     const std::shared_ptr<PathEffect> GetPathEffect() const { return pathEffect_; }
GetPathEffectPtr()89     const PathEffect* GetPathEffectPtr() const { return pathEffect_.get(); }
90 
91     void SetBlender(std::shared_ptr<Blender> blender);
GetBlender()92     const std::shared_ptr<Blender> GetBlender() const { return blender_; }
GetBlenderPtr()93     const Blender* GetBlenderPtr() const { return blender_.get(); }
94 
95     void SetBlenderEnabled(bool blenderEnabled);
GetBlenderEnabled()96     bool GetBlenderEnabled() const { return blenderEnabled_; };
97 
98     void SetLooper(std::shared_ptr<BlurDrawLooper> blurDrawLooper);
99     std::shared_ptr<BlurDrawLooper> GetLooper() const;
100 
101     void SetAntiAlias(bool aa);
IsAntiAlias()102     bool IsAntiAlias() const { return antiAlias_; }
103 
104     void Reset();
105     void Disable();
106 
107     friend bool operator==(const Paint& p1, const Paint& p2);
108     friend bool operator!=(const Paint& p1, const Paint& p2);
109 
110 private:
111     bool antiAlias_ = false;
112     Color color_ = Color::COLOR_BLACK;
113     BlendMode blendMode_ = BlendMode::SRC_OVER;
114     PaintStyle style_ = PaintStyle::PAINT_NONE;
115     scalar width_ = 0.0f;
116     scalar miterLimit_ = DEFAULT_MITER_VAL; // default as 4.0f
117     Pen::JoinStyle join_ = Pen::JoinStyle::DEFAULT_JOIN;
118     Pen::CapStyle cap_ = Pen::CapStyle::DEFAULT_CAP;
119 
120     bool blenderEnabled_ = true;
121     bool hasFilter_ = false;
122     Filter filter_;
123 
124     std::shared_ptr<ColorSpace> colorSpace_ = nullptr;
125     std::shared_ptr<ShaderEffect> shaderEffect_ = nullptr;
126     std::shared_ptr<PathEffect> pathEffect_ = nullptr;
127     std::shared_ptr<Blender> blender_ = nullptr;
128     // blur effect, non-atomic interface
129     std::shared_ptr<BlurDrawLooper> blurDrawLooper_ = nullptr;
130 };
131 } // namespace Drawing
132 } // namespace Rosen
133 } // namespace OHOS
134 #endif // PAINT_H