blob: 3fe9fa2fbc179c5f8e504aa7a6dd0b2fa9b0504f [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"
14
reed4942e752014-10-01 13:59:33 -070015class LcdTextGM : public skiagm::GM {
epoger@google.com213c42b2011-06-28 16:20:27 +000016public:
17 LcdTextGM() {
18 const int pointSize = 36;
19 textHeight = SkIntToScalar(pointSize);
20 }
reed4942e752014-10-01 13:59:33 -070021
epoger@google.com213c42b2011-06-28 16:20:27 +000022protected:
reed4942e752014-10-01 13:59:33 -070023
epoger@google.com213c42b2011-06-28 16:20:27 +000024 SkString onShortName() {
25 return SkString("lcdtext");
26 }
reed4942e752014-10-01 13:59:33 -070027
tfarinaf5393182014-06-09 23:59:03 -070028 SkISize onISize() { return SkISize::Make(640, 480); }
reed4942e752014-10-01 13:59:33 -070029
epoger@google.com213c42b2011-06-28 16:20:27 +000030 virtual void onDraw(SkCanvas* canvas) {
reed4942e752014-10-01 13:59:33 -070031
epoger@google.com213c42b2011-06-28 16:20:27 +000032 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 }
reed4942e752014-10-01 13:59:33 -070042
epoger@google.com213c42b2011-06-28 16:20:27 +000043 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 Clark992c7b02014-07-31 08:58:44 -040049 sk_tool_utils::set_portable_typeface(&paint);
epoger@google.com213c42b2011-06-28 16:20:27 +000050 paint.setSubpixelText(subpixelTextEnabled);
51 paint.setLCDRenderText(lcdRenderTextEnabled);
52 paint.setTextSize(textHeight);
reed4942e752014-10-01 13:59:33 -070053
epoger@google.com213c42b2011-06-28 16:20:27 +000054 canvas->drawText(string.c_str(), string.size(), 0, y, paint);
55 y += textHeight;
56 }
reed4942e752014-10-01 13:59:33 -070057
58private:
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 */
69class 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
80public:
81 LcdTextSizeGM() {}
82
83protected:
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.com213c42b2011-06-28 16:20:27 +0000119
120private:
reed4942e752014-10-01 13:59:33 -0700121 typedef skiagm::GM INHERITED;
epoger@google.com213c42b2011-06-28 16:20:27 +0000122};
123
reeda3aff062014-11-13 10:40:58 -0800124#include "SkSurface.h"
125
126// ensure that we respect the SkPixelGeometry in SurfaceProps
127class 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
fmalitaf4905cc2014-11-13 11:29:07 -0800140protected:
reeda3aff062014-11-13 10:40:58 -0800141 SkString onShortName() SK_OVERRIDE {
142 return SkString("lcdtextprops");
143 }
144
145 SkISize onISize() SK_OVERRIDE { return SkISize::Make(230, 120); }
146
fmalitaf4905cc2014-11-13 11:29:07 -0800147 uint32_t onGetFlags() const SK_OVERRIDE {
148 return kSkip565_Flag;
149 }
150
reeda3aff062014-11-13 10:40:58 -0800151 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.com213c42b2011-06-28 16:20:27 +0000170///////////////////////////////////////////////////////////////////////////////
171
reed4942e752014-10-01 13:59:33 -0700172DEF_GM( return new LcdTextGM; )
173DEF_GM( return new LcdTextSizeGM; )
Florin Malita66b4ce92014-11-13 15:00:03 -0500174// Temporarily disabled (dftext interference)
175// DEF_GM( return new LcdTextProps; )