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