• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/SkPaint.h"
12 #include "include/core/SkPath.h"
13 #include "include/core/SkSize.h"
14 #include "include/core/SkString.h"
15 #include "include/utils/SkCustomTypeface.h"
16 #include "tools/Resources.h"
17 
make_tf()18 static sk_sp<SkTypeface> make_tf() {
19     SkCustomTypefaceBuilder builder;
20     SkFont font;
21     const float upem = font.getTypefaceOrDefault()->getUnitsPerEm();
22 
23     // request a big size, to improve precision at the fontscaler level
24     font.setSize(upem);
25     font.setHinting(SkFontHinting::kNone);
26 
27     // so we can scale our paths back down to 1-point
28     const SkMatrix scale = SkMatrix::Scale(1.0f/upem, 1.0f/upem);
29 
30     {
31         SkFontMetrics metrics;
32         font.getMetrics(&metrics);
33         builder.setMetrics(metrics, 1.0f/upem);
34     }
35     builder.setFontStyle(font.getTypefaceOrDefault()->fontStyle());
36 
37     // Steal the first 128 chars from the default font
38     for (SkGlyphID index = 0; index <= 127; ++index) {
39         SkGlyphID glyph = font.unicharToGlyph(index);
40 
41         SkScalar width;
42         font.getWidths(&glyph, 1, &width);
43         SkPath path;
44         font.getPath(glyph, &path);
45 
46         // we use the charcode to be our glyph index, since we have no cmap table
47         builder.setGlyph(index, width/upem, path.makeTransform(scale));
48     }
49 
50     return builder.detach();
51 }
52 
53 #include "include/core/SkTextBlob.h"
54 
round_trip(sk_sp<SkTypeface> tf)55 static sk_sp<SkTypeface> round_trip(sk_sp<SkTypeface> tf) {
56     auto data = tf->serialize();
57     SkMemoryStream stream(data->data(), data->size());
58     return SkTypeface::MakeDeserialize(&stream);
59 }
60 
61 class UserFontGM : public skiagm::GM {
62     sk_sp<SkTypeface> fTF;
63 
64 public:
UserFontGM()65     UserFontGM() {}
66 
onOnceBeforeDraw()67     void onOnceBeforeDraw() override {
68         fTF = make_tf();
69         // test serialization
70         fTF = round_trip(fTF);
71     }
72 
make_blob(sk_sp<SkTypeface> tf,float size,float * spacing)73     static sk_sp<SkTextBlob> make_blob(sk_sp<SkTypeface> tf, float size, float* spacing) {
74         SkFont font(tf);
75         font.setSize(size);
76         font.setEdging(SkFont::Edging::kAntiAlias);
77         *spacing = font.getMetrics(nullptr);
78         return SkTextBlob::MakeFromString("Typeface", font);
79     }
80 
runAsBench() const81     bool runAsBench() const override { return true; }
82 
onShortName()83     SkString onShortName() override { return SkString("user_typeface"); }
84 
onISize()85     SkISize onISize() override { return {810, 452}; }
86 
onDraw(SkCanvas * canvas)87     void onDraw(SkCanvas* canvas) override {
88         auto waterfall = [&](sk_sp<SkTypeface> tf) {
89             SkPaint paint;
90             paint.setAntiAlias(true);
91 
92             float spacing;
93             float x = 20,
94                   y = 16;
95             for (float size = 9; size <= 100; size *= 1.25f) {
96                 auto blob = make_blob(tf, size, &spacing);
97 
98                 // shared baseline
99                 if (tf == nullptr) {
100                     paint.setColor(0xFFDDDDDD);
101                     canvas->drawRect({0, y, 810, y+1}, paint);
102                 }
103 
104                 paint.setColor(0xFFCCCCCC);
105                 paint.setStyle(SkPaint::kStroke_Style);
106                 canvas->drawRect(blob->bounds().makeOffset(x, y), paint);
107 
108                 paint.setStyle(SkPaint::kFill_Style);
109                 paint.setColor(SK_ColorBLACK);
110                 canvas->drawTextBlob(blob, x, y, paint);
111 
112                 y += SkScalarRoundToInt(spacing * 1.25f + 2);
113             }
114         };
115 
116         waterfall(nullptr);
117         canvas->translate(400, 0);
118         waterfall(fTF);
119     }
120 };
121 DEF_GM(return new UserFontGM;)
122