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  */
7 
8 #include "SkShaper.h"
9 #include "SkFontMetrics.h"
10 #include "SkStream.h"
11 #include "SkTo.h"
12 #include "SkTypeface.h"
13 
14 struct SkShaper::Impl {
15     sk_sp<SkTypeface> fTypeface;
16 };
17 
SkShaper(sk_sp<SkTypeface> tf)18 SkShaper::SkShaper(sk_sp<SkTypeface> tf) : fImpl(new Impl) {
19     fImpl->fTypeface = tf ? std::move(tf) : SkTypeface::MakeDefault();
20 }
21 
~SkShaper()22 SkShaper::~SkShaper() {}
23 
good() const24 bool SkShaper::good() const { return true; }
25 
26 // This example only uses public API, so we don't use SkUTF8_NextUnichar.
utf8_lead_byte_to_count(const char * ptr)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 
shape(RunHandler * handler,const SkFont & srcFont,const char * utf8text,size_t textBytes,bool leftToRight,SkPoint point,SkScalar width) const34 SkPoint SkShaper::shape(RunHandler* handler,
35                         const SkFont& srcFont,
36                         const char* utf8text,
37                         size_t textBytes,
38                         bool leftToRight,
39                         SkPoint point,
40                         SkScalar width) const {
41     sk_ignore_unused_variable(leftToRight);
42     sk_ignore_unused_variable(width);
43 
44     SkFont font(srcFont);
45     font.setTypeface(fImpl->fTypeface);
46     int glyphCount = font.countText(utf8text, textBytes, SkTextEncoding::kUTF8);
47     if (glyphCount <= 0) {
48         return point;
49     }
50 
51     SkFontMetrics metrics;
52     font.getMetrics(&metrics);
53     point.fY -= metrics.fAscent;
54 
55     const RunHandler::RunInfo info = {
56         0,
57         { font.measureText(utf8text, textBytes, SkTextEncoding::kUTF8), 0 },
58         metrics.fAscent,
59         metrics.fDescent,
60         metrics.fLeading,
61     };
62     const auto buffer = handler->newRunBuffer(info, font, glyphCount, textBytes);
63     SkAssertResult(font.textToGlyphs(utf8text, textBytes, SkTextEncoding::kUTF8, buffer.glyphs,
64                                      glyphCount) == glyphCount);
65     font.getPos(buffer.glyphs, glyphCount, buffer.positions, point);
66 
67     if (buffer.utf8text) {
68         memcpy(buffer.utf8text, utf8text, textBytes);
69     }
70 
71     if (buffer.clusters) {
72         const char* txtPtr = utf8text;
73         for (int i = 0; i < glyphCount; ++i) {
74             // Each charater maps to exactly one glyph via SkGlyphCache::unicharToGlyph().
75             buffer.clusters[i] = SkToU32(txtPtr - utf8text);
76             txtPtr += utf8_lead_byte_to_count(txtPtr);
77             SkASSERT(txtPtr <= utf8text + textBytes);
78         }
79     }
80 
81     return point + SkVector::Make(0, metrics.fDescent + metrics.fLeading);
82 }
83