halcanary | 74015ed | 2015-06-04 07:26:54 -0700 | [diff] [blame] | 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 "SkAnnotation.h" |
| 9 | #include "SkData.h" |
| 10 | #include "gm.h" |
| 11 | |
| 12 | static void draw_url_annotated_text_with_box( |
| 13 | SkCanvas* canvas, const void* text, |
| 14 | SkScalar x, SkScalar y, const SkPaint& paint, const char* url) { |
| 15 | size_t byteLength = strlen(static_cast<const char*>(text)); |
| 16 | SkRect bounds; |
| 17 | (void)paint.measureText(text, byteLength, &bounds); |
| 18 | bounds.offset(x, y); |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 19 | sk_sp<SkData> urlData(SkData::MakeWithCString(url)); |
| 20 | SkAnnotateRectWithURL(canvas, bounds, urlData.get()); |
halcanary | 74015ed | 2015-06-04 07:26:54 -0700 | [diff] [blame] | 21 | SkPaint shade; |
| 22 | shade.setColor(0x80346180); |
| 23 | canvas->drawRect(bounds, shade); |
| 24 | canvas->drawText(text, byteLength, x, y, paint); |
| 25 | } |
| 26 | |
| 27 | DEF_SIMPLE_GM(annotated_text, canvas, 512, 512) { |
| 28 | SkAutoCanvasRestore autoCanvasRestore(canvas, true); |
| 29 | canvas->clear(SK_ColorWHITE); |
wangxianzhu | d76665d | 2015-07-17 17:23:15 -0700 | [diff] [blame] | 30 | canvas->clipRect(SkRect::MakeXYWH(64, 64, 256, 256)); |
halcanary | 74015ed | 2015-06-04 07:26:54 -0700 | [diff] [blame] | 31 | canvas->clear(0xFFEEEEEE); |
| 32 | SkPaint p; |
| 33 | p.setTextSize(40); |
| 34 | const char text[] = "Click this link!"; |
| 35 | const char url[] = "https://www.google.com/"; |
| 36 | draw_url_annotated_text_with_box(canvas, text, 200.0f, 80.0f, p, url); |
wangxianzhu | ef6c50a | 2015-09-17 20:38:02 -0700 | [diff] [blame] | 37 | canvas->saveLayer(nullptr, nullptr); |
halcanary | 74015ed | 2015-06-04 07:26:54 -0700 | [diff] [blame] | 38 | canvas->rotate(90); |
| 39 | draw_url_annotated_text_with_box(canvas, text, 150.0f, -55.0f, p, url); |
wangxianzhu | ef6c50a | 2015-09-17 20:38:02 -0700 | [diff] [blame] | 40 | canvas->restore(); |
halcanary | 74015ed | 2015-06-04 07:26:54 -0700 | [diff] [blame] | 41 | } |