reed@google.com | b0a34d8 | 2012-07-11 19:57:55 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011 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 | */ |
Ben Wagner | 1ebeefe | 2018-03-02 16:59:53 -0500 | [diff] [blame] | 7 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "include/core/SkAnnotation.h" |
| 9 | #include "include/core/SkBitmap.h" |
| 10 | #include "include/core/SkCanvas.h" |
| 11 | #include "include/core/SkColor.h" |
| 12 | #include "include/core/SkData.h" |
Ben Wagner | 9707a7e | 2019-05-06 17:17:19 -0400 | [diff] [blame] | 13 | #include "include/core/SkDocument.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 14 | #include "include/core/SkPoint.h" |
| 15 | #include "include/core/SkRect.h" |
| 16 | #include "include/core/SkRefCnt.h" |
| 17 | #include "include/core/SkScalar.h" |
| 18 | #include "include/core/SkStream.h" |
| 19 | #include "include/core/SkTypes.h" |
| 20 | #include "include/docs/SkPDFDocument.h" |
| 21 | #include "tests/Test.h" |
reed@google.com | b0a34d8 | 2012-07-11 19:57:55 +0000 | [diff] [blame] | 22 | |
Ben Wagner | 1ebeefe | 2018-03-02 16:59:53 -0500 | [diff] [blame] | 23 | #include <string.h> |
| 24 | #include <memory> |
| 25 | |
epoger@google.com | b58772f | 2013-03-08 09:09:10 +0000 | [diff] [blame] | 26 | /** Returns true if data (may contain null characters) contains needle (null |
| 27 | * terminated). */ |
| 28 | static bool ContainsString(const char* data, size_t dataSize, const char* needle) { |
| 29 | size_t nSize = strlen(needle); |
| 30 | for (size_t i = 0; i < dataSize - nSize; i++) { |
| 31 | if (strncmp(&data[i], needle, nSize) == 0) { |
| 32 | return true; |
| 33 | } |
| 34 | } |
| 35 | return false; |
| 36 | } |
| 37 | |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 38 | DEF_TEST(Annotation_NoDraw, reporter) { |
reed@google.com | b0a34d8 | 2012-07-11 19:57:55 +0000 | [diff] [blame] | 39 | SkBitmap bm; |
commit-bot@chromium.org | fa9e5fa | 2014-02-13 22:00:04 +0000 | [diff] [blame] | 40 | bm.allocN32Pixels(10, 10); |
junov@google.com | dbfac8a | 2012-12-06 21:47:40 +0000 | [diff] [blame] | 41 | bm.eraseColor(SK_ColorTRANSPARENT); |
reed@google.com | b0a34d8 | 2012-07-11 19:57:55 +0000 | [diff] [blame] | 42 | |
| 43 | SkCanvas canvas(bm); |
| 44 | SkRect r = SkRect::MakeWH(SkIntToScalar(10), SkIntToScalar(10)); |
| 45 | |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 46 | sk_sp<SkData> data(SkData::MakeWithCString("http://www.gooogle.com")); |
reed@google.com | b0a34d8 | 2012-07-11 19:57:55 +0000 | [diff] [blame] | 47 | |
| 48 | REPORTER_ASSERT(reporter, 0 == *bm.getAddr32(0, 0)); |
| 49 | SkAnnotateRectWithURL(&canvas, r, data.get()); |
| 50 | REPORTER_ASSERT(reporter, 0 == *bm.getAddr32(0, 0)); |
| 51 | } |
| 52 | |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 53 | DEF_TEST(Annotation_PdfLink, reporter) { |
halcanary | 2ccdb63 | 2015-08-11 13:35:12 -0700 | [diff] [blame] | 54 | REQUIRE_PDF_DOCUMENT(Annotation_PdfLink, reporter); |
halcanary | 3d32d50 | 2015-03-01 06:55:20 -0800 | [diff] [blame] | 55 | SkDynamicMemoryWStream outStream; |
Hal Canary | 3026d4b | 2019-01-07 10:00:48 -0500 | [diff] [blame] | 56 | auto doc = SkPDF::MakeDocument(&outStream); |
halcanary | 3d32d50 | 2015-03-01 06:55:20 -0800 | [diff] [blame] | 57 | SkCanvas* canvas = doc->beginPage(612.0f, 792.0f); |
| 58 | REPORTER_ASSERT(reporter, canvas); |
vandebo@chromium.org | 238be8c | 2012-07-13 20:06:02 +0000 | [diff] [blame] | 59 | |
| 60 | SkRect r = SkRect::MakeXYWH(SkIntToScalar(72), SkIntToScalar(72), |
| 61 | SkIntToScalar(288), SkIntToScalar(72)); |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 62 | sk_sp<SkData> data(SkData::MakeWithCString("http://www.gooogle.com")); |
halcanary | 3d32d50 | 2015-03-01 06:55:20 -0800 | [diff] [blame] | 63 | SkAnnotateRectWithURL(canvas, r, data.get()); |
vandebo@chromium.org | 238be8c | 2012-07-13 20:06:02 +0000 | [diff] [blame] | 64 | |
reed | d14df7c | 2016-09-22 14:12:46 -0700 | [diff] [blame] | 65 | doc->close(); |
reed | 42943c8 | 2016-09-12 12:01:44 -0700 | [diff] [blame] | 66 | sk_sp<SkData> out = outStream.detachAsData(); |
mtklein | cabc08c | 2015-02-19 08:29:24 -0800 | [diff] [blame] | 67 | const char* rawOutput = (const char*)out->data(); |
vandebo@chromium.org | 238be8c | 2012-07-13 20:06:02 +0000 | [diff] [blame] | 68 | |
mtklein | cabc08c | 2015-02-19 08:29:24 -0800 | [diff] [blame] | 69 | REPORTER_ASSERT(reporter, ContainsString(rawOutput, out->size(), "/Annots ")); |
vandebo@chromium.org | 238be8c | 2012-07-13 20:06:02 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Bryce Thomas | fd5a508 | 2018-02-06 14:53:05 -0800 | [diff] [blame] | 72 | DEF_TEST(Annotation_PdfDefineNamedDestination, reporter) { |
| 73 | REQUIRE_PDF_DOCUMENT(Annotation_PdfNamedDestination, reporter); |
halcanary | 3d32d50 | 2015-03-01 06:55:20 -0800 | [diff] [blame] | 74 | SkDynamicMemoryWStream outStream; |
Hal Canary | 3026d4b | 2019-01-07 10:00:48 -0500 | [diff] [blame] | 75 | auto doc = SkPDF::MakeDocument(&outStream); |
halcanary | 3d32d50 | 2015-03-01 06:55:20 -0800 | [diff] [blame] | 76 | SkCanvas* canvas = doc->beginPage(612.0f, 792.0f); |
| 77 | REPORTER_ASSERT(reporter, canvas); |
epoger@google.com | b58772f | 2013-03-08 09:09:10 +0000 | [diff] [blame] | 78 | |
| 79 | SkPoint p = SkPoint::Make(SkIntToScalar(72), SkIntToScalar(72)); |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 80 | sk_sp<SkData> data(SkData::MakeWithCString("example")); |
halcanary | 3d32d50 | 2015-03-01 06:55:20 -0800 | [diff] [blame] | 81 | SkAnnotateNamedDestination(canvas, p, data.get()); |
epoger@google.com | b58772f | 2013-03-08 09:09:10 +0000 | [diff] [blame] | 82 | |
reed | d14df7c | 2016-09-22 14:12:46 -0700 | [diff] [blame] | 83 | doc->close(); |
reed | 42943c8 | 2016-09-12 12:01:44 -0700 | [diff] [blame] | 84 | sk_sp<SkData> out = outStream.detachAsData(); |
epoger@google.com | b58772f | 2013-03-08 09:09:10 +0000 | [diff] [blame] | 85 | const char* rawOutput = (const char*)out->data(); |
| 86 | |
| 87 | REPORTER_ASSERT(reporter, |
| 88 | ContainsString(rawOutput, out->size(), "/example ")); |
| 89 | } |
Bryce Thomas | fd5a508 | 2018-02-06 14:53:05 -0800 | [diff] [blame] | 90 | |
Mike Klein | 9ca602a | 2018-02-06 19:06:11 -0500 | [diff] [blame] | 91 | #if defined(SK_XML) |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 92 | #include "include/svg/SkSVGCanvas.h" |
Bryce Thomas | fd5a508 | 2018-02-06 14:53:05 -0800 | [diff] [blame] | 93 | |
Mike Klein | 9ca602a | 2018-02-06 19:06:11 -0500 | [diff] [blame] | 94 | DEF_TEST(Annotation_SvgLink, reporter) { |
| 95 | SkDynamicMemoryWStream outStream; |
Mike Klein | 9ca602a | 2018-02-06 19:06:11 -0500 | [diff] [blame] | 96 | SkRect bounds = SkRect::MakeIWH(400, 400); |
Florin Malita | 562017b | 2019-02-14 13:42:15 -0500 | [diff] [blame] | 97 | std::unique_ptr<SkCanvas> canvas = SkSVGCanvas::Make(bounds, &outStream); |
Bryce Thomas | fd5a508 | 2018-02-06 14:53:05 -0800 | [diff] [blame] | 98 | |
Mike Klein | 9ca602a | 2018-02-06 19:06:11 -0500 | [diff] [blame] | 99 | SkRect r = SkRect::MakeXYWH(SkIntToScalar(72), SkIntToScalar(72), SkIntToScalar(288), |
| 100 | SkIntToScalar(72)); |
| 101 | sk_sp<SkData> data(SkData::MakeWithCString("http://www.gooogle.com")); |
| 102 | SkAnnotateRectWithURL(canvas.get(), r, data.get()); |
Bryce Thomas | fd5a508 | 2018-02-06 14:53:05 -0800 | [diff] [blame] | 103 | |
Mike Klein | 9ca602a | 2018-02-06 19:06:11 -0500 | [diff] [blame] | 104 | sk_sp<SkData> out = outStream.detachAsData(); |
| 105 | const char* rawOutput = (const char*)out->data(); |
Bryce Thomas | fd5a508 | 2018-02-06 14:53:05 -0800 | [diff] [blame] | 106 | |
Mike Klein | 9ca602a | 2018-02-06 19:06:11 -0500 | [diff] [blame] | 107 | REPORTER_ASSERT(reporter, |
| 108 | ContainsString(rawOutput, out->size(), "a xlink:href=\"http://www.gooogle.com\"")); |
| 109 | } |
Bryce Thomas | fd5a508 | 2018-02-06 14:53:05 -0800 | [diff] [blame] | 110 | |
Mike Klein | 9ca602a | 2018-02-06 19:06:11 -0500 | [diff] [blame] | 111 | DEF_TEST(Annotation_SvgLinkNamedDestination, reporter) { |
| 112 | SkDynamicMemoryWStream outStream; |
Mike Klein | 9ca602a | 2018-02-06 19:06:11 -0500 | [diff] [blame] | 113 | SkRect bounds = SkRect::MakeIWH(400, 400); |
Florin Malita | 562017b | 2019-02-14 13:42:15 -0500 | [diff] [blame] | 114 | std::unique_ptr<SkCanvas> canvas = SkSVGCanvas::Make(bounds, &outStream); |
Bryce Thomas | fd5a508 | 2018-02-06 14:53:05 -0800 | [diff] [blame] | 115 | |
Mike Klein | 9ca602a | 2018-02-06 19:06:11 -0500 | [diff] [blame] | 116 | SkRect r = SkRect::MakeXYWH(SkIntToScalar(72), SkIntToScalar(72), SkIntToScalar(288), |
| 117 | SkIntToScalar(72)); |
| 118 | sk_sp<SkData> data(SkData::MakeWithCString("http://www.gooogle.com/#NamedDestination")); |
| 119 | SkAnnotateLinkToDestination(canvas.get(), r, data.get()); |
Bryce Thomas | fd5a508 | 2018-02-06 14:53:05 -0800 | [diff] [blame] | 120 | |
Mike Klein | 9ca602a | 2018-02-06 19:06:11 -0500 | [diff] [blame] | 121 | sk_sp<SkData> out = outStream.detachAsData(); |
| 122 | const char* rawOutput = (const char*)out->data(); |
| 123 | |
| 124 | REPORTER_ASSERT(reporter, |
| 125 | ContainsString(rawOutput, out->size(), |
| 126 | "a xlink:href=\"http://www.gooogle.com/#NamedDestination\"")); |
| 127 | } |
| 128 | #endif |