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.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "gm/gm.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkBlendMode.h"
|
|
|
|
#include "include/core/SkBlurTypes.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkCanvas.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkColor.h"
|
|
|
|
#include "include/core/SkColorFilter.h"
|
|
|
|
#include "include/core/SkDrawLooper.h"
|
|
|
|
#include "include/core/SkFont.h"
|
|
|
|
#include "include/core/SkFontTypes.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkMaskFilter.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkMatrix.h"
|
|
|
|
#include "include/core/SkPaint.h"
|
|
|
|
#include "include/core/SkPath.h"
|
|
|
|
#include "include/core/SkPathEffect.h"
|
|
|
|
#include "include/core/SkPoint.h"
|
|
|
|
#include "include/core/SkRect.h"
|
|
|
|
#include "include/core/SkRefCnt.h"
|
|
|
|
#include "include/core/SkScalar.h"
|
|
|
|
#include "include/core/SkShader.h"
|
|
|
|
#include "include/core/SkSize.h"
|
|
|
|
#include "include/core/SkString.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkTextBlob.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkTileMode.h"
|
|
|
|
#include "include/core/SkTypeface.h"
|
|
|
|
#include "include/core/SkTypes.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/effects/Sk2DPathEffect.h"
|
|
|
|
#include "include/effects/SkColorMatrixFilter.h"
|
|
|
|
#include "include/effects/SkGradientShader.h"
|
|
|
|
#include "include/effects/SkLayerDrawLooper.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/private/SkTArray.h"
|
|
|
|
#include "include/private/SkTDArray.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "src/core/SkBlurMask.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "tools/ToolUtils.h"
|
|
|
|
|
|
|
|
#include <string.h>
|
2015-04-10 13:17:26 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
2019-03-20 16:12:10 +00:00
|
|
|
// Unlike the variant in ToolUtils, this version positions the glyphs on a diagonal
|
2018-11-21 20:10:08 +00:00
|
|
|
static void add_to_text_blob(SkTextBlobBuilder* builder, const char* text, const SkFont& font,
|
2015-04-10 13:17:26 +00:00
|
|
|
SkScalar x, SkScalar y) {
|
|
|
|
SkTDArray<uint16_t> glyphs;
|
|
|
|
|
|
|
|
size_t len = strlen(text);
|
2018-11-21 20:10:08 +00:00
|
|
|
glyphs.append(font.countText(text, len, kUTF8_SkTextEncoding));
|
|
|
|
font.textToGlyphs(text, len, kUTF8_SkTextEncoding, glyphs.begin(), glyphs.count());
|
2015-04-10 13:17:26 +00:00
|
|
|
|
2018-11-21 20:10:08 +00:00
|
|
|
const SkScalar advanceX = font.getSize() * 0.85f;
|
|
|
|
const SkScalar advanceY = font.getSize() * 1.5f;
|
2015-04-10 13:17:26 +00:00
|
|
|
|
|
|
|
SkTDArray<SkScalar> pos;
|
|
|
|
for (unsigned i = 0; i < len; ++i) {
|
|
|
|
*pos.append() = x + i * advanceX;
|
|
|
|
*pos.append() = y + i * (advanceY / len);
|
|
|
|
}
|
2018-11-21 20:10:08 +00:00
|
|
|
const SkTextBlobBuilder::RunBuffer& run = builder->allocRunPos(font, glyphs.count());
|
2015-04-10 13:17:26 +00:00
|
|
|
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) {
|
2018-03-14 17:01:17 +00:00
|
|
|
paint->setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle,
|
2016-04-04 17:02:58 +00:00
|
|
|
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),
|
2019-04-03 14:27:45 +00:00
|
|
|
SkTileMode::kClamp);
|
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
|
2018-11-21 20:10:08 +00:00
|
|
|
SkFont font;
|
|
|
|
font.setSize(32);
|
2015-04-10 13:17:26 +00:00
|
|
|
const char* text = "The quick brown fox jumps over the lazy dog";
|
2018-11-21 20:10:08 +00:00
|
|
|
font.setSubpixel(true);
|
|
|
|
font.setEdging(SkFont::Edging::kSubpixelAntiAlias);
|
2019-03-20 16:12:10 +00:00
|
|
|
font.setTypeface(ToolUtils::create_portable_typeface());
|
2018-11-21 20:10:08 +00:00
|
|
|
add_to_text_blob(&builder, text, font, 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 {
|
|
|
|
|
2018-08-16 14:17:03 +00:00
|
|
|
canvas->drawColor(SK_ColorGRAY);
|
2015-04-10 13:17:26 +00:00
|
|
|
|
|
|
|
SkPaint paint;
|
|
|
|
canvas->translate(10, 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;
|
2018-09-05 19:41:23 +00:00
|
|
|
SkTArray<sk_sp<SkDrawLooper>> 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
|
|
|
}
|