blob: 9ab8e559ffcdc044f63638981737302f2a35fb7e [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
8#include "gm.h"
9#include "sk_tool_utils.h"
10
11#include "Resources.h"
12#include "SkCanvas.h"
13#include "SkStream.h"
14#include "SkTo.h"
15#include "SkTypeface.h"
16
17namespace skiagm {
18class ScaledEmojiRenderingGM : public GM {
19public:
20 ScaledEmojiRenderingGM() {}
21
22protected:
23 sk_sp<SkTypeface> typefaces[4];
24
25 void onOnceBeforeDraw() override {
26 typefaces[0] = MakeResourceAsTypeface("fonts/colr.ttf");
27 typefaces[1] = MakeResourceAsTypeface("fonts/sbix.ttf");
28 typefaces[2] = MakeResourceAsTypeface("fonts/cbdt.ttf");
29 typefaces[3] = sk_tool_utils::create_portable_typeface("Emoji", SkFontStyle());
30 }
31
32 SkString onShortName() override {
33 SkString name("scaledemoji_rendering");
34 name.append(sk_tool_utils::platform_font_manager());
35 return name;
36 }
37
38 SkISize onISize() override { return SkISize::Make(1200, 1200); }
39
40 void onDraw(SkCanvas* canvas) override {
41
Mike Kleind46dce32018-08-16 10:17:03 -040042 canvas->drawColor(SK_ColorGRAY);
Bruce Wang77bf48a2018-07-18 15:32:08 -040043 SkScalar y = 0;
44
45 for (const auto& typeface: typefaces) {
46 SkPaint paint;
47 paint.setTypeface(typeface);
48 const char* text = sk_tool_utils::emoji_sample_text();
49 SkPaint::FontMetrics metrics;
50
51 for (SkScalar textSize : { 70, 150 }) {
52 paint.setTextSize(textSize);
53 paint.getFontMetrics(&metrics);
54 // All typefaces should support subpixel mode
55 paint.setSubpixelText(true);
56 y += -metrics.fAscent;
57
58 int len = SkToInt(strlen(text));
59 SkAutoTArray<SkPoint> pos(len);
60 SkAutoTArray<SkScalar> widths(len);
61 int found = paint.getTextWidths(text, len, &widths[0]);
62 SkScalar x = SkIntToScalar(10);
63 for (int i = 0; i < found; ++i) {
64 pos[i].set(x, y);
65 x += widths[i];
66 }
67
68 canvas->drawPosText(text, len, &pos[0], paint);
69 y += metrics.fDescent + metrics.fLeading;
70 }
71 }
72 }
73
74private:
75 typedef GM INHERITED;
76};
77
78//////////////////////////////////////////////////////////////////////////////
79
80DEF_GM(return new ScaledEmojiRenderingGM;)
Mike Kleind46dce32018-08-16 10:17:03 -040081}