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

Side by Side Diff: src/ports/SkFontHost_FreeType.cpp

Issue 748063003: Clean up FreeType code for 2.3.8. (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Address comments. Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « gyp/freetype.gyp ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 1
2 /* 2 /*
3 * Copyright 2006 The Android Open Source Project 3 * Copyright 2006 The Android Open Source Project
4 * 4 *
5 * Use of this source code is governed by a BSD-style license that can be 5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file. 6 * found in the LICENSE file.
7 */ 7 */
8 8
9 #include "SkAdvancedTypefaceMetrics.h" 9 #include "SkAdvancedTypefaceMetrics.h"
10 #include "SkBitmap.h" 10 #include "SkBitmap.h"
(...skipping 13 matching lines...) Expand all
24 #include "SkScalerContext.h" 24 #include "SkScalerContext.h"
25 #include "SkStream.h" 25 #include "SkStream.h"
26 #include "SkString.h" 26 #include "SkString.h"
27 #include "SkTemplates.h" 27 #include "SkTemplates.h"
28 #include "SkThread.h" 28 #include "SkThread.h"
29 29
30 #if defined(SK_CAN_USE_DLOPEN) 30 #if defined(SK_CAN_USE_DLOPEN)
31 #include <dlfcn.h> 31 #include <dlfcn.h>
32 #endif 32 #endif
33 #include <ft2build.h> 33 #include <ft2build.h>
34 #include FT_ADVANCES_H
35 #include FT_BITMAP_H
34 #include FT_FREETYPE_H 36 #include FT_FREETYPE_H
37 #include FT_LCD_FILTER_H
35 #include FT_OUTLINE_H 38 #include FT_OUTLINE_H
36 #include FT_SIZES_H 39 #include FT_SIZES_H
37 #include FT_TRUETYPE_TABLES_H 40 #include FT_TRUETYPE_TABLES_H
38 #include FT_TYPE1_TABLES_H 41 #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 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 43
65 // FT_LOAD_COLOR and the corresponding FT_Pixel_Mode::FT_PIXEL_MODE_BGRA 44 // FT_LOAD_COLOR and the corresponding FT_Pixel_Mode::FT_PIXEL_MODE_BGRA
66 // were introduced in FreeType 2.5.0. 45 // were introduced in FreeType 2.5.0.
67 // The following may be removed once FreeType 2.5.0 is required to build. 46 // The following may be removed once FreeType 2.5.0 is required to build.
68 #ifndef FT_LOAD_COLOR 47 #ifndef FT_LOAD_COLOR
69 # define FT_LOAD_COLOR ( 1L << 20 ) 48 # define FT_LOAD_COLOR ( 1L << 20 )
70 # define FT_PIXEL_MODE_BGRA 7 49 # define FT_PIXEL_MODE_BGRA 7
71 #endif 50 #endif
72 51
73 // FT_HAS_COLOR and the corresponding FT_FACE_FLAG_COLOR 52 // FT_HAS_COLOR and the corresponding FT_FACE_FLAG_COLOR
74 // were introduced in FreeType 2.5.1 53 // were introduced in FreeType 2.5.1
75 // The following may be removed once FreeType 2.5.1 is required to build. 54 // The following may be removed once FreeType 2.5.1 is required to build.
76 #ifndef FT_HAS_COLOR 55 #ifndef FT_HAS_COLOR
77 # define FT_HAS_COLOR(face) false 56 # define FT_HAS_COLOR(face) false
78 #endif 57 #endif
79 58
80 //#define ENABLE_GLYPH_SPEW // for tracing calls 59 //#define ENABLE_GLYPH_SPEW // for tracing calls
81 //#define DUMP_STRIKE_CREATION 60 //#define DUMP_STRIKE_CREATION
82 61 //#define SK_FONTHOST_FREETYPE_USE_NORMAL_LCD_FILTER
62 //#define SK_FONTHOST_FREETYPE_RUNTIME_VERSION
83 //#define SK_GAMMA_APPLY_TO_A8 63 //#define SK_GAMMA_APPLY_TO_A8
84 64
85 using namespace skia_advanced_typeface_metrics_utils; 65 using namespace skia_advanced_typeface_metrics_utils;
86 66
87 static bool isLCD(const SkScalerContext::Rec& rec) { 67 static bool isLCD(const SkScalerContext::Rec& rec) {
88 switch (rec.fMaskFormat) { 68 switch (rec.fMaskFormat) {
89 case SkMask::kLCD16_Format: 69 case SkMask::kLCD16_Format:
90 return true; 70 return true;
91 default: 71 default:
92 return false; 72 return false;
(...skipping 18 matching lines...) Expand all
111 // The following platforms provide FreeType of at least 2.4.0. 91 // The following platforms provide FreeType of at least 2.4.0.
112 // Ubuntu >= 11.04 (previous deprecated April 2013) 92 // Ubuntu >= 11.04 (previous deprecated April 2013)
113 // Debian >= 6.0 (good) 93 // Debian >= 6.0 (good)
114 // OpenSuse >= 11.4 (previous deprecated January 2012 / Nov 2013 for Evergreen 1 1.2) 94 // OpenSuse >= 11.4 (previous deprecated January 2012 / Nov 2013 for Evergreen 1 1.2)
115 // Fedora >= 14 (good) 95 // Fedora >= 14 (good)
116 // Android >= Gingerbread (good) 96 // Android >= Gingerbread (good)
117 typedef FT_Error (*FT_Library_SetLcdFilterWeightsProc)(FT_Library, unsigned char *); 97 typedef FT_Error (*FT_Library_SetLcdFilterWeightsProc)(FT_Library, unsigned char *);
118 98
119 // Caller must lock gFTMutex before calling this function. 99 // Caller must lock gFTMutex before calling this function.
120 static bool InitFreetype() { 100 static bool InitFreetype() {
101 gFTMutex.assertHeld();
102
121 FT_Error err = FT_Init_FreeType(&gFTLibrary); 103 FT_Error err = FT_Init_FreeType(&gFTLibrary);
122 if (err) { 104 if (err) {
123 return false; 105 return false;
124 } 106 }
125 107
126 // Setup LCD filtering. This reduces color fringes for LCD smoothed glyphs. 108 // Setup LCD filtering. This reduces color fringes for LCD smoothed glyphs.
127 #ifdef FT_LCD_FILTER_H
128 // Use default { 0x10, 0x40, 0x70, 0x40, 0x10 }, as it adds up to 0x110, sim ulating ink spread. 109 // Use default { 0x10, 0x40, 0x70, 0x40, 0x10 }, as it adds up to 0x110, sim ulating ink spread.
129 // SetLcdFilter must be called before SetLcdFilterWeights. 110 // SetLcdFilter must be called before SetLcdFilterWeights.
130 err = FT_Library_SetLcdFilter(gFTLibrary, FT_LCD_FILTER_DEFAULT); 111 err = FT_Library_SetLcdFilter(gFTLibrary, FT_LCD_FILTER_DEFAULT);
131 if (0 == err) { 112 if (0 == err) {
132 gLCDSupport = true; 113 gLCDSupport = true;
133 gLCDExtra = 2; //Using a filter adds one full pixel to each side. 114 gLCDExtra = 2; //Using a filter adds one full pixel to each side.
134 115
135 #ifdef SK_FONTHOST_FREETYPE_USE_NORMAL_LCD_FILTER 116 #ifdef SK_FONTHOST_FREETYPE_USE_NORMAL_LCD_FILTER
136 // This also adds to 0x110 simulating ink spread, but provides better re sults than default. 117 // This also adds to 0x110 simulating ink spread, but provides better re sults than default.
137 static unsigned char gGaussianLikeHeavyWeights[] = { 0x1A, 0x43, 0x56, 0 x43, 0x1A, }; 118 static unsigned char gGaussianLikeHeavyWeights[] = { 0x1A, 0x43, 0x56, 0 x43, 0x1A, };
138 119
139 #if defined(SK_FONTHOST_FREETYPE_RUNTIME_VERSION) && \ 120 #if SK_FONTHOST_FREETYPE_RUNTIME_VERSION > 0x020400
140 SK_FONTHOST_FREETYPE_RUNTIME_VERSION > 0x020400
141 err = FT_Library_SetLcdFilterWeights(gFTLibrary, gGaussianLikeHeavyWeigh ts); 121 err = FT_Library_SetLcdFilterWeights(gFTLibrary, gGaussianLikeHeavyWeigh ts);
142 #elif defined(SK_CAN_USE_DLOPEN) && SK_CAN_USE_DLOPEN == 1 122 #elif SK_CAN_USE_DLOPEN == 1
143 //The FreeType library is already loaded, so symbols are available in pr ocess. 123 //The FreeType library is already loaded, so symbols are available in pr ocess.
144 void* self = dlopen(NULL, RTLD_LAZY); 124 void* self = dlopen(NULL, RTLD_LAZY);
145 if (self) { 125 if (self) {
146 FT_Library_SetLcdFilterWeightsProc setLcdFilterWeights; 126 FT_Library_SetLcdFilterWeightsProc setLcdFilterWeights;
147 //The following cast is non-standard, but safe for POSIX. 127 //The following cast is non-standard, but safe for POSIX.
148 *reinterpret_cast<void**>(&setLcdFilterWeights) = dlsym(self, "FT_Li brary_SetLcdFilterWeights"); 128 *reinterpret_cast<void**>(&setLcdFilterWeights) = dlsym(self, "FT_Li brary_SetLcdFilterWeights");
149 dlclose(self); 129 dlclose(self);
150 130
151 if (setLcdFilterWeights) { 131 if (setLcdFilterWeights) {
152 err = setLcdFilterWeights(gFTLibrary, gGaussianLikeHeavyWeights) ; 132 err = setLcdFilterWeights(gFTLibrary, gGaussianLikeHeavyWeights) ;
153 } 133 }
154 } 134 }
155 #endif 135 #endif
156 #endif 136 #endif
157 } 137 }
158 #else
159 gLCDSupport = false;
160 #endif
161 gLCDSupportValid = true; 138 gLCDSupportValid = true;
162 139
163 return true; 140 return true;
164 } 141 }
165 142
166 // Called while holding gFTMutex. 143 // Called while holding gFTMutex.
167 static void determine_lcd_support(bool* lcdSupported) { 144 static void determine_lcd_support(bool* lcdSupported) {
168 if (!gLCDSupportValid) { 145 if (!gLCDSupportValid) {
169 // This will determine LCD support as a side effect. 146 // This will determine LCD support as a side effect.
170 InitFreetype(); 147 InitFreetype();
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after
284 sk_bzero(&fFTStream, sizeof(fFTStream)); 261 sk_bzero(&fFTStream, sizeof(fFTStream));
285 fFTStream.size = fSkStream->getLength(); 262 fFTStream.size = fSkStream->getLength();
286 fFTStream.descriptor.pointer = fSkStream; 263 fFTStream.descriptor.pointer = fSkStream;
287 fFTStream.read = sk_stream_read; 264 fFTStream.read = sk_stream_read;
288 fFTStream.close = sk_stream_close; 265 fFTStream.close = sk_stream_close;
289 } 266 }
290 267
291 // Will return 0 on failure 268 // Will return 0 on failure
292 // Caller must lock gFTMutex before calling this function. 269 // Caller must lock gFTMutex before calling this function.
293 static SkFaceRec* ref_ft_face(const SkTypeface* typeface) { 270 static SkFaceRec* ref_ft_face(const SkTypeface* typeface) {
271 gFTMutex.assertHeld();
272
294 const SkFontID fontID = typeface->uniqueID(); 273 const SkFontID fontID = typeface->uniqueID();
295 SkFaceRec* rec = gFaceRecHead; 274 SkFaceRec* rec = gFaceRecHead;
296 while (rec) { 275 while (rec) {
297 if (rec->fFontID == fontID) { 276 if (rec->fFontID == fontID) {
298 SkASSERT(rec->fFace); 277 SkASSERT(rec->fFace);
299 rec->fRefCnt += 1; 278 rec->fRefCnt += 1;
300 return rec; 279 return rec;
301 } 280 }
302 rec = rec->fNext; 281 rec = rec->fNext;
303 } 282 }
304 283
305 int face_index; 284 int face_index;
306 SkStream* strm = typeface->openStream(&face_index); 285 SkStream* strm = typeface->openStream(&face_index);
307 if (NULL == strm) { 286 if (NULL == strm) {
308 return NULL; 287 return NULL;
309 } 288 }
310 289
311 // this passes ownership of strm to the rec 290 // this passes ownership of strm to the rec
312 rec = SkNEW_ARGS(SkFaceRec, (strm, fontID)); 291 rec = SkNEW_ARGS(SkFaceRec, (strm, fontID));
313 292
314 FT_Open_Args args; 293 FT_Open_Args args;
315 memset(&args, 0, sizeof(args)); 294 memset(&args, 0, sizeof(args));
316 const void* memoryBase = strm->getMemoryBase(); 295 const void* memoryBase = strm->getMemoryBase();
317 296
318 if (memoryBase) { 297 if (memoryBase) {
319 //printf("mmap(%s)\n", keyString.c_str());
320 args.flags = FT_OPEN_MEMORY; 298 args.flags = FT_OPEN_MEMORY;
321 args.memory_base = (const FT_Byte*)memoryBase; 299 args.memory_base = (const FT_Byte*)memoryBase;
322 args.memory_size = strm->getLength(); 300 args.memory_size = strm->getLength();
323 } else { 301 } else {
324 //printf("fopen(%s)\n", keyString.c_str());
325 args.flags = FT_OPEN_STREAM; 302 args.flags = FT_OPEN_STREAM;
326 args.stream = &rec->fFTStream; 303 args.stream = &rec->fFTStream;
327 } 304 }
328 305
329 FT_Error err = FT_Open_Face(gFTLibrary, &args, face_index, &rec->fFace); 306 FT_Error err = FT_Open_Face(gFTLibrary, &args, face_index, &rec->fFace);
330 if (err) { // bad filename, try the default font 307 if (err) { // bad filename, try the default font
331 fprintf(stderr, "ERROR: unable to open font '%x'\n", fontID); 308 SkDEBUGF(("ERROR: unable to open font '%x'\n", fontID));
332 SkDELETE(rec); 309 SkDELETE(rec);
333 return NULL; 310 return NULL;
334 } else { 311 } else {
335 SkASSERT(rec->fFace); 312 SkASSERT(rec->fFace);
336 //fprintf(stderr, "Opened font '%s'\n", filename.c_str());
337 rec->fNext = gFaceRecHead; 313 rec->fNext = gFaceRecHead;
338 gFaceRecHead = rec; 314 gFaceRecHead = rec;
339 return rec; 315 return rec;
340 } 316 }
341 } 317 }
342 318
343 // Caller must lock gFTMutex before calling this function. 319 // Caller must lock gFTMutex before calling this function.
344 static void unref_ft_face(FT_Face face) { 320 static void unref_ft_face(FT_Face face) {
321 gFTMutex.assertHeld();
322
345 SkFaceRec* rec = gFaceRecHead; 323 SkFaceRec* rec = gFaceRecHead;
346 SkFaceRec* prev = NULL; 324 SkFaceRec* prev = NULL;
347 while (rec) { 325 while (rec) {
348 SkFaceRec* next = rec->fNext; 326 SkFaceRec* next = rec->fNext;
349 if (rec->fFace == face) { 327 if (rec->fFace == face) {
350 if (--rec->fRefCnt == 0) { 328 if (--rec->fRefCnt == 0) {
351 if (prev) { 329 if (prev) {
352 prev->fNext = next; 330 prev->fNext = next;
353 } else { 331 } else {
354 gFaceRecHead = next; 332 gFaceRecHead = next;
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
392 SkFaceRec* rec() { return fRec; } 370 SkFaceRec* rec() { return fRec; }
393 FT_Face face() { return fFace; } 371 FT_Face face() { return fFace; }
394 372
395 private: 373 private:
396 SkFaceRec* fRec; 374 SkFaceRec* fRec;
397 FT_Face fFace; 375 FT_Face fFace;
398 }; 376 };
399 377
400 /////////////////////////////////////////////////////////////////////////// 378 ///////////////////////////////////////////////////////////////////////////
401 379
402 // Work around for old versions of freetype.
403 static FT_Error getAdvances(FT_Face face, FT_UInt start, FT_UInt count,
404 FT_Int32 loadFlags, FT_Fixed* advances) {
405 #ifdef FT_ADVANCES_H
406 return FT_Get_Advances(face, start, count, loadFlags, advances);
407 #else
408 if (!face || start >= face->num_glyphs ||
409 start + count > face->num_glyphs || loadFlags != FT_LOAD_NO_SCALE) {
410 return 6; // "Invalid argument."
411 }
412 if (count == 0)
413 return 0;
414
415 for (int i = 0; i < count; i++) {
416 FT_Error err = FT_Load_Glyph(face, start + i, FT_LOAD_NO_SCALE);
417 if (err)
418 return err;
419 advances[i] = face->glyph->advance.x;
420 }
421
422 return 0;
423 #endif
424 }
425
426 static bool canEmbed(FT_Face face) { 380 static bool canEmbed(FT_Face face) {
427 #ifdef FT_FSTYPE_RESTRICTED_LICENSE_EMBEDDING
428 FT_UShort fsType = FT_Get_FSType_Flags(face); 381 FT_UShort fsType = FT_Get_FSType_Flags(face);
429 return (fsType & (FT_FSTYPE_RESTRICTED_LICENSE_EMBEDDING | 382 return (fsType & (FT_FSTYPE_RESTRICTED_LICENSE_EMBEDDING |
430 FT_FSTYPE_BITMAP_EMBEDDING_ONLY)) == 0; 383 FT_FSTYPE_BITMAP_EMBEDDING_ONLY)) == 0;
431 #else
432 // No embedding is 0x2 and bitmap embedding only is 0x200.
433 TT_OS2* os2_table;
434 if ((os2_table = (TT_OS2*)FT_Get_Sfnt_Table(face, ft_sfnt_os2)) != NULL) {
435 return (os2_table->fsType & 0x202) == 0;
436 }
437 return false; // We tried, fail safe.
438 #endif
439 } 384 }
440 385
441 static bool canSubset(FT_Face face) { 386 static bool canSubset(FT_Face face) {
442 #ifdef FT_FSTYPE_NO_SUBSETTING
443 FT_UShort fsType = FT_Get_FSType_Flags(face); 387 FT_UShort fsType = FT_Get_FSType_Flags(face);
444 return (fsType & FT_FSTYPE_NO_SUBSETTING) == 0; 388 return (fsType & FT_FSTYPE_NO_SUBSETTING) == 0;
445 #else
446 // No subset is 0x100.
447 TT_OS2* os2_table;
448 if ((os2_table = (TT_OS2*)FT_Get_Sfnt_Table(face, ft_sfnt_os2)) != NULL) {
449 return (os2_table->fsType & 0x100) == 0;
450 }
451 return false; // We tried, fail safe.
452 #endif
453 } 389 }
454 390
455 static bool GetLetterCBox(FT_Face face, char letter, FT_BBox* bbox) { 391 static bool GetLetterCBox(FT_Face face, char letter, FT_BBox* bbox) {
456 const FT_UInt glyph_id = FT_Get_Char_Index(face, letter); 392 const FT_UInt glyph_id = FT_Get_Char_Index(face, letter);
457 if (!glyph_id) 393 if (!glyph_id)
458 return false; 394 return false;
459 if (FT_Load_Glyph(face, glyph_id, FT_LOAD_NO_SCALE) != 0) 395 if (FT_Load_Glyph(face, glyph_id, FT_LOAD_NO_SCALE) != 0)
460 return false; 396 return false;
461 FT_Outline_Get_CBox(&face->glyph->outline, bbox); 397 FT_Outline_Get_CBox(&face->glyph->outline, bbox);
462 return true; 398 return true;
463 } 399 }
464 400
465 static bool getWidthAdvance(FT_Face face, int gId, int16_t* data) { 401 static bool getWidthAdvance(FT_Face face, int gId, int16_t* data) {
466 FT_Fixed advance = 0; 402 FT_Fixed advance = 0;
467 if (getAdvances(face, gId, 1, FT_LOAD_NO_SCALE, &advance)) { 403 if (FT_Get_Advances(face, gId, 1, FT_LOAD_NO_SCALE, &advance)) {
468 return false; 404 return false;
469 } 405 }
470 SkASSERT(data); 406 SkASSERT(data);
471 *data = advance; 407 *data = advance;
472 return true; 408 return true;
473 } 409 }
474 410
475 static void populate_glyph_to_unicode(FT_Face& face, 411 static void populate_glyph_to_unicode(FT_Face& face, SkTDArray<SkUnichar>* glyph ToUnicode) {
476 SkTDArray<SkUnichar>* glyphToUnicode) {
477 // Check and see if we have Unicode cmaps. 412 // Check and see if we have Unicode cmaps.
478 for (int i = 0; i < face->num_charmaps; ++i) { 413 for (int i = 0; i < face->num_charmaps; ++i) {
479 // CMaps known to support Unicode: 414 // CMaps known to support Unicode:
480 // Platform ID Encoding ID Name 415 // Platform ID Encoding ID Name
481 // ----------- ----------- ----------------------------------- 416 // ----------- ----------- -----------------------------------
482 // 0 0,1 Apple Unicode 417 // 0 0,1 Apple Unicode
483 // 0 3 Apple Unicode 2.0 (preferred) 418 // 0 3 Apple Unicode 2.0 (preferred)
484 // 3 1 Microsoft Unicode UCS-2 419 // 3 1 Microsoft Unicode UCS-2
485 // 3 10 Microsoft Unicode UCS-4 (preferred) 420 // 3 10 Microsoft Unicode UCS-4 (preferred)
486 // 421 //
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after
659 int16_t advance = face->max_advance_width; 594 int16_t advance = face->max_advance_width;
660 info->fGlyphWidths->fAdvance.append(1, &advance); 595 info->fGlyphWidths->fAdvance.append(1, &advance);
661 finishRange(info->fGlyphWidths.get(), 0, 596 finishRange(info->fGlyphWidths.get(), 0,
662 SkAdvancedTypefaceMetrics::WidthRange::kDefault); 597 SkAdvancedTypefaceMetrics::WidthRange::kDefault);
663 } else if (!cid) { 598 } else if (!cid) {
664 appendRange(&info->fGlyphWidths, 0); 599 appendRange(&info->fGlyphWidths, 0);
665 // So as to not blow out the stack, get advances in batches. 600 // So as to not blow out the stack, get advances in batches.
666 for (int gID = 0; gID < face->num_glyphs; gID += 128) { 601 for (int gID = 0; gID < face->num_glyphs; gID += 128) {
667 FT_Fixed advances[128]; 602 FT_Fixed advances[128];
668 int advanceCount = 128; 603 int advanceCount = 128;
669 if (gID + advanceCount > face->num_glyphs) 604 if (gID + advanceCount > face->num_glyphs) {
670 advanceCount = face->num_glyphs - gID; 605 advanceCount = face->num_glyphs - gID;
671 getAdvances(face, gID, advanceCount, FT_LOAD_NO_SCALE, 606 }
672 advances); 607 FT_Get_Advances(face, gID, advanceCount, FT_LOAD_NO_SCALE, advan ces);
673 for (int i = 0; i < advanceCount; i++) { 608 for (int i = 0; i < advanceCount; i++) {
674 int16_t advance = advances[i]; 609 int16_t advance = advances[i];
675 info->fGlyphWidths->fAdvance.append(1, &advance); 610 info->fGlyphWidths->fAdvance.append(1, &advance);
676 } 611 }
677 } 612 }
678 finishRange(info->fGlyphWidths.get(), face->num_glyphs - 1, 613 finishRange(info->fGlyphWidths.get(), face->num_glyphs - 1,
679 SkAdvancedTypefaceMetrics::WidthRange::kRange); 614 SkAdvancedTypefaceMetrics::WidthRange::kRange);
680 } else { 615 } else {
681 info->fGlyphWidths.reset( 616 info->fGlyphWidths.reset(
682 getAdvanceData(face, 617 getAdvanceData(face,
(...skipping 413 matching lines...) Expand 10 before | Expand all | Expand 10 after
1096 if (glyphIndex == glyph) { 1031 if (glyphIndex == glyph) {
1097 return charCode; 1032 return charCode;
1098 } 1033 }
1099 charCode = FT_Get_Next_Char( fFace, charCode, &glyphIndex ); 1034 charCode = FT_Get_Next_Char( fFace, charCode, &glyphIndex );
1100 } 1035 }
1101 1036
1102 return 0; 1037 return 0;
1103 } 1038 }
1104 1039
1105 void SkScalerContext_FreeType::generateAdvance(SkGlyph* glyph) { 1040 void SkScalerContext_FreeType::generateAdvance(SkGlyph* glyph) {
1106 #ifdef FT_ADVANCES_H
1107 /* unhinted and light hinted text have linearly scaled advances 1041 /* unhinted and light hinted text have linearly scaled advances
1108 * which are very cheap to compute with some font formats... 1042 * which are very cheap to compute with some font formats...
1109 */ 1043 */
1110 if (fDoLinearMetrics) { 1044 if (fDoLinearMetrics) {
1111 SkAutoMutexAcquire ac(gFTMutex); 1045 SkAutoMutexAcquire ac(gFTMutex);
1112 1046
1113 if (this->setupSize()) { 1047 if (this->setupSize()) {
1114 glyph->zeroMetrics(); 1048 glyph->zeroMetrics();
1115 return; 1049 return;
1116 } 1050 }
1117 1051
1118 FT_Error error; 1052 FT_Error error;
1119 FT_Fixed advance; 1053 FT_Fixed advance;
1120 1054
1121 error = FT_Get_Advance( fFace, glyph->getGlyphID(), 1055 error = FT_Get_Advance( fFace, glyph->getGlyphID(),
1122 fLoadGlyphFlags | FT_ADVANCE_FLAG_FAST_ONLY, 1056 fLoadGlyphFlags | FT_ADVANCE_FLAG_FAST_ONLY,
1123 &advance ); 1057 &advance );
1124 if (0 == error) { 1058 if (0 == error) {
1125 glyph->fRsbDelta = 0; 1059 glyph->fRsbDelta = 0;
1126 glyph->fLsbDelta = 0; 1060 glyph->fLsbDelta = 0;
1127 glyph->fAdvanceX = SkFixedMul(fMatrix22.xx, advance); 1061 glyph->fAdvanceX = SkFixedMul(fMatrix22.xx, advance);
1128 glyph->fAdvanceY = - SkFixedMul(fMatrix22.yx, advance); 1062 glyph->fAdvanceY = - SkFixedMul(fMatrix22.yx, advance);
1129 return; 1063 return;
1130 } 1064 }
1131 } 1065 }
1132 #endif /* FT_ADVANCES_H */ 1066
1133 /* otherwise, we need to load/hint the glyph, which is slower */ 1067 /* otherwise, we need to load/hint the glyph, which is slower */
1134 this->generateMetrics(glyph); 1068 this->generateMetrics(glyph);
1135 return; 1069 return;
1136 } 1070 }
1137 1071
1138 void SkScalerContext_FreeType::getBBoxForCurrentGlyph(SkGlyph* glyph, 1072 void SkScalerContext_FreeType::getBBoxForCurrentGlyph(SkGlyph* glyph,
1139 FT_BBox* bbox, 1073 FT_BBox* bbox,
1140 bool snapToPixelBoundary) { 1074 bool snapToPixelBoundary) {
1141 1075
1142 FT_Outline_Get_CBox(&fFace->glyph->outline, bbox); 1076 FT_Outline_Get_CBox(&fFace->glyph->outline, bbox);
(...skipping 26 matching lines...) Expand all
1169 FT_Vector_Transform(&vector, &fMatrix22); 1103 FT_Vector_Transform(&vector, &fMatrix22);
1170 bbox->xMin += vector.x; 1104 bbox->xMin += vector.x;
1171 bbox->xMax += vector.x; 1105 bbox->xMax += vector.x;
1172 bbox->yMin += vector.y; 1106 bbox->yMin += vector.y;
1173 bbox->yMax += vector.y; 1107 bbox->yMax += vector.y;
1174 } 1108 }
1175 } 1109 }
1176 1110
1177 bool SkScalerContext_FreeType::getCBoxForLetter(char letter, FT_BBox* bbox) { 1111 bool SkScalerContext_FreeType::getCBoxForLetter(char letter, FT_BBox* bbox) {
1178 const FT_UInt glyph_id = FT_Get_Char_Index(fFace, letter); 1112 const FT_UInt glyph_id = FT_Get_Char_Index(fFace, letter);
1179 if (!glyph_id) 1113 if (!glyph_id) {
1180 return false; 1114 return false;
1181 if (FT_Load_Glyph(fFace, glyph_id, fLoadGlyphFlags) != 0) 1115 }
1116 if (FT_Load_Glyph(fFace, glyph_id, fLoadGlyphFlags) != 0) {
1182 return false; 1117 return false;
1118 }
1183 emboldenIfNeeded(fFace, fFace->glyph); 1119 emboldenIfNeeded(fFace, fFace->glyph);
1184 FT_Outline_Get_CBox(&fFace->glyph->outline, bbox); 1120 FT_Outline_Get_CBox(&fFace->glyph->outline, bbox);
1185 return true; 1121 return true;
1186 } 1122 }
1187 1123
1188 void SkScalerContext_FreeType::updateGlyphIfLCD(SkGlyph* glyph) { 1124 void SkScalerContext_FreeType::updateGlyphIfLCD(SkGlyph* glyph) {
1189 if (isLCD(fRec)) { 1125 if (isLCD(fRec)) {
1190 if (fLCDIsVert) { 1126 if (fLCDIsVert) {
1191 glyph->fHeight += gLCDExtra; 1127 glyph->fHeight += gLCDExtra;
1192 glyph->fTop -= gLCDExtra >> 1; 1128 glyph->fTop -= gLCDExtra >> 1;
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after
1307 scaleGlyphMetrics(*glyph, SkScalarDiv(SkFixedToScalar(fScaleY), 1243 scaleGlyphMetrics(*glyph, SkScalarDiv(SkFixedToScalar(fScaleY),
1308 SkIntToScalar(fFace->size->metrics .y_ppem))); 1244 SkIntToScalar(fFace->size->metrics .y_ppem)));
1309 } 1245 }
1310 1246
1311 #ifdef ENABLE_GLYPH_SPEW 1247 #ifdef ENABLE_GLYPH_SPEW
1312 SkDEBUGF(("FT_Set_Char_Size(this:%p sx:%x sy:%x ", this, fScaleX, fScaleY)); 1248 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(), fLoadG lyphFlags, glyph->fWidth)); 1249 SkDEBUGF(("Metrics(glyph:%d flags:0x%x) w:%d\n", glyph->getGlyphID(), fLoadG lyphFlags, glyph->fWidth));
1314 #endif 1250 #endif
1315 } 1251 }
1316 1252
1253 static void clear_glyph_image(const SkGlyph& glyph) {
1254 sk_bzero(glyph.fImage, glyph.rowBytes() * glyph.fHeight);
1255 }
1317 1256
1318 void SkScalerContext_FreeType::generateImage(const SkGlyph& glyph) { 1257 void SkScalerContext_FreeType::generateImage(const SkGlyph& glyph) {
1319 SkAutoMutexAcquire ac(gFTMutex); 1258 SkAutoMutexAcquire ac(gFTMutex);
1320 1259
1321 FT_Error err;
1322
1323 if (this->setupSize()) { 1260 if (this->setupSize()) {
1324 goto ERROR; 1261 clear_glyph_image(glyph);
1325 }
1326
1327 err = FT_Load_Glyph( fFace, glyph.getGlyphID(), 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(), glyph.fWidth, glyph.fHeight, glyph.rowBy tes(), fLoadGlyphFlags, err));
1331 ERROR:
1332 memset(glyph.fImage, 0, glyph.rowBytes() * glyph.fHeight);
1333 return; 1262 return;
1334 } 1263 }
1335 1264
1265 FT_Error err = FT_Load_Glyph(fFace, glyph.getGlyphID(), fLoadGlyphFlags);
1266 if (err != 0) {
1267 SkDEBUGF(("SkScalerContext_FreeType::generateImage: FT_Load_Glyph(glyph: %d width:%d height:%d rb:%d flags:%d) returned 0x%x\n",
1268 glyph.getGlyphID(), glyph.fWidth, glyph.fHeight, glyph.rowByte s(), fLoadGlyphFlags, err));
1269 clear_glyph_image(glyph);
1270 return;
1271 }
1272
1336 emboldenIfNeeded(fFace, fFace->glyph); 1273 emboldenIfNeeded(fFace, fFace->glyph);
1337 generateGlyphImage(fFace, glyph); 1274 generateGlyphImage(fFace, glyph);
1338 } 1275 }
1339 1276
1340 1277
1341 void SkScalerContext_FreeType::generatePath(const SkGlyph& glyph, 1278 void SkScalerContext_FreeType::generatePath(const SkGlyph& glyph, SkPath* path) {
1342 SkPath* path) {
1343 SkAutoMutexAcquire ac(gFTMutex); 1279 SkAutoMutexAcquire ac(gFTMutex);
1344 1280
1345 SkASSERT(path); 1281 SkASSERT(path);
1346 1282
1347 if (this->setupSize()) { 1283 if (this->setupSize()) {
1348 path->reset(); 1284 path->reset();
1349 return; 1285 return;
1350 } 1286 }
1351 1287
1352 uint32_t flags = fLoadGlyphFlags; 1288 uint32_t flags = fLoadGlyphFlags;
(...skipping 447 matching lines...) Expand 10 before | Expand all | Expand 10 after
1800 if (style) { 1736 if (style) {
1801 *style = SkFontStyle(weight, width, slant); 1737 *style = SkFontStyle(weight, width, slant);
1802 } 1738 }
1803 if (isFixedPitch) { 1739 if (isFixedPitch) {
1804 *isFixedPitch = FT_IS_FIXED_WIDTH(face); 1740 *isFixedPitch = FT_IS_FIXED_WIDTH(face);
1805 } 1741 }
1806 1742
1807 FT_Done_Face(face); 1743 FT_Done_Face(face);
1808 return true; 1744 return true;
1809 } 1745 }
OLDNEW
« no previous file with comments | « gyp/freetype.gyp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698