blob: 8eaf06cbee230a39ac3f70706c492b278e4bd9ce [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
reed@google.comffe49f52011-11-22 19:42:41 +000098static const uint8_t* gGammaTables[2];
99
reed@android.com8a1c16f2008-12-17 15:59:43 +0000100/////////////////////////////////////////////////////////////////////////
101
agl@chromium.orge76073b2010-06-04 20:31:17 +0000102// See http://freetype.sourceforge.net/freetype2/docs/reference/ft2-bitmap_handling.html#FT_Bitmap_Embolden
103// This value was chosen by eyeballing the result in Firefox and trying to match it.
104static const FT_Pos kBitmapEmboldenStrength = 1 << 6;
105
agl@chromium.org309485b2009-07-21 17:41:32 +0000106static bool
107InitFreetype() {
108 FT_Error err = FT_Init_FreeType(&gFTLibrary);
reed@google.comea2333d2011-03-14 16:44:56 +0000109 if (err) {
agl@chromium.org309485b2009-07-21 17:41:32 +0000110 return false;
reed@google.comea2333d2011-03-14 16:44:56 +0000111 }
agl@chromium.org309485b2009-07-21 17:41:32 +0000112
agl@chromium.org309485b2009-07-21 17:41:32 +0000113 // Setup LCD filtering. This reduces colour fringes for LCD rendered
114 // glyphs.
epoger@google.comb371ed12011-06-29 21:20:52 +0000115#ifdef FT_LCD_FILTER_H
agl@chromium.org309485b2009-07-21 17:41:32 +0000116 err = FT_Library_SetLcdFilter(gFTLibrary, FT_LCD_FILTER_DEFAULT);
agl@chromium.orgf18d8762009-07-28 18:38:08 +0000117 gLCDSupport = err == 0;
epoger@google.com5070d792011-06-29 20:43:14 +0000118#else
119 gLCDSupport = false;
120#endif
reed@android.com61608aa2009-07-31 14:52:54 +0000121 gLCDSupportValid = true;
agl@chromium.org309485b2009-07-21 17:41:32 +0000122
123 return true;
124}
125
reed@android.com8a1c16f2008-12-17 15:59:43 +0000126class SkScalerContext_FreeType : public SkScalerContext {
127public:
128 SkScalerContext_FreeType(const SkDescriptor* desc);
129 virtual ~SkScalerContext_FreeType();
agl@chromium.orgcc3096b2009-04-22 22:09:04 +0000130
reed@android.com62900b42009-02-11 15:07:19 +0000131 bool success() const {
reed@android.coma0f5d152009-06-22 17:38:10 +0000132 return fFaceRec != NULL &&
133 fFTSize != NULL &&
134 fFace != NULL;
reed@android.com62900b42009-02-11 15:07:19 +0000135 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000136
137protected:
ctguil@chromium.org0bc7bf52011-03-04 19:04:57 +0000138 virtual unsigned generateGlyphCount();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000139 virtual uint16_t generateCharToGlyph(SkUnichar uni);
140 virtual void generateAdvance(SkGlyph* glyph);
141 virtual void generateMetrics(SkGlyph* glyph);
142 virtual void generateImage(const SkGlyph& glyph);
143 virtual void generatePath(const SkGlyph& glyph, SkPath* path);
144 virtual void generateFontMetrics(SkPaint::FontMetrics* mx,
145 SkPaint::FontMetrics* my);
reed@android.com9d3a9852010-01-08 14:07:42 +0000146 virtual SkUnichar generateGlyphToChar(uint16_t glyph);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000147
148private:
149 SkFaceRec* fFaceRec;
150 FT_Face fFace; // reference to shared face in gFaceRecHead
151 FT_Size fFTSize; // our own copy
152 SkFixed fScaleX, fScaleY;
153 FT_Matrix fMatrix22;
154 uint32_t fLoadGlyphFlags;
reed@google.combdc99882011-11-21 14:36:57 +0000155 bool fDoLinearMetrics;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000156
157 FT_Error setupSize();
senorblanco@chromium.org4526a842010-02-05 23:08:20 +0000158 void emboldenOutline(FT_Outline* outline);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000159};
160
161///////////////////////////////////////////////////////////////////////////
162///////////////////////////////////////////////////////////////////////////
163
164#include "SkStream.h"
165
166struct SkFaceRec {
167 SkFaceRec* fNext;
168 FT_Face fFace;
169 FT_StreamRec fFTStream;
170 SkStream* fSkStream;
171 uint32_t fRefCnt;
172 uint32_t fFontID;
173
reed@android.comb1d9d2e2009-03-04 17:37:51 +0000174 // assumes ownership of the stream, will call unref() when its done
reed@android.com8a1c16f2008-12-17 15:59:43 +0000175 SkFaceRec(SkStream* strm, uint32_t fontID);
176 ~SkFaceRec() {
reed@android.comb1d9d2e2009-03-04 17:37:51 +0000177 fSkStream->unref();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000178 }
179};
180
181extern "C" {
182 static unsigned long sk_stream_read(FT_Stream stream,
183 unsigned long offset,
184 unsigned char* buffer,
185 unsigned long count ) {
186 SkStream* str = (SkStream*)stream->descriptor.pointer;
187
188 if (count) {
189 if (!str->rewind()) {
190 return 0;
191 } else {
192 unsigned long ret;
193 if (offset) {
194 ret = str->read(NULL, offset);
195 if (ret != offset) {
196 return 0;
197 }
198 }
199 ret = str->read(buffer, count);
200 if (ret != count) {
201 return 0;
202 }
203 count = ret;
204 }
205 }
206 return count;
207 }
208
209 static void sk_stream_close( FT_Stream stream) {}
210}
211
212SkFaceRec::SkFaceRec(SkStream* strm, uint32_t fontID)
213 : fSkStream(strm), fFontID(fontID) {
214// SkDEBUGF(("SkFaceRec: opening %s (%p)\n", key.c_str(), strm));
215
reed@android.com4516f472009-06-29 16:25:36 +0000216 sk_bzero(&fFTStream, sizeof(fFTStream));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000217 fFTStream.size = fSkStream->getLength();
218 fFTStream.descriptor.pointer = fSkStream;
219 fFTStream.read = sk_stream_read;
220 fFTStream.close = sk_stream_close;
221}
222
reed@android.com62900b42009-02-11 15:07:19 +0000223// Will return 0 on failure
reed@android.com8a1c16f2008-12-17 15:59:43 +0000224static SkFaceRec* ref_ft_face(uint32_t fontID) {
225 SkFaceRec* rec = gFaceRecHead;
226 while (rec) {
227 if (rec->fFontID == fontID) {
228 SkASSERT(rec->fFace);
229 rec->fRefCnt += 1;
230 return rec;
231 }
232 rec = rec->fNext;
233 }
234
235 SkStream* strm = SkFontHost::OpenStream(fontID);
236 if (NULL == strm) {
237 SkDEBUGF(("SkFontHost::OpenStream failed opening %x\n", fontID));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000238 return 0;
239 }
240
241 // this passes ownership of strm to the rec
242 rec = SkNEW_ARGS(SkFaceRec, (strm, fontID));
243
244 FT_Open_Args args;
245 memset(&args, 0, sizeof(args));
246 const void* memoryBase = strm->getMemoryBase();
247
248 if (NULL != memoryBase) {
249//printf("mmap(%s)\n", keyString.c_str());
250 args.flags = FT_OPEN_MEMORY;
251 args.memory_base = (const FT_Byte*)memoryBase;
252 args.memory_size = strm->getLength();
253 } else {
254//printf("fopen(%s)\n", keyString.c_str());
255 args.flags = FT_OPEN_STREAM;
256 args.stream = &rec->fFTStream;
257 }
258
agl@chromium.org61a678a2010-08-06 18:08:18 +0000259 int face_index;
260 int length = SkFontHost::GetFileName(fontID, NULL, 0, &face_index);
261 FT_Error err = FT_Open_Face(gFTLibrary, &args, length ? face_index : 0,
262 &rec->fFace);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000263
264 if (err) { // bad filename, try the default font
265 fprintf(stderr, "ERROR: unable to open font '%x'\n", fontID);
266 SkDELETE(rec);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000267 return 0;
268 } else {
269 SkASSERT(rec->fFace);
270 //fprintf(stderr, "Opened font '%s'\n", filename.c_str());
271 rec->fNext = gFaceRecHead;
272 gFaceRecHead = rec;
273 rec->fRefCnt = 1;
274 return rec;
275 }
276}
277
278static void unref_ft_face(FT_Face face) {
279 SkFaceRec* rec = gFaceRecHead;
280 SkFaceRec* prev = NULL;
281 while (rec) {
282 SkFaceRec* next = rec->fNext;
283 if (rec->fFace == face) {
284 if (--rec->fRefCnt == 0) {
285 if (prev) {
286 prev->fNext = next;
287 } else {
288 gFaceRecHead = next;
289 }
290 FT_Done_Face(face);
291 SkDELETE(rec);
292 }
293 return;
294 }
295 prev = rec;
296 rec = next;
297 }
tomhudson@google.com0c00f212011-12-28 14:59:50 +0000298 SkDEBUGFAIL("shouldn't get here, face not in list");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000299}
300
301///////////////////////////////////////////////////////////////////////////
302
vandebo@chromium.org16be6b82011-01-28 21:28:56 +0000303// Work around for old versions of freetype.
304static FT_Error getAdvances(FT_Face face, FT_UInt start, FT_UInt count,
305 FT_Int32 loadFlags, FT_Fixed* advances) {
306#ifdef FT_ADVANCES_H
307 return FT_Get_Advances(face, start, count, loadFlags, advances);
308#else
309 if (!face || start >= face->num_glyphs ||
310 start + count > face->num_glyphs || loadFlags != FT_LOAD_NO_SCALE) {
311 return 6; // "Invalid argument."
312 }
313 if (count == 0)
314 return 0;
315
316 for (int i = 0; i < count; i++) {
317 FT_Error err = FT_Load_Glyph(face, start + i, FT_LOAD_NO_SCALE);
318 if (err)
319 return err;
320 advances[i] = face->glyph->advance.x;
321 }
322
323 return 0;
324#endif
325}
326
327static bool canEmbed(FT_Face face) {
djsollen@google.com3839ca12011-11-03 17:31:41 +0000328// The Android freetype library does not compile the FT_Get_FSType_Flags
329// function, so we are required to add the !defined(SK_BUILD_FOR_ANDROID) until
330// support is added to Androids port of freetype.
331#if defined(FT_FSTYPE_RESTRICTED_LICENSE_EMBEDDING) && !defined(SK_BUILD_FOR_ANDROID)
vandebo@chromium.org16be6b82011-01-28 21:28:56 +0000332 FT_UShort fsType = FT_Get_FSType_Flags(face);
333 return (fsType & (FT_FSTYPE_RESTRICTED_LICENSE_EMBEDDING |
334 FT_FSTYPE_BITMAP_EMBEDDING_ONLY)) == 0;
335#else
336 // No embedding is 0x2 and bitmap embedding only is 0x200.
337 TT_OS2* os2_table;
338 if ((os2_table = (TT_OS2*)FT_Get_Sfnt_Table(face, ft_sfnt_os2)) != NULL) {
339 return (os2_table->fsType & 0x202) == 0;
340 }
341 return false; // We tried, fail safe.
342#endif
343}
344
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000345static bool GetLetterCBox(FT_Face face, char letter, FT_BBox* bbox) {
346 const FT_UInt glyph_id = FT_Get_Char_Index(face, letter);
347 if (!glyph_id)
348 return false;
349 FT_Load_Glyph(face, glyph_id, FT_LOAD_NO_SCALE);
350 FT_Outline_Get_CBox(&face->glyph->outline, bbox);
351 return true;
352}
353
vandebo@chromium.org6f72d1e2011-02-14 23:19:59 +0000354static bool getWidthAdvance(FT_Face face, int gId, int16_t* data) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000355 FT_Fixed advance = 0;
vandebo@chromium.org6f72d1e2011-02-14 23:19:59 +0000356 if (getAdvances(face, gId, 1, FT_LOAD_NO_SCALE, &advance)) {
357 return false;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000358 }
vandebo@chromium.org6f72d1e2011-02-14 23:19:59 +0000359 SkASSERT(data);
360 *data = advance;
361 return true;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000362}
363
vandebo@chromium.org6744d492011-05-09 18:13:47 +0000364static void populate_glyph_to_unicode(FT_Face& face,
365 SkTDArray<SkUnichar>* glyphToUnicode) {
366 // Check and see if we have Unicode cmaps.
367 for (int i = 0; i < face->num_charmaps; ++i) {
368 // CMaps known to support Unicode:
369 // Platform ID Encoding ID Name
370 // ----------- ----------- -----------------------------------
371 // 0 0,1 Apple Unicode
372 // 0 3 Apple Unicode 2.0 (preferred)
373 // 3 1 Microsoft Unicode UCS-2
374 // 3 10 Microsoft Unicode UCS-4 (preferred)
375 //
376 // See Apple TrueType Reference Manual
377 // http://developer.apple.com/fonts/TTRefMan/RM06/Chap6cmap.html
378 // http://developer.apple.com/fonts/TTRefMan/RM06/Chap6name.html#ID
379 // Microsoft OpenType Specification
380 // http://www.microsoft.com/typography/otspec/cmap.htm
381
382 FT_UShort platformId = face->charmaps[i]->platform_id;
383 FT_UShort encodingId = face->charmaps[i]->encoding_id;
384
385 if (platformId != 0 && platformId != 3) {
386 continue;
387 }
388 if (platformId == 3 && encodingId != 1 && encodingId != 10) {
389 continue;
390 }
391 bool preferredMap = ((platformId == 3 && encodingId == 10) ||
392 (platformId == 0 && encodingId == 3));
393
394 FT_Set_Charmap(face, face->charmaps[i]);
395 if (glyphToUnicode->isEmpty()) {
396 glyphToUnicode->setCount(face->num_glyphs);
397 memset(glyphToUnicode->begin(), 0,
398 sizeof(SkUnichar) * face->num_glyphs);
399 }
400
401 // Iterate through each cmap entry.
402 FT_UInt glyphIndex;
403 for (SkUnichar charCode = FT_Get_First_Char(face, &glyphIndex);
404 glyphIndex != 0;
405 charCode = FT_Get_Next_Char(face, charCode, &glyphIndex)) {
406 if (charCode &&
407 ((*glyphToUnicode)[glyphIndex] == 0 || preferredMap)) {
408 (*glyphToUnicode)[glyphIndex] = charCode;
409 }
410 }
411 }
412}
413
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000414// static
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000415SkAdvancedTypefaceMetrics* SkFontHost::GetAdvancedTypefaceMetrics(
vandebo@chromium.org325cb9a2011-03-30 18:36:29 +0000416 uint32_t fontID,
vandebo@chromium.org37ad8fb2011-08-18 02:38:50 +0000417 SkAdvancedTypefaceMetrics::PerGlyphInfo perGlyphInfo,
418 const uint32_t* glyphIDs,
419 uint32_t glyphIDsCount) {
djsollen@google.comda957722011-11-16 17:00:46 +0000420#if defined(SK_BUILD_FOR_MAC)
reed@google.com8a5d6922011-03-14 15:08:03 +0000421 return NULL;
422#else
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000423 SkAutoMutexAcquire ac(gFTMutex);
424 FT_Library libInit = NULL;
425 if (gFTCount == 0) {
426 if (!InitFreetype())
427 sk_throw();
428 libInit = gFTLibrary;
429 }
430 SkAutoTCallIProc<struct FT_LibraryRec_, FT_Done_FreeType> ftLib(libInit);
431 SkFaceRec* rec = ref_ft_face(fontID);
432 if (NULL == rec)
433 return NULL;
434 FT_Face face = rec->fFace;
435
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000436 SkAdvancedTypefaceMetrics* info = new SkAdvancedTypefaceMetrics;
437 info->fFontName.set(FT_Get_Postscript_Name(face));
438 info->fMultiMaster = FT_HAS_MULTIPLE_MASTERS(face);
439 info->fLastGlyphID = face->num_glyphs - 1;
440 info->fEmSize = 1000;
441
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000442 bool cid = false;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000443 const char* fontType = FT_Get_X11_Font_Format(face);
vandebo@chromium.orgc3a2ae52011-02-03 21:48:23 +0000444 if (strcmp(fontType, "Type 1") == 0) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000445 info->fType = SkAdvancedTypefaceMetrics::kType1_Font;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000446 } else if (strcmp(fontType, "CID Type 1") == 0) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000447 info->fType = SkAdvancedTypefaceMetrics::kType1CID_Font;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000448 cid = true;
449 } else if (strcmp(fontType, "CFF") == 0) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000450 info->fType = SkAdvancedTypefaceMetrics::kCFF_Font;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000451 } else if (strcmp(fontType, "TrueType") == 0) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000452 info->fType = SkAdvancedTypefaceMetrics::kTrueType_Font;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000453 cid = true;
454 TT_Header* ttHeader;
455 if ((ttHeader = (TT_Header*)FT_Get_Sfnt_Table(face,
456 ft_sfnt_head)) != NULL) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000457 info->fEmSize = ttHeader->Units_Per_EM;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000458 }
459 }
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000460
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000461 info->fStyle = 0;
462 if (FT_IS_FIXED_WIDTH(face))
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000463 info->fStyle |= SkAdvancedTypefaceMetrics::kFixedPitch_Style;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000464 if (face->style_flags & FT_STYLE_FLAG_ITALIC)
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000465 info->fStyle |= SkAdvancedTypefaceMetrics::kItalic_Style;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000466 // We should set either Symbolic or Nonsymbolic; Nonsymbolic if the font's
467 // character set is a subset of 'Adobe standard Latin.'
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000468 info->fStyle |= SkAdvancedTypefaceMetrics::kSymbolic_Style;
469
470 PS_FontInfoRec ps_info;
471 TT_Postscript* tt_info;
472 if (FT_Get_PS_Font_Info(face, &ps_info) == 0) {
473 info->fItalicAngle = ps_info.italic_angle;
474 } else if ((tt_info =
475 (TT_Postscript*)FT_Get_Sfnt_Table(face,
476 ft_sfnt_post)) != NULL) {
477 info->fItalicAngle = SkFixedToScalar(tt_info->italicAngle);
478 } else {
479 info->fItalicAngle = 0;
480 }
481
482 info->fAscent = face->ascender;
483 info->fDescent = face->descender;
484
485 // Figure out a good guess for StemV - Min width of i, I, !, 1.
486 // This probably isn't very good with an italic font.
487 int16_t min_width = SHRT_MAX;
vandebo@chromium.org6f72d1e2011-02-14 23:19:59 +0000488 info->fStemV = 0;
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000489 char stem_chars[] = {'i', 'I', '!', '1'};
490 for (size_t i = 0; i < SK_ARRAY_COUNT(stem_chars); i++) {
491 FT_BBox bbox;
492 if (GetLetterCBox(face, stem_chars[i], &bbox)) {
493 int16_t width = bbox.xMax - bbox.xMin;
494 if (width > 0 && width < min_width) {
495 min_width = width;
496 info->fStemV = min_width;
497 }
498 }
499 }
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000500
501 TT_PCLT* pclt_info;
502 TT_OS2* os2_table;
503 if ((pclt_info = (TT_PCLT*)FT_Get_Sfnt_Table(face, ft_sfnt_pclt)) != NULL) {
504 info->fCapHeight = pclt_info->CapHeight;
505 uint8_t serif_style = pclt_info->SerifStyle & 0x3F;
506 if (serif_style >= 2 && serif_style <= 6)
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000507 info->fStyle |= SkAdvancedTypefaceMetrics::kSerif_Style;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000508 else if (serif_style >= 9 && serif_style <= 12)
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000509 info->fStyle |= SkAdvancedTypefaceMetrics::kScript_Style;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000510 } else if ((os2_table =
511 (TT_OS2*)FT_Get_Sfnt_Table(face, ft_sfnt_os2)) != NULL) {
512 info->fCapHeight = os2_table->sCapHeight;
513 } else {
514 // Figure out a good guess for CapHeight: average the height of M and X.
515 FT_BBox m_bbox, x_bbox;
516 bool got_m, got_x;
517 got_m = GetLetterCBox(face, 'M', &m_bbox);
518 got_x = GetLetterCBox(face, 'X', &x_bbox);
519 if (got_m && got_x) {
520 info->fCapHeight = (m_bbox.yMax - m_bbox.yMin + x_bbox.yMax -
521 x_bbox.yMin) / 2;
522 } else if (got_m && !got_x) {
523 info->fCapHeight = m_bbox.yMax - m_bbox.yMin;
524 } else if (!got_m && got_x) {
525 info->fCapHeight = x_bbox.yMax - x_bbox.yMin;
526 }
527 }
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000528
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000529 info->fBBox = SkIRect::MakeLTRB(face->bbox.xMin, face->bbox.yMax,
530 face->bbox.xMax, face->bbox.yMin);
531
vandebo@chromium.org37ad8fb2011-08-18 02:38:50 +0000532 if (!canEmbed(face) || !FT_IS_SCALABLE(face) ||
vandebo@chromium.org325cb9a2011-03-30 18:36:29 +0000533 info->fType == SkAdvancedTypefaceMetrics::kOther_Font) {
534 perGlyphInfo = SkAdvancedTypefaceMetrics::kNo_PerGlyphInfo;
535 }
536
537 if (perGlyphInfo & SkAdvancedTypefaceMetrics::kHAdvance_PerGlyphInfo) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000538 if (FT_IS_FIXED_WIDTH(face)) {
539 appendRange(&info->fGlyphWidths, 0);
540 int16_t advance = face->max_advance_width;
541 info->fGlyphWidths->fAdvance.append(1, &advance);
542 finishRange(info->fGlyphWidths.get(), 0,
543 SkAdvancedTypefaceMetrics::WidthRange::kDefault);
vandebo@chromium.org6f72d1e2011-02-14 23:19:59 +0000544 } else if (!cid) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000545 appendRange(&info->fGlyphWidths, 0);
546 // So as to not blow out the stack, get advances in batches.
547 for (int gID = 0; gID < face->num_glyphs; gID += 128) {
548 FT_Fixed advances[128];
549 int advanceCount = 128;
550 if (gID + advanceCount > face->num_glyphs)
551 advanceCount = face->num_glyphs - gID + 1;
552 getAdvances(face, gID, advanceCount, FT_LOAD_NO_SCALE,
553 advances);
554 for (int i = 0; i < advanceCount; i++) {
555 int16_t advance = advances[gID + i];
556 info->fGlyphWidths->fAdvance.append(1, &advance);
557 }
558 }
559 finishRange(info->fGlyphWidths.get(), face->num_glyphs - 1,
560 SkAdvancedTypefaceMetrics::WidthRange::kRange);
561 } else {
vandebo@chromium.org6f72d1e2011-02-14 23:19:59 +0000562 info->fGlyphWidths.reset(
vandebo@chromium.org37ad8fb2011-08-18 02:38:50 +0000563 getAdvanceData(face,
564 face->num_glyphs,
565 glyphIDs,
566 glyphIDsCount,
567 &getWidthAdvance));
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000568 }
vandebo@chromium.org325cb9a2011-03-30 18:36:29 +0000569 }
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000570
vandebo@chromium.org325cb9a2011-03-30 18:36:29 +0000571 if (perGlyphInfo & SkAdvancedTypefaceMetrics::kVAdvance_PerGlyphInfo &&
572 FT_HAS_VERTICAL(face)) {
573 SkASSERT(false); // Not implemented yet.
574 }
575
576 if (perGlyphInfo & SkAdvancedTypefaceMetrics::kGlyphNames_PerGlyphInfo &&
577 info->fType == SkAdvancedTypefaceMetrics::kType1_Font) {
578 // Postscript fonts may contain more than 255 glyphs, so we end up
579 // using multiple font descriptions with a glyph ordering. Record
580 // the name of each glyph.
581 info->fGlyphNames.reset(
582 new SkAutoTArray<SkString>(face->num_glyphs));
583 for (int gID = 0; gID < face->num_glyphs; gID++) {
584 char glyphName[128]; // PS limit for names is 127 bytes.
585 FT_Get_Glyph_Name(face, gID, glyphName, 128);
586 info->fGlyphNames->get()[gID].set(glyphName);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000587 }
588 }
589
vandebo@chromium.org6744d492011-05-09 18:13:47 +0000590 if (perGlyphInfo & SkAdvancedTypefaceMetrics::kToUnicode_PerGlyphInfo &&
591 info->fType != SkAdvancedTypefaceMetrics::kType1_Font &&
592 face->num_charmaps) {
593 populate_glyph_to_unicode(face, &(info->fGlyphToUnicode));
594 }
595
vandebo@chromium.orgc3a2ae52011-02-03 21:48:23 +0000596 if (!canEmbed(face))
597 info->fType = SkAdvancedTypefaceMetrics::kNotEmbeddable_Font;
598
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000599 unref_ft_face(face);
600 return info;
reed@google.com8a5d6922011-03-14 15:08:03 +0000601#endif
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000602}
vandebo@chromium.org37ad8fb2011-08-18 02:38:50 +0000603
reed@google.com618ef5e2011-01-26 22:10:41 +0000604///////////////////////////////////////////////////////////////////////////
605
reed@google.comffe49f52011-11-22 19:42:41 +0000606#define BLACK_LUMINANCE_LIMIT 0x40
607#define WHITE_LUMINANCE_LIMIT 0xA0
608
reed@google.com8ed436c2011-07-21 14:12:36 +0000609static bool bothZero(SkScalar a, SkScalar b) {
610 return 0 == a && 0 == b;
611}
612
613// returns false if there is any non-90-rotation or skew
614static bool isAxisAligned(const SkScalerContext::Rec& rec) {
615 return 0 == rec.fPreSkewX &&
616 (bothZero(rec.fPost2x2[0][1], rec.fPost2x2[1][0]) ||
617 bothZero(rec.fPost2x2[0][0], rec.fPost2x2[1][1]));
618}
619
reed@google.com618ef5e2011-01-26 22:10:41 +0000620void SkFontHost::FilterRec(SkScalerContext::Rec* rec) {
621 if (!gLCDSupportValid) {
622 InitFreetype();
623 FT_Done_FreeType(gFTLibrary);
624 }
reed@google.com5b31b0f2011-02-23 14:41:42 +0000625
reed@google.comeffc5012011-06-27 16:44:46 +0000626 if (!gLCDSupport && isLCD(*rec)) {
reed@google.com618ef5e2011-01-26 22:10:41 +0000627 // If the runtime Freetype library doesn't support LCD mode, we disable
628 // it here.
629 rec->fMaskFormat = SkMask::kA8_Format;
630 }
reed@google.com5b31b0f2011-02-23 14:41:42 +0000631
reed@google.com618ef5e2011-01-26 22:10:41 +0000632 SkPaint::Hinting h = rec->getHinting();
reed@google.comeffc5012011-06-27 16:44:46 +0000633 if (SkPaint::kFull_Hinting == h && !isLCD(*rec)) {
reed@google.com618ef5e2011-01-26 22:10:41 +0000634 // collapse full->normal hinting if we're not doing LCD
635 h = SkPaint::kNormal_Hinting;
636 } else if ((rec->fFlags & SkScalerContext::kSubpixelPositioning_Flag) &&
637 SkPaint::kNo_Hinting != h) {
638 // to do subpixel, we must have at most slight hinting
639 h = SkPaint::kSlight_Hinting;
640 }
vandebo@chromium.org37ad8fb2011-08-18 02:38:50 +0000641#ifndef SK_IGNORE_ROTATED_FREETYPE_FIX
reed@google.com8ed436c2011-07-21 14:12:36 +0000642 // rotated text looks bad with hinting, so we disable it as needed
643 if (!isAxisAligned(*rec)) {
644 h = SkPaint::kNo_Hinting;
645 }
bsalomon@google.com0e35ca82011-07-22 17:56:19 +0000646#endif
reed@google.com618ef5e2011-01-26 22:10:41 +0000647 rec->setHinting(h);
reed@google.comffe49f52011-11-22 19:42:41 +0000648
649 // for compatibility at the moment, discretize luminance to 3 settings
650 // black, white, gray. This helps with fontcache utilization, since we
651 // won't create multiple entries that in the end map to the same results.
652 {
653 unsigned lum = rec->getLuminanceByte();
654 if (gGammaTables[0] || gGammaTables[1]) {
655 if (lum <= BLACK_LUMINANCE_LIMIT) {
656 lum = 0;
657 } else if (lum >= WHITE_LUMINANCE_LIMIT) {
658 lum = SkScalerContext::kLuminance_Max;
659 } else {
660 lum = SkScalerContext::kLuminance_Max >> 1;
661 }
662 } else {
663 lum = 0; // no gamma correct, so use 0 since SkPaint uses that
664 // when measuring text w/o regard for luminance
665 }
666 rec->setLuminanceBits(lum);
667 }
reed@google.com618ef5e2011-01-26 22:10:41 +0000668}
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000669
djsollen@google.comda957722011-11-16 17:00:46 +0000670#ifdef SK_BUILD_FOR_ANDROID
djsollen@google.comcd9d69b2011-03-14 20:30:14 +0000671uint32_t SkFontHost::GetUnitsPerEm(SkFontID fontID) {
672 SkAutoMutexAcquire ac(gFTMutex);
673 SkFaceRec *rec = ref_ft_face(fontID);
674 uint16_t unitsPerEm = 0;
675
676 if (rec != NULL && rec->fFace != NULL) {
677 unitsPerEm = rec->fFace->units_per_EM;
678 unref_ft_face(rec->fFace);
679 }
680
681 return (uint32_t)unitsPerEm;
682}
683#endif
684
reed@android.com8a1c16f2008-12-17 15:59:43 +0000685SkScalerContext_FreeType::SkScalerContext_FreeType(const SkDescriptor* desc)
reed@android.com62900b42009-02-11 15:07:19 +0000686 : SkScalerContext(desc) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000687 SkAutoMutexAcquire ac(gFTMutex);
688
reed@android.com8a1c16f2008-12-17 15:59:43 +0000689 if (gFTCount == 0) {
reed@android.com659aaf92009-07-23 15:20:21 +0000690 if (!InitFreetype()) {
691 sk_throw();
692 }
reed@google.comffe49f52011-11-22 19:42:41 +0000693 SkFontHost::GetGammaTables(gGammaTables);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000694 }
695 ++gFTCount;
696
697 // load the font file
reed@android.com62900b42009-02-11 15:07:19 +0000698 fFTSize = NULL;
699 fFace = NULL;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000700 fFaceRec = ref_ft_face(fRec.fFontID);
reed@android.com62900b42009-02-11 15:07:19 +0000701 if (NULL == fFaceRec) {
702 return;
703 }
704 fFace = fFaceRec->fFace;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000705
706 // compute our factors from the record
707
708 SkMatrix m;
709
710 fRec.getSingleMatrix(&m);
711
712#ifdef DUMP_STRIKE_CREATION
713 SkString keyString;
714 SkFontHost::GetDescriptorKeyString(desc, &keyString);
715 printf("========== strike [%g %g %g] [%g %g %g %g] hints %d format %d %s\n", SkScalarToFloat(fRec.fTextSize),
716 SkScalarToFloat(fRec.fPreScaleX), SkScalarToFloat(fRec.fPreSkewX),
717 SkScalarToFloat(fRec.fPost2x2[0][0]), SkScalarToFloat(fRec.fPost2x2[0][1]),
718 SkScalarToFloat(fRec.fPost2x2[1][0]), SkScalarToFloat(fRec.fPost2x2[1][1]),
agl@chromium.org309485b2009-07-21 17:41:32 +0000719 fRec.getHinting(), fRec.fMaskFormat, keyString.c_str());
reed@android.com8a1c16f2008-12-17 15:59:43 +0000720#endif
721
722 // now compute our scale factors
723 SkScalar sx = m.getScaleX();
724 SkScalar sy = m.getScaleY();
725
726 if (m.getSkewX() || m.getSkewY() || sx < 0 || sy < 0) {
727 // sort of give up on hinting
728 sx = SkMaxScalar(SkScalarAbs(sx), SkScalarAbs(m.getSkewX()));
729 sy = SkMaxScalar(SkScalarAbs(m.getSkewY()), SkScalarAbs(sy));
730 sx = sy = SkScalarAve(sx, sy);
731
732 SkScalar inv = SkScalarInvert(sx);
733
734 // flip the skew elements to go from our Y-down system to FreeType's
735 fMatrix22.xx = SkScalarToFixed(SkScalarMul(m.getScaleX(), inv));
736 fMatrix22.xy = -SkScalarToFixed(SkScalarMul(m.getSkewX(), inv));
737 fMatrix22.yx = -SkScalarToFixed(SkScalarMul(m.getSkewY(), inv));
738 fMatrix22.yy = SkScalarToFixed(SkScalarMul(m.getScaleY(), inv));
739 } else {
740 fMatrix22.xx = fMatrix22.yy = SK_Fixed1;
741 fMatrix22.xy = fMatrix22.yx = 0;
742 }
743
744 fScaleX = SkScalarToFixed(sx);
745 fScaleY = SkScalarToFixed(sy);
746
747 // compute the flags we send to Load_Glyph
748 {
reed@android.come4d0bc02009-07-24 19:53:20 +0000749 FT_Int32 loadFlags = FT_LOAD_DEFAULT;
reed@google.combdc99882011-11-21 14:36:57 +0000750 bool linearMetrics = false;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000751
agl@chromium.org70a303f2010-05-10 14:15:50 +0000752 if (SkMask::kBW_Format == fRec.fMaskFormat) {
753 // See http://code.google.com/p/chromium/issues/detail?id=43252#c24
754 loadFlags = FT_LOAD_TARGET_MONO;
reed@google.comeffc5012011-06-27 16:44:46 +0000755 if (fRec.getHinting() == SkPaint::kNo_Hinting) {
agl@chromium.org70a303f2010-05-10 14:15:50 +0000756 loadFlags = FT_LOAD_NO_HINTING;
reed@google.combdc99882011-11-21 14:36:57 +0000757 linearMetrics = true;
reed@google.comeffc5012011-06-27 16:44:46 +0000758 }
agl@chromium.org70a303f2010-05-10 14:15:50 +0000759 } else {
760 switch (fRec.getHinting()) {
761 case SkPaint::kNo_Hinting:
762 loadFlags = FT_LOAD_NO_HINTING;
reed@google.combdc99882011-11-21 14:36:57 +0000763 linearMetrics = true;
agl@chromium.org70a303f2010-05-10 14:15:50 +0000764 break;
765 case SkPaint::kSlight_Hinting:
766 loadFlags = FT_LOAD_TARGET_LIGHT; // This implies FORCE_AUTOHINT
reed@google.combdc99882011-11-21 14:36:57 +0000767 linearMetrics = true;
agl@chromium.org70a303f2010-05-10 14:15:50 +0000768 break;
769 case SkPaint::kNormal_Hinting:
agl@chromium.orga2c71cb2010-06-17 20:49:17 +0000770 if (fRec.fFlags & SkScalerContext::kAutohinting_Flag)
771 loadFlags = FT_LOAD_FORCE_AUTOHINT;
772 else
773 loadFlags = FT_LOAD_NO_AUTOHINT;
agl@chromium.org70a303f2010-05-10 14:15:50 +0000774 break;
775 case SkPaint::kFull_Hinting:
agl@chromium.orga2c71cb2010-06-17 20:49:17 +0000776 if (fRec.fFlags & SkScalerContext::kAutohinting_Flag) {
777 loadFlags = FT_LOAD_FORCE_AUTOHINT;
778 break;
779 }
agl@chromium.org70a303f2010-05-10 14:15:50 +0000780 loadFlags = FT_LOAD_TARGET_NORMAL;
reed@google.comeffc5012011-06-27 16:44:46 +0000781 if (isLCD(fRec)) {
782 if (fRec.fFlags & SkScalerContext::kLCD_Vertical_Flag) {
783 loadFlags = FT_LOAD_TARGET_LCD_V;
784 } else {
785 loadFlags = FT_LOAD_TARGET_LCD;
786 }
reed@google.comea2333d2011-03-14 16:44:56 +0000787 }
agl@chromium.org70a303f2010-05-10 14:15:50 +0000788 break;
789 default:
790 SkDebugf("---------- UNKNOWN hinting %d\n", fRec.getHinting());
791 break;
792 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000793 }
794
reed@google.comeffc5012011-06-27 16:44:46 +0000795 if ((fRec.fFlags & SkScalerContext::kEmbeddedBitmapText_Flag) == 0) {
agl@chromium.orge0d08992009-08-07 19:19:23 +0000796 loadFlags |= FT_LOAD_NO_BITMAP;
reed@google.comeffc5012011-06-27 16:44:46 +0000797 }
agl@chromium.orge0d08992009-08-07 19:19:23 +0000798
reed@google.com96a9f7912011-05-06 11:49:30 +0000799 // Always using FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH to get correct
800 // advances, as fontconfig and cairo do.
801 // See http://code.google.com/p/skia/issues/detail?id=222.
802 loadFlags |= FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH;
803
reed@android.come4d0bc02009-07-24 19:53:20 +0000804 fLoadGlyphFlags = loadFlags;
reed@google.combdc99882011-11-21 14:36:57 +0000805 fDoLinearMetrics = linearMetrics;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000806 }
807
808 // now create the FT_Size
809
810 {
811 FT_Error err;
812
813 err = FT_New_Size(fFace, &fFTSize);
814 if (err != 0) {
815 SkDEBUGF(("SkScalerContext_FreeType::FT_New_Size(%x): FT_Set_Char_Size(0x%x, 0x%x) returned 0x%x\n",
816 fFaceRec->fFontID, fScaleX, fScaleY, err));
817 fFace = NULL;
818 return;
819 }
820
821 err = FT_Activate_Size(fFTSize);
822 if (err != 0) {
823 SkDEBUGF(("SkScalerContext_FreeType::FT_Activate_Size(%x, 0x%x, 0x%x) returned 0x%x\n",
824 fFaceRec->fFontID, fScaleX, fScaleY, err));
825 fFTSize = NULL;
826 }
827
828 err = FT_Set_Char_Size( fFace,
829 SkFixedToFDot6(fScaleX), SkFixedToFDot6(fScaleY),
830 72, 72);
831 if (err != 0) {
832 SkDEBUGF(("SkScalerContext_FreeType::FT_Set_Char_Size(%x, 0x%x, 0x%x) returned 0x%x\n",
833 fFaceRec->fFontID, fScaleX, fScaleY, err));
834 fFace = NULL;
835 return;
836 }
837
838 FT_Set_Transform( fFace, &fMatrix22, NULL);
839 }
840}
841
842SkScalerContext_FreeType::~SkScalerContext_FreeType() {
843 if (fFTSize != NULL) {
844 FT_Done_Size(fFTSize);
845 }
846
847 SkAutoMutexAcquire ac(gFTMutex);
848
849 if (fFace != NULL) {
850 unref_ft_face(fFace);
851 }
852 if (--gFTCount == 0) {
853// SkDEBUGF(("FT_Done_FreeType\n"));
854 FT_Done_FreeType(gFTLibrary);
855 SkDEBUGCODE(gFTLibrary = NULL;)
856 }
857}
858
859/* We call this before each use of the fFace, since we may be sharing
860 this face with other context (at different sizes).
861*/
862FT_Error SkScalerContext_FreeType::setupSize() {
863 /* In the off-chance that a font has been removed, we want to error out
864 right away, so call resolve just to be sure.
865
866 TODO: perhaps we can skip this, by walking the global font cache and
867 killing all of the contexts when we know that a given fontID is going
868 away...
869 */
reed@android.comb1d9d2e2009-03-04 17:37:51 +0000870 if (!SkFontHost::ValidFontID(fRec.fFontID)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000871 return (FT_Error)-1;
872 }
873
874 FT_Error err = FT_Activate_Size(fFTSize);
875
876 if (err != 0) {
877 SkDEBUGF(("SkScalerContext_FreeType::FT_Activate_Size(%x, 0x%x, 0x%x) returned 0x%x\n",
878 fFaceRec->fFontID, fScaleX, fScaleY, err));
879 fFTSize = NULL;
880 } else {
881 // seems we need to reset this every time (not sure why, but without it
882 // I get random italics from some other fFTSize)
883 FT_Set_Transform( fFace, &fMatrix22, NULL);
884 }
885 return err;
886}
887
senorblanco@chromium.org4526a842010-02-05 23:08:20 +0000888void SkScalerContext_FreeType::emboldenOutline(FT_Outline* outline) {
889 FT_Pos strength;
890 strength = FT_MulFix(fFace->units_per_EM, fFace->size->metrics.y_scale)
891 / 24;
892 FT_Outline_Embolden(outline, strength);
893}
894
ctguil@chromium.org0bc7bf52011-03-04 19:04:57 +0000895unsigned SkScalerContext_FreeType::generateGlyphCount() {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000896 return fFace->num_glyphs;
897}
898
899uint16_t SkScalerContext_FreeType::generateCharToGlyph(SkUnichar uni) {
900 return SkToU16(FT_Get_Char_Index( fFace, uni ));
901}
902
reed@android.com9d3a9852010-01-08 14:07:42 +0000903SkUnichar SkScalerContext_FreeType::generateGlyphToChar(uint16_t glyph) {
904 // iterate through each cmap entry, looking for matching glyph indices
905 FT_UInt glyphIndex;
906 SkUnichar charCode = FT_Get_First_Char( fFace, &glyphIndex );
907
908 while (glyphIndex != 0) {
909 if (glyphIndex == glyph) {
910 return charCode;
911 }
912 charCode = FT_Get_Next_Char( fFace, charCode, &glyphIndex );
913 }
914
915 return 0;
916}
917
reed@android.com8a1c16f2008-12-17 15:59:43 +0000918static FT_Pixel_Mode compute_pixel_mode(SkMask::Format format) {
919 switch (format) {
920 case SkMask::kBW_Format:
921 return FT_PIXEL_MODE_MONO;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000922 case SkMask::kA8_Format:
923 default:
924 return FT_PIXEL_MODE_GRAY;
925 }
926}
927
reed@android.com8a1c16f2008-12-17 15:59:43 +0000928void SkScalerContext_FreeType::generateAdvance(SkGlyph* glyph) {
929#ifdef FT_ADVANCES_H
930 /* unhinted and light hinted text have linearly scaled advances
931 * which are very cheap to compute with some font formats...
932 */
reed@google.combdc99882011-11-21 14:36:57 +0000933 if (fDoLinearMetrics) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000934 SkAutoMutexAcquire ac(gFTMutex);
935
936 if (this->setupSize()) {
reed@android.com62900b42009-02-11 15:07:19 +0000937 glyph->zeroMetrics();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000938 return;
939 }
940
941 FT_Error error;
942 FT_Fixed advance;
943
944 error = FT_Get_Advance( fFace, glyph->getGlyphID(fBaseGlyphCount),
945 fLoadGlyphFlags | FT_ADVANCE_FLAG_FAST_ONLY,
946 &advance );
947 if (0 == error) {
948 glyph->fRsbDelta = 0;
949 glyph->fLsbDelta = 0;
950 glyph->fAdvanceX = advance; // advance *2/3; //DEBUG
951 glyph->fAdvanceY = 0;
952 return;
953 }
954 }
955#endif /* FT_ADVANCES_H */
956 /* otherwise, we need to load/hint the glyph, which is slower */
957 this->generateMetrics(glyph);
958 return;
959}
960
961void SkScalerContext_FreeType::generateMetrics(SkGlyph* glyph) {
962 SkAutoMutexAcquire ac(gFTMutex);
963
964 glyph->fRsbDelta = 0;
965 glyph->fLsbDelta = 0;
966
967 FT_Error err;
968
969 if (this->setupSize()) {
970 goto ERROR;
971 }
972
973 err = FT_Load_Glyph( fFace, glyph->getGlyphID(fBaseGlyphCount), fLoadGlyphFlags );
974 if (err != 0) {
975 SkDEBUGF(("SkScalerContext_FreeType::generateMetrics(%x): FT_Load_Glyph(glyph:%d flags:%d) returned 0x%x\n",
976 fFaceRec->fFontID, glyph->getGlyphID(fBaseGlyphCount), fLoadGlyphFlags, err));
977 ERROR:
reed@android.com62900b42009-02-11 15:07:19 +0000978 glyph->zeroMetrics();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000979 return;
980 }
981
982 switch ( fFace->glyph->format ) {
thakis@chromium.org598b8592011-05-24 05:42:55 +0000983 case FT_GLYPH_FORMAT_OUTLINE: {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000984 FT_BBox bbox;
985
bungeman@google.com0f0c2882011-11-04 15:47:41 +0000986 if (0 == fFace->glyph->outline.n_contours) {
987 glyph->fWidth = 0;
988 glyph->fHeight = 0;
989 glyph->fTop = 0;
990 glyph->fLeft = 0;
991 break;
992 }
993
senorblanco@chromium.org4526a842010-02-05 23:08:20 +0000994 if (fRec.fFlags & kEmbolden_Flag) {
995 emboldenOutline(&fFace->glyph->outline);
996 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000997 FT_Outline_Get_CBox(&fFace->glyph->outline, &bbox);
998
agl@chromium.orga2c71cb2010-06-17 20:49:17 +0000999 if (fRec.fFlags & SkScalerContext::kSubpixelPositioning_Flag) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001000 int dx = glyph->getSubXFixed() >> 10;
1001 int dy = glyph->getSubYFixed() >> 10;
1002 // negate dy since freetype-y-goes-up and skia-y-goes-down
1003 bbox.xMin += dx;
1004 bbox.yMin -= dy;
1005 bbox.xMax += dx;
1006 bbox.yMax -= dy;
1007 }
1008
1009 bbox.xMin &= ~63;
1010 bbox.yMin &= ~63;
1011 bbox.xMax = (bbox.xMax + 63) & ~63;
1012 bbox.yMax = (bbox.yMax + 63) & ~63;
1013
1014 glyph->fWidth = SkToU16((bbox.xMax - bbox.xMin) >> 6);
1015 glyph->fHeight = SkToU16((bbox.yMax - bbox.yMin) >> 6);
1016 glyph->fTop = -SkToS16(bbox.yMax >> 6);
1017 glyph->fLeft = SkToS16(bbox.xMin >> 6);
1018 break;
thakis@chromium.org598b8592011-05-24 05:42:55 +00001019 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001020
1021 case FT_GLYPH_FORMAT_BITMAP:
agl@chromium.orge76073b2010-06-04 20:31:17 +00001022 if (fRec.fFlags & kEmbolden_Flag) {
1023 FT_GlyphSlot_Own_Bitmap(fFace->glyph);
1024 FT_Bitmap_Embolden(gFTLibrary, &fFace->glyph->bitmap, kBitmapEmboldenStrength, 0);
1025 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001026 glyph->fWidth = SkToU16(fFace->glyph->bitmap.width);
1027 glyph->fHeight = SkToU16(fFace->glyph->bitmap.rows);
1028 glyph->fTop = -SkToS16(fFace->glyph->bitmap_top);
1029 glyph->fLeft = SkToS16(fFace->glyph->bitmap_left);
1030 break;
1031
1032 default:
tomhudson@google.com0c00f212011-12-28 14:59:50 +00001033 SkDEBUGFAIL("unknown glyph format");
reed@android.com8a1c16f2008-12-17 15:59:43 +00001034 goto ERROR;
1035 }
1036
agl@chromium.orga2c71cb2010-06-17 20:49:17 +00001037 if ((fRec.fFlags & SkScalerContext::kSubpixelPositioning_Flag) == 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001038 glyph->fAdvanceX = SkFDot6ToFixed(fFace->glyph->advance.x);
1039 glyph->fAdvanceY = -SkFDot6ToFixed(fFace->glyph->advance.y);
1040 if (fRec.fFlags & kDevKernText_Flag) {
1041 glyph->fRsbDelta = SkToS8(fFace->glyph->rsb_delta);
1042 glyph->fLsbDelta = SkToS8(fFace->glyph->lsb_delta);
1043 }
1044 } else {
1045 glyph->fAdvanceX = SkFixedMul(fMatrix22.xx, fFace->glyph->linearHoriAdvance);
1046 glyph->fAdvanceY = -SkFixedMul(fMatrix22.yx, fFace->glyph->linearHoriAdvance);
1047 }
1048
1049#ifdef ENABLE_GLYPH_SPEW
1050 SkDEBUGF(("FT_Set_Char_Size(this:%p sx:%x sy:%x ", this, fScaleX, fScaleY));
1051 SkDEBUGF(("Metrics(glyph:%d flags:0x%x) w:%d\n", glyph->getGlyphID(fBaseGlyphCount), fLoadGlyphFlags, glyph->fWidth));
1052#endif
1053}
1054
reed@google.combde3c8e2011-05-18 11:58:10 +00001055static int lerp(int start, int end) {
1056 SkASSERT((unsigned)SK_FREETYPE_LCD_LERP <= 256);
1057 return start + ((end - start) * (SK_FREETYPE_LCD_LERP) >> 8);
reed@google.comc5181342011-05-17 20:52:46 +00001058}
1059
1060static uint16_t packTriple(unsigned r, unsigned g, unsigned b) {
1061 if (SK_FREETYPE_LCD_LERP) {
reed@google.combde3c8e2011-05-18 11:58:10 +00001062 // want (a+b+c)/3, but we approx to avoid the divide
1063 unsigned ave = (5 * (r + g + b) + b) >> 4;
1064 r = lerp(r, ave);
1065 g = lerp(g, ave);
1066 b = lerp(b, ave);
reed@google.comc5181342011-05-17 20:52:46 +00001067 }
1068 return SkPackRGB16(r >> 3, g >> 2, b >> 3);
1069}
1070
reed@google.com73824072011-06-23 13:17:30 +00001071static uint16_t grayToRGB16(U8CPU gray) {
1072 SkASSERT(gray <= 255);
1073 return SkPackRGB16(gray >> 3, gray >> 2, gray >> 3);
1074}
1075
1076static int bittst(const uint8_t data[], int bitOffset) {
1077 SkASSERT(bitOffset >= 0);
reed@google.comc8e0f932011-06-23 19:39:49 +00001078 int lowBit = data[bitOffset >> 3] >> (~bitOffset & 7);
reed@google.com73824072011-06-23 13:17:30 +00001079 return lowBit & 1;
1080}
1081
reed@google.comeffc5012011-06-27 16:44:46 +00001082static void copyFT2LCD16(const SkGlyph& glyph, const FT_Bitmap& bitmap,
1083 int lcdIsBGR) {
reed@google.comea2333d2011-03-14 16:44:56 +00001084 SkASSERT(glyph.fHeight == bitmap.rows);
reed@google.comea2333d2011-03-14 16:44:56 +00001085 uint16_t* dst = reinterpret_cast<uint16_t*>(glyph.fImage);
reed@google.com73824072011-06-23 13:17:30 +00001086 const size_t dstRB = glyph.rowBytes();
1087 const int width = glyph.fWidth;
1088 const uint8_t* src = bitmap.buffer;
reed@google.comea2333d2011-03-14 16:44:56 +00001089
reed@google.com73824072011-06-23 13:17:30 +00001090 switch (bitmap.pixel_mode) {
1091 case FT_PIXEL_MODE_MONO: {
1092 for (int y = 0; y < glyph.fHeight; ++y) {
1093 for (int x = 0; x < width; ++x) {
1094 dst[x] = -bittst(src, x);
1095 }
1096 dst = (uint16_t*)((char*)dst + dstRB);
1097 src += bitmap.pitch;
1098 }
1099 } break;
1100 case FT_PIXEL_MODE_GRAY: {
1101 for (int y = 0; y < glyph.fHeight; ++y) {
1102 for (int x = 0; x < width; ++x) {
1103 dst[x] = grayToRGB16(src[x]);
1104 }
1105 dst = (uint16_t*)((char*)dst + dstRB);
1106 src += bitmap.pitch;
1107 }
1108 } break;
1109 default: {
1110 SkASSERT(glyph.fWidth * 3 == bitmap.width - 6);
1111 src += 3;
1112 for (int y = 0; y < glyph.fHeight; y++) {
1113 const uint8_t* triple = src;
reed@google.comeffc5012011-06-27 16:44:46 +00001114 if (lcdIsBGR) {
1115 for (int x = 0; x < width; x++) {
1116 dst[x] = packTriple(triple[2], triple[1], triple[0]);
1117 triple += 3;
1118 }
1119 } else {
1120 for (int x = 0; x < width; x++) {
1121 dst[x] = packTriple(triple[0], triple[1], triple[2]);
1122 triple += 3;
1123 }
reed@google.com73824072011-06-23 13:17:30 +00001124 }
1125 src += bitmap.pitch;
1126 dst = (uint16_t*)((char*)dst + dstRB);
1127 }
1128 } break;
reed@google.comea2333d2011-03-14 16:44:56 +00001129 }
1130}
1131
reed@android.com8a1c16f2008-12-17 15:59:43 +00001132void SkScalerContext_FreeType::generateImage(const SkGlyph& glyph) {
1133 SkAutoMutexAcquire ac(gFTMutex);
1134
1135 FT_Error err;
1136
1137 if (this->setupSize()) {
1138 goto ERROR;
1139 }
1140
1141 err = FT_Load_Glyph( fFace, glyph.getGlyphID(fBaseGlyphCount), fLoadGlyphFlags);
1142 if (err != 0) {
1143 SkDEBUGF(("SkScalerContext_FreeType::generateImage: FT_Load_Glyph(glyph:%d width:%d height:%d rb:%d flags:%d) returned 0x%x\n",
1144 glyph.getGlyphID(fBaseGlyphCount), glyph.fWidth, glyph.fHeight, glyph.rowBytes(), fLoadGlyphFlags, err));
1145 ERROR:
1146 memset(glyph.fImage, 0, glyph.rowBytes() * glyph.fHeight);
1147 return;
1148 }
1149
1150 switch ( fFace->glyph->format ) {
1151 case FT_GLYPH_FORMAT_OUTLINE: {
1152 FT_Outline* outline = &fFace->glyph->outline;
1153 FT_BBox bbox;
1154 FT_Bitmap target;
1155
senorblanco@chromium.org4526a842010-02-05 23:08:20 +00001156 if (fRec.fFlags & kEmbolden_Flag) {
1157 emboldenOutline(outline);
1158 }
1159
reed@android.com8a1c16f2008-12-17 15:59:43 +00001160 int dx = 0, dy = 0;
agl@chromium.orga2c71cb2010-06-17 20:49:17 +00001161 if (fRec.fFlags & SkScalerContext::kSubpixelPositioning_Flag) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001162 dx = glyph.getSubXFixed() >> 10;
1163 dy = glyph.getSubYFixed() >> 10;
1164 // negate dy since freetype-y-goes-up and skia-y-goes-down
1165 dy = -dy;
1166 }
1167 FT_Outline_Get_CBox(outline, &bbox);
1168 /*
1169 what we really want to do for subpixel is
1170 offset(dx, dy)
1171 compute_bounds
1172 offset(bbox & !63)
1173 but that is two calls to offset, so we do the following, which
1174 achieves the same thing with only one offset call.
1175 */
1176 FT_Outline_Translate(outline, dx - ((bbox.xMin + dx) & ~63),
1177 dy - ((bbox.yMin + dy) & ~63));
1178
reed@google.comea2333d2011-03-14 16:44:56 +00001179 if (SkMask::kLCD16_Format == glyph.fMaskFormat) {
1180 FT_Render_Glyph(fFace->glyph, FT_RENDER_MODE_LCD);
reed@google.comeffc5012011-06-27 16:44:46 +00001181 copyFT2LCD16(glyph, fFace->glyph->bitmap,
1182 fRec.fFlags & SkScalerContext::kLCD_BGROrder_Flag);
reed@google.comea2333d2011-03-14 16:44:56 +00001183 } else {
1184 target.width = glyph.fWidth;
1185 target.rows = glyph.fHeight;
1186 target.pitch = glyph.rowBytes();
1187 target.buffer = reinterpret_cast<uint8_t*>(glyph.fImage);
1188 target.pixel_mode = compute_pixel_mode(
1189 (SkMask::Format)fRec.fMaskFormat);
1190 target.num_grays = 256;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001191
reed@google.comea2333d2011-03-14 16:44:56 +00001192 memset(glyph.fImage, 0, glyph.rowBytes() * glyph.fHeight);
1193 FT_Outline_Get_Bitmap(gFTLibrary, outline, &target);
1194 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001195 } break;
1196
1197 case FT_GLYPH_FORMAT_BITMAP: {
agl@chromium.orge76073b2010-06-04 20:31:17 +00001198 if (fRec.fFlags & kEmbolden_Flag) {
1199 FT_GlyphSlot_Own_Bitmap(fFace->glyph);
1200 FT_Bitmap_Embolden(gFTLibrary, &fFace->glyph->bitmap, kBitmapEmboldenStrength, 0);
1201 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001202 SkASSERT_CONTINUE(glyph.fWidth == fFace->glyph->bitmap.width);
1203 SkASSERT_CONTINUE(glyph.fHeight == fFace->glyph->bitmap.rows);
1204 SkASSERT_CONTINUE(glyph.fTop == -fFace->glyph->bitmap_top);
1205 SkASSERT_CONTINUE(glyph.fLeft == fFace->glyph->bitmap_left);
1206
1207 const uint8_t* src = (const uint8_t*)fFace->glyph->bitmap.buffer;
1208 uint8_t* dst = (uint8_t*)glyph.fImage;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001209
agl@chromium.org558434a2009-08-11 17:22:38 +00001210 if (fFace->glyph->bitmap.pixel_mode == FT_PIXEL_MODE_GRAY ||
1211 (fFace->glyph->bitmap.pixel_mode == FT_PIXEL_MODE_MONO &&
1212 glyph.fMaskFormat == SkMask::kBW_Format)) {
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001213 unsigned srcRowBytes = fFace->glyph->bitmap.pitch;
1214 unsigned dstRowBytes = glyph.rowBytes();
1215 unsigned minRowBytes = SkMin32(srcRowBytes, dstRowBytes);
1216 unsigned extraRowBytes = dstRowBytes - minRowBytes;
1217
1218 for (int y = fFace->glyph->bitmap.rows - 1; y >= 0; --y) {
1219 memcpy(dst, src, minRowBytes);
1220 memset(dst + minRowBytes, 0, extraRowBytes);
1221 src += srcRowBytes;
1222 dst += dstRowBytes;
1223 }
agl@chromium.org558434a2009-08-11 17:22:38 +00001224 } else if (fFace->glyph->bitmap.pixel_mode == FT_PIXEL_MODE_MONO &&
reed@google.comeffc5012011-06-27 16:44:46 +00001225 glyph.fMaskFormat == SkMask::kA8_Format) {
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001226 for (int y = 0; y < fFace->glyph->bitmap.rows; ++y) {
1227 uint8_t byte = 0;
1228 int bits = 0;
1229 const uint8_t* src_row = src;
1230 uint8_t* dst_row = dst;
1231
1232 for (int x = 0; x < fFace->glyph->bitmap.width; ++x) {
1233 if (!bits) {
1234 byte = *src_row++;
1235 bits = 8;
1236 }
1237
1238 *dst_row++ = byte & 0x80 ? 0xff : 0;
1239 bits--;
1240 byte <<= 1;
1241 }
1242
1243 src += fFace->glyph->bitmap.pitch;
1244 dst += glyph.rowBytes();
1245 }
reed@google.com73824072011-06-23 13:17:30 +00001246 } else if (SkMask::kLCD16_Format == glyph.fMaskFormat) {
reed@google.comeffc5012011-06-27 16:44:46 +00001247 copyFT2LCD16(glyph, fFace->glyph->bitmap,
1248 fRec.fFlags & SkScalerContext::kLCD_BGROrder_Flag);
agl@chromium.org558434a2009-08-11 17:22:38 +00001249 } else {
tomhudson@google.com0c00f212011-12-28 14:59:50 +00001250 SkDEBUGFAIL("unknown glyph bitmap transform needed");
reed@android.com8a1c16f2008-12-17 15:59:43 +00001251 }
1252 } break;
1253
1254 default:
tomhudson@google.com0c00f212011-12-28 14:59:50 +00001255 SkDEBUGFAIL("unknown glyph format");
reed@android.com8a1c16f2008-12-17 15:59:43 +00001256 goto ERROR;
1257 }
reed@google.com1f6b4ae2011-11-22 14:20:55 +00001258
reed@google.comffe49f52011-11-22 19:42:41 +00001259 if (gGammaTables[0] || gGammaTables[1]) {
1260 bool isWhite = fRec.getLuminanceByte() >= WHITE_LUMINANCE_LIMIT;
1261 bool isBlack = fRec.getLuminanceByte() <= BLACK_LUMINANCE_LIMIT;
1262 if ((isWhite | isBlack) && SkMask::kA8_Format == glyph.fMaskFormat) {
1263 int index = isBlack ? 0 : 1;
1264 if (gGammaTables[index]) {
1265 const uint8_t* SK_RESTRICT table = gGammaTables[index];
1266 uint8_t* SK_RESTRICT dst = (uint8_t*)glyph.fImage;
1267 unsigned rowBytes = glyph.rowBytes();
1268
1269 for (int y = glyph.fHeight - 1; y >= 0; --y) {
1270 for (int x = glyph.fWidth - 1; x >= 0; --x) {
1271 dst[x] = table[dst[x]];
1272 }
1273 dst += rowBytes;
reed@google.com1f6b4ae2011-11-22 14:20:55 +00001274 }
reed@google.com1f6b4ae2011-11-22 14:20:55 +00001275 }
1276 }
1277 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001278}
1279
1280///////////////////////////////////////////////////////////////////////////////
1281
1282#define ft2sk(x) SkFixedToScalar((x) << 10)
1283
reed@android.com6f252972009-01-14 16:46:16 +00001284#if FREETYPE_MAJOR >= 2 && FREETYPE_MINOR >= 2
reed@android.com8a1c16f2008-12-17 15:59:43 +00001285 #define CONST_PARAM const
1286#else // older freetype doesn't use const here
1287 #define CONST_PARAM
1288#endif
1289
1290static int move_proc(CONST_PARAM FT_Vector* pt, void* ctx) {
1291 SkPath* path = (SkPath*)ctx;
1292 path->close(); // to close the previous contour (if any)
1293 path->moveTo(ft2sk(pt->x), -ft2sk(pt->y));
1294 return 0;
1295}
1296
1297static int line_proc(CONST_PARAM FT_Vector* pt, void* ctx) {
1298 SkPath* path = (SkPath*)ctx;
1299 path->lineTo(ft2sk(pt->x), -ft2sk(pt->y));
1300 return 0;
1301}
1302
1303static int quad_proc(CONST_PARAM FT_Vector* pt0, CONST_PARAM FT_Vector* pt1,
1304 void* ctx) {
1305 SkPath* path = (SkPath*)ctx;
1306 path->quadTo(ft2sk(pt0->x), -ft2sk(pt0->y), ft2sk(pt1->x), -ft2sk(pt1->y));
1307 return 0;
1308}
1309
1310static int cubic_proc(CONST_PARAM FT_Vector* pt0, CONST_PARAM FT_Vector* pt1,
1311 CONST_PARAM FT_Vector* pt2, void* ctx) {
1312 SkPath* path = (SkPath*)ctx;
1313 path->cubicTo(ft2sk(pt0->x), -ft2sk(pt0->y), ft2sk(pt1->x),
1314 -ft2sk(pt1->y), ft2sk(pt2->x), -ft2sk(pt2->y));
1315 return 0;
1316}
1317
1318void SkScalerContext_FreeType::generatePath(const SkGlyph& glyph,
1319 SkPath* path) {
1320 SkAutoMutexAcquire ac(gFTMutex);
1321
1322 SkASSERT(&glyph && path);
1323
1324 if (this->setupSize()) {
1325 path->reset();
1326 return;
1327 }
1328
1329 uint32_t flags = fLoadGlyphFlags;
1330 flags |= FT_LOAD_NO_BITMAP; // ignore embedded bitmaps so we're sure to get the outline
1331 flags &= ~FT_LOAD_RENDER; // don't scan convert (we just want the outline)
1332
1333 FT_Error err = FT_Load_Glyph( fFace, glyph.getGlyphID(fBaseGlyphCount), flags);
1334
1335 if (err != 0) {
1336 SkDEBUGF(("SkScalerContext_FreeType::generatePath: FT_Load_Glyph(glyph:%d flags:%d) returned 0x%x\n",
1337 glyph.getGlyphID(fBaseGlyphCount), flags, err));
1338 path->reset();
1339 return;
1340 }
1341
senorblanco@chromium.org4526a842010-02-05 23:08:20 +00001342 if (fRec.fFlags & kEmbolden_Flag) {
1343 emboldenOutline(&fFace->glyph->outline);
1344 }
1345
reed@android.com8a1c16f2008-12-17 15:59:43 +00001346 FT_Outline_Funcs funcs;
1347
1348 funcs.move_to = move_proc;
1349 funcs.line_to = line_proc;
1350 funcs.conic_to = quad_proc;
1351 funcs.cubic_to = cubic_proc;
1352 funcs.shift = 0;
1353 funcs.delta = 0;
1354
1355 err = FT_Outline_Decompose(&fFace->glyph->outline, &funcs, path);
1356
1357 if (err != 0) {
1358 SkDEBUGF(("SkScalerContext_FreeType::generatePath: FT_Load_Glyph(glyph:%d flags:%d) returned 0x%x\n",
1359 glyph.getGlyphID(fBaseGlyphCount), flags, err));
1360 path->reset();
1361 return;
1362 }
1363
1364 path->close();
1365}
1366
1367void SkScalerContext_FreeType::generateFontMetrics(SkPaint::FontMetrics* mx,
1368 SkPaint::FontMetrics* my) {
1369 if (NULL == mx && NULL == my) {
1370 return;
1371 }
1372
1373 SkAutoMutexAcquire ac(gFTMutex);
1374
1375 if (this->setupSize()) {
reed@android.coma8a8b8b2009-05-04 15:00:11 +00001376 ERROR:
reed@android.com8a1c16f2008-12-17 15:59:43 +00001377 if (mx) {
reed@android.com4516f472009-06-29 16:25:36 +00001378 sk_bzero(mx, sizeof(SkPaint::FontMetrics));
reed@android.com8a1c16f2008-12-17 15:59:43 +00001379 }
1380 if (my) {
reed@android.com4516f472009-06-29 16:25:36 +00001381 sk_bzero(my, sizeof(SkPaint::FontMetrics));
reed@android.com8a1c16f2008-12-17 15:59:43 +00001382 }
1383 return;
1384 }
1385
reed@android.coma8a8b8b2009-05-04 15:00:11 +00001386 FT_Face face = fFace;
1387 int upem = face->units_per_EM;
1388 if (upem <= 0) {
1389 goto ERROR;
1390 }
1391
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001392 SkPoint pts[6];
1393 SkFixed ys[6];
reed@android.com8a1c16f2008-12-17 15:59:43 +00001394 SkFixed scaleY = fScaleY;
1395 SkFixed mxy = fMatrix22.xy;
1396 SkFixed myy = fMatrix22.yy;
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001397 SkScalar xmin = SkIntToScalar(face->bbox.xMin) / upem;
1398 SkScalar xmax = SkIntToScalar(face->bbox.xMax) / upem;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001399
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001400 int leading = face->height - (face->ascender + -face->descender);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001401 if (leading < 0) {
1402 leading = 0;
1403 }
1404
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001405 // Try to get the OS/2 table from the font. This contains the specific
1406 // average font width metrics which Windows uses.
1407 TT_OS2* os2 = (TT_OS2*) FT_Get_Sfnt_Table(face, ft_sfnt_os2);
1408
reed@android.com8a1c16f2008-12-17 15:59:43 +00001409 ys[0] = -face->bbox.yMax;
1410 ys[1] = -face->ascender;
1411 ys[2] = -face->descender;
1412 ys[3] = -face->bbox.yMin;
1413 ys[4] = leading;
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001414 ys[5] = os2 ? os2->xAvgCharWidth : 0;
1415
1416 SkScalar x_height;
1417 if (os2 && os2->sxHeight) {
1418 x_height = SkFixedToScalar(SkMulDiv(fScaleX, os2->sxHeight, upem));
1419 } else {
1420 const FT_UInt x_glyph = FT_Get_Char_Index(fFace, 'x');
1421 if (x_glyph) {
1422 FT_BBox bbox;
1423 FT_Load_Glyph(fFace, x_glyph, fLoadGlyphFlags);
senorblanco@chromium.org4526a842010-02-05 23:08:20 +00001424 if (fRec.fFlags & kEmbolden_Flag) {
1425 emboldenOutline(&fFace->glyph->outline);
1426 }
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001427 FT_Outline_Get_CBox(&fFace->glyph->outline, &bbox);
1428 x_height = SkIntToScalar(bbox.yMax) / 64;
1429 } else {
1430 x_height = 0;
1431 }
1432 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001433
1434 // convert upem-y values into scalar points
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001435 for (int i = 0; i < 6; i++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001436 SkFixed y = SkMulDiv(scaleY, ys[i], upem);
1437 SkFixed x = SkFixedMul(mxy, y);
1438 y = SkFixedMul(myy, y);
1439 pts[i].set(SkFixedToScalar(x), SkFixedToScalar(y));
1440 }
1441
1442 if (mx) {
1443 mx->fTop = pts[0].fX;
1444 mx->fAscent = pts[1].fX;
1445 mx->fDescent = pts[2].fX;
1446 mx->fBottom = pts[3].fX;
1447 mx->fLeading = pts[4].fX;
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001448 mx->fAvgCharWidth = pts[5].fX;
1449 mx->fXMin = xmin;
1450 mx->fXMax = xmax;
1451 mx->fXHeight = x_height;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001452 }
1453 if (my) {
1454 my->fTop = pts[0].fY;
1455 my->fAscent = pts[1].fY;
1456 my->fDescent = pts[2].fY;
1457 my->fBottom = pts[3].fY;
1458 my->fLeading = pts[4].fY;
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001459 my->fAvgCharWidth = pts[5].fY;
1460 my->fXMin = xmin;
1461 my->fXMax = xmax;
1462 my->fXHeight = x_height;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001463 }
1464}
1465
1466////////////////////////////////////////////////////////////////////////
1467////////////////////////////////////////////////////////////////////////
1468
1469SkScalerContext* SkFontHost::CreateScalerContext(const SkDescriptor* desc) {
reed@android.com62900b42009-02-11 15:07:19 +00001470 SkScalerContext_FreeType* c = SkNEW_ARGS(SkScalerContext_FreeType, (desc));
1471 if (!c->success()) {
1472 SkDELETE(c);
1473 c = NULL;
1474 }
1475 return c;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001476}
1477
1478///////////////////////////////////////////////////////////////////////////////
1479
1480/* Export this so that other parts of our FonttHost port can make use of our
1481 ability to extract the name+style from a stream, using FreeType's api.
1482*/
reed@google.com5b31b0f2011-02-23 14:41:42 +00001483SkTypeface::Style find_name_and_attributes(SkStream* stream, SkString* name,
1484 bool* isFixedWidth) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001485 FT_Library library;
reed@android.combfbd4ff2009-07-23 17:44:41 +00001486 if (FT_Init_FreeType(&library)) {
djsollen@google.com7b34ea62011-02-24 16:28:51 +00001487 name->reset();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001488 return SkTypeface::kNormal;
1489 }
1490
1491 FT_Open_Args args;
1492 memset(&args, 0, sizeof(args));
1493
1494 const void* memoryBase = stream->getMemoryBase();
1495 FT_StreamRec streamRec;
1496
1497 if (NULL != memoryBase) {
1498 args.flags = FT_OPEN_MEMORY;
1499 args.memory_base = (const FT_Byte*)memoryBase;
1500 args.memory_size = stream->getLength();
1501 } else {
1502 memset(&streamRec, 0, sizeof(streamRec));
1503 streamRec.size = stream->read(NULL, 0);
1504 streamRec.descriptor.pointer = stream;
1505 streamRec.read = sk_stream_read;
1506 streamRec.close = sk_stream_close;
1507
1508 args.flags = FT_OPEN_STREAM;
1509 args.stream = &streamRec;
1510 }
1511
1512 FT_Face face;
1513 if (FT_Open_Face(library, &args, 0, &face)) {
1514 FT_Done_FreeType(library);
djsollen@google.com7b34ea62011-02-24 16:28:51 +00001515 name->reset();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001516 return SkTypeface::kNormal;
1517 }
1518
1519 name->set(face->family_name);
1520 int style = SkTypeface::kNormal;
1521
1522 if (face->style_flags & FT_STYLE_FLAG_BOLD) {
1523 style |= SkTypeface::kBold;
1524 }
1525 if (face->style_flags & FT_STYLE_FLAG_ITALIC) {
1526 style |= SkTypeface::kItalic;
1527 }
reed@google.com5b31b0f2011-02-23 14:41:42 +00001528 if (isFixedWidth) {
1529 *isFixedWidth = FT_IS_FIXED_WIDTH(face);
1530 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001531
1532 FT_Done_Face(face);
1533 FT_Done_FreeType(library);
1534 return (SkTypeface::Style)style;
1535}