blob: 978bfd641ee1ca6afea86337a478a001408012e0 [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 }
halcanary9d524f22016-03-29 09:03:52 -070026
epoger@google.com213c42b2011-06-28 16:20:27 +000027protected:
halcanary9d524f22016-03-29 09:03:52 -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 }
halcanary9d524f22016-03-29 09:03:52 -070034
tfarinaf5393182014-06-09 23:59:03 -070035 SkISize onISize() { return SkISize::Make(640, 480); }
halcanary9d524f22016-03-29 09:03:52 -070036
epoger@google.com213c42b2011-06-28 16:20:27 +000037 virtual void onDraw(SkCanvas* canvas) {
halcanary9d524f22016-03-29 09:03:52 -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 }
halcanary9d524f22016-03-29 09:03:52 -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);
halcanary9d524f22016-03-29 09:03:52 -070059
epoger@google.com213c42b2011-06-28 16:20:27 +000060 canvas->drawText(string.c_str(), string.size(), 0, y, paint);
61 y += textHeight;
62 }
halcanary9d524f22016-03-29 09:03:52 -070063
reed4942e752014-10-01 13:59:33 -070064private:
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() {}
halcanary9d524f22016-03-29 09:03:52 -070088
reed4942e752014-10-01 13:59:33 -070089protected:
90 SkString onShortName() {
91 return SkString("lcdtextsize");
92 }
halcanary9d524f22016-03-29 09:03:52 -070093
reed4942e752014-10-01 13:59:33 -070094 SkISize onISize() { return SkISize::Make(320, 120); }
halcanary9d524f22016-03-29 09:03:52 -070095
reed4942e752014-10-01 13:59:33 -070096 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};
reed4942e752014-10-01 13:59:33 -0700129DEF_GM( return new LcdTextGM; )
130DEF_GM( return new LcdTextSizeGM; )
reed70ee31b2015-12-10 13:44:45 -0800131
132///////////////////////////////////////////////////////////////////////////////////////////////////
133
134DEF_SIMPLE_GM(savelayer_lcdtext, canvas, 620, 260) {
135 SkPaint paint;
136 paint.setAntiAlias(true);
137 paint.setLCDRenderText(true);
138 paint.setTextSize(20);
139
140 canvas->drawText("Hamburgefons", 12, 30, 30, paint);
141
142 const bool gPreserveLCDText[] = { false, true };
143
144 canvas->translate(0, 20);
145 for (auto preserve : gPreserveLCDText) {
146 preserve ? canvas->saveLayerPreserveLCDTextRequests(nullptr, nullptr)
147 : canvas->saveLayer(nullptr, nullptr);
bungeman4b323602016-03-24 08:17:55 -0700148 if (preserve) {
149 SkPaint noLCD = paint;
150 noLCD.setLCDRenderText(false);
151 canvas->drawText("LCD not supported", 17, 30, 60, noLCD);
152 } else {
153 canvas->drawText("Hamburgefons", 12, 30, 60, paint);
154 }
reed70ee31b2015-12-10 13:44:45 -0800155
156 SkPaint p;
157 p.setColor(0xFFCCCCCC);
158 canvas->drawRect(SkRect::MakeLTRB(25, 70, 200, 100), p);
159 canvas->drawText("Hamburgefons", 12, 30, 90, paint);
160
161 canvas->restore();
162 canvas->translate(0, 80);
163 }
164}