blob: 5d21d158a7310d8d48547f33bbcc13ad5590ce3e [file] [log] [blame]
halcanary74015ed2015-06-04 07:26:54 -07001/*
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 Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
9#include "include/core/SkAnnotation.h"
Ben Wagnerd1701ba2019-04-30 13:44:26 -040010#include "include/core/SkCanvas.h"
11#include "include/core/SkColor.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "include/core/SkData.h"
13#include "include/core/SkFont.h"
Ben Wagnerd1701ba2019-04-30 13:44:26 -040014#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>
halcanary74015ed2015-06-04 07:26:54 -070021
22static void draw_url_annotated_text_with_box(
23 SkCanvas* canvas, const void* text,
Mike Reed17c574a2018-12-12 18:10:38 -050024 SkScalar x, SkScalar y, const SkFont& font, const char* url) {
halcanary74015ed2015-06-04 07:26:54 -070025 size_t byteLength = strlen(static_cast<const char*>(text));
26 SkRect bounds;
Ben Wagner51e15a62019-05-07 15:38:46 -040027 (void)font.measureText(text, byteLength, SkTextEncoding::kUTF8, &bounds);
halcanary74015ed2015-06-04 07:26:54 -070028 bounds.offset(x, y);
bungeman38d909e2016-08-02 14:40:46 -070029 sk_sp<SkData> urlData(SkData::MakeWithCString(url));
30 SkAnnotateRectWithURL(canvas, bounds, urlData.get());
halcanary74015ed2015-06-04 07:26:54 -070031 SkPaint shade;
32 shade.setColor(0x80346180);
33 canvas->drawRect(bounds, shade);
Ben Wagner51e15a62019-05-07 15:38:46 -040034 canvas->drawSimpleText(text, byteLength, SkTextEncoding::kUTF8, x, y, font, SkPaint());
halcanary74015ed2015-06-04 07:26:54 -070035}
36
37DEF_SIMPLE_GM(annotated_text, canvas, 512, 512) {
38 SkAutoCanvasRestore autoCanvasRestore(canvas, true);
39 canvas->clear(SK_ColorWHITE);
wangxianzhud76665d2015-07-17 17:23:15 -070040 canvas->clipRect(SkRect::MakeXYWH(64, 64, 256, 256));
halcanary74015ed2015-06-04 07:26:54 -070041 canvas->clear(0xFFEEEEEE);
Mike Reed17c574a2018-12-12 18:10:38 -050042 SkFont font;
43 font.setEdging(SkFont::Edging::kAlias);
44 font.setSize(40);
halcanary74015ed2015-06-04 07:26:54 -070045 const char text[] = "Click this link!";
46 const char url[] = "https://www.google.com/";
Mike Reed17c574a2018-12-12 18:10:38 -050047 draw_url_annotated_text_with_box(canvas, text, 200.0f, 80.0f, font, url);
wangxianzhuef6c50a2015-09-17 20:38:02 -070048 canvas->saveLayer(nullptr, nullptr);
halcanary74015ed2015-06-04 07:26:54 -070049 canvas->rotate(90);
Mike Reed17c574a2018-12-12 18:10:38 -050050 draw_url_annotated_text_with_box(canvas, text, 150.0f, -55.0f, font, url);
wangxianzhuef6c50a2015-09-17 20:38:02 -070051 canvas->restore();
halcanary74015ed2015-06-04 07:26:54 -070052}