SkTextUtils.cpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /*
  2. * Copyright 2018 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 "include/core/SkPath.h"
  8. #include "include/core/SkTextBlob.h"
  9. #include "include/utils/SkTextUtils.h"
  10. #include "src/core/SkFontPriv.h"
  11. void SkTextUtils::Draw(SkCanvas* canvas, const void* text, size_t size, SkTextEncoding encoding,
  12. SkScalar x, SkScalar y, const SkFont& font, const SkPaint& paint,
  13. Align align) {
  14. if (align != kLeft_Align) {
  15. SkScalar width = font.measureText(text, size, encoding);
  16. if (align == kCenter_Align) {
  17. width *= 0.5f;
  18. }
  19. x -= width;
  20. }
  21. canvas->drawTextBlob(SkTextBlob::MakeFromText(text, size, font, encoding), x, y, paint);
  22. }
  23. void SkTextUtils::GetPath(const void* text, size_t length, SkTextEncoding encoding,
  24. SkScalar x, SkScalar y, const SkFont& font, SkPath* path) {
  25. SkAutoToGlyphs ag(font, text, length, encoding);
  26. SkAutoTArray<SkPoint> pos(ag.count());
  27. font.getPos(ag.glyphs(), ag.count(), pos.get(), {x, y});
  28. struct Rec {
  29. SkPath* fDst;
  30. const SkPoint* fPos;
  31. } rec = { path, pos.get() };
  32. path->reset();
  33. font.getPaths(ag.glyphs(), ag.count(), [](const SkPath* src, const SkMatrix& mx, void* ctx) {
  34. Rec* rec = (Rec*)ctx;
  35. if (src) {
  36. SkMatrix m(mx);
  37. m.postTranslate(rec->fPos->fX, rec->fPos->fY);
  38. rec->fDst->addPath(*src, m);
  39. }
  40. rec->fPos += 1;
  41. }, &rec);
  42. }