From 692aabce8f372e3e7aeb06ac06bbc1b77a6686ac Mon Sep 17 00:00:00 2001 From: Jason Simmons Date: Mon, 26 Sep 2016 11:30:11 -0700 Subject: [PATCH] Implement ellipsizing of text in the engine (flutter/engine#3056) See https://github.com/flutter/flutter/issues/4478 --- engine/src/flutter/lib/ui/text.dart | 35 ++++++++++++++----- .../flutter/lib/ui/text/paragraph_builder.cc | 15 +++++--- 2 files changed, 38 insertions(+), 12 deletions(-) diff --git a/engine/src/flutter/lib/ui/text.dart b/engine/src/flutter/lib/ui/text.dart index cb0f7f7093..5403f4c5f5 100644 --- a/engine/src/flutter/lib/ui/text.dart +++ b/engine/src/flutter/lib/ui/text.dart @@ -93,6 +93,15 @@ enum TextAlign { center } +/// How to handle text that overflows its bounds +enum TextOverflow { + /// Default + clip, + + /// Render as a single line that is ellipsized if it is too wide to fit + ellipsis +} + /// A horizontal line used for aligning text enum TextBaseline { // The horizontal line used to align the bottom of glyphs for alphabetic characters @@ -375,13 +384,16 @@ class TextStyle { // // - Element 3: The enum index of the |fontStyle|. // +// - Element 4: The enum index of the |textOverflow|. +// Int32List _encodeParagraphStyle(TextAlign textAlign, FontWeight fontWeight, FontStyle fontStyle, + TextOverflow textOverflow, String fontFamily, double fontSize, double lineHeight) { - Int32List result = new Int32List(4); + Int32List result = new Int32List(5); if (textAlign != null) { result[0] |= 1 << 1; result[1] = textAlign.index; @@ -394,18 +406,22 @@ Int32List _encodeParagraphStyle(TextAlign textAlign, result[0] |= 1 << 3; result[3] = fontStyle.index; } - if (fontFamily != null) { + if (textOverflow != null) { result[0] |= 1 << 4; - // Passed separately to native. + result[4] = textOverflow.index; } - if (fontSize != null) { + if (fontFamily != null) { result[0] |= 1 << 5; // Passed separately to native. } - if (lineHeight != null) { + if (fontSize != null) { result[0] |= 1 << 6; // Passed separately to native. } + if (lineHeight != null) { + result[0] |= 1 << 7; + // Passed separately to native. + } return result; } @@ -423,12 +439,14 @@ class ParagraphStyle { TextAlign textAlign, FontWeight fontWeight, FontStyle fontStyle, + TextOverflow textOverflow, String fontFamily, double fontSize, double lineHeight }) : _encoded = _encodeParagraphStyle(textAlign, fontWeight, fontStyle, + textOverflow, fontFamily, fontSize, lineHeight), @@ -465,9 +483,10 @@ class ParagraphStyle { 'textAlign: ${ _encoded[0] & 0x02 == 0x02 ? TextAlign.values[_encoded[1]] : "unspecified"}, ' 'fontWeight: ${ _encoded[0] & 0x04 == 0x04 ? FontWeight.values[_encoded[2]] : "unspecified"}, ' 'fontStyle: ${ _encoded[0] & 0x08 == 0x08 ? FontStyle.values[_encoded[3]] : "unspecified"}, ' - 'fontFamily: ${ _encoded[0] & 0x10 == 0x10 ? _fontFamily : "unspecified"}, ' - 'fontSize: ${ _encoded[0] & 0x20 == 0x20 ? _fontSize : "unspecified"}, ' - 'lineHeight: ${ _encoded[0] & 0x40 == 0x40 ? "${_lineHeight}x" : "unspecified"}' + 'textOverflow: ${ _encoded[0] & 0x10 == 0x10 ? TextOverflow.values[_encoded[4]] : "unspecified"}, ' + 'fontFamily: ${ _encoded[0] & 0x20 == 0x20 ? _fontFamily : "unspecified"}, ' + 'fontSize: ${ _encoded[0] & 0x40 == 0x40 ? _fontSize : "unspecified"}, ' + 'lineHeight: ${ _encoded[0] & 0x80 == 0x80 ? "${_lineHeight}x" : "unspecified"}' ')'; } } diff --git a/engine/src/flutter/lib/ui/text/paragraph_builder.cc b/engine/src/flutter/lib/ui/text/paragraph_builder.cc index 1fa29e1679..94de11d2fb 100644 --- a/engine/src/flutter/lib/ui/text/paragraph_builder.cc +++ b/engine/src/flutter/lib/ui/text/paragraph_builder.cc @@ -95,13 +95,15 @@ const int tsHeightMask = 1 << tsHeightIndex; const int psTextAlignIndex = 1; const int psFontWeightIndex = 2; const int psFontStyleIndex = 3; -const int psFontFamilyIndex = 4; -const int psFontSizeIndex = 5; -const int psLineHeightIndex = 6; +const int psTextOverflowIndex = 4; +const int psFontFamilyIndex = 5; +const int psFontSizeIndex = 6; +const int psLineHeightIndex = 7; const int psTextAlignMask = 1 << psTextAlignIndex; const int psFontWeightMask = 1 << psFontWeightIndex; const int psFontStyleMask = 1 << psFontStyleIndex; +const int psTextOverflowMask = 1 << psTextOverflowIndex; const int psFontFamilyMask = 1 << psFontFamilyIndex; const int psFontSizeMask = 1 << psFontSizeIndex; const int psLineHeightMask = 1 << psLineHeightIndex; @@ -241,7 +243,7 @@ ftl::RefPtr ParagraphBuilder::build(tonic::Int32List& encoded, const std::string& fontFamily, double fontSize, double lineHeight) { - FTL_DCHECK(encoded.num_elements() == 4); + FTL_DCHECK(encoded.num_elements() == 5); int32_t mask = encoded[0]; if (mask) { @@ -282,6 +284,11 @@ ftl::RefPtr ParagraphBuilder::build(tonic::Int32List& encoded, if (mask & psLineHeightMask) style->setLineHeight(Length(lineHeight * 100.0, Percent)); + if (mask & psTextOverflowMask) { + style->setTextOverflow( + static_cast(encoded[psTextOverflowIndex])); + } + m_renderParagraph->setStyle(style.release()); }