reed@google.com | b0a34d8 | 2012-07-11 19:57:55 +0000 | [diff] [blame] | 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" |
reed@google.com | 4979f32 | 2013-10-14 16:49:15 +0000 | [diff] [blame] | 9 | #include "SkData.h" |
bungeman | d3ebb48 | 2015-08-05 13:57:49 -0700 | [diff] [blame^] | 10 | #include "SkPaint.h" |
| 11 | #include "SkPoint.h" |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 12 | #include "SkReadBuffer.h" |
| 13 | #include "SkWriteBuffer.h" |
reed@google.com | b0a34d8 | 2012-07-11 19:57:55 +0000 | [diff] [blame] | 14 | |
commit-bot@chromium.org | 950923b | 2013-10-29 20:44:39 +0000 | [diff] [blame] | 15 | SkAnnotation::SkAnnotation(const char key[], SkData* value) : fKey(key) { |
reed@google.com | 4979f32 | 2013-10-14 16:49:15 +0000 | [diff] [blame] | 16 | if (NULL == value) { |
| 17 | value = SkData::NewEmpty(); |
reed@google.com | b0a34d8 | 2012-07-11 19:57:55 +0000 | [diff] [blame] | 18 | } else { |
reed@google.com | 4979f32 | 2013-10-14 16:49:15 +0000 | [diff] [blame] | 19 | value->ref(); |
reed@google.com | b0a34d8 | 2012-07-11 19:57:55 +0000 | [diff] [blame] | 20 | } |
reed@google.com | 4979f32 | 2013-10-14 16:49:15 +0000 | [diff] [blame] | 21 | fData = value; |
reed@google.com | b0a34d8 | 2012-07-11 19:57:55 +0000 | [diff] [blame] | 22 | } |
| 23 | |
| 24 | SkAnnotation::~SkAnnotation() { |
reed@google.com | 4979f32 | 2013-10-14 16:49:15 +0000 | [diff] [blame] | 25 | fData->unref(); |
reed@google.com | b0a34d8 | 2012-07-11 19:57:55 +0000 | [diff] [blame] | 26 | } |
| 27 | |
reed@google.com | 4979f32 | 2013-10-14 16:49:15 +0000 | [diff] [blame] | 28 | SkData* SkAnnotation::find(const char key[]) const { |
| 29 | return fKey.equals(key) ? fData : NULL; |
vandebo@chromium.org | 238be8c | 2012-07-13 20:06:02 +0000 | [diff] [blame] | 30 | } |
| 31 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 32 | SkAnnotation::SkAnnotation(SkReadBuffer& buffer) { |
reed@google.com | 4979f32 | 2013-10-14 16:49:15 +0000 | [diff] [blame] | 33 | buffer.readString(&fKey); |
| 34 | fData = buffer.readByteArrayAsData(); |
reed@google.com | b0a34d8 | 2012-07-11 19:57:55 +0000 | [diff] [blame] | 35 | } |
| 36 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 37 | void SkAnnotation::writeToBuffer(SkWriteBuffer& buffer) const { |
reed@google.com | 4979f32 | 2013-10-14 16:49:15 +0000 | [diff] [blame] | 38 | buffer.writeString(fKey.c_str()); |
| 39 | buffer.writeDataAsByteArray(fData); |
reed@google.com | b0a34d8 | 2012-07-11 19:57:55 +0000 | [diff] [blame] | 40 | } |
| 41 | |
reed@google.com | b0a34d8 | 2012-07-11 19:57:55 +0000 | [diff] [blame] | 42 | const char* SkAnnotationKeys::URL_Key() { |
| 43 | return "SkAnnotationKey_URL"; |
| 44 | }; |
| 45 | |
epoger@google.com | b58772f | 2013-03-08 09:09:10 +0000 | [diff] [blame] | 46 | const char* SkAnnotationKeys::Define_Named_Dest_Key() { |
| 47 | return "SkAnnotationKey_Define_Named_Dest"; |
| 48 | }; |
| 49 | |
| 50 | const char* SkAnnotationKeys::Link_Named_Dest_Key() { |
| 51 | return "SkAnnotationKey_Link_Named_Dest"; |
| 52 | }; |
| 53 | |
reed@google.com | b0a34d8 | 2012-07-11 19:57:55 +0000 | [diff] [blame] | 54 | /////////////////////////////////////////////////////////////////////////////// |
| 55 | |
| 56 | #include "SkCanvas.h" |
| 57 | |
epoger@google.com | b58772f | 2013-03-08 09:09:10 +0000 | [diff] [blame] | 58 | static void annotate_paint(SkPaint& paint, const char* key, SkData* value) { |
commit-bot@chromium.org | d957984 | 2014-02-27 11:47:36 +0000 | [diff] [blame] | 59 | paint.setAnnotation(SkAnnotation::Create(key, value))->unref(); |
epoger@google.com | b58772f | 2013-03-08 09:09:10 +0000 | [diff] [blame] | 60 | } |
| 61 | |
reed@google.com | b0a34d8 | 2012-07-11 19:57:55 +0000 | [diff] [blame] | 62 | void SkAnnotateRectWithURL(SkCanvas* canvas, const SkRect& rect, SkData* value) { |
| 63 | if (NULL == value) { |
| 64 | return; |
| 65 | } |
epoger@google.com | 1cad898 | 2013-03-06 00:05:13 +0000 | [diff] [blame] | 66 | SkPaint paint; |
epoger@google.com | b58772f | 2013-03-08 09:09:10 +0000 | [diff] [blame] | 67 | annotate_paint(paint, SkAnnotationKeys::URL_Key(), value); |
| 68 | canvas->drawRect(rect, paint); |
| 69 | } |
epoger@google.com | 812b6f5 | 2013-03-06 00:47:22 +0000 | [diff] [blame] | 70 | |
epoger@google.com | b58772f | 2013-03-08 09:09:10 +0000 | [diff] [blame] | 71 | void 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 | |
| 80 | void 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.com | b0a34d8 | 2012-07-11 19:57:55 +0000 | [diff] [blame] | 86 | canvas->drawRect(rect, paint); |
| 87 | } |