Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions ci/licenses_golden/licenses_flutter
Original file line number Diff line number Diff line change
Expand Up @@ -354,13 +354,11 @@ FILE: ../../../flutter/lib/ui/text/asset_manager_font_provider.cc
FILE: ../../../flutter/lib/ui/text/asset_manager_font_provider.h
FILE: ../../../flutter/lib/ui/text/font_collection.cc
FILE: ../../../flutter/lib/ui/text/font_collection.h
FILE: ../../../flutter/lib/ui/text/line_metrics.cc
FILE: ../../../flutter/lib/ui/text/line_metrics.h
FILE: ../../../flutter/lib/ui/text/paragraph.cc
FILE: ../../../flutter/lib/ui/text/paragraph.h
FILE: ../../../flutter/lib/ui/text/paragraph_builder.cc
FILE: ../../../flutter/lib/ui/text/paragraph_builder.h
FILE: ../../../flutter/lib/ui/text/text_box.cc
FILE: ../../../flutter/lib/ui/text/text_box.h
FILE: ../../../flutter/lib/ui/ui.dart
FILE: ../../../flutter/lib/ui/ui_dart_state.cc
Expand Down
2 changes: 0 additions & 2 deletions lib/ui/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,11 @@ source_set("ui") {
"text/asset_manager_font_provider.h",
"text/font_collection.cc",
"text/font_collection.h",
"text/line_metrics.cc",
"text/line_metrics.h",
"text/paragraph.cc",
"text/paragraph.h",
"text/paragraph_builder.cc",
"text/paragraph_builder.h",
"text/text_box.cc",
"text/text_box.h",
"ui_dart_state.cc",
"ui_dart_state.h",
Expand Down
111 changes: 82 additions & 29 deletions lib/ui/text.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1306,16 +1306,6 @@ class TextBox {
this.direction,
);

@pragma('vm:entry-point')
// ignore: unused_element
TextBox._(
this.left,
this.top,
this.right,
this.bottom,
int directionIndex,
) : direction = TextDirection.values[directionIndex];

/// The left edge of the text box, irrespective of direction.
///
/// To get the leading edge (which may depend on the [direction]), consider [start].
Expand Down Expand Up @@ -1764,20 +1754,6 @@ class LineMetrics {
this.lineNumber,
});

@pragma('vm:entry-point')
// ignore: unused_element
LineMetrics._(
this.hardBreak,
this.ascent,
this.descent,
this.unscaledAscent,
this.height,
this.width,
this.left,
this.baseline,
this.lineNumber,
);

/// True if this line ends with an explicit line break (e.g. '\n') or is the end
/// of the paragraph. False otherwise.
final bool hardBreak;
Expand Down Expand Up @@ -1843,6 +1819,39 @@ class LineMetrics {
///
/// For example, the first line is line 0, second line is line 1.
final int lineNumber;

@override
bool operator ==(Object other) {
if (other.runtimeType != runtimeType) {
return false;
}
return other is LineMetrics
&& other.hardBreak == hardBreak
&& other.ascent == ascent
&& other.descent == descent
&& other.unscaledAscent == unscaledAscent
&& other.height == height
&& other.width == width
&& other.left == left
&& other.baseline == baseline
&& other.lineNumber == lineNumber;
}

@override
int get hashCode => hashValues(hardBreak, ascent, descent, unscaledAscent, height, width, left, baseline, lineNumber);

@override
String toString() {
return 'LineMetrics(hardBreak: $hardBreak, '
'ascent: $ascent, '
'descent: $descent, '
'unscaledAscent: $unscaledAscent, '
'height: $height, '
'width: $width, '
'left: $left, '
'baseline: $baseline, '
'lineNumber: $lineNumber)';
}
}

/// A paragraph of text.
Expand Down Expand Up @@ -1914,6 +1923,21 @@ class Paragraph extends NativeFieldWrapperClass2 {
void layout(ParagraphConstraints constraints) => _layout(constraints.width);
void _layout(double width) native 'Paragraph_layout';

List<TextBox> _decodeTextBoxes(Float32List encoded) {
final int count = encoded.length ~/ 5;
final List<TextBox> boxes = List<TextBox>(count);
int position = 0;
for (int index = 0; index < count; index += 1) {
boxes[index] = TextBox.fromLTRBD(
encoded[position++],
encoded[position++],
encoded[position++],
encoded[position++],
TextDirection.values[encoded[position++].toInt()],
);
}
return boxes;
}
/// Returns a list of text boxes that enclose the given text range.
///
/// The [boxHeightStyle] and [boxWidthStyle] parameters allow customization
Expand All @@ -1930,17 +1954,21 @@ class Paragraph extends NativeFieldWrapperClass2 {
List<TextBox> getBoxesForRange(int start, int end, {BoxHeightStyle boxHeightStyle = BoxHeightStyle.tight, BoxWidthStyle boxWidthStyle = BoxWidthStyle.tight}) {
assert(boxHeightStyle != null);
assert(boxWidthStyle != null);
return _getBoxesForRange(start, end, boxHeightStyle.index, boxWidthStyle.index);
return _decodeTextBoxes(_getBoxesForRange(start, end, boxHeightStyle.index, boxWidthStyle.index));
}
List<TextBox> _getBoxesForRange(int start, int end, int boxHeightStyle, int boxWidthStyle) native 'Paragraph_getRectsForRange';
// See paragraph.cc for the layout of this return value.
Float32List _getBoxesForRange(int start, int end, int boxHeightStyle, int boxWidthStyle) native 'Paragraph_getRectsForRange';

/// Returns a list of text boxes that enclose all placeholders in the paragraph.
///
/// The order of the boxes are in the same order as passed in through [addPlaceholder].
///
/// Coordinates of the [TextBox] are relative to the upper-left corner of the paragraph,
/// where positive y values indicate down.
List<TextBox> getBoxesForPlaceholders() native 'Paragraph_getRectsForPlaceholders';
List<TextBox> getBoxesForPlaceholders() {
return _decodeTextBoxes(_getBoxesForPlaceholders());
}
Float32List _getBoxesForPlaceholders() native 'Paragraph_getRectsForPlaceholders';

/// Returns the text position closest to the given offset.
TextPosition getPositionForOffset(Offset offset) {
Expand Down Expand Up @@ -1987,7 +2015,27 @@ class Paragraph extends NativeFieldWrapperClass2 {
///
/// This can potentially return a large amount of data, so it is not recommended
/// to repeatedly call this. Instead, cache the results.
List<LineMetrics> computeLineMetrics() native 'Paragraph_computeLineMetrics';
List<LineMetrics> computeLineMetrics() {
final Float64List encoded = _computeLineMetrics();
final int count = encoded.length ~/ 9;
int position = 0;
final List<LineMetrics> metrics = List<LineMetrics>(count);
for (int index = 0; index < metrics.length; index += 1) {
metrics[index] = LineMetrics(
hardBreak: encoded[position++] != 0,
ascent: encoded[position++],
descent: encoded[position++],
unscaledAscent: encoded[position++],
height: encoded[position++],
width: encoded[position++],
left: encoded[position++],
baseline: encoded[position++],
lineNumber: encoded[position++].toInt(),
);
}
return metrics;
}
Float64List _computeLineMetrics() native 'Paragraph_computeLineMetrics';
}

/// Builds a [Paragraph] containing text with the given styling information.
Expand Down Expand Up @@ -2195,7 +2243,12 @@ class ParagraphBuilder extends NativeFieldWrapperClass2 {
///
/// After calling this function, the paragraph builder object is invalid and
/// cannot be used further.
Paragraph build() native 'ParagraphBuilder_build';
Paragraph build() {
final Paragraph paragraph = Paragraph._();
_build(paragraph);
return paragraph;
}
void _build(Paragraph outParagraph) native 'ParagraphBuilder_build';
}

/// Loads a font from a buffer and makes it available for rendering text.
Expand Down
48 changes: 0 additions & 48 deletions lib/ui/text/line_metrics.cc

This file was deleted.

13 changes: 0 additions & 13 deletions lib/ui/text/line_metrics.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,4 @@ struct LineMetrics {

} // namespace flutter

namespace tonic {
template <>
struct DartConverter<flutter::LineMetrics> {
static Dart_Handle ToDart(const flutter::LineMetrics& val);
};

template <>
struct DartListFactory<flutter::LineMetrics> {
static Dart_Handle NewList(intptr_t length);
};

} // namespace tonic

#endif // FLUTTER_LIB_UI_TEXT_LINE_METRICS_H_
71 changes: 50 additions & 21 deletions lib/ui/text/paragraph.cc
Original file line number Diff line number Diff line change
Expand Up @@ -94,28 +94,40 @@ void Paragraph::paint(Canvas* canvas, double x, double y) {
m_paragraph->Paint(sk_canvas, x, y);
}

std::vector<TextBox> Paragraph::getRectsForRange(unsigned start,
unsigned end,
unsigned boxHeightStyle,
unsigned boxWidthStyle) {
std::vector<TextBox> result;
static tonic::Float32List EncodeTextBoxes(
const std::vector<txt::Paragraph::TextBox>& boxes) {
// Layout:
// First value is the number of values.
// Then there are boxes.size() groups of 5 which are LTRBD, where D is the
// text direction index.
tonic::Float32List result(
Dart_NewTypedData(Dart_TypedData_kFloat32, boxes.size() * 5));
unsigned long position = 0;
for (unsigned long i = 0; i < boxes.size(); i++) {
const txt::Paragraph::TextBox& box = boxes[i];
result[position++] = box.rect.fLeft;
result[position++] = box.rect.fTop;
result[position++] = box.rect.fRight;
result[position++] = box.rect.fBottom;
result[position++] = static_cast<float>(box.direction);
}
return result;
}

tonic::Float32List Paragraph::getRectsForRange(unsigned start,
unsigned end,
unsigned boxHeightStyle,
unsigned boxWidthStyle) {
std::vector<txt::Paragraph::TextBox> boxes = m_paragraph->GetRectsForRange(
start, end, static_cast<txt::Paragraph::RectHeightStyle>(boxHeightStyle),
static_cast<txt::Paragraph::RectWidthStyle>(boxWidthStyle));
for (const txt::Paragraph::TextBox& box : boxes) {
result.emplace_back(box.rect, static_cast<TextDirection>(box.direction));
}
return result;
return EncodeTextBoxes(boxes);
}

std::vector<TextBox> Paragraph::getRectsForPlaceholders() {
std::vector<TextBox> result;
tonic::Float32List Paragraph::getRectsForPlaceholders() {
std::vector<txt::Paragraph::TextBox> boxes =
m_paragraph->GetRectsForPlaceholders();
for (const txt::Paragraph::TextBox& box : boxes) {
result.emplace_back(box.rect, static_cast<TextDirection>(box.direction));
}
return result;
return EncodeTextBoxes(boxes);
}

Dart_Handle Paragraph::getPositionForOffset(double dx, double dy) {
Expand Down Expand Up @@ -152,14 +164,31 @@ Dart_Handle Paragraph::getLineBoundary(unsigned offset) {
return result;
}

std::vector<LineMetrics> Paragraph::computeLineMetrics() {
std::vector<LineMetrics> result;
tonic::Float64List Paragraph::computeLineMetrics() {
std::vector<txt::LineMetrics> metrics = m_paragraph->GetLineMetrics();
for (txt::LineMetrics& line : metrics) {
result.emplace_back(&line.hard_break, &line.ascent, &line.descent,
&line.unscaled_ascent, &line.height, &line.width,
&line.left, &line.baseline, &line.line_number);

// Layout:
// boxes.size() groups of 9 which are the line metrics
// properties
tonic::Float64List result(
Dart_NewTypedData(Dart_TypedData_kFloat64, metrics.size() * 9));
unsigned long position = 0;
for (unsigned long i = 0; i < metrics.size(); i++) {
const txt::LineMetrics& line = metrics[i];
result[position++] = static_cast<double>(line.hard_break);
result[position++] = line.ascent;
result[position++] = line.descent;
result[position++] = line.unscaled_ascent;
// We add then round to get the height. The
// definition of height here is different
// than the one in LibTxt.
result[position++] = round(line.ascent + line.descent);
result[position++] = line.width;
result[position++] = line.left;
result[position++] = line.baseline;
result[position++] = static_cast<double>(line.line_number);
}

return result;
}

Expand Down
19 changes: 10 additions & 9 deletions lib/ui/text/paragraph.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ class Paragraph : public RefCountedDartWrappable<Paragraph> {
FML_FRIEND_MAKE_REF_COUNTED(Paragraph);

public:
static fml::RefPtr<Paragraph> Create(
std::unique_ptr<txt::Paragraph> paragraph) {
return fml::MakeRefCounted<Paragraph>(std::move(paragraph));
static void Create(Dart_Handle paragraph_handle,
std::unique_ptr<txt::Paragraph> txt_paragraph) {
auto paragraph = fml::MakeRefCounted<Paragraph>(std::move(txt_paragraph));
paragraph->AssociateWithDartWrapper(paragraph_handle);
}

~Paragraph() override;
Expand All @@ -42,15 +43,15 @@ class Paragraph : public RefCountedDartWrappable<Paragraph> {
void layout(double width);
void paint(Canvas* canvas, double x, double y);

std::vector<TextBox> getRectsForRange(unsigned start,
unsigned end,
unsigned boxHeightStyle,
unsigned boxWidthStyle);
std::vector<TextBox> getRectsForPlaceholders();
tonic::Float32List getRectsForRange(unsigned start,
unsigned end,
unsigned boxHeightStyle,
unsigned boxWidthStyle);
tonic::Float32List getRectsForPlaceholders();
Dart_Handle getPositionForOffset(double dx, double dy);
Dart_Handle getWordBoundary(unsigned offset);
Dart_Handle getLineBoundary(unsigned offset);
std::vector<LineMetrics> computeLineMetrics();
tonic::Float64List computeLineMetrics();

size_t GetAllocationSize() override;

Expand Down
Loading