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