Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
lcdtext.cpp
Go to the documentation of this file.
1/*
2 * 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.
6 */
7
8#include "gm/gm.h"
11#include "include/core/SkFont.h"
17#include "include/core/SkSize.h"
22
23class LcdTextGM : public skiagm::GM {
24 static constexpr SkScalar kTextHeight = 36;
26
27 SkString getName() const override { return SkString("lcdtext"); }
28
29 SkISize getISize() override { return {640, 480}; }
30
31 void onDraw(SkCanvas* canvas) override {
32 fY = kTextHeight;
33 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 }
42
43 void drawText(SkCanvas* canvas, const SkString& string,
44 bool subpixelTextEnabled, bool lcdRenderTextEnabled) {
46 paint.setColor(SK_ColorBLACK);
47 paint.setDither(true);
48 SkFont font(ToolUtils::DefaultPortableTypeface(), kTextHeight);
49 if (subpixelTextEnabled) {
50 font.setSubpixel(true);
51 }
52 if (lcdRenderTextEnabled) {
54 }
55 canvas->drawString(string, 0, fY, font, paint);
56 fY += kTextHeight;
57 }
58};
59
60/*
61 * Skia will automatically disable LCD requests if the total size exceeds some limit
62 * (hard coded in this test for now, as it is now available as an API)
63 *
64 * Test this both by changing "textsize" and by changing the computed size (textsize * CTM)
65 */
66class LcdTextSizeGM : public skiagm::GM {
67 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
73 SkString getName() const override { return SkString("lcdtextsize"); }
74
75 SkISize getISize() override { return {320, 120}; }
76
77 void onDraw(SkCanvas* canvas) override {
78 const char* lcd_text = "LCD";
79 const char* gray_text = "GRAY";
80
81 constexpr static float kLCDTextSizeLimit = 48;
82
83 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
95 for (size_t i = 0; i < std::size(rec); ++i) {
96 const SkPoint loc = rec[i].fLoc;
97 SkAutoCanvasRestore acr(canvas, true);
98
99 SkFont font(ToolUtils::DefaultPortableTypeface(), rec[i].fTextSize);
101
102 ScaleAbout(canvas, rec[i].fScale, rec[i].fScale, loc.x(), loc.y());
103 canvas->drawString(rec[i].fText, loc.x(), loc.y(), font, SkPaint());
104 }
105 }
106};
107
109 static constexpr SkScalar kTextHeight = 36;
110
111 SkString getName() const override { return SkString("savelayerpreservelcdtext"); }
112
113 SkISize getISize() override { return {620, 300}; }
114
115 void onDraw(SkCanvas* canvas) override {
116 drawText(canvas, SkString("SaveLayer PreserveLCDText"), 50,
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);
128 paint.setColor(SK_ColorWHITE);
129 canvas->drawRect(SkRect::MakeXYWH(0, y - 10, 640, kTextHeight + 20), paint);
130 paint.setColor(SK_ColorBLACK);
131 SkFont font(ToolUtils::DefaultPortableTypeface(), kTextHeight);
133 canvas->drawString(string, 10, y, font, paint);
134 canvas->restore();
135 }
136};
137
138DEF_GM(return new LcdTextGM;)
139DEF_GM(return new LcdTextSizeGM;)
constexpr SkColor SK_ColorBLACK
Definition SkColor.h:103
constexpr SkColor SK_ColorWHITE
Definition SkColor.h:122
void onDraw(SkCanvas *canvas) override
Definition lcdtext.cpp:31
SkString getName() const override
Definition lcdtext.cpp:27
SkISize getISize() override
Definition lcdtext.cpp:29
SkISize getISize() override
Definition lcdtext.cpp:75
SkString getName() const override
Definition lcdtext.cpp:73
void onDraw(SkCanvas *canvas) override
Definition lcdtext.cpp:77
SkISize getISize() override
Definition lcdtext.cpp:113
void onDraw(SkCanvas *canvas) override
Definition lcdtext.cpp:115
SkString getName() const override
Definition lcdtext.cpp:111
int saveLayer(const SkRect *bounds, const SkPaint *paint)
Definition SkCanvas.cpp:500
uint32_t SaveLayerFlags
Definition SkCanvas.h:677
void drawRect(const SkRect &rect, const SkPaint &paint)
void restore()
Definition SkCanvas.cpp:465
@ kPreserveLCDText_SaveLayerFlag
Definition SkCanvas.h:671
void concat(const SkMatrix &matrix)
void drawString(const char str[], SkScalar x, SkScalar y, const SkFont &font, const SkPaint &paint)
Definition SkCanvas.h:1803
@ kSubpixelAntiAlias
glyph positioned in pixel using transparency
const Paint & paint
float SkScalar
Definition extension.cpp:12
constexpr SkScalar kTextHeight
Definition glyph_pos.cpp:27
#define DEF_GM(CODE)
Definition gm.h:40
double y
sk_sp< SkTypeface > DefaultPortableTypeface()
font
Font Metadata and Metrics.
constexpr float y() const
constexpr float x() const
static constexpr SkRect MakeXYWH(float x, float y, float w, float h)
Definition SkRect.h:659