[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
diff --git a/src/core/SkAnnotation.cpp b/src/core/SkAnnotation.cpp
index 94ce6a0..06ab509 100644
--- a/src/core/SkAnnotation.cpp
+++ b/src/core/SkAnnotation.cpp
@@ -23,6 +23,10 @@
     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 55aae8d..bba703c 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 @@
         return;
     }
 
+    if (handleAnnotations(r, *d.fMatrix, paint)) {
+        return;
+    }
+
     ScopedContentEntry content(this, d, paint);
     if (!content.entry()) {
         return;
@@ -763,6 +768,10 @@
         return;
     }
 
+    if (handleAnnotations(pathPtr->getBounds(), *d.fMatrix, paint)) {
+        return;
+    }
+
     ScopedContentEntry content(this, d, paint);
     if (!content.entry()) {
         return;
@@ -1113,6 +1122,10 @@
     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 @@
     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 3f3dec9..5a9254d 100644
--- a/src/pdf/SkPDFPage.cpp
+++ b/src/pdf/SkPDFPage.cpp
@@ -24,6 +24,13 @@
     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 49c7156..d113a0b 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)