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 "SkDataSet.h"
10 #include "SkFlattenableBuffers.h"
11 #include "SkStream.h"
12
SkAnnotation(SkDataSet * data,uint32_t flags)13 SkAnnotation::SkAnnotation(SkDataSet* data, uint32_t flags) {
14 if (NULL == data) {
15 data = SkDataSet::NewEmpty();
16 } else {
17 data->ref();
18 }
19 fDataSet = data;
20 fFlags = flags;
21 }
22
~SkAnnotation()23 SkAnnotation::~SkAnnotation() {
24 fDataSet->unref();
25 }
26
find(const char name[]) const27 SkData* SkAnnotation::find(const char name[]) const {
28 return fDataSet->find(name);
29 }
30
SkAnnotation(SkFlattenableReadBuffer & buffer)31 SkAnnotation::SkAnnotation(SkFlattenableReadBuffer& buffer) : INHERITED(buffer) {
32 fFlags = buffer.readUInt();
33 fDataSet = buffer.readFlattenableT<SkDataSet>();
34 }
35
flatten(SkFlattenableWriteBuffer & buffer) const36 void SkAnnotation::flatten(SkFlattenableWriteBuffer& buffer) const {
37 buffer.writeUInt(fFlags);
38 buffer.writeFlattenable(fDataSet);
39 }
40
URL_Key()41 const char* SkAnnotationKeys::URL_Key() {
42 return "SkAnnotationKey_URL";
43 };
44
45 ///////////////////////////////////////////////////////////////////////////////
46
47 #include "SkCanvas.h"
48
SkAnnotateRectWithURL(SkCanvas * canvas,const SkRect & rect,SkData * value)49 void SkAnnotateRectWithURL(SkCanvas* canvas, const SkRect& rect, SkData* value) {
50 if (NULL == value) {
51 return;
52 }
53
54 const char* key = SkAnnotationKeys::URL_Key();
55 SkAutoTUnref<SkDataSet> dataset(SkNEW_ARGS(SkDataSet, (key, value)));
56 SkAnnotation* ann = SkNEW_ARGS(SkAnnotation, (dataset,
57 SkAnnotation::kNoDraw_Flag));
58
59 SkPaint paint;
60 paint.setAnnotation(ann)->unref();
61 SkASSERT(paint.isNoDrawAnnotation());
62
63 canvas->drawRect(rect, paint);
64 }
65