blob: f20b26af8d3010fc6f5346ec59b6c7a29673d823 [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;
153
154 FT_Error setupSize();
senorblanco@chromium.org4526a842010-02-05 23:08:20 +0000155 void emboldenOutline(FT_Outline* outline);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000156};
157
158///////////////////////////////////////////////////////////////////////////
159///////////////////////////////////////////////////////////////////////////
160
161#include "SkStream.h"
162
163struct SkFaceRec {
164 SkFaceRec* fNext;
165 FT_Face fFace;
166 FT_StreamRec fFTStream;
167 SkStream* fSkStream;
168 uint32_t fRefCnt;
169 uint32_t fFontID;
170
reed@android.comb1d9d2e2009-03-04 17:37:51 +0000171 // assumes ownership of the stream, will call unref() when its done
reed@android.com8a1c16f2008-12-17 15:59:43 +0000172 SkFaceRec(SkStream* strm, uint32_t fontID);
173 ~SkFaceRec() {
reed@android.comb1d9d2e2009-03-04 17:37:51 +0000174 fSkStream->unref();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000175 }
176};
177
178extern "C" {
179 static unsigned long sk_stream_read(FT_Stream stream,
180 unsigned long offset,
181 unsigned char* buffer,
182 unsigned long count ) {
183 SkStream* str = (SkStream*)stream->descriptor.pointer;
184
185 if (count) {
186 if (!str->rewind()) {
187 return 0;
188 } else {
189 unsigned long ret;
190 if (offset) {
191 ret = str->read(NULL, offset);
192 if (ret != offset) {
193 return 0;
194 }
195 }
196 ret = str->read(buffer, count);
197 if (ret != count) {
198 return 0;
199 }
200 count = ret;
201 }
202 }
203 return count;
204 }
205
206 static void sk_stream_close( FT_Stream stream) {}
207}
208
209SkFaceRec::SkFaceRec(SkStream* strm, uint32_t fontID)
210 : fSkStream(strm), fFontID(fontID) {
211// SkDEBUGF(("SkFaceRec: opening %s (%p)\n", key.c_str(), strm));
212
reed@android.com4516f472009-06-29 16:25:36 +0000213 sk_bzero(&fFTStream, sizeof(fFTStream));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000214 fFTStream.size = fSkStream->getLength();
215 fFTStream.descriptor.pointer = fSkStream;
216 fFTStream.read = sk_stream_read;
217 fFTStream.close = sk_stream_close;
218}
219
reed@android.com62900b42009-02-11 15:07:19 +0000220// Will return 0 on failure
reed@android.com8a1c16f2008-12-17 15:59:43 +0000221static SkFaceRec* ref_ft_face(uint32_t fontID) {
222 SkFaceRec* rec = gFaceRecHead;
223 while (rec) {
224 if (rec->fFontID == fontID) {
225 SkASSERT(rec->fFace);
226 rec->fRefCnt += 1;
227 return rec;
228 }
229 rec = rec->fNext;
230 }
231
232 SkStream* strm = SkFontHost::OpenStream(fontID);
233 if (NULL == strm) {
234 SkDEBUGF(("SkFontHost::OpenStream failed opening %x\n", fontID));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000235 return 0;
236 }
237
238 // this passes ownership of strm to the rec
239 rec = SkNEW_ARGS(SkFaceRec, (strm, fontID));
240
241 FT_Open_Args args;
242 memset(&args, 0, sizeof(args));
243 const void* memoryBase = strm->getMemoryBase();
244
245 if (NULL != memoryBase) {
246//printf("mmap(%s)\n", keyString.c_str());
247 args.flags = FT_OPEN_MEMORY;
248 args.memory_base = (const FT_Byte*)memoryBase;
249 args.memory_size = strm->getLength();
250 } else {
251//printf("fopen(%s)\n", keyString.c_str());
252 args.flags = FT_OPEN_STREAM;
253 args.stream = &rec->fFTStream;
254 }
255
agl@chromium.org61a678a2010-08-06 18:08:18 +0000256 int face_index;
257 int length = SkFontHost::GetFileName(fontID, NULL, 0, &face_index);
258 FT_Error err = FT_Open_Face(gFTLibrary, &args, length ? face_index : 0,
259 &rec->fFace);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000260
261 if (err) { // bad filename, try the default font
262 fprintf(stderr, "ERROR: unable to open font '%x'\n", fontID);
263 SkDELETE(rec);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000264 return 0;
265 } else {
266 SkASSERT(rec->fFace);
267 //fprintf(stderr, "Opened font '%s'\n", filename.c_str());
268 rec->fNext = gFaceRecHead;
269 gFaceRecHead = rec;
270 rec->fRefCnt = 1;
271 return rec;
272 }
273}
274
275static void unref_ft_face(FT_Face face) {
276 SkFaceRec* rec = gFaceRecHead;
277 SkFaceRec* prev = NULL;
278 while (rec) {
279 SkFaceRec* next = rec->fNext;
280 if (rec->fFace == face) {
281 if (--rec->fRefCnt == 0) {
282 if (prev) {
283 prev->fNext = next;
284 } else {
285 gFaceRecHead = next;
286 }
287 FT_Done_Face(face);
288 SkDELETE(rec);
289 }
290 return;
291 }
292 prev = rec;
293 rec = next;
294 }
295 SkASSERT("shouldn't get here, face not in list");
296}
297
298///////////////////////////////////////////////////////////////////////////
299
vandebo@chromium.org16be6b82011-01-28 21:28:56 +0000300// Work around for old versions of freetype.
301static FT_Error getAdvances(FT_Face face, FT_UInt start, FT_UInt count,
302 FT_Int32 loadFlags, FT_Fixed* advances) {
303#ifdef FT_ADVANCES_H
304 return FT_Get_Advances(face, start, count, loadFlags, advances);
305#else
306 if (!face || start >= face->num_glyphs ||
307 start + count > face->num_glyphs || loadFlags != FT_LOAD_NO_SCALE) {
308 return 6; // "Invalid argument."
309 }
310 if (count == 0)
311 return 0;
312
313 for (int i = 0; i < count; i++) {
314 FT_Error err = FT_Load_Glyph(face, start + i, FT_LOAD_NO_SCALE);
315 if (err)
316 return err;
317 advances[i] = face->glyph->advance.x;
318 }
319
320 return 0;
321#endif
322}
323
324static bool canEmbed(FT_Face face) {
djsollen@google.com3839ca12011-11-03 17:31:41 +0000325// The Android freetype library does not compile the FT_Get_FSType_Flags
326// function, so we are required to add the !defined(SK_BUILD_FOR_ANDROID) until
327// support is added to Androids port of freetype.
328#if defined(FT_FSTYPE_RESTRICTED_LICENSE_EMBEDDING) && !defined(SK_BUILD_FOR_ANDROID)
vandebo@chromium.org16be6b82011-01-28 21:28:56 +0000329 FT_UShort fsType = FT_Get_FSType_Flags(face);
330 return (fsType & (FT_FSTYPE_RESTRICTED_LICENSE_EMBEDDING |
331 FT_FSTYPE_BITMAP_EMBEDDING_ONLY)) == 0;
332#else
333 // No embedding is 0x2 and bitmap embedding only is 0x200.
334 TT_OS2* os2_table;
335 if ((os2_table = (TT_OS2*)FT_Get_Sfnt_Table(face, ft_sfnt_os2)) != NULL) {
336 return (os2_table->fsType & 0x202) == 0;
337 }
338 return false; // We tried, fail safe.
339#endif
340}
341
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000342static bool GetLetterCBox(FT_Face face, char letter, FT_BBox* bbox) {
343 const FT_UInt glyph_id = FT_Get_Char_Index(face, letter);
344 if (!glyph_id)
345 return false;
346 FT_Load_Glyph(face, glyph_id, FT_LOAD_NO_SCALE);
347 FT_Outline_Get_CBox(&face->glyph->outline, bbox);
348 return true;
349}
350
vandebo@chromium.org6f72d1e2011-02-14 23:19:59 +0000351static bool getWidthAdvance(FT_Face face, int gId, int16_t* data) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000352 FT_Fixed advance = 0;
vandebo@chromium.org6f72d1e2011-02-14 23:19:59 +0000353 if (getAdvances(face, gId, 1, FT_LOAD_NO_SCALE, &advance)) {
354 return false;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000355 }
vandebo@chromium.org6f72d1e2011-02-14 23:19:59 +0000356 SkASSERT(data);
357 *data = advance;
358 return true;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000359}
360
vandebo@chromium.org6744d492011-05-09 18:13:47 +0000361static void populate_glyph_to_unicode(FT_Face& face,
362 SkTDArray<SkUnichar>* glyphToUnicode) {
363 // Check and see if we have Unicode cmaps.
364 for (int i = 0; i < face->num_charmaps; ++i) {
365 // CMaps known to support Unicode:
366 // Platform ID Encoding ID Name
367 // ----------- ----------- -----------------------------------
368 // 0 0,1 Apple Unicode
369 // 0 3 Apple Unicode 2.0 (preferred)
370 // 3 1 Microsoft Unicode UCS-2
371 // 3 10 Microsoft Unicode UCS-4 (preferred)
372 //
373 // See Apple TrueType Reference Manual
374 // http://developer.apple.com/fonts/TTRefMan/RM06/Chap6cmap.html
375 // http://developer.apple.com/fonts/TTRefMan/RM06/Chap6name.html#ID
376 // Microsoft OpenType Specification
377 // http://www.microsoft.com/typography/otspec/cmap.htm
378
379 FT_UShort platformId = face->charmaps[i]->platform_id;
380 FT_UShort encodingId = face->charmaps[i]->encoding_id;
381
382 if (platformId != 0 && platformId != 3) {
383 continue;
384 }
385 if (platformId == 3 && encodingId != 1 && encodingId != 10) {
386 continue;
387 }
388 bool preferredMap = ((platformId == 3 && encodingId == 10) ||
389 (platformId == 0 && encodingId == 3));
390
391 FT_Set_Charmap(face, face->charmaps[i]);
392 if (glyphToUnicode->isEmpty()) {
393 glyphToUnicode->setCount(face->num_glyphs);
394 memset(glyphToUnicode->begin(), 0,
395 sizeof(SkUnichar) * face->num_glyphs);
396 }
397
398 // Iterate through each cmap entry.
399 FT_UInt glyphIndex;
400 for (SkUnichar charCode = FT_Get_First_Char(face, &glyphIndex);
401 glyphIndex != 0;
402 charCode = FT_Get_Next_Char(face, charCode, &glyphIndex)) {
403 if (charCode &&
404 ((*glyphToUnicode)[glyphIndex] == 0 || preferredMap)) {
405 (*glyphToUnicode)[glyphIndex] = charCode;
406 }
407 }
408 }
409}
410
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000411// static
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000412SkAdvancedTypefaceMetrics* SkFontHost::GetAdvancedTypefaceMetrics(
vandebo@chromium.org325cb9a2011-03-30 18:36:29 +0000413 uint32_t fontID,
vandebo@chromium.org37ad8fb2011-08-18 02:38:50 +0000414 SkAdvancedTypefaceMetrics::PerGlyphInfo perGlyphInfo,
415 const uint32_t* glyphIDs,
416 uint32_t glyphIDsCount) {
djsollen@google.comcd9d69b2011-03-14 20:30:14 +0000417#if defined(SK_BUILD_FOR_MAC) || defined(ANDROID)
reed@google.com8a5d6922011-03-14 15:08:03 +0000418 return NULL;
419#else
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000420 SkAutoMutexAcquire ac(gFTMutex);
421 FT_Library libInit = NULL;
422 if (gFTCount == 0) {
423 if (!InitFreetype())
424 sk_throw();
425 libInit = gFTLibrary;
426 }
427 SkAutoTCallIProc<struct FT_LibraryRec_, FT_Done_FreeType> ftLib(libInit);
428 SkFaceRec* rec = ref_ft_face(fontID);
429 if (NULL == rec)
430 return NULL;
431 FT_Face face = rec->fFace;
432
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000433 SkAdvancedTypefaceMetrics* info = new SkAdvancedTypefaceMetrics;
434 info->fFontName.set(FT_Get_Postscript_Name(face));
435 info->fMultiMaster = FT_HAS_MULTIPLE_MASTERS(face);
436 info->fLastGlyphID = face->num_glyphs - 1;
437 info->fEmSize = 1000;
438
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000439 bool cid = false;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000440 const char* fontType = FT_Get_X11_Font_Format(face);
vandebo@chromium.orgc3a2ae52011-02-03 21:48:23 +0000441 if (strcmp(fontType, "Type 1") == 0) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000442 info->fType = SkAdvancedTypefaceMetrics::kType1_Font;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000443 } else if (strcmp(fontType, "CID Type 1") == 0) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000444 info->fType = SkAdvancedTypefaceMetrics::kType1CID_Font;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000445 cid = true;
446 } else if (strcmp(fontType, "CFF") == 0) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000447 info->fType = SkAdvancedTypefaceMetrics::kCFF_Font;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000448 } else if (strcmp(fontType, "TrueType") == 0) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000449 info->fType = SkAdvancedTypefaceMetrics::kTrueType_Font;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000450 cid = true;
451 TT_Header* ttHeader;
452 if ((ttHeader = (TT_Header*)FT_Get_Sfnt_Table(face,
453 ft_sfnt_head)) != NULL) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000454 info->fEmSize = ttHeader->Units_Per_EM;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000455 }
456 }
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000457
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000458 info->fStyle = 0;
459 if (FT_IS_FIXED_WIDTH(face))
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000460 info->fStyle |= SkAdvancedTypefaceMetrics::kFixedPitch_Style;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000461 if (face->style_flags & FT_STYLE_FLAG_ITALIC)
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000462 info->fStyle |= SkAdvancedTypefaceMetrics::kItalic_Style;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000463 // We should set either Symbolic or Nonsymbolic; Nonsymbolic if the font's
464 // character set is a subset of 'Adobe standard Latin.'
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000465 info->fStyle |= SkAdvancedTypefaceMetrics::kSymbolic_Style;
466
467 PS_FontInfoRec ps_info;
468 TT_Postscript* tt_info;
469 if (FT_Get_PS_Font_Info(face, &ps_info) == 0) {
470 info->fItalicAngle = ps_info.italic_angle;
471 } else if ((tt_info =
472 (TT_Postscript*)FT_Get_Sfnt_Table(face,
473 ft_sfnt_post)) != NULL) {
474 info->fItalicAngle = SkFixedToScalar(tt_info->italicAngle);
475 } else {
476 info->fItalicAngle = 0;
477 }
478
479 info->fAscent = face->ascender;
480 info->fDescent = face->descender;
481
482 // Figure out a good guess for StemV - Min width of i, I, !, 1.
483 // This probably isn't very good with an italic font.
484 int16_t min_width = SHRT_MAX;
vandebo@chromium.org6f72d1e2011-02-14 23:19:59 +0000485 info->fStemV = 0;
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000486 char stem_chars[] = {'i', 'I', '!', '1'};
487 for (size_t i = 0; i < SK_ARRAY_COUNT(stem_chars); i++) {
488 FT_BBox bbox;
489 if (GetLetterCBox(face, stem_chars[i], &bbox)) {
490 int16_t width = bbox.xMax - bbox.xMin;
491 if (width > 0 && width < min_width) {
492 min_width = width;
493 info->fStemV = min_width;
494 }
495 }
496 }
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000497
498 TT_PCLT* pclt_info;
499 TT_OS2* os2_table;
500 if ((pclt_info = (TT_PCLT*)FT_Get_Sfnt_Table(face, ft_sfnt_pclt)) != NULL) {
501 info->fCapHeight = pclt_info->CapHeight;
502 uint8_t serif_style = pclt_info->SerifStyle & 0x3F;
503 if (serif_style >= 2 && serif_style <= 6)
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000504 info->fStyle |= SkAdvancedTypefaceMetrics::kSerif_Style;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000505 else if (serif_style >= 9 && serif_style <= 12)
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000506 info->fStyle |= SkAdvancedTypefaceMetrics::kScript_Style;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000507 } else if ((os2_table =
508 (TT_OS2*)FT_Get_Sfnt_Table(face, ft_sfnt_os2)) != NULL) {
509 info->fCapHeight = os2_table->sCapHeight;
510 } else {
511 // Figure out a good guess for CapHeight: average the height of M and X.
512 FT_BBox m_bbox, x_bbox;
513 bool got_m, got_x;
514 got_m = GetLetterCBox(face, 'M', &m_bbox);
515 got_x = GetLetterCBox(face, 'X', &x_bbox);
516 if (got_m && got_x) {
517 info->fCapHeight = (m_bbox.yMax - m_bbox.yMin + x_bbox.yMax -
518 x_bbox.yMin) / 2;
519 } else if (got_m && !got_x) {
520 info->fCapHeight = m_bbox.yMax - m_bbox.yMin;
521 } else if (!got_m && got_x) {
522 info->fCapHeight = x_bbox.yMax - x_bbox.yMin;
523 }
524 }
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000525
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000526 info->fBBox = SkIRect::MakeLTRB(face->bbox.xMin, face->bbox.yMax,
527 face->bbox.xMax, face->bbox.yMin);
528
vandebo@chromium.org37ad8fb2011-08-18 02:38:50 +0000529 if (!canEmbed(face) || !FT_IS_SCALABLE(face) ||
vandebo@chromium.org325cb9a2011-03-30 18:36:29 +0000530 info->fType == SkAdvancedTypefaceMetrics::kOther_Font) {
531 perGlyphInfo = SkAdvancedTypefaceMetrics::kNo_PerGlyphInfo;
532 }
533
534 if (perGlyphInfo & SkAdvancedTypefaceMetrics::kHAdvance_PerGlyphInfo) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000535 if (FT_IS_FIXED_WIDTH(face)) {
536 appendRange(&info->fGlyphWidths, 0);
537 int16_t advance = face->max_advance_width;
538 info->fGlyphWidths->fAdvance.append(1, &advance);
539 finishRange(info->fGlyphWidths.get(), 0,
540 SkAdvancedTypefaceMetrics::WidthRange::kDefault);
vandebo@chromium.org6f72d1e2011-02-14 23:19:59 +0000541 } else if (!cid) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000542 appendRange(&info->fGlyphWidths, 0);
543 // So as to not blow out the stack, get advances in batches.
544 for (int gID = 0; gID < face->num_glyphs; gID += 128) {
545 FT_Fixed advances[128];
546 int advanceCount = 128;
547 if (gID + advanceCount > face->num_glyphs)
548 advanceCount = face->num_glyphs - gID + 1;
549 getAdvances(face, gID, advanceCount, FT_LOAD_NO_SCALE,
550 advances);
551 for (int i = 0; i < advanceCount; i++) {
552 int16_t advance = advances[gID + i];
553 info->fGlyphWidths->fAdvance.append(1, &advance);
554 }
555 }
556 finishRange(info->fGlyphWidths.get(), face->num_glyphs - 1,
557 SkAdvancedTypefaceMetrics::WidthRange::kRange);
558 } else {
vandebo@chromium.org6f72d1e2011-02-14 23:19:59 +0000559 info->fGlyphWidths.reset(
vandebo@chromium.org37ad8fb2011-08-18 02:38:50 +0000560 getAdvanceData(face,
561 face->num_glyphs,
562 glyphIDs,
563 glyphIDsCount,
564 &getWidthAdvance));
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000565 }
vandebo@chromium.org325cb9a2011-03-30 18:36:29 +0000566 }
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000567
vandebo@chromium.org325cb9a2011-03-30 18:36:29 +0000568 if (perGlyphInfo & SkAdvancedTypefaceMetrics::kVAdvance_PerGlyphInfo &&
569 FT_HAS_VERTICAL(face)) {
570 SkASSERT(false); // Not implemented yet.
571 }
572
573 if (perGlyphInfo & SkAdvancedTypefaceMetrics::kGlyphNames_PerGlyphInfo &&
574 info->fType == SkAdvancedTypefaceMetrics::kType1_Font) {
575 // Postscript fonts may contain more than 255 glyphs, so we end up
576 // using multiple font descriptions with a glyph ordering. Record
577 // the name of each glyph.
578 info->fGlyphNames.reset(
579 new SkAutoTArray<SkString>(face->num_glyphs));
580 for (int gID = 0; gID < face->num_glyphs; gID++) {
581 char glyphName[128]; // PS limit for names is 127 bytes.
582 FT_Get_Glyph_Name(face, gID, glyphName, 128);
583 info->fGlyphNames->get()[gID].set(glyphName);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000584 }
585 }
586
vandebo@chromium.org6744d492011-05-09 18:13:47 +0000587 if (perGlyphInfo & SkAdvancedTypefaceMetrics::kToUnicode_PerGlyphInfo &&
588 info->fType != SkAdvancedTypefaceMetrics::kType1_Font &&
589 face->num_charmaps) {
590 populate_glyph_to_unicode(face, &(info->fGlyphToUnicode));
591 }
592
vandebo@chromium.orgc3a2ae52011-02-03 21:48:23 +0000593 if (!canEmbed(face))
594 info->fType = SkAdvancedTypefaceMetrics::kNotEmbeddable_Font;
595
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000596 unref_ft_face(face);
597 return info;
reed@google.com8a5d6922011-03-14 15:08:03 +0000598#endif
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000599}
vandebo@chromium.org37ad8fb2011-08-18 02:38:50 +0000600
reed@google.com618ef5e2011-01-26 22:10:41 +0000601///////////////////////////////////////////////////////////////////////////
602
reed@google.com8ed436c2011-07-21 14:12:36 +0000603static bool bothZero(SkScalar a, SkScalar b) {
604 return 0 == a && 0 == b;
605}
606
607// returns false if there is any non-90-rotation or skew
608static bool isAxisAligned(const SkScalerContext::Rec& rec) {
609 return 0 == rec.fPreSkewX &&
610 (bothZero(rec.fPost2x2[0][1], rec.fPost2x2[1][0]) ||
611 bothZero(rec.fPost2x2[0][0], rec.fPost2x2[1][1]));
612}
613
reed@google.com618ef5e2011-01-26 22:10:41 +0000614void SkFontHost::FilterRec(SkScalerContext::Rec* rec) {
615 if (!gLCDSupportValid) {
616 InitFreetype();
617 FT_Done_FreeType(gFTLibrary);
618 }
reed@google.com5b31b0f2011-02-23 14:41:42 +0000619
reed@google.comeffc5012011-06-27 16:44:46 +0000620 if (!gLCDSupport && isLCD(*rec)) {
reed@google.com618ef5e2011-01-26 22:10:41 +0000621 // If the runtime Freetype library doesn't support LCD mode, we disable
622 // it here.
623 rec->fMaskFormat = SkMask::kA8_Format;
624 }
reed@google.com5b31b0f2011-02-23 14:41:42 +0000625
reed@google.com618ef5e2011-01-26 22:10:41 +0000626 SkPaint::Hinting h = rec->getHinting();
reed@google.comeffc5012011-06-27 16:44:46 +0000627 if (SkPaint::kFull_Hinting == h && !isLCD(*rec)) {
reed@google.com618ef5e2011-01-26 22:10:41 +0000628 // collapse full->normal hinting if we're not doing LCD
629 h = SkPaint::kNormal_Hinting;
630 } else if ((rec->fFlags & SkScalerContext::kSubpixelPositioning_Flag) &&
631 SkPaint::kNo_Hinting != h) {
632 // to do subpixel, we must have at most slight hinting
633 h = SkPaint::kSlight_Hinting;
634 }
vandebo@chromium.org37ad8fb2011-08-18 02:38:50 +0000635#ifndef SK_IGNORE_ROTATED_FREETYPE_FIX
reed@google.com8ed436c2011-07-21 14:12:36 +0000636 // rotated text looks bad with hinting, so we disable it as needed
637 if (!isAxisAligned(*rec)) {
638 h = SkPaint::kNo_Hinting;
639 }
bsalomon@google.com0e35ca82011-07-22 17:56:19 +0000640#endif
reed@google.com618ef5e2011-01-26 22:10:41 +0000641 rec->setHinting(h);
642}
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000643
djsollen@google.comcd9d69b2011-03-14 20:30:14 +0000644#ifdef ANDROID
645uint32_t SkFontHost::GetUnitsPerEm(SkFontID fontID) {
646 SkAutoMutexAcquire ac(gFTMutex);
647 SkFaceRec *rec = ref_ft_face(fontID);
648 uint16_t unitsPerEm = 0;
649
650 if (rec != NULL && rec->fFace != NULL) {
651 unitsPerEm = rec->fFace->units_per_EM;
652 unref_ft_face(rec->fFace);
653 }
654
655 return (uint32_t)unitsPerEm;
656}
657#endif
658
reed@android.com8a1c16f2008-12-17 15:59:43 +0000659SkScalerContext_FreeType::SkScalerContext_FreeType(const SkDescriptor* desc)
reed@android.com62900b42009-02-11 15:07:19 +0000660 : SkScalerContext(desc) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000661 SkAutoMutexAcquire ac(gFTMutex);
662
reed@android.com8a1c16f2008-12-17 15:59:43 +0000663 if (gFTCount == 0) {
reed@android.com659aaf92009-07-23 15:20:21 +0000664 if (!InitFreetype()) {
665 sk_throw();
666 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000667 }
668 ++gFTCount;
669
670 // load the font file
reed@android.com62900b42009-02-11 15:07:19 +0000671 fFTSize = NULL;
672 fFace = NULL;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000673 fFaceRec = ref_ft_face(fRec.fFontID);
reed@android.com62900b42009-02-11 15:07:19 +0000674 if (NULL == fFaceRec) {
675 return;
676 }
677 fFace = fFaceRec->fFace;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000678
679 // compute our factors from the record
680
681 SkMatrix m;
682
683 fRec.getSingleMatrix(&m);
684
685#ifdef DUMP_STRIKE_CREATION
686 SkString keyString;
687 SkFontHost::GetDescriptorKeyString(desc, &keyString);
688 printf("========== strike [%g %g %g] [%g %g %g %g] hints %d format %d %s\n", SkScalarToFloat(fRec.fTextSize),
689 SkScalarToFloat(fRec.fPreScaleX), SkScalarToFloat(fRec.fPreSkewX),
690 SkScalarToFloat(fRec.fPost2x2[0][0]), SkScalarToFloat(fRec.fPost2x2[0][1]),
691 SkScalarToFloat(fRec.fPost2x2[1][0]), SkScalarToFloat(fRec.fPost2x2[1][1]),
agl@chromium.org309485b2009-07-21 17:41:32 +0000692 fRec.getHinting(), fRec.fMaskFormat, keyString.c_str());
reed@android.com8a1c16f2008-12-17 15:59:43 +0000693#endif
694
695 // now compute our scale factors
696 SkScalar sx = m.getScaleX();
697 SkScalar sy = m.getScaleY();
698
699 if (m.getSkewX() || m.getSkewY() || sx < 0 || sy < 0) {
700 // sort of give up on hinting
701 sx = SkMaxScalar(SkScalarAbs(sx), SkScalarAbs(m.getSkewX()));
702 sy = SkMaxScalar(SkScalarAbs(m.getSkewY()), SkScalarAbs(sy));
703 sx = sy = SkScalarAve(sx, sy);
704
705 SkScalar inv = SkScalarInvert(sx);
706
707 // flip the skew elements to go from our Y-down system to FreeType's
708 fMatrix22.xx = SkScalarToFixed(SkScalarMul(m.getScaleX(), inv));
709 fMatrix22.xy = -SkScalarToFixed(SkScalarMul(m.getSkewX(), inv));
710 fMatrix22.yx = -SkScalarToFixed(SkScalarMul(m.getSkewY(), inv));
711 fMatrix22.yy = SkScalarToFixed(SkScalarMul(m.getScaleY(), inv));
712 } else {
713 fMatrix22.xx = fMatrix22.yy = SK_Fixed1;
714 fMatrix22.xy = fMatrix22.yx = 0;
715 }
716
717 fScaleX = SkScalarToFixed(sx);
718 fScaleY = SkScalarToFixed(sy);
719
720 // compute the flags we send to Load_Glyph
721 {
reed@android.come4d0bc02009-07-24 19:53:20 +0000722 FT_Int32 loadFlags = FT_LOAD_DEFAULT;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000723
agl@chromium.org70a303f2010-05-10 14:15:50 +0000724 if (SkMask::kBW_Format == fRec.fMaskFormat) {
725 // See http://code.google.com/p/chromium/issues/detail?id=43252#c24
726 loadFlags = FT_LOAD_TARGET_MONO;
reed@google.comeffc5012011-06-27 16:44:46 +0000727 if (fRec.getHinting() == SkPaint::kNo_Hinting) {
agl@chromium.org70a303f2010-05-10 14:15:50 +0000728 loadFlags = FT_LOAD_NO_HINTING;
reed@google.comeffc5012011-06-27 16:44:46 +0000729 }
agl@chromium.org70a303f2010-05-10 14:15:50 +0000730 } else {
731 switch (fRec.getHinting()) {
732 case SkPaint::kNo_Hinting:
733 loadFlags = FT_LOAD_NO_HINTING;
734 break;
735 case SkPaint::kSlight_Hinting:
736 loadFlags = FT_LOAD_TARGET_LIGHT; // This implies FORCE_AUTOHINT
737 break;
738 case SkPaint::kNormal_Hinting:
agl@chromium.orga2c71cb2010-06-17 20:49:17 +0000739 if (fRec.fFlags & SkScalerContext::kAutohinting_Flag)
740 loadFlags = FT_LOAD_FORCE_AUTOHINT;
741 else
742 loadFlags = FT_LOAD_NO_AUTOHINT;
agl@chromium.org70a303f2010-05-10 14:15:50 +0000743 break;
744 case SkPaint::kFull_Hinting:
agl@chromium.orga2c71cb2010-06-17 20:49:17 +0000745 if (fRec.fFlags & SkScalerContext::kAutohinting_Flag) {
746 loadFlags = FT_LOAD_FORCE_AUTOHINT;
747 break;
748 }
agl@chromium.org70a303f2010-05-10 14:15:50 +0000749 loadFlags = FT_LOAD_TARGET_NORMAL;
reed@google.comeffc5012011-06-27 16:44:46 +0000750 if (isLCD(fRec)) {
751 if (fRec.fFlags & SkScalerContext::kLCD_Vertical_Flag) {
752 loadFlags = FT_LOAD_TARGET_LCD_V;
753 } else {
754 loadFlags = FT_LOAD_TARGET_LCD;
755 }
reed@google.comea2333d2011-03-14 16:44:56 +0000756 }
agl@chromium.org70a303f2010-05-10 14:15:50 +0000757 break;
758 default:
759 SkDebugf("---------- UNKNOWN hinting %d\n", fRec.getHinting());
760 break;
761 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000762 }
763
reed@google.comeffc5012011-06-27 16:44:46 +0000764 if ((fRec.fFlags & SkScalerContext::kEmbeddedBitmapText_Flag) == 0) {
agl@chromium.orge0d08992009-08-07 19:19:23 +0000765 loadFlags |= FT_LOAD_NO_BITMAP;
reed@google.comeffc5012011-06-27 16:44:46 +0000766 }
agl@chromium.orge0d08992009-08-07 19:19:23 +0000767
reed@google.com96a9f7912011-05-06 11:49:30 +0000768 // Always using FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH to get correct
769 // advances, as fontconfig and cairo do.
770 // See http://code.google.com/p/skia/issues/detail?id=222.
771 loadFlags |= FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH;
772
reed@android.come4d0bc02009-07-24 19:53:20 +0000773 fLoadGlyphFlags = loadFlags;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000774 }
775
776 // now create the FT_Size
777
778 {
779 FT_Error err;
780
781 err = FT_New_Size(fFace, &fFTSize);
782 if (err != 0) {
783 SkDEBUGF(("SkScalerContext_FreeType::FT_New_Size(%x): FT_Set_Char_Size(0x%x, 0x%x) returned 0x%x\n",
784 fFaceRec->fFontID, fScaleX, fScaleY, err));
785 fFace = NULL;
786 return;
787 }
788
789 err = FT_Activate_Size(fFTSize);
790 if (err != 0) {
791 SkDEBUGF(("SkScalerContext_FreeType::FT_Activate_Size(%x, 0x%x, 0x%x) returned 0x%x\n",
792 fFaceRec->fFontID, fScaleX, fScaleY, err));
793 fFTSize = NULL;
794 }
795
796 err = FT_Set_Char_Size( fFace,
797 SkFixedToFDot6(fScaleX), SkFixedToFDot6(fScaleY),
798 72, 72);
799 if (err != 0) {
800 SkDEBUGF(("SkScalerContext_FreeType::FT_Set_Char_Size(%x, 0x%x, 0x%x) returned 0x%x\n",
801 fFaceRec->fFontID, fScaleX, fScaleY, err));
802 fFace = NULL;
803 return;
804 }
805
806 FT_Set_Transform( fFace, &fMatrix22, NULL);
807 }
808}
809
810SkScalerContext_FreeType::~SkScalerContext_FreeType() {
811 if (fFTSize != NULL) {
812 FT_Done_Size(fFTSize);
813 }
814
815 SkAutoMutexAcquire ac(gFTMutex);
816
817 if (fFace != NULL) {
818 unref_ft_face(fFace);
819 }
820 if (--gFTCount == 0) {
821// SkDEBUGF(("FT_Done_FreeType\n"));
822 FT_Done_FreeType(gFTLibrary);
823 SkDEBUGCODE(gFTLibrary = NULL;)
824 }
825}
826
827/* We call this before each use of the fFace, since we may be sharing
828 this face with other context (at different sizes).
829*/
830FT_Error SkScalerContext_FreeType::setupSize() {
831 /* In the off-chance that a font has been removed, we want to error out
832 right away, so call resolve just to be sure.
833
834 TODO: perhaps we can skip this, by walking the global font cache and
835 killing all of the contexts when we know that a given fontID is going
836 away...
837 */
reed@android.comb1d9d2e2009-03-04 17:37:51 +0000838 if (!SkFontHost::ValidFontID(fRec.fFontID)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000839 return (FT_Error)-1;
840 }
841
842 FT_Error err = FT_Activate_Size(fFTSize);
843
844 if (err != 0) {
845 SkDEBUGF(("SkScalerContext_FreeType::FT_Activate_Size(%x, 0x%x, 0x%x) returned 0x%x\n",
846 fFaceRec->fFontID, fScaleX, fScaleY, err));
847 fFTSize = NULL;
848 } else {
849 // seems we need to reset this every time (not sure why, but without it
850 // I get random italics from some other fFTSize)
851 FT_Set_Transform( fFace, &fMatrix22, NULL);
852 }
853 return err;
854}
855
senorblanco@chromium.org4526a842010-02-05 23:08:20 +0000856void SkScalerContext_FreeType::emboldenOutline(FT_Outline* outline) {
857 FT_Pos strength;
858 strength = FT_MulFix(fFace->units_per_EM, fFace->size->metrics.y_scale)
859 / 24;
860 FT_Outline_Embolden(outline, strength);
861}
862
ctguil@chromium.org0bc7bf52011-03-04 19:04:57 +0000863unsigned SkScalerContext_FreeType::generateGlyphCount() {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000864 return fFace->num_glyphs;
865}
866
867uint16_t SkScalerContext_FreeType::generateCharToGlyph(SkUnichar uni) {
868 return SkToU16(FT_Get_Char_Index( fFace, uni ));
869}
870
reed@android.com9d3a9852010-01-08 14:07:42 +0000871SkUnichar SkScalerContext_FreeType::generateGlyphToChar(uint16_t glyph) {
872 // iterate through each cmap entry, looking for matching glyph indices
873 FT_UInt glyphIndex;
874 SkUnichar charCode = FT_Get_First_Char( fFace, &glyphIndex );
875
876 while (glyphIndex != 0) {
877 if (glyphIndex == glyph) {
878 return charCode;
879 }
880 charCode = FT_Get_Next_Char( fFace, charCode, &glyphIndex );
881 }
882
883 return 0;
884}
885
reed@android.com8a1c16f2008-12-17 15:59:43 +0000886static FT_Pixel_Mode compute_pixel_mode(SkMask::Format format) {
887 switch (format) {
888 case SkMask::kBW_Format:
889 return FT_PIXEL_MODE_MONO;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000890 case SkMask::kA8_Format:
891 default:
892 return FT_PIXEL_MODE_GRAY;
893 }
894}
895
reed@android.com8a1c16f2008-12-17 15:59:43 +0000896void SkScalerContext_FreeType::generateAdvance(SkGlyph* glyph) {
897#ifdef FT_ADVANCES_H
898 /* unhinted and light hinted text have linearly scaled advances
899 * which are very cheap to compute with some font formats...
900 */
901 {
902 SkAutoMutexAcquire ac(gFTMutex);
903
904 if (this->setupSize()) {
reed@android.com62900b42009-02-11 15:07:19 +0000905 glyph->zeroMetrics();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000906 return;
907 }
908
909 FT_Error error;
910 FT_Fixed advance;
911
912 error = FT_Get_Advance( fFace, glyph->getGlyphID(fBaseGlyphCount),
913 fLoadGlyphFlags | FT_ADVANCE_FLAG_FAST_ONLY,
914 &advance );
915 if (0 == error) {
916 glyph->fRsbDelta = 0;
917 glyph->fLsbDelta = 0;
918 glyph->fAdvanceX = advance; // advance *2/3; //DEBUG
919 glyph->fAdvanceY = 0;
920 return;
921 }
922 }
923#endif /* FT_ADVANCES_H */
924 /* otherwise, we need to load/hint the glyph, which is slower */
925 this->generateMetrics(glyph);
926 return;
927}
928
929void SkScalerContext_FreeType::generateMetrics(SkGlyph* glyph) {
930 SkAutoMutexAcquire ac(gFTMutex);
931
932 glyph->fRsbDelta = 0;
933 glyph->fLsbDelta = 0;
934
935 FT_Error err;
936
937 if (this->setupSize()) {
938 goto ERROR;
939 }
940
941 err = FT_Load_Glyph( fFace, glyph->getGlyphID(fBaseGlyphCount), fLoadGlyphFlags );
942 if (err != 0) {
943 SkDEBUGF(("SkScalerContext_FreeType::generateMetrics(%x): FT_Load_Glyph(glyph:%d flags:%d) returned 0x%x\n",
944 fFaceRec->fFontID, glyph->getGlyphID(fBaseGlyphCount), fLoadGlyphFlags, err));
945 ERROR:
reed@android.com62900b42009-02-11 15:07:19 +0000946 glyph->zeroMetrics();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000947 return;
948 }
949
950 switch ( fFace->glyph->format ) {
thakis@chromium.org598b8592011-05-24 05:42:55 +0000951 case FT_GLYPH_FORMAT_OUTLINE: {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000952 FT_BBox bbox;
953
bungeman@google.com0f0c2882011-11-04 15:47:41 +0000954 if (0 == fFace->glyph->outline.n_contours) {
955 glyph->fWidth = 0;
956 glyph->fHeight = 0;
957 glyph->fTop = 0;
958 glyph->fLeft = 0;
959 break;
960 }
961
senorblanco@chromium.org4526a842010-02-05 23:08:20 +0000962 if (fRec.fFlags & kEmbolden_Flag) {
963 emboldenOutline(&fFace->glyph->outline);
964 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000965 FT_Outline_Get_CBox(&fFace->glyph->outline, &bbox);
966
agl@chromium.orga2c71cb2010-06-17 20:49:17 +0000967 if (fRec.fFlags & SkScalerContext::kSubpixelPositioning_Flag) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000968 int dx = glyph->getSubXFixed() >> 10;
969 int dy = glyph->getSubYFixed() >> 10;
970 // negate dy since freetype-y-goes-up and skia-y-goes-down
971 bbox.xMin += dx;
972 bbox.yMin -= dy;
973 bbox.xMax += dx;
974 bbox.yMax -= dy;
975 }
976
977 bbox.xMin &= ~63;
978 bbox.yMin &= ~63;
979 bbox.xMax = (bbox.xMax + 63) & ~63;
980 bbox.yMax = (bbox.yMax + 63) & ~63;
981
982 glyph->fWidth = SkToU16((bbox.xMax - bbox.xMin) >> 6);
983 glyph->fHeight = SkToU16((bbox.yMax - bbox.yMin) >> 6);
984 glyph->fTop = -SkToS16(bbox.yMax >> 6);
985 glyph->fLeft = SkToS16(bbox.xMin >> 6);
986 break;
thakis@chromium.org598b8592011-05-24 05:42:55 +0000987 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000988
989 case FT_GLYPH_FORMAT_BITMAP:
agl@chromium.orge76073b2010-06-04 20:31:17 +0000990 if (fRec.fFlags & kEmbolden_Flag) {
991 FT_GlyphSlot_Own_Bitmap(fFace->glyph);
992 FT_Bitmap_Embolden(gFTLibrary, &fFace->glyph->bitmap, kBitmapEmboldenStrength, 0);
993 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000994 glyph->fWidth = SkToU16(fFace->glyph->bitmap.width);
995 glyph->fHeight = SkToU16(fFace->glyph->bitmap.rows);
996 glyph->fTop = -SkToS16(fFace->glyph->bitmap_top);
997 glyph->fLeft = SkToS16(fFace->glyph->bitmap_left);
998 break;
999
1000 default:
1001 SkASSERT(!"unknown glyph format");
1002 goto ERROR;
1003 }
1004
agl@chromium.orga2c71cb2010-06-17 20:49:17 +00001005 if ((fRec.fFlags & SkScalerContext::kSubpixelPositioning_Flag) == 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001006 glyph->fAdvanceX = SkFDot6ToFixed(fFace->glyph->advance.x);
1007 glyph->fAdvanceY = -SkFDot6ToFixed(fFace->glyph->advance.y);
1008 if (fRec.fFlags & kDevKernText_Flag) {
1009 glyph->fRsbDelta = SkToS8(fFace->glyph->rsb_delta);
1010 glyph->fLsbDelta = SkToS8(fFace->glyph->lsb_delta);
1011 }
1012 } else {
1013 glyph->fAdvanceX = SkFixedMul(fMatrix22.xx, fFace->glyph->linearHoriAdvance);
1014 glyph->fAdvanceY = -SkFixedMul(fMatrix22.yx, fFace->glyph->linearHoriAdvance);
1015 }
1016
1017#ifdef ENABLE_GLYPH_SPEW
1018 SkDEBUGF(("FT_Set_Char_Size(this:%p sx:%x sy:%x ", this, fScaleX, fScaleY));
1019 SkDEBUGF(("Metrics(glyph:%d flags:0x%x) w:%d\n", glyph->getGlyphID(fBaseGlyphCount), fLoadGlyphFlags, glyph->fWidth));
1020#endif
1021}
1022
reed@google.combde3c8e2011-05-18 11:58:10 +00001023static int lerp(int start, int end) {
1024 SkASSERT((unsigned)SK_FREETYPE_LCD_LERP <= 256);
1025 return start + ((end - start) * (SK_FREETYPE_LCD_LERP) >> 8);
reed@google.comc5181342011-05-17 20:52:46 +00001026}
1027
1028static uint16_t packTriple(unsigned r, unsigned g, unsigned b) {
1029 if (SK_FREETYPE_LCD_LERP) {
reed@google.combde3c8e2011-05-18 11:58:10 +00001030 // want (a+b+c)/3, but we approx to avoid the divide
1031 unsigned ave = (5 * (r + g + b) + b) >> 4;
1032 r = lerp(r, ave);
1033 g = lerp(g, ave);
1034 b = lerp(b, ave);
reed@google.comc5181342011-05-17 20:52:46 +00001035 }
1036 return SkPackRGB16(r >> 3, g >> 2, b >> 3);
1037}
1038
reed@google.com73824072011-06-23 13:17:30 +00001039static uint16_t grayToRGB16(U8CPU gray) {
1040 SkASSERT(gray <= 255);
1041 return SkPackRGB16(gray >> 3, gray >> 2, gray >> 3);
1042}
1043
1044static int bittst(const uint8_t data[], int bitOffset) {
1045 SkASSERT(bitOffset >= 0);
reed@google.comc8e0f932011-06-23 19:39:49 +00001046 int lowBit = data[bitOffset >> 3] >> (~bitOffset & 7);
reed@google.com73824072011-06-23 13:17:30 +00001047 return lowBit & 1;
1048}
1049
reed@google.comeffc5012011-06-27 16:44:46 +00001050static void copyFT2LCD16(const SkGlyph& glyph, const FT_Bitmap& bitmap,
1051 int lcdIsBGR) {
reed@google.comea2333d2011-03-14 16:44:56 +00001052 SkASSERT(glyph.fHeight == bitmap.rows);
reed@google.comea2333d2011-03-14 16:44:56 +00001053 uint16_t* dst = reinterpret_cast<uint16_t*>(glyph.fImage);
reed@google.com73824072011-06-23 13:17:30 +00001054 const size_t dstRB = glyph.rowBytes();
1055 const int width = glyph.fWidth;
1056 const uint8_t* src = bitmap.buffer;
reed@google.comea2333d2011-03-14 16:44:56 +00001057
reed@google.com73824072011-06-23 13:17:30 +00001058 switch (bitmap.pixel_mode) {
1059 case FT_PIXEL_MODE_MONO: {
1060 for (int y = 0; y < glyph.fHeight; ++y) {
1061 for (int x = 0; x < width; ++x) {
1062 dst[x] = -bittst(src, x);
1063 }
1064 dst = (uint16_t*)((char*)dst + dstRB);
1065 src += bitmap.pitch;
1066 }
1067 } break;
1068 case FT_PIXEL_MODE_GRAY: {
1069 for (int y = 0; y < glyph.fHeight; ++y) {
1070 for (int x = 0; x < width; ++x) {
1071 dst[x] = grayToRGB16(src[x]);
1072 }
1073 dst = (uint16_t*)((char*)dst + dstRB);
1074 src += bitmap.pitch;
1075 }
1076 } break;
1077 default: {
1078 SkASSERT(glyph.fWidth * 3 == bitmap.width - 6);
1079 src += 3;
1080 for (int y = 0; y < glyph.fHeight; y++) {
1081 const uint8_t* triple = src;
reed@google.comeffc5012011-06-27 16:44:46 +00001082 if (lcdIsBGR) {
1083 for (int x = 0; x < width; x++) {
1084 dst[x] = packTriple(triple[2], triple[1], triple[0]);
1085 triple += 3;
1086 }
1087 } else {
1088 for (int x = 0; x < width; x++) {
1089 dst[x] = packTriple(triple[0], triple[1], triple[2]);
1090 triple += 3;
1091 }
reed@google.com73824072011-06-23 13:17:30 +00001092 }
1093 src += bitmap.pitch;
1094 dst = (uint16_t*)((char*)dst + dstRB);
1095 }
1096 } break;
reed@google.comea2333d2011-03-14 16:44:56 +00001097 }
1098}
1099
reed@android.com8a1c16f2008-12-17 15:59:43 +00001100void SkScalerContext_FreeType::generateImage(const SkGlyph& glyph) {
1101 SkAutoMutexAcquire ac(gFTMutex);
1102
1103 FT_Error err;
1104
1105 if (this->setupSize()) {
1106 goto ERROR;
1107 }
1108
1109 err = FT_Load_Glyph( fFace, glyph.getGlyphID(fBaseGlyphCount), fLoadGlyphFlags);
1110 if (err != 0) {
1111 SkDEBUGF(("SkScalerContext_FreeType::generateImage: FT_Load_Glyph(glyph:%d width:%d height:%d rb:%d flags:%d) returned 0x%x\n",
1112 glyph.getGlyphID(fBaseGlyphCount), glyph.fWidth, glyph.fHeight, glyph.rowBytes(), fLoadGlyphFlags, err));
1113 ERROR:
1114 memset(glyph.fImage, 0, glyph.rowBytes() * glyph.fHeight);
1115 return;
1116 }
1117
1118 switch ( fFace->glyph->format ) {
1119 case FT_GLYPH_FORMAT_OUTLINE: {
1120 FT_Outline* outline = &fFace->glyph->outline;
1121 FT_BBox bbox;
1122 FT_Bitmap target;
1123
senorblanco@chromium.org4526a842010-02-05 23:08:20 +00001124 if (fRec.fFlags & kEmbolden_Flag) {
1125 emboldenOutline(outline);
1126 }
1127
reed@android.com8a1c16f2008-12-17 15:59:43 +00001128 int dx = 0, dy = 0;
agl@chromium.orga2c71cb2010-06-17 20:49:17 +00001129 if (fRec.fFlags & SkScalerContext::kSubpixelPositioning_Flag) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001130 dx = glyph.getSubXFixed() >> 10;
1131 dy = glyph.getSubYFixed() >> 10;
1132 // negate dy since freetype-y-goes-up and skia-y-goes-down
1133 dy = -dy;
1134 }
1135 FT_Outline_Get_CBox(outline, &bbox);
1136 /*
1137 what we really want to do for subpixel is
1138 offset(dx, dy)
1139 compute_bounds
1140 offset(bbox & !63)
1141 but that is two calls to offset, so we do the following, which
1142 achieves the same thing with only one offset call.
1143 */
1144 FT_Outline_Translate(outline, dx - ((bbox.xMin + dx) & ~63),
1145 dy - ((bbox.yMin + dy) & ~63));
1146
reed@google.comea2333d2011-03-14 16:44:56 +00001147 if (SkMask::kLCD16_Format == glyph.fMaskFormat) {
1148 FT_Render_Glyph(fFace->glyph, FT_RENDER_MODE_LCD);
reed@google.comeffc5012011-06-27 16:44:46 +00001149 copyFT2LCD16(glyph, fFace->glyph->bitmap,
1150 fRec.fFlags & SkScalerContext::kLCD_BGROrder_Flag);
reed@google.comea2333d2011-03-14 16:44:56 +00001151 } else {
1152 target.width = glyph.fWidth;
1153 target.rows = glyph.fHeight;
1154 target.pitch = glyph.rowBytes();
1155 target.buffer = reinterpret_cast<uint8_t*>(glyph.fImage);
1156 target.pixel_mode = compute_pixel_mode(
1157 (SkMask::Format)fRec.fMaskFormat);
1158 target.num_grays = 256;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001159
reed@google.comea2333d2011-03-14 16:44:56 +00001160 memset(glyph.fImage, 0, glyph.rowBytes() * glyph.fHeight);
1161 FT_Outline_Get_Bitmap(gFTLibrary, outline, &target);
1162 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001163 } break;
1164
1165 case FT_GLYPH_FORMAT_BITMAP: {
agl@chromium.orge76073b2010-06-04 20:31:17 +00001166 if (fRec.fFlags & kEmbolden_Flag) {
1167 FT_GlyphSlot_Own_Bitmap(fFace->glyph);
1168 FT_Bitmap_Embolden(gFTLibrary, &fFace->glyph->bitmap, kBitmapEmboldenStrength, 0);
1169 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001170 SkASSERT_CONTINUE(glyph.fWidth == fFace->glyph->bitmap.width);
1171 SkASSERT_CONTINUE(glyph.fHeight == fFace->glyph->bitmap.rows);
1172 SkASSERT_CONTINUE(glyph.fTop == -fFace->glyph->bitmap_top);
1173 SkASSERT_CONTINUE(glyph.fLeft == fFace->glyph->bitmap_left);
1174
1175 const uint8_t* src = (const uint8_t*)fFace->glyph->bitmap.buffer;
1176 uint8_t* dst = (uint8_t*)glyph.fImage;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001177
agl@chromium.org558434a2009-08-11 17:22:38 +00001178 if (fFace->glyph->bitmap.pixel_mode == FT_PIXEL_MODE_GRAY ||
1179 (fFace->glyph->bitmap.pixel_mode == FT_PIXEL_MODE_MONO &&
1180 glyph.fMaskFormat == SkMask::kBW_Format)) {
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001181 unsigned srcRowBytes = fFace->glyph->bitmap.pitch;
1182 unsigned dstRowBytes = glyph.rowBytes();
1183 unsigned minRowBytes = SkMin32(srcRowBytes, dstRowBytes);
1184 unsigned extraRowBytes = dstRowBytes - minRowBytes;
1185
1186 for (int y = fFace->glyph->bitmap.rows - 1; y >= 0; --y) {
1187 memcpy(dst, src, minRowBytes);
1188 memset(dst + minRowBytes, 0, extraRowBytes);
1189 src += srcRowBytes;
1190 dst += dstRowBytes;
1191 }
agl@chromium.org558434a2009-08-11 17:22:38 +00001192 } else if (fFace->glyph->bitmap.pixel_mode == FT_PIXEL_MODE_MONO &&
reed@google.comeffc5012011-06-27 16:44:46 +00001193 glyph.fMaskFormat == SkMask::kA8_Format) {
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001194 for (int y = 0; y < fFace->glyph->bitmap.rows; ++y) {
1195 uint8_t byte = 0;
1196 int bits = 0;
1197 const uint8_t* src_row = src;
1198 uint8_t* dst_row = dst;
1199
1200 for (int x = 0; x < fFace->glyph->bitmap.width; ++x) {
1201 if (!bits) {
1202 byte = *src_row++;
1203 bits = 8;
1204 }
1205
1206 *dst_row++ = byte & 0x80 ? 0xff : 0;
1207 bits--;
1208 byte <<= 1;
1209 }
1210
1211 src += fFace->glyph->bitmap.pitch;
1212 dst += glyph.rowBytes();
1213 }
reed@google.com73824072011-06-23 13:17:30 +00001214 } else if (SkMask::kLCD16_Format == glyph.fMaskFormat) {
reed@google.comeffc5012011-06-27 16:44:46 +00001215 copyFT2LCD16(glyph, fFace->glyph->bitmap,
1216 fRec.fFlags & SkScalerContext::kLCD_BGROrder_Flag);
agl@chromium.org558434a2009-08-11 17:22:38 +00001217 } else {
reed@google.com73824072011-06-23 13:17:30 +00001218 SkASSERT(!"unknown glyph bitmap transform needed");
reed@android.com8a1c16f2008-12-17 15:59:43 +00001219 }
1220 } break;
1221
1222 default:
1223 SkASSERT(!"unknown glyph format");
1224 goto ERROR;
1225 }
1226}
1227
1228///////////////////////////////////////////////////////////////////////////////
1229
1230#define ft2sk(x) SkFixedToScalar((x) << 10)
1231
reed@android.com6f252972009-01-14 16:46:16 +00001232#if FREETYPE_MAJOR >= 2 && FREETYPE_MINOR >= 2
reed@android.com8a1c16f2008-12-17 15:59:43 +00001233 #define CONST_PARAM const
1234#else // older freetype doesn't use const here
1235 #define CONST_PARAM
1236#endif
1237
1238static int move_proc(CONST_PARAM FT_Vector* pt, void* ctx) {
1239 SkPath* path = (SkPath*)ctx;
1240 path->close(); // to close the previous contour (if any)
1241 path->moveTo(ft2sk(pt->x), -ft2sk(pt->y));
1242 return 0;
1243}
1244
1245static int line_proc(CONST_PARAM FT_Vector* pt, void* ctx) {
1246 SkPath* path = (SkPath*)ctx;
1247 path->lineTo(ft2sk(pt->x), -ft2sk(pt->y));
1248 return 0;
1249}
1250
1251static int quad_proc(CONST_PARAM FT_Vector* pt0, CONST_PARAM FT_Vector* pt1,
1252 void* ctx) {
1253 SkPath* path = (SkPath*)ctx;
1254 path->quadTo(ft2sk(pt0->x), -ft2sk(pt0->y), ft2sk(pt1->x), -ft2sk(pt1->y));
1255 return 0;
1256}
1257
1258static int cubic_proc(CONST_PARAM FT_Vector* pt0, CONST_PARAM FT_Vector* pt1,
1259 CONST_PARAM FT_Vector* pt2, void* ctx) {
1260 SkPath* path = (SkPath*)ctx;
1261 path->cubicTo(ft2sk(pt0->x), -ft2sk(pt0->y), ft2sk(pt1->x),
1262 -ft2sk(pt1->y), ft2sk(pt2->x), -ft2sk(pt2->y));
1263 return 0;
1264}
1265
1266void SkScalerContext_FreeType::generatePath(const SkGlyph& glyph,
1267 SkPath* path) {
1268 SkAutoMutexAcquire ac(gFTMutex);
1269
1270 SkASSERT(&glyph && path);
1271
1272 if (this->setupSize()) {
1273 path->reset();
1274 return;
1275 }
1276
1277 uint32_t flags = fLoadGlyphFlags;
1278 flags |= FT_LOAD_NO_BITMAP; // ignore embedded bitmaps so we're sure to get the outline
1279 flags &= ~FT_LOAD_RENDER; // don't scan convert (we just want the outline)
1280
1281 FT_Error err = FT_Load_Glyph( fFace, glyph.getGlyphID(fBaseGlyphCount), flags);
1282
1283 if (err != 0) {
1284 SkDEBUGF(("SkScalerContext_FreeType::generatePath: FT_Load_Glyph(glyph:%d flags:%d) returned 0x%x\n",
1285 glyph.getGlyphID(fBaseGlyphCount), flags, err));
1286 path->reset();
1287 return;
1288 }
1289
senorblanco@chromium.org4526a842010-02-05 23:08:20 +00001290 if (fRec.fFlags & kEmbolden_Flag) {
1291 emboldenOutline(&fFace->glyph->outline);
1292 }
1293
reed@android.com8a1c16f2008-12-17 15:59:43 +00001294 FT_Outline_Funcs funcs;
1295
1296 funcs.move_to = move_proc;
1297 funcs.line_to = line_proc;
1298 funcs.conic_to = quad_proc;
1299 funcs.cubic_to = cubic_proc;
1300 funcs.shift = 0;
1301 funcs.delta = 0;
1302
1303 err = FT_Outline_Decompose(&fFace->glyph->outline, &funcs, path);
1304
1305 if (err != 0) {
1306 SkDEBUGF(("SkScalerContext_FreeType::generatePath: FT_Load_Glyph(glyph:%d flags:%d) returned 0x%x\n",
1307 glyph.getGlyphID(fBaseGlyphCount), flags, err));
1308 path->reset();
1309 return;
1310 }
1311
1312 path->close();
1313}
1314
1315void SkScalerContext_FreeType::generateFontMetrics(SkPaint::FontMetrics* mx,
1316 SkPaint::FontMetrics* my) {
1317 if (NULL == mx && NULL == my) {
1318 return;
1319 }
1320
1321 SkAutoMutexAcquire ac(gFTMutex);
1322
1323 if (this->setupSize()) {
reed@android.coma8a8b8b2009-05-04 15:00:11 +00001324 ERROR:
reed@android.com8a1c16f2008-12-17 15:59:43 +00001325 if (mx) {
reed@android.com4516f472009-06-29 16:25:36 +00001326 sk_bzero(mx, sizeof(SkPaint::FontMetrics));
reed@android.com8a1c16f2008-12-17 15:59:43 +00001327 }
1328 if (my) {
reed@android.com4516f472009-06-29 16:25:36 +00001329 sk_bzero(my, sizeof(SkPaint::FontMetrics));
reed@android.com8a1c16f2008-12-17 15:59:43 +00001330 }
1331 return;
1332 }
1333
reed@android.coma8a8b8b2009-05-04 15:00:11 +00001334 FT_Face face = fFace;
1335 int upem = face->units_per_EM;
1336 if (upem <= 0) {
1337 goto ERROR;
1338 }
1339
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001340 SkPoint pts[6];
1341 SkFixed ys[6];
reed@android.com8a1c16f2008-12-17 15:59:43 +00001342 SkFixed scaleY = fScaleY;
1343 SkFixed mxy = fMatrix22.xy;
1344 SkFixed myy = fMatrix22.yy;
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001345 SkScalar xmin = SkIntToScalar(face->bbox.xMin) / upem;
1346 SkScalar xmax = SkIntToScalar(face->bbox.xMax) / upem;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001347
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001348 int leading = face->height - (face->ascender + -face->descender);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001349 if (leading < 0) {
1350 leading = 0;
1351 }
1352
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001353 // Try to get the OS/2 table from the font. This contains the specific
1354 // average font width metrics which Windows uses.
1355 TT_OS2* os2 = (TT_OS2*) FT_Get_Sfnt_Table(face, ft_sfnt_os2);
1356
reed@android.com8a1c16f2008-12-17 15:59:43 +00001357 ys[0] = -face->bbox.yMax;
1358 ys[1] = -face->ascender;
1359 ys[2] = -face->descender;
1360 ys[3] = -face->bbox.yMin;
1361 ys[4] = leading;
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001362 ys[5] = os2 ? os2->xAvgCharWidth : 0;
1363
1364 SkScalar x_height;
1365 if (os2 && os2->sxHeight) {
1366 x_height = SkFixedToScalar(SkMulDiv(fScaleX, os2->sxHeight, upem));
1367 } else {
1368 const FT_UInt x_glyph = FT_Get_Char_Index(fFace, 'x');
1369 if (x_glyph) {
1370 FT_BBox bbox;
1371 FT_Load_Glyph(fFace, x_glyph, fLoadGlyphFlags);
senorblanco@chromium.org4526a842010-02-05 23:08:20 +00001372 if (fRec.fFlags & kEmbolden_Flag) {
1373 emboldenOutline(&fFace->glyph->outline);
1374 }
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001375 FT_Outline_Get_CBox(&fFace->glyph->outline, &bbox);
1376 x_height = SkIntToScalar(bbox.yMax) / 64;
1377 } else {
1378 x_height = 0;
1379 }
1380 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001381
1382 // convert upem-y values into scalar points
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001383 for (int i = 0; i < 6; i++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001384 SkFixed y = SkMulDiv(scaleY, ys[i], upem);
1385 SkFixed x = SkFixedMul(mxy, y);
1386 y = SkFixedMul(myy, y);
1387 pts[i].set(SkFixedToScalar(x), SkFixedToScalar(y));
1388 }
1389
1390 if (mx) {
1391 mx->fTop = pts[0].fX;
1392 mx->fAscent = pts[1].fX;
1393 mx->fDescent = pts[2].fX;
1394 mx->fBottom = pts[3].fX;
1395 mx->fLeading = pts[4].fX;
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001396 mx->fAvgCharWidth = pts[5].fX;
1397 mx->fXMin = xmin;
1398 mx->fXMax = xmax;
1399 mx->fXHeight = x_height;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001400 }
1401 if (my) {
1402 my->fTop = pts[0].fY;
1403 my->fAscent = pts[1].fY;
1404 my->fDescent = pts[2].fY;
1405 my->fBottom = pts[3].fY;
1406 my->fLeading = pts[4].fY;
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001407 my->fAvgCharWidth = pts[5].fY;
1408 my->fXMin = xmin;
1409 my->fXMax = xmax;
1410 my->fXHeight = x_height;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001411 }
1412}
1413
1414////////////////////////////////////////////////////////////////////////
1415////////////////////////////////////////////////////////////////////////
1416
1417SkScalerContext* SkFontHost::CreateScalerContext(const SkDescriptor* desc) {
reed@android.com62900b42009-02-11 15:07:19 +00001418 SkScalerContext_FreeType* c = SkNEW_ARGS(SkScalerContext_FreeType, (desc));
1419 if (!c->success()) {
1420 SkDELETE(c);
1421 c = NULL;
1422 }
1423 return c;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001424}
1425
1426///////////////////////////////////////////////////////////////////////////////
1427
1428/* Export this so that other parts of our FonttHost port can make use of our
1429 ability to extract the name+style from a stream, using FreeType's api.
1430*/
reed@google.com5b31b0f2011-02-23 14:41:42 +00001431SkTypeface::Style find_name_and_attributes(SkStream* stream, SkString* name,
1432 bool* isFixedWidth) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001433 FT_Library library;
reed@android.combfbd4ff2009-07-23 17:44:41 +00001434 if (FT_Init_FreeType(&library)) {
djsollen@google.com7b34ea62011-02-24 16:28:51 +00001435 name->reset();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001436 return SkTypeface::kNormal;
1437 }
1438
1439 FT_Open_Args args;
1440 memset(&args, 0, sizeof(args));
1441
1442 const void* memoryBase = stream->getMemoryBase();
1443 FT_StreamRec streamRec;
1444
1445 if (NULL != memoryBase) {
1446 args.flags = FT_OPEN_MEMORY;
1447 args.memory_base = (const FT_Byte*)memoryBase;
1448 args.memory_size = stream->getLength();
1449 } else {
1450 memset(&streamRec, 0, sizeof(streamRec));
1451 streamRec.size = stream->read(NULL, 0);
1452 streamRec.descriptor.pointer = stream;
1453 streamRec.read = sk_stream_read;
1454 streamRec.close = sk_stream_close;
1455
1456 args.flags = FT_OPEN_STREAM;
1457 args.stream = &streamRec;
1458 }
1459
1460 FT_Face face;
1461 if (FT_Open_Face(library, &args, 0, &face)) {
1462 FT_Done_FreeType(library);
djsollen@google.com7b34ea62011-02-24 16:28:51 +00001463 name->reset();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001464 return SkTypeface::kNormal;
1465 }
1466
1467 name->set(face->family_name);
1468 int style = SkTypeface::kNormal;
1469
1470 if (face->style_flags & FT_STYLE_FLAG_BOLD) {
1471 style |= SkTypeface::kBold;
1472 }
1473 if (face->style_flags & FT_STYLE_FLAG_ITALIC) {
1474 style |= SkTypeface::kItalic;
1475 }
reed@google.com5b31b0f2011-02-23 14:41:42 +00001476 if (isFixedWidth) {
1477 *isFixedWidth = FT_IS_FIXED_WIDTH(face);
1478 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001479
1480 FT_Done_Face(face);
1481 FT_Done_FreeType(library);
1482 return (SkTypeface::Style)style;
1483}