| 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 "gm/gm.h" |
| 9 |
#include "include/core/SkAnnotation.h" |
| 10 |
#include "include/core/SkCanvas.h" |
| 11 |
#include "include/core/SkColor.h" |
| 12 |
#include "include/core/SkData.h" |
| 13 |
#include "include/core/SkFont.h" |
| 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 |
#include "tools/fonts/FontToolUtils.h" |
| 20 |
|
| 21 |
#include <string.h> |
| 22 |
|
| 23 |
static void draw_url_annotated_text_with_box( |
| 24 |
SkCanvas* canvas, const void* text, |
| 25 |
SkScalar x, SkScalar y, const SkFont& font, const char* url) { |
| 26 |
size_t byteLength = strlen(static_cast<const char*>(text)); |
| 27 |
SkRect bounds; |
| 28 |
(void)font.measureText(text, byteLength, SkTextEncoding::kUTF8, &bounds); |
| 29 |
bounds.offset(x, y); |
| 30 |
sk_sp<SkData> urlData(SkData::MakeWithCString(url)); |
| 31 |
SkAnnotateRectWithURL(canvas, bounds, urlData.get()); |
| 32 |
SkPaint shade; |
| 33 |
shade.setColor(0x80346180); |
| 34 |
canvas->drawRect(bounds, shade); |
| 35 |
canvas->drawSimpleText(text, byteLength, SkTextEncoding::kUTF8, x, y, font, SkPaint()); |
| 36 |
} |
| 37 |
|
| 38 |
DEF_SIMPLE_GM(annotated_text, canvas, 512, 512) { |
| 39 |
SkAutoCanvasRestore autoCanvasRestore(canvas, true); |
| 40 |
canvas->clear(SK_ColorWHITE); |
| 41 |
canvas->clipRect(SkRect::MakeXYWH(64, 64, 256, 256)); |
| 42 |
canvas->clear(0xFFEEEEEE); |
| 43 |
SkFont font = ToolUtils::DefaultPortableFont(); |
| 44 |
font.setEdging(SkFont::Edging::kAlias); |
| 45 |
font.setSize(40); |
| 46 |
const char text[] = "Click this link!"; |
| 47 |
const char url[] = "https://www.google.com/"; |
| 48 |
draw_url_annotated_text_with_box(canvas, text, 200.0f, 80.0f, font, url); |
| 49 |
canvas->saveLayer(nullptr, nullptr); |
| 50 |
canvas->rotate(90); |
| 51 |
draw_url_annotated_text_with_box(canvas, text, 150.0f, -55.0f, font, url); |
| 52 |
canvas->restore(); |
| 53 |
} |