blob: db4dbeaef2f4d74aa0d7f0aa580fc343be2835ed [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
2/*
3 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
reed@android.com8a1c16f2008-12-17 15:59:43 +00008#include "SampleCode.h"
9#include "SkView.h"
10#include "SkCanvas.h"
11#include "Sk64.h"
12#include "SkGradientShader.h"
13#include "SkGraphics.h"
14#include "SkImageDecoder.h"
15#include "SkKernel33MaskFilter.h"
16#include "SkPath.h"
17#include "SkRandom.h"
18#include "SkRegion.h"
19#include "SkShader.h"
20#include "SkUtils.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000021#include "SkColorPriv.h"
22#include "SkColorFilter.h"
23#include "SkTime.h"
24#include "SkTypeface.h"
25#include "SkXfermode.h"
26
27#include "SkStream.h"
28#include "SkXMLParser.h"
29
reed@android.com8a1c16f2008-12-17 15:59:43 +000030class ReduceNoise : public SkKernel33ProcMaskFilter {
31public:
32 ReduceNoise(int percent256) : SkKernel33ProcMaskFilter(percent256) {}
33 virtual uint8_t computeValue(uint8_t* const* srcRows)
34 {
35 int c = srcRows[1][1];
36 int min = 255, max = 0;
37 for (int i = 0; i < 3; i++)
38 for (int j = 0; j < 3; j++)
39 if (i != 1 || j != 1)
40 {
41 int v = srcRows[i][j];
42 if (max < v)
43 max = v;
44 if (min > v)
45 min = v;
46 }
47 if (c > max) c = max;
48 // if (c < min) c = min;
49 return c;
50 }
djsollen@google.comba28d032012-03-26 17:57:35 +000051 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(ReduceNoise)
52
reed@android.com8a1c16f2008-12-17 15:59:43 +000053private:
54 ReduceNoise(SkFlattenableReadBuffer& rb) : SkKernel33ProcMaskFilter(rb) {}
reed@android.com8a1c16f2008-12-17 15:59:43 +000055};
56
57class Darken : public SkKernel33ProcMaskFilter {
58public:
59 Darken(int percent256) : SkKernel33ProcMaskFilter(percent256) {}
60 virtual uint8_t computeValue(uint8_t* const* srcRows)
61 {
62 int c = srcRows[1][1];
63 float f = c / 255.f;
reed@google.com82065d62011-02-07 15:30:46 +000064
reed@google.com9e39bb32011-05-18 12:17:53 +000065 if (c >= 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000066 f = sqrtf(f);
reed@google.com9e39bb32011-05-18 12:17:53 +000067 } else {
reed@android.com8a1c16f2008-12-17 15:59:43 +000068 f *= f;
69 }
70 SkASSERT(f >= 0 && f <= 1);
71 return (int)(f * 255);
72 }
djsollen@google.comba28d032012-03-26 17:57:35 +000073 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(Darken)
74
reed@android.com8a1c16f2008-12-17 15:59:43 +000075private:
76 Darken(SkFlattenableReadBuffer& rb) : SkKernel33ProcMaskFilter(rb) {}
reed@android.com8a1c16f2008-12-17 15:59:43 +000077};
78
79static SkMaskFilter* makemf() { return new Darken(0x30); }
80
reed@google.com9e39bb32011-05-18 12:17:53 +000081static void test_breakText() {
reed@android.com8a1c16f2008-12-17 15:59:43 +000082 SkPaint paint;
83 const char* text = "sdfkljAKLDFJKEWkldfjlk#$%&sdfs.dsj";
84 size_t length = strlen(text);
85 SkScalar width = paint.measureText(text, length);
reed@google.com82065d62011-02-07 15:30:46 +000086
reed@android.com8a1c16f2008-12-17 15:59:43 +000087 SkScalar mm = 0;
88 SkScalar nn = 0;
reed@google.com9e39bb32011-05-18 12:17:53 +000089 for (SkScalar w = 0; w <= width; w += SK_Scalar1) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000090 SkScalar m;
91 size_t n = paint.breakText(text, length, w, &m,
92 SkPaint::kBackward_TextBufferDirection);
reed@google.com82065d62011-02-07 15:30:46 +000093
reed@android.com8a1c16f2008-12-17 15:59:43 +000094 SkASSERT(n <= length);
95 SkASSERT(m <= width);
reed@google.com82065d62011-02-07 15:30:46 +000096
reed@google.com9e39bb32011-05-18 12:17:53 +000097 if (n == 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000098 SkASSERT(m == 0);
reed@google.com9e39bb32011-05-18 12:17:53 +000099 } else {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000100 // now assert that we're monotonic
reed@google.com9e39bb32011-05-18 12:17:53 +0000101 if (n == nn) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000102 SkASSERT(m == mm);
reed@google.com9e39bb32011-05-18 12:17:53 +0000103 } else {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000104 SkASSERT(n > nn);
105 SkASSERT(m > mm);
106 }
107 }
tomhudson@google.comffe39bd2012-05-17 15:38:00 +0000108 nn = SkIntToScalar((unsigned int)n);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000109 mm = m;
110 }
111
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +0000112 SkDEBUGCODE(size_t length2 =) paint.breakText(text, length, width, &mm);
reed@google.com261b8e22011-04-14 17:53:24 +0000113 SkASSERT(length2 == length);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000114 SkASSERT(mm == width);
115}
116
117static SkRandom gRand;
118
119class SkPowerMode : public SkXfermode {
120public:
121 SkPowerMode(SkScalar exponent) { this->init(exponent); }
122
reed@google.com9e39bb32011-05-18 12:17:53 +0000123 virtual void xfer16(uint16_t dst[], const SkPMColor src[], int count,
124 const SkAlpha aa[]);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000125
126 typedef SkFlattenable* (*Factory)(SkFlattenableReadBuffer&);
reed@google.com82065d62011-02-07 15:30:46 +0000127
djsollen@google.comba28d032012-03-26 17:57:35 +0000128 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkPowerMode)
reed@google.com82065d62011-02-07 15:30:46 +0000129
reed@android.com8a1c16f2008-12-17 15:59:43 +0000130private:
131 SkScalar fExp; // user's value
132 uint8_t fTable[256]; // cache
133
134 void init(SkScalar exponent);
djsollen@google.com54924242012-03-29 15:18:04 +0000135 SkPowerMode(SkFlattenableReadBuffer& b) : INHERITED(b) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000136 // read the exponent
137 this->init(SkFixedToScalar(b.readS32()));
138 }
djsollen@google.com54924242012-03-29 15:18:04 +0000139 virtual void flatten(SkFlattenableWriteBuffer& b) const SK_OVERRIDE {
140 this->INHERITED::flatten(b);
141 b.write32(SkScalarToFixed(fExp));
142 }
reed@google.com82065d62011-02-07 15:30:46 +0000143
reed@android.com8a1c16f2008-12-17 15:59:43 +0000144 typedef SkXfermode INHERITED;
145};
146
reed@google.com9e39bb32011-05-18 12:17:53 +0000147void SkPowerMode::init(SkScalar e) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000148 fExp = e;
149 float ee = SkScalarToFloat(e);
reed@google.com82065d62011-02-07 15:30:46 +0000150
reed@android.com8a1c16f2008-12-17 15:59:43 +0000151 printf("------ %g\n", ee);
reed@google.com9e39bb32011-05-18 12:17:53 +0000152 for (int i = 0; i < 256; i++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000153 float x = i / 255.f;
154 // printf(" %d %g", i, x);
155 x = powf(x, ee);
156 // printf(" %g", x);
157 int xx = SkScalarRound(SkFloatToScalar(x * 255));
158 // printf(" %d\n", xx);
159 fTable[i] = SkToU8(xx);
160 }
161}
162
reed@google.com9e39bb32011-05-18 12:17:53 +0000163void SkPowerMode::xfer16(uint16_t dst[], const SkPMColor src[], int count,
164 const SkAlpha aa[]) {
165 for (int i = 0; i < count; i++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000166 SkPMColor c = src[i];
167 int r = SkGetPackedR32(c);
168 int g = SkGetPackedG32(c);
169 int b = SkGetPackedB32(c);
170 r = fTable[r];
171 g = fTable[g];
172 b = fTable[b];
173 dst[i] = SkPack888ToRGB16(r, g, b);
174 }
175}
176
177static const struct {
178 const char* fName;
179 uint32_t fFlags;
180 bool fFlushCache;
181} gHints[] = {
182 { "Linear", SkPaint::kLinearText_Flag, false },
183 { "Normal", 0, true },
184 { "Subpixel", SkPaint::kSubpixelText_Flag, true }
185};
186
reed@android.com8a1c16f2008-12-17 15:59:43 +0000187static void DrawTheText(SkCanvas* canvas, const char text[], size_t length,
188 SkScalar x, SkScalar y, const SkPaint& paint,
reed@google.com9e39bb32011-05-18 12:17:53 +0000189 SkScalar clickX, SkMaskFilter* mf) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000190 SkPaint p(paint);
191
192#if 0
193 canvas->drawText(text, length, x, y, paint);
194#else
195 {
196 SkPoint pts[1000];
197 SkScalar xpos = x;
198 SkASSERT(length <= SK_ARRAY_COUNT(pts));
reed@google.com9e39bb32011-05-18 12:17:53 +0000199 for (size_t i = 0; i < length; i++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000200 pts[i].set(xpos, y), xpos += paint.getTextSize();
reed@google.com9e39bb32011-05-18 12:17:53 +0000201 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000202 canvas->drawPosText(text, length, pts, paint);
203 }
204#endif
205
206 p.setSubpixelText(true);
207 x += SkIntToScalar(180);
208 canvas->drawText(text, length, x, y, p);
209
reed@android.com8a1c16f2008-12-17 15:59:43 +0000210#ifdef SK_DEBUG
reed@google.com9e39bb32011-05-18 12:17:53 +0000211 if (true) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000212 // p.setMaskFilter(mf);
213 p.setSubpixelText(false);
214 p.setLinearText(true);
215 x += SkIntToScalar(180);
216 canvas->drawText(text, length, x, y, p);
217 }
218#endif
219}
220
reed@google.com9e39bb32011-05-18 12:17:53 +0000221class TextSpeedView : public SampleView {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000222public:
reed@google.com9e39bb32011-05-18 12:17:53 +0000223 TextSpeedView() {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000224 fMF = makemf();
225
226 fHints = 0;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000227 fClickX = 0;
228
reed@google.com82065d62011-02-07 15:30:46 +0000229 test_breakText();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000230 }
reed@google.com82065d62011-02-07 15:30:46 +0000231
reed@google.com9e39bb32011-05-18 12:17:53 +0000232 virtual ~TextSpeedView() {
reed@google.com82065d62011-02-07 15:30:46 +0000233 SkSafeUnref(fMF);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000234 }
235
236protected:
237 // overrides from SkEventSink
reed@google.com9e39bb32011-05-18 12:17:53 +0000238 virtual bool onQuery(SkEvent* evt) {
239 if (SampleCode::TitleQ(*evt)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000240 SampleCode::TitleR(evt, "Text");
241 return true;
242 }
243 return this->INHERITED::onQuery(evt);
244 }
reed@google.com82065d62011-02-07 15:30:46 +0000245
reed@google.com9e39bb32011-05-18 12:17:53 +0000246 static void make_textstrip(SkBitmap* bm) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000247 bm->setConfig(SkBitmap::kRGB_565_Config, 200, 18);
248 bm->allocPixels();
249 bm->eraseColor(SK_ColorWHITE);
reed@google.com82065d62011-02-07 15:30:46 +0000250
reed@android.com8a1c16f2008-12-17 15:59:43 +0000251 SkCanvas canvas(*bm);
252 SkPaint paint;
253 const char* s = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit";
reed@google.com82065d62011-02-07 15:30:46 +0000254
reed@android.com8a1c16f2008-12-17 15:59:43 +0000255 paint.setFlags(paint.getFlags() | SkPaint::kAntiAlias_Flag
256 | SkPaint::kDevKernText_Flag);
257 paint.setTextSize(SkIntToScalar(14));
258 canvas.drawText(s, strlen(s), SkIntToScalar(8), SkIntToScalar(14), paint);
259 }
reed@google.com82065d62011-02-07 15:30:46 +0000260
reed@google.com9e39bb32011-05-18 12:17:53 +0000261 static void fill_pts(SkPoint pts[], size_t n, SkRandom* rand) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000262 for (size_t i = 0; i < n; i++)
263 pts[i].set(rand->nextUScalar1() * 640, rand->nextUScalar1() * 480);
264 }
reed@google.com82065d62011-02-07 15:30:46 +0000265
reed@google.com9e39bb32011-05-18 12:17:53 +0000266 virtual void onDrawContent(SkCanvas* canvas) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000267 SkAutoCanvasRestore restore(canvas, false);
268 {
269 SkRect r;
270 r.set(0, 0, SkIntToScalar(1000), SkIntToScalar(20));
271 // canvas->saveLayer(&r, NULL, SkCanvas::kHasAlphaLayer_SaveFlag);
272 }
273
274 SkPaint paint;
275// const uint16_t glyphs[] = { 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19 };
276 int index = fHints % SK_ARRAY_COUNT(gHints);
277 index = 1;
278// const char* style = gHints[index].fName;
reed@google.com82065d62011-02-07 15:30:46 +0000279
reed@android.com8a1c16f2008-12-17 15:59:43 +0000280// canvas->translate(0, SkIntToScalar(50));
281
282 // canvas->drawText(style, strlen(style), SkIntToScalar(20), SkIntToScalar(20), paint);
283
reed@android.com04d86c62010-01-25 22:02:44 +0000284 SkSafeUnref(paint.setTypeface(SkTypeface::CreateFromFile("/skimages/samplefont.ttf")));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000285 paint.setAntiAlias(true);
286 paint.setFlags(paint.getFlags() | gHints[index].fFlags);
reed@google.com82065d62011-02-07 15:30:46 +0000287
reed@android.com8a1c16f2008-12-17 15:59:43 +0000288 SkRect clip;
289 clip.set(SkIntToScalar(25), SkIntToScalar(34), SkIntToScalar(88), SkIntToScalar(155));
reed@google.com82065d62011-02-07 15:30:46 +0000290
reed@android.com8a1c16f2008-12-17 15:59:43 +0000291 const char* text = "Hamburgefons";
292 size_t length = strlen(text);
293
reed@google.com2f3dc9d2011-05-02 17:33:45 +0000294 SkScalar y = SkIntToScalar(0);
295 for (int i = 9; i <= 24; i++) {
296 paint.setTextSize(SkIntToScalar(i) /*+ (gRand.nextU() & 0xFFFF)*/);
reed@google.com9e39bb32011-05-18 12:17:53 +0000297 for (SkScalar dx = 0; dx <= SkIntToScalar(3)/4;
298 dx += SkIntToScalar(1) /* /4 */) {
reed@google.com2f3dc9d2011-05-02 17:33:45 +0000299 y += paint.getFontSpacing();
reed@google.com9e39bb32011-05-18 12:17:53 +0000300 DrawTheText(canvas, text, length, SkIntToScalar(20) + dx, y,
301 paint, fClickX, fMF);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000302 }
303 }
reed@google.com2f3dc9d2011-05-02 17:33:45 +0000304 if (gHints[index].fFlushCache) {
305// SkGraphics::SetFontCacheUsed(0);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000306 }
307 }
reed@google.com82065d62011-02-07 15:30:46 +0000308
reed@google.com9e39bb32011-05-18 12:17:53 +0000309 virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000310 fClickX = x;
311 this->inval(NULL);
312 return this->INHERITED::onFindClickHandler(x, y);
313 }
reed@google.com82065d62011-02-07 15:30:46 +0000314
reed@google.com9e39bb32011-05-18 12:17:53 +0000315 virtual bool onClick(Click* click) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000316 return this->INHERITED::onClick(click);
317 }
reed@google.com82065d62011-02-07 15:30:46 +0000318
reed@android.com8a1c16f2008-12-17 15:59:43 +0000319private:
320 int fHints;
321 SkScalar fClickX;
322 SkMaskFilter* fMF;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000323
reed@google.com9e39bb32011-05-18 12:17:53 +0000324 typedef SampleView INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000325};
326
327//////////////////////////////////////////////////////////////////////////////
328
329static SkView* MyFactory() { return new TextSpeedView; }
330static SkViewRegister reg(MyFactory);
331