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" |
| 13 | #include "SkCanvas.h" |
fmalita | 65d79ce | 2014-11-19 09:23:22 -0800 | [diff] [blame] | 14 | #include "SkPicture.h" |
| 15 | #include "SkPictureImageFilter.h" |
| 16 | #include "SkPictureRecorder.h" |
| 17 | #include "SkSurface.h" |
| 18 | |
epoger@google.com | 213c42b | 2011-06-28 16:20:27 +0000 | [diff] [blame] | 19 | |
reed | 4942e75 | 2014-10-01 13:59:33 -0700 | [diff] [blame] | 20 | class LcdTextGM : public skiagm::GM { |
epoger@google.com | 213c42b | 2011-06-28 16:20:27 +0000 | [diff] [blame] | 21 | public: |
| 22 | LcdTextGM() { |
| 23 | const int pointSize = 36; |
| 24 | textHeight = SkIntToScalar(pointSize); |
| 25 | } |
reed | 4942e75 | 2014-10-01 13:59:33 -0700 | [diff] [blame] | 26 | |
epoger@google.com | 213c42b | 2011-06-28 16:20:27 +0000 | [diff] [blame] | 27 | protected: |
reed | 4942e75 | 2014-10-01 13:59:33 -0700 | [diff] [blame] | 28 | |
epoger@google.com | 213c42b | 2011-06-28 16:20:27 +0000 | [diff] [blame] | 29 | SkString onShortName() { |
caryclark | 3721355 | 2015-07-24 11:08:01 -0700 | [diff] [blame] | 30 | SkString name("lcdtext"); |
| 31 | name.append(sk_tool_utils::major_platform_os_name()); |
| 32 | return name; |
epoger@google.com | 213c42b | 2011-06-28 16:20:27 +0000 | [diff] [blame] | 33 | } |
reed | 4942e75 | 2014-10-01 13:59:33 -0700 | [diff] [blame] | 34 | |
tfarina | f539318 | 2014-06-09 23:59:03 -0700 | [diff] [blame] | 35 | SkISize onISize() { return SkISize::Make(640, 480); } |
reed | 4942e75 | 2014-10-01 13:59:33 -0700 | [diff] [blame] | 36 | |
epoger@google.com | 213c42b | 2011-06-28 16:20:27 +0000 | [diff] [blame] | 37 | virtual void onDraw(SkCanvas* canvas) { |
reed | 4942e75 | 2014-10-01 13:59:33 -0700 | [diff] [blame] | 38 | |
epoger@google.com | 213c42b | 2011-06-28 16:20:27 +0000 | [diff] [blame] | 39 | y = textHeight; |
| 40 | drawText(canvas, SkString("TEXT: SubpixelTrue LCDRenderTrue"), |
| 41 | true, true); |
| 42 | drawText(canvas, SkString("TEXT: SubpixelTrue LCDRenderFalse"), |
| 43 | true, false); |
| 44 | drawText(canvas, SkString("TEXT: SubpixelFalse LCDRenderTrue"), |
| 45 | false, true); |
| 46 | drawText(canvas, SkString("TEXT: SubpixelFalse LCDRenderFalse"), |
| 47 | false, false); |
| 48 | } |
reed | 4942e75 | 2014-10-01 13:59:33 -0700 | [diff] [blame] | 49 | |
epoger@google.com | 213c42b | 2011-06-28 16:20:27 +0000 | [diff] [blame] | 50 | void drawText(SkCanvas* canvas, const SkString& string, |
| 51 | bool subpixelTextEnabled, bool lcdRenderTextEnabled) { |
| 52 | SkPaint paint; |
| 53 | paint.setColor(SK_ColorBLACK); |
| 54 | paint.setDither(true); |
| 55 | paint.setAntiAlias(true); |
epoger@google.com | 213c42b | 2011-06-28 16:20:27 +0000 | [diff] [blame] | 56 | paint.setSubpixelText(subpixelTextEnabled); |
| 57 | paint.setLCDRenderText(lcdRenderTextEnabled); |
| 58 | paint.setTextSize(textHeight); |
reed | 4942e75 | 2014-10-01 13:59:33 -0700 | [diff] [blame] | 59 | |
epoger@google.com | 213c42b | 2011-06-28 16:20:27 +0000 | [diff] [blame] | 60 | canvas->drawText(string.c_str(), string.size(), 0, y, paint); |
| 61 | y += textHeight; |
| 62 | } |
reed | 4942e75 | 2014-10-01 13:59:33 -0700 | [diff] [blame] | 63 | |
| 64 | private: |
| 65 | typedef skiagm::GM INHERITED; |
| 66 | SkScalar y, textHeight; |
| 67 | }; |
| 68 | |
| 69 | /* |
| 70 | * Skia will automatically disable LCD requests if the total size exceeds some limit |
| 71 | * (hard coded in this test for now, as it is now avaiable as an API) |
| 72 | * |
| 73 | * Test this both by changing "textsize" and by changing the computed size (textsize * CTM) |
| 74 | */ |
| 75 | class LcdTextSizeGM : public skiagm::GM { |
| 76 | enum { |
| 77 | kLCDTextSizeLimit = 48 |
| 78 | }; |
| 79 | |
| 80 | static void ScaleAbout(SkCanvas* canvas, SkScalar sx, SkScalar sy, SkScalar px, SkScalar py) { |
| 81 | SkMatrix m; |
| 82 | m.setScale(sx, sy, px, py); |
| 83 | canvas->concat(m); |
| 84 | } |
| 85 | |
| 86 | public: |
| 87 | LcdTextSizeGM() {} |
| 88 | |
| 89 | protected: |
| 90 | SkString onShortName() { |
| 91 | return SkString("lcdtextsize"); |
| 92 | } |
| 93 | |
| 94 | SkISize onISize() { return SkISize::Make(320, 120); } |
| 95 | |
| 96 | virtual void onDraw(SkCanvas* canvas) { |
| 97 | const char* lcd_text = "LCD"; |
| 98 | const char* gray_text = "GRAY"; |
| 99 | |
| 100 | SkPaint paint; |
| 101 | paint.setAntiAlias(true); |
| 102 | paint.setLCDRenderText(true); |
| 103 | |
| 104 | const struct { |
| 105 | SkPoint fLoc; |
| 106 | SkScalar fTextSize; |
| 107 | SkScalar fScale; |
| 108 | const char* fText; |
| 109 | } rec[] = { |
| 110 | { { 10, 50 }, kLCDTextSizeLimit - 1, 1, lcd_text }, |
| 111 | { { 160, 50 }, kLCDTextSizeLimit + 1, 1, gray_text }, |
| 112 | { { 10, 100 }, kLCDTextSizeLimit / 2, 1.99f, lcd_text }, |
| 113 | { { 160, 100 }, kLCDTextSizeLimit / 2, 2.01f, gray_text }, |
| 114 | }; |
| 115 | |
| 116 | for (size_t i = 0; i < SK_ARRAY_COUNT(rec); ++i) { |
| 117 | const SkPoint loc = rec[i].fLoc; |
| 118 | SkAutoCanvasRestore acr(canvas, true); |
| 119 | |
| 120 | paint.setTextSize(rec[i].fTextSize); |
| 121 | ScaleAbout(canvas, rec[i].fScale, rec[i].fScale, loc.x(), loc.y()); |
| 122 | canvas->drawText(rec[i].fText, strlen(rec[i].fText), loc.x(), loc.y(), paint); |
| 123 | } |
| 124 | } |
epoger@google.com | 213c42b | 2011-06-28 16:20:27 +0000 | [diff] [blame] | 125 | |
| 126 | private: |
reed | 4942e75 | 2014-10-01 13:59:33 -0700 | [diff] [blame] | 127 | typedef skiagm::GM INHERITED; |
epoger@google.com | 213c42b | 2011-06-28 16:20:27 +0000 | [diff] [blame] | 128 | }; |
reed | 4942e75 | 2014-10-01 13:59:33 -0700 | [diff] [blame] | 129 | DEF_GM( return new LcdTextGM; ) |
| 130 | DEF_GM( return new LcdTextSizeGM; ) |
reed | 70ee31b | 2015-12-10 13:44:45 -0800 | [diff] [blame] | 131 | |
| 132 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 133 | |
| 134 | DEF_SIMPLE_GM(savelayer_lcdtext, canvas, 620, 260) { |
| 135 | SkPaint paint; |
| 136 | paint.setAntiAlias(true); |
| 137 | paint.setLCDRenderText(true); |
| 138 | paint.setTextSize(20); |
| 139 | |
| 140 | canvas->drawText("Hamburgefons", 12, 30, 30, paint); |
| 141 | |
| 142 | const bool gPreserveLCDText[] = { false, true }; |
| 143 | |
| 144 | canvas->translate(0, 20); |
| 145 | for (auto preserve : gPreserveLCDText) { |
| 146 | preserve ? canvas->saveLayerPreserveLCDTextRequests(nullptr, nullptr) |
| 147 | : canvas->saveLayer(nullptr, nullptr); |
| 148 | canvas->drawText("Hamburgefons", 12, 30, 60, paint); |
| 149 | |
| 150 | SkPaint p; |
| 151 | p.setColor(0xFFCCCCCC); |
| 152 | canvas->drawRect(SkRect::MakeLTRB(25, 70, 200, 100), p); |
| 153 | canvas->drawText("Hamburgefons", 12, 30, 90, paint); |
| 154 | |
| 155 | canvas->restore(); |
| 156 | canvas->translate(0, 80); |
| 157 | } |
| 158 | } |