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.
|
|
|
|
*/
|
|
|
|
#include "gm.h"
|
2014-10-20 20:12:54 +00:00
|
|
|
#include "Resources.h"
|
|
|
|
#include "SkCanvas.h"
|
|
|
|
#include "SkStream.h"
|
2014-09-16 12:45:19 +00:00
|
|
|
#include "SkSurface.h"
|
|
|
|
#include "SkTypeface.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 {
|
2016-05-12 17:09:30 +00:00
|
|
|
fEmojiTypeface = sk_tool_utils::emoji_typeface();
|
2015-07-21 19:27:36 +00:00
|
|
|
fEmojiText = sk_tool_utils::emoji_sample_text();
|
2014-10-20 20:12:54 +00:00
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkString onShortName() override {
|
2015-07-21 19:27:36 +00:00
|
|
|
SkString name("dftext");
|
|
|
|
name.append(sk_tool_utils::platform_os_emoji());
|
|
|
|
return name;
|
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);
|
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
virtual 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
|
|
|
|
#if SK_SUPPORT_GPU
|
|
|
|
GrContext* ctx = inputCanvas->getGrContext();
|
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());
|
2016-07-26 18:36:05 +00:00
|
|
|
SkSurfaceProps props(SkSurfaceProps::kUseDeviceIndependentFonts_Flag,
|
2014-09-22 14:29:03 +00:00
|
|
|
SkSurfaceProps::kLegacyFontHost_InitType);
|
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
|
|
|
|
canvas->setMatrix(inputCanvas->getTotalMatrix());
|
2014-09-16 12:45:19 +00:00
|
|
|
#else
|
|
|
|
SkCanvas* canvas = inputCanvas;
|
|
|
|
#endif
|
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);
|
|
|
|
paint.setSubpixelText(true);
|
2015-06-22 13:46:56 +00:00
|
|
|
|
2016-05-31 18:42:36 +00:00
|
|
|
sk_tool_utils::set_portable_typeface(&paint, "serif");
|
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]);
|
|
|
|
paint.setTextSize(textSizes[i]);
|
|
|
|
canvas->drawText(text, textLen, 0, 0, paint);
|
2015-08-27 14:41:13 +00:00
|
|
|
y += paint.getFontMetrics(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) {
|
|
|
|
paint.setTextSize(SkIntToScalar(ps));
|
|
|
|
canvas->drawText(text, textLen, rotX, rotY, paint);
|
2015-08-27 14:41:13 +00:00
|
|
|
rotY += paint.getFontMetrics(nullptr);
|
2014-09-16 12:45:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// check scaling down
|
|
|
|
paint.setLCDRenderText(true);
|
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);
|
|
|
|
paint.setTextSize(textSizes[i]);
|
|
|
|
canvas->drawText(text, textLen, 0, 0, paint);
|
2015-08-27 14:41:13 +00:00
|
|
|
y += paint.getFontMetrics(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);
|
|
|
|
|
2014-12-13 00:41:46 +00:00
|
|
|
SkAutoTArray<SkPoint> pos(SkToInt(textLen));
|
|
|
|
SkAutoTArray<SkScalar> widths(SkToInt(textLen));
|
2014-09-22 21:26:53 +00:00
|
|
|
paint.setTextSize(textSizes[0]);
|
|
|
|
|
|
|
|
paint.getTextWidths(text, textLen, &widths[0]);
|
|
|
|
|
|
|
|
SkScalar x = SkIntToScalar(340);
|
|
|
|
SkScalar y = SkIntToScalar(75);
|
|
|
|
for (unsigned int i = 0; i < textLen; ++i) {
|
|
|
|
pos[i].set(x, y);
|
|
|
|
x += widths[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
canvas->drawPosText(text, textLen, &pos[0], paint);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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);
|
2014-10-06 19:50:54 +00:00
|
|
|
paint.setTextSize(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]);
|
|
|
|
|
|
|
|
canvas->drawText(text, textLen, x, y, paint);
|
2015-08-27 14:41:13 +00:00
|
|
|
y += paint.getFontMetrics(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);
|
2014-10-06 19:50:54 +00:00
|
|
|
paint.setTextSize(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]);
|
|
|
|
|
|
|
|
canvas->drawText(text, textLen, x, y, paint);
|
2015-08-27 14:41:13 +00:00
|
|
|
y += paint.getFontMetrics(nullptr);
|
2014-09-16 12:45:19 +00:00
|
|
|
}
|
|
|
|
|
2015-03-16 18:32:49 +00:00
|
|
|
// check skew
|
|
|
|
{
|
|
|
|
paint.setLCDRenderText(false);
|
|
|
|
SkAutoCanvasRestore acr(canvas, true);
|
|
|
|
canvas->skew(0.0f, 0.151515f);
|
2015-03-16 19:58:43 +00:00
|
|
|
paint.setTextSize(SkIntToScalar(32));
|
2015-03-16 18:32:49 +00:00
|
|
|
canvas->drawText(text, textLen, 745, 70, paint);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
paint.setLCDRenderText(true);
|
|
|
|
SkAutoCanvasRestore acr(canvas, true);
|
|
|
|
canvas->skew(0.5f, 0.0f);
|
2015-03-16 19:58:43 +00:00
|
|
|
paint.setTextSize(SkIntToScalar(32));
|
2015-07-23 21:07:58 +00:00
|
|
|
canvas->drawText(text, textLen, 580, 125, paint);
|
2015-03-16 18:32:49 +00:00
|
|
|
}
|
|
|
|
|
2014-10-20 20:12:54 +00:00
|
|
|
// check color emoji
|
2015-07-21 19:27:36 +00:00
|
|
|
if (fEmojiTypeface) {
|
|
|
|
paint.setTypeface(fEmojiTypeface);
|
|
|
|
paint.setTextSize(SkIntToScalar(19));
|
2015-07-23 21:07:58 +00:00
|
|
|
canvas->drawText(fEmojiText, strlen(fEmojiText), 670, 90, paint);
|
2015-07-21 19:27:36 +00:00
|
|
|
}
|
2014-09-16 12:45:19 +00:00
|
|
|
#if SK_SUPPORT_GPU
|
|
|
|
// 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();
|
2016-03-17 17:51:11 +00:00
|
|
|
inputCanvas->drawImage(surface->makeImageSnapshot().get(), 0, 0, nullptr);
|
2014-09-22 21:26:53 +00:00
|
|
|
}
|
2014-09-16 12:45:19 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2014-10-20 20:12:54 +00:00
|
|
|
typedef skiagm::GM INHERITED;
|
|
|
|
};
|
2014-09-16 12:45:19 +00:00
|
|
|
|
2015-08-26 20:07:48 +00:00
|
|
|
DEF_GM(return new DFTextGM;)
|