| 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/SkPathBuilder.h" |
| 15 |
#include "include/core/SkPoint.h" |
| 16 |
#include "include/core/SkScalar.h" |
| 17 |
#include "include/core/SkTextBlob.h" |
| 18 |
#include "include/private/base/SkTemplates.h" |
| 19 |
#include "src/core/SkFontPriv.h" |
| 20 |
|
| 21 |
using namespace skia_private; |
| 22 |
|
| 23 |
class SkPaint; |
| 24 |
|
| 25 |
void SkTextUtils::Draw(SkCanvas* canvas, const void* text, size_t size, SkTextEncoding encoding, |
| 26 |
SkScalar x, SkScalar y, const SkFont& font, const SkPaint& paint, |
| 27 |
Align align) { |
| 28 |
if (align != kLeft_Align) { |
| 29 |
SkScalar width = font.measureText(text, size, encoding); |
| 30 |
if (align == kCenter_Align) { |
| 31 |
width *= 0.5f; |
| 32 |
} |
| 33 |
x -= width; |
| 34 |
} |
| 35 |
|
| 36 |
canvas->drawTextBlob(SkTextBlob::MakeFromText(text, size, font, encoding), x, y, paint); |
| 37 |
} |
| 38 |
|
| 39 |
void SkTextUtils::GetPath(const void* text, size_t length, SkTextEncoding encoding, |
| 40 |
SkScalar x, SkScalar y, const SkFont& font, SkPath* path) { |
| 41 |
SkAutoToGlyphs ag(font, text, length, encoding); |
| 42 |
AutoTArray<SkPoint> pos(ag.count()); |
| 43 |
font.getPos(ag.glyphs(), pos, {x, y}); |
| 44 |
|
| 45 |
struct Rec { |
| 46 |
SkPathBuilder fDst; |
| 47 |
const SkPoint* fPos; |
| 48 |
} rec = { {}, pos.get() }; |
| 49 |
|
| 50 |
font.getPaths(ag.glyphs(), [](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 |
*path = rec.fDst.detach(); |
| 60 |
} |
| 61 |
|