blob: 1b67103ffdb5b354346cbfe4d9fd6dcf184f7995 [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"
reed@android.com8a1c16f2008-12-17 15:59:43 +000015#include "SkPath.h"
16#include "SkRandom.h"
17#include "SkRegion.h"
18#include "SkShader.h"
19#include "SkUtils.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000020#include "SkColorPriv.h"
21#include "SkColorFilter.h"
22#include "SkTime.h"
23#include "SkTypeface.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000024#include "SkStream.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000025
reed@android.com8a1c16f2008-12-17 15:59:43 +000026static 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
commit-bot@chromium.orge003aec2014-05-12 13:35:55 +000036static void DrawTheText(SkCanvas* canvas, const char text[], size_t length, SkScalar x, SkScalar y,
37 const SkPaint& paint, SkScalar clickX) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000038 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));
reed@google.com9e39bb32011-05-18 12:17:53 +000047 for (size_t i = 0; i < length; i++) {
Brian Osman16adfa32016-10-18 14:42:44 -040048 pts[i].set(xpos, y);
49 xpos += paint.getTextSize();
reed@google.com9e39bb32011-05-18 12:17:53 +000050 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000051 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
reed@android.com8a1c16f2008-12-17 15:59:43 +000059#ifdef SK_DEBUG
reed@google.com9e39bb32011-05-18 12:17:53 +000060 if (true) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000061 p.setSubpixelText(false);
62 p.setLinearText(true);
63 x += SkIntToScalar(180);
64 canvas->drawText(text, length, x, y, p);
65 }
66#endif
67}
68
reed@google.com9e39bb32011-05-18 12:17:53 +000069class TextSpeedView : public SampleView {
reed@android.com8a1c16f2008-12-17 15:59:43 +000070public:
rmistry@google.comae933ce2012-08-23 18:19:56 +000071 TextSpeedView() {
reed@android.com8a1c16f2008-12-17 15:59:43 +000072 fHints = 0;
reed@android.com8a1c16f2008-12-17 15:59:43 +000073 fClickX = 0;
reed@android.com8a1c16f2008-12-17 15:59:43 +000074 }
reed@google.com82065d62011-02-07 15:30:46 +000075
reed@android.com8a1c16f2008-12-17 15:59:43 +000076protected:
77 // overrides from SkEventSink
mtklein36352bf2015-03-25 18:17:31 -070078 bool onQuery(SkEvent* evt) override {
reed@google.com9e39bb32011-05-18 12:17:53 +000079 if (SampleCode::TitleQ(*evt)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000080 SampleCode::TitleR(evt, "Text");
81 return true;
82 }
83 return this->INHERITED::onQuery(evt);
84 }
reed@google.com82065d62011-02-07 15:30:46 +000085
reed@google.com9e39bb32011-05-18 12:17:53 +000086 static void make_textstrip(SkBitmap* bm) {
commit-bot@chromium.orga8c18312014-02-17 02:55:57 +000087 bm->allocPixels(SkImageInfo::Make(200, 18, kRGB_565_SkColorType,
88 kOpaque_SkAlphaType));
reed@android.com8a1c16f2008-12-17 15:59:43 +000089 bm->eraseColor(SK_ColorWHITE);
reed@google.com82065d62011-02-07 15:30:46 +000090
reed@android.com8a1c16f2008-12-17 15:59:43 +000091 SkCanvas canvas(*bm);
92 SkPaint paint;
93 const char* s = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit";
reed@google.com82065d62011-02-07 15:30:46 +000094
Herb Derbyfcac00f2018-05-01 11:57:56 -040095 paint.setFlags(paint.getFlags() | SkPaint::kAntiAlias_Flag);
reed@android.com8a1c16f2008-12-17 15:59:43 +000096 paint.setTextSize(SkIntToScalar(14));
Cary Clark2a475ea2017-04-28 15:35:12 -040097 canvas.drawString(s, SkIntToScalar(8), SkIntToScalar(14), paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +000098 }
reed@google.com82065d62011-02-07 15:30:46 +000099
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000100 static void fill_pts(SkPoint pts[], size_t n, SkRandom* rand) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000101 for (size_t i = 0; i < n; i++)
102 pts[i].set(rand->nextUScalar1() * 640, rand->nextUScalar1() * 480);
103 }
reed@google.com82065d62011-02-07 15:30:46 +0000104
mtklein36352bf2015-03-25 18:17:31 -0700105 void onDrawContent(SkCanvas* canvas) override {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000106 SkAutoCanvasRestore restore(canvas, false);
107 {
108 SkRect r;
109 r.set(0, 0, SkIntToScalar(1000), SkIntToScalar(20));
halcanary96fcdcc2015-08-27 07:41:13 -0700110 // canvas->saveLayer(&r, nullptr, SkCanvas::kHasAlphaLayer_SaveFlag);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000111 }
112
113 SkPaint paint;
114// const uint16_t glyphs[] = { 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19 };
115 int index = fHints % SK_ARRAY_COUNT(gHints);
116 index = 1;
117// const char* style = gHints[index].fName;
reed@google.com82065d62011-02-07 15:30:46 +0000118
reed@android.com8a1c16f2008-12-17 15:59:43 +0000119// canvas->translate(0, SkIntToScalar(50));
120
Cary Clark2a475ea2017-04-28 15:35:12 -0400121 // canvas->drawString(style, SkIntToScalar(20), SkIntToScalar(20), paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000122
bungeman13b9c952016-05-12 10:09:30 -0700123 paint.setTypeface(SkTypeface::MakeFromFile("/skimages/samplefont.ttf"));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000124 paint.setAntiAlias(true);
125 paint.setFlags(paint.getFlags() | gHints[index].fFlags);
reed@google.com82065d62011-02-07 15:30:46 +0000126
reed@android.com8a1c16f2008-12-17 15:59:43 +0000127 SkRect clip;
128 clip.set(SkIntToScalar(25), SkIntToScalar(34), SkIntToScalar(88), SkIntToScalar(155));
reed@google.com82065d62011-02-07 15:30:46 +0000129
reed@android.com8a1c16f2008-12-17 15:59:43 +0000130 const char* text = "Hamburgefons";
131 size_t length = strlen(text);
132
reed@google.com2f3dc9d2011-05-02 17:33:45 +0000133 SkScalar y = SkIntToScalar(0);
134 for (int i = 9; i <= 24; i++) {
135 paint.setTextSize(SkIntToScalar(i) /*+ (gRand.nextU() & 0xFFFF)*/);
reed@google.com9e39bb32011-05-18 12:17:53 +0000136 for (SkScalar dx = 0; dx <= SkIntToScalar(3)/4;
137 dx += SkIntToScalar(1) /* /4 */) {
reed@google.com2f3dc9d2011-05-02 17:33:45 +0000138 y += paint.getFontSpacing();
commit-bot@chromium.orge003aec2014-05-12 13:35:55 +0000139 DrawTheText(canvas, text, length, SkIntToScalar(20) + dx, y, paint, fClickX);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000140 }
141 }
reed@google.com2f3dc9d2011-05-02 17:33:45 +0000142 if (gHints[index].fFlushCache) {
143// SkGraphics::SetFontCacheUsed(0);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000144 }
145 }
reed@google.com82065d62011-02-07 15:30:46 +0000146
reed@google.com4d5c26d2013-01-08 16:17:50 +0000147 virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y,
mtklein36352bf2015-03-25 18:17:31 -0700148 unsigned modi) override {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000149 fClickX = x;
reed@google.com4d5c26d2013-01-08 16:17:50 +0000150 return this->INHERITED::onFindClickHandler(x, y, modi);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000151 }
reed@google.com82065d62011-02-07 15:30:46 +0000152
mtklein36352bf2015-03-25 18:17:31 -0700153 bool onClick(Click* click) override {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000154 return this->INHERITED::onClick(click);
155 }
reed@google.com82065d62011-02-07 15:30:46 +0000156
reed@android.com8a1c16f2008-12-17 15:59:43 +0000157private:
158 int fHints;
159 SkScalar fClickX;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000160
reed@google.com9e39bb32011-05-18 12:17:53 +0000161 typedef SampleView INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000162};
163
164//////////////////////////////////////////////////////////////////////////////
165
166static SkView* MyFactory() { return new TextSpeedView; }
167static SkViewRegister reg(MyFactory);