Florin Malita | 51012ce | 2018-01-31 17:06:59 -0500 | [diff] [blame] | 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" |
Florin Malita | 51012ce | 2018-01-31 17:06:59 -0500 | [diff] [blame] | 14 | #include "SkTypeface.h" |
| 15 | |
| 16 | namespace sksg { |
| 17 | |
| 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 | |
| 22 | Text::Text(sk_sp<SkTypeface> tf, const SkString& text) |
| 23 | : fTypeface(std::move(tf)) |
| 24 | , fText(text) {} |
| 25 | |
| 26 | Text::~Text() = default; |
| 27 | |
Florin Malita | 77af386 | 2018-08-15 16:51:42 -0400 | [diff] [blame] | 28 | SkPoint Text::alignedPosition(SkScalar advance) const { |
| 29 | auto aligned = fPosition; |
| 30 | |
| 31 | switch (fAlign) { |
Mike Reed | 3a42ec0 | 2018-10-30 12:53:21 -0400 | [diff] [blame] | 32 | case SkTextUtils::kLeft_Align: |
Florin Malita | 77af386 | 2018-08-15 16:51:42 -0400 | [diff] [blame] | 33 | break; |
Mike Reed | 3a42ec0 | 2018-10-30 12:53:21 -0400 | [diff] [blame] | 34 | case SkTextUtils::kCenter_Align: |
Florin Malita | 77af386 | 2018-08-15 16:51:42 -0400 | [diff] [blame] | 35 | aligned.offset(-advance / 2, 0); |
| 36 | break; |
Mike Reed | 3a42ec0 | 2018-10-30 12:53:21 -0400 | [diff] [blame] | 37 | case SkTextUtils::kRight_Align: |
Florin Malita | 77af386 | 2018-08-15 16:51:42 -0400 | [diff] [blame] | 38 | aligned.offset(-advance, 0); |
| 39 | break; |
| 40 | } |
| 41 | |
| 42 | return aligned; |
| 43 | } |
| 44 | |
Florin Malita | 51012ce | 2018-01-31 17:06:59 -0500 | [diff] [blame] | 45 | SkRect Text::onRevalidate(InvalidationController*, const SkMatrix&) { |
| 46 | // TODO: we could potentially track invals which don't require rebuilding the blob. |
| 47 | |
Mike Reed | 2ed7820 | 2018-11-21 15:10:08 -0500 | [diff] [blame] | 48 | SkFont font; |
Florin Malita | 51012ce | 2018-01-31 17:06:59 -0500 | [diff] [blame] | 49 | font.setTypeface(fTypeface); |
Mike Reed | 2ed7820 | 2018-11-21 15:10:08 -0500 | [diff] [blame] | 50 | font.setSize(fSize); |
| 51 | font.setScaleX(fScaleX); |
| 52 | font.setSkewX(fSkewX); |
Florin Malita | f7d6ac1 | 2018-11-21 16:03:58 -0500 | [diff] [blame] | 53 | font.setEdging(fEdging); |
Florin Malita | 51012ce | 2018-01-31 17:06:59 -0500 | [diff] [blame] | 54 | font.setHinting(fHinting); |
| 55 | |
Florin Malita | 77af386 | 2018-08-15 16:51:42 -0400 | [diff] [blame] | 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 | |
Mike Reed | 722dd61 | 2018-12-07 14:38:20 -0500 | [diff] [blame] | 60 | fBlob = SkTextBlob::MakeFromText(fText.c_str(), fText.size(), font, kUTF8_SkTextEncoding); |
Florin Malita | 77af386 | 2018-08-15 16:51:42 -0400 | [diff] [blame] | 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()); |
Florin Malita | 51012ce | 2018-01-31 17:06:59 -0500 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | void Text::onDraw(SkCanvas* canvas, const SkPaint& paint) const { |
Florin Malita | 77af386 | 2018-08-15 16:51:42 -0400 | [diff] [blame] | 72 | const auto aligned_pos = this->alignedPosition(this->bounds().width()); |
| 73 | canvas->drawTextBlob(fBlob, aligned_pos.x(), aligned_pos.y(), paint); |
Florin Malita | 51012ce | 2018-01-31 17:06:59 -0500 | [diff] [blame] | 74 | } |
| 75 | |
Florin Malita | eb46bd8 | 2019-02-12 09:33:21 -0500 | [diff] [blame] | 76 | bool Text::onContains(const SkPoint& p) const { |
| 77 | return this->asPath().contains(p.x(), p.y()); |
| 78 | } |
| 79 | |
Florin Malita | 51012ce | 2018-01-31 17:06:59 -0500 | [diff] [blame] | 80 | SkPath Text::onAsPath() const { |
| 81 | // TODO |
| 82 | return SkPath(); |
| 83 | } |
| 84 | |
| 85 | void Text::onClip(SkCanvas* canvas, bool antiAlias) const { |
| 86 | canvas->clipPath(this->asPath(), antiAlias); |
| 87 | } |
| 88 | |
Florin Malita | 0a8b4e1 | 2018-11-01 11:16:18 -0400 | [diff] [blame] | 89 | sk_sp<TextBlob> TextBlob::Make(sk_sp<SkTextBlob> blob) { |
| 90 | return sk_sp<TextBlob>(new TextBlob(std::move(blob))); |
| 91 | } |
| 92 | |
| 93 | TextBlob::TextBlob(sk_sp<SkTextBlob> blob) |
| 94 | : fBlob(std::move(blob)) {} |
| 95 | |
| 96 | TextBlob::~TextBlob() = default; |
| 97 | |
| 98 | SkRect TextBlob::onRevalidate(InvalidationController*, const SkMatrix&) { |
| 99 | return fBlob ? fBlob->bounds() : SkRect::MakeEmpty(); |
| 100 | } |
| 101 | |
| 102 | void TextBlob::onDraw(SkCanvas* canvas, const SkPaint& paint) const { |
| 103 | canvas->drawTextBlob(fBlob, fPosition.x(), fPosition.y(), paint); |
| 104 | } |
| 105 | |
Florin Malita | eb46bd8 | 2019-02-12 09:33:21 -0500 | [diff] [blame] | 106 | bool TextBlob::onContains(const SkPoint& p) const { |
| 107 | return this->asPath().contains(p.x(), p.y()); |
| 108 | } |
| 109 | |
Florin Malita | 0a8b4e1 | 2018-11-01 11:16:18 -0400 | [diff] [blame] | 110 | SkPath TextBlob::onAsPath() const { |
| 111 | // TODO |
| 112 | return SkPath(); |
| 113 | } |
| 114 | |
| 115 | void TextBlob::onClip(SkCanvas* canvas, bool antiAlias) const { |
| 116 | canvas->clipPath(this->asPath(), antiAlias); |
| 117 | } |
| 118 | |
Florin Malita | 51012ce | 2018-01-31 17:06:59 -0500 | [diff] [blame] | 119 | } // namespace sksg |