Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit d911ece

Browse files
committed
add tests
1 parent a46a7ed commit d911ece

5 files changed

Lines changed: 113 additions & 0 deletions

File tree

lib/web_ui/lib/src/engine/compositor/text.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class SkParagraphStyle implements ui.ParagraphStyle {
2525
String fontFamily,
2626
double fontSize,
2727
double height,
28+
int boundingLineHeightBehavior,
2829
ui.FontWeight fontWeight,
2930
ui.FontStyle fontStyle,
3031
ui.StrutStyle strutStyle,
@@ -38,6 +39,7 @@ class SkParagraphStyle implements ui.ParagraphStyle {
3839
fontFamily,
3940
fontSize,
4041
height,
42+
boundingLineHeightBehavior,
4143
fontWeight,
4244
fontStyle,
4345
ellipsis,
@@ -85,6 +87,7 @@ class SkParagraphStyle implements ui.ParagraphStyle {
8587
String fontFamily,
8688
double fontSize,
8789
double height,
90+
int boundingLineHeightBehavior,
8891
ui.FontWeight fontWeight,
8992
ui.FontStyle fontStyle,
9093
String ellipsis,
@@ -129,6 +132,10 @@ class SkParagraphStyle implements ui.ParagraphStyle {
129132
skParagraphStyle['heightMultiplier'] = height;
130133
}
131134

135+
if (boundingLineHeightBehavior != null) {
136+
skParagraphStyle['boundingLineHeightBehavior'] = boundingLineHeightBehavior;
137+
}
138+
132139
if (maxLines != null) {
133140
skParagraphStyle['maxLines'] = maxLines;
134141
}

lib/web_ui/lib/src/engine/text/paragraph.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,7 @@ class EngineParagraphStyle implements ui.ParagraphStyle {
456456
String fontFamily,
457457
double fontSize,
458458
double height,
459+
int boundingLineHeightBehavior,
459460
ui.FontWeight fontWeight,
460461
ui.FontStyle fontStyle,
461462
ui.StrutStyle strutStyle,
@@ -469,6 +470,7 @@ class EngineParagraphStyle implements ui.ParagraphStyle {
469470
_fontFamily = fontFamily,
470471
_fontSize = fontSize,
471472
_height = height,
473+
_boundingLineHeightBehavior = boundingLineHeightBehavior,
472474
// TODO(b/128317744): add support for strut style.
473475
_strutStyle = strutStyle,
474476
_ellipsis = ellipsis,
@@ -482,6 +484,7 @@ class EngineParagraphStyle implements ui.ParagraphStyle {
482484
final String _fontFamily;
483485
final double _fontSize;
484486
final double _height;
487+
final int _boundingLineHeightBehavior;
485488
final EngineStrutStyle _strutStyle;
486489
final String _ellipsis;
487490
final ui.Locale _locale;
@@ -535,6 +538,7 @@ class EngineParagraphStyle implements ui.ParagraphStyle {
535538
_fontFamily == typedOther._fontFamily ||
536539
_fontSize == typedOther._fontSize ||
537540
_height == typedOther._height ||
541+
_boundingLineHeightBehavior == typedOther._boundingLineHeightBehavior ||
538542
_ellipsis == typedOther._ellipsis ||
539543
_locale == typedOther._locale;
540544
}
@@ -555,6 +559,7 @@ class EngineParagraphStyle implements ui.ParagraphStyle {
555559
'fontFamily: ${_fontFamily ?? "unspecified"}, '
556560
'fontSize: ${_fontSize != null ? _fontSize.toStringAsFixed(1) : "unspecified"}, '
557561
'height: ${_height != null ? "${_height.toStringAsFixed(1)}x" : "unspecified"}, '
562+
'boundingLineHeightBehavior: ${_boundingLineHeightBehavior ?? "unspecified"}, '
558563
'ellipsis: ${_ellipsis != null ? "\"$_ellipsis\"" : "unspecified"}, '
559564
'locale: ${_locale ?? "unspecified"}'
560565
')';

lib/web_ui/lib/src/ui/text.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,7 @@ abstract class ParagraphStyle {
584584
String fontFamily,
585585
double fontSize,
586586
double height,
587+
int boundingLineHeightBehavior,
587588
FontWeight fontWeight,
588589
FontStyle fontStyle,
589590
StrutStyle strutStyle,
@@ -598,6 +599,7 @@ abstract class ParagraphStyle {
598599
fontFamily: fontFamily,
599600
fontSize: fontSize,
600601
height: height,
602+
boundingLineHeightBehavior: boundingLineHeightBehavior,
601603
fontWeight: fontWeight,
602604
fontStyle: fontStyle,
603605
strutStyle: strutStyle,
@@ -612,6 +614,7 @@ abstract class ParagraphStyle {
612614
fontFamily: fontFamily,
613615
fontSize: fontSize,
614616
height: height,
617+
boundingLineHeightBehavior: boundingLineHeightBehavior,
615618
fontWeight: fontWeight,
616619
fontStyle: fontStyle,
617620
strutStyle: strutStyle,

third_party/txt/src/txt/paragraph_txt.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ class ParagraphTxt : public Paragraph {
162162
FRIEND_TEST(ParagraphTest, FontFeaturesParagraph);
163163
FRIEND_TEST(ParagraphTest, GetGlyphPositionAtCoordinateSegfault);
164164
FRIEND_TEST(ParagraphTest, KhmerLineBreaker);
165+
FRIEND_TEST(ParagraphTest, BoundingLineHeightBehaviorRectsParagraph);
165166

166167
// Starting data to layout.
167168
std::vector<uint16_t> text_;

third_party/txt/tests/paragraph_unittests.cc

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5846,4 +5846,101 @@ TEST_F(ParagraphTest, KhmerLineBreaker) {
58465846
ASSERT_TRUE(Snapshot());
58475847
}
58485848

5849+
TEST_F(ParagraphTest, BoundingLineHeightBehaviorRectsParagraph) {
5850+
// clang-format off
5851+
const char* text =
5852+
"line1\nline2\nline3";
5853+
// clang-format on
5854+
auto icu_text = icu::UnicodeString::fromUTF8(text);
5855+
std::u16string u16_text(icu_text.getBuffer(),
5856+
icu_text.getBuffer() + icu_text.length());
5857+
5858+
txt::ParagraphStyle paragraph_style;
5859+
paragraph_style.bounding_line_height_behavior =
5860+
txt::BoundaryLineHeightBehavior::kDisableFirst &
5861+
txt::BoundaryLineHeightBehavior::kDisableLast;
5862+
5863+
txt::ParagraphBuilderTxt builder(paragraph_style, GetTestFontCollection());
5864+
5865+
txt::TextStyle text_style;
5866+
text_style.color = SK_ColorBLACK;
5867+
text_style.font_families = std::vector<std::string>(1, "Roboto");
5868+
text_style.font_size = 30;
5869+
text_style.height = 5;
5870+
text_style.has_height_override = true;
5871+
builder.PushStyle(text_style);
5872+
builder.AddText(u16_text);
5873+
5874+
builder.Pop();
5875+
5876+
auto paragraph = BuildParagraph(builder);
5877+
paragraph->Layout(GetTestCanvasWidth() - 300);
5878+
5879+
paragraph->Paint(GetCanvas(), 0, 0);
5880+
5881+
for (size_t i = 0; i < u16_text.length(); i++) {
5882+
ASSERT_EQ(paragraph->text_[i], u16_text[i]);
5883+
}
5884+
5885+
ASSERT_EQ(paragraph->records_.size(), 3ull);
5886+
5887+
SkPaint paint;
5888+
paint.setStyle(SkPaint::kStroke_Style);
5889+
paint.setAntiAlias(true);
5890+
paint.setStrokeWidth(1);
5891+
5892+
// Tests for GetRectsForRange()
5893+
Paragraph::RectHeightStyle rect_height_style =
5894+
Paragraph::RectHeightStyle::kMax;
5895+
Paragraph::RectWidthStyle rect_width_style =
5896+
Paragraph::RectWidthStyle::kTight;
5897+
paint.setColor(SK_ColorRED);
5898+
std::vector<txt::Paragraph::TextBox> boxes =
5899+
paragraph->GetRectsForRange(0, 0, rect_height_style, rect_width_style);
5900+
for (size_t i = 0; i < boxes.size(); ++i) {
5901+
GetCanvas()->drawRect(boxes[i].rect, paint);
5902+
}
5903+
EXPECT_EQ(boxes.size(), 0ull);
5904+
5905+
// First line. Shorter due to disabled height modifications on first ascent.
5906+
boxes =
5907+
paragraph->GetRectsForRange(0, 3, rect_height_style, rect_width_style);
5908+
for (size_t i = 0; i < boxes.size(); ++i) {
5909+
GetCanvas()->drawRect(boxes[i].rect, paint);
5910+
}
5911+
EXPECT_EQ(boxes.size(), 1ull);
5912+
EXPECT_FLOAT_EQ(boxes[0].rect.left(), 0);
5913+
EXPECT_FLOAT_EQ(boxes[0].rect.right(), 31.117188);
5914+
EXPECT_FLOAT_EQ(boxes[0].rect.top(), -0.08203125);
5915+
EXPECT_FLOAT_EQ(boxes[0].rect.bottom(), 59);
5916+
EXPECT_FLOAT_EQ(boxes[0].rect.bottom() - boxes[0].rect.top(), 59.082031);
5917+
5918+
// Second line. Normal.
5919+
boxes =
5920+
paragraph->GetRectsForRange(6, 10, rect_height_style, rect_width_style);
5921+
for (size_t i = 0; i < boxes.size(); ++i) {
5922+
GetCanvas()->drawRect(boxes[i].rect, paint);
5923+
}
5924+
EXPECT_EQ(boxes.size(), 1ull);
5925+
EXPECT_FLOAT_EQ(boxes[0].rect.left(), 0);
5926+
EXPECT_FLOAT_EQ(boxes[0].rect.right(), 47.011719);
5927+
EXPECT_FLOAT_EQ(boxes[0].rect.top(), 59);
5928+
EXPECT_FLOAT_EQ(boxes[0].rect.bottom(), 209);
5929+
EXPECT_FLOAT_EQ(boxes[0].rect.bottom() - boxes[0].rect.top(), 150);
5930+
5931+
boxes =
5932+
paragraph->GetRectsForRange(12, 17, rect_height_style, rect_width_style);
5933+
for (size_t i = 0; i < boxes.size(); ++i) {
5934+
GetCanvas()->drawRect(boxes[i].rect, paint);
5935+
}
5936+
EXPECT_EQ(boxes.size(), 1ull);
5937+
EXPECT_FLOAT_EQ(boxes[0].rect.left(), 0);
5938+
EXPECT_FLOAT_EQ(boxes[0].rect.right(), 63.859375);
5939+
EXPECT_FLOAT_EQ(boxes[0].rect.top(), 208.92578);
5940+
EXPECT_FLOAT_EQ(boxes[0].rect.bottom(), 335);
5941+
EXPECT_FLOAT_EQ(boxes[0].rect.bottom() - boxes[0].rect.top(), 126.07422);
5942+
5943+
ASSERT_TRUE(Snapshot());
5944+
}
5945+
58495946
} // namespace txt

0 commit comments

Comments
 (0)