epoger@google.com | b58772f | 2013-03-08 09:09:10 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
| 12 | namespace 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 | */ |
| 19 | class InternalLinksGM : public GM { |
| 20 | public: |
| 21 | InternalLinksGM() { |
caryclark | 65cdba6 | 2015-06-15 06:51:08 -0700 | [diff] [blame] | 22 | this->setBGColor(sk_tool_utils::color_to_565(0xFFDDDDDD)); |
epoger@google.com | b58772f | 2013-03-08 09:09:10 +0000 | [diff] [blame] | 23 | } |
| 24 | |
| 25 | protected: |
| 26 | virtual SkString onShortName() { |
| 27 | return SkString("internal_links"); |
| 28 | } |
| 29 | |
| 30 | virtual SkISize onISize() { |
tfarina | f539318 | 2014-06-09 23:59:03 -0700 | [diff] [blame] | 31 | return SkISize::Make(700, 500); |
epoger@google.com | b58772f | 2013-03-08 09:09:10 +0000 | [diff] [blame] | 32 | } |
| 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.com | b58772f | 2013-03-08 09:09:10 +0000 | [diff] [blame] | 52 | private: |
| 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); |
caryclark | 1818acb | 2015-07-24 12:09:25 -0700 | [diff] [blame] | 63 | sk_tool_utils::set_portable_typeface(&paint); |
epoger@google.com | b58772f | 2013-03-08 09:09:10 +0000 | [diff] [blame] | 64 | 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 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 74 | static GM* MyFactory(void*) { return new InternalLinksGM; } |
epoger@google.com | b58772f | 2013-03-08 09:09:10 +0000 | [diff] [blame] | 75 | static GMRegistry reg(MyFactory); |
| 76 | |
| 77 | } |