Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
userfont.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2020 Google LLC
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"
13#include "include/core/SkPath.h"
15#include "include/core/SkSize.h"
19#include "src/core/SkFontPriv.h"
20#include "tools/Resources.h"
22
24 const auto bounds = path.computeTightBounds();
25
26 SkPictureRecorder recorder;
27 auto* canvas = recorder.beginRecording(bounds);
28
30 paint.setColor(0xff008000);
31 paint.setAntiAlias(true);
32
33 canvas->drawPath(path, paint);
34
35 return recorder.finishRecordingAsDrawable();
36}
37
41 SkASSERT(font.getTypeface());
42 const float upem = font.getTypeface()->getUnitsPerEm();
43
44 // request a big size, to improve precision at the fontscaler level
45 font.setSize(upem);
46 font.setHinting(SkFontHinting::kNone);
47
48 // so we can scale our paths back down to 1-point
49 const SkMatrix scale = SkMatrix::Scale(1.0f/upem, 1.0f/upem);
50
51 {
52 SkFontMetrics metrics;
53 font.getMetrics(&metrics);
54 builder.setMetrics(metrics, 1.0f/upem);
55 }
56 builder.setFontStyle(font.getTypeface()->fontStyle());
57
58 // Steal the first 128 chars from the default font
59 for (SkGlyphID index = 0; index <= 127; ++index) {
60 SkGlyphID glyph = font.unicharToGlyph(index);
61
63 font.getWidths(&glyph, 1, &width);
64 SkPath path;
65 font.getPath(glyph, &path);
66 path.transform(scale);
67
68 // we use the charcode to be our glyph index, since we have no cmap table
69 if (index % 2) {
70 builder.setGlyph(index, width/upem, make_drawable(path), path.computeTightBounds());
71 } else {
72 builder.setGlyph(index, width/upem, path);
73 }
74 }
75
76 return builder.detach();
77}
78
80
82 auto data = tf->serialize();
83 SkMemoryStream stream(data->data(), data->size());
84 sk_sp<SkTypeface> face = SkTypeface::MakeDeserialize(&stream, nullptr);
85 SkASSERT(face);
86 return face;
87}
88
89class UserFontGM : public skiagm::GM {
91
92public:
94
95 void onOnceBeforeDraw() override {
96 fTF = make_tf();
97 // test serialization
98 fTF = round_trip(fTF);
99 }
100
101 static sk_sp<SkTextBlob> make_blob(sk_sp<SkTypeface> tf, float size, float* spacing) {
102 SkFont font(tf);
103 font.setSize(size);
104 font.setEdging(SkFont::Edging::kAntiAlias);
105 *spacing = font.getMetrics(nullptr);
106 return SkTextBlob::MakeFromString("Typeface", font);
107 }
108
109 bool runAsBench() const override { return true; }
110
111 SkString getName() const override { return SkString("user_typeface"); }
112
113 SkISize getISize() override { return {810, 452}; }
114
115 void onDraw(SkCanvas* canvas) override {
116 auto waterfall = [&](sk_sp<SkTypeface> tf, bool defaultFace) {
118 paint.setAntiAlias(true);
119
120 float spacing;
121 float x = 20,
122 y = 16;
123 for (float size = 9; size <= 100; size *= 1.25f) {
124 auto blob = make_blob(tf, size, &spacing);
125
126 // shared baseline
127 if (defaultFace) {
128 paint.setColor(0xFFDDDDDD);
129 canvas->drawRect({0, y, 810, y+1}, paint);
130 }
131
132 paint.setColor(0xFFCCCCCC);
134 canvas->drawRect(blob->bounds().makeOffset(x, y), paint);
135
136 paint.setStyle(SkPaint::kFill_Style);
137 paint.setColor(SK_ColorBLACK);
138 canvas->drawTextBlob(blob, x, y, paint);
139
140 y += SkScalarRoundToInt(spacing * 1.25f + 2);
141 }
142 };
143
144 waterfall(ToolUtils::DefaultTypeface(), true);
145 canvas->translate(400, 0);
146 waterfall(fTF, false);
147 }
148};
149DEF_GM(return new UserFontGM;)
#define SkASSERT(cond)
Definition SkAssert.h:116
constexpr SkColor SK_ColorBLACK
Definition SkColor.h:103
@ kNone
glyph outlines unchanged
#define SkScalarRoundToInt(x)
Definition SkScalar.h:37
uint16_t SkGlyphID
Definition SkTypes.h:179
static sk_sp< SkTextBlob > make_blob()
void drawRect(const SkRect &rect, const SkPaint &paint)
void translate(SkScalar dx, SkScalar dy)
void drawTextBlob(const SkTextBlob *blob, SkScalar x, SkScalar y, const SkPaint &paint)
@ kAntiAlias
may have transparent pixels on glyph edges
static SkMatrix Scale(SkScalar sx, SkScalar sy)
Definition SkMatrix.h:75
@ kStroke_Style
set to stroke geometry
Definition SkPaint.h:194
@ kFill_Style
set to fill geometry
Definition SkPaint.h:193
SkCanvas * beginRecording(const SkRect &bounds, sk_sp< SkBBoxHierarchy > bbh)
sk_sp< SkDrawable > finishRecordingAsDrawable()
static sk_sp< SkTextBlob > MakeFromString(const char *string, const SkFont &font, SkTextEncoding encoding=SkTextEncoding::kUTF8)
Definition SkTextBlob.h:115
static sk_sp< SkTypeface > MakeDeserialize(SkStream *, sk_sp< SkFontMgr > lastResortMgr)
void onDraw(SkCanvas *canvas) override
Definition userfont.cpp:115
bool runAsBench() const override
Definition userfont.cpp:109
void onOnceBeforeDraw() override
Definition userfont.cpp:95
SkString getName() const override
Definition userfont.cpp:111
static sk_sp< SkTextBlob > make_blob(sk_sp< SkTypeface > tf, float size, float *spacing)
Definition userfont.cpp:101
SkISize getISize() override
Definition userfont.cpp:113
const Paint & paint
float SkScalar
Definition extension.cpp:12
#define DEF_GM(CODE)
Definition gm.h:40
double y
double x
SkFont DefaultFont()
sk_sp< SkTypeface > DefaultTypeface()
int32_t width
const Scalar scale
static sk_sp< SkDrawable > make_drawable(const SkPath &path)
Definition userfont.cpp:23
static sk_sp< SkTypeface > make_tf()
Definition userfont.cpp:38
static sk_sp< SkTypeface > round_trip(sk_sp< SkTypeface > tf)
Definition userfont.cpp:81