blob: e605ac0c5716c95d68c34b00c4bf79219fff849c [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");
Mike Kleinb251b722017-11-13 11:03:16 -050032 name.append(sk_tool_utils::platform_font_manager());
caryclark37213552015-07-24 11:08:01 -070033 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);
Hal Canary6ac0df82019-01-07 16:01:22 -050056 SkFont font(nullptr, textHeight);
57 if (subpixelTextEnabled) {
58 font.setSubpixel(true);
59 }
60 if (lcdRenderTextEnabled) {
61 font.setEdging(SkFont::Edging::kSubpixelAntiAlias);
62 }
63 canvas->drawString(string, 0, y, font, paint);
epoger@google.com213c42b2011-06-28 16:20:27 +000064 y += textHeight;
65 }
halcanary9d524f22016-03-29 09:03:52 -070066
reed4942e752014-10-01 13:59:33 -070067private:
68 typedef skiagm::GM INHERITED;
69 SkScalar y, textHeight;
70};
71
72/*
73 * Skia will automatically disable LCD requests if the total size exceeds some limit
74 * (hard coded in this test for now, as it is now avaiable as an API)
75 *
76 * Test this both by changing "textsize" and by changing the computed size (textsize * CTM)
77 */
78class LcdTextSizeGM : public skiagm::GM {
79 enum {
80 kLCDTextSizeLimit = 48
81 };
82
83 static void ScaleAbout(SkCanvas* canvas, SkScalar sx, SkScalar sy, SkScalar px, SkScalar py) {
84 SkMatrix m;
85 m.setScale(sx, sy, px, py);
86 canvas->concat(m);
87 }
88
89public:
90 LcdTextSizeGM() {}
halcanary9d524f22016-03-29 09:03:52 -070091
reed4942e752014-10-01 13:59:33 -070092protected:
93 SkString onShortName() {
94 return SkString("lcdtextsize");
95 }
halcanary9d524f22016-03-29 09:03:52 -070096
reed4942e752014-10-01 13:59:33 -070097 SkISize onISize() { return SkISize::Make(320, 120); }
halcanary9d524f22016-03-29 09:03:52 -070098
reed4942e752014-10-01 13:59:33 -070099 virtual void onDraw(SkCanvas* canvas) {
100 const char* lcd_text = "LCD";
101 const char* gray_text = "GRAY";
102
reed4942e752014-10-01 13:59:33 -0700103 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
Hal Canary6ac0df82019-01-07 16:01:22 -0500119 SkFont font(nullptr, rec[i].fTextSize);
120 font.setEdging(SkFont::Edging::kSubpixelAntiAlias);
121
reed4942e752014-10-01 13:59:33 -0700122 ScaleAbout(canvas, rec[i].fScale, rec[i].fScale, loc.x(), loc.y());
Hal Canary6ac0df82019-01-07 16:01:22 -0500123 canvas->drawString(rec[i].fText, loc.x(), loc.y(), font, SkPaint());
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; )