blob: 2894a3d1594cd8a9866456ceb64f2bf95408e317 [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) {
325#ifdef FT_FSTYPE_RESTRICTED_LICENSE_EMBEDDING
326 FT_UShort fsType = FT_Get_FSType_Flags(face);
327 return (fsType & (FT_FSTYPE_RESTRICTED_LICENSE_EMBEDDING |
328 FT_FSTYPE_BITMAP_EMBEDDING_ONLY)) == 0;
329#else
330 // No embedding is 0x2 and bitmap embedding only is 0x200.
331 TT_OS2* os2_table;
332 if ((os2_table = (TT_OS2*)FT_Get_Sfnt_Table(face, ft_sfnt_os2)) != NULL) {
333 return (os2_table->fsType & 0x202) == 0;
334 }
335 return false; // We tried, fail safe.
336#endif
337}
338
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000339static bool GetLetterCBox(FT_Face face, char letter, FT_BBox* bbox) {
340 const FT_UInt glyph_id = FT_Get_Char_Index(face, letter);
341 if (!glyph_id)
342 return false;
343 FT_Load_Glyph(face, glyph_id, FT_LOAD_NO_SCALE);
344 FT_Outline_Get_CBox(&face->glyph->outline, bbox);
345 return true;
346}
347
vandebo@chromium.org6f72d1e2011-02-14 23:19:59 +0000348static bool getWidthAdvance(FT_Face face, int gId, int16_t* data) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000349 FT_Fixed advance = 0;
vandebo@chromium.org6f72d1e2011-02-14 23:19:59 +0000350 if (getAdvances(face, gId, 1, FT_LOAD_NO_SCALE, &advance)) {
351 return false;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000352 }
vandebo@chromium.org6f72d1e2011-02-14 23:19:59 +0000353 SkASSERT(data);
354 *data = advance;
355 return true;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000356}
357
vandebo@chromium.org6744d492011-05-09 18:13:47 +0000358static void populate_glyph_to_unicode(FT_Face& face,
359 SkTDArray<SkUnichar>* glyphToUnicode) {
360 // Check and see if we have Unicode cmaps.
361 for (int i = 0; i < face->num_charmaps; ++i) {
362 // CMaps known to support Unicode:
363 // Platform ID Encoding ID Name
364 // ----------- ----------- -----------------------------------
365 // 0 0,1 Apple Unicode
366 // 0 3 Apple Unicode 2.0 (preferred)
367 // 3 1 Microsoft Unicode UCS-2
368 // 3 10 Microsoft Unicode UCS-4 (preferred)
369 //
370 // See Apple TrueType Reference Manual
371 // http://developer.apple.com/fonts/TTRefMan/RM06/Chap6cmap.html
372 // http://developer.apple.com/fonts/TTRefMan/RM06/Chap6name.html#ID
373 // Microsoft OpenType Specification
374 // http://www.microsoft.com/typography/otspec/cmap.htm
375
376 FT_UShort platformId = face->charmaps[i]->platform_id;
377 FT_UShort encodingId = face->charmaps[i]->encoding_id;
378
379 if (platformId != 0 && platformId != 3) {
380 continue;
381 }
382 if (platformId == 3 && encodingId != 1 && encodingId != 10) {
383 continue;
384 }
385 bool preferredMap = ((platformId == 3 && encodingId == 10) ||
386 (platformId == 0 && encodingId == 3));
387
388 FT_Set_Charmap(face, face->charmaps[i]);
389 if (glyphToUnicode->isEmpty()) {
390 glyphToUnicode->setCount(face->num_glyphs);
391 memset(glyphToUnicode->begin(), 0,
392 sizeof(SkUnichar) * face->num_glyphs);
393 }
394
395 // Iterate through each cmap entry.
396 FT_UInt glyphIndex;
397 for (SkUnichar charCode = FT_Get_First_Char(face, &glyphIndex);
398 glyphIndex != 0;
399 charCode = FT_Get_Next_Char(face, charCode, &glyphIndex)) {
400 if (charCode &&
401 ((*glyphToUnicode)[glyphIndex] == 0 || preferredMap)) {
402 (*glyphToUnicode)[glyphIndex] = charCode;
403 }
404 }
405 }
406}
407
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000408// static
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000409SkAdvancedTypefaceMetrics* SkFontHost::GetAdvancedTypefaceMetrics(
vandebo@chromium.org325cb9a2011-03-30 18:36:29 +0000410 uint32_t fontID,
vandebo@chromium.org7b13aca2011-08-16 23:30:48 +0000411 SkAdvancedTypefaceMetrics::PerGlyphInfo perGlyphInfo) {
djsollen@google.comcd9d69b2011-03-14 20:30:14 +0000412#if defined(SK_BUILD_FOR_MAC) || defined(ANDROID)
reed@google.com8a5d6922011-03-14 15:08:03 +0000413 return NULL;
414#else
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000415 SkAutoMutexAcquire ac(gFTMutex);
416 FT_Library libInit = NULL;
417 if (gFTCount == 0) {
418 if (!InitFreetype())
419 sk_throw();
420 libInit = gFTLibrary;
421 }
422 SkAutoTCallIProc<struct FT_LibraryRec_, FT_Done_FreeType> ftLib(libInit);
423 SkFaceRec* rec = ref_ft_face(fontID);
424 if (NULL == rec)
425 return NULL;
426 FT_Face face = rec->fFace;
427
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000428 SkAdvancedTypefaceMetrics* info = new SkAdvancedTypefaceMetrics;
429 info->fFontName.set(FT_Get_Postscript_Name(face));
430 info->fMultiMaster = FT_HAS_MULTIPLE_MASTERS(face);
431 info->fLastGlyphID = face->num_glyphs - 1;
432 info->fEmSize = 1000;
433
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000434 bool cid = false;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000435 const char* fontType = FT_Get_X11_Font_Format(face);
vandebo@chromium.orgc3a2ae52011-02-03 21:48:23 +0000436 if (strcmp(fontType, "Type 1") == 0) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000437 info->fType = SkAdvancedTypefaceMetrics::kType1_Font;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000438 } else if (strcmp(fontType, "CID Type 1") == 0) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000439 info->fType = SkAdvancedTypefaceMetrics::kType1CID_Font;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000440 cid = true;
441 } else if (strcmp(fontType, "CFF") == 0) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000442 info->fType = SkAdvancedTypefaceMetrics::kCFF_Font;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000443 } else if (strcmp(fontType, "TrueType") == 0) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000444 info->fType = SkAdvancedTypefaceMetrics::kTrueType_Font;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000445 cid = true;
446 TT_Header* ttHeader;
447 if ((ttHeader = (TT_Header*)FT_Get_Sfnt_Table(face,
448 ft_sfnt_head)) != NULL) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000449 info->fEmSize = ttHeader->Units_Per_EM;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000450 }
451 }
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000452
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000453 info->fStyle = 0;
454 if (FT_IS_FIXED_WIDTH(face))
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000455 info->fStyle |= SkAdvancedTypefaceMetrics::kFixedPitch_Style;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000456 if (face->style_flags & FT_STYLE_FLAG_ITALIC)
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000457 info->fStyle |= SkAdvancedTypefaceMetrics::kItalic_Style;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000458 // We should set either Symbolic or Nonsymbolic; Nonsymbolic if the font's
459 // character set is a subset of 'Adobe standard Latin.'
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000460 info->fStyle |= SkAdvancedTypefaceMetrics::kSymbolic_Style;
461
462 PS_FontInfoRec ps_info;
463 TT_Postscript* tt_info;
464 if (FT_Get_PS_Font_Info(face, &ps_info) == 0) {
465 info->fItalicAngle = ps_info.italic_angle;
466 } else if ((tt_info =
467 (TT_Postscript*)FT_Get_Sfnt_Table(face,
468 ft_sfnt_post)) != NULL) {
469 info->fItalicAngle = SkFixedToScalar(tt_info->italicAngle);
470 } else {
471 info->fItalicAngle = 0;
472 }
473
474 info->fAscent = face->ascender;
475 info->fDescent = face->descender;
476
477 // Figure out a good guess for StemV - Min width of i, I, !, 1.
478 // This probably isn't very good with an italic font.
479 int16_t min_width = SHRT_MAX;
vandebo@chromium.org6f72d1e2011-02-14 23:19:59 +0000480 info->fStemV = 0;
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000481 char stem_chars[] = {'i', 'I', '!', '1'};
482 for (size_t i = 0; i < SK_ARRAY_COUNT(stem_chars); i++) {
483 FT_BBox bbox;
484 if (GetLetterCBox(face, stem_chars[i], &bbox)) {
485 int16_t width = bbox.xMax - bbox.xMin;
486 if (width > 0 && width < min_width) {
487 min_width = width;
488 info->fStemV = min_width;
489 }
490 }
491 }
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000492
493 TT_PCLT* pclt_info;
494 TT_OS2* os2_table;
495 if ((pclt_info = (TT_PCLT*)FT_Get_Sfnt_Table(face, ft_sfnt_pclt)) != NULL) {
496 info->fCapHeight = pclt_info->CapHeight;
497 uint8_t serif_style = pclt_info->SerifStyle & 0x3F;
498 if (serif_style >= 2 && serif_style <= 6)
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000499 info->fStyle |= SkAdvancedTypefaceMetrics::kSerif_Style;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000500 else if (serif_style >= 9 && serif_style <= 12)
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000501 info->fStyle |= SkAdvancedTypefaceMetrics::kScript_Style;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000502 } else if ((os2_table =
503 (TT_OS2*)FT_Get_Sfnt_Table(face, ft_sfnt_os2)) != NULL) {
504 info->fCapHeight = os2_table->sCapHeight;
505 } else {
506 // Figure out a good guess for CapHeight: average the height of M and X.
507 FT_BBox m_bbox, x_bbox;
508 bool got_m, got_x;
509 got_m = GetLetterCBox(face, 'M', &m_bbox);
510 got_x = GetLetterCBox(face, 'X', &x_bbox);
511 if (got_m && got_x) {
512 info->fCapHeight = (m_bbox.yMax - m_bbox.yMin + x_bbox.yMax -
513 x_bbox.yMin) / 2;
514 } else if (got_m && !got_x) {
515 info->fCapHeight = m_bbox.yMax - m_bbox.yMin;
516 } else if (!got_m && got_x) {
517 info->fCapHeight = x_bbox.yMax - x_bbox.yMin;
518 }
519 }
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000520
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000521 info->fBBox = SkIRect::MakeLTRB(face->bbox.xMin, face->bbox.yMax,
522 face->bbox.xMax, face->bbox.yMin);
523
vandebo@chromium.org7b13aca2011-08-16 23:30:48 +0000524 if (!canEmbed(face) || !FT_IS_SCALABLE(face) ||
vandebo@chromium.org325cb9a2011-03-30 18:36:29 +0000525 info->fType == SkAdvancedTypefaceMetrics::kOther_Font) {
526 perGlyphInfo = SkAdvancedTypefaceMetrics::kNo_PerGlyphInfo;
527 }
528
529 if (perGlyphInfo & SkAdvancedTypefaceMetrics::kHAdvance_PerGlyphInfo) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000530 if (FT_IS_FIXED_WIDTH(face)) {
531 appendRange(&info->fGlyphWidths, 0);
532 int16_t advance = face->max_advance_width;
533 info->fGlyphWidths->fAdvance.append(1, &advance);
534 finishRange(info->fGlyphWidths.get(), 0,
535 SkAdvancedTypefaceMetrics::WidthRange::kDefault);
vandebo@chromium.org6f72d1e2011-02-14 23:19:59 +0000536 } else if (!cid) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000537 appendRange(&info->fGlyphWidths, 0);
538 // So as to not blow out the stack, get advances in batches.
539 for (int gID = 0; gID < face->num_glyphs; gID += 128) {
540 FT_Fixed advances[128];
541 int advanceCount = 128;
542 if (gID + advanceCount > face->num_glyphs)
543 advanceCount = face->num_glyphs - gID + 1;
544 getAdvances(face, gID, advanceCount, FT_LOAD_NO_SCALE,
545 advances);
546 for (int i = 0; i < advanceCount; i++) {
547 int16_t advance = advances[gID + i];
548 info->fGlyphWidths->fAdvance.append(1, &advance);
549 }
550 }
551 finishRange(info->fGlyphWidths.get(), face->num_glyphs - 1,
552 SkAdvancedTypefaceMetrics::WidthRange::kRange);
553 } else {
vandebo@chromium.org6f72d1e2011-02-14 23:19:59 +0000554 info->fGlyphWidths.reset(
vandebo@chromium.org7b13aca2011-08-16 23:30:48 +0000555 getAdvanceData(face, face->num_glyphs, &getWidthAdvance));
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000556 }
vandebo@chromium.org325cb9a2011-03-30 18:36:29 +0000557 }
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000558
vandebo@chromium.org325cb9a2011-03-30 18:36:29 +0000559 if (perGlyphInfo & SkAdvancedTypefaceMetrics::kVAdvance_PerGlyphInfo &&
560 FT_HAS_VERTICAL(face)) {
561 SkASSERT(false); // Not implemented yet.
562 }
563
564 if (perGlyphInfo & SkAdvancedTypefaceMetrics::kGlyphNames_PerGlyphInfo &&
565 info->fType == SkAdvancedTypefaceMetrics::kType1_Font) {
566 // Postscript fonts may contain more than 255 glyphs, so we end up
567 // using multiple font descriptions with a glyph ordering. Record
568 // the name of each glyph.
569 info->fGlyphNames.reset(
570 new SkAutoTArray<SkString>(face->num_glyphs));
571 for (int gID = 0; gID < face->num_glyphs; gID++) {
572 char glyphName[128]; // PS limit for names is 127 bytes.
573 FT_Get_Glyph_Name(face, gID, glyphName, 128);
574 info->fGlyphNames->get()[gID].set(glyphName);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000575 }
576 }
577
vandebo@chromium.org6744d492011-05-09 18:13:47 +0000578 if (perGlyphInfo & SkAdvancedTypefaceMetrics::kToUnicode_PerGlyphInfo &&
579 info->fType != SkAdvancedTypefaceMetrics::kType1_Font &&
580 face->num_charmaps) {
581 populate_glyph_to_unicode(face, &(info->fGlyphToUnicode));
582 }
583
vandebo@chromium.orgc3a2ae52011-02-03 21:48:23 +0000584 if (!canEmbed(face))
585 info->fType = SkAdvancedTypefaceMetrics::kNotEmbeddable_Font;
586
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000587 unref_ft_face(face);
588 return info;
reed@google.com8a5d6922011-03-14 15:08:03 +0000589#endif
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000590}
reed@google.com618ef5e2011-01-26 22:10:41 +0000591///////////////////////////////////////////////////////////////////////////
592
reed@google.com8ed436c2011-07-21 14:12:36 +0000593static bool bothZero(SkScalar a, SkScalar b) {
594 return 0 == a && 0 == b;
595}
596
597// returns false if there is any non-90-rotation or skew
598static bool isAxisAligned(const SkScalerContext::Rec& rec) {
599 return 0 == rec.fPreSkewX &&
600 (bothZero(rec.fPost2x2[0][1], rec.fPost2x2[1][0]) ||
601 bothZero(rec.fPost2x2[0][0], rec.fPost2x2[1][1]));
602}
603
reed@google.com618ef5e2011-01-26 22:10:41 +0000604void SkFontHost::FilterRec(SkScalerContext::Rec* rec) {
605 if (!gLCDSupportValid) {
606 InitFreetype();
607 FT_Done_FreeType(gFTLibrary);
608 }
reed@google.com5b31b0f2011-02-23 14:41:42 +0000609
reed@google.comeffc5012011-06-27 16:44:46 +0000610 if (!gLCDSupport && isLCD(*rec)) {
reed@google.com618ef5e2011-01-26 22:10:41 +0000611 // If the runtime Freetype library doesn't support LCD mode, we disable
612 // it here.
613 rec->fMaskFormat = SkMask::kA8_Format;
614 }
reed@google.com5b31b0f2011-02-23 14:41:42 +0000615
reed@google.com618ef5e2011-01-26 22:10:41 +0000616 SkPaint::Hinting h = rec->getHinting();
reed@google.comeffc5012011-06-27 16:44:46 +0000617 if (SkPaint::kFull_Hinting == h && !isLCD(*rec)) {
reed@google.com618ef5e2011-01-26 22:10:41 +0000618 // collapse full->normal hinting if we're not doing LCD
619 h = SkPaint::kNormal_Hinting;
620 } else if ((rec->fFlags & SkScalerContext::kSubpixelPositioning_Flag) &&
621 SkPaint::kNo_Hinting != h) {
622 // to do subpixel, we must have at most slight hinting
623 h = SkPaint::kSlight_Hinting;
624 }
vandebo@chromium.org7b13aca2011-08-16 23:30:48 +0000625#ifndef SK_IGNORE_ROTATED_FREETYPE_FIX
reed@google.com8ed436c2011-07-21 14:12:36 +0000626 // rotated text looks bad with hinting, so we disable it as needed
627 if (!isAxisAligned(*rec)) {
628 h = SkPaint::kNo_Hinting;
629 }
bsalomon@google.com0e35ca82011-07-22 17:56:19 +0000630#endif
reed@google.com618ef5e2011-01-26 22:10:41 +0000631 rec->setHinting(h);
632}
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000633
djsollen@google.comcd9d69b2011-03-14 20:30:14 +0000634#ifdef ANDROID
635uint32_t SkFontHost::GetUnitsPerEm(SkFontID fontID) {
636 SkAutoMutexAcquire ac(gFTMutex);
637 SkFaceRec *rec = ref_ft_face(fontID);
638 uint16_t unitsPerEm = 0;
639
640 if (rec != NULL && rec->fFace != NULL) {
641 unitsPerEm = rec->fFace->units_per_EM;
642 unref_ft_face(rec->fFace);
643 }
644
645 return (uint32_t)unitsPerEm;
646}
647#endif
648
reed@android.com8a1c16f2008-12-17 15:59:43 +0000649SkScalerContext_FreeType::SkScalerContext_FreeType(const SkDescriptor* desc)
reed@android.com62900b42009-02-11 15:07:19 +0000650 : SkScalerContext(desc) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000651 SkAutoMutexAcquire ac(gFTMutex);
652
reed@android.com8a1c16f2008-12-17 15:59:43 +0000653 if (gFTCount == 0) {
reed@android.com659aaf92009-07-23 15:20:21 +0000654 if (!InitFreetype()) {
655 sk_throw();
656 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000657 }
658 ++gFTCount;
659
660 // load the font file
reed@android.com62900b42009-02-11 15:07:19 +0000661 fFTSize = NULL;
662 fFace = NULL;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000663 fFaceRec = ref_ft_face(fRec.fFontID);
reed@android.com62900b42009-02-11 15:07:19 +0000664 if (NULL == fFaceRec) {
665 return;
666 }
667 fFace = fFaceRec->fFace;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000668
669 // compute our factors from the record
670
671 SkMatrix m;
672
673 fRec.getSingleMatrix(&m);
674
675#ifdef DUMP_STRIKE_CREATION
676 SkString keyString;
677 SkFontHost::GetDescriptorKeyString(desc, &keyString);
678 printf("========== strike [%g %g %g] [%g %g %g %g] hints %d format %d %s\n", SkScalarToFloat(fRec.fTextSize),
679 SkScalarToFloat(fRec.fPreScaleX), SkScalarToFloat(fRec.fPreSkewX),
680 SkScalarToFloat(fRec.fPost2x2[0][0]), SkScalarToFloat(fRec.fPost2x2[0][1]),
681 SkScalarToFloat(fRec.fPost2x2[1][0]), SkScalarToFloat(fRec.fPost2x2[1][1]),
agl@chromium.org309485b2009-07-21 17:41:32 +0000682 fRec.getHinting(), fRec.fMaskFormat, keyString.c_str());
reed@android.com8a1c16f2008-12-17 15:59:43 +0000683#endif
684
685 // now compute our scale factors
686 SkScalar sx = m.getScaleX();
687 SkScalar sy = m.getScaleY();
688
689 if (m.getSkewX() || m.getSkewY() || sx < 0 || sy < 0) {
690 // sort of give up on hinting
691 sx = SkMaxScalar(SkScalarAbs(sx), SkScalarAbs(m.getSkewX()));
692 sy = SkMaxScalar(SkScalarAbs(m.getSkewY()), SkScalarAbs(sy));
693 sx = sy = SkScalarAve(sx, sy);
694
695 SkScalar inv = SkScalarInvert(sx);
696
697 // flip the skew elements to go from our Y-down system to FreeType's
698 fMatrix22.xx = SkScalarToFixed(SkScalarMul(m.getScaleX(), inv));
699 fMatrix22.xy = -SkScalarToFixed(SkScalarMul(m.getSkewX(), inv));
700 fMatrix22.yx = -SkScalarToFixed(SkScalarMul(m.getSkewY(), inv));
701 fMatrix22.yy = SkScalarToFixed(SkScalarMul(m.getScaleY(), inv));
702 } else {
703 fMatrix22.xx = fMatrix22.yy = SK_Fixed1;
704 fMatrix22.xy = fMatrix22.yx = 0;
705 }
706
707 fScaleX = SkScalarToFixed(sx);
708 fScaleY = SkScalarToFixed(sy);
709
710 // compute the flags we send to Load_Glyph
711 {
reed@android.come4d0bc02009-07-24 19:53:20 +0000712 FT_Int32 loadFlags = FT_LOAD_DEFAULT;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000713
agl@chromium.org70a303f2010-05-10 14:15:50 +0000714 if (SkMask::kBW_Format == fRec.fMaskFormat) {
715 // See http://code.google.com/p/chromium/issues/detail?id=43252#c24
716 loadFlags = FT_LOAD_TARGET_MONO;
reed@google.comeffc5012011-06-27 16:44:46 +0000717 if (fRec.getHinting() == SkPaint::kNo_Hinting) {
agl@chromium.org70a303f2010-05-10 14:15:50 +0000718 loadFlags = FT_LOAD_NO_HINTING;
reed@google.comeffc5012011-06-27 16:44:46 +0000719 }
agl@chromium.org70a303f2010-05-10 14:15:50 +0000720 } else {
721 switch (fRec.getHinting()) {
722 case SkPaint::kNo_Hinting:
723 loadFlags = FT_LOAD_NO_HINTING;
724 break;
725 case SkPaint::kSlight_Hinting:
726 loadFlags = FT_LOAD_TARGET_LIGHT; // This implies FORCE_AUTOHINT
727 break;
728 case SkPaint::kNormal_Hinting:
agl@chromium.orga2c71cb2010-06-17 20:49:17 +0000729 if (fRec.fFlags & SkScalerContext::kAutohinting_Flag)
730 loadFlags = FT_LOAD_FORCE_AUTOHINT;
731 else
732 loadFlags = FT_LOAD_NO_AUTOHINT;
agl@chromium.org70a303f2010-05-10 14:15:50 +0000733 break;
734 case SkPaint::kFull_Hinting:
agl@chromium.orga2c71cb2010-06-17 20:49:17 +0000735 if (fRec.fFlags & SkScalerContext::kAutohinting_Flag) {
736 loadFlags = FT_LOAD_FORCE_AUTOHINT;
737 break;
738 }
agl@chromium.org70a303f2010-05-10 14:15:50 +0000739 loadFlags = FT_LOAD_TARGET_NORMAL;
reed@google.comeffc5012011-06-27 16:44:46 +0000740 if (isLCD(fRec)) {
741 if (fRec.fFlags & SkScalerContext::kLCD_Vertical_Flag) {
742 loadFlags = FT_LOAD_TARGET_LCD_V;
743 } else {
744 loadFlags = FT_LOAD_TARGET_LCD;
745 }
reed@google.comea2333d2011-03-14 16:44:56 +0000746 }
agl@chromium.org70a303f2010-05-10 14:15:50 +0000747 break;
748 default:
749 SkDebugf("---------- UNKNOWN hinting %d\n", fRec.getHinting());
750 break;
751 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000752 }
753
reed@google.comeffc5012011-06-27 16:44:46 +0000754 if ((fRec.fFlags & SkScalerContext::kEmbeddedBitmapText_Flag) == 0) {
agl@chromium.orge0d08992009-08-07 19:19:23 +0000755 loadFlags |= FT_LOAD_NO_BITMAP;
reed@google.comeffc5012011-06-27 16:44:46 +0000756 }
agl@chromium.orge0d08992009-08-07 19:19:23 +0000757
reed@google.com96a9f7912011-05-06 11:49:30 +0000758 // Always using FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH to get correct
759 // advances, as fontconfig and cairo do.
760 // See http://code.google.com/p/skia/issues/detail?id=222.
761 loadFlags |= FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH;
762
reed@android.come4d0bc02009-07-24 19:53:20 +0000763 fLoadGlyphFlags = loadFlags;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000764 }
765
766 // now create the FT_Size
767
768 {
769 FT_Error err;
770
771 err = FT_New_Size(fFace, &fFTSize);
772 if (err != 0) {
773 SkDEBUGF(("SkScalerContext_FreeType::FT_New_Size(%x): FT_Set_Char_Size(0x%x, 0x%x) returned 0x%x\n",
774 fFaceRec->fFontID, fScaleX, fScaleY, err));
775 fFace = NULL;
776 return;
777 }
778
779 err = FT_Activate_Size(fFTSize);
780 if (err != 0) {
781 SkDEBUGF(("SkScalerContext_FreeType::FT_Activate_Size(%x, 0x%x, 0x%x) returned 0x%x\n",
782 fFaceRec->fFontID, fScaleX, fScaleY, err));
783 fFTSize = NULL;
784 }
785
786 err = FT_Set_Char_Size( fFace,
787 SkFixedToFDot6(fScaleX), SkFixedToFDot6(fScaleY),
788 72, 72);
789 if (err != 0) {
790 SkDEBUGF(("SkScalerContext_FreeType::FT_Set_Char_Size(%x, 0x%x, 0x%x) returned 0x%x\n",
791 fFaceRec->fFontID, fScaleX, fScaleY, err));
792 fFace = NULL;
793 return;
794 }
795
796 FT_Set_Transform( fFace, &fMatrix22, NULL);
797 }
798}
799
800SkScalerContext_FreeType::~SkScalerContext_FreeType() {
801 if (fFTSize != NULL) {
802 FT_Done_Size(fFTSize);
803 }
804
805 SkAutoMutexAcquire ac(gFTMutex);
806
807 if (fFace != NULL) {
808 unref_ft_face(fFace);
809 }
810 if (--gFTCount == 0) {
811// SkDEBUGF(("FT_Done_FreeType\n"));
812 FT_Done_FreeType(gFTLibrary);
813 SkDEBUGCODE(gFTLibrary = NULL;)
814 }
815}
816
817/* We call this before each use of the fFace, since we may be sharing
818 this face with other context (at different sizes).
819*/
820FT_Error SkScalerContext_FreeType::setupSize() {
821 /* In the off-chance that a font has been removed, we want to error out
822 right away, so call resolve just to be sure.
823
824 TODO: perhaps we can skip this, by walking the global font cache and
825 killing all of the contexts when we know that a given fontID is going
826 away...
827 */
reed@android.comb1d9d2e2009-03-04 17:37:51 +0000828 if (!SkFontHost::ValidFontID(fRec.fFontID)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000829 return (FT_Error)-1;
830 }
831
832 FT_Error err = FT_Activate_Size(fFTSize);
833
834 if (err != 0) {
835 SkDEBUGF(("SkScalerContext_FreeType::FT_Activate_Size(%x, 0x%x, 0x%x) returned 0x%x\n",
836 fFaceRec->fFontID, fScaleX, fScaleY, err));
837 fFTSize = NULL;
838 } else {
839 // seems we need to reset this every time (not sure why, but without it
840 // I get random italics from some other fFTSize)
841 FT_Set_Transform( fFace, &fMatrix22, NULL);
842 }
843 return err;
844}
845
senorblanco@chromium.org4526a842010-02-05 23:08:20 +0000846void SkScalerContext_FreeType::emboldenOutline(FT_Outline* outline) {
847 FT_Pos strength;
848 strength = FT_MulFix(fFace->units_per_EM, fFace->size->metrics.y_scale)
849 / 24;
850 FT_Outline_Embolden(outline, strength);
851}
852
ctguil@chromium.org0bc7bf52011-03-04 19:04:57 +0000853unsigned SkScalerContext_FreeType::generateGlyphCount() {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000854 return fFace->num_glyphs;
855}
856
857uint16_t SkScalerContext_FreeType::generateCharToGlyph(SkUnichar uni) {
858 return SkToU16(FT_Get_Char_Index( fFace, uni ));
859}
860
reed@android.com9d3a9852010-01-08 14:07:42 +0000861SkUnichar SkScalerContext_FreeType::generateGlyphToChar(uint16_t glyph) {
862 // iterate through each cmap entry, looking for matching glyph indices
863 FT_UInt glyphIndex;
864 SkUnichar charCode = FT_Get_First_Char( fFace, &glyphIndex );
865
866 while (glyphIndex != 0) {
867 if (glyphIndex == glyph) {
868 return charCode;
869 }
870 charCode = FT_Get_Next_Char( fFace, charCode, &glyphIndex );
871 }
872
873 return 0;
874}
875
reed@android.com8a1c16f2008-12-17 15:59:43 +0000876static FT_Pixel_Mode compute_pixel_mode(SkMask::Format format) {
877 switch (format) {
878 case SkMask::kBW_Format:
879 return FT_PIXEL_MODE_MONO;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000880 case SkMask::kA8_Format:
881 default:
882 return FT_PIXEL_MODE_GRAY;
883 }
884}
885
reed@android.com8a1c16f2008-12-17 15:59:43 +0000886void SkScalerContext_FreeType::generateAdvance(SkGlyph* glyph) {
887#ifdef FT_ADVANCES_H
888 /* unhinted and light hinted text have linearly scaled advances
889 * which are very cheap to compute with some font formats...
890 */
891 {
892 SkAutoMutexAcquire ac(gFTMutex);
893
894 if (this->setupSize()) {
reed@android.com62900b42009-02-11 15:07:19 +0000895 glyph->zeroMetrics();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000896 return;
897 }
898
899 FT_Error error;
900 FT_Fixed advance;
901
902 error = FT_Get_Advance( fFace, glyph->getGlyphID(fBaseGlyphCount),
903 fLoadGlyphFlags | FT_ADVANCE_FLAG_FAST_ONLY,
904 &advance );
905 if (0 == error) {
906 glyph->fRsbDelta = 0;
907 glyph->fLsbDelta = 0;
908 glyph->fAdvanceX = advance; // advance *2/3; //DEBUG
909 glyph->fAdvanceY = 0;
910 return;
911 }
912 }
913#endif /* FT_ADVANCES_H */
914 /* otherwise, we need to load/hint the glyph, which is slower */
915 this->generateMetrics(glyph);
916 return;
917}
918
919void SkScalerContext_FreeType::generateMetrics(SkGlyph* glyph) {
920 SkAutoMutexAcquire ac(gFTMutex);
921
922 glyph->fRsbDelta = 0;
923 glyph->fLsbDelta = 0;
924
925 FT_Error err;
926
927 if (this->setupSize()) {
928 goto ERROR;
929 }
930
931 err = FT_Load_Glyph( fFace, glyph->getGlyphID(fBaseGlyphCount), fLoadGlyphFlags );
932 if (err != 0) {
933 SkDEBUGF(("SkScalerContext_FreeType::generateMetrics(%x): FT_Load_Glyph(glyph:%d flags:%d) returned 0x%x\n",
934 fFaceRec->fFontID, glyph->getGlyphID(fBaseGlyphCount), fLoadGlyphFlags, err));
935 ERROR:
reed@android.com62900b42009-02-11 15:07:19 +0000936 glyph->zeroMetrics();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000937 return;
938 }
939
940 switch ( fFace->glyph->format ) {
thakis@chromium.org598b8592011-05-24 05:42:55 +0000941 case FT_GLYPH_FORMAT_OUTLINE: {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000942 FT_BBox bbox;
943
senorblanco@chromium.org4526a842010-02-05 23:08:20 +0000944 if (fRec.fFlags & kEmbolden_Flag) {
945 emboldenOutline(&fFace->glyph->outline);
946 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000947 FT_Outline_Get_CBox(&fFace->glyph->outline, &bbox);
948
agl@chromium.orga2c71cb2010-06-17 20:49:17 +0000949 if (fRec.fFlags & SkScalerContext::kSubpixelPositioning_Flag) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000950 int dx = glyph->getSubXFixed() >> 10;
951 int dy = glyph->getSubYFixed() >> 10;
952 // negate dy since freetype-y-goes-up and skia-y-goes-down
953 bbox.xMin += dx;
954 bbox.yMin -= dy;
955 bbox.xMax += dx;
956 bbox.yMax -= dy;
957 }
958
959 bbox.xMin &= ~63;
960 bbox.yMin &= ~63;
961 bbox.xMax = (bbox.xMax + 63) & ~63;
962 bbox.yMax = (bbox.yMax + 63) & ~63;
963
964 glyph->fWidth = SkToU16((bbox.xMax - bbox.xMin) >> 6);
965 glyph->fHeight = SkToU16((bbox.yMax - bbox.yMin) >> 6);
966 glyph->fTop = -SkToS16(bbox.yMax >> 6);
967 glyph->fLeft = SkToS16(bbox.xMin >> 6);
968 break;
thakis@chromium.org598b8592011-05-24 05:42:55 +0000969 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000970
971 case FT_GLYPH_FORMAT_BITMAP:
agl@chromium.orge76073b2010-06-04 20:31:17 +0000972 if (fRec.fFlags & kEmbolden_Flag) {
973 FT_GlyphSlot_Own_Bitmap(fFace->glyph);
974 FT_Bitmap_Embolden(gFTLibrary, &fFace->glyph->bitmap, kBitmapEmboldenStrength, 0);
975 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000976 glyph->fWidth = SkToU16(fFace->glyph->bitmap.width);
977 glyph->fHeight = SkToU16(fFace->glyph->bitmap.rows);
978 glyph->fTop = -SkToS16(fFace->glyph->bitmap_top);
979 glyph->fLeft = SkToS16(fFace->glyph->bitmap_left);
980 break;
981
982 default:
983 SkASSERT(!"unknown glyph format");
984 goto ERROR;
985 }
986
agl@chromium.orga2c71cb2010-06-17 20:49:17 +0000987 if ((fRec.fFlags & SkScalerContext::kSubpixelPositioning_Flag) == 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000988 glyph->fAdvanceX = SkFDot6ToFixed(fFace->glyph->advance.x);
989 glyph->fAdvanceY = -SkFDot6ToFixed(fFace->glyph->advance.y);
990 if (fRec.fFlags & kDevKernText_Flag) {
991 glyph->fRsbDelta = SkToS8(fFace->glyph->rsb_delta);
992 glyph->fLsbDelta = SkToS8(fFace->glyph->lsb_delta);
993 }
994 } else {
995 glyph->fAdvanceX = SkFixedMul(fMatrix22.xx, fFace->glyph->linearHoriAdvance);
996 glyph->fAdvanceY = -SkFixedMul(fMatrix22.yx, fFace->glyph->linearHoriAdvance);
997 }
998
999#ifdef ENABLE_GLYPH_SPEW
1000 SkDEBUGF(("FT_Set_Char_Size(this:%p sx:%x sy:%x ", this, fScaleX, fScaleY));
1001 SkDEBUGF(("Metrics(glyph:%d flags:0x%x) w:%d\n", glyph->getGlyphID(fBaseGlyphCount), fLoadGlyphFlags, glyph->fWidth));
1002#endif
1003}
1004
reed@google.combde3c8e2011-05-18 11:58:10 +00001005static int lerp(int start, int end) {
1006 SkASSERT((unsigned)SK_FREETYPE_LCD_LERP <= 256);
1007 return start + ((end - start) * (SK_FREETYPE_LCD_LERP) >> 8);
reed@google.comc5181342011-05-17 20:52:46 +00001008}
1009
1010static uint16_t packTriple(unsigned r, unsigned g, unsigned b) {
1011 if (SK_FREETYPE_LCD_LERP) {
reed@google.combde3c8e2011-05-18 11:58:10 +00001012 // want (a+b+c)/3, but we approx to avoid the divide
1013 unsigned ave = (5 * (r + g + b) + b) >> 4;
1014 r = lerp(r, ave);
1015 g = lerp(g, ave);
1016 b = lerp(b, ave);
reed@google.comc5181342011-05-17 20:52:46 +00001017 }
1018 return SkPackRGB16(r >> 3, g >> 2, b >> 3);
1019}
1020
reed@google.com73824072011-06-23 13:17:30 +00001021static uint16_t grayToRGB16(U8CPU gray) {
1022 SkASSERT(gray <= 255);
1023 return SkPackRGB16(gray >> 3, gray >> 2, gray >> 3);
1024}
1025
1026static int bittst(const uint8_t data[], int bitOffset) {
1027 SkASSERT(bitOffset >= 0);
reed@google.comc8e0f932011-06-23 19:39:49 +00001028 int lowBit = data[bitOffset >> 3] >> (~bitOffset & 7);
reed@google.com73824072011-06-23 13:17:30 +00001029 return lowBit & 1;
1030}
1031
reed@google.comeffc5012011-06-27 16:44:46 +00001032static void copyFT2LCD16(const SkGlyph& glyph, const FT_Bitmap& bitmap,
1033 int lcdIsBGR) {
reed@google.comea2333d2011-03-14 16:44:56 +00001034 SkASSERT(glyph.fHeight == bitmap.rows);
reed@google.comea2333d2011-03-14 16:44:56 +00001035 uint16_t* dst = reinterpret_cast<uint16_t*>(glyph.fImage);
reed@google.com73824072011-06-23 13:17:30 +00001036 const size_t dstRB = glyph.rowBytes();
1037 const int width = glyph.fWidth;
1038 const uint8_t* src = bitmap.buffer;
reed@google.comea2333d2011-03-14 16:44:56 +00001039
reed@google.com73824072011-06-23 13:17:30 +00001040 switch (bitmap.pixel_mode) {
1041 case FT_PIXEL_MODE_MONO: {
1042 for (int y = 0; y < glyph.fHeight; ++y) {
1043 for (int x = 0; x < width; ++x) {
1044 dst[x] = -bittst(src, x);
1045 }
1046 dst = (uint16_t*)((char*)dst + dstRB);
1047 src += bitmap.pitch;
1048 }
1049 } break;
1050 case FT_PIXEL_MODE_GRAY: {
1051 for (int y = 0; y < glyph.fHeight; ++y) {
1052 for (int x = 0; x < width; ++x) {
1053 dst[x] = grayToRGB16(src[x]);
1054 }
1055 dst = (uint16_t*)((char*)dst + dstRB);
1056 src += bitmap.pitch;
1057 }
1058 } break;
1059 default: {
1060 SkASSERT(glyph.fWidth * 3 == bitmap.width - 6);
1061 src += 3;
1062 for (int y = 0; y < glyph.fHeight; y++) {
1063 const uint8_t* triple = src;
reed@google.comeffc5012011-06-27 16:44:46 +00001064 if (lcdIsBGR) {
1065 for (int x = 0; x < width; x++) {
1066 dst[x] = packTriple(triple[2], triple[1], triple[0]);
1067 triple += 3;
1068 }
1069 } else {
1070 for (int x = 0; x < width; x++) {
1071 dst[x] = packTriple(triple[0], triple[1], triple[2]);
1072 triple += 3;
1073 }
reed@google.com73824072011-06-23 13:17:30 +00001074 }
1075 src += bitmap.pitch;
1076 dst = (uint16_t*)((char*)dst + dstRB);
1077 }
1078 } break;
reed@google.comea2333d2011-03-14 16:44:56 +00001079 }
1080}
1081
reed@android.com8a1c16f2008-12-17 15:59:43 +00001082void SkScalerContext_FreeType::generateImage(const SkGlyph& glyph) {
1083 SkAutoMutexAcquire ac(gFTMutex);
1084
1085 FT_Error err;
1086
1087 if (this->setupSize()) {
1088 goto ERROR;
1089 }
1090
1091 err = FT_Load_Glyph( fFace, glyph.getGlyphID(fBaseGlyphCount), fLoadGlyphFlags);
1092 if (err != 0) {
1093 SkDEBUGF(("SkScalerContext_FreeType::generateImage: FT_Load_Glyph(glyph:%d width:%d height:%d rb:%d flags:%d) returned 0x%x\n",
1094 glyph.getGlyphID(fBaseGlyphCount), glyph.fWidth, glyph.fHeight, glyph.rowBytes(), fLoadGlyphFlags, err));
1095 ERROR:
1096 memset(glyph.fImage, 0, glyph.rowBytes() * glyph.fHeight);
1097 return;
1098 }
1099
1100 switch ( fFace->glyph->format ) {
1101 case FT_GLYPH_FORMAT_OUTLINE: {
1102 FT_Outline* outline = &fFace->glyph->outline;
1103 FT_BBox bbox;
1104 FT_Bitmap target;
1105
senorblanco@chromium.org4526a842010-02-05 23:08:20 +00001106 if (fRec.fFlags & kEmbolden_Flag) {
1107 emboldenOutline(outline);
1108 }
1109
reed@android.com8a1c16f2008-12-17 15:59:43 +00001110 int dx = 0, dy = 0;
agl@chromium.orga2c71cb2010-06-17 20:49:17 +00001111 if (fRec.fFlags & SkScalerContext::kSubpixelPositioning_Flag) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001112 dx = glyph.getSubXFixed() >> 10;
1113 dy = glyph.getSubYFixed() >> 10;
1114 // negate dy since freetype-y-goes-up and skia-y-goes-down
1115 dy = -dy;
1116 }
1117 FT_Outline_Get_CBox(outline, &bbox);
1118 /*
1119 what we really want to do for subpixel is
1120 offset(dx, dy)
1121 compute_bounds
1122 offset(bbox & !63)
1123 but that is two calls to offset, so we do the following, which
1124 achieves the same thing with only one offset call.
1125 */
1126 FT_Outline_Translate(outline, dx - ((bbox.xMin + dx) & ~63),
1127 dy - ((bbox.yMin + dy) & ~63));
1128
reed@google.comea2333d2011-03-14 16:44:56 +00001129 if (SkMask::kLCD16_Format == glyph.fMaskFormat) {
1130 FT_Render_Glyph(fFace->glyph, FT_RENDER_MODE_LCD);
reed@google.comeffc5012011-06-27 16:44:46 +00001131 copyFT2LCD16(glyph, fFace->glyph->bitmap,
1132 fRec.fFlags & SkScalerContext::kLCD_BGROrder_Flag);
reed@google.comea2333d2011-03-14 16:44:56 +00001133 } else {
1134 target.width = glyph.fWidth;
1135 target.rows = glyph.fHeight;
1136 target.pitch = glyph.rowBytes();
1137 target.buffer = reinterpret_cast<uint8_t*>(glyph.fImage);
1138 target.pixel_mode = compute_pixel_mode(
1139 (SkMask::Format)fRec.fMaskFormat);
1140 target.num_grays = 256;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001141
reed@google.comea2333d2011-03-14 16:44:56 +00001142 memset(glyph.fImage, 0, glyph.rowBytes() * glyph.fHeight);
1143 FT_Outline_Get_Bitmap(gFTLibrary, outline, &target);
1144 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001145 } break;
1146
1147 case FT_GLYPH_FORMAT_BITMAP: {
agl@chromium.orge76073b2010-06-04 20:31:17 +00001148 if (fRec.fFlags & kEmbolden_Flag) {
1149 FT_GlyphSlot_Own_Bitmap(fFace->glyph);
1150 FT_Bitmap_Embolden(gFTLibrary, &fFace->glyph->bitmap, kBitmapEmboldenStrength, 0);
1151 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001152 SkASSERT_CONTINUE(glyph.fWidth == fFace->glyph->bitmap.width);
1153 SkASSERT_CONTINUE(glyph.fHeight == fFace->glyph->bitmap.rows);
1154 SkASSERT_CONTINUE(glyph.fTop == -fFace->glyph->bitmap_top);
1155 SkASSERT_CONTINUE(glyph.fLeft == fFace->glyph->bitmap_left);
1156
1157 const uint8_t* src = (const uint8_t*)fFace->glyph->bitmap.buffer;
1158 uint8_t* dst = (uint8_t*)glyph.fImage;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001159
agl@chromium.org558434a2009-08-11 17:22:38 +00001160 if (fFace->glyph->bitmap.pixel_mode == FT_PIXEL_MODE_GRAY ||
1161 (fFace->glyph->bitmap.pixel_mode == FT_PIXEL_MODE_MONO &&
1162 glyph.fMaskFormat == SkMask::kBW_Format)) {
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001163 unsigned srcRowBytes = fFace->glyph->bitmap.pitch;
1164 unsigned dstRowBytes = glyph.rowBytes();
1165 unsigned minRowBytes = SkMin32(srcRowBytes, dstRowBytes);
1166 unsigned extraRowBytes = dstRowBytes - minRowBytes;
1167
1168 for (int y = fFace->glyph->bitmap.rows - 1; y >= 0; --y) {
1169 memcpy(dst, src, minRowBytes);
1170 memset(dst + minRowBytes, 0, extraRowBytes);
1171 src += srcRowBytes;
1172 dst += dstRowBytes;
1173 }
agl@chromium.org558434a2009-08-11 17:22:38 +00001174 } else if (fFace->glyph->bitmap.pixel_mode == FT_PIXEL_MODE_MONO &&
reed@google.comeffc5012011-06-27 16:44:46 +00001175 glyph.fMaskFormat == SkMask::kA8_Format) {
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001176 for (int y = 0; y < fFace->glyph->bitmap.rows; ++y) {
1177 uint8_t byte = 0;
1178 int bits = 0;
1179 const uint8_t* src_row = src;
1180 uint8_t* dst_row = dst;
1181
1182 for (int x = 0; x < fFace->glyph->bitmap.width; ++x) {
1183 if (!bits) {
1184 byte = *src_row++;
1185 bits = 8;
1186 }
1187
1188 *dst_row++ = byte & 0x80 ? 0xff : 0;
1189 bits--;
1190 byte <<= 1;
1191 }
1192
1193 src += fFace->glyph->bitmap.pitch;
1194 dst += glyph.rowBytes();
1195 }
reed@google.com73824072011-06-23 13:17:30 +00001196 } else if (SkMask::kLCD16_Format == glyph.fMaskFormat) {
reed@google.comeffc5012011-06-27 16:44:46 +00001197 copyFT2LCD16(glyph, fFace->glyph->bitmap,
1198 fRec.fFlags & SkScalerContext::kLCD_BGROrder_Flag);
agl@chromium.org558434a2009-08-11 17:22:38 +00001199 } else {
reed@google.com73824072011-06-23 13:17:30 +00001200 SkASSERT(!"unknown glyph bitmap transform needed");
reed@android.com8a1c16f2008-12-17 15:59:43 +00001201 }
1202 } break;
1203
1204 default:
1205 SkASSERT(!"unknown glyph format");
1206 goto ERROR;
1207 }
1208}
1209
1210///////////////////////////////////////////////////////////////////////////////
1211
1212#define ft2sk(x) SkFixedToScalar((x) << 10)
1213
reed@android.com6f252972009-01-14 16:46:16 +00001214#if FREETYPE_MAJOR >= 2 && FREETYPE_MINOR >= 2
reed@android.com8a1c16f2008-12-17 15:59:43 +00001215 #define CONST_PARAM const
1216#else // older freetype doesn't use const here
1217 #define CONST_PARAM
1218#endif
1219
1220static int move_proc(CONST_PARAM FT_Vector* pt, void* ctx) {
1221 SkPath* path = (SkPath*)ctx;
1222 path->close(); // to close the previous contour (if any)
1223 path->moveTo(ft2sk(pt->x), -ft2sk(pt->y));
1224 return 0;
1225}
1226
1227static int line_proc(CONST_PARAM FT_Vector* pt, void* ctx) {
1228 SkPath* path = (SkPath*)ctx;
1229 path->lineTo(ft2sk(pt->x), -ft2sk(pt->y));
1230 return 0;
1231}
1232
1233static int quad_proc(CONST_PARAM FT_Vector* pt0, CONST_PARAM FT_Vector* pt1,
1234 void* ctx) {
1235 SkPath* path = (SkPath*)ctx;
1236 path->quadTo(ft2sk(pt0->x), -ft2sk(pt0->y), ft2sk(pt1->x), -ft2sk(pt1->y));
1237 return 0;
1238}
1239
1240static int cubic_proc(CONST_PARAM FT_Vector* pt0, CONST_PARAM FT_Vector* pt1,
1241 CONST_PARAM FT_Vector* pt2, void* ctx) {
1242 SkPath* path = (SkPath*)ctx;
1243 path->cubicTo(ft2sk(pt0->x), -ft2sk(pt0->y), ft2sk(pt1->x),
1244 -ft2sk(pt1->y), ft2sk(pt2->x), -ft2sk(pt2->y));
1245 return 0;
1246}
1247
1248void SkScalerContext_FreeType::generatePath(const SkGlyph& glyph,
1249 SkPath* path) {
1250 SkAutoMutexAcquire ac(gFTMutex);
1251
1252 SkASSERT(&glyph && path);
1253
1254 if (this->setupSize()) {
1255 path->reset();
1256 return;
1257 }
1258
1259 uint32_t flags = fLoadGlyphFlags;
1260 flags |= FT_LOAD_NO_BITMAP; // ignore embedded bitmaps so we're sure to get the outline
1261 flags &= ~FT_LOAD_RENDER; // don't scan convert (we just want the outline)
1262
1263 FT_Error err = FT_Load_Glyph( fFace, glyph.getGlyphID(fBaseGlyphCount), flags);
1264
1265 if (err != 0) {
1266 SkDEBUGF(("SkScalerContext_FreeType::generatePath: FT_Load_Glyph(glyph:%d flags:%d) returned 0x%x\n",
1267 glyph.getGlyphID(fBaseGlyphCount), flags, err));
1268 path->reset();
1269 return;
1270 }
1271
senorblanco@chromium.org4526a842010-02-05 23:08:20 +00001272 if (fRec.fFlags & kEmbolden_Flag) {
1273 emboldenOutline(&fFace->glyph->outline);
1274 }
1275
reed@android.com8a1c16f2008-12-17 15:59:43 +00001276 FT_Outline_Funcs funcs;
1277
1278 funcs.move_to = move_proc;
1279 funcs.line_to = line_proc;
1280 funcs.conic_to = quad_proc;
1281 funcs.cubic_to = cubic_proc;
1282 funcs.shift = 0;
1283 funcs.delta = 0;
1284
1285 err = FT_Outline_Decompose(&fFace->glyph->outline, &funcs, path);
1286
1287 if (err != 0) {
1288 SkDEBUGF(("SkScalerContext_FreeType::generatePath: FT_Load_Glyph(glyph:%d flags:%d) returned 0x%x\n",
1289 glyph.getGlyphID(fBaseGlyphCount), flags, err));
1290 path->reset();
1291 return;
1292 }
1293
1294 path->close();
1295}
1296
1297void SkScalerContext_FreeType::generateFontMetrics(SkPaint::FontMetrics* mx,
1298 SkPaint::FontMetrics* my) {
1299 if (NULL == mx && NULL == my) {
1300 return;
1301 }
1302
1303 SkAutoMutexAcquire ac(gFTMutex);
1304
1305 if (this->setupSize()) {
reed@android.coma8a8b8b2009-05-04 15:00:11 +00001306 ERROR:
reed@android.com8a1c16f2008-12-17 15:59:43 +00001307 if (mx) {
reed@android.com4516f472009-06-29 16:25:36 +00001308 sk_bzero(mx, sizeof(SkPaint::FontMetrics));
reed@android.com8a1c16f2008-12-17 15:59:43 +00001309 }
1310 if (my) {
reed@android.com4516f472009-06-29 16:25:36 +00001311 sk_bzero(my, sizeof(SkPaint::FontMetrics));
reed@android.com8a1c16f2008-12-17 15:59:43 +00001312 }
1313 return;
1314 }
1315
reed@android.coma8a8b8b2009-05-04 15:00:11 +00001316 FT_Face face = fFace;
1317 int upem = face->units_per_EM;
1318 if (upem <= 0) {
1319 goto ERROR;
1320 }
1321
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001322 SkPoint pts[6];
1323 SkFixed ys[6];
reed@android.com8a1c16f2008-12-17 15:59:43 +00001324 SkFixed scaleY = fScaleY;
1325 SkFixed mxy = fMatrix22.xy;
1326 SkFixed myy = fMatrix22.yy;
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001327 SkScalar xmin = SkIntToScalar(face->bbox.xMin) / upem;
1328 SkScalar xmax = SkIntToScalar(face->bbox.xMax) / upem;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001329
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001330 int leading = face->height - (face->ascender + -face->descender);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001331 if (leading < 0) {
1332 leading = 0;
1333 }
1334
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001335 // Try to get the OS/2 table from the font. This contains the specific
1336 // average font width metrics which Windows uses.
1337 TT_OS2* os2 = (TT_OS2*) FT_Get_Sfnt_Table(face, ft_sfnt_os2);
1338
reed@android.com8a1c16f2008-12-17 15:59:43 +00001339 ys[0] = -face->bbox.yMax;
1340 ys[1] = -face->ascender;
1341 ys[2] = -face->descender;
1342 ys[3] = -face->bbox.yMin;
1343 ys[4] = leading;
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001344 ys[5] = os2 ? os2->xAvgCharWidth : 0;
1345
1346 SkScalar x_height;
1347 if (os2 && os2->sxHeight) {
1348 x_height = SkFixedToScalar(SkMulDiv(fScaleX, os2->sxHeight, upem));
1349 } else {
1350 const FT_UInt x_glyph = FT_Get_Char_Index(fFace, 'x');
1351 if (x_glyph) {
1352 FT_BBox bbox;
1353 FT_Load_Glyph(fFace, x_glyph, fLoadGlyphFlags);
senorblanco@chromium.org4526a842010-02-05 23:08:20 +00001354 if (fRec.fFlags & kEmbolden_Flag) {
1355 emboldenOutline(&fFace->glyph->outline);
1356 }
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001357 FT_Outline_Get_CBox(&fFace->glyph->outline, &bbox);
1358 x_height = SkIntToScalar(bbox.yMax) / 64;
1359 } else {
1360 x_height = 0;
1361 }
1362 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001363
1364 // convert upem-y values into scalar points
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001365 for (int i = 0; i < 6; i++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001366 SkFixed y = SkMulDiv(scaleY, ys[i], upem);
1367 SkFixed x = SkFixedMul(mxy, y);
1368 y = SkFixedMul(myy, y);
1369 pts[i].set(SkFixedToScalar(x), SkFixedToScalar(y));
1370 }
1371
1372 if (mx) {
1373 mx->fTop = pts[0].fX;
1374 mx->fAscent = pts[1].fX;
1375 mx->fDescent = pts[2].fX;
1376 mx->fBottom = pts[3].fX;
1377 mx->fLeading = pts[4].fX;
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001378 mx->fAvgCharWidth = pts[5].fX;
1379 mx->fXMin = xmin;
1380 mx->fXMax = xmax;
1381 mx->fXHeight = x_height;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001382 }
1383 if (my) {
1384 my->fTop = pts[0].fY;
1385 my->fAscent = pts[1].fY;
1386 my->fDescent = pts[2].fY;
1387 my->fBottom = pts[3].fY;
1388 my->fLeading = pts[4].fY;
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001389 my->fAvgCharWidth = pts[5].fY;
1390 my->fXMin = xmin;
1391 my->fXMax = xmax;
1392 my->fXHeight = x_height;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001393 }
1394}
1395
1396////////////////////////////////////////////////////////////////////////
1397////////////////////////////////////////////////////////////////////////
1398
1399SkScalerContext* SkFontHost::CreateScalerContext(const SkDescriptor* desc) {
reed@android.com62900b42009-02-11 15:07:19 +00001400 SkScalerContext_FreeType* c = SkNEW_ARGS(SkScalerContext_FreeType, (desc));
1401 if (!c->success()) {
1402 SkDELETE(c);
1403 c = NULL;
1404 }
1405 return c;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001406}
1407
1408///////////////////////////////////////////////////////////////////////////////
1409
1410/* Export this so that other parts of our FonttHost port can make use of our
1411 ability to extract the name+style from a stream, using FreeType's api.
1412*/
reed@google.com5b31b0f2011-02-23 14:41:42 +00001413SkTypeface::Style find_name_and_attributes(SkStream* stream, SkString* name,
1414 bool* isFixedWidth) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001415 FT_Library library;
reed@android.combfbd4ff2009-07-23 17:44:41 +00001416 if (FT_Init_FreeType(&library)) {
djsollen@google.com7b34ea62011-02-24 16:28:51 +00001417 name->reset();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001418 return SkTypeface::kNormal;
1419 }
1420
1421 FT_Open_Args args;
1422 memset(&args, 0, sizeof(args));
1423
1424 const void* memoryBase = stream->getMemoryBase();
1425 FT_StreamRec streamRec;
1426
1427 if (NULL != memoryBase) {
1428 args.flags = FT_OPEN_MEMORY;
1429 args.memory_base = (const FT_Byte*)memoryBase;
1430 args.memory_size = stream->getLength();
1431 } else {
1432 memset(&streamRec, 0, sizeof(streamRec));
1433 streamRec.size = stream->read(NULL, 0);
1434 streamRec.descriptor.pointer = stream;
1435 streamRec.read = sk_stream_read;
1436 streamRec.close = sk_stream_close;
1437
1438 args.flags = FT_OPEN_STREAM;
1439 args.stream = &streamRec;
1440 }
1441
1442 FT_Face face;
1443 if (FT_Open_Face(library, &args, 0, &face)) {
1444 FT_Done_FreeType(library);
djsollen@google.com7b34ea62011-02-24 16:28:51 +00001445 name->reset();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001446 return SkTypeface::kNormal;
1447 }
1448
1449 name->set(face->family_name);
1450 int style = SkTypeface::kNormal;
1451
1452 if (face->style_flags & FT_STYLE_FLAG_BOLD) {
1453 style |= SkTypeface::kBold;
1454 }
1455 if (face->style_flags & FT_STYLE_FLAG_ITALIC) {
1456 style |= SkTypeface::kItalic;
1457 }
reed@google.com5b31b0f2011-02-23 14:41:42 +00001458 if (isFixedWidth) {
1459 *isFixedWidth = FT_IS_FIXED_WIDTH(face);
1460 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001461
1462 FT_Done_Face(face);
1463 FT_Done_FreeType(library);
1464 return (SkTypeface::Style)style;
1465}