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 double GetLongestLine() const override; 99 100 private: 101 #ifndef USE_GRAPHIC_TEXT_GINE 102 std::unique_ptr<txt::ParagraphStyle> CreateParagraphStyle(bool isErrorText = false); 103 std::unique_ptr<txt::TextStyle> CreateTextStyle(const TextStyle& style, bool isPlaceholder = false); 104 #else 105 std::unique_ptr<Rosen::TypographyStyle> CreateParagraphStyle(bool isErrorText = false); 106 std::unique_ptr<Rosen::TextStyle> CreateTextStyle(const TextStyle& style, bool isPlaceholder = false); 107 #endif 108 109 double PreferredLineHeight() override; 110 void UpdateCaretProto(); 111 // Case of upstream affinity, which means the caret size should fit with the glyphs before [extent]. 112 bool ComputeOffsetForCaretUpstream(int32_t extent, CaretMetrics& result) const; 113 bool ComputeOffsetForCaretDownstream(int32_t extent, CaretMetrics& result) const; 114 bool ComputeOffsetForCaretCloserToClick(int32_t extent, CaretMetrics& result) const; 115 // Make an offset when no text exists. The position of caret depends on the [textAlign_] && [textDirection_]. 116 Offset MakeEmptyOffset() const; 117 Size Measure() override; 118 double GetRealTextWidth() const override; 119 #ifndef USE_GRAPHIC_TEXT_GINE 120 double MeasureParagraph( 121 const std::unique_ptr<txt::ParagraphStyle>& paragraphStyle, std::unique_ptr<txt::TextStyle>& txtStyle); 122 #else 123 double MeasureParagraph( 124 const std::unique_ptr<Rosen::TypographyStyle>& paragraphStyle, std::unique_ptr<Rosen::TextStyle>& txtStyle); 125 #endif 126 Size ComputeLayoutSize(const Size& size, double decorationHeight); 127 128 Rect GetInnerRect(const Decoration& decoration, const Rect& outer, double dipScale) const; 129 bool GetCaretRect(int32_t extent, Rect& caretRect, double caretHeightOffset = 0.0) const override; 130 // Compute the offset of caret and text. Must be called after paragraph built. 131 void ComputeOffsetAfterLayout(); 132 // Compute the offset to align text and icon to vertical center. 133 Offset ComputeVerticalOffsetForCenter(double outerHeight, double innerHeight) const; 134 #ifndef USE_GRAPHIC_TEXT_GINE 135 void SetShaderIfNeeded(std::unique_ptr<txt::ParagraphStyle> paragraphStyle, 136 std::unique_ptr<txt::TextStyle> txtStyle, double textAreaWidth); 137 #else 138 void SetShaderIfNeeded(std::unique_ptr<Rosen::TypographyStyle> paragraphStyle, 139 std::unique_ptr<Rosen::TextStyle> txtStyle, double textAreaWidth); 140 #endif 141 #ifndef USE_ROSEN_DRAWING 142 sk_sp<SkShader> MakeGradientShader(double shadeWidth) const; 143 #else 144 std::shared_ptr<RSShaderEffect> MakeGradientShader(double shadeWidth) const; 145 #endif 146 #ifndef USE_GRAPHIC_TEXT_GINE 147 std::shared_ptr<txt::FontCollection> GetFontCollection(); 148 #else 149 std::shared_ptr<Rosen::FontCollection> GetFontCollection(); 150 #endif 151 void ResetParagraphIfNeeded(); 152 void ComputeExtendHeight(double decorationHeight); 153 double GetBoundaryOfParagraph(bool isLeftBoundary) const; 154 bool AdjustCursorAndSelectionForLtr(bool isBeforeCharRtl, bool isAfterCharRtl, const std::wstring& textBeforeCursor, 155 const std::wstring& textAfterCursor, int32_t& result); 156 bool AdjustCursorAndSelectionForRtl(bool isBeforeCharRtl, bool isAfterCharRtl, const std::wstring& textBeforeCursor, 157 const std::wstring& textAfterCursor, int32_t& result); 158 bool NeedAdjustPosition(const std::wstring& textBeforeCursor); 159 160 // Paint cursor at the extent position. When [affinity] supported, extends this function. 161 #ifndef USE_ROSEN_DRAWING 162 void PaintCaret(SkCanvas& canvas, const Rect& caretRect); 163 void PaintDecoration(const Offset& offset, SkCanvas* canvas, const Size& size, RenderContext& context); 164 void PaintSelectCaret(SkCanvas* canvas); 165 void PaintIcon(const Offset& offset, RenderContext& context); 166 void PaintSelection(SkCanvas* canvas) const; 167 void PaintTextAndPlaceholder(SkCanvas* canvas) const; 168 void PaintErrorText(SkCanvas* canvas) const; 169 void PaintCountText(SkCanvas* canvas) const; 170 void PaintOverlayForHoverAndPress(const Offset& offset, SkCanvas* canvas) const; 171 void PaintTextField(const Offset& offset, RenderContext& context, SkCanvas* canvas, bool isMagnifier = false); 172 SkVector GetSkRadii(const Radius& radius) const; 173 void PaintScrollBar(const Offset& offset, RenderContext& context, SkCanvas* canvas); 174 #else 175 void PaintCaret(RSCanvas& canvas, const Rect& caretRect); 176 void PaintDecoration(const Offset& offset, RSCanvas* canvas, const Size& size, RenderContext& context); 177 void PaintSelectCaret(RSCanvas* canvas); 178 void PaintIcon(const Offset& offset, RenderContext& context); 179 void PaintSelection(RSCanvas* canvas) const; 180 void PaintTextAndPlaceholder(RSCanvas* canvas) const; 181 void PaintErrorText(RSCanvas* canvas) const; 182 void PaintCountText(RSCanvas* canvas) const; 183 void PaintOverlayForHoverAndPress(const Offset& offset, RSCanvas* canvas) const; 184 void PaintTextField(const Offset& offset, RenderContext& context, RSCanvas* canvas, bool isMagnifier = false); 185 RSPoint GetRSRadii(const Radius& radius) const; 186 void PaintScrollBar(const Offset& offset, RenderContext& context, RSCanvas* canvas); 187 #endif 188 void PaintFocus(const Offset& offset, const Size& widthHeight, RenderContext& context); 189 190 #ifndef USE_GRAPHIC_TEXT_GINE 191 std::unique_ptr<txt::Paragraph> paragraph_; 192 std::unique_ptr<txt::Paragraph> errorParagraph_; 193 std::unique_ptr<txt::Paragraph> countParagraph_; 194 std::unique_ptr<txt::Paragraph> placeholderParagraph_; 195 #else 196 std::unique_ptr<Rosen::Typography> paragraph_; 197 std::unique_ptr<Rosen::Typography> errorParagraph_; 198 std::unique_ptr<Rosen::Typography> countParagraph_; 199 std::unique_ptr<Rosen::Typography> placeholderParagraph_; 200 #endif 201 // Used to estimate size. 202 #ifndef USE_GRAPHIC_TEXT_GINE 203 std::unique_ptr<txt::Paragraph> template_; 204 #else 205 std::unique_ptr<Rosen::Typography> template_; 206 #endif 207 208 Rect startCaretRect_; 209 Size lastLayoutSize_; 210 double originInnerWidth_ = 0.0; 211 double realTextWidth_ = 0.0; 212 213 #ifndef USE_ROSEN_DRAWING 214 SkBitmap canvasCache_ {}; 215 std::unique_ptr<SkCanvas> magnifierCanvas_; 216 #else 217 RSBitmap canvasCache_ {}; 218 std::unique_ptr<RSCanvas> magnifierCanvas_; 219 #endif 220 }; 221 222 } // namespace OHOS::Ace 223 224 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_TEXT_FIELD_ROSEN_RENDER_TEXT_FIELD_H 225