blob: 5a549eb74c7def85473d199348701086435cad87 [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"
Mike Klein33d20552017-03-22 13:47:51 -040013#include "sk_tool_utils.h"
epoger@google.com213c42b2011-06-28 16:20:27 +000014#include "SkCanvas.h"
fmalita65d79ce2014-11-19 09:23:22 -080015#include "SkPicture.h"
16#include "SkPictureImageFilter.h"
17#include "SkPictureRecorder.h"
18#include "SkSurface.h"
19
epoger@google.com213c42b2011-06-28 16:20:27 +000020
reed4942e752014-10-01 13:59:33 -070021class LcdTextGM : public skiagm::GM {
epoger@google.com213c42b2011-06-28 16:20:27 +000022public:
23 LcdTextGM() {
24 const int pointSize = 36;
25 textHeight = SkIntToScalar(pointSize);
26 }
halcanary9d524f22016-03-29 09:03:52 -070027
epoger@google.com213c42b2011-06-28 16:20:27 +000028protected:
halcanary9d524f22016-03-29 09:03:52 -070029
epoger@google.com213c42b2011-06-28 16:20:27 +000030 SkString onShortName() {
caryclark37213552015-07-24 11:08:01 -070031 SkString name("lcdtext");
32 name.append(sk_tool_utils::major_platform_os_name());
33 return name;
epoger@google.com213c42b2011-06-28 16:20:27 +000034 }
halcanary9d524f22016-03-29 09:03:52 -070035
tfarinaf5393182014-06-09 23:59:03 -070036 SkISize onISize() { return SkISize::Make(640, 480); }
halcanary9d524f22016-03-29 09:03:52 -070037
epoger@google.com213c42b2011-06-28 16:20:27 +000038 virtual void onDraw(SkCanvas* canvas) {
halcanary9d524f22016-03-29 09:03:52 -070039
epoger@google.com213c42b2011-06-28 16:20:27 +000040 y = textHeight;
41 drawText(canvas, SkString("TEXT: SubpixelTrue LCDRenderTrue"),
42 true, true);
43 drawText(canvas, SkString("TEXT: SubpixelTrue LCDRenderFalse"),
44 true, false);
45 drawText(canvas, SkString("TEXT: SubpixelFalse LCDRenderTrue"),
46 false, true);
47 drawText(canvas, SkString("TEXT: SubpixelFalse LCDRenderFalse"),
48 false, false);
49 }
halcanary9d524f22016-03-29 09:03:52 -070050
epoger@google.com213c42b2011-06-28 16:20:27 +000051 void drawText(SkCanvas* canvas, const SkString& string,
52 bool subpixelTextEnabled, bool lcdRenderTextEnabled) {
53 SkPaint paint;
54 paint.setColor(SK_ColorBLACK);
55 paint.setDither(true);
56 paint.setAntiAlias(true);
epoger@google.com213c42b2011-06-28 16:20:27 +000057 paint.setSubpixelText(subpixelTextEnabled);
58 paint.setLCDRenderText(lcdRenderTextEnabled);
59 paint.setTextSize(textHeight);
halcanary9d524f22016-03-29 09:03:52 -070060
epoger@google.com213c42b2011-06-28 16:20:27 +000061 canvas->drawText(string.c_str(), string.size(), 0, y, paint);
62 y += textHeight;
63 }
halcanary9d524f22016-03-29 09:03:52 -070064
reed4942e752014-10-01 13:59:33 -070065private:
66 typedef skiagm::GM INHERITED;
67 SkScalar y, textHeight;
68};
69
70/*
71 * Skia will automatically disable LCD requests if the total size exceeds some limit
72 * (hard coded in this test for now, as it is now avaiable as an API)
73 *
74 * Test this both by changing "textsize" and by changing the computed size (textsize * CTM)
75 */
76class LcdTextSizeGM : public skiagm::GM {
77 enum {
78 kLCDTextSizeLimit = 48
79 };
80
81 static void ScaleAbout(SkCanvas* canvas, SkScalar sx, SkScalar sy, SkScalar px, SkScalar py) {
82 SkMatrix m;
83 m.setScale(sx, sy, px, py);
84 canvas->concat(m);
85 }
86
87public:
88 LcdTextSizeGM() {}
halcanary9d524f22016-03-29 09:03:52 -070089
reed4942e752014-10-01 13:59:33 -070090protected:
91 SkString onShortName() {
92 return SkString("lcdtextsize");
93 }
halcanary9d524f22016-03-29 09:03:52 -070094
reed4942e752014-10-01 13:59:33 -070095 SkISize onISize() { return SkISize::Make(320, 120); }
halcanary9d524f22016-03-29 09:03:52 -070096
reed4942e752014-10-01 13:59:33 -070097 virtual void onDraw(SkCanvas* canvas) {
98 const char* lcd_text = "LCD";
99 const char* gray_text = "GRAY";
100
101 SkPaint paint;
102 paint.setAntiAlias(true);
103 paint.setLCDRenderText(true);
104
105 const struct {
106 SkPoint fLoc;
107 SkScalar fTextSize;
108 SkScalar fScale;
109 const char* fText;
110 } rec[] = {
111 { { 10, 50 }, kLCDTextSizeLimit - 1, 1, lcd_text },
112 { { 160, 50 }, kLCDTextSizeLimit + 1, 1, gray_text },
113 { { 10, 100 }, kLCDTextSizeLimit / 2, 1.99f, lcd_text },
114 { { 160, 100 }, kLCDTextSizeLimit / 2, 2.01f, gray_text },
115 };
116
117 for (size_t i = 0; i < SK_ARRAY_COUNT(rec); ++i) {
118 const SkPoint loc = rec[i].fLoc;
119 SkAutoCanvasRestore acr(canvas, true);
120
121 paint.setTextSize(rec[i].fTextSize);
122 ScaleAbout(canvas, rec[i].fScale, rec[i].fScale, loc.x(), loc.y());
123 canvas->drawText(rec[i].fText, strlen(rec[i].fText), loc.x(), loc.y(), paint);
124 }
125 }
epoger@google.com213c42b2011-06-28 16:20:27 +0000126
127private:
reed4942e752014-10-01 13:59:33 -0700128 typedef skiagm::GM INHERITED;
epoger@google.com213c42b2011-06-28 16:20:27 +0000129};
reed4942e752014-10-01 13:59:33 -0700130DEF_GM( return new LcdTextGM; )
131DEF_GM( return new LcdTextSizeGM; )
reed70ee31b2015-12-10 13:44:45 -0800132
133///////////////////////////////////////////////////////////////////////////////////////////////////
134
135DEF_SIMPLE_GM(savelayer_lcdtext, canvas, 620, 260) {
136 SkPaint paint;
137 paint.setAntiAlias(true);
138 paint.setLCDRenderText(true);
139 paint.setTextSize(20);
140
141 canvas->drawText("Hamburgefons", 12, 30, 30, paint);
142
143 const bool gPreserveLCDText[] = { false, true };
144
145 canvas->translate(0, 20);
146 for (auto preserve : gPreserveLCDText) {
147 preserve ? canvas->saveLayerPreserveLCDTextRequests(nullptr, nullptr)
148 : canvas->saveLayer(nullptr, nullptr);
bungeman4b323602016-03-24 08:17:55 -0700149 if (preserve) {
150 SkPaint noLCD = paint;
151 noLCD.setLCDRenderText(false);
152 canvas->drawText("LCD not supported", 17, 30, 60, noLCD);
153 } else {
154 canvas->drawText("Hamburgefons", 12, 30, 60, paint);
155 }
reed70ee31b2015-12-10 13:44:45 -0800156
157 SkPaint p;
158 p.setColor(0xFFCCCCCC);
159 canvas->drawRect(SkRect::MakeLTRB(25, 70, 200, 100), p);
160 canvas->drawText("Hamburgefons", 12, 30, 90, paint);
161
162 canvas->restore();
163 canvas->translate(0, 80);
164 }
165}