| OLD | NEW |
| 1 #include "SampleCode.h" | 1 #include "SampleCode.h" |
| 2 #include "SkView.h" | 2 #include "SkView.h" |
| 3 #include "SkCanvas.h" | 3 #include "SkCanvas.h" |
| 4 #include "SkGradientShader.h" | 4 #include "SkGradientShader.h" |
| 5 #include "SkGraphics.h" | 5 #include "SkGraphics.h" |
| 6 #include "SkImageDecoder.h" | 6 #include "SkImageDecoder.h" |
| 7 #include "SkPackBits.h" | 7 #include "SkPackBits.h" |
| 8 #include "SkPath.h" | 8 #include "SkPath.h" |
| 9 #include "SkPathMeasure.h" | 9 #include "SkPathMeasure.h" |
| 10 #include "SkRandom.h" | 10 #include "SkRandom.h" |
| (...skipping 24 matching lines...) Expand all Loading... |
| 35 } | 35 } |
| 36 return w; | 36 return w; |
| 37 } | 37 } |
| 38 | 38 |
| 39 static void test_measure(const SkPaint& paint) { | 39 static void test_measure(const SkPaint& paint) { |
| 40 char text[256]; | 40 char text[256]; |
| 41 SkScalar widths[256]; | 41 SkScalar widths[256]; |
| 42 SkRect rects[256]; | 42 SkRect rects[256]; |
| 43 SkRect bounds; | 43 SkRect bounds; |
| 44 int count = 256; | 44 int count = 256; |
| 45 | 45 |
| 46 SkRandom rand; | 46 SkRandom rand; |
| 47 | 47 |
| 48 for (int i = 0; i < 100; i++) { | 48 for (int i = 0; i < 100; i++) { |
| 49 rand_text(text, rand, 256); | 49 rand_text(text, rand, 256); |
| 50 paint.getTextWidths(text, count, widths, NULL); | 50 paint.getTextWidths(text, count, widths, NULL); |
| 51 SkScalar tw0 = sum_widths(widths, count); | 51 SkScalar tw0 = sum_widths(widths, count); |
| 52 paint.getTextWidths(text, count, widths, rects); | 52 paint.getTextWidths(text, count, widths, rects); |
| 53 SkScalar tw1 = sum_widths(widths, count); | 53 SkScalar tw1 = sum_widths(widths, count); |
| 54 SkASSERT(tw0 == tw1); | 54 SkASSERT(tw0 == tw1); |
| 55 | 55 |
| 56 SkScalar w0 = paint.measureText(text, count, NULL); | 56 SkScalar w0 = paint.measureText(text, count, NULL); |
| 57 SkScalar w1 = paint.measureText(text, count, &bounds); | 57 SkScalar w1 = paint.measureText(text, count, &bounds); |
| 58 SkASSERT(w0 == w1); | 58 SkASSERT(w0 == w1); |
| 59 SkASSERT(w0 == tw0); | 59 SkASSERT(w0 == tw0); |
| 60 | 60 |
| 61 SkRect r = rects[0]; | 61 SkRect r = rects[0]; |
| 62 SkScalar x = 0; | 62 SkScalar x = 0; |
| 63 for (int j = 1; j < count; j++) { | 63 for (int j = 1; j < count; j++) { |
| 64 x += widths[j-1]; | 64 x += widths[j-1]; |
| 65 rects[j].offset(x, 0); | 65 rects[j].offset(x, 0); |
| 66 r.join(rects[j]); | 66 r.join(rects[j]); |
| 67 } | 67 } |
| 68 SkASSERT(r == bounds); | 68 SkASSERT(r == bounds); |
| 69 | 69 |
| 70 if (r != bounds) { | 70 if (r != bounds) { |
| 71 printf("flags=%x i=%d [%g %g %g %g] [%g %g %g %g]\n", | 71 printf("flags=%x i=%d [%g %g %g %g] [%g %g %g %g]\n", |
| 72 paint.getFlags(), i, | 72 paint.getFlags(), i, |
| 73 SkScalarToFloat(r.fLeft), | 73 SkScalarToFloat(r.fLeft), |
| 74 SkScalarToFloat(r.fTop), | 74 SkScalarToFloat(r.fTop), |
| 75 SkScalarToFloat(r.fRight), | 75 SkScalarToFloat(r.fRight), |
| 76 SkScalarToFloat(r.fBottom), | 76 SkScalarToFloat(r.fBottom), |
| 77 SkScalarToFloat(bounds.fLeft), | 77 SkScalarToFloat(bounds.fLeft), |
| 78 SkScalarToFloat(bounds.fTop), | 78 SkScalarToFloat(bounds.fTop), |
| 79 SkScalarToFloat(bounds.fRight), | 79 SkScalarToFloat(bounds.fRight), |
| 80 SkScalarToFloat(bounds.fBottom)); | 80 SkScalarToFloat(bounds.fBottom)); |
| 81 } | 81 } |
| 82 } | 82 } |
| 83 } | 83 } |
| 84 | 84 |
| 85 static void test_measure() { | 85 static void test_measure() { |
| 86 SkPaint paint; | 86 SkPaint paint; |
| 87 | 87 |
| 88 for (int i = 0; i <= SkPaint::kAllFlags; i++) { | 88 for (int i = 0; i <= SkPaint::kAllFlags; i++) { |
| 89 paint.setFlags(i); | 89 paint.setFlags(i); |
| 90 test_measure(paint); | 90 test_measure(paint); |
| 91 } | 91 } |
| 92 } | 92 } |
| 93 | 93 |
| 94 ////////////////////////////////////////////////////////////////////////////// | 94 ////////////////////////////////////////////////////////////////////////////// |
| 95 | 95 |
| 96 static void test_textBounds(SkCanvas* canvas) { | 96 static void test_textBounds(SkCanvas* canvas) { |
| 97 // canvas->scale(SK_Scalar1/2, SK_Scalar1/2); | 97 // canvas->scale(SK_Scalar1/2, SK_Scalar1/2); |
| 98 | 98 |
| 99 // canvas->rotate(SkIntToScalar(30)); | 99 // canvas->rotate(SkIntToScalar(30)); |
| 100 | 100 |
| 101 gDevKern = !gDevKern; | 101 gDevKern = !gDevKern; |
| 102 | 102 |
| 103 SkScalar x = SkIntToScalar(50); | 103 SkScalar x = SkIntToScalar(50); |
| 104 SkScalar y = SkIntToScalar(150); | 104 SkScalar y = SkIntToScalar(150); |
| 105 SkScalar w[100]; | 105 SkScalar w[100]; |
| 106 SkRect r[100], bounds; | 106 SkRect r[100], bounds; |
| 107 | 107 |
| 108 SkPaint paint; | 108 SkPaint paint; |
| 109 paint.setTextSize(SkIntToScalar(64)); | 109 paint.setTextSize(SkIntToScalar(64)); |
| 110 paint.setAntiAlias(true); | 110 paint.setAntiAlias(true); |
| 111 paint.setDevKernText(gDevKern); | 111 paint.setDevKernText(gDevKern); |
| 112 | 112 |
| 113 (void)paint.measureText(gText, strlen(gText), &bounds, NULL); | 113 (void)paint.measureText(gText, strlen(gText), &bounds, NULL); |
| 114 paint.setColor(SK_ColorGREEN); | 114 paint.setColor(SK_ColorGREEN); |
| 115 bounds.offset(x, y); | 115 bounds.offset(x, y); |
| 116 canvas->drawRect(bounds, paint); | 116 canvas->drawRect(bounds, paint); |
| 117 | 117 |
| 118 int count = paint.getTextWidths(gText, strlen(gText), w, r); | 118 int count = paint.getTextWidths(gText, strlen(gText), w, r); |
| 119 | 119 |
| 120 paint.setColor(SK_ColorRED); | 120 paint.setColor(SK_ColorRED); |
| 121 for (int i = 0; i < count; i++) { | 121 for (int i = 0; i < count; i++) { |
| 122 r[i].offset(x, y); | 122 r[i].offset(x, y); |
| 123 canvas->drawRect(r[i], paint); | 123 canvas->drawRect(r[i], paint); |
| 124 x += w[i]; | 124 x += w[i]; |
| 125 } | 125 } |
| 126 x = SkIntToScalar(50); | 126 x = SkIntToScalar(50); |
| 127 paint.setColor(gDevKern ? SK_ColorDKGRAY : SK_ColorBLACK); | 127 paint.setColor(gDevKern ? SK_ColorDKGRAY : SK_ColorBLACK); |
| 128 canvas->drawText(gText, strlen(gText), x, y, paint); | 128 canvas->drawText(gText, strlen(gText), x, y, paint); |
| 129 } | 129 } |
| 130 | 130 |
| 131 static void create_src(SkBitmap* bitmap, SkBitmap::Config config) { | 131 static void create_src(SkBitmap* bitmap, SkBitmap::Config config) { |
| 132 bitmap->setConfig(config, 100, 100); | 132 bitmap->setConfig(config, 100, 100); |
| 133 bitmap->allocPixels(); | 133 bitmap->allocPixels(); |
| 134 bitmap->eraseColor(0); | 134 bitmap->eraseColor(0); |
| 135 | 135 |
| 136 SkCanvas canvas(*bitmap); | 136 SkCanvas canvas(*bitmap); |
| 137 SkPaint paint; | 137 SkPaint paint; |
| 138 | 138 |
| 139 paint.setAntiAlias(true); | 139 paint.setAntiAlias(true); |
| 140 canvas.drawCircle(SkIntToScalar(50), SkIntToScalar(50), | 140 canvas.drawCircle(SkIntToScalar(50), SkIntToScalar(50), |
| 141 SkIntToScalar(50), paint); | 141 SkIntToScalar(50), paint); |
| 142 } | 142 } |
| 143 | 143 |
| 144 static void blur(SkBitmap* dst, const SkBitmap& src, SkScalar radius) { | 144 static void blur(SkBitmap* dst, const SkBitmap& src, SkScalar radius) { |
| 145 *dst = src; | 145 *dst = src; |
| 146 } | 146 } |
| 147 | 147 |
| 148 static void test_bitmap_blur(SkCanvas* canvas) { | 148 static void test_bitmap_blur(SkCanvas* canvas) { |
| 149 SkBitmap src, dst; | 149 SkBitmap src, dst; |
| 150 | 150 |
| 151 create_src(&src, SkBitmap::kARGB_8888_Config); | 151 create_src(&src, SkBitmap::kARGB_8888_Config); |
| 152 blur(&dst, src, SkIntToScalar(4)); | 152 blur(&dst, src, SkIntToScalar(4)); |
| 153 | 153 |
| 154 SkPaint paint; | 154 SkPaint paint; |
| 155 | 155 |
| 156 paint.setColor(SK_ColorRED); | 156 paint.setColor(SK_ColorRED); |
| 157 | 157 |
| 158 canvas->drawBitmap(dst, SkIntToScalar(30), SkIntToScalar(60), &paint); | 158 canvas->drawBitmap(dst, SkIntToScalar(30), SkIntToScalar(60), &paint); |
| 159 } | 159 } |
| 160 | 160 |
| 161 static SkScalar getpathlen(const SkPath& path) { | 161 static SkScalar getpathlen(const SkPath& path) { |
| 162 SkPathMeasure meas(path, false); | 162 SkPathMeasure meas(path, false); |
| 163 return meas.getLength(); | 163 return meas.getLength(); |
| 164 } | 164 } |
| 165 | 165 |
| 166 static void test_textpathmatrix(SkCanvas* canvas) { | 166 static void test_textpathmatrix(SkCanvas* canvas) { |
| 167 SkPaint paint; | 167 SkPaint paint; |
| 168 SkPath path; | 168 SkPath path; |
| 169 SkMatrix matrix; | 169 SkMatrix matrix; |
| 170 | 170 |
| 171 path.moveTo(SkIntToScalar(200), SkIntToScalar(300)); | 171 path.moveTo(SkIntToScalar(200), SkIntToScalar(300)); |
| 172 path.quadTo(SkIntToScalar(400), SkIntToScalar(100), | 172 path.quadTo(SkIntToScalar(400), SkIntToScalar(100), |
| 173 SkIntToScalar(600), SkIntToScalar(300)); | 173 SkIntToScalar(600), SkIntToScalar(300)); |
| 174 | 174 |
| 175 paint.setAntiAlias(true); | 175 paint.setAntiAlias(true); |
| 176 | 176 |
| 177 paint.setStyle(SkPaint::kStroke_Style); | 177 paint.setStyle(SkPaint::kStroke_Style); |
| 178 canvas->drawPath(path, paint); | 178 canvas->drawPath(path, paint); |
| 179 paint.setStyle(SkPaint::kFill_Style); | 179 paint.setStyle(SkPaint::kFill_Style); |
| 180 paint.setTextSize(SkIntToScalar(48)); | 180 paint.setTextSize(SkIntToScalar(48)); |
| 181 paint.setTextAlign(SkPaint::kRight_Align); | 181 paint.setTextAlign(SkPaint::kRight_Align); |
| 182 | 182 |
| 183 const char* text = "Android"; | 183 const char* text = "Android"; |
| 184 size_t len = strlen(text); | 184 size_t len = strlen(text); |
| 185 SkScalar pathLen = getpathlen(path); | 185 SkScalar pathLen = getpathlen(path); |
| 186 | 186 |
| 187 canvas->drawTextOnPath(text, len, path, NULL, paint); | 187 canvas->drawTextOnPath(text, len, path, NULL, paint); |
| 188 | 188 |
| 189 paint.setColor(SK_ColorRED); | 189 paint.setColor(SK_ColorRED); |
| 190 matrix.setScale(-SK_Scalar1, SK_Scalar1); | 190 matrix.setScale(-SK_Scalar1, SK_Scalar1); |
| 191 matrix.postTranslate(pathLen, 0); | 191 matrix.postTranslate(pathLen, 0); |
| 192 canvas->drawTextOnPath(text, len, path, &matrix, paint); | 192 canvas->drawTextOnPath(text, len, path, &matrix, paint); |
| 193 | 193 |
| 194 paint.setColor(SK_ColorBLUE); | 194 paint.setColor(SK_ColorBLUE); |
| 195 matrix.setScale(SK_Scalar1, -SK_Scalar1); | 195 matrix.setScale(SK_Scalar1, -SK_Scalar1); |
| 196 canvas->drawTextOnPath(text, len, path, &matrix, paint); | 196 canvas->drawTextOnPath(text, len, path, &matrix, paint); |
| 197 | 197 |
| 198 paint.setColor(SK_ColorGREEN); | 198 paint.setColor(SK_ColorGREEN); |
| 199 matrix.setScale(-SK_Scalar1, -SK_Scalar1); | 199 matrix.setScale(-SK_Scalar1, -SK_Scalar1); |
| 200 matrix.postTranslate(pathLen, 0); | 200 matrix.postTranslate(pathLen, 0); |
| 201 canvas->drawTextOnPath(text, len, path, &matrix, paint); | 201 canvas->drawTextOnPath(text, len, path, &matrix, paint); |
| 202 } | 202 } |
| 203 | 203 |
| 204 class TextOnPathView : public SkView { | 204 class TextOnPathView : public SkView { |
| 205 public: | 205 public: |
| 206 SkPath fPath; | 206 SkPath fPath; |
| 207 SkScalar fHOffset; | 207 SkScalar fHOffset; |
| 208 | 208 |
| 209 TextOnPathView() { | 209 TextOnPathView() { |
| 210 SkRect r; | 210 SkRect r; |
| 211 r.set(SkIntToScalar(100), SkIntToScalar(100), | 211 r.set(SkIntToScalar(100), SkIntToScalar(100), |
| 212 SkIntToScalar(300), SkIntToScalar(300)); | 212 SkIntToScalar(300), SkIntToScalar(300)); |
| 213 fPath.addOval(r); | 213 fPath.addOval(r); |
| 214 | 214 |
| 215 fHOffset = SkIntToScalar(50); | 215 fHOffset = SkIntToScalar(50); |
| 216 } | 216 } |
| 217 | 217 |
| 218 protected: | 218 protected: |
| 219 // overrides from SkEventSink | 219 // overrides from SkEventSink |
| 220 virtual bool onQuery(SkEvent* evt) { | 220 virtual bool onQuery(SkEvent* evt) { |
| 221 if (SampleCode::TitleQ(*evt)) { | 221 if (SampleCode::TitleQ(*evt)) { |
| 222 SampleCode::TitleR(evt, "Text On Path"); | 222 SampleCode::TitleR(evt, "Text On Path"); |
| 223 return true; | 223 return true; |
| 224 } | 224 } |
| 225 return this->INHERITED::onQuery(evt); | 225 return this->INHERITED::onQuery(evt); |
| 226 } | 226 } |
| 227 | 227 |
| 228 void drawBG(SkCanvas* canvas) { | 228 void drawBG(SkCanvas* canvas) { |
| 229 canvas->drawColor(SK_ColorWHITE); | 229 canvas->drawColor(SK_ColorWHITE); |
| 230 #if 0 | 230 #if 0 |
| 231 SkRect r; | 231 SkRect r; |
| 232 SkPaint p; | 232 SkPaint p; |
| 233 SkRandom rand; | 233 SkRandom rand; |
| 234 p.setAntiAlias(true); | 234 p.setAntiAlias(true); |
| 235 | 235 |
| 236 for (int i = 0; i < 100; i++) { | 236 for (int i = 0; i < 100; i++) { |
| 237 SkScalar x = rand.nextUScalar1() * 300 + SkIntToScalar(50); | 237 SkScalar x = rand.nextUScalar1() * 300 + SkIntToScalar(50); |
| 238 SkScalar y = rand.nextUScalar1() * 200 + SkIntToScalar(50); | 238 SkScalar y = rand.nextUScalar1() * 200 + SkIntToScalar(50); |
| 239 SkScalar w = rand.nextUScalar1() * 10; | 239 SkScalar w = rand.nextUScalar1() * 10; |
| 240 SkScalar h = rand.nextUScalar1() * 10; | 240 SkScalar h = rand.nextUScalar1() * 10; |
| 241 r.set(x, y, x + w, y + h); | 241 r.set(x, y, x + w, y + h); |
| 242 canvas->drawRect(r, p); | 242 canvas->drawRect(r, p); |
| 243 } | 243 } |
| 244 | 244 |
| 245 test_textBounds(canvas); | 245 test_textBounds(canvas); |
| 246 // return; | 246 // return; |
| 247 | 247 |
| 248 SkBitmap bm; | 248 SkBitmap bm; |
| 249 if (SkImageDecoder::DecodeFile("/loading_tile.png", | 249 if (SkImageDecoder::DecodeFile("/loading_tile.png", |
| 250 &bm, SkBitmap::kRGB_565_Config, true)) | 250 &bm, SkBitmap::kRGB_565_Config, true)) |
| 251 canvas->drawBitmap(bm, 0, 0); | 251 canvas->drawBitmap(bm, 0, 0); |
| 252 #endif | 252 #endif |
| 253 } | 253 } |
| 254 | 254 |
| 255 virtual void onDraw(SkCanvas* canvas) { | 255 virtual void onDraw(SkCanvas* canvas) { |
| 256 this->drawBG(canvas); | 256 this->drawBG(canvas); |
| 257 | 257 |
| 258 SkPaint paint; | 258 SkPaint paint; |
| 259 | 259 |
| 260 paint.setAntiAlias(true); | 260 paint.setAntiAlias(true); |
| 261 paint.setTextSize(SkIntToScalar(50)); | 261 paint.setTextSize(SkIntToScalar(50)); |
| 262 | 262 |
| 263 for (int j = 0; j < REPEAT_COUNT; j++) { | 263 for (int j = 0; j < REPEAT_COUNT; j++) { |
| 264 SkScalar x = fHOffset; | 264 SkScalar x = fHOffset; |
| 265 | 265 |
| 266 paint.setColor(SK_ColorBLACK); | 266 paint.setColor(SK_ColorBLACK); |
| 267 canvas->drawTextOnPathHV(gText, sizeof(gText)-1, fPath, | 267 canvas->drawTextOnPathHV(gText, sizeof(gText)-1, fPath, |
| 268 x, paint.getTextSize()/2, paint); | 268 x, paint.getTextSize()/2, paint); |
| 269 | 269 |
| 270 paint.setColor(SK_ColorRED); | 270 paint.setColor(SK_ColorRED); |
| 271 canvas->drawTextOnPathHV(gText, sizeof(gText)-1, fPath, | 271 canvas->drawTextOnPathHV(gText, sizeof(gText)-1, fPath, |
| 272 x + SkIntToScalar(50), 0, paint); | 272 x + SkIntToScalar(50), 0, paint); |
| 273 | 273 |
| 274 paint.setColor(SK_ColorBLUE); | 274 paint.setColor(SK_ColorBLUE); |
| 275 canvas->drawTextOnPathHV(gText, sizeof(gText)-1, fPath, | 275 canvas->drawTextOnPathHV(gText, sizeof(gText)-1, fPath, |
| 276 x + SkIntToScalar(100), -paint.getTextSize()/2, paint); | 276 x + SkIntToScalar(100), -paint.getTextSize()/2, paint); |
| 277 } | 277 } |
| 278 | 278 |
| 279 paint.setColor(SK_ColorGREEN); | 279 paint.setColor(SK_ColorGREEN); |
| 280 paint.setStyle(SkPaint::kStroke_Style); | 280 paint.setStyle(SkPaint::kStroke_Style); |
| 281 canvas->drawPath(fPath, paint); | 281 canvas->drawPath(fPath, paint); |
| 282 | 282 |
| 283 canvas->translate(SkIntToScalar(200), 0); | 283 canvas->translate(SkIntToScalar(200), 0); |
| 284 test_textpathmatrix(canvas); | 284 test_textpathmatrix(canvas); |
| 285 | 285 |
| 286 test_bitmap_blur(canvas); | 286 test_bitmap_blur(canvas); |
| 287 | 287 |
| 288 if (REPEAT_COUNT > 1) | 288 if (REPEAT_COUNT > 1) |
| 289 this->inval(NULL); | 289 this->inval(NULL); |
| 290 } | 290 } |
| 291 | 291 |
| 292 virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y) { | 292 virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y) { |
| 293 fHints += 1; | 293 fHints += 1; |
| 294 this->inval(NULL); | 294 this->inval(NULL); |
| 295 return this->INHERITED::onFindClickHandler(x, y); | 295 return this->INHERITED::onFindClickHandler(x, y); |
| 296 } | 296 } |
| 297 | 297 |
| 298 virtual bool onClick(Click* click) { | 298 virtual bool onClick(Click* click) { |
| 299 return this->INHERITED::onClick(click); | 299 return this->INHERITED::onClick(click); |
| 300 } | 300 } |
| 301 | 301 |
| 302 private: | 302 private: |
| 303 int fHints; | 303 int fHints; |
| 304 typedef SkView INHERITED; | 304 typedef SkView INHERITED; |
| 305 }; | 305 }; |
| 306 | 306 |
| 307 ////////////////////////////////////////////////////////////////////////////// | 307 ////////////////////////////////////////////////////////////////////////////// |
| 308 | 308 |
| 309 static SkView* MyFactory() { | 309 static SkView* MyFactory() { |
| 310 return new TextOnPathView; | 310 return new TextOnPathView; |
| 311 } | 311 } |
| 312 | 312 |
| 313 static SkViewRegister reg(MyFactory); | 313 static SkViewRegister reg(MyFactory); |
| 314 | 314 |
| OLD | NEW |