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 */
7
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"
13 #include "include/core/SkDocument.h"
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"
22
23 #include <string.h>
24 #include <memory>
25
26 /** Returns true if data (may contain null characters) contains needle (null
27 * terminated). */
ContainsString(const char * data,size_t dataSize,const char * needle)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
DEF_TEST(Annotation_NoDraw,reporter)38 DEF_TEST(Annotation_NoDraw, reporter) {
39 SkBitmap bm;
40 bm.allocN32Pixels(10, 10);
41 bm.eraseColor(SK_ColorTRANSPARENT);
42
43 SkCanvas canvas(bm);
44 SkRect r = SkRect::MakeWH(SkIntToScalar(10), SkIntToScalar(10));
45
46 sk_sp<SkData> data(SkData::MakeWithCString("http://www.gooogle.com"));
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
DEF_TEST(Annotation_PdfLink,reporter)53 DEF_TEST(Annotation_PdfLink, reporter) {
54 REQUIRE_PDF_DOCUMENT(Annotation_PdfLink, reporter);
55 SkDynamicMemoryWStream outStream;
56 auto doc = SkPDF::MakeDocument(&outStream);
57 SkCanvas* canvas = doc->beginPage(612.0f, 792.0f);
58 REPORTER_ASSERT(reporter, canvas);
59
60 SkRect r = SkRect::MakeXYWH(SkIntToScalar(72), SkIntToScalar(72),
61 SkIntToScalar(288), SkIntToScalar(72));
62 sk_sp<SkData> data(SkData::MakeWithCString("http://www.gooogle.com"));
63 SkAnnotateRectWithURL(canvas, r, data.get());
64
65 doc->close();
66 sk_sp<SkData> out = outStream.detachAsData();
67 const char* rawOutput = (const char*)out->data();
68
69 REPORTER_ASSERT(reporter, ContainsString(rawOutput, out->size(), "/Annots "));
70 }
71
DEF_TEST(Annotation_PdfDefineNamedDestination,reporter)72 DEF_TEST(Annotation_PdfDefineNamedDestination, reporter) {
73 REQUIRE_PDF_DOCUMENT(Annotation_PdfNamedDestination, reporter);
74 SkDynamicMemoryWStream outStream;
75 auto doc = SkPDF::MakeDocument(&outStream);
76 SkCanvas* canvas = doc->beginPage(612.0f, 792.0f);
77 REPORTER_ASSERT(reporter, canvas);
78
79 SkPoint p = SkPoint::Make(SkIntToScalar(72), SkIntToScalar(72));
80 sk_sp<SkData> data(SkData::MakeWithCString("example"));
81 SkAnnotateNamedDestination(canvas, p, data.get());
82
83 doc->close();
84 sk_sp<SkData> out = outStream.detachAsData();
85 const char* rawOutput = (const char*)out->data();
86
87 REPORTER_ASSERT(reporter,
88 ContainsString(rawOutput, out->size(), "/example "));
89 }
90
91 #if defined(SK_XML)
92 #include "include/svg/SkSVGCanvas.h"
93
DEF_TEST(Annotation_SvgLink,reporter)94 DEF_TEST(Annotation_SvgLink, reporter) {
95 SkDynamicMemoryWStream outStream;
96 SkRect bounds = SkRect::MakeIWH(400, 400);
97 std::unique_ptr<SkCanvas> canvas = SkSVGCanvas::Make(bounds, &outStream);
98
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());
103
104 sk_sp<SkData> out = outStream.detachAsData();
105 const char* rawOutput = (const char*)out->data();
106
107 REPORTER_ASSERT(reporter,
108 ContainsString(rawOutput, out->size(), "a xlink:href=\"http://www.gooogle.com\""));
109 }
110
DEF_TEST(Annotation_SvgLinkNamedDestination,reporter)111 DEF_TEST(Annotation_SvgLinkNamedDestination, reporter) {
112 SkDynamicMemoryWStream outStream;
113 SkRect bounds = SkRect::MakeIWH(400, 400);
114 std::unique_ptr<SkCanvas> canvas = SkSVGCanvas::Make(bounds, &outStream);
115
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());
120
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
129