blob: e6b42771c06c001b0b5c676061f366b52d3c4a58 [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"
11#include "SkTextBlob.h"
Hal Canaryc640d0d2018-06-13 09:59:02 -040012#include "SkTo.h"
halcanary13cba492016-08-03 10:43:55 -070013#include "SkTypeface.h"
14
15struct SkShaper::Impl {
16 sk_sp<SkTypeface> fTypeface;
17};
18
19SkShaper::SkShaper(sk_sp<SkTypeface> tf) : fImpl(new Impl) {
20 fImpl->fTypeface = tf ? std::move(tf) : SkTypeface::MakeDefault();
21}
22
23SkShaper::~SkShaper() {}
24
25bool SkShaper::good() const { return true; }
26
halcanary3eee9d92016-09-10 07:01:53 -070027// This example only uses public API, so we don't use SkUTF8_NextUnichar.
28unsigned 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 Wagner5d4dd8b2018-01-25 14:37:17 -050035SkPoint 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 Wagnera25fbef2017-08-30 13:56:19 -040042 sk_ignore_unused_variable(leftToRight);
43
halcanary13cba492016-08-03 10:43:55 -070044 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 Wagner5d4dd8b2018-01-25 14:37:17 -050049 return point;
halcanary13cba492016-08-03 10:43:55 -070050 }
51 SkRect bounds;
Ben Wagner5d4dd8b2018-01-25 14:37:17 -050052 SkPaint::FontMetrics metrics;
53 paint.getFontMetrics(&metrics);
54 point.fY -= metrics.fAscent;
halcanary13cba492016-08-03 10:43:55 -070055 (void)paint.measureText(utf8text, textBytes, &bounds);
56 paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
halcanary3eee9d92016-09-10 07:01:53 -070057 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 }
halcanary13cba492016-08-03 10:43:55 -070067 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 Wagner5d4dd8b2018-01-25 14:37:17 -050076 point.fY += metrics.fDescent + metrics.fLeading;
77
78 return point;
halcanary13cba492016-08-03 10:43:55 -070079}