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() const47 PaintStyle GetStyle() const { return style_; } IsValid() const48 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() const54 const Color& GetColor() const { return color_; } GetColor4f()55 const Color4f& GetColor4f() { return color_.GetColor4f(); } GetColorSpace() const56 const std::shared_ptr<ColorSpace> GetColorSpace() const { return colorSpace_; } GetColorSpacePtr() const57 const ColorSpace* GetColorSpacePtr() const { return colorSpace_.get(); } 58 59 void SetAlpha(uint32_t a); 60 void SetAlphaF(scalar a); GetAlpha() const61 inline uint32_t GetAlpha() const { return color_.GetAlpha(); } GetAlphaF() const62 inline scalar GetAlphaF() const { return color_.GetAlphaF(); } 63 64 void SetWidth(scalar width); GetWidth() const65 scalar GetWidth() const { return width_; } 66 67 void SetMiterLimit(scalar limit); GetMiterLimit() const68 scalar GetMiterLimit() const { return miterLimit_; } 69 70 void SetCapStyle(Pen::CapStyle cs); GetCapStyle() const71 Pen::CapStyle GetCapStyle() const { return cap_; } 72 73 void SetJoinStyle(Pen::JoinStyle js); GetJoinStyle() const74 Pen::JoinStyle GetJoinStyle() const { return join_; } 75 76 void SetBlendMode(BlendMode mode); GetBlendMode() const77 BlendMode GetBlendMode() const { return blendMode_; } 78 79 void SetFilter(const Filter& filter); GetFilter() const80 const Filter GetFilter() const { return filter_; } HasFilter() const81 bool HasFilter() const { return hasFilter_; } 82 83 void SetShaderEffect(std::shared_ptr<ShaderEffect> e); GetShaderEffect() const84 const std::shared_ptr<ShaderEffect> GetShaderEffect() const { return shaderEffect_; } GetShaderEffectPtr() const85 const ShaderEffect* GetShaderEffectPtr() const { return shaderEffect_.get(); } 86 87 void SetPathEffect(std::shared_ptr<PathEffect> e); GetPathEffect() const88 const std::shared_ptr<PathEffect> GetPathEffect() const { return pathEffect_; } GetPathEffectPtr() const89 const PathEffect* GetPathEffectPtr() const { return pathEffect_.get(); } 90 91 void SetBlender(std::shared_ptr<Blender> blender); GetBlender() const92 const std::shared_ptr<Blender> GetBlender() const { return blender_; } GetBlenderPtr() const93 const Blender* GetBlenderPtr() const { return blender_.get(); } 94 95 void SetBlenderEnabled(bool blenderEnabled); GetBlenderEnabled() const96 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() const102 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 void Dump(std::string& out) const; 111 112 private: 113 bool antiAlias_ = false; 114 Color color_ = Color::COLOR_BLACK; 115 BlendMode blendMode_ = BlendMode::SRC_OVER; 116 PaintStyle style_ = PaintStyle::PAINT_NONE; 117 scalar width_ = 0.0f; 118 scalar miterLimit_ = DEFAULT_MITER_VAL; // default as 4.0f 119 Pen::JoinStyle join_ = Pen::JoinStyle::DEFAULT_JOIN; 120 Pen::CapStyle cap_ = Pen::CapStyle::DEFAULT_CAP; 121 122 bool blenderEnabled_ = true; 123 bool hasFilter_ = false; 124 Filter filter_; 125 126 std::shared_ptr<ColorSpace> colorSpace_ = nullptr; 127 std::shared_ptr<ShaderEffect> shaderEffect_ = nullptr; 128 std::shared_ptr<PathEffect> pathEffect_ = nullptr; 129 std::shared_ptr<Blender> blender_ = nullptr; 130 // blur effect, non-atomic interface 131 std::shared_ptr<BlurDrawLooper> blurDrawLooper_ = nullptr; 132 }; 133 } // namespace Drawing 134 } // namespace Rosen 135 } // namespace OHOS 136 #endif // PAINT_H