blob: 995cd69d421ea39af79a9b50a66a3aca8676b232 [file] [log] [blame]
epoger@google.comb58772f2013-03-08 09:09:10 +00001/*
2 * Copyright 2013 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#include "gm.h"
8
9#include "SkAnnotation.h"
10#include "SkData.h"
11
12namespace skiagm {
13
14/** Draws two rectangles. In output formats that support internal links (PDF),
15 * clicking the one labeled "Link to A" should take you to the one labeled
16 * "Target A". Note that you'll need to zoom your PDF viewer in a fair bit in
17 * order for the scrolling to not be blocked by the edge of the document.
18 */
19class InternalLinksGM : public GM {
20public:
21 InternalLinksGM() {
caryclark65cdba62015-06-15 06:51:08 -070022 this->setBGColor(sk_tool_utils::color_to_565(0xFFDDDDDD));
epoger@google.comb58772f2013-03-08 09:09:10 +000023 }
24
25protected:
26 virtual SkString onShortName() {
27 return SkString("internal_links");
28 }
29
30 virtual SkISize onISize() {
tfarinaf5393182014-06-09 23:59:03 -070031 return SkISize::Make(700, 500);
epoger@google.comb58772f2013-03-08 09:09:10 +000032 }
33
34 virtual void onDraw(SkCanvas* canvas) {
35 SkAutoTUnref<SkData> name(SkData::NewWithCString("target-a"));
36
37 canvas->save();
38 canvas->translate(SkIntToScalar(100), SkIntToScalar(100));
39 drawLabeledRect(canvas, "Link to A", 0, 0);
40 SkRect rect = SkRect::MakeXYWH(0, 0, SkIntToScalar(50), SkIntToScalar(20));
41 SkAnnotateLinkToDestination(canvas, rect, name.get());
42 canvas->restore();
43
44 canvas->save();
45 canvas->translate(SkIntToScalar(200), SkIntToScalar(200));
46 SkPoint point = SkPoint::Make(SkIntToScalar(100), SkIntToScalar(50));
47 drawLabeledRect(canvas, "Target A", point.x(), point.y());
48 SkAnnotateNamedDestination(canvas, point, name.get());
49 canvas->restore();
50 }
51
epoger@google.comb58772f2013-03-08 09:09:10 +000052private:
53 /** Draw an arbitrary rectangle at a given location and label it with some
54 * text. */
55 void drawLabeledRect(SkCanvas* canvas, const char* text, SkScalar x, SkScalar y) {
56 SkPaint paint;
57 paint.setColor(SK_ColorBLUE);
58 SkRect rect = SkRect::MakeXYWH(x, y,
59 SkIntToScalar(50), SkIntToScalar(20));
60 canvas->drawRect(rect, paint);
61
62 paint.setAntiAlias(true);
caryclark1818acb2015-07-24 12:09:25 -070063 sk_tool_utils::set_portable_typeface(&paint);
epoger@google.comb58772f2013-03-08 09:09:10 +000064 paint.setTextSize(SkIntToScalar(25));
65 paint.setColor(SK_ColorBLACK);
66 canvas->drawText(text, strlen(text), x, y, paint);
67 }
68
69 typedef GM INHERITED;
70};
71
72//////////////////////////////////////////////////////////////////////////////
73
halcanary385fe4d2015-08-26 13:07:48 -070074static GM* MyFactory(void*) { return new InternalLinksGM; }
epoger@google.comb58772f2013-03-08 09:09:10 +000075static GMRegistry reg(MyFactory);
76
77}