blob: a6b442a2a7170edc4adfe7547a1ade90bf111e90 [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"
Kevin Lubickbca43ec2023-10-30 10:11:22 -040019#include "tools/fonts/FontToolUtils.h"
Ben Wagnerd1701ba2019-04-30 13:44:26 -040020
21#include <string.h>
halcanary74015ed2015-06-04 07:26:54 -070022
23static void draw_url_annotated_text_with_box(
24 SkCanvas* canvas, const void* text,
Mike Reed17c574a2018-12-12 18:10:38 -050025 SkScalar x, SkScalar y, const SkFont& font, const char* url) {
halcanary74015ed2015-06-04 07:26:54 -070026 size_t byteLength = strlen(static_cast<const char*>(text));
27 SkRect bounds;
Ben Wagner51e15a62019-05-07 15:38:46 -040028 (void)font.measureText(text, byteLength, SkTextEncoding::kUTF8, &bounds);
halcanary74015ed2015-06-04 07:26:54 -070029 bounds.offset(x, y);
bungeman38d909e2016-08-02 14:40:46 -070030 sk_sp<SkData> urlData(SkData::MakeWithCString(url));
31 SkAnnotateRectWithURL(canvas, bounds, urlData.get());
halcanary74015ed2015-06-04 07:26:54 -070032 SkPaint shade;
33 shade.setColor(0x80346180);
34 canvas->drawRect(bounds, shade);
Ben Wagner51e15a62019-05-07 15:38:46 -040035 canvas->drawSimpleText(text, byteLength, SkTextEncoding::kUTF8, x, y, font, SkPaint());
halcanary74015ed2015-06-04 07:26:54 -070036}
37
38DEF_SIMPLE_GM(annotated_text, canvas, 512, 512) {
39 SkAutoCanvasRestore autoCanvasRestore(canvas, true);
40 canvas->clear(SK_ColorWHITE);
wangxianzhud76665d2015-07-17 17:23:15 -070041 canvas->clipRect(SkRect::MakeXYWH(64, 64, 256, 256));
halcanary74015ed2015-06-04 07:26:54 -070042 canvas->clear(0xFFEEEEEE);
Kevin Lubickbca43ec2023-10-30 10:11:22 -040043 SkFont font = ToolUtils::DefaultPortableFont();
Mike Reed17c574a2018-12-12 18:10:38 -050044 font.setEdging(SkFont::Edging::kAlias);
45 font.setSize(40);
halcanary74015ed2015-06-04 07:26:54 -070046 const char text[] = "Click this link!";
47 const char url[] = "https://www.google.com/";
Mike Reed17c574a2018-12-12 18:10:38 -050048 draw_url_annotated_text_with_box(canvas, text, 200.0f, 80.0f, font, url);
wangxianzhuef6c50a2015-09-17 20:38:02 -070049 canvas->saveLayer(nullptr, nullptr);
halcanary74015ed2015-06-04 07:26:54 -070050 canvas->rotate(90);
Mike Reed17c574a2018-12-12 18:10:38 -050051 draw_url_annotated_text_with_box(canvas, text, 150.0f, -55.0f, font, url);
wangxianzhuef6c50a2015-09-17 20:38:02 -070052 canvas->restore();
halcanary74015ed2015-06-04 07:26:54 -070053}