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" |
Hal Canary | c640d0d | 2018-06-13 09:59:02 -0400 | [diff] [blame] | 11 | #include "SkTo.h" |
halcanary | 13cba49 | 2016-08-03 10:43:55 -0700 | [diff] [blame] | 12 | #include "SkTypeface.h" |
| 13 | |
| 14 | struct SkShaper::Impl { |
| 15 | sk_sp<SkTypeface> fTypeface; |
| 16 | }; |
| 17 | |
| 18 | SkShaper::SkShaper(sk_sp<SkTypeface> tf) : fImpl(new Impl) { |
| 19 | fImpl->fTypeface = tf ? std::move(tf) : SkTypeface::MakeDefault(); |
| 20 | } |
| 21 | |
| 22 | SkShaper::~SkShaper() {} |
| 23 | |
| 24 | bool SkShaper::good() const { return true; } |
| 25 | |
halcanary | 3eee9d9 | 2016-09-10 07:01:53 -0700 | [diff] [blame] | 26 | // This example only uses public API, so we don't use SkUTF8_NextUnichar. |
| 27 | unsigned utf8_lead_byte_to_count(const char* ptr) { |
| 28 | uint8_t c = *(const uint8_t*)ptr; |
| 29 | SkASSERT(c <= 0xF7); |
| 30 | SkASSERT((c & 0xC0) != 0x80); |
| 31 | return (((0xE5 << 24) >> ((unsigned)c >> 4 << 1)) & 3) + 1; |
| 32 | } |
| 33 | |
Florin Malita | 9867f61 | 2018-12-12 10:54:49 -0500 | [diff] [blame^] | 34 | SkPoint SkShaper::shape(LineHandler* handler, |
Mike Reed | 6d59568 | 2018-12-05 17:28:14 -0500 | [diff] [blame] | 35 | const SkFont& srcFont, |
Ben Wagner | 5d4dd8b | 2018-01-25 14:37:17 -0500 | [diff] [blame] | 36 | const char* utf8text, |
| 37 | size_t textBytes, |
| 38 | bool leftToRight, |
| 39 | SkPoint point, |
| 40 | SkScalar width) const { |
Ben Wagner | a25fbef | 2017-08-30 13:56:19 -0400 | [diff] [blame] | 41 | sk_ignore_unused_variable(leftToRight); |
Florin Malita | 9867f61 | 2018-12-12 10:54:49 -0500 | [diff] [blame^] | 42 | sk_ignore_unused_variable(width); |
Ben Wagner | a25fbef | 2017-08-30 13:56:19 -0400 | [diff] [blame] | 43 | |
Mike Reed | 6d59568 | 2018-12-05 17:28:14 -0500 | [diff] [blame] | 44 | SkFont font(srcFont); |
| 45 | font.setTypeface(fImpl->fTypeface); |
| 46 | int glyphCount = font.countText(utf8text, textBytes, SkTextEncoding::kUTF8); |
halcanary | 13cba49 | 2016-08-03 10:43:55 -0700 | [diff] [blame] | 47 | if (glyphCount <= 0) { |
Ben Wagner | 5d4dd8b | 2018-01-25 14:37:17 -0500 | [diff] [blame] | 48 | return point; |
halcanary | 13cba49 | 2016-08-03 10:43:55 -0700 | [diff] [blame] | 49 | } |
Florin Malita | 9867f61 | 2018-12-12 10:54:49 -0500 | [diff] [blame^] | 50 | |
Mike Reed | b5784ac | 2018-11-12 09:35:15 -0500 | [diff] [blame] | 51 | SkFontMetrics metrics; |
Mike Reed | 6d59568 | 2018-12-05 17:28:14 -0500 | [diff] [blame] | 52 | font.getMetrics(&metrics); |
Ben Wagner | 5d4dd8b | 2018-01-25 14:37:17 -0500 | [diff] [blame] | 53 | point.fY -= metrics.fAscent; |
Ben Wagner | 5d4dd8b | 2018-01-25 14:37:17 -0500 | [diff] [blame] | 54 | |
Florin Malita | 9867f61 | 2018-12-12 10:54:49 -0500 | [diff] [blame^] | 55 | const auto buffer = handler->newLineBuffer(font, glyphCount, textBytes); |
| 56 | SkAssertResult(font.textToGlyphs(utf8text, textBytes, SkTextEncoding::kUTF8, buffer.glyphs, |
| 57 | glyphCount) == glyphCount); |
| 58 | font.getPos(buffer.glyphs, glyphCount, buffer.positions, point); |
| 59 | |
| 60 | if (buffer.utf8text) { |
| 61 | memcpy(buffer.utf8text, utf8text, textBytes); |
| 62 | } |
| 63 | |
| 64 | if (buffer.clusters) { |
| 65 | const char* txtPtr = utf8text; |
| 66 | for (int i = 0; i < glyphCount; ++i) { |
| 67 | // Each charater maps to exactly one glyph via SkGlyphCache::unicharToGlyph(). |
| 68 | buffer.clusters[i] = SkToU32(txtPtr - utf8text); |
| 69 | txtPtr += utf8_lead_byte_to_count(txtPtr); |
| 70 | SkASSERT(txtPtr <= utf8text + textBytes); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | return point + SkVector::Make(0, metrics.fDescent + metrics.fLeading); |
halcanary | 13cba49 | 2016-08-03 10:43:55 -0700 | [diff] [blame] | 75 | } |