reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame^] | 1 | #include "SampleCode.h" |
| 2 | #include "SkView.h" |
| 3 | #include "SkCanvas.h" |
| 4 | #include "SkGradientShader.h" |
| 5 | #include "SkPath.h" |
| 6 | #include "SkRegion.h" |
| 7 | #include "SkShader.h" |
| 8 | #include "SkUtils.h" |
| 9 | #include "SkColorPriv.h" |
| 10 | #include "SkColorFilter.h" |
| 11 | #include "SkTypeface.h" |
| 12 | #include "SkAvoidXfermode.h" |
| 13 | |
| 14 | static inline SkPMColor rgb2gray(SkPMColor c) |
| 15 | { |
| 16 | unsigned r = SkGetPackedR32(c); |
| 17 | unsigned g = SkGetPackedG32(c); |
| 18 | unsigned b = SkGetPackedB32(c); |
| 19 | |
| 20 | unsigned x = r * 5 + g * 7 + b * 4 >> 4; |
| 21 | |
| 22 | return SkPackARGB32(0, x, x, x) | (c & (SK_A32_MASK << SK_A32_SHIFT)); |
| 23 | } |
| 24 | |
| 25 | class SkGrayScaleColorFilter : public SkColorFilter { |
| 26 | public: |
| 27 | virtual void filterSpan(const SkPMColor src[], int count, SkPMColor result[]) |
| 28 | { |
| 29 | for (int i = 0; i < count; i++) |
| 30 | result[i] = rgb2gray(src[i]); |
| 31 | } |
| 32 | }; |
| 33 | |
| 34 | class SkChannelMaskColorFilter : public SkColorFilter { |
| 35 | public: |
| 36 | SkChannelMaskColorFilter(U8CPU redMask, U8CPU greenMask, U8CPU blueMask) |
| 37 | { |
| 38 | fMask = SkPackARGB32(0xFF, redMask, greenMask, blueMask); |
| 39 | } |
| 40 | |
| 41 | virtual void filterSpan(const SkPMColor src[], int count, SkPMColor result[]) |
| 42 | { |
| 43 | SkPMColor mask = fMask; |
| 44 | for (int i = 0; i < count; i++) |
| 45 | result[i] = src[i] & mask; |
| 46 | } |
| 47 | |
| 48 | private: |
| 49 | SkPMColor fMask; |
| 50 | }; |
| 51 | |
| 52 | /////////////////////////////////////////////////////////// |
| 53 | |
| 54 | #include "SkGradientShader.h" |
| 55 | #include "SkLayerRasterizer.h" |
| 56 | #include "SkBlurMaskFilter.h" |
| 57 | |
| 58 | #include "Sk2DPathEffect.h" |
| 59 | |
| 60 | class Dot2DPathEffect : public Sk2DPathEffect { |
| 61 | public: |
| 62 | Dot2DPathEffect(SkScalar radius, const SkMatrix& matrix, |
| 63 | SkTDArray<SkPoint>* pts) |
| 64 | : Sk2DPathEffect(matrix), fRadius(radius), fPts(pts) {} |
| 65 | |
| 66 | virtual void flatten(SkFlattenableWriteBuffer& buffer) |
| 67 | { |
| 68 | this->INHERITED::flatten(buffer); |
| 69 | |
| 70 | buffer.writeScalar(fRadius); |
| 71 | } |
| 72 | virtual Factory getFactory() { return CreateProc; } |
| 73 | |
| 74 | protected: |
| 75 | virtual void begin(const SkIRect& uvBounds, SkPath* dst) { |
| 76 | if (fPts) { |
| 77 | fPts->reset(); |
| 78 | } |
| 79 | this->INHERITED::begin(uvBounds, dst); |
| 80 | } |
| 81 | // virtual void end(SkPath* dst) {} |
| 82 | virtual void next(const SkPoint& loc, int u, int v, SkPath* dst) |
| 83 | { |
| 84 | if (fPts) { |
| 85 | *fPts->append() = loc; |
| 86 | } |
| 87 | dst->addCircle(loc.fX, loc.fY, fRadius); |
| 88 | } |
| 89 | |
| 90 | Dot2DPathEffect(SkFlattenableReadBuffer& buffer) : Sk2DPathEffect(buffer) |
| 91 | { |
| 92 | fRadius = buffer.readScalar(); |
| 93 | fPts = NULL; |
| 94 | } |
| 95 | private: |
| 96 | SkScalar fRadius; |
| 97 | SkTDArray<SkPoint>* fPts; |
| 98 | |
| 99 | static SkFlattenable* CreateProc(SkFlattenableReadBuffer& buffer) |
| 100 | { |
| 101 | return new Dot2DPathEffect(buffer); |
| 102 | } |
| 103 | |
| 104 | typedef Sk2DPathEffect INHERITED; |
| 105 | }; |
| 106 | |
| 107 | class InverseFillPE : public SkPathEffect { |
| 108 | public: |
| 109 | InverseFillPE() {} |
| 110 | virtual bool filterPath(SkPath* dst, const SkPath& src, SkScalar* width) { |
| 111 | *dst = src; |
| 112 | dst->setFillType(SkPath::kInverseWinding_FillType); |
| 113 | return true; |
| 114 | } |
| 115 | virtual Factory getFactory() { return Factory; } |
| 116 | protected: |
| 117 | // InverseFillPE(SkFlattenableReadBuffer& buffer) : INHERITED(buffer) {} |
| 118 | private: |
| 119 | static SkFlattenable* Factory(SkFlattenableReadBuffer& buffer) { |
| 120 | return new InverseFillPE; |
| 121 | } |
| 122 | typedef SkPathEffect INHERITED; |
| 123 | }; |
| 124 | |
| 125 | static SkPathEffect* makepe(float interp, SkTDArray<SkPoint>* pts) { |
| 126 | SkMatrix lattice; |
| 127 | SkScalar rad = 3 + SkIntToScalar(4) * (1 - interp); |
| 128 | lattice.setScale(rad*2, rad*2, 0, 0); |
| 129 | lattice.postSkew(SK_Scalar1/3, 0, 0, 0); |
| 130 | return new Dot2DPathEffect(rad, lattice, pts); |
| 131 | } |
| 132 | |
| 133 | static void r7(SkLayerRasterizer* rast, SkPaint& p, SkScalar interp) { |
| 134 | p.setPathEffect(makepe(interp, NULL))->unref(); |
| 135 | rast->addLayer(p); |
| 136 | #if 0 |
| 137 | p.setPathEffect(new InverseFillPE())->unref(); |
| 138 | p.setXfermodeMode(SkXfermode::kSrcIn_Mode); |
| 139 | p.setXfermodeMode(SkXfermode::kClear_Mode); |
| 140 | p.setAlpha((1 - interp) * 255); |
| 141 | rast->addLayer(p); |
| 142 | #endif |
| 143 | } |
| 144 | |
| 145 | typedef void (*raster_proc)(SkLayerRasterizer*, SkPaint&); |
| 146 | |
| 147 | #include "SkXfermode.h" |
| 148 | |
| 149 | static void apply_shader(SkPaint* paint, float scale) |
| 150 | { |
| 151 | SkPaint p; |
| 152 | SkLayerRasterizer* rast = new SkLayerRasterizer; |
| 153 | |
| 154 | p.setAntiAlias(true); |
| 155 | r7(rast, p, scale); |
| 156 | paint->setRasterizer(rast)->unref(); |
| 157 | |
| 158 | paint->setColor(SK_ColorBLUE); |
| 159 | } |
| 160 | |
| 161 | class ClockFaceView : public SkView { |
| 162 | SkTypeface* fFace; |
| 163 | SkScalar fInterp; |
| 164 | SkScalar fDx; |
| 165 | public: |
| 166 | ClockFaceView() |
| 167 | { |
| 168 | fFace = SkTypeface::CreateFromFile("/Users/reed/Downloads/p052024l.pfb"); |
| 169 | fInterp = 0; |
| 170 | fDx = SK_Scalar1/64; |
| 171 | } |
| 172 | |
| 173 | virtual ~ClockFaceView() |
| 174 | { |
| 175 | fFace->safeUnref(); |
| 176 | } |
| 177 | |
| 178 | protected: |
| 179 | // overrides from SkEventSink |
| 180 | virtual bool onQuery(SkEvent* evt) |
| 181 | { |
| 182 | if (SampleCode::TitleQ(*evt)) |
| 183 | { |
| 184 | SampleCode::TitleR(evt, "Text Effects"); |
| 185 | return true; |
| 186 | } |
| 187 | return this->INHERITED::onQuery(evt); |
| 188 | } |
| 189 | |
| 190 | void drawBG(SkCanvas* canvas) |
| 191 | { |
| 192 | // canvas->drawColor(0xFFDDDDDD); |
| 193 | canvas->drawColor(SK_ColorWHITE); |
| 194 | } |
| 195 | |
| 196 | static void drawdots(SkCanvas* canvas, const SkPaint& orig) { |
| 197 | SkTDArray<SkPoint> pts; |
| 198 | SkPathEffect* pe = makepe(0, &pts); |
| 199 | |
| 200 | SkScalar width = -1; |
| 201 | SkPath path, dstPath; |
| 202 | orig.getTextPath("9", 1, 0, 0, &path); |
| 203 | pe->filterPath(&dstPath, path, &width); |
| 204 | |
| 205 | SkPaint p; |
| 206 | p.setAntiAlias(true); |
| 207 | p.setStrokeWidth(10); |
| 208 | p.setColor(SK_ColorRED); |
| 209 | canvas->drawPoints(SkCanvas::kPoints_PointMode, pts.count(), pts.begin(), |
| 210 | p); |
| 211 | } |
| 212 | |
| 213 | virtual void onDraw(SkCanvas* canvas) { |
| 214 | this->drawBG(canvas); |
| 215 | |
| 216 | SkScalar x = SkIntToScalar(20); |
| 217 | SkScalar y = SkIntToScalar(300); |
| 218 | SkPaint paint; |
| 219 | |
| 220 | paint.setAntiAlias(true); |
| 221 | paint.setTextSize(SkIntToScalar(240)); |
| 222 | paint.setTypeface(SkTypeface::CreateFromName("sans-serif", |
| 223 | SkTypeface::kBold)); |
| 224 | |
| 225 | SkString str("9"); |
| 226 | |
| 227 | paint.setTypeface(fFace); |
| 228 | |
| 229 | apply_shader(&paint, fInterp); |
| 230 | canvas->drawText(str.c_str(), str.size(), x, y, paint); |
| 231 | |
| 232 | // drawdots(canvas, paint); |
| 233 | |
| 234 | if (false) { |
| 235 | fInterp += fDx; |
| 236 | if (fInterp > 1) { |
| 237 | fInterp = 1; |
| 238 | fDx = -fDx; |
| 239 | } else if (fInterp < 0) { |
| 240 | fInterp = 0; |
| 241 | fDx = -fDx; |
| 242 | } |
| 243 | this->inval(NULL); |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | private: |
| 248 | typedef SkView INHERITED; |
| 249 | }; |
| 250 | |
| 251 | ////////////////////////////////////////////////////////////////////////////// |
| 252 | |
| 253 | static SkView* MyFactory() { return new ClockFaceView; } |
| 254 | static SkViewRegister reg(MyFactory); |
| 255 | |