blob: 39734972a173b1692c11fe45cc532e8201b57578 [file] [log] [blame]
Mike Reed331ccfd2018-10-25 12:36:06 -04001/*
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
Mike Reedf78b7ea2018-12-25 22:06:17 -05008#include "SkFontPriv.h"
9#include "SkPath.h"
Mike Reed331ccfd2018-10-25 12:36:06 -040010#include "SkTextUtils.h"
Mike Reed34c9b6d2018-12-12 15:48:16 -050011#include "SkTextBlob.h"
Mike Reed331ccfd2018-10-25 12:36:06 -040012
Mike Reeddc5863c2018-12-23 23:19:14 -050013void SkTextUtils::Draw(SkCanvas* canvas, const void* text, size_t size, SkTextEncoding encoding,
14 SkScalar x, SkScalar y, const SkFont& font, const SkPaint& paint,
15 Align align) {
Mike Reed3a42ec02018-10-30 12:53:21 -040016 if (align != kLeft_Align) {
Mike Reeddc5863c2018-12-23 23:19:14 -050017 SkScalar width = font.measureText(text, size, encoding);
Mike Reed3a42ec02018-10-30 12:53:21 -040018 if (align == kCenter_Align) {
Mike Reed34c9b6d2018-12-12 15:48:16 -050019 width *= 0.5f;
Mike Reed331ccfd2018-10-25 12:36:06 -040020 }
Mike Reed34c9b6d2018-12-12 15:48:16 -050021 x -= width;
Mike Reed331ccfd2018-10-25 12:36:06 -040022 }
23
Mike Reeddc5863c2018-12-23 23:19:14 -050024 canvas->drawTextBlob(SkTextBlob::MakeFromText(text, size, font, encoding), x, y, paint);
Mike Reed331ccfd2018-10-25 12:36:06 -040025}
26
Mike Reedf78b7ea2018-12-25 22:06:17 -050027void SkTextUtils::GetPath(const void* text, size_t length, SkTextEncoding encoding,
28 SkScalar x, SkScalar y, const SkFont& font, SkPath* path) {
29 SkAutoToGlyphs ag(font, text, length, encoding);
30 SkAutoTArray<SkPoint> pos(ag.count());
31 font.getPos(ag.glyphs(), ag.count(), &pos[0], {x, y});
32
33 struct Rec {
34 SkPath* fDst;
35 const SkPoint* fPos;
36 } rec = { path, &pos[0] };
37
38 path->reset();
39 font.getPaths(ag.glyphs(), ag.count(), [](const SkPath* src, const SkMatrix& mx, void* ctx) {
40 Rec* rec = (Rec*)ctx;
41 if (src) {
42 SkMatrix m(mx);
43 m.postTranslate(rec->fPos->fX, rec->fPos->fY);
44 rec->fDst->addPath(*src, m);
45 }
46 rec->fPos += 1;
47 }, &rec);
48}
49