1 /* 2 * Copyright (c) 2022-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_RENDER_PAPAGRAPH_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_RENDER_PAPAGRAPH_H 18 19 #include "base/geometry/ng/size_t.h" 20 #include "base/image/pixel_map.h" 21 #include "base/memory/ace_type.h" 22 #include "core/components/common/layout/constants.h" 23 #include "core/components/common/properties/text_style.h" 24 #include "core/components_ng/render/drawing_forward.h" 25 #include "core/components_ng/render/font_collection.h" 26 #include "core/pipeline_ng/pipeline_context.h" 27 28 namespace OHOS::Ace::NG { 29 30 struct LeadingMargin { 31 SizeF size; 32 RefPtr<PixelMap> pixmap; 33 34 bool operator==(const LeadingMargin& other) const 35 { 36 return size == other.size && pixmap == other.pixmap; 37 } 38 }; 39 40 struct ParagraphStyle { 41 TextDirection direction = TextDirection::AUTO; 42 TextAlign align = TextAlign::LEFT; 43 uint32_t maxLines = 1; 44 std::string fontLocale; 45 WordBreak wordBreak = WordBreak::NORMAL; 46 EllipsisMode ellipsisMode = EllipsisMode::TAIL; 47 TextOverflow textOverflow = TextOverflow::CLIP; 48 std::optional<LeadingMargin> leadingMargin; 49 double fontSize = 14.0; 50 }; 51 52 struct CaretMetricsF { 53 CaretMetricsF() = default; CaretMetricsFCaretMetricsF54 CaretMetricsF(const OffsetF& position, float h) 55 { 56 offset = position; 57 height = h; 58 } ResetCaretMetricsF59 void Reset() 60 { 61 offset.Reset(); 62 height = 0.0; 63 } 64 65 OffsetF offset; 66 // When caret is close to different glyphs, the height will be different. 67 float height = 0.0f; ToStringCaretMetricsF68 std::string ToString() const 69 { 70 std::string result = "Offset: "; 71 result += offset.ToString(); 72 result += ", height: "; 73 result += std::to_string(height); 74 return result; 75 } 76 }; 77 78 // Paragraph is interface for drawing text and text paragraph. 79 class Paragraph : public virtual AceType { 80 DECLARE_ACE_TYPE(NG::Paragraph, AceType) 81 82 public: 83 static RefPtr<Paragraph> Create(const ParagraphStyle& paraStyle, const RefPtr<FontCollection>& fontCollection); 84 85 // whether the paragraph has been build 86 virtual bool IsValid() = 0; 87 88 // interfaces for build text paragraph 89 virtual void PushStyle(const TextStyle& style) = 0; 90 virtual void PopStyle() = 0; 91 virtual void AddText(const std::u16string& text) = 0; 92 virtual void AddSymbol(const std::uint32_t& symbolId) = 0; 93 virtual int32_t AddPlaceholder(const PlaceholderRun& span) = 0; 94 virtual void Build() = 0; 95 virtual void Reset() = 0; 96 97 // interfaces for layout 98 virtual void Layout(float width) = 0; 99 virtual float GetHeight() = 0; 100 virtual float GetTextWidth() = 0; 101 virtual size_t GetLineCount() = 0; 102 virtual float GetMaxIntrinsicWidth() = 0; 103 virtual bool DidExceedMaxLines() = 0; 104 virtual float GetLongestLine() = 0; 105 virtual float GetMaxWidth() = 0; 106 virtual float GetAlphabeticBaseline() = 0; 107 virtual float GetCharacterWidth(int32_t index) = 0; 108 virtual int32_t GetGlyphIndexByCoordinate(const Offset& offset, bool isSelectionPos = false) = 0; 109 virtual void GetRectsForRange(int32_t start, int32_t end, std::vector<RectF>& selectedRects) = 0; 110 virtual void GetRectsForPlaceholders(std::vector<RectF>& selectedRects) = 0; 111 virtual bool ComputeOffsetForCaretDownstream( 112 int32_t extent, CaretMetricsF& result, bool needLineHighest = true) = 0; 113 virtual bool ComputeOffsetForCaretUpstream(int32_t extent, CaretMetricsF& result, bool needLineHighest = true) = 0; 114 virtual bool CalcCaretMetricsByPosition( 115 int32_t extent, CaretMetricsF& caretCaretMetric, TextAffinity textAffinity) = 0; 116 virtual bool CalcCaretMetricsByPosition(int32_t extent, CaretMetricsF& caretCaretMetric, 117 const OffsetF& lastTouchOffset, TextAffinity& textAffinity) = 0; 118 virtual void SetIndents(const std::vector<float>& indents) = 0; 119 virtual bool GetWordBoundary(int32_t offset, int32_t& start, int32_t& end) = 0; 120 virtual std::u16string GetParagraphText() = 0; 121 virtual const ParagraphStyle& GetParagraphStyle() const = 0; 122 // interfaces for pass on Symbol Animation interface 123 virtual void SetParagraphSymbolAnimation(const RefPtr<FrameNode>& frameNode) = 0; 124 // interfaces for painting 125 virtual void Paint(RSCanvas& canvas, float x, float y) = 0; 126 #ifndef USE_ROSEN_DRAWING 127 virtual void Paint(SkCanvas* skCanvas, float x, float y) = 0; 128 #endif 129 }; 130 } // namespace OHOS::Ace::NG 131 132 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_RENDER_PAPAGRAPH_H 133