Bruce Wang | 77bf48a | 2018-07-18 15:32:08 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "gm/gm.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 9 | #include "include/core/SkCanvas.h" |
| 10 | #include "include/core/SkColor.h" |
| 11 | #include "include/core/SkFont.h" |
| 12 | #include "include/core/SkFontMetrics.h" |
| 13 | #include "include/core/SkFontStyle.h" |
| 14 | #include "include/core/SkFontTypes.h" |
| 15 | #include "include/core/SkPaint.h" |
| 16 | #include "include/core/SkRefCnt.h" |
| 17 | #include "include/core/SkScalar.h" |
| 18 | #include "include/core/SkSize.h" |
| 19 | #include "include/core/SkString.h" |
| 20 | #include "include/core/SkTypeface.h" |
Ben Wagner | 17c639f | 2022-04-14 10:12:44 -0400 | [diff] [blame] | 21 | #include "src/core/SkEnumerate.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 22 | #include "tools/Resources.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 23 | #include "tools/ToolUtils.h" |
Kevin Lubick | e836c3a | 2023-10-20 06:55:35 -0400 | [diff] [blame] | 24 | #include "tools/fonts/FontToolUtils.h" |
Bruce Wang | 77bf48a | 2018-07-18 15:32:08 -0400 | [diff] [blame] | 25 | |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 26 | #include <string.h> |
| 27 | #include <initializer_list> |
Bruce Wang | 77bf48a | 2018-07-18 15:32:08 -0400 | [diff] [blame] | 28 | |
| 29 | namespace skiagm { |
| 30 | class ScaledEmojiRenderingGM : public GM { |
| 31 | public: |
| 32 | ScaledEmojiRenderingGM() {} |
| 33 | |
| 34 | protected: |
Ben Wagner | 17c639f | 2022-04-14 10:12:44 -0400 | [diff] [blame] | 35 | struct Test { |
| 36 | enum class Source { Resource, Portable }; |
| 37 | Source const fontSource; |
| 38 | char const * const fontName; |
| 39 | char const * const text; |
| 40 | }; |
Kevin Lubick | e836c3a | 2023-10-20 06:55:35 -0400 | [diff] [blame] | 41 | static constexpr char const * const sampleText = ToolUtils::EmojiSampleText(); |
Ben Wagner | 17c639f | 2022-04-14 10:12:44 -0400 | [diff] [blame] | 42 | static constexpr const Test tests[] = { |
Ben Wagner | 9cbadcd | 2022-04-20 17:52:50 -0400 | [diff] [blame] | 43 | { Test::Source::Resource, "fonts/colr.ttf" , sampleText }, |
| 44 | { Test::Source::Resource, "fonts/sbix.ttf" , sampleText }, |
| 45 | { Test::Source::Resource, "fonts/cbdt.ttf" , sampleText }, |
| 46 | { Test::Source::Portable, "Emoji" , sampleText }, |
Ben Wagner | ae7ec91 | 2022-06-01 17:02:53 -0400 | [diff] [blame] | 47 | { Test::Source::Resource, "fonts/SampleSVG.ttf", "abcdefghij" }, |
Ben Wagner | 17c639f | 2022-04-14 10:12:44 -0400 | [diff] [blame] | 48 | }; |
Herb Derby | c37b386 | 2022-06-21 09:49:17 -0400 | [diff] [blame] | 49 | sk_sp<SkTypeface> typefaces[std::size(tests)]; |
Bruce Wang | 77bf48a | 2018-07-18 15:32:08 -0400 | [diff] [blame] | 50 | void onOnceBeforeDraw() override { |
Ben Wagner | 17c639f | 2022-04-14 10:12:44 -0400 | [diff] [blame] | 51 | for (auto&& [i, test] : SkMakeEnumerate(tests)) { |
| 52 | if (test.fontSource == Test::Source::Resource) { |
Kevin Lubick | 1e97119 | 2023-11-10 16:14:44 -0500 | [diff] [blame] | 53 | typefaces[i] = ToolUtils::CreateTypefaceFromResource(test.fontName); |
Kevin Lubick | bca43ec | 2023-10-30 10:11:22 -0400 | [diff] [blame] | 54 | if (!typefaces[i]) { |
| 55 | typefaces[i] = ToolUtils::DefaultTypeface(); |
| 56 | } |
Ben Wagner | 17c639f | 2022-04-14 10:12:44 -0400 | [diff] [blame] | 57 | } else if (test.fontSource == Test::Source::Portable) { |
Kevin Lubick | e836c3a | 2023-10-20 06:55:35 -0400 | [diff] [blame] | 58 | typefaces[i] = ToolUtils::CreatePortableTypeface(test.fontName, SkFontStyle()); |
Ben Wagner | 17c639f | 2022-04-14 10:12:44 -0400 | [diff] [blame] | 59 | } else { |
| 60 | SK_ABORT("Unknown test type"); |
| 61 | } |
| 62 | } |
Bruce Wang | 77bf48a | 2018-07-18 15:32:08 -0400 | [diff] [blame] | 63 | } |
| 64 | |
Leandro Lovisolo | 24fa211 | 2023-08-15 19:05:17 +0000 | [diff] [blame] | 65 | SkString getName() const override { return SkString("scaledemoji_rendering"); } |
Bruce Wang | 77bf48a | 2018-07-18 15:32:08 -0400 | [diff] [blame] | 66 | |
Leandro Lovisolo | 8f02388 | 2023-08-15 21:13:52 +0000 | [diff] [blame] | 67 | SkISize getISize() override { return SkISize::Make(1200, 1200); } |
Bruce Wang | 77bf48a | 2018-07-18 15:32:08 -0400 | [diff] [blame] | 68 | |
| 69 | void onDraw(SkCanvas* canvas) override { |
| 70 | |
Mike Klein | d46dce3 | 2018-08-16 10:17:03 -0400 | [diff] [blame] | 71 | canvas->drawColor(SK_ColorGRAY); |
Ben Wagner | 17c639f | 2022-04-14 10:12:44 -0400 | [diff] [blame] | 72 | SkPaint textPaint; |
| 73 | textPaint.setColor(SK_ColorCYAN); |
| 74 | |
| 75 | SkPaint boundsPaint; |
| 76 | boundsPaint.setStrokeWidth(2); |
| 77 | boundsPaint.setStyle(SkPaint::kStroke_Style); |
| 78 | boundsPaint.setColor(SK_ColorGREEN); |
| 79 | |
| 80 | SkPaint advancePaint; |
| 81 | advancePaint.setColor(SK_ColorRED); |
Bruce Wang | 77bf48a | 2018-07-18 15:32:08 -0400 | [diff] [blame] | 82 | |
Ben Wagner | 6689951 | 2021-12-01 17:27:08 -0500 | [diff] [blame] | 83 | SkScalar y = 0; |
Ben Wagner | 17c639f | 2022-04-14 10:12:44 -0400 | [diff] [blame] | 84 | for (auto&& [i, test] : SkMakeEnumerate(tests)) { |
| 85 | SkFont font(typefaces[i]); |
Mike Reed | 088b74e | 2018-12-24 14:52:46 -0500 | [diff] [blame] | 86 | font.setEdging(SkFont::Edging::kAlias); |
| 87 | |
Ben Wagner | 17c639f | 2022-04-14 10:12:44 -0400 | [diff] [blame] | 88 | const char* text = test.text; |
Mike Reed | b5784ac | 2018-11-12 09:35:15 -0500 | [diff] [blame] | 89 | SkFontMetrics metrics; |
Bruce Wang | 77bf48a | 2018-07-18 15:32:08 -0400 | [diff] [blame] | 90 | |
| 91 | for (SkScalar textSize : { 70, 150 }) { |
Mike Reed | 088b74e | 2018-12-24 14:52:46 -0500 | [diff] [blame] | 92 | font.setSize(textSize); |
| 93 | font.getMetrics(&metrics); |
Bruce Wang | 77bf48a | 2018-07-18 15:32:08 -0400 | [diff] [blame] | 94 | // All typefaces should support subpixel mode |
Mike Reed | 088b74e | 2018-12-24 14:52:46 -0500 | [diff] [blame] | 95 | font.setSubpixel(true); |
Ben Wagner | 6689951 | 2021-12-01 17:27:08 -0500 | [diff] [blame] | 96 | |
Bruce Wang | 77bf48a | 2018-07-18 15:32:08 -0400 | [diff] [blame] | 97 | y += -metrics.fAscent; |
| 98 | |
Ben Wagner | 6689951 | 2021-12-01 17:27:08 -0500 | [diff] [blame] | 99 | SkScalar x = 0; |
| 100 | for (bool fakeBold : { false, true }) { |
| 101 | font.setEmbolden(fakeBold); |
| 102 | SkRect bounds; |
Ben Wagner | 17c639f | 2022-04-14 10:12:44 -0400 | [diff] [blame] | 103 | SkScalar advance = font.measureText(text, strlen(text), SkTextEncoding::kUTF8, |
| 104 | &bounds, &textPaint); |
Ben Wagner | 6689951 | 2021-12-01 17:27:08 -0500 | [diff] [blame] | 105 | canvas->drawSimpleText(text, strlen(text), SkTextEncoding::kUTF8, |
Ben Wagner | 17c639f | 2022-04-14 10:12:44 -0400 | [diff] [blame] | 106 | x, y, font, textPaint); |
| 107 | if ((false)) { |
| 108 | bounds.offset(x, y); |
| 109 | canvas->drawRect(bounds, boundsPaint); |
| 110 | SkRect advanceRect = SkRect::MakeLTRB(x, y + 2, x + advance, y + 4); |
| 111 | canvas->drawRect(advanceRect, advancePaint); |
| 112 | } |
Ben Wagner | 6689951 | 2021-12-01 17:27:08 -0500 | [diff] [blame] | 113 | x += bounds.width() * 1.2; |
| 114 | } |
Bruce Wang | 77bf48a | 2018-07-18 15:32:08 -0400 | [diff] [blame] | 115 | y += metrics.fDescent + metrics.fLeading; |
Ben Wagner | 6689951 | 2021-12-01 17:27:08 -0500 | [diff] [blame] | 116 | x = 0; |
Bruce Wang | 77bf48a | 2018-07-18 15:32:08 -0400 | [diff] [blame] | 117 | } |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | private: |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 122 | using INHERITED = GM; |
Bruce Wang | 77bf48a | 2018-07-18 15:32:08 -0400 | [diff] [blame] | 123 | }; |
| 124 | |
| 125 | ////////////////////////////////////////////////////////////////////////////// |
| 126 | |
| 127 | DEF_GM(return new ScaledEmojiRenderingGM;) |
John Stiles | a6841be | 2020-08-06 14:11:56 -0400 | [diff] [blame] | 128 | } // namespace skiagm |