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); |
| 56 | paint.setAntiAlias(true); |
epoger@google.com | 213c42b | 2011-06-28 16:20:27 +0000 | [diff] [blame] | 57 | paint.setSubpixelText(subpixelTextEnabled); |
| 58 | paint.setLCDRenderText(lcdRenderTextEnabled); |
| 59 | paint.setTextSize(textHeight); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 60 | |
Cary Clark | 2a475ea | 2017-04-28 15:35:12 -0400 | [diff] [blame] | 61 | canvas->drawString(string, 0, y, paint); |
epoger@google.com | 213c42b | 2011-06-28 16:20:27 +0000 | [diff] [blame] | 62 | y += textHeight; |
| 63 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 64 | |
reed | 4942e75 | 2014-10-01 13:59:33 -0700 | [diff] [blame] | 65 | private: |
| 66 | typedef skiagm::GM INHERITED; |
| 67 | SkScalar y, textHeight; |
| 68 | }; |
| 69 | |
| 70 | /* |
| 71 | * Skia will automatically disable LCD requests if the total size exceeds some limit |
| 72 | * (hard coded in this test for now, as it is now avaiable as an API) |
| 73 | * |
| 74 | * Test this both by changing "textsize" and by changing the computed size (textsize * CTM) |
| 75 | */ |
| 76 | class LcdTextSizeGM : public skiagm::GM { |
| 77 | enum { |
| 78 | kLCDTextSizeLimit = 48 |
| 79 | }; |
| 80 | |
| 81 | static void ScaleAbout(SkCanvas* canvas, SkScalar sx, SkScalar sy, SkScalar px, SkScalar py) { |
| 82 | SkMatrix m; |
| 83 | m.setScale(sx, sy, px, py); |
| 84 | canvas->concat(m); |
| 85 | } |
| 86 | |
| 87 | public: |
| 88 | LcdTextSizeGM() {} |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 89 | |
reed | 4942e75 | 2014-10-01 13:59:33 -0700 | [diff] [blame] | 90 | protected: |
| 91 | SkString onShortName() { |
| 92 | return SkString("lcdtextsize"); |
| 93 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 94 | |
reed | 4942e75 | 2014-10-01 13:59:33 -0700 | [diff] [blame] | 95 | SkISize onISize() { return SkISize::Make(320, 120); } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 96 | |
reed | 4942e75 | 2014-10-01 13:59:33 -0700 | [diff] [blame] | 97 | virtual void onDraw(SkCanvas* canvas) { |
| 98 | const char* lcd_text = "LCD"; |
| 99 | const char* gray_text = "GRAY"; |
| 100 | |
| 101 | SkPaint paint; |
| 102 | paint.setAntiAlias(true); |
| 103 | paint.setLCDRenderText(true); |
| 104 | |
| 105 | const struct { |
| 106 | SkPoint fLoc; |
| 107 | SkScalar fTextSize; |
| 108 | SkScalar fScale; |
| 109 | const char* fText; |
| 110 | } rec[] = { |
| 111 | { { 10, 50 }, kLCDTextSizeLimit - 1, 1, lcd_text }, |
| 112 | { { 160, 50 }, kLCDTextSizeLimit + 1, 1, gray_text }, |
| 113 | { { 10, 100 }, kLCDTextSizeLimit / 2, 1.99f, lcd_text }, |
| 114 | { { 160, 100 }, kLCDTextSizeLimit / 2, 2.01f, gray_text }, |
| 115 | }; |
| 116 | |
| 117 | for (size_t i = 0; i < SK_ARRAY_COUNT(rec); ++i) { |
| 118 | const SkPoint loc = rec[i].fLoc; |
| 119 | SkAutoCanvasRestore acr(canvas, true); |
| 120 | |
| 121 | paint.setTextSize(rec[i].fTextSize); |
| 122 | ScaleAbout(canvas, rec[i].fScale, rec[i].fScale, loc.x(), loc.y()); |
Cary Clark | 2a475ea | 2017-04-28 15:35:12 -0400 | [diff] [blame] | 123 | canvas->drawString(rec[i].fText, loc.x(), loc.y(), paint); |
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; ) |
reed | 70ee31b | 2015-12-10 13:44:45 -0800 | [diff] [blame] | 132 | |
| 133 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 134 | |
| 135 | DEF_SIMPLE_GM(savelayer_lcdtext, canvas, 620, 260) { |
| 136 | SkPaint paint; |
| 137 | paint.setAntiAlias(true); |
| 138 | paint.setLCDRenderText(true); |
| 139 | paint.setTextSize(20); |
| 140 | |
Cary Clark | 2a475ea | 2017-04-28 15:35:12 -0400 | [diff] [blame] | 141 | canvas->drawString("Hamburgefons", 30, 30, paint); |
reed | 70ee31b | 2015-12-10 13:44:45 -0800 | [diff] [blame] | 142 | |
| 143 | const bool gPreserveLCDText[] = { false, true }; |
| 144 | |
| 145 | canvas->translate(0, 20); |
| 146 | for (auto preserve : gPreserveLCDText) { |
| 147 | preserve ? canvas->saveLayerPreserveLCDTextRequests(nullptr, nullptr) |
| 148 | : canvas->saveLayer(nullptr, nullptr); |
Brian Osman | 0610a46 | 2017-05-23 17:09:19 -0400 | [diff] [blame] | 149 | if (preserve && !canvas->imageInfo().colorSpace()) { |
| 150 | SkPaint noLCD = paint; |
| 151 | noLCD.setLCDRenderText(false); |
| 152 | canvas->drawString("LCD not supported", 30, 60, noLCD); |
| 153 | } else { |
| 154 | canvas->drawString("Hamburgefons", 30, 60, paint); |
| 155 | } |
reed | 70ee31b | 2015-12-10 13:44:45 -0800 | [diff] [blame] | 156 | |
| 157 | SkPaint p; |
| 158 | p.setColor(0xFFCCCCCC); |
| 159 | canvas->drawRect(SkRect::MakeLTRB(25, 70, 200, 100), p); |
Cary Clark | 2a475ea | 2017-04-28 15:35:12 -0400 | [diff] [blame] | 160 | canvas->drawString("Hamburgefons", 30, 90, paint); |
reed | 70ee31b | 2015-12-10 13:44:45 -0800 | [diff] [blame] | 161 | |
| 162 | canvas->restore(); |
| 163 | canvas->translate(0, 80); |
| 164 | } |
| 165 | } |