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.
|
|
|
|
*/
|
|
|
|
|
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/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/SkTextBlob.h"
|
|
|
|
#include "include/core/SkTypeface.h"
|
2022-04-04 20:08:54 +00:00
|
|
|
#include "src/utils/SkUTF.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "tools/ToolUtils.h"
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <initializer_list>
|
2018-02-13 20:21:34 +00:00
|
|
|
|
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 {
|
2019-03-20 16:12:10 +00:00
|
|
|
fEmojiFont.fTypeface = ToolUtils::emoji_typeface();
|
|
|
|
fEmojiFont.fText = ToolUtils::emoji_sample_text();
|
2018-02-13 20:21:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SkString onShortName() override {
|
2019-03-08 17:11:55 +00:00
|
|
|
return SkString("scaledemoji");
|
2018-02-13 20:21:34 +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);
|
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;
|
2019-05-07 19:38:46 +00:00
|
|
|
canvas->drawSimpleText(text, strlen(text), SkTextEncoding::kUTF8, 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:
|
2020-09-03 02:42:33 +00:00
|
|
|
using INHERITED = GM;
|
2018-03-02 15:41:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class ScaledEmojiPosGM : public GM {
|
|
|
|
public:
|
|
|
|
ScaledEmojiPosGM() {}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
struct EmojiFont {
|
|
|
|
sk_sp<SkTypeface> fTypeface;
|
|
|
|
const char* fText;
|
|
|
|
} fEmojiFont;
|
|
|
|
|
|
|
|
void onOnceBeforeDraw() override {
|
2019-03-20 16:12:10 +00:00
|
|
|
fEmojiFont.fTypeface = ToolUtils::emoji_typeface();
|
|
|
|
fEmojiFont.fText = ToolUtils::emoji_sample_text();
|
2018-03-02 15:41:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SkString onShortName() override {
|
2019-03-08 17:11:55 +00:00
|
|
|
return SkString("scaledemojipos");
|
2018-03-02 15:41:43 +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);
|
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;
|
|
|
|
}
|
2021-04-23 20:21:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
using INHERITED = GM;
|
|
|
|
};
|
|
|
|
|
|
|
|
class ScaledEmojiPerspectiveGM : public GM {
|
|
|
|
public:
|
|
|
|
ScaledEmojiPerspectiveGM() {}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
struct EmojiFont {
|
|
|
|
sk_sp<SkTypeface> fTypeface;
|
2022-04-04 20:08:54 +00:00
|
|
|
SkString fText;
|
2021-04-23 20:21:58 +00:00
|
|
|
} fEmojiFont;
|
|
|
|
|
|
|
|
void onOnceBeforeDraw() override {
|
|
|
|
fEmojiFont.fTypeface = ToolUtils::emoji_typeface();
|
2022-04-04 20:08:54 +00:00
|
|
|
|
|
|
|
int count = 0;
|
|
|
|
const char* ch_ptr = ToolUtils::emoji_sample_text();
|
|
|
|
const char* ch_end = ch_ptr + strlen(ch_ptr);
|
|
|
|
while (ch_ptr < ch_end && count < 2) {
|
|
|
|
SkUnichar ch = SkUTF::NextUTF8(&ch_ptr, ch_end);
|
|
|
|
if (ch != ' ') {
|
|
|
|
fEmojiFont.fText.appendUnichar(ch);
|
|
|
|
++count;
|
|
|
|
}
|
|
|
|
}
|
2021-04-23 20:21:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SkString onShortName() override {
|
|
|
|
return SkString("scaledemojiperspective");
|
|
|
|
}
|
|
|
|
|
|
|
|
SkISize onISize() override { return SkISize::Make(1200, 1200); }
|
|
|
|
|
|
|
|
void onDraw(SkCanvas* canvas) override {
|
|
|
|
|
|
|
|
canvas->drawColor(SK_ColorGRAY);
|
|
|
|
SkMatrix taper;
|
|
|
|
taper.setPerspY(-0.0025f);
|
|
|
|
|
|
|
|
SkPaint paint;
|
|
|
|
SkFont font;
|
|
|
|
font.setTypeface(fEmojiFont.fTypeface);
|
|
|
|
font.setSize(40);
|
2022-04-04 20:08:54 +00:00
|
|
|
sk_sp<SkTextBlob> blob = make_hpos_test_blob_utf8(fEmojiFont.fText.c_str(), font);
|
2018-03-02 15:41:43 +00:00
|
|
|
|
2021-04-23 20:21:58 +00:00
|
|
|
// draw text at different point sizes
|
|
|
|
// Testing GPU bitmap path, SDF path with no scaling,
|
|
|
|
// SDF path with scaling, path rendering with scaling
|
|
|
|
SkFontMetrics metrics;
|
|
|
|
font.getMetrics(&metrics);
|
|
|
|
for (auto rotate : {0.0, 45.0, 90.0, 135.0, 180.0, 225.0, 270.0, 315.0}) {
|
|
|
|
canvas->save();
|
|
|
|
SkMatrix perspective;
|
|
|
|
perspective.postTranslate(-600, -600);
|
|
|
|
perspective.postConcat(taper);
|
|
|
|
perspective.postRotate(rotate);
|
|
|
|
perspective.postTranslate(600, 600);
|
|
|
|
canvas->concat(perspective);
|
|
|
|
SkScalar y = 670;
|
|
|
|
for (int i = 0; i < 5; i++) {
|
|
|
|
|
|
|
|
y += -metrics.fAscent;
|
|
|
|
|
|
|
|
// Draw with an origin.
|
|
|
|
canvas->drawTextBlob(blob, 565, y, paint);
|
|
|
|
|
|
|
|
y += metrics.fDescent + metrics.fLeading;
|
|
|
|
}
|
|
|
|
canvas->restore();
|
|
|
|
}
|
2018-03-02 15:41:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2020-09-03 02:42:33 +00:00
|
|
|
using INHERITED = GM;
|
2018-02-13 20:21:34 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
DEF_GM(return new ScaledEmojiGM;)
|
2018-03-02 15:41:43 +00:00
|
|
|
DEF_GM(return new ScaledEmojiPosGM;)
|
2021-04-23 20:21:58 +00:00
|
|
|
DEF_GM(return new ScaledEmojiPerspectiveGM;)
|
2020-08-06 18:11:56 +00:00
|
|
|
} // namespace skiagm
|