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