Mike Reed | b0210d2 | 2020-05-07 16:58:40 -0400 | [diff] [blame] | 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() { |
Florin Malita | 340cd9c | 2020-05-14 16:21:53 -0400 | [diff] [blame] | 19 | SkCustomTypefaceBuilder builder; |
Mike Reed | b0210d2 | 2020-05-07 16:58:40 -0400 | [diff] [blame] | 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; |
Mike Reed | b0210d2 | 2020-05-07 16:58:40 -0400 | [diff] [blame] | 44 | |
Mike Reed | b0210d2 | 2020-05-07 16:58:40 -0400 | [diff] [blame] | 45 | public: |
| 46 | UserFontGM() {} |
| 47 | |
| 48 | void onOnceBeforeDraw() override { |
| 49 | fTF = make_tf(); |
Mike Reed | 2ddf7f3 | 2020-05-18 14:06:38 -0400 | [diff] [blame^] | 50 | } |
Mike Reed | b0210d2 | 2020-05-07 16:58:40 -0400 | [diff] [blame] | 51 | |
Mike Reed | 2ddf7f3 | 2020-05-18 14:06:38 -0400 | [diff] [blame^] | 52 | static sk_sp<SkTextBlob> make_blob(sk_sp<SkTypeface> tf, float size) { |
| 53 | SkFont font(tf); |
| 54 | font.setSize(size); |
Mike Reed | b0210d2 | 2020-05-07 16:58:40 -0400 | [diff] [blame] | 55 | font.setEdging(SkFont::Edging::kAntiAlias); |
Mike Reed | 2ddf7f3 | 2020-05-18 14:06:38 -0400 | [diff] [blame^] | 56 | return SkTextBlob::MakeFromString("Typeface", font); |
Mike Reed | b0210d2 | 2020-05-07 16:58:40 -0400 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | bool runAsBench() const override { return true; } |
| 60 | |
| 61 | SkString onShortName() override { return SkString("user_typeface"); } |
| 62 | |
Mike Reed | 2ddf7f3 | 2020-05-18 14:06:38 -0400 | [diff] [blame^] | 63 | SkISize onISize() override { return {810, 512}; } |
Mike Reed | b0210d2 | 2020-05-07 16:58:40 -0400 | [diff] [blame] | 64 | |
| 65 | void onDraw(SkCanvas* canvas) override { |
Mike Reed | 2ddf7f3 | 2020-05-18 14:06:38 -0400 | [diff] [blame^] | 66 | auto waterfall = [&](sk_sp<SkTypeface> tf) { |
| 67 | SkPaint paint; |
| 68 | paint.setAntiAlias(true); |
Mike Reed | b0210d2 | 2020-05-07 16:58:40 -0400 | [diff] [blame] | 69 | |
Mike Reed | 2ddf7f3 | 2020-05-18 14:06:38 -0400 | [diff] [blame^] | 70 | float x = 20, |
| 71 | y = 16; |
| 72 | for (float size = 9; size <= 100; size *= 1.25f) { |
| 73 | auto blob = make_blob(tf, size); |
Mike Reed | b0210d2 | 2020-05-07 16:58:40 -0400 | [diff] [blame] | 74 | |
Mike Reed | 2ddf7f3 | 2020-05-18 14:06:38 -0400 | [diff] [blame^] | 75 | 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 Reed | b0210d2 | 2020-05-07 16:58:40 -0400 | [diff] [blame] | 90 | } |
| 91 | }; |
| 92 | DEF_GM(return new UserFontGM;) |