2 * Copyright 2011 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #include "SampleCode.h"
11 #include "SkReadBuffer.h"
12 #include "SkWriteBuffer.h"
13 #include "SkGradientShader.h"
14 #include "SkGraphics.h"
15 #include "SkImageDecoder.h"
21 #include "SkColorPriv.h"
22 #include "SkColorFilter.h"
24 #include "SkTypeface.h"
25 #include "SkXfermode.h"
28 #include "SkXMLParser.h"
30 static void test_breakText() {
32 const char* text = "sdfkljAKLDFJKEWkldfjlk#$%&sdfs.dsj";
33 size_t length = strlen(text);
34 SkScalar width = paint.measureText(text, length);
38 for (SkScalar w = 0; w <= width; w += SK_Scalar1) {
40 size_t n = paint.breakText(text, length, w, &m);
42 SkASSERT(n <= length);
48 // now assert that we're monotonic
56 nn = SkIntToScalar((unsigned int)n);
60 SkDEBUGCODE(size_t length2 =) paint.breakText(text, length, width, &mm);
61 SkASSERT(length2 == length);
62 SkASSERT(mm == width);
70 { "Linear", SkPaint::kLinearText_Flag, false },
71 { "Normal", 0, true },
72 { "Subpixel", SkPaint::kSubpixelText_Flag, true }
75 static void DrawTheText(SkCanvas* canvas, const char text[], size_t length, SkScalar x, SkScalar y,
76 const SkPaint& paint, SkScalar clickX) {
80 canvas->drawText(text, length, x, y, paint);
85 SkASSERT(length <= SK_ARRAY_COUNT(pts));
86 for (size_t i = 0; i < length; i++) {
87 pts[i].set(xpos, y), xpos += paint.getTextSize();
89 canvas->drawPosText(text, length, pts, paint);
93 p.setSubpixelText(true);
94 x += SkIntToScalar(180);
95 canvas->drawText(text, length, x, y, p);
99 p.setSubpixelText(false);
100 p.setLinearText(true);
101 x += SkIntToScalar(180);
102 canvas->drawText(text, length, x, y, p);
107 class TextSpeedView : public SampleView {
117 // overrides from SkEventSink
118 bool onQuery(SkEvent* evt) override {
119 if (SampleCode::TitleQ(*evt)) {
120 SampleCode::TitleR(evt, "Text");
123 return this->INHERITED::onQuery(evt);
126 static void make_textstrip(SkBitmap* bm) {
127 bm->allocPixels(SkImageInfo::Make(200, 18, kRGB_565_SkColorType,
128 kOpaque_SkAlphaType));
129 bm->eraseColor(SK_ColorWHITE);
131 SkCanvas canvas(*bm);
133 const char* s = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit";
135 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);
141 static void fill_pts(SkPoint pts[], size_t n, SkRandom* rand) {
142 for (size_t i = 0; i < n; i++)
143 pts[i].set(rand->nextUScalar1() * 640, rand->nextUScalar1() * 480);
146 void onDrawContent(SkCanvas* canvas) override {
147 SkAutoCanvasRestore restore(canvas, false);
150 r.set(0, 0, SkIntToScalar(1000), SkIntToScalar(20));
151 // canvas->saveLayer(&r, NULL, SkCanvas::kHasAlphaLayer_SaveFlag);
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);
158 // const char* style = gHints[index].fName;
160 // canvas->translate(0, SkIntToScalar(50));
162 // canvas->drawText(style, strlen(style), SkIntToScalar(20), SkIntToScalar(20), paint);
164 SkSafeUnref(paint.setTypeface(SkTypeface::CreateFromFile("/skimages/samplefont.ttf")));
165 paint.setAntiAlias(true);
166 paint.setFlags(paint.getFlags() | gHints[index].fFlags);
169 clip.set(SkIntToScalar(25), SkIntToScalar(34), SkIntToScalar(88), SkIntToScalar(155));
171 const char* text = "Hamburgefons";
172 size_t length = strlen(text);
174 SkScalar y = SkIntToScalar(0);
175 for (int i = 9; i <= 24; i++) {
176 paint.setTextSize(SkIntToScalar(i) /*+ (gRand.nextU() & 0xFFFF)*/);
177 for (SkScalar dx = 0; dx <= SkIntToScalar(3)/4;
178 dx += SkIntToScalar(1) /* /4 */) {
179 y += paint.getFontSpacing();
180 DrawTheText(canvas, text, length, SkIntToScalar(20) + dx, y, paint, fClickX);
183 if (gHints[index].fFlushCache) {
184 // SkGraphics::SetFontCacheUsed(0);
188 virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y,
189 unsigned modi) override {
192 return this->INHERITED::onFindClickHandler(x, y, modi);
195 bool onClick(Click* click) override {
196 return this->INHERITED::onClick(click);
203 typedef SampleView INHERITED;
206 //////////////////////////////////////////////////////////////////////////////
208 static SkView* MyFactory() { return new TextSpeedView; }
209 static SkViewRegister reg(MyFactory);