blob: 5ea9396f06dc20d345658129cccdbfa355793e6e [file] [log] [blame]
epoger@google.com213c42b2011-06-28 16:20:27 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * 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.com213c42b2011-06-28 16:20:27 +00006 */
7
epoger@google.comec3ed6a2011-07-28 14:26:00 +00008
epoger@google.com213c42b2011-06-28 16:20:27 +00009/* Tests text rendering with LCD and subpixel rendering turned on and off.
10 */
11
12#include "gm.h"
13#include "SkCanvas.h"
fmalita65d79ce2014-11-19 09:23:22 -080014#include "SkPicture.h"
15#include "SkPictureImageFilter.h"
16#include "SkPictureRecorder.h"
17#include "SkSurface.h"
18
epoger@google.com213c42b2011-06-28 16:20:27 +000019
reed4942e752014-10-01 13:59:33 -070020class LcdTextGM : public skiagm::GM {
epoger@google.com213c42b2011-06-28 16:20:27 +000021public:
22 LcdTextGM() {
23 const int pointSize = 36;
24 textHeight = SkIntToScalar(pointSize);
25 }
reed4942e752014-10-01 13:59:33 -070026
epoger@google.com213c42b2011-06-28 16:20:27 +000027protected:
reed4942e752014-10-01 13:59:33 -070028
epoger@google.com213c42b2011-06-28 16:20:27 +000029 SkString onShortName() {
30 return SkString("lcdtext");
31 }
reed4942e752014-10-01 13:59:33 -070032
tfarinaf5393182014-06-09 23:59:03 -070033 SkISize onISize() { return SkISize::Make(640, 480); }
reed4942e752014-10-01 13:59:33 -070034
epoger@google.com213c42b2011-06-28 16:20:27 +000035 virtual void onDraw(SkCanvas* canvas) {
reed4942e752014-10-01 13:59:33 -070036
epoger@google.com213c42b2011-06-28 16:20:27 +000037 y = textHeight;
38 drawText(canvas, SkString("TEXT: SubpixelTrue LCDRenderTrue"),
39 true, true);
40 drawText(canvas, SkString("TEXT: SubpixelTrue LCDRenderFalse"),
41 true, false);
42 drawText(canvas, SkString("TEXT: SubpixelFalse LCDRenderTrue"),
43 false, true);
44 drawText(canvas, SkString("TEXT: SubpixelFalse LCDRenderFalse"),
45 false, false);
46 }
reed4942e752014-10-01 13:59:33 -070047
epoger@google.com213c42b2011-06-28 16:20:27 +000048 void drawText(SkCanvas* canvas, const SkString& string,
49 bool subpixelTextEnabled, bool lcdRenderTextEnabled) {
50 SkPaint paint;
51 paint.setColor(SK_ColorBLACK);
52 paint.setDither(true);
53 paint.setAntiAlias(true);
Cary Clark992c7b02014-07-31 08:58:44 -040054 sk_tool_utils::set_portable_typeface(&paint);
epoger@google.com213c42b2011-06-28 16:20:27 +000055 paint.setSubpixelText(subpixelTextEnabled);
56 paint.setLCDRenderText(lcdRenderTextEnabled);
57 paint.setTextSize(textHeight);
reed4942e752014-10-01 13:59:33 -070058
epoger@google.com213c42b2011-06-28 16:20:27 +000059 canvas->drawText(string.c_str(), string.size(), 0, y, paint);
60 y += textHeight;
61 }
reed4942e752014-10-01 13:59:33 -070062
63private:
64 typedef skiagm::GM INHERITED;
65 SkScalar y, textHeight;
66};
67
68/*
69 * Skia will automatically disable LCD requests if the total size exceeds some limit
70 * (hard coded in this test for now, as it is now avaiable as an API)
71 *
72 * Test this both by changing "textsize" and by changing the computed size (textsize * CTM)
73 */
74class LcdTextSizeGM : public skiagm::GM {
75 enum {
76 kLCDTextSizeLimit = 48
77 };
78
79 static void ScaleAbout(SkCanvas* canvas, SkScalar sx, SkScalar sy, SkScalar px, SkScalar py) {
80 SkMatrix m;
81 m.setScale(sx, sy, px, py);
82 canvas->concat(m);
83 }
84
85public:
86 LcdTextSizeGM() {}
87
88protected:
89 SkString onShortName() {
90 return SkString("lcdtextsize");
91 }
92
93 SkISize onISize() { return SkISize::Make(320, 120); }
94
95 virtual void onDraw(SkCanvas* canvas) {
96 const char* lcd_text = "LCD";
97 const char* gray_text = "GRAY";
98
99 SkPaint paint;
100 paint.setAntiAlias(true);
101 paint.setLCDRenderText(true);
102
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
119 paint.setTextSize(rec[i].fTextSize);
120 ScaleAbout(canvas, rec[i].fScale, rec[i].fScale, loc.x(), loc.y());
121 canvas->drawText(rec[i].fText, strlen(rec[i].fText), loc.x(), loc.y(), paint);
122 }
123 }
epoger@google.com213c42b2011-06-28 16:20:27 +0000124
125private:
reed4942e752014-10-01 13:59:33 -0700126 typedef skiagm::GM INHERITED;
epoger@google.com213c42b2011-06-28 16:20:27 +0000127};
128
reed4942e752014-10-01 13:59:33 -0700129DEF_GM( return new LcdTextGM; )
130DEF_GM( return new LcdTextSizeGM; )