• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_TEXT_FIELD_ROSEN_RENDER_TEXT_FIELD_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_TEXT_FIELD_ROSEN_RENDER_TEXT_FIELD_H
18 
19 #include <memory>
20 #include <string>
21 
22 #include "rosen_text/font_collection.h"
23 #include "rosen_text/typography.h"
24 #include "rosen_text/typography_style.h"
25 #ifndef USE_ROSEN_DRAWING
26 #include "third_party/skia/include/core/SkBitmap.h"
27 #include "third_party/skia/include/core/SkCanvas.h"
28 #else
29 #include "core/components_ng/render/drawing.h"
30 #endif
31 
32 #include "core/components/common/properties/decoration.h"
33 #include "core/components/text_field/render_text_field.h"
34 
35 namespace OHOS::Rosen {
36 class FontCollection;
37 struct TypographyStyle;
38 struct TextStyle;
39 } // namespace OHOS::Rosen
40 namespace OHOS::Ace {
41 
42 class Component;
43 
44 class RosenRenderTextField final : public RenderTextField {
45     DECLARE_ACE_TYPE(RosenRenderTextField, RenderTextField);
46 
47 public:
48     RosenRenderTextField() = default;
49     ~RosenRenderTextField() override = default;
50 
51     void Paint(RenderContext& context, const Offset& offset) override;
52 
GetCaretRect()53     const Rect& GetCaretRect() const
54     {
55         return caretRect_;
56     }
57 
GetStartRect()58     const Rect& GetStartRect() const
59     {
60         return startCaretRect_;
61     }
62 
63 #ifndef USE_ROSEN_DRAWING
GetBitmap()64     const SkBitmap& GetBitmap() const
65 #else
66     const RSBitmap& GetBitmap() const
67 #endif
68     {
69         return canvasCache_;
70     }
71 
72 protected:
73     int32_t GetCursorPositionForMoveUp() override;
74     int32_t GetCursorPositionForMoveDown() override;
75     int32_t GetCursorPositionForClick(const Offset& offset) override;
76     int32_t AdjustCursorAndSelection(int32_t currentCursorPosition) override;
77     DirectionStatus GetDirectionStatusOfPosition(int32_t position) const override;
78     Offset GetHandleOffset(int32_t extend) override;
79     Size ComputeDeflateSizeOfErrorAndCountText() const override;
80     void ResetStatus() override;
81     double GetLongestLine() const override;
82 
83 private:
84     std::unique_ptr<Rosen::TypographyStyle> CreateParagraphStyle(bool isErrorText = false);
85     std::unique_ptr<Rosen::TextStyle> CreateTextStyle(const TextStyle& style, bool isPlaceholder = false);
86 
87     double PreferredLineHeight() override;
88     void UpdateCaretProto();
89     // Case of upstream affinity, which means the caret size should fit with the glyphs before [extent].
90     bool ComputeOffsetForCaretUpstream(int32_t extent, CaretMetrics& result) const;
91     bool ComputeOffsetForCaretDownstream(int32_t extent, CaretMetrics& result) const;
92     bool ComputeOffsetForCaretCloserToClick(int32_t extent, CaretMetrics& result) const;
93     // Make an offset when no text exists. The position of caret depends on the [textAlign_] && [textDirection_].
94     Offset MakeEmptyOffset() const;
95     Size Measure() override;
96     double GetRealTextWidth() const override;
97     double MeasureParagraph(
98         const std::unique_ptr<Rosen::TypographyStyle>& paragraphStyle, std::unique_ptr<Rosen::TextStyle>& txtStyle);
99     Size ComputeLayoutSize(const Size& size, double decorationHeight);
100 
101     Rect GetInnerRect(const Decoration& decoration, const Rect& outer, double dipScale) const;
102     bool GetCaretRect(int32_t extent, Rect& caretRect, double caretHeightOffset = 0.0) const override;
103     // Compute the offset of caret and text. Must be called after paragraph built.
104     void ComputeOffsetAfterLayout();
105     // Compute the offset to align text and icon to vertical center.
106     Offset ComputeVerticalOffsetForCenter(double outerHeight, double innerHeight) const;
107     void SetShaderIfNeeded(std::unique_ptr<Rosen::TypographyStyle> paragraphStyle,
108         std::unique_ptr<Rosen::TextStyle> txtStyle, double textAreaWidth);
109 #ifndef USE_ROSEN_DRAWING
110     sk_sp<SkShader> MakeGradientShader(double shadeWidth) const;
111 #else
112     std::shared_ptr<RSShaderEffect> MakeGradientShader(double shadeWidth) const;
113 #endif
114     std::shared_ptr<Rosen::FontCollection> GetFontCollection();
115     void ResetParagraphIfNeeded();
116     void ComputeExtendHeight(double decorationHeight);
117     double GetBoundaryOfParagraph(bool isLeftBoundary) const;
118     bool AdjustCursorAndSelectionForLtr(bool isBeforeCharRtl, bool isAfterCharRtl, const std::wstring& textBeforeCursor,
119         const std::wstring& textAfterCursor, int32_t& result);
120     bool AdjustCursorAndSelectionForRtl(bool isBeforeCharRtl, bool isAfterCharRtl, const std::wstring& textBeforeCursor,
121         const std::wstring& textAfterCursor, int32_t& result);
122     bool NeedAdjustPosition(const std::wstring& textBeforeCursor);
123 
124     // Paint cursor at the extent position. When [affinity] supported, extends this function.
125 #ifndef USE_ROSEN_DRAWING
126     void PaintCaret(SkCanvas& canvas, const Rect& caretRect);
127     void PaintDecoration(const Offset& offset, SkCanvas* canvas, const Size& size, RenderContext& context);
128     void PaintSelectCaret(SkCanvas* canvas);
129     void PaintIcon(const Offset& offset, RenderContext& context);
130     void PaintSelection(SkCanvas* canvas) const;
131     void PaintTextAndPlaceholder(SkCanvas* canvas) const;
132     void PaintErrorText(SkCanvas* canvas) const;
133     void PaintCountText(SkCanvas* canvas) const;
134     void PaintOverlayForHoverAndPress(const Offset& offset, SkCanvas* canvas) const;
135     void PaintTextField(const Offset& offset, RenderContext& context, SkCanvas* canvas, bool isMagnifier = false);
136     SkVector GetSkRadii(const Radius& radius) const;
137     void PaintScrollBar(const Offset& offset, RenderContext& context, SkCanvas* canvas);
138 #else
139     void PaintCaret(RSCanvas& canvas, const Rect& caretRect);
140     void PaintDecoration(const Offset& offset, RSCanvas* canvas, const Size& size, RenderContext& context);
141     void PaintSelectCaret(RSCanvas* canvas);
142     void PaintIcon(const Offset& offset, RenderContext& context);
143     void PaintSelection(RSCanvas* canvas) const;
144     void PaintTextAndPlaceholder(RSCanvas* canvas) const;
145     void PaintErrorText(RSCanvas* canvas) const;
146     void PaintCountText(RSCanvas* canvas) const;
147     void PaintOverlayForHoverAndPress(const Offset& offset, RSCanvas* canvas) const;
148     void PaintTextField(const Offset& offset, RenderContext& context, RSCanvas* canvas, bool isMagnifier = false);
149     RSPoint GetRSRadii(const Radius& radius) const;
150     void PaintScrollBar(const Offset& offset, RenderContext& context, RSCanvas* canvas);
151 #endif
152     void PaintFocus(const Offset& offset, const Size& widthHeight, RenderContext& context);
153 
154     std::unique_ptr<Rosen::Typography> paragraph_;
155     std::unique_ptr<Rosen::Typography> errorParagraph_;
156     std::unique_ptr<Rosen::Typography> countParagraph_;
157     std::unique_ptr<Rosen::Typography> placeholderParagraph_;
158     // Used to estimate size.
159     std::unique_ptr<Rosen::Typography> template_;
160 
161     Rect startCaretRect_;
162     Size lastLayoutSize_;
163     double originInnerWidth_ = 0.0;
164     double realTextWidth_ = 0.0;
165 
166 #ifndef USE_ROSEN_DRAWING
167     SkBitmap canvasCache_ {};
168     std::unique_ptr<SkCanvas> magnifierCanvas_;
169 #else
170     RSBitmap canvasCache_ {};
171     std::unique_ptr<RSCanvas> magnifierCanvas_;
172 #endif
173 };
174 
175 } // namespace OHOS::Ace
176 
177 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_TEXT_FIELD_ROSEN_RENDER_TEXT_FIELD_H
178