-
Notifications
You must be signed in to change notification settings - Fork 6k
Expose more methods on ui.Paragraph: lines
#46125
Changes from 6 commits
4325bd2
79aea2e
f1fb5ed
cf02536
7f1329e
e7c5cbf
006c363
9e34175
f88d96f
67a8a57
05b0982
9a9c9a6
b505da8
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 | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -40,7 +40,7 @@ class CanvasParagraph implements ui.Paragraph { | |||||||||||||||
| final EngineParagraphStyle paragraphStyle; | ||||||||||||||||
|
|
||||||||||||||||
| /// The full textual content of the paragraph. | ||||||||||||||||
| late String plainText; | ||||||||||||||||
| final String plainText; | ||||||||||||||||
|
|
||||||||||||||||
| /// Whether this paragraph can be drawn on a bitmap canvas. | ||||||||||||||||
| /// | ||||||||||||||||
|
|
@@ -240,6 +240,32 @@ class CanvasParagraph implements ui.Paragraph { | |||||||||||||||
| return lines.map((ParagraphLine line) => line.lineMetrics).toList(); | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| @override | ||||||||||||||||
| EngineLineMetrics? getLineMetricsAt(int lineNumber) { | ||||||||||||||||
| return 0 <= lineNumber && lineNumber < lines.length | ||||||||||||||||
| ? lines[lineNumber].lineMetrics | ||||||||||||||||
| : null; | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| @override | ||||||||||||||||
| int get numberOfLines => lines.length; | ||||||||||||||||
|
|
||||||||||||||||
| @override | ||||||||||||||||
| int? getLineNumberAt(int codeUnitOffset) => _findLine(codeUnitOffset, 0, lines.length); | ||||||||||||||||
|
|
||||||||||||||||
| int? _findLine(int codeUnitOffset, int startLine, int endLine) { | ||||||||||||||||
| if (endLine <= startLine || codeUnitOffset < lines[startLine].startIndex || lines[endLine - 1].endIndex <= codeUnitOffset) { | ||||||||||||||||
| return null; | ||||||||||||||||
| } | ||||||||||||||||
| if (endLine == startLine + 1) { | ||||||||||||||||
| return startLine; | ||||||||||||||||
| } | ||||||||||||||||
| // endLine >= startLine + 2 thus we have | ||||||||||||||||
| // startLine + 1 <= midIndex <= endLine - 1 | ||||||||||||||||
| final int midIndex = (startLine + endLine) ~/ 2; | ||||||||||||||||
| return _findLine(codeUnitOffset, midIndex, endLine) ?? _findLine(codeUnitOffset, startLine, midIndex); | ||||||||||||||||
| } | ||||||||||||||||
|
Comment on lines
+251
to
+262
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. There's code here that does the same thing: engine/lib/web_ui/lib/src/engine/text/canvas_paragraph.dart Lines 226 to 232 in 85e1dd9
Yours seems to be more performant (because binary search), so it would be nice to use it in |
||||||||||||||||
|
|
||||||||||||||||
| bool _disposed = false; | ||||||||||||||||
|
|
||||||||||||||||
| @override | ||||||||||||||||
|
|
||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -74,14 +74,13 @@ SKWASM_EXPORT size_t paragraph_getLineCount(Paragraph* paragraph) { | |
|
|
||
| SKWASM_EXPORT int paragraph_getLineNumberAt(Paragraph* paragraph, | ||
| size_t characterIndex) { | ||
| return paragraph->getLineNumberAt(characterIndex); | ||
| return paragraph->getLineNumberAtUTF16Offset(characterIndex); | ||
| } | ||
|
|
||
| SKWASM_EXPORT LineMetrics* paragraph_getLineMetricsAtIndex(Paragraph* paragraph, | ||
| size_t index) { | ||
| size_t lineNumber) { | ||
| auto metrics = new LineMetrics(); | ||
| paragraph->getLineMetricsAt(index, metrics); | ||
| return metrics; | ||
| return paragraph->getLineMetricsAt(lineNumber, metrics) ? metrics : nullptr; | ||
|
||
| } | ||
|
|
||
| struct TextBoxList { | ||
|
|
||
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.
Use a
std::arrayforargumentsand callsize()here