• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/utils/SkTextUtils.h"
9 
10 #include "include/core/SkCanvas.h"
11 #include "include/core/SkFont.h"
12 #include "include/core/SkMatrix.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/private/base/SkTemplates.h"
18 #include "src/core/SkFontPriv.h"
19 
20 using namespace skia_private;
21 
22 class SkPaint;
23 
Draw(SkCanvas * canvas,const void * text,size_t size,SkTextEncoding encoding,SkScalar x,SkScalar y,const SkFont & font,const SkPaint & paint,Align align)24 void SkTextUtils::Draw(SkCanvas* canvas, const void* text, size_t size, SkTextEncoding encoding,
25                        SkScalar x, SkScalar y, const SkFont& font, const SkPaint& paint,
26                        Align align) {
27     if (align != kLeft_Align) {
28         SkScalar width = font.measureText(text, size, encoding);
29         if (align == kCenter_Align) {
30             width *= 0.5f;
31         }
32         x -= width;
33     }
34 
35     canvas->drawTextBlob(SkTextBlob::MakeFromText(text, size, font, encoding), x, y, paint);
36 }
37 
GetPath(const void * text,size_t length,SkTextEncoding encoding,SkScalar x,SkScalar y,const SkFont & font,SkPath * path)38 void SkTextUtils::GetPath(const void* text, size_t length, SkTextEncoding encoding,
39                           SkScalar x, SkScalar y, const SkFont& font, SkPath* path) {
40     SkAutoToGlyphs ag(font, text, length, encoding);
41     AutoTArray<SkPoint> pos(ag.count());
42     font.getPos(ag.glyphs(), ag.count(), pos.get(), {x, y});
43 
44     struct Rec {
45         SkPath* fDst;
46         const SkPoint* fPos;
47     } rec = { path, pos.get() };
48 
49     path->reset();
50     font.getPaths(ag.glyphs(), ag.count(), [](const SkPath* src, const SkMatrix& mx, void* ctx) {
51         Rec* rec = (Rec*)ctx;
52         if (src) {
53             SkMatrix m(mx);
54             m.postTranslate(rec->fPos->fX, rec->fPos->fY);
55             rec->fDst->addPath(*src, m);
56         }
57         rec->fPos += 1;
58     }, &rec);
59 }
60 
61