epoger@google.com | 213c42b | 2011-06-28 16:20:27 +0000 | [diff] [blame] | 1 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 2 | * Copyright 2011 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
epoger@google.com | 213c42b | 2011-06-28 16:20:27 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "gm/gm.h" |
| 9 | #include "include/core/SkCanvas.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 10 | #include "include/core/SkColor.h" |
| 11 | #include "include/core/SkFont.h" |
| 12 | #include "include/core/SkMatrix.h" |
| 13 | #include "include/core/SkPaint.h" |
| 14 | #include "include/core/SkPoint.h" |
| 15 | #include "include/core/SkRefCnt.h" |
| 16 | #include "include/core/SkScalar.h" |
| 17 | #include "include/core/SkSize.h" |
| 18 | #include "include/core/SkString.h" |
| 19 | #include "include/core/SkTypeface.h" |
| 20 | #include "include/core/SkTypes.h" |
epoger@google.com | 213c42b | 2011-06-28 16:20:27 +0000 | [diff] [blame] | 21 | |
reed | 4942e75 | 2014-10-01 13:59:33 -0700 | [diff] [blame] | 22 | class LcdTextGM : public skiagm::GM { |
Hal Canary | fa3305a | 2019-07-18 12:36:54 -0400 | [diff] [blame] | 23 | static constexpr SkScalar kTextHeight = 36; |
| 24 | SkScalar fY = kTextHeight; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 25 | |
Hal Canary | fa3305a | 2019-07-18 12:36:54 -0400 | [diff] [blame] | 26 | SkString onShortName() override { return SkString("lcdtext"); } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 27 | |
Hal Canary | fa3305a | 2019-07-18 12:36:54 -0400 | [diff] [blame] | 28 | SkISize onISize() override { return {640, 480}; } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 29 | |
Hal Canary | fa3305a | 2019-07-18 12:36:54 -0400 | [diff] [blame] | 30 | void onDraw(SkCanvas* canvas) override { |
| 31 | fY = kTextHeight; |
epoger@google.com | 213c42b | 2011-06-28 16:20:27 +0000 | [diff] [blame] | 32 | drawText(canvas, SkString("TEXT: SubpixelTrue LCDRenderTrue"), |
| 33 | true, true); |
| 34 | drawText(canvas, SkString("TEXT: SubpixelTrue LCDRenderFalse"), |
| 35 | true, false); |
| 36 | drawText(canvas, SkString("TEXT: SubpixelFalse LCDRenderTrue"), |
| 37 | false, true); |
| 38 | drawText(canvas, SkString("TEXT: SubpixelFalse LCDRenderFalse"), |
| 39 | false, false); |
| 40 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 41 | |
epoger@google.com | 213c42b | 2011-06-28 16:20:27 +0000 | [diff] [blame] | 42 | void drawText(SkCanvas* canvas, const SkString& string, |
| 43 | bool subpixelTextEnabled, bool lcdRenderTextEnabled) { |
| 44 | SkPaint paint; |
| 45 | paint.setColor(SK_ColorBLACK); |
| 46 | paint.setDither(true); |
Hal Canary | fa3305a | 2019-07-18 12:36:54 -0400 | [diff] [blame] | 47 | SkFont font(nullptr, kTextHeight); |
Hal Canary | 6ac0df8 | 2019-01-07 16:01:22 -0500 | [diff] [blame] | 48 | if (subpixelTextEnabled) { |
| 49 | font.setSubpixel(true); |
| 50 | } |
| 51 | if (lcdRenderTextEnabled) { |
| 52 | font.setEdging(SkFont::Edging::kSubpixelAntiAlias); |
| 53 | } |
Hal Canary | fa3305a | 2019-07-18 12:36:54 -0400 | [diff] [blame] | 54 | canvas->drawString(string, 0, fY, font, paint); |
| 55 | fY += kTextHeight; |
epoger@google.com | 213c42b | 2011-06-28 16:20:27 +0000 | [diff] [blame] | 56 | } |
reed | 4942e75 | 2014-10-01 13:59:33 -0700 | [diff] [blame] | 57 | }; |
| 58 | |
| 59 | /* |
| 60 | * Skia will automatically disable LCD requests if the total size exceeds some limit |
| 61 | * (hard coded in this test for now, as it is now avaiable as an API) |
| 62 | * |
| 63 | * Test this both by changing "textsize" and by changing the computed size (textsize * CTM) |
| 64 | */ |
| 65 | class LcdTextSizeGM : public skiagm::GM { |
reed | 4942e75 | 2014-10-01 13:59:33 -0700 | [diff] [blame] | 66 | static void ScaleAbout(SkCanvas* canvas, SkScalar sx, SkScalar sy, SkScalar px, SkScalar py) { |
| 67 | SkMatrix m; |
| 68 | m.setScale(sx, sy, px, py); |
| 69 | canvas->concat(m); |
| 70 | } |
| 71 | |
Hal Canary | fa3305a | 2019-07-18 12:36:54 -0400 | [diff] [blame] | 72 | SkString onShortName() override { return SkString("lcdtextsize"); } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 73 | |
Hal Canary | fa3305a | 2019-07-18 12:36:54 -0400 | [diff] [blame] | 74 | SkISize onISize() override { return {320, 120}; } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 75 | |
Hal Canary | fa3305a | 2019-07-18 12:36:54 -0400 | [diff] [blame] | 76 | void onDraw(SkCanvas* canvas) override { |
reed | 4942e75 | 2014-10-01 13:59:33 -0700 | [diff] [blame] | 77 | const char* lcd_text = "LCD"; |
| 78 | const char* gray_text = "GRAY"; |
| 79 | |
Hal Canary | fa3305a | 2019-07-18 12:36:54 -0400 | [diff] [blame] | 80 | constexpr static float kLCDTextSizeLimit = 48; |
| 81 | |
reed | 4942e75 | 2014-10-01 13:59:33 -0700 | [diff] [blame] | 82 | const struct { |
| 83 | SkPoint fLoc; |
| 84 | SkScalar fTextSize; |
| 85 | SkScalar fScale; |
| 86 | const char* fText; |
| 87 | } rec[] = { |
| 88 | { { 10, 50 }, kLCDTextSizeLimit - 1, 1, lcd_text }, |
| 89 | { { 160, 50 }, kLCDTextSizeLimit + 1, 1, gray_text }, |
| 90 | { { 10, 100 }, kLCDTextSizeLimit / 2, 1.99f, lcd_text }, |
| 91 | { { 160, 100 }, kLCDTextSizeLimit / 2, 2.01f, gray_text }, |
| 92 | }; |
| 93 | |
| 94 | for (size_t i = 0; i < SK_ARRAY_COUNT(rec); ++i) { |
| 95 | const SkPoint loc = rec[i].fLoc; |
| 96 | SkAutoCanvasRestore acr(canvas, true); |
| 97 | |
Hal Canary | 6ac0df8 | 2019-01-07 16:01:22 -0500 | [diff] [blame] | 98 | SkFont font(nullptr, rec[i].fTextSize); |
| 99 | font.setEdging(SkFont::Edging::kSubpixelAntiAlias); |
| 100 | |
reed | 4942e75 | 2014-10-01 13:59:33 -0700 | [diff] [blame] | 101 | ScaleAbout(canvas, rec[i].fScale, rec[i].fScale, loc.x(), loc.y()); |
Hal Canary | 6ac0df8 | 2019-01-07 16:01:22 -0500 | [diff] [blame] | 102 | canvas->drawString(rec[i].fText, loc.x(), loc.y(), font, SkPaint()); |
reed | 4942e75 | 2014-10-01 13:59:33 -0700 | [diff] [blame] | 103 | } |
| 104 | } |
epoger@google.com | 213c42b | 2011-06-28 16:20:27 +0000 | [diff] [blame] | 105 | }; |
Xianzhu Wang | a91e478 | 2020-05-01 09:34:57 -0700 | [diff] [blame] | 106 | |
| 107 | class SaveLayerPreserveLCDTextGM : public skiagm::GM { |
| 108 | static constexpr SkScalar kTextHeight = 36; |
| 109 | |
| 110 | SkString onShortName() override { return SkString("savelayerpreservelcdtext"); } |
| 111 | |
| 112 | SkISize onISize() override { return {620, 300}; } |
| 113 | |
| 114 | void onDraw(SkCanvas* canvas) override { |
| 115 | drawText(canvas, SkString("SaveLayer PreserveLCDText"), 50, |
| 116 | SkCanvas::kPreserveLCDText_SaveLayerFlag); |
| 117 | drawText(canvas, SkString("SaveLayer Default (LCDText not preserved)"), 150, 0); |
| 118 | } |
| 119 | |
| 120 | void drawText(SkCanvas* canvas, |
| 121 | const SkString& string, |
| 122 | int y, |
| 123 | SkCanvas::SaveLayerFlags saveLayerFlags) { |
| 124 | SkCanvas::SaveLayerRec rec(nullptr, nullptr, saveLayerFlags); |
| 125 | canvas->saveLayer(rec); |
| 126 | SkPaint paint; |
| 127 | paint.setColor(SK_ColorWHITE); |
| 128 | canvas->drawRect(SkRect::MakeXYWH(0, y - 10, 640, kTextHeight + 20), paint); |
| 129 | paint.setColor(SK_ColorBLACK); |
| 130 | SkFont font(nullptr, kTextHeight); |
| 131 | font.setEdging(SkFont::Edging::kSubpixelAntiAlias); |
| 132 | canvas->drawString(string, 10, y, font, paint); |
| 133 | canvas->restore(); |
| 134 | } |
| 135 | }; |
| 136 | |
| 137 | DEF_GM(return new LcdTextGM;) |
| 138 | DEF_GM(return new LcdTextSizeGM;) |
| 139 | DEF_GM(return new SaveLayerPreserveLCDTextGM;) |