1 /*
2 * Copyright 2014 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 "include/core/SkBitmap.h"
9 #include "include/core/SkCanvas.h"
10 #include "include/core/SkFontMetrics.h"
11 #include "include/core/SkImageInfo.h"
12 #include "include/core/SkMatrix.h"
13 #include "include/core/SkPath.h"
14 #include "include/core/SkPoint.h"
15 #include "include/core/SkRect.h"
16 #include "include/core/SkString.h"
17 #include "include/private/SkTDArray.h"
18 #include "include/private/SkTo.h"
19 #include "src/core/SkAdvancedTypefaceMetrics.h"
20 #include "src/core/SkFontDescriptor.h"
21 #include "src/core/SkFontPriv.h"
22 #include "src/core/SkGlyph.h"
23 #include "src/core/SkPaintPriv.h"
24 #include "src/core/SkScalerContext.h"
25 #include "src/core/SkUtils.h"
26 #include "src/sfnt/SkOTUtils.h"
27 #include "tools/fonts/TestTypeface.h"
28
29 #include <utility>
30
31 class SkDescriptor;
32
SkTestFont(const SkTestFontData & fontData)33 SkTestFont::SkTestFont(const SkTestFontData& fontData)
34 : INHERITED()
35 , fCharCodes(fontData.fCharCodes)
36 , fCharCodesCount(fontData.fCharCodes ? fontData.fCharCodesCount : 0)
37 , fWidths(fontData.fWidths)
38 , fMetrics(fontData.fMetrics)
39 , fName(fontData.fName)
40 , fPaths(nullptr) {
41 init(fontData.fPoints, fontData.fVerbs);
42 }
43
~SkTestFont()44 SkTestFont::~SkTestFont() {
45 for (unsigned index = 0; index < fCharCodesCount; ++index) {
46 delete fPaths[index];
47 }
48 delete[] fPaths;
49 }
50
glyphForUnichar(SkUnichar charCode) const51 SkGlyphID SkTestFont::glyphForUnichar(SkUnichar charCode) const {
52 for (size_t index = 0; index < fCharCodesCount; ++index) {
53 if (fCharCodes[index] == charCode) {
54 return SkTo<SkGlyphID>(index);
55 }
56 }
57 return 0;
58 }
59
init(const SkScalar * pts,const unsigned char * verbs)60 void SkTestFont::init(const SkScalar* pts, const unsigned char* verbs) {
61 fPaths = new SkPath*[fCharCodesCount];
62 for (unsigned index = 0; index < fCharCodesCount; ++index) {
63 SkPath* path = new SkPath;
64 SkPath::Verb verb;
65 while ((verb = (SkPath::Verb)*verbs++) != SkPath::kDone_Verb) {
66 switch (verb) {
67 case SkPath::kMove_Verb:
68 path->moveTo(pts[0], pts[1]);
69 pts += 2;
70 break;
71 case SkPath::kLine_Verb:
72 path->lineTo(pts[0], pts[1]);
73 pts += 2;
74 break;
75 case SkPath::kQuad_Verb:
76 path->quadTo(pts[0], pts[1], pts[2], pts[3]);
77 pts += 4;
78 break;
79 case SkPath::kCubic_Verb:
80 path->cubicTo(pts[0], pts[1], pts[2], pts[3], pts[4], pts[5]);
81 pts += 6;
82 break;
83 case SkPath::kClose_Verb: path->close(); break;
84 default: SkDEBUGFAIL("bad verb"); return;
85 }
86 }
87 // This should make SkPath::getBounds() queries threadsafe.
88 path->updateBoundsCache();
89 fPaths[index] = path;
90 }
91 }
92
TestTypeface(sk_sp<SkTestFont> testFont,const SkFontStyle & style)93 TestTypeface::TestTypeface(sk_sp<SkTestFont> testFont, const SkFontStyle& style)
94 : SkTypeface(style, false), fTestFont(std::move(testFont)) {}
95
getAdvance(SkGlyph * glyph)96 void TestTypeface::getAdvance(SkGlyph* glyph) {
97 SkGlyphID glyphID = glyph->getGlyphID();
98 glyphID = glyphID < fTestFont->fCharCodesCount ? glyphID : 0;
99
100 // TODO(benjaminwagner): Update users to use floats.
101 glyph->fAdvanceX = SkFixedToFloat(fTestFont->fWidths[glyphID]);
102 glyph->fAdvanceY = 0;
103 }
104
getFontMetrics(SkFontMetrics * metrics)105 void TestTypeface::getFontMetrics(SkFontMetrics* metrics) { *metrics = fTestFont->fMetrics; }
106
getPath(SkGlyphID glyphID,SkPath * path)107 void TestTypeface::getPath(SkGlyphID glyphID, SkPath* path) {
108 glyphID = glyphID < fTestFont->fCharCodesCount ? glyphID : 0;
109 *path = *fTestFont->fPaths[glyphID];
110 }
111
onFilterRec(SkScalerContextRec * rec) const112 void TestTypeface::onFilterRec(SkScalerContextRec* rec) const {
113 rec->setHinting(SkFontHinting::kNone);
114 }
115
getGlyphToUnicodeMap(SkUnichar * glyphToUnicode) const116 void TestTypeface::getGlyphToUnicodeMap(SkUnichar* glyphToUnicode) const {
117 unsigned glyphCount = fTestFont->fCharCodesCount;
118 for (unsigned gid = 0; gid < glyphCount; ++gid) {
119 glyphToUnicode[gid] = SkTo<SkUnichar>(fTestFont->fCharCodes[gid]);
120 }
121 }
122
onGetAdvancedMetrics() const123 std::unique_ptr<SkAdvancedTypefaceMetrics> TestTypeface::onGetAdvancedMetrics() const { // pdf only
124 std::unique_ptr<SkAdvancedTypefaceMetrics>info(new SkAdvancedTypefaceMetrics);
125 info->fFontName.set(fTestFont->fName);
126 return info;
127 }
128
onGetFontDescriptor(SkFontDescriptor * desc,bool * isLocal) const129 void TestTypeface::onGetFontDescriptor(SkFontDescriptor* desc, bool* isLocal) const {
130 desc->setFamilyName(fTestFont->fName);
131 desc->setStyle(this->fontStyle());
132 *isLocal = false;
133 }
134
onCharsToGlyphs(const SkUnichar uni[],int count,SkGlyphID glyphs[]) const135 void TestTypeface::onCharsToGlyphs(const SkUnichar uni[], int count, SkGlyphID glyphs[]) const {
136 for (int i = 0; i < count; ++i) {
137 glyphs[i] = fTestFont->glyphForUnichar(uni[i]);
138 }
139 }
140
onGetFamilyName(SkString * familyName) const141 void TestTypeface::onGetFamilyName(SkString* familyName) const { *familyName = fTestFont->fName; }
142
onCreateFamilyNameIterator() const143 SkTypeface::LocalizedStrings* TestTypeface::onCreateFamilyNameIterator() const {
144 SkString familyName(fTestFont->fName);
145 SkString language("und"); // undetermined
146 return new SkOTUtils::LocalizedStrings_SingleName(familyName, language);
147 }
148
149 class SkTestScalerContext : public SkScalerContext {
150 public:
SkTestScalerContext(sk_sp<TestTypeface> face,const SkScalerContextEffects & effects,const SkDescriptor * desc)151 SkTestScalerContext(sk_sp<TestTypeface> face,
152 const SkScalerContextEffects& effects,
153 const SkDescriptor* desc)
154 : SkScalerContext(std::move(face), effects, desc) {
155 fRec.getSingleMatrix(&fMatrix);
156 this->forceGenerateImageFromPath();
157 }
158
159 protected:
getTestTypeface() const160 TestTypeface* getTestTypeface() const {
161 return static_cast<TestTypeface*>(this->getTypeface());
162 }
163
generateGlyphCount()164 unsigned generateGlyphCount() override { return this->getTestTypeface()->onCountGlyphs(); }
165
generateAdvance(SkGlyph * glyph)166 bool generateAdvance(SkGlyph* glyph) override {
167 this->getTestTypeface()->getAdvance(glyph);
168
169 const SkVector advance =
170 fMatrix.mapXY(SkFloatToScalar(glyph->fAdvanceX), SkFloatToScalar(glyph->fAdvanceY));
171 glyph->fAdvanceX = SkScalarToFloat(advance.fX);
172 glyph->fAdvanceY = SkScalarToFloat(advance.fY);
173 return true;
174 }
175
generateMetrics(SkGlyph * glyph)176 void generateMetrics(SkGlyph* glyph) override {
177 glyph->zeroMetrics();
178 this->generateAdvance(glyph);
179 // Always generates from paths, so SkScalerContext::getMetrics will figure the bounds.
180 }
181
generateImage(const SkGlyph &)182 void generateImage(const SkGlyph&) override { SK_ABORT("Should have generated from path."); }
183
generatePath(SkGlyphID glyph,SkPath * path)184 bool generatePath(SkGlyphID glyph, SkPath* path) override {
185 this->getTestTypeface()->getPath(glyph, path);
186 path->transform(fMatrix);
187 return true;
188 }
189
generateFontMetrics(SkFontMetrics * metrics)190 void generateFontMetrics(SkFontMetrics* metrics) override {
191 this->getTestTypeface()->getFontMetrics(metrics);
192 SkFontPriv::ScaleFontMetrics(metrics, fMatrix.getScaleY());
193 }
194
195 private:
196 SkMatrix fMatrix;
197 };
198
onCreateScalerContext(const SkScalerContextEffects & effects,const SkDescriptor * desc) const199 SkScalerContext* TestTypeface::onCreateScalerContext(const SkScalerContextEffects& effects,
200 const SkDescriptor* desc) const {
201 return new SkTestScalerContext(sk_ref_sp(const_cast<TestTypeface*>(this)), effects, desc);
202 }
203