Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
184 changes: 105 additions & 79 deletions lib/ui/text.dart

Large diffs are not rendered by default.

38 changes: 16 additions & 22 deletions lib/ui/text/paragraph_builder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,26 +32,27 @@ namespace {

// TextStyle

const int tsLeadingDistributionIndex = 0;
const int tsColorIndex = 1;
const int tsTextDecorationIndex = 2;
const int tsTextDecorationColorIndex = 3;
const int tsTextDecorationStyleIndex = 4;
const int tsFontWeightIndex = 5;
const int tsFontStyleIndex = 6;
const int tsTextBaselineIndex = 7;
const int tsLeadingDistributionIndex = 8;
const int tsTextDecorationThicknessIndex = 9;
const int tsFontFamilyIndex = 10;
const int tsFontSizeIndex = 11;
const int tsLetterSpacingIndex = 12;
const int tsWordSpacingIndex = 13;
const int tsHeightIndex = 14;
const int tsLocaleIndex = 15;
const int tsBackgroundIndex = 16;
const int tsForegroundIndex = 17;
const int tsTextShadowsIndex = 18;
const int tsFontFeaturesIndex = 19;
const int tsTextDecorationThicknessIndex = 8;
const int tsFontFamilyIndex = 9;
const int tsFontSizeIndex = 10;
const int tsLetterSpacingIndex = 11;
const int tsWordSpacingIndex = 12;
const int tsHeightIndex = 13;
const int tsLocaleIndex = 14;
const int tsBackgroundIndex = 15;
const int tsForegroundIndex = 16;
const int tsTextShadowsIndex = 17;
const int tsFontFeaturesIndex = 18;

const int tsLeadingDistributionMask = 1 << tsLeadingDistributionIndex;
const int tsColorMask = 1 << tsColorIndex;
const int tsTextDecorationMask = 1 << tsTextDecorationIndex;
const int tsTextDecorationColorMask = 1 << tsTextDecorationColorIndex;
Expand All @@ -60,7 +61,6 @@ const int tsTextDecorationThicknessMask = 1 << tsTextDecorationThicknessIndex;
const int tsFontWeightMask = 1 << tsFontWeightIndex;
const int tsFontStyleMask = 1 << tsFontStyleIndex;
const int tsTextBaselineMask = 1 << tsTextBaselineIndex;
const int tsLeadingDistributionMask = 1 << tsLeadingDistributionIndex;
const int tsFontFamilyMask = 1 << tsFontFamilyIndex;
const int tsFontSizeMask = 1 << tsFontSizeIndex;
const int tsLetterSpacingMask = 1 << tsLetterSpacingIndex;
Expand Down Expand Up @@ -201,10 +201,8 @@ void decodeStrut(Dart_Handle strut_data,
paragraph_style.strut_font_style =
static_cast<txt::FontStyle>(uint8_data[byte_count++]);
}
if (mask & sLeadingDistributionMask) {
paragraph_style.strut_has_leading_distribution_override = true;
paragraph_style.strut_half_leading = uint8_data[byte_count++];
}

paragraph_style.strut_half_leading = mask & sLeadingDistributionMask;

std::vector<float> float_data;
float_data.resize((byte_data.length_in_bytes() - byte_count) / 4);
Expand Down Expand Up @@ -389,6 +387,7 @@ void ParagraphBuilder::pushStyle(tonic::Int32List& encoded,
// explicitly given.
txt::TextStyle style = m_paragraphBuilder->PeekStyle();

style.half_leading = mask & tsLeadingDistributionMask;
// Only change the style property from the previous value if a new explicitly
// set value is available
if (mask & tsColorMask) {
Expand Down Expand Up @@ -418,11 +417,6 @@ void ParagraphBuilder::pushStyle(tonic::Int32List& encoded,
// property wasn't wired up either.
}

style.has_leading_distribution_override = mask & tsLeadingDistributionMask;
if (mask & tsLeadingDistributionMask) {
style.half_leading = encoded[tsLeadingDistributionIndex];
}

if (mask & (tsFontWeightMask | tsFontStyleMask | tsFontSizeMask |
tsLetterSpacingMask | tsWordSpacingMask)) {
if (mask & tsFontWeightMask) {
Expand Down
3 changes: 2 additions & 1 deletion lib/web_ui/lib/src/engine/canvaskit/text.dart
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,8 @@ class CkParagraphStyle implements ui.ParagraphStyle {
}

if (textHeightBehavior != null) {
properties.textHeightBehavior = textHeightBehavior.encode();
properties.textHeightBehavior = (textHeightBehavior.applyHeightToFirstAscent ? 0 : 1 << 0)
| (textHeightBehavior.applyHeightToLastDescent ? 0 : 1 << 1);
}

if (ellipsis != null) {
Expand Down
10 changes: 0 additions & 10 deletions lib/web_ui/lib/src/ui/text.dart
Original file line number Diff line number Diff line change
Expand Up @@ -214,20 +214,10 @@ class TextHeightBehavior {
this.applyHeightToLastDescent = true,
this.leadingDistribution = TextLeadingDistribution.proportional,
});
TextHeightBehavior.fromEncoded(int encoded)
: applyHeightToFirstAscent = (encoded & 0x1) == 0,
applyHeightToLastDescent = (encoded & 0x2) == 0,
leadingDistribution = TextLeadingDistribution.values[encoded >> 2];
final bool applyHeightToFirstAscent;
final bool applyHeightToLastDescent;
final TextLeadingDistribution leadingDistribution;

int encode() {
return (applyHeightToFirstAscent ? 0 : 1 << 0)
| (applyHeightToLastDescent ? 0 : 1 << 1)
| (leadingDistribution.index << 2);
}

@override
bool operator ==(Object other) {
if (other.runtimeType != runtimeType)
Expand Down
2 changes: 1 addition & 1 deletion lib/web_ui/test/canvaskit/canvaskit_api_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1273,7 +1273,7 @@ void _paragraphTests() {
props.textAlign = canvasKit.TextAlign.Center;
props.textDirection = canvasKit.TextDirection.RTL;
props.heightMultiplier = 3;
props.textHeightBehavior = ui.TextHeightBehavior().encode();
props.textHeightBehavior = 0;
props.maxLines = 4;
props.ellipsis = '___';
props.textStyle = SkTextStyleProperties()
Expand Down
16 changes: 0 additions & 16 deletions testing/dart/text_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -107,22 +107,6 @@ void main() {
expect(behavior4.leadingDistribution, equals(TextLeadingDistribution.even));
});

test('encode works', () {
expect(behavior0.encode(), equals(0));
expect(behavior1.encode(), equals(3));
expect(behavior2.encode(), equals(1));
expect(behavior3.encode(), equals(2));
expect(behavior4.encode(), equals(6));
});

test('decode works', () {
expect(TextHeightBehavior.fromEncoded(0), equals(behavior0));
expect(TextHeightBehavior.fromEncoded(3), equals(behavior1));
expect(TextHeightBehavior.fromEncoded(1), equals(behavior2));
expect(TextHeightBehavior.fromEncoded(2), equals(behavior3));
expect(TextHeightBehavior.fromEncoded(6), equals(behavior4));
});

test('toString works', () {
expect(behavior0.toString(), equals('TextHeightBehavior(applyHeightToFirstAscent: true, applyHeightToLastDescent: true, leadingDistribution: TextLeadingDistribution.proportional)'));
expect(behavior1.toString(), equals('TextHeightBehavior(applyHeightToFirstAscent: false, applyHeightToLastDescent: false, leadingDistribution: TextLeadingDistribution.proportional)'));
Expand Down
1 change: 1 addition & 0 deletions third_party/txt/src/skia/paragraph_builder_skia.cc
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ skt::TextStyle TxtToSkia(const TextStyle& txt) {
skia.setWordSpacing(SkDoubleToScalar(txt.word_spacing));
skia.setHeight(SkDoubleToScalar(txt.height));
skia.setHeightOverride(txt.has_height_override);
skia.setHalfLeading(txt.half_leading);

skia.setLocale(SkString(txt.locale.c_str()));
if (txt.has_background) {
Expand Down
8 changes: 0 additions & 8 deletions third_party/txt/src/txt/paragraph_style.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,6 @@ enum class TextDirection {
// the line will use the default font metric provided ascent/descent and
// ParagraphStyle.height or TextStyle.height will not take effect.
//
// kEvenLeading determines how the leading is distributed over and under the
// text. When true, half of the leading is added to the top of the text and the
// other half is added to the bottom of the text. Otherwise, instead of
// distributing the space evenly, it's distributed proportionally to the font's
// ascent/descent ratio.
//
// The default behavior is kAll where height adjustments are enabled for all
// lines.
//
Expand All @@ -66,7 +60,6 @@ enum TextHeightBehavior {
kDisableFirstAscent = 0x1,
kDisableLastDescent = 0x2,
kDisableAll = 0x1 | 0x2,
kEvenLeading = 0x1 << 2,
};

class ParagraphStyle {
Expand All @@ -92,7 +85,6 @@ class ParagraphStyle {
double strut_height = 1;
bool strut_has_height_override = false;
bool strut_half_leading = false;
bool strut_has_leading_distribution_override = false;
double strut_leading = -1; // Negative to use font's default leading. [0,inf)
// to use custom leading as a ratio of font size.
bool force_strut_height = false;
Expand Down
17 changes: 3 additions & 14 deletions third_party/txt/src/txt/paragraph_txt.cc
Original file line number Diff line number Diff line change
Expand Up @@ -566,14 +566,8 @@ void ParagraphTxt::ComputeStrut(StrutMetrics* strut, SkFont& font) {
: (paragraph_style_.strut_leading *
paragraph_style_.strut_font_size);

const bool half_leading_enabled =
paragraph_style_.strut_has_leading_distribution_override
? paragraph_style_.strut_half_leading
: paragraph_style_.text_height_behavior &
TextHeightBehavior::kEvenLeading;

const double available_height =
half_leading_enabled ? metrics_height : strut_height;
paragraph_style_.strut_half_leading ? metrics_height : strut_height;

strut->ascent =
(-strut_metrics.fAscent / metrics_height) * available_height;
Expand Down Expand Up @@ -1213,11 +1207,6 @@ void ParagraphTxt::UpdateLineMetrics(const SkFontMetrics& metrics,
const double blob_height = style.has_height_override
? style.height * style.font_size
: metrics_font_height + metrics.fLeading;
const bool half_leading_enabled =
style.has_leading_distribution_override
? style.half_leading
: paragraph_style_.text_height_behavior &
TextHeightBehavior::kEvenLeading;

// Scale the ascent and descent such that the sum of ascent and
// descent is `style.height * style.font_size`.
Expand Down Expand Up @@ -1266,14 +1255,14 @@ void ParagraphTxt::UpdateLineMetrics(const SkFontMetrics& metrics,
// a sane, consistent, and reasonable "blob_height" to be specified,
// though it breaks with what is done by any of the platforms above.
const bool shouldNormalizeFont =
style.has_height_override && !half_leading_enabled;
style.has_height_override && !style.half_leading;
const double font_height =
shouldNormalizeFont ? style.font_size : metrics_font_height;

// Reserve the outermost vertical space we want to distribute evenly over
// and under the text ("half-leading").
double leading;
if (half_leading_enabled) {
if (style.half_leading) {
leading = blob_height - font_height;
} else {
leading = style.has_height_override ? 0.0 : metrics.fLeading;
Expand Down
3 changes: 0 additions & 3 deletions third_party/txt/src/txt/text_style.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,6 @@ bool TextStyle::equals(const TextStyle& other) const {
return false;
if (has_height_override != other.has_height_override)
return false;
if (has_leading_distribution_override !=
other.has_leading_distribution_override)
return false;
if (half_leading != other.half_leading)
return false;
if (locale != other.locale)
Expand Down
3 changes: 0 additions & 3 deletions third_party/txt/src/txt/text_style.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,6 @@ class TextStyle {
FontStyle font_style = FontStyle::normal;
TextBaseline text_baseline = TextBaseline::kAlphabetic;
bool half_leading = false;
// whether TextStyle.half_leading should be applied or we should defer to
// ParagraphStyle.half_leading.
bool has_leading_distribution_override = false;
// An ordered list of fonts in order of priority. The first font is more
// highly preferred than the last font.
std::vector<std::string> font_families;
Expand Down
Loading