1 2 /* 3 * Copyright 2006 The Android Open Source Project 4 * 5 * Use of this source code is governed by a BSD-style license that can be 6 * found in the LICENSE file. 7 */ 8 9 #include "SkAdvancedTypefaceMetrics.h" 10 #include "SkBitmap.h" 11 #include "SkCanvas.h" 12 #include "SkColorPriv.h" 13 #include "SkDescriptor.h" 14 #include "SkFDot6.h" 15 #include "SkFloatingPoint.h" 16 #include "SkFontHost.h" 17 #include "SkFontHost_FreeType_common.h" 18 #include "SkGlyph.h" 19 #include "SkMask.h" 20 #include "SkMaskGamma.h" 21 #include "SkMatrix22.h" 22 #include "SkOTUtils.h" 23 #include "SkOnce.h" 24 #include "SkScalerContext.h" 25 #include "SkStream.h" 26 #include "SkString.h" 27 #include "SkTemplates.h" 28 #include "SkThread.h" 29 30 #if defined(SK_CAN_USE_DLOPEN) 31 #include <dlfcn.h> 32 #endif 33 #include <ft2build.h> 34 #include FT_FREETYPE_H 35 #include FT_OUTLINE_H 36 #include FT_SIZES_H 37 #include FT_TRUETYPE_TABLES_H 38 #include FT_TYPE1_TABLES_H 39 #include FT_BITMAP_H 40 // In the past, FT_GlyphSlot_Own_Bitmap was defined in this header file. 41 #include FT_SYNTHESIS_H 42 #include FT_XFREE86_H 43 #ifdef FT_LCD_FILTER_H 44 #include FT_LCD_FILTER_H 45 #endif 46 47 // Defined in FreeType 2.3.8 and later. 48 // This is a silly build time check, we would need a runtime check if we really cared. 49 #ifdef FT_ADVANCES_H 50 #include FT_ADVANCES_H 51 #endif 52 53 #if 0 54 // Also include the files by name for build tools which require this. 55 #include <freetype/freetype.h> 56 #include <freetype/ftoutln.h> 57 #include <freetype/ftsizes.h> 58 #include <freetype/tttables.h> 59 #include <freetype/ftadvanc.h> 60 #include <freetype/ftlcdfil.h> 61 #include <freetype/ftbitmap.h> 62 #include <freetype/ftsynth.h> 63 #endif 64 65 // FT_LOAD_COLOR and the corresponding FT_Pixel_Mode::FT_PIXEL_MODE_BGRA 66 // were introduced in FreeType 2.5.0. 67 // The following may be removed once FreeType 2.5.0 is required to build. 68 #ifndef FT_LOAD_COLOR 69 # define FT_LOAD_COLOR ( 1L << 20 ) 70 # define FT_PIXEL_MODE_BGRA 7 71 #endif 72 73 // FT_HAS_COLOR and the corresponding FT_FACE_FLAG_COLOR 74 // were introduced in FreeType 2.5.1 75 // The following may be removed once FreeType 2.5.1 is required to build. 76 #ifndef FT_HAS_COLOR 77 # define FT_HAS_COLOR(face) false 78 #endif 79 80 //#define ENABLE_GLYPH_SPEW // for tracing calls 81 //#define DUMP_STRIKE_CREATION 82 83 //#define SK_GAMMA_APPLY_TO_A8 84 85 using namespace skia_advanced_typeface_metrics_utils; 86 87 static bool isLCD(const SkScalerContext::Rec& rec) { 88 switch (rec.fMaskFormat) { 89 case SkMask::kLCD16_Format: 90 case SkMask::kLCD32_Format: 91 return true; 92 default: 93 return false; 94 } 95 } 96 97 ////////////////////////////////////////////////////////////////////////// 98 99 struct SkFaceRec; 100 101 SK_DECLARE_STATIC_MUTEX(gFTMutex); 102 static int gFTCount; 103 static FT_Library gFTLibrary; 104 static SkFaceRec* gFaceRecHead; 105 static bool gLCDSupportValid; // true iff |gLCDSupport| has been set. 106 static bool gLCDSupport; // true iff LCD is supported by the runtime. 107 static int gLCDExtra; // number of extra pixels for filtering. 108 109 ///////////////////////////////////////////////////////////////////////// 110 111 // FT_Library_SetLcdFilterWeights was introduced in FreeType 2.4.0. 112 // The following platforms provide FreeType of at least 2.4.0. 113 // Ubuntu >= 11.04 (previous deprecated April 2013) 114 // Debian >= 6.0 (good) 115 // OpenSuse >= 11.4 (previous deprecated January 2012 / Nov 2013 for Evergreen 11.2) 116 // Fedora >= 14 (good) 117 // Android >= Gingerbread (good) 118 typedef FT_Error (*FT_Library_SetLcdFilterWeightsProc)(FT_Library, unsigned char*); 119 120 // Caller must lock gFTMutex before calling this function. 121 static bool InitFreetype() { 122 FT_Error err = FT_Init_FreeType(&gFTLibrary); 123 if (err) { 124 return false; 125 } 126 127 // Setup LCD filtering. This reduces color fringes for LCD smoothed glyphs. 128 #ifdef FT_LCD_FILTER_H 129 // Use default { 0x10, 0x40, 0x70, 0x40, 0x10 }, as it adds up to 0x110, simulating ink spread. 130 // SetLcdFilter must be called before SetLcdFilterWeights. 131 err = FT_Library_SetLcdFilter(gFTLibrary, FT_LCD_FILTER_DEFAULT); 132 if (0 == err) { 133 gLCDSupport = true; 134 gLCDExtra = 2; //Using a filter adds one full pixel to each side. 135 136 #ifdef SK_FONTHOST_FREETYPE_USE_NORMAL_LCD_FILTER 137 // This also adds to 0x110 simulating ink spread, but provides better results than default. 138 static unsigned char gGaussianLikeHeavyWeights[] = { 0x1A, 0x43, 0x56, 0x43, 0x1A, }; 139 140 #if defined(SK_FONTHOST_FREETYPE_RUNTIME_VERSION) && \ 141 SK_FONTHOST_FREETYPE_RUNTIME_VERSION > 0x020400 142 err = FT_Library_SetLcdFilterWeights(gFTLibrary, gGaussianLikeHeavyWeights); 143 #elif defined(SK_CAN_USE_DLOPEN) && SK_CAN_USE_DLOPEN == 1 144 //The FreeType library is already loaded, so symbols are available in process. 145 void* self = dlopen(NULL, RTLD_LAZY); 146 if (NULL != self) { 147 FT_Library_SetLcdFilterWeightsProc setLcdFilterWeights; 148 //The following cast is non-standard, but safe for POSIX. 149 *reinterpret_cast<void**>(&setLcdFilterWeights) = dlsym(self, "FT_Library_SetLcdFilterWeights"); 150 dlclose(self); 151 152 if (NULL != setLcdFilterWeights) { 153 err = setLcdFilterWeights(gFTLibrary, gGaussianLikeHeavyWeights); 154 } 155 } 156 #endif 157 #endif 158 } 159 #else 160 gLCDSupport = false; 161 #endif 162 gLCDSupportValid = true; 163 164 return true; 165 } 166 167 // Called while holding gFTMutex. 168 static void determine_lcd_support(bool* lcdSupported) { 169 if (!gLCDSupportValid) { 170 // This will determine LCD support as a side effect. 171 InitFreetype(); 172 FT_Done_FreeType(gFTLibrary); 173 } 174 SkASSERT(gLCDSupportValid); 175 *lcdSupported = gLCDSupport; 176 } 177 178 // Lazy, once, wrapper to ask the FreeType Library if it can support LCD text 179 static bool is_lcd_supported() { 180 static bool lcdSupported = false; 181 SkOnce(&gLCDSupportValid, &gFTMutex, determine_lcd_support, &lcdSupported); 182 return lcdSupported; 183 } 184 185 class SkScalerContext_FreeType : public SkScalerContext_FreeType_Base { 186 public: 187 SkScalerContext_FreeType(SkTypeface*, const SkDescriptor* desc); 188 virtual ~SkScalerContext_FreeType(); 189 190 bool success() const { 191 return fFaceRec != NULL && 192 fFTSize != NULL && 193 fFace != NULL; 194 } 195 196 protected: 197 virtual unsigned generateGlyphCount() SK_OVERRIDE; 198 virtual uint16_t generateCharToGlyph(SkUnichar uni) SK_OVERRIDE; 199 virtual void generateAdvance(SkGlyph* glyph) SK_OVERRIDE; 200 virtual void generateMetrics(SkGlyph* glyph) SK_OVERRIDE; 201 virtual void generateImage(const SkGlyph& glyph) SK_OVERRIDE; 202 virtual void generatePath(const SkGlyph& glyph, SkPath* path) SK_OVERRIDE; 203 virtual void generateFontMetrics(SkPaint::FontMetrics* mx, 204 SkPaint::FontMetrics* my) SK_OVERRIDE; 205 virtual SkUnichar generateGlyphToChar(uint16_t glyph) SK_OVERRIDE; 206 207 private: 208 SkFaceRec* fFaceRec; 209 FT_Face fFace; // reference to shared face in gFaceRecHead 210 FT_Size fFTSize; // our own copy 211 FT_Int fStrikeIndex; 212 SkFixed fScaleX, fScaleY; 213 FT_Matrix fMatrix22; 214 uint32_t fLoadGlyphFlags; 215 bool fDoLinearMetrics; 216 bool fLCDIsVert; 217 218 // Need scalar versions for generateFontMetrics 219 SkVector fScale; 220 SkMatrix fMatrix22Scalar; 221 222 FT_Error setupSize(); 223 void getBBoxForCurrentGlyph(SkGlyph* glyph, FT_BBox* bbox, 224 bool snapToPixelBoundary = false); 225 bool getCBoxForLetter(char letter, FT_BBox* bbox); 226 // Caller must lock gFTMutex before calling this function. 227 void updateGlyphIfLCD(SkGlyph* glyph); 228 // Caller must lock gFTMutex before calling this function. 229 // update FreeType2 glyph slot with glyph emboldened 230 void emboldenIfNeeded(FT_Face face, FT_GlyphSlot glyph); 231 }; 232 233 /////////////////////////////////////////////////////////////////////////// 234 /////////////////////////////////////////////////////////////////////////// 235 236 struct SkFaceRec { 237 SkFaceRec* fNext; 238 FT_Face fFace; 239 FT_StreamRec fFTStream; 240 SkStream* fSkStream; 241 uint32_t fRefCnt; 242 uint32_t fFontID; 243 244 // assumes ownership of the stream, will call unref() when its done 245 SkFaceRec(SkStream* strm, uint32_t fontID); 246 ~SkFaceRec() { 247 fSkStream->unref(); 248 } 249 }; 250 251 extern "C" { 252 static unsigned long sk_stream_read(FT_Stream stream, 253 unsigned long offset, 254 unsigned char* buffer, 255 unsigned long count ) { 256 SkStream* str = (SkStream*)stream->descriptor.pointer; 257 258 if (count) { 259 if (!str->rewind()) { 260 return 0; 261 } else { 262 unsigned long ret; 263 if (offset) { 264 ret = str->read(NULL, offset); 265 if (ret != offset) { 266 return 0; 267 } 268 } 269 ret = str->read(buffer, count); 270 if (ret != count) { 271 return 0; 272 } 273 count = ret; 274 } 275 } 276 return count; 277 } 278 279 static void sk_stream_close(FT_Stream) {} 280 } 281 282 SkFaceRec::SkFaceRec(SkStream* strm, uint32_t fontID) 283 : fNext(NULL), fSkStream(strm), fRefCnt(1), fFontID(fontID) { 284 // SkDEBUGF(("SkFaceRec: opening %s (%p)\n", key.c_str(), strm)); 285 286 sk_bzero(&fFTStream, sizeof(fFTStream)); 287 fFTStream.size = fSkStream->getLength(); 288 fFTStream.descriptor.pointer = fSkStream; 289 fFTStream.read = sk_stream_read; 290 fFTStream.close = sk_stream_close; 291 } 292 293 // Will return 0 on failure 294 // Caller must lock gFTMutex before calling this function. 295 static SkFaceRec* ref_ft_face(const SkTypeface* typeface) { 296 const SkFontID fontID = typeface->uniqueID(); 297 SkFaceRec* rec = gFaceRecHead; 298 while (rec) { 299 if (rec->fFontID == fontID) { 300 SkASSERT(rec->fFace); 301 rec->fRefCnt += 1; 302 return rec; 303 } 304 rec = rec->fNext; 305 } 306 307 int face_index; 308 SkStream* strm = typeface->openStream(&face_index); 309 if (NULL == strm) { 310 return NULL; 311 } 312 313 // this passes ownership of strm to the rec 314 rec = SkNEW_ARGS(SkFaceRec, (strm, fontID)); 315 316 FT_Open_Args args; 317 memset(&args, 0, sizeof(args)); 318 const void* memoryBase = strm->getMemoryBase(); 319 320 if (NULL != memoryBase) { 321 //printf("mmap(%s)\n", keyString.c_str()); 322 args.flags = FT_OPEN_MEMORY; 323 args.memory_base = (const FT_Byte*)memoryBase; 324 args.memory_size = strm->getLength(); 325 } else { 326 //printf("fopen(%s)\n", keyString.c_str()); 327 args.flags = FT_OPEN_STREAM; 328 args.stream = &rec->fFTStream; 329 } 330 331 FT_Error err = FT_Open_Face(gFTLibrary, &args, face_index, &rec->fFace); 332 if (err) { // bad filename, try the default font 333 fprintf(stderr, "ERROR: unable to open font '%x'\n", fontID); 334 SkDELETE(rec); 335 return NULL; 336 } else { 337 SkASSERT(rec->fFace); 338 //fprintf(stderr, "Opened font '%s'\n", filename.c_str()); 339 rec->fNext = gFaceRecHead; 340 gFaceRecHead = rec; 341 return rec; 342 } 343 } 344 345 // Caller must lock gFTMutex before calling this function. 346 static void unref_ft_face(FT_Face face) { 347 SkFaceRec* rec = gFaceRecHead; 348 SkFaceRec* prev = NULL; 349 while (rec) { 350 SkFaceRec* next = rec->fNext; 351 if (rec->fFace == face) { 352 if (--rec->fRefCnt == 0) { 353 if (prev) { 354 prev->fNext = next; 355 } else { 356 gFaceRecHead = next; 357 } 358 FT_Done_Face(face); 359 SkDELETE(rec); 360 } 361 return; 362 } 363 prev = rec; 364 rec = next; 365 } 366 SkDEBUGFAIL("shouldn't get here, face not in list"); 367 } 368 369 class AutoFTAccess { 370 public: 371 AutoFTAccess(const SkTypeface* tf) : fRec(NULL), fFace(NULL) { 372 gFTMutex.acquire(); 373 if (1 == ++gFTCount) { 374 if (!InitFreetype()) { 375 sk_throw(); 376 } 377 } 378 fRec = ref_ft_face(tf); 379 if (fRec) { 380 fFace = fRec->fFace; 381 } 382 } 383 384 ~AutoFTAccess() { 385 if (fFace) { 386 unref_ft_face(fFace); 387 } 388 if (0 == --gFTCount) { 389 FT_Done_FreeType(gFTLibrary); 390 } 391 gFTMutex.release(); 392 } 393 394 SkFaceRec* rec() { return fRec; } 395 FT_Face face() { return fFace; } 396 397 private: 398 SkFaceRec* fRec; 399 FT_Face fFace; 400 }; 401 402 /////////////////////////////////////////////////////////////////////////// 403 404 // Work around for old versions of freetype. 405 static FT_Error getAdvances(FT_Face face, FT_UInt start, FT_UInt count, 406 FT_Int32 loadFlags, FT_Fixed* advances) { 407 #ifdef FT_ADVANCES_H 408 return FT_Get_Advances(face, start, count, loadFlags, advances); 409 #else 410 if (!face || start >= face->num_glyphs || 411 start + count > face->num_glyphs || loadFlags != FT_LOAD_NO_SCALE) { 412 return 6; // "Invalid argument." 413 } 414 if (count == 0) 415 return 0; 416 417 for (int i = 0; i < count; i++) { 418 FT_Error err = FT_Load_Glyph(face, start + i, FT_LOAD_NO_SCALE); 419 if (err) 420 return err; 421 advances[i] = face->glyph->advance.x; 422 } 423 424 return 0; 425 #endif 426 } 427 428 static bool canEmbed(FT_Face face) { 429 #ifdef FT_FSTYPE_RESTRICTED_LICENSE_EMBEDDING 430 FT_UShort fsType = FT_Get_FSType_Flags(face); 431 return (fsType & (FT_FSTYPE_RESTRICTED_LICENSE_EMBEDDING | 432 FT_FSTYPE_BITMAP_EMBEDDING_ONLY)) == 0; 433 #else 434 // No embedding is 0x2 and bitmap embedding only is 0x200. 435 TT_OS2* os2_table; 436 if ((os2_table = (TT_OS2*)FT_Get_Sfnt_Table(face, ft_sfnt_os2)) != NULL) { 437 return (os2_table->fsType & 0x202) == 0; 438 } 439 return false; // We tried, fail safe. 440 #endif 441 } 442 443 static bool canSubset(FT_Face face) { 444 #ifdef FT_FSTYPE_NO_SUBSETTING 445 FT_UShort fsType = FT_Get_FSType_Flags(face); 446 return (fsType & FT_FSTYPE_NO_SUBSETTING) == 0; 447 #else 448 // No subset is 0x100. 449 TT_OS2* os2_table; 450 if ((os2_table = (TT_OS2*)FT_Get_Sfnt_Table(face, ft_sfnt_os2)) != NULL) { 451 return (os2_table->fsType & 0x100) == 0; 452 } 453 return false; // We tried, fail safe. 454 #endif 455 } 456 457 static bool GetLetterCBox(FT_Face face, char letter, FT_BBox* bbox) { 458 const FT_UInt glyph_id = FT_Get_Char_Index(face, letter); 459 if (!glyph_id) 460 return false; 461 if (FT_Load_Glyph(face, glyph_id, FT_LOAD_NO_SCALE) != 0) 462 return false; 463 FT_Outline_Get_CBox(&face->glyph->outline, bbox); 464 return true; 465 } 466 467 static bool getWidthAdvance(FT_Face face, int gId, int16_t* data) { 468 FT_Fixed advance = 0; 469 if (getAdvances(face, gId, 1, FT_LOAD_NO_SCALE, &advance)) { 470 return false; 471 } 472 SkASSERT(data); 473 *data = advance; 474 return true; 475 } 476 477 static void populate_glyph_to_unicode(FT_Face& face, 478 SkTDArray<SkUnichar>* glyphToUnicode) { 479 // Check and see if we have Unicode cmaps. 480 for (int i = 0; i < face->num_charmaps; ++i) { 481 // CMaps known to support Unicode: 482 // Platform ID Encoding ID Name 483 // ----------- ----------- ----------------------------------- 484 // 0 0,1 Apple Unicode 485 // 0 3 Apple Unicode 2.0 (preferred) 486 // 3 1 Microsoft Unicode UCS-2 487 // 3 10 Microsoft Unicode UCS-4 (preferred) 488 // 489 // See Apple TrueType Reference Manual 490 // http://developer.apple.com/fonts/TTRefMan/RM06/Chap6cmap.html 491 // http://developer.apple.com/fonts/TTRefMan/RM06/Chap6name.html#ID 492 // Microsoft OpenType Specification 493 // http://www.microsoft.com/typography/otspec/cmap.htm 494 495 FT_UShort platformId = face->charmaps[i]->platform_id; 496 FT_UShort encodingId = face->charmaps[i]->encoding_id; 497 498 if (platformId != 0 && platformId != 3) { 499 continue; 500 } 501 if (platformId == 3 && encodingId != 1 && encodingId != 10) { 502 continue; 503 } 504 bool preferredMap = ((platformId == 3 && encodingId == 10) || 505 (platformId == 0 && encodingId == 3)); 506 507 FT_Set_Charmap(face, face->charmaps[i]); 508 if (glyphToUnicode->isEmpty()) { 509 glyphToUnicode->setCount(face->num_glyphs); 510 memset(glyphToUnicode->begin(), 0, 511 sizeof(SkUnichar) * face->num_glyphs); 512 } 513 514 // Iterate through each cmap entry. 515 FT_UInt glyphIndex; 516 for (SkUnichar charCode = FT_Get_First_Char(face, &glyphIndex); 517 glyphIndex != 0; 518 charCode = FT_Get_Next_Char(face, charCode, &glyphIndex)) { 519 if (charCode && 520 ((*glyphToUnicode)[glyphIndex] == 0 || preferredMap)) { 521 (*glyphToUnicode)[glyphIndex] = charCode; 522 } 523 } 524 } 525 } 526 527 SkAdvancedTypefaceMetrics* SkTypeface_FreeType::onGetAdvancedTypefaceMetrics( 528 SkAdvancedTypefaceMetrics::PerGlyphInfo perGlyphInfo, 529 const uint32_t* glyphIDs, 530 uint32_t glyphIDsCount) const { 531 #if defined(SK_BUILD_FOR_MAC) 532 return NULL; 533 #else 534 AutoFTAccess fta(this); 535 FT_Face face = fta.face(); 536 if (!face) { 537 return NULL; 538 } 539 540 SkAdvancedTypefaceMetrics* info = new SkAdvancedTypefaceMetrics; 541 info->fFontName.set(FT_Get_Postscript_Name(face)); 542 info->fFlags = SkAdvancedTypefaceMetrics::kEmpty_FontFlag; 543 if (FT_HAS_MULTIPLE_MASTERS(face)) { 544 info->fFlags = SkTBitOr<SkAdvancedTypefaceMetrics::FontFlags>( 545 info->fFlags, SkAdvancedTypefaceMetrics::kMultiMaster_FontFlag); 546 } 547 if (!canEmbed(face)) { 548 info->fFlags = SkTBitOr<SkAdvancedTypefaceMetrics::FontFlags>( 549 info->fFlags, 550 SkAdvancedTypefaceMetrics::kNotEmbeddable_FontFlag); 551 } 552 if (!canSubset(face)) { 553 info->fFlags = SkTBitOr<SkAdvancedTypefaceMetrics::FontFlags>( 554 info->fFlags, 555 SkAdvancedTypefaceMetrics::kNotSubsettable_FontFlag); 556 } 557 info->fLastGlyphID = face->num_glyphs - 1; 558 info->fEmSize = 1000; 559 560 bool cid = false; 561 const char* fontType = FT_Get_X11_Font_Format(face); 562 if (strcmp(fontType, "Type 1") == 0) { 563 info->fType = SkAdvancedTypefaceMetrics::kType1_Font; 564 } else if (strcmp(fontType, "CID Type 1") == 0) { 565 info->fType = SkAdvancedTypefaceMetrics::kType1CID_Font; 566 cid = true; 567 } else if (strcmp(fontType, "CFF") == 0) { 568 info->fType = SkAdvancedTypefaceMetrics::kCFF_Font; 569 } else if (strcmp(fontType, "TrueType") == 0) { 570 info->fType = SkAdvancedTypefaceMetrics::kTrueType_Font; 571 cid = true; 572 TT_Header* ttHeader; 573 if ((ttHeader = (TT_Header*)FT_Get_Sfnt_Table(face, 574 ft_sfnt_head)) != NULL) { 575 info->fEmSize = ttHeader->Units_Per_EM; 576 } 577 } else { 578 info->fType = SkAdvancedTypefaceMetrics::kOther_Font; 579 } 580 581 info->fStyle = 0; 582 if (FT_IS_FIXED_WIDTH(face)) 583 info->fStyle |= SkAdvancedTypefaceMetrics::kFixedPitch_Style; 584 if (face->style_flags & FT_STYLE_FLAG_ITALIC) 585 info->fStyle |= SkAdvancedTypefaceMetrics::kItalic_Style; 586 587 PS_FontInfoRec ps_info; 588 TT_Postscript* tt_info; 589 if (FT_Get_PS_Font_Info(face, &ps_info) == 0) { 590 info->fItalicAngle = ps_info.italic_angle; 591 } else if ((tt_info = 592 (TT_Postscript*)FT_Get_Sfnt_Table(face, 593 ft_sfnt_post)) != NULL) { 594 info->fItalicAngle = SkFixedToScalar(tt_info->italicAngle); 595 } else { 596 info->fItalicAngle = 0; 597 } 598 599 info->fAscent = face->ascender; 600 info->fDescent = face->descender; 601 602 // Figure out a good guess for StemV - Min width of i, I, !, 1. 603 // This probably isn't very good with an italic font. 604 int16_t min_width = SHRT_MAX; 605 info->fStemV = 0; 606 char stem_chars[] = {'i', 'I', '!', '1'}; 607 for (size_t i = 0; i < SK_ARRAY_COUNT(stem_chars); i++) { 608 FT_BBox bbox; 609 if (GetLetterCBox(face, stem_chars[i], &bbox)) { 610 int16_t width = bbox.xMax - bbox.xMin; 611 if (width > 0 && width < min_width) { 612 min_width = width; 613 info->fStemV = min_width; 614 } 615 } 616 } 617 618 TT_PCLT* pclt_info; 619 TT_OS2* os2_table; 620 if ((pclt_info = (TT_PCLT*)FT_Get_Sfnt_Table(face, ft_sfnt_pclt)) != NULL) { 621 info->fCapHeight = pclt_info->CapHeight; 622 uint8_t serif_style = pclt_info->SerifStyle & 0x3F; 623 if (serif_style >= 2 && serif_style <= 6) 624 info->fStyle |= SkAdvancedTypefaceMetrics::kSerif_Style; 625 else if (serif_style >= 9 && serif_style <= 12) 626 info->fStyle |= SkAdvancedTypefaceMetrics::kScript_Style; 627 } else if (((os2_table = (TT_OS2*)FT_Get_Sfnt_Table(face, ft_sfnt_os2)) != NULL) && 628 // sCapHeight is available only when version 2 or later. 629 os2_table->version != 0xFFFF && 630 os2_table->version >= 2) { 631 info->fCapHeight = os2_table->sCapHeight; 632 } else { 633 // Figure out a good guess for CapHeight: average the height of M and X. 634 FT_BBox m_bbox, x_bbox; 635 bool got_m, got_x; 636 got_m = GetLetterCBox(face, 'M', &m_bbox); 637 got_x = GetLetterCBox(face, 'X', &x_bbox); 638 if (got_m && got_x) { 639 info->fCapHeight = (m_bbox.yMax - m_bbox.yMin + x_bbox.yMax - 640 x_bbox.yMin) / 2; 641 } else if (got_m && !got_x) { 642 info->fCapHeight = m_bbox.yMax - m_bbox.yMin; 643 } else if (!got_m && got_x) { 644 info->fCapHeight = x_bbox.yMax - x_bbox.yMin; 645 } else { 646 // Last resort, use the ascent. 647 info->fCapHeight = info->fAscent; 648 } 649 } 650 651 info->fBBox = SkIRect::MakeLTRB(face->bbox.xMin, face->bbox.yMax, 652 face->bbox.xMax, face->bbox.yMin); 653 654 if (!FT_IS_SCALABLE(face)) { 655 perGlyphInfo = SkAdvancedTypefaceMetrics::kNo_PerGlyphInfo; 656 } 657 658 if (perGlyphInfo & SkAdvancedTypefaceMetrics::kHAdvance_PerGlyphInfo) { 659 if (FT_IS_FIXED_WIDTH(face)) { 660 appendRange(&info->fGlyphWidths, 0); 661 int16_t advance = face->max_advance_width; 662 info->fGlyphWidths->fAdvance.append(1, &advance); 663 finishRange(info->fGlyphWidths.get(), 0, 664 SkAdvancedTypefaceMetrics::WidthRange::kDefault); 665 } else if (!cid) { 666 appendRange(&info->fGlyphWidths, 0); 667 // So as to not blow out the stack, get advances in batches. 668 for (int gID = 0; gID < face->num_glyphs; gID += 128) { 669 FT_Fixed advances[128]; 670 int advanceCount = 128; 671 if (gID + advanceCount > face->num_glyphs) 672 advanceCount = face->num_glyphs - gID; 673 getAdvances(face, gID, advanceCount, FT_LOAD_NO_SCALE, 674 advances); 675 for (int i = 0; i < advanceCount; i++) { 676 int16_t advance = advances[i]; 677 info->fGlyphWidths->fAdvance.append(1, &advance); 678 } 679 } 680 finishRange(info->fGlyphWidths.get(), face->num_glyphs - 1, 681 SkAdvancedTypefaceMetrics::WidthRange::kRange); 682 } else { 683 info->fGlyphWidths.reset( 684 getAdvanceData(face, 685 face->num_glyphs, 686 glyphIDs, 687 glyphIDsCount, 688 &getWidthAdvance)); 689 } 690 } 691 692 if (perGlyphInfo & SkAdvancedTypefaceMetrics::kVAdvance_PerGlyphInfo && 693 FT_HAS_VERTICAL(face)) { 694 SkASSERT(false); // Not implemented yet. 695 } 696 697 if (perGlyphInfo & SkAdvancedTypefaceMetrics::kGlyphNames_PerGlyphInfo && 698 info->fType == SkAdvancedTypefaceMetrics::kType1_Font) { 699 // Postscript fonts may contain more than 255 glyphs, so we end up 700 // using multiple font descriptions with a glyph ordering. Record 701 // the name of each glyph. 702 info->fGlyphNames.reset( 703 new SkAutoTArray<SkString>(face->num_glyphs)); 704 for (int gID = 0; gID < face->num_glyphs; gID++) { 705 char glyphName[128]; // PS limit for names is 127 bytes. 706 FT_Get_Glyph_Name(face, gID, glyphName, 128); 707 info->fGlyphNames->get()[gID].set(glyphName); 708 } 709 } 710 711 if (perGlyphInfo & SkAdvancedTypefaceMetrics::kToUnicode_PerGlyphInfo && 712 info->fType != SkAdvancedTypefaceMetrics::kType1_Font && 713 face->num_charmaps) { 714 populate_glyph_to_unicode(face, &(info->fGlyphToUnicode)); 715 } 716 717 return info; 718 #endif 719 } 720 721 /////////////////////////////////////////////////////////////////////////// 722 723 static bool bothZero(SkScalar a, SkScalar b) { 724 return 0 == a && 0 == b; 725 } 726 727 // returns false if there is any non-90-rotation or skew 728 static bool isAxisAligned(const SkScalerContext::Rec& rec) { 729 return 0 == rec.fPreSkewX && 730 (bothZero(rec.fPost2x2[0][1], rec.fPost2x2[1][0]) || 731 bothZero(rec.fPost2x2[0][0], rec.fPost2x2[1][1])); 732 } 733 734 SkScalerContext* SkTypeface_FreeType::onCreateScalerContext( 735 const SkDescriptor* desc) const { 736 SkScalerContext_FreeType* c = SkNEW_ARGS(SkScalerContext_FreeType, 737 (const_cast<SkTypeface_FreeType*>(this), 738 desc)); 739 if (!c->success()) { 740 SkDELETE(c); 741 c = NULL; 742 } 743 return c; 744 } 745 746 void SkTypeface_FreeType::onFilterRec(SkScalerContextRec* rec) const { 747 //BOGUS: http://code.google.com/p/chromium/issues/detail?id=121119 748 //Cap the requested size as larger sizes give bogus values. 749 //Remove when http://code.google.com/p/skia/issues/detail?id=554 is fixed. 750 if (rec->fTextSize > SkIntToScalar(1 << 14)) { 751 rec->fTextSize = SkIntToScalar(1 << 14); 752 } 753 754 if (!is_lcd_supported() && isLCD(*rec)) { 755 // If the runtime Freetype library doesn't support LCD mode, we disable 756 // it here. 757 rec->fMaskFormat = SkMask::kA8_Format; 758 } 759 760 SkPaint::Hinting h = rec->getHinting(); 761 if (SkPaint::kFull_Hinting == h && !isLCD(*rec)) { 762 // collapse full->normal hinting if we're not doing LCD 763 h = SkPaint::kNormal_Hinting; 764 } 765 if ((rec->fFlags & SkScalerContext::kSubpixelPositioning_Flag)) { 766 if (SkPaint::kNo_Hinting != h) { 767 h = SkPaint::kSlight_Hinting; 768 } 769 } 770 771 // rotated text looks bad with hinting, so we disable it as needed 772 if (!isAxisAligned(*rec)) { 773 h = SkPaint::kNo_Hinting; 774 } 775 rec->setHinting(h); 776 777 #ifndef SK_GAMMA_APPLY_TO_A8 778 if (!isLCD(*rec)) { 779 rec->ignorePreBlend(); 780 } 781 #endif 782 } 783 784 int SkTypeface_FreeType::onGetUPEM() const { 785 AutoFTAccess fta(this); 786 FT_Face face = fta.face(); 787 return face ? face->units_per_EM : 0; 788 } 789 790 bool SkTypeface_FreeType::onGetKerningPairAdjustments(const uint16_t glyphs[], 791 int count, int32_t adjustments[]) const { 792 AutoFTAccess fta(this); 793 FT_Face face = fta.face(); 794 if (!face || !FT_HAS_KERNING(face)) { 795 return false; 796 } 797 798 for (int i = 0; i < count - 1; ++i) { 799 FT_Vector delta; 800 FT_Error err = FT_Get_Kerning(face, glyphs[i], glyphs[i+1], 801 FT_KERNING_UNSCALED, &delta); 802 if (err) { 803 return false; 804 } 805 adjustments[i] = delta.x; 806 } 807 return true; 808 } 809 810 static FT_Int chooseBitmapStrike(FT_Face face, SkFixed scaleY) { 811 // early out if face is bad 812 if (face == NULL) { 813 SkDEBUGF(("chooseBitmapStrike aborted due to NULL face\n")); 814 return -1; 815 } 816 // determine target ppem 817 FT_Pos targetPPEM = SkFixedToFDot6(scaleY); 818 // find a bitmap strike equal to or just larger than the requested size 819 FT_Int chosenStrikeIndex = -1; 820 FT_Pos chosenPPEM = 0; 821 for (FT_Int strikeIndex = 0; strikeIndex < face->num_fixed_sizes; ++strikeIndex) { 822 FT_Pos thisPPEM = face->available_sizes[strikeIndex].y_ppem; 823 if (thisPPEM == targetPPEM) { 824 // exact match - our search stops here 825 chosenPPEM = thisPPEM; 826 chosenStrikeIndex = strikeIndex; 827 break; 828 } else if (chosenPPEM < targetPPEM) { 829 // attempt to increase chosenPPEM 830 if (thisPPEM > chosenPPEM) { 831 chosenPPEM = thisPPEM; 832 chosenStrikeIndex = strikeIndex; 833 } 834 } else { 835 // attempt to decrease chosenPPEM, but not below targetPPEM 836 if (thisPPEM < chosenPPEM && thisPPEM > targetPPEM) { 837 chosenPPEM = thisPPEM; 838 chosenStrikeIndex = strikeIndex; 839 } 840 } 841 } 842 if (chosenStrikeIndex != -1) { 843 // use the chosen strike 844 FT_Error err = FT_Select_Size(face, chosenStrikeIndex); 845 if (err != 0) { 846 SkDEBUGF(("FT_Select_Size(%s, %d) returned 0x%x\n", face->family_name, 847 chosenStrikeIndex, err)); 848 chosenStrikeIndex = -1; 849 } 850 } 851 return chosenStrikeIndex; 852 } 853 854 SkScalerContext_FreeType::SkScalerContext_FreeType(SkTypeface* typeface, 855 const SkDescriptor* desc) 856 : SkScalerContext_FreeType_Base(typeface, desc) { 857 SkAutoMutexAcquire ac(gFTMutex); 858 859 if (gFTCount == 0) { 860 if (!InitFreetype()) { 861 sk_throw(); 862 } 863 } 864 ++gFTCount; 865 866 // load the font file 867 fStrikeIndex = -1; 868 fFTSize = NULL; 869 fFace = NULL; 870 fFaceRec = ref_ft_face(typeface); 871 if (NULL == fFaceRec) { 872 return; 873 } 874 fFace = fFaceRec->fFace; 875 876 // A is the total matrix. 877 SkMatrix A; 878 fRec.getSingleMatrix(&A); 879 880 SkScalar sx = A.getScaleX(); 881 SkScalar sy = A.getScaleY(); 882 fMatrix22Scalar.reset(); 883 884 // In GDI, the hinter is aware of the current transformation 885 // (the transform is in some sense applied before/with the hinting). 886 // The bytecode can then test if it is rotated or stretched and decide 887 // to apply instructions or not. 888 // 889 // FreeType, however, always does the transformation strictly after hinting. 890 // It just sets 'rotated' and 'stretched' to false and only applies the 891 // size before hinting. 892 // 893 // Also, FreeType respects the head::flags::IntegerScaling flag, 894 // (although this is patched out on most major distros) 895 // so it is critical to get the size correct on the request. 896 // 897 // This also gets us the actual closest size on bitmap fonts as well. 898 if (A.getSkewX() || A.getSkewY() || sx < 0 || sy < 0) { 899 // h is where A maps the horizontal baseline. 900 SkPoint h = SkPoint::Make(SK_Scalar1, 0); 901 A.mapPoints(&h, 1); 902 903 // G is the Givens Matrix for A (rotational matrix where GA[0][1] == 0). 904 SkMatrix G; 905 SkComputeGivensRotation(h, &G); 906 907 // GA is the matrix A with rotation removed. 908 SkMatrix GA(G); 909 GA.preConcat(A); 910 911 sx = SkScalarAbs(GA.get(SkMatrix::kMScaleX)); 912 sy = SkScalarAbs(GA.get(SkMatrix::kMScaleY)); 913 914 // sA is the total matrix A without the text scale. 915 SkMatrix sA(A); 916 sA.preScale(SkScalarInvert(sx), SkScalarInvert(sy)); //remove text size 917 918 fMatrix22Scalar.setScaleX(sA.getScaleX()); 919 fMatrix22Scalar.setSkewX(-sA.getSkewX()); 920 fMatrix22Scalar.setSkewY(-sA.getSkewY()); 921 fMatrix22Scalar.setScaleY(sA.getScaleY()); 922 } 923 fScale.set(sx, sy); 924 fScaleX = SkScalarToFixed(sx); 925 fScaleY = SkScalarToFixed(sy); 926 fMatrix22.xx = SkScalarToFixed(fMatrix22Scalar.getScaleX()); 927 fMatrix22.xy = SkScalarToFixed(fMatrix22Scalar.getSkewX()); 928 fMatrix22.yx = SkScalarToFixed(fMatrix22Scalar.getSkewY()); 929 fMatrix22.yy = SkScalarToFixed(fMatrix22Scalar.getScaleY()); 930 931 fLCDIsVert = SkToBool(fRec.fFlags & SkScalerContext::kLCD_Vertical_Flag); 932 933 // compute the flags we send to Load_Glyph 934 bool linearMetrics = SkToBool(fRec.fFlags & SkScalerContext::kSubpixelPositioning_Flag); 935 { 936 FT_Int32 loadFlags = FT_LOAD_DEFAULT; 937 938 if (SkMask::kBW_Format == fRec.fMaskFormat) { 939 // See http://code.google.com/p/chromium/issues/detail?id=43252#c24 940 loadFlags = FT_LOAD_TARGET_MONO; 941 if (fRec.getHinting() == SkPaint::kNo_Hinting) { 942 loadFlags = FT_LOAD_NO_HINTING; 943 linearMetrics = true; 944 } 945 } else { 946 switch (fRec.getHinting()) { 947 case SkPaint::kNo_Hinting: 948 loadFlags = FT_LOAD_NO_HINTING; 949 linearMetrics = true; 950 break; 951 case SkPaint::kSlight_Hinting: 952 loadFlags = FT_LOAD_TARGET_LIGHT; // This implies FORCE_AUTOHINT 953 break; 954 case SkPaint::kNormal_Hinting: 955 if (fRec.fFlags & SkScalerContext::kForceAutohinting_Flag) { 956 loadFlags = FT_LOAD_FORCE_AUTOHINT; 957 #ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK 958 } else { 959 loadFlags = FT_LOAD_NO_AUTOHINT; 960 #endif 961 } 962 break; 963 case SkPaint::kFull_Hinting: 964 if (fRec.fFlags & SkScalerContext::kForceAutohinting_Flag) { 965 loadFlags = FT_LOAD_FORCE_AUTOHINT; 966 break; 967 } 968 loadFlags = FT_LOAD_TARGET_NORMAL; 969 if (isLCD(fRec)) { 970 if (fLCDIsVert) { 971 loadFlags = FT_LOAD_TARGET_LCD_V; 972 } else { 973 loadFlags = FT_LOAD_TARGET_LCD; 974 } 975 } 976 break; 977 default: 978 SkDebugf("---------- UNKNOWN hinting %d\n", fRec.getHinting()); 979 break; 980 } 981 } 982 983 if ((fRec.fFlags & SkScalerContext::kEmbeddedBitmapText_Flag) == 0) { 984 loadFlags |= FT_LOAD_NO_BITMAP; 985 } 986 987 // Always using FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH to get correct 988 // advances, as fontconfig and cairo do. 989 // See http://code.google.com/p/skia/issues/detail?id=222. 990 loadFlags |= FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH; 991 992 // Use vertical layout if requested. 993 if (fRec.fFlags & SkScalerContext::kVertical_Flag) { 994 loadFlags |= FT_LOAD_VERTICAL_LAYOUT; 995 } 996 997 loadFlags |= FT_LOAD_COLOR; 998 999 fLoadGlyphFlags = loadFlags; 1000 } 1001 1002 FT_Error err = FT_New_Size(fFace, &fFTSize); 1003 if (err != 0) { 1004 SkDEBUGF(("FT_New_Size returned %x for face %s\n", err, fFace->family_name)); 1005 fFace = NULL; 1006 return; 1007 } 1008 err = FT_Activate_Size(fFTSize); 1009 if (err != 0) { 1010 SkDEBUGF(("FT_Activate_Size(%08x, 0x%x, 0x%x) returned 0x%x\n", fFace, fScaleX, fScaleY, 1011 err)); 1012 fFTSize = NULL; 1013 return; 1014 } 1015 1016 if (FT_IS_SCALABLE(fFace)) { 1017 err = FT_Set_Char_Size(fFace, SkFixedToFDot6(fScaleX), SkFixedToFDot6(fScaleY), 72, 72); 1018 if (err != 0) { 1019 SkDEBUGF(("FT_Set_CharSize(%08x, 0x%x, 0x%x) returned 0x%x\n", 1020 fFace, fScaleX, fScaleY, err)); 1021 fFace = NULL; 1022 return; 1023 } 1024 FT_Set_Transform(fFace, &fMatrix22, NULL); 1025 } else if (FT_HAS_FIXED_SIZES(fFace)) { 1026 fStrikeIndex = chooseBitmapStrike(fFace, fScaleY); 1027 if (fStrikeIndex == -1) { 1028 SkDEBUGF(("no glyphs for font \"%s\" size %f?\n", 1029 fFace->family_name, SkFixedToScalar(fScaleY))); 1030 } else { 1031 // FreeType does no provide linear metrics for bitmap fonts. 1032 linearMetrics = false; 1033 1034 // FreeType documentation says: 1035 // FT_LOAD_NO_BITMAP -- Ignore bitmap strikes when loading. 1036 // Bitmap-only fonts ignore this flag. 1037 // 1038 // However, in FreeType 2.5.1 color bitmap only fonts do not ignore this flag. 1039 // Force this flag off for bitmap only fonts. 1040 fLoadGlyphFlags &= ~FT_LOAD_NO_BITMAP; 1041 } 1042 } else { 1043 SkDEBUGF(("unknown kind of font \"%s\" size %f?\n", 1044 fFace->family_name, SkFixedToScalar(fScaleY))); 1045 } 1046 1047 fDoLinearMetrics = linearMetrics; 1048 } 1049 1050 SkScalerContext_FreeType::~SkScalerContext_FreeType() { 1051 SkAutoMutexAcquire ac(gFTMutex); 1052 1053 if (fFTSize != NULL) { 1054 FT_Done_Size(fFTSize); 1055 } 1056 1057 if (fFace != NULL) { 1058 unref_ft_face(fFace); 1059 } 1060 if (--gFTCount == 0) { 1061 FT_Done_FreeType(gFTLibrary); 1062 SkDEBUGCODE(gFTLibrary = NULL;) 1063 } 1064 } 1065 1066 /* We call this before each use of the fFace, since we may be sharing 1067 this face with other context (at different sizes). 1068 */ 1069 FT_Error SkScalerContext_FreeType::setupSize() { 1070 FT_Error err = FT_Activate_Size(fFTSize); 1071 if (err != 0) { 1072 SkDEBUGF(("SkScalerContext_FreeType::FT_Activate_Size(%x, 0x%x, 0x%x) returned 0x%x\n", 1073 fFaceRec->fFontID, fScaleX, fScaleY, err)); 1074 fFTSize = NULL; 1075 return err; 1076 } 1077 1078 // seems we need to reset this every time (not sure why, but without it 1079 // I get random italics from some other fFTSize) 1080 FT_Set_Transform(fFace, &fMatrix22, NULL); 1081 return 0; 1082 } 1083 1084 unsigned SkScalerContext_FreeType::generateGlyphCount() { 1085 return fFace->num_glyphs; 1086 } 1087 1088 uint16_t SkScalerContext_FreeType::generateCharToGlyph(SkUnichar uni) { 1089 return SkToU16(FT_Get_Char_Index( fFace, uni )); 1090 } 1091 1092 SkUnichar SkScalerContext_FreeType::generateGlyphToChar(uint16_t glyph) { 1093 // iterate through each cmap entry, looking for matching glyph indices 1094 FT_UInt glyphIndex; 1095 SkUnichar charCode = FT_Get_First_Char( fFace, &glyphIndex ); 1096 1097 while (glyphIndex != 0) { 1098 if (glyphIndex == glyph) { 1099 return charCode; 1100 } 1101 charCode = FT_Get_Next_Char( fFace, charCode, &glyphIndex ); 1102 } 1103 1104 return 0; 1105 } 1106 1107 void SkScalerContext_FreeType::generateAdvance(SkGlyph* glyph) { 1108 #ifdef FT_ADVANCES_H 1109 /* unhinted and light hinted text have linearly scaled advances 1110 * which are very cheap to compute with some font formats... 1111 */ 1112 if (fDoLinearMetrics) { 1113 SkAutoMutexAcquire ac(gFTMutex); 1114 1115 if (this->setupSize()) { 1116 glyph->zeroMetrics(); 1117 return; 1118 } 1119 1120 FT_Error error; 1121 FT_Fixed advance; 1122 1123 error = FT_Get_Advance( fFace, glyph->getGlyphID(fBaseGlyphCount), 1124 fLoadGlyphFlags | FT_ADVANCE_FLAG_FAST_ONLY, 1125 &advance ); 1126 if (0 == error) { 1127 glyph->fRsbDelta = 0; 1128 glyph->fLsbDelta = 0; 1129 glyph->fAdvanceX = SkFixedMul(fMatrix22.xx, advance); 1130 glyph->fAdvanceY = - SkFixedMul(fMatrix22.yx, advance); 1131 return; 1132 } 1133 } 1134 #endif /* FT_ADVANCES_H */ 1135 /* otherwise, we need to load/hint the glyph, which is slower */ 1136 this->generateMetrics(glyph); 1137 return; 1138 } 1139 1140 void SkScalerContext_FreeType::getBBoxForCurrentGlyph(SkGlyph* glyph, 1141 FT_BBox* bbox, 1142 bool snapToPixelBoundary) { 1143 1144 FT_Outline_Get_CBox(&fFace->glyph->outline, bbox); 1145 1146 if (fRec.fFlags & SkScalerContext::kSubpixelPositioning_Flag) { 1147 int dx = SkFixedToFDot6(glyph->getSubXFixed()); 1148 int dy = SkFixedToFDot6(glyph->getSubYFixed()); 1149 // negate dy since freetype-y-goes-up and skia-y-goes-down 1150 bbox->xMin += dx; 1151 bbox->yMin -= dy; 1152 bbox->xMax += dx; 1153 bbox->yMax -= dy; 1154 } 1155 1156 // outset the box to integral boundaries 1157 if (snapToPixelBoundary) { 1158 bbox->xMin &= ~63; 1159 bbox->yMin &= ~63; 1160 bbox->xMax = (bbox->xMax + 63) & ~63; 1161 bbox->yMax = (bbox->yMax + 63) & ~63; 1162 } 1163 1164 // Must come after snapToPixelBoundary so that the width and height are 1165 // consistent. Otherwise asserts will fire later on when generating the 1166 // glyph image. 1167 if (fRec.fFlags & SkScalerContext::kVertical_Flag) { 1168 FT_Vector vector; 1169 vector.x = fFace->glyph->metrics.vertBearingX - fFace->glyph->metrics.horiBearingX; 1170 vector.y = -fFace->glyph->metrics.vertBearingY - fFace->glyph->metrics.horiBearingY; 1171 FT_Vector_Transform(&vector, &fMatrix22); 1172 bbox->xMin += vector.x; 1173 bbox->xMax += vector.x; 1174 bbox->yMin += vector.y; 1175 bbox->yMax += vector.y; 1176 } 1177 } 1178 1179 bool SkScalerContext_FreeType::getCBoxForLetter(char letter, FT_BBox* bbox) { 1180 const FT_UInt glyph_id = FT_Get_Char_Index(fFace, letter); 1181 if (!glyph_id) 1182 return false; 1183 if (FT_Load_Glyph(fFace, glyph_id, fLoadGlyphFlags) != 0) 1184 return false; 1185 emboldenIfNeeded(fFace, fFace->glyph); 1186 FT_Outline_Get_CBox(&fFace->glyph->outline, bbox); 1187 return true; 1188 } 1189 1190 void SkScalerContext_FreeType::updateGlyphIfLCD(SkGlyph* glyph) { 1191 if (isLCD(fRec)) { 1192 if (fLCDIsVert) { 1193 glyph->fHeight += gLCDExtra; 1194 glyph->fTop -= gLCDExtra >> 1; 1195 } else { 1196 glyph->fWidth += gLCDExtra; 1197 glyph->fLeft -= gLCDExtra >> 1; 1198 } 1199 } 1200 } 1201 1202 inline void scaleGlyphMetrics(SkGlyph& glyph, SkScalar scale) { 1203 glyph.fWidth *= scale; 1204 glyph.fHeight *= scale; 1205 glyph.fTop *= scale; 1206 glyph.fLeft *= scale; 1207 1208 SkFixed fixedScale = SkScalarToFixed(scale); 1209 glyph.fAdvanceX = SkFixedMul(glyph.fAdvanceX, fixedScale); 1210 glyph.fAdvanceY = SkFixedMul(glyph.fAdvanceY, fixedScale); 1211 } 1212 1213 void SkScalerContext_FreeType::generateMetrics(SkGlyph* glyph) { 1214 SkAutoMutexAcquire ac(gFTMutex); 1215 1216 glyph->fRsbDelta = 0; 1217 glyph->fLsbDelta = 0; 1218 1219 FT_Error err; 1220 1221 if (this->setupSize()) { 1222 goto ERROR; 1223 } 1224 1225 err = FT_Load_Glyph( fFace, glyph->getGlyphID(fBaseGlyphCount), fLoadGlyphFlags ); 1226 if (err != 0) { 1227 #if 0 1228 SkDEBUGF(("SkScalerContext_FreeType::generateMetrics(%x): FT_Load_Glyph(glyph:%d flags:%x) returned 0x%x\n", 1229 fFaceRec->fFontID, glyph->getGlyphID(fBaseGlyphCount), fLoadGlyphFlags, err)); 1230 #endif 1231 ERROR: 1232 glyph->zeroMetrics(); 1233 return; 1234 } 1235 emboldenIfNeeded(fFace, fFace->glyph); 1236 1237 switch ( fFace->glyph->format ) { 1238 case FT_GLYPH_FORMAT_OUTLINE: 1239 if (0 == fFace->glyph->outline.n_contours) { 1240 glyph->fWidth = 0; 1241 glyph->fHeight = 0; 1242 glyph->fTop = 0; 1243 glyph->fLeft = 0; 1244 } else { 1245 FT_BBox bbox; 1246 getBBoxForCurrentGlyph(glyph, &bbox, true); 1247 1248 glyph->fWidth = SkToU16(SkFDot6Floor(bbox.xMax - bbox.xMin)); 1249 glyph->fHeight = SkToU16(SkFDot6Floor(bbox.yMax - bbox.yMin)); 1250 glyph->fTop = -SkToS16(SkFDot6Floor(bbox.yMax)); 1251 glyph->fLeft = SkToS16(SkFDot6Floor(bbox.xMin)); 1252 1253 updateGlyphIfLCD(glyph); 1254 } 1255 break; 1256 1257 case FT_GLYPH_FORMAT_BITMAP: 1258 if (fRec.fFlags & SkScalerContext::kVertical_Flag) { 1259 FT_Vector vector; 1260 vector.x = fFace->glyph->metrics.vertBearingX - fFace->glyph->metrics.horiBearingX; 1261 vector.y = -fFace->glyph->metrics.vertBearingY - fFace->glyph->metrics.horiBearingY; 1262 FT_Vector_Transform(&vector, &fMatrix22); 1263 fFace->glyph->bitmap_left += SkFDot6Floor(vector.x); 1264 fFace->glyph->bitmap_top += SkFDot6Floor(vector.y); 1265 } 1266 1267 if (fFace->glyph->bitmap.pixel_mode == FT_PIXEL_MODE_BGRA) { 1268 glyph->fMaskFormat = SkMask::kARGB32_Format; 1269 } 1270 1271 glyph->fWidth = SkToU16(fFace->glyph->bitmap.width); 1272 glyph->fHeight = SkToU16(fFace->glyph->bitmap.rows); 1273 glyph->fTop = -SkToS16(fFace->glyph->bitmap_top); 1274 glyph->fLeft = SkToS16(fFace->glyph->bitmap_left); 1275 break; 1276 1277 default: 1278 SkDEBUGFAIL("unknown glyph format"); 1279 goto ERROR; 1280 } 1281 1282 if (fRec.fFlags & SkScalerContext::kVertical_Flag) { 1283 if (fDoLinearMetrics) { 1284 glyph->fAdvanceX = -SkFixedMul(fMatrix22.xy, fFace->glyph->linearVertAdvance); 1285 glyph->fAdvanceY = SkFixedMul(fMatrix22.yy, fFace->glyph->linearVertAdvance); 1286 } else { 1287 glyph->fAdvanceX = -SkFDot6ToFixed(fFace->glyph->advance.x); 1288 glyph->fAdvanceY = SkFDot6ToFixed(fFace->glyph->advance.y); 1289 } 1290 } else { 1291 if (fDoLinearMetrics) { 1292 glyph->fAdvanceX = SkFixedMul(fMatrix22.xx, fFace->glyph->linearHoriAdvance); 1293 glyph->fAdvanceY = -SkFixedMul(fMatrix22.yx, fFace->glyph->linearHoriAdvance); 1294 } else { 1295 glyph->fAdvanceX = SkFDot6ToFixed(fFace->glyph->advance.x); 1296 glyph->fAdvanceY = -SkFDot6ToFixed(fFace->glyph->advance.y); 1297 1298 if (fRec.fFlags & kDevKernText_Flag) { 1299 glyph->fRsbDelta = SkToS8(fFace->glyph->rsb_delta); 1300 glyph->fLsbDelta = SkToS8(fFace->glyph->lsb_delta); 1301 } 1302 } 1303 } 1304 1305 if (fFace->glyph->format == FT_GLYPH_FORMAT_BITMAP && fScaleY && fFace->size->metrics.y_ppem) { 1306 // NOTE: both dimensions are scaled by y_ppem. this is WAI. 1307 scaleGlyphMetrics(*glyph, SkScalarDiv(SkFixedToScalar(fScaleY), 1308 SkIntToScalar(fFace->size->metrics.y_ppem))); 1309 } 1310 1311 #ifdef ENABLE_GLYPH_SPEW 1312 SkDEBUGF(("FT_Set_Char_Size(this:%p sx:%x sy:%x ", this, fScaleX, fScaleY)); 1313 SkDEBUGF(("Metrics(glyph:%d flags:0x%x) w:%d\n", glyph->getGlyphID(fBaseGlyphCount), fLoadGlyphFlags, glyph->fWidth)); 1314 #endif 1315 } 1316 1317 1318 void SkScalerContext_FreeType::generateImage(const SkGlyph& glyph) { 1319 SkAutoMutexAcquire ac(gFTMutex); 1320 1321 FT_Error err; 1322 1323 if (this->setupSize()) { 1324 goto ERROR; 1325 } 1326 1327 err = FT_Load_Glyph( fFace, glyph.getGlyphID(fBaseGlyphCount), fLoadGlyphFlags); 1328 if (err != 0) { 1329 SkDEBUGF(("SkScalerContext_FreeType::generateImage: FT_Load_Glyph(glyph:%d width:%d height:%d rb:%d flags:%d) returned 0x%x\n", 1330 glyph.getGlyphID(fBaseGlyphCount), glyph.fWidth, glyph.fHeight, glyph.rowBytes(), fLoadGlyphFlags, err)); 1331 ERROR: 1332 memset(glyph.fImage, 0, glyph.rowBytes() * glyph.fHeight); 1333 return; 1334 } 1335 1336 emboldenIfNeeded(fFace, fFace->glyph); 1337 generateGlyphImage(fFace, glyph); 1338 } 1339 1340 1341 void SkScalerContext_FreeType::generatePath(const SkGlyph& glyph, 1342 SkPath* path) { 1343 SkAutoMutexAcquire ac(gFTMutex); 1344 1345 SkASSERT(&glyph && path); 1346 1347 if (this->setupSize()) { 1348 path->reset(); 1349 return; 1350 } 1351 1352 uint32_t flags = fLoadGlyphFlags; 1353 flags |= FT_LOAD_NO_BITMAP; // ignore embedded bitmaps so we're sure to get the outline 1354 flags &= ~FT_LOAD_RENDER; // don't scan convert (we just want the outline) 1355 1356 FT_Error err = FT_Load_Glyph( fFace, glyph.getGlyphID(fBaseGlyphCount), flags); 1357 1358 if (err != 0) { 1359 SkDEBUGF(("SkScalerContext_FreeType::generatePath: FT_Load_Glyph(glyph:%d flags:%d) returned 0x%x\n", 1360 glyph.getGlyphID(fBaseGlyphCount), flags, err)); 1361 path->reset(); 1362 return; 1363 } 1364 emboldenIfNeeded(fFace, fFace->glyph); 1365 1366 generateGlyphPath(fFace, path); 1367 1368 // The path's origin from FreeType is always the horizontal layout origin. 1369 // Offset the path so that it is relative to the vertical origin if needed. 1370 if (fRec.fFlags & SkScalerContext::kVertical_Flag) { 1371 FT_Vector vector; 1372 vector.x = fFace->glyph->metrics.vertBearingX - fFace->glyph->metrics.horiBearingX; 1373 vector.y = -fFace->glyph->metrics.vertBearingY - fFace->glyph->metrics.horiBearingY; 1374 FT_Vector_Transform(&vector, &fMatrix22); 1375 path->offset(SkFDot6ToScalar(vector.x), -SkFDot6ToScalar(vector.y)); 1376 } 1377 } 1378 1379 void SkScalerContext_FreeType::generateFontMetrics(SkPaint::FontMetrics* mx, 1380 SkPaint::FontMetrics* my) { 1381 if (NULL == mx && NULL == my) { 1382 return; 1383 } 1384 1385 SkAutoMutexAcquire ac(gFTMutex); 1386 1387 if (this->setupSize()) { 1388 ERROR: 1389 if (mx) { 1390 sk_bzero(mx, sizeof(SkPaint::FontMetrics)); 1391 } 1392 if (my) { 1393 sk_bzero(my, sizeof(SkPaint::FontMetrics)); 1394 } 1395 return; 1396 } 1397 1398 FT_Face face = fFace; 1399 SkScalar scaleX = fScale.x(); 1400 SkScalar scaleY = fScale.y(); 1401 SkScalar mxy = fMatrix22Scalar.getSkewX() * scaleY; 1402 SkScalar myy = fMatrix22Scalar.getScaleY() * scaleY; 1403 1404 // fetch units/EM from "head" table if needed (ie for bitmap fonts) 1405 SkScalar upem = SkIntToScalar(face->units_per_EM); 1406 if (!upem) { 1407 TT_Header* ttHeader = (TT_Header*)FT_Get_Sfnt_Table(face, ft_sfnt_head); 1408 if (ttHeader) { 1409 upem = SkIntToScalar(ttHeader->Units_Per_EM); 1410 } 1411 } 1412 1413 // use the os/2 table as a source of reasonable defaults. 1414 SkScalar x_height = 0.0f; 1415 SkScalar avgCharWidth = 0.0f; 1416 SkScalar cap_height = 0.0f; 1417 TT_OS2* os2 = (TT_OS2*) FT_Get_Sfnt_Table(face, ft_sfnt_os2); 1418 if (os2) { 1419 x_height = scaleX * SkIntToScalar(os2->sxHeight) / upem; 1420 avgCharWidth = SkIntToScalar(os2->xAvgCharWidth) / upem; 1421 if (os2->version != 0xFFFF && os2->version >= 2) { 1422 cap_height = scaleX * SkIntToScalar(os2->sCapHeight) / upem; 1423 } 1424 } 1425 1426 // pull from format-specific metrics as needed 1427 SkScalar ascent, descent, leading, xmin, xmax, ymin, ymax; 1428 SkScalar underlineThickness, underlinePosition; 1429 if (face->face_flags & FT_FACE_FLAG_SCALABLE) { // scalable outline font 1430 ascent = -SkIntToScalar(face->ascender) / upem; 1431 descent = -SkIntToScalar(face->descender) / upem; 1432 leading = SkIntToScalar(face->height + (face->descender - face->ascender)) / upem; 1433 xmin = SkIntToScalar(face->bbox.xMin) / upem; 1434 xmax = SkIntToScalar(face->bbox.xMax) / upem; 1435 ymin = -SkIntToScalar(face->bbox.yMin) / upem; 1436 ymax = -SkIntToScalar(face->bbox.yMax) / upem; 1437 underlineThickness = SkIntToScalar(face->underline_thickness) / upem; 1438 underlinePosition = -SkIntToScalar(face->underline_position + 1439 face->underline_thickness / 2) / upem; 1440 1441 if(mx) { 1442 mx->fFlags |= SkPaint::FontMetrics::kUnderlineThinknessIsValid_Flag; 1443 mx->fFlags |= SkPaint::FontMetrics::kUnderlinePositionIsValid_Flag; 1444 } 1445 if(my){ 1446 my->fFlags |= SkPaint::FontMetrics::kUnderlineThinknessIsValid_Flag; 1447 my->fFlags |= SkPaint::FontMetrics::kUnderlinePositionIsValid_Flag; 1448 } 1449 // we may be able to synthesize x_height and cap_height from outline 1450 if (!x_height) { 1451 FT_BBox bbox; 1452 if (getCBoxForLetter('x', &bbox)) { 1453 x_height = SkIntToScalar(bbox.yMax) / 64.0f; 1454 } 1455 } 1456 if (!cap_height) { 1457 FT_BBox bbox; 1458 if (getCBoxForLetter('H', &bbox)) { 1459 cap_height = SkIntToScalar(bbox.yMax) / 64.0f; 1460 } 1461 } 1462 } else if (fStrikeIndex != -1) { // bitmap strike metrics 1463 SkScalar xppem = SkIntToScalar(face->size->metrics.x_ppem); 1464 SkScalar yppem = SkIntToScalar(face->size->metrics.y_ppem); 1465 ascent = -SkIntToScalar(face->size->metrics.ascender) / (yppem * 64.0f); 1466 descent = -SkIntToScalar(face->size->metrics.descender) / (yppem * 64.0f); 1467 leading = (SkIntToScalar(face->size->metrics.height) / (yppem * 64.0f)) 1468 + ascent - descent; 1469 xmin = 0.0f; 1470 xmax = SkIntToScalar(face->available_sizes[fStrikeIndex].width) / xppem; 1471 ymin = descent + leading; 1472 ymax = ascent - descent; 1473 underlineThickness = 0; 1474 underlinePosition = 0; 1475 1476 if(mx) { 1477 mx->fFlags &= ~SkPaint::FontMetrics::kUnderlineThinknessIsValid_Flag; 1478 mx->fFlags &= ~SkPaint::FontMetrics::kUnderlinePositionIsValid_Flag; 1479 } 1480 if(my){ 1481 my->fFlags &= ~SkPaint::FontMetrics::kUnderlineThinknessIsValid_Flag; 1482 my->fFlags &= ~SkPaint::FontMetrics::kUnderlinePositionIsValid_Flag; 1483 } 1484 } else { 1485 goto ERROR; 1486 } 1487 1488 // synthesize elements that were not provided by the os/2 table or format-specific metrics 1489 if (!x_height) { 1490 x_height = -ascent; 1491 } 1492 if (!avgCharWidth) { 1493 avgCharWidth = xmax - xmin; 1494 } 1495 if (!cap_height) { 1496 cap_height = -ascent; 1497 } 1498 1499 // disallow negative linespacing 1500 if (leading < 0.0f) { 1501 leading = 0.0f; 1502 } 1503 1504 if (mx) { 1505 mx->fTop = ymax * mxy; 1506 mx->fAscent = ascent * mxy; 1507 mx->fDescent = descent * mxy; 1508 mx->fBottom = ymin * mxy; 1509 mx->fLeading = leading * mxy; 1510 mx->fAvgCharWidth = avgCharWidth * mxy; 1511 mx->fXMin = xmin; 1512 mx->fXMax = xmax; 1513 mx->fXHeight = x_height; 1514 mx->fCapHeight = cap_height; 1515 mx->fUnderlineThickness = underlineThickness * mxy; 1516 mx->fUnderlinePosition = underlinePosition * mxy; 1517 } 1518 if (my) { 1519 my->fTop = ymax * myy; 1520 my->fAscent = ascent * myy; 1521 my->fDescent = descent * myy; 1522 my->fBottom = ymin * myy; 1523 my->fLeading = leading * myy; 1524 my->fAvgCharWidth = avgCharWidth * myy; 1525 my->fXMin = xmin; 1526 my->fXMax = xmax; 1527 my->fXHeight = x_height; 1528 my->fCapHeight = cap_height; 1529 my->fUnderlineThickness = underlineThickness * myy; 1530 my->fUnderlinePosition = underlinePosition * myy; 1531 } 1532 } 1533 1534 /////////////////////////////////////////////////////////////////////////////// 1535 1536 // hand-tuned value to reduce outline embolden strength 1537 #ifndef SK_OUTLINE_EMBOLDEN_DIVISOR 1538 #ifdef SK_BUILD_FOR_ANDROID 1539 #define SK_OUTLINE_EMBOLDEN_DIVISOR 34 1540 #else 1541 #define SK_OUTLINE_EMBOLDEN_DIVISOR 24 1542 #endif 1543 #endif 1544 1545 /////////////////////////////////////////////////////////////////////////////// 1546 1547 void SkScalerContext_FreeType::emboldenIfNeeded(FT_Face face, FT_GlyphSlot glyph) 1548 { 1549 // check to see if the embolden bit is set 1550 if (0 == (fRec.fFlags & SkScalerContext::kEmbolden_Flag)) { 1551 return; 1552 } 1553 1554 #if defined(SK_BUILD_FOR_ANDROID_FRAMEWORK) 1555 // Android doesn't want to embolden a font that is already bold. 1556 if ((fFace->style_flags & FT_STYLE_FLAG_BOLD)) { 1557 return; 1558 } 1559 #endif 1560 1561 switch (glyph->format) { 1562 case FT_GLYPH_FORMAT_OUTLINE: 1563 FT_Pos strength; 1564 strength = FT_MulFix(face->units_per_EM, face->size->metrics.y_scale) 1565 / SK_OUTLINE_EMBOLDEN_DIVISOR; 1566 FT_Outline_Embolden(&glyph->outline, strength); 1567 break; 1568 case FT_GLYPH_FORMAT_BITMAP: 1569 FT_GlyphSlot_Own_Bitmap(glyph); 1570 FT_Bitmap_Embolden(glyph->library, &glyph->bitmap, kBitmapEmboldenStrength, 0); 1571 break; 1572 default: 1573 SkDEBUGFAIL("unknown glyph format"); 1574 } 1575 } 1576 1577 /////////////////////////////////////////////////////////////////////////////// 1578 1579 #include "SkUtils.h" 1580 1581 static SkUnichar next_utf8(const void** chars) { 1582 return SkUTF8_NextUnichar((const char**)chars); 1583 } 1584 1585 static SkUnichar next_utf16(const void** chars) { 1586 return SkUTF16_NextUnichar((const uint16_t**)chars); 1587 } 1588 1589 static SkUnichar next_utf32(const void** chars) { 1590 const SkUnichar** uniChars = (const SkUnichar**)chars; 1591 SkUnichar uni = **uniChars; 1592 *uniChars += 1; 1593 return uni; 1594 } 1595 1596 typedef SkUnichar (*EncodingProc)(const void**); 1597 1598 static EncodingProc find_encoding_proc(SkTypeface::Encoding enc) { 1599 static const EncodingProc gProcs[] = { 1600 next_utf8, next_utf16, next_utf32 1601 }; 1602 SkASSERT((size_t)enc < SK_ARRAY_COUNT(gProcs)); 1603 return gProcs[enc]; 1604 } 1605 1606 int SkTypeface_FreeType::onCharsToGlyphs(const void* chars, Encoding encoding, 1607 uint16_t glyphs[], int glyphCount) const { 1608 AutoFTAccess fta(this); 1609 FT_Face face = fta.face(); 1610 if (!face) { 1611 if (glyphs) { 1612 sk_bzero(glyphs, glyphCount * sizeof(glyphs[0])); 1613 } 1614 return 0; 1615 } 1616 1617 EncodingProc next_uni_proc = find_encoding_proc(encoding); 1618 1619 if (NULL == glyphs) { 1620 for (int i = 0; i < glyphCount; ++i) { 1621 if (0 == FT_Get_Char_Index(face, next_uni_proc(&chars))) { 1622 return i; 1623 } 1624 } 1625 return glyphCount; 1626 } else { 1627 int first = glyphCount; 1628 for (int i = 0; i < glyphCount; ++i) { 1629 unsigned id = FT_Get_Char_Index(face, next_uni_proc(&chars)); 1630 glyphs[i] = SkToU16(id); 1631 if (0 == id && i < first) { 1632 first = i; 1633 } 1634 } 1635 return first; 1636 } 1637 } 1638 1639 int SkTypeface_FreeType::onCountGlyphs() const { 1640 // we cache this value, using -1 as a sentinel for "not computed" 1641 if (fGlyphCount < 0) { 1642 AutoFTAccess fta(this); 1643 FT_Face face = fta.face(); 1644 // if the face failed, we still assign a non-negative value 1645 fGlyphCount = face ? face->num_glyphs : 0; 1646 } 1647 return fGlyphCount; 1648 } 1649 1650 SkTypeface::LocalizedStrings* SkTypeface_FreeType::onCreateFamilyNameIterator() const { 1651 SkTypeface::LocalizedStrings* nameIter = 1652 SkOTUtils::LocalizedStrings_NameTable::CreateForFamilyNames(*this); 1653 if (NULL == nameIter) { 1654 SkString familyName; 1655 this->getFamilyName(&familyName); 1656 SkString language("und"); //undetermined 1657 nameIter = new SkOTUtils::LocalizedStrings_SingleName(familyName, language); 1658 } 1659 return nameIter; 1660 } 1661 1662 int SkTypeface_FreeType::onGetTableTags(SkFontTableTag tags[]) const { 1663 AutoFTAccess fta(this); 1664 FT_Face face = fta.face(); 1665 1666 FT_ULong tableCount = 0; 1667 FT_Error error; 1668 1669 // When 'tag' is NULL, returns number of tables in 'length'. 1670 error = FT_Sfnt_Table_Info(face, 0, NULL, &tableCount); 1671 if (error) { 1672 return 0; 1673 } 1674 1675 if (tags) { 1676 for (FT_ULong tableIndex = 0; tableIndex < tableCount; ++tableIndex) { 1677 FT_ULong tableTag; 1678 FT_ULong tablelength; 1679 error = FT_Sfnt_Table_Info(face, tableIndex, &tableTag, &tablelength); 1680 if (error) { 1681 return 0; 1682 } 1683 tags[tableIndex] = static_cast<SkFontTableTag>(tableTag); 1684 } 1685 } 1686 return tableCount; 1687 } 1688 1689 size_t SkTypeface_FreeType::onGetTableData(SkFontTableTag tag, size_t offset, 1690 size_t length, void* data) const 1691 { 1692 AutoFTAccess fta(this); 1693 FT_Face face = fta.face(); 1694 1695 FT_ULong tableLength = 0; 1696 FT_Error error; 1697 1698 // When 'length' is 0 it is overwritten with the full table length; 'offset' is ignored. 1699 error = FT_Load_Sfnt_Table(face, tag, 0, NULL, &tableLength); 1700 if (error) { 1701 return 0; 1702 } 1703 1704 if (offset > tableLength) { 1705 return 0; 1706 } 1707 FT_ULong size = SkTMin((FT_ULong)length, tableLength - (FT_ULong)offset); 1708 if (NULL != data) { 1709 error = FT_Load_Sfnt_Table(face, tag, offset, reinterpret_cast<FT_Byte*>(data), &size); 1710 if (error) { 1711 return 0; 1712 } 1713 } 1714 1715 return size; 1716 } 1717 1718 /////////////////////////////////////////////////////////////////////////////// 1719 /////////////////////////////////////////////////////////////////////////////// 1720 1721 /* Export this so that other parts of our FonttHost port can make use of our 1722 ability to extract the name+style from a stream, using FreeType's api. 1723 */ 1724 bool find_name_and_attributes(SkStream* stream, SkString* name, 1725 SkTypeface::Style* style, bool* isFixedPitch) { 1726 FT_Library library; 1727 if (FT_Init_FreeType(&library)) { 1728 return false; 1729 } 1730 1731 FT_Open_Args args; 1732 memset(&args, 0, sizeof(args)); 1733 1734 const void* memoryBase = stream->getMemoryBase(); 1735 FT_StreamRec streamRec; 1736 1737 if (NULL != memoryBase) { 1738 args.flags = FT_OPEN_MEMORY; 1739 args.memory_base = (const FT_Byte*)memoryBase; 1740 args.memory_size = stream->getLength(); 1741 } else { 1742 memset(&streamRec, 0, sizeof(streamRec)); 1743 streamRec.size = stream->getLength(); 1744 streamRec.descriptor.pointer = stream; 1745 streamRec.read = sk_stream_read; 1746 streamRec.close = sk_stream_close; 1747 1748 args.flags = FT_OPEN_STREAM; 1749 args.stream = &streamRec; 1750 } 1751 1752 FT_Face face; 1753 if (FT_Open_Face(library, &args, 0, &face)) { 1754 FT_Done_FreeType(library); 1755 return false; 1756 } 1757 1758 int tempStyle = SkTypeface::kNormal; 1759 if (face->style_flags & FT_STYLE_FLAG_BOLD) { 1760 tempStyle |= SkTypeface::kBold; 1761 } 1762 if (face->style_flags & FT_STYLE_FLAG_ITALIC) { 1763 tempStyle |= SkTypeface::kItalic; 1764 } 1765 1766 if (name) { 1767 name->set(face->family_name); 1768 } 1769 if (style) { 1770 *style = (SkTypeface::Style) tempStyle; 1771 } 1772 if (isFixedPitch) { 1773 *isFixedPitch = FT_IS_FIXED_WIDTH(face); 1774 } 1775 1776 FT_Done_Face(face); 1777 FT_Done_FreeType(library); 1778 return true; 1779 } 1780