Update rive-cpp to 2.0 version
[platform/core/uifw/rive-tizen.git] / submodule / skia / src / utils / SkTextUtils.cpp
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/SkFontTypes.h"
13 #include "include/core/SkMatrix.h"
14 #include "include/core/SkPath.h"
15 #include "include/core/SkPoint.h"
16 #include "include/core/SkScalar.h"
17 #include "include/core/SkTextBlob.h"
18 #include "include/private/SkTemplates.h"
19 #include "src/core/SkFontPriv.h"
20
21 class SkPaint;
22
23 void SkTextUtils::Draw(SkCanvas* canvas, const void* text, size_t size, SkTextEncoding encoding,
24                        SkScalar x, SkScalar y, const SkFont& font, const SkPaint& paint,
25                        Align align) {
26     if (align != kLeft_Align) {
27         SkScalar width = font.measureText(text, size, encoding);
28         if (align == kCenter_Align) {
29             width *= 0.5f;
30         }
31         x -= width;
32     }
33
34     canvas->drawTextBlob(SkTextBlob::MakeFromText(text, size, font, encoding), x, y, paint);
35 }
36
37 void SkTextUtils::GetPath(const void* text, size_t length, SkTextEncoding encoding,
38                           SkScalar x, SkScalar y, const SkFont& font, SkPath* path) {
39     SkAutoToGlyphs ag(font, text, length, encoding);
40     SkAutoTArray<SkPoint> pos(ag.count());
41     font.getPos(ag.glyphs(), ag.count(), pos.get(), {x, y});
42
43     struct Rec {
44         SkPath* fDst;
45         const SkPoint* fPos;
46     } rec = { path, pos.get() };
47
48     path->reset();
49     font.getPaths(ag.glyphs(), ag.count(), [](const SkPath* src, const SkMatrix& mx, void* ctx) {
50         Rec* rec = (Rec*)ctx;
51         if (src) {
52             SkMatrix m(mx);
53             m.postTranslate(rec->fPos->fX, rec->fPos->fY);
54             rec->fDst->addPath(*src, m);
55         }
56         rec->fPos += 1;
57     }, &rec);
58 }
59