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