2014-09-16 12:45:19 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2011 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
2018-06-13 13:59:02 +00:00
|
|
|
|
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/SkColorSpace.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkFont.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkFontStyle.h"
|
|
|
|
#include "include/core/SkFontTypes.h"
|
|
|
|
#include "include/core/SkImageInfo.h"
|
|
|
|
#include "include/core/SkMatrix.h"
|
|
|
|
#include "include/core/SkPaint.h"
|
|
|
|
#include "include/core/SkPoint.h"
|
|
|
|
#include "include/core/SkRect.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/SkSurface.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkSurfaceProps.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkTextBlob.h"
|
|
|
|
#include "include/core/SkTypeface.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkTypes.h"
|
|
|
|
#include "include/private/SkTemplates.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/private/SkTo.h"
|
|
|
|
#include "tools/ToolUtils.h"
|
2014-09-16 12:45:19 +00:00
|
|
|
|
2019-05-01 21:28:53 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
2014-10-20 20:12:54 +00:00
|
|
|
class DFTextGM : public skiagm::GM {
|
2014-09-16 12:45:19 +00:00
|
|
|
public:
|
|
|
|
DFTextGM() {
|
|
|
|
this->setBGColor(0xFFFFFFFF);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2015-03-26 01:17:31 +00:00
|
|
|
void onOnceBeforeDraw() override {
|
2019-03-20 16:12:10 +00:00
|
|
|
fEmojiTypeface = ToolUtils::emoji_typeface();
|
|
|
|
fEmojiText = ToolUtils::emoji_sample_text();
|
2014-10-20 20:12:54 +00:00
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkString onShortName() override {
|
2019-03-08 17:11:55 +00:00
|
|
|
return SkString("dftext");
|
2014-09-16 12:45:19 +00:00
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkISize onISize() override {
|
2014-09-16 12:45:19 +00:00
|
|
|
return SkISize::Make(1024, 768);
|
|
|
|
}
|
|
|
|
|
2020-08-14 02:58:04 +00:00
|
|
|
void onDraw(SkCanvas* inputCanvas) override {
|
2014-10-06 19:50:54 +00:00
|
|
|
SkScalar textSizes[] = { 9.0f, 9.0f*2.0f, 9.0f*5.0f, 9.0f*2.0f*5.0f };
|
2014-09-16 12:45:19 +00:00
|
|
|
SkScalar scales[] = { 2.0f*5.0f, 5.0f, 2.0f, 1.0f };
|
|
|
|
|
|
|
|
// set up offscreen rendering with distance field text
|
2020-07-07 14:20:27 +00:00
|
|
|
auto ctx = inputCanvas->recordingContext();
|
2016-06-20 15:25:02 +00:00
|
|
|
SkISize size = onISize();
|
|
|
|
SkImageInfo info = SkImageInfo::MakeN32(size.width(), size.height(), kPremul_SkAlphaType,
|
2017-01-12 15:13:40 +00:00
|
|
|
inputCanvas->imageInfo().refColorSpace());
|
2020-09-24 18:49:00 +00:00
|
|
|
SkSurfaceProps inputProps;
|
|
|
|
inputCanvas->getProps(&inputProps);
|
|
|
|
SkSurfaceProps props(SkSurfaceProps::kUseDeviceIndependentFonts_Flag | inputProps.flags(),
|
|
|
|
inputProps.pixelGeometry());
|
2016-03-24 01:59:25 +00:00
|
|
|
auto surface(SkSurface::MakeRenderTarget(ctx, SkBudgeted::kNo, info, 0, &props));
|
|
|
|
SkCanvas* canvas = surface ? surface->getCanvas() : inputCanvas;
|
2014-10-07 14:59:45 +00:00
|
|
|
// init our new canvas with the old canvas's matrix
|
2020-12-03 16:21:31 +00:00
|
|
|
canvas->setMatrix(inputCanvas->getLocalToDeviceAs3x3());
|
2014-09-22 21:26:53 +00:00
|
|
|
// apply global scale to test glyph positioning
|
|
|
|
canvas->scale(1.05f, 1.05f);
|
2014-09-16 12:45:19 +00:00
|
|
|
canvas->clear(0xffffffff);
|
|
|
|
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
2015-06-22 13:46:56 +00:00
|
|
|
|
2019-03-20 16:12:10 +00:00
|
|
|
SkFont font(ToolUtils::create_portable_typeface("serif", SkFontStyle()));
|
2018-12-13 21:42:53 +00:00
|
|
|
font.setSubpixel(true);
|
2014-09-16 12:45:19 +00:00
|
|
|
|
|
|
|
const char* text = "Hamburgefons";
|
|
|
|
const size_t textLen = strlen(text);
|
|
|
|
|
|
|
|
// check scaling up
|
|
|
|
SkScalar x = SkIntToScalar(0);
|
|
|
|
SkScalar y = SkIntToScalar(78);
|
|
|
|
for (size_t i = 0; i < SK_ARRAY_COUNT(textSizes); ++i) {
|
|
|
|
SkAutoCanvasRestore acr(canvas, true);
|
|
|
|
canvas->translate(x, y);
|
|
|
|
canvas->scale(scales[i], scales[i]);
|
2018-12-13 21:42:53 +00:00
|
|
|
font.setSize(textSizes[i]);
|
2019-05-07 19:38:46 +00:00
|
|
|
canvas->drawSimpleText(text, textLen, SkTextEncoding::kUTF8, 0, 0, font, paint);
|
2018-12-13 21:42:53 +00:00
|
|
|
y += font.getMetrics(nullptr)*scales[i];
|
2014-09-16 12:45:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// check rotation
|
|
|
|
for (size_t i = 0; i < 5; ++i) {
|
|
|
|
SkScalar rotX = SkIntToScalar(10);
|
|
|
|
SkScalar rotY = y;
|
|
|
|
|
|
|
|
SkAutoCanvasRestore acr(canvas, true);
|
|
|
|
canvas->translate(SkIntToScalar(10 + i * 200), -80);
|
2016-07-12 22:01:19 +00:00
|
|
|
canvas->rotate(SkIntToScalar(i * 5), rotX, rotY);
|
2014-09-16 12:45:19 +00:00
|
|
|
for (int ps = 6; ps <= 32; ps += 3) {
|
2018-12-13 21:42:53 +00:00
|
|
|
font.setSize(SkIntToScalar(ps));
|
2019-05-07 19:38:46 +00:00
|
|
|
canvas->drawSimpleText(text, textLen, SkTextEncoding::kUTF8, rotX, rotY, font, paint);
|
2018-12-13 21:42:53 +00:00
|
|
|
rotY += font.getMetrics(nullptr);
|
2014-09-16 12:45:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// check scaling down
|
2018-12-13 21:42:53 +00:00
|
|
|
font.setEdging(SkFont::Edging::kSubpixelAntiAlias);
|
2014-09-22 21:26:53 +00:00
|
|
|
x = SkIntToScalar(680);
|
2014-09-16 12:45:19 +00:00
|
|
|
y = SkIntToScalar(20);
|
|
|
|
size_t arraySize = SK_ARRAY_COUNT(textSizes);
|
|
|
|
for (size_t i = 0; i < arraySize; ++i) {
|
|
|
|
SkAutoCanvasRestore acr(canvas, true);
|
|
|
|
canvas->translate(x, y);
|
|
|
|
SkScalar scaleFactor = SkScalarInvert(scales[arraySize - i - 1]);
|
|
|
|
canvas->scale(scaleFactor, scaleFactor);
|
2018-12-13 21:42:53 +00:00
|
|
|
font.setSize(textSizes[i]);
|
2019-05-07 19:38:46 +00:00
|
|
|
canvas->drawSimpleText(text, textLen, SkTextEncoding::kUTF8, 0, 0, font, paint);
|
2018-12-13 21:42:53 +00:00
|
|
|
y += font.getMetrics(nullptr)*scaleFactor;
|
2014-09-16 12:45:19 +00:00
|
|
|
}
|
|
|
|
|
2014-09-22 21:26:53 +00:00
|
|
|
// check pos text
|
|
|
|
{
|
|
|
|
SkAutoCanvasRestore acr(canvas, true);
|
|
|
|
|
|
|
|
canvas->scale(2.0f, 2.0f);
|
|
|
|
|
2018-12-13 21:42:53 +00:00
|
|
|
SkAutoTArray<SkGlyphID> glyphs(SkToInt(textLen));
|
2019-05-07 19:38:46 +00:00
|
|
|
int count = font.textToGlyphs(text, textLen, SkTextEncoding::kUTF8, glyphs.get(), textLen);
|
2018-12-13 21:42:53 +00:00
|
|
|
SkAutoTArray<SkPoint> pos(count);
|
|
|
|
font.setSize(textSizes[0]);
|
|
|
|
font.getPos(glyphs.get(), count, pos.get(), {340, 75});
|
2014-09-22 21:26:53 +00:00
|
|
|
|
2018-12-13 21:42:53 +00:00
|
|
|
auto blob = SkTextBlob::MakeFromPosText(glyphs.get(), count * sizeof(SkGlyphID),
|
2019-05-07 19:38:46 +00:00
|
|
|
pos.get(), font, SkTextEncoding::kGlyphID);
|
2018-12-13 21:42:53 +00:00
|
|
|
canvas->drawTextBlob(blob, 0, 0, paint);
|
2014-09-22 21:26:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-09-16 12:45:19 +00:00
|
|
|
// check gamma-corrected blending
|
|
|
|
const SkColor fg[] = {
|
|
|
|
0xFFFFFFFF,
|
|
|
|
0xFFFFFF00, 0xFFFF00FF, 0xFF00FFFF,
|
|
|
|
0xFFFF0000, 0xFF00FF00, 0xFF0000FF,
|
|
|
|
0xFF000000,
|
|
|
|
};
|
|
|
|
|
2015-07-21 19:27:36 +00:00
|
|
|
paint.setColor(0xFFF7F3F7);
|
2015-07-23 21:07:58 +00:00
|
|
|
SkRect r = SkRect::MakeLTRB(670, 215, 820, 397);
|
2014-09-16 12:45:19 +00:00
|
|
|
canvas->drawRect(r, paint);
|
|
|
|
|
2014-09-22 21:26:53 +00:00
|
|
|
x = SkIntToScalar(680);
|
2015-07-23 21:07:58 +00:00
|
|
|
y = SkIntToScalar(235);
|
2018-12-13 21:42:53 +00:00
|
|
|
font.setSize(SkIntToScalar(19));
|
2014-09-16 12:45:19 +00:00
|
|
|
for (size_t i = 0; i < SK_ARRAY_COUNT(fg); ++i) {
|
|
|
|
paint.setColor(fg[i]);
|
|
|
|
|
2019-05-07 19:38:46 +00:00
|
|
|
canvas->drawSimpleText(text, textLen, SkTextEncoding::kUTF8, x, y, font, paint);
|
2018-12-13 21:42:53 +00:00
|
|
|
y += font.getMetrics(nullptr);
|
2014-09-16 12:45:19 +00:00
|
|
|
}
|
|
|
|
|
2015-07-21 19:27:36 +00:00
|
|
|
paint.setColor(0xFF181C18);
|
2015-07-23 21:07:58 +00:00
|
|
|
r = SkRect::MakeLTRB(820, 215, 970, 397);
|
2014-09-16 12:45:19 +00:00
|
|
|
canvas->drawRect(r, paint);
|
|
|
|
|
2014-09-22 21:26:53 +00:00
|
|
|
x = SkIntToScalar(830);
|
2015-07-23 21:07:58 +00:00
|
|
|
y = SkIntToScalar(235);
|
2018-12-13 21:42:53 +00:00
|
|
|
font.setSize(SkIntToScalar(19));
|
2014-09-16 12:45:19 +00:00
|
|
|
for (size_t i = 0; i < SK_ARRAY_COUNT(fg); ++i) {
|
|
|
|
paint.setColor(fg[i]);
|
|
|
|
|
2019-05-07 19:38:46 +00:00
|
|
|
canvas->drawSimpleText(text, textLen, SkTextEncoding::kUTF8, x, y, font, paint);
|
2018-12-13 21:42:53 +00:00
|
|
|
y += font.getMetrics(nullptr);
|
2014-09-16 12:45:19 +00:00
|
|
|
}
|
|
|
|
|
2015-03-16 18:32:49 +00:00
|
|
|
// check skew
|
|
|
|
{
|
2018-12-13 21:42:53 +00:00
|
|
|
font.setEdging(SkFont::Edging::kAntiAlias);
|
2015-03-16 18:32:49 +00:00
|
|
|
SkAutoCanvasRestore acr(canvas, true);
|
|
|
|
canvas->skew(0.0f, 0.151515f);
|
2018-12-13 21:42:53 +00:00
|
|
|
font.setSize(SkIntToScalar(32));
|
2019-05-07 19:38:46 +00:00
|
|
|
canvas->drawSimpleText(text, textLen, SkTextEncoding::kUTF8, 745, 70, font, paint);
|
2015-03-16 18:32:49 +00:00
|
|
|
}
|
|
|
|
{
|
2018-12-13 21:42:53 +00:00
|
|
|
font.setEdging(SkFont::Edging::kSubpixelAntiAlias);
|
2015-03-16 18:32:49 +00:00
|
|
|
SkAutoCanvasRestore acr(canvas, true);
|
|
|
|
canvas->skew(0.5f, 0.0f);
|
2018-12-13 21:42:53 +00:00
|
|
|
font.setSize(SkIntToScalar(32));
|
2019-05-07 19:38:46 +00:00
|
|
|
canvas->drawSimpleText(text, textLen, SkTextEncoding::kUTF8, 580, 125, font, paint);
|
2015-03-16 18:32:49 +00:00
|
|
|
}
|
|
|
|
|
2018-06-07 13:41:28 +00:00
|
|
|
// check perspective
|
|
|
|
{
|
2018-12-13 21:42:53 +00:00
|
|
|
font.setEdging(SkFont::Edging::kAntiAlias);
|
2018-06-07 13:41:28 +00:00
|
|
|
SkAutoCanvasRestore acr(canvas, true);
|
|
|
|
SkMatrix persp;
|
|
|
|
persp.setAll(0.9839f, 0, 0,
|
|
|
|
0.2246f, 0.6829f, 0,
|
|
|
|
0.0002352f, -0.0003844f, 1);
|
|
|
|
canvas->concat(persp);
|
|
|
|
canvas->translate(1100, -295);
|
2018-12-13 21:42:53 +00:00
|
|
|
font.setSize(37.5f);
|
2019-05-07 19:38:46 +00:00
|
|
|
canvas->drawSimpleText(text, textLen, SkTextEncoding::kUTF8, 0, 0, font, paint);
|
2018-06-07 13:41:28 +00:00
|
|
|
}
|
|
|
|
{
|
2018-12-13 21:42:53 +00:00
|
|
|
font.setSubpixel(false);
|
|
|
|
font.setEdging(SkFont::Edging::kAlias);
|
2018-06-07 13:41:28 +00:00
|
|
|
SkAutoCanvasRestore acr(canvas, true);
|
|
|
|
SkMatrix persp;
|
|
|
|
persp.setAll(0.9839f, 0, 0,
|
|
|
|
0.2246f, 0.6829f, 0,
|
|
|
|
0.0002352f, -0.0003844f, 1);
|
|
|
|
canvas->concat(persp);
|
|
|
|
canvas->translate(1075, -245);
|
2018-06-07 19:54:17 +00:00
|
|
|
canvas->scale(375, 375);
|
2018-12-13 21:42:53 +00:00
|
|
|
font.setSize(0.1f);
|
2019-05-07 19:38:46 +00:00
|
|
|
canvas->drawSimpleText(text, textLen, SkTextEncoding::kUTF8, 0, 0, font, paint);
|
2018-06-07 13:41:28 +00:00
|
|
|
}
|
|
|
|
|
2014-10-20 20:12:54 +00:00
|
|
|
// check color emoji
|
2015-07-21 19:27:36 +00:00
|
|
|
if (fEmojiTypeface) {
|
2018-12-13 21:42:53 +00:00
|
|
|
SkFont emoiFont;
|
|
|
|
emoiFont.setSubpixel(true);
|
|
|
|
emoiFont.setTypeface(fEmojiTypeface);
|
|
|
|
emoiFont.setSize(SkIntToScalar(19));
|
2019-05-07 19:38:46 +00:00
|
|
|
canvas->drawSimpleText(fEmojiText, strlen(fEmojiText), SkTextEncoding::kUTF8, 670, 90, emoiFont, paint);
|
2015-07-21 19:27:36 +00:00
|
|
|
}
|
2018-06-07 13:41:28 +00:00
|
|
|
|
2014-09-16 12:45:19 +00:00
|
|
|
// render offscreen buffer
|
2014-09-22 21:26:53 +00:00
|
|
|
if (surface) {
|
2014-10-07 14:59:45 +00:00
|
|
|
SkAutoCanvasRestore acr(inputCanvas, true);
|
|
|
|
// since we prepended this matrix already, we blit using identity
|
|
|
|
inputCanvas->resetMatrix();
|
2021-01-23 17:23:23 +00:00
|
|
|
inputCanvas->drawImage(surface->makeImageSnapshot().get(), 0, 0);
|
2014-09-22 21:26:53 +00:00
|
|
|
}
|
2014-09-16 12:45:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2016-05-12 17:09:30 +00:00
|
|
|
sk_sp<SkTypeface> fEmojiTypeface;
|
2015-07-21 19:27:36 +00:00
|
|
|
const char* fEmojiText;
|
2014-09-16 12:45:19 +00:00
|
|
|
|
2020-09-03 02:42:33 +00:00
|
|
|
using INHERITED = skiagm::GM;
|
2014-10-20 20:12:54 +00:00
|
|
|
};
|
2014-09-16 12:45:19 +00:00
|
|
|
|
2015-08-26 20:07:48 +00:00
|
|
|
DEF_GM(return new DFTextGM;)
|