2011-07-28 14:26:00 +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.
|
|
|
|
*/
|
2009-10-20 03:26:17 +00:00
|
|
|
#include "SampleCode.h"
|
|
|
|
#include "SkView.h"
|
|
|
|
#include "SkCanvas.h"
|
2014-01-30 18:58:24 +00:00
|
|
|
#include "SkReadBuffer.h"
|
|
|
|
#include "SkWriteBuffer.h"
|
2009-10-20 03:26:17 +00:00
|
|
|
#include "SkGradientShader.h"
|
|
|
|
#include "SkPath.h"
|
|
|
|
#include "SkRegion.h"
|
|
|
|
#include "SkShader.h"
|
|
|
|
#include "SkUtils.h"
|
|
|
|
#include "SkColorPriv.h"
|
|
|
|
#include "SkColorFilter.h"
|
2015-09-15 19:53:07 +00:00
|
|
|
#include "SkStrokeRec.h"
|
2009-10-20 03:26:17 +00:00
|
|
|
#include "SkTypeface.h"
|
|
|
|
|
2012-12-24 18:15:57 +00:00
|
|
|
static inline SkPMColor rgb2gray(SkPMColor c) {
|
2009-10-20 03:26:17 +00:00
|
|
|
unsigned r = SkGetPackedR32(c);
|
|
|
|
unsigned g = SkGetPackedG32(c);
|
|
|
|
unsigned b = SkGetPackedB32(c);
|
2011-02-13 18:21:16 +00:00
|
|
|
|
2011-05-19 19:58:58 +00:00
|
|
|
unsigned x = (r * 5 + g * 7 + b * 4) >> 4;
|
2011-02-13 18:21:16 +00:00
|
|
|
|
2009-10-20 03:26:17 +00:00
|
|
|
return SkPackARGB32(0, x, x, x) | (c & (SK_A32_MASK << SK_A32_SHIFT));
|
|
|
|
}
|
|
|
|
|
|
|
|
class SkGrayScaleColorFilter : public SkColorFilter {
|
|
|
|
public:
|
2012-12-24 18:15:57 +00:00
|
|
|
virtual void filterSpan(const SkPMColor src[], int count,
|
2015-03-26 01:17:31 +00:00
|
|
|
SkPMColor result[]) const override {
|
2012-12-24 18:15:57 +00:00
|
|
|
for (int i = 0; i < count; i++) {
|
2009-10-20 03:26:17 +00:00
|
|
|
result[i] = rgb2gray(src[i]);
|
2012-12-24 18:15:57 +00:00
|
|
|
}
|
2009-10-20 03:26:17 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class SkChannelMaskColorFilter : public SkColorFilter {
|
|
|
|
public:
|
2012-12-24 18:15:57 +00:00
|
|
|
SkChannelMaskColorFilter(U8CPU redMask, U8CPU greenMask, U8CPU blueMask) {
|
2009-10-20 03:26:17 +00:00
|
|
|
fMask = SkPackARGB32(0xFF, redMask, greenMask, blueMask);
|
|
|
|
}
|
|
|
|
|
2012-12-24 18:15:57 +00:00
|
|
|
virtual void filterSpan(const SkPMColor src[], int count,
|
2015-03-26 01:17:31 +00:00
|
|
|
SkPMColor result[]) const override {
|
2009-10-20 03:26:17 +00:00
|
|
|
SkPMColor mask = fMask;
|
2012-12-24 18:15:57 +00:00
|
|
|
for (int i = 0; i < count; i++) {
|
2009-10-20 03:26:17 +00:00
|
|
|
result[i] = src[i] & mask;
|
2012-12-24 18:15:57 +00:00
|
|
|
}
|
2009-10-20 03:26:17 +00:00
|
|
|
}
|
2011-02-13 18:21:16 +00:00
|
|
|
|
2009-10-20 03:26:17 +00:00
|
|
|
private:
|
|
|
|
SkPMColor fMask;
|
|
|
|
};
|
|
|
|
|
2012-12-24 18:15:57 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2009-10-20 03:26:17 +00:00
|
|
|
|
|
|
|
#include "SkGradientShader.h"
|
|
|
|
#include "SkLayerRasterizer.h"
|
|
|
|
#include "SkBlurMaskFilter.h"
|
|
|
|
|
|
|
|
#include "Sk2DPathEffect.h"
|
|
|
|
|
|
|
|
class Dot2DPathEffect : public Sk2DPathEffect {
|
|
|
|
public:
|
|
|
|
Dot2DPathEffect(SkScalar radius, const SkMatrix& matrix,
|
|
|
|
SkTDArray<SkPoint>* pts)
|
|
|
|
: Sk2DPathEffect(matrix), fRadius(radius), fPts(pts) {}
|
|
|
|
|
2012-03-26 17:57:35 +00:00
|
|
|
SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(Dot2DPathEffect)
|
2016-06-16 16:52:35 +00:00
|
|
|
class Registrar {
|
|
|
|
public:
|
|
|
|
Registrar() {
|
|
|
|
SkFlattenable::Register("Dot2DPathEffect",
|
|
|
|
Dot2DPathEffect::CreateProc,
|
|
|
|
Dot2DPathEffect::GetFlattenableType());
|
|
|
|
}
|
|
|
|
};
|
2009-10-20 03:26:17 +00:00
|
|
|
protected:
|
2015-03-26 01:17:31 +00:00
|
|
|
void begin(const SkIRect& uvBounds, SkPath* dst) const override {
|
2009-10-20 03:26:17 +00:00
|
|
|
if (fPts) {
|
|
|
|
fPts->reset();
|
|
|
|
}
|
|
|
|
this->INHERITED::begin(uvBounds, dst);
|
|
|
|
}
|
2012-12-18 16:12:09 +00:00
|
|
|
|
|
|
|
virtual void next(const SkPoint& loc, int u, int v,
|
2015-03-26 01:17:31 +00:00
|
|
|
SkPath* dst) const override {
|
2009-10-20 03:26:17 +00:00
|
|
|
if (fPts) {
|
|
|
|
*fPts->append() = loc;
|
|
|
|
}
|
|
|
|
dst->addCircle(loc.fX, loc.fY, fRadius);
|
|
|
|
}
|
2011-02-13 18:21:16 +00:00
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void flatten(SkWriteBuffer& buffer) const override {
|
2014-08-21 14:59:51 +00:00
|
|
|
buffer.writeMatrix(this->getMatrix());
|
2012-03-29 15:18:04 +00:00
|
|
|
buffer.writeScalar(fRadius);
|
|
|
|
}
|
|
|
|
|
2009-10-20 03:26:17 +00:00
|
|
|
private:
|
|
|
|
SkScalar fRadius;
|
|
|
|
SkTDArray<SkPoint>* fPts;
|
|
|
|
|
|
|
|
typedef Sk2DPathEffect INHERITED;
|
|
|
|
};
|
|
|
|
|
2016-06-16 16:52:35 +00:00
|
|
|
static Dot2DPathEffect::Registrar gReg0;
|
|
|
|
|
2016-04-03 16:11:13 +00:00
|
|
|
sk_sp<SkFlattenable> Dot2DPathEffect::CreateProc(SkReadBuffer& buffer) {
|
2014-08-21 14:59:51 +00:00
|
|
|
SkMatrix matrix;
|
|
|
|
buffer.readMatrix(&matrix);
|
2016-04-03 16:11:13 +00:00
|
|
|
return sk_make_sp<Dot2DPathEffect>(buffer.readScalar(), matrix, nullptr);
|
2014-08-21 14:59:51 +00:00
|
|
|
}
|
|
|
|
|
2009-10-20 03:26:17 +00:00
|
|
|
class InverseFillPE : public SkPathEffect {
|
|
|
|
public:
|
|
|
|
InverseFillPE() {}
|
2012-12-18 16:12:09 +00:00
|
|
|
virtual bool filterPath(SkPath* dst, const SkPath& src,
|
2015-03-26 01:17:31 +00:00
|
|
|
SkStrokeRec*, const SkRect*) const override {
|
2009-10-20 03:26:17 +00:00
|
|
|
*dst = src;
|
|
|
|
dst->setFillType(SkPath::kInverseWinding_FillType);
|
|
|
|
return true;
|
|
|
|
}
|
2015-01-26 14:08:52 +00:00
|
|
|
|
|
|
|
#ifndef SK_IGNORE_TO_STRING
|
2015-03-26 01:17:31 +00:00
|
|
|
void toString(SkString* str) const override {
|
2015-01-26 14:08:52 +00:00
|
|
|
str->appendf("InverseFillPE: ()");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-03-26 17:57:35 +00:00
|
|
|
SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(InverseFillPE)
|
|
|
|
|
2009-10-20 03:26:17 +00:00
|
|
|
private:
|
|
|
|
typedef SkPathEffect INHERITED;
|
|
|
|
};
|
|
|
|
|
2016-04-03 16:11:13 +00:00
|
|
|
sk_sp<SkFlattenable> InverseFillPE::CreateProc(SkReadBuffer& buffer) {
|
|
|
|
return sk_make_sp<InverseFillPE>();
|
|
|
|
}
|
2014-08-21 14:59:51 +00:00
|
|
|
|
2016-03-18 18:22:57 +00:00
|
|
|
static sk_sp<SkPathEffect> makepe(float interp, SkTDArray<SkPoint>* pts) {
|
2009-10-20 03:26:17 +00:00
|
|
|
SkMatrix lattice;
|
|
|
|
SkScalar rad = 3 + SkIntToScalar(4) * (1 - interp);
|
|
|
|
lattice.setScale(rad*2, rad*2, 0, 0);
|
|
|
|
lattice.postSkew(SK_Scalar1/3, 0, 0, 0);
|
2016-03-18 18:22:57 +00:00
|
|
|
return sk_make_sp<Dot2DPathEffect>(rad, lattice, pts);
|
2009-10-20 03:26:17 +00:00
|
|
|
}
|
|
|
|
|
2014-02-26 13:27:37 +00:00
|
|
|
static void r7(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p, SkScalar interp) {
|
2016-03-18 18:22:57 +00:00
|
|
|
p.setPathEffect(makepe(SkScalarToFloat(interp), nullptr));
|
2014-02-26 13:27:37 +00:00
|
|
|
rastBuilder->addLayer(p);
|
2009-10-20 03:26:17 +00:00
|
|
|
#if 0
|
|
|
|
p.setPathEffect(new InverseFillPE())->unref();
|
|
|
|
p.setXfermodeMode(SkXfermode::kSrcIn_Mode);
|
|
|
|
p.setXfermodeMode(SkXfermode::kClear_Mode);
|
|
|
|
p.setAlpha((1 - interp) * 255);
|
2014-02-26 13:27:37 +00:00
|
|
|
rastBuilder->addLayer(p);
|
2009-10-20 03:26:17 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef void (*raster_proc)(SkLayerRasterizer*, SkPaint&);
|
|
|
|
|
|
|
|
#include "SkXfermode.h"
|
|
|
|
|
|
|
|
static void apply_shader(SkPaint* paint, float scale)
|
2011-02-13 18:21:16 +00:00
|
|
|
{
|
2009-10-20 03:26:17 +00:00
|
|
|
SkPaint p;
|
2014-02-26 13:27:37 +00:00
|
|
|
SkLayerRasterizer::Builder rastBuilder;
|
2009-10-20 03:26:17 +00:00
|
|
|
|
|
|
|
p.setAntiAlias(true);
|
2014-02-26 13:27:37 +00:00
|
|
|
r7(&rastBuilder, p, scale);
|
2016-03-21 20:25:16 +00:00
|
|
|
paint->setRasterizer(rastBuilder.detach());
|
2009-10-20 03:26:17 +00:00
|
|
|
|
|
|
|
paint->setColor(SK_ColorBLUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
class ClockFaceView : public SkView {
|
2016-05-12 17:09:30 +00:00
|
|
|
sk_sp<SkTypeface> fFace;
|
2009-10-20 03:26:17 +00:00
|
|
|
SkScalar fInterp;
|
|
|
|
SkScalar fDx;
|
2012-12-24 18:15:57 +00:00
|
|
|
|
2009-10-20 03:26:17 +00:00
|
|
|
public:
|
2012-12-24 18:15:57 +00:00
|
|
|
ClockFaceView() {
|
2016-05-12 17:09:30 +00:00
|
|
|
fFace = SkTypeface::MakeFromFile("/Users/reed/Downloads/p052024l.pfb");
|
2009-10-20 03:26:17 +00:00
|
|
|
fInterp = 0;
|
|
|
|
fDx = SK_Scalar1/64;
|
|
|
|
}
|
2011-02-13 18:21:16 +00:00
|
|
|
|
2009-10-20 03:26:17 +00:00
|
|
|
protected:
|
|
|
|
// overrides from SkEventSink
|
2012-12-24 18:15:57 +00:00
|
|
|
virtual bool onQuery(SkEvent* evt) {
|
|
|
|
if (SampleCode::TitleQ(*evt)) {
|
2009-10-20 03:26:17 +00:00
|
|
|
SampleCode::TitleR(evt, "Text Effects");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return this->INHERITED::onQuery(evt);
|
|
|
|
}
|
2011-02-13 18:21:16 +00:00
|
|
|
|
2012-12-24 18:15:57 +00:00
|
|
|
void drawBG(SkCanvas* canvas) {
|
2009-10-20 03:26:17 +00:00
|
|
|
// canvas->drawColor(0xFFDDDDDD);
|
|
|
|
canvas->drawColor(SK_ColorWHITE);
|
|
|
|
}
|
2011-02-13 18:21:16 +00:00
|
|
|
|
2009-10-20 03:26:17 +00:00
|
|
|
static void drawdots(SkCanvas* canvas, const SkPaint& orig) {
|
|
|
|
SkTDArray<SkPoint> pts;
|
2016-03-18 18:22:57 +00:00
|
|
|
auto pe = makepe(0, &pts);
|
2011-02-13 18:21:16 +00:00
|
|
|
|
Change patheffect to take a (new) StrokeRec object, which encapsulates the fill
or stroke parameters for a path.
Today, the patheffect only sees if the caller was going to stroke or fill, and
if stroke, it just sees the width. With this change, the effect can see all of the
related parameters (e.g. cap/join/miter). No other change is intended at this
time.
After this change, I hope to use this additional data to allow SkDashPathEffect
to, at times, apply the stroke as part of its effect, which may be much more
efficient than first dashing, and then reading that and stroking it.
Most of these files changed just because of the new parameter to filterPath. The
key changes are in SkPathEffect.[h,cpp], SkPaint.cpp and SkScalerContext.cpp
Review URL: https://codereview.appspot.com/6250051
git-svn-id: http://skia.googlecode.com/svn/trunk@4048 2bbb7eff-a529-9590-31e7-b0007b416f81
2012-05-25 01:04:12 +00:00
|
|
|
SkStrokeRec rec(SkStrokeRec::kFill_InitStyle);
|
2009-10-20 03:26:17 +00:00
|
|
|
SkPath path, dstPath;
|
|
|
|
orig.getTextPath("9", 1, 0, 0, &path);
|
2015-08-27 14:41:13 +00:00
|
|
|
pe->filterPath(&dstPath, path, &rec, nullptr);
|
2011-02-13 18:21:16 +00:00
|
|
|
|
2009-10-20 03:26:17 +00:00
|
|
|
SkPaint p;
|
|
|
|
p.setAntiAlias(true);
|
|
|
|
p.setStrokeWidth(10);
|
|
|
|
p.setColor(SK_ColorRED);
|
2016-03-18 18:22:57 +00:00
|
|
|
canvas->drawPoints(SkCanvas::kPoints_PointMode, pts.count(), pts.begin(), p);
|
2009-10-20 03:26:17 +00:00
|
|
|
}
|
2011-02-13 18:21:16 +00:00
|
|
|
|
2009-10-20 03:26:17 +00:00
|
|
|
virtual void onDraw(SkCanvas* canvas) {
|
|
|
|
this->drawBG(canvas);
|
2011-02-13 18:21:16 +00:00
|
|
|
|
2009-10-20 03:26:17 +00:00
|
|
|
SkScalar x = SkIntToScalar(20);
|
|
|
|
SkScalar y = SkIntToScalar(300);
|
|
|
|
SkPaint paint;
|
2011-02-13 18:21:16 +00:00
|
|
|
|
2009-10-20 03:26:17 +00:00
|
|
|
paint.setAntiAlias(true);
|
|
|
|
paint.setTextSize(SkIntToScalar(240));
|
2016-05-31 18:42:36 +00:00
|
|
|
paint.setTypeface(SkTypeface::MakeFromName("sans-serif",
|
|
|
|
SkFontStyle::FromOldStyle(SkTypeface::kBold)));
|
2009-10-20 03:26:17 +00:00
|
|
|
|
|
|
|
SkString str("9");
|
|
|
|
|
|
|
|
paint.setTypeface(fFace);
|
2011-02-13 18:21:16 +00:00
|
|
|
|
2012-05-08 13:15:37 +00:00
|
|
|
apply_shader(&paint, SkScalarToFloat(fInterp));
|
2009-10-20 03:26:17 +00:00
|
|
|
canvas->drawText(str.c_str(), str.size(), x, y, paint);
|
|
|
|
|
|
|
|
// drawdots(canvas, paint);
|
|
|
|
|
|
|
|
if (false) {
|
|
|
|
fInterp += fDx;
|
|
|
|
if (fInterp > 1) {
|
|
|
|
fInterp = 1;
|
|
|
|
fDx = -fDx;
|
|
|
|
} else if (fInterp < 0) {
|
|
|
|
fInterp = 0;
|
|
|
|
fDx = -fDx;
|
|
|
|
}
|
2015-08-27 14:41:13 +00:00
|
|
|
this->inval(nullptr);
|
2009-10-20 03:26:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
typedef SkView INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
static SkView* MyFactory() { return new ClockFaceView; }
|
|
|
|
static SkViewRegister reg(MyFactory);
|