blob: b0746b620411ac311d4f7dc6f55de897cf94648e [file] [log] [blame]
reed@android.com8a1c16f2008-12-17 15:59:43 +00001/* libs/graphics/ports/SkFontHost_FreeType.cpp
2**
3** Copyright 2006, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9** http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
agl@chromium.org309485b2009-07-21 17:41:32 +000018#include "SkColorPriv.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000019#include "SkScalerContext.h"
20#include "SkBitmap.h"
21#include "SkCanvas.h"
22#include "SkDescriptor.h"
23#include "SkFDot6.h"
24#include "SkFontHost.h"
25#include "SkMask.h"
26#include "SkStream.h"
27#include "SkString.h"
28#include "SkThread.h"
29#include "SkTemplates.h"
30
31#include <ft2build.h>
32#include FT_FREETYPE_H
33#include FT_OUTLINE_H
34#include FT_SIZES_H
agl@chromium.orgcc3096b2009-04-22 22:09:04 +000035#include FT_TRUETYPE_TABLES_H
agl@chromium.org309485b2009-07-21 17:41:32 +000036
reed@android.comf5493692009-07-22 19:21:01 +000037#if defined(SK_SUPPORT_LCDTEXT)
agl@chromium.org309485b2009-07-21 17:41:32 +000038#include FT_LCD_FILTER_H
39#endif
40
reed@android.com8a1c16f2008-12-17 15:59:43 +000041#ifdef FT_ADVANCES_H
42#include FT_ADVANCES_H
43#endif
44
agl@chromium.orgcc3096b2009-04-22 22:09:04 +000045#if 0
46// Also include the files by name for build tools which require this.
47#include <freetype/freetype.h>
48#include <freetype/ftoutln.h>
49#include <freetype/ftsizes.h>
50#include <freetype/tttables.h>
51#include <freetype/ftadvanc.h>
agl@chromium.org309485b2009-07-21 17:41:32 +000052#include <freetype/ftlcdfil.h>
agl@chromium.orgcc3096b2009-04-22 22:09:04 +000053#endif
54
reed@android.com8a1c16f2008-12-17 15:59:43 +000055//#define ENABLE_GLYPH_SPEW // for tracing calls
56//#define DUMP_STRIKE_CREATION
57
58#ifdef SK_DEBUG
59 #define SkASSERT_CONTINUE(pred) \
60 do { \
61 if (!(pred)) \
62 SkDebugf("file %s:%d: assert failed '" #pred "'\n", __FILE__, __LINE__); \
63 } while (false)
64#else
65 #define SkASSERT_CONTINUE(pred)
66#endif
67
68//////////////////////////////////////////////////////////////////////////
69
70struct SkFaceRec;
71
72static SkMutex gFTMutex;
73static int gFTCount;
74static FT_Library gFTLibrary;
75static SkFaceRec* gFaceRecHead;
agl@chromium.orgf18d8762009-07-28 18:38:08 +000076static bool gLCDSupportValid; // true iff |gLCDSupport| has been set.
77static bool gLCDSupport; // true iff LCD is supported by the runtime.
reed@android.com8a1c16f2008-12-17 15:59:43 +000078
79/////////////////////////////////////////////////////////////////////////
80
agl@chromium.org309485b2009-07-21 17:41:32 +000081static bool
82InitFreetype() {
83 FT_Error err = FT_Init_FreeType(&gFTLibrary);
84 if (err)
85 return false;
86
reed@android.comf5493692009-07-22 19:21:01 +000087#if defined(SK_SUPPORT_LCDTEXT)
agl@chromium.org309485b2009-07-21 17:41:32 +000088 // Setup LCD filtering. This reduces colour fringes for LCD rendered
89 // glyphs.
90 err = FT_Library_SetLcdFilter(gFTLibrary, FT_LCD_FILTER_DEFAULT);
agl@chromium.orgf18d8762009-07-28 18:38:08 +000091 gLCDSupport = err == 0;
agl@chromium.org309485b2009-07-21 17:41:32 +000092#endif
reed@android.com61608aa2009-07-31 14:52:54 +000093 gLCDSupportValid = true;
agl@chromium.org309485b2009-07-21 17:41:32 +000094
95 return true;
96}
97
reed@android.com8a1c16f2008-12-17 15:59:43 +000098class SkScalerContext_FreeType : public SkScalerContext {
99public:
100 SkScalerContext_FreeType(const SkDescriptor* desc);
101 virtual ~SkScalerContext_FreeType();
agl@chromium.orgcc3096b2009-04-22 22:09:04 +0000102
reed@android.com62900b42009-02-11 15:07:19 +0000103 bool success() const {
reed@android.coma0f5d152009-06-22 17:38:10 +0000104 return fFaceRec != NULL &&
105 fFTSize != NULL &&
106 fFace != NULL;
reed@android.com62900b42009-02-11 15:07:19 +0000107 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000108
109protected:
110 virtual unsigned generateGlyphCount() const;
111 virtual uint16_t generateCharToGlyph(SkUnichar uni);
112 virtual void generateAdvance(SkGlyph* glyph);
113 virtual void generateMetrics(SkGlyph* glyph);
114 virtual void generateImage(const SkGlyph& glyph);
115 virtual void generatePath(const SkGlyph& glyph, SkPath* path);
116 virtual void generateFontMetrics(SkPaint::FontMetrics* mx,
117 SkPaint::FontMetrics* my);
reed@android.com9d3a9852010-01-08 14:07:42 +0000118 virtual SkUnichar generateGlyphToChar(uint16_t glyph);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000119
120private:
121 SkFaceRec* fFaceRec;
122 FT_Face fFace; // reference to shared face in gFaceRecHead
123 FT_Size fFTSize; // our own copy
124 SkFixed fScaleX, fScaleY;
125 FT_Matrix fMatrix22;
126 uint32_t fLoadGlyphFlags;
127
128 FT_Error setupSize();
senorblanco@chromium.org4526a842010-02-05 23:08:20 +0000129 void emboldenOutline(FT_Outline* outline);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000130};
131
132///////////////////////////////////////////////////////////////////////////
133///////////////////////////////////////////////////////////////////////////
134
135#include "SkStream.h"
136
137struct SkFaceRec {
138 SkFaceRec* fNext;
139 FT_Face fFace;
140 FT_StreamRec fFTStream;
141 SkStream* fSkStream;
142 uint32_t fRefCnt;
143 uint32_t fFontID;
144
reed@android.comb1d9d2e2009-03-04 17:37:51 +0000145 // assumes ownership of the stream, will call unref() when its done
reed@android.com8a1c16f2008-12-17 15:59:43 +0000146 SkFaceRec(SkStream* strm, uint32_t fontID);
147 ~SkFaceRec() {
reed@android.comb1d9d2e2009-03-04 17:37:51 +0000148 fSkStream->unref();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000149 }
150};
151
152extern "C" {
153 static unsigned long sk_stream_read(FT_Stream stream,
154 unsigned long offset,
155 unsigned char* buffer,
156 unsigned long count ) {
157 SkStream* str = (SkStream*)stream->descriptor.pointer;
158
159 if (count) {
160 if (!str->rewind()) {
161 return 0;
162 } else {
163 unsigned long ret;
164 if (offset) {
165 ret = str->read(NULL, offset);
166 if (ret != offset) {
167 return 0;
168 }
169 }
170 ret = str->read(buffer, count);
171 if (ret != count) {
172 return 0;
173 }
174 count = ret;
175 }
176 }
177 return count;
178 }
179
180 static void sk_stream_close( FT_Stream stream) {}
181}
182
183SkFaceRec::SkFaceRec(SkStream* strm, uint32_t fontID)
184 : fSkStream(strm), fFontID(fontID) {
185// SkDEBUGF(("SkFaceRec: opening %s (%p)\n", key.c_str(), strm));
186
reed@android.com4516f472009-06-29 16:25:36 +0000187 sk_bzero(&fFTStream, sizeof(fFTStream));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000188 fFTStream.size = fSkStream->getLength();
189 fFTStream.descriptor.pointer = fSkStream;
190 fFTStream.read = sk_stream_read;
191 fFTStream.close = sk_stream_close;
192}
193
reed@android.com62900b42009-02-11 15:07:19 +0000194// Will return 0 on failure
reed@android.com8a1c16f2008-12-17 15:59:43 +0000195static SkFaceRec* ref_ft_face(uint32_t fontID) {
196 SkFaceRec* rec = gFaceRecHead;
197 while (rec) {
198 if (rec->fFontID == fontID) {
199 SkASSERT(rec->fFace);
200 rec->fRefCnt += 1;
201 return rec;
202 }
203 rec = rec->fNext;
204 }
205
206 SkStream* strm = SkFontHost::OpenStream(fontID);
207 if (NULL == strm) {
208 SkDEBUGF(("SkFontHost::OpenStream failed opening %x\n", fontID));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000209 return 0;
210 }
211
212 // this passes ownership of strm to the rec
213 rec = SkNEW_ARGS(SkFaceRec, (strm, fontID));
214
215 FT_Open_Args args;
216 memset(&args, 0, sizeof(args));
217 const void* memoryBase = strm->getMemoryBase();
218
219 if (NULL != memoryBase) {
220//printf("mmap(%s)\n", keyString.c_str());
221 args.flags = FT_OPEN_MEMORY;
222 args.memory_base = (const FT_Byte*)memoryBase;
223 args.memory_size = strm->getLength();
224 } else {
225//printf("fopen(%s)\n", keyString.c_str());
226 args.flags = FT_OPEN_STREAM;
227 args.stream = &rec->fFTStream;
228 }
229
230 FT_Error err = FT_Open_Face(gFTLibrary, &args, 0, &rec->fFace);
231
232 if (err) { // bad filename, try the default font
233 fprintf(stderr, "ERROR: unable to open font '%x'\n", fontID);
234 SkDELETE(rec);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000235 return 0;
236 } else {
237 SkASSERT(rec->fFace);
238 //fprintf(stderr, "Opened font '%s'\n", filename.c_str());
239 rec->fNext = gFaceRecHead;
240 gFaceRecHead = rec;
241 rec->fRefCnt = 1;
242 return rec;
243 }
244}
245
246static void unref_ft_face(FT_Face face) {
247 SkFaceRec* rec = gFaceRecHead;
248 SkFaceRec* prev = NULL;
249 while (rec) {
250 SkFaceRec* next = rec->fNext;
251 if (rec->fFace == face) {
252 if (--rec->fRefCnt == 0) {
253 if (prev) {
254 prev->fNext = next;
255 } else {
256 gFaceRecHead = next;
257 }
258 FT_Done_Face(face);
259 SkDELETE(rec);
260 }
261 return;
262 }
263 prev = rec;
264 rec = next;
265 }
266 SkASSERT("shouldn't get here, face not in list");
267}
268
269///////////////////////////////////////////////////////////////////////////
270
reed@android.com36a4c2a2009-07-22 19:52:11 +0000271void SkFontHost::FilterRec(SkScalerContext::Rec* rec) {
agl@chromium.orgf18d8762009-07-28 18:38:08 +0000272 if (!gLCDSupportValid) {
273 InitFreetype();
274 FT_Done_FreeType(gFTLibrary);
275 }
276
277 if (!gLCDSupport && rec->isLCD()) {
278 // If the runtime Freetype library doesn't support LCD mode, we disable
279 // it here.
280 rec->fMaskFormat = SkMask::kA8_Format;
281 }
282
reed@android.com36a4c2a2009-07-22 19:52:11 +0000283 SkPaint::Hinting h = rec->getHinting();
284 if (SkPaint::kFull_Hinting == h && !rec->isLCD()) {
agl@chromium.orgb4234a22010-01-21 11:43:44 +0000285 // collapse full->normal hinting if we're not doing LCD
reed@android.come4d0bc02009-07-24 19:53:20 +0000286 h = SkPaint::kNormal_Hinting;
287 } else if (rec->fSubpixelPositioning && SkPaint::kNo_Hinting != h) {
288 // to do subpixel, we must have at most slight hinting
289 h = SkPaint::kSlight_Hinting;
reed@android.com36a4c2a2009-07-22 19:52:11 +0000290 }
reed@android.come4d0bc02009-07-24 19:53:20 +0000291 rec->setHinting(h);
reed@android.com36a4c2a2009-07-22 19:52:11 +0000292}
293
reed@android.com8a1c16f2008-12-17 15:59:43 +0000294SkScalerContext_FreeType::SkScalerContext_FreeType(const SkDescriptor* desc)
reed@android.com62900b42009-02-11 15:07:19 +0000295 : SkScalerContext(desc) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000296 SkAutoMutexAcquire ac(gFTMutex);
297
reed@android.com8a1c16f2008-12-17 15:59:43 +0000298 if (gFTCount == 0) {
reed@android.com659aaf92009-07-23 15:20:21 +0000299 if (!InitFreetype()) {
300 sk_throw();
301 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000302 }
303 ++gFTCount;
304
305 // load the font file
reed@android.com62900b42009-02-11 15:07:19 +0000306 fFTSize = NULL;
307 fFace = NULL;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000308 fFaceRec = ref_ft_face(fRec.fFontID);
reed@android.com62900b42009-02-11 15:07:19 +0000309 if (NULL == fFaceRec) {
310 return;
311 }
312 fFace = fFaceRec->fFace;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000313
314 // compute our factors from the record
315
316 SkMatrix m;
317
318 fRec.getSingleMatrix(&m);
319
320#ifdef DUMP_STRIKE_CREATION
321 SkString keyString;
322 SkFontHost::GetDescriptorKeyString(desc, &keyString);
323 printf("========== strike [%g %g %g] [%g %g %g %g] hints %d format %d %s\n", SkScalarToFloat(fRec.fTextSize),
324 SkScalarToFloat(fRec.fPreScaleX), SkScalarToFloat(fRec.fPreSkewX),
325 SkScalarToFloat(fRec.fPost2x2[0][0]), SkScalarToFloat(fRec.fPost2x2[0][1]),
326 SkScalarToFloat(fRec.fPost2x2[1][0]), SkScalarToFloat(fRec.fPost2x2[1][1]),
agl@chromium.org309485b2009-07-21 17:41:32 +0000327 fRec.getHinting(), fRec.fMaskFormat, keyString.c_str());
reed@android.com8a1c16f2008-12-17 15:59:43 +0000328#endif
329
330 // now compute our scale factors
331 SkScalar sx = m.getScaleX();
332 SkScalar sy = m.getScaleY();
333
334 if (m.getSkewX() || m.getSkewY() || sx < 0 || sy < 0) {
335 // sort of give up on hinting
336 sx = SkMaxScalar(SkScalarAbs(sx), SkScalarAbs(m.getSkewX()));
337 sy = SkMaxScalar(SkScalarAbs(m.getSkewY()), SkScalarAbs(sy));
338 sx = sy = SkScalarAve(sx, sy);
339
340 SkScalar inv = SkScalarInvert(sx);
341
342 // flip the skew elements to go from our Y-down system to FreeType's
343 fMatrix22.xx = SkScalarToFixed(SkScalarMul(m.getScaleX(), inv));
344 fMatrix22.xy = -SkScalarToFixed(SkScalarMul(m.getSkewX(), inv));
345 fMatrix22.yx = -SkScalarToFixed(SkScalarMul(m.getSkewY(), inv));
346 fMatrix22.yy = SkScalarToFixed(SkScalarMul(m.getScaleY(), inv));
347 } else {
348 fMatrix22.xx = fMatrix22.yy = SK_Fixed1;
349 fMatrix22.xy = fMatrix22.yx = 0;
350 }
351
352 fScaleX = SkScalarToFixed(sx);
353 fScaleY = SkScalarToFixed(sy);
354
355 // compute the flags we send to Load_Glyph
356 {
reed@android.come4d0bc02009-07-24 19:53:20 +0000357 FT_Int32 loadFlags = FT_LOAD_DEFAULT;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000358
agl@chromium.org70a303f2010-05-10 14:15:50 +0000359 if (SkMask::kBW_Format == fRec.fMaskFormat) {
360 // See http://code.google.com/p/chromium/issues/detail?id=43252#c24
361 loadFlags = FT_LOAD_TARGET_MONO;
362 if (fRec.getHinting() == SkPaint::kNo_Hinting)
363 loadFlags = FT_LOAD_NO_HINTING;
364 } else {
365 switch (fRec.getHinting()) {
366 case SkPaint::kNo_Hinting:
367 loadFlags = FT_LOAD_NO_HINTING;
368 break;
369 case SkPaint::kSlight_Hinting:
370 loadFlags = FT_LOAD_TARGET_LIGHT; // This implies FORCE_AUTOHINT
371 break;
372 case SkPaint::kNormal_Hinting:
373 loadFlags = FT_LOAD_TARGET_NORMAL;
374 break;
375 case SkPaint::kFull_Hinting:
376 loadFlags = FT_LOAD_TARGET_NORMAL;
377 if (SkMask::kHorizontalLCD_Format == fRec.fMaskFormat)
378 loadFlags = FT_LOAD_TARGET_LCD;
379 else if (SkMask::kVerticalLCD_Format == fRec.fMaskFormat)
380 loadFlags = FT_LOAD_TARGET_LCD_V;
381 break;
382 default:
383 SkDebugf("---------- UNKNOWN hinting %d\n", fRec.getHinting());
384 break;
385 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000386 }
387
agl@chromium.org99e1b902010-01-05 01:19:44 +0000388 if ((fRec.fFlags & SkScalerContext::kEmbeddedBitmapText_Flag) == 0)
agl@chromium.orge0d08992009-08-07 19:19:23 +0000389 loadFlags |= FT_LOAD_NO_BITMAP;
agl@chromium.orge0d08992009-08-07 19:19:23 +0000390
reed@android.come4d0bc02009-07-24 19:53:20 +0000391 fLoadGlyphFlags = loadFlags;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000392 }
393
394 // now create the FT_Size
395
396 {
397 FT_Error err;
398
399 err = FT_New_Size(fFace, &fFTSize);
400 if (err != 0) {
401 SkDEBUGF(("SkScalerContext_FreeType::FT_New_Size(%x): FT_Set_Char_Size(0x%x, 0x%x) returned 0x%x\n",
402 fFaceRec->fFontID, fScaleX, fScaleY, err));
403 fFace = NULL;
404 return;
405 }
406
407 err = FT_Activate_Size(fFTSize);
408 if (err != 0) {
409 SkDEBUGF(("SkScalerContext_FreeType::FT_Activate_Size(%x, 0x%x, 0x%x) returned 0x%x\n",
410 fFaceRec->fFontID, fScaleX, fScaleY, err));
411 fFTSize = NULL;
412 }
413
414 err = FT_Set_Char_Size( fFace,
415 SkFixedToFDot6(fScaleX), SkFixedToFDot6(fScaleY),
416 72, 72);
417 if (err != 0) {
418 SkDEBUGF(("SkScalerContext_FreeType::FT_Set_Char_Size(%x, 0x%x, 0x%x) returned 0x%x\n",
419 fFaceRec->fFontID, fScaleX, fScaleY, err));
420 fFace = NULL;
421 return;
422 }
423
424 FT_Set_Transform( fFace, &fMatrix22, NULL);
425 }
426}
427
428SkScalerContext_FreeType::~SkScalerContext_FreeType() {
429 if (fFTSize != NULL) {
430 FT_Done_Size(fFTSize);
431 }
432
433 SkAutoMutexAcquire ac(gFTMutex);
434
435 if (fFace != NULL) {
436 unref_ft_face(fFace);
437 }
438 if (--gFTCount == 0) {
439// SkDEBUGF(("FT_Done_FreeType\n"));
440 FT_Done_FreeType(gFTLibrary);
441 SkDEBUGCODE(gFTLibrary = NULL;)
442 }
443}
444
445/* We call this before each use of the fFace, since we may be sharing
446 this face with other context (at different sizes).
447*/
448FT_Error SkScalerContext_FreeType::setupSize() {
449 /* In the off-chance that a font has been removed, we want to error out
450 right away, so call resolve just to be sure.
451
452 TODO: perhaps we can skip this, by walking the global font cache and
453 killing all of the contexts when we know that a given fontID is going
454 away...
455 */
reed@android.comb1d9d2e2009-03-04 17:37:51 +0000456 if (!SkFontHost::ValidFontID(fRec.fFontID)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000457 return (FT_Error)-1;
458 }
459
460 FT_Error err = FT_Activate_Size(fFTSize);
461
462 if (err != 0) {
463 SkDEBUGF(("SkScalerContext_FreeType::FT_Activate_Size(%x, 0x%x, 0x%x) returned 0x%x\n",
464 fFaceRec->fFontID, fScaleX, fScaleY, err));
465 fFTSize = NULL;
466 } else {
467 // seems we need to reset this every time (not sure why, but without it
468 // I get random italics from some other fFTSize)
469 FT_Set_Transform( fFace, &fMatrix22, NULL);
470 }
471 return err;
472}
473
senorblanco@chromium.org4526a842010-02-05 23:08:20 +0000474void SkScalerContext_FreeType::emboldenOutline(FT_Outline* outline) {
475 FT_Pos strength;
476 strength = FT_MulFix(fFace->units_per_EM, fFace->size->metrics.y_scale)
477 / 24;
478 FT_Outline_Embolden(outline, strength);
479}
480
reed@android.com8a1c16f2008-12-17 15:59:43 +0000481unsigned SkScalerContext_FreeType::generateGlyphCount() const {
482 return fFace->num_glyphs;
483}
484
485uint16_t SkScalerContext_FreeType::generateCharToGlyph(SkUnichar uni) {
486 return SkToU16(FT_Get_Char_Index( fFace, uni ));
487}
488
reed@android.com9d3a9852010-01-08 14:07:42 +0000489SkUnichar SkScalerContext_FreeType::generateGlyphToChar(uint16_t glyph) {
490 // iterate through each cmap entry, looking for matching glyph indices
491 FT_UInt glyphIndex;
492 SkUnichar charCode = FT_Get_First_Char( fFace, &glyphIndex );
493
494 while (glyphIndex != 0) {
495 if (glyphIndex == glyph) {
496 return charCode;
497 }
498 charCode = FT_Get_Next_Char( fFace, charCode, &glyphIndex );
499 }
500
501 return 0;
502}
503
reed@android.com8a1c16f2008-12-17 15:59:43 +0000504static FT_Pixel_Mode compute_pixel_mode(SkMask::Format format) {
505 switch (format) {
agl@chromium.org309485b2009-07-21 17:41:32 +0000506 case SkMask::kHorizontalLCD_Format:
507 case SkMask::kVerticalLCD_Format:
508 SkASSERT(!"An LCD format should never be passed here");
509 return FT_PIXEL_MODE_GRAY;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000510 case SkMask::kBW_Format:
511 return FT_PIXEL_MODE_MONO;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000512 case SkMask::kA8_Format:
513 default:
514 return FT_PIXEL_MODE_GRAY;
515 }
516}
517
reed@android.com8a1c16f2008-12-17 15:59:43 +0000518void SkScalerContext_FreeType::generateAdvance(SkGlyph* glyph) {
519#ifdef FT_ADVANCES_H
520 /* unhinted and light hinted text have linearly scaled advances
521 * which are very cheap to compute with some font formats...
522 */
523 {
524 SkAutoMutexAcquire ac(gFTMutex);
525
526 if (this->setupSize()) {
reed@android.com62900b42009-02-11 15:07:19 +0000527 glyph->zeroMetrics();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000528 return;
529 }
530
531 FT_Error error;
532 FT_Fixed advance;
533
534 error = FT_Get_Advance( fFace, glyph->getGlyphID(fBaseGlyphCount),
535 fLoadGlyphFlags | FT_ADVANCE_FLAG_FAST_ONLY,
536 &advance );
537 if (0 == error) {
538 glyph->fRsbDelta = 0;
539 glyph->fLsbDelta = 0;
540 glyph->fAdvanceX = advance; // advance *2/3; //DEBUG
541 glyph->fAdvanceY = 0;
542 return;
543 }
544 }
545#endif /* FT_ADVANCES_H */
546 /* otherwise, we need to load/hint the glyph, which is slower */
547 this->generateMetrics(glyph);
548 return;
549}
550
551void SkScalerContext_FreeType::generateMetrics(SkGlyph* glyph) {
552 SkAutoMutexAcquire ac(gFTMutex);
553
554 glyph->fRsbDelta = 0;
555 glyph->fLsbDelta = 0;
556
557 FT_Error err;
558
559 if (this->setupSize()) {
560 goto ERROR;
561 }
562
563 err = FT_Load_Glyph( fFace, glyph->getGlyphID(fBaseGlyphCount), fLoadGlyphFlags );
564 if (err != 0) {
565 SkDEBUGF(("SkScalerContext_FreeType::generateMetrics(%x): FT_Load_Glyph(glyph:%d flags:%d) returned 0x%x\n",
566 fFaceRec->fFontID, glyph->getGlyphID(fBaseGlyphCount), fLoadGlyphFlags, err));
567 ERROR:
reed@android.com62900b42009-02-11 15:07:19 +0000568 glyph->zeroMetrics();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000569 return;
570 }
571
572 switch ( fFace->glyph->format ) {
573 case FT_GLYPH_FORMAT_OUTLINE:
574 FT_BBox bbox;
575
senorblanco@chromium.org4526a842010-02-05 23:08:20 +0000576 if (fRec.fFlags & kEmbolden_Flag) {
577 emboldenOutline(&fFace->glyph->outline);
578 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000579 FT_Outline_Get_CBox(&fFace->glyph->outline, &bbox);
580
agl@chromium.org309485b2009-07-21 17:41:32 +0000581 if (fRec.fSubpixelPositioning) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000582 int dx = glyph->getSubXFixed() >> 10;
583 int dy = glyph->getSubYFixed() >> 10;
584 // negate dy since freetype-y-goes-up and skia-y-goes-down
585 bbox.xMin += dx;
586 bbox.yMin -= dy;
587 bbox.xMax += dx;
588 bbox.yMax -= dy;
589 }
590
591 bbox.xMin &= ~63;
592 bbox.yMin &= ~63;
593 bbox.xMax = (bbox.xMax + 63) & ~63;
594 bbox.yMax = (bbox.yMax + 63) & ~63;
595
596 glyph->fWidth = SkToU16((bbox.xMax - bbox.xMin) >> 6);
597 glyph->fHeight = SkToU16((bbox.yMax - bbox.yMin) >> 6);
598 glyph->fTop = -SkToS16(bbox.yMax >> 6);
599 glyph->fLeft = SkToS16(bbox.xMin >> 6);
600 break;
601
602 case FT_GLYPH_FORMAT_BITMAP:
603 glyph->fWidth = SkToU16(fFace->glyph->bitmap.width);
604 glyph->fHeight = SkToU16(fFace->glyph->bitmap.rows);
605 glyph->fTop = -SkToS16(fFace->glyph->bitmap_top);
606 glyph->fLeft = SkToS16(fFace->glyph->bitmap_left);
607 break;
608
609 default:
610 SkASSERT(!"unknown glyph format");
611 goto ERROR;
612 }
613
agl@chromium.org309485b2009-07-21 17:41:32 +0000614 if (!fRec.fSubpixelPositioning) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000615 glyph->fAdvanceX = SkFDot6ToFixed(fFace->glyph->advance.x);
616 glyph->fAdvanceY = -SkFDot6ToFixed(fFace->glyph->advance.y);
617 if (fRec.fFlags & kDevKernText_Flag) {
618 glyph->fRsbDelta = SkToS8(fFace->glyph->rsb_delta);
619 glyph->fLsbDelta = SkToS8(fFace->glyph->lsb_delta);
620 }
621 } else {
622 glyph->fAdvanceX = SkFixedMul(fMatrix22.xx, fFace->glyph->linearHoriAdvance);
623 glyph->fAdvanceY = -SkFixedMul(fMatrix22.yx, fFace->glyph->linearHoriAdvance);
624 }
625
626#ifdef ENABLE_GLYPH_SPEW
627 SkDEBUGF(("FT_Set_Char_Size(this:%p sx:%x sy:%x ", this, fScaleX, fScaleY));
628 SkDEBUGF(("Metrics(glyph:%d flags:0x%x) w:%d\n", glyph->getGlyphID(fBaseGlyphCount), fLoadGlyphFlags, glyph->fWidth));
629#endif
630}
631
reed@android.comf5493692009-07-22 19:21:01 +0000632#if defined(SK_SUPPORT_LCDTEXT)
agl@chromium.org309485b2009-07-21 17:41:32 +0000633namespace skia_freetype_support {
634// extern functions from SkFontHost_FreeType_Subpixel
635extern void CopyFreetypeBitmapToLCDMask(const SkGlyph& dest, const FT_Bitmap& source);
636extern void CopyFreetypeBitmapToVerticalLCDMask(const SkGlyph& dest, const FT_Bitmap& source);
637}
638
639using namespace skia_freetype_support;
640#endif
641
reed@android.com8a1c16f2008-12-17 15:59:43 +0000642void SkScalerContext_FreeType::generateImage(const SkGlyph& glyph) {
643 SkAutoMutexAcquire ac(gFTMutex);
644
645 FT_Error err;
646
647 if (this->setupSize()) {
648 goto ERROR;
649 }
650
651 err = FT_Load_Glyph( fFace, glyph.getGlyphID(fBaseGlyphCount), fLoadGlyphFlags);
652 if (err != 0) {
653 SkDEBUGF(("SkScalerContext_FreeType::generateImage: FT_Load_Glyph(glyph:%d width:%d height:%d rb:%d flags:%d) returned 0x%x\n",
654 glyph.getGlyphID(fBaseGlyphCount), glyph.fWidth, glyph.fHeight, glyph.rowBytes(), fLoadGlyphFlags, err));
655 ERROR:
656 memset(glyph.fImage, 0, glyph.rowBytes() * glyph.fHeight);
657 return;
658 }
659
agl@chromium.org309485b2009-07-21 17:41:32 +0000660 const bool lcdRenderMode = fRec.fMaskFormat == SkMask::kHorizontalLCD_Format ||
661 fRec.fMaskFormat == SkMask::kVerticalLCD_Format;
662
reed@android.com8a1c16f2008-12-17 15:59:43 +0000663 switch ( fFace->glyph->format ) {
664 case FT_GLYPH_FORMAT_OUTLINE: {
665 FT_Outline* outline = &fFace->glyph->outline;
666 FT_BBox bbox;
667 FT_Bitmap target;
668
senorblanco@chromium.org4526a842010-02-05 23:08:20 +0000669 if (fRec.fFlags & kEmbolden_Flag) {
670 emboldenOutline(outline);
671 }
672
reed@android.com8a1c16f2008-12-17 15:59:43 +0000673 int dx = 0, dy = 0;
agl@chromium.org309485b2009-07-21 17:41:32 +0000674 if (fRec.fSubpixelPositioning) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000675 dx = glyph.getSubXFixed() >> 10;
676 dy = glyph.getSubYFixed() >> 10;
677 // negate dy since freetype-y-goes-up and skia-y-goes-down
678 dy = -dy;
679 }
680 FT_Outline_Get_CBox(outline, &bbox);
681 /*
682 what we really want to do for subpixel is
683 offset(dx, dy)
684 compute_bounds
685 offset(bbox & !63)
686 but that is two calls to offset, so we do the following, which
687 achieves the same thing with only one offset call.
688 */
689 FT_Outline_Translate(outline, dx - ((bbox.xMin + dx) & ~63),
690 dy - ((bbox.yMin + dy) & ~63));
691
reed@android.comf5493692009-07-22 19:21:01 +0000692#if defined(SK_SUPPORT_LCDTEXT)
agl@chromium.org309485b2009-07-21 17:41:32 +0000693 if (lcdRenderMode) {
694 // FT_Outline_Get_Bitmap cannot render LCD glyphs. In this case
695 // we have to call FT_Render_Glyph and memcpy the image out.
696 const bool isVertical = fRec.fMaskFormat == SkMask::kVerticalLCD_Format;
697 FT_Render_Mode mode = isVertical ? FT_RENDER_MODE_LCD_V : FT_RENDER_MODE_LCD;
698 FT_Render_Glyph(fFace->glyph, mode);
699
700 if (isVertical)
701 CopyFreetypeBitmapToVerticalLCDMask(glyph, fFace->glyph->bitmap);
702 else
703 CopyFreetypeBitmapToLCDMask(glyph, fFace->glyph->bitmap);
704
705 break;
706 }
707#endif
708
reed@android.com8a1c16f2008-12-17 15:59:43 +0000709 target.width = glyph.fWidth;
710 target.rows = glyph.fHeight;
711 target.pitch = glyph.rowBytes();
712 target.buffer = reinterpret_cast<uint8_t*>(glyph.fImage);
713 target.pixel_mode = compute_pixel_mode(
714 (SkMask::Format)fRec.fMaskFormat);
715 target.num_grays = 256;
716
717 memset(glyph.fImage, 0, glyph.rowBytes() * glyph.fHeight);
718 FT_Outline_Get_Bitmap(gFTLibrary, outline, &target);
719 } break;
720
721 case FT_GLYPH_FORMAT_BITMAP: {
722 SkASSERT_CONTINUE(glyph.fWidth == fFace->glyph->bitmap.width);
723 SkASSERT_CONTINUE(glyph.fHeight == fFace->glyph->bitmap.rows);
724 SkASSERT_CONTINUE(glyph.fTop == -fFace->glyph->bitmap_top);
725 SkASSERT_CONTINUE(glyph.fLeft == fFace->glyph->bitmap_left);
726
727 const uint8_t* src = (const uint8_t*)fFace->glyph->bitmap.buffer;
728 uint8_t* dst = (uint8_t*)glyph.fImage;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000729
agl@chromium.org558434a2009-08-11 17:22:38 +0000730 if (fFace->glyph->bitmap.pixel_mode == FT_PIXEL_MODE_GRAY ||
731 (fFace->glyph->bitmap.pixel_mode == FT_PIXEL_MODE_MONO &&
732 glyph.fMaskFormat == SkMask::kBW_Format)) {
agl@chromium.orgcc3096b2009-04-22 22:09:04 +0000733 unsigned srcRowBytes = fFace->glyph->bitmap.pitch;
734 unsigned dstRowBytes = glyph.rowBytes();
735 unsigned minRowBytes = SkMin32(srcRowBytes, dstRowBytes);
736 unsigned extraRowBytes = dstRowBytes - minRowBytes;
737
738 for (int y = fFace->glyph->bitmap.rows - 1; y >= 0; --y) {
739 memcpy(dst, src, minRowBytes);
740 memset(dst + minRowBytes, 0, extraRowBytes);
741 src += srcRowBytes;
742 dst += dstRowBytes;
743 }
agl@chromium.org558434a2009-08-11 17:22:38 +0000744 } else if (fFace->glyph->bitmap.pixel_mode == FT_PIXEL_MODE_MONO &&
agl@chromium.orge95c91e2010-01-04 18:27:55 +0000745 (glyph.fMaskFormat == SkMask::kA8_Format ||
746 glyph.fMaskFormat == SkMask::kHorizontalLCD_Format ||
747 glyph.fMaskFormat == SkMask::kVerticalLCD_Format)) {
agl@chromium.orgcc3096b2009-04-22 22:09:04 +0000748 for (int y = 0; y < fFace->glyph->bitmap.rows; ++y) {
749 uint8_t byte = 0;
750 int bits = 0;
751 const uint8_t* src_row = src;
752 uint8_t* dst_row = dst;
753
754 for (int x = 0; x < fFace->glyph->bitmap.width; ++x) {
755 if (!bits) {
756 byte = *src_row++;
757 bits = 8;
758 }
759
760 *dst_row++ = byte & 0x80 ? 0xff : 0;
761 bits--;
762 byte <<= 1;
763 }
764
765 src += fFace->glyph->bitmap.pitch;
766 dst += glyph.rowBytes();
767 }
agl@chromium.org558434a2009-08-11 17:22:38 +0000768 } else {
769 SkASSERT(!"unknown glyph bitmap transform needed");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000770 }
agl@chromium.org309485b2009-07-21 17:41:32 +0000771
772 if (lcdRenderMode)
773 glyph.expandA8ToLCD();
774
reed@android.com8a1c16f2008-12-17 15:59:43 +0000775 } break;
776
777 default:
778 SkASSERT(!"unknown glyph format");
779 goto ERROR;
780 }
781}
782
783///////////////////////////////////////////////////////////////////////////////
784
785#define ft2sk(x) SkFixedToScalar((x) << 10)
786
reed@android.com6f252972009-01-14 16:46:16 +0000787#if FREETYPE_MAJOR >= 2 && FREETYPE_MINOR >= 2
reed@android.com8a1c16f2008-12-17 15:59:43 +0000788 #define CONST_PARAM const
789#else // older freetype doesn't use const here
790 #define CONST_PARAM
791#endif
792
793static int move_proc(CONST_PARAM FT_Vector* pt, void* ctx) {
794 SkPath* path = (SkPath*)ctx;
795 path->close(); // to close the previous contour (if any)
796 path->moveTo(ft2sk(pt->x), -ft2sk(pt->y));
797 return 0;
798}
799
800static int line_proc(CONST_PARAM FT_Vector* pt, void* ctx) {
801 SkPath* path = (SkPath*)ctx;
802 path->lineTo(ft2sk(pt->x), -ft2sk(pt->y));
803 return 0;
804}
805
806static int quad_proc(CONST_PARAM FT_Vector* pt0, CONST_PARAM FT_Vector* pt1,
807 void* ctx) {
808 SkPath* path = (SkPath*)ctx;
809 path->quadTo(ft2sk(pt0->x), -ft2sk(pt0->y), ft2sk(pt1->x), -ft2sk(pt1->y));
810 return 0;
811}
812
813static int cubic_proc(CONST_PARAM FT_Vector* pt0, CONST_PARAM FT_Vector* pt1,
814 CONST_PARAM FT_Vector* pt2, void* ctx) {
815 SkPath* path = (SkPath*)ctx;
816 path->cubicTo(ft2sk(pt0->x), -ft2sk(pt0->y), ft2sk(pt1->x),
817 -ft2sk(pt1->y), ft2sk(pt2->x), -ft2sk(pt2->y));
818 return 0;
819}
820
821void SkScalerContext_FreeType::generatePath(const SkGlyph& glyph,
822 SkPath* path) {
823 SkAutoMutexAcquire ac(gFTMutex);
824
825 SkASSERT(&glyph && path);
826
827 if (this->setupSize()) {
828 path->reset();
829 return;
830 }
831
832 uint32_t flags = fLoadGlyphFlags;
833 flags |= FT_LOAD_NO_BITMAP; // ignore embedded bitmaps so we're sure to get the outline
834 flags &= ~FT_LOAD_RENDER; // don't scan convert (we just want the outline)
835
836 FT_Error err = FT_Load_Glyph( fFace, glyph.getGlyphID(fBaseGlyphCount), flags);
837
838 if (err != 0) {
839 SkDEBUGF(("SkScalerContext_FreeType::generatePath: FT_Load_Glyph(glyph:%d flags:%d) returned 0x%x\n",
840 glyph.getGlyphID(fBaseGlyphCount), flags, err));
841 path->reset();
842 return;
843 }
844
senorblanco@chromium.org4526a842010-02-05 23:08:20 +0000845 if (fRec.fFlags & kEmbolden_Flag) {
846 emboldenOutline(&fFace->glyph->outline);
847 }
848
reed@android.com8a1c16f2008-12-17 15:59:43 +0000849 FT_Outline_Funcs funcs;
850
851 funcs.move_to = move_proc;
852 funcs.line_to = line_proc;
853 funcs.conic_to = quad_proc;
854 funcs.cubic_to = cubic_proc;
855 funcs.shift = 0;
856 funcs.delta = 0;
857
858 err = FT_Outline_Decompose(&fFace->glyph->outline, &funcs, path);
859
860 if (err != 0) {
861 SkDEBUGF(("SkScalerContext_FreeType::generatePath: FT_Load_Glyph(glyph:%d flags:%d) returned 0x%x\n",
862 glyph.getGlyphID(fBaseGlyphCount), flags, err));
863 path->reset();
864 return;
865 }
866
867 path->close();
868}
869
870void SkScalerContext_FreeType::generateFontMetrics(SkPaint::FontMetrics* mx,
871 SkPaint::FontMetrics* my) {
872 if (NULL == mx && NULL == my) {
873 return;
874 }
875
876 SkAutoMutexAcquire ac(gFTMutex);
877
878 if (this->setupSize()) {
reed@android.coma8a8b8b2009-05-04 15:00:11 +0000879 ERROR:
reed@android.com8a1c16f2008-12-17 15:59:43 +0000880 if (mx) {
reed@android.com4516f472009-06-29 16:25:36 +0000881 sk_bzero(mx, sizeof(SkPaint::FontMetrics));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000882 }
883 if (my) {
reed@android.com4516f472009-06-29 16:25:36 +0000884 sk_bzero(my, sizeof(SkPaint::FontMetrics));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000885 }
886 return;
887 }
888
reed@android.coma8a8b8b2009-05-04 15:00:11 +0000889 FT_Face face = fFace;
890 int upem = face->units_per_EM;
891 if (upem <= 0) {
892 goto ERROR;
893 }
894
agl@chromium.orgcc3096b2009-04-22 22:09:04 +0000895 SkPoint pts[6];
896 SkFixed ys[6];
reed@android.com8a1c16f2008-12-17 15:59:43 +0000897 SkFixed scaleY = fScaleY;
898 SkFixed mxy = fMatrix22.xy;
899 SkFixed myy = fMatrix22.yy;
agl@chromium.orgcc3096b2009-04-22 22:09:04 +0000900 SkScalar xmin = SkIntToScalar(face->bbox.xMin) / upem;
901 SkScalar xmax = SkIntToScalar(face->bbox.xMax) / upem;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000902
agl@chromium.orgcc3096b2009-04-22 22:09:04 +0000903 int leading = face->height - (face->ascender + -face->descender);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000904 if (leading < 0) {
905 leading = 0;
906 }
907
agl@chromium.orgcc3096b2009-04-22 22:09:04 +0000908 // Try to get the OS/2 table from the font. This contains the specific
909 // average font width metrics which Windows uses.
910 TT_OS2* os2 = (TT_OS2*) FT_Get_Sfnt_Table(face, ft_sfnt_os2);
911
reed@android.com8a1c16f2008-12-17 15:59:43 +0000912 ys[0] = -face->bbox.yMax;
913 ys[1] = -face->ascender;
914 ys[2] = -face->descender;
915 ys[3] = -face->bbox.yMin;
916 ys[4] = leading;
agl@chromium.orgcc3096b2009-04-22 22:09:04 +0000917 ys[5] = os2 ? os2->xAvgCharWidth : 0;
918
919 SkScalar x_height;
920 if (os2 && os2->sxHeight) {
921 x_height = SkFixedToScalar(SkMulDiv(fScaleX, os2->sxHeight, upem));
922 } else {
923 const FT_UInt x_glyph = FT_Get_Char_Index(fFace, 'x');
924 if (x_glyph) {
925 FT_BBox bbox;
926 FT_Load_Glyph(fFace, x_glyph, fLoadGlyphFlags);
senorblanco@chromium.org4526a842010-02-05 23:08:20 +0000927 if (fRec.fFlags & kEmbolden_Flag) {
928 emboldenOutline(&fFace->glyph->outline);
929 }
agl@chromium.orgcc3096b2009-04-22 22:09:04 +0000930 FT_Outline_Get_CBox(&fFace->glyph->outline, &bbox);
931 x_height = SkIntToScalar(bbox.yMax) / 64;
932 } else {
933 x_height = 0;
934 }
935 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000936
937 // convert upem-y values into scalar points
agl@chromium.orgcc3096b2009-04-22 22:09:04 +0000938 for (int i = 0; i < 6; i++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000939 SkFixed y = SkMulDiv(scaleY, ys[i], upem);
940 SkFixed x = SkFixedMul(mxy, y);
941 y = SkFixedMul(myy, y);
942 pts[i].set(SkFixedToScalar(x), SkFixedToScalar(y));
943 }
944
945 if (mx) {
946 mx->fTop = pts[0].fX;
947 mx->fAscent = pts[1].fX;
948 mx->fDescent = pts[2].fX;
949 mx->fBottom = pts[3].fX;
950 mx->fLeading = pts[4].fX;
agl@chromium.orgcc3096b2009-04-22 22:09:04 +0000951 mx->fAvgCharWidth = pts[5].fX;
952 mx->fXMin = xmin;
953 mx->fXMax = xmax;
954 mx->fXHeight = x_height;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000955 }
956 if (my) {
957 my->fTop = pts[0].fY;
958 my->fAscent = pts[1].fY;
959 my->fDescent = pts[2].fY;
960 my->fBottom = pts[3].fY;
961 my->fLeading = pts[4].fY;
agl@chromium.orgcc3096b2009-04-22 22:09:04 +0000962 my->fAvgCharWidth = pts[5].fY;
963 my->fXMin = xmin;
964 my->fXMax = xmax;
965 my->fXHeight = x_height;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000966 }
967}
968
969////////////////////////////////////////////////////////////////////////
970////////////////////////////////////////////////////////////////////////
971
972SkScalerContext* SkFontHost::CreateScalerContext(const SkDescriptor* desc) {
reed@android.com62900b42009-02-11 15:07:19 +0000973 SkScalerContext_FreeType* c = SkNEW_ARGS(SkScalerContext_FreeType, (desc));
974 if (!c->success()) {
975 SkDELETE(c);
976 c = NULL;
977 }
978 return c;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000979}
980
981///////////////////////////////////////////////////////////////////////////////
982
983/* Export this so that other parts of our FonttHost port can make use of our
984 ability to extract the name+style from a stream, using FreeType's api.
985*/
986SkTypeface::Style find_name_and_style(SkStream* stream, SkString* name) {
987 FT_Library library;
reed@android.combfbd4ff2009-07-23 17:44:41 +0000988 if (FT_Init_FreeType(&library)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000989 name->set(NULL);
990 return SkTypeface::kNormal;
991 }
992
993 FT_Open_Args args;
994 memset(&args, 0, sizeof(args));
995
996 const void* memoryBase = stream->getMemoryBase();
997 FT_StreamRec streamRec;
998
999 if (NULL != memoryBase) {
1000 args.flags = FT_OPEN_MEMORY;
1001 args.memory_base = (const FT_Byte*)memoryBase;
1002 args.memory_size = stream->getLength();
1003 } else {
1004 memset(&streamRec, 0, sizeof(streamRec));
1005 streamRec.size = stream->read(NULL, 0);
1006 streamRec.descriptor.pointer = stream;
1007 streamRec.read = sk_stream_read;
1008 streamRec.close = sk_stream_close;
1009
1010 args.flags = FT_OPEN_STREAM;
1011 args.stream = &streamRec;
1012 }
1013
1014 FT_Face face;
1015 if (FT_Open_Face(library, &args, 0, &face)) {
1016 FT_Done_FreeType(library);
1017 name->set(NULL);
1018 return SkTypeface::kNormal;
1019 }
1020
1021 name->set(face->family_name);
1022 int style = SkTypeface::kNormal;
1023
1024 if (face->style_flags & FT_STYLE_FLAG_BOLD) {
1025 style |= SkTypeface::kBold;
1026 }
1027 if (face->style_flags & FT_STYLE_FLAG_ITALIC) {
1028 style |= SkTypeface::kItalic;
1029 }
1030
1031 FT_Done_Face(face);
1032 FT_Done_FreeType(library);
1033 return (SkTypeface::Style)style;
1034}