blob: 0f3dded50c9665f77f54e78e1cee85a56ab4460c [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
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
reed@android.com8a1c16f2008-12-17 15:59:43 +00009#include "SkBitmap.h"
10#include "SkCanvas.h"
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000011#include "SkColorPriv.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000012#include "SkDescriptor.h"
13#include "SkFDot6.h"
bungeman@google.com3aacb412012-03-13 14:55:12 +000014#include "SkFloatingPoint.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000015#include "SkFontHost.h"
george@mozilla.comc59b5da2012-08-23 00:39:08 +000016#include "SkFontHost_FreeType_common.h"
bungeman@google.combbe50132012-07-24 20:33:21 +000017#include "SkGlyph.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000018#include "SkMask.h"
bungeman@google.com97efada2012-07-30 20:40:50 +000019#include "SkMaskGamma.h"
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +000020#include "SkAdvancedTypefaceMetrics.h"
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000021#include "SkScalerContext.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000022#include "SkStream.h"
23#include "SkString.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000024#include "SkTemplates.h"
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000025#include "SkThread.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000026
bungeman@google.comfd668cf2012-08-24 17:46:11 +000027#if defined(SK_CAN_USE_DLOPEN)
28#include <dlfcn.h>
29#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +000030#include <ft2build.h>
31#include FT_FREETYPE_H
32#include FT_OUTLINE_H
33#include FT_SIZES_H
agl@chromium.orgcc3096b2009-04-22 22:09:04 +000034#include FT_TRUETYPE_TABLES_H
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000035#include FT_TYPE1_TABLES_H
agl@chromium.orge76073b2010-06-04 20:31:17 +000036#include FT_BITMAP_H
agl@chromium.org36bb6972010-06-04 20:57:16 +000037// In the past, FT_GlyphSlot_Own_Bitmap was defined in this header file.
38#include FT_SYNTHESIS_H
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000039#include FT_XFREE86_H
epoger@google.com5070d792011-06-29 20:43:14 +000040#ifdef FT_LCD_FILTER_H
agl@chromium.org309485b2009-07-21 17:41:32 +000041#include FT_LCD_FILTER_H
epoger@google.com5070d792011-06-29 20:43:14 +000042#endif
agl@chromium.org309485b2009-07-21 17:41:32 +000043
reed@android.com8a1c16f2008-12-17 15:59:43 +000044#ifdef FT_ADVANCES_H
45#include FT_ADVANCES_H
46#endif
47
agl@chromium.orgcc3096b2009-04-22 22:09:04 +000048#if 0
49// Also include the files by name for build tools which require this.
50#include <freetype/freetype.h>
51#include <freetype/ftoutln.h>
52#include <freetype/ftsizes.h>
53#include <freetype/tttables.h>
54#include <freetype/ftadvanc.h>
agl@chromium.org309485b2009-07-21 17:41:32 +000055#include <freetype/ftlcdfil.h>
agl@chromium.orge76073b2010-06-04 20:31:17 +000056#include <freetype/ftbitmap.h>
agl@chromium.org36bb6972010-06-04 20:57:16 +000057#include <freetype/ftsynth.h>
agl@chromium.orgcc3096b2009-04-22 22:09:04 +000058#endif
59
reed@android.com8a1c16f2008-12-17 15:59:43 +000060//#define ENABLE_GLYPH_SPEW // for tracing calls
61//#define DUMP_STRIKE_CREATION
62
reed@google.com1ac83502012-02-28 17:06:02 +000063//#define SK_GAMMA_APPLY_TO_A8
reed@google.com1ac83502012-02-28 17:06:02 +000064
vandebo@chromium.org6f72d1e2011-02-14 23:19:59 +000065using namespace skia_advanced_typeface_metrics_utils;
66
reed@google.comeffc5012011-06-27 16:44:46 +000067static bool isLCD(const SkScalerContext::Rec& rec) {
68 switch (rec.fMaskFormat) {
69 case SkMask::kLCD16_Format:
70 case SkMask::kLCD32_Format:
71 return true;
72 default:
73 return false;
74 }
75}
76
reed@android.com8a1c16f2008-12-17 15:59:43 +000077//////////////////////////////////////////////////////////////////////////
78
79struct SkFaceRec;
80
digit@google.com1771cbf2012-01-26 21:26:40 +000081SK_DECLARE_STATIC_MUTEX(gFTMutex);
reed@android.com8a1c16f2008-12-17 15:59:43 +000082static int gFTCount;
83static FT_Library gFTLibrary;
84static SkFaceRec* gFaceRecHead;
agl@chromium.orgf18d8762009-07-28 18:38:08 +000085static bool gLCDSupportValid; // true iff |gLCDSupport| has been set.
86static bool gLCDSupport; // true iff LCD is supported by the runtime.
reed@google.coma1c32562012-03-01 19:38:23 +000087static int gLCDExtra; // number of extra pixels for filtering.
reed@android.com8a1c16f2008-12-17 15:59:43 +000088
bungeman@google.comfd668cf2012-08-24 17:46:11 +000089// FT_Library_SetLcdFilterWeights was introduced in FreeType 2.4.0.
90// The following platforms provide FreeType of at least 2.4.0.
91// Ubuntu >= 11.04 (previous deprecated April 2013)
92// Debian >= 6.0 (good)
93// OpenSuse >= 11.4 (previous deprecated January 2012 / Nov 2013 for Evergreen 11.2)
94// Fedora >= 14 (good)
95// Android >= Gingerbread (good)
96typedef FT_Error (*FT_Library_SetLcdFilterWeightsProc)(FT_Library, unsigned char*);
97
reed@android.com8a1c16f2008-12-17 15:59:43 +000098/////////////////////////////////////////////////////////////////////////
99
bungeman@google.comfd668cf2012-08-24 17:46:11 +0000100static bool InitFreetype() {
agl@chromium.org309485b2009-07-21 17:41:32 +0000101 FT_Error err = FT_Init_FreeType(&gFTLibrary);
reed@google.comea2333d2011-03-14 16:44:56 +0000102 if (err) {
agl@chromium.org309485b2009-07-21 17:41:32 +0000103 return false;
reed@google.comea2333d2011-03-14 16:44:56 +0000104 }
agl@chromium.org309485b2009-07-21 17:41:32 +0000105
bungeman@google.comfd668cf2012-08-24 17:46:11 +0000106 // Setup LCD filtering. This reduces color fringes for LCD smoothed glyphs.
epoger@google.comb371ed12011-06-29 21:20:52 +0000107#ifdef FT_LCD_FILTER_H
bungeman@google.comfd668cf2012-08-24 17:46:11 +0000108 //Use light as default, as FT_LCD_FILTER_DEFAULT adds up to 0x110.
109 err = FT_Library_SetLcdFilter(gFTLibrary, FT_LCD_FILTER_LIGHT);
110 if (0 == err) {
111 gLCDSupport = true;
112 gLCDExtra = 2; //Using a filter adds one full pixel to each side.
113
114 static unsigned char gaussianLikeWeights[] = { 0x17, 0x40, 0x52, 0x40, 0x17 };
115 //static unsigned char triangleLikeWeights[] = { 0x1C, 0x39, 0x56, 0x39, 0x1C };
116
117#if defined(SK_FONTHOST_FREETYPE_RUNTIME_VERSION) && \
118 SK_FONTHOST_FREETYPE_RUNTIME_VERSION > 0x020400
119 err = FT_Library_SetLcdFilterWeights(gFTLibrary, gaussianLikeWeights);
120#elif defined(SK_CAN_USE_DLOPEN) && SK_CAN_USE_DLOPEN == 1
121 //The FreeType library is already loaded, so symbols are available in process.
122 void* self = dlopen(NULL, RTLD_LAZY);
123 if (NULL != self) {
124 FT_Library_SetLcdFilterWeightsProc setLcdFilterWeights;
125 //The following cast is non-standard, but safe for POSIX.
126 *reinterpret_cast<void**>(&setLcdFilterWeights) = dlsym(self, "FT_Library_SetLcdFilterWeights");
127 dlclose(self);
128
129 if (NULL != setLcdFilterWeights) {
130 err = setLcdFilterWeights(gFTLibrary, gaussianLikeWeights);
131 }
132 }
133#endif
reed@google.coma1c32562012-03-01 19:38:23 +0000134 }
epoger@google.com5070d792011-06-29 20:43:14 +0000135#else
136 gLCDSupport = false;
137#endif
reed@android.com61608aa2009-07-31 14:52:54 +0000138 gLCDSupportValid = true;
agl@chromium.org309485b2009-07-21 17:41:32 +0000139
140 return true;
141}
142
george@mozilla.comc59b5da2012-08-23 00:39:08 +0000143class SkScalerContext_FreeType : public SkScalerContext_FreeType_Base {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000144public:
145 SkScalerContext_FreeType(const SkDescriptor* desc);
146 virtual ~SkScalerContext_FreeType();
agl@chromium.orgcc3096b2009-04-22 22:09:04 +0000147
reed@android.com62900b42009-02-11 15:07:19 +0000148 bool success() const {
reed@android.coma0f5d152009-06-22 17:38:10 +0000149 return fFaceRec != NULL &&
150 fFTSize != NULL &&
151 fFace != NULL;
reed@android.com62900b42009-02-11 15:07:19 +0000152 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000153
154protected:
ctguil@chromium.org0bc7bf52011-03-04 19:04:57 +0000155 virtual unsigned generateGlyphCount();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000156 virtual uint16_t generateCharToGlyph(SkUnichar uni);
157 virtual void generateAdvance(SkGlyph* glyph);
158 virtual void generateMetrics(SkGlyph* glyph);
bungeman@google.com97efada2012-07-30 20:40:50 +0000159 virtual void generateImage(const SkGlyph& glyph, SkMaskGamma::PreBlend* maskPreBlend);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000160 virtual void generatePath(const SkGlyph& glyph, SkPath* path);
161 virtual void generateFontMetrics(SkPaint::FontMetrics* mx,
162 SkPaint::FontMetrics* my);
reed@android.com9d3a9852010-01-08 14:07:42 +0000163 virtual SkUnichar generateGlyphToChar(uint16_t glyph);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000164
165private:
166 SkFaceRec* fFaceRec;
167 FT_Face fFace; // reference to shared face in gFaceRecHead
168 FT_Size fFTSize; // our own copy
169 SkFixed fScaleX, fScaleY;
170 FT_Matrix fMatrix22;
171 uint32_t fLoadGlyphFlags;
reed@google.combdc99882011-11-21 14:36:57 +0000172 bool fDoLinearMetrics;
reed@google.coma1bfa212012-03-08 21:57:12 +0000173 bool fLCDIsVert;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000174
175 FT_Error setupSize();
djsollen@google.comd8b599c2012-03-19 19:44:19 +0000176 void getBBoxForCurrentGlyph(SkGlyph* glyph, FT_BBox* bbox,
177 bool snapToPixelBoundary = false);
178 void updateGlyphIfLCD(SkGlyph* glyph);
bungeman@google.com875eb982012-04-12 15:53:23 +0000179 void updateGlyphPosIfLCD(SkGlyph* glyph);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000180};
181
182///////////////////////////////////////////////////////////////////////////
183///////////////////////////////////////////////////////////////////////////
184
185#include "SkStream.h"
186
187struct SkFaceRec {
188 SkFaceRec* fNext;
189 FT_Face fFace;
190 FT_StreamRec fFTStream;
191 SkStream* fSkStream;
192 uint32_t fRefCnt;
193 uint32_t fFontID;
194
reed@android.comb1d9d2e2009-03-04 17:37:51 +0000195 // assumes ownership of the stream, will call unref() when its done
reed@android.com8a1c16f2008-12-17 15:59:43 +0000196 SkFaceRec(SkStream* strm, uint32_t fontID);
197 ~SkFaceRec() {
reed@android.comb1d9d2e2009-03-04 17:37:51 +0000198 fSkStream->unref();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000199 }
200};
201
202extern "C" {
203 static unsigned long sk_stream_read(FT_Stream stream,
204 unsigned long offset,
205 unsigned char* buffer,
206 unsigned long count ) {
207 SkStream* str = (SkStream*)stream->descriptor.pointer;
208
209 if (count) {
210 if (!str->rewind()) {
211 return 0;
212 } else {
213 unsigned long ret;
214 if (offset) {
215 ret = str->read(NULL, offset);
216 if (ret != offset) {
217 return 0;
218 }
219 }
220 ret = str->read(buffer, count);
221 if (ret != count) {
222 return 0;
223 }
224 count = ret;
225 }
226 }
227 return count;
228 }
229
230 static void sk_stream_close( FT_Stream stream) {}
231}
232
233SkFaceRec::SkFaceRec(SkStream* strm, uint32_t fontID)
vandebo@chromium.org9af25f32012-03-28 21:24:27 +0000234 : fNext(NULL), fSkStream(strm), fRefCnt(1), fFontID(fontID) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000235// SkDEBUGF(("SkFaceRec: opening %s (%p)\n", key.c_str(), strm));
236
reed@android.com4516f472009-06-29 16:25:36 +0000237 sk_bzero(&fFTStream, sizeof(fFTStream));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000238 fFTStream.size = fSkStream->getLength();
239 fFTStream.descriptor.pointer = fSkStream;
240 fFTStream.read = sk_stream_read;
241 fFTStream.close = sk_stream_close;
242}
243
reed@android.com62900b42009-02-11 15:07:19 +0000244// Will return 0 on failure
reed@android.com8a1c16f2008-12-17 15:59:43 +0000245static SkFaceRec* ref_ft_face(uint32_t fontID) {
246 SkFaceRec* rec = gFaceRecHead;
247 while (rec) {
248 if (rec->fFontID == fontID) {
249 SkASSERT(rec->fFace);
250 rec->fRefCnt += 1;
251 return rec;
252 }
253 rec = rec->fNext;
254 }
255
256 SkStream* strm = SkFontHost::OpenStream(fontID);
257 if (NULL == strm) {
258 SkDEBUGF(("SkFontHost::OpenStream failed opening %x\n", fontID));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000259 return 0;
260 }
261
262 // this passes ownership of strm to the rec
263 rec = SkNEW_ARGS(SkFaceRec, (strm, fontID));
264
265 FT_Open_Args args;
266 memset(&args, 0, sizeof(args));
267 const void* memoryBase = strm->getMemoryBase();
268
269 if (NULL != memoryBase) {
270//printf("mmap(%s)\n", keyString.c_str());
271 args.flags = FT_OPEN_MEMORY;
272 args.memory_base = (const FT_Byte*)memoryBase;
273 args.memory_size = strm->getLength();
274 } else {
275//printf("fopen(%s)\n", keyString.c_str());
276 args.flags = FT_OPEN_STREAM;
277 args.stream = &rec->fFTStream;
278 }
279
agl@chromium.org61a678a2010-08-06 18:08:18 +0000280 int face_index;
281 int length = SkFontHost::GetFileName(fontID, NULL, 0, &face_index);
282 FT_Error err = FT_Open_Face(gFTLibrary, &args, length ? face_index : 0,
283 &rec->fFace);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000284
285 if (err) { // bad filename, try the default font
286 fprintf(stderr, "ERROR: unable to open font '%x'\n", fontID);
287 SkDELETE(rec);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000288 return 0;
289 } else {
290 SkASSERT(rec->fFace);
291 //fprintf(stderr, "Opened font '%s'\n", filename.c_str());
292 rec->fNext = gFaceRecHead;
293 gFaceRecHead = rec;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000294 return rec;
295 }
296}
297
298static void unref_ft_face(FT_Face face) {
299 SkFaceRec* rec = gFaceRecHead;
300 SkFaceRec* prev = NULL;
301 while (rec) {
302 SkFaceRec* next = rec->fNext;
303 if (rec->fFace == face) {
304 if (--rec->fRefCnt == 0) {
305 if (prev) {
306 prev->fNext = next;
307 } else {
308 gFaceRecHead = next;
309 }
310 FT_Done_Face(face);
311 SkDELETE(rec);
312 }
313 return;
314 }
315 prev = rec;
316 rec = next;
317 }
tomhudson@google.com0c00f212011-12-28 14:59:50 +0000318 SkDEBUGFAIL("shouldn't get here, face not in list");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000319}
320
321///////////////////////////////////////////////////////////////////////////
322
vandebo@chromium.org16be6b82011-01-28 21:28:56 +0000323// Work around for old versions of freetype.
324static FT_Error getAdvances(FT_Face face, FT_UInt start, FT_UInt count,
325 FT_Int32 loadFlags, FT_Fixed* advances) {
326#ifdef FT_ADVANCES_H
327 return FT_Get_Advances(face, start, count, loadFlags, advances);
328#else
329 if (!face || start >= face->num_glyphs ||
330 start + count > face->num_glyphs || loadFlags != FT_LOAD_NO_SCALE) {
331 return 6; // "Invalid argument."
332 }
333 if (count == 0)
334 return 0;
335
336 for (int i = 0; i < count; i++) {
337 FT_Error err = FT_Load_Glyph(face, start + i, FT_LOAD_NO_SCALE);
338 if (err)
339 return err;
340 advances[i] = face->glyph->advance.x;
341 }
342
343 return 0;
344#endif
345}
346
347static bool canEmbed(FT_Face face) {
djsollen@google.comfa394d42012-01-09 20:40:25 +0000348#ifdef FT_FSTYPE_RESTRICTED_LICENSE_EMBEDDING
vandebo@chromium.org16be6b82011-01-28 21:28:56 +0000349 FT_UShort fsType = FT_Get_FSType_Flags(face);
350 return (fsType & (FT_FSTYPE_RESTRICTED_LICENSE_EMBEDDING |
351 FT_FSTYPE_BITMAP_EMBEDDING_ONLY)) == 0;
352#else
353 // No embedding is 0x2 and bitmap embedding only is 0x200.
354 TT_OS2* os2_table;
355 if ((os2_table = (TT_OS2*)FT_Get_Sfnt_Table(face, ft_sfnt_os2)) != NULL) {
356 return (os2_table->fsType & 0x202) == 0;
357 }
358 return false; // We tried, fail safe.
359#endif
360}
361
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000362static bool GetLetterCBox(FT_Face face, char letter, FT_BBox* bbox) {
363 const FT_UInt glyph_id = FT_Get_Char_Index(face, letter);
364 if (!glyph_id)
365 return false;
366 FT_Load_Glyph(face, glyph_id, FT_LOAD_NO_SCALE);
367 FT_Outline_Get_CBox(&face->glyph->outline, bbox);
368 return true;
369}
370
vandebo@chromium.org6f72d1e2011-02-14 23:19:59 +0000371static bool getWidthAdvance(FT_Face face, int gId, int16_t* data) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000372 FT_Fixed advance = 0;
vandebo@chromium.org6f72d1e2011-02-14 23:19:59 +0000373 if (getAdvances(face, gId, 1, FT_LOAD_NO_SCALE, &advance)) {
374 return false;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000375 }
vandebo@chromium.org6f72d1e2011-02-14 23:19:59 +0000376 SkASSERT(data);
377 *data = advance;
378 return true;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000379}
380
vandebo@chromium.org6744d492011-05-09 18:13:47 +0000381static void populate_glyph_to_unicode(FT_Face& face,
382 SkTDArray<SkUnichar>* glyphToUnicode) {
383 // Check and see if we have Unicode cmaps.
384 for (int i = 0; i < face->num_charmaps; ++i) {
385 // CMaps known to support Unicode:
386 // Platform ID Encoding ID Name
387 // ----------- ----------- -----------------------------------
388 // 0 0,1 Apple Unicode
389 // 0 3 Apple Unicode 2.0 (preferred)
390 // 3 1 Microsoft Unicode UCS-2
391 // 3 10 Microsoft Unicode UCS-4 (preferred)
392 //
393 // See Apple TrueType Reference Manual
394 // http://developer.apple.com/fonts/TTRefMan/RM06/Chap6cmap.html
395 // http://developer.apple.com/fonts/TTRefMan/RM06/Chap6name.html#ID
396 // Microsoft OpenType Specification
397 // http://www.microsoft.com/typography/otspec/cmap.htm
398
399 FT_UShort platformId = face->charmaps[i]->platform_id;
400 FT_UShort encodingId = face->charmaps[i]->encoding_id;
401
402 if (platformId != 0 && platformId != 3) {
403 continue;
404 }
405 if (platformId == 3 && encodingId != 1 && encodingId != 10) {
406 continue;
407 }
408 bool preferredMap = ((platformId == 3 && encodingId == 10) ||
409 (platformId == 0 && encodingId == 3));
410
411 FT_Set_Charmap(face, face->charmaps[i]);
412 if (glyphToUnicode->isEmpty()) {
413 glyphToUnicode->setCount(face->num_glyphs);
414 memset(glyphToUnicode->begin(), 0,
415 sizeof(SkUnichar) * face->num_glyphs);
416 }
417
418 // Iterate through each cmap entry.
419 FT_UInt glyphIndex;
420 for (SkUnichar charCode = FT_Get_First_Char(face, &glyphIndex);
421 glyphIndex != 0;
422 charCode = FT_Get_Next_Char(face, charCode, &glyphIndex)) {
423 if (charCode &&
424 ((*glyphToUnicode)[glyphIndex] == 0 || preferredMap)) {
425 (*glyphToUnicode)[glyphIndex] = charCode;
426 }
427 }
428 }
429}
430
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000431// static
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000432SkAdvancedTypefaceMetrics* SkFontHost::GetAdvancedTypefaceMetrics(
vandebo@chromium.org325cb9a2011-03-30 18:36:29 +0000433 uint32_t fontID,
vandebo@chromium.org37ad8fb2011-08-18 02:38:50 +0000434 SkAdvancedTypefaceMetrics::PerGlyphInfo perGlyphInfo,
435 const uint32_t* glyphIDs,
436 uint32_t glyphIDsCount) {
djsollen@google.comda957722011-11-16 17:00:46 +0000437#if defined(SK_BUILD_FOR_MAC)
reed@google.com8a5d6922011-03-14 15:08:03 +0000438 return NULL;
439#else
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000440 SkAutoMutexAcquire ac(gFTMutex);
441 FT_Library libInit = NULL;
442 if (gFTCount == 0) {
443 if (!InitFreetype())
444 sk_throw();
445 libInit = gFTLibrary;
446 }
447 SkAutoTCallIProc<struct FT_LibraryRec_, FT_Done_FreeType> ftLib(libInit);
448 SkFaceRec* rec = ref_ft_face(fontID);
449 if (NULL == rec)
450 return NULL;
451 FT_Face face = rec->fFace;
452
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000453 SkAdvancedTypefaceMetrics* info = new SkAdvancedTypefaceMetrics;
454 info->fFontName.set(FT_Get_Postscript_Name(face));
455 info->fMultiMaster = FT_HAS_MULTIPLE_MASTERS(face);
456 info->fLastGlyphID = face->num_glyphs - 1;
457 info->fEmSize = 1000;
458
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000459 bool cid = false;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000460 const char* fontType = FT_Get_X11_Font_Format(face);
vandebo@chromium.orgc3a2ae52011-02-03 21:48:23 +0000461 if (strcmp(fontType, "Type 1") == 0) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000462 info->fType = SkAdvancedTypefaceMetrics::kType1_Font;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000463 } else if (strcmp(fontType, "CID Type 1") == 0) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000464 info->fType = SkAdvancedTypefaceMetrics::kType1CID_Font;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000465 cid = true;
466 } else if (strcmp(fontType, "CFF") == 0) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000467 info->fType = SkAdvancedTypefaceMetrics::kCFF_Font;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000468 } else if (strcmp(fontType, "TrueType") == 0) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000469 info->fType = SkAdvancedTypefaceMetrics::kTrueType_Font;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000470 cid = true;
471 TT_Header* ttHeader;
472 if ((ttHeader = (TT_Header*)FT_Get_Sfnt_Table(face,
473 ft_sfnt_head)) != NULL) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000474 info->fEmSize = ttHeader->Units_Per_EM;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000475 }
476 }
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000477
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000478 info->fStyle = 0;
479 if (FT_IS_FIXED_WIDTH(face))
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000480 info->fStyle |= SkAdvancedTypefaceMetrics::kFixedPitch_Style;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000481 if (face->style_flags & FT_STYLE_FLAG_ITALIC)
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000482 info->fStyle |= SkAdvancedTypefaceMetrics::kItalic_Style;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000483 // We should set either Symbolic or Nonsymbolic; Nonsymbolic if the font's
484 // character set is a subset of 'Adobe standard Latin.'
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000485 info->fStyle |= SkAdvancedTypefaceMetrics::kSymbolic_Style;
486
487 PS_FontInfoRec ps_info;
488 TT_Postscript* tt_info;
489 if (FT_Get_PS_Font_Info(face, &ps_info) == 0) {
490 info->fItalicAngle = ps_info.italic_angle;
491 } else if ((tt_info =
492 (TT_Postscript*)FT_Get_Sfnt_Table(face,
493 ft_sfnt_post)) != NULL) {
494 info->fItalicAngle = SkFixedToScalar(tt_info->italicAngle);
495 } else {
496 info->fItalicAngle = 0;
497 }
498
499 info->fAscent = face->ascender;
500 info->fDescent = face->descender;
501
502 // Figure out a good guess for StemV - Min width of i, I, !, 1.
503 // This probably isn't very good with an italic font.
504 int16_t min_width = SHRT_MAX;
vandebo@chromium.org6f72d1e2011-02-14 23:19:59 +0000505 info->fStemV = 0;
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000506 char stem_chars[] = {'i', 'I', '!', '1'};
507 for (size_t i = 0; i < SK_ARRAY_COUNT(stem_chars); i++) {
508 FT_BBox bbox;
509 if (GetLetterCBox(face, stem_chars[i], &bbox)) {
510 int16_t width = bbox.xMax - bbox.xMin;
511 if (width > 0 && width < min_width) {
512 min_width = width;
513 info->fStemV = min_width;
514 }
515 }
516 }
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000517
518 TT_PCLT* pclt_info;
519 TT_OS2* os2_table;
520 if ((pclt_info = (TT_PCLT*)FT_Get_Sfnt_Table(face, ft_sfnt_pclt)) != NULL) {
521 info->fCapHeight = pclt_info->CapHeight;
522 uint8_t serif_style = pclt_info->SerifStyle & 0x3F;
523 if (serif_style >= 2 && serif_style <= 6)
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000524 info->fStyle |= SkAdvancedTypefaceMetrics::kSerif_Style;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000525 else if (serif_style >= 9 && serif_style <= 12)
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000526 info->fStyle |= SkAdvancedTypefaceMetrics::kScript_Style;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000527 } else if ((os2_table =
528 (TT_OS2*)FT_Get_Sfnt_Table(face, ft_sfnt_os2)) != NULL) {
529 info->fCapHeight = os2_table->sCapHeight;
530 } else {
531 // Figure out a good guess for CapHeight: average the height of M and X.
532 FT_BBox m_bbox, x_bbox;
533 bool got_m, got_x;
534 got_m = GetLetterCBox(face, 'M', &m_bbox);
535 got_x = GetLetterCBox(face, 'X', &x_bbox);
536 if (got_m && got_x) {
537 info->fCapHeight = (m_bbox.yMax - m_bbox.yMin + x_bbox.yMax -
538 x_bbox.yMin) / 2;
539 } else if (got_m && !got_x) {
540 info->fCapHeight = m_bbox.yMax - m_bbox.yMin;
541 } else if (!got_m && got_x) {
542 info->fCapHeight = x_bbox.yMax - x_bbox.yMin;
543 }
544 }
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000545
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000546 info->fBBox = SkIRect::MakeLTRB(face->bbox.xMin, face->bbox.yMax,
547 face->bbox.xMax, face->bbox.yMin);
548
vandebo@chromium.org37ad8fb2011-08-18 02:38:50 +0000549 if (!canEmbed(face) || !FT_IS_SCALABLE(face) ||
vandebo@chromium.org325cb9a2011-03-30 18:36:29 +0000550 info->fType == SkAdvancedTypefaceMetrics::kOther_Font) {
551 perGlyphInfo = SkAdvancedTypefaceMetrics::kNo_PerGlyphInfo;
552 }
553
554 if (perGlyphInfo & SkAdvancedTypefaceMetrics::kHAdvance_PerGlyphInfo) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000555 if (FT_IS_FIXED_WIDTH(face)) {
556 appendRange(&info->fGlyphWidths, 0);
557 int16_t advance = face->max_advance_width;
558 info->fGlyphWidths->fAdvance.append(1, &advance);
559 finishRange(info->fGlyphWidths.get(), 0,
560 SkAdvancedTypefaceMetrics::WidthRange::kDefault);
vandebo@chromium.org6f72d1e2011-02-14 23:19:59 +0000561 } else if (!cid) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000562 appendRange(&info->fGlyphWidths, 0);
563 // So as to not blow out the stack, get advances in batches.
564 for (int gID = 0; gID < face->num_glyphs; gID += 128) {
565 FT_Fixed advances[128];
566 int advanceCount = 128;
567 if (gID + advanceCount > face->num_glyphs)
568 advanceCount = face->num_glyphs - gID + 1;
569 getAdvances(face, gID, advanceCount, FT_LOAD_NO_SCALE,
570 advances);
571 for (int i = 0; i < advanceCount; i++) {
572 int16_t advance = advances[gID + i];
573 info->fGlyphWidths->fAdvance.append(1, &advance);
574 }
575 }
576 finishRange(info->fGlyphWidths.get(), face->num_glyphs - 1,
577 SkAdvancedTypefaceMetrics::WidthRange::kRange);
578 } else {
vandebo@chromium.org6f72d1e2011-02-14 23:19:59 +0000579 info->fGlyphWidths.reset(
vandebo@chromium.org37ad8fb2011-08-18 02:38:50 +0000580 getAdvanceData(face,
581 face->num_glyphs,
582 glyphIDs,
583 glyphIDsCount,
584 &getWidthAdvance));
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000585 }
vandebo@chromium.org325cb9a2011-03-30 18:36:29 +0000586 }
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000587
vandebo@chromium.org325cb9a2011-03-30 18:36:29 +0000588 if (perGlyphInfo & SkAdvancedTypefaceMetrics::kVAdvance_PerGlyphInfo &&
589 FT_HAS_VERTICAL(face)) {
590 SkASSERT(false); // Not implemented yet.
591 }
592
593 if (perGlyphInfo & SkAdvancedTypefaceMetrics::kGlyphNames_PerGlyphInfo &&
594 info->fType == SkAdvancedTypefaceMetrics::kType1_Font) {
595 // Postscript fonts may contain more than 255 glyphs, so we end up
596 // using multiple font descriptions with a glyph ordering. Record
597 // the name of each glyph.
598 info->fGlyphNames.reset(
599 new SkAutoTArray<SkString>(face->num_glyphs));
600 for (int gID = 0; gID < face->num_glyphs; gID++) {
601 char glyphName[128]; // PS limit for names is 127 bytes.
602 FT_Get_Glyph_Name(face, gID, glyphName, 128);
603 info->fGlyphNames->get()[gID].set(glyphName);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000604 }
605 }
606
vandebo@chromium.org6744d492011-05-09 18:13:47 +0000607 if (perGlyphInfo & SkAdvancedTypefaceMetrics::kToUnicode_PerGlyphInfo &&
608 info->fType != SkAdvancedTypefaceMetrics::kType1_Font &&
609 face->num_charmaps) {
610 populate_glyph_to_unicode(face, &(info->fGlyphToUnicode));
611 }
612
vandebo@chromium.orgc3a2ae52011-02-03 21:48:23 +0000613 if (!canEmbed(face))
614 info->fType = SkAdvancedTypefaceMetrics::kNotEmbeddable_Font;
615
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000616 unref_ft_face(face);
617 return info;
reed@google.com8a5d6922011-03-14 15:08:03 +0000618#endif
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000619}
vandebo@chromium.org37ad8fb2011-08-18 02:38:50 +0000620
reed@google.com618ef5e2011-01-26 22:10:41 +0000621///////////////////////////////////////////////////////////////////////////
622
reed@google.comffe49f52011-11-22 19:42:41 +0000623#define BLACK_LUMINANCE_LIMIT 0x40
624#define WHITE_LUMINANCE_LIMIT 0xA0
625
reed@google.com8ed436c2011-07-21 14:12:36 +0000626static bool bothZero(SkScalar a, SkScalar b) {
627 return 0 == a && 0 == b;
628}
629
630// returns false if there is any non-90-rotation or skew
631static bool isAxisAligned(const SkScalerContext::Rec& rec) {
632 return 0 == rec.fPreSkewX &&
633 (bothZero(rec.fPost2x2[0][1], rec.fPost2x2[1][0]) ||
634 bothZero(rec.fPost2x2[0][0], rec.fPost2x2[1][1]));
635}
636
reed@google.com618ef5e2011-01-26 22:10:41 +0000637void SkFontHost::FilterRec(SkScalerContext::Rec* rec) {
bungeman@google.com8cf32262012-04-02 14:34:30 +0000638 //BOGUS: http://code.google.com/p/chromium/issues/detail?id=121119
639 //Cap the requested size as larger sizes give bogus values.
640 //Remove when http://code.google.com/p/skia/issues/detail?id=554 is fixed.
bungeman@google.com5582e632012-04-02 14:51:54 +0000641 if (rec->fTextSize > SkIntToScalar(1 << 14)) {
642 rec->fTextSize = SkIntToScalar(1 << 14);
bungeman@google.com8cf32262012-04-02 14:34:30 +0000643 }
skia.committer@gmail.coma27096b2012-08-30 14:38:00 +0000644
reed@google.com618ef5e2011-01-26 22:10:41 +0000645 if (!gLCDSupportValid) {
646 InitFreetype();
647 FT_Done_FreeType(gFTLibrary);
648 }
reed@google.com5b31b0f2011-02-23 14:41:42 +0000649
reed@google.comeffc5012011-06-27 16:44:46 +0000650 if (!gLCDSupport && isLCD(*rec)) {
reed@google.com618ef5e2011-01-26 22:10:41 +0000651 // If the runtime Freetype library doesn't support LCD mode, we disable
652 // it here.
653 rec->fMaskFormat = SkMask::kA8_Format;
654 }
reed@google.com5b31b0f2011-02-23 14:41:42 +0000655
reed@google.com618ef5e2011-01-26 22:10:41 +0000656 SkPaint::Hinting h = rec->getHinting();
reed@google.comeffc5012011-06-27 16:44:46 +0000657 if (SkPaint::kFull_Hinting == h && !isLCD(*rec)) {
reed@google.com618ef5e2011-01-26 22:10:41 +0000658 // collapse full->normal hinting if we're not doing LCD
659 h = SkPaint::kNormal_Hinting;
reed@google.com618ef5e2011-01-26 22:10:41 +0000660 }
bungeman@google.comf4f2b802012-03-08 19:19:51 +0000661 if ((rec->fFlags & SkScalerContext::kSubpixelPositioning_Flag)) {
reed@google.com1ac83502012-02-28 17:06:02 +0000662 if (SkPaint::kNo_Hinting != h) {
663 h = SkPaint::kSlight_Hinting;
664 }
665 }
666
vandebo@chromium.org37ad8fb2011-08-18 02:38:50 +0000667#ifndef SK_IGNORE_ROTATED_FREETYPE_FIX
reed@google.com8ed436c2011-07-21 14:12:36 +0000668 // rotated text looks bad with hinting, so we disable it as needed
669 if (!isAxisAligned(*rec)) {
670 h = SkPaint::kNo_Hinting;
671 }
bsalomon@google.com0e35ca82011-07-22 17:56:19 +0000672#endif
reed@google.com618ef5e2011-01-26 22:10:41 +0000673 rec->setHinting(h);
reed@google.comffe49f52011-11-22 19:42:41 +0000674
bungeman@google.com97efada2012-07-30 20:40:50 +0000675#ifndef SK_GAMMA_APPLY_TO_A8
676 if (!isLCD(*rec)) {
677 rec->ignorePreBlend();
reed@google.comffe49f52011-11-22 19:42:41 +0000678 }
reed@google.com1ac83502012-02-28 17:06:02 +0000679#endif
reed@google.com618ef5e2011-01-26 22:10:41 +0000680}
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000681
djsollen@google.comda957722011-11-16 17:00:46 +0000682#ifdef SK_BUILD_FOR_ANDROID
djsollen@google.comcd9d69b2011-03-14 20:30:14 +0000683uint32_t SkFontHost::GetUnitsPerEm(SkFontID fontID) {
684 SkAutoMutexAcquire ac(gFTMutex);
685 SkFaceRec *rec = ref_ft_face(fontID);
686 uint16_t unitsPerEm = 0;
687
688 if (rec != NULL && rec->fFace != NULL) {
689 unitsPerEm = rec->fFace->units_per_EM;
690 unref_ft_face(rec->fFace);
691 }
692
693 return (uint32_t)unitsPerEm;
694}
695#endif
696
reed@android.com8a1c16f2008-12-17 15:59:43 +0000697SkScalerContext_FreeType::SkScalerContext_FreeType(const SkDescriptor* desc)
george@mozilla.comc59b5da2012-08-23 00:39:08 +0000698 : SkScalerContext_FreeType_Base(desc) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000699 SkAutoMutexAcquire ac(gFTMutex);
700
reed@android.com8a1c16f2008-12-17 15:59:43 +0000701 if (gFTCount == 0) {
reed@android.com659aaf92009-07-23 15:20:21 +0000702 if (!InitFreetype()) {
703 sk_throw();
704 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000705 }
706 ++gFTCount;
707
708 // load the font file
reed@android.com62900b42009-02-11 15:07:19 +0000709 fFTSize = NULL;
710 fFace = NULL;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000711 fFaceRec = ref_ft_face(fRec.fFontID);
reed@android.com62900b42009-02-11 15:07:19 +0000712 if (NULL == fFaceRec) {
713 return;
714 }
715 fFace = fFaceRec->fFace;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000716
717 // compute our factors from the record
718
719 SkMatrix m;
720
721 fRec.getSingleMatrix(&m);
722
723#ifdef DUMP_STRIKE_CREATION
724 SkString keyString;
725 SkFontHost::GetDescriptorKeyString(desc, &keyString);
726 printf("========== strike [%g %g %g] [%g %g %g %g] hints %d format %d %s\n", SkScalarToFloat(fRec.fTextSize),
727 SkScalarToFloat(fRec.fPreScaleX), SkScalarToFloat(fRec.fPreSkewX),
728 SkScalarToFloat(fRec.fPost2x2[0][0]), SkScalarToFloat(fRec.fPost2x2[0][1]),
729 SkScalarToFloat(fRec.fPost2x2[1][0]), SkScalarToFloat(fRec.fPost2x2[1][1]),
agl@chromium.org309485b2009-07-21 17:41:32 +0000730 fRec.getHinting(), fRec.fMaskFormat, keyString.c_str());
reed@android.com8a1c16f2008-12-17 15:59:43 +0000731#endif
732
733 // now compute our scale factors
734 SkScalar sx = m.getScaleX();
735 SkScalar sy = m.getScaleY();
736
737 if (m.getSkewX() || m.getSkewY() || sx < 0 || sy < 0) {
738 // sort of give up on hinting
739 sx = SkMaxScalar(SkScalarAbs(sx), SkScalarAbs(m.getSkewX()));
740 sy = SkMaxScalar(SkScalarAbs(m.getSkewY()), SkScalarAbs(sy));
741 sx = sy = SkScalarAve(sx, sy);
742
743 SkScalar inv = SkScalarInvert(sx);
744
745 // flip the skew elements to go from our Y-down system to FreeType's
746 fMatrix22.xx = SkScalarToFixed(SkScalarMul(m.getScaleX(), inv));
747 fMatrix22.xy = -SkScalarToFixed(SkScalarMul(m.getSkewX(), inv));
748 fMatrix22.yx = -SkScalarToFixed(SkScalarMul(m.getSkewY(), inv));
749 fMatrix22.yy = SkScalarToFixed(SkScalarMul(m.getScaleY(), inv));
750 } else {
751 fMatrix22.xx = fMatrix22.yy = SK_Fixed1;
752 fMatrix22.xy = fMatrix22.yx = 0;
753 }
754
reed@google.com1f1543f2012-09-12 21:08:33 +0000755#ifdef SK_SUPPORT_HINTING_SCALE_FACTOR
reed@google.com9548be32012-09-14 16:00:01 +0000756 if (fRec.getHinting() == SkPaint::kNo_Hinting) {
757 fScaleX = SkScalarToFixed(sx);
758 fScaleY = SkScalarToFixed(sy);
759 } else {
760 SkScalar hintingScaleFactor = fRec.fHintingScaleFactor;
reed@google.com1f1543f2012-09-12 21:08:33 +0000761
reed@google.com9548be32012-09-14 16:00:01 +0000762 fScaleX = SkScalarToFixed(sx / hintingScaleFactor);
763 fScaleY = SkScalarToFixed(sy / hintingScaleFactor);
reed@google.com1f1543f2012-09-12 21:08:33 +0000764
reed@google.com9548be32012-09-14 16:00:01 +0000765 fMatrix22.xx *= hintingScaleFactor;
766 fMatrix22.xy *= hintingScaleFactor;
767 fMatrix22.yx *= hintingScaleFactor;
768 fMatrix22.yy *= hintingScaleFactor;
769 }
reed@google.com1f1543f2012-09-12 21:08:33 +0000770#else
reed@android.com8a1c16f2008-12-17 15:59:43 +0000771 fScaleX = SkScalarToFixed(sx);
772 fScaleY = SkScalarToFixed(sy);
reed@google.com1f1543f2012-09-12 21:08:33 +0000773#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000774
reed@google.coma1bfa212012-03-08 21:57:12 +0000775 fLCDIsVert = SkToBool(fRec.fFlags & SkScalerContext::kLCD_Vertical_Flag);
776
reed@android.com8a1c16f2008-12-17 15:59:43 +0000777 // compute the flags we send to Load_Glyph
778 {
reed@android.come4d0bc02009-07-24 19:53:20 +0000779 FT_Int32 loadFlags = FT_LOAD_DEFAULT;
bungeman@google.com34f10262012-03-23 18:11:47 +0000780 bool linearMetrics = SkToBool(fRec.fFlags & SkScalerContext::kSubpixelPositioning_Flag);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000781
agl@chromium.org70a303f2010-05-10 14:15:50 +0000782 if (SkMask::kBW_Format == fRec.fMaskFormat) {
783 // See http://code.google.com/p/chromium/issues/detail?id=43252#c24
784 loadFlags = FT_LOAD_TARGET_MONO;
reed@google.comeffc5012011-06-27 16:44:46 +0000785 if (fRec.getHinting() == SkPaint::kNo_Hinting) {
agl@chromium.org70a303f2010-05-10 14:15:50 +0000786 loadFlags = FT_LOAD_NO_HINTING;
reed@google.combdc99882011-11-21 14:36:57 +0000787 linearMetrics = true;
reed@google.comeffc5012011-06-27 16:44:46 +0000788 }
agl@chromium.org70a303f2010-05-10 14:15:50 +0000789 } else {
790 switch (fRec.getHinting()) {
791 case SkPaint::kNo_Hinting:
792 loadFlags = FT_LOAD_NO_HINTING;
reed@google.combdc99882011-11-21 14:36:57 +0000793 linearMetrics = true;
agl@chromium.org70a303f2010-05-10 14:15:50 +0000794 break;
795 case SkPaint::kSlight_Hinting:
796 loadFlags = FT_LOAD_TARGET_LIGHT; // This implies FORCE_AUTOHINT
797 break;
798 case SkPaint::kNormal_Hinting:
agl@chromium.orga2c71cb2010-06-17 20:49:17 +0000799 if (fRec.fFlags & SkScalerContext::kAutohinting_Flag)
800 loadFlags = FT_LOAD_FORCE_AUTOHINT;
801 else
802 loadFlags = FT_LOAD_NO_AUTOHINT;
agl@chromium.org70a303f2010-05-10 14:15:50 +0000803 break;
804 case SkPaint::kFull_Hinting:
agl@chromium.orga2c71cb2010-06-17 20:49:17 +0000805 if (fRec.fFlags & SkScalerContext::kAutohinting_Flag) {
806 loadFlags = FT_LOAD_FORCE_AUTOHINT;
807 break;
808 }
agl@chromium.org70a303f2010-05-10 14:15:50 +0000809 loadFlags = FT_LOAD_TARGET_NORMAL;
reed@google.comeffc5012011-06-27 16:44:46 +0000810 if (isLCD(fRec)) {
reed@google.coma1bfa212012-03-08 21:57:12 +0000811 if (fLCDIsVert) {
reed@google.comeffc5012011-06-27 16:44:46 +0000812 loadFlags = FT_LOAD_TARGET_LCD_V;
813 } else {
814 loadFlags = FT_LOAD_TARGET_LCD;
815 }
reed@google.comea2333d2011-03-14 16:44:56 +0000816 }
agl@chromium.org70a303f2010-05-10 14:15:50 +0000817 break;
818 default:
819 SkDebugf("---------- UNKNOWN hinting %d\n", fRec.getHinting());
820 break;
821 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000822 }
823
reed@google.comeffc5012011-06-27 16:44:46 +0000824 if ((fRec.fFlags & SkScalerContext::kEmbeddedBitmapText_Flag) == 0) {
agl@chromium.orge0d08992009-08-07 19:19:23 +0000825 loadFlags |= FT_LOAD_NO_BITMAP;
reed@google.comeffc5012011-06-27 16:44:46 +0000826 }
agl@chromium.orge0d08992009-08-07 19:19:23 +0000827
reed@google.com96a9f7912011-05-06 11:49:30 +0000828 // Always using FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH to get correct
829 // advances, as fontconfig and cairo do.
830 // See http://code.google.com/p/skia/issues/detail?id=222.
831 loadFlags |= FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH;
832
reed@android.come4d0bc02009-07-24 19:53:20 +0000833 fLoadGlyphFlags = loadFlags;
reed@google.combdc99882011-11-21 14:36:57 +0000834 fDoLinearMetrics = linearMetrics;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000835 }
836
837 // now create the FT_Size
838
839 {
840 FT_Error err;
841
842 err = FT_New_Size(fFace, &fFTSize);
843 if (err != 0) {
844 SkDEBUGF(("SkScalerContext_FreeType::FT_New_Size(%x): FT_Set_Char_Size(0x%x, 0x%x) returned 0x%x\n",
845 fFaceRec->fFontID, fScaleX, fScaleY, err));
846 fFace = NULL;
847 return;
848 }
849
850 err = FT_Activate_Size(fFTSize);
851 if (err != 0) {
852 SkDEBUGF(("SkScalerContext_FreeType::FT_Activate_Size(%x, 0x%x, 0x%x) returned 0x%x\n",
853 fFaceRec->fFontID, fScaleX, fScaleY, err));
854 fFTSize = NULL;
855 }
856
857 err = FT_Set_Char_Size( fFace,
858 SkFixedToFDot6(fScaleX), SkFixedToFDot6(fScaleY),
859 72, 72);
860 if (err != 0) {
861 SkDEBUGF(("SkScalerContext_FreeType::FT_Set_Char_Size(%x, 0x%x, 0x%x) returned 0x%x\n",
862 fFaceRec->fFontID, fScaleX, fScaleY, err));
863 fFace = NULL;
864 return;
865 }
866
867 FT_Set_Transform( fFace, &fMatrix22, NULL);
868 }
869}
870
871SkScalerContext_FreeType::~SkScalerContext_FreeType() {
872 if (fFTSize != NULL) {
873 FT_Done_Size(fFTSize);
874 }
875
876 SkAutoMutexAcquire ac(gFTMutex);
877
878 if (fFace != NULL) {
879 unref_ft_face(fFace);
880 }
881 if (--gFTCount == 0) {
882// SkDEBUGF(("FT_Done_FreeType\n"));
883 FT_Done_FreeType(gFTLibrary);
884 SkDEBUGCODE(gFTLibrary = NULL;)
885 }
886}
887
888/* We call this before each use of the fFace, since we may be sharing
889 this face with other context (at different sizes).
890*/
891FT_Error SkScalerContext_FreeType::setupSize() {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000892 FT_Error err = FT_Activate_Size(fFTSize);
893
894 if (err != 0) {
895 SkDEBUGF(("SkScalerContext_FreeType::FT_Activate_Size(%x, 0x%x, 0x%x) returned 0x%x\n",
896 fFaceRec->fFontID, fScaleX, fScaleY, err));
897 fFTSize = NULL;
898 } else {
899 // seems we need to reset this every time (not sure why, but without it
900 // I get random italics from some other fFTSize)
901 FT_Set_Transform( fFace, &fMatrix22, NULL);
902 }
903 return err;
904}
905
ctguil@chromium.org0bc7bf52011-03-04 19:04:57 +0000906unsigned SkScalerContext_FreeType::generateGlyphCount() {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000907 return fFace->num_glyphs;
908}
909
910uint16_t SkScalerContext_FreeType::generateCharToGlyph(SkUnichar uni) {
911 return SkToU16(FT_Get_Char_Index( fFace, uni ));
912}
913
reed@android.com9d3a9852010-01-08 14:07:42 +0000914SkUnichar SkScalerContext_FreeType::generateGlyphToChar(uint16_t glyph) {
915 // iterate through each cmap entry, looking for matching glyph indices
916 FT_UInt glyphIndex;
917 SkUnichar charCode = FT_Get_First_Char( fFace, &glyphIndex );
918
919 while (glyphIndex != 0) {
920 if (glyphIndex == glyph) {
921 return charCode;
922 }
923 charCode = FT_Get_Next_Char( fFace, charCode, &glyphIndex );
924 }
925
926 return 0;
927}
928
reed@android.com8a1c16f2008-12-17 15:59:43 +0000929void SkScalerContext_FreeType::generateAdvance(SkGlyph* glyph) {
930#ifdef FT_ADVANCES_H
931 /* unhinted and light hinted text have linearly scaled advances
932 * which are very cheap to compute with some font formats...
933 */
reed@google.combdc99882011-11-21 14:36:57 +0000934 if (fDoLinearMetrics) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000935 SkAutoMutexAcquire ac(gFTMutex);
936
937 if (this->setupSize()) {
reed@android.com62900b42009-02-11 15:07:19 +0000938 glyph->zeroMetrics();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000939 return;
940 }
941
942 FT_Error error;
943 FT_Fixed advance;
944
945 error = FT_Get_Advance( fFace, glyph->getGlyphID(fBaseGlyphCount),
946 fLoadGlyphFlags | FT_ADVANCE_FLAG_FAST_ONLY,
947 &advance );
948 if (0 == error) {
949 glyph->fRsbDelta = 0;
950 glyph->fLsbDelta = 0;
reed@google.comd074c372012-07-18 13:45:58 +0000951 glyph->fAdvanceX = SkFixedMul(fMatrix22.xx, advance);
952 glyph->fAdvanceY = - SkFixedMul(fMatrix22.yx, advance);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000953 return;
954 }
955 }
956#endif /* FT_ADVANCES_H */
957 /* otherwise, we need to load/hint the glyph, which is slower */
958 this->generateMetrics(glyph);
959 return;
960}
961
djsollen@google.comd8b599c2012-03-19 19:44:19 +0000962void SkScalerContext_FreeType::getBBoxForCurrentGlyph(SkGlyph* glyph,
963 FT_BBox* bbox,
964 bool snapToPixelBoundary) {
965
966 FT_Outline_Get_CBox(&fFace->glyph->outline, bbox);
967
968 if (fRec.fFlags & SkScalerContext::kSubpixelPositioning_Flag) {
george@mozilla.comc59b5da2012-08-23 00:39:08 +0000969 int dx = SkFixedToFDot6(glyph->getSubXFixed());
970 int dy = SkFixedToFDot6(glyph->getSubYFixed());
djsollen@google.comd8b599c2012-03-19 19:44:19 +0000971 // negate dy since freetype-y-goes-up and skia-y-goes-down
972 bbox->xMin += dx;
973 bbox->yMin -= dy;
974 bbox->xMax += dx;
975 bbox->yMax -= dy;
976 }
977
978 // outset the box to integral boundaries
979 if (snapToPixelBoundary) {
980 bbox->xMin &= ~63;
981 bbox->yMin &= ~63;
982 bbox->xMax = (bbox->xMax + 63) & ~63;
983 bbox->yMax = (bbox->yMax + 63) & ~63;
984 }
985}
986
987void SkScalerContext_FreeType::updateGlyphIfLCD(SkGlyph* glyph) {
988 if (isLCD(fRec)) {
989 if (fLCDIsVert) {
990 glyph->fHeight += gLCDExtra;
991 glyph->fTop -= gLCDExtra >> 1;
992 } else {
993 glyph->fWidth += gLCDExtra;
994 glyph->fLeft -= gLCDExtra >> 1;
995 }
996 }
997}
bungeman@google.com875eb982012-04-12 15:53:23 +0000998void SkScalerContext_FreeType::updateGlyphPosIfLCD(SkGlyph* glyph) {
999 if (isLCD(fRec)) {
1000 if (fLCDIsVert) {
1001 glyph->fTop -= gLCDExtra >> 1;
1002 } else {
1003 glyph->fLeft -= gLCDExtra >> 1;
1004 }
1005 }
1006}
djsollen@google.comd8b599c2012-03-19 19:44:19 +00001007
reed@android.com8a1c16f2008-12-17 15:59:43 +00001008void SkScalerContext_FreeType::generateMetrics(SkGlyph* glyph) {
1009 SkAutoMutexAcquire ac(gFTMutex);
1010
1011 glyph->fRsbDelta = 0;
1012 glyph->fLsbDelta = 0;
1013
1014 FT_Error err;
1015
1016 if (this->setupSize()) {
1017 goto ERROR;
1018 }
1019
1020 err = FT_Load_Glyph( fFace, glyph->getGlyphID(fBaseGlyphCount), fLoadGlyphFlags );
1021 if (err != 0) {
1022 SkDEBUGF(("SkScalerContext_FreeType::generateMetrics(%x): FT_Load_Glyph(glyph:%d flags:%d) returned 0x%x\n",
1023 fFaceRec->fFontID, glyph->getGlyphID(fBaseGlyphCount), fLoadGlyphFlags, err));
1024 ERROR:
reed@android.com62900b42009-02-11 15:07:19 +00001025 glyph->zeroMetrics();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001026 return;
1027 }
1028
vandebo@chromium.org6390c722012-03-28 21:03:22 +00001029 SkFixed vLeft = 0, vTop = 0;
djsollen@google.comd8b599c2012-03-19 19:44:19 +00001030
reed@android.com8a1c16f2008-12-17 15:59:43 +00001031 switch ( fFace->glyph->format ) {
thakis@chromium.org598b8592011-05-24 05:42:55 +00001032 case FT_GLYPH_FORMAT_OUTLINE: {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001033 FT_BBox bbox;
1034
bungeman@google.com0f0c2882011-11-04 15:47:41 +00001035 if (0 == fFace->glyph->outline.n_contours) {
1036 glyph->fWidth = 0;
1037 glyph->fHeight = 0;
1038 glyph->fTop = 0;
1039 glyph->fLeft = 0;
1040 break;
1041 }
1042
senorblanco@chromium.org4526a842010-02-05 23:08:20 +00001043 if (fRec.fFlags & kEmbolden_Flag) {
george@mozilla.comc59b5da2012-08-23 00:39:08 +00001044 emboldenOutline(fFace, &fFace->glyph->outline);
senorblanco@chromium.org4526a842010-02-05 23:08:20 +00001045 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001046
djsollen@google.comd8b599c2012-03-19 19:44:19 +00001047 getBBoxForCurrentGlyph(glyph, &bbox, true);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001048
1049 glyph->fWidth = SkToU16((bbox.xMax - bbox.xMin) >> 6);
1050 glyph->fHeight = SkToU16((bbox.yMax - bbox.yMin) >> 6);
1051 glyph->fTop = -SkToS16(bbox.yMax >> 6);
1052 glyph->fLeft = SkToS16(bbox.xMin >> 6);
reed@google.coma1c32562012-03-01 19:38:23 +00001053
djsollen@google.comd8b599c2012-03-19 19:44:19 +00001054 if ((fRec.fFlags & SkScalerContext::kVertical_Flag)) {
george@mozilla.comc59b5da2012-08-23 00:39:08 +00001055 vLeft = SkFDot6ToFixed(bbox.xMin);
1056 vTop = SkFDot6ToFixed(bbox.yMax);
reed@google.coma1c32562012-03-01 19:38:23 +00001057 }
djsollen@google.comd8b599c2012-03-19 19:44:19 +00001058
1059 updateGlyphIfLCD(glyph);
1060
reed@android.com8a1c16f2008-12-17 15:59:43 +00001061 break;
thakis@chromium.org598b8592011-05-24 05:42:55 +00001062 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001063
1064 case FT_GLYPH_FORMAT_BITMAP:
agl@chromium.orge76073b2010-06-04 20:31:17 +00001065 if (fRec.fFlags & kEmbolden_Flag) {
1066 FT_GlyphSlot_Own_Bitmap(fFace->glyph);
1067 FT_Bitmap_Embolden(gFTLibrary, &fFace->glyph->bitmap, kBitmapEmboldenStrength, 0);
1068 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001069 glyph->fWidth = SkToU16(fFace->glyph->bitmap.width);
1070 glyph->fHeight = SkToU16(fFace->glyph->bitmap.rows);
1071 glyph->fTop = -SkToS16(fFace->glyph->bitmap_top);
1072 glyph->fLeft = SkToS16(fFace->glyph->bitmap_left);
1073 break;
1074
1075 default:
tomhudson@google.com0c00f212011-12-28 14:59:50 +00001076 SkDEBUGFAIL("unknown glyph format");
reed@android.com8a1c16f2008-12-17 15:59:43 +00001077 goto ERROR;
1078 }
1079
bungeman@google.com34f10262012-03-23 18:11:47 +00001080 if (fDoLinearMetrics) {
1081 glyph->fAdvanceX = SkFixedMul(fMatrix22.xx, fFace->glyph->linearHoriAdvance);
1082 glyph->fAdvanceY = -SkFixedMul(fMatrix22.yx, fFace->glyph->linearHoriAdvance);
1083 } else {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001084 glyph->fAdvanceX = SkFDot6ToFixed(fFace->glyph->advance.x);
1085 glyph->fAdvanceY = -SkFDot6ToFixed(fFace->glyph->advance.y);
bungeman@google.com34f10262012-03-23 18:11:47 +00001086
reed@android.com8a1c16f2008-12-17 15:59:43 +00001087 if (fRec.fFlags & kDevKernText_Flag) {
1088 glyph->fRsbDelta = SkToS8(fFace->glyph->rsb_delta);
1089 glyph->fLsbDelta = SkToS8(fFace->glyph->lsb_delta);
1090 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001091 }
1092
djsollen@google.comd8b599c2012-03-19 19:44:19 +00001093 if ((fRec.fFlags & SkScalerContext::kVertical_Flag)
1094 && fFace->glyph->format == FT_GLYPH_FORMAT_OUTLINE) {
1095
1096 //TODO: do we need to specially handle SubpixelPositioning and Kerning?
1097
1098 FT_Matrix identityMatrix;
1099 identityMatrix.xx = identityMatrix.yy = SK_Fixed1;
1100 identityMatrix.xy = identityMatrix.yx = 0;
1101
1102 // if the matrix is not the identity matrix then we need to re-load the
1103 // glyph with the identity matrix to get the necessary bounding box
1104 if (memcmp(&fMatrix22, &identityMatrix, sizeof(FT_Matrix)) != 0) {
1105
1106 FT_Set_Transform(fFace, &identityMatrix, NULL);
1107
1108 err = FT_Load_Glyph( fFace, glyph->getGlyphID(fBaseGlyphCount), fLoadGlyphFlags );
1109 if (err != 0) {
1110 SkDEBUGF(("SkScalerContext_FreeType::generateMetrics(%x): FT_Load_Glyph(glyph:%d flags:%d) returned 0x%x\n",
1111 fFaceRec->fFontID, glyph->getGlyphID(fBaseGlyphCount), fLoadGlyphFlags, err));
1112 goto ERROR;
1113 }
1114
1115 if (fRec.fFlags & kEmbolden_Flag) {
george@mozilla.comc59b5da2012-08-23 00:39:08 +00001116 emboldenOutline(fFace, &fFace->glyph->outline);
djsollen@google.comd8b599c2012-03-19 19:44:19 +00001117 }
1118 }
1119
1120 // bounding box of the unskewed and unscaled glyph
vandebo@chromium.org6390c722012-03-28 21:03:22 +00001121 FT_BBox bbox = {0, 0, 0, 0}; // Suppress Coverity warning.
djsollen@google.comd8b599c2012-03-19 19:44:19 +00001122 getBBoxForCurrentGlyph(glyph, &bbox);
1123
1124 // compute the vertical gap above and below the glyph if the glyph were
1125 // centered within the linearVertAdvance
george@mozilla.comc59b5da2012-08-23 00:39:08 +00001126 SkFixed vGap = (fFace->glyph->linearVertAdvance - SkFDot6ToFixed(bbox.yMax - bbox.yMin)) / 2;
djsollen@google.comd8b599c2012-03-19 19:44:19 +00001127
1128 // the origin point of the glyph when rendered vertically
1129 FT_Vector vOrigin;
1130 vOrigin.x = fFace->glyph->linearHoriAdvance / 2;
george@mozilla.comc59b5da2012-08-23 00:39:08 +00001131 vOrigin.y = vGap + SkFDot6ToFixed(bbox.yMax);
djsollen@google.comd8b599c2012-03-19 19:44:19 +00001132
1133 // transform the vertical origin based on the matrix of the actual glyph
1134 FT_Vector_Transform(&vOrigin, &fMatrix22);
1135
1136 // compute a new offset vector for the glyph by subtracting the vertical
1137 // origin from the original horizontal offset vector
1138 glyph->fLeft = SkFixedRoundToInt(vLeft - vOrigin.x);
1139 glyph->fTop = -SkFixedRoundToInt(vTop - vOrigin.y);
1140
bungeman@google.com875eb982012-04-12 15:53:23 +00001141 updateGlyphPosIfLCD(glyph);
djsollen@google.comd8b599c2012-03-19 19:44:19 +00001142
1143 // use the vertical advance values computed by freetype
1144 glyph->fAdvanceX = -SkFixedMul(fMatrix22.xy, fFace->glyph->linearVertAdvance);
1145 glyph->fAdvanceY = SkFixedMul(fMatrix22.yy, fFace->glyph->linearVertAdvance);
1146 }
1147
1148
reed@android.com8a1c16f2008-12-17 15:59:43 +00001149#ifdef ENABLE_GLYPH_SPEW
1150 SkDEBUGF(("FT_Set_Char_Size(this:%p sx:%x sy:%x ", this, fScaleX, fScaleY));
1151 SkDEBUGF(("Metrics(glyph:%d flags:0x%x) w:%d\n", glyph->getGlyphID(fBaseGlyphCount), fLoadGlyphFlags, glyph->fWidth));
1152#endif
1153}
1154
reed@google.comea2333d2011-03-14 16:44:56 +00001155
bungeman@google.com97efada2012-07-30 20:40:50 +00001156void SkScalerContext_FreeType::generateImage(const SkGlyph& glyph, SkMaskGamma::PreBlend* maskPreBlend) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001157 SkAutoMutexAcquire ac(gFTMutex);
1158
1159 FT_Error err;
1160
1161 if (this->setupSize()) {
1162 goto ERROR;
1163 }
1164
1165 err = FT_Load_Glyph( fFace, glyph.getGlyphID(fBaseGlyphCount), fLoadGlyphFlags);
1166 if (err != 0) {
1167 SkDEBUGF(("SkScalerContext_FreeType::generateImage: FT_Load_Glyph(glyph:%d width:%d height:%d rb:%d flags:%d) returned 0x%x\n",
1168 glyph.getGlyphID(fBaseGlyphCount), glyph.fWidth, glyph.fHeight, glyph.rowBytes(), fLoadGlyphFlags, err));
1169 ERROR:
1170 memset(glyph.fImage, 0, glyph.rowBytes() * glyph.fHeight);
1171 return;
1172 }
1173
george@mozilla.comc59b5da2012-08-23 00:39:08 +00001174 generateGlyphImage(fFace, glyph, maskPreBlend);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001175}
1176
reed@android.com8a1c16f2008-12-17 15:59:43 +00001177
1178void SkScalerContext_FreeType::generatePath(const SkGlyph& glyph,
1179 SkPath* path) {
1180 SkAutoMutexAcquire ac(gFTMutex);
1181
1182 SkASSERT(&glyph && path);
1183
1184 if (this->setupSize()) {
1185 path->reset();
1186 return;
1187 }
1188
1189 uint32_t flags = fLoadGlyphFlags;
1190 flags |= FT_LOAD_NO_BITMAP; // ignore embedded bitmaps so we're sure to get the outline
1191 flags &= ~FT_LOAD_RENDER; // don't scan convert (we just want the outline)
1192
1193 FT_Error err = FT_Load_Glyph( fFace, glyph.getGlyphID(fBaseGlyphCount), flags);
1194
1195 if (err != 0) {
1196 SkDEBUGF(("SkScalerContext_FreeType::generatePath: FT_Load_Glyph(glyph:%d flags:%d) returned 0x%x\n",
1197 glyph.getGlyphID(fBaseGlyphCount), flags, err));
1198 path->reset();
1199 return;
1200 }
1201
george@mozilla.comc59b5da2012-08-23 00:39:08 +00001202 generateGlyphPath(fFace, glyph, path);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001203}
1204
1205void SkScalerContext_FreeType::generateFontMetrics(SkPaint::FontMetrics* mx,
1206 SkPaint::FontMetrics* my) {
1207 if (NULL == mx && NULL == my) {
1208 return;
1209 }
1210
1211 SkAutoMutexAcquire ac(gFTMutex);
1212
1213 if (this->setupSize()) {
reed@android.coma8a8b8b2009-05-04 15:00:11 +00001214 ERROR:
reed@android.com8a1c16f2008-12-17 15:59:43 +00001215 if (mx) {
reed@android.com4516f472009-06-29 16:25:36 +00001216 sk_bzero(mx, sizeof(SkPaint::FontMetrics));
reed@android.com8a1c16f2008-12-17 15:59:43 +00001217 }
1218 if (my) {
reed@android.com4516f472009-06-29 16:25:36 +00001219 sk_bzero(my, sizeof(SkPaint::FontMetrics));
reed@android.com8a1c16f2008-12-17 15:59:43 +00001220 }
1221 return;
1222 }
1223
reed@android.coma8a8b8b2009-05-04 15:00:11 +00001224 FT_Face face = fFace;
1225 int upem = face->units_per_EM;
1226 if (upem <= 0) {
1227 goto ERROR;
1228 }
1229
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001230 SkPoint pts[6];
1231 SkFixed ys[6];
reed@android.com8a1c16f2008-12-17 15:59:43 +00001232 SkFixed scaleY = fScaleY;
1233 SkFixed mxy = fMatrix22.xy;
1234 SkFixed myy = fMatrix22.yy;
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001235 SkScalar xmin = SkIntToScalar(face->bbox.xMin) / upem;
1236 SkScalar xmax = SkIntToScalar(face->bbox.xMax) / upem;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001237
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001238 int leading = face->height - (face->ascender + -face->descender);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001239 if (leading < 0) {
1240 leading = 0;
1241 }
1242
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001243 // Try to get the OS/2 table from the font. This contains the specific
1244 // average font width metrics which Windows uses.
1245 TT_OS2* os2 = (TT_OS2*) FT_Get_Sfnt_Table(face, ft_sfnt_os2);
1246
reed@android.com8a1c16f2008-12-17 15:59:43 +00001247 ys[0] = -face->bbox.yMax;
1248 ys[1] = -face->ascender;
1249 ys[2] = -face->descender;
1250 ys[3] = -face->bbox.yMin;
1251 ys[4] = leading;
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001252 ys[5] = os2 ? os2->xAvgCharWidth : 0;
1253
1254 SkScalar x_height;
1255 if (os2 && os2->sxHeight) {
1256 x_height = SkFixedToScalar(SkMulDiv(fScaleX, os2->sxHeight, upem));
1257 } else {
1258 const FT_UInt x_glyph = FT_Get_Char_Index(fFace, 'x');
1259 if (x_glyph) {
1260 FT_BBox bbox;
1261 FT_Load_Glyph(fFace, x_glyph, fLoadGlyphFlags);
senorblanco@chromium.org4526a842010-02-05 23:08:20 +00001262 if (fRec.fFlags & kEmbolden_Flag) {
george@mozilla.comc59b5da2012-08-23 00:39:08 +00001263 emboldenOutline(fFace, &fFace->glyph->outline);
senorblanco@chromium.org4526a842010-02-05 23:08:20 +00001264 }
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001265 FT_Outline_Get_CBox(&fFace->glyph->outline, &bbox);
epoger@google.comb90113d2012-01-18 19:20:39 +00001266 x_height = SkFixedToScalar(SkFDot6ToFixed(bbox.yMax));
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001267 } else {
1268 x_height = 0;
1269 }
1270 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001271
1272 // convert upem-y values into scalar points
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001273 for (int i = 0; i < 6; i++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001274 SkFixed y = SkMulDiv(scaleY, ys[i], upem);
1275 SkFixed x = SkFixedMul(mxy, y);
1276 y = SkFixedMul(myy, y);
1277 pts[i].set(SkFixedToScalar(x), SkFixedToScalar(y));
1278 }
1279
1280 if (mx) {
1281 mx->fTop = pts[0].fX;
1282 mx->fAscent = pts[1].fX;
1283 mx->fDescent = pts[2].fX;
1284 mx->fBottom = pts[3].fX;
1285 mx->fLeading = pts[4].fX;
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001286 mx->fAvgCharWidth = pts[5].fX;
1287 mx->fXMin = xmin;
1288 mx->fXMax = xmax;
1289 mx->fXHeight = x_height;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001290 }
1291 if (my) {
1292 my->fTop = pts[0].fY;
1293 my->fAscent = pts[1].fY;
1294 my->fDescent = pts[2].fY;
1295 my->fBottom = pts[3].fY;
1296 my->fLeading = pts[4].fY;
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001297 my->fAvgCharWidth = pts[5].fY;
1298 my->fXMin = xmin;
1299 my->fXMax = xmax;
1300 my->fXHeight = x_height;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001301 }
1302}
1303
1304////////////////////////////////////////////////////////////////////////
1305////////////////////////////////////////////////////////////////////////
1306
1307SkScalerContext* SkFontHost::CreateScalerContext(const SkDescriptor* desc) {
reed@android.com62900b42009-02-11 15:07:19 +00001308 SkScalerContext_FreeType* c = SkNEW_ARGS(SkScalerContext_FreeType, (desc));
1309 if (!c->success()) {
1310 SkDELETE(c);
1311 c = NULL;
1312 }
1313 return c;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001314}
1315
1316///////////////////////////////////////////////////////////////////////////////
1317
1318/* Export this so that other parts of our FonttHost port can make use of our
1319 ability to extract the name+style from a stream, using FreeType's api.
1320*/
djsollen@google.com4dc686d2012-02-15 21:03:45 +00001321bool find_name_and_attributes(SkStream* stream, SkString* name,
1322 SkTypeface::Style* style, bool* isFixedWidth) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001323 FT_Library library;
reed@android.combfbd4ff2009-07-23 17:44:41 +00001324 if (FT_Init_FreeType(&library)) {
djsollen@google.com4dc686d2012-02-15 21:03:45 +00001325 return false;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001326 }
1327
1328 FT_Open_Args args;
1329 memset(&args, 0, sizeof(args));
1330
1331 const void* memoryBase = stream->getMemoryBase();
1332 FT_StreamRec streamRec;
1333
1334 if (NULL != memoryBase) {
1335 args.flags = FT_OPEN_MEMORY;
1336 args.memory_base = (const FT_Byte*)memoryBase;
1337 args.memory_size = stream->getLength();
1338 } else {
1339 memset(&streamRec, 0, sizeof(streamRec));
1340 streamRec.size = stream->read(NULL, 0);
1341 streamRec.descriptor.pointer = stream;
1342 streamRec.read = sk_stream_read;
1343 streamRec.close = sk_stream_close;
1344
1345 args.flags = FT_OPEN_STREAM;
1346 args.stream = &streamRec;
1347 }
1348
1349 FT_Face face;
1350 if (FT_Open_Face(library, &args, 0, &face)) {
1351 FT_Done_FreeType(library);
djsollen@google.com4dc686d2012-02-15 21:03:45 +00001352 return false;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001353 }
1354
djsollen@google.com4dc686d2012-02-15 21:03:45 +00001355 int tempStyle = SkTypeface::kNormal;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001356 if (face->style_flags & FT_STYLE_FLAG_BOLD) {
djsollen@google.com4dc686d2012-02-15 21:03:45 +00001357 tempStyle |= SkTypeface::kBold;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001358 }
1359 if (face->style_flags & FT_STYLE_FLAG_ITALIC) {
djsollen@google.com4dc686d2012-02-15 21:03:45 +00001360 tempStyle |= SkTypeface::kItalic;
1361 }
1362
1363 if (name) {
1364 name->set(face->family_name);
1365 }
1366 if (style) {
1367 *style = (SkTypeface::Style) tempStyle;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001368 }
reed@google.com5b31b0f2011-02-23 14:41:42 +00001369 if (isFixedWidth) {
1370 *isFixedWidth = FT_IS_FIXED_WIDTH(face);
1371 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001372
1373 FT_Done_Face(face);
1374 FT_Done_FreeType(library);
djsollen@google.com4dc686d2012-02-15 21:03:45 +00001375 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001376}