halcanary | 13cba49 | 2016-08-03 10:43:55 -0700 | [diff] [blame] | 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 | */ |
Hal Canary | c640d0d | 2018-06-13 09:59:02 -0400 | [diff] [blame] | 7 | |
halcanary | 13cba49 | 2016-08-03 10:43:55 -0700 | [diff] [blame] | 8 | #include "SkShaper.h" |
Hal Canary | c640d0d | 2018-06-13 09:59:02 -0400 | [diff] [blame] | 9 | |
halcanary | 13cba49 | 2016-08-03 10:43:55 -0700 | [diff] [blame] | 10 | #include "SkStream.h" |
| 11 | #include "SkTextBlob.h" |
Hal Canary | c640d0d | 2018-06-13 09:59:02 -0400 | [diff] [blame] | 12 | #include "SkTo.h" |
halcanary | 13cba49 | 2016-08-03 10:43:55 -0700 | [diff] [blame] | 13 | #include "SkTypeface.h" |
| 14 | |
| 15 | struct SkShaper::Impl { |
| 16 | sk_sp<SkTypeface> fTypeface; |
| 17 | }; |
| 18 | |
| 19 | SkShaper::SkShaper(sk_sp<SkTypeface> tf) : fImpl(new Impl) { |
| 20 | fImpl->fTypeface = tf ? std::move(tf) : SkTypeface::MakeDefault(); |
| 21 | } |
| 22 | |
| 23 | SkShaper::~SkShaper() {} |
| 24 | |
| 25 | bool SkShaper::good() const { return true; } |
| 26 | |
halcanary | 3eee9d9 | 2016-09-10 07:01:53 -0700 | [diff] [blame] | 27 | // This example only uses public API, so we don't use SkUTF8_NextUnichar. |
| 28 | unsigned utf8_lead_byte_to_count(const char* ptr) { |
| 29 | uint8_t c = *(const uint8_t*)ptr; |
| 30 | SkASSERT(c <= 0xF7); |
| 31 | SkASSERT((c & 0xC0) != 0x80); |
| 32 | return (((0xE5 << 24) >> ((unsigned)c >> 4 << 1)) & 3) + 1; |
| 33 | } |
| 34 | |
Ben Wagner | 5d4dd8b | 2018-01-25 14:37:17 -0500 | [diff] [blame] | 35 | SkPoint SkShaper::shape(SkTextBlobBuilder* builder, |
| 36 | const SkPaint& srcPaint, |
| 37 | const char* utf8text, |
| 38 | size_t textBytes, |
| 39 | bool leftToRight, |
| 40 | SkPoint point, |
| 41 | SkScalar width) const { |
Ben Wagner | a25fbef | 2017-08-30 13:56:19 -0400 | [diff] [blame] | 42 | sk_ignore_unused_variable(leftToRight); |
| 43 | |
halcanary | 13cba49 | 2016-08-03 10:43:55 -0700 | [diff] [blame] | 44 | SkPaint paint(srcPaint); |
| 45 | paint.setTypeface(fImpl->fTypeface); |
| 46 | paint.setTextEncoding(SkPaint::kUTF8_TextEncoding); |
| 47 | int glyphCount = paint.countText(utf8text, textBytes); |
| 48 | if (glyphCount <= 0) { |
Ben Wagner | 5d4dd8b | 2018-01-25 14:37:17 -0500 | [diff] [blame] | 49 | return point; |
halcanary | 13cba49 | 2016-08-03 10:43:55 -0700 | [diff] [blame] | 50 | } |
| 51 | SkRect bounds; |
Ben Wagner | 5d4dd8b | 2018-01-25 14:37:17 -0500 | [diff] [blame] | 52 | SkPaint::FontMetrics metrics; |
| 53 | paint.getFontMetrics(&metrics); |
| 54 | point.fY -= metrics.fAscent; |
halcanary | 13cba49 | 2016-08-03 10:43:55 -0700 | [diff] [blame] | 55 | (void)paint.measureText(utf8text, textBytes, &bounds); |
| 56 | paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding); |
halcanary | 3eee9d9 | 2016-09-10 07:01:53 -0700 | [diff] [blame] | 57 | const SkTextBlobBuilder::RunBuffer& runBuffer = |
| 58 | builder->allocRunTextPosH(paint, glyphCount, point.y(), textBytes, SkString(), &bounds); |
| 59 | memcpy(runBuffer.utf8text, utf8text, textBytes); |
| 60 | const char* txtPtr = utf8text; |
| 61 | for (int i = 0; i < glyphCount; ++i) { |
| 62 | // Each charater maps to exactly one glyph via SkGlyphCache::unicharToGlyph(). |
| 63 | runBuffer.clusters[i] = SkToU32(txtPtr - utf8text); |
| 64 | txtPtr += utf8_lead_byte_to_count(txtPtr); |
| 65 | SkASSERT(txtPtr <= utf8text + textBytes); |
| 66 | } |
halcanary | 13cba49 | 2016-08-03 10:43:55 -0700 | [diff] [blame] | 67 | paint.setTextEncoding(SkPaint::kUTF8_TextEncoding); |
| 68 | (void)paint.textToGlyphs(utf8text, textBytes, runBuffer.glyphs); |
| 69 | (void)paint.getTextWidths(utf8text, textBytes, runBuffer.pos); |
| 70 | SkScalar x = point.x(); |
| 71 | for (int i = 0; i < glyphCount; ++i) { |
| 72 | SkScalar w = runBuffer.pos[i]; |
| 73 | runBuffer.pos[i] = x; |
| 74 | x += w; |
| 75 | } |
Ben Wagner | 5d4dd8b | 2018-01-25 14:37:17 -0500 | [diff] [blame] | 76 | point.fY += metrics.fDescent + metrics.fLeading; |
| 77 | |
| 78 | return point; |
halcanary | 13cba49 | 2016-08-03 10:43:55 -0700 | [diff] [blame] | 79 | } |