2012-07-11 19:57:55 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2011 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
2018-03-02 21:59:53 +00:00
|
|
|
|
2012-07-11 19:57:55 +00:00
|
|
|
#include "SkAnnotation.h"
|
2018-03-02 21:59:53 +00:00
|
|
|
#include "SkBitmap.h"
|
2012-07-11 19:57:55 +00:00
|
|
|
#include "SkCanvas.h"
|
2018-03-02 21:59:53 +00:00
|
|
|
#include "SkColor.h"
|
2014-01-24 20:56:26 +00:00
|
|
|
#include "SkData.h"
|
2018-09-07 18:33:14 +00:00
|
|
|
#include "SkPDFDocument.h"
|
2018-03-02 21:59:53 +00:00
|
|
|
#include "SkPoint.h"
|
|
|
|
#include "SkRect.h"
|
|
|
|
#include "SkRefCnt.h"
|
|
|
|
#include "SkScalar.h"
|
2015-03-01 14:55:20 +00:00
|
|
|
#include "SkStream.h"
|
2018-03-02 21:59:53 +00:00
|
|
|
#include "SkTypes.h"
|
2014-01-24 20:56:26 +00:00
|
|
|
#include "Test.h"
|
2012-07-11 19:57:55 +00:00
|
|
|
|
2018-03-02 21:59:53 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <memory>
|
|
|
|
|
2013-03-08 09:09:10 +00:00
|
|
|
/** Returns true if data (may contain null characters) contains needle (null
|
|
|
|
* terminated). */
|
|
|
|
static bool ContainsString(const char* data, size_t dataSize, const char* needle) {
|
|
|
|
size_t nSize = strlen(needle);
|
|
|
|
for (size_t i = 0; i < dataSize - nSize; i++) {
|
|
|
|
if (strncmp(&data[i], needle, nSize) == 0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-12-12 21:11:12 +00:00
|
|
|
DEF_TEST(Annotation_NoDraw, reporter) {
|
2012-07-11 19:57:55 +00:00
|
|
|
SkBitmap bm;
|
2014-02-13 22:00:04 +00:00
|
|
|
bm.allocN32Pixels(10, 10);
|
2012-12-06 21:47:40 +00:00
|
|
|
bm.eraseColor(SK_ColorTRANSPARENT);
|
2012-07-11 19:57:55 +00:00
|
|
|
|
|
|
|
SkCanvas canvas(bm);
|
|
|
|
SkRect r = SkRect::MakeWH(SkIntToScalar(10), SkIntToScalar(10));
|
|
|
|
|
2016-08-02 21:40:46 +00:00
|
|
|
sk_sp<SkData> data(SkData::MakeWithCString("http://www.gooogle.com"));
|
2012-07-11 19:57:55 +00:00
|
|
|
|
|
|
|
REPORTER_ASSERT(reporter, 0 == *bm.getAddr32(0, 0));
|
|
|
|
SkAnnotateRectWithURL(&canvas, r, data.get());
|
|
|
|
REPORTER_ASSERT(reporter, 0 == *bm.getAddr32(0, 0));
|
|
|
|
}
|
|
|
|
|
2013-12-12 21:11:12 +00:00
|
|
|
DEF_TEST(Annotation_PdfLink, reporter) {
|
2015-08-11 20:35:12 +00:00
|
|
|
REQUIRE_PDF_DOCUMENT(Annotation_PdfLink, reporter);
|
2015-03-01 14:55:20 +00:00
|
|
|
SkDynamicMemoryWStream outStream;
|
2019-01-07 15:00:48 +00:00
|
|
|
auto doc = SkPDF::MakeDocument(&outStream);
|
2015-03-01 14:55:20 +00:00
|
|
|
SkCanvas* canvas = doc->beginPage(612.0f, 792.0f);
|
|
|
|
REPORTER_ASSERT(reporter, canvas);
|
2012-07-13 20:06:02 +00:00
|
|
|
|
|
|
|
SkRect r = SkRect::MakeXYWH(SkIntToScalar(72), SkIntToScalar(72),
|
|
|
|
SkIntToScalar(288), SkIntToScalar(72));
|
2016-08-02 21:40:46 +00:00
|
|
|
sk_sp<SkData> data(SkData::MakeWithCString("http://www.gooogle.com"));
|
2015-03-01 14:55:20 +00:00
|
|
|
SkAnnotateRectWithURL(canvas, r, data.get());
|
2012-07-13 20:06:02 +00:00
|
|
|
|
2016-09-22 21:12:46 +00:00
|
|
|
doc->close();
|
2016-09-12 19:01:44 +00:00
|
|
|
sk_sp<SkData> out = outStream.detachAsData();
|
2015-02-19 16:29:24 +00:00
|
|
|
const char* rawOutput = (const char*)out->data();
|
2012-07-13 20:06:02 +00:00
|
|
|
|
2015-02-19 16:29:24 +00:00
|
|
|
REPORTER_ASSERT(reporter, ContainsString(rawOutput, out->size(), "/Annots "));
|
2012-07-13 20:06:02 +00:00
|
|
|
}
|
|
|
|
|
2018-02-06 22:53:05 +00:00
|
|
|
DEF_TEST(Annotation_PdfDefineNamedDestination, reporter) {
|
|
|
|
REQUIRE_PDF_DOCUMENT(Annotation_PdfNamedDestination, reporter);
|
2015-03-01 14:55:20 +00:00
|
|
|
SkDynamicMemoryWStream outStream;
|
2019-01-07 15:00:48 +00:00
|
|
|
auto doc = SkPDF::MakeDocument(&outStream);
|
2015-03-01 14:55:20 +00:00
|
|
|
SkCanvas* canvas = doc->beginPage(612.0f, 792.0f);
|
|
|
|
REPORTER_ASSERT(reporter, canvas);
|
2013-03-08 09:09:10 +00:00
|
|
|
|
|
|
|
SkPoint p = SkPoint::Make(SkIntToScalar(72), SkIntToScalar(72));
|
2016-08-02 21:40:46 +00:00
|
|
|
sk_sp<SkData> data(SkData::MakeWithCString("example"));
|
2015-03-01 14:55:20 +00:00
|
|
|
SkAnnotateNamedDestination(canvas, p, data.get());
|
2013-03-08 09:09:10 +00:00
|
|
|
|
2016-09-22 21:12:46 +00:00
|
|
|
doc->close();
|
2016-09-12 19:01:44 +00:00
|
|
|
sk_sp<SkData> out = outStream.detachAsData();
|
2013-03-08 09:09:10 +00:00
|
|
|
const char* rawOutput = (const char*)out->data();
|
|
|
|
|
|
|
|
REPORTER_ASSERT(reporter,
|
|
|
|
ContainsString(rawOutput, out->size(), "/example "));
|
|
|
|
}
|
2018-02-06 22:53:05 +00:00
|
|
|
|
2018-02-07 00:06:11 +00:00
|
|
|
#if defined(SK_XML)
|
|
|
|
#include "SkSVGCanvas.h"
|
|
|
|
#include "SkXMLWriter.h"
|
2018-02-06 22:53:05 +00:00
|
|
|
|
2018-02-07 00:06:11 +00:00
|
|
|
DEF_TEST(Annotation_SvgLink, reporter) {
|
|
|
|
SkDynamicMemoryWStream outStream;
|
|
|
|
SkRect bounds = SkRect::MakeIWH(400, 400);
|
2019-02-14 18:42:15 +00:00
|
|
|
std::unique_ptr<SkCanvas> canvas = SkSVGCanvas::Make(bounds, &outStream);
|
2018-02-06 22:53:05 +00:00
|
|
|
|
2018-02-07 00:06:11 +00:00
|
|
|
SkRect r = SkRect::MakeXYWH(SkIntToScalar(72), SkIntToScalar(72), SkIntToScalar(288),
|
|
|
|
SkIntToScalar(72));
|
|
|
|
sk_sp<SkData> data(SkData::MakeWithCString("http://www.gooogle.com"));
|
|
|
|
SkAnnotateRectWithURL(canvas.get(), r, data.get());
|
2018-02-06 22:53:05 +00:00
|
|
|
|
2018-02-07 00:06:11 +00:00
|
|
|
canvas->flush();
|
|
|
|
sk_sp<SkData> out = outStream.detachAsData();
|
|
|
|
const char* rawOutput = (const char*)out->data();
|
2018-02-06 22:53:05 +00:00
|
|
|
|
2018-02-07 00:06:11 +00:00
|
|
|
REPORTER_ASSERT(reporter,
|
|
|
|
ContainsString(rawOutput, out->size(), "a xlink:href=\"http://www.gooogle.com\""));
|
|
|
|
}
|
2018-02-06 22:53:05 +00:00
|
|
|
|
2018-02-07 00:06:11 +00:00
|
|
|
DEF_TEST(Annotation_SvgLinkNamedDestination, reporter) {
|
|
|
|
SkDynamicMemoryWStream outStream;
|
|
|
|
SkRect bounds = SkRect::MakeIWH(400, 400);
|
2019-02-14 18:42:15 +00:00
|
|
|
std::unique_ptr<SkCanvas> canvas = SkSVGCanvas::Make(bounds, &outStream);
|
2018-02-06 22:53:05 +00:00
|
|
|
|
2018-02-07 00:06:11 +00:00
|
|
|
SkRect r = SkRect::MakeXYWH(SkIntToScalar(72), SkIntToScalar(72), SkIntToScalar(288),
|
|
|
|
SkIntToScalar(72));
|
|
|
|
sk_sp<SkData> data(SkData::MakeWithCString("http://www.gooogle.com/#NamedDestination"));
|
|
|
|
SkAnnotateLinkToDestination(canvas.get(), r, data.get());
|
2018-02-06 22:53:05 +00:00
|
|
|
|
2018-02-07 00:06:11 +00:00
|
|
|
canvas->flush();
|
|
|
|
sk_sp<SkData> out = outStream.detachAsData();
|
|
|
|
const char* rawOutput = (const char*)out->data();
|
|
|
|
|
|
|
|
REPORTER_ASSERT(reporter,
|
|
|
|
ContainsString(rawOutput, out->size(),
|
|
|
|
"a xlink:href=\"http://www.gooogle.com/#NamedDestination\""));
|
|
|
|
}
|
|
|
|
#endif
|