Flutter Engine
The Flutter Engine
paragraph.h
Go to the documentation of this file.
1
2/*
3 * Copyright 2017 Google Inc.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#ifndef LIB_TXT_SRC_PARAGRAPH_H_
19#define LIB_TXT_SRC_PARAGRAPH_H_
20
21#include "flutter/display_list/dl_builder.h"
22#include "line_metrics.h"
23#include "paragraph_style.h"
28
29class SkCanvas;
30
31namespace txt {
32
33// Interface for text layout engines. The current implementation is based on
34// Skia's SkShaper/SkParagraph text layout module.
35class Paragraph {
36 public:
38
39 // Options for various types of bounding boxes provided by
40 // GetRectsForRange(...).
41 enum class RectHeightStyle {
42 // Provide tight bounding boxes that fit heights per run.
43 kTight,
44
45 // The height of the boxes will be the maximum height of all runs in the
46 // line. All rects in the same line will be the same height.
47 kMax,
48
49 // Extends the top and/or bottom edge of the bounds to fully cover any line
50 // spacing. The top edge of each line should be the same as the bottom edge
51 // of the line above. There should be no gaps in vertical coverage given any
52 // ParagraphStyle line_height.
53 //
54 // The top and bottom of each rect will cover half of the
55 // space above and half of the space below the line.
56 kIncludeLineSpacingMiddle,
57 // The line spacing will be added to the top of the rect.
58 kIncludeLineSpacingTop,
59 // The line spacing will be added to the bottom of the rect.
60 kIncludeLineSpacingBottom,
61
62 // Calculate boxes based on the strut's metrics.
63 kStrut
64 };
65
66 enum class RectWidthStyle {
67 // Provide tight bounding boxes that fit widths to the runs of each line
68 // independently.
69 kTight,
70
71 // Extends the width of the last rect of each line to match the position of
72 // the widest rect over all the lines.
73 kMax
74 };
75
77 const size_t position;
79
81 };
82
83 struct TextBox {
86
88 };
89
90 template <typename T>
91 struct Range {
92 Range() : start(), end() {}
93 Range(T s, T e) : start(s), end(e) {}
94
96
97 bool operator==(const Range<T>& other) const {
98 return start == other.start && end == other.end;
99 }
100
101 T width() const { return end - start; }
102
103 void Shift(T delta) {
104 start += delta;
105 end += delta;
106 }
107 };
108
109 virtual ~Paragraph() = default;
110
111 // Returns the width provided in the Layout() method. This is the maximum
112 // width any line in the laid out paragraph can occupy. We expect that
113 // GetMaxWidth() >= GetLayoutWidth().
114 virtual double GetMaxWidth() = 0;
115
116 // Returns the height of the laid out paragraph. NOTE this is not a tight
117 // bounding height of the glyphs, as some glyphs do not reach as low as they
118 // can.
119 virtual double GetHeight() = 0;
120
121 // Returns the width of the longest line as found in Layout(), which is
122 // defined as the horizontal distance from the left edge of the leftmost glyph
123 // to the right edge of the rightmost glyph. We expect that
124 // GetLongestLine() <= GetMaxWidth().
125 virtual double GetLongestLine() = 0;
126
127 // Returns the actual max width of the longest line after Layout().
128 virtual double GetMinIntrinsicWidth() = 0;
129
130 // Returns the total width covered by the paragraph without linebreaking.
131 virtual double GetMaxIntrinsicWidth() = 0;
132
133 // Distance from top of paragraph to the Alphabetic baseline of the first
134 // line. Used for alphabetic fonts (A-Z, a-z, greek, etc.)
135 virtual double GetAlphabeticBaseline() = 0;
136
137 // Distance from top of paragraph to the Ideographic baseline of the first
138 // line. Used for ideographic fonts (Chinese, Japanese, Korean, etc.)
139 virtual double GetIdeographicBaseline() = 0;
140
141 // Checks if the layout extends past the maximum lines and had to be
142 // truncated.
143 virtual bool DidExceedMaxLines() = 0;
144
145 // Layout calculates the positioning of all the glyphs. Must call this method
146 // before Painting and getting any statistics from this class.
147 virtual void Layout(double width) = 0;
148
149 // Paints the laid out text onto the supplied DisplayListBuilder at
150 // (x, y) offset from the origin. Only valid after Layout() is called.
152 double x,
153 double y) = 0;
154
155 // Returns a vector of bounding boxes that enclose all text between start and
156 // end glyph indexes, including start and excluding end.
157 virtual std::vector<TextBox> GetRectsForRange(
158 size_t start,
159 size_t end,
160 RectHeightStyle rect_height_style,
161 RectWidthStyle rect_width_style) = 0;
162
163 // Returns a vector of bounding boxes that bound all inline placeholders in
164 // the paragraph.
165 //
166 // There will be one box for each inline placeholder. The boxes will be in the
167 // same order as they were added to the paragraph. The bounds will always be
168 // tight and should fully enclose the area where the placeholder should be.
169 //
170 // More granular boxes may be obtained through GetRectsForRange, which will
171 // return bounds on both text as well as inline placeholders.
172 virtual std::vector<TextBox> GetRectsForPlaceholders() = 0;
173
174 // Returns the index of the glyph that corresponds to the provided coordinate,
175 // with the top left corner as the origin, and +y direction as down.
177 double dy) = 0;
178
179 virtual bool GetGlyphInfoAt(
180 unsigned offset,
181 skia::textlayout::Paragraph::GlyphInfo* glyphInfo) const = 0;
182
184 double dx,
185 double dy,
186 skia::textlayout::Paragraph::GlyphInfo* glyphInfo) const = 0;
187
188 // Finds the first and last glyphs that define a word containing the glyph at
189 // index offset.
191
192 virtual std::vector<LineMetrics>& GetLineMetrics() = 0;
193
194 virtual bool GetLineMetricsAt(
195 int lineNumber,
196 skia::textlayout::LineMetrics* lineMetrics) const = 0;
197
198 // Returns the total number of visible lines in the paragraph.
199 virtual size_t GetNumberOfLines() const = 0;
200
201 // Returns the zero-indexed line number that contains the given code unit
202 // offset. Returns -1 if the given offset is out of bounds, or points to a
203 // codepoint that is logically after the last visible codepoint.
204 //
205 // If the offset points to a hard line break, this method returns the line
206 // number of the line this hard line break breaks, intead of the new line it
207 // creates.
208 virtual int GetLineNumberAt(size_t utf16Offset) const = 0;
209};
210
211} // namespace txt
212
213#endif // LIB_TXT_SRC_PARAGRAPH_H_
virtual double GetLongestLine()=0
virtual ~Paragraph()=default
virtual std::vector< TextBox > GetRectsForRange(size_t start, size_t end, RectHeightStyle rect_height_style, RectWidthStyle rect_width_style)=0
virtual void Layout(double width)=0
virtual bool GetGlyphInfoAt(unsigned offset, skia::textlayout::Paragraph::GlyphInfo *glyphInfo) const =0
virtual std::vector< LineMetrics > & GetLineMetrics()=0
virtual size_t GetNumberOfLines() const =0
virtual bool GetClosestGlyphInfoAtCoordinate(double dx, double dy, skia::textlayout::Paragraph::GlyphInfo *glyphInfo) const =0
virtual bool DidExceedMaxLines()=0
virtual double GetHeight()=0
virtual bool GetLineMetricsAt(int lineNumber, skia::textlayout::LineMetrics *lineMetrics) const =0
virtual double GetMinIntrinsicWidth()=0
virtual double GetAlphabeticBaseline()=0
virtual int GetLineNumberAt(size_t utf16Offset) const =0
virtual bool Paint(flutter::DisplayListBuilder *builder, double x, double y)=0
virtual PositionWithAffinity GetGlyphPositionAtCoordinate(double dx, double dy)=0
virtual Range< size_t > GetWordBoundary(size_t offset)=0
virtual double GetMaxIntrinsicWidth()=0
virtual double GetIdeographicBaseline()=0
virtual std::vector< TextBox > GetRectsForPlaceholders()=0
virtual double GetMaxWidth()=0
VULKAN_HPP_DEFAULT_DISPATCH_LOADER_DYNAMIC_STORAGE auto & d
Definition: main.cc:19
struct MyStruct s
struct MyStruct a[10]
double y
double x
skia_private::AutoTArray< sk_sp< SkImageFilter > > filters TypedMatrix matrix TypedMatrix matrix SkScalar dx
Definition: SkRecords.h:208
#define T
Definition: precompiler.cc:65
SeparatedVector2 offset
PositionWithAffinity(size_t p, Affinity a)
Definition: paragraph.h:80
void Shift(T delta)
Definition: paragraph.h:103
bool operator==(const Range< T > &other) const
Definition: paragraph.h:97
TextBox(SkRect r, TextDirection d)
Definition: paragraph.h:87
TextDirection direction
Definition: paragraph.h:85