Flutter Engine
The Flutter Engine
paragraph.cpp
Go to the documentation of this file.
1// Copyright 2013 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
6#include "../export.h"
7#include "DartTypes.h"
8#include "TextStyle.h"
10
11using namespace skia::textlayout;
12
14 delete paragraph;
15}
16
18 return paragraph->getMaxWidth();
19}
20
22 return paragraph->getHeight();
23}
24
26 return paragraph->getLongestLine();
27}
28
30 return paragraph->getMinIntrinsicWidth();
31}
32
34 return paragraph->getMaxIntrinsicWidth();
35}
36
38 return paragraph->getAlphabeticBaseline();
39}
40
42 return paragraph->getIdeographicBaseline();
43}
44
46 return paragraph->didExceedMaxLines();
47}
48
50 paragraph->layout(width);
51}
52
56 Affinity* outAffinity) {
57 auto position = paragraph->getGlyphPositionAtCoordinate(offsetX, offsetY);
58 if (outAffinity) {
59 *outAffinity = position.affinity;
60 }
61 return position.position;
62}
63
65 Paragraph* paragraph,
68 // Out parameters:
69 SkRect* graphemeLayoutBounds, // 1 SkRect
70 size_t* graphemeCodeUnitRange, // 2 size_ts: [start, end]
71 bool* booleanFlags) { // 1 boolean: isLTR
72 Paragraph::GlyphInfo glyphInfo;
73 if (!paragraph->getClosestUTF16GlyphInfoAt(offsetX, offsetY, &glyphInfo)) {
74 return false;
75 }
76 // This is more verbose than memcpying the whole struct but ideally we don't
77 // want to depend on the exact memory layout of the struct.
78 std::memcpy(graphemeLayoutBounds, &glyphInfo.fGraphemeLayoutBounds,
79 sizeof(SkRect));
80 std::memcpy(graphemeCodeUnitRange, &glyphInfo.fGraphemeClusterTextRange,
81 2 * sizeof(size_t));
82 booleanFlags[0] =
84 return true;
85}
86
88 Paragraph* paragraph,
89 size_t index,
90 // Out parameters:
91 SkRect* graphemeLayoutBounds, // 1 SkRect
92 size_t* graphemeCodeUnitRange, // 2 size_ts: [start, end]
93 bool* booleanFlags) { // 1 boolean: isLTR
94 Paragraph::GlyphInfo glyphInfo;
95 if (!paragraph->getGlyphInfoAtUTF16Offset(index, &glyphInfo)) {
96 return false;
97 }
98 std::memcpy(graphemeLayoutBounds, &glyphInfo.fGraphemeLayoutBounds,
99 sizeof(SkRect));
100 std::memcpy(graphemeCodeUnitRange, &glyphInfo.fGraphemeClusterTextRange,
101 2 * sizeof(size_t));
102 booleanFlags[0] =
104 return true;
105}
106
108 Paragraph* paragraph,
109 unsigned int position,
110 int32_t* outRange // Two `int32_t`s, start and end
111) {
112 auto range = paragraph->getWordBoundary(position);
113 outRange[0] = range.start;
114 outRange[1] = range.end;
115}
116
118 return paragraph->lineNumber();
119}
120
122 size_t characterIndex) {
123 return paragraph->getLineNumberAtUTF16Offset(characterIndex);
124}
125
127 size_t lineNumber) {
128 auto metrics = new LineMetrics();
129 if (paragraph->getLineMetricsAt(lineNumber, metrics)) {
130 return metrics;
131 } else {
132 delete metrics;
133 return nullptr;
134 }
135}
136
138 std::vector<TextBox> boxes;
139};
140
142 delete list;
143}
144
146 return list->boxes.size();
147}
148
150 size_t index,
151 SkRect* outRect) {
152 const auto& box = list->boxes[index];
153 *outRect = box.rect;
154 return box.direction;
155}
156
158 Paragraph* paragraph,
159 int start,
160 int end,
161 RectHeightStyle heightStyle,
162 RectWidthStyle widthStyle) {
163 return new TextBoxList{
164 paragraph->getRectsForRange(start, end, heightStyle, widthStyle)};
165}
166
168 Paragraph* paragraph) {
169 return new TextBoxList{paragraph->getRectsForPlaceholders()};
170}
171
172// Returns a list of the code points that were unable to be rendered with the
173// selected fonts. The list is deduplicated, so each code point in the output
174// is unique.
175// If `nullptr` is passed in for `outCodePoints`, we simply return the count
176// of the code points.
177// Note: This must be called after the paragraph has been laid out at least
178// once in order to get valid data.
180 SkUnichar* outCodePoints,
181 int outLength) {
182 if (!outCodePoints) {
183 return paragraph->unresolvedCodepoints().size();
184 }
185 int outIndex = 0;
186 for (SkUnichar character : paragraph->unresolvedCodepoints()) {
187 if (outIndex < outLength) {
188 outCodePoints[outIndex] = character;
189 outIndex++;
190 } else {
191 break;
192 }
193 }
194 return outIndex;
195}
int32_t SkUnichar
Definition: SkTypes.h:175
virtual std::vector< TextBox > getRectsForRange(unsigned start, unsigned end, RectHeightStyle rectHeightStyle, RectWidthStyle rectWidthStyle)=0
virtual bool getGlyphInfoAtUTF16Offset(size_t codeUnitIndex, GlyphInfo *glyphInfo)=0
virtual SkRange< size_t > getWordBoundary(unsigned offset)=0
SkScalar getAlphabeticBaseline()
Definition: Paragraph.h:34
virtual std::unordered_set< SkUnichar > unresolvedCodepoints()=0
virtual std::vector< TextBox > getRectsForPlaceholders()=0
virtual int getLineNumberAtUTF16Offset(size_t codeUnitIndex)=0
virtual bool getClosestUTF16GlyphInfoAt(SkScalar dx, SkScalar dy, GlyphInfo *glyphInfo)=0
virtual size_t lineNumber()=0
virtual PositionWithAffinity getGlyphPositionAtCoordinate(SkScalar dx, SkScalar dy)=0
SkScalar getMinIntrinsicWidth()
Definition: Paragraph.h:30
SkScalar getIdeographicBaseline()
Definition: Paragraph.h:36
virtual void layout(SkScalar width)=0
SkScalar getMaxIntrinsicWidth()
Definition: Paragraph.h:32
virtual bool getLineMetricsAt(int lineNumber, LineMetrics *lineMetrics) const =0
float SkScalar
Definition: extension.cpp:12
glong glong end
SKWASM_EXPORT SkScalar paragraph_getMaxIntrinsicWidth(Paragraph *paragraph)
Definition: paragraph.cpp:33
SKWASM_EXPORT SkScalar paragraph_getIdeographicBaseline(Paragraph *paragraph)
Definition: paragraph.cpp:41
SKWASM_EXPORT void paragraph_layout(Paragraph *paragraph, SkScalar width)
Definition: paragraph.cpp:49
SKWASM_EXPORT int32_t paragraph_getPositionForOffset(Paragraph *paragraph, SkScalar offsetX, SkScalar offsetY, Affinity *outAffinity)
Definition: paragraph.cpp:53
SKWASM_EXPORT SkScalar paragraph_getMinIntrinsicWidth(Paragraph *paragraph)
Definition: paragraph.cpp:29
SKWASM_EXPORT LineMetrics * paragraph_getLineMetricsAtIndex(Paragraph *paragraph, size_t lineNumber)
Definition: paragraph.cpp:126
SKWASM_EXPORT bool paragraph_getDidExceedMaxLines(Paragraph *paragraph)
Definition: paragraph.cpp:45
SKWASM_EXPORT SkScalar paragraph_getAlphabeticBaseline(Paragraph *paragraph)
Definition: paragraph.cpp:37
SKWASM_EXPORT bool paragraph_getClosestGlyphInfoAtCoordinate(Paragraph *paragraph, SkScalar offsetX, SkScalar offsetY, SkRect *graphemeLayoutBounds, size_t *graphemeCodeUnitRange, bool *booleanFlags)
Definition: paragraph.cpp:64
SKWASM_EXPORT size_t textBoxList_getLength(TextBoxList *list)
Definition: paragraph.cpp:145
SKWASM_EXPORT SkScalar paragraph_getHeight(Paragraph *paragraph)
Definition: paragraph.cpp:21
SKWASM_EXPORT bool paragraph_getGlyphInfoAt(Paragraph *paragraph, size_t index, SkRect *graphemeLayoutBounds, size_t *graphemeCodeUnitRange, bool *booleanFlags)
Definition: paragraph.cpp:87
SKWASM_EXPORT TextBoxList * paragraph_getBoxesForPlaceholders(Paragraph *paragraph)
Definition: paragraph.cpp:167
SKWASM_EXPORT SkScalar paragraph_getLongestLine(Paragraph *paragraph)
Definition: paragraph.cpp:25
SKWASM_EXPORT int paragraph_getUnresolvedCodePoints(Paragraph *paragraph, SkUnichar *outCodePoints, int outLength)
Definition: paragraph.cpp:179
SKWASM_EXPORT int paragraph_getLineNumberAt(Paragraph *paragraph, size_t characterIndex)
Definition: paragraph.cpp:121
SKWASM_EXPORT size_t paragraph_getLineCount(Paragraph *paragraph)
Definition: paragraph.cpp:117
SKWASM_EXPORT SkScalar paragraph_getWidth(Paragraph *paragraph)
Definition: paragraph.cpp:17
SKWASM_EXPORT TextDirection textBoxList_getBoxAtIndex(TextBoxList *list, size_t index, SkRect *outRect)
Definition: paragraph.cpp:149
SKWASM_EXPORT TextBoxList * paragraph_getBoxesForRange(Paragraph *paragraph, int start, int end, RectHeightStyle heightStyle, RectWidthStyle widthStyle)
Definition: paragraph.cpp:157
SKWASM_EXPORT void textBoxList_dispose(TextBoxList *list)
Definition: paragraph.cpp:141
SKWASM_EXPORT void paragraph_getWordBoundary(Paragraph *paragraph, unsigned int position, int32_t *outRange)
Definition: paragraph.cpp:107
SKWASM_EXPORT void paragraph_dispose(Paragraph *paragraph)
Definition: paragraph.cpp:13
SkScalar offsetX
SkScalar offsetY
int32_t width
std::vector< TextBox > boxes
Definition: paragraph.cpp:138
#define SKWASM_EXPORT
Definition: export.h:10