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
8 #include "include/core/SkPath.h"
9 #include "include/core/SkTextBlob.h"
10 #include "include/utils/SkTextUtils.h"
11 #include "src/core/SkFontPriv.h"
12
Draw(SkCanvas * canvas,const void * text,size_t size,SkTextEncoding encoding,SkScalar x,SkScalar y,const SkFont & font,const SkPaint & paint,Align align)13 void 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) {
16 if (align != kLeft_Align) {
17 SkScalar width = font.measureText(text, size, encoding);
18 if (align == kCenter_Align) {
19 width *= 0.5f;
20 }
21 x -= width;
22 }
23
24 canvas->drawTextBlob(SkTextBlob::MakeFromText(text, size, font, encoding), x, y, paint);
25 }
26
GetPath(const void * text,size_t length,SkTextEncoding encoding,SkScalar x,SkScalar y,const SkFont & font,SkPath * path)27 void 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.get(), {x, y});
32
33 struct Rec {
34 SkPath* fDst;
35 const SkPoint* fPos;
36 } rec = { path, pos.get() };
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
50