2015-04-10 13:17: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-10 13:17:26 +00:00
|
|
|
|
|
|
|
#include "Sk2DPathEffect.h"
|
|
|
|
#include "SkBlurMask.h"
|
|
|
|
#include "SkBlurMaskFilter.h"
|
2016-01-11 20:34:04 +00:00
|
|
|
#include "SkColorMatrixFilter.h"
|
2015-04-10 13:17:26 +00:00
|
|
|
#include "SkCanvas.h"
|
|
|
|
#include "SkGradientShader.h"
|
|
|
|
#include "SkGraphics.h"
|
|
|
|
#include "SkLayerDrawLooper.h"
|
|
|
|
#include "SkRandom.h"
|
|
|
|
#include "SkTextBlob.h"
|
|
|
|
|
|
|
|
namespace skiagm {
|
|
|
|
|
2016-09-01 18:24:54 +00:00
|
|
|
constexpr int kWidth = 1250;
|
|
|
|
constexpr int kHeight = 700;
|
2015-04-10 13:17:26 +00:00
|
|
|
|
2015-04-14 19:17:27 +00:00
|
|
|
// Unlike the variant in sk_tool_utils, this version positions the glyphs on a diagonal
|
2015-04-10 13:17:26 +00:00
|
|
|
static void add_to_text_blob(SkTextBlobBuilder* builder, const char* text, const SkPaint& origPaint,
|
|
|
|
SkScalar x, SkScalar y) {
|
|
|
|
SkPaint paint(origPaint);
|
|
|
|
SkTDArray<uint16_t> glyphs;
|
|
|
|
|
|
|
|
size_t len = strlen(text);
|
2015-08-27 14:41:13 +00:00
|
|
|
glyphs.append(paint.textToGlyphs(text, len, nullptr));
|
2015-04-10 13:17:26 +00:00
|
|
|
paint.textToGlyphs(text, len, glyphs.begin());
|
|
|
|
|
|
|
|
const SkScalar advanceX = paint.getTextSize() * 0.85f;
|
|
|
|
const SkScalar advanceY = paint.getTextSize() * 1.5f;
|
|
|
|
|
|
|
|
SkTDArray<SkScalar> pos;
|
|
|
|
for (unsigned i = 0; i < len; ++i) {
|
|
|
|
*pos.append() = x + i * advanceX;
|
|
|
|
*pos.append() = y + i * (advanceY / len);
|
|
|
|
}
|
|
|
|
|
|
|
|
paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
|
|
|
|
const SkTextBlobBuilder::RunBuffer& run = builder->allocRunPos(paint, glyphs.count());
|
|
|
|
memcpy(run.glyphs, glyphs.begin(), glyphs.count() * sizeof(uint16_t));
|
|
|
|
memcpy(run.pos, pos.begin(), len * sizeof(SkScalar) * 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef void (*LooperProc)(SkPaint*);
|
|
|
|
|
|
|
|
struct LooperSettings {
|
2016-10-06 00:33:02 +00:00
|
|
|
SkBlendMode fMode;
|
2015-04-10 13:17:26 +00:00
|
|
|
SkColor fColor;
|
|
|
|
SkPaint::Style fStyle;
|
|
|
|
SkScalar fWidth;
|
|
|
|
SkScalar fOffset;
|
|
|
|
SkScalar fSkewX;
|
|
|
|
bool fEffect;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void mask_filter(SkPaint* paint) {
|
2016-04-04 17:02:58 +00:00
|
|
|
paint->setMaskFilter(SkBlurMaskFilter::Make(kNormal_SkBlurStyle,
|
|
|
|
SkBlurMask::ConvertRadiusToSigma(3.f)));
|
2015-04-10 13:17:26 +00:00
|
|
|
}
|
|
|
|
|
2016-03-18 18:22:57 +00:00
|
|
|
static sk_sp<SkPathEffect> make_tile_effect() {
|
2015-04-10 13:17:26 +00:00
|
|
|
SkMatrix m;
|
|
|
|
m.setScale(1.f, 1.f);
|
|
|
|
|
|
|
|
SkPath path;
|
|
|
|
path.addCircle(0, 0, SkIntToScalar(5));
|
|
|
|
|
2016-03-18 18:22:57 +00:00
|
|
|
return SkPath2DPathEffect::Make(m, path);
|
2015-04-10 13:17:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void path_effect(SkPaint* paint) {
|
2016-03-18 18:22:57 +00:00
|
|
|
paint->setPathEffect(make_tile_effect());
|
2015-04-10 13:17:26 +00:00
|
|
|
}
|
|
|
|
|
2016-03-13 21:13:58 +00:00
|
|
|
static sk_sp<SkShader> make_shader(const SkRect& bounds) {
|
2015-04-10 13:17:26 +00:00
|
|
|
const SkPoint pts[] = {
|
|
|
|
{ bounds.left(), bounds.top() },
|
|
|
|
{ bounds.right(), bounds.bottom() },
|
|
|
|
};
|
|
|
|
const SkColor colors[] = {
|
|
|
|
SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorBLACK,
|
|
|
|
SK_ColorCYAN, SK_ColorMAGENTA, SK_ColorYELLOW,
|
|
|
|
};
|
2016-03-13 21:13:58 +00:00
|
|
|
return SkGradientShader::MakeLinear(pts, colors, nullptr, SK_ARRAY_COUNT(colors),
|
|
|
|
SkShader::kClamp_TileMode);
|
2015-04-10 13:17:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void color_filter(SkPaint* paint) {
|
|
|
|
SkRect r;
|
|
|
|
r.setWH(SkIntToScalar(kWidth), 50);
|
2016-03-13 21:13:58 +00:00
|
|
|
paint->setShader(make_shader(r));
|
2016-03-22 17:17:23 +00:00
|
|
|
paint->setColorFilter(SkColorMatrixFilter::MakeLightingFilter(0xF0F0F0, 0));
|
2015-04-10 13:17:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void kitchen_sink(SkPaint* paint) {
|
|
|
|
color_filter(paint);
|
|
|
|
path_effect(paint);
|
|
|
|
mask_filter(paint);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-03-21 20:25:16 +00:00
|
|
|
static sk_sp<SkDrawLooper> setupLooper(SkLayerDrawLooper::BitFlags bits,
|
|
|
|
LooperProc proc,
|
|
|
|
const LooperSettings settings[],
|
|
|
|
size_t size) {
|
2015-04-10 13:17:26 +00:00
|
|
|
SkLayerDrawLooper::Builder looperBuilder;
|
|
|
|
|
|
|
|
SkLayerDrawLooper::LayerInfo info;
|
|
|
|
info.fPaintBits = bits;
|
|
|
|
|
2016-11-03 18:45:31 +00:00
|
|
|
info.fColorMode = SkBlendMode::kSrc;
|
2015-04-10 13:17:26 +00:00
|
|
|
|
|
|
|
for (size_t i = 0; i < size; i++) {
|
|
|
|
info.fOffset.set(settings[i].fOffset, settings[i].fOffset);
|
|
|
|
SkPaint* paint = looperBuilder.addLayer(info);
|
2016-10-06 00:33:02 +00:00
|
|
|
paint->setBlendMode(settings[i].fMode);
|
2015-04-10 13:17:26 +00:00
|
|
|
paint->setColor(settings[i].fColor);
|
|
|
|
paint->setStyle(settings[i].fStyle);
|
|
|
|
paint->setStrokeWidth(settings[i].fWidth);
|
|
|
|
if (settings[i].fEffect) {
|
|
|
|
(*proc)(paint);
|
|
|
|
}
|
|
|
|
}
|
2016-03-21 20:25:16 +00:00
|
|
|
return looperBuilder.detach();
|
2015-04-10 13:17:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class TextBlobLooperGM : public GM {
|
|
|
|
public:
|
|
|
|
TextBlobLooperGM() {}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
void onOnceBeforeDraw() override {
|
|
|
|
SkTextBlobBuilder builder;
|
|
|
|
|
|
|
|
// LCD
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setTextSize(32);
|
|
|
|
const char* text = "The quick brown fox jumps over the lazy dog";
|
|
|
|
paint.setSubpixelText(true);
|
|
|
|
paint.setLCDRenderText(true);
|
|
|
|
paint.setAntiAlias(true);
|
2015-07-28 17:37:53 +00:00
|
|
|
sk_tool_utils::set_portable_typeface(&paint);
|
2015-04-10 13:17:26 +00:00
|
|
|
add_to_text_blob(&builder, text, paint, 0, 0);
|
2016-09-13 17:00:23 +00:00
|
|
|
fBlob = builder.make();
|
2015-04-10 13:17:26 +00:00
|
|
|
|
|
|
|
// create a looper which sandwhiches an effect in two normal draws
|
|
|
|
LooperSettings looperSandwhich[] = {
|
2016-10-06 00:33:02 +00:00
|
|
|
{ SkBlendMode::kSrc, SK_ColorMAGENTA, SkPaint::kFill_Style, 0, 0, 0, false },
|
|
|
|
{ SkBlendMode::kSrcOver, 0x88000000, SkPaint::kFill_Style, 0, 10.f, 0, true },
|
|
|
|
{ SkBlendMode::kSrcOver, 0x50FF00FF, SkPaint::kFill_Style, 0, 20.f, 0, false },
|
2015-04-10 13:17:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
LooperSettings compound[] = {
|
2016-10-06 00:33:02 +00:00
|
|
|
{ SkBlendMode::kSrc, SK_ColorWHITE, SkPaint::kStroke_Style, 1.f * 3/4, 0, 0, false },
|
|
|
|
{ SkBlendMode::kSrc, SK_ColorRED, SkPaint::kStroke_Style, 4.f, 0, 0, false },
|
|
|
|
{ SkBlendMode::kSrc, SK_ColorBLUE, SkPaint::kFill_Style, 0, 0, 0, false },
|
|
|
|
{ SkBlendMode::kSrcOver, 0x88000000, SkPaint::kFill_Style, 0, 10.f, 0, true }
|
2015-04-10 13:17:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
LooperSettings xfermode[] = {
|
2016-10-06 00:33:02 +00:00
|
|
|
{ SkBlendMode::kDifference, SK_ColorWHITE, SkPaint::kFill_Style, 0, 0, 0, false },
|
|
|
|
{ SkBlendMode::kSrcOver, 0xFF000000, SkPaint::kFill_Style, 0, 1.f, 0, true },
|
|
|
|
{ SkBlendMode::kSrcOver, 0x50FF00FF, SkPaint::kFill_Style, 0, 2.f, 0, false },
|
2015-04-10 13:17:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// NOTE, this should be ignored by textblobs
|
|
|
|
LooperSettings skew[] = {
|
2016-10-06 00:33:02 +00:00
|
|
|
{ SkBlendMode::kSrc, SK_ColorRED, SkPaint::kFill_Style, 0, 0, -1.f, false },
|
|
|
|
{ SkBlendMode::kSrc, SK_ColorGREEN, SkPaint::kFill_Style, 0, 10.f, -1.f, false },
|
|
|
|
{ SkBlendMode::kSrc, SK_ColorBLUE, SkPaint::kFill_Style, 0, 20.f, -1.f, false },
|
2015-04-10 13:17:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
LooperSettings kitchenSink[] = {
|
2016-10-06 00:33:02 +00:00
|
|
|
{ SkBlendMode::kSrc, SK_ColorWHITE, SkPaint::kStroke_Style, 1.f * 3/4, 0, 0, false },
|
|
|
|
{ SkBlendMode::kSrc, SK_ColorBLACK, SkPaint::kFill_Style, 0, 0, 0, false },
|
|
|
|
{ SkBlendMode::kDifference, SK_ColorWHITE, SkPaint::kFill_Style, 1.f, 10.f, 0, false },
|
|
|
|
{ SkBlendMode::kSrc, SK_ColorWHITE, SkPaint::kFill_Style, 0, 10.f, 0, true },
|
|
|
|
{ SkBlendMode::kSrcOver, 0x50FF00FF, SkPaint::kFill_Style, 0, 20.f, 0, false },
|
2015-04-10 13:17:26 +00:00
|
|
|
};
|
|
|
|
|
2016-03-21 20:25:16 +00:00
|
|
|
fLoopers.push_back(setupLooper(SkLayerDrawLooper::kMaskFilter_Bit |
|
|
|
|
SkLayerDrawLooper::kXfermode_Bit |
|
|
|
|
SkLayerDrawLooper::kStyle_Bit, &mask_filter,
|
|
|
|
compound, SK_ARRAY_COUNT(compound)));
|
|
|
|
fLoopers.push_back(setupLooper(SkLayerDrawLooper::kPathEffect_Bit |
|
|
|
|
SkLayerDrawLooper::kXfermode_Bit, &path_effect,
|
|
|
|
looperSandwhich, SK_ARRAY_COUNT(looperSandwhich)));
|
|
|
|
fLoopers.push_back(setupLooper(SkLayerDrawLooper::kShader_Bit |
|
|
|
|
SkLayerDrawLooper::kColorFilter_Bit |
|
|
|
|
SkLayerDrawLooper::kXfermode_Bit, &color_filter,
|
|
|
|
looperSandwhich, SK_ARRAY_COUNT(looperSandwhich)));
|
|
|
|
fLoopers.push_back(setupLooper(SkLayerDrawLooper::kShader_Bit |
|
|
|
|
SkLayerDrawLooper::kColorFilter_Bit |
|
|
|
|
SkLayerDrawLooper::kXfermode_Bit, &color_filter,
|
|
|
|
xfermode, SK_ARRAY_COUNT(xfermode)));
|
|
|
|
fLoopers.push_back(setupLooper(0, nullptr, skew, SK_ARRAY_COUNT(skew)));
|
|
|
|
fLoopers.push_back(setupLooper(SkLayerDrawLooper::kMaskFilter_Bit |
|
|
|
|
SkLayerDrawLooper::kShader_Bit |
|
|
|
|
SkLayerDrawLooper::kColorFilter_Bit |
|
|
|
|
SkLayerDrawLooper::kPathEffect_Bit |
|
|
|
|
SkLayerDrawLooper::kStyle_Bit |
|
|
|
|
SkLayerDrawLooper::kXfermode_Bit, &kitchen_sink,
|
|
|
|
kitchenSink, SK_ARRAY_COUNT(kitchenSink)));
|
2015-04-10 13:17:26 +00:00
|
|
|
|
|
|
|
// Test we respect overrides
|
2016-03-21 20:25:16 +00:00
|
|
|
fLoopers.push_back(setupLooper(0, &kitchen_sink,
|
|
|
|
kitchenSink, SK_ARRAY_COUNT(kitchenSink)));
|
2015-04-10 13:17:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SkString onShortName() override {
|
|
|
|
return SkString("textbloblooper");
|
|
|
|
}
|
|
|
|
|
|
|
|
SkISize onISize() override {
|
|
|
|
return SkISize::Make(kWidth, kHeight);
|
|
|
|
}
|
|
|
|
|
|
|
|
void onDraw(SkCanvas* canvas) override {
|
|
|
|
|
2015-07-28 17:37:53 +00:00
|
|
|
canvas->drawColor(sk_tool_utils::color_to_565(SK_ColorGRAY));
|
2015-04-10 13:17:26 +00:00
|
|
|
|
|
|
|
SkPaint paint;
|
|
|
|
canvas->translate(10, 40);
|
|
|
|
|
|
|
|
paint.setTextSize(40);
|
|
|
|
|
|
|
|
SkRect bounds = fBlob->bounds();
|
|
|
|
|
|
|
|
int y = 0;
|
|
|
|
for (int looper = 0; looper < fLoopers.count(); looper++) {
|
|
|
|
paint.setLooper(fLoopers[looper]);
|
|
|
|
canvas->save();
|
|
|
|
canvas->translate(0, SkIntToScalar(y));
|
|
|
|
canvas->drawTextBlob(fBlob, 0, 0, paint);
|
|
|
|
canvas->restore();
|
|
|
|
y += SkScalarFloorToInt(bounds.height());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2016-09-13 17:00:23 +00:00
|
|
|
sk_sp<SkTextBlob> fBlob;
|
2016-03-21 20:25:16 +00:00
|
|
|
SkTArray<sk_sp<SkDrawLooper>, true> fLoopers;
|
2015-04-10 13:17:26 +00:00
|
|
|
|
|
|
|
typedef GM INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2015-08-26 20:07:48 +00:00
|
|
|
DEF_GM(return new TextBlobLooperGM;)
|
2015-04-10 13:17:26 +00:00
|
|
|
}
|