blob: 1b377ba8182efdc6452f1565b477963c71893a1a [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
Cary Clark2a475ea2017-04-28 15:35:12 -040061 canvas->drawString(string, 0, y, paint);
epoger@google.com213c42b2011-06-28 16:20:27 +000062 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());
Cary Clark2a475ea2017-04-28 15:35:12 -0400123 canvas->drawString(rec[i].fText, loc.x(), loc.y(), paint);
reed4942e752014-10-01 13:59:33 -0700124 }
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
Cary Clark2a475ea2017-04-28 15:35:12 -0400141 canvas->drawString("Hamburgefons", 30, 30, paint);
reed70ee31b2015-12-10 13:44:45 -0800142
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);
Brian Osman0610a462017-05-23 17:09:19 -0400149 if (preserve && !canvas->imageInfo().colorSpace()) {
150 SkPaint noLCD = paint;
151 noLCD.setLCDRenderText(false);
152 canvas->drawString("LCD not supported", 30, 60, noLCD);
153 } else {
154 canvas->drawString("Hamburgefons", 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);
Cary Clark2a475ea2017-04-28 15:35:12 -0400160 canvas->drawString("Hamburgefons", 30, 90, paint);
reed70ee31b2015-12-10 13:44:45 -0800161
162 canvas->restore();
163 canvas->translate(0, 80);
164 }
165}