1 /* 2 * Copyright (c) 2024 Huawei Device Co., Ltd.. All rights reserved. 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 ROSEN_MODULES_SPTEXT_PARAGRAPH_H 17 #define ROSEN_MODULES_SPTEXT_PARAGRAPH_H 18 19 #include <vector> 20 21 #include "include/core/SkRect.h" 22 #include "line_metrics.h" 23 #include "modules/skparagraph/include/Metrics.h" 24 #include "modules/skparagraph/include/Paragraph.h" 25 #include "paragraph_style.h" 26 #include "rosen_text/symbol_animation_config.h" 27 #include "text_line_base.h" 28 #include "txt/text_style.h" 29 #include "utils.h" 30 31 class SkCanvas; 32 33 namespace OHOS { 34 namespace Rosen { 35 namespace Drawing { 36 class Canvas; 37 struct FontMetrics; 38 } 39 } // namespace Rosen 40 } // namespace OHOS 41 42 namespace OHOS { 43 namespace Rosen { 44 namespace SPText { 45 enum class RectWidthStyle { 46 TIGHT, 47 MAX 48 }; 49 50 enum class RectHeightStyle { 51 TIGHT, 52 MAX, 53 INCLUDE_LINESPACING_MIDDLE, 54 INCLUDE_LINESPACING_TOP, 55 INCLUDE_LINESPACING_BOTTOM, 56 STRUT 57 }; 58 59 enum class Affinity { 60 UPSTREAM, 61 DOWNSTREAM, 62 }; 63 64 struct PositionWithAffinity { PositionWithAffinityPositionWithAffinity65 PositionWithAffinity(size_t p, Affinity a) : position(p), affinity(a) {} 66 67 const size_t position; 68 const Affinity affinity; 69 }; 70 71 struct TextBox { TextBoxTextBox72 TextBox(SkRect r, TextDirection d) : rect(r), direction(d) {} 73 74 SkRect rect; 75 TextDirection direction; 76 }; 77 78 // Paragraph can be laid out and then drawn on the canvas. 79 // Relevant information can be obtained from Paragraph. 80 class Paragraph { 81 public: 82 virtual ~Paragraph() = default; 83 84 // Returns the width limit provided in Layout() method. 85 // This is the maximum width limit for multi-line text. 86 virtual double GetMaxWidth() = 0; 87 88 // Returns the height of the laid out Paragraph. 89 virtual double GetHeight() = 0; 90 91 // Returns the width of the longest line as found in Layout(). 92 virtual double GetLongestLine() = 0; 93 94 // Returns the actual max width with indent of the longest line after Layout(). 95 virtual double GetLongestLineWithIndent() = 0; 96 97 // Returns the actual max width of the longest line after Layout(). 98 virtual double GetMinIntrinsicWidth() = 0; 99 100 // Returns the total width covered by the paragraph without linebreaking. 101 virtual double GetMaxIntrinsicWidth() = 0; 102 103 // Returns the distance from top of Paragraph to 104 // the alphabetic baseline of the first line. 105 // Used for alphabetic fonts (A-Z, a-z, greek, etc.) 106 virtual double GetAlphabeticBaseline() = 0; 107 108 // Returns the distance from top of Paragraph to the ideographic baseline 109 // of the first line.Used for ideographic fonts (Chinese, Japanese, Korean, etc.) 110 virtual double GetIdeographicBaseline() = 0; 111 112 // Returns the distance from the horizontal line indicated by the baseline 113 // attribute to the top of the bounding rectangle of the given text. 114 virtual double GetGlyphsBoundsTop() = 0; 115 116 // Returns the distance from the horizontal line indicated by the baseline 117 // attribute to the bottom of the bounding rectangle of the given text. 118 virtual double GetGlyphsBoundsBottom() = 0; 119 120 // Returns the distance parallel to the baseline from the alignment point given by the 121 // textAlign attribute to the left side of the bounding rectangle of the given text. 122 virtual double GetGlyphsBoundsLeft() = 0; 123 124 // Returns the distance parallel to the baseline from the alignment point given by the 125 // textAlign attribute to the right side of the bounding rectangle of the given text. 126 virtual double GetGlyphsBoundsRight() = 0; 127 128 // Returns true if Paragraph exceeds max lines, it also means that 129 // some content was replaced by an ellipsis. 130 virtual bool DidExceedMaxLines() = 0; 131 132 // Returns the total number of visible lines in the paragraph. 133 virtual size_t GetLineCount() const = 0; 134 135 // Set the text indent. 136 // indents The indents for multi-line text. 137 virtual void SetIndents(const std::vector<float>& indents) = 0; 138 139 // Get the text indent in index. 140 // index The index of element in indents vector. 141 virtual float DetectIndents(size_t index) = 0; 142 143 // Mark the Typography as dirty, and initially state the Typography. 144 virtual void MarkDirty() = 0; 145 146 // Get the unresolved Glyphs count of lines in a text. 147 virtual int32_t GetUnresolvedGlyphsCount() = 0; 148 149 // Update the font size of lines in a text. 150 virtual void UpdateFontSize(size_t from, size_t to, float fontSize) = 0; 151 152 // Layout calculates the positioning of all the glyphs. 153 // This method must be called before other methods are called. 154 virtual void Layout(double width) = 0; 155 156 // Paints the laid out text onto the supplied canvas at (x, y). 157 virtual void Paint(SkCanvas* canvas, double x, double y) = 0; 158 159 // Paints the laid out text onto the supplied canvas at (x, y). 160 virtual void Paint(Drawing::Canvas* canvas, double x, double y) = 0; 161 162 // Paints the text extension path of the layout. 163 virtual void Paint(Drawing::Canvas* canvas, Drawing::Path* path, double hOffset, double vOffset) = 0; 164 165 // Returns a vector of bounding boxes that enclose all text 166 // between start and end glyph indexes. The bounding boxes 167 // can be used to display selections. 168 virtual std::vector<TextBox> GetRectsForRange(size_t start, size_t end, 169 RectHeightStyle rectHeightStyle, RectWidthStyle rectWidthStyle) = 0; 170 171 // Return a vector of bounding boxes that bound all placeholders in Paragraph. 172 // The bounds are tight and each box include only one placeholder. 173 virtual std::vector<TextBox> GetRectsForPlaceholders() = 0; 174 175 // Returns the index of the glyph corresponding to the provided coordinates. 176 // The upper left corner is the origin, and the +y direction is downward. 177 virtual PositionWithAffinity GetGlyphPositionAtCoordinate(double dx, double dy) = 0; 178 179 // Returns the word range of a given glyph in a paragraph. 180 virtual Range<size_t> GetWordBoundary(size_t offset) = 0; 181 182 virtual Range<size_t> GetActualTextRange(int lineNumber, bool includeSpaces) = 0; 183 184 virtual std::vector<skia::textlayout::LineMetrics> GetLineMetrics() = 0; 185 186 virtual bool GetLineMetricsAt(int lineNumber, skia::textlayout::LineMetrics* lineMetrics) const = 0; 187 188 virtual void SetAnimation( 189 std::function<bool(const std::shared_ptr<TextEngine::SymbolAnimationConfig>&)>& animationFunc) = 0; 190 virtual std::function<bool(const std::shared_ptr<TextEngine::SymbolAnimationConfig>&)> GetAnimation() = 0; 191 192 virtual void SetParagraghId(uint32_t id) = 0; 193 194 virtual OHOS::Rosen::Drawing::FontMetrics MeasureText() = 0; 195 virtual OHOS::Rosen::Drawing::FontMetrics GetFontMetricsResult(const OHOS::Rosen::SPText::TextStyle& textStyle) = 0; 196 virtual bool GetLineFontMetrics(const size_t lineNumber, 197 size_t& charNumber, std::vector<Drawing::FontMetrics>& fontMetrics) = 0; 198 virtual std::vector<std::unique_ptr<SPText::TextLineBase>> GetTextLines() const = 0; 199 virtual std::unique_ptr<Paragraph> CloneSelf() = 0; 200 virtual TextStyle SkStyleToTextStyle(const skia::textlayout::TextStyle& skStyle) = 0; 201 virtual void UpdateColor(size_t from, size_t to, const RSColor& color, 202 skia::textlayout::UtfEncodeType encodeType = skia::textlayout::UtfEncodeType::kUtf8) = 0; 203 virtual Range<size_t> GetEllipsisTextRange() = 0; 204 virtual OHOS::Rosen::Drawing::RectI GeneratePaintRegion(double x, double y) = 0; 205 virtual void UpdateForegroundBrush(const TextStyle& spTextStyle) = 0; 206 virtual void Relayout(double width, const ParagraphStyle& paragrahStyle, 207 const std::vector<TextStyle>& textStyes) = 0; 208 virtual bool IsLayoutDone() = 0; 209 virtual void SetLayoutState(size_t state) = 0; 210 virtual void ApplyTextStyleChanges(const std::vector<OHOS::Rosen::SPText::TextStyle>& textStyles) = 0; 211 virtual std::vector<TextBlobRecordInfo> GetTextBlobRecordInfo() const = 0; 212 virtual bool HasSkipTextBlobDrawing() const = 0; 213 virtual void SetSkipTextBlobDrawing(bool state) = 0; 214 virtual bool CanPaintAllText() const = 0; 215 virtual std::string GetDumpInfo() const = 0; 216 }; 217 } // namespace SPText 218 } // namespace Rosen 219 } // namespace OHOS 220 221 #endif // ROSEN_MODULES_SPTEXT_PARAGRAPH_H 222