blob: 09390151fb4a74b7782fe0c20f205f3027ec677b [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.org37ad8fb2011-08-18 02:38:50 +0000411 SkAdvancedTypefaceMetrics::PerGlyphInfo perGlyphInfo,
412 const uint32_t* glyphIDs,
413 uint32_t glyphIDsCount) {
djsollen@google.comcd9d69b2011-03-14 20:30:14 +0000414#if defined(SK_BUILD_FOR_MAC) || defined(ANDROID)
reed@google.com8a5d6922011-03-14 15:08:03 +0000415 return NULL;
416#else
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000417 SkAutoMutexAcquire ac(gFTMutex);
418 FT_Library libInit = NULL;
419 if (gFTCount == 0) {
420 if (!InitFreetype())
421 sk_throw();
422 libInit = gFTLibrary;
423 }
424 SkAutoTCallIProc<struct FT_LibraryRec_, FT_Done_FreeType> ftLib(libInit);
425 SkFaceRec* rec = ref_ft_face(fontID);
426 if (NULL == rec)
427 return NULL;
428 FT_Face face = rec->fFace;
429
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000430 SkAdvancedTypefaceMetrics* info = new SkAdvancedTypefaceMetrics;
431 info->fFontName.set(FT_Get_Postscript_Name(face));
432 info->fMultiMaster = FT_HAS_MULTIPLE_MASTERS(face);
433 info->fLastGlyphID = face->num_glyphs - 1;
434 info->fEmSize = 1000;
435
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000436 bool cid = false;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000437 const char* fontType = FT_Get_X11_Font_Format(face);
vandebo@chromium.orgc3a2ae52011-02-03 21:48:23 +0000438 if (strcmp(fontType, "Type 1") == 0) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000439 info->fType = SkAdvancedTypefaceMetrics::kType1_Font;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000440 } else if (strcmp(fontType, "CID Type 1") == 0) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000441 info->fType = SkAdvancedTypefaceMetrics::kType1CID_Font;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000442 cid = true;
443 } else if (strcmp(fontType, "CFF") == 0) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000444 info->fType = SkAdvancedTypefaceMetrics::kCFF_Font;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000445 } else if (strcmp(fontType, "TrueType") == 0) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000446 info->fType = SkAdvancedTypefaceMetrics::kTrueType_Font;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000447 cid = true;
448 TT_Header* ttHeader;
449 if ((ttHeader = (TT_Header*)FT_Get_Sfnt_Table(face,
450 ft_sfnt_head)) != NULL) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000451 info->fEmSize = ttHeader->Units_Per_EM;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000452 }
453 }
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000454
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000455 info->fStyle = 0;
456 if (FT_IS_FIXED_WIDTH(face))
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000457 info->fStyle |= SkAdvancedTypefaceMetrics::kFixedPitch_Style;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000458 if (face->style_flags & FT_STYLE_FLAG_ITALIC)
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000459 info->fStyle |= SkAdvancedTypefaceMetrics::kItalic_Style;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000460 // We should set either Symbolic or Nonsymbolic; Nonsymbolic if the font's
461 // character set is a subset of 'Adobe standard Latin.'
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000462 info->fStyle |= SkAdvancedTypefaceMetrics::kSymbolic_Style;
463
464 PS_FontInfoRec ps_info;
465 TT_Postscript* tt_info;
466 if (FT_Get_PS_Font_Info(face, &ps_info) == 0) {
467 info->fItalicAngle = ps_info.italic_angle;
468 } else if ((tt_info =
469 (TT_Postscript*)FT_Get_Sfnt_Table(face,
470 ft_sfnt_post)) != NULL) {
471 info->fItalicAngle = SkFixedToScalar(tt_info->italicAngle);
472 } else {
473 info->fItalicAngle = 0;
474 }
475
476 info->fAscent = face->ascender;
477 info->fDescent = face->descender;
478
479 // Figure out a good guess for StemV - Min width of i, I, !, 1.
480 // This probably isn't very good with an italic font.
481 int16_t min_width = SHRT_MAX;
vandebo@chromium.org6f72d1e2011-02-14 23:19:59 +0000482 info->fStemV = 0;
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000483 char stem_chars[] = {'i', 'I', '!', '1'};
484 for (size_t i = 0; i < SK_ARRAY_COUNT(stem_chars); i++) {
485 FT_BBox bbox;
486 if (GetLetterCBox(face, stem_chars[i], &bbox)) {
487 int16_t width = bbox.xMax - bbox.xMin;
488 if (width > 0 && width < min_width) {
489 min_width = width;
490 info->fStemV = min_width;
491 }
492 }
493 }
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000494
495 TT_PCLT* pclt_info;
496 TT_OS2* os2_table;
497 if ((pclt_info = (TT_PCLT*)FT_Get_Sfnt_Table(face, ft_sfnt_pclt)) != NULL) {
498 info->fCapHeight = pclt_info->CapHeight;
499 uint8_t serif_style = pclt_info->SerifStyle & 0x3F;
500 if (serif_style >= 2 && serif_style <= 6)
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000501 info->fStyle |= SkAdvancedTypefaceMetrics::kSerif_Style;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000502 else if (serif_style >= 9 && serif_style <= 12)
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000503 info->fStyle |= SkAdvancedTypefaceMetrics::kScript_Style;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000504 } else if ((os2_table =
505 (TT_OS2*)FT_Get_Sfnt_Table(face, ft_sfnt_os2)) != NULL) {
506 info->fCapHeight = os2_table->sCapHeight;
507 } else {
508 // Figure out a good guess for CapHeight: average the height of M and X.
509 FT_BBox m_bbox, x_bbox;
510 bool got_m, got_x;
511 got_m = GetLetterCBox(face, 'M', &m_bbox);
512 got_x = GetLetterCBox(face, 'X', &x_bbox);
513 if (got_m && got_x) {
514 info->fCapHeight = (m_bbox.yMax - m_bbox.yMin + x_bbox.yMax -
515 x_bbox.yMin) / 2;
516 } else if (got_m && !got_x) {
517 info->fCapHeight = m_bbox.yMax - m_bbox.yMin;
518 } else if (!got_m && got_x) {
519 info->fCapHeight = x_bbox.yMax - x_bbox.yMin;
520 }
521 }
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000522
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000523 info->fBBox = SkIRect::MakeLTRB(face->bbox.xMin, face->bbox.yMax,
524 face->bbox.xMax, face->bbox.yMin);
525
vandebo@chromium.org37ad8fb2011-08-18 02:38:50 +0000526 if (!canEmbed(face) || !FT_IS_SCALABLE(face) ||
vandebo@chromium.org325cb9a2011-03-30 18:36:29 +0000527 info->fType == SkAdvancedTypefaceMetrics::kOther_Font) {
528 perGlyphInfo = SkAdvancedTypefaceMetrics::kNo_PerGlyphInfo;
529 }
530
531 if (perGlyphInfo & SkAdvancedTypefaceMetrics::kHAdvance_PerGlyphInfo) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000532 if (FT_IS_FIXED_WIDTH(face)) {
533 appendRange(&info->fGlyphWidths, 0);
534 int16_t advance = face->max_advance_width;
535 info->fGlyphWidths->fAdvance.append(1, &advance);
536 finishRange(info->fGlyphWidths.get(), 0,
537 SkAdvancedTypefaceMetrics::WidthRange::kDefault);
vandebo@chromium.org6f72d1e2011-02-14 23:19:59 +0000538 } else if (!cid) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000539 appendRange(&info->fGlyphWidths, 0);
540 // So as to not blow out the stack, get advances in batches.
541 for (int gID = 0; gID < face->num_glyphs; gID += 128) {
542 FT_Fixed advances[128];
543 int advanceCount = 128;
544 if (gID + advanceCount > face->num_glyphs)
545 advanceCount = face->num_glyphs - gID + 1;
546 getAdvances(face, gID, advanceCount, FT_LOAD_NO_SCALE,
547 advances);
548 for (int i = 0; i < advanceCount; i++) {
549 int16_t advance = advances[gID + i];
550 info->fGlyphWidths->fAdvance.append(1, &advance);
551 }
552 }
553 finishRange(info->fGlyphWidths.get(), face->num_glyphs - 1,
554 SkAdvancedTypefaceMetrics::WidthRange::kRange);
555 } else {
vandebo@chromium.org6f72d1e2011-02-14 23:19:59 +0000556 info->fGlyphWidths.reset(
vandebo@chromium.org37ad8fb2011-08-18 02:38:50 +0000557 getAdvanceData(face,
558 face->num_glyphs,
559 glyphIDs,
560 glyphIDsCount,
561 &getWidthAdvance));
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000562 }
vandebo@chromium.org325cb9a2011-03-30 18:36:29 +0000563 }
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000564
vandebo@chromium.org325cb9a2011-03-30 18:36:29 +0000565 if (perGlyphInfo & SkAdvancedTypefaceMetrics::kVAdvance_PerGlyphInfo &&
566 FT_HAS_VERTICAL(face)) {
567 SkASSERT(false); // Not implemented yet.
568 }
569
570 if (perGlyphInfo & SkAdvancedTypefaceMetrics::kGlyphNames_PerGlyphInfo &&
571 info->fType == SkAdvancedTypefaceMetrics::kType1_Font) {
572 // Postscript fonts may contain more than 255 glyphs, so we end up
573 // using multiple font descriptions with a glyph ordering. Record
574 // the name of each glyph.
575 info->fGlyphNames.reset(
576 new SkAutoTArray<SkString>(face->num_glyphs));
577 for (int gID = 0; gID < face->num_glyphs; gID++) {
578 char glyphName[128]; // PS limit for names is 127 bytes.
579 FT_Get_Glyph_Name(face, gID, glyphName, 128);
580 info->fGlyphNames->get()[gID].set(glyphName);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000581 }
582 }
583
vandebo@chromium.org6744d492011-05-09 18:13:47 +0000584 if (perGlyphInfo & SkAdvancedTypefaceMetrics::kToUnicode_PerGlyphInfo &&
585 info->fType != SkAdvancedTypefaceMetrics::kType1_Font &&
586 face->num_charmaps) {
587 populate_glyph_to_unicode(face, &(info->fGlyphToUnicode));
588 }
589
vandebo@chromium.orgc3a2ae52011-02-03 21:48:23 +0000590 if (!canEmbed(face))
591 info->fType = SkAdvancedTypefaceMetrics::kNotEmbeddable_Font;
592
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000593 unref_ft_face(face);
594 return info;
reed@google.com8a5d6922011-03-14 15:08:03 +0000595#endif
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000596}
vandebo@chromium.org37ad8fb2011-08-18 02:38:50 +0000597
reed@google.com618ef5e2011-01-26 22:10:41 +0000598///////////////////////////////////////////////////////////////////////////
599
reed@google.com8ed436c2011-07-21 14:12:36 +0000600static bool bothZero(SkScalar a, SkScalar b) {
601 return 0 == a && 0 == b;
602}
603
604// returns false if there is any non-90-rotation or skew
605static bool isAxisAligned(const SkScalerContext::Rec& rec) {
606 return 0 == rec.fPreSkewX &&
607 (bothZero(rec.fPost2x2[0][1], rec.fPost2x2[1][0]) ||
608 bothZero(rec.fPost2x2[0][0], rec.fPost2x2[1][1]));
609}
610
reed@google.com618ef5e2011-01-26 22:10:41 +0000611void SkFontHost::FilterRec(SkScalerContext::Rec* rec) {
612 if (!gLCDSupportValid) {
613 InitFreetype();
614 FT_Done_FreeType(gFTLibrary);
615 }
reed@google.com5b31b0f2011-02-23 14:41:42 +0000616
reed@google.comeffc5012011-06-27 16:44:46 +0000617 if (!gLCDSupport && isLCD(*rec)) {
reed@google.com618ef5e2011-01-26 22:10:41 +0000618 // If the runtime Freetype library doesn't support LCD mode, we disable
619 // it here.
620 rec->fMaskFormat = SkMask::kA8_Format;
621 }
reed@google.com5b31b0f2011-02-23 14:41:42 +0000622
reed@google.com618ef5e2011-01-26 22:10:41 +0000623 SkPaint::Hinting h = rec->getHinting();
reed@google.comeffc5012011-06-27 16:44:46 +0000624 if (SkPaint::kFull_Hinting == h && !isLCD(*rec)) {
reed@google.com618ef5e2011-01-26 22:10:41 +0000625 // collapse full->normal hinting if we're not doing LCD
626 h = SkPaint::kNormal_Hinting;
627 } else if ((rec->fFlags & SkScalerContext::kSubpixelPositioning_Flag) &&
628 SkPaint::kNo_Hinting != h) {
629 // to do subpixel, we must have at most slight hinting
630 h = SkPaint::kSlight_Hinting;
631 }
vandebo@chromium.org37ad8fb2011-08-18 02:38:50 +0000632#ifndef SK_IGNORE_ROTATED_FREETYPE_FIX
reed@google.com8ed436c2011-07-21 14:12:36 +0000633 // rotated text looks bad with hinting, so we disable it as needed
634 if (!isAxisAligned(*rec)) {
635 h = SkPaint::kNo_Hinting;
636 }
bsalomon@google.com0e35ca82011-07-22 17:56:19 +0000637#endif
reed@google.com618ef5e2011-01-26 22:10:41 +0000638 rec->setHinting(h);
639}
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000640
djsollen@google.comcd9d69b2011-03-14 20:30:14 +0000641#ifdef ANDROID
642uint32_t SkFontHost::GetUnitsPerEm(SkFontID fontID) {
643 SkAutoMutexAcquire ac(gFTMutex);
644 SkFaceRec *rec = ref_ft_face(fontID);
645 uint16_t unitsPerEm = 0;
646
647 if (rec != NULL && rec->fFace != NULL) {
648 unitsPerEm = rec->fFace->units_per_EM;
649 unref_ft_face(rec->fFace);
650 }
651
652 return (uint32_t)unitsPerEm;
653}
654#endif
655
reed@android.com8a1c16f2008-12-17 15:59:43 +0000656SkScalerContext_FreeType::SkScalerContext_FreeType(const SkDescriptor* desc)
reed@android.com62900b42009-02-11 15:07:19 +0000657 : SkScalerContext(desc) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000658 SkAutoMutexAcquire ac(gFTMutex);
659
reed@android.com8a1c16f2008-12-17 15:59:43 +0000660 if (gFTCount == 0) {
reed@android.com659aaf92009-07-23 15:20:21 +0000661 if (!InitFreetype()) {
662 sk_throw();
663 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000664 }
665 ++gFTCount;
666
667 // load the font file
reed@android.com62900b42009-02-11 15:07:19 +0000668 fFTSize = NULL;
669 fFace = NULL;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000670 fFaceRec = ref_ft_face(fRec.fFontID);
reed@android.com62900b42009-02-11 15:07:19 +0000671 if (NULL == fFaceRec) {
672 return;
673 }
674 fFace = fFaceRec->fFace;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000675
676 // compute our factors from the record
677
678 SkMatrix m;
679
680 fRec.getSingleMatrix(&m);
681
682#ifdef DUMP_STRIKE_CREATION
683 SkString keyString;
684 SkFontHost::GetDescriptorKeyString(desc, &keyString);
685 printf("========== strike [%g %g %g] [%g %g %g %g] hints %d format %d %s\n", SkScalarToFloat(fRec.fTextSize),
686 SkScalarToFloat(fRec.fPreScaleX), SkScalarToFloat(fRec.fPreSkewX),
687 SkScalarToFloat(fRec.fPost2x2[0][0]), SkScalarToFloat(fRec.fPost2x2[0][1]),
688 SkScalarToFloat(fRec.fPost2x2[1][0]), SkScalarToFloat(fRec.fPost2x2[1][1]),
agl@chromium.org309485b2009-07-21 17:41:32 +0000689 fRec.getHinting(), fRec.fMaskFormat, keyString.c_str());
reed@android.com8a1c16f2008-12-17 15:59:43 +0000690#endif
691
692 // now compute our scale factors
693 SkScalar sx = m.getScaleX();
694 SkScalar sy = m.getScaleY();
695
696 if (m.getSkewX() || m.getSkewY() || sx < 0 || sy < 0) {
697 // sort of give up on hinting
698 sx = SkMaxScalar(SkScalarAbs(sx), SkScalarAbs(m.getSkewX()));
699 sy = SkMaxScalar(SkScalarAbs(m.getSkewY()), SkScalarAbs(sy));
700 sx = sy = SkScalarAve(sx, sy);
701
702 SkScalar inv = SkScalarInvert(sx);
703
704 // flip the skew elements to go from our Y-down system to FreeType's
705 fMatrix22.xx = SkScalarToFixed(SkScalarMul(m.getScaleX(), inv));
706 fMatrix22.xy = -SkScalarToFixed(SkScalarMul(m.getSkewX(), inv));
707 fMatrix22.yx = -SkScalarToFixed(SkScalarMul(m.getSkewY(), inv));
708 fMatrix22.yy = SkScalarToFixed(SkScalarMul(m.getScaleY(), inv));
709 } else {
710 fMatrix22.xx = fMatrix22.yy = SK_Fixed1;
711 fMatrix22.xy = fMatrix22.yx = 0;
712 }
713
714 fScaleX = SkScalarToFixed(sx);
715 fScaleY = SkScalarToFixed(sy);
716
717 // compute the flags we send to Load_Glyph
718 {
reed@android.come4d0bc02009-07-24 19:53:20 +0000719 FT_Int32 loadFlags = FT_LOAD_DEFAULT;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000720
agl@chromium.org70a303f2010-05-10 14:15:50 +0000721 if (SkMask::kBW_Format == fRec.fMaskFormat) {
722 // See http://code.google.com/p/chromium/issues/detail?id=43252#c24
723 loadFlags = FT_LOAD_TARGET_MONO;
reed@google.comeffc5012011-06-27 16:44:46 +0000724 if (fRec.getHinting() == SkPaint::kNo_Hinting) {
agl@chromium.org70a303f2010-05-10 14:15:50 +0000725 loadFlags = FT_LOAD_NO_HINTING;
reed@google.comeffc5012011-06-27 16:44:46 +0000726 }
agl@chromium.org70a303f2010-05-10 14:15:50 +0000727 } else {
728 switch (fRec.getHinting()) {
729 case SkPaint::kNo_Hinting:
730 loadFlags = FT_LOAD_NO_HINTING;
731 break;
732 case SkPaint::kSlight_Hinting:
733 loadFlags = FT_LOAD_TARGET_LIGHT; // This implies FORCE_AUTOHINT
734 break;
735 case SkPaint::kNormal_Hinting:
agl@chromium.orga2c71cb2010-06-17 20:49:17 +0000736 if (fRec.fFlags & SkScalerContext::kAutohinting_Flag)
737 loadFlags = FT_LOAD_FORCE_AUTOHINT;
738 else
739 loadFlags = FT_LOAD_NO_AUTOHINT;
agl@chromium.org70a303f2010-05-10 14:15:50 +0000740 break;
741 case SkPaint::kFull_Hinting:
agl@chromium.orga2c71cb2010-06-17 20:49:17 +0000742 if (fRec.fFlags & SkScalerContext::kAutohinting_Flag) {
743 loadFlags = FT_LOAD_FORCE_AUTOHINT;
744 break;
745 }
agl@chromium.org70a303f2010-05-10 14:15:50 +0000746 loadFlags = FT_LOAD_TARGET_NORMAL;
reed@google.comeffc5012011-06-27 16:44:46 +0000747 if (isLCD(fRec)) {
748 if (fRec.fFlags & SkScalerContext::kLCD_Vertical_Flag) {
749 loadFlags = FT_LOAD_TARGET_LCD_V;
750 } else {
751 loadFlags = FT_LOAD_TARGET_LCD;
752 }
reed@google.comea2333d2011-03-14 16:44:56 +0000753 }
agl@chromium.org70a303f2010-05-10 14:15:50 +0000754 break;
755 default:
756 SkDebugf("---------- UNKNOWN hinting %d\n", fRec.getHinting());
757 break;
758 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000759 }
760
reed@google.comeffc5012011-06-27 16:44:46 +0000761 if ((fRec.fFlags & SkScalerContext::kEmbeddedBitmapText_Flag) == 0) {
agl@chromium.orge0d08992009-08-07 19:19:23 +0000762 loadFlags |= FT_LOAD_NO_BITMAP;
reed@google.comeffc5012011-06-27 16:44:46 +0000763 }
agl@chromium.orge0d08992009-08-07 19:19:23 +0000764
reed@google.com96a9f7912011-05-06 11:49:30 +0000765 // Always using FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH to get correct
766 // advances, as fontconfig and cairo do.
767 // See http://code.google.com/p/skia/issues/detail?id=222.
768 loadFlags |= FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH;
769
reed@android.come4d0bc02009-07-24 19:53:20 +0000770 fLoadGlyphFlags = loadFlags;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000771 }
772
773 // now create the FT_Size
774
775 {
776 FT_Error err;
777
778 err = FT_New_Size(fFace, &fFTSize);
779 if (err != 0) {
780 SkDEBUGF(("SkScalerContext_FreeType::FT_New_Size(%x): FT_Set_Char_Size(0x%x, 0x%x) returned 0x%x\n",
781 fFaceRec->fFontID, fScaleX, fScaleY, err));
782 fFace = NULL;
783 return;
784 }
785
786 err = FT_Activate_Size(fFTSize);
787 if (err != 0) {
788 SkDEBUGF(("SkScalerContext_FreeType::FT_Activate_Size(%x, 0x%x, 0x%x) returned 0x%x\n",
789 fFaceRec->fFontID, fScaleX, fScaleY, err));
790 fFTSize = NULL;
791 }
792
793 err = FT_Set_Char_Size( fFace,
794 SkFixedToFDot6(fScaleX), SkFixedToFDot6(fScaleY),
795 72, 72);
796 if (err != 0) {
797 SkDEBUGF(("SkScalerContext_FreeType::FT_Set_Char_Size(%x, 0x%x, 0x%x) returned 0x%x\n",
798 fFaceRec->fFontID, fScaleX, fScaleY, err));
799 fFace = NULL;
800 return;
801 }
802
803 FT_Set_Transform( fFace, &fMatrix22, NULL);
804 }
805}
806
807SkScalerContext_FreeType::~SkScalerContext_FreeType() {
808 if (fFTSize != NULL) {
809 FT_Done_Size(fFTSize);
810 }
811
812 SkAutoMutexAcquire ac(gFTMutex);
813
814 if (fFace != NULL) {
815 unref_ft_face(fFace);
816 }
817 if (--gFTCount == 0) {
818// SkDEBUGF(("FT_Done_FreeType\n"));
819 FT_Done_FreeType(gFTLibrary);
820 SkDEBUGCODE(gFTLibrary = NULL;)
821 }
822}
823
824/* We call this before each use of the fFace, since we may be sharing
825 this face with other context (at different sizes).
826*/
827FT_Error SkScalerContext_FreeType::setupSize() {
828 /* In the off-chance that a font has been removed, we want to error out
829 right away, so call resolve just to be sure.
830
831 TODO: perhaps we can skip this, by walking the global font cache and
832 killing all of the contexts when we know that a given fontID is going
833 away...
834 */
reed@android.comb1d9d2e2009-03-04 17:37:51 +0000835 if (!SkFontHost::ValidFontID(fRec.fFontID)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000836 return (FT_Error)-1;
837 }
838
839 FT_Error err = FT_Activate_Size(fFTSize);
840
841 if (err != 0) {
842 SkDEBUGF(("SkScalerContext_FreeType::FT_Activate_Size(%x, 0x%x, 0x%x) returned 0x%x\n",
843 fFaceRec->fFontID, fScaleX, fScaleY, err));
844 fFTSize = NULL;
845 } else {
846 // seems we need to reset this every time (not sure why, but without it
847 // I get random italics from some other fFTSize)
848 FT_Set_Transform( fFace, &fMatrix22, NULL);
849 }
850 return err;
851}
852
senorblanco@chromium.org4526a842010-02-05 23:08:20 +0000853void SkScalerContext_FreeType::emboldenOutline(FT_Outline* outline) {
854 FT_Pos strength;
855 strength = FT_MulFix(fFace->units_per_EM, fFace->size->metrics.y_scale)
856 / 24;
857 FT_Outline_Embolden(outline, strength);
858}
859
ctguil@chromium.org0bc7bf52011-03-04 19:04:57 +0000860unsigned SkScalerContext_FreeType::generateGlyphCount() {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000861 return fFace->num_glyphs;
862}
863
864uint16_t SkScalerContext_FreeType::generateCharToGlyph(SkUnichar uni) {
865 return SkToU16(FT_Get_Char_Index( fFace, uni ));
866}
867
reed@android.com9d3a9852010-01-08 14:07:42 +0000868SkUnichar SkScalerContext_FreeType::generateGlyphToChar(uint16_t glyph) {
869 // iterate through each cmap entry, looking for matching glyph indices
870 FT_UInt glyphIndex;
871 SkUnichar charCode = FT_Get_First_Char( fFace, &glyphIndex );
872
873 while (glyphIndex != 0) {
874 if (glyphIndex == glyph) {
875 return charCode;
876 }
877 charCode = FT_Get_Next_Char( fFace, charCode, &glyphIndex );
878 }
879
880 return 0;
881}
882
reed@android.com8a1c16f2008-12-17 15:59:43 +0000883static FT_Pixel_Mode compute_pixel_mode(SkMask::Format format) {
884 switch (format) {
885 case SkMask::kBW_Format:
886 return FT_PIXEL_MODE_MONO;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000887 case SkMask::kA8_Format:
888 default:
889 return FT_PIXEL_MODE_GRAY;
890 }
891}
892
reed@android.com8a1c16f2008-12-17 15:59:43 +0000893void SkScalerContext_FreeType::generateAdvance(SkGlyph* glyph) {
894#ifdef FT_ADVANCES_H
895 /* unhinted and light hinted text have linearly scaled advances
896 * which are very cheap to compute with some font formats...
897 */
898 {
899 SkAutoMutexAcquire ac(gFTMutex);
900
901 if (this->setupSize()) {
reed@android.com62900b42009-02-11 15:07:19 +0000902 glyph->zeroMetrics();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000903 return;
904 }
905
906 FT_Error error;
907 FT_Fixed advance;
908
909 error = FT_Get_Advance( fFace, glyph->getGlyphID(fBaseGlyphCount),
910 fLoadGlyphFlags | FT_ADVANCE_FLAG_FAST_ONLY,
911 &advance );
912 if (0 == error) {
913 glyph->fRsbDelta = 0;
914 glyph->fLsbDelta = 0;
915 glyph->fAdvanceX = advance; // advance *2/3; //DEBUG
916 glyph->fAdvanceY = 0;
917 return;
918 }
919 }
920#endif /* FT_ADVANCES_H */
921 /* otherwise, we need to load/hint the glyph, which is slower */
922 this->generateMetrics(glyph);
923 return;
924}
925
926void SkScalerContext_FreeType::generateMetrics(SkGlyph* glyph) {
927 SkAutoMutexAcquire ac(gFTMutex);
928
929 glyph->fRsbDelta = 0;
930 glyph->fLsbDelta = 0;
931
932 FT_Error err;
933
934 if (this->setupSize()) {
935 goto ERROR;
936 }
937
938 err = FT_Load_Glyph( fFace, glyph->getGlyphID(fBaseGlyphCount), fLoadGlyphFlags );
939 if (err != 0) {
940 SkDEBUGF(("SkScalerContext_FreeType::generateMetrics(%x): FT_Load_Glyph(glyph:%d flags:%d) returned 0x%x\n",
941 fFaceRec->fFontID, glyph->getGlyphID(fBaseGlyphCount), fLoadGlyphFlags, err));
942 ERROR:
reed@android.com62900b42009-02-11 15:07:19 +0000943 glyph->zeroMetrics();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000944 return;
945 }
946
947 switch ( fFace->glyph->format ) {
thakis@chromium.org598b8592011-05-24 05:42:55 +0000948 case FT_GLYPH_FORMAT_OUTLINE: {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000949 FT_BBox bbox;
950
senorblanco@chromium.org4526a842010-02-05 23:08:20 +0000951 if (fRec.fFlags & kEmbolden_Flag) {
952 emboldenOutline(&fFace->glyph->outline);
953 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000954 FT_Outline_Get_CBox(&fFace->glyph->outline, &bbox);
955
agl@chromium.orga2c71cb2010-06-17 20:49:17 +0000956 if (fRec.fFlags & SkScalerContext::kSubpixelPositioning_Flag) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000957 int dx = glyph->getSubXFixed() >> 10;
958 int dy = glyph->getSubYFixed() >> 10;
959 // negate dy since freetype-y-goes-up and skia-y-goes-down
960 bbox.xMin += dx;
961 bbox.yMin -= dy;
962 bbox.xMax += dx;
963 bbox.yMax -= dy;
964 }
965
966 bbox.xMin &= ~63;
967 bbox.yMin &= ~63;
968 bbox.xMax = (bbox.xMax + 63) & ~63;
969 bbox.yMax = (bbox.yMax + 63) & ~63;
970
971 glyph->fWidth = SkToU16((bbox.xMax - bbox.xMin) >> 6);
972 glyph->fHeight = SkToU16((bbox.yMax - bbox.yMin) >> 6);
973 glyph->fTop = -SkToS16(bbox.yMax >> 6);
974 glyph->fLeft = SkToS16(bbox.xMin >> 6);
975 break;
thakis@chromium.org598b8592011-05-24 05:42:55 +0000976 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000977
978 case FT_GLYPH_FORMAT_BITMAP:
agl@chromium.orge76073b2010-06-04 20:31:17 +0000979 if (fRec.fFlags & kEmbolden_Flag) {
980 FT_GlyphSlot_Own_Bitmap(fFace->glyph);
981 FT_Bitmap_Embolden(gFTLibrary, &fFace->glyph->bitmap, kBitmapEmboldenStrength, 0);
982 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000983 glyph->fWidth = SkToU16(fFace->glyph->bitmap.width);
984 glyph->fHeight = SkToU16(fFace->glyph->bitmap.rows);
985 glyph->fTop = -SkToS16(fFace->glyph->bitmap_top);
986 glyph->fLeft = SkToS16(fFace->glyph->bitmap_left);
987 break;
988
989 default:
990 SkASSERT(!"unknown glyph format");
991 goto ERROR;
992 }
993
agl@chromium.orga2c71cb2010-06-17 20:49:17 +0000994 if ((fRec.fFlags & SkScalerContext::kSubpixelPositioning_Flag) == 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000995 glyph->fAdvanceX = SkFDot6ToFixed(fFace->glyph->advance.x);
996 glyph->fAdvanceY = -SkFDot6ToFixed(fFace->glyph->advance.y);
997 if (fRec.fFlags & kDevKernText_Flag) {
998 glyph->fRsbDelta = SkToS8(fFace->glyph->rsb_delta);
999 glyph->fLsbDelta = SkToS8(fFace->glyph->lsb_delta);
1000 }
1001 } else {
1002 glyph->fAdvanceX = SkFixedMul(fMatrix22.xx, fFace->glyph->linearHoriAdvance);
1003 glyph->fAdvanceY = -SkFixedMul(fMatrix22.yx, fFace->glyph->linearHoriAdvance);
1004 }
1005
1006#ifdef ENABLE_GLYPH_SPEW
1007 SkDEBUGF(("FT_Set_Char_Size(this:%p sx:%x sy:%x ", this, fScaleX, fScaleY));
1008 SkDEBUGF(("Metrics(glyph:%d flags:0x%x) w:%d\n", glyph->getGlyphID(fBaseGlyphCount), fLoadGlyphFlags, glyph->fWidth));
1009#endif
1010}
1011
reed@google.combde3c8e2011-05-18 11:58:10 +00001012static int lerp(int start, int end) {
1013 SkASSERT((unsigned)SK_FREETYPE_LCD_LERP <= 256);
1014 return start + ((end - start) * (SK_FREETYPE_LCD_LERP) >> 8);
reed@google.comc5181342011-05-17 20:52:46 +00001015}
1016
1017static uint16_t packTriple(unsigned r, unsigned g, unsigned b) {
1018 if (SK_FREETYPE_LCD_LERP) {
reed@google.combde3c8e2011-05-18 11:58:10 +00001019 // want (a+b+c)/3, but we approx to avoid the divide
1020 unsigned ave = (5 * (r + g + b) + b) >> 4;
1021 r = lerp(r, ave);
1022 g = lerp(g, ave);
1023 b = lerp(b, ave);
reed@google.comc5181342011-05-17 20:52:46 +00001024 }
1025 return SkPackRGB16(r >> 3, g >> 2, b >> 3);
1026}
1027
reed@google.com73824072011-06-23 13:17:30 +00001028static uint16_t grayToRGB16(U8CPU gray) {
1029 SkASSERT(gray <= 255);
1030 return SkPackRGB16(gray >> 3, gray >> 2, gray >> 3);
1031}
1032
1033static int bittst(const uint8_t data[], int bitOffset) {
1034 SkASSERT(bitOffset >= 0);
reed@google.comc8e0f932011-06-23 19:39:49 +00001035 int lowBit = data[bitOffset >> 3] >> (~bitOffset & 7);
reed@google.com73824072011-06-23 13:17:30 +00001036 return lowBit & 1;
1037}
1038
reed@google.comeffc5012011-06-27 16:44:46 +00001039static void copyFT2LCD16(const SkGlyph& glyph, const FT_Bitmap& bitmap,
1040 int lcdIsBGR) {
reed@google.comea2333d2011-03-14 16:44:56 +00001041 SkASSERT(glyph.fHeight == bitmap.rows);
reed@google.comea2333d2011-03-14 16:44:56 +00001042 uint16_t* dst = reinterpret_cast<uint16_t*>(glyph.fImage);
reed@google.com73824072011-06-23 13:17:30 +00001043 const size_t dstRB = glyph.rowBytes();
1044 const int width = glyph.fWidth;
1045 const uint8_t* src = bitmap.buffer;
reed@google.comea2333d2011-03-14 16:44:56 +00001046
reed@google.com73824072011-06-23 13:17:30 +00001047 switch (bitmap.pixel_mode) {
1048 case FT_PIXEL_MODE_MONO: {
1049 for (int y = 0; y < glyph.fHeight; ++y) {
1050 for (int x = 0; x < width; ++x) {
1051 dst[x] = -bittst(src, x);
1052 }
1053 dst = (uint16_t*)((char*)dst + dstRB);
1054 src += bitmap.pitch;
1055 }
1056 } break;
1057 case FT_PIXEL_MODE_GRAY: {
1058 for (int y = 0; y < glyph.fHeight; ++y) {
1059 for (int x = 0; x < width; ++x) {
1060 dst[x] = grayToRGB16(src[x]);
1061 }
1062 dst = (uint16_t*)((char*)dst + dstRB);
1063 src += bitmap.pitch;
1064 }
1065 } break;
1066 default: {
1067 SkASSERT(glyph.fWidth * 3 == bitmap.width - 6);
1068 src += 3;
1069 for (int y = 0; y < glyph.fHeight; y++) {
1070 const uint8_t* triple = src;
reed@google.comeffc5012011-06-27 16:44:46 +00001071 if (lcdIsBGR) {
1072 for (int x = 0; x < width; x++) {
1073 dst[x] = packTriple(triple[2], triple[1], triple[0]);
1074 triple += 3;
1075 }
1076 } else {
1077 for (int x = 0; x < width; x++) {
1078 dst[x] = packTriple(triple[0], triple[1], triple[2]);
1079 triple += 3;
1080 }
reed@google.com73824072011-06-23 13:17:30 +00001081 }
1082 src += bitmap.pitch;
1083 dst = (uint16_t*)((char*)dst + dstRB);
1084 }
1085 } break;
reed@google.comea2333d2011-03-14 16:44:56 +00001086 }
1087}
1088
reed@android.com8a1c16f2008-12-17 15:59:43 +00001089void SkScalerContext_FreeType::generateImage(const SkGlyph& glyph) {
1090 SkAutoMutexAcquire ac(gFTMutex);
1091
1092 FT_Error err;
1093
1094 if (this->setupSize()) {
1095 goto ERROR;
1096 }
1097
1098 err = FT_Load_Glyph( fFace, glyph.getGlyphID(fBaseGlyphCount), fLoadGlyphFlags);
1099 if (err != 0) {
1100 SkDEBUGF(("SkScalerContext_FreeType::generateImage: FT_Load_Glyph(glyph:%d width:%d height:%d rb:%d flags:%d) returned 0x%x\n",
1101 glyph.getGlyphID(fBaseGlyphCount), glyph.fWidth, glyph.fHeight, glyph.rowBytes(), fLoadGlyphFlags, err));
1102 ERROR:
1103 memset(glyph.fImage, 0, glyph.rowBytes() * glyph.fHeight);
1104 return;
1105 }
1106
1107 switch ( fFace->glyph->format ) {
1108 case FT_GLYPH_FORMAT_OUTLINE: {
1109 FT_Outline* outline = &fFace->glyph->outline;
1110 FT_BBox bbox;
1111 FT_Bitmap target;
1112
senorblanco@chromium.org4526a842010-02-05 23:08:20 +00001113 if (fRec.fFlags & kEmbolden_Flag) {
1114 emboldenOutline(outline);
1115 }
1116
reed@android.com8a1c16f2008-12-17 15:59:43 +00001117 int dx = 0, dy = 0;
agl@chromium.orga2c71cb2010-06-17 20:49:17 +00001118 if (fRec.fFlags & SkScalerContext::kSubpixelPositioning_Flag) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001119 dx = glyph.getSubXFixed() >> 10;
1120 dy = glyph.getSubYFixed() >> 10;
1121 // negate dy since freetype-y-goes-up and skia-y-goes-down
1122 dy = -dy;
1123 }
1124 FT_Outline_Get_CBox(outline, &bbox);
1125 /*
1126 what we really want to do for subpixel is
1127 offset(dx, dy)
1128 compute_bounds
1129 offset(bbox & !63)
1130 but that is two calls to offset, so we do the following, which
1131 achieves the same thing with only one offset call.
1132 */
1133 FT_Outline_Translate(outline, dx - ((bbox.xMin + dx) & ~63),
1134 dy - ((bbox.yMin + dy) & ~63));
1135
reed@google.comea2333d2011-03-14 16:44:56 +00001136 if (SkMask::kLCD16_Format == glyph.fMaskFormat) {
1137 FT_Render_Glyph(fFace->glyph, FT_RENDER_MODE_LCD);
reed@google.comeffc5012011-06-27 16:44:46 +00001138 copyFT2LCD16(glyph, fFace->glyph->bitmap,
1139 fRec.fFlags & SkScalerContext::kLCD_BGROrder_Flag);
reed@google.comea2333d2011-03-14 16:44:56 +00001140 } else {
1141 target.width = glyph.fWidth;
1142 target.rows = glyph.fHeight;
1143 target.pitch = glyph.rowBytes();
1144 target.buffer = reinterpret_cast<uint8_t*>(glyph.fImage);
1145 target.pixel_mode = compute_pixel_mode(
1146 (SkMask::Format)fRec.fMaskFormat);
1147 target.num_grays = 256;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001148
reed@google.comea2333d2011-03-14 16:44:56 +00001149 memset(glyph.fImage, 0, glyph.rowBytes() * glyph.fHeight);
1150 FT_Outline_Get_Bitmap(gFTLibrary, outline, &target);
1151 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001152 } break;
1153
1154 case FT_GLYPH_FORMAT_BITMAP: {
agl@chromium.orge76073b2010-06-04 20:31:17 +00001155 if (fRec.fFlags & kEmbolden_Flag) {
1156 FT_GlyphSlot_Own_Bitmap(fFace->glyph);
1157 FT_Bitmap_Embolden(gFTLibrary, &fFace->glyph->bitmap, kBitmapEmboldenStrength, 0);
1158 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001159 SkASSERT_CONTINUE(glyph.fWidth == fFace->glyph->bitmap.width);
1160 SkASSERT_CONTINUE(glyph.fHeight == fFace->glyph->bitmap.rows);
1161 SkASSERT_CONTINUE(glyph.fTop == -fFace->glyph->bitmap_top);
1162 SkASSERT_CONTINUE(glyph.fLeft == fFace->glyph->bitmap_left);
1163
1164 const uint8_t* src = (const uint8_t*)fFace->glyph->bitmap.buffer;
1165 uint8_t* dst = (uint8_t*)glyph.fImage;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001166
agl@chromium.org558434a2009-08-11 17:22:38 +00001167 if (fFace->glyph->bitmap.pixel_mode == FT_PIXEL_MODE_GRAY ||
1168 (fFace->glyph->bitmap.pixel_mode == FT_PIXEL_MODE_MONO &&
1169 glyph.fMaskFormat == SkMask::kBW_Format)) {
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001170 unsigned srcRowBytes = fFace->glyph->bitmap.pitch;
1171 unsigned dstRowBytes = glyph.rowBytes();
1172 unsigned minRowBytes = SkMin32(srcRowBytes, dstRowBytes);
1173 unsigned extraRowBytes = dstRowBytes - minRowBytes;
1174
1175 for (int y = fFace->glyph->bitmap.rows - 1; y >= 0; --y) {
1176 memcpy(dst, src, minRowBytes);
1177 memset(dst + minRowBytes, 0, extraRowBytes);
1178 src += srcRowBytes;
1179 dst += dstRowBytes;
1180 }
agl@chromium.org558434a2009-08-11 17:22:38 +00001181 } else if (fFace->glyph->bitmap.pixel_mode == FT_PIXEL_MODE_MONO &&
reed@google.comeffc5012011-06-27 16:44:46 +00001182 glyph.fMaskFormat == SkMask::kA8_Format) {
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001183 for (int y = 0; y < fFace->glyph->bitmap.rows; ++y) {
1184 uint8_t byte = 0;
1185 int bits = 0;
1186 const uint8_t* src_row = src;
1187 uint8_t* dst_row = dst;
1188
1189 for (int x = 0; x < fFace->glyph->bitmap.width; ++x) {
1190 if (!bits) {
1191 byte = *src_row++;
1192 bits = 8;
1193 }
1194
1195 *dst_row++ = byte & 0x80 ? 0xff : 0;
1196 bits--;
1197 byte <<= 1;
1198 }
1199
1200 src += fFace->glyph->bitmap.pitch;
1201 dst += glyph.rowBytes();
1202 }
reed@google.com73824072011-06-23 13:17:30 +00001203 } else if (SkMask::kLCD16_Format == glyph.fMaskFormat) {
reed@google.comeffc5012011-06-27 16:44:46 +00001204 copyFT2LCD16(glyph, fFace->glyph->bitmap,
1205 fRec.fFlags & SkScalerContext::kLCD_BGROrder_Flag);
agl@chromium.org558434a2009-08-11 17:22:38 +00001206 } else {
reed@google.com73824072011-06-23 13:17:30 +00001207 SkASSERT(!"unknown glyph bitmap transform needed");
reed@android.com8a1c16f2008-12-17 15:59:43 +00001208 }
1209 } break;
1210
1211 default:
1212 SkASSERT(!"unknown glyph format");
1213 goto ERROR;
1214 }
1215}
1216
1217///////////////////////////////////////////////////////////////////////////////
1218
1219#define ft2sk(x) SkFixedToScalar((x) << 10)
1220
reed@android.com6f252972009-01-14 16:46:16 +00001221#if FREETYPE_MAJOR >= 2 && FREETYPE_MINOR >= 2
reed@android.com8a1c16f2008-12-17 15:59:43 +00001222 #define CONST_PARAM const
1223#else // older freetype doesn't use const here
1224 #define CONST_PARAM
1225#endif
1226
1227static int move_proc(CONST_PARAM FT_Vector* pt, void* ctx) {
1228 SkPath* path = (SkPath*)ctx;
1229 path->close(); // to close the previous contour (if any)
1230 path->moveTo(ft2sk(pt->x), -ft2sk(pt->y));
1231 return 0;
1232}
1233
1234static int line_proc(CONST_PARAM FT_Vector* pt, void* ctx) {
1235 SkPath* path = (SkPath*)ctx;
1236 path->lineTo(ft2sk(pt->x), -ft2sk(pt->y));
1237 return 0;
1238}
1239
1240static int quad_proc(CONST_PARAM FT_Vector* pt0, CONST_PARAM FT_Vector* pt1,
1241 void* ctx) {
1242 SkPath* path = (SkPath*)ctx;
1243 path->quadTo(ft2sk(pt0->x), -ft2sk(pt0->y), ft2sk(pt1->x), -ft2sk(pt1->y));
1244 return 0;
1245}
1246
1247static int cubic_proc(CONST_PARAM FT_Vector* pt0, CONST_PARAM FT_Vector* pt1,
1248 CONST_PARAM FT_Vector* pt2, void* ctx) {
1249 SkPath* path = (SkPath*)ctx;
1250 path->cubicTo(ft2sk(pt0->x), -ft2sk(pt0->y), ft2sk(pt1->x),
1251 -ft2sk(pt1->y), ft2sk(pt2->x), -ft2sk(pt2->y));
1252 return 0;
1253}
1254
1255void SkScalerContext_FreeType::generatePath(const SkGlyph& glyph,
1256 SkPath* path) {
1257 SkAutoMutexAcquire ac(gFTMutex);
1258
1259 SkASSERT(&glyph && path);
1260
1261 if (this->setupSize()) {
1262 path->reset();
1263 return;
1264 }
1265
1266 uint32_t flags = fLoadGlyphFlags;
1267 flags |= FT_LOAD_NO_BITMAP; // ignore embedded bitmaps so we're sure to get the outline
1268 flags &= ~FT_LOAD_RENDER; // don't scan convert (we just want the outline)
1269
1270 FT_Error err = FT_Load_Glyph( fFace, glyph.getGlyphID(fBaseGlyphCount), flags);
1271
1272 if (err != 0) {
1273 SkDEBUGF(("SkScalerContext_FreeType::generatePath: FT_Load_Glyph(glyph:%d flags:%d) returned 0x%x\n",
1274 glyph.getGlyphID(fBaseGlyphCount), flags, err));
1275 path->reset();
1276 return;
1277 }
1278
senorblanco@chromium.org4526a842010-02-05 23:08:20 +00001279 if (fRec.fFlags & kEmbolden_Flag) {
1280 emboldenOutline(&fFace->glyph->outline);
1281 }
1282
reed@android.com8a1c16f2008-12-17 15:59:43 +00001283 FT_Outline_Funcs funcs;
1284
1285 funcs.move_to = move_proc;
1286 funcs.line_to = line_proc;
1287 funcs.conic_to = quad_proc;
1288 funcs.cubic_to = cubic_proc;
1289 funcs.shift = 0;
1290 funcs.delta = 0;
1291
1292 err = FT_Outline_Decompose(&fFace->glyph->outline, &funcs, path);
1293
1294 if (err != 0) {
1295 SkDEBUGF(("SkScalerContext_FreeType::generatePath: FT_Load_Glyph(glyph:%d flags:%d) returned 0x%x\n",
1296 glyph.getGlyphID(fBaseGlyphCount), flags, err));
1297 path->reset();
1298 return;
1299 }
1300
1301 path->close();
1302}
1303
1304void SkScalerContext_FreeType::generateFontMetrics(SkPaint::FontMetrics* mx,
1305 SkPaint::FontMetrics* my) {
1306 if (NULL == mx && NULL == my) {
1307 return;
1308 }
1309
1310 SkAutoMutexAcquire ac(gFTMutex);
1311
1312 if (this->setupSize()) {
reed@android.coma8a8b8b2009-05-04 15:00:11 +00001313 ERROR:
reed@android.com8a1c16f2008-12-17 15:59:43 +00001314 if (mx) {
reed@android.com4516f472009-06-29 16:25:36 +00001315 sk_bzero(mx, sizeof(SkPaint::FontMetrics));
reed@android.com8a1c16f2008-12-17 15:59:43 +00001316 }
1317 if (my) {
reed@android.com4516f472009-06-29 16:25:36 +00001318 sk_bzero(my, sizeof(SkPaint::FontMetrics));
reed@android.com8a1c16f2008-12-17 15:59:43 +00001319 }
1320 return;
1321 }
1322
reed@android.coma8a8b8b2009-05-04 15:00:11 +00001323 FT_Face face = fFace;
1324 int upem = face->units_per_EM;
1325 if (upem <= 0) {
1326 goto ERROR;
1327 }
1328
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001329 SkPoint pts[6];
1330 SkFixed ys[6];
reed@android.com8a1c16f2008-12-17 15:59:43 +00001331 SkFixed scaleY = fScaleY;
1332 SkFixed mxy = fMatrix22.xy;
1333 SkFixed myy = fMatrix22.yy;
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001334 SkScalar xmin = SkIntToScalar(face->bbox.xMin) / upem;
1335 SkScalar xmax = SkIntToScalar(face->bbox.xMax) / upem;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001336
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001337 int leading = face->height - (face->ascender + -face->descender);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001338 if (leading < 0) {
1339 leading = 0;
1340 }
1341
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001342 // Try to get the OS/2 table from the font. This contains the specific
1343 // average font width metrics which Windows uses.
1344 TT_OS2* os2 = (TT_OS2*) FT_Get_Sfnt_Table(face, ft_sfnt_os2);
1345
reed@android.com8a1c16f2008-12-17 15:59:43 +00001346 ys[0] = -face->bbox.yMax;
1347 ys[1] = -face->ascender;
1348 ys[2] = -face->descender;
1349 ys[3] = -face->bbox.yMin;
1350 ys[4] = leading;
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001351 ys[5] = os2 ? os2->xAvgCharWidth : 0;
1352
1353 SkScalar x_height;
1354 if (os2 && os2->sxHeight) {
1355 x_height = SkFixedToScalar(SkMulDiv(fScaleX, os2->sxHeight, upem));
1356 } else {
1357 const FT_UInt x_glyph = FT_Get_Char_Index(fFace, 'x');
1358 if (x_glyph) {
1359 FT_BBox bbox;
1360 FT_Load_Glyph(fFace, x_glyph, fLoadGlyphFlags);
senorblanco@chromium.org4526a842010-02-05 23:08:20 +00001361 if (fRec.fFlags & kEmbolden_Flag) {
1362 emboldenOutline(&fFace->glyph->outline);
1363 }
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001364 FT_Outline_Get_CBox(&fFace->glyph->outline, &bbox);
1365 x_height = SkIntToScalar(bbox.yMax) / 64;
1366 } else {
1367 x_height = 0;
1368 }
1369 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001370
1371 // convert upem-y values into scalar points
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001372 for (int i = 0; i < 6; i++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001373 SkFixed y = SkMulDiv(scaleY, ys[i], upem);
1374 SkFixed x = SkFixedMul(mxy, y);
1375 y = SkFixedMul(myy, y);
1376 pts[i].set(SkFixedToScalar(x), SkFixedToScalar(y));
1377 }
1378
1379 if (mx) {
1380 mx->fTop = pts[0].fX;
1381 mx->fAscent = pts[1].fX;
1382 mx->fDescent = pts[2].fX;
1383 mx->fBottom = pts[3].fX;
1384 mx->fLeading = pts[4].fX;
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001385 mx->fAvgCharWidth = pts[5].fX;
1386 mx->fXMin = xmin;
1387 mx->fXMax = xmax;
1388 mx->fXHeight = x_height;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001389 }
1390 if (my) {
1391 my->fTop = pts[0].fY;
1392 my->fAscent = pts[1].fY;
1393 my->fDescent = pts[2].fY;
1394 my->fBottom = pts[3].fY;
1395 my->fLeading = pts[4].fY;
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001396 my->fAvgCharWidth = pts[5].fY;
1397 my->fXMin = xmin;
1398 my->fXMax = xmax;
1399 my->fXHeight = x_height;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001400 }
1401}
1402
1403////////////////////////////////////////////////////////////////////////
1404////////////////////////////////////////////////////////////////////////
1405
1406SkScalerContext* SkFontHost::CreateScalerContext(const SkDescriptor* desc) {
reed@android.com62900b42009-02-11 15:07:19 +00001407 SkScalerContext_FreeType* c = SkNEW_ARGS(SkScalerContext_FreeType, (desc));
1408 if (!c->success()) {
1409 SkDELETE(c);
1410 c = NULL;
1411 }
1412 return c;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001413}
1414
1415///////////////////////////////////////////////////////////////////////////////
1416
1417/* Export this so that other parts of our FonttHost port can make use of our
1418 ability to extract the name+style from a stream, using FreeType's api.
1419*/
reed@google.com5b31b0f2011-02-23 14:41:42 +00001420SkTypeface::Style find_name_and_attributes(SkStream* stream, SkString* name,
1421 bool* isFixedWidth) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001422 FT_Library library;
reed@android.combfbd4ff2009-07-23 17:44:41 +00001423 if (FT_Init_FreeType(&library)) {
djsollen@google.com7b34ea62011-02-24 16:28:51 +00001424 name->reset();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001425 return SkTypeface::kNormal;
1426 }
1427
1428 FT_Open_Args args;
1429 memset(&args, 0, sizeof(args));
1430
1431 const void* memoryBase = stream->getMemoryBase();
1432 FT_StreamRec streamRec;
1433
1434 if (NULL != memoryBase) {
1435 args.flags = FT_OPEN_MEMORY;
1436 args.memory_base = (const FT_Byte*)memoryBase;
1437 args.memory_size = stream->getLength();
1438 } else {
1439 memset(&streamRec, 0, sizeof(streamRec));
1440 streamRec.size = stream->read(NULL, 0);
1441 streamRec.descriptor.pointer = stream;
1442 streamRec.read = sk_stream_read;
1443 streamRec.close = sk_stream_close;
1444
1445 args.flags = FT_OPEN_STREAM;
1446 args.stream = &streamRec;
1447 }
1448
1449 FT_Face face;
1450 if (FT_Open_Face(library, &args, 0, &face)) {
1451 FT_Done_FreeType(library);
djsollen@google.com7b34ea62011-02-24 16:28:51 +00001452 name->reset();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001453 return SkTypeface::kNormal;
1454 }
1455
1456 name->set(face->family_name);
1457 int style = SkTypeface::kNormal;
1458
1459 if (face->style_flags & FT_STYLE_FLAG_BOLD) {
1460 style |= SkTypeface::kBold;
1461 }
1462 if (face->style_flags & FT_STYLE_FLAG_ITALIC) {
1463 style |= SkTypeface::kItalic;
1464 }
reed@google.com5b31b0f2011-02-23 14:41:42 +00001465 if (isFixedWidth) {
1466 *isFixedWidth = FT_IS_FIXED_WIDTH(face);
1467 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001468
1469 FT_Done_Face(face);
1470 FT_Done_FreeType(library);
1471 return (SkTypeface::Style)style;
1472}