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