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