internal_links.cpp 2.5 KB

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