blob: 6daec824f32639de4f55d85a03a2f2a2fafa6c54 [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() {
caryclark37213552015-07-24 11:08:01 -070030 SkString name("lcdtext");
31 name.append(sk_tool_utils::major_platform_os_name());
32 return name;
epoger@google.com213c42b2011-06-28 16:20:27 +000033 }
reed4942e752014-10-01 13:59:33 -070034
tfarinaf5393182014-06-09 23:59:03 -070035 SkISize onISize() { return SkISize::Make(640, 480); }
reed4942e752014-10-01 13:59:33 -070036
epoger@google.com213c42b2011-06-28 16:20:27 +000037 virtual void onDraw(SkCanvas* canvas) {
reed4942e752014-10-01 13:59:33 -070038
epoger@google.com213c42b2011-06-28 16:20:27 +000039 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 }
reed4942e752014-10-01 13:59:33 -070049
epoger@google.com213c42b2011-06-28 16:20:27 +000050 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.com213c42b2011-06-28 16:20:27 +000056 paint.setSubpixelText(subpixelTextEnabled);
57 paint.setLCDRenderText(lcdRenderTextEnabled);
58 paint.setTextSize(textHeight);
reed4942e752014-10-01 13:59:33 -070059
epoger@google.com213c42b2011-06-28 16:20:27 +000060 canvas->drawText(string.c_str(), string.size(), 0, y, paint);
61 y += textHeight;
62 }
reed4942e752014-10-01 13:59:33 -070063
64private:
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 */
75class 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
86public:
87 LcdTextSizeGM() {}
88
89protected:
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.com213c42b2011-06-28 16:20:27 +0000125
126private:
reed4942e752014-10-01 13:59:33 -0700127 typedef skiagm::GM INHERITED;
epoger@google.com213c42b2011-06-28 16:20:27 +0000128};
129
reed4942e752014-10-01 13:59:33 -0700130DEF_GM( return new LcdTextGM; )
131DEF_GM( return new LcdTextSizeGM; )