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