michael@0: /* michael@0: * Copyright 2012 Google Inc. michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: michael@0: #include "SkAnnotation.h" michael@0: #include "SkData.h" michael@0: #include "SkReadBuffer.h" michael@0: #include "SkWriteBuffer.h" michael@0: #include "SkPoint.h" michael@0: #include "SkStream.h" michael@0: michael@0: SkAnnotation::SkAnnotation(const char key[], SkData* value) : fKey(key) { michael@0: if (NULL == value) { michael@0: value = SkData::NewEmpty(); michael@0: } else { michael@0: value->ref(); michael@0: } michael@0: fData = value; michael@0: } michael@0: michael@0: SkAnnotation::~SkAnnotation() { michael@0: fData->unref(); michael@0: } michael@0: michael@0: SkData* SkAnnotation::find(const char key[]) const { michael@0: return fKey.equals(key) ? fData : NULL; michael@0: } michael@0: michael@0: SkAnnotation::SkAnnotation(SkReadBuffer& buffer) { michael@0: buffer.readString(&fKey); michael@0: fData = buffer.readByteArrayAsData(); michael@0: } michael@0: michael@0: void SkAnnotation::writeToBuffer(SkWriteBuffer& buffer) const { michael@0: buffer.writeString(fKey.c_str()); michael@0: buffer.writeDataAsByteArray(fData); michael@0: } michael@0: michael@0: const char* SkAnnotationKeys::URL_Key() { michael@0: return "SkAnnotationKey_URL"; michael@0: }; michael@0: michael@0: const char* SkAnnotationKeys::Define_Named_Dest_Key() { michael@0: return "SkAnnotationKey_Define_Named_Dest"; michael@0: }; michael@0: michael@0: const char* SkAnnotationKeys::Link_Named_Dest_Key() { michael@0: return "SkAnnotationKey_Link_Named_Dest"; michael@0: }; michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: #include "SkCanvas.h" michael@0: michael@0: static void annotate_paint(SkPaint& paint, const char* key, SkData* value) { michael@0: paint.setAnnotation(SkAnnotation::Create(key, value))->unref(); michael@0: } michael@0: michael@0: void SkAnnotateRectWithURL(SkCanvas* canvas, const SkRect& rect, SkData* value) { michael@0: if (NULL == value) { michael@0: return; michael@0: } michael@0: SkPaint paint; michael@0: annotate_paint(paint, SkAnnotationKeys::URL_Key(), value); michael@0: canvas->drawRect(rect, paint); michael@0: } michael@0: michael@0: void SkAnnotateNamedDestination(SkCanvas* canvas, const SkPoint& point, SkData* name) { michael@0: if (NULL == name) { michael@0: return; michael@0: } michael@0: SkPaint paint; michael@0: annotate_paint(paint, SkAnnotationKeys::Define_Named_Dest_Key(), name); michael@0: canvas->drawPoint(point.x(), point.y(), paint); michael@0: } michael@0: michael@0: void SkAnnotateLinkToDestination(SkCanvas* canvas, const SkRect& rect, SkData* name) { michael@0: if (NULL == name) { michael@0: return; michael@0: } michael@0: SkPaint paint; michael@0: annotate_paint(paint, SkAnnotationKeys::Link_Named_Dest_Key(), name); michael@0: canvas->drawRect(rect, paint); michael@0: }