-
Notifications
You must be signed in to change notification settings - Fork 6k
remove usage of Dart_New for paragraph/libtxt #16837
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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]. | ||
|
|
@@ -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; | ||
|
|
@@ -1914,6 +1890,20 @@ class Paragraph extends NativeFieldWrapperClass2 { | |
| void layout(ParagraphConstraints constraints) => _layout(constraints.width); | ||
| void _layout(double width) native 'Paragraph_layout'; | ||
|
|
||
| List<TextBox> _decodeTextBoxes(Float32List encoded) { | ||
| final List<TextBox> boxes = List<TextBox>(encoded[0].toInt()); | ||
| for (int index = 0; index < boxes.length; index += 1) { | ||
| final int position = (index * 5) + 1; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Avoid the cost of the multiply by advancing the index by 5 on each iteration
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
| boxes[index] = TextBox.fromLTRBD( | ||
| encoded[position + 0], | ||
| encoded[position + 1], | ||
| encoded[position + 2], | ||
| encoded[position + 3], | ||
| TextDirection.values[encoded[position + 4].toInt()], | ||
| ); | ||
| } | ||
| return boxes; | ||
| } | ||
| /// Returns a list of text boxes that enclose the given text range. | ||
| /// | ||
| /// The [boxHeightStyle] and [boxWidthStyle] parameters allow customization | ||
|
|
@@ -1930,17 +1920,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) { | ||
|
|
@@ -1987,7 +1981,26 @@ 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 List<LineMetrics> metrics = List<LineMetrics>(encoded[0].toInt()); | ||
| for (int index = 0; index < metrics.length; index += 1) { | ||
| final int position = (index * 5) + 1; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Advance the position by 9 instead of 5 for LineMetrics
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done, and added some tests that didn't exist that would have caught this. |
||
| metrics[index] = LineMetrics( | ||
| hardBreak: encoded[position + 0] != 0, | ||
| ascent: encoded[position + 1], | ||
| descent: encoded[position + 2], | ||
| unscaledAscent: encoded[position + 3], | ||
| height: encoded[position + 4], | ||
| width: encoded[position + 5], | ||
| left: encoded[position + 6], | ||
| baseline: encoded[position + 7], | ||
| lineNumber: encoded[position + 8].toInt(), | ||
| ); | ||
| } | ||
| return metrics; | ||
| } | ||
| Float64List _computeLineMetrics() native 'Paragraph_computeLineMetrics'; | ||
| } | ||
|
|
||
| /// Builds a [Paragraph] containing text with the given styling information. | ||
|
|
@@ -2195,7 +2208,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. | ||
|
|
||
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -94,28 +94,42 @@ 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. | ||
| auto count = boxes.size() * 5; | ||
| tonic::Float32List result( | ||
| Dart_NewTypedData(Dart_TypedData_kFloat32, 1 + count)); | ||
| result[0] = count; | ||
| for (unsigned long i = 0; i < boxes.size(); i++) { | ||
| auto position = (i * 5) + 1; | ||
| const txt::Paragraph::TextBox& box = boxes[i]; | ||
| result[position + 0] = box.rect.fLeft; | ||
| result[position + 1] = box.rect.fTop; | ||
| result[position + 2] = box.rect.fRight; | ||
| result[position + 3] = box.rect.fBottom; | ||
| result[position + 4] = 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) { | ||
|
|
@@ -152,14 +166,34 @@ 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: | ||
| // First value is the number of values. | ||
| // Then there are boxes.size() groups of 9 which are the line metrics | ||
| // properties | ||
| auto count = metrics.size() * 9; | ||
| tonic::Float64List result( | ||
| Dart_NewTypedData(Dart_TypedData_kFloat64, 1 + count)); | ||
| result[0] = count; | ||
| for (unsigned long i = 0; i < metrics.size(); i++) { | ||
| auto position = (i * 9) + 1; | ||
| const txt::LineMetrics& line = metrics[i]; | ||
| result[position + 0] = static_cast<float>(line.hard_break); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. cast this to
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
| result[position + 1] = line.ascent; | ||
| result[position + 2] = line.descent; | ||
| result[position + 3] = 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 + 4] = round(line.ascent + line.descent); | ||
| result[position + 5] = line.width; | ||
| result[position + 6] = line.left; | ||
| result[position + 7] = line.baseline; | ||
| result[position + 8] = static_cast<double>(line.line_number); | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The count of text boxes is redundant with the length of the encoded Float32List
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done