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" |
| 14 | |
reed | 4942e75 | 2014-10-01 13:59:33 -0700 | [diff] [blame] | 15 | class LcdTextGM : public skiagm::GM { |
epoger@google.com | 213c42b | 2011-06-28 16:20:27 +0000 | [diff] [blame] | 16 | public: |
| 17 | LcdTextGM() { |
| 18 | const int pointSize = 36; |
| 19 | textHeight = SkIntToScalar(pointSize); |
| 20 | } |
reed | 4942e75 | 2014-10-01 13:59:33 -0700 | [diff] [blame] | 21 | |
epoger@google.com | 213c42b | 2011-06-28 16:20:27 +0000 | [diff] [blame] | 22 | protected: |
reed | 4942e75 | 2014-10-01 13:59:33 -0700 | [diff] [blame] | 23 | |
epoger@google.com | 213c42b | 2011-06-28 16:20:27 +0000 | [diff] [blame] | 24 | SkString onShortName() { |
| 25 | return SkString("lcdtext"); |
| 26 | } |
reed | 4942e75 | 2014-10-01 13:59:33 -0700 | [diff] [blame] | 27 | |
tfarina | f539318 | 2014-06-09 23:59:03 -0700 | [diff] [blame] | 28 | SkISize onISize() { return SkISize::Make(640, 480); } |
reed | 4942e75 | 2014-10-01 13:59:33 -0700 | [diff] [blame] | 29 | |
epoger@google.com | 213c42b | 2011-06-28 16:20:27 +0000 | [diff] [blame] | 30 | virtual void onDraw(SkCanvas* canvas) { |
reed | 4942e75 | 2014-10-01 13:59:33 -0700 | [diff] [blame] | 31 | |
epoger@google.com | 213c42b | 2011-06-28 16:20:27 +0000 | [diff] [blame] | 32 | y = textHeight; |
| 33 | drawText(canvas, SkString("TEXT: SubpixelTrue LCDRenderTrue"), |
| 34 | true, true); |
| 35 | drawText(canvas, SkString("TEXT: SubpixelTrue LCDRenderFalse"), |
| 36 | true, false); |
| 37 | drawText(canvas, SkString("TEXT: SubpixelFalse LCDRenderTrue"), |
| 38 | false, true); |
| 39 | drawText(canvas, SkString("TEXT: SubpixelFalse LCDRenderFalse"), |
| 40 | false, false); |
| 41 | } |
reed | 4942e75 | 2014-10-01 13:59:33 -0700 | [diff] [blame] | 42 | |
epoger@google.com | 213c42b | 2011-06-28 16:20:27 +0000 | [diff] [blame] | 43 | void drawText(SkCanvas* canvas, const SkString& string, |
| 44 | bool subpixelTextEnabled, bool lcdRenderTextEnabled) { |
| 45 | SkPaint paint; |
| 46 | paint.setColor(SK_ColorBLACK); |
| 47 | paint.setDither(true); |
| 48 | paint.setAntiAlias(true); |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 49 | sk_tool_utils::set_portable_typeface(&paint); |
epoger@google.com | 213c42b | 2011-06-28 16:20:27 +0000 | [diff] [blame] | 50 | paint.setSubpixelText(subpixelTextEnabled); |
| 51 | paint.setLCDRenderText(lcdRenderTextEnabled); |
| 52 | paint.setTextSize(textHeight); |
reed | 4942e75 | 2014-10-01 13:59:33 -0700 | [diff] [blame] | 53 | |
epoger@google.com | 213c42b | 2011-06-28 16:20:27 +0000 | [diff] [blame] | 54 | canvas->drawText(string.c_str(), string.size(), 0, y, paint); |
| 55 | y += textHeight; |
| 56 | } |
reed | 4942e75 | 2014-10-01 13:59:33 -0700 | [diff] [blame] | 57 | |
| 58 | private: |
| 59 | typedef skiagm::GM INHERITED; |
| 60 | SkScalar y, textHeight; |
| 61 | }; |
| 62 | |
| 63 | /* |
| 64 | * Skia will automatically disable LCD requests if the total size exceeds some limit |
| 65 | * (hard coded in this test for now, as it is now avaiable as an API) |
| 66 | * |
| 67 | * Test this both by changing "textsize" and by changing the computed size (textsize * CTM) |
| 68 | */ |
| 69 | class LcdTextSizeGM : public skiagm::GM { |
| 70 | enum { |
| 71 | kLCDTextSizeLimit = 48 |
| 72 | }; |
| 73 | |
| 74 | static void ScaleAbout(SkCanvas* canvas, SkScalar sx, SkScalar sy, SkScalar px, SkScalar py) { |
| 75 | SkMatrix m; |
| 76 | m.setScale(sx, sy, px, py); |
| 77 | canvas->concat(m); |
| 78 | } |
| 79 | |
| 80 | public: |
| 81 | LcdTextSizeGM() {} |
| 82 | |
| 83 | protected: |
| 84 | SkString onShortName() { |
| 85 | return SkString("lcdtextsize"); |
| 86 | } |
| 87 | |
| 88 | SkISize onISize() { return SkISize::Make(320, 120); } |
| 89 | |
| 90 | virtual void onDraw(SkCanvas* canvas) { |
| 91 | const char* lcd_text = "LCD"; |
| 92 | const char* gray_text = "GRAY"; |
| 93 | |
| 94 | SkPaint paint; |
| 95 | paint.setAntiAlias(true); |
| 96 | paint.setLCDRenderText(true); |
| 97 | |
| 98 | const struct { |
| 99 | SkPoint fLoc; |
| 100 | SkScalar fTextSize; |
| 101 | SkScalar fScale; |
| 102 | const char* fText; |
| 103 | } rec[] = { |
| 104 | { { 10, 50 }, kLCDTextSizeLimit - 1, 1, lcd_text }, |
| 105 | { { 160, 50 }, kLCDTextSizeLimit + 1, 1, gray_text }, |
| 106 | { { 10, 100 }, kLCDTextSizeLimit / 2, 1.99f, lcd_text }, |
| 107 | { { 160, 100 }, kLCDTextSizeLimit / 2, 2.01f, gray_text }, |
| 108 | }; |
| 109 | |
| 110 | for (size_t i = 0; i < SK_ARRAY_COUNT(rec); ++i) { |
| 111 | const SkPoint loc = rec[i].fLoc; |
| 112 | SkAutoCanvasRestore acr(canvas, true); |
| 113 | |
| 114 | paint.setTextSize(rec[i].fTextSize); |
| 115 | ScaleAbout(canvas, rec[i].fScale, rec[i].fScale, loc.x(), loc.y()); |
| 116 | canvas->drawText(rec[i].fText, strlen(rec[i].fText), loc.x(), loc.y(), paint); |
| 117 | } |
| 118 | } |
epoger@google.com | 213c42b | 2011-06-28 16:20:27 +0000 | [diff] [blame] | 119 | |
| 120 | private: |
reed | 4942e75 | 2014-10-01 13:59:33 -0700 | [diff] [blame] | 121 | typedef skiagm::GM INHERITED; |
epoger@google.com | 213c42b | 2011-06-28 16:20:27 +0000 | [diff] [blame] | 122 | }; |
| 123 | |
reed | a3aff06 | 2014-11-13 10:40:58 -0800 | [diff] [blame] | 124 | #include "SkSurface.h" |
| 125 | |
| 126 | // ensure that we respect the SkPixelGeometry in SurfaceProps |
| 127 | class LcdTextProps : public skiagm::GM { |
| 128 | static void DrawText(SkCanvas* canvas) { |
| 129 | canvas->drawColor(SK_ColorWHITE); |
| 130 | SkPaint paint; |
| 131 | paint.setAntiAlias(true); |
| 132 | paint.setLCDRenderText(true); |
| 133 | paint.setTextSize(30); |
| 134 | canvas->drawText("Base", 4, 4, 30, paint); |
| 135 | canvas->saveLayer(NULL, NULL); |
| 136 | canvas->drawText("Layer", 5, 4, 70, paint); |
| 137 | canvas->restore(); |
| 138 | } |
| 139 | |
fmalita | f4905cc | 2014-11-13 11:29:07 -0800 | [diff] [blame] | 140 | protected: |
reed | a3aff06 | 2014-11-13 10:40:58 -0800 | [diff] [blame] | 141 | SkString onShortName() SK_OVERRIDE { |
| 142 | return SkString("lcdtextprops"); |
| 143 | } |
| 144 | |
| 145 | SkISize onISize() SK_OVERRIDE { return SkISize::Make(230, 120); } |
| 146 | |
fmalita | f4905cc | 2014-11-13 11:29:07 -0800 | [diff] [blame] | 147 | uint32_t onGetFlags() const SK_OVERRIDE { |
| 148 | return kSkip565_Flag; |
| 149 | } |
| 150 | |
reed | a3aff06 | 2014-11-13 10:40:58 -0800 | [diff] [blame] | 151 | virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { |
| 152 | const SkPixelGeometry geos[] = { |
| 153 | kRGB_H_SkPixelGeometry, |
| 154 | kUnknown_SkPixelGeometry, |
| 155 | }; |
| 156 | |
| 157 | const SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100); |
| 158 | for (size_t i = 0; i < SK_ARRAY_COUNT(geos); ++i) { |
| 159 | SkSurfaceProps props = SkSurfaceProps(0, geos[i]); |
| 160 | SkAutoTUnref<SkSurface> surf(canvas->newSurface(info, &props)); |
| 161 | if (!surf) { |
| 162 | surf.reset(SkSurface::NewRaster(info, &props)); |
| 163 | } |
| 164 | DrawText(surf->getCanvas()); |
| 165 | surf->draw(canvas, SkIntToScalar(i * (info.width() + 10)), 0, NULL); |
| 166 | } |
| 167 | } |
| 168 | }; |
| 169 | |
epoger@google.com | 213c42b | 2011-06-28 16:20:27 +0000 | [diff] [blame] | 170 | /////////////////////////////////////////////////////////////////////////////// |
| 171 | |
reed | 4942e75 | 2014-10-01 13:59:33 -0700 | [diff] [blame] | 172 | DEF_GM( return new LcdTextGM; ) |
| 173 | DEF_GM( return new LcdTextSizeGM; ) |
Florin Malita | 66b4ce9 | 2014-11-13 15:00:03 -0500 | [diff] [blame^] | 174 | // Temporarily disabled (dftext interference) |
| 175 | // DEF_GM( return new LcdTextProps; ) |