getpostextpath.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*
  2. * Copyright 2012 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/SkCanvas.h"
  9. #include "include/core/SkColor.h"
  10. #include "include/core/SkFont.h"
  11. #include "include/core/SkFontTypes.h"
  12. #include "include/core/SkPaint.h"
  13. #include "include/core/SkPath.h"
  14. #include "include/core/SkPoint.h"
  15. #include "include/core/SkScalar.h"
  16. #include "include/core/SkTextBlob.h"
  17. #include "include/core/SkTypeface.h"
  18. #include "include/private/SkTemplates.h"
  19. #include "include/utils/SkRandom.h"
  20. #include "src/core/SkFontPriv.h"
  21. #include "tools/ToolUtils.h"
  22. #include <string.h>
  23. static void strokePath(SkCanvas* canvas, const SkPath& path) {
  24. SkPaint paint;
  25. paint.setAntiAlias(true);
  26. paint.setColor(SK_ColorRED);
  27. paint.setStyle(SkPaint::kStroke_Style);
  28. canvas->drawPath(path, paint);
  29. }
  30. DEF_SIMPLE_GM(getpostextpath, canvas, 480, 780) {
  31. // explicitly add spaces, to test a prev. bug
  32. const char* text = "Ham bur ge fons";
  33. size_t len = strlen(text);
  34. SkPath path;
  35. SkFont font;
  36. font.setTypeface(ToolUtils::create_portable_typeface());
  37. font.setSize(48);
  38. SkPaint paint;
  39. paint.setAntiAlias(true);
  40. canvas->translate(SkIntToScalar(10), SkIntToScalar(64));
  41. canvas->drawSimpleText(text, len, SkTextEncoding::kUTF8, 0, 0, font, paint);
  42. ToolUtils::get_text_path(font, text, len, SkTextEncoding::kUTF8, &path, nullptr);
  43. strokePath(canvas, path);
  44. path.reset();
  45. SkAutoToGlyphs atg(font, text, len, SkTextEncoding::kUTF8);
  46. const int count = atg.count();
  47. SkAutoTArray<SkPoint> pos(count);
  48. SkAutoTArray<SkScalar> widths(count);
  49. font.getWidths(atg.glyphs(), count, &widths[0]);
  50. SkRandom rand;
  51. SkScalar x = SkIntToScalar(20);
  52. SkScalar y = SkIntToScalar(100);
  53. for (int i = 0; i < count; ++i) {
  54. pos[i].set(x, y + rand.nextSScalar1() * 24);
  55. x += widths[i];
  56. }
  57. canvas->translate(0, SkIntToScalar(64));
  58. canvas->drawTextBlob(SkTextBlob::MakeFromPosText(text, len, &pos[0], font), 0, 0, paint);
  59. ToolUtils::get_text_path(font, text, len, SkTextEncoding::kUTF8, &path, &pos[0]);
  60. strokePath(canvas, path);
  61. }