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