Flutter Engine
The Flutter Engine
SkSGText.cpp
Go to the documentation of this file.
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
9
11#include "include/core/SkPath.h"
14
15#include <utility>
16
17class SkMatrix;
18
19namespace sksg {
20
22 return sk_sp<Text>(new Text(std::move(tf), text));
23}
24
25Text::Text(sk_sp<SkTypeface> tf, const SkString& text)
26 : fTypeface(std::move(tf))
27 , fText(text) {}
28
29Text::~Text() = default;
30
31SkPoint Text::alignedPosition(SkScalar advance) const {
32 auto aligned = fPosition;
33
34 switch (fAlign) {
36 break;
38 aligned.offset(-advance / 2, 0);
39 break;
41 aligned.offset(-advance, 0);
42 break;
43 }
44
45 return aligned;
46}
47
49 // TODO: we could potentially track invals which don't require rebuilding the blob.
50
52 font.setTypeface(fTypeface);
53 font.setSize(fSize);
54 font.setScaleX(fScaleX);
55 font.setSkewX(fSkewX);
56 font.setEdging(fEdging);
57 font.setHinting(fHinting);
58
59 // N.B.: fAlign is applied externally (in alignedPosition()), because
60 // 1) SkTextBlob has some trouble computing accurate bounds with alignment.
61 // 2) SkPaint::Align is slated for deprecation.
62
64 if (!fBlob) {
65 return SkRect::MakeEmpty();
66 }
67
68 const auto& bounds = fBlob->bounds();
69 const auto aligned_pos = this->alignedPosition(bounds.width());
70
71 return bounds.makeOffset(aligned_pos.x(), aligned_pos.y());
72}
73
74void Text::onDraw(SkCanvas* canvas, const SkPaint& paint) const {
75 const auto aligned_pos = this->alignedPosition(this->bounds().width());
76 canvas->drawTextBlob(fBlob, aligned_pos.x(), aligned_pos.y(), paint);
77}
78
79bool Text::onContains(const SkPoint& p) const {
80 return this->asPath().contains(p.x(), p.y());
81}
82
84 // TODO
85 return SkPath();
86}
87
88void Text::onClip(SkCanvas* canvas, bool antiAlias) const {
89 canvas->clipPath(this->asPath(), antiAlias);
90}
91
92} // namespace sksg
@ kUTF8
uses bytes to represent UTF-8 or ASCII
void clipPath(const SkPath &path, SkClipOp op, bool doAntiAlias)
Definition: SkCanvas.cpp:1456
void drawTextBlob(const SkTextBlob *blob, SkScalar x, SkScalar y, const SkPaint &paint)
Definition: SkCanvas.cpp:2484
Definition: SkFont.h:35
Definition: SkPath.h:59
bool contains(SkScalar x, SkScalar y) const
Definition: SkPath.cpp:3118
size_t size() const
Definition: SkString.h:131
const char * c_str() const
Definition: SkString.h:133
const SkRect & bounds() const
Definition: SkTextBlob.h:53
static sk_sp< SkTextBlob > MakeFromText(const void *text, size_t byteLength, const SkFont &font, SkTextEncoding encoding=SkTextEncoding::kUTF8)
Definition: SkTextBlob.cpp:788
SkPath asPath() const
const SkRect & bounds() const
Definition: SkSGNode.h:55
~Text() override
static sk_sp< Text > Make(sk_sp< SkTypeface > tf, const SkString &text)
Definition: SkSGText.cpp:21
bool onContains(const SkPoint &) const override
Definition: SkSGText.cpp:79
SkPath onAsPath() const override
Definition: SkSGText.cpp:83
SkRect onRevalidate(InvalidationController *, const SkMatrix &) override
Definition: SkSGText.cpp:48
void onDraw(SkCanvas *, const SkPaint &) const override
Definition: SkSGText.cpp:74
void onClip(SkCanvas *, bool antiAlias) const override
Definition: SkSGText.cpp:88
const Paint & paint
Definition: color_source.cc:38
float SkScalar
Definition: extension.cpp:12
std::u16string text
font
Font Metadata and Metrics.
Definition: Skottie.h:32
Definition: ref_ptr.h:256
int32_t width
constexpr float x() const
Definition: SkPoint_impl.h:181
static constexpr SkRect MakeEmpty()
Definition: SkRect.h:595
constexpr SkRect makeOffset(float dx, float dy) const
Definition: SkRect.h:965
constexpr float width() const
Definition: SkRect.h:762