1/* 2 * Copyright 2015 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 "gm/gm.h" 9#include "include/core/SkAnnotation.h" 10#include "include/core/SkCanvas.h" 11#include "include/core/SkColor.h" 12#include "include/core/SkData.h" 13#include "include/core/SkFont.h" 14#include "include/core/SkFontTypes.h" 15#include "include/core/SkPaint.h" 16#include "include/core/SkRect.h" 17#include "include/core/SkRefCnt.h" 18#include "include/core/SkScalar.h" 19 20#include <string.h> 21 22static void draw_url_annotated_text_with_box( 23 SkCanvas* canvas, const void* text, 24 SkScalar x, SkScalar y, const SkFont& font, const char* url) { 25 size_t byteLength = strlen(static_cast<const char*>(text)); 26 SkRect bounds; 27 (void)font.measureText(text, byteLength, SkTextEncoding::kUTF8, &bounds); 28 bounds.offset(x, y); 29 sk_sp<SkData> urlData(SkData::MakeWithCString(url)); 30 SkAnnotateRectWithURL(canvas, bounds, urlData.get()); 31 SkPaint shade; 32 shade.setColor(0x80346180); 33 canvas->drawRect(bounds, shade); 34 canvas->drawSimpleText(text, byteLength, SkTextEncoding::kUTF8, x, y, font, SkPaint()); 35} 36 37DEF_SIMPLE_GM(annotated_text, canvas, 512, 512) { 38 SkAutoCanvasRestore autoCanvasRestore(canvas, true); 39 canvas->clear(SK_ColorWHITE); 40 canvas->clipRect(SkRect::MakeXYWH(64, 64, 256, 256)); 41 canvas->clear(0xFFEEEEEE); 42 SkFont font; 43 font.setEdging(SkFont::Edging::kAlias); 44 font.setSize(40); 45 const char text[] = "Click this link!"; 46 const char url[] = "https://www.google.com/"; 47 draw_url_annotated_text_with_box(canvas, text, 200.0f, 80.0f, font, url); 48 canvas->saveLayer(nullptr, nullptr); 49 canvas->rotate(90); 50 draw_url_annotated_text_with_box(canvas, text, 150.0f, -55.0f, font, url); 51 canvas->restore(); 52} 53