3 * Copyright 2011 Google Inc.
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
8 #include "SampleCode.h"
11 #include "SkReadBuffer.h"
12 #include "SkWriteBuffer.h"
13 #include "SkGradientShader.h"
18 #include "SkColorPriv.h"
19 #include "SkColorFilter.h"
20 #include "SkTypeface.h"
22 static inline SkPMColor rgb2gray(SkPMColor c) {
23 unsigned r = SkGetPackedR32(c);
24 unsigned g = SkGetPackedG32(c);
25 unsigned b = SkGetPackedB32(c);
27 unsigned x = (r * 5 + g * 7 + b * 4) >> 4;
29 return SkPackARGB32(0, x, x, x) | (c & (SK_A32_MASK << SK_A32_SHIFT));
32 class SkGrayScaleColorFilter : public SkColorFilter {
34 virtual void filterSpan(const SkPMColor src[], int count,
35 SkPMColor result[]) const override {
36 for (int i = 0; i < count; i++) {
37 result[i] = rgb2gray(src[i]);
42 class SkChannelMaskColorFilter : public SkColorFilter {
44 SkChannelMaskColorFilter(U8CPU redMask, U8CPU greenMask, U8CPU blueMask) {
45 fMask = SkPackARGB32(0xFF, redMask, greenMask, blueMask);
48 virtual void filterSpan(const SkPMColor src[], int count,
49 SkPMColor result[]) const override {
50 SkPMColor mask = fMask;
51 for (int i = 0; i < count; i++) {
52 result[i] = src[i] & mask;
60 ///////////////////////////////////////////////////////////////////////////////
62 #include "SkGradientShader.h"
63 #include "SkLayerRasterizer.h"
64 #include "SkBlurMaskFilter.h"
66 #include "Sk2DPathEffect.h"
68 class Dot2DPathEffect : public Sk2DPathEffect {
70 Dot2DPathEffect(SkScalar radius, const SkMatrix& matrix,
71 SkTDArray<SkPoint>* pts)
72 : Sk2DPathEffect(matrix), fRadius(radius), fPts(pts) {}
74 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(Dot2DPathEffect)
77 void begin(const SkIRect& uvBounds, SkPath* dst) const override {
81 this->INHERITED::begin(uvBounds, dst);
84 virtual void next(const SkPoint& loc, int u, int v,
85 SkPath* dst) const override {
87 *fPts->append() = loc;
89 dst->addCircle(loc.fX, loc.fY, fRadius);
92 void flatten(SkWriteBuffer& buffer) const override {
93 buffer.writeMatrix(this->getMatrix());
94 buffer.writeScalar(fRadius);
99 SkTDArray<SkPoint>* fPts;
101 typedef Sk2DPathEffect INHERITED;
104 SkFlattenable* Dot2DPathEffect::CreateProc(SkReadBuffer& buffer) {
106 buffer.readMatrix(&matrix);
107 return SkNEW_ARGS(Dot2DPathEffect, (buffer.readScalar(), matrix, NULL));
110 class InverseFillPE : public SkPathEffect {
113 virtual bool filterPath(SkPath* dst, const SkPath& src,
114 SkStrokeRec*, const SkRect*) const override {
116 dst->setFillType(SkPath::kInverseWinding_FillType);
120 #ifndef SK_IGNORE_TO_STRING
121 void toString(SkString* str) const override {
122 str->appendf("InverseFillPE: ()");
126 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(InverseFillPE)
129 typedef SkPathEffect INHERITED;
132 SkFlattenable* InverseFillPE::CreateProc(SkReadBuffer& buffer) {
133 return SkNEW(InverseFillPE);
136 static SkPathEffect* makepe(float interp, SkTDArray<SkPoint>* pts) {
138 SkScalar rad = 3 + SkIntToScalar(4) * (1 - interp);
139 lattice.setScale(rad*2, rad*2, 0, 0);
140 lattice.postSkew(SK_Scalar1/3, 0, 0, 0);
141 return new Dot2DPathEffect(rad, lattice, pts);
144 static void r7(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p, SkScalar interp) {
145 p.setPathEffect(makepe(SkScalarToFloat(interp), NULL))->unref();
146 rastBuilder->addLayer(p);
148 p.setPathEffect(new InverseFillPE())->unref();
149 p.setXfermodeMode(SkXfermode::kSrcIn_Mode);
150 p.setXfermodeMode(SkXfermode::kClear_Mode);
151 p.setAlpha((1 - interp) * 255);
152 rastBuilder->addLayer(p);
156 typedef void (*raster_proc)(SkLayerRasterizer*, SkPaint&);
158 #include "SkXfermode.h"
160 static void apply_shader(SkPaint* paint, float scale)
163 SkLayerRasterizer::Builder rastBuilder;
165 p.setAntiAlias(true);
166 r7(&rastBuilder, p, scale);
167 paint->setRasterizer(rastBuilder.detachRasterizer())->unref();
169 paint->setColor(SK_ColorBLUE);
172 class ClockFaceView : public SkView {
179 fFace = SkTypeface::CreateFromFile("/Users/reed/Downloads/p052024l.pfb");
184 virtual ~ClockFaceView() {
189 // overrides from SkEventSink
190 virtual bool onQuery(SkEvent* evt) {
191 if (SampleCode::TitleQ(*evt)) {
192 SampleCode::TitleR(evt, "Text Effects");
195 return this->INHERITED::onQuery(evt);
198 void drawBG(SkCanvas* canvas) {
199 // canvas->drawColor(0xFFDDDDDD);
200 canvas->drawColor(SK_ColorWHITE);
203 static void drawdots(SkCanvas* canvas, const SkPaint& orig) {
204 SkTDArray<SkPoint> pts;
205 SkPathEffect* pe = makepe(0, &pts);
207 SkStrokeRec rec(SkStrokeRec::kFill_InitStyle);
208 SkPath path, dstPath;
209 orig.getTextPath("9", 1, 0, 0, &path);
210 pe->filterPath(&dstPath, path, &rec, NULL);
213 p.setAntiAlias(true);
214 p.setStrokeWidth(10);
215 p.setColor(SK_ColorRED);
216 canvas->drawPoints(SkCanvas::kPoints_PointMode, pts.count(), pts.begin(),
220 virtual void onDraw(SkCanvas* canvas) {
221 this->drawBG(canvas);
223 SkScalar x = SkIntToScalar(20);
224 SkScalar y = SkIntToScalar(300);
227 paint.setAntiAlias(true);
228 paint.setTextSize(SkIntToScalar(240));
229 paint.setTypeface(SkTypeface::CreateFromName("sans-serif",
234 paint.setTypeface(fFace);
236 apply_shader(&paint, SkScalarToFloat(fInterp));
237 canvas->drawText(str.c_str(), str.size(), x, y, paint);
239 // drawdots(canvas, paint);
246 } else if (fInterp < 0) {
255 typedef SkView INHERITED;
258 //////////////////////////////////////////////////////////////////////////////
260 static SkView* MyFactory() { return new ClockFaceView; }
261 static SkViewRegister reg(MyFactory);