Flutter Engine
The Flutter Engine
fontscalerdistortable.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2015 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"
10#include "include/core/SkFont.h"
15#include "include/core/SkRect.h"
18#include "include/core/SkSize.h"
23#include "tools/Resources.h"
24#include "tools/SkMetaData.h"
25#include "tools/ToolUtils.h"
27
28#include <string.h>
29#include <memory>
30#include <utility>
31
32namespace skiagm {
33
35public:
37 this->setBGColor(0xFFFFFFFF);
38 }
39
40private:
41 SkString getName() const override { return SkString("fontscalerdistortable"); }
42
43 SkISize getISize() override { return SkISize::Make(550, 700); }
44
45 bool fDirty = true;
46 bool fOverride = false;
47
48 ToolUtils::VariationSliders fVariationSliders;
49
50 bool onGetControls(SkMetaData* controls) override {
51 controls->setBool("Override", fOverride);
52 return fVariationSliders.writeControls(controls);
53 }
54
55 void onSetControls(const SkMetaData& controls) override {
56 bool oldOverride = fOverride;
57 controls.findBool("Override", &fOverride);
58 if (fOverride != oldOverride) {
59 fDirty = true;
60 }
61
62 return fVariationSliders.readControls(controls, &fDirty);
63 }
64
65 struct Info {
66 sk_sp<SkTypeface> distortable;
67 SkFourByteTag axisTag;
68 SkScalar axisMin;
69 SkScalar axisMax;
70 } fInfo;
71
72 void onOnceBeforeDraw() override {
73 constexpr SkFourByteTag wght = SkSetFourByteTag('w','g','h','t');
74 //constexpr SkFourByteTag wdth = SkSetFourByteTag('w','d','t','h');
75 fInfo = {
76 ToolUtils::CreateTypefaceFromResource("fonts/Distortable.ttf"), wght, 0.5f, 2.0f
77 //SkTypeface::MakeFromFile("/Library/Fonts/Skia.ttf"), wght, 0.48f, 3.2f
78 //ToolUtils::CreateTestTypeface("Skia", SkFontStyle()), wdth, 0.62f, 1.3f
79 //SkTypeface::MakeFromFile("/System/Library/Fonts/SFNS.ttf"), wght, 100.0f, 900.0f
80 //ToolUtils::CreateTestTypeface(".SF NS", SkFontStyle()), wght, 100.0f, 900.0f
81 };
82
83 if (!fInfo.distortable) {
84 fInfo.distortable = ToolUtils::DefaultPortableTypeface();
85 }
86 SkASSERT(fInfo.distortable);
87 fVariationSliders = ToolUtils::VariationSliders(fInfo.distortable.get());
88 }
89
90 inline static constexpr int rows = 2;
91 inline static constexpr int cols = 5;
92 sk_sp<SkTypeface> typeface[rows][cols];
93
94 void updateTypefaces() {
96
97 std::unique_ptr<SkStreamAsset> distortableStream( fInfo.distortable
98 ? fInfo.distortable->openStream(nullptr)
99 : nullptr);
100 for (int row = 0; row < rows; ++row) {
101 for (int col = 0; col < cols; ++col) {
104 Coordinate coordinates[2];
105
106 if (fOverride) {
107 SkSpan<const Coordinate> user_coordinates = fVariationSliders.getCoordinates();
108 position = {user_coordinates.data(), static_cast<int>(user_coordinates.size())};
109
110 } else {
111 const int coordinateCount = 2;
112 SkScalar styleValue = SkScalarInterp(fInfo.axisMin, fInfo.axisMax,
113 SkScalar(row*cols + col) / (rows*cols));
114 coordinates[0] = {fInfo.axisTag, styleValue};
115 coordinates[1] = {fInfo.axisTag, styleValue};
116 position = {coordinates, static_cast<int>(coordinateCount)};
117 }
118
119 typeface[row][col] = [&]() -> sk_sp<SkTypeface> {
120 if (row == 0 && fInfo.distortable) {
121 return fInfo.distortable->makeClone(
123 }
124 if (distortableStream) {
125 return fontMgr->makeFromStream(distortableStream->duplicate(),
127 }
128 return nullptr;
129 }();
130 }
131 }
132 fDirty = false;
133 }
134
135 DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) override {
136 if (fDirty) {
137 this->updateTypefaces();
138 }
139
141 paint.setAntiAlias(true);
142 SkFont font;
144
145 const char* text = "abc";
146 const size_t textLen = strlen(text);
147
148 for (int row = 0; row < rows; ++row) {
149 for (int col = 0; col < cols; ++col) {
152
153 font.setTypeface(typeface[row][col] ? typeface[row][col] :
155
156 SkAutoCanvasRestore acr(canvas, true);
157 canvas->translate(SkIntToScalar(30 + col * 100), SkIntToScalar(20));
158 canvas->rotate(SkIntToScalar(col * 5), x, y * 10);
159
160 {
161 SkPaint p;
162 p.setAntiAlias(true);
163 SkRect r;
164 r.setLTRB(x - 3, 15, x - 1, 280);
165 canvas->drawRect(r, p);
166 }
167
168 for (int ps = 6; ps <= 22; ps++) {
169 font.setSize(SkIntToScalar(ps));
170 canvas->drawSimpleText(text, textLen, SkTextEncoding::kUTF8, x, y, font, paint);
171 y += font.getMetrics(nullptr);
172 }
173 }
174 canvas->translate(0, SkIntToScalar(360));
175 font.setSubpixel(true);
176 font.setLinearMetrics(true);
177 font.setBaselineSnap(false);
178 }
179 return DrawResult::kOk;
180 }
181};
182
183//////////////////////////////////////////////////////////////////////////////
184
185DEF_GM( return new FontScalerDistortableGM; )
186
187} // namespace skiagm
#define SkASSERT(cond)
Definition: SkAssert.h:116
@ kUTF8
uses bytes to represent UTF-8 or ASCII
#define SkIntToScalar(x)
Definition: SkScalar.h:57
static SkScalar SkScalarInterp(SkScalar A, SkScalar B, SkScalar t)
Definition: SkScalar.h:131
uint32_t SkFourByteTag
Definition: SkTypes.h:166
static constexpr SkFourByteTag SkSetFourByteTag(char a, char b, char c, char d)
Definition: SkTypes.h:167
void drawRect(const SkRect &rect, const SkPaint &paint)
Definition: SkCanvas.cpp:1673
void drawSimpleText(const void *text, size_t byteLength, SkTextEncoding encoding, SkScalar x, SkScalar y, const SkFont &font, const SkPaint &paint)
Definition: SkCanvas.cpp:2413
void translate(SkScalar dx, SkScalar dy)
Definition: SkCanvas.cpp:1278
void rotate(SkScalar degrees)
Definition: SkCanvas.cpp:1300
sk_sp< SkTypeface > makeFromStream(std::unique_ptr< SkStreamAsset >, int ttcIndex=0) const
Definition: SkFontMgr.cpp:127
Definition: SkFont.h:35
@ kSubpixelAntiAlias
glyph positioned in pixel using transparency
void setBool(const char name[], bool value)
Definition: SkMetaData.cpp:46
bool findBool(const char name[], bool *value=nullptr) const
Definition: SkMetaData.cpp:149
constexpr T * data() const
Definition: SkSpan_impl.h:94
constexpr size_t size() const
Definition: SkSpan_impl.h:95
SkSpan< const SkFontArguments::VariationPosition::Coordinate > getCoordinates()
Definition: ToolUtils.cpp:600
void readControls(const SkMetaData &controls, bool *changed=nullptr)
Definition: ToolUtils.cpp:585
bool writeControls(SkMetaData *controls)
Definition: ToolUtils.cpp:573
Definition: gm.h:110
skiagm::DrawResult DrawResult
Definition: gm.h:112
void setBGColor(SkColor)
Definition: gm.cpp:159
const Paint & paint
Definition: color_source.cc:38
sk_sp< SkFontMgr > fontMgr
Definition: examples.cpp:32
float SkScalar
Definition: extension.cpp:12
std::u16string text
double y
double x
sk_sp< SkTypeface > DefaultPortableTypeface()
sk_sp< SkTypeface > CreateTypefaceFromResource(const char *resource, int ttcIndex)
sk_sp< SkFontMgr > TestFontMgr()
font
Font Metadata and Metrics.
DEF_GM(return F(C(clipbox), 0.0f, 0.0f, {})) DEF_GM(return F(C(clipbox)
Definition: 3d.cpp:16
SkFontArguments & setVariationDesignPosition(VariationPosition position)
Definition: SkSize.h:16
static constexpr SkISize Make(int32_t w, int32_t h)
Definition: SkSize.h:20
void setLTRB(float left, float top, float right, float bottom)
Definition: SkRect.h:865