blob: 112573e96764481cbc5a2cd88c2c4a09b6cef3ed [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;
reed9e96aa02014-10-03 12:44:37 -070040 size_t n = paint.breakText(text, length, w, &m);
reed@google.com82065d62011-02-07 15:30:46 +000041
reed@android.com8a1c16f2008-12-17 15:59:43 +000042 SkASSERT(n <= length);
43 SkASSERT(m <= width);
reed@google.com82065d62011-02-07 15:30:46 +000044
reed@google.com9e39bb32011-05-18 12:17:53 +000045 if (n == 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000046 SkASSERT(m == 0);
reed@google.com9e39bb32011-05-18 12:17:53 +000047 } else {
reed@android.com8a1c16f2008-12-17 15:59:43 +000048 // now assert that we're monotonic
reed@google.com9e39bb32011-05-18 12:17:53 +000049 if (n == nn) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000050 SkASSERT(m == mm);
reed@google.com9e39bb32011-05-18 12:17:53 +000051 } else {
reed@android.com8a1c16f2008-12-17 15:59:43 +000052 SkASSERT(n > nn);
53 SkASSERT(m > mm);
54 }
55 }
tomhudson@google.comffe39bd2012-05-17 15:38:00 +000056 nn = SkIntToScalar((unsigned int)n);
reed@android.com8a1c16f2008-12-17 15:59:43 +000057 mm = m;
58 }
59
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +000060 SkDEBUGCODE(size_t length2 =) paint.breakText(text, length, width, &mm);
reed@google.com261b8e22011-04-14 17:53:24 +000061 SkASSERT(length2 == length);
reed@android.com8a1c16f2008-12-17 15:59:43 +000062 SkASSERT(mm == width);
63}
64
reed@android.com8a1c16f2008-12-17 15:59:43 +000065static const struct {
66 const char* fName;
67 uint32_t fFlags;
68 bool fFlushCache;
69} gHints[] = {
70 { "Linear", SkPaint::kLinearText_Flag, false },
71 { "Normal", 0, true },
72 { "Subpixel", SkPaint::kSubpixelText_Flag, true }
73};
74
commit-bot@chromium.orge003aec2014-05-12 13:35:55 +000075static void DrawTheText(SkCanvas* canvas, const char text[], size_t length, SkScalar x, SkScalar y,
76 const SkPaint& paint, SkScalar clickX) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000077 SkPaint p(paint);
78
79#if 0
80 canvas->drawText(text, length, x, y, paint);
81#else
82 {
83 SkPoint pts[1000];
84 SkScalar xpos = x;
85 SkASSERT(length <= SK_ARRAY_COUNT(pts));
reed@google.com9e39bb32011-05-18 12:17:53 +000086 for (size_t i = 0; i < length; i++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000087 pts[i].set(xpos, y), xpos += paint.getTextSize();
reed@google.com9e39bb32011-05-18 12:17:53 +000088 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000089 canvas->drawPosText(text, length, pts, paint);
90 }
91#endif
92
93 p.setSubpixelText(true);
94 x += SkIntToScalar(180);
95 canvas->drawText(text, length, x, y, p);
96
reed@android.com8a1c16f2008-12-17 15:59:43 +000097#ifdef SK_DEBUG
reed@google.com9e39bb32011-05-18 12:17:53 +000098 if (true) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000099 p.setSubpixelText(false);
100 p.setLinearText(true);
101 x += SkIntToScalar(180);
102 canvas->drawText(text, length, x, y, p);
103 }
104#endif
105}
106
reed@google.com9e39bb32011-05-18 12:17:53 +0000107class TextSpeedView : public SampleView {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000108public:
rmistry@google.comae933ce2012-08-23 18:19:56 +0000109 TextSpeedView() {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000110 fHints = 0;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000111 fClickX = 0;
112
reed@google.com82065d62011-02-07 15:30:46 +0000113 test_breakText();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000114 }
reed@google.com82065d62011-02-07 15:30:46 +0000115
reed@android.com8a1c16f2008-12-17 15:59:43 +0000116protected:
117 // overrides from SkEventSink
mtklein72c9faa2015-01-09 10:06:39 -0800118 bool onQuery(SkEvent* evt) SK_OVERRIDE {
reed@google.com9e39bb32011-05-18 12:17:53 +0000119 if (SampleCode::TitleQ(*evt)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000120 SampleCode::TitleR(evt, "Text");
121 return true;
122 }
123 return this->INHERITED::onQuery(evt);
124 }
reed@google.com82065d62011-02-07 15:30:46 +0000125
reed@google.com9e39bb32011-05-18 12:17:53 +0000126 static void make_textstrip(SkBitmap* bm) {
commit-bot@chromium.orga8c18312014-02-17 02:55:57 +0000127 bm->allocPixels(SkImageInfo::Make(200, 18, kRGB_565_SkColorType,
128 kOpaque_SkAlphaType));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000129 bm->eraseColor(SK_ColorWHITE);
reed@google.com82065d62011-02-07 15:30:46 +0000130
reed@android.com8a1c16f2008-12-17 15:59:43 +0000131 SkCanvas canvas(*bm);
132 SkPaint paint;
133 const char* s = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit";
reed@google.com82065d62011-02-07 15:30:46 +0000134
reed@android.com8a1c16f2008-12-17 15:59:43 +0000135 paint.setFlags(paint.getFlags() | SkPaint::kAntiAlias_Flag
136 | SkPaint::kDevKernText_Flag);
137 paint.setTextSize(SkIntToScalar(14));
138 canvas.drawText(s, strlen(s), SkIntToScalar(8), SkIntToScalar(14), paint);
139 }
reed@google.com82065d62011-02-07 15:30:46 +0000140
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000141 static void fill_pts(SkPoint pts[], size_t n, SkRandom* rand) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000142 for (size_t i = 0; i < n; i++)
143 pts[i].set(rand->nextUScalar1() * 640, rand->nextUScalar1() * 480);
144 }
reed@google.com82065d62011-02-07 15:30:46 +0000145
mtklein72c9faa2015-01-09 10:06:39 -0800146 void onDrawContent(SkCanvas* canvas) SK_OVERRIDE {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000147 SkAutoCanvasRestore restore(canvas, false);
148 {
149 SkRect r;
150 r.set(0, 0, SkIntToScalar(1000), SkIntToScalar(20));
151 // canvas->saveLayer(&r, NULL, SkCanvas::kHasAlphaLayer_SaveFlag);
152 }
153
154 SkPaint paint;
155// const uint16_t glyphs[] = { 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19 };
156 int index = fHints % SK_ARRAY_COUNT(gHints);
157 index = 1;
158// const char* style = gHints[index].fName;
reed@google.com82065d62011-02-07 15:30:46 +0000159
reed@android.com8a1c16f2008-12-17 15:59:43 +0000160// canvas->translate(0, SkIntToScalar(50));
161
162 // canvas->drawText(style, strlen(style), SkIntToScalar(20), SkIntToScalar(20), paint);
163
reed@android.com04d86c62010-01-25 22:02:44 +0000164 SkSafeUnref(paint.setTypeface(SkTypeface::CreateFromFile("/skimages/samplefont.ttf")));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000165 paint.setAntiAlias(true);
166 paint.setFlags(paint.getFlags() | gHints[index].fFlags);
reed@google.com82065d62011-02-07 15:30:46 +0000167
reed@android.com8a1c16f2008-12-17 15:59:43 +0000168 SkRect clip;
169 clip.set(SkIntToScalar(25), SkIntToScalar(34), SkIntToScalar(88), SkIntToScalar(155));
reed@google.com82065d62011-02-07 15:30:46 +0000170
reed@android.com8a1c16f2008-12-17 15:59:43 +0000171 const char* text = "Hamburgefons";
172 size_t length = strlen(text);
173
reed@google.com2f3dc9d2011-05-02 17:33:45 +0000174 SkScalar y = SkIntToScalar(0);
175 for (int i = 9; i <= 24; i++) {
176 paint.setTextSize(SkIntToScalar(i) /*+ (gRand.nextU() & 0xFFFF)*/);
reed@google.com9e39bb32011-05-18 12:17:53 +0000177 for (SkScalar dx = 0; dx <= SkIntToScalar(3)/4;
178 dx += SkIntToScalar(1) /* /4 */) {
reed@google.com2f3dc9d2011-05-02 17:33:45 +0000179 y += paint.getFontSpacing();
commit-bot@chromium.orge003aec2014-05-12 13:35:55 +0000180 DrawTheText(canvas, text, length, SkIntToScalar(20) + dx, y, paint, fClickX);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000181 }
182 }
reed@google.com2f3dc9d2011-05-02 17:33:45 +0000183 if (gHints[index].fFlushCache) {
184// SkGraphics::SetFontCacheUsed(0);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000185 }
186 }
reed@google.com82065d62011-02-07 15:30:46 +0000187
reed@google.com4d5c26d2013-01-08 16:17:50 +0000188 virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y,
189 unsigned modi) SK_OVERRIDE {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000190 fClickX = x;
191 this->inval(NULL);
reed@google.com4d5c26d2013-01-08 16:17:50 +0000192 return this->INHERITED::onFindClickHandler(x, y, modi);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000193 }
reed@google.com82065d62011-02-07 15:30:46 +0000194
mtklein72c9faa2015-01-09 10:06:39 -0800195 bool onClick(Click* click) SK_OVERRIDE {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000196 return this->INHERITED::onClick(click);
197 }
reed@google.com82065d62011-02-07 15:30:46 +0000198
reed@android.com8a1c16f2008-12-17 15:59:43 +0000199private:
200 int fHints;
201 SkScalar fClickX;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000202
reed@google.com9e39bb32011-05-18 12:17:53 +0000203 typedef SampleView INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000204};
205
206//////////////////////////////////////////////////////////////////////////////
207
208static SkView* MyFactory() { return new TextSpeedView; }
209static SkViewRegister reg(MyFactory);