1641e33b3f6014b193856ba5789b1a0278a92e083commit-bot@chromium.org/*
2641e33b3f6014b193856ba5789b1a0278a92e083commit-bot@chromium.org * Copyright 2014 Google Inc.
3641e33b3f6014b193856ba5789b1a0278a92e083commit-bot@chromium.org *
4641e33b3f6014b193856ba5789b1a0278a92e083commit-bot@chromium.org * Use of this source code is governed by a BSD-style license that can be
5641e33b3f6014b193856ba5789b1a0278a92e083commit-bot@chromium.org * found in the LICENSE file.
6641e33b3f6014b193856ba5789b1a0278a92e083commit-bot@chromium.org */
7641e33b3f6014b193856ba5789b1a0278a92e083commit-bot@chromium.org
8641e33b3f6014b193856ba5789b1a0278a92e083commit-bot@chromium.org#include "gm.h"
9641e33b3f6014b193856ba5789b1a0278a92e083commit-bot@chromium.org#include "SkCanvas.h"
1074266814a0d0fd5b0ec9be664be6b629aeddc0eacommit-bot@chromium.org#include "SkDashPathEffect.h"
11641e33b3f6014b193856ba5789b1a0278a92e083commit-bot@chromium.org
120d30c51c6cf45b3a08a3000b6d348c16bdec7f05reed@google.comstatic void test_nulldev(SkCanvas* canvas) {
130d30c51c6cf45b3a08a3000b6d348c16bdec7f05reed@google.com    SkBitmap bm;
146c22573edb234ad14df947278cfed010669a39a7reed    bm.setInfo(SkImageInfo::MakeN32Premul(30, 30));
150d30c51c6cf45b3a08a3000b6d348c16bdec7f05reed@google.com    // notice: no pixels mom! be sure we don't crash
160d30c51c6cf45b3a08a3000b6d348c16bdec7f05reed@google.com    // https://code.google.com/p/chromium/issues/detail?id=352616
170d30c51c6cf45b3a08a3000b6d348c16bdec7f05reed@google.com    SkCanvas c(bm);
180d30c51c6cf45b3a08a3000b6d348c16bdec7f05reed@google.com
190d30c51c6cf45b3a08a3000b6d348c16bdec7f05reed@google.com    SkBitmap src;
200d30c51c6cf45b3a08a3000b6d348c16bdec7f05reed@google.com    src.allocN32Pixels(10, 10);
210d30c51c6cf45b3a08a3000b6d348c16bdec7f05reed@google.com    src.eraseColor(SK_ColorRED);
220d30c51c6cf45b3a08a3000b6d348c16bdec7f05reed@google.com
230d30c51c6cf45b3a08a3000b6d348c16bdec7f05reed@google.com    // ensure we don't crash
240d30c51c6cf45b3a08a3000b6d348c16bdec7f05reed@google.com    c.writePixels(src, 0, 0);
250d30c51c6cf45b3a08a3000b6d348c16bdec7f05reed@google.com}
260d30c51c6cf45b3a08a3000b6d348c16bdec7f05reed@google.com
2774266814a0d0fd5b0ec9be664be6b629aeddc0eacommit-bot@chromium.orgstatic void draw_text_stroked(SkCanvas* canvas, const SkPaint& paint, SkScalar strokeWidth) {
28641e33b3f6014b193856ba5789b1a0278a92e083commit-bot@chromium.org    SkPaint p(paint);
2974266814a0d0fd5b0ec9be664be6b629aeddc0eacommit-bot@chromium.org    SkPoint loc = { 20, 435 };
30affa77db64b75b9d32c7e42282bcc2e4eb2a94c3skia.committer@gmail.com
3174266814a0d0fd5b0ec9be664be6b629aeddc0eacommit-bot@chromium.org    if (strokeWidth > 0) {
3274266814a0d0fd5b0ec9be664be6b629aeddc0eacommit-bot@chromium.org        p.setStyle(SkPaint::kFill_Style);
3374266814a0d0fd5b0ec9be664be6b629aeddc0eacommit-bot@chromium.org        canvas->drawText("P", 1, loc.fX, loc.fY - 225, p);
3474266814a0d0fd5b0ec9be664be6b629aeddc0eacommit-bot@chromium.org        canvas->drawPosText("P", 1, &loc, p);
3574266814a0d0fd5b0ec9be664be6b629aeddc0eacommit-bot@chromium.org    }
36affa77db64b75b9d32c7e42282bcc2e4eb2a94c3skia.committer@gmail.com
37641e33b3f6014b193856ba5789b1a0278a92e083commit-bot@chromium.org    p.setColor(SK_ColorRED);
38641e33b3f6014b193856ba5789b1a0278a92e083commit-bot@chromium.org    p.setStyle(SkPaint::kStroke_Style);
3974266814a0d0fd5b0ec9be664be6b629aeddc0eacommit-bot@chromium.org    p.setStrokeWidth(strokeWidth);
40affa77db64b75b9d32c7e42282bcc2e4eb2a94c3skia.committer@gmail.com
41641e33b3f6014b193856ba5789b1a0278a92e083commit-bot@chromium.org    canvas->drawText("P", 1, loc.fX, loc.fY - 225, p);
42641e33b3f6014b193856ba5789b1a0278a92e083commit-bot@chromium.org    canvas->drawPosText("P", 1, &loc, p);
43641e33b3f6014b193856ba5789b1a0278a92e083commit-bot@chromium.org}
44641e33b3f6014b193856ba5789b1a0278a92e083commit-bot@chromium.org
4574266814a0d0fd5b0ec9be664be6b629aeddc0eacommit-bot@chromium.orgstatic void draw_text_set(SkCanvas* canvas, const SkPaint& paint) {
4674266814a0d0fd5b0ec9be664be6b629aeddc0eacommit-bot@chromium.org    SkAutoCanvasRestore acr(canvas, true);
4774266814a0d0fd5b0ec9be664be6b629aeddc0eacommit-bot@chromium.org
4874266814a0d0fd5b0ec9be664be6b629aeddc0eacommit-bot@chromium.org    draw_text_stroked(canvas, paint, 10);
4974266814a0d0fd5b0ec9be664be6b629aeddc0eacommit-bot@chromium.org
5074266814a0d0fd5b0ec9be664be6b629aeddc0eacommit-bot@chromium.org    canvas->translate(200, 0);
5174266814a0d0fd5b0ec9be664be6b629aeddc0eacommit-bot@chromium.org    draw_text_stroked(canvas, paint, 0);
5274266814a0d0fd5b0ec9be664be6b629aeddc0eacommit-bot@chromium.org
5374266814a0d0fd5b0ec9be664be6b629aeddc0eacommit-bot@chromium.org    const SkScalar intervals[] = { 20, 10, 5, 10 };
5474266814a0d0fd5b0ec9be664be6b629aeddc0eacommit-bot@chromium.org    const SkScalar phase = 0;
5574266814a0d0fd5b0ec9be664be6b629aeddc0eacommit-bot@chromium.org
5674266814a0d0fd5b0ec9be664be6b629aeddc0eacommit-bot@chromium.org    canvas->translate(200, 0);
5774266814a0d0fd5b0ec9be664be6b629aeddc0eacommit-bot@chromium.org    SkPaint p(paint);
5874266814a0d0fd5b0ec9be664be6b629aeddc0eacommit-bot@chromium.org    p.setPathEffect(SkDashPathEffect::Create(intervals, SK_ARRAY_COUNT(intervals), phase))->unref();
597cb5e4797ed7ecb200f90889687a72940efa6d98reed@google.com    draw_text_stroked(canvas, p, 10);
6074266814a0d0fd5b0ec9be664be6b629aeddc0eacommit-bot@chromium.org}
6174266814a0d0fd5b0ec9be664be6b629aeddc0eacommit-bot@chromium.org
62641e33b3f6014b193856ba5789b1a0278a92e083commit-bot@chromium.orgclass StrokeTextGM : public skiagm::GM {
63641e33b3f6014b193856ba5789b1a0278a92e083commit-bot@chromium.org    // Skia has a threshold above which it draws text via paths instead of using scalercontext
64641e33b3f6014b193856ba5789b1a0278a92e083commit-bot@chromium.org    // and caching the glyph. This GM wants to ensure that we draw stroking correctly on both
65641e33b3f6014b193856ba5789b1a0278a92e083commit-bot@chromium.org    // sides of this threshold.
66641e33b3f6014b193856ba5789b1a0278a92e083commit-bot@chromium.org    enum {
67641e33b3f6014b193856ba5789b1a0278a92e083commit-bot@chromium.org        kBelowThreshold_TextSize = 255,
68641e33b3f6014b193856ba5789b1a0278a92e083commit-bot@chromium.org        kAboveThreshold_TextSize = 257
69641e33b3f6014b193856ba5789b1a0278a92e083commit-bot@chromium.org    };
70641e33b3f6014b193856ba5789b1a0278a92e083commit-bot@chromium.orgpublic:
71641e33b3f6014b193856ba5789b1a0278a92e083commit-bot@chromium.org    StrokeTextGM() {}
72641e33b3f6014b193856ba5789b1a0278a92e083commit-bot@chromium.org
73641e33b3f6014b193856ba5789b1a0278a92e083commit-bot@chromium.orgprotected:
74a90c6803865766d28e92091f56f718f5e41fe80fcommit-bot@chromium.org
7536352bf5e38f45a70ee4f4fc132a38048d38206dmtklein    SkString onShortName() override {
76641e33b3f6014b193856ba5789b1a0278a92e083commit-bot@chromium.org        return SkString("stroketext");
77641e33b3f6014b193856ba5789b1a0278a92e083commit-bot@chromium.org    }
78641e33b3f6014b193856ba5789b1a0278a92e083commit-bot@chromium.org
7936352bf5e38f45a70ee4f4fc132a38048d38206dmtklein    SkISize onISize() override {
8074266814a0d0fd5b0ec9be664be6b629aeddc0eacommit-bot@chromium.org        return SkISize::Make(1200, 480);
81641e33b3f6014b193856ba5789b1a0278a92e083commit-bot@chromium.org    }
82641e33b3f6014b193856ba5789b1a0278a92e083commit-bot@chromium.org
8336352bf5e38f45a70ee4f4fc132a38048d38206dmtklein    void onDraw(SkCanvas* canvas) override {
840d30c51c6cf45b3a08a3000b6d348c16bdec7f05reed@google.com        if (true) { test_nulldev(canvas); }
85641e33b3f6014b193856ba5789b1a0278a92e083commit-bot@chromium.org        SkPaint paint;
86641e33b3f6014b193856ba5789b1a0278a92e083commit-bot@chromium.org        paint.setAntiAlias(true);
87992c7b03ef7914a18bfd78e965b0b4c99a5f5672Cary Clark        sk_tool_utils::set_portable_typeface(&paint);
88affa77db64b75b9d32c7e42282bcc2e4eb2a94c3skia.committer@gmail.com
89641e33b3f6014b193856ba5789b1a0278a92e083commit-bot@chromium.org        paint.setTextSize(kBelowThreshold_TextSize);
9074266814a0d0fd5b0ec9be664be6b629aeddc0eacommit-bot@chromium.org        draw_text_set(canvas, paint);
91affa77db64b75b9d32c7e42282bcc2e4eb2a94c3skia.committer@gmail.com
9274266814a0d0fd5b0ec9be664be6b629aeddc0eacommit-bot@chromium.org        canvas->translate(600, 0);
93641e33b3f6014b193856ba5789b1a0278a92e083commit-bot@chromium.org        paint.setTextSize(kAboveThreshold_TextSize);
9474266814a0d0fd5b0ec9be664be6b629aeddc0eacommit-bot@chromium.org        draw_text_set(canvas, paint);
95641e33b3f6014b193856ba5789b1a0278a92e083commit-bot@chromium.org    }
96641e33b3f6014b193856ba5789b1a0278a92e083commit-bot@chromium.org
97641e33b3f6014b193856ba5789b1a0278a92e083commit-bot@chromium.orgprivate:
98641e33b3f6014b193856ba5789b1a0278a92e083commit-bot@chromium.org    typedef skiagm::GM INHERITED;
99641e33b3f6014b193856ba5789b1a0278a92e083commit-bot@chromium.org};
100641e33b3f6014b193856ba5789b1a0278a92e083commit-bot@chromium.org
101641e33b3f6014b193856ba5789b1a0278a92e083commit-bot@chromium.orgDEF_GM( return SkNEW(StrokeTextGM); )
102