From befe39324ed5502807608496fd822fa889c5b931 Mon Sep 17 00:00:00 2001 From: Matt Perry Date: Wed, 11 May 2016 14:53:51 -0400 Subject: [PATCH] Expose a Paragraph.getWordBoundary method to dart. (#2675) * Expose a Paragraph.getWordBoundary method to dart. Used by text selection to select a word. --- sky/engine/core/dart/text.dart | 7 +++++++ sky/engine/core/text/Paragraph.cpp | 25 +++++++++++++++++++++++++ sky/engine/core/text/Paragraph.h | 1 + 3 files changed, 33 insertions(+) diff --git a/sky/engine/core/dart/text.dart b/sky/engine/core/dart/text.dart index 8bec09c35..1ac119f27 100644 --- a/sky/engine/core/dart/text.dart +++ b/sky/engine/core/dart/text.dart @@ -674,6 +674,13 @@ abstract class Paragraph extends NativeFieldWrapperClass2 { return new TextPosition(offset: encoded[0], affinity: TextAffinity.values[encoded[1]]); } List _getPositionForOffset(Offset offset) native "Paragraph_getPositionForOffset"; + + /// Returns the [start, end] of the word at the given offset. Characters not + /// part of a word, such as spaces, symbols, and punctuation, have word breaks + /// on both sides. In such cases, this method will return [offset, offset+1]. + /// Word boundaries are defined more precisely in Unicode Standard Annex #29 + /// http://www.unicode.org/reports/tr29/#Word_Boundaries + List getWordBoundary(int offset) native "Paragraph_getWordBoundary"; } /// Builds a [Paragraph] containing text with the given styling information. diff --git a/sky/engine/core/text/Paragraph.cpp b/sky/engine/core/text/Paragraph.cpp index b5dda70ad..092189755 100644 --- a/sky/engine/core/text/Paragraph.cpp +++ b/sky/engine/core/text/Paragraph.cpp @@ -11,6 +11,7 @@ #include "sky/engine/core/rendering/style/RenderStyle.h" #include "sky/engine/platform/fonts/FontCache.h" #include "sky/engine/platform/graphics/GraphicsContext.h" +#include "sky/engine/platform/text/TextBoundaries.h" #include "sky/engine/public/platform/Platform.h" #include "sky/engine/tonic/dart_args.h" #include "sky/engine/tonic/dart_binding_macros.h" @@ -29,6 +30,7 @@ IMPLEMENT_WRAPPERTYPEINFO(ui, Paragraph); V(Paragraph, alphabeticBaseline) \ V(Paragraph, ideographicBaseline) \ V(Paragraph, layout) \ + V(Paragraph, getWordBoundary) \ V(Paragraph, getRectsForRange) \ V(Paragraph, getPositionForOffset) @@ -153,4 +155,27 @@ Dart_Handle Paragraph::getPositionForOffset(const Offset& offset) { return result; } +Dart_Handle Paragraph::getWordBoundary(unsigned offset) { + String text; + int start, end; + + for (RenderObject* object = m_renderView.get(); object; object = object->nextInPreOrder()) { + if (!object->isText()) + continue; + RenderText* renderText = toRenderText(object); + text.append(renderText->text()); + } + + TextBreakIterator* it = wordBreakIterator(text, 0, text.length()); + end = it->following(offset); + if (end < 0) + end = it->last(); + start = it->previous(); + + Dart_Handle result = Dart_NewList(2); + Dart_ListSetAt(result, 0, ToDart(start)); + Dart_ListSetAt(result, 1, ToDart(end)); + return result; +} + } // namespace blink diff --git a/sky/engine/core/text/Paragraph.h b/sky/engine/core/text/Paragraph.h index 3aa3f9bca..06893e510 100644 --- a/sky/engine/core/text/Paragraph.h +++ b/sky/engine/core/text/Paragraph.h @@ -37,6 +37,7 @@ public: std::vector getRectsForRange(unsigned start, unsigned end); Dart_Handle getPositionForOffset(const Offset& offset); + Dart_Handle getWordBoundary(unsigned offset); RenderView* renderView() const { return m_renderView.get(); } -- GitLab