blob: 8720c179c3799cb5598f6338f9b7f7e6f66477e8 [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
Ben Wagnerb2c4ea62018-08-08 11:36:17 -04008#include "Sample.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +00009#include "SkCanvas.h"
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000010#include "SkReadBuffer.h"
11#include "SkWriteBuffer.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000012#include "SkGradientShader.h"
13#include "SkGraphics.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000014#include "SkPath.h"
15#include "SkRandom.h"
16#include "SkRegion.h"
17#include "SkShader.h"
Hal Canaryea60b952018-08-21 11:45:46 -040018#include "SkUTF.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000019#include "SkColorPriv.h"
20#include "SkColorFilter.h"
21#include "SkTime.h"
22#include "SkTypeface.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000023#include "SkStream.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000024
reed@android.com8a1c16f2008-12-17 15:59:43 +000025static const struct {
26 const char* fName;
27 uint32_t fFlags;
28 bool fFlushCache;
29} gHints[] = {
30 { "Linear", SkPaint::kLinearText_Flag, false },
31 { "Normal", 0, true },
32 { "Subpixel", SkPaint::kSubpixelText_Flag, true }
33};
34
commit-bot@chromium.orge003aec2014-05-12 13:35:55 +000035static void DrawTheText(SkCanvas* canvas, const char text[], size_t length, SkScalar x, SkScalar y,
36 const SkPaint& paint, SkScalar clickX) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000037 SkPaint p(paint);
38
39#if 0
40 canvas->drawText(text, length, x, y, paint);
41#else
42 {
43 SkPoint pts[1000];
44 SkScalar xpos = x;
45 SkASSERT(length <= SK_ARRAY_COUNT(pts));
reed@google.com9e39bb32011-05-18 12:17:53 +000046 for (size_t i = 0; i < length; i++) {
Brian Osman16adfa32016-10-18 14:42:44 -040047 pts[i].set(xpos, y);
48 xpos += paint.getTextSize();
reed@google.com9e39bb32011-05-18 12:17:53 +000049 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000050 canvas->drawPosText(text, length, pts, paint);
51 }
52#endif
53
54 p.setSubpixelText(true);
55 x += SkIntToScalar(180);
56 canvas->drawText(text, length, x, y, p);
57
reed@android.com8a1c16f2008-12-17 15:59:43 +000058#ifdef SK_DEBUG
reed@google.com9e39bb32011-05-18 12:17:53 +000059 if (true) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000060 p.setSubpixelText(false);
61 p.setLinearText(true);
62 x += SkIntToScalar(180);
63 canvas->drawText(text, length, x, y, p);
64 }
65#endif
66}
67
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040068class TextSpeedView : public Sample {
reed@android.com8a1c16f2008-12-17 15:59:43 +000069public:
rmistry@google.comae933ce2012-08-23 18:19:56 +000070 TextSpeedView() {
reed@android.com8a1c16f2008-12-17 15:59:43 +000071 fHints = 0;
reed@android.com8a1c16f2008-12-17 15:59:43 +000072 fClickX = 0;
reed@android.com8a1c16f2008-12-17 15:59:43 +000073 }
reed@google.com82065d62011-02-07 15:30:46 +000074
reed@android.com8a1c16f2008-12-17 15:59:43 +000075protected:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040076 bool onQuery(Sample::Event* evt) override {
77 if (Sample::TitleQ(*evt)) {
78 Sample::TitleR(evt, "Text");
reed@android.com8a1c16f2008-12-17 15:59:43 +000079 return true;
80 }
81 return this->INHERITED::onQuery(evt);
82 }
reed@google.com82065d62011-02-07 15:30:46 +000083
reed@google.com9e39bb32011-05-18 12:17:53 +000084 static void make_textstrip(SkBitmap* bm) {
commit-bot@chromium.orga8c18312014-02-17 02:55:57 +000085 bm->allocPixels(SkImageInfo::Make(200, 18, kRGB_565_SkColorType,
86 kOpaque_SkAlphaType));
reed@android.com8a1c16f2008-12-17 15:59:43 +000087 bm->eraseColor(SK_ColorWHITE);
reed@google.com82065d62011-02-07 15:30:46 +000088
reed@android.com8a1c16f2008-12-17 15:59:43 +000089 SkCanvas canvas(*bm);
90 SkPaint paint;
91 const char* s = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit";
reed@google.com82065d62011-02-07 15:30:46 +000092
Herb Derbyfcac00f2018-05-01 11:57:56 -040093 paint.setFlags(paint.getFlags() | SkPaint::kAntiAlias_Flag);
reed@android.com8a1c16f2008-12-17 15:59:43 +000094 paint.setTextSize(SkIntToScalar(14));
Cary Clark2a475ea2017-04-28 15:35:12 -040095 canvas.drawString(s, SkIntToScalar(8), SkIntToScalar(14), paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +000096 }
reed@google.com82065d62011-02-07 15:30:46 +000097
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000098 static void fill_pts(SkPoint pts[], size_t n, SkRandom* rand) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000099 for (size_t i = 0; i < n; i++)
100 pts[i].set(rand->nextUScalar1() * 640, rand->nextUScalar1() * 480);
101 }
reed@google.com82065d62011-02-07 15:30:46 +0000102
mtklein36352bf2015-03-25 18:17:31 -0700103 void onDrawContent(SkCanvas* canvas) override {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000104 SkAutoCanvasRestore restore(canvas, false);
105 {
106 SkRect r;
107 r.set(0, 0, SkIntToScalar(1000), SkIntToScalar(20));
halcanary96fcdcc2015-08-27 07:41:13 -0700108 // canvas->saveLayer(&r, nullptr, SkCanvas::kHasAlphaLayer_SaveFlag);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000109 }
110
111 SkPaint paint;
112// const uint16_t glyphs[] = { 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19 };
113 int index = fHints % SK_ARRAY_COUNT(gHints);
114 index = 1;
115// const char* style = gHints[index].fName;
reed@google.com82065d62011-02-07 15:30:46 +0000116
reed@android.com8a1c16f2008-12-17 15:59:43 +0000117// canvas->translate(0, SkIntToScalar(50));
118
Cary Clark2a475ea2017-04-28 15:35:12 -0400119 // canvas->drawString(style, SkIntToScalar(20), SkIntToScalar(20), paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000120
bungeman13b9c952016-05-12 10:09:30 -0700121 paint.setTypeface(SkTypeface::MakeFromFile("/skimages/samplefont.ttf"));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000122 paint.setAntiAlias(true);
123 paint.setFlags(paint.getFlags() | gHints[index].fFlags);
reed@google.com82065d62011-02-07 15:30:46 +0000124
reed@android.com8a1c16f2008-12-17 15:59:43 +0000125 SkRect clip;
126 clip.set(SkIntToScalar(25), SkIntToScalar(34), SkIntToScalar(88), SkIntToScalar(155));
reed@google.com82065d62011-02-07 15:30:46 +0000127
reed@android.com8a1c16f2008-12-17 15:59:43 +0000128 const char* text = "Hamburgefons";
129 size_t length = strlen(text);
130
reed@google.com2f3dc9d2011-05-02 17:33:45 +0000131 SkScalar y = SkIntToScalar(0);
132 for (int i = 9; i <= 24; i++) {
133 paint.setTextSize(SkIntToScalar(i) /*+ (gRand.nextU() & 0xFFFF)*/);
reed@google.com9e39bb32011-05-18 12:17:53 +0000134 for (SkScalar dx = 0; dx <= SkIntToScalar(3)/4;
135 dx += SkIntToScalar(1) /* /4 */) {
reed@google.com2f3dc9d2011-05-02 17:33:45 +0000136 y += paint.getFontSpacing();
commit-bot@chromium.orge003aec2014-05-12 13:35:55 +0000137 DrawTheText(canvas, text, length, SkIntToScalar(20) + dx, y, paint, fClickX);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000138 }
139 }
reed@google.com2f3dc9d2011-05-02 17:33:45 +0000140 if (gHints[index].fFlushCache) {
141// SkGraphics::SetFontCacheUsed(0);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000142 }
143 }
reed@google.com82065d62011-02-07 15:30:46 +0000144
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400145 virtual Sample::Click* onFindClickHandler(SkScalar x, SkScalar y,
mtklein36352bf2015-03-25 18:17:31 -0700146 unsigned modi) override {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000147 fClickX = x;
reed@google.com4d5c26d2013-01-08 16:17:50 +0000148 return this->INHERITED::onFindClickHandler(x, y, modi);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000149 }
reed@google.com82065d62011-02-07 15:30:46 +0000150
mtklein36352bf2015-03-25 18:17:31 -0700151 bool onClick(Click* click) override {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000152 return this->INHERITED::onClick(click);
153 }
reed@google.com82065d62011-02-07 15:30:46 +0000154
reed@android.com8a1c16f2008-12-17 15:59:43 +0000155private:
156 int fHints;
157 SkScalar fClickX;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000158
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400159 typedef Sample INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000160};
161
162//////////////////////////////////////////////////////////////////////////////
163
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400164DEF_SAMPLE( return new TextSpeedView(); )