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