blob: 0b5739c7abb95d37dfd0c540b263ea0b0389cf83 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
epoger@google.com213c42b2011-06-28 16:20:27 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
epoger@google.com213c42b2011-06-28 16:20:27 +00007 */
8
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
epoger@google.com213c42b2011-06-28 16:20:27 +000010/* Tests text rendering with LCD and subpixel rendering turned on and off.
11 */
12
13#include "gm.h"
14#include "SkCanvas.h"
15
reed4942e752014-10-01 13:59:33 -070016class LcdTextGM : public skiagm::GM {
epoger@google.com213c42b2011-06-28 16:20:27 +000017public:
18 LcdTextGM() {
19 const int pointSize = 36;
20 textHeight = SkIntToScalar(pointSize);
21 }
reed4942e752014-10-01 13:59:33 -070022
epoger@google.com213c42b2011-06-28 16:20:27 +000023protected:
reed4942e752014-10-01 13:59:33 -070024
epoger@google.com213c42b2011-06-28 16:20:27 +000025 SkString onShortName() {
26 return SkString("lcdtext");
27 }
reed4942e752014-10-01 13:59:33 -070028
tfarinaf5393182014-06-09 23:59:03 -070029 SkISize onISize() { return SkISize::Make(640, 480); }
reed4942e752014-10-01 13:59:33 -070030
epoger@google.com213c42b2011-06-28 16:20:27 +000031 virtual void onDraw(SkCanvas* canvas) {
reed4942e752014-10-01 13:59:33 -070032
epoger@google.com213c42b2011-06-28 16:20:27 +000033 y = textHeight;
34 drawText(canvas, SkString("TEXT: SubpixelTrue LCDRenderTrue"),
35 true, true);
36 drawText(canvas, SkString("TEXT: SubpixelTrue LCDRenderFalse"),
37 true, false);
38 drawText(canvas, SkString("TEXT: SubpixelFalse LCDRenderTrue"),
39 false, true);
40 drawText(canvas, SkString("TEXT: SubpixelFalse LCDRenderFalse"),
41 false, false);
42 }
reed4942e752014-10-01 13:59:33 -070043
epoger@google.com213c42b2011-06-28 16:20:27 +000044 void drawText(SkCanvas* canvas, const SkString& string,
45 bool subpixelTextEnabled, bool lcdRenderTextEnabled) {
46 SkPaint paint;
47 paint.setColor(SK_ColorBLACK);
48 paint.setDither(true);
49 paint.setAntiAlias(true);
Cary Clark992c7b02014-07-31 08:58:44 -040050 sk_tool_utils::set_portable_typeface(&paint);
epoger@google.com213c42b2011-06-28 16:20:27 +000051 paint.setSubpixelText(subpixelTextEnabled);
52 paint.setLCDRenderText(lcdRenderTextEnabled);
53 paint.setTextSize(textHeight);
reed4942e752014-10-01 13:59:33 -070054
epoger@google.com213c42b2011-06-28 16:20:27 +000055 canvas->drawText(string.c_str(), string.size(), 0, y, paint);
56 y += textHeight;
57 }
reed4942e752014-10-01 13:59:33 -070058
59private:
60 typedef skiagm::GM INHERITED;
61 SkScalar y, textHeight;
62};
63
64/*
65 * Skia will automatically disable LCD requests if the total size exceeds some limit
66 * (hard coded in this test for now, as it is now avaiable as an API)
67 *
68 * Test this both by changing "textsize" and by changing the computed size (textsize * CTM)
69 */
70class LcdTextSizeGM : public skiagm::GM {
71 enum {
72 kLCDTextSizeLimit = 48
73 };
74
75 static void ScaleAbout(SkCanvas* canvas, SkScalar sx, SkScalar sy, SkScalar px, SkScalar py) {
76 SkMatrix m;
77 m.setScale(sx, sy, px, py);
78 canvas->concat(m);
79 }
80
81public:
82 LcdTextSizeGM() {}
83
84protected:
85 SkString onShortName() {
86 return SkString("lcdtextsize");
87 }
88
89 SkISize onISize() { return SkISize::Make(320, 120); }
90
91 virtual void onDraw(SkCanvas* canvas) {
92 const char* lcd_text = "LCD";
93 const char* gray_text = "GRAY";
94
95 SkPaint paint;
96 paint.setAntiAlias(true);
97 paint.setLCDRenderText(true);
98
99 const struct {
100 SkPoint fLoc;
101 SkScalar fTextSize;
102 SkScalar fScale;
103 const char* fText;
104 } rec[] = {
105 { { 10, 50 }, kLCDTextSizeLimit - 1, 1, lcd_text },
106 { { 160, 50 }, kLCDTextSizeLimit + 1, 1, gray_text },
107 { { 10, 100 }, kLCDTextSizeLimit / 2, 1.99f, lcd_text },
108 { { 160, 100 }, kLCDTextSizeLimit / 2, 2.01f, gray_text },
109 };
110
111 for (size_t i = 0; i < SK_ARRAY_COUNT(rec); ++i) {
112 const SkPoint loc = rec[i].fLoc;
113 SkAutoCanvasRestore acr(canvas, true);
114
115 paint.setTextSize(rec[i].fTextSize);
116 ScaleAbout(canvas, rec[i].fScale, rec[i].fScale, loc.x(), loc.y());
117 canvas->drawText(rec[i].fText, strlen(rec[i].fText), loc.x(), loc.y(), paint);
118 }
119 }
epoger@google.com213c42b2011-06-28 16:20:27 +0000120
121private:
reed4942e752014-10-01 13:59:33 -0700122 typedef skiagm::GM INHERITED;
epoger@google.com213c42b2011-06-28 16:20:27 +0000123};
124
125///////////////////////////////////////////////////////////////////////////////
126
reed4942e752014-10-01 13:59:33 -0700127DEF_GM( return new LcdTextGM; )
128DEF_GM( return new LcdTextSizeGM; )