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.
|
|
|
|
*/
|
|
|
|
#include "SkAnnotation.h"
|
|
|
|
#include "SkCanvas.h"
|
2014-01-24 20:56:26 +00:00
|
|
|
#include "SkData.h"
|
2012-07-13 20:06:02 +00:00
|
|
|
#include "SkPDFDevice.h"
|
|
|
|
#include "SkPDFDocument.h"
|
2014-01-24 20:56:26 +00:00
|
|
|
#include "Test.h"
|
2012-07-11 19:57:55 +00:00
|
|
|
|
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;
|
|
|
|
bm.setConfig(SkBitmap::kARGB_8888_Config, 10, 10);
|
|
|
|
bm.allocPixels();
|
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));
|
|
|
|
|
|
|
|
SkAutoDataUnref data(SkData::NewWithCString("http://www.gooogle.com"));
|
|
|
|
|
|
|
|
REPORTER_ASSERT(reporter, 0 == *bm.getAddr32(0, 0));
|
|
|
|
SkAnnotateRectWithURL(&canvas, r, data.get());
|
|
|
|
REPORTER_ASSERT(reporter, 0 == *bm.getAddr32(0, 0));
|
|
|
|
}
|
|
|
|
|
2012-07-13 20:06:02 +00:00
|
|
|
struct testCase {
|
|
|
|
SkPDFDocument::Flags flags;
|
|
|
|
bool expectAnnotations;
|
|
|
|
};
|
|
|
|
|
2013-12-12 21:11:12 +00:00
|
|
|
DEF_TEST(Annotation_PdfLink, reporter) {
|
2012-07-13 20:06:02 +00:00
|
|
|
SkISize size = SkISize::Make(612, 792);
|
|
|
|
SkMatrix initialTransform;
|
|
|
|
initialTransform.reset();
|
|
|
|
SkPDFDevice device(size, size, initialTransform);
|
|
|
|
SkCanvas canvas(&device);
|
|
|
|
|
|
|
|
SkRect r = SkRect::MakeXYWH(SkIntToScalar(72), SkIntToScalar(72),
|
|
|
|
SkIntToScalar(288), SkIntToScalar(72));
|
|
|
|
SkAutoDataUnref data(SkData::NewWithCString("http://www.gooogle.com"));
|
|
|
|
SkAnnotateRectWithURL(&canvas, r, data.get());
|
|
|
|
|
|
|
|
testCase tests[] = {{(SkPDFDocument::Flags)0, true},
|
|
|
|
{SkPDFDocument::kNoLinks_Flags, false}};
|
|
|
|
for (size_t testNum = 0; testNum < SK_ARRAY_COUNT(tests); testNum++) {
|
|
|
|
SkPDFDocument doc(tests[testNum].flags);
|
|
|
|
doc.appendPage(&device);
|
|
|
|
SkDynamicMemoryWStream outStream;
|
|
|
|
doc.emitPDF(&outStream);
|
|
|
|
SkAutoDataUnref out(outStream.copyToData());
|
|
|
|
const char* rawOutput = (const char*)out->data();
|
|
|
|
|
2013-03-08 09:09:10 +00:00
|
|
|
REPORTER_ASSERT(reporter,
|
|
|
|
ContainsString(rawOutput, out->size(), "/Annots ")
|
|
|
|
== tests[testNum].expectAnnotations);
|
2012-07-13 20:06:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-12 21:11:12 +00:00
|
|
|
DEF_TEST(Annotation_NamedDestination, reporter) {
|
2013-03-08 09:09:10 +00:00
|
|
|
SkISize size = SkISize::Make(612, 792);
|
|
|
|
SkMatrix initialTransform;
|
|
|
|
initialTransform.reset();
|
|
|
|
SkPDFDevice device(size, size, initialTransform);
|
|
|
|
SkCanvas canvas(&device);
|
|
|
|
|
|
|
|
SkPoint p = SkPoint::Make(SkIntToScalar(72), SkIntToScalar(72));
|
|
|
|
SkAutoDataUnref data(SkData::NewWithCString("example"));
|
|
|
|
SkAnnotateNamedDestination(&canvas, p, data.get());
|
|
|
|
|
|
|
|
SkPDFDocument doc;
|
|
|
|
doc.appendPage(&device);
|
|
|
|
SkDynamicMemoryWStream outStream;
|
|
|
|
doc.emitPDF(&outStream);
|
|
|
|
SkAutoDataUnref out(outStream.copyToData());
|
|
|
|
const char* rawOutput = (const char*)out->data();
|
|
|
|
|
|
|
|
REPORTER_ASSERT(reporter,
|
|
|
|
ContainsString(rawOutput, out->size(), "/example "));
|
|
|
|
}
|