|
| 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 | + |
| 18 | +static sk_sp<SkTypeface> make_tf() { |
| 19 | + SkCustomTypefaceBuilder builder(128); |
| 20 | + 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 | + |
| 42 | +class UserFontGM : public skiagm::GM { |
| 43 | + sk_sp<SkTypeface> fTF; |
| 44 | + sk_sp<SkTextBlob> fBlob; |
| 45 | + |
| 46 | + SkPath fPath; |
| 47 | +public: |
| 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 | + SkPaint paint; |
| 78 | + paint.setColor(SK_ColorRED); |
| 79 | + canvas->drawTextBlob(fBlob, 20, 150, paint); |
| 80 | + } |
| 81 | +}; |
| 82 | +DEF_GM(return new UserFontGM;) |
0 commit comments