2018-07-18 19:32:08 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2018 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "gm/gm.h"
|
|
|
|
#include "include/core/SkCanvas.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkColor.h"
|
|
|
|
#include "include/core/SkFont.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkFontMetrics.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkFontStyle.h"
|
|
|
|
#include "include/core/SkFontTypes.h"
|
|
|
|
#include "include/core/SkPaint.h"
|
|
|
|
#include "include/core/SkRefCnt.h"
|
|
|
|
#include "include/core/SkScalar.h"
|
|
|
|
#include "include/core/SkSize.h"
|
|
|
|
#include "include/core/SkString.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkTypeface.h"
|
2022-04-14 14:12:44 +00:00
|
|
|
#include "src/core/SkEnumerate.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "tools/Resources.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "tools/ToolUtils.h"
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <initializer_list>
|
2018-07-18 19:32:08 +00:00
|
|
|
|
|
|
|
namespace skiagm {
|
|
|
|
class ScaledEmojiRenderingGM : public GM {
|
|
|
|
public:
|
|
|
|
ScaledEmojiRenderingGM() {}
|
|
|
|
|
|
|
|
protected:
|
2022-04-14 14:12:44 +00:00
|
|
|
struct Test {
|
|
|
|
enum class Source { Resource, Portable };
|
|
|
|
Source const fontSource;
|
|
|
|
char const * const fontName;
|
|
|
|
char const * const text;
|
|
|
|
};
|
|
|
|
static constexpr char const * const sampleText = ToolUtils::emoji_sample_text();
|
|
|
|
static constexpr const Test tests[] = {
|
2022-04-20 21:52:50 +00:00
|
|
|
{ Test::Source::Resource, "fonts/colr.ttf" , sampleText },
|
|
|
|
{ Test::Source::Resource, "fonts/sbix.ttf" , sampleText },
|
|
|
|
{ Test::Source::Resource, "fonts/cbdt.ttf" , sampleText },
|
|
|
|
{ Test::Source::Portable, "Emoji" , sampleText },
|
|
|
|
{ Test::Source::Resource, "fonts/SampleSVG.ttf", "abcdefghi" },
|
2022-04-14 14:12:44 +00:00
|
|
|
};
|
|
|
|
sk_sp<SkTypeface> typefaces[SK_ARRAY_COUNT(tests)];
|
2018-07-18 19:32:08 +00:00
|
|
|
void onOnceBeforeDraw() override {
|
2022-04-14 14:12:44 +00:00
|
|
|
for (auto&& [i, test] : SkMakeEnumerate(tests)) {
|
|
|
|
if (test.fontSource == Test::Source::Resource) {
|
|
|
|
typefaces[i] = MakeResourceAsTypeface(test.fontName);
|
|
|
|
} else if (test.fontSource == Test::Source::Portable) {
|
|
|
|
typefaces[i] = ToolUtils::create_portable_typeface(test.fontName, SkFontStyle());
|
|
|
|
} else {
|
|
|
|
SK_ABORT("Unknown test type");
|
|
|
|
}
|
|
|
|
}
|
2018-07-18 19:32:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SkString onShortName() override {
|
2019-03-08 17:11:55 +00:00
|
|
|
return SkString("scaledemoji_rendering");
|
2018-07-18 19:32:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SkISize onISize() override { return SkISize::Make(1200, 1200); }
|
|
|
|
|
|
|
|
void onDraw(SkCanvas* canvas) override {
|
|
|
|
|
2018-08-16 14:17:03 +00:00
|
|
|
canvas->drawColor(SK_ColorGRAY);
|
2022-04-14 14:12:44 +00:00
|
|
|
SkPaint textPaint;
|
|
|
|
textPaint.setColor(SK_ColorCYAN);
|
|
|
|
|
|
|
|
SkPaint boundsPaint;
|
|
|
|
boundsPaint.setStrokeWidth(2);
|
|
|
|
boundsPaint.setStyle(SkPaint::kStroke_Style);
|
|
|
|
boundsPaint.setColor(SK_ColorGREEN);
|
|
|
|
|
|
|
|
SkPaint advancePaint;
|
|
|
|
advancePaint.setColor(SK_ColorRED);
|
2018-07-18 19:32:08 +00:00
|
|
|
|
2021-12-01 22:27:08 +00:00
|
|
|
SkScalar y = 0;
|
2022-04-14 14:12:44 +00:00
|
|
|
for (auto&& [i, test] : SkMakeEnumerate(tests)) {
|
|
|
|
SkFont font(typefaces[i]);
|
2018-12-24 19:52:46 +00:00
|
|
|
font.setEdging(SkFont::Edging::kAlias);
|
|
|
|
|
2022-04-14 14:12:44 +00:00
|
|
|
const char* text = test.text;
|
2018-11-12 14:35:15 +00:00
|
|
|
SkFontMetrics metrics;
|
2018-07-18 19:32:08 +00:00
|
|
|
|
|
|
|
for (SkScalar textSize : { 70, 150 }) {
|
2018-12-24 19:52:46 +00:00
|
|
|
font.setSize(textSize);
|
|
|
|
font.getMetrics(&metrics);
|
2018-07-18 19:32:08 +00:00
|
|
|
// All typefaces should support subpixel mode
|
2018-12-24 19:52:46 +00:00
|
|
|
font.setSubpixel(true);
|
2021-12-01 22:27:08 +00:00
|
|
|
|
2018-07-18 19:32:08 +00:00
|
|
|
y += -metrics.fAscent;
|
|
|
|
|
2021-12-01 22:27:08 +00:00
|
|
|
SkScalar x = 0;
|
|
|
|
for (bool fakeBold : { false, true }) {
|
|
|
|
font.setEmbolden(fakeBold);
|
|
|
|
SkRect bounds;
|
2022-04-14 14:12:44 +00:00
|
|
|
SkScalar advance = font.measureText(text, strlen(text), SkTextEncoding::kUTF8,
|
|
|
|
&bounds, &textPaint);
|
2021-12-01 22:27:08 +00:00
|
|
|
canvas->drawSimpleText(text, strlen(text), SkTextEncoding::kUTF8,
|
2022-04-14 14:12:44 +00:00
|
|
|
x, y, font, textPaint);
|
|
|
|
if ((false)) {
|
|
|
|
bounds.offset(x, y);
|
|
|
|
canvas->drawRect(bounds, boundsPaint);
|
|
|
|
SkRect advanceRect = SkRect::MakeLTRB(x, y + 2, x + advance, y + 4);
|
|
|
|
canvas->drawRect(advanceRect, advancePaint);
|
|
|
|
}
|
2021-12-01 22:27:08 +00:00
|
|
|
x += bounds.width() * 1.2;
|
|
|
|
}
|
2018-07-18 19:32:08 +00:00
|
|
|
y += metrics.fDescent + metrics.fLeading;
|
2021-12-01 22:27:08 +00:00
|
|
|
x = 0;
|
2018-07-18 19:32:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2020-09-03 02:42:33 +00:00
|
|
|
using INHERITED = GM;
|
2018-07-18 19:32:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
DEF_GM(return new ScaledEmojiRenderingGM;)
|
2020-08-06 18:11:56 +00:00
|
|
|
} // namespace skiagm
|