blob: fb7697508473ec4266d09896a147a4b3565e9480 [file] [log] [blame]
commit-bot@chromium.org641e33b2014-03-12 20:31:24 +00001/*
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"
Mike Klein33d20552017-03-22 13:47:51 -04009#include "sk_tool_utils.h"
commit-bot@chromium.org641e33b2014-03-12 20:31:24 +000010#include "SkCanvas.h"
commit-bot@chromium.org74266812014-05-22 17:41:41 +000011#include "SkDashPathEffect.h"
Mike Reed088b74e2018-12-24 14:52:46 -050012#include "SkTextBlob.h"
commit-bot@chromium.org641e33b2014-03-12 20:31:24 +000013
reed@google.com0d30c512014-03-14 14:02:58 +000014static void test_nulldev(SkCanvas* canvas) {
15 SkBitmap bm;
reed6c225732014-06-09 19:52:07 -070016 bm.setInfo(SkImageInfo::MakeN32Premul(30, 30));
reed@google.com0d30c512014-03-14 14:02:58 +000017 // 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
Mike Reed088b74e2018-12-24 14:52:46 -050029static void draw_text_stroked(SkCanvas* canvas, const SkPaint& paint, const SkFont& font,
30 SkScalar strokeWidth) {
commit-bot@chromium.org641e33b2014-03-12 20:31:24 +000031 SkPaint p(paint);
commit-bot@chromium.org74266812014-05-22 17:41:41 +000032 SkPoint loc = { 20, 435 };
skia.committer@gmail.comaffa77d2014-03-13 03:02:23 +000033
commit-bot@chromium.org74266812014-05-22 17:41:41 +000034 if (strokeWidth > 0) {
35 p.setStyle(SkPaint::kFill_Style);
Mike Reed088b74e2018-12-24 14:52:46 -050036 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);
commit-bot@chromium.org74266812014-05-22 17:41:41 +000038 }
skia.committer@gmail.comaffa77d2014-03-13 03:02:23 +000039
commit-bot@chromium.org641e33b2014-03-12 20:31:24 +000040 p.setColor(SK_ColorRED);
41 p.setStyle(SkPaint::kStroke_Style);
commit-bot@chromium.org74266812014-05-22 17:41:41 +000042 p.setStrokeWidth(strokeWidth);
skia.committer@gmail.comaffa77d2014-03-13 03:02:23 +000043
Mike Reed088b74e2018-12-24 14:52:46 -050044 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);
commit-bot@chromium.org641e33b2014-03-12 20:31:24 +000046}
47
Mike Reed088b74e2018-12-24 14:52:46 -050048static void draw_text_set(SkCanvas* canvas, const SkPaint& paint, const SkFont& font) {
commit-bot@chromium.org74266812014-05-22 17:41:41 +000049 SkAutoCanvasRestore acr(canvas, true);
50
Mike Reed088b74e2018-12-24 14:52:46 -050051 draw_text_stroked(canvas, paint, font, 10);
commit-bot@chromium.org74266812014-05-22 17:41:41 +000052
53 canvas->translate(200, 0);
Mike Reed088b74e2018-12-24 14:52:46 -050054 draw_text_stroked(canvas, paint, font, 0);
commit-bot@chromium.org74266812014-05-22 17:41:41 +000055
56 const SkScalar intervals[] = { 20, 10, 5, 10 };
57 const SkScalar phase = 0;
58
59 canvas->translate(200, 0);
60 SkPaint p(paint);
reeda4393342016-03-18 11:22:57 -070061 p.setPathEffect(SkDashPathEffect::Make(intervals, SK_ARRAY_COUNT(intervals), phase));
Mike Reed088b74e2018-12-24 14:52:46 -050062 draw_text_stroked(canvas, p, font, 10);
commit-bot@chromium.org74266812014-05-22 17:41:41 +000063}
64
halcanary2a243382015-09-09 08:16:41 -070065namespace {
commit-bot@chromium.org641e33b2014-03-12 20:31:24 +000066 enum {
67 kBelowThreshold_TextSize = 255,
68 kAboveThreshold_TextSize = 257
69 };
halcanary2a243382015-09-09 08:16:41 -070070}
commit-bot@chromium.org641e33b2014-03-12 20:31:24 +000071
halcanary2a243382015-09-09 08:16:41 -070072DEF_SIMPLE_GM(stroketext, canvas, 1200, 480) {
Mike Reed088b74e2018-12-24 14:52:46 -050073 if (true) { test_nulldev(canvas); }
skia.committer@gmail.comaffa77d2014-03-13 03:02:23 +000074
Mike Reed088b74e2018-12-24 14:52:46 -050075 SkPaint paint;
76 paint.setAntiAlias(true);
skia.committer@gmail.comaffa77d2014-03-13 03:02:23 +000077
Mike Reed088b74e2018-12-24 14:52:46 -050078 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);
halcanary2a243382015-09-09 08:16:41 -070084}