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