SkAnnotation.cpp revision 950923b43761c10d9c8ffc1dfc0c878100d1e702
1/*
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"
9#include "SkData.h"
10#include "SkFlattenableBuffers.h"
11#include "SkPoint.h"
12#include "SkStream.h"
13
14SkAnnotation::SkAnnotation(const char key[], SkData* value) : fKey(key) {
15    if (NULL == value) {
16        value = SkData::NewEmpty();
17    } else {
18        value->ref();
19    }
20    fData = value;
21}
22
23SkAnnotation::~SkAnnotation() {
24    fData->unref();
25}
26
27SkData* SkAnnotation::find(const char key[]) const {
28    return fKey.equals(key) ? fData : NULL;
29}
30
31SkAnnotation::SkAnnotation(SkFlattenableReadBuffer& buffer) {
32    buffer.readString(&fKey);
33    fData = buffer.readByteArrayAsData();
34}
35
36void SkAnnotation::writeToBuffer(SkFlattenableWriteBuffer& buffer) const {
37    buffer.writeString(fKey.c_str());
38    buffer.writeDataAsByteArray(fData);
39}
40
41const char* SkAnnotationKeys::URL_Key() {
42    return "SkAnnotationKey_URL";
43};
44
45const char* SkAnnotationKeys::Define_Named_Dest_Key() {
46    return "SkAnnotationKey_Define_Named_Dest";
47};
48
49const char* SkAnnotationKeys::Link_Named_Dest_Key() {
50    return "SkAnnotationKey_Link_Named_Dest";
51};
52
53///////////////////////////////////////////////////////////////////////////////
54
55#include "SkCanvas.h"
56
57static void annotate_paint(SkPaint& paint, const char* key, SkData* value) {
58    paint.setAnnotation(SkNEW_ARGS(SkAnnotation, (key, value)))->unref();
59}
60
61void SkAnnotateRectWithURL(SkCanvas* canvas, const SkRect& rect, SkData* value) {
62    if (NULL == value) {
63        return;
64    }
65    SkPaint paint;
66    annotate_paint(paint, SkAnnotationKeys::URL_Key(), value);
67    canvas->drawRect(rect, paint);
68}
69
70void SkAnnotateNamedDestination(SkCanvas* canvas, const SkPoint& point, SkData* name) {
71    if (NULL == name) {
72        return;
73    }
74    SkPaint paint;
75    annotate_paint(paint, SkAnnotationKeys::Define_Named_Dest_Key(), name);
76    canvas->drawPoint(point.x(), point.y(), paint);
77}
78
79void SkAnnotateLinkToDestination(SkCanvas* canvas, const SkRect& rect, SkData* name) {
80    if (NULL == name) {
81        return;
82    }
83    SkPaint paint;
84    annotate_paint(paint, SkAnnotationKeys::Link_Named_Dest_Key(), name);
85    canvas->drawRect(rect, paint);
86}
87