2018-02-13 20:21:34 +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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "gm.h"
|
|
|
|
#include "sk_tool_utils.h"
|
|
|
|
|
|
|
|
#include "Resources.h"
|
|
|
|
#include "SkCanvas.h"
|
|
|
|
#include "SkStream.h"
|
2018-12-14 21:47:45 +00:00
|
|
|
#include "SkTextBlob.h"
|
2018-06-13 13:59:02 +00:00
|
|
|
#include "SkTo.h"
|
2018-02-13 20:21:34 +00:00
|
|
|
#include "SkTypeface.h"
|
|
|
|
|
2018-12-17 19:21:31 +00:00
|
|
|
static sk_sp<SkTextBlob> make_hpos_test_blob_utf8(const char* text, const SkFont& font) {
|
|
|
|
constexpr SkTextEncoding enc = SkTextEncoding::kUTF8;
|
|
|
|
SkTextBlobBuilder builder;
|
|
|
|
size_t len = strlen(text);
|
|
|
|
int glyphCount = font.countText(text, len, enc);
|
|
|
|
const auto& buffer = builder.allocRunPosH(font, glyphCount, 0);
|
|
|
|
(void)font.textToGlyphs(text, len, enc, buffer.glyphs, glyphCount);
|
|
|
|
font.getXPos(buffer.glyphs, glyphCount, buffer.pos);
|
|
|
|
return builder.make();
|
|
|
|
}
|
|
|
|
|
2018-02-13 20:21:34 +00:00
|
|
|
namespace skiagm {
|
|
|
|
|
|
|
|
class ScaledEmojiGM : public GM {
|
|
|
|
public:
|
|
|
|
ScaledEmojiGM() { }
|
|
|
|
|
|
|
|
protected:
|
|
|
|
struct EmojiFont {
|
|
|
|
sk_sp<SkTypeface> fTypeface;
|
|
|
|
const char* fText;
|
|
|
|
} fEmojiFont;
|
|
|
|
|
|
|
|
void onOnceBeforeDraw() override {
|
|
|
|
fEmojiFont.fTypeface = sk_tool_utils::emoji_typeface();
|
|
|
|
fEmojiFont.fText = sk_tool_utils::emoji_sample_text();
|
|
|
|
}
|
|
|
|
|
|
|
|
SkString onShortName() override {
|
|
|
|
SkString name("scaledemoji");
|
|
|
|
name.append(sk_tool_utils::platform_font_manager());
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2018-02-13 20:21:34 +00:00
|
|
|
|
|
|
|
SkPaint paint;
|
2018-12-26 03:06:17 +00:00
|
|
|
SkFont font(fEmojiFont.fTypeface);
|
|
|
|
font.setEdging(SkFont::Edging::kAlias);
|
|
|
|
|
2018-02-13 20:21:34 +00:00
|
|
|
const char* text = fEmojiFont.fText;
|
|
|
|
|
|
|
|
// draw text at different point sizes
|
|
|
|
// Testing GPU bitmap path, SDF path with no scaling,
|
|
|
|
// SDF path with scaling, path rendering with scaling
|
2018-11-12 14:35:15 +00:00
|
|
|
SkFontMetrics metrics;
|
2018-02-13 20:21:34 +00:00
|
|
|
SkScalar y = 0;
|
|
|
|
for (SkScalar textSize : { 70, 180, 270, 340 }) {
|
2018-12-26 03:06:17 +00:00
|
|
|
font.setSize(textSize);
|
|
|
|
font.getMetrics(&metrics);
|
2018-02-13 20:21:34 +00:00
|
|
|
y += -metrics.fAscent;
|
2018-12-26 03:06:17 +00:00
|
|
|
canvas->drawSimpleText(text, strlen(text), kUTF8_SkTextEncoding, 10, y, font, paint);
|
2018-02-13 20:21:34 +00:00
|
|
|
y += metrics.fDescent + metrics.fLeading;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-03-02 15:41:43 +00:00
|
|
|
private:
|
|
|
|
typedef GM INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
class ScaledEmojiPosGM : public GM {
|
|
|
|
public:
|
|
|
|
ScaledEmojiPosGM() {}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
struct EmojiFont {
|
|
|
|
sk_sp<SkTypeface> fTypeface;
|
|
|
|
const char* fText;
|
|
|
|
} fEmojiFont;
|
|
|
|
|
|
|
|
void onOnceBeforeDraw() override {
|
|
|
|
fEmojiFont.fTypeface = sk_tool_utils::emoji_typeface();
|
|
|
|
fEmojiFont.fText = sk_tool_utils::emoji_sample_text();
|
|
|
|
}
|
|
|
|
|
|
|
|
SkString onShortName() override {
|
|
|
|
SkString name("scaledemojipos");
|
|
|
|
name.append(sk_tool_utils::platform_font_manager());
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2018-03-02 15:41:43 +00:00
|
|
|
|
|
|
|
SkPaint paint;
|
2018-12-14 21:47:45 +00:00
|
|
|
SkFont font;
|
|
|
|
font.setTypeface(fEmojiFont.fTypeface);
|
2018-03-02 15:41:43 +00:00
|
|
|
const char* text = fEmojiFont.fText;
|
|
|
|
|
|
|
|
// draw text at different point sizes
|
|
|
|
// Testing GPU bitmap path, SDF path with no scaling,
|
|
|
|
// SDF path with scaling, path rendering with scaling
|
2018-11-12 14:35:15 +00:00
|
|
|
SkFontMetrics metrics;
|
2018-03-02 15:41:43 +00:00
|
|
|
SkScalar y = 0;
|
|
|
|
for (SkScalar textSize : { 70, 180, 270, 340 }) {
|
2018-12-14 21:47:45 +00:00
|
|
|
font.setSize(textSize);
|
|
|
|
font.getMetrics(&metrics);
|
2018-03-02 15:41:43 +00:00
|
|
|
y += -metrics.fAscent;
|
|
|
|
|
2018-12-17 19:21:31 +00:00
|
|
|
sk_sp<SkTextBlob> blob = make_hpos_test_blob_utf8(text, font);
|
2018-12-14 21:47:45 +00:00
|
|
|
// Draw with an origin.
|
|
|
|
canvas->drawTextBlob(blob, 10, y, paint);
|
2018-03-02 15:41:43 +00:00
|
|
|
|
2018-12-14 21:47:45 +00:00
|
|
|
// Draw with shifted canvas.
|
|
|
|
canvas->save();
|
|
|
|
canvas->translate(750, 0);
|
|
|
|
canvas->drawTextBlob(blob, 10, y, paint);
|
|
|
|
canvas->restore();
|
2018-03-02 15:41:43 +00:00
|
|
|
|
|
|
|
y += metrics.fDescent + metrics.fLeading;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2018-02-13 20:21:34 +00:00
|
|
|
typedef GM INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
DEF_GM(return new ScaledEmojiGM;)
|
2018-03-02 15:41:43 +00:00
|
|
|
DEF_GM(return new ScaledEmojiPosGM;)
|
2018-02-13 20:21:34 +00:00
|
|
|
|
|
|
|
}
|