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" |
| 21 | #include "tools/Resources.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 22 | #include "tools/ToolUtils.h" |
Bruce Wang | 77bf48a | 2018-07-18 15:32:08 -0400 | [diff] [blame] | 23 | |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 24 | #include <string.h> |
| 25 | #include <initializer_list> |
Bruce Wang | 77bf48a | 2018-07-18 15:32:08 -0400 | [diff] [blame] | 26 | |
| 27 | namespace skiagm { |
| 28 | class ScaledEmojiRenderingGM : public GM { |
| 29 | public: |
| 30 | ScaledEmojiRenderingGM() {} |
| 31 | |
| 32 | protected: |
| 33 | sk_sp<SkTypeface> typefaces[4]; |
| 34 | |
| 35 | void onOnceBeforeDraw() override { |
| 36 | typefaces[0] = MakeResourceAsTypeface("fonts/colr.ttf"); |
| 37 | typefaces[1] = MakeResourceAsTypeface("fonts/sbix.ttf"); |
| 38 | typefaces[2] = MakeResourceAsTypeface("fonts/cbdt.ttf"); |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 39 | typefaces[3] = ToolUtils::create_portable_typeface("Emoji", SkFontStyle()); |
Bruce Wang | 77bf48a | 2018-07-18 15:32:08 -0400 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | SkString onShortName() override { |
Mike Klein | bea1f94 | 2019-03-08 11:11:55 -0600 | [diff] [blame] | 43 | return SkString("scaledemoji_rendering"); |
Bruce Wang | 77bf48a | 2018-07-18 15:32:08 -0400 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | SkISize onISize() override { return SkISize::Make(1200, 1200); } |
| 47 | |
| 48 | void onDraw(SkCanvas* canvas) override { |
| 49 | |
Mike Klein | d46dce3 | 2018-08-16 10:17:03 -0400 | [diff] [blame] | 50 | canvas->drawColor(SK_ColorGRAY); |
Bruce Wang | 77bf48a | 2018-07-18 15:32:08 -0400 | [diff] [blame] | 51 | SkScalar y = 0; |
| 52 | |
| 53 | for (const auto& typeface: typefaces) { |
Mike Reed | 088b74e | 2018-12-24 14:52:46 -0500 | [diff] [blame] | 54 | SkFont font(typeface); |
| 55 | font.setEdging(SkFont::Edging::kAlias); |
| 56 | |
Bruce Wang | 77bf48a | 2018-07-18 15:32:08 -0400 | [diff] [blame] | 57 | SkPaint paint; |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 58 | const char* text = ToolUtils::emoji_sample_text(); |
Mike Reed | b5784ac | 2018-11-12 09:35:15 -0500 | [diff] [blame] | 59 | SkFontMetrics metrics; |
Bruce Wang | 77bf48a | 2018-07-18 15:32:08 -0400 | [diff] [blame] | 60 | |
| 61 | for (SkScalar textSize : { 70, 150 }) { |
Mike Reed | 088b74e | 2018-12-24 14:52:46 -0500 | [diff] [blame] | 62 | font.setSize(textSize); |
| 63 | font.getMetrics(&metrics); |
Bruce Wang | 77bf48a | 2018-07-18 15:32:08 -0400 | [diff] [blame] | 64 | // All typefaces should support subpixel mode |
Mike Reed | 088b74e | 2018-12-24 14:52:46 -0500 | [diff] [blame] | 65 | font.setSubpixel(true); |
Bruce Wang | 77bf48a | 2018-07-18 15:32:08 -0400 | [diff] [blame] | 66 | y += -metrics.fAscent; |
| 67 | |
Ben Wagner | 51e15a6 | 2019-05-07 15:38:46 -0400 | [diff] [blame] | 68 | canvas->drawSimpleText(text, strlen(text), SkTextEncoding::kUTF8, |
Mike Reed | 088b74e | 2018-12-24 14:52:46 -0500 | [diff] [blame] | 69 | 10, y, font, paint); |
Bruce Wang | 77bf48a | 2018-07-18 15:32:08 -0400 | [diff] [blame] | 70 | y += metrics.fDescent + metrics.fLeading; |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | private: |
| 76 | typedef GM INHERITED; |
| 77 | }; |
| 78 | |
| 79 | ////////////////////////////////////////////////////////////////////////////// |
| 80 | |
| 81 | DEF_GM(return new ScaledEmojiRenderingGM;) |
Mike Klein | d46dce3 | 2018-08-16 10:17:03 -0400 | [diff] [blame] | 82 | } |