blob: a6e31bce5f4c8be632cb764915fcd7554ba110fd [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
reeda3aff062014-11-13 10:40:58 -0800129// ensure that we respect the SkPixelGeometry in SurfaceProps
130class LcdTextProps : public skiagm::GM {
131 static void DrawText(SkCanvas* canvas) {
132 canvas->drawColor(SK_ColorWHITE);
133 SkPaint paint;
134 paint.setAntiAlias(true);
135 paint.setLCDRenderText(true);
136 paint.setTextSize(30);
137 canvas->drawText("Base", 4, 4, 30, paint);
138 canvas->saveLayer(NULL, NULL);
139 canvas->drawText("Layer", 5, 4, 70, paint);
140 canvas->restore();
141 }
142
fmalita65d79ce2014-11-19 09:23:22 -0800143 static SkSurface* MakeSurface(SkCanvas* canvas, const SkImageInfo& info, SkPixelGeometry geo) {
144 SkSurfaceProps props = SkSurfaceProps(0, geo);
145 SkSurface* surface = canvas->newSurface(info, &props);
146
147 if (!surface) {
148 surface = SkSurface::NewRaster(info, &props);
149 }
150
151 return surface;
152 }
153
fmalitaf4905cc2014-11-13 11:29:07 -0800154protected:
reeda3aff062014-11-13 10:40:58 -0800155 SkString onShortName() SK_OVERRIDE {
156 return SkString("lcdtextprops");
157 }
158
fmalita65d79ce2014-11-19 09:23:22 -0800159 SkISize onISize() SK_OVERRIDE { return SkISize::Make(230, 230); }
reeda3aff062014-11-13 10:40:58 -0800160
fmalitaf4905cc2014-11-13 11:29:07 -0800161 uint32_t onGetFlags() const SK_OVERRIDE {
162 return kSkip565_Flag;
163 }
164
fmalita65d79ce2014-11-19 09:23:22 -0800165 virtual void onOnceBeforeDraw() SK_OVERRIDE {
166 fInfo = SkImageInfo::MakeN32Premul(100, 100);
167 SkPictureRecorder recorder;
168 DrawText(recorder.beginRecording(SkIntToScalar(fInfo.width()),
169 SkIntToScalar(fInfo.height())));
170 SkAutoTUnref<SkPicture> pic(recorder.endRecording());
171 SkAutoTUnref<SkImageFilter> filter(SkPictureImageFilter::Create(pic.get()));
172 fFilterPaint.setImageFilter(filter.get());
173 }
174
reeda3aff062014-11-13 10:40:58 -0800175 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
176 const SkPixelGeometry geos[] = {
177 kRGB_H_SkPixelGeometry,
178 kUnknown_SkPixelGeometry,
179 };
180
reeda3aff062014-11-13 10:40:58 -0800181 for (size_t i = 0; i < SK_ARRAY_COUNT(geos); ++i) {
fmalita65d79ce2014-11-19 09:23:22 -0800182 SkAutoTUnref<SkSurface> surf(MakeSurface(canvas, fInfo, geos[i]));
reeda3aff062014-11-13 10:40:58 -0800183 DrawText(surf->getCanvas());
fmalita65d79ce2014-11-19 09:23:22 -0800184 surf->draw(canvas, SkIntToScalar(i * (fInfo.width() + 10)), 0, NULL);
185 }
186
187 for (size_t i = 0; i < SK_ARRAY_COUNT(geos); ++i) {
188 SkAutoTUnref<SkSurface> surf(MakeSurface(canvas, fInfo, geos[i]));
189 surf->getCanvas()->saveLayer(NULL, &fFilterPaint);
190 surf->getCanvas()->restore();
191 surf->draw(canvas,
192 SkIntToScalar(i * (fInfo.width() + 10)),
193 SkIntToScalar(fInfo.height() + 10),
194 NULL);
reeda3aff062014-11-13 10:40:58 -0800195 }
196 }
fmalita65d79ce2014-11-19 09:23:22 -0800197
198private:
199 SkPaint fFilterPaint;
200 SkImageInfo fInfo;
201
202 typedef skiagm::GM INHERITED;
reeda3aff062014-11-13 10:40:58 -0800203};
204
epoger@google.com213c42b2011-06-28 16:20:27 +0000205///////////////////////////////////////////////////////////////////////////////
206
reed4942e752014-10-01 13:59:33 -0700207DEF_GM( return new LcdTextGM; )
208DEF_GM( return new LcdTextSizeGM; )
Florin Malita66b4ce92014-11-13 15:00:03 -0500209// Temporarily disabled (dftext interference)
210// DEF_GM( return new LcdTextProps; )