1 /*
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 */
7
8 #include "SampleCode.h"
9 #include "SkView.h"
10 #include "SkCanvas.h"
11 #include "SkReadBuffer.h"
12 #include "SkWriteBuffer.h"
13 #include "SkGradientShader.h"
14 #include "SkGraphics.h"
15 #include "SkImageDecoder.h"
16 #include "SkPath.h"
17 #include "SkRandom.h"
18 #include "SkRegion.h"
19 #include "SkShader.h"
20 #include "SkUtils.h"
21 #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
30 static const struct {
31 const char* fName;
32 uint32_t fFlags;
33 bool fFlushCache;
34 } gHints[] = {
35 { "Linear", SkPaint::kLinearText_Flag, false },
36 { "Normal", 0, true },
37 { "Subpixel", SkPaint::kSubpixelText_Flag, true }
38 };
39
DrawTheText(SkCanvas * canvas,const char text[],size_t length,SkScalar x,SkScalar y,const SkPaint & paint,SkScalar clickX)40 static void DrawTheText(SkCanvas* canvas, const char text[], size_t length, SkScalar x, SkScalar y,
41 const SkPaint& paint, SkScalar clickX) {
42 SkPaint p(paint);
43
44 #if 0
45 canvas->drawText(text, length, x, y, paint);
46 #else
47 {
48 SkPoint pts[1000];
49 SkScalar xpos = x;
50 SkASSERT(length <= SK_ARRAY_COUNT(pts));
51 for (size_t i = 0; i < length; i++) {
52 pts[i].set(xpos, y), xpos += paint.getTextSize();
53 }
54 canvas->drawPosText(text, length, pts, paint);
55 }
56 #endif
57
58 p.setSubpixelText(true);
59 x += SkIntToScalar(180);
60 canvas->drawText(text, length, x, y, p);
61
62 #ifdef SK_DEBUG
63 if (true) {
64 p.setSubpixelText(false);
65 p.setLinearText(true);
66 x += SkIntToScalar(180);
67 canvas->drawText(text, length, x, y, p);
68 }
69 #endif
70 }
71
72 class TextSpeedView : public SampleView {
73 public:
TextSpeedView()74 TextSpeedView() {
75 fHints = 0;
76 fClickX = 0;
77 }
78
79 protected:
80 // overrides from SkEventSink
onQuery(SkEvent * evt)81 bool onQuery(SkEvent* evt) override {
82 if (SampleCode::TitleQ(*evt)) {
83 SampleCode::TitleR(evt, "Text");
84 return true;
85 }
86 return this->INHERITED::onQuery(evt);
87 }
88
make_textstrip(SkBitmap * bm)89 static void make_textstrip(SkBitmap* bm) {
90 bm->allocPixels(SkImageInfo::Make(200, 18, kRGB_565_SkColorType,
91 kOpaque_SkAlphaType));
92 bm->eraseColor(SK_ColorWHITE);
93
94 SkCanvas canvas(*bm);
95 SkPaint paint;
96 const char* s = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit";
97
98 paint.setFlags(paint.getFlags() | SkPaint::kAntiAlias_Flag
99 | SkPaint::kDevKernText_Flag);
100 paint.setTextSize(SkIntToScalar(14));
101 canvas.drawText(s, strlen(s), SkIntToScalar(8), SkIntToScalar(14), paint);
102 }
103
fill_pts(SkPoint pts[],size_t n,SkRandom * rand)104 static void fill_pts(SkPoint pts[], size_t n, SkRandom* rand) {
105 for (size_t i = 0; i < n; i++)
106 pts[i].set(rand->nextUScalar1() * 640, rand->nextUScalar1() * 480);
107 }
108
onDrawContent(SkCanvas * canvas)109 void onDrawContent(SkCanvas* canvas) override {
110 SkAutoCanvasRestore restore(canvas, false);
111 {
112 SkRect r;
113 r.set(0, 0, SkIntToScalar(1000), SkIntToScalar(20));
114 // canvas->saveLayer(&r, nullptr, SkCanvas::kHasAlphaLayer_SaveFlag);
115 }
116
117 SkPaint paint;
118 // const uint16_t glyphs[] = { 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19 };
119 int index = fHints % SK_ARRAY_COUNT(gHints);
120 index = 1;
121 // const char* style = gHints[index].fName;
122
123 // canvas->translate(0, SkIntToScalar(50));
124
125 // canvas->drawText(style, strlen(style), SkIntToScalar(20), SkIntToScalar(20), paint);
126
127 SkSafeUnref(paint.setTypeface(SkTypeface::CreateFromFile("/skimages/samplefont.ttf")));
128 paint.setAntiAlias(true);
129 paint.setFlags(paint.getFlags() | gHints[index].fFlags);
130
131 SkRect clip;
132 clip.set(SkIntToScalar(25), SkIntToScalar(34), SkIntToScalar(88), SkIntToScalar(155));
133
134 const char* text = "Hamburgefons";
135 size_t length = strlen(text);
136
137 SkScalar y = SkIntToScalar(0);
138 for (int i = 9; i <= 24; i++) {
139 paint.setTextSize(SkIntToScalar(i) /*+ (gRand.nextU() & 0xFFFF)*/);
140 for (SkScalar dx = 0; dx <= SkIntToScalar(3)/4;
141 dx += SkIntToScalar(1) /* /4 */) {
142 y += paint.getFontSpacing();
143 DrawTheText(canvas, text, length, SkIntToScalar(20) + dx, y, paint, fClickX);
144 }
145 }
146 if (gHints[index].fFlushCache) {
147 // SkGraphics::SetFontCacheUsed(0);
148 }
149 }
150
onFindClickHandler(SkScalar x,SkScalar y,unsigned modi)151 virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y,
152 unsigned modi) override {
153 fClickX = x;
154 this->inval(nullptr);
155 return this->INHERITED::onFindClickHandler(x, y, modi);
156 }
157
onClick(Click * click)158 bool onClick(Click* click) override {
159 return this->INHERITED::onClick(click);
160 }
161
162 private:
163 int fHints;
164 SkScalar fClickX;
165
166 typedef SampleView INHERITED;
167 };
168
169 //////////////////////////////////////////////////////////////////////////////
170
MyFactory()171 static SkView* MyFactory() { return new TextSpeedView; }
172 static SkViewRegister reg(MyFactory);
173