Debian Codegraph @

gm/userfont.cpp

raw
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"
9 #include "include/core/SkCanvas.h"
10 #include "include/core/SkFont.h"
11 #include "include/core/SkFontMgr.h"
12 #include "include/core/SkPaint.h"
13 #include "include/core/SkPath.h"
14 #include "include/core/SkPictureRecorder.h"
15 #include "include/core/SkSize.h"
16 #include "include/core/SkStream.h"
17 #include "include/core/SkString.h"
18 #include "include/utils/SkCustomTypeface.h"
19 #include "src/core/SkFontPriv.h"
20 #include "tools/Resources.h"
21 #include "tools/fonts/FontToolUtils.h"
22
23 static sk_sp<SkDrawable> make_drawable(const SkPath& path) {
24 const auto bounds = path.computeTightBounds();
25
26 SkPictureRecorder recorder;
27 auto* canvas = recorder.beginRecording(bounds);
28
29 SkPaint paint;
30 paint.setColor(0xff008000);
31 paint.setAntiAlias(true);
32
33 canvas->drawPath(path, paint);
34
35 return recorder.finishRecordingAsDrawable();
36 }
37
38 static sk_sp<SkTypeface> make_tf() {
39 SkCustomTypefaceBuilder builder;
40 SkFont font = ToolUtils::DefaultFont();
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 (SkUnichar index = 0; index <= 127; ++index) {
60 SkGlyphID glyph = font.unicharToGlyph(index);
61
62 SkScalar width = font.getWidth(glyph);
63 SkPath path = font.getPath(glyph).value_or(SkPath()).makeTransform(scale);
64
65 // we use the charcode to be our glyph index, since we have no cmap table
66 if (index % 2) {
67 builder.setGlyph(index, width/upem, make_drawable(path), path.computeTightBounds());
68 } else {
69 builder.setGlyph(index, width/upem, path);
70 }
71 }
72
73 return builder.detach();
74 }
75
76 #include "include/core/SkTextBlob.h"
77
78 static sk_sp<SkTypeface> round_trip(sk_sp<SkTypeface> tf) {
79 auto data = tf->serialize();
80 SkMemoryStream stream(data->data(), data->size());
81 sk_sp<SkTypeface> face = SkTypeface::MakeDeserialize(&stream, nullptr);
82 SkASSERT(face);
83 return face;
84 }
85
86 class UserFontGM : public skiagm::GM {
87 sk_sp<SkTypeface> fTF;
88
89 public:
90 UserFontGM() {}
91
92 void onOnceBeforeDraw() override {
93 fTF = make_tf();
94 // test serialization
95 fTF = round_trip(fTF);
96 }
97
98 static sk_sp<SkTextBlob> make_blob(sk_sp<SkTypeface> tf, float size, float* spacing) {
99 SkFont font(tf);
100 font.setSize(size);
101 font.setEdging(SkFont::Edging::kAntiAlias);
102 *spacing = font.getMetrics(nullptr);
103 return SkTextBlob::MakeFromString("Typeface", font);
104 }
105
106 bool runAsBench() const override { return true; }
107
108 SkString getName() const override { return SkString("user_typeface"); }
109
110 SkISize getISize() override { return {810, 452}; }
111
112 void onDraw(SkCanvas* canvas) override {
113 auto waterfall = [&](sk_sp<SkTypeface> tf, bool defaultFace) {
114 SkPaint paint;
115 paint.setAntiAlias(true);
116
117 float spacing;
118 float x = 20,
119 y = 16;
120 for (float size = 9; size <= 100; size *= 1.25f) {
121 auto blob = make_blob(tf, size, &spacing);
122
123 // shared baseline
124 if (defaultFace) {
125 paint.setColor(0xFFDDDDDD);
126 canvas->drawRect({0, y, 810, y+1}, paint);
127 }
128
129 paint.setColor(0xFFCCCCCC);
130 paint.setStyle(SkPaint::kStroke_Style);
131 canvas->drawRect(blob->bounds().makeOffset(x, y), paint);
132
133 paint.setStyle(SkPaint::kFill_Style);
134 paint.setColor(SK_ColorBLACK);
135 canvas->drawTextBlob(blob, x, y, paint);
136
137 y += SkScalarRoundToInt(spacing * 1.25f + 2);
138 }
139 };
140
141 waterfall(ToolUtils::DefaultTypeface(), true);
142 canvas->translate(400, 0);
143 waterfall(fTF, false);
144 }
145 };
146 DEF_GM(return new UserFontGM;)