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 FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_SHAPE_PAINT_METHOD_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_SHAPE_PAINT_METHOD_H
18 
19 #include "base/geometry/ng/size_t.h"
20 #include "core/components_ng/base/geometry_node.h"
21 #include "core/components_ng/pattern/shape/shape_overlay_modifier.h"
22 #include "core/components_ng/pattern/shape/shape_paint_property.h"
23 #include "core/components_ng/render/node_paint_method.h"
24 
25 namespace OHOS::Ace::NG {
26 class ACE_EXPORT ShapePaintMethod : public NodePaintMethod {
27     DECLARE_ACE_TYPE(ShapePaintMethod, NodePaintMethod)
28 public:
29     ShapePaintMethod() = default;
ShapePaintMethod(const RefPtr<ShapePaintProperty> & shapePaintProperty,const RefPtr<ShapeOverlayModifier> & shapeOverlayModifier)30     ShapePaintMethod(
31         const RefPtr<ShapePaintProperty>& shapePaintProperty,
32         const RefPtr<ShapeOverlayModifier>& shapeOverlayModifier)
33         : propertiesFromAncestor_(shapePaintProperty), shapeOverlayModifier_(shapeOverlayModifier)
34     {}
35     ~ShapePaintMethod() override = default;
36 
GetOverlayModifier(PaintWrapper * paintWrapper)37     RefPtr<Modifier> GetOverlayModifier(PaintWrapper* paintWrapper) override
38     {
39         CHECK_NULL_RETURN(paintWrapper, nullptr);
40         CHECK_NULL_RETURN(shapeOverlayModifier_, nullptr);
41         auto shapePaintProperty = DynamicCast<ShapePaintProperty>(paintWrapper->GetPaintProperty()->Clone());
42         CHECK_NULL_RETURN(shapePaintProperty, nullptr);
43         auto renderContext = paintWrapper->GetRenderContext();
44         CHECK_NULL_RETURN(renderContext, nullptr);
45 
46         auto offset = paintWrapper->GetContentOffset();
47         float width = paintWrapper->GetContentSize().Width();
48         float height = paintWrapper->GetContentSize().Height();
49         float deltaWidth = shapePaintProperty->HasStrokeWidth()
50                                ? ShapeOverlayModifier::SHAPE_OVERLAY_SIZE_FACTOR *
51                                      shapePaintProperty->GetStrokeWidthValue().ConvertToPx()
52                                : ShapeOverlayModifier::SHAPE_OVERLAY_SIZE_DEFAULT;
53         auto scale = renderContext->GetTransformScaleValue({ 1.0f, 1.0f });
54         RectF rect { 0.0f, 0.0f, 0.0f, 0.0f };
55         static const double diff = 1e-10;
56         if (!NearZero(scale.x, diff) && !NearZero(scale.y, diff)) {
57             rect = { offset.GetX() - deltaWidth, offset.GetY() - deltaWidth, (width + deltaWidth * 2) / scale.x,
58                 (height + deltaWidth * 2) / scale.y };
59         }
60         shapeOverlayModifier_->SetBoundsRect(rect);
61         return shapeOverlayModifier_;
62     }
63 
64 protected:
65     RefPtr<ShapePaintProperty> propertiesFromAncestor_;
66     RefPtr<ShapeOverlayModifier> shapeOverlayModifier_;
67     ACE_DISALLOW_COPY_AND_MOVE(ShapePaintMethod);
68 };
69 } // namespace OHOS::Ace::NG
70 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_SHAPE_PAINT_METHOD_H
71