aboutsummaryrefslogtreecommitdiffhomepage
path: root/gm/internal_links.cpp
blob: 6d70c1ef981592fd62871e0e9650b498214f0963 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*
 * Copyright 2013 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */
#include "gm.h"

#include "SkAnnotation.h"
#include "SkData.h"

namespace skiagm {

/** Draws two rectangles. In output formats that support internal links (PDF),
 *  clicking the one labeled "Link to A" should take you to the one labeled
 *  "Target A". Note that you'll need to zoom your PDF viewer in a fair bit in
 *  order for the scrolling to not be blocked by the edge of the document.
 */
class InternalLinksGM : public GM {
public:
    InternalLinksGM() {
        this->setBGColor(sk_tool_utils::color_to_565(0xFFDDDDDD));
    }

protected:
    virtual SkString onShortName() {
        return SkString("internal_links");
    }

    virtual SkISize onISize() {
        return SkISize::Make(700, 500);
    }

    virtual void onDraw(SkCanvas* canvas) {
        sk_sp<SkData> name(SkData::MakeWithCString("target-a"));

        canvas->save();
        canvas->translate(SkIntToScalar(100), SkIntToScalar(100));
        drawLabeledRect(canvas, "Link to A", 0, 0);
        SkRect rect = SkRect::MakeXYWH(0, 0, SkIntToScalar(50), SkIntToScalar(20));
        SkAnnotateLinkToDestination(canvas, rect, name.get());
        canvas->restore();

        canvas->save();
        canvas->translate(SkIntToScalar(200), SkIntToScalar(200));
        SkPoint point = SkPoint::Make(SkIntToScalar(100), SkIntToScalar(50));
        drawLabeledRect(canvas, "Target A", point.x(), point.y());
        SkAnnotateNamedDestination(canvas, point, name.get());
        canvas->restore();
    }

private:
    /** Draw an arbitrary rectangle at a given location and label it with some
     *  text. */
    void drawLabeledRect(SkCanvas* canvas, const char* text, SkScalar x, SkScalar y) {
        SkPaint paint;
        paint.setColor(SK_ColorBLUE);
        SkRect rect = SkRect::MakeXYWH(x, y,
                                       SkIntToScalar(50), SkIntToScalar(20));
        canvas->drawRect(rect, paint);

        paint.setAntiAlias(true);
        sk_tool_utils::set_portable_typeface(&paint);
        paint.setTextSize(SkIntToScalar(25));
        paint.setColor(SK_ColorBLACK);
        canvas->drawText(text, strlen(text), x, y, paint);
    }

    typedef GM INHERITED;
};

//////////////////////////////////////////////////////////////////////////////

static GM* MyFactory(void*) { return new InternalLinksGM; }
static GMRegistry reg(MyFactory);

}