epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2011 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 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" |
| 11 | #include "Sk64.h" |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 12 | #include "SkFlattenableBuffers.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" |
| 16 | #include "SkKernel33MaskFilter.h" |
| 17 | #include "SkPath.h" |
| 18 | #include "SkRandom.h" |
| 19 | #include "SkRegion.h" |
| 20 | #include "SkShader.h" |
| 21 | #include "SkUtils.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 22 | #include "SkColorPriv.h" |
| 23 | #include "SkColorFilter.h" |
| 24 | #include "SkTime.h" |
| 25 | #include "SkTypeface.h" |
| 26 | #include "SkXfermode.h" |
| 27 | |
| 28 | #include "SkStream.h" |
| 29 | #include "SkXMLParser.h" |
| 30 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 31 | class ReduceNoise : public SkKernel33ProcMaskFilter { |
| 32 | public: |
| 33 | ReduceNoise(int percent256) : SkKernel33ProcMaskFilter(percent256) {} |
reed@google.com | 30711b7 | 2012-12-18 19:18:39 +0000 | [diff] [blame] | 34 | virtual uint8_t computeValue(uint8_t* const* srcRows) const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 35 | int c = srcRows[1][1]; |
| 36 | int min = 255, max = 0; |
| 37 | for (int i = 0; i < 3; i++) |
| 38 | for (int j = 0; j < 3; j++) |
| 39 | if (i != 1 || j != 1) |
| 40 | { |
| 41 | int v = srcRows[i][j]; |
| 42 | if (max < v) |
| 43 | max = v; |
| 44 | if (min > v) |
| 45 | min = v; |
| 46 | } |
| 47 | if (c > max) c = max; |
| 48 | // if (c < min) c = min; |
| 49 | return c; |
| 50 | } |
robertphillips@google.com | 0bd80fa | 2013-03-18 17:53:38 +0000 | [diff] [blame] | 51 | |
| 52 | #ifdef SK_DEVELOPER |
| 53 | virtual void toString(SkString* str) const SK_OVERRIDE { |
| 54 | str->append("ReduceNoise: ("); |
| 55 | this->INHERITED::toString(str); |
| 56 | str->append(")"); |
| 57 | } |
| 58 | #endif |
| 59 | |
djsollen@google.com | ba28d03 | 2012-03-26 17:57:35 +0000 | [diff] [blame] | 60 | SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(ReduceNoise) |
| 61 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 62 | private: |
| 63 | ReduceNoise(SkFlattenableReadBuffer& rb) : SkKernel33ProcMaskFilter(rb) {} |
robertphillips@google.com | 0bd80fa | 2013-03-18 17:53:38 +0000 | [diff] [blame] | 64 | |
| 65 | typedef SkKernel33ProcMaskFilter INHERITED; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 66 | }; |
| 67 | |
| 68 | class Darken : public SkKernel33ProcMaskFilter { |
| 69 | public: |
| 70 | Darken(int percent256) : SkKernel33ProcMaskFilter(percent256) {} |
reed@google.com | 30711b7 | 2012-12-18 19:18:39 +0000 | [diff] [blame] | 71 | virtual uint8_t computeValue(uint8_t* const* srcRows) const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 72 | int c = srcRows[1][1]; |
| 73 | float f = c / 255.f; |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 74 | |
reed@google.com | 9e39bb3 | 2011-05-18 12:17:53 +0000 | [diff] [blame] | 75 | if (c >= 0) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 76 | f = sqrtf(f); |
reed@google.com | 9e39bb3 | 2011-05-18 12:17:53 +0000 | [diff] [blame] | 77 | } else { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 78 | f *= f; |
| 79 | } |
| 80 | SkASSERT(f >= 0 && f <= 1); |
| 81 | return (int)(f * 255); |
| 82 | } |
robertphillips@google.com | 0bd80fa | 2013-03-18 17:53:38 +0000 | [diff] [blame] | 83 | |
| 84 | #ifdef SK_DEVELOPER |
skia.committer@gmail.com | 8eaddb0 | 2013-03-19 07:15:10 +0000 | [diff] [blame] | 85 | virtual void toString(SkString* str) const SK_OVERRIDE { |
| 86 | str->append("Darken: ("); |
robertphillips@google.com | 0bd80fa | 2013-03-18 17:53:38 +0000 | [diff] [blame] | 87 | this->INHERITED::toString(str); |
| 88 | str->append(")"); |
| 89 | } |
| 90 | #endif |
| 91 | |
djsollen@google.com | ba28d03 | 2012-03-26 17:57:35 +0000 | [diff] [blame] | 92 | SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(Darken) |
| 93 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 94 | private: |
| 95 | Darken(SkFlattenableReadBuffer& rb) : SkKernel33ProcMaskFilter(rb) {} |
robertphillips@google.com | 0bd80fa | 2013-03-18 17:53:38 +0000 | [diff] [blame] | 96 | |
| 97 | typedef SkKernel33ProcMaskFilter INHERITED; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 98 | }; |
| 99 | |
| 100 | static SkMaskFilter* makemf() { return new Darken(0x30); } |
| 101 | |
reed@google.com | 9e39bb3 | 2011-05-18 12:17:53 +0000 | [diff] [blame] | 102 | static void test_breakText() { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 103 | SkPaint paint; |
| 104 | const char* text = "sdfkljAKLDFJKEWkldfjlk#$%&sdfs.dsj"; |
| 105 | size_t length = strlen(text); |
| 106 | SkScalar width = paint.measureText(text, length); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 107 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 108 | SkScalar mm = 0; |
| 109 | SkScalar nn = 0; |
reed@google.com | 9e39bb3 | 2011-05-18 12:17:53 +0000 | [diff] [blame] | 110 | for (SkScalar w = 0; w <= width; w += SK_Scalar1) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 111 | SkScalar m; |
| 112 | size_t n = paint.breakText(text, length, w, &m, |
| 113 | SkPaint::kBackward_TextBufferDirection); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 114 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 115 | SkASSERT(n <= length); |
| 116 | SkASSERT(m <= width); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 117 | |
reed@google.com | 9e39bb3 | 2011-05-18 12:17:53 +0000 | [diff] [blame] | 118 | if (n == 0) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 119 | SkASSERT(m == 0); |
reed@google.com | 9e39bb3 | 2011-05-18 12:17:53 +0000 | [diff] [blame] | 120 | } else { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 121 | // now assert that we're monotonic |
reed@google.com | 9e39bb3 | 2011-05-18 12:17:53 +0000 | [diff] [blame] | 122 | if (n == nn) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 123 | SkASSERT(m == mm); |
reed@google.com | 9e39bb3 | 2011-05-18 12:17:53 +0000 | [diff] [blame] | 124 | } else { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 125 | SkASSERT(n > nn); |
| 126 | SkASSERT(m > mm); |
| 127 | } |
| 128 | } |
tomhudson@google.com | ffe39bd | 2012-05-17 15:38:00 +0000 | [diff] [blame] | 129 | nn = SkIntToScalar((unsigned int)n); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 130 | mm = m; |
| 131 | } |
| 132 | |
senorblanco@chromium.org | 64cc579 | 2011-05-19 19:58:58 +0000 | [diff] [blame] | 133 | SkDEBUGCODE(size_t length2 =) paint.breakText(text, length, width, &mm); |
reed@google.com | 261b8e2 | 2011-04-14 17:53:24 +0000 | [diff] [blame] | 134 | SkASSERT(length2 == length); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 135 | SkASSERT(mm == width); |
| 136 | } |
| 137 | |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 138 | static SkRandom gRand; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 139 | |
| 140 | class SkPowerMode : public SkXfermode { |
| 141 | public: |
| 142 | SkPowerMode(SkScalar exponent) { this->init(exponent); } |
| 143 | |
reed@google.com | 9e39bb3 | 2011-05-18 12:17:53 +0000 | [diff] [blame] | 144 | virtual void xfer16(uint16_t dst[], const SkPMColor src[], int count, |
mike@reedtribe.org | 3af2b8d | 2012-12-24 18:26:43 +0000 | [diff] [blame] | 145 | const SkAlpha aa[]) const SK_OVERRIDE; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 146 | |
| 147 | typedef SkFlattenable* (*Factory)(SkFlattenableReadBuffer&); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 148 | |
robertphillips@google.com | b83b6b4 | 2013-01-22 14:32:09 +0000 | [diff] [blame] | 149 | SK_DEVELOPER_TO_STRING() |
djsollen@google.com | ba28d03 | 2012-03-26 17:57:35 +0000 | [diff] [blame] | 150 | SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkPowerMode) |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 151 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 152 | private: |
| 153 | SkScalar fExp; // user's value |
| 154 | uint8_t fTable[256]; // cache |
| 155 | |
| 156 | void init(SkScalar exponent); |
djsollen@google.com | 5492424 | 2012-03-29 15:18:04 +0000 | [diff] [blame] | 157 | SkPowerMode(SkFlattenableReadBuffer& b) : INHERITED(b) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 158 | // read the exponent |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 159 | this->init(SkFixedToScalar(b.readFixed())); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 160 | } |
djsollen@google.com | 5492424 | 2012-03-29 15:18:04 +0000 | [diff] [blame] | 161 | virtual void flatten(SkFlattenableWriteBuffer& b) const SK_OVERRIDE { |
| 162 | this->INHERITED::flatten(b); |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 163 | b.writeFixed(SkScalarToFixed(fExp)); |
djsollen@google.com | 5492424 | 2012-03-29 15:18:04 +0000 | [diff] [blame] | 164 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 165 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 166 | typedef SkXfermode INHERITED; |
| 167 | }; |
| 168 | |
reed@google.com | 9e39bb3 | 2011-05-18 12:17:53 +0000 | [diff] [blame] | 169 | void SkPowerMode::init(SkScalar e) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 170 | fExp = e; |
| 171 | float ee = SkScalarToFloat(e); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 172 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 173 | printf("------ %g\n", ee); |
reed@google.com | 9e39bb3 | 2011-05-18 12:17:53 +0000 | [diff] [blame] | 174 | for (int i = 0; i < 256; i++) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 175 | float x = i / 255.f; |
| 176 | // printf(" %d %g", i, x); |
| 177 | x = powf(x, ee); |
| 178 | // printf(" %g", x); |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 179 | int xx = SkScalarRound(x * 255); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 180 | // printf(" %d\n", xx); |
| 181 | fTable[i] = SkToU8(xx); |
| 182 | } |
| 183 | } |
| 184 | |
reed@google.com | 9e39bb3 | 2011-05-18 12:17:53 +0000 | [diff] [blame] | 185 | void SkPowerMode::xfer16(uint16_t dst[], const SkPMColor src[], int count, |
mike@reedtribe.org | 3af2b8d | 2012-12-24 18:26:43 +0000 | [diff] [blame] | 186 | const SkAlpha aa[]) const { |
reed@google.com | 9e39bb3 | 2011-05-18 12:17:53 +0000 | [diff] [blame] | 187 | for (int i = 0; i < count; i++) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 188 | SkPMColor c = src[i]; |
| 189 | int r = SkGetPackedR32(c); |
| 190 | int g = SkGetPackedG32(c); |
| 191 | int b = SkGetPackedB32(c); |
| 192 | r = fTable[r]; |
| 193 | g = fTable[g]; |
| 194 | b = fTable[b]; |
| 195 | dst[i] = SkPack888ToRGB16(r, g, b); |
| 196 | } |
| 197 | } |
| 198 | |
robertphillips@google.com | b83b6b4 | 2013-01-22 14:32:09 +0000 | [diff] [blame] | 199 | #ifdef SK_DEVELOPER |
| 200 | void SkPowerMode::toString(SkString* str) const { |
| 201 | str->append("SkPowerMode: exponent "); |
| 202 | str->appendScalar(fExp); |
| 203 | } |
| 204 | #endif |
| 205 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 206 | static const struct { |
| 207 | const char* fName; |
| 208 | uint32_t fFlags; |
| 209 | bool fFlushCache; |
| 210 | } gHints[] = { |
| 211 | { "Linear", SkPaint::kLinearText_Flag, false }, |
| 212 | { "Normal", 0, true }, |
| 213 | { "Subpixel", SkPaint::kSubpixelText_Flag, true } |
| 214 | }; |
| 215 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 216 | static void DrawTheText(SkCanvas* canvas, const char text[], size_t length, |
| 217 | SkScalar x, SkScalar y, const SkPaint& paint, |
reed@google.com | 9e39bb3 | 2011-05-18 12:17:53 +0000 | [diff] [blame] | 218 | SkScalar clickX, SkMaskFilter* mf) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 219 | SkPaint p(paint); |
| 220 | |
| 221 | #if 0 |
| 222 | canvas->drawText(text, length, x, y, paint); |
| 223 | #else |
| 224 | { |
| 225 | SkPoint pts[1000]; |
| 226 | SkScalar xpos = x; |
| 227 | SkASSERT(length <= SK_ARRAY_COUNT(pts)); |
reed@google.com | 9e39bb3 | 2011-05-18 12:17:53 +0000 | [diff] [blame] | 228 | for (size_t i = 0; i < length; i++) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 229 | pts[i].set(xpos, y), xpos += paint.getTextSize(); |
reed@google.com | 9e39bb3 | 2011-05-18 12:17:53 +0000 | [diff] [blame] | 230 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 231 | canvas->drawPosText(text, length, pts, paint); |
| 232 | } |
| 233 | #endif |
| 234 | |
| 235 | p.setSubpixelText(true); |
| 236 | x += SkIntToScalar(180); |
| 237 | canvas->drawText(text, length, x, y, p); |
| 238 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 239 | #ifdef SK_DEBUG |
reed@google.com | 9e39bb3 | 2011-05-18 12:17:53 +0000 | [diff] [blame] | 240 | if (true) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 241 | // p.setMaskFilter(mf); |
| 242 | p.setSubpixelText(false); |
| 243 | p.setLinearText(true); |
| 244 | x += SkIntToScalar(180); |
| 245 | canvas->drawText(text, length, x, y, p); |
| 246 | } |
| 247 | #endif |
| 248 | } |
| 249 | |
reed@google.com | 9e39bb3 | 2011-05-18 12:17:53 +0000 | [diff] [blame] | 250 | class TextSpeedView : public SampleView { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 251 | public: |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 252 | TextSpeedView() { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 253 | fMF = makemf(); |
| 254 | |
| 255 | fHints = 0; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 256 | fClickX = 0; |
| 257 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 258 | test_breakText(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 259 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 260 | |
reed@google.com | 9e39bb3 | 2011-05-18 12:17:53 +0000 | [diff] [blame] | 261 | virtual ~TextSpeedView() { |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 262 | SkSafeUnref(fMF); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | protected: |
| 266 | // overrides from SkEventSink |
reed@google.com | 9e39bb3 | 2011-05-18 12:17:53 +0000 | [diff] [blame] | 267 | virtual bool onQuery(SkEvent* evt) { |
| 268 | if (SampleCode::TitleQ(*evt)) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 269 | SampleCode::TitleR(evt, "Text"); |
| 270 | return true; |
| 271 | } |
| 272 | return this->INHERITED::onQuery(evt); |
| 273 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 274 | |
reed@google.com | 9e39bb3 | 2011-05-18 12:17:53 +0000 | [diff] [blame] | 275 | static void make_textstrip(SkBitmap* bm) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 276 | bm->setConfig(SkBitmap::kRGB_565_Config, 200, 18); |
| 277 | bm->allocPixels(); |
| 278 | bm->eraseColor(SK_ColorWHITE); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 279 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 280 | SkCanvas canvas(*bm); |
| 281 | SkPaint paint; |
| 282 | const char* s = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit"; |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 283 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 284 | paint.setFlags(paint.getFlags() | SkPaint::kAntiAlias_Flag |
| 285 | | SkPaint::kDevKernText_Flag); |
| 286 | paint.setTextSize(SkIntToScalar(14)); |
| 287 | canvas.drawText(s, strlen(s), SkIntToScalar(8), SkIntToScalar(14), paint); |
| 288 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 289 | |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 290 | static void fill_pts(SkPoint pts[], size_t n, SkRandom* rand) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 291 | for (size_t i = 0; i < n; i++) |
| 292 | pts[i].set(rand->nextUScalar1() * 640, rand->nextUScalar1() * 480); |
| 293 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 294 | |
reed@google.com | 9e39bb3 | 2011-05-18 12:17:53 +0000 | [diff] [blame] | 295 | virtual void onDrawContent(SkCanvas* canvas) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 296 | SkAutoCanvasRestore restore(canvas, false); |
| 297 | { |
| 298 | SkRect r; |
| 299 | r.set(0, 0, SkIntToScalar(1000), SkIntToScalar(20)); |
| 300 | // canvas->saveLayer(&r, NULL, SkCanvas::kHasAlphaLayer_SaveFlag); |
| 301 | } |
| 302 | |
| 303 | SkPaint paint; |
| 304 | // const uint16_t glyphs[] = { 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19 }; |
| 305 | int index = fHints % SK_ARRAY_COUNT(gHints); |
| 306 | index = 1; |
| 307 | // const char* style = gHints[index].fName; |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 308 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 309 | // canvas->translate(0, SkIntToScalar(50)); |
| 310 | |
| 311 | // canvas->drawText(style, strlen(style), SkIntToScalar(20), SkIntToScalar(20), paint); |
| 312 | |
reed@android.com | 04d86c6 | 2010-01-25 22:02:44 +0000 | [diff] [blame] | 313 | SkSafeUnref(paint.setTypeface(SkTypeface::CreateFromFile("/skimages/samplefont.ttf"))); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 314 | paint.setAntiAlias(true); |
| 315 | paint.setFlags(paint.getFlags() | gHints[index].fFlags); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 316 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 317 | SkRect clip; |
| 318 | clip.set(SkIntToScalar(25), SkIntToScalar(34), SkIntToScalar(88), SkIntToScalar(155)); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 319 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 320 | const char* text = "Hamburgefons"; |
| 321 | size_t length = strlen(text); |
| 322 | |
reed@google.com | 2f3dc9d | 2011-05-02 17:33:45 +0000 | [diff] [blame] | 323 | SkScalar y = SkIntToScalar(0); |
| 324 | for (int i = 9; i <= 24; i++) { |
| 325 | paint.setTextSize(SkIntToScalar(i) /*+ (gRand.nextU() & 0xFFFF)*/); |
reed@google.com | 9e39bb3 | 2011-05-18 12:17:53 +0000 | [diff] [blame] | 326 | for (SkScalar dx = 0; dx <= SkIntToScalar(3)/4; |
| 327 | dx += SkIntToScalar(1) /* /4 */) { |
reed@google.com | 2f3dc9d | 2011-05-02 17:33:45 +0000 | [diff] [blame] | 328 | y += paint.getFontSpacing(); |
reed@google.com | 9e39bb3 | 2011-05-18 12:17:53 +0000 | [diff] [blame] | 329 | DrawTheText(canvas, text, length, SkIntToScalar(20) + dx, y, |
| 330 | paint, fClickX, fMF); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 331 | } |
| 332 | } |
reed@google.com | 2f3dc9d | 2011-05-02 17:33:45 +0000 | [diff] [blame] | 333 | if (gHints[index].fFlushCache) { |
| 334 | // SkGraphics::SetFontCacheUsed(0); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 335 | } |
| 336 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 337 | |
reed@google.com | 4d5c26d | 2013-01-08 16:17:50 +0000 | [diff] [blame] | 338 | virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, |
| 339 | unsigned modi) SK_OVERRIDE { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 340 | fClickX = x; |
| 341 | this->inval(NULL); |
reed@google.com | 4d5c26d | 2013-01-08 16:17:50 +0000 | [diff] [blame] | 342 | return this->INHERITED::onFindClickHandler(x, y, modi); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 343 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 344 | |
reed@google.com | 9e39bb3 | 2011-05-18 12:17:53 +0000 | [diff] [blame] | 345 | virtual bool onClick(Click* click) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 346 | return this->INHERITED::onClick(click); |
| 347 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 348 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 349 | private: |
| 350 | int fHints; |
| 351 | SkScalar fClickX; |
| 352 | SkMaskFilter* fMF; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 353 | |
reed@google.com | 9e39bb3 | 2011-05-18 12:17:53 +0000 | [diff] [blame] | 354 | typedef SampleView INHERITED; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 355 | }; |
| 356 | |
| 357 | ////////////////////////////////////////////////////////////////////////////// |
| 358 | |
| 359 | static SkView* MyFactory() { return new TextSpeedView; } |
| 360 | static SkViewRegister reg(MyFactory); |