blob: 4b371c00ec8bece75175847a190ee90c7766ce18 [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"
Mike Reed9a4a05e2019-01-21 20:42:04 -05009#include "SkFontMetrics.h"
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 Malita950243d2019-01-11 11:08:35 -050034SkPoint SkShaper::shape(RunHandler* 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 Malita950243d2019-01-11 11:08:35 -050055 const RunHandler::RunInfo info = {
Florin Malita950243d2019-01-11 11:08:35 -050056 { font.measureText(utf8text, textBytes, SkTextEncoding::kUTF8), 0 },
57 metrics.fAscent,
58 metrics.fDescent,
59 metrics.fLeading,
60 };
Ben Wagner454e5fb2019-02-08 17:46:38 -050061 const auto buffer = handler->newRunBuffer(info, font, glyphCount,
62 SkSpan<const char>(utf8text, textBytes));
Florin Malita9867f612018-12-12 10:54:49 -050063 SkAssertResult(font.textToGlyphs(utf8text, textBytes, SkTextEncoding::kUTF8, buffer.glyphs,
64 glyphCount) == glyphCount);
65 font.getPos(buffer.glyphs, glyphCount, buffer.positions, point);
66
Florin Malita9867f612018-12-12 10:54:49 -050067 if (buffer.clusters) {
68 const char* txtPtr = utf8text;
69 for (int i = 0; i < glyphCount; ++i) {
70 // Each charater maps to exactly one glyph via SkGlyphCache::unicharToGlyph().
71 buffer.clusters[i] = SkToU32(txtPtr - utf8text);
72 txtPtr += utf8_lead_byte_to_count(txtPtr);
73 SkASSERT(txtPtr <= utf8text + textBytes);
74 }
75 }
Ben Wagner454e5fb2019-02-08 17:46:38 -050076 handler->commitRun();
Florin Malita500133b2019-02-07 10:56:55 -050077 handler->commitLine();
78
Florin Malita9867f612018-12-12 10:54:49 -050079 return point + SkVector::Make(0, metrics.fDescent + metrics.fLeading);
halcanary13cba492016-08-03 10:43:55 -070080}