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