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/Resources.h" 24#include "tools/ToolUtils.h" 25 26static void test_nulldev(SkCanvas* canvas) { 27 SkBitmap bm; 28 bm.setInfo(SkImageInfo::MakeN32Premul(30, 30)); 29 // notice: no pixels mom! be sure we don't crash 30 // https://code.google.com/p/chromium/issues/detail?id=352616 31 SkCanvas c(bm); 32 33 SkBitmap src; 34 src.allocN32Pixels(10, 10); 35 src.eraseColor(SK_ColorRED); 36 37 // ensure we don't crash 38 c.writePixels(src, 0, 0); 39} 40 41static void draw_text_stroked(SkCanvas* canvas, const SkPaint& paint, const SkFont& font, 42 SkScalar strokeWidth) { 43 SkPaint p(paint); 44 SkPoint loc = { 20, 435 }; 45 46 if (strokeWidth > 0) { 47 p.setStyle(SkPaint::kFill_Style); 48 canvas->drawSimpleText("P", 1, SkTextEncoding::kUTF8, loc.fX, loc.fY - 225, font, p); 49 canvas->drawTextBlob(SkTextBlob::MakeFromPosText("P", 1, &loc, font), 0, 0, p); 50 } 51 52 p.setColor(SK_ColorRED); 53 p.setStyle(SkPaint::kStroke_Style); 54 p.setStrokeWidth(strokeWidth); 55 56 canvas->drawSimpleText("P", 1, SkTextEncoding::kUTF8, loc.fX, loc.fY - 225, font, p); 57 canvas->drawTextBlob(SkTextBlob::MakeFromPosText("P", 1, &loc, font), 0, 0, p); 58} 59 60static void draw_text_set(SkCanvas* canvas, const SkPaint& paint, const SkFont& font) { 61 SkAutoCanvasRestore acr(canvas, true); 62 63 draw_text_stroked(canvas, paint, font, 10); 64 65 canvas->translate(200, 0); 66 draw_text_stroked(canvas, paint, font, 0); 67 68 const SkScalar intervals[] = { 20, 10, 5, 10 }; 69 const SkScalar phase = 0; 70 71 canvas->translate(200, 0); 72 SkPaint p(paint); 73 p.setPathEffect(SkDashPathEffect::Make(intervals, SK_ARRAY_COUNT(intervals), phase)); 74 draw_text_stroked(canvas, p, font, 10); 75} 76 77namespace { 78 enum { 79 kBelowThreshold_TextSize = 255, 80 kAboveThreshold_TextSize = 257 81 }; 82} // namespace 83 84DEF_SIMPLE_GM(stroketext, canvas, 1200, 480) { 85 if (true) { test_nulldev(canvas); } 86 87 SkPaint paint; 88 paint.setAntiAlias(true); 89 90 SkFont font(ToolUtils::create_portable_typeface(), kBelowThreshold_TextSize); 91 draw_text_set(canvas, paint, font); 92 93 canvas->translate(600, 0); 94 font.setSize(kAboveThreshold_TextSize); 95 draw_text_set(canvas, paint, font); 96} 97 98DEF_SIMPLE_GM_CAN_FAIL(stroketext_native, canvas, msg, 650, 320) { 99 sk_sp<SkTypeface> ttf = MakeResourceAsTypeface("fonts/Stroking.ttf"); 100 sk_sp<SkTypeface> otf = MakeResourceAsTypeface("fonts/Stroking.otf"); 101 if (!ttf && !otf) { 102 msg->append("No support for ttf or otf."); 103 return skiagm::DrawResult::kSkip; 104 } 105 106 SkPaint p; 107 p.setAntiAlias(true); 108 p.setStyle(SkPaint::kStroke_Style); 109 p.setStrokeWidth(10); 110 p.setStrokeCap(SkPaint::kRound_Cap); 111 p.setStrokeJoin(SkPaint::kRound_Join); 112 p.setARGB(0xff, 0xbb, 0x00, 0x00); 113 114 if (ttf) { 115 /* Stroking.ttf is structured like: 116 nothing U+25CB ○ (nothing inside) 117 something U+25C9 ◉ (a tiny thing inside) 118 - off (point off / empty quad with implicit end) (before U+207B ⁻ / after U+208B ₋) 119 + on (point on / empty line) (before U+207A ⁺ / after U+208A ₊) 120 0 off off (two implicit quads) (before U+2070 ⁰ / after U+2080 ₀) 121 1 off on (quad with implicit close around) (before U+00B9 ¹ / after U+2081 ₁) 122 2 on off (quad with implicit close) (before U+00B2 ² / after U+2082 ₂) 123 3 on on (empty line) (before U+00B3 ³ / after U+2083 ₃) 124 */ 125 SkFont font(ttf, 100); 126 canvas->drawString("○◉ ⁻₋⁺₊", 10, 100, font, p); 127 canvas->drawString("⁰₀¹₁²₂³₃", 10, 200, font, p); 128 } 129 130 if (otf) { 131 /* Stroking.otf is structured like: 132 nothing U+25CB ○ 133 something U+25C9 ◉ 134 0 moveto, moveto (before U+2070 ⁰) (nothing there, FreeType ignores these) 135 1 moveto, empty line, moveto (before U+00B9 ¹) (degenerate lineto) 136 3 moveto, empty cubic, moveto (before U+00B3 ³) (degenerate cubicto) 137 f moveto, empty flex, moveto (before U+1DA0 ᶠ) (degenerate flex) 138 */ 139 SkFont font(otf, 100); 140 canvas->drawString("○◉ ⁰¹³ᶠ", 10, 300, font, p); 141 } 142 143 return skiagm::DrawResult::kOk; 144} 145