Skip to content

Commit

Permalink
Pass TextDirection to ParagraphBuilder. (flutter#4001)
Browse files Browse the repository at this point in the history
  • Loading branch information
Hixie authored Aug 23, 2017
1 parent 0062701 commit 8659114
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 40 deletions.
92 changes: 60 additions & 32 deletions lib/ui/text.dart
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ class FontWeight {
}
}

/// Whether to align text horizontally.
/// Whether and how to align text horizontally.
// The order of this enum must match the order of the values in RenderStyleConstants.h's ETextAlign.
enum TextAlign {
/// Align the text on the left edge of the container.
left,
Expand All @@ -95,9 +96,22 @@ enum TextAlign {
/// Stretch lines of text that end with a soft line break to fill the width of
/// the container.
///
/// Lines that end with hard line breaks are left-aligned.
// TODO(ianh): for rtl, this should presumably change to start-aligned.
/// Lines that end with hard line breaks are aligned towards the [start] edge.
justify,

/// Align the text on the leading edge of the container.
///
/// For left-to-right text ([TextDirection.ltr]), this is the left edge.
///
/// For right-to-left text ([TextDirection.rtr]), this is the right edge.
start,

/// Align the text on the trailing edge of the container.
///
/// For left-to-right text ([TextDirection.ltr]), this is the right edge.
///
/// For right-to-left text ([TextDirection.rtr]), this is the left edge.
end,
}

/// A horizontal line used for aligning text.
Expand All @@ -106,7 +120,7 @@ enum TextBaseline {
alphabetic,

// The horizontal line used to align ideographic characters.
ideographic
ideographic,
}

/// A linear decoration to draw near the text.
Expand Down Expand Up @@ -385,6 +399,7 @@ class TextStyle {
// - Element 4: The value of |maxLines|.
//
Int32List _encodeParagraphStyle(TextAlign textAlign,
TextDirection textDirection,
FontWeight fontWeight,
FontStyle fontStyle,
int maxLines,
Expand All @@ -397,32 +412,36 @@ Int32List _encodeParagraphStyle(TextAlign textAlign,
result[0] |= 1 << 1;
result[1] = textAlign.index;
}
if (fontWeight != null) {
if (textDirection != null) {
result[0] |= 1 << 2;
result[2] = fontWeight.index;
result[2] = textDirection.index;
}
if (fontStyle != null) {
if (fontWeight != null) {
result[0] |= 1 << 3;
result[3] = fontStyle.index;
result[3] = fontWeight.index;
}
if (maxLines != null) {
if (fontStyle != null) {
result[0] |= 1 << 4;
result[4] = maxLines;
result[4] = fontStyle.index;
}
if (fontFamily != null) {
if (maxLines != null) {
result[0] |= 1 << 5;
result[5] = maxLines;
}
if (fontFamily != null) {
result[0] |= 1 << 6;
// Passed separately to native.
}
if (fontSize != null) {
result[0] |= 1 << 6;
result[0] |= 1 << 7;
// Passed separately to native.
}
if (lineHeight != null) {
result[0] |= 1 << 7;
result[0] |= 1 << 8;
// Passed separately to native.
}
if (ellipsis != null) {
result[0] |= 1 << 8;
result[0] |= 1 << 9;
// Passed separately to native.
}
return result;
Expand All @@ -439,6 +458,11 @@ class ParagraphStyle {
/// the ellipsis has been added.
// See: https://github.com/flutter/flutter/issues/9819
///
/// * `textDirection`: The directionality of the text, left-to-right (e.g.
/// Norwegian) or right-to-left (e.g. Hebrew). This controls the overall
/// directionality of the paragraph, as well as the meaning of
/// [TextAlign.start] and [TextAlign.end] in the `textAlign` field.
///
/// * `fontWeight`: The typeface thickness to use when painting the text
/// (e.g., bold).
///
Expand Down Expand Up @@ -471,6 +495,7 @@ class ParagraphStyle {
/// considered equivalent and turn off this behavior.
ParagraphStyle({
TextAlign textAlign,
TextDirection textDirection,
FontWeight fontWeight,
FontStyle fontStyle,
int maxLines,
Expand All @@ -479,6 +504,7 @@ class ParagraphStyle {
double lineHeight,
String ellipsis,
}) : _encoded = _encodeParagraphStyle(textAlign,
textDirection,
fontWeight,
fontStyle,
maxLines,
Expand All @@ -500,10 +526,10 @@ class ParagraphStyle {
bool operator ==(dynamic other) {
if (identical(this, other))
return true;
if (other is! ParagraphStyle)
if (other.runtimeType != runtimeType)
return false;
final ParagraphStyle typedOther = other;
if ( _fontFamily != typedOther._fontFamily ||
if (_fontFamily != typedOther._fontFamily ||
_fontSize != typedOther._fontSize ||
_lineHeight != typedOther._lineHeight ||
_ellipsis != typedOther._ellipsis)
Expand All @@ -515,23 +541,25 @@ class ParagraphStyle {
return true;
}

int get hashCode => hashValues(hashList(_encoded), _lineHeight, _ellipsis);
int get hashCode => hashValues(hashList(_encoded), _fontFamily, _fontSize, _lineHeight, _ellipsis);

String toString() {
return '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"}, '
'maxLines: ${ _encoded[0] & 0x10 == 0x10 ? _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"}, '
'ellipsis: ${ _encoded[0] & 0x100 == 0x100 ? "\"$_ellipsis\"" : "unspecified"}'
return '$runtimeType('
'textAlign: ${ _encoded[0] & 0x002 == 0x002 ? TextAlign.values[_encoded[1]] : "unspecified"}, '
'textDirection: ${ _encoded[0] & 0x004 == 0x004 ? TextDirection.values[_encoded[2]] : "unspecified"}, '
'fontWeight: ${ _encoded[0] & 0x008 == 0x008 ? FontWeight.values[_encoded[3]] : "unspecified"}, '
'fontStyle: ${ _encoded[0] & 0x010 == 0x010 ? FontStyle.values[_encoded[4]] : "unspecified"}, '
'maxLines: ${ _encoded[0] & 0x020 == 0x020 ? _encoded[5] : "unspecified"}, '
'fontFamily: ${ _encoded[0] & 0x040 == 0x040 ? _fontFamily : "unspecified"}, '
'fontSize: ${ _encoded[0] & 0x080 == 0x080 ? _fontSize : "unspecified"}, '
'lineHeight: ${ _encoded[0] & 0x100 == 0x100 ? "${_lineHeight}x" : "unspecified"}, '
'ellipsis: ${ _encoded[0] & 0x200 == 0x200 ? "\"$_ellipsis\"" : "unspecified"}'
')';
}
}

/// A direction in which text flows.
// The order of this enum must match the order of the values in TextDirection.h's TextDirection.
enum TextDirection {
/// The text flows from right to left (e.g. Arabic, Hebrew).
rtl,
Expand All @@ -547,15 +575,15 @@ class TextBox {
this.top,
this.right,
this.bottom,
this.direction
this.direction,
);

TextBox._(
this.left,
this.top,
this.right,
this.bottom,
int directionIndex
int directionIndex,
) : direction = TextDirection.values[directionIndex];

/// The left edge of the text box, irrespective of direction.
Expand Down Expand Up @@ -589,7 +617,7 @@ class TextBox {
bool operator ==(dynamic other) {
if (identical(this, other))
return true;
if (other is! TextBox)
if (other.runtimeType != runtimeType)
return false;
final TextBox typedOther = other;
return typedOther.left == left
Expand Down Expand Up @@ -622,7 +650,7 @@ enum TextAffinity {
///
/// For example, if the offset of the text position is a line break, the
/// position represents the start of the second line.
downstream
downstream,
}

/// A visual position in a string of text.
Expand Down Expand Up @@ -675,15 +703,15 @@ class ParagraphConstraints {
final double width;

bool operator ==(dynamic other) {
if (other is! ParagraphConstraints)
if (other.runtimeType != runtimeType)
return false;
final ParagraphConstraints typedOther = other;
return typedOther.width == width;
}

int get hashCode => width.hashCode;

String toString() => 'ParagraphConstraints(width: $width)';
String toString() => '$runtimeType(width: $width)';
}

/// A paragraph of text.
Expand Down
19 changes: 12 additions & 7 deletions lib/ui/text/paragraph_builder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,17 @@ const int tsHeightMask = 1 << tsHeightIndex;
// ParagraphStyle

const int psTextAlignIndex = 1;
const int psFontWeightIndex = 2;
const int psFontStyleIndex = 3;
const int psMaxLinesIndex = 4;
const int psFontFamilyIndex = 5;
const int psFontSizeIndex = 6;
const int psLineHeightIndex = 7;
const int psEllipsisIndex = 8;
const int psTextDirectionIndex = 2;
const int psFontWeightIndex = 3;
const int psFontStyleIndex = 4;
const int psMaxLinesIndex = 5;
const int psFontFamilyIndex = 6;
const int psFontSizeIndex = 7;
const int psLineHeightIndex = 8;
const int psEllipsisIndex = 9;

const int psTextAlignMask = 1 << psTextAlignIndex;
const int psTextDirectionMask = 1 << psTextDirectionIndex;
const int psFontWeightMask = 1 << psFontWeightIndex;
const int psFontStyleMask = 1 << psFontStyleIndex;
const int psMaxLinesMask = 1 << psMaxLinesIndex;
Expand Down Expand Up @@ -113,6 +115,9 @@ PassRefPtr<RenderStyle> decodeParagraphStyle(
if (mask & psTextAlignMask)
style->setTextAlign(static_cast<ETextAlign>(encoded[psTextAlignIndex]));

if (mask & psTextDirectionMask)
style->setDirection(static_cast<TextDirection>(encoded[psTextDirectionIndex]));

if (mask & (psFontWeightMask | psFontStyleMask | psFontFamilyMask |
psFontSizeMask)) {
FontDescription fontDescription = style->fontDescription();
Expand Down
2 changes: 1 addition & 1 deletion sky/engine/core/rendering/style/RenderStyleConstants.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ enum EWhiteSpace {
NORMAL, PRE, PRE_WRAP, PRE_LINE, NOWRAP, KHTML_NOWRAP
};

// The order of this enum must match the order of the text align values in CSSValueKeywords.in.
// The order of this enum must match the order of the values in dart:ui's TextAlign.
enum ETextAlign {
LEFT, RIGHT, CENTER, JUSTIFY, TASTART, TAEND,
};
Expand Down
1 change: 1 addition & 0 deletions sky/engine/platform/text/TextDirection.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

namespace blink {

// The order of this enum must match the order of the values in dart:ui's TextDirection.
enum TextDirection { RTL, LTR };

inline bool isLeftToRightDirection(TextDirection direction) { return direction == LTR; }
Expand Down

0 comments on commit 8659114

Please sign in to comment.