Fix TextHeightBehavior
Change-Id: I08148d2e942691c8d12070940bc2ee28855ffe59
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/288761
Reviewed-by: Ben Wagner <bungeman@google.com>
Commit-Queue: Julia Lavrova <jlavrova@google.com>
diff --git a/samplecode/SampleParagraph.cpp b/samplecode/SampleParagraph.cpp
index 908c662..6208ea7 100644
--- a/samplecode/SampleParagraph.cpp
+++ b/samplecode/SampleParagraph.cpp
@@ -2636,6 +2636,101 @@
typedef Sample INHERITED;
};
+class ParagraphView41 : public ParagraphView_Base {
+protected:
+ SkString name() override { return SkString("Paragraph41"); }
+
+ void onDrawContent(SkCanvas* canvas) override {
+
+ canvas->drawColor(SK_ColorWHITE);
+
+ auto fontCollection = sk_make_sp<FontCollection>();
+ fontCollection->setDefaultFontManager(SkFontMgr::RefDefault());
+ fontCollection->enableFontFallback();
+
+ SkPaint line;
+ line.setColor(SK_ColorRED);
+ line.setStyle(SkPaint::kStroke_Style);
+ line.setAntiAlias(true);
+ line.setStrokeWidth(1);
+
+ auto draw = [&](SkColor color, TextHeightBehavior thb) {
+ ParagraphStyle paragraph_style;
+ paragraph_style.setTextHeightBehavior(thb);
+ ParagraphBuilderImpl builder(paragraph_style, fontCollection);
+ TextStyle text_style;
+ text_style.setColor(SK_ColorBLACK);
+ SkPaint paint;
+ paint.setColor(color);
+ text_style.setBackgroundColor(paint);
+ text_style.setFontFamilies({SkString("Roboto")});
+ text_style.setFontSize(20);
+ text_style.setHeight(5);
+ text_style.setHeightOverride(true);
+ builder.pushStyle(text_style);
+ builder.addText("World domination is such an ugly phrase - I prefer to call it world optimisation");
+ auto paragraph = builder.Build();
+ paragraph->layout(width());
+ paragraph->paint(canvas, 0, 0);
+ canvas->drawLine(0, paragraph->getHeight(), paragraph->getMaxWidth(), paragraph->getHeight(), line);
+ canvas->translate(0, paragraph->getHeight());
+ };
+
+ draw(SK_ColorLTGRAY, TextHeightBehavior::kDisableFirstAscent);
+ draw(SK_ColorYELLOW, TextHeightBehavior::kDisableLastDescent);
+ draw(SK_ColorGRAY, TextHeightBehavior::kDisableAll);
+
+ }
+
+private:
+ typedef Sample INHERITED;
+};
+
+class ParagraphView42 : public ParagraphView_Base {
+protected:
+ SkString name() override { return SkString("Paragraph42"); }
+
+ void onDrawContent(SkCanvas* canvas) override {
+
+ SkString text("Atwater Peel Sherbrooke Bonaventure\nhi\nwasssup!");
+ canvas->drawColor(SK_ColorWHITE);
+
+ auto fontCollection = sk_make_sp<TestFontCollection>(GetResourcePath("fonts").c_str(), true, true);
+
+ ParagraphStyle paragraph_style;
+ ParagraphBuilderImpl builder(paragraph_style, fontCollection);
+ TextStyle text_style;
+ text_style.setColor(SK_ColorBLACK);
+ text_style.setFontFamilies({SkString("Ahem")});
+ text_style.setFontSize(16);
+ text_style.setHeight(4);
+ text_style.setHeightOverride(true);
+ builder.pushStyle(text_style);
+ builder.addText(text.c_str());
+ auto paragraph = builder.Build();
+ paragraph->layout(width());
+
+ auto boxes = paragraph->getRectsForRange(0, 7, RectHeightStyle::kIncludeLineSpacingTop, RectWidthStyle::kMax);
+ for (auto& box : boxes) {
+ SkPaint paint;
+ paint.setColor(SK_ColorGRAY);
+ canvas->drawRect(box.rect, paint);
+ }
+
+ auto boxes2 = paragraph->getRectsForRange(0, 7, RectHeightStyle::kTight, RectWidthStyle::kMax);
+ for (auto& box : boxes2) {
+ SkPaint paint;
+ paint.setColor(SK_ColorRED);
+ canvas->drawRect(box.rect, paint);
+ }
+
+ paragraph->paint(canvas, 0, 0);
+ }
+
+private:
+ typedef Sample INHERITED;
+};
+//
//////////////////////////////////////////////////////////////////////////////
DEF_SAMPLE(return new ParagraphView1();)
DEF_SAMPLE(return new ParagraphView2();)
@@ -2675,3 +2770,5 @@
DEF_SAMPLE(return new ParagraphView37();)
DEF_SAMPLE(return new ParagraphView38();)
DEF_SAMPLE(return new ParagraphView39();)
+DEF_SAMPLE(return new ParagraphView41();)
+DEF_SAMPLE(return new ParagraphView42();)