Herb Derby | 25c0e1a | 2021-11-10 08:34:51 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2021 Google Inc. |
| 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/SkColor.h" |
| 11 | #include "include/core/SkFont.h" |
| 12 | #include "include/core/SkFontStyle.h" |
| 13 | #include "include/core/SkFontTypes.h" |
| 14 | #include "include/core/SkPaint.h" |
| 15 | #include "include/core/SkPoint.h" |
| 16 | #include "include/core/SkRect.h" |
| 17 | #include "include/core/SkRefCnt.h" |
| 18 | #include "include/core/SkScalar.h" |
| 19 | #include "include/core/SkSize.h" |
| 20 | #include "include/core/SkString.h" |
| 21 | #include "include/core/SkTextBlob.h" |
| 22 | #include "include/core/SkTypeface.h" |
| 23 | #include "include/core/SkTypes.h" |
Kevin Lubick | dc6cc02 | 2023-01-13 11:24:27 -0500 | [diff] [blame] | 24 | #include "include/private/base/SkTDArray.h" |
Jim Van Verth | e55e016 | 2022-05-20 15:01:58 -0400 | [diff] [blame] | 25 | #include "include/private/chromium/Slug.h" |
Herb Derby | 25c0e1a | 2021-11-10 08:34:51 -0500 | [diff] [blame] | 26 | #include "tools/ToolUtils.h" |
Kevin Lubick | e836c3a | 2023-10-20 06:55:35 -0400 | [diff] [blame] | 27 | #include "tools/fonts/FontToolUtils.h" |
Herb Derby | 25c0e1a | 2021-11-10 08:34:51 -0500 | [diff] [blame] | 28 | |
Robert Phillips | 9360fae | 2023-06-20 13:34:37 -0400 | [diff] [blame] | 29 | #if defined(SK_GRAPHITE) |
| 30 | #include "include/gpu/graphite/ContextOptions.h" |
| 31 | #endif |
| 32 | |
| 33 | #if defined(SK_GANESH) || defined(SK_GRAPHITE) |
Herb Derby | a889f61 | 2022-04-21 15:40:13 -0600 | [diff] [blame] | 34 | #include "include/gpu/GrContextOptions.h" |
| 35 | |
Herb Derby | 25c0e1a | 2021-11-10 08:34:51 -0500 | [diff] [blame] | 36 | class SlugGM : public skiagm::GM { |
| 37 | public: |
Herb Derby | a889f61 | 2022-04-21 15:40:13 -0600 | [diff] [blame] | 38 | SlugGM(const char* txt) : fText(txt) {} |
Herb Derby | 25c0e1a | 2021-11-10 08:34:51 -0500 | [diff] [blame] | 39 | |
| 40 | protected: |
Herb Derby | a889f61 | 2022-04-21 15:40:13 -0600 | [diff] [blame] | 41 | void modifyGrContextOptions(GrContextOptions* ctxOptions) override { |
| 42 | ctxOptions->fSupportBilerpFromGlyphAtlas = true; |
| 43 | } |
| 44 | |
Robert Phillips | 9360fae | 2023-06-20 13:34:37 -0400 | [diff] [blame] | 45 | #if defined(SK_GRAPHITE) |
| 46 | void modifyGraphiteContextOptions(skgpu::graphite::ContextOptions* options) const override { |
| 47 | options->fSupportBilerpFromGlyphAtlas = true; |
| 48 | } |
| 49 | #endif |
| 50 | |
Herb Derby | 25c0e1a | 2021-11-10 08:34:51 -0500 | [diff] [blame] | 51 | void onOnceBeforeDraw() override { |
Kevin Lubick | e836c3a | 2023-10-20 06:55:35 -0400 | [diff] [blame] | 52 | fTypeface = ToolUtils::CreatePortableTypeface("serif", SkFontStyle()); |
Herb Derby | 25c0e1a | 2021-11-10 08:34:51 -0500 | [diff] [blame] | 53 | SkFont font(fTypeface); |
| 54 | size_t txtLen = strlen(fText); |
| 55 | int glyphCount = font.countText(fText, txtLen, SkTextEncoding::kUTF8); |
| 56 | |
| 57 | fGlyphs.append(glyphCount); |
| 58 | font.textToGlyphs(fText, txtLen, SkTextEncoding::kUTF8, fGlyphs.begin(), glyphCount); |
| 59 | } |
| 60 | |
Leandro Lovisolo | 24fa211 | 2023-08-15 19:05:17 +0000 | [diff] [blame] | 61 | SkString getName() const override { return SkString("slug"); } |
Herb Derby | 25c0e1a | 2021-11-10 08:34:51 -0500 | [diff] [blame] | 62 | |
Leandro Lovisolo | 8f02388 | 2023-08-15 21:13:52 +0000 | [diff] [blame] | 63 | SkISize getISize() override { return SkISize::Make(1000, 480); } |
Herb Derby | 25c0e1a | 2021-11-10 08:34:51 -0500 | [diff] [blame] | 64 | |
| 65 | void onDraw(SkCanvas* canvas) override { |
| 66 | sk_sp<SkTextBlob> blob(this->makeBlob()); |
| 67 | SkPaint p; |
| 68 | p.setAntiAlias(true); |
Herb Derby | 672062d | 2021-12-02 15:49:41 -0500 | [diff] [blame] | 69 | canvas->clipIRect(SkIRect::MakeSize(this->getISize()).makeInset(40, 50)); |
Herb Derby | 25c0e1a | 2021-11-10 08:34:51 -0500 | [diff] [blame] | 70 | canvas->scale(1.3f, 1.3f); |
Jim Van Verth | e55e016 | 2022-05-20 15:01:58 -0400 | [diff] [blame] | 71 | sk_sp<sktext::gpu::Slug> slug = sktext::gpu::Slug::ConvertBlob(canvas, *blob, {10, 10}, p); |
Herb Derby | 25c0e1a | 2021-11-10 08:34:51 -0500 | [diff] [blame] | 72 | if (slug == nullptr) { |
| 73 | return; |
| 74 | } |
| 75 | canvas->translate(0.5, 0.5); |
| 76 | canvas->translate(30, 30); |
| 77 | canvas->drawTextBlob(blob, 10, 10, p); |
| 78 | canvas->translate(370, 0); |
| 79 | slug->draw(canvas); |
| 80 | for (float scale = 1.5; scale < 4; scale += 0.5) { |
| 81 | canvas->translate(-370, 20 * scale); |
| 82 | canvas->save(); |
| 83 | canvas->scale(scale, scale); |
| 84 | canvas->rotate(5); |
| 85 | canvas->drawTextBlob(blob, 10, 10, p); |
| 86 | canvas->restore(); |
| 87 | canvas->translate(370, 0); |
| 88 | canvas->save(); |
| 89 | canvas->scale(scale, scale); |
| 90 | canvas->rotate(5); |
| 91 | |
| 92 | slug->draw(canvas); |
| 93 | canvas->restore(); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | private: |
| 98 | sk_sp<SkTextBlob> makeBlob() { |
| 99 | SkTextBlobBuilder builder; |
| 100 | |
| 101 | SkFont font; |
| 102 | font.setSubpixel(true); |
| 103 | font.setEdging(SkFont::Edging::kAntiAlias); |
| 104 | font.setTypeface(fTypeface); |
| 105 | font.setSize(16); |
| 106 | |
Herb Derby | 3050ef5 | 2022-09-29 21:12:37 -0400 | [diff] [blame] | 107 | const SkTextBlobBuilder::RunBuffer& buf = builder.allocRun(font, fGlyphs.size(), 0, 0); |
| 108 | memcpy(buf.glyphs, fGlyphs.begin(), fGlyphs.size() * sizeof(uint16_t)); |
Herb Derby | 25c0e1a | 2021-11-10 08:34:51 -0500 | [diff] [blame] | 109 | return builder.make(); |
| 110 | } |
| 111 | |
| 112 | SkTDArray<uint16_t> fGlyphs; |
| 113 | sk_sp<SkTypeface> fTypeface; |
| 114 | const char* fText; |
| 115 | using INHERITED = skiagm::GM; |
| 116 | }; |
| 117 | |
| 118 | DEF_GM(return new SlugGM("hamburgefons");) |
| 119 | #endif |