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 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 8 | |
epoger@google.com | 213c42b | 2011-06-28 16:20:27 +0000 | [diff] [blame] | 9 | /* Tests text rendering with LCD and subpixel rendering turned on and off. |
| 10 | */ |
| 11 | |
| 12 | #include "gm.h" |
Mike Klein | 33d2055 | 2017-03-22 13:47:51 -0400 | [diff] [blame] | 13 | #include "sk_tool_utils.h" |
epoger@google.com | 213c42b | 2011-06-28 16:20:27 +0000 | [diff] [blame] | 14 | #include "SkCanvas.h" |
fmalita | 65d79ce | 2014-11-19 09:23:22 -0800 | [diff] [blame] | 15 | #include "SkPicture.h" |
| 16 | #include "SkPictureImageFilter.h" |
| 17 | #include "SkPictureRecorder.h" |
| 18 | #include "SkSurface.h" |
| 19 | |
epoger@google.com | 213c42b | 2011-06-28 16:20:27 +0000 | [diff] [blame] | 20 | |
reed | 4942e75 | 2014-10-01 13:59:33 -0700 | [diff] [blame] | 21 | class LcdTextGM : public skiagm::GM { |
epoger@google.com | 213c42b | 2011-06-28 16:20:27 +0000 | [diff] [blame] | 22 | public: |
| 23 | LcdTextGM() { |
| 24 | const int pointSize = 36; |
| 25 | textHeight = SkIntToScalar(pointSize); |
| 26 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 27 | |
epoger@google.com | 213c42b | 2011-06-28 16:20:27 +0000 | [diff] [blame] | 28 | protected: |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 29 | |
epoger@google.com | 213c42b | 2011-06-28 16:20:27 +0000 | [diff] [blame] | 30 | SkString onShortName() { |
caryclark | 3721355 | 2015-07-24 11:08:01 -0700 | [diff] [blame] | 31 | SkString name("lcdtext"); |
Mike Klein | b251b72 | 2017-11-13 11:03:16 -0500 | [diff] [blame] | 32 | name.append(sk_tool_utils::platform_font_manager()); |
caryclark | 3721355 | 2015-07-24 11:08:01 -0700 | [diff] [blame] | 33 | return name; |
epoger@google.com | 213c42b | 2011-06-28 16:20:27 +0000 | [diff] [blame] | 34 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 35 | |
tfarina | f539318 | 2014-06-09 23:59:03 -0700 | [diff] [blame] | 36 | SkISize onISize() { return SkISize::Make(640, 480); } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 37 | |
epoger@google.com | 213c42b | 2011-06-28 16:20:27 +0000 | [diff] [blame] | 38 | virtual void onDraw(SkCanvas* canvas) { |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 39 | |
epoger@google.com | 213c42b | 2011-06-28 16:20:27 +0000 | [diff] [blame] | 40 | y = textHeight; |
| 41 | drawText(canvas, SkString("TEXT: SubpixelTrue LCDRenderTrue"), |
| 42 | true, true); |
| 43 | drawText(canvas, SkString("TEXT: SubpixelTrue LCDRenderFalse"), |
| 44 | true, false); |
| 45 | drawText(canvas, SkString("TEXT: SubpixelFalse LCDRenderTrue"), |
| 46 | false, true); |
| 47 | drawText(canvas, SkString("TEXT: SubpixelFalse LCDRenderFalse"), |
| 48 | false, false); |
| 49 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 50 | |
epoger@google.com | 213c42b | 2011-06-28 16:20:27 +0000 | [diff] [blame] | 51 | void drawText(SkCanvas* canvas, const SkString& string, |
| 52 | bool subpixelTextEnabled, bool lcdRenderTextEnabled) { |
| 53 | SkPaint paint; |
| 54 | paint.setColor(SK_ColorBLACK); |
| 55 | paint.setDither(true); |
Hal Canary | 6ac0df8 | 2019-01-07 16:01:22 -0500 | [diff] [blame] | 56 | SkFont font(nullptr, textHeight); |
| 57 | if (subpixelTextEnabled) { |
| 58 | font.setSubpixel(true); |
| 59 | } |
| 60 | if (lcdRenderTextEnabled) { |
| 61 | font.setEdging(SkFont::Edging::kSubpixelAntiAlias); |
| 62 | } |
| 63 | canvas->drawString(string, 0, y, font, paint); |
epoger@google.com | 213c42b | 2011-06-28 16:20:27 +0000 | [diff] [blame] | 64 | y += textHeight; |
| 65 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 66 | |
reed | 4942e75 | 2014-10-01 13:59:33 -0700 | [diff] [blame] | 67 | private: |
| 68 | typedef skiagm::GM INHERITED; |
| 69 | SkScalar y, textHeight; |
| 70 | }; |
| 71 | |
| 72 | /* |
| 73 | * Skia will automatically disable LCD requests if the total size exceeds some limit |
| 74 | * (hard coded in this test for now, as it is now avaiable as an API) |
| 75 | * |
| 76 | * Test this both by changing "textsize" and by changing the computed size (textsize * CTM) |
| 77 | */ |
| 78 | class LcdTextSizeGM : public skiagm::GM { |
| 79 | enum { |
| 80 | kLCDTextSizeLimit = 48 |
| 81 | }; |
| 82 | |
| 83 | static void ScaleAbout(SkCanvas* canvas, SkScalar sx, SkScalar sy, SkScalar px, SkScalar py) { |
| 84 | SkMatrix m; |
| 85 | m.setScale(sx, sy, px, py); |
| 86 | canvas->concat(m); |
| 87 | } |
| 88 | |
| 89 | public: |
| 90 | LcdTextSizeGM() {} |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 91 | |
reed | 4942e75 | 2014-10-01 13:59:33 -0700 | [diff] [blame] | 92 | protected: |
| 93 | SkString onShortName() { |
| 94 | return SkString("lcdtextsize"); |
| 95 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 96 | |
reed | 4942e75 | 2014-10-01 13:59:33 -0700 | [diff] [blame] | 97 | SkISize onISize() { return SkISize::Make(320, 120); } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 98 | |
reed | 4942e75 | 2014-10-01 13:59:33 -0700 | [diff] [blame] | 99 | virtual void onDraw(SkCanvas* canvas) { |
| 100 | const char* lcd_text = "LCD"; |
| 101 | const char* gray_text = "GRAY"; |
| 102 | |
reed | 4942e75 | 2014-10-01 13:59:33 -0700 | [diff] [blame] | 103 | const struct { |
| 104 | SkPoint fLoc; |
| 105 | SkScalar fTextSize; |
| 106 | SkScalar fScale; |
| 107 | const char* fText; |
| 108 | } rec[] = { |
| 109 | { { 10, 50 }, kLCDTextSizeLimit - 1, 1, lcd_text }, |
| 110 | { { 160, 50 }, kLCDTextSizeLimit + 1, 1, gray_text }, |
| 111 | { { 10, 100 }, kLCDTextSizeLimit / 2, 1.99f, lcd_text }, |
| 112 | { { 160, 100 }, kLCDTextSizeLimit / 2, 2.01f, gray_text }, |
| 113 | }; |
| 114 | |
| 115 | for (size_t i = 0; i < SK_ARRAY_COUNT(rec); ++i) { |
| 116 | const SkPoint loc = rec[i].fLoc; |
| 117 | SkAutoCanvasRestore acr(canvas, true); |
| 118 | |
Hal Canary | 6ac0df8 | 2019-01-07 16:01:22 -0500 | [diff] [blame] | 119 | SkFont font(nullptr, rec[i].fTextSize); |
| 120 | font.setEdging(SkFont::Edging::kSubpixelAntiAlias); |
| 121 | |
reed | 4942e75 | 2014-10-01 13:59:33 -0700 | [diff] [blame] | 122 | ScaleAbout(canvas, rec[i].fScale, rec[i].fScale, loc.x(), loc.y()); |
Hal Canary | 6ac0df8 | 2019-01-07 16:01:22 -0500 | [diff] [blame] | 123 | canvas->drawString(rec[i].fText, loc.x(), loc.y(), font, SkPaint()); |
reed | 4942e75 | 2014-10-01 13:59:33 -0700 | [diff] [blame] | 124 | } |
| 125 | } |
epoger@google.com | 213c42b | 2011-06-28 16:20:27 +0000 | [diff] [blame] | 126 | |
| 127 | private: |
reed | 4942e75 | 2014-10-01 13:59:33 -0700 | [diff] [blame] | 128 | typedef skiagm::GM INHERITED; |
epoger@google.com | 213c42b | 2011-06-28 16:20:27 +0000 | [diff] [blame] | 129 | }; |
reed | 4942e75 | 2014-10-01 13:59:33 -0700 | [diff] [blame] | 130 | DEF_GM( return new LcdTextGM; ) |
| 131 | DEF_GM( return new LcdTextSizeGM; ) |