| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2014 Google Inc. | 2 * Copyright 2014 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "gm.h" | 8 #include "gm.h" |
| 9 #include "SkCanvas.h" | 9 #include "SkCanvas.h" |
| 10 #include "SkDashPathEffect.h" |
| 10 | 11 |
| 11 static void test_nulldev(SkCanvas* canvas) { | 12 static void test_nulldev(SkCanvas* canvas) { |
| 12 SkBitmap bm; | 13 SkBitmap bm; |
| 13 bm.setConfig(SkBitmap::kARGB_8888_Config, 30, 30); | 14 bm.setConfig(SkBitmap::kARGB_8888_Config, 30, 30); |
| 14 // notice: no pixels mom! be sure we don't crash | 15 // notice: no pixels mom! be sure we don't crash |
| 15 // https://code.google.com/p/chromium/issues/detail?id=352616 | 16 // https://code.google.com/p/chromium/issues/detail?id=352616 |
| 16 SkCanvas c(bm); | 17 SkCanvas c(bm); |
| 17 | 18 |
| 18 SkBitmap src; | 19 SkBitmap src; |
| 19 src.allocN32Pixels(10, 10); | 20 src.allocN32Pixels(10, 10); |
| 20 src.eraseColor(SK_ColorRED); | 21 src.eraseColor(SK_ColorRED); |
| 21 | 22 |
| 22 // ensure we don't crash | 23 // ensure we don't crash |
| 23 c.writePixels(src, 0, 0); | 24 c.writePixels(src, 0, 0); |
| 24 } | 25 } |
| 25 | 26 |
| 26 static void draw_text_stroked(SkCanvas* canvas, const SkPaint& paint) { | 27 static void draw_text_stroked(SkCanvas* canvas, const SkPaint& paint, SkScalar s
trokeWidth) { |
| 27 SkPaint p(paint); | 28 SkPaint p(paint); |
| 28 SkPoint loc = { 20, 450 }; | 29 SkPoint loc = { 20, 435 }; |
| 29 | 30 |
| 30 canvas->drawText("P", 1, loc.fX, loc.fY - 225, p); | 31 if (strokeWidth > 0) { |
| 31 canvas->drawPosText("P", 1, &loc, p); | 32 p.setStyle(SkPaint::kFill_Style); |
| 33 canvas->drawText("P", 1, loc.fX, loc.fY - 225, p); |
| 34 canvas->drawPosText("P", 1, &loc, p); |
| 35 } |
| 32 | 36 |
| 33 p.setColor(SK_ColorRED); | 37 p.setColor(SK_ColorRED); |
| 34 p.setStyle(SkPaint::kStroke_Style); | 38 p.setStyle(SkPaint::kStroke_Style); |
| 35 p.setStrokeWidth(10); | 39 p.setStrokeWidth(strokeWidth); |
| 36 | 40 |
| 37 canvas->drawText("P", 1, loc.fX, loc.fY - 225, p); | 41 canvas->drawText("P", 1, loc.fX, loc.fY - 225, p); |
| 38 canvas->drawPosText("P", 1, &loc, p); | 42 canvas->drawPosText("P", 1, &loc, p); |
| 39 } | 43 } |
| 40 | 44 |
| 45 static void draw_text_set(SkCanvas* canvas, const SkPaint& paint) { |
| 46 SkAutoCanvasRestore acr(canvas, true); |
| 47 |
| 48 draw_text_stroked(canvas, paint, 10); |
| 49 |
| 50 canvas->translate(200, 0); |
| 51 draw_text_stroked(canvas, paint, 0); |
| 52 |
| 53 const SkScalar intervals[] = { 20, 10, 5, 10 }; |
| 54 const SkScalar phase = 0; |
| 55 |
| 56 canvas->translate(200, 0); |
| 57 SkPaint p(paint); |
| 58 p.setPathEffect(SkDashPathEffect::Create(intervals, SK_ARRAY_COUNT(intervals
), phase))->unref(); |
| 59 draw_text_stroked(canvas, paint, 10); |
| 60 } |
| 61 |
| 41 class StrokeTextGM : public skiagm::GM { | 62 class StrokeTextGM : public skiagm::GM { |
| 42 // Skia has a threshold above which it draws text via paths instead of using
scalercontext | 63 // Skia has a threshold above which it draws text via paths instead of using
scalercontext |
| 43 // and caching the glyph. This GM wants to ensure that we draw stroking corr
ectly on both | 64 // and caching the glyph. This GM wants to ensure that we draw stroking corr
ectly on both |
| 44 // sides of this threshold. | 65 // sides of this threshold. |
| 45 enum { | 66 enum { |
| 46 kBelowThreshold_TextSize = 255, | 67 kBelowThreshold_TextSize = 255, |
| 47 kAboveThreshold_TextSize = 257 | 68 kAboveThreshold_TextSize = 257 |
| 48 }; | 69 }; |
| 49 public: | 70 public: |
| 50 StrokeTextGM() {} | 71 StrokeTextGM() {} |
| 51 | 72 |
| 52 protected: | 73 protected: |
| 53 virtual uint32_t onGetFlags() const SK_OVERRIDE { | 74 virtual uint32_t onGetFlags() const SK_OVERRIDE { |
| 54 return kSkipTiled_Flag; | 75 return kSkipTiled_Flag; |
| 55 } | 76 } |
| 56 | 77 |
| 57 virtual SkString onShortName() SK_OVERRIDE { | 78 virtual SkString onShortName() SK_OVERRIDE { |
| 58 return SkString("stroketext"); | 79 return SkString("stroketext"); |
| 59 } | 80 } |
| 60 | 81 |
| 61 virtual SkISize onISize() SK_OVERRIDE { | 82 virtual SkISize onISize() SK_OVERRIDE { |
| 62 return SkISize::Make(640, 480); | 83 return SkISize::Make(1200, 480); |
| 63 } | 84 } |
| 64 | 85 |
| 65 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { | 86 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { |
| 66 if (true) { test_nulldev(canvas); } | 87 if (true) { test_nulldev(canvas); } |
| 67 SkPaint paint; | 88 SkPaint paint; |
| 68 paint.setAntiAlias(true); | 89 paint.setAntiAlias(true); |
| 69 | 90 |
| 70 paint.setTextSize(kBelowThreshold_TextSize); | 91 paint.setTextSize(kBelowThreshold_TextSize); |
| 71 draw_text_stroked(canvas, paint); | 92 draw_text_set(canvas, paint); |
| 72 | 93 |
| 73 canvas->translate(200, 0); | 94 canvas->translate(600, 0); |
| 74 paint.setTextSize(kAboveThreshold_TextSize); | 95 paint.setTextSize(kAboveThreshold_TextSize); |
| 75 draw_text_stroked(canvas, paint); | 96 draw_text_set(canvas, paint); |
| 76 } | 97 } |
| 77 | 98 |
| 78 private: | 99 private: |
| 79 typedef skiagm::GM INHERITED; | 100 typedef skiagm::GM INHERITED; |
| 80 }; | 101 }; |
| 81 | 102 |
| 82 DEF_GM( return SkNEW(StrokeTextGM); ) | 103 DEF_GM( return SkNEW(StrokeTextGM); ) |
| OLD | NEW |