blob: 55c9cf96c682cff4bd6adf987fbfbcefedbb1e31 [file] [log] [blame]
Mike Reedb0210d22020-05-07 16:58:40 -04001/*
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
18static sk_sp<SkTypeface> make_tf() {
Florin Malita340cd9c2020-05-14 16:21:53 -040019 SkCustomTypefaceBuilder builder;
Mike Reedb0210d22020-05-07 16:58:40 -040020 SkFont font;
21 font.setSize(1.0f);
22 font.setHinting(SkFontHinting::kNone);
23
Mike Reed98bc22c2020-05-18 16:17:05 -040024 {
25 SkFontMetrics metrics;
26 font.getMetrics(&metrics);
27 builder.setMetrics(metrics);
28 }
29
Mike Reedb0210d22020-05-07 16:58:40 -040030 // Steal the first 128 chars from the default font
31 for (SkGlyphID index = 0; index <= 127; ++index) {
32 SkGlyphID glyph = font.unicharToGlyph(index);
33
34 SkScalar width;
35 font.getWidths(&glyph, 1, &width);
36 SkPath path;
37 font.getPath(glyph, &path);
38
39 // we use the charcode to be our glyph index, since we have no cmap table
40 builder.setGlyph(index, width, path);
41 }
42
43 return builder.detach();
44}
45
46#include "include/core/SkTextBlob.h"
47
Mike Reed98bc22c2020-05-18 16:17:05 -040048static sk_sp<SkTypeface> round_trip(sk_sp<SkTypeface> tf) {
49 auto data = tf->serialize();
50 SkMemoryStream stream(data->data(), data->size());
51 return SkTypeface::MakeDeserialize(&stream);
52}
53
Mike Reedb0210d22020-05-07 16:58:40 -040054class UserFontGM : public skiagm::GM {
55 sk_sp<SkTypeface> fTF;
Mike Reedb0210d22020-05-07 16:58:40 -040056
Mike Reedb0210d22020-05-07 16:58:40 -040057public:
58 UserFontGM() {}
59
60 void onOnceBeforeDraw() override {
61 fTF = make_tf();
Mike Reed98bc22c2020-05-18 16:17:05 -040062 // test serialization
63 fTF = round_trip(fTF);
Mike Reed2ddf7f32020-05-18 14:06:38 -040064 }
Mike Reedb0210d22020-05-07 16:58:40 -040065
Mike Reed98bc22c2020-05-18 16:17:05 -040066 static sk_sp<SkTextBlob> make_blob(sk_sp<SkTypeface> tf, float size, float* spacing) {
Mike Reed2ddf7f32020-05-18 14:06:38 -040067 SkFont font(tf);
68 font.setSize(size);
Mike Reedb0210d22020-05-07 16:58:40 -040069 font.setEdging(SkFont::Edging::kAntiAlias);
Mike Reed98bc22c2020-05-18 16:17:05 -040070 *spacing = font.getMetrics(nullptr);
Mike Reed2ddf7f32020-05-18 14:06:38 -040071 return SkTextBlob::MakeFromString("Typeface", font);
Mike Reedb0210d22020-05-07 16:58:40 -040072 }
73
74 bool runAsBench() const override { return true; }
75
76 SkString onShortName() override { return SkString("user_typeface"); }
77
Mike Reed98bc22c2020-05-18 16:17:05 -040078 SkISize onISize() override { return {810, 452}; }
Mike Reedb0210d22020-05-07 16:58:40 -040079
80 void onDraw(SkCanvas* canvas) override {
Mike Reed2ddf7f32020-05-18 14:06:38 -040081 auto waterfall = [&](sk_sp<SkTypeface> tf) {
82 SkPaint paint;
83 paint.setAntiAlias(true);
Mike Reedb0210d22020-05-07 16:58:40 -040084
Mike Reed98bc22c2020-05-18 16:17:05 -040085 float spacing;
Mike Reed2ddf7f32020-05-18 14:06:38 -040086 float x = 20,
87 y = 16;
88 for (float size = 9; size <= 100; size *= 1.25f) {
Mike Reed98bc22c2020-05-18 16:17:05 -040089 auto blob = make_blob(tf, size, &spacing);
90
91 // shared baseline
92 if (tf == nullptr) {
93 paint.setColor(0xFFDDDDDD);
94 canvas->drawRect({0, y, 810, y+1}, paint);
95 }
Mike Reedb0210d22020-05-07 16:58:40 -040096
Mike Reed2ddf7f32020-05-18 14:06:38 -040097 paint.setColor(0xFFCCCCCC);
98 paint.setStyle(SkPaint::kStroke_Style);
99 canvas->drawRect(blob->bounds().makeOffset(x, y), paint);
100
101 paint.setStyle(SkPaint::kFill_Style);
102 paint.setColor(SK_ColorBLACK);
103 canvas->drawTextBlob(blob, x, y, paint);
104
Mike Reed98bc22c2020-05-18 16:17:05 -0400105 y += SkScalarRoundToInt(spacing * 1.25f + 2);
Mike Reed2ddf7f32020-05-18 14:06:38 -0400106 }
107 };
108
109 waterfall(nullptr);
110 canvas->translate(400, 0);
111 waterfall(fTF);
Mike Reedb0210d22020-05-07 16:58:40 -0400112 }
113};
114DEF_GM(return new UserFontGM;)