blob: 4c283e14533044d077cff73a0d880255d162b0a0 [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"
Kevin Lubickbca43ec2023-10-30 10:11:22 -040021#include "tools/fonts/FontToolUtils.h"
epoger@google.com213c42b2011-06-28 16:20:27 +000022
reed4942e752014-10-01 13:59:33 -070023class LcdTextGM : public skiagm::GM {
Hal Canaryfa3305a2019-07-18 12:36:54 -040024 static constexpr SkScalar kTextHeight = 36;
25 SkScalar fY = kTextHeight;
halcanary9d524f22016-03-29 09:03:52 -070026
Leandro Lovisolo24fa2112023-08-15 19:05:17 +000027 SkString getName() const override { return SkString("lcdtext"); }
halcanary9d524f22016-03-29 09:03:52 -070028
Leandro Lovisolo8f023882023-08-15 21:13:52 +000029 SkISize getISize() override { return {640, 480}; }
halcanary9d524f22016-03-29 09:03:52 -070030
Hal Canaryfa3305a2019-07-18 12:36:54 -040031 void onDraw(SkCanvas* canvas) override {
32 fY = kTextHeight;
epoger@google.com213c42b2011-06-28 16:20:27 +000033 drawText(canvas, SkString("TEXT: SubpixelTrue LCDRenderTrue"),
34 true, true);
35 drawText(canvas, SkString("TEXT: SubpixelTrue LCDRenderFalse"),
36 true, false);
37 drawText(canvas, SkString("TEXT: SubpixelFalse LCDRenderTrue"),
38 false, true);
39 drawText(canvas, SkString("TEXT: SubpixelFalse LCDRenderFalse"),
40 false, false);
41 }
halcanary9d524f22016-03-29 09:03:52 -070042
epoger@google.com213c42b2011-06-28 16:20:27 +000043 void drawText(SkCanvas* canvas, const SkString& string,
44 bool subpixelTextEnabled, bool lcdRenderTextEnabled) {
45 SkPaint paint;
46 paint.setColor(SK_ColorBLACK);
47 paint.setDither(true);
Kevin Lubickbca43ec2023-10-30 10:11:22 -040048 SkFont font(ToolUtils::DefaultPortableTypeface(), kTextHeight);
Hal Canary6ac0df82019-01-07 16:01:22 -050049 if (subpixelTextEnabled) {
50 font.setSubpixel(true);
51 }
52 if (lcdRenderTextEnabled) {
53 font.setEdging(SkFont::Edging::kSubpixelAntiAlias);
54 }
Hal Canaryfa3305a2019-07-18 12:36:54 -040055 canvas->drawString(string, 0, fY, font, paint);
56 fY += kTextHeight;
epoger@google.com213c42b2011-06-28 16:20:27 +000057 }
reed4942e752014-10-01 13:59:33 -070058};
59
60/*
61 * Skia will automatically disable LCD requests if the total size exceeds some limit
Kevin Lubickbca43ec2023-10-30 10:11:22 -040062 * (hard coded in this test for now, as it is now available as an API)
reed4942e752014-10-01 13:59:33 -070063 *
64 * Test this both by changing "textsize" and by changing the computed size (textsize * CTM)
65 */
66class LcdTextSizeGM : public skiagm::GM {
reed4942e752014-10-01 13:59:33 -070067 static void ScaleAbout(SkCanvas* canvas, SkScalar sx, SkScalar sy, SkScalar px, SkScalar py) {
68 SkMatrix m;
69 m.setScale(sx, sy, px, py);
70 canvas->concat(m);
71 }
72
Leandro Lovisolo24fa2112023-08-15 19:05:17 +000073 SkString getName() const override { return SkString("lcdtextsize"); }
halcanary9d524f22016-03-29 09:03:52 -070074
Leandro Lovisolo8f023882023-08-15 21:13:52 +000075 SkISize getISize() override { return {320, 120}; }
halcanary9d524f22016-03-29 09:03:52 -070076
Hal Canaryfa3305a2019-07-18 12:36:54 -040077 void onDraw(SkCanvas* canvas) override {
reed4942e752014-10-01 13:59:33 -070078 const char* lcd_text = "LCD";
79 const char* gray_text = "GRAY";
80
Hal Canaryfa3305a2019-07-18 12:36:54 -040081 constexpr static float kLCDTextSizeLimit = 48;
82
reed4942e752014-10-01 13:59:33 -070083 const struct {
84 SkPoint fLoc;
85 SkScalar fTextSize;
86 SkScalar fScale;
87 const char* fText;
88 } rec[] = {
89 { { 10, 50 }, kLCDTextSizeLimit - 1, 1, lcd_text },
90 { { 160, 50 }, kLCDTextSizeLimit + 1, 1, gray_text },
91 { { 10, 100 }, kLCDTextSizeLimit / 2, 1.99f, lcd_text },
92 { { 160, 100 }, kLCDTextSizeLimit / 2, 2.01f, gray_text },
93 };
94
Herb Derbyc37b3862022-06-21 09:49:17 -040095 for (size_t i = 0; i < std::size(rec); ++i) {
reed4942e752014-10-01 13:59:33 -070096 const SkPoint loc = rec[i].fLoc;
97 SkAutoCanvasRestore acr(canvas, true);
98
Kevin Lubickbca43ec2023-10-30 10:11:22 -040099 SkFont font(ToolUtils::DefaultPortableTypeface(), rec[i].fTextSize);
Hal Canary6ac0df82019-01-07 16:01:22 -0500100 font.setEdging(SkFont::Edging::kSubpixelAntiAlias);
101
reed4942e752014-10-01 13:59:33 -0700102 ScaleAbout(canvas, rec[i].fScale, rec[i].fScale, loc.x(), loc.y());
Hal Canary6ac0df82019-01-07 16:01:22 -0500103 canvas->drawString(rec[i].fText, loc.x(), loc.y(), font, SkPaint());
reed4942e752014-10-01 13:59:33 -0700104 }
105 }
epoger@google.com213c42b2011-06-28 16:20:27 +0000106};
Xianzhu Wanga91e4782020-05-01 09:34:57 -0700107
108class SaveLayerPreserveLCDTextGM : public skiagm::GM {
109 static constexpr SkScalar kTextHeight = 36;
110
Leandro Lovisolo24fa2112023-08-15 19:05:17 +0000111 SkString getName() const override { return SkString("savelayerpreservelcdtext"); }
Xianzhu Wanga91e4782020-05-01 09:34:57 -0700112
Leandro Lovisolo8f023882023-08-15 21:13:52 +0000113 SkISize getISize() override { return {620, 300}; }
Xianzhu Wanga91e4782020-05-01 09:34:57 -0700114
115 void onDraw(SkCanvas* canvas) override {
116 drawText(canvas, SkString("SaveLayer PreserveLCDText"), 50,
117 SkCanvas::kPreserveLCDText_SaveLayerFlag);
118 drawText(canvas, SkString("SaveLayer Default (LCDText not preserved)"), 150, 0);
119 }
120
121 void drawText(SkCanvas* canvas,
122 const SkString& string,
123 int y,
124 SkCanvas::SaveLayerFlags saveLayerFlags) {
125 SkCanvas::SaveLayerRec rec(nullptr, nullptr, saveLayerFlags);
126 canvas->saveLayer(rec);
127 SkPaint paint;
128 paint.setColor(SK_ColorWHITE);
129 canvas->drawRect(SkRect::MakeXYWH(0, y - 10, 640, kTextHeight + 20), paint);
130 paint.setColor(SK_ColorBLACK);
Kevin Lubickbca43ec2023-10-30 10:11:22 -0400131 SkFont font(ToolUtils::DefaultPortableTypeface(), kTextHeight);
Xianzhu Wanga91e4782020-05-01 09:34:57 -0700132 font.setEdging(SkFont::Edging::kSubpixelAntiAlias);
133 canvas->drawString(string, 10, y, font, paint);
134 canvas->restore();
135 }
136};
137
138DEF_GM(return new LcdTextGM;)
139DEF_GM(return new LcdTextSizeGM;)
140DEF_GM(return new SaveLayerPreserveLCDTextGM;)