blob: aa7a02af4f14825bef457c289acfa6e7e4000532 [file] [log] [blame]
reed@google.comb0a34d82012-07-11 19:57:55 +00001/*
2 * Copyright 2012 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 "SkAnnotation.h"
reed@google.com4979f322013-10-14 16:49:15 +00009#include "SkData.h"
bungemand3ebb482015-08-05 13:57:49 -070010#include "SkPaint.h"
11#include "SkPoint.h"
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000012#include "SkReadBuffer.h"
13#include "SkWriteBuffer.h"
reed@google.comb0a34d82012-07-11 19:57:55 +000014
commit-bot@chromium.org950923b2013-10-29 20:44:39 +000015SkAnnotation::SkAnnotation(const char key[], SkData* value) : fKey(key) {
reed@google.com4979f322013-10-14 16:49:15 +000016 if (NULL == value) {
17 value = SkData::NewEmpty();
reed@google.comb0a34d82012-07-11 19:57:55 +000018 } else {
reed@google.com4979f322013-10-14 16:49:15 +000019 value->ref();
reed@google.comb0a34d82012-07-11 19:57:55 +000020 }
reed@google.com4979f322013-10-14 16:49:15 +000021 fData = value;
reed@google.comb0a34d82012-07-11 19:57:55 +000022}
23
24SkAnnotation::~SkAnnotation() {
reed@google.com4979f322013-10-14 16:49:15 +000025 fData->unref();
reed@google.comb0a34d82012-07-11 19:57:55 +000026}
27
reed@google.com4979f322013-10-14 16:49:15 +000028SkData* SkAnnotation::find(const char key[]) const {
29 return fKey.equals(key) ? fData : NULL;
vandebo@chromium.org238be8c2012-07-13 20:06:02 +000030}
31
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000032SkAnnotation::SkAnnotation(SkReadBuffer& buffer) {
reed@google.com4979f322013-10-14 16:49:15 +000033 buffer.readString(&fKey);
34 fData = buffer.readByteArrayAsData();
reed@google.comb0a34d82012-07-11 19:57:55 +000035}
36
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000037void SkAnnotation::writeToBuffer(SkWriteBuffer& buffer) const {
reed@google.com4979f322013-10-14 16:49:15 +000038 buffer.writeString(fKey.c_str());
39 buffer.writeDataAsByteArray(fData);
reed@google.comb0a34d82012-07-11 19:57:55 +000040}
41
reed@google.comb0a34d82012-07-11 19:57:55 +000042const char* SkAnnotationKeys::URL_Key() {
43 return "SkAnnotationKey_URL";
44};
45
epoger@google.comb58772f2013-03-08 09:09:10 +000046const char* SkAnnotationKeys::Define_Named_Dest_Key() {
47 return "SkAnnotationKey_Define_Named_Dest";
48};
49
50const char* SkAnnotationKeys::Link_Named_Dest_Key() {
51 return "SkAnnotationKey_Link_Named_Dest";
52};
53
reed@google.comb0a34d82012-07-11 19:57:55 +000054///////////////////////////////////////////////////////////////////////////////
55
56#include "SkCanvas.h"
57
epoger@google.comb58772f2013-03-08 09:09:10 +000058static void annotate_paint(SkPaint& paint, const char* key, SkData* value) {
commit-bot@chromium.orgd9579842014-02-27 11:47:36 +000059 paint.setAnnotation(SkAnnotation::Create(key, value))->unref();
epoger@google.comb58772f2013-03-08 09:09:10 +000060}
61
reed@google.comb0a34d82012-07-11 19:57:55 +000062void SkAnnotateRectWithURL(SkCanvas* canvas, const SkRect& rect, SkData* value) {
63 if (NULL == value) {
64 return;
65 }
epoger@google.com1cad8982013-03-06 00:05:13 +000066 SkPaint paint;
epoger@google.comb58772f2013-03-08 09:09:10 +000067 annotate_paint(paint, SkAnnotationKeys::URL_Key(), value);
68 canvas->drawRect(rect, paint);
69}
epoger@google.com812b6f52013-03-06 00:47:22 +000070
epoger@google.comb58772f2013-03-08 09:09:10 +000071void SkAnnotateNamedDestination(SkCanvas* canvas, const SkPoint& point, SkData* name) {
72 if (NULL == name) {
73 return;
74 }
75 SkPaint paint;
76 annotate_paint(paint, SkAnnotationKeys::Define_Named_Dest_Key(), name);
77 canvas->drawPoint(point.x(), point.y(), paint);
78}
79
80void SkAnnotateLinkToDestination(SkCanvas* canvas, const SkRect& rect, SkData* name) {
81 if (NULL == name) {
82 return;
83 }
84 SkPaint paint;
85 annotate_paint(paint, SkAnnotationKeys::Link_Named_Dest_Key(), name);
reed@google.comb0a34d82012-07-11 19:57:55 +000086 canvas->drawRect(rect, paint);
87}