blob: 4bc7870f9ca488421d05ae7d5be603b41b4b16c3 [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
reed@android.com8a1c16f2008-12-17 15:59:43 +000010#include "SkBitmap.h"
11#include "SkCanvas.h"
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000012#include "SkColorPriv.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000013#include "SkDescriptor.h"
14#include "SkFDot6.h"
15#include "SkFontHost.h"
16#include "SkMask.h"
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +000017#include "SkAdvancedTypefaceMetrics.h"
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000018#include "SkScalerContext.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000019#include "SkStream.h"
20#include "SkString.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000021#include "SkTemplates.h"
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000022#include "SkThread.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000023
24#include <ft2build.h>
25#include FT_FREETYPE_H
26#include FT_OUTLINE_H
27#include FT_SIZES_H
agl@chromium.orgcc3096b2009-04-22 22:09:04 +000028#include FT_TRUETYPE_TABLES_H
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000029#include FT_TYPE1_TABLES_H
agl@chromium.orge76073b2010-06-04 20:31:17 +000030#include FT_BITMAP_H
agl@chromium.org36bb6972010-06-04 20:57:16 +000031// In the past, FT_GlyphSlot_Own_Bitmap was defined in this header file.
32#include FT_SYNTHESIS_H
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000033#include FT_XFREE86_H
epoger@google.com5070d792011-06-29 20:43:14 +000034#ifdef FT_LCD_FILTER_H
agl@chromium.org309485b2009-07-21 17:41:32 +000035#include FT_LCD_FILTER_H
epoger@google.com5070d792011-06-29 20:43:14 +000036#endif
agl@chromium.org309485b2009-07-21 17:41:32 +000037
reed@android.com8a1c16f2008-12-17 15:59:43 +000038#ifdef FT_ADVANCES_H
39#include FT_ADVANCES_H
40#endif
41
agl@chromium.orgcc3096b2009-04-22 22:09:04 +000042#if 0
43// Also include the files by name for build tools which require this.
44#include <freetype/freetype.h>
45#include <freetype/ftoutln.h>
46#include <freetype/ftsizes.h>
47#include <freetype/tttables.h>
48#include <freetype/ftadvanc.h>
agl@chromium.org309485b2009-07-21 17:41:32 +000049#include <freetype/ftlcdfil.h>
agl@chromium.orge76073b2010-06-04 20:31:17 +000050#include <freetype/ftbitmap.h>
agl@chromium.org36bb6972010-06-04 20:57:16 +000051#include <freetype/ftsynth.h>
agl@chromium.orgcc3096b2009-04-22 22:09:04 +000052#endif
53
reed@android.com8a1c16f2008-12-17 15:59:43 +000054//#define ENABLE_GLYPH_SPEW // for tracing calls
55//#define DUMP_STRIKE_CREATION
56
57#ifdef SK_DEBUG
58 #define SkASSERT_CONTINUE(pred) \
59 do { \
60 if (!(pred)) \
61 SkDebugf("file %s:%d: assert failed '" #pred "'\n", __FILE__, __LINE__); \
62 } while (false)
63#else
64 #define SkASSERT_CONTINUE(pred)
65#endif
66
vandebo@chromium.org6f72d1e2011-02-14 23:19:59 +000067using namespace skia_advanced_typeface_metrics_utils;
68
reed@google.combde3c8e2011-05-18 11:58:10 +000069// SK_FREETYPE_LCD_LERP should be 0...256
70// 0 means no color reduction (e.g. just as returned from FreeType)
71// 256 means 100% color reduction (e.g. gray)
reed@google.comc5181342011-05-17 20:52:46 +000072//
73#ifndef SK_FREETYPE_LCD_LERP
reed@google.combde3c8e2011-05-18 11:58:10 +000074 #define SK_FREETYPE_LCD_LERP 96
reed@google.comc5181342011-05-17 20:52:46 +000075#endif
76
reed@google.comeffc5012011-06-27 16:44:46 +000077static bool isLCD(const SkScalerContext::Rec& rec) {
78 switch (rec.fMaskFormat) {
79 case SkMask::kLCD16_Format:
80 case SkMask::kLCD32_Format:
81 return true;
82 default:
83 return false;
84 }
85}
86
reed@android.com8a1c16f2008-12-17 15:59:43 +000087//////////////////////////////////////////////////////////////////////////
88
89struct SkFaceRec;
90
91static SkMutex gFTMutex;
92static int gFTCount;
93static FT_Library gFTLibrary;
94static SkFaceRec* gFaceRecHead;
agl@chromium.orgf18d8762009-07-28 18:38:08 +000095static bool gLCDSupportValid; // true iff |gLCDSupport| has been set.
96static bool gLCDSupport; // true iff LCD is supported by the runtime.
reed@android.com8a1c16f2008-12-17 15:59:43 +000097
98/////////////////////////////////////////////////////////////////////////
99
agl@chromium.orge76073b2010-06-04 20:31:17 +0000100// See http://freetype.sourceforge.net/freetype2/docs/reference/ft2-bitmap_handling.html#FT_Bitmap_Embolden
101// This value was chosen by eyeballing the result in Firefox and trying to match it.
102static const FT_Pos kBitmapEmboldenStrength = 1 << 6;
103
agl@chromium.org309485b2009-07-21 17:41:32 +0000104static bool
105InitFreetype() {
106 FT_Error err = FT_Init_FreeType(&gFTLibrary);
reed@google.comea2333d2011-03-14 16:44:56 +0000107 if (err) {
agl@chromium.org309485b2009-07-21 17:41:32 +0000108 return false;
reed@google.comea2333d2011-03-14 16:44:56 +0000109 }
agl@chromium.org309485b2009-07-21 17:41:32 +0000110
agl@chromium.org309485b2009-07-21 17:41:32 +0000111 // Setup LCD filtering. This reduces colour fringes for LCD rendered
112 // glyphs.
epoger@google.comb371ed12011-06-29 21:20:52 +0000113#ifdef FT_LCD_FILTER_H
agl@chromium.org309485b2009-07-21 17:41:32 +0000114 err = FT_Library_SetLcdFilter(gFTLibrary, FT_LCD_FILTER_DEFAULT);
agl@chromium.orgf18d8762009-07-28 18:38:08 +0000115 gLCDSupport = err == 0;
epoger@google.com5070d792011-06-29 20:43:14 +0000116#else
117 gLCDSupport = false;
118#endif
reed@android.com61608aa2009-07-31 14:52:54 +0000119 gLCDSupportValid = true;
agl@chromium.org309485b2009-07-21 17:41:32 +0000120
121 return true;
122}
123
reed@android.com8a1c16f2008-12-17 15:59:43 +0000124class SkScalerContext_FreeType : public SkScalerContext {
125public:
126 SkScalerContext_FreeType(const SkDescriptor* desc);
127 virtual ~SkScalerContext_FreeType();
agl@chromium.orgcc3096b2009-04-22 22:09:04 +0000128
reed@android.com62900b42009-02-11 15:07:19 +0000129 bool success() const {
reed@android.coma0f5d152009-06-22 17:38:10 +0000130 return fFaceRec != NULL &&
131 fFTSize != NULL &&
132 fFace != NULL;
reed@android.com62900b42009-02-11 15:07:19 +0000133 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000134
135protected:
ctguil@chromium.org0bc7bf52011-03-04 19:04:57 +0000136 virtual unsigned generateGlyphCount();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000137 virtual uint16_t generateCharToGlyph(SkUnichar uni);
138 virtual void generateAdvance(SkGlyph* glyph);
139 virtual void generateMetrics(SkGlyph* glyph);
140 virtual void generateImage(const SkGlyph& glyph);
141 virtual void generatePath(const SkGlyph& glyph, SkPath* path);
142 virtual void generateFontMetrics(SkPaint::FontMetrics* mx,
143 SkPaint::FontMetrics* my);
reed@android.com9d3a9852010-01-08 14:07:42 +0000144 virtual SkUnichar generateGlyphToChar(uint16_t glyph);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000145
146private:
147 SkFaceRec* fFaceRec;
148 FT_Face fFace; // reference to shared face in gFaceRecHead
149 FT_Size fFTSize; // our own copy
150 SkFixed fScaleX, fScaleY;
151 FT_Matrix fMatrix22;
152 uint32_t fLoadGlyphFlags;
reed@google.combdc99882011-11-21 14:36:57 +0000153 bool fDoLinearMetrics;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000154
155 FT_Error setupSize();
senorblanco@chromium.org4526a842010-02-05 23:08:20 +0000156 void emboldenOutline(FT_Outline* outline);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000157};
158
159///////////////////////////////////////////////////////////////////////////
160///////////////////////////////////////////////////////////////////////////
161
162#include "SkStream.h"
163
164struct SkFaceRec {
165 SkFaceRec* fNext;
166 FT_Face fFace;
167 FT_StreamRec fFTStream;
168 SkStream* fSkStream;
169 uint32_t fRefCnt;
170 uint32_t fFontID;
171
reed@android.comb1d9d2e2009-03-04 17:37:51 +0000172 // assumes ownership of the stream, will call unref() when its done
reed@android.com8a1c16f2008-12-17 15:59:43 +0000173 SkFaceRec(SkStream* strm, uint32_t fontID);
174 ~SkFaceRec() {
reed@android.comb1d9d2e2009-03-04 17:37:51 +0000175 fSkStream->unref();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000176 }
177};
178
179extern "C" {
180 static unsigned long sk_stream_read(FT_Stream stream,
181 unsigned long offset,
182 unsigned char* buffer,
183 unsigned long count ) {
184 SkStream* str = (SkStream*)stream->descriptor.pointer;
185
186 if (count) {
187 if (!str->rewind()) {
188 return 0;
189 } else {
190 unsigned long ret;
191 if (offset) {
192 ret = str->read(NULL, offset);
193 if (ret != offset) {
194 return 0;
195 }
196 }
197 ret = str->read(buffer, count);
198 if (ret != count) {
199 return 0;
200 }
201 count = ret;
202 }
203 }
204 return count;
205 }
206
207 static void sk_stream_close( FT_Stream stream) {}
208}
209
210SkFaceRec::SkFaceRec(SkStream* strm, uint32_t fontID)
211 : fSkStream(strm), fFontID(fontID) {
212// SkDEBUGF(("SkFaceRec: opening %s (%p)\n", key.c_str(), strm));
213
reed@android.com4516f472009-06-29 16:25:36 +0000214 sk_bzero(&fFTStream, sizeof(fFTStream));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000215 fFTStream.size = fSkStream->getLength();
216 fFTStream.descriptor.pointer = fSkStream;
217 fFTStream.read = sk_stream_read;
218 fFTStream.close = sk_stream_close;
219}
220
reed@android.com62900b42009-02-11 15:07:19 +0000221// Will return 0 on failure
reed@android.com8a1c16f2008-12-17 15:59:43 +0000222static SkFaceRec* ref_ft_face(uint32_t fontID) {
223 SkFaceRec* rec = gFaceRecHead;
224 while (rec) {
225 if (rec->fFontID == fontID) {
226 SkASSERT(rec->fFace);
227 rec->fRefCnt += 1;
228 return rec;
229 }
230 rec = rec->fNext;
231 }
232
233 SkStream* strm = SkFontHost::OpenStream(fontID);
234 if (NULL == strm) {
235 SkDEBUGF(("SkFontHost::OpenStream failed opening %x\n", fontID));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000236 return 0;
237 }
238
239 // this passes ownership of strm to the rec
240 rec = SkNEW_ARGS(SkFaceRec, (strm, fontID));
241
242 FT_Open_Args args;
243 memset(&args, 0, sizeof(args));
244 const void* memoryBase = strm->getMemoryBase();
245
246 if (NULL != memoryBase) {
247//printf("mmap(%s)\n", keyString.c_str());
248 args.flags = FT_OPEN_MEMORY;
249 args.memory_base = (const FT_Byte*)memoryBase;
250 args.memory_size = strm->getLength();
251 } else {
252//printf("fopen(%s)\n", keyString.c_str());
253 args.flags = FT_OPEN_STREAM;
254 args.stream = &rec->fFTStream;
255 }
256
agl@chromium.org61a678a2010-08-06 18:08:18 +0000257 int face_index;
258 int length = SkFontHost::GetFileName(fontID, NULL, 0, &face_index);
259 FT_Error err = FT_Open_Face(gFTLibrary, &args, length ? face_index : 0,
260 &rec->fFace);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000261
262 if (err) { // bad filename, try the default font
263 fprintf(stderr, "ERROR: unable to open font '%x'\n", fontID);
264 SkDELETE(rec);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000265 return 0;
266 } else {
267 SkASSERT(rec->fFace);
268 //fprintf(stderr, "Opened font '%s'\n", filename.c_str());
269 rec->fNext = gFaceRecHead;
270 gFaceRecHead = rec;
271 rec->fRefCnt = 1;
272 return rec;
273 }
274}
275
276static void unref_ft_face(FT_Face face) {
277 SkFaceRec* rec = gFaceRecHead;
278 SkFaceRec* prev = NULL;
279 while (rec) {
280 SkFaceRec* next = rec->fNext;
281 if (rec->fFace == face) {
282 if (--rec->fRefCnt == 0) {
283 if (prev) {
284 prev->fNext = next;
285 } else {
286 gFaceRecHead = next;
287 }
288 FT_Done_Face(face);
289 SkDELETE(rec);
290 }
291 return;
292 }
293 prev = rec;
294 rec = next;
295 }
296 SkASSERT("shouldn't get here, face not in list");
297}
298
299///////////////////////////////////////////////////////////////////////////
300
vandebo@chromium.org16be6b82011-01-28 21:28:56 +0000301// Work around for old versions of freetype.
302static FT_Error getAdvances(FT_Face face, FT_UInt start, FT_UInt count,
303 FT_Int32 loadFlags, FT_Fixed* advances) {
304#ifdef FT_ADVANCES_H
305 return FT_Get_Advances(face, start, count, loadFlags, advances);
306#else
307 if (!face || start >= face->num_glyphs ||
308 start + count > face->num_glyphs || loadFlags != FT_LOAD_NO_SCALE) {
309 return 6; // "Invalid argument."
310 }
311 if (count == 0)
312 return 0;
313
314 for (int i = 0; i < count; i++) {
315 FT_Error err = FT_Load_Glyph(face, start + i, FT_LOAD_NO_SCALE);
316 if (err)
317 return err;
318 advances[i] = face->glyph->advance.x;
319 }
320
321 return 0;
322#endif
323}
324
325static bool canEmbed(FT_Face face) {
djsollen@google.com3839ca12011-11-03 17:31:41 +0000326// The Android freetype library does not compile the FT_Get_FSType_Flags
327// function, so we are required to add the !defined(SK_BUILD_FOR_ANDROID) until
328// support is added to Androids port of freetype.
329#if defined(FT_FSTYPE_RESTRICTED_LICENSE_EMBEDDING) && !defined(SK_BUILD_FOR_ANDROID)
vandebo@chromium.org16be6b82011-01-28 21:28:56 +0000330 FT_UShort fsType = FT_Get_FSType_Flags(face);
331 return (fsType & (FT_FSTYPE_RESTRICTED_LICENSE_EMBEDDING |
332 FT_FSTYPE_BITMAP_EMBEDDING_ONLY)) == 0;
333#else
334 // No embedding is 0x2 and bitmap embedding only is 0x200.
335 TT_OS2* os2_table;
336 if ((os2_table = (TT_OS2*)FT_Get_Sfnt_Table(face, ft_sfnt_os2)) != NULL) {
337 return (os2_table->fsType & 0x202) == 0;
338 }
339 return false; // We tried, fail safe.
340#endif
341}
342
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000343static bool GetLetterCBox(FT_Face face, char letter, FT_BBox* bbox) {
344 const FT_UInt glyph_id = FT_Get_Char_Index(face, letter);
345 if (!glyph_id)
346 return false;
347 FT_Load_Glyph(face, glyph_id, FT_LOAD_NO_SCALE);
348 FT_Outline_Get_CBox(&face->glyph->outline, bbox);
349 return true;
350}
351
vandebo@chromium.org6f72d1e2011-02-14 23:19:59 +0000352static bool getWidthAdvance(FT_Face face, int gId, int16_t* data) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000353 FT_Fixed advance = 0;
vandebo@chromium.org6f72d1e2011-02-14 23:19:59 +0000354 if (getAdvances(face, gId, 1, FT_LOAD_NO_SCALE, &advance)) {
355 return false;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000356 }
vandebo@chromium.org6f72d1e2011-02-14 23:19:59 +0000357 SkASSERT(data);
358 *data = advance;
359 return true;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000360}
361
vandebo@chromium.org6744d492011-05-09 18:13:47 +0000362static void populate_glyph_to_unicode(FT_Face& face,
363 SkTDArray<SkUnichar>* glyphToUnicode) {
364 // Check and see if we have Unicode cmaps.
365 for (int i = 0; i < face->num_charmaps; ++i) {
366 // CMaps known to support Unicode:
367 // Platform ID Encoding ID Name
368 // ----------- ----------- -----------------------------------
369 // 0 0,1 Apple Unicode
370 // 0 3 Apple Unicode 2.0 (preferred)
371 // 3 1 Microsoft Unicode UCS-2
372 // 3 10 Microsoft Unicode UCS-4 (preferred)
373 //
374 // See Apple TrueType Reference Manual
375 // http://developer.apple.com/fonts/TTRefMan/RM06/Chap6cmap.html
376 // http://developer.apple.com/fonts/TTRefMan/RM06/Chap6name.html#ID
377 // Microsoft OpenType Specification
378 // http://www.microsoft.com/typography/otspec/cmap.htm
379
380 FT_UShort platformId = face->charmaps[i]->platform_id;
381 FT_UShort encodingId = face->charmaps[i]->encoding_id;
382
383 if (platformId != 0 && platformId != 3) {
384 continue;
385 }
386 if (platformId == 3 && encodingId != 1 && encodingId != 10) {
387 continue;
388 }
389 bool preferredMap = ((platformId == 3 && encodingId == 10) ||
390 (platformId == 0 && encodingId == 3));
391
392 FT_Set_Charmap(face, face->charmaps[i]);
393 if (glyphToUnicode->isEmpty()) {
394 glyphToUnicode->setCount(face->num_glyphs);
395 memset(glyphToUnicode->begin(), 0,
396 sizeof(SkUnichar) * face->num_glyphs);
397 }
398
399 // Iterate through each cmap entry.
400 FT_UInt glyphIndex;
401 for (SkUnichar charCode = FT_Get_First_Char(face, &glyphIndex);
402 glyphIndex != 0;
403 charCode = FT_Get_Next_Char(face, charCode, &glyphIndex)) {
404 if (charCode &&
405 ((*glyphToUnicode)[glyphIndex] == 0 || preferredMap)) {
406 (*glyphToUnicode)[glyphIndex] = charCode;
407 }
408 }
409 }
410}
411
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000412// static
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000413SkAdvancedTypefaceMetrics* SkFontHost::GetAdvancedTypefaceMetrics(
vandebo@chromium.org325cb9a2011-03-30 18:36:29 +0000414 uint32_t fontID,
vandebo@chromium.org37ad8fb2011-08-18 02:38:50 +0000415 SkAdvancedTypefaceMetrics::PerGlyphInfo perGlyphInfo,
416 const uint32_t* glyphIDs,
417 uint32_t glyphIDsCount) {
djsollen@google.comda957722011-11-16 17:00:46 +0000418#if defined(SK_BUILD_FOR_MAC)
reed@google.com8a5d6922011-03-14 15:08:03 +0000419 return NULL;
420#else
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000421 SkAutoMutexAcquire ac(gFTMutex);
422 FT_Library libInit = NULL;
423 if (gFTCount == 0) {
424 if (!InitFreetype())
425 sk_throw();
426 libInit = gFTLibrary;
427 }
428 SkAutoTCallIProc<struct FT_LibraryRec_, FT_Done_FreeType> ftLib(libInit);
429 SkFaceRec* rec = ref_ft_face(fontID);
430 if (NULL == rec)
431 return NULL;
432 FT_Face face = rec->fFace;
433
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000434 SkAdvancedTypefaceMetrics* info = new SkAdvancedTypefaceMetrics;
435 info->fFontName.set(FT_Get_Postscript_Name(face));
436 info->fMultiMaster = FT_HAS_MULTIPLE_MASTERS(face);
437 info->fLastGlyphID = face->num_glyphs - 1;
438 info->fEmSize = 1000;
439
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000440 bool cid = false;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000441 const char* fontType = FT_Get_X11_Font_Format(face);
vandebo@chromium.orgc3a2ae52011-02-03 21:48:23 +0000442 if (strcmp(fontType, "Type 1") == 0) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000443 info->fType = SkAdvancedTypefaceMetrics::kType1_Font;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000444 } else if (strcmp(fontType, "CID Type 1") == 0) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000445 info->fType = SkAdvancedTypefaceMetrics::kType1CID_Font;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000446 cid = true;
447 } else if (strcmp(fontType, "CFF") == 0) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000448 info->fType = SkAdvancedTypefaceMetrics::kCFF_Font;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000449 } else if (strcmp(fontType, "TrueType") == 0) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000450 info->fType = SkAdvancedTypefaceMetrics::kTrueType_Font;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000451 cid = true;
452 TT_Header* ttHeader;
453 if ((ttHeader = (TT_Header*)FT_Get_Sfnt_Table(face,
454 ft_sfnt_head)) != NULL) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000455 info->fEmSize = ttHeader->Units_Per_EM;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000456 }
457 }
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000458
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000459 info->fStyle = 0;
460 if (FT_IS_FIXED_WIDTH(face))
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000461 info->fStyle |= SkAdvancedTypefaceMetrics::kFixedPitch_Style;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000462 if (face->style_flags & FT_STYLE_FLAG_ITALIC)
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000463 info->fStyle |= SkAdvancedTypefaceMetrics::kItalic_Style;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000464 // We should set either Symbolic or Nonsymbolic; Nonsymbolic if the font's
465 // character set is a subset of 'Adobe standard Latin.'
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000466 info->fStyle |= SkAdvancedTypefaceMetrics::kSymbolic_Style;
467
468 PS_FontInfoRec ps_info;
469 TT_Postscript* tt_info;
470 if (FT_Get_PS_Font_Info(face, &ps_info) == 0) {
471 info->fItalicAngle = ps_info.italic_angle;
472 } else if ((tt_info =
473 (TT_Postscript*)FT_Get_Sfnt_Table(face,
474 ft_sfnt_post)) != NULL) {
475 info->fItalicAngle = SkFixedToScalar(tt_info->italicAngle);
476 } else {
477 info->fItalicAngle = 0;
478 }
479
480 info->fAscent = face->ascender;
481 info->fDescent = face->descender;
482
483 // Figure out a good guess for StemV - Min width of i, I, !, 1.
484 // This probably isn't very good with an italic font.
485 int16_t min_width = SHRT_MAX;
vandebo@chromium.org6f72d1e2011-02-14 23:19:59 +0000486 info->fStemV = 0;
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000487 char stem_chars[] = {'i', 'I', '!', '1'};
488 for (size_t i = 0; i < SK_ARRAY_COUNT(stem_chars); i++) {
489 FT_BBox bbox;
490 if (GetLetterCBox(face, stem_chars[i], &bbox)) {
491 int16_t width = bbox.xMax - bbox.xMin;
492 if (width > 0 && width < min_width) {
493 min_width = width;
494 info->fStemV = min_width;
495 }
496 }
497 }
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000498
499 TT_PCLT* pclt_info;
500 TT_OS2* os2_table;
501 if ((pclt_info = (TT_PCLT*)FT_Get_Sfnt_Table(face, ft_sfnt_pclt)) != NULL) {
502 info->fCapHeight = pclt_info->CapHeight;
503 uint8_t serif_style = pclt_info->SerifStyle & 0x3F;
504 if (serif_style >= 2 && serif_style <= 6)
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000505 info->fStyle |= SkAdvancedTypefaceMetrics::kSerif_Style;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000506 else if (serif_style >= 9 && serif_style <= 12)
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000507 info->fStyle |= SkAdvancedTypefaceMetrics::kScript_Style;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000508 } else if ((os2_table =
509 (TT_OS2*)FT_Get_Sfnt_Table(face, ft_sfnt_os2)) != NULL) {
510 info->fCapHeight = os2_table->sCapHeight;
511 } else {
512 // Figure out a good guess for CapHeight: average the height of M and X.
513 FT_BBox m_bbox, x_bbox;
514 bool got_m, got_x;
515 got_m = GetLetterCBox(face, 'M', &m_bbox);
516 got_x = GetLetterCBox(face, 'X', &x_bbox);
517 if (got_m && got_x) {
518 info->fCapHeight = (m_bbox.yMax - m_bbox.yMin + x_bbox.yMax -
519 x_bbox.yMin) / 2;
520 } else if (got_m && !got_x) {
521 info->fCapHeight = m_bbox.yMax - m_bbox.yMin;
522 } else if (!got_m && got_x) {
523 info->fCapHeight = x_bbox.yMax - x_bbox.yMin;
524 }
525 }
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000526
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000527 info->fBBox = SkIRect::MakeLTRB(face->bbox.xMin, face->bbox.yMax,
528 face->bbox.xMax, face->bbox.yMin);
529
vandebo@chromium.org37ad8fb2011-08-18 02:38:50 +0000530 if (!canEmbed(face) || !FT_IS_SCALABLE(face) ||
vandebo@chromium.org325cb9a2011-03-30 18:36:29 +0000531 info->fType == SkAdvancedTypefaceMetrics::kOther_Font) {
532 perGlyphInfo = SkAdvancedTypefaceMetrics::kNo_PerGlyphInfo;
533 }
534
535 if (perGlyphInfo & SkAdvancedTypefaceMetrics::kHAdvance_PerGlyphInfo) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000536 if (FT_IS_FIXED_WIDTH(face)) {
537 appendRange(&info->fGlyphWidths, 0);
538 int16_t advance = face->max_advance_width;
539 info->fGlyphWidths->fAdvance.append(1, &advance);
540 finishRange(info->fGlyphWidths.get(), 0,
541 SkAdvancedTypefaceMetrics::WidthRange::kDefault);
vandebo@chromium.org6f72d1e2011-02-14 23:19:59 +0000542 } else if (!cid) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000543 appendRange(&info->fGlyphWidths, 0);
544 // So as to not blow out the stack, get advances in batches.
545 for (int gID = 0; gID < face->num_glyphs; gID += 128) {
546 FT_Fixed advances[128];
547 int advanceCount = 128;
548 if (gID + advanceCount > face->num_glyphs)
549 advanceCount = face->num_glyphs - gID + 1;
550 getAdvances(face, gID, advanceCount, FT_LOAD_NO_SCALE,
551 advances);
552 for (int i = 0; i < advanceCount; i++) {
553 int16_t advance = advances[gID + i];
554 info->fGlyphWidths->fAdvance.append(1, &advance);
555 }
556 }
557 finishRange(info->fGlyphWidths.get(), face->num_glyphs - 1,
558 SkAdvancedTypefaceMetrics::WidthRange::kRange);
559 } else {
vandebo@chromium.org6f72d1e2011-02-14 23:19:59 +0000560 info->fGlyphWidths.reset(
vandebo@chromium.org37ad8fb2011-08-18 02:38:50 +0000561 getAdvanceData(face,
562 face->num_glyphs,
563 glyphIDs,
564 glyphIDsCount,
565 &getWidthAdvance));
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000566 }
vandebo@chromium.org325cb9a2011-03-30 18:36:29 +0000567 }
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000568
vandebo@chromium.org325cb9a2011-03-30 18:36:29 +0000569 if (perGlyphInfo & SkAdvancedTypefaceMetrics::kVAdvance_PerGlyphInfo &&
570 FT_HAS_VERTICAL(face)) {
571 SkASSERT(false); // Not implemented yet.
572 }
573
574 if (perGlyphInfo & SkAdvancedTypefaceMetrics::kGlyphNames_PerGlyphInfo &&
575 info->fType == SkAdvancedTypefaceMetrics::kType1_Font) {
576 // Postscript fonts may contain more than 255 glyphs, so we end up
577 // using multiple font descriptions with a glyph ordering. Record
578 // the name of each glyph.
579 info->fGlyphNames.reset(
580 new SkAutoTArray<SkString>(face->num_glyphs));
581 for (int gID = 0; gID < face->num_glyphs; gID++) {
582 char glyphName[128]; // PS limit for names is 127 bytes.
583 FT_Get_Glyph_Name(face, gID, glyphName, 128);
584 info->fGlyphNames->get()[gID].set(glyphName);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000585 }
586 }
587
vandebo@chromium.org6744d492011-05-09 18:13:47 +0000588 if (perGlyphInfo & SkAdvancedTypefaceMetrics::kToUnicode_PerGlyphInfo &&
589 info->fType != SkAdvancedTypefaceMetrics::kType1_Font &&
590 face->num_charmaps) {
591 populate_glyph_to_unicode(face, &(info->fGlyphToUnicode));
592 }
593
vandebo@chromium.orgc3a2ae52011-02-03 21:48:23 +0000594 if (!canEmbed(face))
595 info->fType = SkAdvancedTypefaceMetrics::kNotEmbeddable_Font;
596
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000597 unref_ft_face(face);
598 return info;
reed@google.com8a5d6922011-03-14 15:08:03 +0000599#endif
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000600}
vandebo@chromium.org37ad8fb2011-08-18 02:38:50 +0000601
reed@google.com618ef5e2011-01-26 22:10:41 +0000602///////////////////////////////////////////////////////////////////////////
603
reed@google.com8ed436c2011-07-21 14:12:36 +0000604static bool bothZero(SkScalar a, SkScalar b) {
605 return 0 == a && 0 == b;
606}
607
608// returns false if there is any non-90-rotation or skew
609static bool isAxisAligned(const SkScalerContext::Rec& rec) {
610 return 0 == rec.fPreSkewX &&
611 (bothZero(rec.fPost2x2[0][1], rec.fPost2x2[1][0]) ||
612 bothZero(rec.fPost2x2[0][0], rec.fPost2x2[1][1]));
613}
614
reed@google.com618ef5e2011-01-26 22:10:41 +0000615void SkFontHost::FilterRec(SkScalerContext::Rec* rec) {
616 if (!gLCDSupportValid) {
617 InitFreetype();
618 FT_Done_FreeType(gFTLibrary);
619 }
reed@google.com5b31b0f2011-02-23 14:41:42 +0000620
reed@google.comeffc5012011-06-27 16:44:46 +0000621 if (!gLCDSupport && isLCD(*rec)) {
reed@google.com618ef5e2011-01-26 22:10:41 +0000622 // If the runtime Freetype library doesn't support LCD mode, we disable
623 // it here.
624 rec->fMaskFormat = SkMask::kA8_Format;
625 }
reed@google.com5b31b0f2011-02-23 14:41:42 +0000626
reed@google.com618ef5e2011-01-26 22:10:41 +0000627 SkPaint::Hinting h = rec->getHinting();
reed@google.comeffc5012011-06-27 16:44:46 +0000628 if (SkPaint::kFull_Hinting == h && !isLCD(*rec)) {
reed@google.com618ef5e2011-01-26 22:10:41 +0000629 // collapse full->normal hinting if we're not doing LCD
630 h = SkPaint::kNormal_Hinting;
631 } else if ((rec->fFlags & SkScalerContext::kSubpixelPositioning_Flag) &&
632 SkPaint::kNo_Hinting != h) {
633 // to do subpixel, we must have at most slight hinting
634 h = SkPaint::kSlight_Hinting;
635 }
vandebo@chromium.org37ad8fb2011-08-18 02:38:50 +0000636#ifndef SK_IGNORE_ROTATED_FREETYPE_FIX
reed@google.com8ed436c2011-07-21 14:12:36 +0000637 // rotated text looks bad with hinting, so we disable it as needed
638 if (!isAxisAligned(*rec)) {
639 h = SkPaint::kNo_Hinting;
640 }
bsalomon@google.com0e35ca82011-07-22 17:56:19 +0000641#endif
reed@google.com618ef5e2011-01-26 22:10:41 +0000642 rec->setHinting(h);
643}
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000644
djsollen@google.comda957722011-11-16 17:00:46 +0000645#ifdef SK_BUILD_FOR_ANDROID
djsollen@google.comcd9d69b2011-03-14 20:30:14 +0000646uint32_t SkFontHost::GetUnitsPerEm(SkFontID fontID) {
647 SkAutoMutexAcquire ac(gFTMutex);
648 SkFaceRec *rec = ref_ft_face(fontID);
649 uint16_t unitsPerEm = 0;
650
651 if (rec != NULL && rec->fFace != NULL) {
652 unitsPerEm = rec->fFace->units_per_EM;
653 unref_ft_face(rec->fFace);
654 }
655
656 return (uint32_t)unitsPerEm;
657}
658#endif
659
reed@android.com8a1c16f2008-12-17 15:59:43 +0000660SkScalerContext_FreeType::SkScalerContext_FreeType(const SkDescriptor* desc)
reed@android.com62900b42009-02-11 15:07:19 +0000661 : SkScalerContext(desc) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000662 SkAutoMutexAcquire ac(gFTMutex);
663
reed@android.com8a1c16f2008-12-17 15:59:43 +0000664 if (gFTCount == 0) {
reed@android.com659aaf92009-07-23 15:20:21 +0000665 if (!InitFreetype()) {
666 sk_throw();
667 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000668 }
669 ++gFTCount;
670
671 // load the font file
reed@android.com62900b42009-02-11 15:07:19 +0000672 fFTSize = NULL;
673 fFace = NULL;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000674 fFaceRec = ref_ft_face(fRec.fFontID);
reed@android.com62900b42009-02-11 15:07:19 +0000675 if (NULL == fFaceRec) {
676 return;
677 }
678 fFace = fFaceRec->fFace;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000679
680 // compute our factors from the record
681
682 SkMatrix m;
683
684 fRec.getSingleMatrix(&m);
685
686#ifdef DUMP_STRIKE_CREATION
687 SkString keyString;
688 SkFontHost::GetDescriptorKeyString(desc, &keyString);
689 printf("========== strike [%g %g %g] [%g %g %g %g] hints %d format %d %s\n", SkScalarToFloat(fRec.fTextSize),
690 SkScalarToFloat(fRec.fPreScaleX), SkScalarToFloat(fRec.fPreSkewX),
691 SkScalarToFloat(fRec.fPost2x2[0][0]), SkScalarToFloat(fRec.fPost2x2[0][1]),
692 SkScalarToFloat(fRec.fPost2x2[1][0]), SkScalarToFloat(fRec.fPost2x2[1][1]),
agl@chromium.org309485b2009-07-21 17:41:32 +0000693 fRec.getHinting(), fRec.fMaskFormat, keyString.c_str());
reed@android.com8a1c16f2008-12-17 15:59:43 +0000694#endif
695
696 // now compute our scale factors
697 SkScalar sx = m.getScaleX();
698 SkScalar sy = m.getScaleY();
699
700 if (m.getSkewX() || m.getSkewY() || sx < 0 || sy < 0) {
701 // sort of give up on hinting
702 sx = SkMaxScalar(SkScalarAbs(sx), SkScalarAbs(m.getSkewX()));
703 sy = SkMaxScalar(SkScalarAbs(m.getSkewY()), SkScalarAbs(sy));
704 sx = sy = SkScalarAve(sx, sy);
705
706 SkScalar inv = SkScalarInvert(sx);
707
708 // flip the skew elements to go from our Y-down system to FreeType's
709 fMatrix22.xx = SkScalarToFixed(SkScalarMul(m.getScaleX(), inv));
710 fMatrix22.xy = -SkScalarToFixed(SkScalarMul(m.getSkewX(), inv));
711 fMatrix22.yx = -SkScalarToFixed(SkScalarMul(m.getSkewY(), inv));
712 fMatrix22.yy = SkScalarToFixed(SkScalarMul(m.getScaleY(), inv));
713 } else {
714 fMatrix22.xx = fMatrix22.yy = SK_Fixed1;
715 fMatrix22.xy = fMatrix22.yx = 0;
716 }
717
718 fScaleX = SkScalarToFixed(sx);
719 fScaleY = SkScalarToFixed(sy);
720
721 // compute the flags we send to Load_Glyph
722 {
reed@android.come4d0bc02009-07-24 19:53:20 +0000723 FT_Int32 loadFlags = FT_LOAD_DEFAULT;
reed@google.combdc99882011-11-21 14:36:57 +0000724 bool linearMetrics = false;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000725
agl@chromium.org70a303f2010-05-10 14:15:50 +0000726 if (SkMask::kBW_Format == fRec.fMaskFormat) {
727 // See http://code.google.com/p/chromium/issues/detail?id=43252#c24
728 loadFlags = FT_LOAD_TARGET_MONO;
reed@google.comeffc5012011-06-27 16:44:46 +0000729 if (fRec.getHinting() == SkPaint::kNo_Hinting) {
agl@chromium.org70a303f2010-05-10 14:15:50 +0000730 loadFlags = FT_LOAD_NO_HINTING;
reed@google.combdc99882011-11-21 14:36:57 +0000731 linearMetrics = true;
reed@google.comeffc5012011-06-27 16:44:46 +0000732 }
agl@chromium.org70a303f2010-05-10 14:15:50 +0000733 } else {
734 switch (fRec.getHinting()) {
735 case SkPaint::kNo_Hinting:
736 loadFlags = FT_LOAD_NO_HINTING;
reed@google.combdc99882011-11-21 14:36:57 +0000737 linearMetrics = true;
agl@chromium.org70a303f2010-05-10 14:15:50 +0000738 break;
739 case SkPaint::kSlight_Hinting:
740 loadFlags = FT_LOAD_TARGET_LIGHT; // This implies FORCE_AUTOHINT
reed@google.combdc99882011-11-21 14:36:57 +0000741 linearMetrics = true;
agl@chromium.org70a303f2010-05-10 14:15:50 +0000742 break;
743 case SkPaint::kNormal_Hinting:
agl@chromium.orga2c71cb2010-06-17 20:49:17 +0000744 if (fRec.fFlags & SkScalerContext::kAutohinting_Flag)
745 loadFlags = FT_LOAD_FORCE_AUTOHINT;
746 else
747 loadFlags = FT_LOAD_NO_AUTOHINT;
agl@chromium.org70a303f2010-05-10 14:15:50 +0000748 break;
749 case SkPaint::kFull_Hinting:
agl@chromium.orga2c71cb2010-06-17 20:49:17 +0000750 if (fRec.fFlags & SkScalerContext::kAutohinting_Flag) {
751 loadFlags = FT_LOAD_FORCE_AUTOHINT;
752 break;
753 }
agl@chromium.org70a303f2010-05-10 14:15:50 +0000754 loadFlags = FT_LOAD_TARGET_NORMAL;
reed@google.comeffc5012011-06-27 16:44:46 +0000755 if (isLCD(fRec)) {
756 if (fRec.fFlags & SkScalerContext::kLCD_Vertical_Flag) {
757 loadFlags = FT_LOAD_TARGET_LCD_V;
758 } else {
759 loadFlags = FT_LOAD_TARGET_LCD;
760 }
reed@google.comea2333d2011-03-14 16:44:56 +0000761 }
agl@chromium.org70a303f2010-05-10 14:15:50 +0000762 break;
763 default:
764 SkDebugf("---------- UNKNOWN hinting %d\n", fRec.getHinting());
765 break;
766 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000767 }
768
reed@google.comeffc5012011-06-27 16:44:46 +0000769 if ((fRec.fFlags & SkScalerContext::kEmbeddedBitmapText_Flag) == 0) {
agl@chromium.orge0d08992009-08-07 19:19:23 +0000770 loadFlags |= FT_LOAD_NO_BITMAP;
reed@google.comeffc5012011-06-27 16:44:46 +0000771 }
agl@chromium.orge0d08992009-08-07 19:19:23 +0000772
reed@google.com96a9f7912011-05-06 11:49:30 +0000773 // Always using FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH to get correct
774 // advances, as fontconfig and cairo do.
775 // See http://code.google.com/p/skia/issues/detail?id=222.
776 loadFlags |= FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH;
777
reed@android.come4d0bc02009-07-24 19:53:20 +0000778 fLoadGlyphFlags = loadFlags;
reed@google.combdc99882011-11-21 14:36:57 +0000779 fDoLinearMetrics = linearMetrics;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000780 }
781
782 // now create the FT_Size
783
784 {
785 FT_Error err;
786
787 err = FT_New_Size(fFace, &fFTSize);
788 if (err != 0) {
789 SkDEBUGF(("SkScalerContext_FreeType::FT_New_Size(%x): FT_Set_Char_Size(0x%x, 0x%x) returned 0x%x\n",
790 fFaceRec->fFontID, fScaleX, fScaleY, err));
791 fFace = NULL;
792 return;
793 }
794
795 err = FT_Activate_Size(fFTSize);
796 if (err != 0) {
797 SkDEBUGF(("SkScalerContext_FreeType::FT_Activate_Size(%x, 0x%x, 0x%x) returned 0x%x\n",
798 fFaceRec->fFontID, fScaleX, fScaleY, err));
799 fFTSize = NULL;
800 }
801
802 err = FT_Set_Char_Size( fFace,
803 SkFixedToFDot6(fScaleX), SkFixedToFDot6(fScaleY),
804 72, 72);
805 if (err != 0) {
806 SkDEBUGF(("SkScalerContext_FreeType::FT_Set_Char_Size(%x, 0x%x, 0x%x) returned 0x%x\n",
807 fFaceRec->fFontID, fScaleX, fScaleY, err));
808 fFace = NULL;
809 return;
810 }
811
812 FT_Set_Transform( fFace, &fMatrix22, NULL);
813 }
814}
815
816SkScalerContext_FreeType::~SkScalerContext_FreeType() {
817 if (fFTSize != NULL) {
818 FT_Done_Size(fFTSize);
819 }
820
821 SkAutoMutexAcquire ac(gFTMutex);
822
823 if (fFace != NULL) {
824 unref_ft_face(fFace);
825 }
826 if (--gFTCount == 0) {
827// SkDEBUGF(("FT_Done_FreeType\n"));
828 FT_Done_FreeType(gFTLibrary);
829 SkDEBUGCODE(gFTLibrary = NULL;)
830 }
831}
832
833/* We call this before each use of the fFace, since we may be sharing
834 this face with other context (at different sizes).
835*/
836FT_Error SkScalerContext_FreeType::setupSize() {
837 /* In the off-chance that a font has been removed, we want to error out
838 right away, so call resolve just to be sure.
839
840 TODO: perhaps we can skip this, by walking the global font cache and
841 killing all of the contexts when we know that a given fontID is going
842 away...
843 */
reed@android.comb1d9d2e2009-03-04 17:37:51 +0000844 if (!SkFontHost::ValidFontID(fRec.fFontID)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000845 return (FT_Error)-1;
846 }
847
848 FT_Error err = FT_Activate_Size(fFTSize);
849
850 if (err != 0) {
851 SkDEBUGF(("SkScalerContext_FreeType::FT_Activate_Size(%x, 0x%x, 0x%x) returned 0x%x\n",
852 fFaceRec->fFontID, fScaleX, fScaleY, err));
853 fFTSize = NULL;
854 } else {
855 // seems we need to reset this every time (not sure why, but without it
856 // I get random italics from some other fFTSize)
857 FT_Set_Transform( fFace, &fMatrix22, NULL);
858 }
859 return err;
860}
861
senorblanco@chromium.org4526a842010-02-05 23:08:20 +0000862void SkScalerContext_FreeType::emboldenOutline(FT_Outline* outline) {
863 FT_Pos strength;
864 strength = FT_MulFix(fFace->units_per_EM, fFace->size->metrics.y_scale)
865 / 24;
866 FT_Outline_Embolden(outline, strength);
867}
868
ctguil@chromium.org0bc7bf52011-03-04 19:04:57 +0000869unsigned SkScalerContext_FreeType::generateGlyphCount() {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000870 return fFace->num_glyphs;
871}
872
873uint16_t SkScalerContext_FreeType::generateCharToGlyph(SkUnichar uni) {
874 return SkToU16(FT_Get_Char_Index( fFace, uni ));
875}
876
reed@android.com9d3a9852010-01-08 14:07:42 +0000877SkUnichar SkScalerContext_FreeType::generateGlyphToChar(uint16_t glyph) {
878 // iterate through each cmap entry, looking for matching glyph indices
879 FT_UInt glyphIndex;
880 SkUnichar charCode = FT_Get_First_Char( fFace, &glyphIndex );
881
882 while (glyphIndex != 0) {
883 if (glyphIndex == glyph) {
884 return charCode;
885 }
886 charCode = FT_Get_Next_Char( fFace, charCode, &glyphIndex );
887 }
888
889 return 0;
890}
891
reed@android.com8a1c16f2008-12-17 15:59:43 +0000892static FT_Pixel_Mode compute_pixel_mode(SkMask::Format format) {
893 switch (format) {
894 case SkMask::kBW_Format:
895 return FT_PIXEL_MODE_MONO;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000896 case SkMask::kA8_Format:
897 default:
898 return FT_PIXEL_MODE_GRAY;
899 }
900}
901
reed@android.com8a1c16f2008-12-17 15:59:43 +0000902void SkScalerContext_FreeType::generateAdvance(SkGlyph* glyph) {
903#ifdef FT_ADVANCES_H
904 /* unhinted and light hinted text have linearly scaled advances
905 * which are very cheap to compute with some font formats...
906 */
reed@google.combdc99882011-11-21 14:36:57 +0000907 if (fDoLinearMetrics) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000908 SkAutoMutexAcquire ac(gFTMutex);
909
910 if (this->setupSize()) {
reed@android.com62900b42009-02-11 15:07:19 +0000911 glyph->zeroMetrics();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000912 return;
913 }
914
915 FT_Error error;
916 FT_Fixed advance;
917
918 error = FT_Get_Advance( fFace, glyph->getGlyphID(fBaseGlyphCount),
919 fLoadGlyphFlags | FT_ADVANCE_FLAG_FAST_ONLY,
920 &advance );
921 if (0 == error) {
922 glyph->fRsbDelta = 0;
923 glyph->fLsbDelta = 0;
924 glyph->fAdvanceX = advance; // advance *2/3; //DEBUG
925 glyph->fAdvanceY = 0;
926 return;
927 }
928 }
929#endif /* FT_ADVANCES_H */
930 /* otherwise, we need to load/hint the glyph, which is slower */
931 this->generateMetrics(glyph);
932 return;
933}
934
935void SkScalerContext_FreeType::generateMetrics(SkGlyph* glyph) {
936 SkAutoMutexAcquire ac(gFTMutex);
937
938 glyph->fRsbDelta = 0;
939 glyph->fLsbDelta = 0;
940
941 FT_Error err;
942
943 if (this->setupSize()) {
944 goto ERROR;
945 }
946
947 err = FT_Load_Glyph( fFace, glyph->getGlyphID(fBaseGlyphCount), fLoadGlyphFlags );
948 if (err != 0) {
949 SkDEBUGF(("SkScalerContext_FreeType::generateMetrics(%x): FT_Load_Glyph(glyph:%d flags:%d) returned 0x%x\n",
950 fFaceRec->fFontID, glyph->getGlyphID(fBaseGlyphCount), fLoadGlyphFlags, err));
951 ERROR:
reed@android.com62900b42009-02-11 15:07:19 +0000952 glyph->zeroMetrics();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000953 return;
954 }
955
956 switch ( fFace->glyph->format ) {
thakis@chromium.org598b8592011-05-24 05:42:55 +0000957 case FT_GLYPH_FORMAT_OUTLINE: {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000958 FT_BBox bbox;
959
bungeman@google.com0f0c2882011-11-04 15:47:41 +0000960 if (0 == fFace->glyph->outline.n_contours) {
961 glyph->fWidth = 0;
962 glyph->fHeight = 0;
963 glyph->fTop = 0;
964 glyph->fLeft = 0;
965 break;
966 }
967
senorblanco@chromium.org4526a842010-02-05 23:08:20 +0000968 if (fRec.fFlags & kEmbolden_Flag) {
969 emboldenOutline(&fFace->glyph->outline);
970 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000971 FT_Outline_Get_CBox(&fFace->glyph->outline, &bbox);
972
agl@chromium.orga2c71cb2010-06-17 20:49:17 +0000973 if (fRec.fFlags & SkScalerContext::kSubpixelPositioning_Flag) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000974 int dx = glyph->getSubXFixed() >> 10;
975 int dy = glyph->getSubYFixed() >> 10;
976 // negate dy since freetype-y-goes-up and skia-y-goes-down
977 bbox.xMin += dx;
978 bbox.yMin -= dy;
979 bbox.xMax += dx;
980 bbox.yMax -= dy;
981 }
982
983 bbox.xMin &= ~63;
984 bbox.yMin &= ~63;
985 bbox.xMax = (bbox.xMax + 63) & ~63;
986 bbox.yMax = (bbox.yMax + 63) & ~63;
987
988 glyph->fWidth = SkToU16((bbox.xMax - bbox.xMin) >> 6);
989 glyph->fHeight = SkToU16((bbox.yMax - bbox.yMin) >> 6);
990 glyph->fTop = -SkToS16(bbox.yMax >> 6);
991 glyph->fLeft = SkToS16(bbox.xMin >> 6);
992 break;
thakis@chromium.org598b8592011-05-24 05:42:55 +0000993 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000994
995 case FT_GLYPH_FORMAT_BITMAP:
agl@chromium.orge76073b2010-06-04 20:31:17 +0000996 if (fRec.fFlags & kEmbolden_Flag) {
997 FT_GlyphSlot_Own_Bitmap(fFace->glyph);
998 FT_Bitmap_Embolden(gFTLibrary, &fFace->glyph->bitmap, kBitmapEmboldenStrength, 0);
999 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001000 glyph->fWidth = SkToU16(fFace->glyph->bitmap.width);
1001 glyph->fHeight = SkToU16(fFace->glyph->bitmap.rows);
1002 glyph->fTop = -SkToS16(fFace->glyph->bitmap_top);
1003 glyph->fLeft = SkToS16(fFace->glyph->bitmap_left);
1004 break;
1005
1006 default:
1007 SkASSERT(!"unknown glyph format");
1008 goto ERROR;
1009 }
1010
agl@chromium.orga2c71cb2010-06-17 20:49:17 +00001011 if ((fRec.fFlags & SkScalerContext::kSubpixelPositioning_Flag) == 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001012 glyph->fAdvanceX = SkFDot6ToFixed(fFace->glyph->advance.x);
1013 glyph->fAdvanceY = -SkFDot6ToFixed(fFace->glyph->advance.y);
1014 if (fRec.fFlags & kDevKernText_Flag) {
1015 glyph->fRsbDelta = SkToS8(fFace->glyph->rsb_delta);
1016 glyph->fLsbDelta = SkToS8(fFace->glyph->lsb_delta);
1017 }
1018 } else {
1019 glyph->fAdvanceX = SkFixedMul(fMatrix22.xx, fFace->glyph->linearHoriAdvance);
1020 glyph->fAdvanceY = -SkFixedMul(fMatrix22.yx, fFace->glyph->linearHoriAdvance);
1021 }
1022
1023#ifdef ENABLE_GLYPH_SPEW
1024 SkDEBUGF(("FT_Set_Char_Size(this:%p sx:%x sy:%x ", this, fScaleX, fScaleY));
1025 SkDEBUGF(("Metrics(glyph:%d flags:0x%x) w:%d\n", glyph->getGlyphID(fBaseGlyphCount), fLoadGlyphFlags, glyph->fWidth));
1026#endif
1027}
1028
reed@google.combde3c8e2011-05-18 11:58:10 +00001029static int lerp(int start, int end) {
1030 SkASSERT((unsigned)SK_FREETYPE_LCD_LERP <= 256);
1031 return start + ((end - start) * (SK_FREETYPE_LCD_LERP) >> 8);
reed@google.comc5181342011-05-17 20:52:46 +00001032}
1033
1034static uint16_t packTriple(unsigned r, unsigned g, unsigned b) {
1035 if (SK_FREETYPE_LCD_LERP) {
reed@google.combde3c8e2011-05-18 11:58:10 +00001036 // want (a+b+c)/3, but we approx to avoid the divide
1037 unsigned ave = (5 * (r + g + b) + b) >> 4;
1038 r = lerp(r, ave);
1039 g = lerp(g, ave);
1040 b = lerp(b, ave);
reed@google.comc5181342011-05-17 20:52:46 +00001041 }
1042 return SkPackRGB16(r >> 3, g >> 2, b >> 3);
1043}
1044
reed@google.com73824072011-06-23 13:17:30 +00001045static uint16_t grayToRGB16(U8CPU gray) {
1046 SkASSERT(gray <= 255);
1047 return SkPackRGB16(gray >> 3, gray >> 2, gray >> 3);
1048}
1049
1050static int bittst(const uint8_t data[], int bitOffset) {
1051 SkASSERT(bitOffset >= 0);
reed@google.comc8e0f932011-06-23 19:39:49 +00001052 int lowBit = data[bitOffset >> 3] >> (~bitOffset & 7);
reed@google.com73824072011-06-23 13:17:30 +00001053 return lowBit & 1;
1054}
1055
reed@google.comeffc5012011-06-27 16:44:46 +00001056static void copyFT2LCD16(const SkGlyph& glyph, const FT_Bitmap& bitmap,
1057 int lcdIsBGR) {
reed@google.comea2333d2011-03-14 16:44:56 +00001058 SkASSERT(glyph.fHeight == bitmap.rows);
reed@google.comea2333d2011-03-14 16:44:56 +00001059 uint16_t* dst = reinterpret_cast<uint16_t*>(glyph.fImage);
reed@google.com73824072011-06-23 13:17:30 +00001060 const size_t dstRB = glyph.rowBytes();
1061 const int width = glyph.fWidth;
1062 const uint8_t* src = bitmap.buffer;
reed@google.comea2333d2011-03-14 16:44:56 +00001063
reed@google.com73824072011-06-23 13:17:30 +00001064 switch (bitmap.pixel_mode) {
1065 case FT_PIXEL_MODE_MONO: {
1066 for (int y = 0; y < glyph.fHeight; ++y) {
1067 for (int x = 0; x < width; ++x) {
1068 dst[x] = -bittst(src, x);
1069 }
1070 dst = (uint16_t*)((char*)dst + dstRB);
1071 src += bitmap.pitch;
1072 }
1073 } break;
1074 case FT_PIXEL_MODE_GRAY: {
1075 for (int y = 0; y < glyph.fHeight; ++y) {
1076 for (int x = 0; x < width; ++x) {
1077 dst[x] = grayToRGB16(src[x]);
1078 }
1079 dst = (uint16_t*)((char*)dst + dstRB);
1080 src += bitmap.pitch;
1081 }
1082 } break;
1083 default: {
1084 SkASSERT(glyph.fWidth * 3 == bitmap.width - 6);
1085 src += 3;
1086 for (int y = 0; y < glyph.fHeight; y++) {
1087 const uint8_t* triple = src;
reed@google.comeffc5012011-06-27 16:44:46 +00001088 if (lcdIsBGR) {
1089 for (int x = 0; x < width; x++) {
1090 dst[x] = packTriple(triple[2], triple[1], triple[0]);
1091 triple += 3;
1092 }
1093 } else {
1094 for (int x = 0; x < width; x++) {
1095 dst[x] = packTriple(triple[0], triple[1], triple[2]);
1096 triple += 3;
1097 }
reed@google.com73824072011-06-23 13:17:30 +00001098 }
1099 src += bitmap.pitch;
1100 dst = (uint16_t*)((char*)dst + dstRB);
1101 }
1102 } break;
reed@google.comea2333d2011-03-14 16:44:56 +00001103 }
1104}
1105
reed@android.com8a1c16f2008-12-17 15:59:43 +00001106void SkScalerContext_FreeType::generateImage(const SkGlyph& glyph) {
1107 SkAutoMutexAcquire ac(gFTMutex);
1108
1109 FT_Error err;
1110
1111 if (this->setupSize()) {
1112 goto ERROR;
1113 }
1114
1115 err = FT_Load_Glyph( fFace, glyph.getGlyphID(fBaseGlyphCount), fLoadGlyphFlags);
1116 if (err != 0) {
1117 SkDEBUGF(("SkScalerContext_FreeType::generateImage: FT_Load_Glyph(glyph:%d width:%d height:%d rb:%d flags:%d) returned 0x%x\n",
1118 glyph.getGlyphID(fBaseGlyphCount), glyph.fWidth, glyph.fHeight, glyph.rowBytes(), fLoadGlyphFlags, err));
1119 ERROR:
1120 memset(glyph.fImage, 0, glyph.rowBytes() * glyph.fHeight);
1121 return;
1122 }
1123
1124 switch ( fFace->glyph->format ) {
1125 case FT_GLYPH_FORMAT_OUTLINE: {
1126 FT_Outline* outline = &fFace->glyph->outline;
1127 FT_BBox bbox;
1128 FT_Bitmap target;
1129
senorblanco@chromium.org4526a842010-02-05 23:08:20 +00001130 if (fRec.fFlags & kEmbolden_Flag) {
1131 emboldenOutline(outline);
1132 }
1133
reed@android.com8a1c16f2008-12-17 15:59:43 +00001134 int dx = 0, dy = 0;
agl@chromium.orga2c71cb2010-06-17 20:49:17 +00001135 if (fRec.fFlags & SkScalerContext::kSubpixelPositioning_Flag) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001136 dx = glyph.getSubXFixed() >> 10;
1137 dy = glyph.getSubYFixed() >> 10;
1138 // negate dy since freetype-y-goes-up and skia-y-goes-down
1139 dy = -dy;
1140 }
1141 FT_Outline_Get_CBox(outline, &bbox);
1142 /*
1143 what we really want to do for subpixel is
1144 offset(dx, dy)
1145 compute_bounds
1146 offset(bbox & !63)
1147 but that is two calls to offset, so we do the following, which
1148 achieves the same thing with only one offset call.
1149 */
1150 FT_Outline_Translate(outline, dx - ((bbox.xMin + dx) & ~63),
1151 dy - ((bbox.yMin + dy) & ~63));
1152
reed@google.comea2333d2011-03-14 16:44:56 +00001153 if (SkMask::kLCD16_Format == glyph.fMaskFormat) {
1154 FT_Render_Glyph(fFace->glyph, FT_RENDER_MODE_LCD);
reed@google.comeffc5012011-06-27 16:44:46 +00001155 copyFT2LCD16(glyph, fFace->glyph->bitmap,
1156 fRec.fFlags & SkScalerContext::kLCD_BGROrder_Flag);
reed@google.comea2333d2011-03-14 16:44:56 +00001157 } else {
1158 target.width = glyph.fWidth;
1159 target.rows = glyph.fHeight;
1160 target.pitch = glyph.rowBytes();
1161 target.buffer = reinterpret_cast<uint8_t*>(glyph.fImage);
1162 target.pixel_mode = compute_pixel_mode(
1163 (SkMask::Format)fRec.fMaskFormat);
1164 target.num_grays = 256;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001165
reed@google.comea2333d2011-03-14 16:44:56 +00001166 memset(glyph.fImage, 0, glyph.rowBytes() * glyph.fHeight);
1167 FT_Outline_Get_Bitmap(gFTLibrary, outline, &target);
1168 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001169 } break;
1170
1171 case FT_GLYPH_FORMAT_BITMAP: {
agl@chromium.orge76073b2010-06-04 20:31:17 +00001172 if (fRec.fFlags & kEmbolden_Flag) {
1173 FT_GlyphSlot_Own_Bitmap(fFace->glyph);
1174 FT_Bitmap_Embolden(gFTLibrary, &fFace->glyph->bitmap, kBitmapEmboldenStrength, 0);
1175 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001176 SkASSERT_CONTINUE(glyph.fWidth == fFace->glyph->bitmap.width);
1177 SkASSERT_CONTINUE(glyph.fHeight == fFace->glyph->bitmap.rows);
1178 SkASSERT_CONTINUE(glyph.fTop == -fFace->glyph->bitmap_top);
1179 SkASSERT_CONTINUE(glyph.fLeft == fFace->glyph->bitmap_left);
1180
1181 const uint8_t* src = (const uint8_t*)fFace->glyph->bitmap.buffer;
1182 uint8_t* dst = (uint8_t*)glyph.fImage;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001183
agl@chromium.org558434a2009-08-11 17:22:38 +00001184 if (fFace->glyph->bitmap.pixel_mode == FT_PIXEL_MODE_GRAY ||
1185 (fFace->glyph->bitmap.pixel_mode == FT_PIXEL_MODE_MONO &&
1186 glyph.fMaskFormat == SkMask::kBW_Format)) {
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001187 unsigned srcRowBytes = fFace->glyph->bitmap.pitch;
1188 unsigned dstRowBytes = glyph.rowBytes();
1189 unsigned minRowBytes = SkMin32(srcRowBytes, dstRowBytes);
1190 unsigned extraRowBytes = dstRowBytes - minRowBytes;
1191
1192 for (int y = fFace->glyph->bitmap.rows - 1; y >= 0; --y) {
1193 memcpy(dst, src, minRowBytes);
1194 memset(dst + minRowBytes, 0, extraRowBytes);
1195 src += srcRowBytes;
1196 dst += dstRowBytes;
1197 }
agl@chromium.org558434a2009-08-11 17:22:38 +00001198 } else if (fFace->glyph->bitmap.pixel_mode == FT_PIXEL_MODE_MONO &&
reed@google.comeffc5012011-06-27 16:44:46 +00001199 glyph.fMaskFormat == SkMask::kA8_Format) {
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001200 for (int y = 0; y < fFace->glyph->bitmap.rows; ++y) {
1201 uint8_t byte = 0;
1202 int bits = 0;
1203 const uint8_t* src_row = src;
1204 uint8_t* dst_row = dst;
1205
1206 for (int x = 0; x < fFace->glyph->bitmap.width; ++x) {
1207 if (!bits) {
1208 byte = *src_row++;
1209 bits = 8;
1210 }
1211
1212 *dst_row++ = byte & 0x80 ? 0xff : 0;
1213 bits--;
1214 byte <<= 1;
1215 }
1216
1217 src += fFace->glyph->bitmap.pitch;
1218 dst += glyph.rowBytes();
1219 }
reed@google.com73824072011-06-23 13:17:30 +00001220 } else if (SkMask::kLCD16_Format == glyph.fMaskFormat) {
reed@google.comeffc5012011-06-27 16:44:46 +00001221 copyFT2LCD16(glyph, fFace->glyph->bitmap,
1222 fRec.fFlags & SkScalerContext::kLCD_BGROrder_Flag);
agl@chromium.org558434a2009-08-11 17:22:38 +00001223 } else {
reed@google.com73824072011-06-23 13:17:30 +00001224 SkASSERT(!"unknown glyph bitmap transform needed");
reed@android.com8a1c16f2008-12-17 15:59:43 +00001225 }
1226 } break;
1227
1228 default:
1229 SkASSERT(!"unknown glyph format");
1230 goto ERROR;
1231 }
reed@google.com1f6b4ae2011-11-22 14:20:55 +00001232
1233 if ((fRec.fFlags & (kGammaForBlack_Flag | kGammaForBlack_Flag)) &&
1234 SkMask::kA8_Format == glyph.fMaskFormat) {
1235 const uint8_t* tables[2];
1236 SkFontHost::GetGammaTables(tables);
1237 int index = (fRec.fFlags & kGammaForBlack_Flag) ? 0 : 1;
1238 if (tables[index]) {
1239 const uint8_t* SK_RESTRICT table = tables[index];
1240 uint8_t* SK_RESTRICT dst = (uint8_t*)glyph.fImage;
1241 unsigned rowBytes = glyph.rowBytes();
1242
1243 for (int y = glyph.fHeight - 1; y >= 0; --y) {
1244 for (int x = glyph.fWidth - 1; x >= 0; --x) {
1245 dst[x] = table[dst[x]];
1246 }
1247 dst += rowBytes;
1248 }
1249 }
1250 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001251}
1252
1253///////////////////////////////////////////////////////////////////////////////
1254
1255#define ft2sk(x) SkFixedToScalar((x) << 10)
1256
reed@android.com6f252972009-01-14 16:46:16 +00001257#if FREETYPE_MAJOR >= 2 && FREETYPE_MINOR >= 2
reed@android.com8a1c16f2008-12-17 15:59:43 +00001258 #define CONST_PARAM const
1259#else // older freetype doesn't use const here
1260 #define CONST_PARAM
1261#endif
1262
1263static int move_proc(CONST_PARAM FT_Vector* pt, void* ctx) {
1264 SkPath* path = (SkPath*)ctx;
1265 path->close(); // to close the previous contour (if any)
1266 path->moveTo(ft2sk(pt->x), -ft2sk(pt->y));
1267 return 0;
1268}
1269
1270static int line_proc(CONST_PARAM FT_Vector* pt, void* ctx) {
1271 SkPath* path = (SkPath*)ctx;
1272 path->lineTo(ft2sk(pt->x), -ft2sk(pt->y));
1273 return 0;
1274}
1275
1276static int quad_proc(CONST_PARAM FT_Vector* pt0, CONST_PARAM FT_Vector* pt1,
1277 void* ctx) {
1278 SkPath* path = (SkPath*)ctx;
1279 path->quadTo(ft2sk(pt0->x), -ft2sk(pt0->y), ft2sk(pt1->x), -ft2sk(pt1->y));
1280 return 0;
1281}
1282
1283static int cubic_proc(CONST_PARAM FT_Vector* pt0, CONST_PARAM FT_Vector* pt1,
1284 CONST_PARAM FT_Vector* pt2, void* ctx) {
1285 SkPath* path = (SkPath*)ctx;
1286 path->cubicTo(ft2sk(pt0->x), -ft2sk(pt0->y), ft2sk(pt1->x),
1287 -ft2sk(pt1->y), ft2sk(pt2->x), -ft2sk(pt2->y));
1288 return 0;
1289}
1290
1291void SkScalerContext_FreeType::generatePath(const SkGlyph& glyph,
1292 SkPath* path) {
1293 SkAutoMutexAcquire ac(gFTMutex);
1294
1295 SkASSERT(&glyph && path);
1296
1297 if (this->setupSize()) {
1298 path->reset();
1299 return;
1300 }
1301
1302 uint32_t flags = fLoadGlyphFlags;
1303 flags |= FT_LOAD_NO_BITMAP; // ignore embedded bitmaps so we're sure to get the outline
1304 flags &= ~FT_LOAD_RENDER; // don't scan convert (we just want the outline)
1305
1306 FT_Error err = FT_Load_Glyph( fFace, glyph.getGlyphID(fBaseGlyphCount), flags);
1307
1308 if (err != 0) {
1309 SkDEBUGF(("SkScalerContext_FreeType::generatePath: FT_Load_Glyph(glyph:%d flags:%d) returned 0x%x\n",
1310 glyph.getGlyphID(fBaseGlyphCount), flags, err));
1311 path->reset();
1312 return;
1313 }
1314
senorblanco@chromium.org4526a842010-02-05 23:08:20 +00001315 if (fRec.fFlags & kEmbolden_Flag) {
1316 emboldenOutline(&fFace->glyph->outline);
1317 }
1318
reed@android.com8a1c16f2008-12-17 15:59:43 +00001319 FT_Outline_Funcs funcs;
1320
1321 funcs.move_to = move_proc;
1322 funcs.line_to = line_proc;
1323 funcs.conic_to = quad_proc;
1324 funcs.cubic_to = cubic_proc;
1325 funcs.shift = 0;
1326 funcs.delta = 0;
1327
1328 err = FT_Outline_Decompose(&fFace->glyph->outline, &funcs, path);
1329
1330 if (err != 0) {
1331 SkDEBUGF(("SkScalerContext_FreeType::generatePath: FT_Load_Glyph(glyph:%d flags:%d) returned 0x%x\n",
1332 glyph.getGlyphID(fBaseGlyphCount), flags, err));
1333 path->reset();
1334 return;
1335 }
1336
1337 path->close();
1338}
1339
1340void SkScalerContext_FreeType::generateFontMetrics(SkPaint::FontMetrics* mx,
1341 SkPaint::FontMetrics* my) {
1342 if (NULL == mx && NULL == my) {
1343 return;
1344 }
1345
1346 SkAutoMutexAcquire ac(gFTMutex);
1347
1348 if (this->setupSize()) {
reed@android.coma8a8b8b2009-05-04 15:00:11 +00001349 ERROR:
reed@android.com8a1c16f2008-12-17 15:59:43 +00001350 if (mx) {
reed@android.com4516f472009-06-29 16:25:36 +00001351 sk_bzero(mx, sizeof(SkPaint::FontMetrics));
reed@android.com8a1c16f2008-12-17 15:59:43 +00001352 }
1353 if (my) {
reed@android.com4516f472009-06-29 16:25:36 +00001354 sk_bzero(my, sizeof(SkPaint::FontMetrics));
reed@android.com8a1c16f2008-12-17 15:59:43 +00001355 }
1356 return;
1357 }
1358
reed@android.coma8a8b8b2009-05-04 15:00:11 +00001359 FT_Face face = fFace;
1360 int upem = face->units_per_EM;
1361 if (upem <= 0) {
1362 goto ERROR;
1363 }
1364
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001365 SkPoint pts[6];
1366 SkFixed ys[6];
reed@android.com8a1c16f2008-12-17 15:59:43 +00001367 SkFixed scaleY = fScaleY;
1368 SkFixed mxy = fMatrix22.xy;
1369 SkFixed myy = fMatrix22.yy;
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001370 SkScalar xmin = SkIntToScalar(face->bbox.xMin) / upem;
1371 SkScalar xmax = SkIntToScalar(face->bbox.xMax) / upem;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001372
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001373 int leading = face->height - (face->ascender + -face->descender);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001374 if (leading < 0) {
1375 leading = 0;
1376 }
1377
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001378 // Try to get the OS/2 table from the font. This contains the specific
1379 // average font width metrics which Windows uses.
1380 TT_OS2* os2 = (TT_OS2*) FT_Get_Sfnt_Table(face, ft_sfnt_os2);
1381
reed@android.com8a1c16f2008-12-17 15:59:43 +00001382 ys[0] = -face->bbox.yMax;
1383 ys[1] = -face->ascender;
1384 ys[2] = -face->descender;
1385 ys[3] = -face->bbox.yMin;
1386 ys[4] = leading;
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001387 ys[5] = os2 ? os2->xAvgCharWidth : 0;
1388
1389 SkScalar x_height;
1390 if (os2 && os2->sxHeight) {
1391 x_height = SkFixedToScalar(SkMulDiv(fScaleX, os2->sxHeight, upem));
1392 } else {
1393 const FT_UInt x_glyph = FT_Get_Char_Index(fFace, 'x');
1394 if (x_glyph) {
1395 FT_BBox bbox;
1396 FT_Load_Glyph(fFace, x_glyph, fLoadGlyphFlags);
senorblanco@chromium.org4526a842010-02-05 23:08:20 +00001397 if (fRec.fFlags & kEmbolden_Flag) {
1398 emboldenOutline(&fFace->glyph->outline);
1399 }
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001400 FT_Outline_Get_CBox(&fFace->glyph->outline, &bbox);
1401 x_height = SkIntToScalar(bbox.yMax) / 64;
1402 } else {
1403 x_height = 0;
1404 }
1405 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001406
1407 // convert upem-y values into scalar points
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001408 for (int i = 0; i < 6; i++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001409 SkFixed y = SkMulDiv(scaleY, ys[i], upem);
1410 SkFixed x = SkFixedMul(mxy, y);
1411 y = SkFixedMul(myy, y);
1412 pts[i].set(SkFixedToScalar(x), SkFixedToScalar(y));
1413 }
1414
1415 if (mx) {
1416 mx->fTop = pts[0].fX;
1417 mx->fAscent = pts[1].fX;
1418 mx->fDescent = pts[2].fX;
1419 mx->fBottom = pts[3].fX;
1420 mx->fLeading = pts[4].fX;
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001421 mx->fAvgCharWidth = pts[5].fX;
1422 mx->fXMin = xmin;
1423 mx->fXMax = xmax;
1424 mx->fXHeight = x_height;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001425 }
1426 if (my) {
1427 my->fTop = pts[0].fY;
1428 my->fAscent = pts[1].fY;
1429 my->fDescent = pts[2].fY;
1430 my->fBottom = pts[3].fY;
1431 my->fLeading = pts[4].fY;
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001432 my->fAvgCharWidth = pts[5].fY;
1433 my->fXMin = xmin;
1434 my->fXMax = xmax;
1435 my->fXHeight = x_height;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001436 }
1437}
1438
1439////////////////////////////////////////////////////////////////////////
1440////////////////////////////////////////////////////////////////////////
1441
1442SkScalerContext* SkFontHost::CreateScalerContext(const SkDescriptor* desc) {
reed@android.com62900b42009-02-11 15:07:19 +00001443 SkScalerContext_FreeType* c = SkNEW_ARGS(SkScalerContext_FreeType, (desc));
1444 if (!c->success()) {
1445 SkDELETE(c);
1446 c = NULL;
1447 }
1448 return c;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001449}
1450
1451///////////////////////////////////////////////////////////////////////////////
1452
1453/* Export this so that other parts of our FonttHost port can make use of our
1454 ability to extract the name+style from a stream, using FreeType's api.
1455*/
reed@google.com5b31b0f2011-02-23 14:41:42 +00001456SkTypeface::Style find_name_and_attributes(SkStream* stream, SkString* name,
1457 bool* isFixedWidth) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001458 FT_Library library;
reed@android.combfbd4ff2009-07-23 17:44:41 +00001459 if (FT_Init_FreeType(&library)) {
djsollen@google.com7b34ea62011-02-24 16:28:51 +00001460 name->reset();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001461 return SkTypeface::kNormal;
1462 }
1463
1464 FT_Open_Args args;
1465 memset(&args, 0, sizeof(args));
1466
1467 const void* memoryBase = stream->getMemoryBase();
1468 FT_StreamRec streamRec;
1469
1470 if (NULL != memoryBase) {
1471 args.flags = FT_OPEN_MEMORY;
1472 args.memory_base = (const FT_Byte*)memoryBase;
1473 args.memory_size = stream->getLength();
1474 } else {
1475 memset(&streamRec, 0, sizeof(streamRec));
1476 streamRec.size = stream->read(NULL, 0);
1477 streamRec.descriptor.pointer = stream;
1478 streamRec.read = sk_stream_read;
1479 streamRec.close = sk_stream_close;
1480
1481 args.flags = FT_OPEN_STREAM;
1482 args.stream = &streamRec;
1483 }
1484
1485 FT_Face face;
1486 if (FT_Open_Face(library, &args, 0, &face)) {
1487 FT_Done_FreeType(library);
djsollen@google.com7b34ea62011-02-24 16:28:51 +00001488 name->reset();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001489 return SkTypeface::kNormal;
1490 }
1491
1492 name->set(face->family_name);
1493 int style = SkTypeface::kNormal;
1494
1495 if (face->style_flags & FT_STYLE_FLAG_BOLD) {
1496 style |= SkTypeface::kBold;
1497 }
1498 if (face->style_flags & FT_STYLE_FLAG_ITALIC) {
1499 style |= SkTypeface::kItalic;
1500 }
reed@google.com5b31b0f2011-02-23 14:41:42 +00001501 if (isFixedWidth) {
1502 *isFixedWidth = FT_IS_FIXED_WIDTH(face);
1503 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001504
1505 FT_Done_Face(face);
1506 FT_Done_FreeType(library);
1507 return (SkTypeface::Style)style;
1508}