Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
scaledemoji_rendering.cpp
Go to the documentation of this file.
1/*
2* Copyright 2018 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"
18#include "include/core/SkSize.h"
22#include "tools/Resources.h"
23#include "tools/ToolUtils.h"
25
26#include <string.h>
27#include <initializer_list>
28
29namespace skiagm {
30class ScaledEmojiRenderingGM : public GM {
31public:
33
34protected:
43 void onOnceBeforeDraw() override {
44 for (auto&& [i, format] : SkMakeEnumerate(formatsToTest)) {
46 if (!fontSamples[i].typeface) {
48 }
49 }
50 }
51
52 SkString getName() const override { return SkString("scaledemoji_rendering"); }
53
54 SkISize getISize() override { return SkISize::Make(1200, 1200); }
55
56 void onDraw(SkCanvas* canvas) override {
57
58 canvas->drawColor(SK_ColorGRAY);
59 SkPaint textPaint;
60 textPaint.setColor(SK_ColorCYAN);
61
62 SkPaint boundsPaint;
63 boundsPaint.setStrokeWidth(2);
65 boundsPaint.setColor(SK_ColorGREEN);
66
67 SkPaint advancePaint;
68 advancePaint.setColor(SK_ColorRED);
69
70 SkScalar y = 0;
71 for (auto& sample : fontSamples) {
72 SkFont font(sample.typeface);
73 font.setEdging(SkFont::Edging::kAlias);
74
75 const char* text = sample.sampleText;
76 SkFontMetrics metrics;
77
78 for (SkScalar textSize : { 70, 150 }) {
79 font.setSize(textSize);
80 font.getMetrics(&metrics);
81 // All typefaces should support subpixel mode
82 font.setSubpixel(true);
83
84 y += -metrics.fAscent;
85
86 SkScalar x = 0;
87 for (bool fakeBold : { false, true }) {
88 font.setEmbolden(fakeBold);
89 SkRect bounds;
90 SkScalar advance = font.measureText(text, strlen(text), SkTextEncoding::kUTF8,
91 &bounds, &textPaint);
93 x, y, font, textPaint);
94 if ((false)) {
95 bounds.offset(x, y);
96 canvas->drawRect(bounds, boundsPaint);
97 SkRect advanceRect = SkRect::MakeLTRB(x, y + 2, x + advance, y + 4);
98 canvas->drawRect(advanceRect, advancePaint);
99 }
100 x += bounds.width() * 1.2;
101 }
102 y += metrics.fDescent + metrics.fLeading;
103 x = 0;
104 }
105 }
106 }
107
108private:
109 using INHERITED = GM;
110};
111
112//////////////////////////////////////////////////////////////////////////////
113
114DEF_GM(return new ScaledEmojiRenderingGM;)
115} // namespace skiagm
constexpr SkColor SK_ColorCYAN
Definition SkColor.h:143
constexpr SkColor SK_ColorGRAY
Definition SkColor.h:113
constexpr SkColor SK_ColorRED
Definition SkColor.h:126
constexpr SkColor SK_ColorGREEN
Definition SkColor.h:131
constexpr SkEnumerate< Iter > SkMakeEnumerate(C &c)
@ kUTF8
uses bytes to represent UTF-8 or ASCII
void drawRect(const SkRect &rect, const SkPaint &paint)
void drawSimpleText(const void *text, size_t byteLength, SkTextEncoding encoding, SkScalar x, SkScalar y, const SkFont &font, const SkPaint &paint)
void drawColor(SkColor color, SkBlendMode mode=SkBlendMode::kSrcOver)
Definition SkCanvas.h:1182
@ kAlias
no transparent pixels on glyph edges
void setStyle(Style style)
Definition SkPaint.cpp:105
void setColor(SkColor color)
Definition SkPaint.cpp:119
@ kStroke_Style
set to stroke geometry
Definition SkPaint.h:194
void setStrokeWidth(SkScalar width)
Definition SkPaint.cpp:159
void onDraw(SkCanvas *canvas) override
ToolUtils::EmojiTestSample fontSamples[std::size(formatsToTest)]
static constexpr ToolUtils::EmojiFontFormat formatsToTest[]
float SkScalar
Definition extension.cpp:12
uint32_t uint32_t * format
#define DEF_GM(CODE)
Definition gm.h:40
std::u16string text
double y
double x
EmojiTestSample EmojiSample()
sk_sp< SkTypeface > DefaultTypeface()
SkScalar fLeading
distance to add between lines, typically positive or zero
SkScalar fAscent
distance to reserve above baseline, typically negative
SkScalar fDescent
distance to reserve below baseline, typically positive
static constexpr SkISize Make(int32_t w, int32_t h)
Definition SkSize.h:20
static constexpr SkRect MakeLTRB(float l, float t, float r, float b)
Definition SkRect.h:646
sk_sp< SkTypeface > typeface