• 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 "SkSGText.h"
9 
10 #include "SkCanvas.h"
11 #include "SkPaint.h"
12 #include "SkPath.h"
13 #include "SkTArray.h"
14 #include "SkTypeface.h"
15 
16 namespace sksg {
17 
Make(sk_sp<SkTypeface> tf,const SkString & text)18 sk_sp<Text> Text::Make(sk_sp<SkTypeface> tf, const SkString& text) {
19     return sk_sp<Text>(new Text(std::move(tf), text));
20 }
21 
Text(sk_sp<SkTypeface> tf,const SkString & text)22 Text::Text(sk_sp<SkTypeface> tf, const SkString& text)
23     : fTypeface(std::move(tf))
24     , fText(text) {}
25 
26 Text::~Text() = default;
27 
alignedPosition(SkScalar advance) const28 SkPoint Text::alignedPosition(SkScalar advance) const {
29     auto aligned = fPosition;
30 
31     switch (fAlign) {
32     case SkTextUtils::kLeft_Align:
33         break;
34     case SkTextUtils::kCenter_Align:
35         aligned.offset(-advance / 2, 0);
36         break;
37     case SkTextUtils::kRight_Align:
38         aligned.offset(-advance, 0);
39         break;
40     }
41 
42     return aligned;
43 }
44 
onRevalidate(InvalidationController *,const SkMatrix &)45 SkRect Text::onRevalidate(InvalidationController*, const SkMatrix&) {
46     // TODO: we could potentially track invals which don't require rebuilding the blob.
47 
48     SkFont font;
49     font.setTypeface(fTypeface);
50     font.setSize(fSize);
51     font.setScaleX(fScaleX);
52     font.setSkewX(fSkewX);
53     font.setEdging(fEdging);
54     font.setHinting(fHinting);
55 
56     // N.B.: fAlign is applied externally (in alignedPosition()), because
57     //  1) SkTextBlob has some trouble computing accurate bounds with alignment.
58     //  2) SkPaint::Align is slated for deprecation.
59 
60     fBlob = SkTextBlob::MakeFromText(fText.c_str(), fText.size(), font, kUTF8_SkTextEncoding);
61     if (!fBlob) {
62         return SkRect::MakeEmpty();
63     }
64 
65     const auto& bounds = fBlob->bounds();
66     const auto aligned_pos = this->alignedPosition(bounds.width());
67 
68     return bounds.makeOffset(aligned_pos.x(), aligned_pos.y());
69 }
70 
onDraw(SkCanvas * canvas,const SkPaint & paint) const71 void Text::onDraw(SkCanvas* canvas, const SkPaint& paint) const {
72     const auto aligned_pos = this->alignedPosition(this->bounds().width());
73     canvas->drawTextBlob(fBlob, aligned_pos.x(), aligned_pos.y(), paint);
74 }
75 
onAsPath() const76 SkPath Text::onAsPath() const {
77     // TODO
78     return SkPath();
79 }
80 
onClip(SkCanvas * canvas,bool antiAlias) const81 void Text::onClip(SkCanvas* canvas, bool antiAlias) const {
82     canvas->clipPath(this->asPath(), antiAlias);
83 }
84 
Make(sk_sp<SkTextBlob> blob)85 sk_sp<TextBlob> TextBlob::Make(sk_sp<SkTextBlob> blob) {
86     return sk_sp<TextBlob>(new TextBlob(std::move(blob)));
87 }
88 
TextBlob(sk_sp<SkTextBlob> blob)89 TextBlob::TextBlob(sk_sp<SkTextBlob> blob)
90     : fBlob(std::move(blob)) {}
91 
92 TextBlob::~TextBlob() = default;
93 
onRevalidate(InvalidationController *,const SkMatrix &)94 SkRect TextBlob::onRevalidate(InvalidationController*, const SkMatrix&) {
95     return fBlob ? fBlob->bounds() : SkRect::MakeEmpty();
96 }
97 
onDraw(SkCanvas * canvas,const SkPaint & paint) const98 void TextBlob::onDraw(SkCanvas* canvas, const SkPaint& paint) const {
99     canvas->drawTextBlob(fBlob, fPosition.x(), fPosition.y(), paint);
100 }
101 
onAsPath() const102 SkPath TextBlob::onAsPath() const {
103     // TODO
104     return SkPath();
105 }
106 
onClip(SkCanvas * canvas,bool antiAlias) const107 void TextBlob::onClip(SkCanvas* canvas, bool antiAlias) const {
108     canvas->clipPath(this->asPath(), antiAlias);
109 }
110 
111 } // namespace sksg
112