OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright 2016 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 #include "SkShaper.h" |
| 8 #include "SkStream.h" |
| 9 #include "SkTextBlob.h" |
| 10 #include "SkTypeface.h" |
| 11 |
| 12 struct SkShaper::Impl { |
| 13 sk_sp<SkTypeface> fTypeface; |
| 14 }; |
| 15 |
| 16 SkShaper::SkShaper(sk_sp<SkTypeface> tf) : fImpl(new Impl) { |
| 17 fImpl->fTypeface = tf ? std::move(tf) : SkTypeface::MakeDefault(); |
| 18 } |
| 19 |
| 20 SkShaper::~SkShaper() {} |
| 21 |
| 22 bool SkShaper::good() const { return true; } |
| 23 |
| 24 SkScalar SkShaper::shape(SkTextBlobBuilder* builder, |
| 25 const SkPaint& srcPaint, |
| 26 const char* utf8text, |
| 27 size_t textBytes, |
| 28 SkPoint point) const { |
| 29 SkPaint paint(srcPaint); |
| 30 paint.setTypeface(fImpl->fTypeface); |
| 31 paint.setTextEncoding(SkPaint::kUTF8_TextEncoding); |
| 32 int glyphCount = paint.countText(utf8text, textBytes); |
| 33 if (glyphCount <= 0) { |
| 34 return 0; |
| 35 } |
| 36 SkRect bounds; |
| 37 (void)paint.measureText(utf8text, textBytes, &bounds); |
| 38 paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding); |
| 39 const SkTextBlobBuilder::RunBuffer& runBuffer = builder->allocRunPosH( |
| 40 paint, glyphCount, point.y(), &bounds); |
| 41 paint.setTextEncoding(SkPaint::kUTF8_TextEncoding); |
| 42 (void)paint.textToGlyphs(utf8text, textBytes, runBuffer.glyphs); |
| 43 (void)paint.getTextWidths(utf8text, textBytes, runBuffer.pos); |
| 44 SkScalar x = point.x(); |
| 45 for (int i = 0; i < glyphCount; ++i) { |
| 46 SkScalar w = runBuffer.pos[i]; |
| 47 runBuffer.pos[i] = x; |
| 48 x += w; |
| 49 } |
| 50 return (SkScalar)x; |
| 51 } |
OLD | NEW |