aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGravatar vandebo@chromium.org <vandebo@chromium.org@2bbb7eff-a529-9590-31e7-b0007b416f81>2012-07-13 20:06:02 +0000
committerGravatar vandebo@chromium.org <vandebo@chromium.org@2bbb7eff-a529-9590-31e7-b0007b416f81>2012-07-13 20:06:02 +0000
commit238be8c7e5de5a83517440a3db7f7965b47fb010 (patch)
tree574cb480c44361b9f962ea4862d5033b171d6e57
parente742bf0ab19659145325ac894f7e0b78c8efbd89 (diff)
[PDF] Add link annotations.
Review URL: https://codereview.appspot.com/6346100 git-svn-id: http://skia.googlecode.com/svn/trunk@4609 2bbb7eff-a529-9590-31e7-b0007b416f81
-rw-r--r--include/pdf/SkPDFDevice.h9
-rw-r--r--include/pdf/SkPDFDocument.h6
-rw-r--r--src/core/SkAnnotation.cpp4
-rw-r--r--src/pdf/SkPDFDevice.cpp62
-rw-r--r--src/pdf/SkPDFPage.cpp7
-rw-r--r--src/pdf/SkPDFStream.cpp3
-rw-r--r--tests/AnnotationTest.cpp48
-rw-r--r--tests/PDFPrimitivesTest.cpp2
8 files changed, 136 insertions, 5 deletions
diff --git a/include/pdf/SkPDFDevice.h b/include/pdf/SkPDFDevice.h
index 589cde965f..c01a383fa7 100644
--- a/include/pdf/SkPDFDevice.h
+++ b/include/pdf/SkPDFDevice.h
@@ -14,6 +14,7 @@
#include "SkDevice.h"
#include "SkPaint.h"
#include "SkPath.h"
+#include "SkRect.h"
#include "SkRefCnt.h"
#include "SkStream.h"
#include "SkTScopedPtr.h"
@@ -142,6 +143,10 @@ public:
*/
SK_API SkRefPtr<SkPDFArray> getMediaBox() const;
+ /** Get the annotations from this page.
+ */
+ SK_API SkRefPtr<SkPDFArray> getAnnotations() const;
+
/** Returns a SkStream with the page contents. The caller is responsible
for a reference to the returned value.
DEPRECATED: use copyContentToData()
@@ -180,6 +185,7 @@ private:
SkMatrix fInitialTransform;
SkClipStack fExistingClipStack;
SkRegion fExistingClipRegion;
+ SkRefPtr<SkPDFArray> fAnnotations;
SkRefPtr<SkPDFDict> fResourceDict;
SkTDArray<SkPDFGraphicState*> fGraphicStateResources;
@@ -263,6 +269,9 @@ private:
*/
void copyContentEntriesToData(ContentEntry* entry, SkWStream* data) const;
+ bool handleAnnotations(const SkRect& r, const SkMatrix& matrix,
+ const SkPaint& paint);
+
typedef SkDevice INHERITED;
};
diff --git a/include/pdf/SkPDFDocument.h b/include/pdf/SkPDFDocument.h
index 1a4a51fa43..2caa28f18f 100644
--- a/include/pdf/SkPDFDocument.h
+++ b/include/pdf/SkPDFDocument.h
@@ -29,10 +29,10 @@ class SkWStream;
class SkPDFDocument {
public:
enum Flags {
- kNoCompression_Flag = 0x01, //!< mask disable stream compression.
- kNoEmbedding_Flag = 0x02, //!< mask do not embed fonts.
+ kNoCompression_Flags = 0x01, //!< mask disable stream compression.
+ kNoLinks_Flags = 0x02, //!< do not honor link annotations.
- kDraftMode_Flags = 0x03,
+ kDraftMode_Flags = 0x01,
};
/** Create a PDF document.
*/
diff --git a/src/core/SkAnnotation.cpp b/src/core/SkAnnotation.cpp
index 94ce6a0fab..06ab509b87 100644
--- a/src/core/SkAnnotation.cpp
+++ b/src/core/SkAnnotation.cpp
@@ -23,6 +23,10 @@ SkAnnotation::~SkAnnotation() {
fDataSet->unref();
}
+SkData* SkAnnotation::find(const char name[]) const {
+ return fDataSet->find(name);
+}
+
SkAnnotation::SkAnnotation(SkFlattenableReadBuffer& buffer) : INHERITED(buffer) {
fFlags = buffer.readU32();
fDataSet = SkNEW_ARGS(SkDataSet, (buffer));
diff --git a/src/pdf/SkPDFDevice.cpp b/src/pdf/SkPDFDevice.cpp
index 55aae8dd56..bba703c23c 100644
--- a/src/pdf/SkPDFDevice.cpp
+++ b/src/pdf/SkPDFDevice.cpp
@@ -9,6 +9,7 @@
#include "SkPDFDevice.h"
+#include "SkAnnotation.h"
#include "SkColor.h"
#include "SkClipStack.h"
#include "SkData.h"
@@ -711,6 +712,10 @@ void SkPDFDevice::drawRect(const SkDraw& d, const SkRect& r,
return;
}
+ if (handleAnnotations(r, *d.fMatrix, paint)) {
+ return;
+ }
+
ScopedContentEntry content(this, d, paint);
if (!content.entry()) {
return;
@@ -763,6 +768,10 @@ void SkPDFDevice::drawPath(const SkDraw& d, const SkPath& origPath,
return;
}
+ if (handleAnnotations(pathPtr->getBounds(), *d.fMatrix, paint)) {
+ return;
+ }
+
ScopedContentEntry content(this, d, paint);
if (!content.entry()) {
return;
@@ -1113,6 +1122,10 @@ SkRefPtr<SkPDFArray> SkPDFDevice::getMediaBox() const {
return mediaBox;
}
+SkRefPtr<SkPDFArray> SkPDFDevice::getAnnotations() const {
+ return SkRefPtr<SkPDFArray>(fAnnotations);
+}
+
SkStream* SkPDFDevice::content() const {
SkMemoryStream* result = new SkMemoryStream;
result->setData(this->copyContentToData())->unref();
@@ -1170,6 +1183,55 @@ SkData* SkPDFDevice::copyContentToData() const {
return data.copyToData();
}
+bool SkPDFDevice::handleAnnotations(const SkRect& r, const SkMatrix& matrix,
+ const SkPaint& p) {
+ SkAnnotation* annotationInfo = p.getAnnotation();
+ if (!annotationInfo) {
+ return false;
+ }
+ SkData* urlData = annotationInfo->find(SkAnnotationKeys::URL_Key());
+ if (!urlData) {
+ return false;
+ }
+
+ SkString url(static_cast<const char *>(urlData->data()),
+ urlData->size() - 1);
+ SkMatrix transform = matrix;
+ transform.postConcat(fInitialTransform);
+ SkRect translatedRect;
+ transform.mapRect(&translatedRect, r);
+
+ if (fAnnotations.get() == NULL) {
+ fAnnotations = new SkPDFArray;
+ fAnnotations->unref(); // Both new and SkRefPtr took a reference.
+ }
+ SkAutoTUnref<SkPDFDict> annotation(new SkPDFDict("Annot"));
+ annotation->insertName("Subtype", "Link");
+ fAnnotations->append(annotation.get());
+
+ SkAutoTUnref<SkPDFArray> border(new SkPDFArray);
+ border->reserve(3);
+ border->appendInt(0); // Horizontal corner radius.
+ border->appendInt(0); // Vertical corner radius.
+ border->appendInt(0); // Width, 0 = no border.
+ annotation->insert("Border", border.get());
+
+ SkAutoTUnref<SkPDFArray> rect(new SkPDFArray);
+ rect->reserve(4);
+ rect->appendScalar(translatedRect.fLeft);
+ rect->appendScalar(translatedRect.fTop);
+ rect->appendScalar(translatedRect.fRight);
+ rect->appendScalar(translatedRect.fBottom);
+ annotation->insert("Rect", rect.get());
+
+ SkAutoTUnref<SkPDFDict> action(new SkPDFDict("Action"));
+ action->insertName("S", "URI");
+ action->insert("URI", new SkPDFString(url))->unref();
+ annotation->insert("A", action.get());
+
+ return p.isNoDrawAnnotation();
+}
+
void SkPDFDevice::createFormXObjectFromDevice(
SkRefPtr<SkPDFFormXObject>* xobject) {
*xobject = new SkPDFFormXObject(this);
diff --git a/src/pdf/SkPDFPage.cpp b/src/pdf/SkPDFPage.cpp
index 3f3dec98b3..5a9254d69d 100644
--- a/src/pdf/SkPDFPage.cpp
+++ b/src/pdf/SkPDFPage.cpp
@@ -24,6 +24,13 @@ void SkPDFPage::finalizePage(SkPDFCatalog* catalog, bool firstPage,
if (fContentStream.get() == NULL) {
insert("Resources", fDevice->getResourceDict());
insert("MediaBox", fDevice->getMediaBox().get());
+ if (!SkToBool(catalog->getDocumentFlags() &
+ SkPDFDocument::kNoLinks_Flags)) {
+ SkRefPtr<SkPDFArray> annots = fDevice->getAnnotations();
+ if (annots.get() && annots->size() > 0) {
+ insert("Annots", annots.get());
+ }
+ }
SkRefPtr<SkStream> content = fDevice->content();
content->unref(); // SkRefPtr and content() both took a reference.
diff --git a/src/pdf/SkPDFStream.cpp b/src/pdf/SkPDFStream.cpp
index 49c71560c2..d113a0b355 100644
--- a/src/pdf/SkPDFStream.cpp
+++ b/src/pdf/SkPDFStream.cpp
@@ -14,7 +14,8 @@
#include "SkStream.h"
static bool skip_compression(SkPDFCatalog* catalog) {
- return catalog->getDocumentFlags() & SkPDFDocument::kNoCompression_Flag;
+ return SkToBool(catalog->getDocumentFlags() &
+ SkPDFDocument::kNoCompression_Flags);
}
SkPDFStream::SkPDFStream(SkStream* stream)
diff --git a/tests/AnnotationTest.cpp b/tests/AnnotationTest.cpp
index fe2cd01900..17da6226fe 100644
--- a/tests/AnnotationTest.cpp
+++ b/tests/AnnotationTest.cpp
@@ -9,6 +9,8 @@
#include "SkAnnotation.h"
#include "SkData.h"
#include "SkCanvas.h"
+#include "SkPDFDevice.h"
+#include "SkPDFDocument.h"
static void test_nodraw(skiatest::Reporter* reporter) {
SkBitmap bm;
@@ -26,8 +28,54 @@ static void test_nodraw(skiatest::Reporter* reporter) {
REPORTER_ASSERT(reporter, 0 == *bm.getAddr32(0, 0));
}
+struct testCase {
+ SkPDFDocument::Flags flags;
+ bool expectAnnotations;
+};
+
+static void test_pdf_link_annotations(skiatest::Reporter* reporter) {
+ 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();
+
+ bool found = false;
+ for (size_t i = 0; i < out->size() - 8; i++) {
+ if (rawOutput[i + 0] == '/' &&
+ rawOutput[i + 1] == 'A' &&
+ rawOutput[i + 2] == 'n' &&
+ rawOutput[i + 3] == 'n' &&
+ rawOutput[i + 4] == 'o' &&
+ rawOutput[i + 5] == 't' &&
+ rawOutput[i + 6] == 's' &&
+ rawOutput[i + 7] == ' ') {
+ found = true;
+ break;
+ }
+ }
+ REPORTER_ASSERT(reporter, found == tests[testNum].expectAnnotations);
+ }
+}
+
static void TestAnnotation(skiatest::Reporter* reporter) {
test_nodraw(reporter);
+ test_pdf_link_annotations(reporter);
}
#include "TestClassDef.h"
diff --git a/tests/PDFPrimitivesTest.cpp b/tests/PDFPrimitivesTest.cpp
index ea3093f60f..33366be560 100644
--- a/tests/PDFPrimitivesTest.cpp
+++ b/tests/PDFPrimitivesTest.cpp
@@ -49,7 +49,7 @@ static void CheckObjectOutput(skiatest::Reporter* reporter, SkPDFObject* obj,
bool indirect, bool compression) {
SkPDFDocument::Flags docFlags = (SkPDFDocument::Flags) 0;
if (!compression) {
- docFlags = SkTBitOr(docFlags, SkPDFDocument::kNoCompression_Flag);
+ docFlags = SkTBitOr(docFlags, SkPDFDocument::kNoCompression_Flags);
}
SkPDFCatalog catalog(docFlags);
size_t directSize = obj->getOutputSize(&catalog, false);