3 * Copyright 2006 The Android Open Source Project
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
9 #include "SkAdvancedTypefaceMetrics.h"
12 #include "SkColorPriv.h"
13 #include "SkDescriptor.h"
15 #include "SkFloatingPoint.h"
16 #include "SkFontHost.h"
17 #include "SkFontHost_FreeType_common.h"
20 #include "SkMaskGamma.h"
21 #include "SkMatrix22.h"
22 #include "SkOTUtils.h"
24 #include "SkScalerContext.h"
27 #include "SkTemplates.h"
30 #if defined(SK_CAN_USE_DLOPEN)
34 #include FT_FREETYPE_H
37 #include FT_TRUETYPE_TABLES_H
38 #include FT_TYPE1_TABLES_H
40 // In the past, FT_GlyphSlot_Own_Bitmap was defined in this header file.
41 #include FT_SYNTHESIS_H
43 #ifdef FT_LCD_FILTER_H
44 #include FT_LCD_FILTER_H
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.
50 #include FT_ADVANCES_H
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>
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.
69 # define FT_LOAD_COLOR ( 1L << 20 )
70 # define FT_PIXEL_MODE_BGRA 7
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.
77 # define FT_HAS_COLOR(face) false
80 //#define ENABLE_GLYPH_SPEW // for tracing calls
81 //#define DUMP_STRIKE_CREATION
83 //#define SK_GAMMA_APPLY_TO_A8
85 using namespace skia_advanced_typeface_metrics_utils;
87 static bool isLCD(const SkScalerContext::Rec& rec) {
88 switch (rec.fMaskFormat) {
89 case SkMask::kLCD16_Format:
90 case SkMask::kLCD32_Format:
97 //////////////////////////////////////////////////////////////////////////
101 SK_DECLARE_STATIC_MUTEX(gFTMutex);
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.
109 /////////////////////////////////////////////////////////////////////////
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*);
120 // Caller must lock gFTMutex before calling this function.
121 static bool InitFreetype() {
122 FT_Error err = FT_Init_FreeType(&gFTLibrary);
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);
134 gLCDExtra = 2; //Using a filter adds one full pixel to each side.
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, };
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);
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");
152 if (setLcdFilterWeights) {
153 err = setLcdFilterWeights(gFTLibrary, gGaussianLikeHeavyWeights);
162 gLCDSupportValid = true;
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.
172 FT_Done_FreeType(gFTLibrary);
174 SkASSERT(gLCDSupportValid);
175 *lcdSupported = gLCDSupport;
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);
185 class SkScalerContext_FreeType : public SkScalerContext_FreeType_Base {
187 SkScalerContext_FreeType(SkTypeface*, const SkDescriptor* desc);
188 virtual ~SkScalerContext_FreeType();
190 bool success() const {
191 return fFaceRec != NULL &&
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;
208 FT_Face fFace; // reference to shared face in gFaceRecHead
209 FT_Size fFTSize; // our own copy
211 SkFixed fScaleX, fScaleY;
213 uint32_t fLoadGlyphFlags;
214 bool fDoLinearMetrics;
217 // Need scalar versions for generateFontMetrics
219 SkMatrix fMatrix22Scalar;
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);
232 ///////////////////////////////////////////////////////////////////////////
233 ///////////////////////////////////////////////////////////////////////////
238 FT_StreamRec fFTStream;
243 // assumes ownership of the stream, will call unref() when its done
244 SkFaceRec(SkStream* strm, uint32_t fontID);
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;
258 if (!str->rewind()) {
263 ret = str->read(NULL, offset);
268 ret = str->read(buffer, count);
278 static void sk_stream_close(FT_Stream) {}
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));
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;
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;
298 if (rec->fFontID == fontID) {
299 SkASSERT(rec->fFace);
307 SkStream* strm = typeface->openStream(&face_index);
312 // this passes ownership of strm to the rec
313 rec = SkNEW_ARGS(SkFaceRec, (strm, fontID));
316 memset(&args, 0, sizeof(args));
317 const void* memoryBase = strm->getMemoryBase();
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();
325 //printf("fopen(%s)\n", keyString.c_str());
326 args.flags = FT_OPEN_STREAM;
327 args.stream = &rec->fFTStream;
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);
336 SkASSERT(rec->fFace);
337 //fprintf(stderr, "Opened font '%s'\n", filename.c_str());
338 rec->fNext = gFaceRecHead;
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;
349 SkFaceRec* next = rec->fNext;
350 if (rec->fFace == face) {
351 if (--rec->fRefCnt == 0) {
365 SkDEBUGFAIL("shouldn't get here, face not in list");
370 AutoFTAccess(const SkTypeface* tf) : fRec(NULL), fFace(NULL) {
372 if (1 == ++gFTCount) {
373 if (!InitFreetype()) {
377 fRec = ref_ft_face(tf);
385 unref_ft_face(fFace);
387 if (0 == --gFTCount) {
388 FT_Done_FreeType(gFTLibrary);
393 SkFaceRec* rec() { return fRec; }
394 FT_Face face() { return fFace; }
401 ///////////////////////////////////////////////////////////////////////////
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) {
407 return FT_Get_Advances(face, start, count, loadFlags, advances);
409 if (!face || start >= face->num_glyphs ||
410 start + count > face->num_glyphs || loadFlags != FT_LOAD_NO_SCALE) {
411 return 6; // "Invalid argument."
416 for (int i = 0; i < count; i++) {
417 FT_Error err = FT_Load_Glyph(face, start + i, FT_LOAD_NO_SCALE);
420 advances[i] = face->glyph->advance.x;
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;
433 // No embedding is 0x2 and bitmap embedding only is 0x200.
435 if ((os2_table = (TT_OS2*)FT_Get_Sfnt_Table(face, ft_sfnt_os2)) != NULL) {
436 return (os2_table->fsType & 0x202) == 0;
438 return false; // We tried, fail safe.
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;
447 // No subset is 0x100.
449 if ((os2_table = (TT_OS2*)FT_Get_Sfnt_Table(face, ft_sfnt_os2)) != NULL) {
450 return (os2_table->fsType & 0x100) == 0;
452 return false; // We tried, fail safe.
456 static bool GetLetterCBox(FT_Face face, char letter, FT_BBox* bbox) {
457 const FT_UInt glyph_id = FT_Get_Char_Index(face, letter);
460 if (FT_Load_Glyph(face, glyph_id, FT_LOAD_NO_SCALE) != 0)
462 FT_Outline_Get_CBox(&face->glyph->outline, bbox);
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)) {
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)
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
494 FT_UShort platformId = face->charmaps[i]->platform_id;
495 FT_UShort encodingId = face->charmaps[i]->encoding_id;
497 if (platformId != 0 && platformId != 3) {
500 if (platformId == 3 && encodingId != 1 && encodingId != 10) {
503 bool preferredMap = ((platformId == 3 && encodingId == 10) ||
504 (platformId == 0 && encodingId == 3));
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);
513 // Iterate through each cmap entry.
515 for (SkUnichar charCode = FT_Get_First_Char(face, &glyphIndex);
517 charCode = FT_Get_Next_Char(face, charCode, &glyphIndex)) {
519 ((*glyphToUnicode)[glyphIndex] == 0 || preferredMap)) {
520 (*glyphToUnicode)[glyphIndex] = charCode;
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)
533 AutoFTAccess fta(this);
534 FT_Face face = fta.face();
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);
546 if (!canEmbed(face)) {
547 info->fFlags = SkTBitOr<SkAdvancedTypefaceMetrics::FontFlags>(
549 SkAdvancedTypefaceMetrics::kNotEmbeddable_FontFlag);
551 if (!canSubset(face)) {
552 info->fFlags = SkTBitOr<SkAdvancedTypefaceMetrics::FontFlags>(
554 SkAdvancedTypefaceMetrics::kNotSubsettable_FontFlag);
556 info->fLastGlyphID = face->num_glyphs - 1;
557 info->fEmSize = 1000;
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;
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;
572 if ((ttHeader = (TT_Header*)FT_Get_Sfnt_Table(face,
573 ft_sfnt_head)) != NULL) {
574 info->fEmSize = ttHeader->Units_Per_EM;
577 info->fType = SkAdvancedTypefaceMetrics::kOther_Font;
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;
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);
595 info->fItalicAngle = 0;
598 info->fAscent = face->ascender;
599 info->fDescent = face->descender;
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;
605 char stem_chars[] = {'i', 'I', '!', '1'};
606 for (size_t i = 0; i < SK_ARRAY_COUNT(stem_chars); i++) {
608 if (GetLetterCBox(face, stem_chars[i], &bbox)) {
609 int16_t width = bbox.xMax - bbox.xMin;
610 if (width > 0 && width < min_width) {
612 info->fStemV = min_width;
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;
632 // Figure out a good guess for CapHeight: average the height of M and X.
633 FT_BBox m_bbox, x_bbox;
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 -
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;
645 // Last resort, use the ascent.
646 info->fCapHeight = info->fAscent;
650 info->fBBox = SkIRect::MakeLTRB(face->bbox.xMin, face->bbox.yMax,
651 face->bbox.xMax, face->bbox.yMin);
653 if (!FT_IS_SCALABLE(face)) {
654 perGlyphInfo = SkAdvancedTypefaceMetrics::kNo_PerGlyphInfo;
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);
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,
674 for (int i = 0; i < advanceCount; i++) {
675 int16_t advance = advances[i];
676 info->fGlyphWidths->fAdvance.append(1, &advance);
679 finishRange(info->fGlyphWidths.get(), face->num_glyphs - 1,
680 SkAdvancedTypefaceMetrics::WidthRange::kRange);
682 info->fGlyphWidths.reset(
691 if (perGlyphInfo & SkAdvancedTypefaceMetrics::kVAdvance_PerGlyphInfo &&
692 FT_HAS_VERTICAL(face)) {
693 SkASSERT(false); // Not implemented yet.
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);
710 if (perGlyphInfo & SkAdvancedTypefaceMetrics::kToUnicode_PerGlyphInfo &&
711 info->fType != SkAdvancedTypefaceMetrics::kType1_Font &&
712 face->num_charmaps) {
713 populate_glyph_to_unicode(face, &(info->fGlyphToUnicode));
720 ///////////////////////////////////////////////////////////////////////////
722 static bool bothZero(SkScalar a, SkScalar b) {
723 return 0 == a && 0 == b;
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]));
733 SkScalerContext* SkTypeface_FreeType::onCreateScalerContext(
734 const SkDescriptor* desc) const {
735 SkScalerContext_FreeType* c = SkNEW_ARGS(SkScalerContext_FreeType,
736 (const_cast<SkTypeface_FreeType*>(this),
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);
753 if (!is_lcd_supported() && isLCD(*rec)) {
754 // If the runtime Freetype library doesn't support LCD mode, we disable
756 rec->fMaskFormat = SkMask::kA8_Format;
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;
764 if ((rec->fFlags & SkScalerContext::kSubpixelPositioning_Flag)) {
765 if (SkPaint::kNo_Hinting != h) {
766 h = SkPaint::kSlight_Hinting;
770 // rotated text looks bad with hinting, so we disable it as needed
771 if (!isAxisAligned(*rec)) {
772 h = SkPaint::kNo_Hinting;
776 #ifndef SK_GAMMA_APPLY_TO_A8
778 rec->ignorePreBlend();
783 int SkTypeface_FreeType::onGetUPEM() const {
784 AutoFTAccess fta(this);
785 FT_Face face = fta.face();
786 return face ? face->units_per_EM : 0;
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)) {
797 for (int i = 0; i < count - 1; ++i) {
799 FT_Error err = FT_Get_Kerning(face, glyphs[i], glyphs[i+1],
800 FT_KERNING_UNSCALED, &delta);
804 adjustments[i] = delta.x;
809 static FT_Int chooseBitmapStrike(FT_Face face, SkFixed scaleY) {
810 // early out if face is bad
812 SkDEBUGF(("chooseBitmapStrike aborted due to NULL face\n"));
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;
827 } else if (chosenPPEM < targetPPEM) {
828 // attempt to increase chosenPPEM
829 if (thisPPEM > chosenPPEM) {
830 chosenPPEM = thisPPEM;
831 chosenStrikeIndex = strikeIndex;
834 // attempt to decrease chosenPPEM, but not below targetPPEM
835 if (thisPPEM < chosenPPEM && thisPPEM > targetPPEM) {
836 chosenPPEM = thisPPEM;
837 chosenStrikeIndex = strikeIndex;
841 if (chosenStrikeIndex != -1) {
842 // use the chosen strike
843 FT_Error err = FT_Select_Size(face, chosenStrikeIndex);
845 SkDEBUGF(("FT_Select_Size(%s, %d) returned 0x%x\n", face->family_name,
846 chosenStrikeIndex, err));
847 chosenStrikeIndex = -1;
850 return chosenStrikeIndex;
853 SkScalerContext_FreeType::SkScalerContext_FreeType(SkTypeface* typeface,
854 const SkDescriptor* desc)
855 : SkScalerContext_FreeType_Base(typeface, desc) {
856 SkAutoMutexAcquire ac(gFTMutex);
859 if (!InitFreetype()) {
865 // load the font file
869 fFaceRec = ref_ft_face(typeface);
870 if (NULL == fFaceRec) {
873 fFace = fFaceRec->fFace;
875 // A is the total matrix.
877 fRec.getSingleMatrix(&A);
879 SkScalar sx = A.getScaleX();
880 SkScalar sy = A.getScaleY();
881 fMatrix22Scalar.reset();
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.
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.
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.
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);
902 // G is the Givens Matrix for A (rotational matrix where GA[0][1] == 0).
904 SkComputeGivensRotation(h, &G);
906 // GA is the matrix A with rotation removed.
910 sx = SkScalarAbs(GA.get(SkMatrix::kMScaleX));
911 sy = SkScalarAbs(GA.get(SkMatrix::kMScaleY));
913 // sA is the total matrix A without the text scale.
915 sA.preScale(SkScalarInvert(sx), SkScalarInvert(sy)); //remove text size
917 fMatrix22Scalar.setScaleX(sA.getScaleX());
918 fMatrix22Scalar.setSkewX(-sA.getSkewX());
919 fMatrix22Scalar.setSkewY(-sA.getSkewY());
920 fMatrix22Scalar.setScaleY(sA.getScaleY());
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());
930 fLCDIsVert = SkToBool(fRec.fFlags & SkScalerContext::kLCD_Vertical_Flag);
932 // compute the flags we send to Load_Glyph
933 bool linearMetrics = SkToBool(fRec.fFlags & SkScalerContext::kSubpixelPositioning_Flag);
935 FT_Int32 loadFlags = FT_LOAD_DEFAULT;
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;
945 switch (fRec.getHinting()) {
946 case SkPaint::kNo_Hinting:
947 loadFlags = FT_LOAD_NO_HINTING;
948 linearMetrics = true;
950 case SkPaint::kSlight_Hinting:
951 loadFlags = FT_LOAD_TARGET_LIGHT; // This implies FORCE_AUTOHINT
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
958 loadFlags = FT_LOAD_NO_AUTOHINT;
962 case SkPaint::kFull_Hinting:
963 if (fRec.fFlags & SkScalerContext::kForceAutohinting_Flag) {
964 loadFlags = FT_LOAD_FORCE_AUTOHINT;
967 loadFlags = FT_LOAD_TARGET_NORMAL;
970 loadFlags = FT_LOAD_TARGET_LCD_V;
972 loadFlags = FT_LOAD_TARGET_LCD;
977 SkDebugf("---------- UNKNOWN hinting %d\n", fRec.getHinting());
982 if ((fRec.fFlags & SkScalerContext::kEmbeddedBitmapText_Flag) == 0) {
983 loadFlags |= FT_LOAD_NO_BITMAP;
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;
991 // Use vertical layout if requested.
992 if (fRec.fFlags & SkScalerContext::kVertical_Flag) {
993 loadFlags |= FT_LOAD_VERTICAL_LAYOUT;
996 loadFlags |= FT_LOAD_COLOR;
998 fLoadGlyphFlags = loadFlags;
1001 FT_Error err = FT_New_Size(fFace, &fFTSize);
1003 SkDEBUGF(("FT_New_Size returned %x for face %s\n", err, fFace->family_name));
1007 err = FT_Activate_Size(fFTSize);
1009 SkDEBUGF(("FT_Activate_Size(%08x, 0x%x, 0x%x) returned 0x%x\n", fFace, fScaleX, fScaleY,
1015 if (FT_IS_SCALABLE(fFace)) {
1016 err = FT_Set_Char_Size(fFace, SkFixedToFDot6(fScaleX), SkFixedToFDot6(fScaleY), 72, 72);
1018 SkDEBUGF(("FT_Set_CharSize(%08x, 0x%x, 0x%x) returned 0x%x\n",
1019 fFace, fScaleX, fScaleY, err));
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)));
1030 // FreeType does no provide linear metrics for bitmap fonts.
1031 linearMetrics = false;
1033 // FreeType documentation says:
1034 // FT_LOAD_NO_BITMAP -- Ignore bitmap strikes when loading.
1035 // Bitmap-only fonts ignore this flag.
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;
1042 SkDEBUGF(("unknown kind of font \"%s\" size %f?\n",
1043 fFace->family_name, SkFixedToScalar(fScaleY)));
1046 fDoLinearMetrics = linearMetrics;
1049 SkScalerContext_FreeType::~SkScalerContext_FreeType() {
1050 SkAutoMutexAcquire ac(gFTMutex);
1052 if (fFTSize != NULL) {
1053 FT_Done_Size(fFTSize);
1056 if (fFace != NULL) {
1057 unref_ft_face(fFace);
1059 if (--gFTCount == 0) {
1060 FT_Done_FreeType(gFTLibrary);
1061 SkDEBUGCODE(gFTLibrary = NULL;)
1065 /* We call this before each use of the fFace, since we may be sharing
1066 this face with other context (at different sizes).
1068 FT_Error SkScalerContext_FreeType::setupSize() {
1069 FT_Error err = FT_Activate_Size(fFTSize);
1071 SkDEBUGF(("SkScalerContext_FreeType::FT_Activate_Size(%x, 0x%x, 0x%x) returned 0x%x\n",
1072 fFaceRec->fFontID, fScaleX, fScaleY, err));
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);
1083 unsigned SkScalerContext_FreeType::generateGlyphCount() {
1084 return fFace->num_glyphs;
1087 uint16_t SkScalerContext_FreeType::generateCharToGlyph(SkUnichar uni) {
1088 return SkToU16(FT_Get_Char_Index( fFace, uni ));
1091 SkUnichar SkScalerContext_FreeType::generateGlyphToChar(uint16_t glyph) {
1092 // iterate through each cmap entry, looking for matching glyph indices
1094 SkUnichar charCode = FT_Get_First_Char( fFace, &glyphIndex );
1096 while (glyphIndex != 0) {
1097 if (glyphIndex == glyph) {
1100 charCode = FT_Get_Next_Char( fFace, charCode, &glyphIndex );
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...
1111 if (fDoLinearMetrics) {
1112 SkAutoMutexAcquire ac(gFTMutex);
1114 if (this->setupSize()) {
1115 glyph->zeroMetrics();
1122 error = FT_Get_Advance( fFace, glyph->getGlyphID(),
1123 fLoadGlyphFlags | FT_ADVANCE_FLAG_FAST_ONLY,
1126 glyph->fRsbDelta = 0;
1127 glyph->fLsbDelta = 0;
1128 glyph->fAdvanceX = SkFixedMul(fMatrix22.xx, advance);
1129 glyph->fAdvanceY = - SkFixedMul(fMatrix22.yx, advance);
1133 #endif /* FT_ADVANCES_H */
1134 /* otherwise, we need to load/hint the glyph, which is slower */
1135 this->generateMetrics(glyph);
1139 void SkScalerContext_FreeType::getBBoxForCurrentGlyph(SkGlyph* glyph,
1141 bool snapToPixelBoundary) {
1143 FT_Outline_Get_CBox(&fFace->glyph->outline, bbox);
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
1155 // outset the box to integral boundaries
1156 if (snapToPixelBoundary) {
1159 bbox->xMax = (bbox->xMax + 63) & ~63;
1160 bbox->yMax = (bbox->yMax + 63) & ~63;
1163 // Must come after snapToPixelBoundary so that the width and height are
1164 // consistent. Otherwise asserts will fire later on when generating the
1166 if (fRec.fFlags & SkScalerContext::kVertical_Flag) {
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;
1178 bool SkScalerContext_FreeType::getCBoxForLetter(char letter, FT_BBox* bbox) {
1179 const FT_UInt glyph_id = FT_Get_Char_Index(fFace, letter);
1182 if (FT_Load_Glyph(fFace, glyph_id, fLoadGlyphFlags) != 0)
1184 emboldenIfNeeded(fFace, fFace->glyph);
1185 FT_Outline_Get_CBox(&fFace->glyph->outline, bbox);
1189 void SkScalerContext_FreeType::updateGlyphIfLCD(SkGlyph* glyph) {
1192 glyph->fHeight += gLCDExtra;
1193 glyph->fTop -= gLCDExtra >> 1;
1195 glyph->fWidth += gLCDExtra;
1196 glyph->fLeft -= gLCDExtra >> 1;
1201 inline void scaleGlyphMetrics(SkGlyph& glyph, SkScalar scale) {
1202 glyph.fWidth *= scale;
1203 glyph.fHeight *= scale;
1204 glyph.fTop *= scale;
1205 glyph.fLeft *= scale;
1207 SkFixed fixedScale = SkScalarToFixed(scale);
1208 glyph.fAdvanceX = SkFixedMul(glyph.fAdvanceX, fixedScale);
1209 glyph.fAdvanceY = SkFixedMul(glyph.fAdvanceY, fixedScale);
1212 void SkScalerContext_FreeType::generateMetrics(SkGlyph* glyph) {
1213 SkAutoMutexAcquire ac(gFTMutex);
1215 glyph->fRsbDelta = 0;
1216 glyph->fLsbDelta = 0;
1220 if (this->setupSize()) {
1224 err = FT_Load_Glyph( fFace, glyph->getGlyphID(), fLoadGlyphFlags );
1227 SkDEBUGF(("SkScalerContext_FreeType::generateMetrics(%x): FT_Load_Glyph(glyph:%d flags:%x) returned 0x%x\n",
1228 fFaceRec->fFontID, glyph->getGlyphID(), fLoadGlyphFlags, err));
1231 glyph->zeroMetrics();
1234 emboldenIfNeeded(fFace, fFace->glyph);
1236 switch ( fFace->glyph->format ) {
1237 case FT_GLYPH_FORMAT_OUTLINE:
1238 if (0 == fFace->glyph->outline.n_contours) {
1245 getBBoxForCurrentGlyph(glyph, &bbox, true);
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));
1252 updateGlyphIfLCD(glyph);
1256 case FT_GLYPH_FORMAT_BITMAP:
1257 if (fRec.fFlags & SkScalerContext::kVertical_Flag) {
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);
1266 if (fFace->glyph->bitmap.pixel_mode == FT_PIXEL_MODE_BGRA) {
1267 glyph->fMaskFormat = SkMask::kARGB32_Format;
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);
1277 SkDEBUGFAIL("unknown glyph format");
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);
1286 glyph->fAdvanceX = -SkFDot6ToFixed(fFace->glyph->advance.x);
1287 glyph->fAdvanceY = SkFDot6ToFixed(fFace->glyph->advance.y);
1290 if (fDoLinearMetrics) {
1291 glyph->fAdvanceX = SkFixedMul(fMatrix22.xx, fFace->glyph->linearHoriAdvance);
1292 glyph->fAdvanceY = -SkFixedMul(fMatrix22.yx, fFace->glyph->linearHoriAdvance);
1294 glyph->fAdvanceX = SkFDot6ToFixed(fFace->glyph->advance.x);
1295 glyph->fAdvanceY = -SkFDot6ToFixed(fFace->glyph->advance.y);
1297 if (fRec.fFlags & kDevKernText_Flag) {
1298 glyph->fRsbDelta = SkToS8(fFace->glyph->rsb_delta);
1299 glyph->fLsbDelta = SkToS8(fFace->glyph->lsb_delta);
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)));
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));
1319 void SkScalerContext_FreeType::generateImage(const SkGlyph& glyph) {
1320 SkAutoMutexAcquire ac(gFTMutex);
1324 if (this->setupSize()) {
1328 err = FT_Load_Glyph( fFace, glyph.getGlyphID(), fLoadGlyphFlags);
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));
1333 memset(glyph.fImage, 0, glyph.rowBytes() * glyph.fHeight);
1337 emboldenIfNeeded(fFace, fFace->glyph);
1338 generateGlyphImage(fFace, glyph);
1342 void SkScalerContext_FreeType::generatePath(const SkGlyph& glyph,
1344 SkAutoMutexAcquire ac(gFTMutex);
1348 if (this->setupSize()) {
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)
1357 FT_Error err = FT_Load_Glyph( fFace, glyph.getGlyphID(), flags);
1360 SkDEBUGF(("SkScalerContext_FreeType::generatePath: FT_Load_Glyph(glyph:%d flags:%d) returned 0x%x\n",
1361 glyph.getGlyphID(), flags, err));
1365 emboldenIfNeeded(fFace, fFace->glyph);
1367 generateGlyphPath(fFace, path);
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) {
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));
1380 void SkScalerContext_FreeType::generateFontMetrics(SkPaint::FontMetrics* metrics) {
1381 if (NULL == metrics) {
1385 SkAutoMutexAcquire ac(gFTMutex);
1387 if (this->setupSize()) {
1389 sk_bzero(metrics, sizeof(*metrics));
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;
1399 // fetch units/EM from "head" table if needed (ie for bitmap fonts)
1400 SkScalar upem = SkIntToScalar(face->units_per_EM);
1402 TT_Header* ttHeader = (TT_Header*)FT_Get_Sfnt_Table(face, ft_sfnt_head);
1404 upem = SkIntToScalar(ttHeader->Units_Per_EM);
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);
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;
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;
1436 metrics->fFlags |= SkPaint::FontMetrics::kUnderlineThinknessIsValid_Flag;
1437 metrics->fFlags |= SkPaint::FontMetrics::kUnderlinePositionIsValid_Flag;
1439 // we may be able to synthesize x_height and cap_height from outline
1442 if (getCBoxForLetter('x', &bbox)) {
1443 x_height = SkIntToScalar(bbox.yMax) / 64.0f;
1448 if (getCBoxForLetter('H', &bbox)) {
1449 cap_height = SkIntToScalar(bbox.yMax) / 64.0f;
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))
1460 xmax = SkIntToScalar(face->available_sizes[fStrikeIndex].width) / xppem;
1461 ymin = descent + leading;
1462 ymax = ascent - descent;
1463 underlineThickness = 0;
1464 underlinePosition = 0;
1466 metrics->fFlags &= ~SkPaint::FontMetrics::kUnderlineThinknessIsValid_Flag;
1467 metrics->fFlags &= ~SkPaint::FontMetrics::kUnderlinePositionIsValid_Flag;
1472 // synthesize elements that were not provided by the os/2 table or format-specific metrics
1476 if (!avgCharWidth) {
1477 avgCharWidth = xmax - xmin;
1480 cap_height = -ascent;
1483 // disallow negative linespacing
1484 if (leading < 0.0f) {
1488 SkScalar scale = myy;
1489 if (this->isVertical()) {
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;
1506 void SkScalerContext_FreeType::emboldenIfNeeded(FT_Face face, FT_GlyphSlot glyph)
1508 // check to see if the embolden bit is set
1509 if (0 == (fRec.fFlags & SkScalerContext::kEmbolden_Flag)) {
1513 switch (glyph->format) {
1514 case FT_GLYPH_FORMAT_OUTLINE:
1516 strength = FT_MulFix(face->units_per_EM, face->size->metrics.y_scale) / 24;
1517 FT_Outline_Embolden(&glyph->outline, strength);
1519 case FT_GLYPH_FORMAT_BITMAP:
1520 FT_GlyphSlot_Own_Bitmap(glyph);
1521 FT_Bitmap_Embolden(glyph->library, &glyph->bitmap, kBitmapEmboldenStrength, 0);
1524 SkDEBUGFAIL("unknown glyph format");
1528 ///////////////////////////////////////////////////////////////////////////////
1530 #include "SkUtils.h"
1532 static SkUnichar next_utf8(const void** chars) {
1533 return SkUTF8_NextUnichar((const char**)chars);
1536 static SkUnichar next_utf16(const void** chars) {
1537 return SkUTF16_NextUnichar((const uint16_t**)chars);
1540 static SkUnichar next_utf32(const void** chars) {
1541 const SkUnichar** uniChars = (const SkUnichar**)chars;
1542 SkUnichar uni = **uniChars;
1547 typedef SkUnichar (*EncodingProc)(const void**);
1549 static EncodingProc find_encoding_proc(SkTypeface::Encoding enc) {
1550 static const EncodingProc gProcs[] = {
1551 next_utf8, next_utf16, next_utf32
1553 SkASSERT((size_t)enc < SK_ARRAY_COUNT(gProcs));
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();
1563 sk_bzero(glyphs, glyphCount * sizeof(glyphs[0]));
1568 EncodingProc next_uni_proc = find_encoding_proc(encoding);
1570 if (NULL == glyphs) {
1571 for (int i = 0; i < glyphCount; ++i) {
1572 if (0 == FT_Get_Char_Index(face, next_uni_proc(&chars))) {
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) {
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;
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);
1613 int SkTypeface_FreeType::onGetTableTags(SkFontTableTag tags[]) const {
1614 AutoFTAccess fta(this);
1615 FT_Face face = fta.face();
1617 FT_ULong tableCount = 0;
1620 // When 'tag' is NULL, returns number of tables in 'length'.
1621 error = FT_Sfnt_Table_Info(face, 0, NULL, &tableCount);
1627 for (FT_ULong tableIndex = 0; tableIndex < tableCount; ++tableIndex) {
1629 FT_ULong tablelength;
1630 error = FT_Sfnt_Table_Info(face, tableIndex, &tableTag, &tablelength);
1634 tags[tableIndex] = static_cast<SkFontTableTag>(tableTag);
1640 size_t SkTypeface_FreeType::onGetTableData(SkFontTableTag tag, size_t offset,
1641 size_t length, void* data) const
1643 AutoFTAccess fta(this);
1644 FT_Face face = fta.face();
1646 FT_ULong tableLength = 0;
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);
1655 if (offset > tableLength) {
1658 FT_ULong size = SkTMin((FT_ULong)length, tableLength - (FT_ULong)offset);
1660 error = FT_Load_Sfnt_Table(face, tag, offset, reinterpret_cast<FT_Byte*>(data), &size);
1669 ///////////////////////////////////////////////////////////////////////////////
1670 ///////////////////////////////////////////////////////////////////////////////
1672 /*static*/ bool SkTypeface_FreeType::ScanFont(
1673 SkStream* stream, int ttcIndex, SkString* name, SkTypeface::Style* style, bool* isFixedPitch)
1676 if (FT_Init_FreeType(&library)) {
1681 memset(&args, 0, sizeof(args));
1683 const void* memoryBase = stream->getMemoryBase();
1684 FT_StreamRec streamRec;
1687 args.flags = FT_OPEN_MEMORY;
1688 args.memory_base = (const FT_Byte*)memoryBase;
1689 args.memory_size = stream->getLength();
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;
1697 args.flags = FT_OPEN_STREAM;
1698 args.stream = &streamRec;
1702 if (FT_Open_Face(library, &args, ttcIndex, &face)) {
1703 FT_Done_FreeType(library);
1707 int tempStyle = SkTypeface::kNormal;
1708 if (face->style_flags & FT_STYLE_FLAG_BOLD) {
1709 tempStyle |= SkTypeface::kBold;
1711 if (face->style_flags & FT_STYLE_FLAG_ITALIC) {
1712 tempStyle |= SkTypeface::kItalic;
1716 name->set(face->family_name);
1719 *style = (SkTypeface::Style) tempStyle;
1722 *isFixedPitch = FT_IS_FIXED_WIDTH(face);
1726 FT_Done_FreeType(library);