1 /* 2 * Copyright 2014 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 */ 7 8 #include "gm.h" 9 #include "sk_tool_utils.h" 10 #include "SkCanvas.h" 11 #include "SkDashPathEffect.h" 12 #include "SkTextBlob.h" 13 14 static void test_nulldev(SkCanvas* canvas) { 15 SkBitmap bm; 16 bm.setInfo(SkImageInfo::MakeN32Premul(30, 30)); 17 // notice: no pixels mom! be sure we don't crash 18 // https://code.google.com/p/chromium/issues/detail?id=352616 19 SkCanvas c(bm); 20 21 SkBitmap src; 22 src.allocN32Pixels(10, 10); 23 src.eraseColor(SK_ColorRED); 24 25 // ensure we don't crash 26 c.writePixels(src, 0, 0); 27 } 28 29 static void draw_text_stroked(SkCanvas* canvas, const SkPaint& paint, const SkFont& font, 30 SkScalar strokeWidth) { 31 SkPaint p(paint); 32 SkPoint loc = { 20, 435 }; 33 34 if (strokeWidth > 0) { 35 p.setStyle(SkPaint::kFill_Style); 36 canvas->drawSimpleText("P", 1, kUTF8_SkTextEncoding, loc.fX, loc.fY - 225, font, p); 37 canvas->drawTextBlob(SkTextBlob::MakeFromPosText("P", 1, &loc, font), 0, 0, p); 38 } 39 40 p.setColor(SK_ColorRED); 41 p.setStyle(SkPaint::kStroke_Style); 42 p.setStrokeWidth(strokeWidth); 43 44 canvas->drawSimpleText("P", 1, kUTF8_SkTextEncoding, loc.fX, loc.fY - 225, font, p); 45 canvas->drawTextBlob(SkTextBlob::MakeFromPosText("P", 1, &loc, font), 0, 0, p); 46 } 47 48 static void draw_text_set(SkCanvas* canvas, const SkPaint& paint, const SkFont& font) { 49 SkAutoCanvasRestore acr(canvas, true); 50 51 draw_text_stroked(canvas, paint, font, 10); 52 53 canvas->translate(200, 0); 54 draw_text_stroked(canvas, paint, font, 0); 55 56 const SkScalar intervals[] = { 20, 10, 5, 10 }; 57 const SkScalar phase = 0; 58 59 canvas->translate(200, 0); 60 SkPaint p(paint); 61 p.setPathEffect(SkDashPathEffect::Make(intervals, SK_ARRAY_COUNT(intervals), phase)); 62 draw_text_stroked(canvas, p, font, 10); 63 } 64 65 namespace { 66 enum { 67 kBelowThreshold_TextSize = 255, 68 kAboveThreshold_TextSize = 257 69 }; 70 } 71 72 DEF_SIMPLE_GM(stroketext, canvas, 1200, 480) { 73 if (true) { test_nulldev(canvas); } 74 75 SkPaint paint; 76 paint.setAntiAlias(true); 77 78 SkFont font(sk_tool_utils::create_portable_typeface(), kBelowThreshold_TextSize); 79 draw_text_set(canvas, paint, font); 80 81 canvas->translate(600, 0); 82 font.setSize(kAboveThreshold_TextSize); 83 draw_text_set(canvas, paint, font); 84 } 85