blob: cbc95d053a557b5835ddef10a5790e71e9c8b985 [file] [log] [blame]
Bruce Wang77bf48a2018-07-18 15:32:08 -04001/*
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 Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -04009#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 Wagner17c639f2022-04-14 10:12:44 -040021#include "src/core/SkEnumerate.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040022#include "tools/Resources.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "tools/ToolUtils.h"
Kevin Lubicke836c3a2023-10-20 06:55:35 -040024#include "tools/fonts/FontToolUtils.h"
Bruce Wang77bf48a2018-07-18 15:32:08 -040025
Ben Wagner7fde8e12019-05-01 17:28:53 -040026#include <string.h>
27#include <initializer_list>
Bruce Wang77bf48a2018-07-18 15:32:08 -040028
29namespace skiagm {
30class ScaledEmojiRenderingGM : public GM {
31public:
32 ScaledEmojiRenderingGM() {}
33
34protected:
Ben Wagner17c639f2022-04-14 10:12:44 -040035 struct Test {
36 enum class Source { Resource, Portable };
37 Source const fontSource;
38 char const * const fontName;
39 char const * const text;
40 };
Kevin Lubicke836c3a2023-10-20 06:55:35 -040041 static constexpr char const * const sampleText = ToolUtils::EmojiSampleText();
Ben Wagner17c639f2022-04-14 10:12:44 -040042 static constexpr const Test tests[] = {
Ben Wagner9cbadcd2022-04-20 17:52:50 -040043 { 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 Wagnerae7ec912022-06-01 17:02:53 -040047 { Test::Source::Resource, "fonts/SampleSVG.ttf", "abcdefghij" },
Ben Wagner17c639f2022-04-14 10:12:44 -040048 };
Herb Derbyc37b3862022-06-21 09:49:17 -040049 sk_sp<SkTypeface> typefaces[std::size(tests)];
Bruce Wang77bf48a2018-07-18 15:32:08 -040050 void onOnceBeforeDraw() override {
Ben Wagner17c639f2022-04-14 10:12:44 -040051 for (auto&& [i, test] : SkMakeEnumerate(tests)) {
52 if (test.fontSource == Test::Source::Resource) {
Kevin Lubick1e971192023-11-10 16:14:44 -050053 typefaces[i] = ToolUtils::CreateTypefaceFromResource(test.fontName);
Kevin Lubickbca43ec2023-10-30 10:11:22 -040054 if (!typefaces[i]) {
55 typefaces[i] = ToolUtils::DefaultTypeface();
56 }
Ben Wagner17c639f2022-04-14 10:12:44 -040057 } else if (test.fontSource == Test::Source::Portable) {
Kevin Lubicke836c3a2023-10-20 06:55:35 -040058 typefaces[i] = ToolUtils::CreatePortableTypeface(test.fontName, SkFontStyle());
Ben Wagner17c639f2022-04-14 10:12:44 -040059 } else {
60 SK_ABORT("Unknown test type");
61 }
62 }
Bruce Wang77bf48a2018-07-18 15:32:08 -040063 }
64
Leandro Lovisolo24fa2112023-08-15 19:05:17 +000065 SkString getName() const override { return SkString("scaledemoji_rendering"); }
Bruce Wang77bf48a2018-07-18 15:32:08 -040066
Leandro Lovisolo8f023882023-08-15 21:13:52 +000067 SkISize getISize() override { return SkISize::Make(1200, 1200); }
Bruce Wang77bf48a2018-07-18 15:32:08 -040068
69 void onDraw(SkCanvas* canvas) override {
70
Mike Kleind46dce32018-08-16 10:17:03 -040071 canvas->drawColor(SK_ColorGRAY);
Ben Wagner17c639f2022-04-14 10:12:44 -040072 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 Wang77bf48a2018-07-18 15:32:08 -040082
Ben Wagner66899512021-12-01 17:27:08 -050083 SkScalar y = 0;
Ben Wagner17c639f2022-04-14 10:12:44 -040084 for (auto&& [i, test] : SkMakeEnumerate(tests)) {
85 SkFont font(typefaces[i]);
Mike Reed088b74e2018-12-24 14:52:46 -050086 font.setEdging(SkFont::Edging::kAlias);
87
Ben Wagner17c639f2022-04-14 10:12:44 -040088 const char* text = test.text;
Mike Reedb5784ac2018-11-12 09:35:15 -050089 SkFontMetrics metrics;
Bruce Wang77bf48a2018-07-18 15:32:08 -040090
91 for (SkScalar textSize : { 70, 150 }) {
Mike Reed088b74e2018-12-24 14:52:46 -050092 font.setSize(textSize);
93 font.getMetrics(&metrics);
Bruce Wang77bf48a2018-07-18 15:32:08 -040094 // All typefaces should support subpixel mode
Mike Reed088b74e2018-12-24 14:52:46 -050095 font.setSubpixel(true);
Ben Wagner66899512021-12-01 17:27:08 -050096
Bruce Wang77bf48a2018-07-18 15:32:08 -040097 y += -metrics.fAscent;
98
Ben Wagner66899512021-12-01 17:27:08 -050099 SkScalar x = 0;
100 for (bool fakeBold : { false, true }) {
101 font.setEmbolden(fakeBold);
102 SkRect bounds;
Ben Wagner17c639f2022-04-14 10:12:44 -0400103 SkScalar advance = font.measureText(text, strlen(text), SkTextEncoding::kUTF8,
104 &bounds, &textPaint);
Ben Wagner66899512021-12-01 17:27:08 -0500105 canvas->drawSimpleText(text, strlen(text), SkTextEncoding::kUTF8,
Ben Wagner17c639f2022-04-14 10:12:44 -0400106 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 Wagner66899512021-12-01 17:27:08 -0500113 x += bounds.width() * 1.2;
114 }
Bruce Wang77bf48a2018-07-18 15:32:08 -0400115 y += metrics.fDescent + metrics.fLeading;
Ben Wagner66899512021-12-01 17:27:08 -0500116 x = 0;
Bruce Wang77bf48a2018-07-18 15:32:08 -0400117 }
118 }
119 }
120
121private:
John Stiles7571f9e2020-09-02 22:42:33 -0400122 using INHERITED = GM;
Bruce Wang77bf48a2018-07-18 15:32:08 -0400123};
124
125//////////////////////////////////////////////////////////////////////////////
126
127DEF_GM(return new ScaledEmojiRenderingGM;)
John Stilesa6841be2020-08-06 14:11:56 -0400128} // namespace skiagm