blob: b228ab1c44587e03028799a345af4847ab405f12 [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
24 // Steal the first 128 chars from the default font
25 for (SkGlyphID index = 0; index <= 127; ++index) {
26 SkGlyphID glyph = font.unicharToGlyph(index);
27
28 SkScalar width;
29 font.getWidths(&glyph, 1, &width);
30 SkPath path;
31 font.getPath(glyph, &path);
32
33 // we use the charcode to be our glyph index, since we have no cmap table
34 builder.setGlyph(index, width, path);
35 }
36
37 return builder.detach();
38}
39
40#include "include/core/SkTextBlob.h"
41
42class UserFontGM : public skiagm::GM {
43 sk_sp<SkTypeface> fTF;
Mike Reedb0210d22020-05-07 16:58:40 -040044
Mike Reedb0210d22020-05-07 16:58:40 -040045public:
46 UserFontGM() {}
47
48 void onOnceBeforeDraw() override {
49 fTF = make_tf();
Mike Reed2ddf7f32020-05-18 14:06:38 -040050 }
Mike Reedb0210d22020-05-07 16:58:40 -040051
Mike Reed2ddf7f32020-05-18 14:06:38 -040052 static sk_sp<SkTextBlob> make_blob(sk_sp<SkTypeface> tf, float size) {
53 SkFont font(tf);
54 font.setSize(size);
Mike Reedb0210d22020-05-07 16:58:40 -040055 font.setEdging(SkFont::Edging::kAntiAlias);
Mike Reed2ddf7f32020-05-18 14:06:38 -040056 return SkTextBlob::MakeFromString("Typeface", font);
Mike Reedb0210d22020-05-07 16:58:40 -040057 }
58
59 bool runAsBench() const override { return true; }
60
61 SkString onShortName() override { return SkString("user_typeface"); }
62
Mike Reed2ddf7f32020-05-18 14:06:38 -040063 SkISize onISize() override { return {810, 512}; }
Mike Reedb0210d22020-05-07 16:58:40 -040064
65 void onDraw(SkCanvas* canvas) override {
Mike Reed2ddf7f32020-05-18 14:06:38 -040066 auto waterfall = [&](sk_sp<SkTypeface> tf) {
67 SkPaint paint;
68 paint.setAntiAlias(true);
Mike Reedb0210d22020-05-07 16:58:40 -040069
Mike Reed2ddf7f32020-05-18 14:06:38 -040070 float x = 20,
71 y = 16;
72 for (float size = 9; size <= 100; size *= 1.25f) {
73 auto blob = make_blob(tf, size);
Mike Reedb0210d22020-05-07 16:58:40 -040074
Mike Reed2ddf7f32020-05-18 14:06:38 -040075 paint.setColor(0xFFCCCCCC);
76 paint.setStyle(SkPaint::kStroke_Style);
77 canvas->drawRect(blob->bounds().makeOffset(x, y), paint);
78
79 paint.setStyle(SkPaint::kFill_Style);
80 paint.setColor(SK_ColorBLACK);
81 canvas->drawTextBlob(blob, x, y, paint);
82
83 y += size * 1.5f;
84 }
85 };
86
87 waterfall(nullptr);
88 canvas->translate(400, 0);
89 waterfall(fTF);
Mike Reedb0210d22020-05-07 16:58:40 -040090 }
91};
92DEF_GM(return new UserFontGM;)