annotated_text.cpp 1.8 KB

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