Fixing line metrics (baseline) - de-coupled from ICU CL
Bug: skia:10347
Change-Id: I8a10f4873d88f1ecf4698cedb27efe031c7eaa51
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/297720
Commit-Queue: Julia Lavrova <jlavrova@google.com>
Reviewed-by: Ben Wagner <bungeman@google.com>
diff --git a/modules/skparagraph/tests/SkParagraphTest.cpp b/modules/skparagraph/tests/SkParagraphTest.cpp
index 9a70087..c325ee5 100644
--- a/modules/skparagraph/tests/SkParagraphTest.cpp
+++ b/modules/skparagraph/tests/SkParagraphTest.cpp
@@ -5299,3 +5299,83 @@
SkASSERT(nearlyEqual(SK_ScalarNaN, SK_ScalarNegativeInfinity) == false);
SkASSERT(nearlyEqual(SK_ScalarNaN, SK_ScalarNaN) == false);
};
+
+DEF_TEST(SkParagraph_LineMetrics, reporter) {
+
+ sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
+ if (!fontCollection->fontsFound()) return;
+
+ TestCanvas canvas("SkParagraph_LineMetrics.png");
+
+ const char* text = "One line of text\n";
+ const size_t len = strlen(text);
+
+ ParagraphStyle paragraph_style;
+ paragraph_style.turnHintingOff();
+ ParagraphBuilderImpl builder(paragraph_style, fontCollection);
+
+ TextStyle text_style;
+ text_style.setFontFamilies({SkString("Roboto")});
+ text_style.setColor(SK_ColorBLACK);
+
+ text_style.setFontSize(8);
+ builder.pushStyle(text_style);
+ builder.addText(text, len);
+ builder.pop();
+
+ text_style.setFontSize(12);
+ builder.pushStyle(text_style);
+ builder.addText(text, len);
+ builder.pop();
+
+ text_style.setFontSize(18);
+ builder.pushStyle(text_style);
+ builder.addText(text, len);
+ builder.pop();
+
+ text_style.setFontSize(30);
+ builder.pushStyle(text_style);
+ builder.addText(text, len - 1); // Skip the last \n
+ builder.pop();
+
+ auto paragraph = builder.Build();
+ paragraph->layout(TestCanvasWidth);
+
+ std::vector<LineMetrics> metrics;
+ paragraph->getLineMetrics(metrics);
+
+ SkDEBUGCODE(auto impl = static_cast<ParagraphImpl*>(paragraph.get());)
+ SkASSERT(metrics.size() == impl->lines().size());
+ for (size_t i = 0; i < metrics.size(); ++i) {
+ SkDEBUGCODE(auto& line = impl->lines()[i];)
+ SkDEBUGCODE(auto baseline = metrics[i].fBaseline;)
+ SkDEBUGCODE(auto top = line.offset().fY;)
+ SkDEBUGCODE(auto bottom = line.offset().fY + line.height();)
+ SkASSERT( baseline > top && baseline <= bottom);
+ }
+
+ paragraph->paint(canvas.get(), 0, 0);
+ auto rects = paragraph->getRectsForRange(0, len * 4, RectHeightStyle::kMax, RectWidthStyle::kTight);
+
+ SkPaint red;
+ red.setColor(SK_ColorRED);
+ red.setStyle(SkPaint::kStroke_Style);
+ red.setAntiAlias(true);
+ red.setStrokeWidth(1);
+
+ for (auto& rect : rects) {
+ canvas.get()->drawRect(rect.rect, red);
+ }
+
+ SkPaint green;
+ green.setColor(SK_ColorGREEN);
+ green.setStyle(SkPaint::kStroke_Style);
+ green.setAntiAlias(true);
+ green.setStrokeWidth(1);
+ for (auto& metric : metrics) {
+ auto x0 = 0.0;
+ auto x1 = metric.fWidth;
+ auto y = metric.fBaseline;
+ canvas.get()->drawLine(x0, y, x1, y, green);
+ }
+};