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.
|
|
|
|
*/
|
|
|
|
|
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"
|
|
|
|
#include "include/core/SkFontTypes.h"
|
|
|
|
#include "include/core/SkPaint.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/SkTextBlob.h"
|
|
|
|
#include "include/core/SkTypeface.h"
|
|
|
|
#include "tools/Resources.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "tools/ToolUtils.h"
|
|
|
|
|
|
|
|
#include <string.h>
|
2015-04-03 15:07:26 +00:00
|
|
|
|
|
|
|
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);
|
2019-02-15 21:13:57 +00:00
|
|
|
paint.setAlphaf(0.125f);
|
2015-04-03 15:07:26 +00:00
|
|
|
canvas->drawTextBlob(blob, 0, 0, paint);
|
|
|
|
canvas->clipRect(clipRect);
|
2019-02-15 21:13:57 +00:00
|
|
|
paint.setAlphaf(1.0f);
|
2015-04-03 15:07:26 +00:00
|
|
|
canvas->drawTextBlob(blob, 0, 0, paint);
|
|
|
|
canvas->restore();
|
|
|
|
}
|
|
|
|
|
|
|
|
class MixedTextBlobsGM : public GM {
|
|
|
|
public:
|
|
|
|
MixedTextBlobsGM() { }
|
|
|
|
|
|
|
|
protected:
|
|
|
|
void onOnceBeforeDraw() override {
|
2019-03-20 16:12:10 +00:00
|
|
|
fEmojiTypeface = ToolUtils::planet_typeface();
|
2019-03-08 23:05:47 +00:00
|
|
|
fEmojiText = "♁♃";
|
2017-12-08 19:25:14 +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
|
2019-03-20 16:12:10 +00:00
|
|
|
SkFont font(ToolUtils::create_portable_typeface(), 385);
|
2018-12-22 03:22:31 +00:00
|
|
|
font.setEdging(SkFont::Edging::kAlias);
|
2015-04-03 15:07:26 +00:00
|
|
|
const char* text = "O";
|
|
|
|
|
|
|
|
SkRect bounds;
|
2019-05-07 19:38:46 +00:00
|
|
|
font.measureText(text, strlen(text), SkTextEncoding::kUTF8, &bounds);
|
2015-04-03 15:07:26 +00:00
|
|
|
|
|
|
|
SkScalar yOffset = bounds.height();
|
2019-03-20 16:12:10 +00:00
|
|
|
ToolUtils::add_to_text_blob(&builder, text, font, 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
|
2018-12-22 03:22:31 +00:00
|
|
|
font.setSize(32);
|
|
|
|
font.setEdging(SkFont::Edging::kSubpixelAntiAlias);
|
|
|
|
font.setSubpixel(true);
|
2015-04-03 15:07:26 +00:00
|
|
|
text = "LCD!!!!!";
|
2019-05-07 19:38:46 +00:00
|
|
|
font.measureText(text, strlen(text), SkTextEncoding::kUTF8, &bounds);
|
2019-03-20 16:12:10 +00:00
|
|
|
ToolUtils::add_to_text_blob(&builder,
|
|
|
|
text,
|
|
|
|
font,
|
|
|
|
xOffset - bounds.width() * 0.25f,
|
|
|
|
yOffset - bounds.height() * 0.5f);
|
2015-04-03 15:07:26 +00:00
|
|
|
|
2019-03-08 23:05:47 +00:00
|
|
|
// color emoji font with large glyph
|
2015-07-21 19:27:36 +00:00
|
|
|
if (fEmojiTypeface) {
|
2018-12-22 03:22:31 +00:00
|
|
|
font.setEdging(SkFont::Edging::kAlias);
|
|
|
|
font.setSubpixel(false);
|
|
|
|
font.setTypeface(fEmojiTypeface);
|
2019-05-07 19:38:46 +00:00
|
|
|
font.measureText(fEmojiText, strlen(fEmojiText), SkTextEncoding::kUTF8, &bounds);
|
2019-03-20 16:12:10 +00:00
|
|
|
ToolUtils::add_to_text_blob(&builder, fEmojiText, font, xOffset, yOffset);
|
2015-07-21 19:27:36 +00:00
|
|
|
}
|
2015-04-03 15:07:26 +00:00
|
|
|
|
2019-03-08 23:05:47 +00:00
|
|
|
// outline font with large glyph
|
2018-12-22 03:22:31 +00:00
|
|
|
font.setSize(12);
|
2015-04-03 15:07:26 +00:00
|
|
|
text = "aA";
|
2018-12-22 03:22:31 +00:00
|
|
|
font.setTypeface(fReallyBigATypeface);
|
2019-03-20 16:12:10 +00:00
|
|
|
ToolUtils::add_to_text_blob(&builder, text, font, corruptedAx, corruptedAy);
|
2016-09-13 17:00:23 +00:00
|
|
|
fBlob = builder.make();
|
2015-04-03 15:07:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SkString onShortName() override {
|
2019-03-08 17:11:55 +00:00
|
|
|
return SkString("mixedtextblobs");
|
2015-04-03 15:07:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SkISize onISize() override {
|
|
|
|
return SkISize::Make(kWidth, kHeight);
|
|
|
|
}
|
|
|
|
|
|
|
|
void onDraw(SkCanvas* canvas) override {
|
|
|
|
|
2018-08-16 14:17:03 +00:00
|
|
|
canvas->drawColor(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);
|
|
|
|
|
|
|
|
// 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
|
|
|
|
2020-09-03 02:42:33 +00:00
|
|
|
using INHERITED = GM;
|
2015-04-03 15:07:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2015-08-26 20:07:48 +00:00
|
|
|
DEF_GM(return new MixedTextBlobsGM;)
|
2020-08-06 18:11:56 +00:00
|
|
|
} // namespace skiagm
|