blob: 3d542621a8780d3dcf41b4f08e3fa1a70a1a3500 [file] [log] [blame]
halcanary13cba492016-08-03 10:43:55 -07001/*
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 Canaryc640d0d2018-06-13 09:59:02 -04007
halcanary13cba492016-08-03 10:43:55 -07008#include "SkShaper.h"
Hal Canaryc640d0d2018-06-13 09:59:02 -04009
halcanary13cba492016-08-03 10:43:55 -070010#include "SkStream.h"
Hal Canaryc640d0d2018-06-13 09:59:02 -040011#include "SkTo.h"
halcanary13cba492016-08-03 10:43:55 -070012#include "SkTypeface.h"
13
14struct SkShaper::Impl {
15 sk_sp<SkTypeface> fTypeface;
16};
17
18SkShaper::SkShaper(sk_sp<SkTypeface> tf) : fImpl(new Impl) {
19 fImpl->fTypeface = tf ? std::move(tf) : SkTypeface::MakeDefault();
20}
21
22SkShaper::~SkShaper() {}
23
24bool SkShaper::good() const { return true; }
25
halcanary3eee9d92016-09-10 07:01:53 -070026// This example only uses public API, so we don't use SkUTF8_NextUnichar.
27unsigned 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 Malita9867f612018-12-12 10:54:49 -050034SkPoint SkShaper::shape(LineHandler* handler,
Mike Reed6d595682018-12-05 17:28:14 -050035 const SkFont& srcFont,
Ben Wagner5d4dd8b2018-01-25 14:37:17 -050036 const char* utf8text,
37 size_t textBytes,
38 bool leftToRight,
39 SkPoint point,
40 SkScalar width) const {
Ben Wagnera25fbef2017-08-30 13:56:19 -040041 sk_ignore_unused_variable(leftToRight);
Florin Malita9867f612018-12-12 10:54:49 -050042 sk_ignore_unused_variable(width);
Ben Wagnera25fbef2017-08-30 13:56:19 -040043
Mike Reed6d595682018-12-05 17:28:14 -050044 SkFont font(srcFont);
45 font.setTypeface(fImpl->fTypeface);
46 int glyphCount = font.countText(utf8text, textBytes, SkTextEncoding::kUTF8);
halcanary13cba492016-08-03 10:43:55 -070047 if (glyphCount <= 0) {
Ben Wagner5d4dd8b2018-01-25 14:37:17 -050048 return point;
halcanary13cba492016-08-03 10:43:55 -070049 }
Florin Malita9867f612018-12-12 10:54:49 -050050
Mike Reedb5784ac2018-11-12 09:35:15 -050051 SkFontMetrics metrics;
Mike Reed6d595682018-12-05 17:28:14 -050052 font.getMetrics(&metrics);
Ben Wagner5d4dd8b2018-01-25 14:37:17 -050053 point.fY -= metrics.fAscent;
Ben Wagner5d4dd8b2018-01-25 14:37:17 -050054
Florin Malita9867f612018-12-12 10:54:49 -050055 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);
halcanary13cba492016-08-03 10:43:55 -070075}