blob: ed20109644bc7a40cde8fada37429e9f45b74781 [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;
44 sk_sp<SkTextBlob> fBlob;
45
46 SkPath fPath;
47public:
48 UserFontGM() {}
49
50 void onOnceBeforeDraw() override {
51 fTF = make_tf();
52
53 SkFont font(fTF);
54 font.setSize(100);
55 font.setEdging(SkFont::Edging::kAntiAlias);
56
57 std::vector<SkGlyphID> array;
58 auto expand8to16 = [&](const char str[]) {
59 for (int i = 0; str[i]; ++i) {
60 array.push_back(str[i]);
61 }
62 };
63
64 expand8to16("User Typeface");
65 fBlob = SkTextBlob::MakeFromText(array.data(), array.size() * sizeof(SkGlyphID),
66 font, SkTextEncoding::kGlyphID);
67
68 }
69
70 bool runAsBench() const override { return true; }
71
72 SkString onShortName() override { return SkString("user_typeface"); }
73
74 SkISize onISize() override { return {512, 512}; }
75
76 void onDraw(SkCanvas* canvas) override {
77 SkScalar x = 20,
78 y = 250;
79
80 SkPaint paint;
81 paint.setStyle(SkPaint::kStroke_Style);
82 canvas->drawRect(fBlob->bounds().makeOffset(x, y), paint);
83
84 paint.setStyle(SkPaint::kFill_Style);
85 paint.setColor(SK_ColorRED);
86 canvas->drawTextBlob(fBlob, x, y, paint);
87 }
88};
89DEF_GM(return new UserFontGM;)