2015-04-03 15:07:26 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2013 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"
|
2017-03-22 17:47:51 +00:00
|
|
|
#include "sk_tool_utils.h"
|
2015-04-03 15:07:26 +00:00
|
|
|
|
|
|
|
#include "Resources.h"
|
|
|
|
#include "SkCanvas.h"
|
|
|
|
#include "SkGradientShader.h"
|
|
|
|
#include "SkStream.h"
|
|
|
|
#include "SkTextBlob.h"
|
|
|
|
#include "SkTypeface.h"
|
|
|
|
|
|
|
|
namespace skiagm {
|
|
|
|
|
|
|
|
static void draw_blob(SkCanvas* canvas, const SkTextBlob* blob, const SkPaint& skPaint,
|
|
|
|
const SkRect& clipRect) {
|
|
|
|
SkPaint clipHairline;
|
|
|
|
clipHairline.setColor(SK_ColorWHITE);
|
|
|
|
clipHairline.setStyle(SkPaint::kStroke_Style);
|
|
|
|
|
|
|
|
SkPaint paint(skPaint);
|
|
|
|
canvas->save();
|
|
|
|
canvas->drawRect(clipRect, clipHairline);
|
|
|
|
paint.setAlpha(0x20);
|
|
|
|
canvas->drawTextBlob(blob, 0, 0, paint);
|
|
|
|
canvas->clipRect(clipRect);
|
|
|
|
paint.setAlpha(0xFF);
|
|
|
|
canvas->drawTextBlob(blob, 0, 0, paint);
|
|
|
|
canvas->restore();
|
|
|
|
}
|
|
|
|
|
|
|
|
class MixedTextBlobsGM : public GM {
|
|
|
|
public:
|
|
|
|
MixedTextBlobsGM() { }
|
|
|
|
|
|
|
|
protected:
|
|
|
|
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();
|
2016-05-12 17:09:30 +00:00
|
|
|
fReallyBigATypeface = MakeResourceAsTypeface("/fonts/ReallyBigA.ttf");
|
2015-04-03 15:07:26 +00:00
|
|
|
|
|
|
|
SkTextBlobBuilder builder;
|
|
|
|
|
|
|
|
// make textblob
|
|
|
|
// Text so large we draw as paths
|
|
|
|
SkPaint paint;
|
2015-06-22 15:08:09 +00:00
|
|
|
paint.setTextSize(385);
|
2015-04-03 15:07:26 +00:00
|
|
|
const char* text = "O";
|
2015-07-24 19:09:25 +00:00
|
|
|
sk_tool_utils::set_portable_typeface(&paint);
|
2015-04-03 15:07:26 +00:00
|
|
|
|
|
|
|
SkRect bounds;
|
|
|
|
paint.measureText(text, strlen(text), &bounds);
|
|
|
|
|
|
|
|
SkScalar yOffset = bounds.height();
|
2015-04-14 19:17:27 +00:00
|
|
|
sk_tool_utils::add_to_text_blob(&builder, text, paint, 10, yOffset);
|
2015-04-03 15:07:26 +00:00
|
|
|
SkScalar corruptedAx = bounds.width();
|
|
|
|
SkScalar corruptedAy = yOffset;
|
|
|
|
|
|
|
|
const SkScalar boundsHalfWidth = bounds.width() * SK_ScalarHalf;
|
|
|
|
const SkScalar boundsHalfHeight = bounds.height() * SK_ScalarHalf;
|
|
|
|
|
|
|
|
SkScalar xOffset = boundsHalfWidth;
|
|
|
|
yOffset = boundsHalfHeight;
|
|
|
|
|
|
|
|
// LCD
|
|
|
|
paint.setTextSize(32);
|
|
|
|
text = "LCD!!!!!";
|
2015-07-24 20:16:24 +00:00
|
|
|
paint.setAntiAlias(true);
|
2015-04-03 15:07:26 +00:00
|
|
|
paint.setSubpixelText(true);
|
|
|
|
paint.setLCDRenderText(true);
|
|
|
|
paint.measureText(text, strlen(text), &bounds);
|
2015-04-14 19:17:27 +00:00
|
|
|
sk_tool_utils::add_to_text_blob(&builder, text, paint, xOffset - bounds.width() * 0.25f,
|
|
|
|
yOffset - bounds.height() * 0.5f);
|
2015-04-03 15:07:26 +00:00
|
|
|
yOffset += bounds.height();
|
|
|
|
|
|
|
|
// color emoji
|
2015-07-21 19:27:36 +00:00
|
|
|
if (fEmojiTypeface) {
|
2015-07-24 20:16:24 +00:00
|
|
|
paint.setAntiAlias(false);
|
2015-07-21 19:27:36 +00:00
|
|
|
paint.setSubpixelText(false);
|
|
|
|
paint.setLCDRenderText(false);
|
|
|
|
paint.setTypeface(fEmojiTypeface);
|
|
|
|
text = fEmojiText;
|
|
|
|
paint.measureText(text, strlen(text), &bounds);
|
|
|
|
sk_tool_utils::add_to_text_blob(&builder, text, paint, xOffset - bounds.width() * 0.3f,
|
|
|
|
yOffset);
|
|
|
|
}
|
2015-04-03 15:07:26 +00:00
|
|
|
|
|
|
|
// Corrupted font
|
|
|
|
paint.setTextSize(12);
|
|
|
|
text = "aA";
|
|
|
|
paint.setTypeface(fReallyBigATypeface);
|
2015-04-14 19:17:27 +00:00
|
|
|
sk_tool_utils::add_to_text_blob(&builder, text, paint, corruptedAx, corruptedAy);
|
2016-09-13 17:00:23 +00:00
|
|
|
fBlob = builder.make();
|
2015-04-03 15:07:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SkString onShortName() override {
|
2015-07-21 19:27:36 +00:00
|
|
|
SkString name("mixedtextblobs");
|
|
|
|
name.append(sk_tool_utils::platform_os_emoji());
|
|
|
|
return name;
|
2015-04-03 15:07:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SkISize onISize() override {
|
|
|
|
return SkISize::Make(kWidth, kHeight);
|
|
|
|
}
|
|
|
|
|
|
|
|
void onDraw(SkCanvas* canvas) override {
|
|
|
|
|
2015-07-21 19:27:36 +00:00
|
|
|
canvas->drawColor(sk_tool_utils::color_to_565(SK_ColorGRAY));
|
2015-04-03 15:07:26 +00:00
|
|
|
|
|
|
|
SkPaint paint;
|
|
|
|
|
|
|
|
// setup work needed to draw text with different clips
|
|
|
|
paint.setColor(SK_ColorBLACK);
|
|
|
|
canvas->translate(10, 40);
|
|
|
|
|
|
|
|
paint.setTextSize(40);
|
|
|
|
|
|
|
|
// compute the bounds of the text and setup some clips
|
|
|
|
SkRect bounds = fBlob->bounds();
|
|
|
|
|
|
|
|
const SkScalar boundsHalfWidth = bounds.width() * SK_ScalarHalf;
|
|
|
|
const SkScalar boundsHalfHeight = bounds.height() * SK_ScalarHalf;
|
|
|
|
const SkScalar boundsQuarterWidth = boundsHalfWidth * SK_ScalarHalf;
|
|
|
|
const SkScalar boundsQuarterHeight = boundsHalfHeight * SK_ScalarHalf;
|
|
|
|
|
|
|
|
SkRect upperLeftClip = SkRect::MakeXYWH(bounds.left(), bounds.top(),
|
|
|
|
boundsHalfWidth, boundsHalfHeight);
|
|
|
|
SkRect lowerRightClip = SkRect::MakeXYWH(bounds.centerX(), bounds.centerY(),
|
|
|
|
boundsHalfWidth, boundsHalfHeight);
|
|
|
|
SkRect interiorClip = bounds;
|
|
|
|
interiorClip.inset(boundsQuarterWidth, boundsQuarterHeight);
|
|
|
|
|
|
|
|
const SkRect clipRects[] = { bounds, upperLeftClip, lowerRightClip, interiorClip};
|
|
|
|
|
|
|
|
size_t count = sizeof(clipRects) / sizeof(SkRect);
|
|
|
|
for (size_t x = 0; x < count; ++x) {
|
2016-09-13 17:00:23 +00:00
|
|
|
draw_blob(canvas, fBlob.get(), paint, clipRects[x]);
|
2015-04-03 15:07:26 +00:00
|
|
|
if (x == (count >> 1) - 1) {
|
|
|
|
canvas->translate(SkScalarFloorToScalar(bounds.width() + SkIntToScalar(25)),
|
|
|
|
-(x * SkScalarFloorToScalar(bounds.height() +
|
|
|
|
SkIntToScalar(25))));
|
|
|
|
} else {
|
|
|
|
canvas->translate(0, SkScalarFloorToScalar(bounds.height() + SkIntToScalar(25)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2016-05-12 17:09:30 +00:00
|
|
|
sk_sp<SkTypeface> fEmojiTypeface;
|
|
|
|
sk_sp<SkTypeface> fReallyBigATypeface;
|
2015-04-03 15:07:26 +00:00
|
|
|
const char* fEmojiText;
|
2016-09-13 17:00:23 +00:00
|
|
|
sk_sp<SkTextBlob> fBlob;
|
2015-04-03 15:07:26 +00:00
|
|
|
|
2016-09-01 18:24:54 +00:00
|
|
|
static constexpr int kWidth = 1250;
|
|
|
|
static constexpr int kHeight = 700;
|
2015-04-03 15:07:26 +00:00
|
|
|
|
|
|
|
typedef GM INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2015-08-26 20:07:48 +00:00
|
|
|
DEF_GM(return new MixedTextBlobsGM;)
|
2015-04-03 15:07:26 +00:00
|
|
|
}
|