blob: c56ebc8b90f9f8262fdf5c9cb147192b8a3f8caa [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
reed@google.combf0001d2014-01-13 14:53:55 +00007
reed@android.com8a1c16f2008-12-17 15:59:43 +00008#include "SampleCode.h"
9#include "SkView.h"
10#include "SkCanvas.h"
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000011#include "SkReadBuffer.h"
12#include "SkWriteBuffer.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000013#include "SkGradientShader.h"
14#include "SkGraphics.h"
15#include "SkImageDecoder.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000016#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@google.com9e39bb32011-05-18 12:17:53 +000030static void test_breakText() {
reed@android.com8a1c16f2008-12-17 15:59:43 +000031 SkPaint paint;
32 const char* text = "sdfkljAKLDFJKEWkldfjlk#$%&sdfs.dsj";
33 size_t length = strlen(text);
34 SkScalar width = paint.measureText(text, length);
reed@google.com82065d62011-02-07 15:30:46 +000035
reed@android.com8a1c16f2008-12-17 15:59:43 +000036 SkScalar mm = 0;
37 SkScalar nn = 0;
reed@google.com9e39bb32011-05-18 12:17:53 +000038 for (SkScalar w = 0; w <= width; w += SK_Scalar1) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000039 SkScalar m;
40 size_t n = paint.breakText(text, length, w, &m,
41 SkPaint::kBackward_TextBufferDirection);
reed@google.com82065d62011-02-07 15:30:46 +000042
reed@android.com8a1c16f2008-12-17 15:59:43 +000043 SkASSERT(n <= length);
44 SkASSERT(m <= width);
reed@google.com82065d62011-02-07 15:30:46 +000045
reed@google.com9e39bb32011-05-18 12:17:53 +000046 if (n == 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000047 SkASSERT(m == 0);
reed@google.com9e39bb32011-05-18 12:17:53 +000048 } else {
reed@android.com8a1c16f2008-12-17 15:59:43 +000049 // now assert that we're monotonic
reed@google.com9e39bb32011-05-18 12:17:53 +000050 if (n == nn) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000051 SkASSERT(m == mm);
reed@google.com9e39bb32011-05-18 12:17:53 +000052 } else {
reed@android.com8a1c16f2008-12-17 15:59:43 +000053 SkASSERT(n > nn);
54 SkASSERT(m > mm);
55 }
56 }
tomhudson@google.comffe39bd2012-05-17 15:38:00 +000057 nn = SkIntToScalar((unsigned int)n);
reed@android.com8a1c16f2008-12-17 15:59:43 +000058 mm = m;
59 }
60
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +000061 SkDEBUGCODE(size_t length2 =) paint.breakText(text, length, width, &mm);
reed@google.com261b8e22011-04-14 17:53:24 +000062 SkASSERT(length2 == length);
reed@android.com8a1c16f2008-12-17 15:59:43 +000063 SkASSERT(mm == width);
64}
65
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000066static SkRandom gRand;
reed@android.com8a1c16f2008-12-17 15:59:43 +000067
68class SkPowerMode : public SkXfermode {
69public:
70 SkPowerMode(SkScalar exponent) { this->init(exponent); }
71
reed@google.com9e39bb32011-05-18 12:17:53 +000072 virtual void xfer16(uint16_t dst[], const SkPMColor src[], int count,
mike@reedtribe.org3af2b8d2012-12-24 18:26:43 +000073 const SkAlpha aa[]) const SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +000074
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000075 typedef SkFlattenable* (*Factory)(SkReadBuffer&);
reed@google.com82065d62011-02-07 15:30:46 +000076
commit-bot@chromium.org0f10f7b2014-03-13 18:02:17 +000077 SK_TO_STRING_OVERRIDE()
djsollen@google.comba28d032012-03-26 17:57:35 +000078 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkPowerMode)
reed@google.com82065d62011-02-07 15:30:46 +000079
reed@android.com8a1c16f2008-12-17 15:59:43 +000080private:
81 SkScalar fExp; // user's value
82 uint8_t fTable[256]; // cache
83
84 void init(SkScalar exponent);
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000085 SkPowerMode(SkReadBuffer& b) : INHERITED(b) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000086 // read the exponent
djsollen@google.comc73dd5c2012-08-07 15:54:32 +000087 this->init(SkFixedToScalar(b.readFixed()));
reed@android.com8a1c16f2008-12-17 15:59:43 +000088 }
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000089 virtual void flatten(SkWriteBuffer& b) const SK_OVERRIDE {
djsollen@google.com54924242012-03-29 15:18:04 +000090 this->INHERITED::flatten(b);
djsollen@google.comc73dd5c2012-08-07 15:54:32 +000091 b.writeFixed(SkScalarToFixed(fExp));
djsollen@google.com54924242012-03-29 15:18:04 +000092 }
reed@google.com82065d62011-02-07 15:30:46 +000093
reed@android.com8a1c16f2008-12-17 15:59:43 +000094 typedef SkXfermode INHERITED;
95};
96
reed@google.com9e39bb32011-05-18 12:17:53 +000097void SkPowerMode::init(SkScalar e) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000098 fExp = e;
99 float ee = SkScalarToFloat(e);
reed@google.com82065d62011-02-07 15:30:46 +0000100
reed@android.com8a1c16f2008-12-17 15:59:43 +0000101 printf("------ %g\n", ee);
reed@google.com9e39bb32011-05-18 12:17:53 +0000102 for (int i = 0; i < 256; i++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000103 float x = i / 255.f;
104 // printf(" %d %g", i, x);
105 x = powf(x, ee);
106 // printf(" %g", x);
reed@google.come1ca7052013-12-17 19:22:07 +0000107 int xx = SkScalarRoundToInt(x * 255);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000108 // printf(" %d\n", xx);
109 fTable[i] = SkToU8(xx);
110 }
111}
112
reed@google.com9e39bb32011-05-18 12:17:53 +0000113void SkPowerMode::xfer16(uint16_t dst[], const SkPMColor src[], int count,
mike@reedtribe.org3af2b8d2012-12-24 18:26:43 +0000114 const SkAlpha aa[]) const {
reed@google.com9e39bb32011-05-18 12:17:53 +0000115 for (int i = 0; i < count; i++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000116 SkPMColor c = src[i];
117 int r = SkGetPackedR32(c);
118 int g = SkGetPackedG32(c);
119 int b = SkGetPackedB32(c);
120 r = fTable[r];
121 g = fTable[g];
122 b = fTable[b];
123 dst[i] = SkPack888ToRGB16(r, g, b);
124 }
125}
126
commit-bot@chromium.org0f10f7b2014-03-13 18:02:17 +0000127#ifndef SK_IGNORE_TO_STRING
robertphillips@google.comb83b6b42013-01-22 14:32:09 +0000128void SkPowerMode::toString(SkString* str) const {
129 str->append("SkPowerMode: exponent ");
130 str->appendScalar(fExp);
131}
132#endif
133
reed@android.com8a1c16f2008-12-17 15:59:43 +0000134static const struct {
135 const char* fName;
136 uint32_t fFlags;
137 bool fFlushCache;
138} gHints[] = {
139 { "Linear", SkPaint::kLinearText_Flag, false },
140 { "Normal", 0, true },
141 { "Subpixel", SkPaint::kSubpixelText_Flag, true }
142};
143
commit-bot@chromium.orge003aec2014-05-12 13:35:55 +0000144static void DrawTheText(SkCanvas* canvas, const char text[], size_t length, SkScalar x, SkScalar y,
145 const SkPaint& paint, SkScalar clickX) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000146 SkPaint p(paint);
147
148#if 0
149 canvas->drawText(text, length, x, y, paint);
150#else
151 {
152 SkPoint pts[1000];
153 SkScalar xpos = x;
154 SkASSERT(length <= SK_ARRAY_COUNT(pts));
reed@google.com9e39bb32011-05-18 12:17:53 +0000155 for (size_t i = 0; i < length; i++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000156 pts[i].set(xpos, y), xpos += paint.getTextSize();
reed@google.com9e39bb32011-05-18 12:17:53 +0000157 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000158 canvas->drawPosText(text, length, pts, paint);
159 }
160#endif
161
162 p.setSubpixelText(true);
163 x += SkIntToScalar(180);
164 canvas->drawText(text, length, x, y, p);
165
reed@android.com8a1c16f2008-12-17 15:59:43 +0000166#ifdef SK_DEBUG
reed@google.com9e39bb32011-05-18 12:17:53 +0000167 if (true) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000168 p.setSubpixelText(false);
169 p.setLinearText(true);
170 x += SkIntToScalar(180);
171 canvas->drawText(text, length, x, y, p);
172 }
173#endif
174}
175
reed@google.com9e39bb32011-05-18 12:17:53 +0000176class TextSpeedView : public SampleView {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000177public:
rmistry@google.comae933ce2012-08-23 18:19:56 +0000178 TextSpeedView() {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000179 fHints = 0;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000180 fClickX = 0;
181
reed@google.com82065d62011-02-07 15:30:46 +0000182 test_breakText();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000183 }
reed@google.com82065d62011-02-07 15:30:46 +0000184
reed@android.com8a1c16f2008-12-17 15:59:43 +0000185protected:
186 // overrides from SkEventSink
reed@google.com9e39bb32011-05-18 12:17:53 +0000187 virtual bool onQuery(SkEvent* evt) {
188 if (SampleCode::TitleQ(*evt)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000189 SampleCode::TitleR(evt, "Text");
190 return true;
191 }
192 return this->INHERITED::onQuery(evt);
193 }
reed@google.com82065d62011-02-07 15:30:46 +0000194
reed@google.com9e39bb32011-05-18 12:17:53 +0000195 static void make_textstrip(SkBitmap* bm) {
commit-bot@chromium.orga8c18312014-02-17 02:55:57 +0000196 bm->allocPixels(SkImageInfo::Make(200, 18, kRGB_565_SkColorType,
197 kOpaque_SkAlphaType));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000198 bm->eraseColor(SK_ColorWHITE);
reed@google.com82065d62011-02-07 15:30:46 +0000199
reed@android.com8a1c16f2008-12-17 15:59:43 +0000200 SkCanvas canvas(*bm);
201 SkPaint paint;
202 const char* s = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit";
reed@google.com82065d62011-02-07 15:30:46 +0000203
reed@android.com8a1c16f2008-12-17 15:59:43 +0000204 paint.setFlags(paint.getFlags() | SkPaint::kAntiAlias_Flag
205 | SkPaint::kDevKernText_Flag);
206 paint.setTextSize(SkIntToScalar(14));
207 canvas.drawText(s, strlen(s), SkIntToScalar(8), SkIntToScalar(14), paint);
208 }
reed@google.com82065d62011-02-07 15:30:46 +0000209
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000210 static void fill_pts(SkPoint pts[], size_t n, SkRandom* rand) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000211 for (size_t i = 0; i < n; i++)
212 pts[i].set(rand->nextUScalar1() * 640, rand->nextUScalar1() * 480);
213 }
reed@google.com82065d62011-02-07 15:30:46 +0000214
reed@google.com9e39bb32011-05-18 12:17:53 +0000215 virtual void onDrawContent(SkCanvas* canvas) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000216 SkAutoCanvasRestore restore(canvas, false);
217 {
218 SkRect r;
219 r.set(0, 0, SkIntToScalar(1000), SkIntToScalar(20));
220 // canvas->saveLayer(&r, NULL, SkCanvas::kHasAlphaLayer_SaveFlag);
221 }
222
223 SkPaint paint;
224// const uint16_t glyphs[] = { 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19 };
225 int index = fHints % SK_ARRAY_COUNT(gHints);
226 index = 1;
227// const char* style = gHints[index].fName;
reed@google.com82065d62011-02-07 15:30:46 +0000228
reed@android.com8a1c16f2008-12-17 15:59:43 +0000229// canvas->translate(0, SkIntToScalar(50));
230
231 // canvas->drawText(style, strlen(style), SkIntToScalar(20), SkIntToScalar(20), paint);
232
reed@android.com04d86c62010-01-25 22:02:44 +0000233 SkSafeUnref(paint.setTypeface(SkTypeface::CreateFromFile("/skimages/samplefont.ttf")));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000234 paint.setAntiAlias(true);
235 paint.setFlags(paint.getFlags() | gHints[index].fFlags);
reed@google.com82065d62011-02-07 15:30:46 +0000236
reed@android.com8a1c16f2008-12-17 15:59:43 +0000237 SkRect clip;
238 clip.set(SkIntToScalar(25), SkIntToScalar(34), SkIntToScalar(88), SkIntToScalar(155));
reed@google.com82065d62011-02-07 15:30:46 +0000239
reed@android.com8a1c16f2008-12-17 15:59:43 +0000240 const char* text = "Hamburgefons";
241 size_t length = strlen(text);
242
reed@google.com2f3dc9d2011-05-02 17:33:45 +0000243 SkScalar y = SkIntToScalar(0);
244 for (int i = 9; i <= 24; i++) {
245 paint.setTextSize(SkIntToScalar(i) /*+ (gRand.nextU() & 0xFFFF)*/);
reed@google.com9e39bb32011-05-18 12:17:53 +0000246 for (SkScalar dx = 0; dx <= SkIntToScalar(3)/4;
247 dx += SkIntToScalar(1) /* /4 */) {
reed@google.com2f3dc9d2011-05-02 17:33:45 +0000248 y += paint.getFontSpacing();
commit-bot@chromium.orge003aec2014-05-12 13:35:55 +0000249 DrawTheText(canvas, text, length, SkIntToScalar(20) + dx, y, paint, fClickX);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000250 }
251 }
reed@google.com2f3dc9d2011-05-02 17:33:45 +0000252 if (gHints[index].fFlushCache) {
253// SkGraphics::SetFontCacheUsed(0);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000254 }
255 }
reed@google.com82065d62011-02-07 15:30:46 +0000256
reed@google.com4d5c26d2013-01-08 16:17:50 +0000257 virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y,
258 unsigned modi) SK_OVERRIDE {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000259 fClickX = x;
260 this->inval(NULL);
reed@google.com4d5c26d2013-01-08 16:17:50 +0000261 return this->INHERITED::onFindClickHandler(x, y, modi);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000262 }
reed@google.com82065d62011-02-07 15:30:46 +0000263
reed@google.com9e39bb32011-05-18 12:17:53 +0000264 virtual bool onClick(Click* click) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000265 return this->INHERITED::onClick(click);
266 }
reed@google.com82065d62011-02-07 15:30:46 +0000267
reed@android.com8a1c16f2008-12-17 15:59:43 +0000268private:
269 int fHints;
270 SkScalar fClickX;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000271
reed@google.com9e39bb32011-05-18 12:17:53 +0000272 typedef SampleView INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000273};
274
275//////////////////////////////////////////////////////////////////////////////
276
277static SkView* MyFactory() { return new TextSpeedView; }
278static SkViewRegister reg(MyFactory);