| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2016 Google Inc. | 2 * Copyright 2016 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 #include "SkShaper.h" | 7 #include "SkShaper.h" |
| 8 #include "SkStream.h" | 8 #include "SkStream.h" |
| 9 #include "SkTextBlob.h" | 9 #include "SkTextBlob.h" |
| 10 #include "SkTypeface.h" | 10 #include "SkTypeface.h" |
| 11 | 11 |
| 12 struct SkShaper::Impl { | 12 struct SkShaper::Impl { |
| 13 sk_sp<SkTypeface> fTypeface; | 13 sk_sp<SkTypeface> fTypeface; |
| 14 }; | 14 }; |
| 15 | 15 |
| 16 SkShaper::SkShaper(sk_sp<SkTypeface> tf) : fImpl(new Impl) { | 16 SkShaper::SkShaper(sk_sp<SkTypeface> tf) : fImpl(new Impl) { |
| 17 fImpl->fTypeface = tf ? std::move(tf) : SkTypeface::MakeDefault(); | 17 fImpl->fTypeface = tf ? std::move(tf) : SkTypeface::MakeDefault(); |
| 18 } | 18 } |
| 19 | 19 |
| 20 SkShaper::~SkShaper() {} | 20 SkShaper::~SkShaper() {} |
| 21 | 21 |
| 22 bool SkShaper::good() const { return true; } | 22 bool SkShaper::good() const { return true; } |
| 23 | 23 |
| 24 // This example only uses public API, so we don't use SkUTF8_NextUnichar. |
| 25 unsigned utf8_lead_byte_to_count(const char* ptr) { |
| 26 uint8_t c = *(const uint8_t*)ptr; |
| 27 SkASSERT(c <= 0xF7); |
| 28 SkASSERT((c & 0xC0) != 0x80); |
| 29 return (((0xE5 << 24) >> ((unsigned)c >> 4 << 1)) & 3) + 1; |
| 30 } |
| 31 |
| 24 SkScalar SkShaper::shape(SkTextBlobBuilder* builder, | 32 SkScalar SkShaper::shape(SkTextBlobBuilder* builder, |
| 25 const SkPaint& srcPaint, | 33 const SkPaint& srcPaint, |
| 26 const char* utf8text, | 34 const char* utf8text, |
| 27 size_t textBytes, | 35 size_t textBytes, |
| 28 SkPoint point) const { | 36 SkPoint point) const { |
| 29 SkPaint paint(srcPaint); | 37 SkPaint paint(srcPaint); |
| 30 paint.setTypeface(fImpl->fTypeface); | 38 paint.setTypeface(fImpl->fTypeface); |
| 31 paint.setTextEncoding(SkPaint::kUTF8_TextEncoding); | 39 paint.setTextEncoding(SkPaint::kUTF8_TextEncoding); |
| 32 int glyphCount = paint.countText(utf8text, textBytes); | 40 int glyphCount = paint.countText(utf8text, textBytes); |
| 33 if (glyphCount <= 0) { | 41 if (glyphCount <= 0) { |
| 34 return 0; | 42 return 0; |
| 35 } | 43 } |
| 36 SkRect bounds; | 44 SkRect bounds; |
| 37 (void)paint.measureText(utf8text, textBytes, &bounds); | 45 (void)paint.measureText(utf8text, textBytes, &bounds); |
| 38 paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding); | 46 paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding); |
| 39 const SkTextBlobBuilder::RunBuffer& runBuffer = builder->allocRunPosH( | 47 const SkTextBlobBuilder::RunBuffer& runBuffer = |
| 40 paint, glyphCount, point.y(), &bounds); | 48 builder->allocRunTextPosH(paint, glyphCount, point.y(), textBytes, SkStr
ing(), &bounds); |
| 49 memcpy(runBuffer.utf8text, utf8text, textBytes); |
| 50 const char* txtPtr = utf8text; |
| 51 for (int i = 0; i < glyphCount; ++i) { |
| 52 // Each charater maps to exactly one glyph via SkGlyphCache::unicharToGl
yph(). |
| 53 runBuffer.clusters[i] = SkToU32(txtPtr - utf8text); |
| 54 txtPtr += utf8_lead_byte_to_count(txtPtr); |
| 55 SkASSERT(txtPtr <= utf8text + textBytes); |
| 56 } |
| 41 paint.setTextEncoding(SkPaint::kUTF8_TextEncoding); | 57 paint.setTextEncoding(SkPaint::kUTF8_TextEncoding); |
| 42 (void)paint.textToGlyphs(utf8text, textBytes, runBuffer.glyphs); | 58 (void)paint.textToGlyphs(utf8text, textBytes, runBuffer.glyphs); |
| 43 (void)paint.getTextWidths(utf8text, textBytes, runBuffer.pos); | 59 (void)paint.getTextWidths(utf8text, textBytes, runBuffer.pos); |
| 44 SkScalar x = point.x(); | 60 SkScalar x = point.x(); |
| 45 for (int i = 0; i < glyphCount; ++i) { | 61 for (int i = 0; i < glyphCount; ++i) { |
| 46 SkScalar w = runBuffer.pos[i]; | 62 SkScalar w = runBuffer.pos[i]; |
| 47 runBuffer.pos[i] = x; | 63 runBuffer.pos[i] = x; |
| 48 x += w; | 64 x += w; |
| 49 } | 65 } |
| 50 return (SkScalar)x; | 66 return (SkScalar)x; |
| 51 } | 67 } |
| OLD | NEW |