Include placeholder widths in minIntrinsicWidth
Bug: skia:11179
Change-Id: Ic69c5a54b142584eddb28351c326952f4dd77376
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/355976
Reviewed-by: Ben Wagner <bungeman@google.com>
Commit-Queue: Julia Lavrova <jlavrova@google.com>
diff --git a/modules/skparagraph/src/TextWrapper.cpp b/modules/skparagraph/src/TextWrapper.cpp
index 8e0846e..d43c5b8 100644
--- a/modules/skparagraph/src/TextWrapper.cpp
+++ b/modules/skparagraph/src/TextWrapper.cpp
@@ -98,12 +98,13 @@
if (cluster->run()->isPlaceholder()) {
if (!fClusters.empty()) {
- // Placeholder ends the previous word
+ // Placeholder ends the previous word (placeholders are ignored in trimming)
fMinIntrinsicWidth = std::max(fMinIntrinsicWidth, getClustersTrimmedWidth());
fWords.extend(fClusters);
}
- // It also creates a separate word; it does not count in fMinIntrinsicWidth
+ // Placeholder is separate word and its width now is counted in minIntrinsicWidth
+ fMinIntrinsicWidth = std::max(fMinIntrinsicWidth, cluster->width());
fWords.extend(cluster);
} else {
fClusters.extend(cluster);
@@ -369,9 +370,10 @@
lastWordLength = 0;
} else if (cluster->run()->isPlaceholder()) {
// Placeholder ends the previous word and creates a separate one
- // it does not count in fMinIntrinsicWidth
- softLineMaxIntrinsicWidth += cluster->width();
fMinIntrinsicWidth = std::max(fMinIntrinsicWidth, lastWordLength);
+ // Placeholder width now counts in fMinIntrinsicWidth
+ softLineMaxIntrinsicWidth += cluster->width();
+ fMinIntrinsicWidth = std::max(fMinIntrinsicWidth, cluster->width());
lastWordLength = 0;
} else {
// Nothing out of ordinary - just add this cluster to the word and to the line
diff --git a/modules/skparagraph/tests/SkParagraphTest.cpp b/modules/skparagraph/tests/SkParagraphTest.cpp
index e54ad08..b6b5a9c 100644
--- a/modules/skparagraph/tests/SkParagraphTest.cpp
+++ b/modules/skparagraph/tests/SkParagraphTest.cpp
@@ -5774,3 +5774,47 @@
paint("Loooooooooooooooooooooooooooooooooooong text");
paint("");
}
+
+DEF_TEST(SkParagraph_PlaceholderWidth, reporter) {
+
+ sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
+ if (!fontCollection->fontsFound()) return;
+
+ TestCanvas canvas("SkParagraph_PlaceholderWidth.png");
+
+ const char* text = "1 23 456 7890"; // 13 * 50 = 650
+
+ ParagraphStyle paragraph_style;
+ TextStyle text_style;
+ text_style.setColor(SK_ColorBLACK);
+ text_style.setFontSize(50);
+ text_style.setFontFamilies({SkString("Ahem")});
+ PlaceholderStyle placeholder(300, 50, PlaceholderAlignment::kBaseline, TextBaseline::kAlphabetic, 0);
+
+ auto draw = [&](bool withPlaceholder) {
+ ParagraphBuilderImpl builder(paragraph_style, fontCollection);
+ builder.pushStyle(text_style);
+ builder.addText(text);
+ if (withPlaceholder) {
+ SkPaint red;
+ red.setColor(SK_ColorRED);
+ text_style.setBackgroundColor(red);
+ builder.pushStyle(text_style);
+ builder.addPlaceholder(placeholder);
+ }
+ builder.addText(text);
+
+ auto paragraph = builder.Build();
+ paragraph->layout(950);
+ paragraph->paint(canvas.get(), 0, 0);
+ canvas.get()->translate(0, paragraph->getHeight());
+ return paragraph->getMinIntrinsicWidth();
+ };
+
+ auto len1 = draw(true);
+ auto len2 = draw(false);
+
+ // placeholder: 300 "78901": 250
+ REPORTER_ASSERT(reporter, SkScalarNearlyEqual(len1, 300.0f));
+ REPORTER_ASSERT(reporter, SkScalarNearlyEqual(len2, 250.0f));
+}