blob: 97046f4ba89fda49c264225fd9f5ae76b0fa34e6 [file] [log] [blame]
commit-bot@chromium.org6fa81d72013-12-26 15:50:29 +00001
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002/*
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"
bungeman@google.coma9802692013-08-07 02:45:25 +000020#include "SkOTUtils.h"
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +000021#include "SkAdvancedTypefaceMetrics.h"
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000022#include "SkScalerContext.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000023#include "SkStream.h"
24#include "SkString.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000025#include "SkTemplates.h"
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000026#include "SkThread.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000027
bungeman@google.comfd668cf2012-08-24 17:46:11 +000028#if defined(SK_CAN_USE_DLOPEN)
29#include <dlfcn.h>
30#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +000031#include <ft2build.h>
32#include FT_FREETYPE_H
33#include FT_OUTLINE_H
34#include FT_SIZES_H
agl@chromium.orgcc3096b2009-04-22 22:09:04 +000035#include FT_TRUETYPE_TABLES_H
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000036#include FT_TYPE1_TABLES_H
agl@chromium.orge76073b2010-06-04 20:31:17 +000037#include FT_BITMAP_H
agl@chromium.org36bb6972010-06-04 20:57:16 +000038// In the past, FT_GlyphSlot_Own_Bitmap was defined in this header file.
39#include FT_SYNTHESIS_H
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000040#include FT_XFREE86_H
epoger@google.com5070d792011-06-29 20:43:14 +000041#ifdef FT_LCD_FILTER_H
agl@chromium.org309485b2009-07-21 17:41:32 +000042#include FT_LCD_FILTER_H
epoger@google.com5070d792011-06-29 20:43:14 +000043#endif
agl@chromium.org309485b2009-07-21 17:41:32 +000044
reed@android.com8a1c16f2008-12-17 15:59:43 +000045#ifdef FT_ADVANCES_H
46#include FT_ADVANCES_H
47#endif
48
agl@chromium.orgcc3096b2009-04-22 22:09:04 +000049#if 0
50// Also include the files by name for build tools which require this.
51#include <freetype/freetype.h>
52#include <freetype/ftoutln.h>
53#include <freetype/ftsizes.h>
54#include <freetype/tttables.h>
55#include <freetype/ftadvanc.h>
agl@chromium.org309485b2009-07-21 17:41:32 +000056#include <freetype/ftlcdfil.h>
agl@chromium.orge76073b2010-06-04 20:31:17 +000057#include <freetype/ftbitmap.h>
agl@chromium.org36bb6972010-06-04 20:57:16 +000058#include <freetype/ftsynth.h>
agl@chromium.orgcc3096b2009-04-22 22:09:04 +000059#endif
60
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +000061// FT_LOAD_COLOR and the corresponding FT_Pixel_Mode::FT_PIXEL_MODE_BGRA
62// were introduced in FreeType 2.5.0.
63// The following may be removed once FreeType 2.5.0 is required to build.
64#ifndef FT_LOAD_COLOR
65# define FT_LOAD_COLOR ( 1L << 20 )
66# define FT_PIXEL_MODE_BGRA 7
67#endif
68
69// FT_HAS_COLOR and the corresponding FT_FACE_FLAG_COLOR
70// were introduced in FreeType 2.5.1
71// The following may be removed once FreeType 2.5.1 is required to build.
72#ifndef FT_HAS_COLOR
73# define FT_HAS_COLOR(face) false
74#endif
75
reed@android.com8a1c16f2008-12-17 15:59:43 +000076//#define ENABLE_GLYPH_SPEW // for tracing calls
77//#define DUMP_STRIKE_CREATION
78
reed@google.com1ac83502012-02-28 17:06:02 +000079//#define SK_GAMMA_APPLY_TO_A8
reed@google.com1ac83502012-02-28 17:06:02 +000080
vandebo@chromium.org6f72d1e2011-02-14 23:19:59 +000081using namespace skia_advanced_typeface_metrics_utils;
82
reed@google.comeffc5012011-06-27 16:44:46 +000083static bool isLCD(const SkScalerContext::Rec& rec) {
84 switch (rec.fMaskFormat) {
85 case SkMask::kLCD16_Format:
86 case SkMask::kLCD32_Format:
87 return true;
88 default:
89 return false;
90 }
91}
92
reed@android.com8a1c16f2008-12-17 15:59:43 +000093//////////////////////////////////////////////////////////////////////////
94
95struct SkFaceRec;
96
digit@google.com1771cbf2012-01-26 21:26:40 +000097SK_DECLARE_STATIC_MUTEX(gFTMutex);
reed@android.com8a1c16f2008-12-17 15:59:43 +000098static int gFTCount;
99static FT_Library gFTLibrary;
100static SkFaceRec* gFaceRecHead;
agl@chromium.orgf18d8762009-07-28 18:38:08 +0000101static bool gLCDSupportValid; // true iff |gLCDSupport| has been set.
102static bool gLCDSupport; // true iff LCD is supported by the runtime.
reed@google.coma1c32562012-03-01 19:38:23 +0000103static int gLCDExtra; // number of extra pixels for filtering.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000104
bungeman@google.com62566f32012-10-13 03:21:53 +0000105/////////////////////////////////////////////////////////////////////////
106
bungeman@google.comfd668cf2012-08-24 17:46:11 +0000107// FT_Library_SetLcdFilterWeights was introduced in FreeType 2.4.0.
108// The following platforms provide FreeType of at least 2.4.0.
109// Ubuntu >= 11.04 (previous deprecated April 2013)
110// Debian >= 6.0 (good)
111// OpenSuse >= 11.4 (previous deprecated January 2012 / Nov 2013 for Evergreen 11.2)
112// Fedora >= 14 (good)
113// Android >= Gingerbread (good)
114typedef FT_Error (*FT_Library_SetLcdFilterWeightsProc)(FT_Library, unsigned char*);
115
scroggo@google.com94bc60f2012-10-04 20:45:06 +0000116// Caller must lock gFTMutex before calling this function.
bungeman@google.comfd668cf2012-08-24 17:46:11 +0000117static bool InitFreetype() {
agl@chromium.org309485b2009-07-21 17:41:32 +0000118 FT_Error err = FT_Init_FreeType(&gFTLibrary);
reed@google.comea2333d2011-03-14 16:44:56 +0000119 if (err) {
agl@chromium.org309485b2009-07-21 17:41:32 +0000120 return false;
reed@google.comea2333d2011-03-14 16:44:56 +0000121 }
agl@chromium.org309485b2009-07-21 17:41:32 +0000122
bungeman@google.comfd668cf2012-08-24 17:46:11 +0000123 // Setup LCD filtering. This reduces color fringes for LCD smoothed glyphs.
epoger@google.comb371ed12011-06-29 21:20:52 +0000124#ifdef FT_LCD_FILTER_H
bungeman@google.com62566f32012-10-13 03:21:53 +0000125 // Use default { 0x10, 0x40, 0x70, 0x40, 0x10 }, as it adds up to 0x110, simulating ink spread.
126 // SetLcdFilter must be called before SetLcdFilterWeights.
127 err = FT_Library_SetLcdFilter(gFTLibrary, FT_LCD_FILTER_DEFAULT);
bungeman@google.comfd668cf2012-08-24 17:46:11 +0000128 if (0 == err) {
129 gLCDSupport = true;
130 gLCDExtra = 2; //Using a filter adds one full pixel to each side.
131
bungeman@google.com62566f32012-10-13 03:21:53 +0000132#ifdef SK_FONTHOST_FREETYPE_USE_NORMAL_LCD_FILTER
133 // This also adds to 0x110 simulating ink spread, but provides better results than default.
134 static unsigned char gGaussianLikeHeavyWeights[] = { 0x1A, 0x43, 0x56, 0x43, 0x1A, };
bungeman@google.comfd668cf2012-08-24 17:46:11 +0000135
136#if defined(SK_FONTHOST_FREETYPE_RUNTIME_VERSION) && \
137 SK_FONTHOST_FREETYPE_RUNTIME_VERSION > 0x020400
bungeman@google.com62566f32012-10-13 03:21:53 +0000138 err = FT_Library_SetLcdFilterWeights(gFTLibrary, gGaussianLikeHeavyWeights);
bungeman@google.comfd668cf2012-08-24 17:46:11 +0000139#elif defined(SK_CAN_USE_DLOPEN) && SK_CAN_USE_DLOPEN == 1
140 //The FreeType library is already loaded, so symbols are available in process.
141 void* self = dlopen(NULL, RTLD_LAZY);
142 if (NULL != self) {
143 FT_Library_SetLcdFilterWeightsProc setLcdFilterWeights;
144 //The following cast is non-standard, but safe for POSIX.
145 *reinterpret_cast<void**>(&setLcdFilterWeights) = dlsym(self, "FT_Library_SetLcdFilterWeights");
146 dlclose(self);
147
148 if (NULL != setLcdFilterWeights) {
bungeman@google.com62566f32012-10-13 03:21:53 +0000149 err = setLcdFilterWeights(gFTLibrary, gGaussianLikeHeavyWeights);
bungeman@google.comfd668cf2012-08-24 17:46:11 +0000150 }
151 }
152#endif
bungeman@google.com62566f32012-10-13 03:21:53 +0000153#endif
reed@google.coma1c32562012-03-01 19:38:23 +0000154 }
epoger@google.com5070d792011-06-29 20:43:14 +0000155#else
156 gLCDSupport = false;
157#endif
reed@android.com61608aa2009-07-31 14:52:54 +0000158 gLCDSupportValid = true;
agl@chromium.org309485b2009-07-21 17:41:32 +0000159
160 return true;
161}
162
reed@google.comfb2fdcc2012-10-17 15:49:36 +0000163// Lazy, once, wrapper to ask the FreeType Library if it can support LCD text
164static bool is_lcd_supported() {
165 if (!gLCDSupportValid) {
166 SkAutoMutexAcquire ac(gFTMutex);
167
168 if (!gLCDSupportValid) {
169 InitFreetype();
170 FT_Done_FreeType(gFTLibrary);
171 }
172 }
173 return gLCDSupport;
174}
175
george@mozilla.comc59b5da2012-08-23 00:39:08 +0000176class SkScalerContext_FreeType : public SkScalerContext_FreeType_Base {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000177public:
reed@google.com0da48612013-03-19 16:06:52 +0000178 SkScalerContext_FreeType(SkTypeface*, const SkDescriptor* desc);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000179 virtual ~SkScalerContext_FreeType();
agl@chromium.orgcc3096b2009-04-22 22:09:04 +0000180
reed@android.com62900b42009-02-11 15:07:19 +0000181 bool success() const {
reed@android.coma0f5d152009-06-22 17:38:10 +0000182 return fFaceRec != NULL &&
183 fFTSize != NULL &&
184 fFace != NULL;
reed@android.com62900b42009-02-11 15:07:19 +0000185 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000186
187protected:
bungeman@google.coma76de722012-10-26 19:35:54 +0000188 virtual unsigned generateGlyphCount() SK_OVERRIDE;
189 virtual uint16_t generateCharToGlyph(SkUnichar uni) SK_OVERRIDE;
190 virtual void generateAdvance(SkGlyph* glyph) SK_OVERRIDE;
191 virtual void generateMetrics(SkGlyph* glyph) SK_OVERRIDE;
192 virtual void generateImage(const SkGlyph& glyph) SK_OVERRIDE;
193 virtual void generatePath(const SkGlyph& glyph, SkPath* path) SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000194 virtual void generateFontMetrics(SkPaint::FontMetrics* mx,
bungeman@google.coma76de722012-10-26 19:35:54 +0000195 SkPaint::FontMetrics* my) SK_OVERRIDE;
196 virtual SkUnichar generateGlyphToChar(uint16_t glyph) SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000197
198private:
199 SkFaceRec* fFaceRec;
200 FT_Face fFace; // reference to shared face in gFaceRecHead
201 FT_Size fFTSize; // our own copy
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +0000202 FT_Int fStrikeIndex;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000203 SkFixed fScaleX, fScaleY;
204 FT_Matrix fMatrix22;
205 uint32_t fLoadGlyphFlags;
reed@google.combdc99882011-11-21 14:36:57 +0000206 bool fDoLinearMetrics;
reed@google.coma1bfa212012-03-08 21:57:12 +0000207 bool fLCDIsVert;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000208
reed@google.comf073b332013-05-06 12:21:16 +0000209 // Need scalar versions for generateFontMetrics
210 SkVector fScale;
211 SkMatrix fMatrix22Scalar;
212
reed@android.com8a1c16f2008-12-17 15:59:43 +0000213 FT_Error setupSize();
djsollen@google.comd8b599c2012-03-19 19:44:19 +0000214 void getBBoxForCurrentGlyph(SkGlyph* glyph, FT_BBox* bbox,
215 bool snapToPixelBoundary = false);
bungeman@google.comcbe1b542013-12-16 17:02:39 +0000216 bool getCBoxForLetter(char letter, FT_BBox* bbox);
scroggo@google.com94bc60f2012-10-04 20:45:06 +0000217 // Caller must lock gFTMutex before calling this function.
djsollen@google.comd8b599c2012-03-19 19:44:19 +0000218 void updateGlyphIfLCD(SkGlyph* glyph);
commit-bot@chromium.org6fa81d72013-12-26 15:50:29 +0000219 // Caller must lock gFTMutex before calling this function.
220 // update FreeType2 glyph slot with glyph emboldened
221 void emboldenIfNeeded(FT_Face face, FT_GlyphSlot glyph);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000222};
223
224///////////////////////////////////////////////////////////////////////////
225///////////////////////////////////////////////////////////////////////////
226
reed@android.com8a1c16f2008-12-17 15:59:43 +0000227struct SkFaceRec {
228 SkFaceRec* fNext;
229 FT_Face fFace;
230 FT_StreamRec fFTStream;
231 SkStream* fSkStream;
232 uint32_t fRefCnt;
233 uint32_t fFontID;
234
reed@android.comb1d9d2e2009-03-04 17:37:51 +0000235 // assumes ownership of the stream, will call unref() when its done
reed@android.com8a1c16f2008-12-17 15:59:43 +0000236 SkFaceRec(SkStream* strm, uint32_t fontID);
237 ~SkFaceRec() {
reed@android.comb1d9d2e2009-03-04 17:37:51 +0000238 fSkStream->unref();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000239 }
240};
241
242extern "C" {
243 static unsigned long sk_stream_read(FT_Stream stream,
244 unsigned long offset,
245 unsigned char* buffer,
246 unsigned long count ) {
247 SkStream* str = (SkStream*)stream->descriptor.pointer;
248
249 if (count) {
250 if (!str->rewind()) {
251 return 0;
252 } else {
253 unsigned long ret;
254 if (offset) {
255 ret = str->read(NULL, offset);
256 if (ret != offset) {
257 return 0;
258 }
259 }
260 ret = str->read(buffer, count);
261 if (ret != count) {
262 return 0;
263 }
264 count = ret;
265 }
266 }
267 return count;
268 }
269
sugoi@google.com66a58ac2013-03-05 20:40:52 +0000270 static void sk_stream_close(FT_Stream) {}
reed@android.com8a1c16f2008-12-17 15:59:43 +0000271}
272
273SkFaceRec::SkFaceRec(SkStream* strm, uint32_t fontID)
vandebo@chromium.org9af25f32012-03-28 21:24:27 +0000274 : fNext(NULL), fSkStream(strm), fRefCnt(1), fFontID(fontID) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000275// SkDEBUGF(("SkFaceRec: opening %s (%p)\n", key.c_str(), strm));
276
reed@android.com4516f472009-06-29 16:25:36 +0000277 sk_bzero(&fFTStream, sizeof(fFTStream));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000278 fFTStream.size = fSkStream->getLength();
279 fFTStream.descriptor.pointer = fSkStream;
280 fFTStream.read = sk_stream_read;
281 fFTStream.close = sk_stream_close;
282}
283
reed@android.com62900b42009-02-11 15:07:19 +0000284// Will return 0 on failure
scroggo@google.com94bc60f2012-10-04 20:45:06 +0000285// Caller must lock gFTMutex before calling this function.
reed@google.com2cdc6712013-03-21 18:22:00 +0000286static SkFaceRec* ref_ft_face(const SkTypeface* typeface) {
287 const SkFontID fontID = typeface->uniqueID();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000288 SkFaceRec* rec = gFaceRecHead;
289 while (rec) {
290 if (rec->fFontID == fontID) {
291 SkASSERT(rec->fFace);
292 rec->fRefCnt += 1;
293 return rec;
294 }
295 rec = rec->fNext;
296 }
297
reed@google.com2cdc6712013-03-21 18:22:00 +0000298 int face_index;
299 SkStream* strm = typeface->openStream(&face_index);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000300 if (NULL == strm) {
reed@google.com2cdc6712013-03-21 18:22:00 +0000301 return NULL;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000302 }
303
304 // this passes ownership of strm to the rec
305 rec = SkNEW_ARGS(SkFaceRec, (strm, fontID));
306
307 FT_Open_Args args;
308 memset(&args, 0, sizeof(args));
309 const void* memoryBase = strm->getMemoryBase();
310
311 if (NULL != memoryBase) {
312//printf("mmap(%s)\n", keyString.c_str());
313 args.flags = FT_OPEN_MEMORY;
314 args.memory_base = (const FT_Byte*)memoryBase;
315 args.memory_size = strm->getLength();
316 } else {
317//printf("fopen(%s)\n", keyString.c_str());
318 args.flags = FT_OPEN_STREAM;
319 args.stream = &rec->fFTStream;
320 }
321
reed@google.com2cdc6712013-03-21 18:22:00 +0000322 FT_Error err = FT_Open_Face(gFTLibrary, &args, face_index, &rec->fFace);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000323 if (err) { // bad filename, try the default font
324 fprintf(stderr, "ERROR: unable to open font '%x'\n", fontID);
325 SkDELETE(rec);
reed@google.com2cdc6712013-03-21 18:22:00 +0000326 return NULL;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000327 } else {
328 SkASSERT(rec->fFace);
329 //fprintf(stderr, "Opened font '%s'\n", filename.c_str());
330 rec->fNext = gFaceRecHead;
331 gFaceRecHead = rec;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000332 return rec;
333 }
334}
335
scroggo@google.com94bc60f2012-10-04 20:45:06 +0000336// Caller must lock gFTMutex before calling this function.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000337static void unref_ft_face(FT_Face face) {
338 SkFaceRec* rec = gFaceRecHead;
339 SkFaceRec* prev = NULL;
340 while (rec) {
341 SkFaceRec* next = rec->fNext;
342 if (rec->fFace == face) {
343 if (--rec->fRefCnt == 0) {
344 if (prev) {
345 prev->fNext = next;
346 } else {
347 gFaceRecHead = next;
348 }
349 FT_Done_Face(face);
350 SkDELETE(rec);
351 }
352 return;
353 }
354 prev = rec;
355 rec = next;
356 }
tomhudson@google.com0c00f212011-12-28 14:59:50 +0000357 SkDEBUGFAIL("shouldn't get here, face not in list");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000358}
359
reed@google.comb4162b12013-07-02 16:32:29 +0000360class AutoFTAccess {
361public:
362 AutoFTAccess(const SkTypeface* tf) : fRec(NULL), fFace(NULL) {
363 gFTMutex.acquire();
364 if (1 == ++gFTCount) {
365 if (!InitFreetype()) {
366 sk_throw();
367 }
368 }
369 fRec = ref_ft_face(tf);
370 if (fRec) {
371 fFace = fRec->fFace;
372 }
373 }
374
375 ~AutoFTAccess() {
376 if (fFace) {
377 unref_ft_face(fFace);
378 }
379 if (0 == --gFTCount) {
380 FT_Done_FreeType(gFTLibrary);
381 }
382 gFTMutex.release();
383 }
384
385 SkFaceRec* rec() { return fRec; }
386 FT_Face face() { return fFace; }
387
388private:
389 SkFaceRec* fRec;
390 FT_Face fFace;
391};
392
reed@android.com8a1c16f2008-12-17 15:59:43 +0000393///////////////////////////////////////////////////////////////////////////
394
vandebo@chromium.org16be6b82011-01-28 21:28:56 +0000395// Work around for old versions of freetype.
396static FT_Error getAdvances(FT_Face face, FT_UInt start, FT_UInt count,
397 FT_Int32 loadFlags, FT_Fixed* advances) {
398#ifdef FT_ADVANCES_H
399 return FT_Get_Advances(face, start, count, loadFlags, advances);
400#else
401 if (!face || start >= face->num_glyphs ||
402 start + count > face->num_glyphs || loadFlags != FT_LOAD_NO_SCALE) {
403 return 6; // "Invalid argument."
404 }
405 if (count == 0)
406 return 0;
407
408 for (int i = 0; i < count; i++) {
409 FT_Error err = FT_Load_Glyph(face, start + i, FT_LOAD_NO_SCALE);
410 if (err)
411 return err;
412 advances[i] = face->glyph->advance.x;
413 }
414
415 return 0;
416#endif
417}
418
419static bool canEmbed(FT_Face face) {
djsollen@google.comfa394d42012-01-09 20:40:25 +0000420#ifdef FT_FSTYPE_RESTRICTED_LICENSE_EMBEDDING
vandebo@chromium.org16be6b82011-01-28 21:28:56 +0000421 FT_UShort fsType = FT_Get_FSType_Flags(face);
422 return (fsType & (FT_FSTYPE_RESTRICTED_LICENSE_EMBEDDING |
423 FT_FSTYPE_BITMAP_EMBEDDING_ONLY)) == 0;
424#else
425 // No embedding is 0x2 and bitmap embedding only is 0x200.
426 TT_OS2* os2_table;
427 if ((os2_table = (TT_OS2*)FT_Get_Sfnt_Table(face, ft_sfnt_os2)) != NULL) {
428 return (os2_table->fsType & 0x202) == 0;
429 }
430 return false; // We tried, fail safe.
431#endif
432}
433
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000434static bool GetLetterCBox(FT_Face face, char letter, FT_BBox* bbox) {
435 const FT_UInt glyph_id = FT_Get_Char_Index(face, letter);
436 if (!glyph_id)
437 return false;
bungeman@google.comcbe1b542013-12-16 17:02:39 +0000438 if (FT_Load_Glyph(face, glyph_id, FT_LOAD_NO_SCALE) != 0)
439 return false;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000440 FT_Outline_Get_CBox(&face->glyph->outline, bbox);
441 return true;
442}
443
vandebo@chromium.org6f72d1e2011-02-14 23:19:59 +0000444static bool getWidthAdvance(FT_Face face, int gId, int16_t* data) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000445 FT_Fixed advance = 0;
vandebo@chromium.org6f72d1e2011-02-14 23:19:59 +0000446 if (getAdvances(face, gId, 1, FT_LOAD_NO_SCALE, &advance)) {
447 return false;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000448 }
vandebo@chromium.org6f72d1e2011-02-14 23:19:59 +0000449 SkASSERT(data);
450 *data = advance;
451 return true;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000452}
453
vandebo@chromium.org6744d492011-05-09 18:13:47 +0000454static void populate_glyph_to_unicode(FT_Face& face,
455 SkTDArray<SkUnichar>* glyphToUnicode) {
456 // Check and see if we have Unicode cmaps.
457 for (int i = 0; i < face->num_charmaps; ++i) {
458 // CMaps known to support Unicode:
459 // Platform ID Encoding ID Name
460 // ----------- ----------- -----------------------------------
461 // 0 0,1 Apple Unicode
462 // 0 3 Apple Unicode 2.0 (preferred)
463 // 3 1 Microsoft Unicode UCS-2
464 // 3 10 Microsoft Unicode UCS-4 (preferred)
465 //
466 // See Apple TrueType Reference Manual
467 // http://developer.apple.com/fonts/TTRefMan/RM06/Chap6cmap.html
468 // http://developer.apple.com/fonts/TTRefMan/RM06/Chap6name.html#ID
469 // Microsoft OpenType Specification
470 // http://www.microsoft.com/typography/otspec/cmap.htm
471
472 FT_UShort platformId = face->charmaps[i]->platform_id;
473 FT_UShort encodingId = face->charmaps[i]->encoding_id;
474
475 if (platformId != 0 && platformId != 3) {
476 continue;
477 }
478 if (platformId == 3 && encodingId != 1 && encodingId != 10) {
479 continue;
480 }
481 bool preferredMap = ((platformId == 3 && encodingId == 10) ||
482 (platformId == 0 && encodingId == 3));
483
484 FT_Set_Charmap(face, face->charmaps[i]);
485 if (glyphToUnicode->isEmpty()) {
486 glyphToUnicode->setCount(face->num_glyphs);
487 memset(glyphToUnicode->begin(), 0,
488 sizeof(SkUnichar) * face->num_glyphs);
489 }
490
491 // Iterate through each cmap entry.
492 FT_UInt glyphIndex;
493 for (SkUnichar charCode = FT_Get_First_Char(face, &glyphIndex);
494 glyphIndex != 0;
495 charCode = FT_Get_Next_Char(face, charCode, &glyphIndex)) {
496 if (charCode &&
497 ((*glyphToUnicode)[glyphIndex] == 0 || preferredMap)) {
498 (*glyphToUnicode)[glyphIndex] = charCode;
499 }
500 }
501 }
502}
503
reed@google.com2689f612013-03-20 20:01:47 +0000504SkAdvancedTypefaceMetrics* SkTypeface_FreeType::onGetAdvancedTypefaceMetrics(
vandebo@chromium.org37ad8fb2011-08-18 02:38:50 +0000505 SkAdvancedTypefaceMetrics::PerGlyphInfo perGlyphInfo,
506 const uint32_t* glyphIDs,
reed@google.com2689f612013-03-20 20:01:47 +0000507 uint32_t glyphIDsCount) const {
djsollen@google.comda957722011-11-16 17:00:46 +0000508#if defined(SK_BUILD_FOR_MAC)
reed@google.com8a5d6922011-03-14 15:08:03 +0000509 return NULL;
510#else
reed@google.comb4162b12013-07-02 16:32:29 +0000511 AutoFTAccess fta(this);
512 FT_Face face = fta.face();
513 if (!face) {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000514 return NULL;
reed@google.comb4162b12013-07-02 16:32:29 +0000515 }
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000516
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000517 SkAdvancedTypefaceMetrics* info = new SkAdvancedTypefaceMetrics;
518 info->fFontName.set(FT_Get_Postscript_Name(face));
vandebo@chromium.org5f209e62013-12-10 17:22:41 +0000519 info->fMultiMaster = FT_HAS_MULTIPLE_MASTERS(face);
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000520 info->fLastGlyphID = face->num_glyphs - 1;
521 info->fEmSize = 1000;
522
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000523 bool cid = false;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000524 const char* fontType = FT_Get_X11_Font_Format(face);
vandebo@chromium.orgc3a2ae52011-02-03 21:48:23 +0000525 if (strcmp(fontType, "Type 1") == 0) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000526 info->fType = SkAdvancedTypefaceMetrics::kType1_Font;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000527 } else if (strcmp(fontType, "CID Type 1") == 0) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000528 info->fType = SkAdvancedTypefaceMetrics::kType1CID_Font;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000529 cid = true;
530 } else if (strcmp(fontType, "CFF") == 0) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000531 info->fType = SkAdvancedTypefaceMetrics::kCFF_Font;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000532 } else if (strcmp(fontType, "TrueType") == 0) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000533 info->fType = SkAdvancedTypefaceMetrics::kTrueType_Font;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000534 cid = true;
535 TT_Header* ttHeader;
536 if ((ttHeader = (TT_Header*)FT_Get_Sfnt_Table(face,
537 ft_sfnt_head)) != NULL) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000538 info->fEmSize = ttHeader->Units_Per_EM;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000539 }
bungeman@google.com4d71db82013-12-02 19:10:02 +0000540 } else {
541 info->fType = SkAdvancedTypefaceMetrics::kOther_Font;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000542 }
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000543
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000544 info->fStyle = 0;
545 if (FT_IS_FIXED_WIDTH(face))
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000546 info->fStyle |= SkAdvancedTypefaceMetrics::kFixedPitch_Style;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000547 if (face->style_flags & FT_STYLE_FLAG_ITALIC)
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000548 info->fStyle |= SkAdvancedTypefaceMetrics::kItalic_Style;
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000549
550 PS_FontInfoRec ps_info;
551 TT_Postscript* tt_info;
552 if (FT_Get_PS_Font_Info(face, &ps_info) == 0) {
553 info->fItalicAngle = ps_info.italic_angle;
554 } else if ((tt_info =
555 (TT_Postscript*)FT_Get_Sfnt_Table(face,
556 ft_sfnt_post)) != NULL) {
557 info->fItalicAngle = SkFixedToScalar(tt_info->italicAngle);
558 } else {
559 info->fItalicAngle = 0;
560 }
561
562 info->fAscent = face->ascender;
563 info->fDescent = face->descender;
564
565 // Figure out a good guess for StemV - Min width of i, I, !, 1.
566 // This probably isn't very good with an italic font.
567 int16_t min_width = SHRT_MAX;
vandebo@chromium.org6f72d1e2011-02-14 23:19:59 +0000568 info->fStemV = 0;
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000569 char stem_chars[] = {'i', 'I', '!', '1'};
570 for (size_t i = 0; i < SK_ARRAY_COUNT(stem_chars); i++) {
571 FT_BBox bbox;
572 if (GetLetterCBox(face, stem_chars[i], &bbox)) {
573 int16_t width = bbox.xMax - bbox.xMin;
574 if (width > 0 && width < min_width) {
575 min_width = width;
576 info->fStemV = min_width;
577 }
578 }
579 }
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000580
581 TT_PCLT* pclt_info;
582 TT_OS2* os2_table;
583 if ((pclt_info = (TT_PCLT*)FT_Get_Sfnt_Table(face, ft_sfnt_pclt)) != NULL) {
584 info->fCapHeight = pclt_info->CapHeight;
585 uint8_t serif_style = pclt_info->SerifStyle & 0x3F;
586 if (serif_style >= 2 && serif_style <= 6)
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000587 info->fStyle |= SkAdvancedTypefaceMetrics::kSerif_Style;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000588 else if (serif_style >= 9 && serif_style <= 12)
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000589 info->fStyle |= SkAdvancedTypefaceMetrics::kScript_Style;
bungeman@google.comcbe1b542013-12-16 17:02:39 +0000590 } else if (((os2_table = (TT_OS2*)FT_Get_Sfnt_Table(face, ft_sfnt_os2)) != NULL) &&
591 // sCapHeight is available only when version 2 or later.
592 os2_table->version != 0xFFFF &&
593 os2_table->version >= 2) {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000594 info->fCapHeight = os2_table->sCapHeight;
595 } else {
596 // Figure out a good guess for CapHeight: average the height of M and X.
597 FT_BBox m_bbox, x_bbox;
598 bool got_m, got_x;
599 got_m = GetLetterCBox(face, 'M', &m_bbox);
600 got_x = GetLetterCBox(face, 'X', &x_bbox);
601 if (got_m && got_x) {
602 info->fCapHeight = (m_bbox.yMax - m_bbox.yMin + x_bbox.yMax -
603 x_bbox.yMin) / 2;
604 } else if (got_m && !got_x) {
605 info->fCapHeight = m_bbox.yMax - m_bbox.yMin;
606 } else if (!got_m && got_x) {
607 info->fCapHeight = x_bbox.yMax - x_bbox.yMin;
bungeman@google.com12bd4a02013-12-19 19:34:22 +0000608 } else {
609 // Last resort, use the ascent.
610 info->fCapHeight = info->fAscent;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000611 }
612 }
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000613
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000614 info->fBBox = SkIRect::MakeLTRB(face->bbox.xMin, face->bbox.yMax,
615 face->bbox.xMax, face->bbox.yMin);
616
vandebo@chromium.org5f209e62013-12-10 17:22:41 +0000617 if (!canEmbed(face) || !FT_IS_SCALABLE(face) ||
618 info->fType == SkAdvancedTypefaceMetrics::kOther_Font) {
vandebo@chromium.org325cb9a2011-03-30 18:36:29 +0000619 perGlyphInfo = SkAdvancedTypefaceMetrics::kNo_PerGlyphInfo;
620 }
621
622 if (perGlyphInfo & SkAdvancedTypefaceMetrics::kHAdvance_PerGlyphInfo) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000623 if (FT_IS_FIXED_WIDTH(face)) {
624 appendRange(&info->fGlyphWidths, 0);
625 int16_t advance = face->max_advance_width;
626 info->fGlyphWidths->fAdvance.append(1, &advance);
627 finishRange(info->fGlyphWidths.get(), 0,
628 SkAdvancedTypefaceMetrics::WidthRange::kDefault);
vandebo@chromium.org6f72d1e2011-02-14 23:19:59 +0000629 } else if (!cid) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000630 appendRange(&info->fGlyphWidths, 0);
631 // So as to not blow out the stack, get advances in batches.
632 for (int gID = 0; gID < face->num_glyphs; gID += 128) {
633 FT_Fixed advances[128];
634 int advanceCount = 128;
635 if (gID + advanceCount > face->num_glyphs)
bungeman@google.comb8aa4dd2013-10-15 18:50:00 +0000636 advanceCount = face->num_glyphs - gID;
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000637 getAdvances(face, gID, advanceCount, FT_LOAD_NO_SCALE,
638 advances);
639 for (int i = 0; i < advanceCount; i++) {
vandebo@chromium.orgce8a1952012-10-22 20:09:31 +0000640 int16_t advance = advances[i];
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000641 info->fGlyphWidths->fAdvance.append(1, &advance);
642 }
643 }
644 finishRange(info->fGlyphWidths.get(), face->num_glyphs - 1,
645 SkAdvancedTypefaceMetrics::WidthRange::kRange);
646 } else {
vandebo@chromium.org6f72d1e2011-02-14 23:19:59 +0000647 info->fGlyphWidths.reset(
vandebo@chromium.org37ad8fb2011-08-18 02:38:50 +0000648 getAdvanceData(face,
649 face->num_glyphs,
650 glyphIDs,
651 glyphIDsCount,
652 &getWidthAdvance));
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000653 }
vandebo@chromium.org325cb9a2011-03-30 18:36:29 +0000654 }
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000655
vandebo@chromium.org325cb9a2011-03-30 18:36:29 +0000656 if (perGlyphInfo & SkAdvancedTypefaceMetrics::kVAdvance_PerGlyphInfo &&
657 FT_HAS_VERTICAL(face)) {
658 SkASSERT(false); // Not implemented yet.
659 }
660
661 if (perGlyphInfo & SkAdvancedTypefaceMetrics::kGlyphNames_PerGlyphInfo &&
662 info->fType == SkAdvancedTypefaceMetrics::kType1_Font) {
663 // Postscript fonts may contain more than 255 glyphs, so we end up
664 // using multiple font descriptions with a glyph ordering. Record
665 // the name of each glyph.
666 info->fGlyphNames.reset(
667 new SkAutoTArray<SkString>(face->num_glyphs));
668 for (int gID = 0; gID < face->num_glyphs; gID++) {
669 char glyphName[128]; // PS limit for names is 127 bytes.
670 FT_Get_Glyph_Name(face, gID, glyphName, 128);
671 info->fGlyphNames->get()[gID].set(glyphName);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000672 }
673 }
674
vandebo@chromium.org6744d492011-05-09 18:13:47 +0000675 if (perGlyphInfo & SkAdvancedTypefaceMetrics::kToUnicode_PerGlyphInfo &&
676 info->fType != SkAdvancedTypefaceMetrics::kType1_Font &&
677 face->num_charmaps) {
678 populate_glyph_to_unicode(face, &(info->fGlyphToUnicode));
679 }
680
vandebo@chromium.org5f209e62013-12-10 17:22:41 +0000681 if (!canEmbed(face))
682 info->fType = SkAdvancedTypefaceMetrics::kNotEmbeddable_Font;
683
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000684 return info;
reed@google.com8a5d6922011-03-14 15:08:03 +0000685#endif
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000686}
vandebo@chromium.org37ad8fb2011-08-18 02:38:50 +0000687
reed@google.com618ef5e2011-01-26 22:10:41 +0000688///////////////////////////////////////////////////////////////////////////
689
reed@google.comffe49f52011-11-22 19:42:41 +0000690#define BLACK_LUMINANCE_LIMIT 0x40
691#define WHITE_LUMINANCE_LIMIT 0xA0
692
reed@google.com8ed436c2011-07-21 14:12:36 +0000693static bool bothZero(SkScalar a, SkScalar b) {
694 return 0 == a && 0 == b;
695}
696
697// returns false if there is any non-90-rotation or skew
698static bool isAxisAligned(const SkScalerContext::Rec& rec) {
699 return 0 == rec.fPreSkewX &&
700 (bothZero(rec.fPost2x2[0][1], rec.fPost2x2[1][0]) ||
701 bothZero(rec.fPost2x2[0][0], rec.fPost2x2[1][1]));
702}
703
reed@google.com0da48612013-03-19 16:06:52 +0000704SkScalerContext* SkTypeface_FreeType::onCreateScalerContext(
705 const SkDescriptor* desc) const {
706 SkScalerContext_FreeType* c = SkNEW_ARGS(SkScalerContext_FreeType,
707 (const_cast<SkTypeface_FreeType*>(this),
708 desc));
709 if (!c->success()) {
710 SkDELETE(c);
711 c = NULL;
712 }
713 return c;
714}
715
716void SkTypeface_FreeType::onFilterRec(SkScalerContextRec* rec) const {
bungeman@google.com8cf32262012-04-02 14:34:30 +0000717 //BOGUS: http://code.google.com/p/chromium/issues/detail?id=121119
718 //Cap the requested size as larger sizes give bogus values.
719 //Remove when http://code.google.com/p/skia/issues/detail?id=554 is fixed.
bungeman@google.com5582e632012-04-02 14:51:54 +0000720 if (rec->fTextSize > SkIntToScalar(1 << 14)) {
scroggo@google.com94bc60f2012-10-04 20:45:06 +0000721 rec->fTextSize = SkIntToScalar(1 << 14);
bungeman@google.com8cf32262012-04-02 14:34:30 +0000722 }
skia.committer@gmail.coma27096b2012-08-30 14:38:00 +0000723
reed@google.comfb2fdcc2012-10-17 15:49:36 +0000724 if (!is_lcd_supported() && isLCD(*rec)) {
reed@google.com618ef5e2011-01-26 22:10:41 +0000725 // If the runtime Freetype library doesn't support LCD mode, we disable
726 // it here.
727 rec->fMaskFormat = SkMask::kA8_Format;
728 }
reed@google.com5b31b0f2011-02-23 14:41:42 +0000729
reed@google.com618ef5e2011-01-26 22:10:41 +0000730 SkPaint::Hinting h = rec->getHinting();
reed@google.comeffc5012011-06-27 16:44:46 +0000731 if (SkPaint::kFull_Hinting == h && !isLCD(*rec)) {
reed@google.com618ef5e2011-01-26 22:10:41 +0000732 // collapse full->normal hinting if we're not doing LCD
733 h = SkPaint::kNormal_Hinting;
reed@google.com618ef5e2011-01-26 22:10:41 +0000734 }
bungeman@google.comf4f2b802012-03-08 19:19:51 +0000735 if ((rec->fFlags & SkScalerContext::kSubpixelPositioning_Flag)) {
reed@google.com1ac83502012-02-28 17:06:02 +0000736 if (SkPaint::kNo_Hinting != h) {
737 h = SkPaint::kSlight_Hinting;
738 }
739 }
740
reed@google.com8ed436c2011-07-21 14:12:36 +0000741 // rotated text looks bad with hinting, so we disable it as needed
742 if (!isAxisAligned(*rec)) {
743 h = SkPaint::kNo_Hinting;
744 }
reed@google.com618ef5e2011-01-26 22:10:41 +0000745 rec->setHinting(h);
reed@google.comffe49f52011-11-22 19:42:41 +0000746
bungeman@google.com97efada2012-07-30 20:40:50 +0000747#ifndef SK_GAMMA_APPLY_TO_A8
748 if (!isLCD(*rec)) {
749 rec->ignorePreBlend();
reed@google.comffe49f52011-11-22 19:42:41 +0000750 }
reed@google.com1ac83502012-02-28 17:06:02 +0000751#endif
reed@google.com618ef5e2011-01-26 22:10:41 +0000752}
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000753
reed@google.com38c37dd2013-03-21 15:36:26 +0000754int SkTypeface_FreeType::onGetUPEM() const {
reed@google.comb4162b12013-07-02 16:32:29 +0000755 AutoFTAccess fta(this);
756 FT_Face face = fta.face();
757 return face ? face->units_per_EM : 0;
djsollen@google.comcd9d69b2011-03-14 20:30:14 +0000758}
djsollen@google.comcd9d69b2011-03-14 20:30:14 +0000759
reed@google.com35fe7372013-10-30 15:07:03 +0000760bool SkTypeface_FreeType::onGetKerningPairAdjustments(const uint16_t glyphs[],
761 int count, int32_t adjustments[]) const {
762 AutoFTAccess fta(this);
763 FT_Face face = fta.face();
764 if (!face || !FT_HAS_KERNING(face)) {
765 return false;
766 }
767
768 for (int i = 0; i < count - 1; ++i) {
769 FT_Vector delta;
770 FT_Error err = FT_Get_Kerning(face, glyphs[i], glyphs[i+1],
771 FT_KERNING_UNSCALED, &delta);
772 if (err) {
773 return false;
774 }
775 adjustments[i] = delta.x;
776 }
777 return true;
778}
779
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +0000780static FT_Int chooseBitmapStrike(FT_Face face, SkFixed scaleY) {
781 // early out if face is bad
782 if (face == NULL) {
783 SkDEBUGF(("chooseBitmapStrike aborted due to NULL face\n"));
784 return -1;
785 }
786 // determine target ppem
787 FT_Pos targetPPEM = SkFixedToFDot6(scaleY);
788 // find a bitmap strike equal to or just larger than the requested size
789 FT_Int chosenStrikeIndex = -1;
790 FT_Pos chosenPPEM = 0;
791 for (FT_Int strikeIndex = 0; strikeIndex < face->num_fixed_sizes; ++strikeIndex) {
792 FT_Pos thisPPEM = face->available_sizes[strikeIndex].y_ppem;
793 if (thisPPEM == targetPPEM) {
794 // exact match - our search stops here
795 chosenPPEM = thisPPEM;
796 chosenStrikeIndex = strikeIndex;
797 break;
798 } else if (chosenPPEM < targetPPEM) {
799 // attempt to increase chosenPPEM
800 if (thisPPEM > chosenPPEM) {
801 chosenPPEM = thisPPEM;
802 chosenStrikeIndex = strikeIndex;
803 }
804 } else {
805 // attempt to decrease chosenPPEM, but not below targetPPEM
806 if (thisPPEM < chosenPPEM && thisPPEM > targetPPEM) {
807 chosenPPEM = thisPPEM;
808 chosenStrikeIndex = strikeIndex;
809 }
810 }
811 }
812 if (chosenStrikeIndex != -1) {
813 // use the chosen strike
814 FT_Error err = FT_Select_Size(face, chosenStrikeIndex);
815 if (err != 0) {
816 SkDEBUGF(("FT_Select_Size(%s, %d) returned 0x%x\n", face->family_name,
817 chosenStrikeIndex, err));
818 chosenStrikeIndex = -1;
819 }
820 }
821 return chosenStrikeIndex;
822}
823
reed@google.com0da48612013-03-19 16:06:52 +0000824SkScalerContext_FreeType::SkScalerContext_FreeType(SkTypeface* typeface,
825 const SkDescriptor* desc)
826 : SkScalerContext_FreeType_Base(typeface, desc) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000827 SkAutoMutexAcquire ac(gFTMutex);
828
reed@android.com8a1c16f2008-12-17 15:59:43 +0000829 if (gFTCount == 0) {
reed@android.com659aaf92009-07-23 15:20:21 +0000830 if (!InitFreetype()) {
831 sk_throw();
832 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000833 }
834 ++gFTCount;
835
836 // load the font file
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +0000837 fStrikeIndex = -1;
reed@android.com62900b42009-02-11 15:07:19 +0000838 fFTSize = NULL;
839 fFace = NULL;
reed@google.com2cdc6712013-03-21 18:22:00 +0000840 fFaceRec = ref_ft_face(typeface);
reed@android.com62900b42009-02-11 15:07:19 +0000841 if (NULL == fFaceRec) {
842 return;
843 }
844 fFace = fFaceRec->fFace;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000845
846 // compute our factors from the record
847
848 SkMatrix m;
849
850 fRec.getSingleMatrix(&m);
851
852#ifdef DUMP_STRIKE_CREATION
853 SkString keyString;
854 SkFontHost::GetDescriptorKeyString(desc, &keyString);
855 printf("========== strike [%g %g %g] [%g %g %g %g] hints %d format %d %s\n", SkScalarToFloat(fRec.fTextSize),
856 SkScalarToFloat(fRec.fPreScaleX), SkScalarToFloat(fRec.fPreSkewX),
857 SkScalarToFloat(fRec.fPost2x2[0][0]), SkScalarToFloat(fRec.fPost2x2[0][1]),
858 SkScalarToFloat(fRec.fPost2x2[1][0]), SkScalarToFloat(fRec.fPost2x2[1][1]),
agl@chromium.org309485b2009-07-21 17:41:32 +0000859 fRec.getHinting(), fRec.fMaskFormat, keyString.c_str());
reed@android.com8a1c16f2008-12-17 15:59:43 +0000860#endif
861
862 // now compute our scale factors
863 SkScalar sx = m.getScaleX();
864 SkScalar sy = m.getScaleY();
865
reed@google.comf073b332013-05-06 12:21:16 +0000866 fMatrix22Scalar.reset();
867
reed@android.com8a1c16f2008-12-17 15:59:43 +0000868 if (m.getSkewX() || m.getSkewY() || sx < 0 || sy < 0) {
869 // sort of give up on hinting
870 sx = SkMaxScalar(SkScalarAbs(sx), SkScalarAbs(m.getSkewX()));
871 sy = SkMaxScalar(SkScalarAbs(m.getSkewY()), SkScalarAbs(sy));
872 sx = sy = SkScalarAve(sx, sy);
873
874 SkScalar inv = SkScalarInvert(sx);
875
876 // flip the skew elements to go from our Y-down system to FreeType's
877 fMatrix22.xx = SkScalarToFixed(SkScalarMul(m.getScaleX(), inv));
878 fMatrix22.xy = -SkScalarToFixed(SkScalarMul(m.getSkewX(), inv));
879 fMatrix22.yx = -SkScalarToFixed(SkScalarMul(m.getSkewY(), inv));
880 fMatrix22.yy = SkScalarToFixed(SkScalarMul(m.getScaleY(), inv));
skia.committer@gmail.come944de72013-05-07 07:01:03 +0000881
reed@google.comf073b332013-05-06 12:21:16 +0000882 fMatrix22Scalar.setScaleX(SkScalarMul(m.getScaleX(), inv));
883 fMatrix22Scalar.setSkewX(-SkScalarMul(m.getSkewX(), inv));
884 fMatrix22Scalar.setSkewY(-SkScalarMul(m.getSkewY(), inv));
885 fMatrix22Scalar.setScaleY(SkScalarMul(m.getScaleY(), inv));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000886 } else {
887 fMatrix22.xx = fMatrix22.yy = SK_Fixed1;
888 fMatrix22.xy = fMatrix22.yx = 0;
889 }
reed@google.comf073b332013-05-06 12:21:16 +0000890 fScale.set(sx, sy);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000891 fScaleX = SkScalarToFixed(sx);
892 fScaleY = SkScalarToFixed(sy);
893
reed@google.coma1bfa212012-03-08 21:57:12 +0000894 fLCDIsVert = SkToBool(fRec.fFlags & SkScalerContext::kLCD_Vertical_Flag);
895
reed@android.com8a1c16f2008-12-17 15:59:43 +0000896 // compute the flags we send to Load_Glyph
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +0000897 bool linearMetrics = SkToBool(fRec.fFlags & SkScalerContext::kSubpixelPositioning_Flag);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000898 {
reed@android.come4d0bc02009-07-24 19:53:20 +0000899 FT_Int32 loadFlags = FT_LOAD_DEFAULT;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000900
agl@chromium.org70a303f2010-05-10 14:15:50 +0000901 if (SkMask::kBW_Format == fRec.fMaskFormat) {
902 // See http://code.google.com/p/chromium/issues/detail?id=43252#c24
903 loadFlags = FT_LOAD_TARGET_MONO;
reed@google.comeffc5012011-06-27 16:44:46 +0000904 if (fRec.getHinting() == SkPaint::kNo_Hinting) {
agl@chromium.org70a303f2010-05-10 14:15:50 +0000905 loadFlags = FT_LOAD_NO_HINTING;
reed@google.combdc99882011-11-21 14:36:57 +0000906 linearMetrics = true;
reed@google.comeffc5012011-06-27 16:44:46 +0000907 }
agl@chromium.org70a303f2010-05-10 14:15:50 +0000908 } else {
909 switch (fRec.getHinting()) {
910 case SkPaint::kNo_Hinting:
911 loadFlags = FT_LOAD_NO_HINTING;
reed@google.combdc99882011-11-21 14:36:57 +0000912 linearMetrics = true;
agl@chromium.org70a303f2010-05-10 14:15:50 +0000913 break;
914 case SkPaint::kSlight_Hinting:
915 loadFlags = FT_LOAD_TARGET_LIGHT; // This implies FORCE_AUTOHINT
916 break;
917 case SkPaint::kNormal_Hinting:
agl@chromium.orga2c71cb2010-06-17 20:49:17 +0000918 if (fRec.fFlags & SkScalerContext::kAutohinting_Flag)
919 loadFlags = FT_LOAD_FORCE_AUTOHINT;
920 else
921 loadFlags = FT_LOAD_NO_AUTOHINT;
agl@chromium.org70a303f2010-05-10 14:15:50 +0000922 break;
923 case SkPaint::kFull_Hinting:
agl@chromium.orga2c71cb2010-06-17 20:49:17 +0000924 if (fRec.fFlags & SkScalerContext::kAutohinting_Flag) {
925 loadFlags = FT_LOAD_FORCE_AUTOHINT;
926 break;
927 }
agl@chromium.org70a303f2010-05-10 14:15:50 +0000928 loadFlags = FT_LOAD_TARGET_NORMAL;
reed@google.comeffc5012011-06-27 16:44:46 +0000929 if (isLCD(fRec)) {
reed@google.coma1bfa212012-03-08 21:57:12 +0000930 if (fLCDIsVert) {
reed@google.comeffc5012011-06-27 16:44:46 +0000931 loadFlags = FT_LOAD_TARGET_LCD_V;
932 } else {
933 loadFlags = FT_LOAD_TARGET_LCD;
934 }
reed@google.comea2333d2011-03-14 16:44:56 +0000935 }
agl@chromium.org70a303f2010-05-10 14:15:50 +0000936 break;
937 default:
938 SkDebugf("---------- UNKNOWN hinting %d\n", fRec.getHinting());
939 break;
940 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000941 }
942
reed@google.comeffc5012011-06-27 16:44:46 +0000943 if ((fRec.fFlags & SkScalerContext::kEmbeddedBitmapText_Flag) == 0) {
agl@chromium.orge0d08992009-08-07 19:19:23 +0000944 loadFlags |= FT_LOAD_NO_BITMAP;
reed@google.comeffc5012011-06-27 16:44:46 +0000945 }
agl@chromium.orge0d08992009-08-07 19:19:23 +0000946
reed@google.com96a9f7912011-05-06 11:49:30 +0000947 // Always using FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH to get correct
948 // advances, as fontconfig and cairo do.
949 // See http://code.google.com/p/skia/issues/detail?id=222.
950 loadFlags |= FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH;
951
bungeman@google.com8ff8a192012-09-25 20:38:28 +0000952 // Use vertical layout if requested.
953 if (fRec.fFlags & SkScalerContext::kVertical_Flag) {
954 loadFlags |= FT_LOAD_VERTICAL_LAYOUT;
955 }
956
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +0000957 loadFlags |= FT_LOAD_COLOR;
958
reed@android.come4d0bc02009-07-24 19:53:20 +0000959 fLoadGlyphFlags = loadFlags;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000960 }
961
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +0000962 FT_Error err = FT_New_Size(fFace, &fFTSize);
963 if (err != 0) {
964 SkDEBUGF(("FT_New_Size returned %x for face %s\n", err, fFace->family_name));
965 fFace = NULL;
966 return;
967 }
968 err = FT_Activate_Size(fFTSize);
969 if (err != 0) {
970 SkDEBUGF(("FT_Activate_Size(%08x, 0x%x, 0x%x) returned 0x%x\n", fFace, fScaleX, fScaleY,
971 err));
972 fFTSize = NULL;
973 return;
974 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000975
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +0000976 if (FT_IS_SCALABLE(fFace)) {
977 err = FT_Set_Char_Size(fFace, SkFixedToFDot6(fScaleX), SkFixedToFDot6(fScaleY), 72, 72);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000978 if (err != 0) {
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +0000979 SkDEBUGF(("FT_Set_CharSize(%08x, 0x%x, 0x%x) returned 0x%x\n",
980 fFace, fScaleX, fScaleY, err));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000981 fFace = NULL;
982 return;
983 }
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +0000984 FT_Set_Transform(fFace, &fMatrix22, NULL);
985 } else if (FT_HAS_FIXED_SIZES(fFace)) {
986 fStrikeIndex = chooseBitmapStrike(fFace, fScaleY);
987 if (fStrikeIndex == -1) {
988 SkDEBUGF(("no glyphs for font \"%s\" size %f?\n",
989 fFace->family_name, SkFixedToScalar(fScaleY)));
990 } else {
991 // FreeType does no provide linear metrics for bitmap fonts.
992 linearMetrics = false;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000993
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +0000994 // FreeType documentation says:
995 // FT_LOAD_NO_BITMAP -- Ignore bitmap strikes when loading.
996 // Bitmap-only fonts ignore this flag.
997 //
998 // However, in FreeType 2.5.1 color bitmap only fonts do not ignore this flag.
999 // Force this flag off for bitmap only fonts.
1000 fLoadGlyphFlags &= ~FT_LOAD_NO_BITMAP;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001001 }
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +00001002 } else {
1003 SkDEBUGF(("unknown kind of font \"%s\" size %f?\n",
1004 fFace->family_name, SkFixedToScalar(fScaleY)));
reed@android.com8a1c16f2008-12-17 15:59:43 +00001005 }
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +00001006
1007 fDoLinearMetrics = linearMetrics;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001008}
1009
1010SkScalerContext_FreeType::~SkScalerContext_FreeType() {
scroggo@google.com94bc60f2012-10-04 20:45:06 +00001011 SkAutoMutexAcquire ac(gFTMutex);
1012
reed@android.com8a1c16f2008-12-17 15:59:43 +00001013 if (fFTSize != NULL) {
1014 FT_Done_Size(fFTSize);
1015 }
1016
reed@android.com8a1c16f2008-12-17 15:59:43 +00001017 if (fFace != NULL) {
1018 unref_ft_face(fFace);
1019 }
1020 if (--gFTCount == 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001021 FT_Done_FreeType(gFTLibrary);
1022 SkDEBUGCODE(gFTLibrary = NULL;)
1023 }
1024}
1025
1026/* We call this before each use of the fFace, since we may be sharing
1027 this face with other context (at different sizes).
1028*/
1029FT_Error SkScalerContext_FreeType::setupSize() {
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +00001030 FT_Error err = FT_Activate_Size(fFTSize);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001031 if (err != 0) {
1032 SkDEBUGF(("SkScalerContext_FreeType::FT_Activate_Size(%x, 0x%x, 0x%x) returned 0x%x\n",
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +00001033 fFaceRec->fFontID, fScaleX, fScaleY, err));
reed@android.com8a1c16f2008-12-17 15:59:43 +00001034 fFTSize = NULL;
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +00001035 return err;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001036 }
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +00001037
1038 // seems we need to reset this every time (not sure why, but without it
1039 // I get random italics from some other fFTSize)
1040 FT_Set_Transform(fFace, &fMatrix22, NULL);
1041 return 0;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001042}
1043
ctguil@chromium.org0bc7bf52011-03-04 19:04:57 +00001044unsigned SkScalerContext_FreeType::generateGlyphCount() {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001045 return fFace->num_glyphs;
1046}
1047
1048uint16_t SkScalerContext_FreeType::generateCharToGlyph(SkUnichar uni) {
1049 return SkToU16(FT_Get_Char_Index( fFace, uni ));
1050}
1051
reed@android.com9d3a9852010-01-08 14:07:42 +00001052SkUnichar SkScalerContext_FreeType::generateGlyphToChar(uint16_t glyph) {
1053 // iterate through each cmap entry, looking for matching glyph indices
1054 FT_UInt glyphIndex;
1055 SkUnichar charCode = FT_Get_First_Char( fFace, &glyphIndex );
1056
1057 while (glyphIndex != 0) {
1058 if (glyphIndex == glyph) {
1059 return charCode;
1060 }
1061 charCode = FT_Get_Next_Char( fFace, charCode, &glyphIndex );
1062 }
1063
1064 return 0;
1065}
1066
reed@android.com8a1c16f2008-12-17 15:59:43 +00001067void SkScalerContext_FreeType::generateAdvance(SkGlyph* glyph) {
1068#ifdef FT_ADVANCES_H
1069 /* unhinted and light hinted text have linearly scaled advances
1070 * which are very cheap to compute with some font formats...
1071 */
reed@google.combdc99882011-11-21 14:36:57 +00001072 if (fDoLinearMetrics) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001073 SkAutoMutexAcquire ac(gFTMutex);
1074
1075 if (this->setupSize()) {
reed@android.com62900b42009-02-11 15:07:19 +00001076 glyph->zeroMetrics();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001077 return;
1078 }
1079
1080 FT_Error error;
1081 FT_Fixed advance;
1082
1083 error = FT_Get_Advance( fFace, glyph->getGlyphID(fBaseGlyphCount),
1084 fLoadGlyphFlags | FT_ADVANCE_FLAG_FAST_ONLY,
1085 &advance );
1086 if (0 == error) {
1087 glyph->fRsbDelta = 0;
1088 glyph->fLsbDelta = 0;
reed@google.comd074c372012-07-18 13:45:58 +00001089 glyph->fAdvanceX = SkFixedMul(fMatrix22.xx, advance);
1090 glyph->fAdvanceY = - SkFixedMul(fMatrix22.yx, advance);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001091 return;
1092 }
1093 }
1094#endif /* FT_ADVANCES_H */
1095 /* otherwise, we need to load/hint the glyph, which is slower */
1096 this->generateMetrics(glyph);
1097 return;
1098}
1099
djsollen@google.comd8b599c2012-03-19 19:44:19 +00001100void SkScalerContext_FreeType::getBBoxForCurrentGlyph(SkGlyph* glyph,
1101 FT_BBox* bbox,
1102 bool snapToPixelBoundary) {
1103
1104 FT_Outline_Get_CBox(&fFace->glyph->outline, bbox);
1105
1106 if (fRec.fFlags & SkScalerContext::kSubpixelPositioning_Flag) {
george@mozilla.comc59b5da2012-08-23 00:39:08 +00001107 int dx = SkFixedToFDot6(glyph->getSubXFixed());
1108 int dy = SkFixedToFDot6(glyph->getSubYFixed());
djsollen@google.comd8b599c2012-03-19 19:44:19 +00001109 // negate dy since freetype-y-goes-up and skia-y-goes-down
1110 bbox->xMin += dx;
1111 bbox->yMin -= dy;
1112 bbox->xMax += dx;
1113 bbox->yMax -= dy;
1114 }
1115
1116 // outset the box to integral boundaries
1117 if (snapToPixelBoundary) {
1118 bbox->xMin &= ~63;
1119 bbox->yMin &= ~63;
1120 bbox->xMax = (bbox->xMax + 63) & ~63;
1121 bbox->yMax = (bbox->yMax + 63) & ~63;
1122 }
bungeman@google.com8ff8a192012-09-25 20:38:28 +00001123
1124 // Must come after snapToPixelBoundary so that the width and height are
1125 // consistent. Otherwise asserts will fire later on when generating the
1126 // glyph image.
1127 if (fRec.fFlags & SkScalerContext::kVertical_Flag) {
1128 FT_Vector vector;
1129 vector.x = fFace->glyph->metrics.vertBearingX - fFace->glyph->metrics.horiBearingX;
1130 vector.y = -fFace->glyph->metrics.vertBearingY - fFace->glyph->metrics.horiBearingY;
1131 FT_Vector_Transform(&vector, &fMatrix22);
1132 bbox->xMin += vector.x;
1133 bbox->xMax += vector.x;
1134 bbox->yMin += vector.y;
1135 bbox->yMax += vector.y;
1136 }
djsollen@google.comd8b599c2012-03-19 19:44:19 +00001137}
1138
bungeman@google.comcbe1b542013-12-16 17:02:39 +00001139bool SkScalerContext_FreeType::getCBoxForLetter(char letter, FT_BBox* bbox) {
1140 const FT_UInt glyph_id = FT_Get_Char_Index(fFace, letter);
1141 if (!glyph_id)
1142 return false;
1143 if (FT_Load_Glyph(fFace, glyph_id, fLoadGlyphFlags) != 0)
1144 return false;
commit-bot@chromium.org6fa81d72013-12-26 15:50:29 +00001145 emboldenIfNeeded(fFace, fFace->glyph);
bungeman@google.comcbe1b542013-12-16 17:02:39 +00001146 FT_Outline_Get_CBox(&fFace->glyph->outline, bbox);
1147 return true;
1148}
1149
djsollen@google.comd8b599c2012-03-19 19:44:19 +00001150void SkScalerContext_FreeType::updateGlyphIfLCD(SkGlyph* glyph) {
1151 if (isLCD(fRec)) {
1152 if (fLCDIsVert) {
1153 glyph->fHeight += gLCDExtra;
1154 glyph->fTop -= gLCDExtra >> 1;
1155 } else {
1156 glyph->fWidth += gLCDExtra;
1157 glyph->fLeft -= gLCDExtra >> 1;
1158 }
1159 }
1160}
1161
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +00001162inline void scaleGlyphMetrics(SkGlyph& glyph, SkScalar scale) {
1163 glyph.fWidth *= scale;
1164 glyph.fHeight *= scale;
1165 glyph.fTop *= scale;
1166 glyph.fLeft *= scale;
1167
1168 SkFixed fixedScale = SkScalarToFixed(scale);
1169 glyph.fAdvanceX = SkFixedMul(glyph.fAdvanceX, fixedScale);
1170 glyph.fAdvanceY = SkFixedMul(glyph.fAdvanceY, fixedScale);
1171}
1172
reed@android.com8a1c16f2008-12-17 15:59:43 +00001173void SkScalerContext_FreeType::generateMetrics(SkGlyph* glyph) {
1174 SkAutoMutexAcquire ac(gFTMutex);
1175
1176 glyph->fRsbDelta = 0;
1177 glyph->fLsbDelta = 0;
1178
1179 FT_Error err;
1180
1181 if (this->setupSize()) {
1182 goto ERROR;
1183 }
1184
1185 err = FT_Load_Glyph( fFace, glyph->getGlyphID(fBaseGlyphCount), fLoadGlyphFlags );
1186 if (err != 0) {
mike@reedtribe.org7a722f02012-11-15 02:12:14 +00001187#if 0
1188 SkDEBUGF(("SkScalerContext_FreeType::generateMetrics(%x): FT_Load_Glyph(glyph:%d flags:%x) returned 0x%x\n",
reed@android.com8a1c16f2008-12-17 15:59:43 +00001189 fFaceRec->fFontID, glyph->getGlyphID(fBaseGlyphCount), fLoadGlyphFlags, err));
mike@reedtribe.org7a722f02012-11-15 02:12:14 +00001190#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +00001191 ERROR:
reed@android.com62900b42009-02-11 15:07:19 +00001192 glyph->zeroMetrics();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001193 return;
1194 }
commit-bot@chromium.org6fa81d72013-12-26 15:50:29 +00001195 emboldenIfNeeded(fFace, fFace->glyph);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001196
1197 switch ( fFace->glyph->format ) {
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +00001198 case FT_GLYPH_FORMAT_OUTLINE:
bungeman@google.com0f0c2882011-11-04 15:47:41 +00001199 if (0 == fFace->glyph->outline.n_contours) {
1200 glyph->fWidth = 0;
1201 glyph->fHeight = 0;
1202 glyph->fTop = 0;
1203 glyph->fLeft = 0;
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +00001204 } else {
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +00001205 FT_BBox bbox;
1206 getBBoxForCurrentGlyph(glyph, &bbox, true);
1207
1208 glyph->fWidth = SkToU16(SkFDot6Floor(bbox.xMax - bbox.xMin));
1209 glyph->fHeight = SkToU16(SkFDot6Floor(bbox.yMax - bbox.yMin));
1210 glyph->fTop = -SkToS16(SkFDot6Floor(bbox.yMax));
1211 glyph->fLeft = SkToS16(SkFDot6Floor(bbox.xMin));
1212
1213 updateGlyphIfLCD(glyph);
bungeman@google.com0f0c2882011-11-04 15:47:41 +00001214 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001215 break;
1216
1217 case FT_GLYPH_FORMAT_BITMAP:
bungeman@google.com8ff8a192012-09-25 20:38:28 +00001218 if (fRec.fFlags & SkScalerContext::kVertical_Flag) {
1219 FT_Vector vector;
1220 vector.x = fFace->glyph->metrics.vertBearingX - fFace->glyph->metrics.horiBearingX;
1221 vector.y = -fFace->glyph->metrics.vertBearingY - fFace->glyph->metrics.horiBearingY;
1222 FT_Vector_Transform(&vector, &fMatrix22);
1223 fFace->glyph->bitmap_left += SkFDot6Floor(vector.x);
1224 fFace->glyph->bitmap_top += SkFDot6Floor(vector.y);
1225 }
1226
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +00001227 if (fFace->glyph->bitmap.pixel_mode == FT_PIXEL_MODE_BGRA) {
1228 glyph->fMaskFormat = SkMask::kARGB32_Format;
1229 }
1230
reed@android.com8a1c16f2008-12-17 15:59:43 +00001231 glyph->fWidth = SkToU16(fFace->glyph->bitmap.width);
1232 glyph->fHeight = SkToU16(fFace->glyph->bitmap.rows);
1233 glyph->fTop = -SkToS16(fFace->glyph->bitmap_top);
1234 glyph->fLeft = SkToS16(fFace->glyph->bitmap_left);
1235 break;
1236
1237 default:
tomhudson@google.com0c00f212011-12-28 14:59:50 +00001238 SkDEBUGFAIL("unknown glyph format");
reed@android.com8a1c16f2008-12-17 15:59:43 +00001239 goto ERROR;
1240 }
1241
bungeman@google.com8ff8a192012-09-25 20:38:28 +00001242 if (fRec.fFlags & SkScalerContext::kVertical_Flag) {
1243 if (fDoLinearMetrics) {
1244 glyph->fAdvanceX = -SkFixedMul(fMatrix22.xy, fFace->glyph->linearVertAdvance);
1245 glyph->fAdvanceY = SkFixedMul(fMatrix22.yy, fFace->glyph->linearVertAdvance);
1246 } else {
1247 glyph->fAdvanceX = -SkFDot6ToFixed(fFace->glyph->advance.x);
1248 glyph->fAdvanceY = SkFDot6ToFixed(fFace->glyph->advance.y);
1249 }
bungeman@google.com34f10262012-03-23 18:11:47 +00001250 } else {
bungeman@google.com8ff8a192012-09-25 20:38:28 +00001251 if (fDoLinearMetrics) {
1252 glyph->fAdvanceX = SkFixedMul(fMatrix22.xx, fFace->glyph->linearHoriAdvance);
1253 glyph->fAdvanceY = -SkFixedMul(fMatrix22.yx, fFace->glyph->linearHoriAdvance);
1254 } else {
1255 glyph->fAdvanceX = SkFDot6ToFixed(fFace->glyph->advance.x);
1256 glyph->fAdvanceY = -SkFDot6ToFixed(fFace->glyph->advance.y);
bungeman@google.com34f10262012-03-23 18:11:47 +00001257
bungeman@google.com8ff8a192012-09-25 20:38:28 +00001258 if (fRec.fFlags & kDevKernText_Flag) {
1259 glyph->fRsbDelta = SkToS8(fFace->glyph->rsb_delta);
1260 glyph->fLsbDelta = SkToS8(fFace->glyph->lsb_delta);
djsollen@google.comd8b599c2012-03-19 19:44:19 +00001261 }
1262 }
djsollen@google.comd8b599c2012-03-19 19:44:19 +00001263 }
1264
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +00001265 if (fFace->glyph->format == FT_GLYPH_FORMAT_BITMAP && fScaleY && fFace->size->metrics.y_ppem) {
1266 // NOTE: both dimensions are scaled by y_ppem. this is WAI.
1267 scaleGlyphMetrics(*glyph, SkScalarDiv(SkFixedToScalar(fScaleY),
1268 SkIntToScalar(fFace->size->metrics.y_ppem)));
1269 }
djsollen@google.comd8b599c2012-03-19 19:44:19 +00001270
reed@android.com8a1c16f2008-12-17 15:59:43 +00001271#ifdef ENABLE_GLYPH_SPEW
1272 SkDEBUGF(("FT_Set_Char_Size(this:%p sx:%x sy:%x ", this, fScaleX, fScaleY));
1273 SkDEBUGF(("Metrics(glyph:%d flags:0x%x) w:%d\n", glyph->getGlyphID(fBaseGlyphCount), fLoadGlyphFlags, glyph->fWidth));
1274#endif
1275}
1276
reed@google.comea2333d2011-03-14 16:44:56 +00001277
bungeman@google.coma76de722012-10-26 19:35:54 +00001278void SkScalerContext_FreeType::generateImage(const SkGlyph& glyph) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001279 SkAutoMutexAcquire ac(gFTMutex);
1280
1281 FT_Error err;
1282
1283 if (this->setupSize()) {
1284 goto ERROR;
1285 }
1286
1287 err = FT_Load_Glyph( fFace, glyph.getGlyphID(fBaseGlyphCount), fLoadGlyphFlags);
1288 if (err != 0) {
1289 SkDEBUGF(("SkScalerContext_FreeType::generateImage: FT_Load_Glyph(glyph:%d width:%d height:%d rb:%d flags:%d) returned 0x%x\n",
1290 glyph.getGlyphID(fBaseGlyphCount), glyph.fWidth, glyph.fHeight, glyph.rowBytes(), fLoadGlyphFlags, err));
1291 ERROR:
1292 memset(glyph.fImage, 0, glyph.rowBytes() * glyph.fHeight);
1293 return;
1294 }
1295
commit-bot@chromium.org6fa81d72013-12-26 15:50:29 +00001296 emboldenIfNeeded(fFace, fFace->glyph);
bungeman@google.coma76de722012-10-26 19:35:54 +00001297 generateGlyphImage(fFace, glyph);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001298}
1299
reed@android.com8a1c16f2008-12-17 15:59:43 +00001300
1301void SkScalerContext_FreeType::generatePath(const SkGlyph& glyph,
1302 SkPath* path) {
1303 SkAutoMutexAcquire ac(gFTMutex);
1304
1305 SkASSERT(&glyph && path);
1306
1307 if (this->setupSize()) {
1308 path->reset();
1309 return;
1310 }
1311
1312 uint32_t flags = fLoadGlyphFlags;
1313 flags |= FT_LOAD_NO_BITMAP; // ignore embedded bitmaps so we're sure to get the outline
1314 flags &= ~FT_LOAD_RENDER; // don't scan convert (we just want the outline)
1315
1316 FT_Error err = FT_Load_Glyph( fFace, glyph.getGlyphID(fBaseGlyphCount), flags);
1317
1318 if (err != 0) {
1319 SkDEBUGF(("SkScalerContext_FreeType::generatePath: FT_Load_Glyph(glyph:%d flags:%d) returned 0x%x\n",
1320 glyph.getGlyphID(fBaseGlyphCount), flags, err));
1321 path->reset();
1322 return;
1323 }
commit-bot@chromium.org6fa81d72013-12-26 15:50:29 +00001324 emboldenIfNeeded(fFace, fFace->glyph);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001325
sugoi@google.com66a58ac2013-03-05 20:40:52 +00001326 generateGlyphPath(fFace, path);
bungeman@google.com8ff8a192012-09-25 20:38:28 +00001327
1328 // The path's origin from FreeType is always the horizontal layout origin.
1329 // Offset the path so that it is relative to the vertical origin if needed.
1330 if (fRec.fFlags & SkScalerContext::kVertical_Flag) {
1331 FT_Vector vector;
1332 vector.x = fFace->glyph->metrics.vertBearingX - fFace->glyph->metrics.horiBearingX;
1333 vector.y = -fFace->glyph->metrics.vertBearingY - fFace->glyph->metrics.horiBearingY;
1334 FT_Vector_Transform(&vector, &fMatrix22);
1335 path->offset(SkFDot6ToScalar(vector.x), -SkFDot6ToScalar(vector.y));
1336 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001337}
1338
1339void SkScalerContext_FreeType::generateFontMetrics(SkPaint::FontMetrics* mx,
1340 SkPaint::FontMetrics* my) {
1341 if (NULL == mx && NULL == my) {
1342 return;
1343 }
1344
1345 SkAutoMutexAcquire ac(gFTMutex);
1346
1347 if (this->setupSize()) {
reed@android.coma8a8b8b2009-05-04 15:00:11 +00001348 ERROR:
reed@android.com8a1c16f2008-12-17 15:59:43 +00001349 if (mx) {
reed@android.com4516f472009-06-29 16:25:36 +00001350 sk_bzero(mx, sizeof(SkPaint::FontMetrics));
reed@android.com8a1c16f2008-12-17 15:59:43 +00001351 }
1352 if (my) {
reed@android.com4516f472009-06-29 16:25:36 +00001353 sk_bzero(my, sizeof(SkPaint::FontMetrics));
reed@android.com8a1c16f2008-12-17 15:59:43 +00001354 }
1355 return;
1356 }
1357
reed@android.coma8a8b8b2009-05-04 15:00:11 +00001358 FT_Face face = fFace;
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +00001359 SkScalar scaleX = fScale.x();
reed@google.comf073b332013-05-06 12:21:16 +00001360 SkScalar scaleY = fScale.y();
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +00001361 SkScalar mxy = fMatrix22Scalar.getSkewX() * scaleY;
1362 SkScalar myy = fMatrix22Scalar.getScaleY() * scaleY;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001363
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +00001364 // fetch units/EM from "head" table if needed (ie for bitmap fonts)
1365 SkScalar upem = SkIntToScalar(face->units_per_EM);
1366 if (!upem) {
1367 TT_Header* ttHeader = (TT_Header*)FT_Get_Sfnt_Table(face, ft_sfnt_head);
1368 if (ttHeader) {
1369 upem = SkIntToScalar(ttHeader->Units_Per_EM);
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001370 }
1371 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001372
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +00001373 // use the os/2 table as a source of reasonable defaults.
1374 SkScalar x_height = 0.0f;
1375 SkScalar avgCharWidth = 0.0f;
bungeman@google.comcbe1b542013-12-16 17:02:39 +00001376 SkScalar cap_height = 0.0f;
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +00001377 TT_OS2* os2 = (TT_OS2*) FT_Get_Sfnt_Table(face, ft_sfnt_os2);
1378 if (os2) {
1379 x_height = scaleX * SkIntToScalar(os2->sxHeight) / upem;
1380 avgCharWidth = SkIntToScalar(os2->xAvgCharWidth) / upem;
bungeman@google.comcbe1b542013-12-16 17:02:39 +00001381 if (os2->version != 0xFFFF && os2->version >= 2) {
1382 cap_height = scaleX * SkIntToScalar(os2->sCapHeight) / upem;
1383 }
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +00001384 }
1385
1386 // pull from format-specific metrics as needed
1387 SkScalar ascent, descent, leading, xmin, xmax, ymin, ymax;
1388 if (face->face_flags & FT_FACE_FLAG_SCALABLE) { // scalable outline font
1389 ascent = -SkIntToScalar(face->ascender) / upem;
1390 descent = -SkIntToScalar(face->descender) / upem;
1391 leading = SkIntToScalar(face->height + (face->descender - face->ascender)) / upem;
1392 xmin = SkIntToScalar(face->bbox.xMin) / upem;
1393 xmax = SkIntToScalar(face->bbox.xMax) / upem;
1394 ymin = -SkIntToScalar(face->bbox.yMin) / upem;
1395 ymax = -SkIntToScalar(face->bbox.yMax) / upem;
bungeman@google.comcbe1b542013-12-16 17:02:39 +00001396 // we may be able to synthesize x_height and cap_height from outline
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +00001397 if (!x_height) {
bungeman@google.comcbe1b542013-12-16 17:02:39 +00001398 FT_BBox bbox;
1399 if (getCBoxForLetter('x', &bbox)) {
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +00001400 x_height = SkIntToScalar(bbox.yMax) / 64.0f;
1401 }
1402 }
bungeman@google.comcbe1b542013-12-16 17:02:39 +00001403 if (!cap_height) {
1404 FT_BBox bbox;
1405 if (getCBoxForLetter('H', &bbox)) {
1406 cap_height = SkIntToScalar(bbox.yMax) / 64.0f;
1407 }
1408 }
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +00001409 } else if (fStrikeIndex != -1) { // bitmap strike metrics
1410 SkScalar xppem = SkIntToScalar(face->size->metrics.x_ppem);
1411 SkScalar yppem = SkIntToScalar(face->size->metrics.y_ppem);
1412 ascent = -SkIntToScalar(face->size->metrics.ascender) / (yppem * 64.0f);
1413 descent = -SkIntToScalar(face->size->metrics.descender) / (yppem * 64.0f);
1414 leading = (SkIntToScalar(face->size->metrics.height) / (yppem * 64.0f))
1415 + ascent - descent;
1416 xmin = 0.0f;
1417 xmax = SkIntToScalar(face->available_sizes[fStrikeIndex].width) / xppem;
1418 ymin = descent + leading;
1419 ymax = ascent - descent;
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +00001420 } else {
1421 goto ERROR;
1422 }
1423
1424 // synthesize elements that were not provided by the os/2 table or format-specific metrics
1425 if (!x_height) {
1426 x_height = -ascent;
1427 }
1428 if (!avgCharWidth) {
1429 avgCharWidth = xmax - xmin;
1430 }
bungeman@google.comcbe1b542013-12-16 17:02:39 +00001431 if (!cap_height) {
1432 cap_height = -ascent;
1433 }
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +00001434
1435 // disallow negative linespacing
1436 if (leading < 0.0f) {
1437 leading = 0.0f;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001438 }
1439
1440 if (mx) {
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +00001441 mx->fTop = ymax * mxy;
1442 mx->fAscent = ascent * mxy;
1443 mx->fDescent = descent * mxy;
1444 mx->fBottom = ymin * mxy;
1445 mx->fLeading = leading * mxy;
1446 mx->fAvgCharWidth = avgCharWidth * mxy;
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001447 mx->fXMin = xmin;
1448 mx->fXMax = xmax;
1449 mx->fXHeight = x_height;
bungeman@google.comcbe1b542013-12-16 17:02:39 +00001450 mx->fCapHeight = cap_height;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001451 }
1452 if (my) {
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +00001453 my->fTop = ymax * myy;
1454 my->fAscent = ascent * myy;
1455 my->fDescent = descent * myy;
1456 my->fBottom = ymin * myy;
1457 my->fLeading = leading * myy;
1458 my->fAvgCharWidth = avgCharWidth * myy;
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001459 my->fXMin = xmin;
1460 my->fXMax = xmax;
1461 my->fXHeight = x_height;
bungeman@google.comcbe1b542013-12-16 17:02:39 +00001462 my->fCapHeight = cap_height;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001463 }
1464}
1465
commit-bot@chromium.org6fa81d72013-12-26 15:50:29 +00001466void SkScalerContext_FreeType::emboldenIfNeeded(FT_Face face, FT_GlyphSlot glyph)
1467{
1468 if (fRec.fFlags & SkScalerContext::kEmbolden_Flag) {
1469 switch ( glyph->format ) {
1470 case FT_GLYPH_FORMAT_OUTLINE:
1471 FT_Pos strength;
1472 strength = FT_MulFix(face->units_per_EM, face->size->metrics.y_scale) / 24;
1473 FT_Outline_Embolden(&glyph->outline, strength);
1474 break;
1475 case FT_GLYPH_FORMAT_BITMAP:
1476 FT_GlyphSlot_Own_Bitmap(glyph);
1477 FT_Bitmap_Embolden(glyph->library, &glyph->bitmap, kBitmapEmboldenStrength, 0);
1478 break;
1479 default:
1480 SkDEBUGFAIL("unknown glyph format");
1481 }
1482 }
1483}
1484
reed@google.comb4162b12013-07-02 16:32:29 +00001485///////////////////////////////////////////////////////////////////////////////
1486
1487#include "SkUtils.h"
1488
1489static SkUnichar next_utf8(const void** chars) {
1490 return SkUTF8_NextUnichar((const char**)chars);
1491}
1492
1493static SkUnichar next_utf16(const void** chars) {
1494 return SkUTF16_NextUnichar((const uint16_t**)chars);
1495}
1496
1497static SkUnichar next_utf32(const void** chars) {
1498 const SkUnichar** uniChars = (const SkUnichar**)chars;
1499 SkUnichar uni = **uniChars;
1500 *uniChars += 1;
1501 return uni;
1502}
1503
1504typedef SkUnichar (*EncodingProc)(const void**);
1505
1506static EncodingProc find_encoding_proc(SkTypeface::Encoding enc) {
1507 static const EncodingProc gProcs[] = {
1508 next_utf8, next_utf16, next_utf32
1509 };
1510 SkASSERT((size_t)enc < SK_ARRAY_COUNT(gProcs));
1511 return gProcs[enc];
1512}
1513
1514int SkTypeface_FreeType::onCharsToGlyphs(const void* chars, Encoding encoding,
1515 uint16_t glyphs[], int glyphCount) const {
1516 AutoFTAccess fta(this);
1517 FT_Face face = fta.face();
1518 if (!face) {
1519 if (glyphs) {
1520 sk_bzero(glyphs, glyphCount * sizeof(glyphs[0]));
1521 }
1522 return 0;
1523 }
1524
1525 EncodingProc next_uni_proc = find_encoding_proc(encoding);
1526
1527 if (NULL == glyphs) {
1528 for (int i = 0; i < glyphCount; ++i) {
1529 if (0 == FT_Get_Char_Index(face, next_uni_proc(&chars))) {
1530 return i;
1531 }
1532 }
1533 return glyphCount;
1534 } else {
1535 int first = glyphCount;
1536 for (int i = 0; i < glyphCount; ++i) {
1537 unsigned id = FT_Get_Char_Index(face, next_uni_proc(&chars));
1538 glyphs[i] = SkToU16(id);
1539 if (0 == id && i < first) {
1540 first = i;
1541 }
1542 }
1543 return first;
1544 }
1545}
1546
1547int SkTypeface_FreeType::onCountGlyphs() const {
1548 // we cache this value, using -1 as a sentinel for "not computed"
1549 if (fGlyphCount < 0) {
1550 AutoFTAccess fta(this);
1551 FT_Face face = fta.face();
1552 // if the face failed, we still assign a non-negative value
1553 fGlyphCount = face ? face->num_glyphs : 0;
1554 }
1555 return fGlyphCount;
1556}
1557
bungeman@google.com839702b2013-08-07 17:09:22 +00001558SkTypeface::LocalizedStrings* SkTypeface_FreeType::onCreateFamilyNameIterator() const {
bungeman@google.coma9802692013-08-07 02:45:25 +00001559 SkTypeface::LocalizedStrings* nameIter =
1560 SkOTUtils::LocalizedStrings_NameTable::CreateForFamilyNames(*this);
1561 if (NULL == nameIter) {
1562 SkString familyName;
1563 this->getFamilyName(&familyName);
1564 SkString language("und"); //undetermined
1565 nameIter = new SkOTUtils::LocalizedStrings_SingleName(familyName, language);
1566 }
1567 return nameIter;
1568}
1569
bungeman@google.comddc218e2013-08-01 22:29:43 +00001570int SkTypeface_FreeType::onGetTableTags(SkFontTableTag tags[]) const {
1571 AutoFTAccess fta(this);
1572 FT_Face face = fta.face();
1573
1574 FT_ULong tableCount = 0;
1575 FT_Error error;
1576
1577 // When 'tag' is NULL, returns number of tables in 'length'.
1578 error = FT_Sfnt_Table_Info(face, 0, NULL, &tableCount);
1579 if (error) {
1580 return 0;
1581 }
1582
1583 if (tags) {
1584 for (FT_ULong tableIndex = 0; tableIndex < tableCount; ++tableIndex) {
1585 FT_ULong tableTag;
1586 FT_ULong tablelength;
1587 error = FT_Sfnt_Table_Info(face, tableIndex, &tableTag, &tablelength);
1588 if (error) {
1589 return 0;
1590 }
1591 tags[tableIndex] = static_cast<SkFontTableTag>(tableTag);
1592 }
1593 }
1594 return tableCount;
1595}
1596
1597size_t SkTypeface_FreeType::onGetTableData(SkFontTableTag tag, size_t offset,
1598 size_t length, void* data) const
1599{
1600 AutoFTAccess fta(this);
1601 FT_Face face = fta.face();
1602
1603 FT_ULong tableLength = 0;
1604 FT_Error error;
1605
1606 // When 'length' is 0 it is overwritten with the full table length; 'offset' is ignored.
1607 error = FT_Load_Sfnt_Table(face, tag, 0, NULL, &tableLength);
1608 if (error) {
1609 return 0;
1610 }
1611
1612 if (offset > tableLength) {
1613 return 0;
1614 }
bungeman@google.com5ecd4fa2013-08-01 22:48:21 +00001615 FT_ULong size = SkTMin((FT_ULong)length, tableLength - (FT_ULong)offset);
bungeman@google.comddc218e2013-08-01 22:29:43 +00001616 if (NULL != data) {
1617 error = FT_Load_Sfnt_Table(face, tag, offset, reinterpret_cast<FT_Byte*>(data), &size);
1618 if (error) {
1619 return 0;
1620 }
1621 }
1622
1623 return size;
1624}
1625
reed@google.comb4162b12013-07-02 16:32:29 +00001626///////////////////////////////////////////////////////////////////////////////
1627///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +00001628
reed@android.com8a1c16f2008-12-17 15:59:43 +00001629/* Export this so that other parts of our FonttHost port can make use of our
1630 ability to extract the name+style from a stream, using FreeType's api.
1631*/
djsollen@google.com4dc686d2012-02-15 21:03:45 +00001632bool find_name_and_attributes(SkStream* stream, SkString* name,
bungeman@google.comfe747652013-03-25 19:36:11 +00001633 SkTypeface::Style* style, bool* isFixedPitch) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001634 FT_Library library;
reed@android.combfbd4ff2009-07-23 17:44:41 +00001635 if (FT_Init_FreeType(&library)) {
djsollen@google.com4dc686d2012-02-15 21:03:45 +00001636 return false;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001637 }
1638
1639 FT_Open_Args args;
1640 memset(&args, 0, sizeof(args));
1641
1642 const void* memoryBase = stream->getMemoryBase();
1643 FT_StreamRec streamRec;
1644
1645 if (NULL != memoryBase) {
1646 args.flags = FT_OPEN_MEMORY;
1647 args.memory_base = (const FT_Byte*)memoryBase;
1648 args.memory_size = stream->getLength();
1649 } else {
1650 memset(&streamRec, 0, sizeof(streamRec));
djsollen@google.com5dd45022013-03-21 13:30:54 +00001651 streamRec.size = stream->getLength();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001652 streamRec.descriptor.pointer = stream;
1653 streamRec.read = sk_stream_read;
1654 streamRec.close = sk_stream_close;
1655
1656 args.flags = FT_OPEN_STREAM;
1657 args.stream = &streamRec;
1658 }
1659
1660 FT_Face face;
1661 if (FT_Open_Face(library, &args, 0, &face)) {
1662 FT_Done_FreeType(library);
djsollen@google.com4dc686d2012-02-15 21:03:45 +00001663 return false;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001664 }
1665
djsollen@google.com4dc686d2012-02-15 21:03:45 +00001666 int tempStyle = SkTypeface::kNormal;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001667 if (face->style_flags & FT_STYLE_FLAG_BOLD) {
djsollen@google.com4dc686d2012-02-15 21:03:45 +00001668 tempStyle |= SkTypeface::kBold;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001669 }
1670 if (face->style_flags & FT_STYLE_FLAG_ITALIC) {
djsollen@google.com4dc686d2012-02-15 21:03:45 +00001671 tempStyle |= SkTypeface::kItalic;
1672 }
1673
1674 if (name) {
1675 name->set(face->family_name);
1676 }
1677 if (style) {
1678 *style = (SkTypeface::Style) tempStyle;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001679 }
bungeman@google.comfe747652013-03-25 19:36:11 +00001680 if (isFixedPitch) {
1681 *isFixedPitch = FT_IS_FIXED_WIDTH(face);
reed@google.com5b31b0f2011-02-23 14:41:42 +00001682 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001683
1684 FT_Done_Face(face);
1685 FT_Done_FreeType(library);
djsollen@google.com4dc686d2012-02-15 21:03:45 +00001686 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001687}