Implement ellipsizing of text in the engine (flutter/engine#3056)

See https://github.com/flutter/flutter/issues/4478
This commit is contained in:
Jason Simmons
2016-09-26 11:30:11 -07:00
committed by GitHub
parent ca79a2a30f
commit 692aabce8f
2 changed files with 38 additions and 12 deletions

View File

@@ -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"}'
')';
}
}

View File

@@ -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<Paragraph> 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<Paragraph> ParagraphBuilder::build(tonic::Int32List& encoded,
if (mask & psLineHeightMask)
style->setLineHeight(Length(lineHeight * 100.0, Percent));
if (mask & psTextOverflowMask) {
style->setTextOverflow(
static_cast<TextOverflow>(encoded[psTextOverflowIndex]));
}
m_renderParagraph->setStyle(style.release());
}