blob: fc6eb12721794b4a16d6f37a47b278f8910c078c [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
9#include "include/core/SkCanvas.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040010#include "include/core/SkColor.h"
11#include "include/core/SkFont.h"
12#include "include/core/SkMatrix.h"
13#include "include/core/SkPaint.h"
14#include "include/core/SkPoint.h"
15#include "include/core/SkRefCnt.h"
16#include "include/core/SkScalar.h"
17#include "include/core/SkSize.h"
18#include "include/core/SkString.h"
19#include "include/core/SkTypeface.h"
20#include "include/core/SkTypes.h"
epoger@google.com213c42b2011-06-28 16:20:27 +000021
reed4942e752014-10-01 13:59:33 -070022class LcdTextGM : public skiagm::GM {
epoger@google.com213c42b2011-06-28 16:20:27 +000023public:
24 LcdTextGM() {
25 const int pointSize = 36;
26 textHeight = SkIntToScalar(pointSize);
27 }
halcanary9d524f22016-03-29 09:03:52 -070028
epoger@google.com213c42b2011-06-28 16:20:27 +000029protected:
halcanary9d524f22016-03-29 09:03:52 -070030
epoger@google.com213c42b2011-06-28 16:20:27 +000031 SkString onShortName() {
Mike Kleinbea1f942019-03-08 11:11:55 -060032 return SkString("lcdtext");
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);
Hal Canary6ac0df82019-01-07 16:01:22 -050055 SkFont font(nullptr, textHeight);
56 if (subpixelTextEnabled) {
57 font.setSubpixel(true);
58 }
59 if (lcdRenderTextEnabled) {
60 font.setEdging(SkFont::Edging::kSubpixelAntiAlias);
61 }
62 canvas->drawString(string, 0, y, font, paint);
epoger@google.com213c42b2011-06-28 16:20:27 +000063 y += textHeight;
64 }
halcanary9d524f22016-03-29 09:03:52 -070065
reed4942e752014-10-01 13:59:33 -070066private:
67 typedef skiagm::GM INHERITED;
68 SkScalar y, textHeight;
69};
70
71/*
72 * Skia will automatically disable LCD requests if the total size exceeds some limit
73 * (hard coded in this test for now, as it is now avaiable as an API)
74 *
75 * Test this both by changing "textsize" and by changing the computed size (textsize * CTM)
76 */
77class LcdTextSizeGM : public skiagm::GM {
78 enum {
79 kLCDTextSizeLimit = 48
80 };
81
82 static void ScaleAbout(SkCanvas* canvas, SkScalar sx, SkScalar sy, SkScalar px, SkScalar py) {
83 SkMatrix m;
84 m.setScale(sx, sy, px, py);
85 canvas->concat(m);
86 }
87
88public:
89 LcdTextSizeGM() {}
halcanary9d524f22016-03-29 09:03:52 -070090
reed4942e752014-10-01 13:59:33 -070091protected:
92 SkString onShortName() {
93 return SkString("lcdtextsize");
94 }
halcanary9d524f22016-03-29 09:03:52 -070095
reed4942e752014-10-01 13:59:33 -070096 SkISize onISize() { return SkISize::Make(320, 120); }
halcanary9d524f22016-03-29 09:03:52 -070097
reed4942e752014-10-01 13:59:33 -070098 virtual void onDraw(SkCanvas* canvas) {
99 const char* lcd_text = "LCD";
100 const char* gray_text = "GRAY";
101
reed4942e752014-10-01 13:59:33 -0700102 const struct {
103 SkPoint fLoc;
104 SkScalar fTextSize;
105 SkScalar fScale;
106 const char* fText;
107 } rec[] = {
108 { { 10, 50 }, kLCDTextSizeLimit - 1, 1, lcd_text },
109 { { 160, 50 }, kLCDTextSizeLimit + 1, 1, gray_text },
110 { { 10, 100 }, kLCDTextSizeLimit / 2, 1.99f, lcd_text },
111 { { 160, 100 }, kLCDTextSizeLimit / 2, 2.01f, gray_text },
112 };
113
114 for (size_t i = 0; i < SK_ARRAY_COUNT(rec); ++i) {
115 const SkPoint loc = rec[i].fLoc;
116 SkAutoCanvasRestore acr(canvas, true);
117
Hal Canary6ac0df82019-01-07 16:01:22 -0500118 SkFont font(nullptr, rec[i].fTextSize);
119 font.setEdging(SkFont::Edging::kSubpixelAntiAlias);
120
reed4942e752014-10-01 13:59:33 -0700121 ScaleAbout(canvas, rec[i].fScale, rec[i].fScale, loc.x(), loc.y());
Hal Canary6ac0df82019-01-07 16:01:22 -0500122 canvas->drawString(rec[i].fText, loc.x(), loc.y(), font, SkPaint());
reed4942e752014-10-01 13:59:33 -0700123 }
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; )