xref: /aosp_15_r20/external/skia/gm/userfont.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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 
make_drawable(const SkPath & path)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 
make_tf()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 (SkGlyphID index = 0; index <= 127; ++index) {
60         SkGlyphID glyph = font.unicharToGlyph(index);
61 
62         SkScalar width;
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 
79 #include "include/core/SkTextBlob.h"
80 
round_trip(sk_sp<SkTypeface> tf)81 static sk_sp<SkTypeface> round_trip(sk_sp<SkTypeface> tf) {
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 
89 class UserFontGM : public skiagm::GM {
90     sk_sp<SkTypeface> fTF;
91 
92 public:
UserFontGM()93     UserFontGM() {}
94 
onOnceBeforeDraw()95     void onOnceBeforeDraw() override {
96         fTF = make_tf();
97         // test serialization
98         fTF = round_trip(fTF);
99     }
100 
make_blob(sk_sp<SkTypeface> tf,float size,float * spacing)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 
runAsBench() const109     bool runAsBench() const override { return true; }
110 
getName() const111     SkString getName() const override { return SkString("user_typeface"); }
112 
getISize()113     SkISize getISize() override { return {810, 452}; }
114 
onDraw(SkCanvas * canvas)115     void onDraw(SkCanvas* canvas) override {
116         auto waterfall = [&](sk_sp<SkTypeface> tf, bool defaultFace) {
117             SkPaint paint;
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);
133                 paint.setStyle(SkPaint::kStroke_Style);
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 };
149 DEF_GM(return new UserFontGM;)
150