Flutter Engine
The Flutter Engine
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);
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
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,
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);
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
int saveLayer(const SkRect *bounds, const SkPaint *paint)
Definition: SkCanvas.cpp:496
uint32_t SaveLayerFlags
Definition: SkCanvas.h:677
void drawRect(const SkRect &rect, const SkPaint &paint)
Definition: SkCanvas.cpp:1673
void restore()
Definition: SkCanvas.cpp:461
@ kPreserveLCDText_SaveLayerFlag
Definition: SkCanvas.h:671
void concat(const SkMatrix &matrix)
Definition: SkCanvas.cpp:1318
void drawString(const char str[], SkScalar x, SkScalar y, const SkFont &font, const SkPaint &paint)
Definition: SkCanvas.h:1803
Definition: SkFont.h:35
@ kSubpixelAntiAlias
glyph positioned in pixel using transparency
Definition: gm.h:110
const Paint & paint
Definition: color_source.cc:38
float SkScalar
Definition: extension.cpp:12
constexpr SkScalar kTextHeight
Definition: glyph_pos.cpp:27
#define DEF_GM(CODE)
Definition: gm.h:40
double y
SkCanvas::SaveLayerFlags saveLayerFlags
Definition: SkRecords.h:192
sk_sp< SkTypeface > DefaultPortableTypeface()
it will be possible to load the file into Perfetto s trace viewer disable asset Prevents usage of any non test fonts unless they were explicitly Loaded via prefetched default font Indicates whether the embedding started a prefetch of the default font manager before creating the engine run In non interactive keep the shell running after the Dart script has completed enable serial On low power devices with low core running concurrent GC tasks on threads can cause them to contend with the UI thread which could potentially lead to jank This option turns off all concurrent GC activities domain network JSON encoded network policy per domain This overrides the DisallowInsecureConnections switch Embedder can specify whether to allow or disallow insecure connections at a domain level old gen heap size
Definition: switches.h:259
font
Font Metadata and Metrics.
Definition: SkSize.h:16
constexpr float y() const
Definition: SkPoint_impl.h:187
constexpr float x() const
Definition: SkPoint_impl.h:181
static constexpr SkRect MakeXYWH(float x, float y, float w, float h)
Definition: SkRect.h:659