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 | */ |
epoger@google.com | b58772f | 2013-03-08 09:09:10 +0000 | [diff] [blame] | 7 | |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 8 | #include "gm/gm.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 9 | #include "include/core/SkAnnotation.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 10 | #include "include/core/SkCanvas.h" |
| 11 | #include "include/core/SkColor.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 12 | #include "include/core/SkData.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 13 | #include "include/core/SkFont.h" |
| 14 | #include "include/core/SkPaint.h" |
| 15 | #include "include/core/SkPoint.h" |
| 16 | #include "include/core/SkRect.h" |
| 17 | #include "include/core/SkRefCnt.h" |
| 18 | #include "include/core/SkScalar.h" |
| 19 | #include "include/core/SkSize.h" |
| 20 | #include "include/core/SkString.h" |
| 21 | #include "include/core/SkTypeface.h" |
| 22 | #include "tools/ToolUtils.h" |
Kevin Lubick | e836c3a | 2023-10-20 06:55:35 -0400 | [diff] [blame] | 23 | #include "tools/fonts/FontToolUtils.h" |
epoger@google.com | b58772f | 2013-03-08 09:09:10 +0000 | [diff] [blame] | 24 | |
Hal Canary | bd865e2 | 2019-07-18 11:51:19 -0400 | [diff] [blame] | 25 | namespace { |
epoger@google.com | b58772f | 2013-03-08 09:09:10 +0000 | [diff] [blame] | 26 | |
| 27 | /** Draws two rectangles. In output formats that support internal links (PDF), |
| 28 | * clicking the one labeled "Link to A" should take you to the one labeled |
| 29 | * "Target A". Note that you'll need to zoom your PDF viewer in a fair bit in |
| 30 | * order for the scrolling to not be blocked by the edge of the document. |
| 31 | */ |
Hal Canary | bd865e2 | 2019-07-18 11:51:19 -0400 | [diff] [blame] | 32 | class InternalLinksGM : public skiagm::GM { |
| 33 | void onOnceBeforeDraw() override { this->setBGColor(0xFFDDDDDD); } |
epoger@google.com | b58772f | 2013-03-08 09:09:10 +0000 | [diff] [blame] | 34 | |
Leandro Lovisolo | 24fa211 | 2023-08-15 19:05:17 +0000 | [diff] [blame] | 35 | SkString getName() const override { return SkString("internal_links"); } |
epoger@google.com | b58772f | 2013-03-08 09:09:10 +0000 | [diff] [blame] | 36 | |
Leandro Lovisolo | 8f02388 | 2023-08-15 21:13:52 +0000 | [diff] [blame] | 37 | SkISize getISize() override { return {700, 500}; } |
epoger@google.com | b58772f | 2013-03-08 09:09:10 +0000 | [diff] [blame] | 38 | |
Hal Canary | bd865e2 | 2019-07-18 11:51:19 -0400 | [diff] [blame] | 39 | void onDraw(SkCanvas* canvas) override { |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 40 | sk_sp<SkData> name(SkData::MakeWithCString("target-a")); |
epoger@google.com | b58772f | 2013-03-08 09:09:10 +0000 | [diff] [blame] | 41 | |
| 42 | canvas->save(); |
| 43 | canvas->translate(SkIntToScalar(100), SkIntToScalar(100)); |
| 44 | drawLabeledRect(canvas, "Link to A", 0, 0); |
| 45 | SkRect rect = SkRect::MakeXYWH(0, 0, SkIntToScalar(50), SkIntToScalar(20)); |
| 46 | SkAnnotateLinkToDestination(canvas, rect, name.get()); |
| 47 | canvas->restore(); |
| 48 | |
| 49 | canvas->save(); |
| 50 | canvas->translate(SkIntToScalar(200), SkIntToScalar(200)); |
| 51 | SkPoint point = SkPoint::Make(SkIntToScalar(100), SkIntToScalar(50)); |
| 52 | drawLabeledRect(canvas, "Target A", point.x(), point.y()); |
| 53 | SkAnnotateNamedDestination(canvas, point, name.get()); |
| 54 | canvas->restore(); |
| 55 | } |
| 56 | |
epoger@google.com | b58772f | 2013-03-08 09:09:10 +0000 | [diff] [blame] | 57 | /** Draw an arbitrary rectangle at a given location and label it with some |
| 58 | * text. */ |
| 59 | void drawLabeledRect(SkCanvas* canvas, const char* text, SkScalar x, SkScalar y) { |
| 60 | SkPaint paint; |
| 61 | paint.setColor(SK_ColorBLUE); |
| 62 | SkRect rect = SkRect::MakeXYWH(x, y, |
| 63 | SkIntToScalar(50), SkIntToScalar(20)); |
| 64 | canvas->drawRect(rect, paint); |
| 65 | |
Kevin Lubick | e836c3a | 2023-10-20 06:55:35 -0400 | [diff] [blame] | 66 | SkFont font(ToolUtils::DefaultPortableTypeface(), 25); |
epoger@google.com | b58772f | 2013-03-08 09:09:10 +0000 | [diff] [blame] | 67 | paint.setColor(SK_ColorBLACK); |
Mike Reed | 1af9b48 | 2019-01-07 11:01:57 -0500 | [diff] [blame] | 68 | canvas->drawString(text, x, y, font, paint); |
epoger@google.com | b58772f | 2013-03-08 09:09:10 +0000 | [diff] [blame] | 69 | } |
epoger@google.com | b58772f | 2013-03-08 09:09:10 +0000 | [diff] [blame] | 70 | }; |
Hal Canary | bd865e2 | 2019-07-18 11:51:19 -0400 | [diff] [blame] | 71 | } // namespace |
epoger@google.com | b58772f | 2013-03-08 09:09:10 +0000 | [diff] [blame] | 72 | |
Hal Canary | e964c18 | 2019-01-23 10:22:01 -0500 | [diff] [blame] | 73 | DEF_GM( return new InternalLinksGM; ) |