xref: /aosp_15_r20/external/skia/gm/internal_links.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2013 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/SkPaint.h"
15 #include "include/core/SkPoint.h"
16 #include "include/core/SkRect.h"
17 #include "include/core/SkRefCnt.h"
18 #include "include/core/SkScalar.h"
19 #include "include/core/SkSize.h"
20 #include "include/core/SkString.h"
21 #include "include/core/SkTypeface.h"
22 #include "tools/ToolUtils.h"
23 #include "tools/fonts/FontToolUtils.h"
24 
25 namespace {
26 
27 /** Draws two rectangles. In output formats that support internal links (PDF),
28  *  clicking the one labeled "Link to A" should take you to the one labeled
29  *  "Target A". Note that you'll need to zoom your PDF viewer in a fair bit in
30  *  order for the scrolling to not be blocked by the edge of the document.
31  */
32 class InternalLinksGM : public skiagm::GM {
onOnceBeforeDraw()33     void onOnceBeforeDraw() override { this->setBGColor(0xFFDDDDDD); }
34 
getName() const35     SkString getName() const override { return SkString("internal_links"); }
36 
getISize()37     SkISize getISize() override { return {700, 500}; }
38 
onDraw(SkCanvas * canvas)39     void onDraw(SkCanvas* canvas) override {
40         sk_sp<SkData> name(SkData::MakeWithCString("target-a"));
41 
42         canvas->save();
43         canvas->translate(SkIntToScalar(100), SkIntToScalar(100));
44         drawLabeledRect(canvas, "Link to A", 0, 0);
45         SkRect rect = SkRect::MakeXYWH(0, 0, SkIntToScalar(50), SkIntToScalar(20));
46         SkAnnotateLinkToDestination(canvas, rect, name.get());
47         canvas->restore();
48 
49         canvas->save();
50         canvas->translate(SkIntToScalar(200), SkIntToScalar(200));
51         SkPoint point = SkPoint::Make(SkIntToScalar(100), SkIntToScalar(50));
52         drawLabeledRect(canvas, "Target A", point.x(), point.y());
53         SkAnnotateNamedDestination(canvas, point, name.get());
54         canvas->restore();
55     }
56 
57     /** Draw an arbitrary rectangle at a given location and label it with some
58      *  text. */
drawLabeledRect(SkCanvas * canvas,const char * text,SkScalar x,SkScalar y)59     void drawLabeledRect(SkCanvas* canvas, const char* text, SkScalar x, SkScalar y) {
60         SkPaint paint;
61         paint.setColor(SK_ColorBLUE);
62         SkRect rect = SkRect::MakeXYWH(x, y,
63                                        SkIntToScalar(50), SkIntToScalar(20));
64         canvas->drawRect(rect, paint);
65 
66         SkFont font(ToolUtils::DefaultPortableTypeface(), 25);
67         paint.setColor(SK_ColorBLACK);
68         canvas->drawString(text, x, y, font, paint);
69     }
70 };
71 }  // namespace
72 
73 DEF_GM( return new InternalLinksGM; )
74