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