blob: 70ec327a3ee026cb918dfd8feb784a5229d20905 [file] [log] [blame]
reed@google.comb0a34d82012-07-11 19:57:55 +00001/*
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 Wagner1ebeefe2018-03-02 16:59:53 -05007
reed@google.comb0a34d82012-07-11 19:57:55 +00008#include "SkAnnotation.h"
Ben Wagner1ebeefe2018-03-02 16:59:53 -05009#include "SkBitmap.h"
reed@google.comb0a34d82012-07-11 19:57:55 +000010#include "SkCanvas.h"
Ben Wagner1ebeefe2018-03-02 16:59:53 -050011#include "SkColor.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000012#include "SkData.h"
Hal Canary23564b92018-09-07 14:33:14 -040013#include "SkPDFDocument.h"
Ben Wagner1ebeefe2018-03-02 16:59:53 -050014#include "SkPoint.h"
15#include "SkRect.h"
16#include "SkRefCnt.h"
17#include "SkScalar.h"
halcanary3d32d502015-03-01 06:55:20 -080018#include "SkStream.h"
Ben Wagner1ebeefe2018-03-02 16:59:53 -050019#include "SkTypes.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000020#include "Test.h"
reed@google.comb0a34d82012-07-11 19:57:55 +000021
Ben Wagner1ebeefe2018-03-02 16:59:53 -050022#include <string.h>
23#include <memory>
24
epoger@google.comb58772f2013-03-08 09:09:10 +000025/** Returns true if data (may contain null characters) contains needle (null
26 * terminated). */
27static 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.orge4fafb12013-12-12 21:11:12 +000037DEF_TEST(Annotation_NoDraw, reporter) {
reed@google.comb0a34d82012-07-11 19:57:55 +000038 SkBitmap bm;
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +000039 bm.allocN32Pixels(10, 10);
junov@google.comdbfac8a2012-12-06 21:47:40 +000040 bm.eraseColor(SK_ColorTRANSPARENT);
reed@google.comb0a34d82012-07-11 19:57:55 +000041
42 SkCanvas canvas(bm);
43 SkRect r = SkRect::MakeWH(SkIntToScalar(10), SkIntToScalar(10));
44
bungeman38d909e2016-08-02 14:40:46 -070045 sk_sp<SkData> data(SkData::MakeWithCString("http://www.gooogle.com"));
reed@google.comb0a34d82012-07-11 19:57:55 +000046
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.orge4fafb12013-12-12 21:11:12 +000052DEF_TEST(Annotation_PdfLink, reporter) {
halcanary2ccdb632015-08-11 13:35:12 -070053 REQUIRE_PDF_DOCUMENT(Annotation_PdfLink, reporter);
halcanary3d32d502015-03-01 06:55:20 -080054 SkDynamicMemoryWStream outStream;
Hal Canary23564b92018-09-07 14:33:14 -040055 sk_sp<SkDocument> doc(SkPDF::MakeDocument(&outStream));
halcanary3d32d502015-03-01 06:55:20 -080056 SkCanvas* canvas = doc->beginPage(612.0f, 792.0f);
57 REPORTER_ASSERT(reporter, canvas);
vandebo@chromium.org238be8c2012-07-13 20:06:02 +000058
59 SkRect r = SkRect::MakeXYWH(SkIntToScalar(72), SkIntToScalar(72),
60 SkIntToScalar(288), SkIntToScalar(72));
bungeman38d909e2016-08-02 14:40:46 -070061 sk_sp<SkData> data(SkData::MakeWithCString("http://www.gooogle.com"));
halcanary3d32d502015-03-01 06:55:20 -080062 SkAnnotateRectWithURL(canvas, r, data.get());
vandebo@chromium.org238be8c2012-07-13 20:06:02 +000063
reedd14df7c2016-09-22 14:12:46 -070064 doc->close();
reed42943c82016-09-12 12:01:44 -070065 sk_sp<SkData> out = outStream.detachAsData();
mtkleincabc08c2015-02-19 08:29:24 -080066 const char* rawOutput = (const char*)out->data();
vandebo@chromium.org238be8c2012-07-13 20:06:02 +000067
mtkleincabc08c2015-02-19 08:29:24 -080068 REPORTER_ASSERT(reporter, ContainsString(rawOutput, out->size(), "/Annots "));
vandebo@chromium.org238be8c2012-07-13 20:06:02 +000069}
70
Bryce Thomasfd5a5082018-02-06 14:53:05 -080071DEF_TEST(Annotation_PdfDefineNamedDestination, reporter) {
72 REQUIRE_PDF_DOCUMENT(Annotation_PdfNamedDestination, reporter);
halcanary3d32d502015-03-01 06:55:20 -080073 SkDynamicMemoryWStream outStream;
Hal Canary23564b92018-09-07 14:33:14 -040074 sk_sp<SkDocument> doc(SkPDF::MakeDocument(&outStream));
halcanary3d32d502015-03-01 06:55:20 -080075 SkCanvas* canvas = doc->beginPage(612.0f, 792.0f);
76 REPORTER_ASSERT(reporter, canvas);
epoger@google.comb58772f2013-03-08 09:09:10 +000077
78 SkPoint p = SkPoint::Make(SkIntToScalar(72), SkIntToScalar(72));
bungeman38d909e2016-08-02 14:40:46 -070079 sk_sp<SkData> data(SkData::MakeWithCString("example"));
halcanary3d32d502015-03-01 06:55:20 -080080 SkAnnotateNamedDestination(canvas, p, data.get());
epoger@google.comb58772f2013-03-08 09:09:10 +000081
reedd14df7c2016-09-22 14:12:46 -070082 doc->close();
reed42943c82016-09-12 12:01:44 -070083 sk_sp<SkData> out = outStream.detachAsData();
epoger@google.comb58772f2013-03-08 09:09:10 +000084 const char* rawOutput = (const char*)out->data();
85
86 REPORTER_ASSERT(reporter,
87 ContainsString(rawOutput, out->size(), "/example "));
88}
Bryce Thomasfd5a5082018-02-06 14:53:05 -080089
Mike Klein9ca602a2018-02-06 19:06:11 -050090#if defined(SK_XML)
91 #include "SkSVGCanvas.h"
92 #include "SkXMLWriter.h"
Bryce Thomasfd5a5082018-02-06 14:53:05 -080093
Mike Klein9ca602a2018-02-06 19:06:11 -050094 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 Thomasfd5a5082018-02-06 14:53:05 -080099
Mike Klein9ca602a2018-02-06 19:06:11 -0500100 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 Thomasfd5a5082018-02-06 14:53:05 -0800104
Mike Klein9ca602a2018-02-06 19:06:11 -0500105 canvas->flush();
106 sk_sp<SkData> out = outStream.detachAsData();
107 const char* rawOutput = (const char*)out->data();
Bryce Thomasfd5a5082018-02-06 14:53:05 -0800108
Mike Klein9ca602a2018-02-06 19:06:11 -0500109 REPORTER_ASSERT(reporter,
110 ContainsString(rawOutput, out->size(), "a xlink:href=\"http://www.gooogle.com\""));
111 }
Bryce Thomasfd5a5082018-02-06 14:53:05 -0800112
Mike Klein9ca602a2018-02-06 19:06:11 -0500113 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 Thomasfd5a5082018-02-06 14:53:05 -0800118
Mike Klein9ca602a2018-02-06 19:06:11 -0500119 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 Thomasfd5a5082018-02-06 14:53:05 -0800123
Mike Klein9ca602a2018-02-06 19:06:11 -0500124 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