Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(314)

Side by Side Diff: src/fonts/SkTestScalerContext.cpp

Issue 407183003: add portable and canonical font support for DM (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: add atexit; rename to create_... Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/fonts/SkTestScalerContext.h ('k') | tools/create_test_font.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2014 Google Inc. 2 * Copyright 2014 Google Inc.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 #include "SkBitmap.h" 8 #include "SkBitmap.h"
9 #include "SkCanvas.h" 9 #include "SkCanvas.h"
10 #include "SkDescriptor.h" 10 #include "SkDescriptor.h"
11 #include "SkFontDescriptor.h"
11 #include "SkGlyph.h" 12 #include "SkGlyph.h"
12 #include "SkMask.h" 13 #include "SkMask.h"
13 // #include "SkOTUtils.h" 14 // #include "SkOTUtils.h"
14 #include "SkScalerContext.h" 15 #include "SkScalerContext.h"
15 #include "SkTestScalerContext.h" 16 #include "SkTestScalerContext.h"
16 #include "SkTypefaceCache.h" 17 #include "SkTypefaceCache.h"
17 18
18 class SkTestTypeface : public SkTypeface { 19 SkTestFont::SkTestFont(const SkTestFontData& fontData)
19 public: 20 : INHERITED()
20 SkTestTypeface(SkPaint::FontMetrics (*funct)(SkTDArray<SkPath*>& , SkTDArray <SkFixed>& ), 21 , fCharCodes(fontData.fCharCodes)
21 SkTypeface::Style style) 22 , fCharCodesCount(fontData.fCharCodesCount)
22 : SkTypeface(style, SkTypefaceCache::NewFontID(), false) { 23 , fWidths(fontData.fWidths)
23 fMetrics = (*funct)(fPaths, fWidths); 24 , fMetrics(fontData.fMetrics)
25 , fName(fontData.fName)
26 , fPaths(NULL)
27 {
28 init(fontData.fPoints, fontData.fVerbs);
29 #ifdef SK_DEBUG
30 sk_bzero(fDebugBits, sizeof(fDebugBits));
31 sk_bzero(fDebugOverage, sizeof(fDebugOverage));
32 #endif
33 }
34
35 SkTestFont::~SkTestFont() {
36 for (unsigned index = 0; index < fCharCodesCount; ++index) {
37 SkDELETE(fPaths[index]);
24 } 38 }
39 SkDELETE_ARRAY(fPaths);
40 }
25 41
26 virtual ~SkTestTypeface() { 42 #ifdef SK_DEBUG
27 fPaths.deleteAll();
28 }
29 43
30 void getAdvance(SkGlyph* glyph) { 44 #include "SkThread.h"
31 glyph->fAdvanceX = fWidths[SkGlyph::ID2Code(glyph->fID)]; 45 SK_DECLARE_STATIC_MUTEX(gUsedCharsMutex);
32 glyph->fAdvanceY = 0;
33 }
34 46
35 void getFontMetrics(SkPaint::FontMetrics* metrics) { 47 #endif
36 *metrics = fMetrics;
37 }
38 48
39 void getMetrics(SkGlyph* glyph) { 49 int SkTestFont::codeToIndex(SkUnichar charCode) const {
40 glyph->fAdvanceX = fWidths[SkGlyph::ID2Code(glyph->fID)]; 50 #ifdef SK_DEBUG // detect missing test font data
41 glyph->fAdvanceY = 0; 51 {
42 } 52 SkAutoMutexAcquire ac(gUsedCharsMutex);
43 53 if (charCode >= ' ' && charCode <= '~') {
44 void getPath(const SkGlyph& glyph, SkPath* path) { 54 int bitOffset = charCode - ' ';
45 *path = *fPaths[SkGlyph::ID2Code(glyph.fID)]; 55 fDebugBits[bitOffset >> 3] |= 1 << (bitOffset & 7);
46 } 56 } else {
47 57 int index = 0;
48 protected: 58 while (fDebugOverage[index] != 0 && fDebugOverage[index] != charCode
49 virtual SkScalerContext* onCreateScalerContext(const SkDescriptor* desc) con st SK_OVERRIDE; 59 && index < (int) sizeof(fDebugOverage)) {
50 60 ++index;
51 virtual void onFilterRec(SkScalerContextRec* rec) const SK_OVERRIDE { 61 }
52 rec->setHinting(SkPaint::kNo_Hinting); 62 SkASSERT(index < (int) sizeof(fDebugOverage));
53 rec->fMaskFormat = SkMask::kA8_Format; 63 if (fDebugOverage[index] == 0) {
54 } 64 fDebugOverage[index] = charCode;
55
56 virtual SkAdvancedTypefaceMetrics* onGetAdvancedTypefaceMetrics(
57 SkAdvancedTypefaceMetrics::PerGlyphInfo ,
58 const uint32_t* glyphIDs,
59 uint32_t glyphIDsCount) const SK_OVERRIDE {
60 // pdf only
61 SkAdvancedTypefaceMetrics* info = new SkAdvancedTypefaceMetrics;
62 info->fEmSize = 0;
63 info->fLastGlyphID = SkToU16(onCountGlyphs() - 1);
64 info->fStyle = 0;
65 info->fFontName.set("SkiaTest");
66 info->fType = SkAdvancedTypefaceMetrics::kOther_Font;
67 info->fItalicAngle = 0;
68 info->fAscent = 0;
69 info->fDescent = 0;
70 info->fStemV = 0;
71 info->fCapHeight = 0;
72 info->fBBox = SkIRect::MakeEmpty();
73 return info;
74 }
75
76 virtual SkStream* onOpenStream(int* ttcIndex) const SK_OVERRIDE {
77 SkASSERT(0); // don't expect to get here
78 return NULL;
79 }
80
81 virtual void onGetFontDescriptor(SkFontDescriptor* desc, bool* isLocal) cons t SK_OVERRIDE {
82 SkASSERT(0); // don't expect to get here
83 }
84
85 virtual int onCharsToGlyphs(const void* chars, Encoding encoding,
86 uint16_t glyphs[], int glyphCount) const SK_OVER RIDE {
87 SkASSERT(encoding == kUTF8_Encoding);
88 for (int index = 0; index < glyphCount; ++index) {
89 int ch = ((unsigned char*) chars)[index];
90 SkASSERT(ch < 0x7F);
91 if (ch < 0x20) {
92 glyphs[index] = 0;
93 } else {
94 glyphs[index] = ch - 0x20;
95 } 65 }
96 } 66 }
97 return glyphCount;
98 } 67 }
68 #endif
69 for (unsigned index = 0; index < fCharCodesCount; ++index) {
70 if (fCharCodes[index] == (unsigned) charCode) {
71 return (int) index;
72 }
73 }
74 SkDEBUGF(("missing '%c' (%d) from %s %d\n", (char) charCode, charCode,
75 fDebugName, fDebugStyle));
76 return 0;
77 }
99 78
100 virtual int onCountGlyphs() const SK_OVERRIDE { 79 void SkTestFont::init(const SkScalar* pts, const unsigned char* verbs) {
101 return fPaths.count(); 80 fPaths = SkNEW_ARRAY(SkPath*, fCharCodesCount);
81 for (unsigned index = 0; index < fCharCodesCount; ++index) {
82 SkPath* path = SkNEW(SkPath);
83 SkPath::Verb verb;
84 while ((verb = (SkPath::Verb) *verbs++) != SkPath::kDone_Verb) {
85 switch (verb) {
86 case SkPath::kMove_Verb:
87 path->moveTo(pts[0], pts[1]);
88 pts += 2;
89 break;
90 case SkPath::kLine_Verb:
91 path->lineTo(pts[0], pts[1]);
92 pts += 2;
93 break;
94 case SkPath::kQuad_Verb:
95 path->quadTo(pts[0], pts[1], pts[2], pts[3]);
96 pts += 4;
97 break;
98 case SkPath::kCubic_Verb:
99 path->cubicTo(pts[0], pts[1], pts[2], pts[3], pts[4], pts[5] );
100 pts += 6;
101 break;
102 case SkPath::kClose_Verb:
103 path->close();
104 break;
105 default:
106 SkDEBUGFAIL("bad verb");
107 return;
108 }
109 }
110 fPaths[index] = path;
102 } 111 }
112 }
113
114 SkTestTypeface::SkTestTypeface(SkTestFont* testFont, SkTypeface::Style style)
115 : SkTypeface(style, SkTypefaceCache::NewFontID(), false)
116 , fTestFont(testFont) {
117 }
103 118
104 virtual int onGetUPEM() const SK_OVERRIDE { 119 void SkTestTypeface::getAdvance(SkGlyph* glyph) {
105 SkASSERT(0); // don't expect to get here 120 glyph->fAdvanceX = fTestFont->fWidths[SkGlyph::ID2Code(glyph->fID)];
106 return 1; 121 glyph->fAdvanceY = 0;
122 }
123
124 void SkTestTypeface::getFontMetrics(SkPaint::FontMetrics* metrics) {
125 *metrics = fTestFont->fMetrics;
126 }
127
128 void SkTestTypeface::getMetrics(SkGlyph* glyph) {
129 glyph->fAdvanceX = fTestFont->fWidths[SkGlyph::ID2Code(glyph->fID)];
130 glyph->fAdvanceY = 0;
131 }
132
133 void SkTestTypeface::getPath(const SkGlyph& glyph, SkPath* path) {
134 *path = *fTestFont->fPaths[SkGlyph::ID2Code(glyph.fID)];
135 }
136
137 void SkTestTypeface::onFilterRec(SkScalerContextRec* rec) const {
138 rec->setHinting(SkPaint::kNo_Hinting);
139 rec->fMaskFormat = SkMask::kA8_Format;
140 }
141
142 SkAdvancedTypefaceMetrics* SkTestTypeface::onGetAdvancedTypefaceMetrics(
143 SkAdvancedTypefaceMetrics::PerGlyphInfo ,
144 const uint32_t* glyphIDs,
145 uint32_t glyphIDsCount) const {
146 // pdf only
147 SkAdvancedTypefaceMetrics* info = new SkAdvancedTypefaceMetrics;
148 info->fEmSize = 0;
149 info->fLastGlyphID = SkToU16(onCountGlyphs() - 1);
150 info->fStyle = 0;
151 info->fFontName.set(fTestFont->fName);
152 info->fType = SkAdvancedTypefaceMetrics::kOther_Font;
153 info->fItalicAngle = 0;
154 info->fAscent = 0;
155 info->fDescent = 0;
156 info->fStemV = 0;
157 info->fCapHeight = 0;
158 info->fBBox = SkIRect::MakeEmpty();
159 return info;
160 }
161
162 void SkTestTypeface::onGetFontDescriptor(SkFontDescriptor* desc, bool* isLocal) const {
163 desc->setFamilyName(fTestFont->fName);
164 desc->setFontFileName(fTestFont->fName);
165 *isLocal = false;
166 }
167
168 int SkTestTypeface::onCharsToGlyphs(const void* chars, Encoding encoding,
169 uint16_t glyphs[], int glyphCount) const {
170 SkASSERT(encoding == kUTF16_Encoding);
171 for (int index = 0; index < glyphCount; ++index) {
172 SkUnichar ch = ((SkUnichar*) chars)[index];
173 glyphs[index] = fTestFont->codeToIndex(ch);
107 } 174 }
175 return glyphCount;
176 }
108 177
109 virtual SkTypeface::LocalizedStrings* onCreateFamilyNameIterator() const SK_ OVERRIDE { 178 SkTypeface::LocalizedStrings* SkTestTypeface::onCreateFamilyNameIterator() const {
110 SkString familyName("SkiaTest"); 179 SkString familyName(fTestFont->fName);
111 SkString language("und"); //undetermined 180 SkString language("und"); //undetermined
112 SkASSERT(0); // incomplete 181 SkASSERT(0); // incomplete
113 return NULL; 182 return NULL;
114 // return new SkOTUtils::LocalizedStrings_SingleName(familyName, language ); 183 // return new SkOTUtils::LocalizedStrings_SingleName(familyName, language);
115 }
116
117 virtual int onGetTableTags(SkFontTableTag tags[]) const SK_OVERRIDE {
118 return 0;
119 }
120
121 virtual size_t onGetTableData(SkFontTableTag tag, size_t offset,
122 size_t length, void* data) const SK_OVERRIDE {
123 return 0;
124 }
125
126 private:
127 SkTDArray<SkPath* > fPaths;
128 SkTDArray<SkFixed> fWidths;
129 SkPaint::FontMetrics fMetrics;
130 friend class SkTestScalerContext;
131 };
132
133 SkTypeface* CreateTestTypeface(SkPaint::FontMetrics (*funct)(SkTDArray<SkPath*>& pathArray,
134 SkTDArray<SkFixed>& widthArray),
135 SkTypeface::Style style) {
136 SkTypeface* test = SkNEW_ARGS(SkTestTypeface, (funct, style));
137 return test;
138 } 184 }
139 185
140 class SkTestScalerContext : public SkScalerContext { 186 class SkTestScalerContext : public SkScalerContext {
141 public: 187 public:
142 SkTestScalerContext(SkTestTypeface* face, const SkDescriptor* desc) 188 SkTestScalerContext(SkTestTypeface* face, const SkDescriptor* desc)
143 : SkScalerContext(face, desc) 189 : SkScalerContext(face, desc)
144 , fFace(face) 190 , fFace(face)
145 { 191 {
146 fRec.getSingleMatrix(&fMatrix); 192 fRec.getSingleMatrix(&fMatrix);
147 this->forceGenerateImageFromPath(); 193 this->forceGenerateImageFromPath();
148 } 194 }
149 195
150 virtual ~SkTestScalerContext() { 196 virtual ~SkTestScalerContext() {
151 } 197 }
152 198
153 protected: 199 protected:
154 virtual unsigned generateGlyphCount() SK_OVERRIDE { 200 virtual unsigned generateGlyphCount() SK_OVERRIDE {
155 return fFace->onCountGlyphs(); 201 return fFace->onCountGlyphs();
156 } 202 }
157 203
158 virtual uint16_t generateCharToGlyph(SkUnichar uni) SK_OVERRIDE { 204 virtual uint16_t generateCharToGlyph(SkUnichar uni) SK_OVERRIDE {
159 uint8_t ch = (uint8_t) uni;
160 SkASSERT(ch < 0x7f);
161 uint16_t glyph; 205 uint16_t glyph;
162 (void) fFace->onCharsToGlyphs((const void *) &ch, SkTypeface::kUTF8_Enco ding, &glyph, 1); 206 (void) fFace->onCharsToGlyphs((const void *) &uni, SkTypeface::kUTF16_En coding, &glyph, 1);
163 return glyph; 207 return glyph;
164 } 208 }
165 209
166 virtual void generateAdvance(SkGlyph* glyph) SK_OVERRIDE { 210 virtual void generateAdvance(SkGlyph* glyph) SK_OVERRIDE {
167 fFace->getAdvance(glyph); 211 fFace->getAdvance(glyph);
168 212
169 SkVector advance; 213 SkVector advance;
170 fMatrix.mapXY(SkFixedToScalar(glyph->fAdvanceX), 214 fMatrix.mapXY(SkFixedToScalar(glyph->fAdvanceX),
171 SkFixedToScalar(glyph->fAdvanceY), &advance); 215 SkFixedToScalar(glyph->fAdvanceY), &advance);
172 glyph->fAdvanceX = SkScalarToFixed(advance.fX); 216 glyph->fAdvanceX = SkScalarToFixed(advance.fX);
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
240 } 284 }
241 285
242 private: 286 private:
243 SkTestTypeface* fFace; 287 SkTestTypeface* fFace;
244 SkMatrix fMatrix; 288 SkMatrix fMatrix;
245 }; 289 };
246 290
247 SkScalerContext* SkTestTypeface::onCreateScalerContext(const SkDescriptor* desc) const { 291 SkScalerContext* SkTestTypeface::onCreateScalerContext(const SkDescriptor* desc) const {
248 return SkNEW_ARGS(SkTestScalerContext, (const_cast<SkTestTypeface*>(this), d esc)); 292 return SkNEW_ARGS(SkTestScalerContext, (const_cast<SkTestTypeface*>(this), d esc));
249 } 293 }
OLDNEW
« no previous file with comments | « src/fonts/SkTestScalerContext.h ('k') | tools/create_test_font.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698