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