blob: 025c68668f838c394e32d38632bf3fe5bb07074f [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"
bungeman@google.com3aacb412012-03-13 14:55:12 +000015#include "SkFloatingPoint.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000016#include "SkFontHost.h"
17#include "SkMask.h"
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +000018#include "SkAdvancedTypefaceMetrics.h"
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000019#include "SkScalerContext.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000020#include "SkStream.h"
21#include "SkString.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000022#include "SkTemplates.h"
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000023#include "SkThread.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000024
25#include <ft2build.h>
26#include FT_FREETYPE_H
27#include FT_OUTLINE_H
28#include FT_SIZES_H
agl@chromium.orgcc3096b2009-04-22 22:09:04 +000029#include FT_TRUETYPE_TABLES_H
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000030#include FT_TYPE1_TABLES_H
agl@chromium.orge76073b2010-06-04 20:31:17 +000031#include FT_BITMAP_H
agl@chromium.org36bb6972010-06-04 20:57:16 +000032// In the past, FT_GlyphSlot_Own_Bitmap was defined in this header file.
33#include FT_SYNTHESIS_H
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000034#include FT_XFREE86_H
epoger@google.com5070d792011-06-29 20:43:14 +000035#ifdef FT_LCD_FILTER_H
agl@chromium.org309485b2009-07-21 17:41:32 +000036#include FT_LCD_FILTER_H
epoger@google.com5070d792011-06-29 20:43:14 +000037#endif
agl@chromium.org309485b2009-07-21 17:41:32 +000038
reed@android.com8a1c16f2008-12-17 15:59:43 +000039#ifdef FT_ADVANCES_H
40#include FT_ADVANCES_H
41#endif
42
agl@chromium.orgcc3096b2009-04-22 22:09:04 +000043#if 0
44// Also include the files by name for build tools which require this.
45#include <freetype/freetype.h>
46#include <freetype/ftoutln.h>
47#include <freetype/ftsizes.h>
48#include <freetype/tttables.h>
49#include <freetype/ftadvanc.h>
agl@chromium.org309485b2009-07-21 17:41:32 +000050#include <freetype/ftlcdfil.h>
agl@chromium.orge76073b2010-06-04 20:31:17 +000051#include <freetype/ftbitmap.h>
agl@chromium.org36bb6972010-06-04 20:57:16 +000052#include <freetype/ftsynth.h>
agl@chromium.orgcc3096b2009-04-22 22:09:04 +000053#endif
54
reed@android.com8a1c16f2008-12-17 15:59:43 +000055//#define ENABLE_GLYPH_SPEW // for tracing calls
56//#define DUMP_STRIKE_CREATION
57
reed@google.com1ac83502012-02-28 17:06:02 +000058//#define SK_GAMMA_APPLY_TO_A8
bungeman@google.com3aacb412012-03-13 14:55:12 +000059//#define SK_GAMMA_SRGB
reed@google.comec9ed812012-03-01 19:40:21 +000060
61#ifndef SK_GAMMA_CONTRAST
62 #define SK_GAMMA_CONTRAST 0x66
63#endif
64#ifndef SK_GAMMA_EXPONENT
65 #define SK_GAMMA_EXPONENT 2.2
66#endif
reed@google.com1ac83502012-02-28 17:06:02 +000067
reed@android.com8a1c16f2008-12-17 15:59:43 +000068#ifdef SK_DEBUG
69 #define SkASSERT_CONTINUE(pred) \
70 do { \
71 if (!(pred)) \
72 SkDebugf("file %s:%d: assert failed '" #pred "'\n", __FILE__, __LINE__); \
73 } while (false)
74#else
75 #define SkASSERT_CONTINUE(pred)
76#endif
77
vandebo@chromium.org6f72d1e2011-02-14 23:19:59 +000078using namespace skia_advanced_typeface_metrics_utils;
79
reed@google.comeffc5012011-06-27 16:44:46 +000080static bool isLCD(const SkScalerContext::Rec& rec) {
81 switch (rec.fMaskFormat) {
82 case SkMask::kLCD16_Format:
83 case SkMask::kLCD32_Format:
84 return true;
85 default:
86 return false;
87 }
88}
89
reed@android.com8a1c16f2008-12-17 15:59:43 +000090//////////////////////////////////////////////////////////////////////////
91
92struct SkFaceRec;
93
digit@google.com1771cbf2012-01-26 21:26:40 +000094SK_DECLARE_STATIC_MUTEX(gFTMutex);
reed@android.com8a1c16f2008-12-17 15:59:43 +000095static int gFTCount;
96static FT_Library gFTLibrary;
97static SkFaceRec* gFaceRecHead;
agl@chromium.orgf18d8762009-07-28 18:38:08 +000098static bool gLCDSupportValid; // true iff |gLCDSupport| has been set.
99static bool gLCDSupport; // true iff LCD is supported by the runtime.
reed@google.coma1c32562012-03-01 19:38:23 +0000100static int gLCDExtra; // number of extra pixels for filtering.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000101
reed@google.comffe49f52011-11-22 19:42:41 +0000102static const uint8_t* gGammaTables[2];
103
reed@android.com8a1c16f2008-12-17 15:59:43 +0000104/////////////////////////////////////////////////////////////////////////
105
agl@chromium.orge76073b2010-06-04 20:31:17 +0000106// See http://freetype.sourceforge.net/freetype2/docs/reference/ft2-bitmap_handling.html#FT_Bitmap_Embolden
107// This value was chosen by eyeballing the result in Firefox and trying to match it.
108static const FT_Pos kBitmapEmboldenStrength = 1 << 6;
109
djsollen@google.comd8b599c2012-03-19 19:44:19 +0000110// convert from Skia's fixed (16.16) to FreeType's fixed (26.6) representation
111static inline int FixedToDot6(SkFixed x) { return x >> 10; }
112// convert from FreeType's fixed (26.6) to Skia's fixed (16.16) representation
113static inline SkFixed Dot6ToFixed(int x) { return x << 10; }
114
agl@chromium.org309485b2009-07-21 17:41:32 +0000115static bool
116InitFreetype() {
117 FT_Error err = FT_Init_FreeType(&gFTLibrary);
reed@google.comea2333d2011-03-14 16:44:56 +0000118 if (err) {
agl@chromium.org309485b2009-07-21 17:41:32 +0000119 return false;
reed@google.comea2333d2011-03-14 16:44:56 +0000120 }
agl@chromium.org309485b2009-07-21 17:41:32 +0000121
agl@chromium.org309485b2009-07-21 17:41:32 +0000122 // Setup LCD filtering. This reduces colour fringes for LCD rendered
123 // glyphs.
epoger@google.comb371ed12011-06-29 21:20:52 +0000124#ifdef FT_LCD_FILTER_H
bungeman@google.comd2dae962012-03-09 20:31:17 +0000125 err = FT_Library_SetLcdFilter(gFTLibrary, FT_LCD_FILTER_DEFAULT);
126// err = FT_Library_SetLcdFilter(gFTLibrary, FT_LCD_FILTER_LIGHT);
agl@chromium.orgf18d8762009-07-28 18:38:08 +0000127 gLCDSupport = err == 0;
reed@google.coma1c32562012-03-01 19:38:23 +0000128 if (gLCDSupport) {
129 gLCDExtra = 2; //DEFAULT and LIGHT add one pixel to each side.
130 }
epoger@google.com5070d792011-06-29 20:43:14 +0000131#else
132 gLCDSupport = false;
133#endif
reed@android.com61608aa2009-07-31 14:52:54 +0000134 gLCDSupportValid = true;
agl@chromium.org309485b2009-07-21 17:41:32 +0000135
136 return true;
137}
138
reed@android.com8a1c16f2008-12-17 15:59:43 +0000139class SkScalerContext_FreeType : public SkScalerContext {
140public:
141 SkScalerContext_FreeType(const SkDescriptor* desc);
142 virtual ~SkScalerContext_FreeType();
agl@chromium.orgcc3096b2009-04-22 22:09:04 +0000143
reed@android.com62900b42009-02-11 15:07:19 +0000144 bool success() const {
reed@android.coma0f5d152009-06-22 17:38:10 +0000145 return fFaceRec != NULL &&
146 fFTSize != NULL &&
147 fFace != NULL;
reed@android.com62900b42009-02-11 15:07:19 +0000148 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000149
150protected:
ctguil@chromium.org0bc7bf52011-03-04 19:04:57 +0000151 virtual unsigned generateGlyphCount();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000152 virtual uint16_t generateCharToGlyph(SkUnichar uni);
153 virtual void generateAdvance(SkGlyph* glyph);
154 virtual void generateMetrics(SkGlyph* glyph);
155 virtual void generateImage(const SkGlyph& glyph);
156 virtual void generatePath(const SkGlyph& glyph, SkPath* path);
157 virtual void generateFontMetrics(SkPaint::FontMetrics* mx,
158 SkPaint::FontMetrics* my);
reed@android.com9d3a9852010-01-08 14:07:42 +0000159 virtual SkUnichar generateGlyphToChar(uint16_t glyph);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000160
161private:
162 SkFaceRec* fFaceRec;
163 FT_Face fFace; // reference to shared face in gFaceRecHead
164 FT_Size fFTSize; // our own copy
165 SkFixed fScaleX, fScaleY;
166 FT_Matrix fMatrix22;
167 uint32_t fLoadGlyphFlags;
reed@google.combdc99882011-11-21 14:36:57 +0000168 bool fDoLinearMetrics;
reed@google.coma1bfa212012-03-08 21:57:12 +0000169 bool fLCDIsVert;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000170
171 FT_Error setupSize();
senorblanco@chromium.org4526a842010-02-05 23:08:20 +0000172 void emboldenOutline(FT_Outline* outline);
djsollen@google.comd8b599c2012-03-19 19:44:19 +0000173 void getBBoxForCurrentGlyph(SkGlyph* glyph, FT_BBox* bbox,
174 bool snapToPixelBoundary = false);
175 void updateGlyphIfLCD(SkGlyph* glyph);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000176};
177
178///////////////////////////////////////////////////////////////////////////
179///////////////////////////////////////////////////////////////////////////
180
181#include "SkStream.h"
182
183struct SkFaceRec {
184 SkFaceRec* fNext;
185 FT_Face fFace;
186 FT_StreamRec fFTStream;
187 SkStream* fSkStream;
188 uint32_t fRefCnt;
189 uint32_t fFontID;
190
reed@android.comb1d9d2e2009-03-04 17:37:51 +0000191 // assumes ownership of the stream, will call unref() when its done
reed@android.com8a1c16f2008-12-17 15:59:43 +0000192 SkFaceRec(SkStream* strm, uint32_t fontID);
193 ~SkFaceRec() {
reed@android.comb1d9d2e2009-03-04 17:37:51 +0000194 fSkStream->unref();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000195 }
196};
197
198extern "C" {
199 static unsigned long sk_stream_read(FT_Stream stream,
200 unsigned long offset,
201 unsigned char* buffer,
202 unsigned long count ) {
203 SkStream* str = (SkStream*)stream->descriptor.pointer;
204
205 if (count) {
206 if (!str->rewind()) {
207 return 0;
208 } else {
209 unsigned long ret;
210 if (offset) {
211 ret = str->read(NULL, offset);
212 if (ret != offset) {
213 return 0;
214 }
215 }
216 ret = str->read(buffer, count);
217 if (ret != count) {
218 return 0;
219 }
220 count = ret;
221 }
222 }
223 return count;
224 }
225
226 static void sk_stream_close( FT_Stream stream) {}
227}
228
229SkFaceRec::SkFaceRec(SkStream* strm, uint32_t fontID)
vandebo@chromium.org9af25f32012-03-28 21:24:27 +0000230 : fNext(NULL), fSkStream(strm), fRefCnt(1), fFontID(fontID) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000231// SkDEBUGF(("SkFaceRec: opening %s (%p)\n", key.c_str(), strm));
232
reed@android.com4516f472009-06-29 16:25:36 +0000233 sk_bzero(&fFTStream, sizeof(fFTStream));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000234 fFTStream.size = fSkStream->getLength();
235 fFTStream.descriptor.pointer = fSkStream;
236 fFTStream.read = sk_stream_read;
237 fFTStream.close = sk_stream_close;
238}
239
reed@android.com62900b42009-02-11 15:07:19 +0000240// Will return 0 on failure
reed@android.com8a1c16f2008-12-17 15:59:43 +0000241static SkFaceRec* ref_ft_face(uint32_t fontID) {
242 SkFaceRec* rec = gFaceRecHead;
243 while (rec) {
244 if (rec->fFontID == fontID) {
245 SkASSERT(rec->fFace);
246 rec->fRefCnt += 1;
247 return rec;
248 }
249 rec = rec->fNext;
250 }
251
252 SkStream* strm = SkFontHost::OpenStream(fontID);
253 if (NULL == strm) {
254 SkDEBUGF(("SkFontHost::OpenStream failed opening %x\n", fontID));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000255 return 0;
256 }
257
258 // this passes ownership of strm to the rec
259 rec = SkNEW_ARGS(SkFaceRec, (strm, fontID));
260
261 FT_Open_Args args;
262 memset(&args, 0, sizeof(args));
263 const void* memoryBase = strm->getMemoryBase();
264
265 if (NULL != memoryBase) {
266//printf("mmap(%s)\n", keyString.c_str());
267 args.flags = FT_OPEN_MEMORY;
268 args.memory_base = (const FT_Byte*)memoryBase;
269 args.memory_size = strm->getLength();
270 } else {
271//printf("fopen(%s)\n", keyString.c_str());
272 args.flags = FT_OPEN_STREAM;
273 args.stream = &rec->fFTStream;
274 }
275
agl@chromium.org61a678a2010-08-06 18:08:18 +0000276 int face_index;
277 int length = SkFontHost::GetFileName(fontID, NULL, 0, &face_index);
278 FT_Error err = FT_Open_Face(gFTLibrary, &args, length ? face_index : 0,
279 &rec->fFace);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000280
281 if (err) { // bad filename, try the default font
282 fprintf(stderr, "ERROR: unable to open font '%x'\n", fontID);
283 SkDELETE(rec);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000284 return 0;
285 } else {
286 SkASSERT(rec->fFace);
287 //fprintf(stderr, "Opened font '%s'\n", filename.c_str());
288 rec->fNext = gFaceRecHead;
289 gFaceRecHead = rec;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000290 return rec;
291 }
292}
293
294static void unref_ft_face(FT_Face face) {
295 SkFaceRec* rec = gFaceRecHead;
296 SkFaceRec* prev = NULL;
297 while (rec) {
298 SkFaceRec* next = rec->fNext;
299 if (rec->fFace == face) {
300 if (--rec->fRefCnt == 0) {
301 if (prev) {
302 prev->fNext = next;
303 } else {
304 gFaceRecHead = next;
305 }
306 FT_Done_Face(face);
307 SkDELETE(rec);
308 }
309 return;
310 }
311 prev = rec;
312 rec = next;
313 }
tomhudson@google.com0c00f212011-12-28 14:59:50 +0000314 SkDEBUGFAIL("shouldn't get here, face not in list");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000315}
316
317///////////////////////////////////////////////////////////////////////////
318
vandebo@chromium.org16be6b82011-01-28 21:28:56 +0000319// Work around for old versions of freetype.
320static FT_Error getAdvances(FT_Face face, FT_UInt start, FT_UInt count,
321 FT_Int32 loadFlags, FT_Fixed* advances) {
322#ifdef FT_ADVANCES_H
323 return FT_Get_Advances(face, start, count, loadFlags, advances);
324#else
325 if (!face || start >= face->num_glyphs ||
326 start + count > face->num_glyphs || loadFlags != FT_LOAD_NO_SCALE) {
327 return 6; // "Invalid argument."
328 }
329 if (count == 0)
330 return 0;
331
332 for (int i = 0; i < count; i++) {
333 FT_Error err = FT_Load_Glyph(face, start + i, FT_LOAD_NO_SCALE);
334 if (err)
335 return err;
336 advances[i] = face->glyph->advance.x;
337 }
338
339 return 0;
340#endif
341}
342
343static bool canEmbed(FT_Face face) {
djsollen@google.comfa394d42012-01-09 20:40:25 +0000344#ifdef FT_FSTYPE_RESTRICTED_LICENSE_EMBEDDING
vandebo@chromium.org16be6b82011-01-28 21:28:56 +0000345 FT_UShort fsType = FT_Get_FSType_Flags(face);
346 return (fsType & (FT_FSTYPE_RESTRICTED_LICENSE_EMBEDDING |
347 FT_FSTYPE_BITMAP_EMBEDDING_ONLY)) == 0;
348#else
349 // No embedding is 0x2 and bitmap embedding only is 0x200.
350 TT_OS2* os2_table;
351 if ((os2_table = (TT_OS2*)FT_Get_Sfnt_Table(face, ft_sfnt_os2)) != NULL) {
352 return (os2_table->fsType & 0x202) == 0;
353 }
354 return false; // We tried, fail safe.
355#endif
356}
357
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000358static bool GetLetterCBox(FT_Face face, char letter, FT_BBox* bbox) {
359 const FT_UInt glyph_id = FT_Get_Char_Index(face, letter);
360 if (!glyph_id)
361 return false;
362 FT_Load_Glyph(face, glyph_id, FT_LOAD_NO_SCALE);
363 FT_Outline_Get_CBox(&face->glyph->outline, bbox);
364 return true;
365}
366
vandebo@chromium.org6f72d1e2011-02-14 23:19:59 +0000367static bool getWidthAdvance(FT_Face face, int gId, int16_t* data) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000368 FT_Fixed advance = 0;
vandebo@chromium.org6f72d1e2011-02-14 23:19:59 +0000369 if (getAdvances(face, gId, 1, FT_LOAD_NO_SCALE, &advance)) {
370 return false;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000371 }
vandebo@chromium.org6f72d1e2011-02-14 23:19:59 +0000372 SkASSERT(data);
373 *data = advance;
374 return true;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000375}
376
vandebo@chromium.org6744d492011-05-09 18:13:47 +0000377static void populate_glyph_to_unicode(FT_Face& face,
378 SkTDArray<SkUnichar>* glyphToUnicode) {
379 // Check and see if we have Unicode cmaps.
380 for (int i = 0; i < face->num_charmaps; ++i) {
381 // CMaps known to support Unicode:
382 // Platform ID Encoding ID Name
383 // ----------- ----------- -----------------------------------
384 // 0 0,1 Apple Unicode
385 // 0 3 Apple Unicode 2.0 (preferred)
386 // 3 1 Microsoft Unicode UCS-2
387 // 3 10 Microsoft Unicode UCS-4 (preferred)
388 //
389 // See Apple TrueType Reference Manual
390 // http://developer.apple.com/fonts/TTRefMan/RM06/Chap6cmap.html
391 // http://developer.apple.com/fonts/TTRefMan/RM06/Chap6name.html#ID
392 // Microsoft OpenType Specification
393 // http://www.microsoft.com/typography/otspec/cmap.htm
394
395 FT_UShort platformId = face->charmaps[i]->platform_id;
396 FT_UShort encodingId = face->charmaps[i]->encoding_id;
397
398 if (platformId != 0 && platformId != 3) {
399 continue;
400 }
401 if (platformId == 3 && encodingId != 1 && encodingId != 10) {
402 continue;
403 }
404 bool preferredMap = ((platformId == 3 && encodingId == 10) ||
405 (platformId == 0 && encodingId == 3));
406
407 FT_Set_Charmap(face, face->charmaps[i]);
408 if (glyphToUnicode->isEmpty()) {
409 glyphToUnicode->setCount(face->num_glyphs);
410 memset(glyphToUnicode->begin(), 0,
411 sizeof(SkUnichar) * face->num_glyphs);
412 }
413
414 // Iterate through each cmap entry.
415 FT_UInt glyphIndex;
416 for (SkUnichar charCode = FT_Get_First_Char(face, &glyphIndex);
417 glyphIndex != 0;
418 charCode = FT_Get_Next_Char(face, charCode, &glyphIndex)) {
419 if (charCode &&
420 ((*glyphToUnicode)[glyphIndex] == 0 || preferredMap)) {
421 (*glyphToUnicode)[glyphIndex] = charCode;
422 }
423 }
424 }
425}
426
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000427// static
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000428SkAdvancedTypefaceMetrics* SkFontHost::GetAdvancedTypefaceMetrics(
vandebo@chromium.org325cb9a2011-03-30 18:36:29 +0000429 uint32_t fontID,
vandebo@chromium.org37ad8fb2011-08-18 02:38:50 +0000430 SkAdvancedTypefaceMetrics::PerGlyphInfo perGlyphInfo,
431 const uint32_t* glyphIDs,
432 uint32_t glyphIDsCount) {
djsollen@google.comda957722011-11-16 17:00:46 +0000433#if defined(SK_BUILD_FOR_MAC)
reed@google.com8a5d6922011-03-14 15:08:03 +0000434 return NULL;
435#else
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000436 SkAutoMutexAcquire ac(gFTMutex);
437 FT_Library libInit = NULL;
438 if (gFTCount == 0) {
439 if (!InitFreetype())
440 sk_throw();
441 libInit = gFTLibrary;
442 }
443 SkAutoTCallIProc<struct FT_LibraryRec_, FT_Done_FreeType> ftLib(libInit);
444 SkFaceRec* rec = ref_ft_face(fontID);
445 if (NULL == rec)
446 return NULL;
447 FT_Face face = rec->fFace;
448
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000449 SkAdvancedTypefaceMetrics* info = new SkAdvancedTypefaceMetrics;
450 info->fFontName.set(FT_Get_Postscript_Name(face));
451 info->fMultiMaster = FT_HAS_MULTIPLE_MASTERS(face);
452 info->fLastGlyphID = face->num_glyphs - 1;
453 info->fEmSize = 1000;
454
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000455 bool cid = false;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000456 const char* fontType = FT_Get_X11_Font_Format(face);
vandebo@chromium.orgc3a2ae52011-02-03 21:48:23 +0000457 if (strcmp(fontType, "Type 1") == 0) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000458 info->fType = SkAdvancedTypefaceMetrics::kType1_Font;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000459 } else if (strcmp(fontType, "CID Type 1") == 0) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000460 info->fType = SkAdvancedTypefaceMetrics::kType1CID_Font;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000461 cid = true;
462 } else if (strcmp(fontType, "CFF") == 0) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000463 info->fType = SkAdvancedTypefaceMetrics::kCFF_Font;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000464 } else if (strcmp(fontType, "TrueType") == 0) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000465 info->fType = SkAdvancedTypefaceMetrics::kTrueType_Font;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000466 cid = true;
467 TT_Header* ttHeader;
468 if ((ttHeader = (TT_Header*)FT_Get_Sfnt_Table(face,
469 ft_sfnt_head)) != NULL) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000470 info->fEmSize = ttHeader->Units_Per_EM;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000471 }
472 }
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000473
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000474 info->fStyle = 0;
475 if (FT_IS_FIXED_WIDTH(face))
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000476 info->fStyle |= SkAdvancedTypefaceMetrics::kFixedPitch_Style;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000477 if (face->style_flags & FT_STYLE_FLAG_ITALIC)
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000478 info->fStyle |= SkAdvancedTypefaceMetrics::kItalic_Style;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000479 // We should set either Symbolic or Nonsymbolic; Nonsymbolic if the font's
480 // character set is a subset of 'Adobe standard Latin.'
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000481 info->fStyle |= SkAdvancedTypefaceMetrics::kSymbolic_Style;
482
483 PS_FontInfoRec ps_info;
484 TT_Postscript* tt_info;
485 if (FT_Get_PS_Font_Info(face, &ps_info) == 0) {
486 info->fItalicAngle = ps_info.italic_angle;
487 } else if ((tt_info =
488 (TT_Postscript*)FT_Get_Sfnt_Table(face,
489 ft_sfnt_post)) != NULL) {
490 info->fItalicAngle = SkFixedToScalar(tt_info->italicAngle);
491 } else {
492 info->fItalicAngle = 0;
493 }
494
495 info->fAscent = face->ascender;
496 info->fDescent = face->descender;
497
498 // Figure out a good guess for StemV - Min width of i, I, !, 1.
499 // This probably isn't very good with an italic font.
500 int16_t min_width = SHRT_MAX;
vandebo@chromium.org6f72d1e2011-02-14 23:19:59 +0000501 info->fStemV = 0;
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000502 char stem_chars[] = {'i', 'I', '!', '1'};
503 for (size_t i = 0; i < SK_ARRAY_COUNT(stem_chars); i++) {
504 FT_BBox bbox;
505 if (GetLetterCBox(face, stem_chars[i], &bbox)) {
506 int16_t width = bbox.xMax - bbox.xMin;
507 if (width > 0 && width < min_width) {
508 min_width = width;
509 info->fStemV = min_width;
510 }
511 }
512 }
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000513
514 TT_PCLT* pclt_info;
515 TT_OS2* os2_table;
516 if ((pclt_info = (TT_PCLT*)FT_Get_Sfnt_Table(face, ft_sfnt_pclt)) != NULL) {
517 info->fCapHeight = pclt_info->CapHeight;
518 uint8_t serif_style = pclt_info->SerifStyle & 0x3F;
519 if (serif_style >= 2 && serif_style <= 6)
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000520 info->fStyle |= SkAdvancedTypefaceMetrics::kSerif_Style;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000521 else if (serif_style >= 9 && serif_style <= 12)
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000522 info->fStyle |= SkAdvancedTypefaceMetrics::kScript_Style;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000523 } else if ((os2_table =
524 (TT_OS2*)FT_Get_Sfnt_Table(face, ft_sfnt_os2)) != NULL) {
525 info->fCapHeight = os2_table->sCapHeight;
526 } else {
527 // Figure out a good guess for CapHeight: average the height of M and X.
528 FT_BBox m_bbox, x_bbox;
529 bool got_m, got_x;
530 got_m = GetLetterCBox(face, 'M', &m_bbox);
531 got_x = GetLetterCBox(face, 'X', &x_bbox);
532 if (got_m && got_x) {
533 info->fCapHeight = (m_bbox.yMax - m_bbox.yMin + x_bbox.yMax -
534 x_bbox.yMin) / 2;
535 } else if (got_m && !got_x) {
536 info->fCapHeight = m_bbox.yMax - m_bbox.yMin;
537 } else if (!got_m && got_x) {
538 info->fCapHeight = x_bbox.yMax - x_bbox.yMin;
539 }
540 }
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000541
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000542 info->fBBox = SkIRect::MakeLTRB(face->bbox.xMin, face->bbox.yMax,
543 face->bbox.xMax, face->bbox.yMin);
544
vandebo@chromium.org37ad8fb2011-08-18 02:38:50 +0000545 if (!canEmbed(face) || !FT_IS_SCALABLE(face) ||
vandebo@chromium.org325cb9a2011-03-30 18:36:29 +0000546 info->fType == SkAdvancedTypefaceMetrics::kOther_Font) {
547 perGlyphInfo = SkAdvancedTypefaceMetrics::kNo_PerGlyphInfo;
548 }
549
550 if (perGlyphInfo & SkAdvancedTypefaceMetrics::kHAdvance_PerGlyphInfo) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000551 if (FT_IS_FIXED_WIDTH(face)) {
552 appendRange(&info->fGlyphWidths, 0);
553 int16_t advance = face->max_advance_width;
554 info->fGlyphWidths->fAdvance.append(1, &advance);
555 finishRange(info->fGlyphWidths.get(), 0,
556 SkAdvancedTypefaceMetrics::WidthRange::kDefault);
vandebo@chromium.org6f72d1e2011-02-14 23:19:59 +0000557 } else if (!cid) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000558 appendRange(&info->fGlyphWidths, 0);
559 // So as to not blow out the stack, get advances in batches.
560 for (int gID = 0; gID < face->num_glyphs; gID += 128) {
561 FT_Fixed advances[128];
562 int advanceCount = 128;
563 if (gID + advanceCount > face->num_glyphs)
564 advanceCount = face->num_glyphs - gID + 1;
565 getAdvances(face, gID, advanceCount, FT_LOAD_NO_SCALE,
566 advances);
567 for (int i = 0; i < advanceCount; i++) {
568 int16_t advance = advances[gID + i];
569 info->fGlyphWidths->fAdvance.append(1, &advance);
570 }
571 }
572 finishRange(info->fGlyphWidths.get(), face->num_glyphs - 1,
573 SkAdvancedTypefaceMetrics::WidthRange::kRange);
574 } else {
vandebo@chromium.org6f72d1e2011-02-14 23:19:59 +0000575 info->fGlyphWidths.reset(
vandebo@chromium.org37ad8fb2011-08-18 02:38:50 +0000576 getAdvanceData(face,
577 face->num_glyphs,
578 glyphIDs,
579 glyphIDsCount,
580 &getWidthAdvance));
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000581 }
vandebo@chromium.org325cb9a2011-03-30 18:36:29 +0000582 }
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000583
vandebo@chromium.org325cb9a2011-03-30 18:36:29 +0000584 if (perGlyphInfo & SkAdvancedTypefaceMetrics::kVAdvance_PerGlyphInfo &&
585 FT_HAS_VERTICAL(face)) {
586 SkASSERT(false); // Not implemented yet.
587 }
588
589 if (perGlyphInfo & SkAdvancedTypefaceMetrics::kGlyphNames_PerGlyphInfo &&
590 info->fType == SkAdvancedTypefaceMetrics::kType1_Font) {
591 // Postscript fonts may contain more than 255 glyphs, so we end up
592 // using multiple font descriptions with a glyph ordering. Record
593 // the name of each glyph.
594 info->fGlyphNames.reset(
595 new SkAutoTArray<SkString>(face->num_glyphs));
596 for (int gID = 0; gID < face->num_glyphs; gID++) {
597 char glyphName[128]; // PS limit for names is 127 bytes.
598 FT_Get_Glyph_Name(face, gID, glyphName, 128);
599 info->fGlyphNames->get()[gID].set(glyphName);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000600 }
601 }
602
vandebo@chromium.org6744d492011-05-09 18:13:47 +0000603 if (perGlyphInfo & SkAdvancedTypefaceMetrics::kToUnicode_PerGlyphInfo &&
604 info->fType != SkAdvancedTypefaceMetrics::kType1_Font &&
605 face->num_charmaps) {
606 populate_glyph_to_unicode(face, &(info->fGlyphToUnicode));
607 }
608
vandebo@chromium.orgc3a2ae52011-02-03 21:48:23 +0000609 if (!canEmbed(face))
610 info->fType = SkAdvancedTypefaceMetrics::kNotEmbeddable_Font;
611
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000612 unref_ft_face(face);
613 return info;
reed@google.com8a5d6922011-03-14 15:08:03 +0000614#endif
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000615}
vandebo@chromium.org37ad8fb2011-08-18 02:38:50 +0000616
reed@google.com618ef5e2011-01-26 22:10:41 +0000617///////////////////////////////////////////////////////////////////////////
618
reed@google.comffe49f52011-11-22 19:42:41 +0000619#define BLACK_LUMINANCE_LIMIT 0x40
620#define WHITE_LUMINANCE_LIMIT 0xA0
621
reed@google.com8ed436c2011-07-21 14:12:36 +0000622static bool bothZero(SkScalar a, SkScalar b) {
623 return 0 == a && 0 == b;
624}
625
626// returns false if there is any non-90-rotation or skew
627static bool isAxisAligned(const SkScalerContext::Rec& rec) {
628 return 0 == rec.fPreSkewX &&
629 (bothZero(rec.fPost2x2[0][1], rec.fPost2x2[1][0]) ||
630 bothZero(rec.fPost2x2[0][0], rec.fPost2x2[1][1]));
631}
632
reed@google.com618ef5e2011-01-26 22:10:41 +0000633void SkFontHost::FilterRec(SkScalerContext::Rec* rec) {
634 if (!gLCDSupportValid) {
635 InitFreetype();
636 FT_Done_FreeType(gFTLibrary);
637 }
reed@google.com5b31b0f2011-02-23 14:41:42 +0000638
reed@google.comeffc5012011-06-27 16:44:46 +0000639 if (!gLCDSupport && isLCD(*rec)) {
reed@google.com618ef5e2011-01-26 22:10:41 +0000640 // If the runtime Freetype library doesn't support LCD mode, we disable
641 // it here.
642 rec->fMaskFormat = SkMask::kA8_Format;
643 }
reed@google.com5b31b0f2011-02-23 14:41:42 +0000644
reed@google.com618ef5e2011-01-26 22:10:41 +0000645 SkPaint::Hinting h = rec->getHinting();
reed@google.comeffc5012011-06-27 16:44:46 +0000646 if (SkPaint::kFull_Hinting == h && !isLCD(*rec)) {
reed@google.com618ef5e2011-01-26 22:10:41 +0000647 // collapse full->normal hinting if we're not doing LCD
648 h = SkPaint::kNormal_Hinting;
reed@google.com618ef5e2011-01-26 22:10:41 +0000649 }
bungeman@google.comf4f2b802012-03-08 19:19:51 +0000650 if ((rec->fFlags & SkScalerContext::kSubpixelPositioning_Flag)) {
reed@google.com1ac83502012-02-28 17:06:02 +0000651 if (SkPaint::kNo_Hinting != h) {
652 h = SkPaint::kSlight_Hinting;
653 }
654 }
655
vandebo@chromium.org37ad8fb2011-08-18 02:38:50 +0000656#ifndef SK_IGNORE_ROTATED_FREETYPE_FIX
reed@google.com8ed436c2011-07-21 14:12:36 +0000657 // rotated text looks bad with hinting, so we disable it as needed
658 if (!isAxisAligned(*rec)) {
659 h = SkPaint::kNo_Hinting;
660 }
bsalomon@google.com0e35ca82011-07-22 17:56:19 +0000661#endif
reed@google.com618ef5e2011-01-26 22:10:41 +0000662 rec->setHinting(h);
reed@google.comffe49f52011-11-22 19:42:41 +0000663
reed@google.com1ac83502012-02-28 17:06:02 +0000664#ifndef SK_USE_COLOR_LUMINANCE
reed@google.comffe49f52011-11-22 19:42:41 +0000665 // for compatibility at the moment, discretize luminance to 3 settings
666 // black, white, gray. This helps with fontcache utilization, since we
667 // won't create multiple entries that in the end map to the same results.
668 {
669 unsigned lum = rec->getLuminanceByte();
670 if (gGammaTables[0] || gGammaTables[1]) {
671 if (lum <= BLACK_LUMINANCE_LIMIT) {
672 lum = 0;
673 } else if (lum >= WHITE_LUMINANCE_LIMIT) {
674 lum = SkScalerContext::kLuminance_Max;
675 } else {
676 lum = SkScalerContext::kLuminance_Max >> 1;
677 }
678 } else {
679 lum = 0; // no gamma correct, so use 0 since SkPaint uses that
680 // when measuring text w/o regard for luminance
681 }
682 rec->setLuminanceBits(lum);
683 }
reed@google.com1ac83502012-02-28 17:06:02 +0000684#endif
reed@google.com618ef5e2011-01-26 22:10:41 +0000685}
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000686
djsollen@google.comda957722011-11-16 17:00:46 +0000687#ifdef SK_BUILD_FOR_ANDROID
djsollen@google.comcd9d69b2011-03-14 20:30:14 +0000688uint32_t SkFontHost::GetUnitsPerEm(SkFontID fontID) {
689 SkAutoMutexAcquire ac(gFTMutex);
690 SkFaceRec *rec = ref_ft_face(fontID);
691 uint16_t unitsPerEm = 0;
692
693 if (rec != NULL && rec->fFace != NULL) {
694 unitsPerEm = rec->fFace->units_per_EM;
695 unref_ft_face(rec->fFace);
696 }
697
698 return (uint32_t)unitsPerEm;
699}
700#endif
701
reed@android.com8a1c16f2008-12-17 15:59:43 +0000702SkScalerContext_FreeType::SkScalerContext_FreeType(const SkDescriptor* desc)
reed@android.com62900b42009-02-11 15:07:19 +0000703 : SkScalerContext(desc) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000704 SkAutoMutexAcquire ac(gFTMutex);
705
reed@android.com8a1c16f2008-12-17 15:59:43 +0000706 if (gFTCount == 0) {
reed@android.com659aaf92009-07-23 15:20:21 +0000707 if (!InitFreetype()) {
708 sk_throw();
709 }
reed@google.comffe49f52011-11-22 19:42:41 +0000710 SkFontHost::GetGammaTables(gGammaTables);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000711 }
712 ++gFTCount;
713
714 // load the font file
reed@android.com62900b42009-02-11 15:07:19 +0000715 fFTSize = NULL;
716 fFace = NULL;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000717 fFaceRec = ref_ft_face(fRec.fFontID);
reed@android.com62900b42009-02-11 15:07:19 +0000718 if (NULL == fFaceRec) {
719 return;
720 }
721 fFace = fFaceRec->fFace;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000722
723 // compute our factors from the record
724
725 SkMatrix m;
726
727 fRec.getSingleMatrix(&m);
728
729#ifdef DUMP_STRIKE_CREATION
730 SkString keyString;
731 SkFontHost::GetDescriptorKeyString(desc, &keyString);
732 printf("========== strike [%g %g %g] [%g %g %g %g] hints %d format %d %s\n", SkScalarToFloat(fRec.fTextSize),
733 SkScalarToFloat(fRec.fPreScaleX), SkScalarToFloat(fRec.fPreSkewX),
734 SkScalarToFloat(fRec.fPost2x2[0][0]), SkScalarToFloat(fRec.fPost2x2[0][1]),
735 SkScalarToFloat(fRec.fPost2x2[1][0]), SkScalarToFloat(fRec.fPost2x2[1][1]),
agl@chromium.org309485b2009-07-21 17:41:32 +0000736 fRec.getHinting(), fRec.fMaskFormat, keyString.c_str());
reed@android.com8a1c16f2008-12-17 15:59:43 +0000737#endif
738
739 // now compute our scale factors
740 SkScalar sx = m.getScaleX();
741 SkScalar sy = m.getScaleY();
742
743 if (m.getSkewX() || m.getSkewY() || sx < 0 || sy < 0) {
744 // sort of give up on hinting
745 sx = SkMaxScalar(SkScalarAbs(sx), SkScalarAbs(m.getSkewX()));
746 sy = SkMaxScalar(SkScalarAbs(m.getSkewY()), SkScalarAbs(sy));
747 sx = sy = SkScalarAve(sx, sy);
748
749 SkScalar inv = SkScalarInvert(sx);
750
751 // flip the skew elements to go from our Y-down system to FreeType's
752 fMatrix22.xx = SkScalarToFixed(SkScalarMul(m.getScaleX(), inv));
753 fMatrix22.xy = -SkScalarToFixed(SkScalarMul(m.getSkewX(), inv));
754 fMatrix22.yx = -SkScalarToFixed(SkScalarMul(m.getSkewY(), inv));
755 fMatrix22.yy = SkScalarToFixed(SkScalarMul(m.getScaleY(), inv));
756 } else {
757 fMatrix22.xx = fMatrix22.yy = SK_Fixed1;
758 fMatrix22.xy = fMatrix22.yx = 0;
759 }
760
761 fScaleX = SkScalarToFixed(sx);
762 fScaleY = SkScalarToFixed(sy);
763
reed@google.coma1bfa212012-03-08 21:57:12 +0000764 fLCDIsVert = SkToBool(fRec.fFlags & SkScalerContext::kLCD_Vertical_Flag);
765
reed@android.com8a1c16f2008-12-17 15:59:43 +0000766 // compute the flags we send to Load_Glyph
767 {
reed@android.come4d0bc02009-07-24 19:53:20 +0000768 FT_Int32 loadFlags = FT_LOAD_DEFAULT;
bungeman@google.com34f10262012-03-23 18:11:47 +0000769 bool linearMetrics = SkToBool(fRec.fFlags & SkScalerContext::kSubpixelPositioning_Flag);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000770
agl@chromium.org70a303f2010-05-10 14:15:50 +0000771 if (SkMask::kBW_Format == fRec.fMaskFormat) {
772 // See http://code.google.com/p/chromium/issues/detail?id=43252#c24
773 loadFlags = FT_LOAD_TARGET_MONO;
reed@google.comeffc5012011-06-27 16:44:46 +0000774 if (fRec.getHinting() == SkPaint::kNo_Hinting) {
agl@chromium.org70a303f2010-05-10 14:15:50 +0000775 loadFlags = FT_LOAD_NO_HINTING;
reed@google.combdc99882011-11-21 14:36:57 +0000776 linearMetrics = true;
reed@google.comeffc5012011-06-27 16:44:46 +0000777 }
agl@chromium.org70a303f2010-05-10 14:15:50 +0000778 } else {
779 switch (fRec.getHinting()) {
780 case SkPaint::kNo_Hinting:
781 loadFlags = FT_LOAD_NO_HINTING;
reed@google.combdc99882011-11-21 14:36:57 +0000782 linearMetrics = true;
agl@chromium.org70a303f2010-05-10 14:15:50 +0000783 break;
784 case SkPaint::kSlight_Hinting:
785 loadFlags = FT_LOAD_TARGET_LIGHT; // This implies FORCE_AUTOHINT
786 break;
787 case SkPaint::kNormal_Hinting:
agl@chromium.orga2c71cb2010-06-17 20:49:17 +0000788 if (fRec.fFlags & SkScalerContext::kAutohinting_Flag)
789 loadFlags = FT_LOAD_FORCE_AUTOHINT;
790 else
791 loadFlags = FT_LOAD_NO_AUTOHINT;
agl@chromium.org70a303f2010-05-10 14:15:50 +0000792 break;
793 case SkPaint::kFull_Hinting:
agl@chromium.orga2c71cb2010-06-17 20:49:17 +0000794 if (fRec.fFlags & SkScalerContext::kAutohinting_Flag) {
795 loadFlags = FT_LOAD_FORCE_AUTOHINT;
796 break;
797 }
agl@chromium.org70a303f2010-05-10 14:15:50 +0000798 loadFlags = FT_LOAD_TARGET_NORMAL;
reed@google.comeffc5012011-06-27 16:44:46 +0000799 if (isLCD(fRec)) {
reed@google.coma1bfa212012-03-08 21:57:12 +0000800 if (fLCDIsVert) {
reed@google.comeffc5012011-06-27 16:44:46 +0000801 loadFlags = FT_LOAD_TARGET_LCD_V;
802 } else {
803 loadFlags = FT_LOAD_TARGET_LCD;
804 }
reed@google.comea2333d2011-03-14 16:44:56 +0000805 }
agl@chromium.org70a303f2010-05-10 14:15:50 +0000806 break;
807 default:
808 SkDebugf("---------- UNKNOWN hinting %d\n", fRec.getHinting());
809 break;
810 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000811 }
812
reed@google.comeffc5012011-06-27 16:44:46 +0000813 if ((fRec.fFlags & SkScalerContext::kEmbeddedBitmapText_Flag) == 0) {
agl@chromium.orge0d08992009-08-07 19:19:23 +0000814 loadFlags |= FT_LOAD_NO_BITMAP;
reed@google.comeffc5012011-06-27 16:44:46 +0000815 }
agl@chromium.orge0d08992009-08-07 19:19:23 +0000816
reed@google.com96a9f7912011-05-06 11:49:30 +0000817 // Always using FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH to get correct
818 // advances, as fontconfig and cairo do.
819 // See http://code.google.com/p/skia/issues/detail?id=222.
820 loadFlags |= FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH;
821
reed@android.come4d0bc02009-07-24 19:53:20 +0000822 fLoadGlyphFlags = loadFlags;
reed@google.combdc99882011-11-21 14:36:57 +0000823 fDoLinearMetrics = linearMetrics;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000824 }
825
826 // now create the FT_Size
827
828 {
829 FT_Error err;
830
831 err = FT_New_Size(fFace, &fFTSize);
832 if (err != 0) {
833 SkDEBUGF(("SkScalerContext_FreeType::FT_New_Size(%x): FT_Set_Char_Size(0x%x, 0x%x) returned 0x%x\n",
834 fFaceRec->fFontID, fScaleX, fScaleY, err));
835 fFace = NULL;
836 return;
837 }
838
839 err = FT_Activate_Size(fFTSize);
840 if (err != 0) {
841 SkDEBUGF(("SkScalerContext_FreeType::FT_Activate_Size(%x, 0x%x, 0x%x) returned 0x%x\n",
842 fFaceRec->fFontID, fScaleX, fScaleY, err));
843 fFTSize = NULL;
844 }
845
846 err = FT_Set_Char_Size( fFace,
847 SkFixedToFDot6(fScaleX), SkFixedToFDot6(fScaleY),
848 72, 72);
849 if (err != 0) {
850 SkDEBUGF(("SkScalerContext_FreeType::FT_Set_Char_Size(%x, 0x%x, 0x%x) returned 0x%x\n",
851 fFaceRec->fFontID, fScaleX, fScaleY, err));
852 fFace = NULL;
853 return;
854 }
855
856 FT_Set_Transform( fFace, &fMatrix22, NULL);
857 }
858}
859
860SkScalerContext_FreeType::~SkScalerContext_FreeType() {
861 if (fFTSize != NULL) {
862 FT_Done_Size(fFTSize);
863 }
864
865 SkAutoMutexAcquire ac(gFTMutex);
866
867 if (fFace != NULL) {
868 unref_ft_face(fFace);
869 }
870 if (--gFTCount == 0) {
871// SkDEBUGF(("FT_Done_FreeType\n"));
872 FT_Done_FreeType(gFTLibrary);
873 SkDEBUGCODE(gFTLibrary = NULL;)
874 }
875}
876
877/* We call this before each use of the fFace, since we may be sharing
878 this face with other context (at different sizes).
879*/
880FT_Error SkScalerContext_FreeType::setupSize() {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000881 FT_Error err = FT_Activate_Size(fFTSize);
882
883 if (err != 0) {
884 SkDEBUGF(("SkScalerContext_FreeType::FT_Activate_Size(%x, 0x%x, 0x%x) returned 0x%x\n",
885 fFaceRec->fFontID, fScaleX, fScaleY, err));
886 fFTSize = NULL;
887 } else {
888 // seems we need to reset this every time (not sure why, but without it
889 // I get random italics from some other fFTSize)
890 FT_Set_Transform( fFace, &fMatrix22, NULL);
891 }
892 return err;
893}
894
senorblanco@chromium.org4526a842010-02-05 23:08:20 +0000895void SkScalerContext_FreeType::emboldenOutline(FT_Outline* outline) {
896 FT_Pos strength;
897 strength = FT_MulFix(fFace->units_per_EM, fFace->size->metrics.y_scale)
898 / 24;
899 FT_Outline_Embolden(outline, strength);
900}
901
ctguil@chromium.org0bc7bf52011-03-04 19:04:57 +0000902unsigned SkScalerContext_FreeType::generateGlyphCount() {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000903 return fFace->num_glyphs;
904}
905
906uint16_t SkScalerContext_FreeType::generateCharToGlyph(SkUnichar uni) {
907 return SkToU16(FT_Get_Char_Index( fFace, uni ));
908}
909
reed@android.com9d3a9852010-01-08 14:07:42 +0000910SkUnichar SkScalerContext_FreeType::generateGlyphToChar(uint16_t glyph) {
911 // iterate through each cmap entry, looking for matching glyph indices
912 FT_UInt glyphIndex;
913 SkUnichar charCode = FT_Get_First_Char( fFace, &glyphIndex );
914
915 while (glyphIndex != 0) {
916 if (glyphIndex == glyph) {
917 return charCode;
918 }
919 charCode = FT_Get_Next_Char( fFace, charCode, &glyphIndex );
920 }
921
922 return 0;
923}
924
reed@android.com8a1c16f2008-12-17 15:59:43 +0000925static FT_Pixel_Mode compute_pixel_mode(SkMask::Format format) {
926 switch (format) {
927 case SkMask::kBW_Format:
928 return FT_PIXEL_MODE_MONO;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000929 case SkMask::kA8_Format:
930 default:
931 return FT_PIXEL_MODE_GRAY;
932 }
933}
934
reed@android.com8a1c16f2008-12-17 15:59:43 +0000935void SkScalerContext_FreeType::generateAdvance(SkGlyph* glyph) {
936#ifdef FT_ADVANCES_H
937 /* unhinted and light hinted text have linearly scaled advances
938 * which are very cheap to compute with some font formats...
939 */
reed@google.combdc99882011-11-21 14:36:57 +0000940 if (fDoLinearMetrics) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000941 SkAutoMutexAcquire ac(gFTMutex);
942
943 if (this->setupSize()) {
reed@android.com62900b42009-02-11 15:07:19 +0000944 glyph->zeroMetrics();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000945 return;
946 }
947
948 FT_Error error;
949 FT_Fixed advance;
950
951 error = FT_Get_Advance( fFace, glyph->getGlyphID(fBaseGlyphCount),
952 fLoadGlyphFlags | FT_ADVANCE_FLAG_FAST_ONLY,
953 &advance );
954 if (0 == error) {
955 glyph->fRsbDelta = 0;
956 glyph->fLsbDelta = 0;
957 glyph->fAdvanceX = advance; // advance *2/3; //DEBUG
958 glyph->fAdvanceY = 0;
959 return;
960 }
961 }
962#endif /* FT_ADVANCES_H */
963 /* otherwise, we need to load/hint the glyph, which is slower */
964 this->generateMetrics(glyph);
965 return;
966}
967
djsollen@google.comd8b599c2012-03-19 19:44:19 +0000968void SkScalerContext_FreeType::getBBoxForCurrentGlyph(SkGlyph* glyph,
969 FT_BBox* bbox,
970 bool snapToPixelBoundary) {
971
972 FT_Outline_Get_CBox(&fFace->glyph->outline, bbox);
973
974 if (fRec.fFlags & SkScalerContext::kSubpixelPositioning_Flag) {
975 int dx = FixedToDot6(glyph->getSubXFixed());
976 int dy = FixedToDot6(glyph->getSubYFixed());
977 // negate dy since freetype-y-goes-up and skia-y-goes-down
978 bbox->xMin += dx;
979 bbox->yMin -= dy;
980 bbox->xMax += dx;
981 bbox->yMax -= dy;
982 }
983
984 // outset the box to integral boundaries
985 if (snapToPixelBoundary) {
986 bbox->xMin &= ~63;
987 bbox->yMin &= ~63;
988 bbox->xMax = (bbox->xMax + 63) & ~63;
989 bbox->yMax = (bbox->yMax + 63) & ~63;
990 }
991}
992
993void SkScalerContext_FreeType::updateGlyphIfLCD(SkGlyph* glyph) {
994 if (isLCD(fRec)) {
995 if (fLCDIsVert) {
996 glyph->fHeight += gLCDExtra;
997 glyph->fTop -= gLCDExtra >> 1;
998 } else {
999 glyph->fWidth += gLCDExtra;
1000 glyph->fLeft -= gLCDExtra >> 1;
1001 }
1002 }
1003}
1004
reed@android.com8a1c16f2008-12-17 15:59:43 +00001005void SkScalerContext_FreeType::generateMetrics(SkGlyph* glyph) {
1006 SkAutoMutexAcquire ac(gFTMutex);
1007
1008 glyph->fRsbDelta = 0;
1009 glyph->fLsbDelta = 0;
1010
1011 FT_Error err;
1012
1013 if (this->setupSize()) {
1014 goto ERROR;
1015 }
1016
1017 err = FT_Load_Glyph( fFace, glyph->getGlyphID(fBaseGlyphCount), fLoadGlyphFlags );
1018 if (err != 0) {
1019 SkDEBUGF(("SkScalerContext_FreeType::generateMetrics(%x): FT_Load_Glyph(glyph:%d flags:%d) returned 0x%x\n",
1020 fFaceRec->fFontID, glyph->getGlyphID(fBaseGlyphCount), fLoadGlyphFlags, err));
1021 ERROR:
reed@android.com62900b42009-02-11 15:07:19 +00001022 glyph->zeroMetrics();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001023 return;
1024 }
1025
vandebo@chromium.org6390c722012-03-28 21:03:22 +00001026 SkFixed vLeft = 0, vTop = 0;
djsollen@google.comd8b599c2012-03-19 19:44:19 +00001027
reed@android.com8a1c16f2008-12-17 15:59:43 +00001028 switch ( fFace->glyph->format ) {
thakis@chromium.org598b8592011-05-24 05:42:55 +00001029 case FT_GLYPH_FORMAT_OUTLINE: {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001030 FT_BBox bbox;
1031
bungeman@google.com0f0c2882011-11-04 15:47:41 +00001032 if (0 == fFace->glyph->outline.n_contours) {
1033 glyph->fWidth = 0;
1034 glyph->fHeight = 0;
1035 glyph->fTop = 0;
1036 glyph->fLeft = 0;
1037 break;
1038 }
1039
senorblanco@chromium.org4526a842010-02-05 23:08:20 +00001040 if (fRec.fFlags & kEmbolden_Flag) {
1041 emboldenOutline(&fFace->glyph->outline);
1042 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001043
djsollen@google.comd8b599c2012-03-19 19:44:19 +00001044 getBBoxForCurrentGlyph(glyph, &bbox, true);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001045
1046 glyph->fWidth = SkToU16((bbox.xMax - bbox.xMin) >> 6);
1047 glyph->fHeight = SkToU16((bbox.yMax - bbox.yMin) >> 6);
1048 glyph->fTop = -SkToS16(bbox.yMax >> 6);
1049 glyph->fLeft = SkToS16(bbox.xMin >> 6);
reed@google.coma1c32562012-03-01 19:38:23 +00001050
djsollen@google.comd8b599c2012-03-19 19:44:19 +00001051 if ((fRec.fFlags & SkScalerContext::kVertical_Flag)) {
1052 vLeft = Dot6ToFixed(bbox.xMin);
1053 vTop = Dot6ToFixed(bbox.yMax);
reed@google.coma1c32562012-03-01 19:38:23 +00001054 }
djsollen@google.comd8b599c2012-03-19 19:44:19 +00001055
1056 updateGlyphIfLCD(glyph);
1057
reed@android.com8a1c16f2008-12-17 15:59:43 +00001058 break;
thakis@chromium.org598b8592011-05-24 05:42:55 +00001059 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001060
1061 case FT_GLYPH_FORMAT_BITMAP:
agl@chromium.orge76073b2010-06-04 20:31:17 +00001062 if (fRec.fFlags & kEmbolden_Flag) {
1063 FT_GlyphSlot_Own_Bitmap(fFace->glyph);
1064 FT_Bitmap_Embolden(gFTLibrary, &fFace->glyph->bitmap, kBitmapEmboldenStrength, 0);
1065 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001066 glyph->fWidth = SkToU16(fFace->glyph->bitmap.width);
1067 glyph->fHeight = SkToU16(fFace->glyph->bitmap.rows);
1068 glyph->fTop = -SkToS16(fFace->glyph->bitmap_top);
1069 glyph->fLeft = SkToS16(fFace->glyph->bitmap_left);
1070 break;
1071
1072 default:
tomhudson@google.com0c00f212011-12-28 14:59:50 +00001073 SkDEBUGFAIL("unknown glyph format");
reed@android.com8a1c16f2008-12-17 15:59:43 +00001074 goto ERROR;
1075 }
1076
bungeman@google.com34f10262012-03-23 18:11:47 +00001077 if (fDoLinearMetrics) {
1078 glyph->fAdvanceX = SkFixedMul(fMatrix22.xx, fFace->glyph->linearHoriAdvance);
1079 glyph->fAdvanceY = -SkFixedMul(fMatrix22.yx, fFace->glyph->linearHoriAdvance);
1080 } else {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001081 glyph->fAdvanceX = SkFDot6ToFixed(fFace->glyph->advance.x);
1082 glyph->fAdvanceY = -SkFDot6ToFixed(fFace->glyph->advance.y);
bungeman@google.com34f10262012-03-23 18:11:47 +00001083
reed@android.com8a1c16f2008-12-17 15:59:43 +00001084 if (fRec.fFlags & kDevKernText_Flag) {
1085 glyph->fRsbDelta = SkToS8(fFace->glyph->rsb_delta);
1086 glyph->fLsbDelta = SkToS8(fFace->glyph->lsb_delta);
1087 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001088 }
1089
djsollen@google.comd8b599c2012-03-19 19:44:19 +00001090 if ((fRec.fFlags & SkScalerContext::kVertical_Flag)
1091 && fFace->glyph->format == FT_GLYPH_FORMAT_OUTLINE) {
1092
1093 //TODO: do we need to specially handle SubpixelPositioning and Kerning?
1094
1095 FT_Matrix identityMatrix;
1096 identityMatrix.xx = identityMatrix.yy = SK_Fixed1;
1097 identityMatrix.xy = identityMatrix.yx = 0;
1098
1099 // if the matrix is not the identity matrix then we need to re-load the
1100 // glyph with the identity matrix to get the necessary bounding box
1101 if (memcmp(&fMatrix22, &identityMatrix, sizeof(FT_Matrix)) != 0) {
1102
1103 FT_Set_Transform(fFace, &identityMatrix, NULL);
1104
1105 err = FT_Load_Glyph( fFace, glyph->getGlyphID(fBaseGlyphCount), fLoadGlyphFlags );
1106 if (err != 0) {
1107 SkDEBUGF(("SkScalerContext_FreeType::generateMetrics(%x): FT_Load_Glyph(glyph:%d flags:%d) returned 0x%x\n",
1108 fFaceRec->fFontID, glyph->getGlyphID(fBaseGlyphCount), fLoadGlyphFlags, err));
1109 goto ERROR;
1110 }
1111
1112 if (fRec.fFlags & kEmbolden_Flag) {
1113 emboldenOutline(&fFace->glyph->outline);
1114 }
1115 }
1116
1117 // bounding box of the unskewed and unscaled glyph
vandebo@chromium.org6390c722012-03-28 21:03:22 +00001118 FT_BBox bbox = {0, 0, 0, 0}; // Suppress Coverity warning.
djsollen@google.comd8b599c2012-03-19 19:44:19 +00001119 getBBoxForCurrentGlyph(glyph, &bbox);
1120
1121 // compute the vertical gap above and below the glyph if the glyph were
1122 // centered within the linearVertAdvance
1123 SkFixed vGap = (fFace->glyph->linearVertAdvance - Dot6ToFixed(bbox.yMax - bbox.yMin)) / 2;
1124
1125 // the origin point of the glyph when rendered vertically
1126 FT_Vector vOrigin;
1127 vOrigin.x = fFace->glyph->linearHoriAdvance / 2;
1128 vOrigin.y = vGap + Dot6ToFixed(bbox.yMax);
1129
1130 // transform the vertical origin based on the matrix of the actual glyph
1131 FT_Vector_Transform(&vOrigin, &fMatrix22);
1132
1133 // compute a new offset vector for the glyph by subtracting the vertical
1134 // origin from the original horizontal offset vector
1135 glyph->fLeft = SkFixedRoundToInt(vLeft - vOrigin.x);
1136 glyph->fTop = -SkFixedRoundToInt(vTop - vOrigin.y);
1137
1138 updateGlyphIfLCD(glyph);
1139
1140 // use the vertical advance values computed by freetype
1141 glyph->fAdvanceX = -SkFixedMul(fMatrix22.xy, fFace->glyph->linearVertAdvance);
1142 glyph->fAdvanceY = SkFixedMul(fMatrix22.yy, fFace->glyph->linearVertAdvance);
1143 }
1144
1145
reed@android.com8a1c16f2008-12-17 15:59:43 +00001146#ifdef ENABLE_GLYPH_SPEW
1147 SkDEBUGF(("FT_Set_Char_Size(this:%p sx:%x sy:%x ", this, fScaleX, fScaleY));
1148 SkDEBUGF(("Metrics(glyph:%d flags:0x%x) w:%d\n", glyph->getGlyphID(fBaseGlyphCount), fLoadGlyphFlags, glyph->fWidth));
1149#endif
1150}
1151
reed@google.com1ac83502012-02-28 17:06:02 +00001152///////////////////////////////////////////////////////////////////////////////
1153
bungeman@google.com3aacb412012-03-13 14:55:12 +00001154#ifdef SK_USE_COLOR_LUMINANCE
1155
1156static float apply_contrast(float srca, float contrast) {
1157 return srca + ((1.0f - srca) * contrast * srca);
reed@google.comc5181342011-05-17 20:52:46 +00001158}
1159
bungeman@google.com3aacb412012-03-13 14:55:12 +00001160#ifdef SK_GAMMA_SRGB
1161static float lin(float per) {
1162 if (per <= 0.04045f) {
1163 return per / 12.92f;
reed@google.comc5181342011-05-17 20:52:46 +00001164 }
bungeman@google.com3aacb412012-03-13 14:55:12 +00001165 return powf((per + 0.055f) / 1.055, 2.4f);
reed@google.com1ac83502012-02-28 17:06:02 +00001166}
bungeman@google.com3aacb412012-03-13 14:55:12 +00001167static float per(float lin) {
1168 if (lin <= 0.0031308f) {
1169 return lin * 12.92f;
reed@google.com1ac83502012-02-28 17:06:02 +00001170 }
bungeman@google.com3aacb412012-03-13 14:55:12 +00001171 return 1.055f * powf(lin, 1.0f / 2.4f) - 0.055f;
1172}
1173#else //SK_GAMMA_SRGB
1174static float lin(float per) {
1175 const float g = SK_GAMMA_EXPONENT;
1176 return powf(per, g);
1177}
1178static float per(float lin) {
1179 const float g = SK_GAMMA_EXPONENT;
1180 return powf(lin, 1.0f / g);
1181}
1182#endif //SK_GAMMA_SRGB
reed@google.com1ac83502012-02-28 17:06:02 +00001183
bungeman@google.com3aacb412012-03-13 14:55:12 +00001184static void build_gamma_table(uint8_t table[256], int srcI) {
1185 const float src = (float)srcI / 255.0f;
1186 const float linSrc = lin(src);
1187 const float linDst = 1.0f - linSrc;
1188 const float dst = per(linDst);
bungeman@google.comd0b6a2d2012-03-05 15:07:25 +00001189
reed@google.com613e9fe2012-02-29 15:08:00 +00001190 // have our contrast value taper off to 0 as the src luminance becomes white
bungeman@google.com3aacb412012-03-13 14:55:12 +00001191 const float contrast = SK_GAMMA_CONTRAST / 255.0f * linDst;
1192 const float step = 1.0f / 256.0f;
reed@google.com1ac83502012-02-28 17:06:02 +00001193
bungeman@google.com3aacb412012-03-13 14:55:12 +00001194 //Remove discontinuity and instability when src is close to dst.
1195 if (fabs(src - dst) < 0.01f) {
1196 float rawSrca = 0.0f;
1197 for (int i = 0; i < 256; ++i, rawSrca += step) {
1198 float srca = apply_contrast(rawSrca, contrast);
1199 table[i] = sk_float_round2int(255.0f * srca);
1200 }
1201 } else {
1202 float rawSrca = 0.0f;
1203 for (int i = 0; i < 256; ++i, rawSrca += step) {
1204 float srca = apply_contrast(rawSrca, contrast);
1205 SkASSERT(srca <= 1.0f);
1206 float dsta = 1 - srca;
reed@google.com1ac83502012-02-28 17:06:02 +00001207
bungeman@google.com3aacb412012-03-13 14:55:12 +00001208 //Calculate the output we want.
1209 float linOut = (linSrc * srca + dsta * linDst);
1210 SkASSERT(linOut <= 1.0f);
1211 float out = per(linOut);
reed@google.com1ac83502012-02-28 17:06:02 +00001212
bungeman@google.com3aacb412012-03-13 14:55:12 +00001213 //Undo what the blit blend will do.
1214 float result = (out - dst) / (src - dst);
1215 SkASSERT(sk_float_round2int(255.0f * result) <= 255);
1216
1217 table[i] = sk_float_round2int(255.0f * result);
1218 }
reed@google.com1ac83502012-02-28 17:06:02 +00001219 }
1220}
1221
1222static const uint8_t* getGammaTable(U8CPU luminance) {
1223 static uint8_t gGammaTables[4][256];
1224 static bool gInited;
1225 if (!gInited) {
bungeman@google.comd0b6a2d2012-03-05 15:07:25 +00001226 build_gamma_table(gGammaTables[0], 0x00);
1227 build_gamma_table(gGammaTables[1], 0x55);
1228 build_gamma_table(gGammaTables[2], 0xAA);
1229 build_gamma_table(gGammaTables[3], 0xFF);
reed@google.com1ac83502012-02-28 17:06:02 +00001230
1231 gInited = true;
1232 }
1233 SkASSERT(0 == (luminance >> 8));
1234 return gGammaTables[luminance >> 6];
1235}
1236
bungeman@google.com3aacb412012-03-13 14:55:12 +00001237#else //SK_USE_COLOR_LUMINANCE
reed@google.comd61b92b2012-03-02 16:02:07 +00001238static const uint8_t* getIdentityTable() {
1239 static bool gOnce;
1240 static uint8_t gIdentityTable[256];
1241 if (!gOnce) {
1242 for (int i = 0; i < 256; ++i) {
1243 gIdentityTable[i] = i;
1244 }
1245 gOnce = true;
1246 }
1247 return gIdentityTable;
1248}
bungeman@google.com3aacb412012-03-13 14:55:12 +00001249#endif //SK_USE_COLOR_LUMINANCE
reed@google.com1ac83502012-02-28 17:06:02 +00001250
1251static uint16_t packTriple(unsigned r, unsigned g, unsigned b) {
reed@google.comc5181342011-05-17 20:52:46 +00001252 return SkPackRGB16(r >> 3, g >> 2, b >> 3);
1253}
1254
reed@google.com73824072011-06-23 13:17:30 +00001255static uint16_t grayToRGB16(U8CPU gray) {
1256 SkASSERT(gray <= 255);
1257 return SkPackRGB16(gray >> 3, gray >> 2, gray >> 3);
1258}
1259
1260static int bittst(const uint8_t data[], int bitOffset) {
1261 SkASSERT(bitOffset >= 0);
reed@google.comc8e0f932011-06-23 19:39:49 +00001262 int lowBit = data[bitOffset >> 3] >> (~bitOffset & 7);
reed@google.com73824072011-06-23 13:17:30 +00001263 return lowBit & 1;
1264}
1265
reed@google.comeffc5012011-06-27 16:44:46 +00001266static void copyFT2LCD16(const SkGlyph& glyph, const FT_Bitmap& bitmap,
reed@google.coma1bfa212012-03-08 21:57:12 +00001267 int lcdIsBGR, bool lcdIsVert, const uint8_t* tableR,
reed@google.com1ac83502012-02-28 17:06:02 +00001268 const uint8_t* tableG, const uint8_t* tableB) {
reed@google.coma1bfa212012-03-08 21:57:12 +00001269 if (lcdIsVert) {
1270 SkASSERT(3 * glyph.fHeight == bitmap.rows);
1271 } else {
1272 SkASSERT(glyph.fHeight == bitmap.rows);
1273 }
1274
reed@google.comea2333d2011-03-14 16:44:56 +00001275 uint16_t* dst = reinterpret_cast<uint16_t*>(glyph.fImage);
reed@google.com73824072011-06-23 13:17:30 +00001276 const size_t dstRB = glyph.rowBytes();
1277 const int width = glyph.fWidth;
1278 const uint8_t* src = bitmap.buffer;
reed@google.comea2333d2011-03-14 16:44:56 +00001279
reed@google.com73824072011-06-23 13:17:30 +00001280 switch (bitmap.pixel_mode) {
1281 case FT_PIXEL_MODE_MONO: {
1282 for (int y = 0; y < glyph.fHeight; ++y) {
1283 for (int x = 0; x < width; ++x) {
1284 dst[x] = -bittst(src, x);
1285 }
1286 dst = (uint16_t*)((char*)dst + dstRB);
1287 src += bitmap.pitch;
1288 }
1289 } break;
1290 case FT_PIXEL_MODE_GRAY: {
1291 for (int y = 0; y < glyph.fHeight; ++y) {
1292 for (int x = 0; x < width; ++x) {
1293 dst[x] = grayToRGB16(src[x]);
1294 }
1295 dst = (uint16_t*)((char*)dst + dstRB);
1296 src += bitmap.pitch;
1297 }
1298 } break;
1299 default: {
reed@google.coma1bfa212012-03-08 21:57:12 +00001300 SkASSERT(lcdIsVert || (glyph.fWidth * 3 == bitmap.width));
reed@google.com73824072011-06-23 13:17:30 +00001301 for (int y = 0; y < glyph.fHeight; y++) {
reed@google.coma1bfa212012-03-08 21:57:12 +00001302 if (lcdIsVert) { // vertical stripes
1303 const uint8_t* srcR = src;
1304 const uint8_t* srcG = srcR + bitmap.pitch;
1305 const uint8_t* srcB = srcG + bitmap.pitch;
1306 if (lcdIsBGR) {
1307 SkTSwap(srcR, srcB);
reed@google.comeffc5012011-06-27 16:44:46 +00001308 }
reed@google.comeffc5012011-06-27 16:44:46 +00001309 for (int x = 0; x < width; x++) {
reed@google.coma1bfa212012-03-08 21:57:12 +00001310 dst[x] = packTriple(tableR[*srcR++],
1311 tableG[*srcG++],
1312 tableB[*srcB++]);
reed@google.comeffc5012011-06-27 16:44:46 +00001313 }
reed@google.coma1bfa212012-03-08 21:57:12 +00001314 src += 3 * bitmap.pitch;
1315 } else { // horizontal stripes
1316 const uint8_t* triple = src;
1317 if (lcdIsBGR) {
1318 for (int x = 0; x < width; x++) {
1319 dst[x] = packTriple(tableR[triple[2]],
1320 tableG[triple[1]],
1321 tableB[triple[0]]);
1322 triple += 3;
1323 }
1324 } else {
1325 for (int x = 0; x < width; x++) {
1326 dst[x] = packTriple(tableR[triple[0]],
1327 tableG[triple[1]],
1328 tableB[triple[2]]);
1329 triple += 3;
1330 }
1331 }
1332 src += bitmap.pitch;
reed@google.com73824072011-06-23 13:17:30 +00001333 }
reed@google.com73824072011-06-23 13:17:30 +00001334 dst = (uint16_t*)((char*)dst + dstRB);
1335 }
1336 } break;
reed@google.comea2333d2011-03-14 16:44:56 +00001337 }
1338}
1339
reed@android.com8a1c16f2008-12-17 15:59:43 +00001340void SkScalerContext_FreeType::generateImage(const SkGlyph& glyph) {
1341 SkAutoMutexAcquire ac(gFTMutex);
1342
1343 FT_Error err;
1344
1345 if (this->setupSize()) {
1346 goto ERROR;
1347 }
1348
1349 err = FT_Load_Glyph( fFace, glyph.getGlyphID(fBaseGlyphCount), fLoadGlyphFlags);
1350 if (err != 0) {
1351 SkDEBUGF(("SkScalerContext_FreeType::generateImage: FT_Load_Glyph(glyph:%d width:%d height:%d rb:%d flags:%d) returned 0x%x\n",
1352 glyph.getGlyphID(fBaseGlyphCount), glyph.fWidth, glyph.fHeight, glyph.rowBytes(), fLoadGlyphFlags, err));
1353 ERROR:
1354 memset(glyph.fImage, 0, glyph.rowBytes() * glyph.fHeight);
1355 return;
1356 }
1357
reed@google.com1ac83502012-02-28 17:06:02 +00001358#ifdef SK_USE_COLOR_LUMINANCE
1359 SkColor lumColor = fRec.getLuminanceColor();
1360 const uint8_t* tableR = getGammaTable(SkColorGetR(lumColor));
1361 const uint8_t* tableG = getGammaTable(SkColorGetG(lumColor));
1362 const uint8_t* tableB = getGammaTable(SkColorGetB(lumColor));
1363#else
1364 unsigned lum = fRec.getLuminanceByte();
reed@google.comd61b92b2012-03-02 16:02:07 +00001365 const uint8_t* tableR;
1366 const uint8_t* tableG;
1367 const uint8_t* tableB;
1368
1369 bool isWhite = lum >= WHITE_LUMINANCE_LIMIT;
1370 bool isBlack = lum <= BLACK_LUMINANCE_LIMIT;
1371 if ((gGammaTables[0] || gGammaTables[1]) && (isBlack || isWhite)) {
1372 tableR = tableG = tableB = gGammaTables[isBlack ? 0 : 1];
1373 } else {
1374 tableR = tableG = tableB = getIdentityTable();
1375 }
reed@google.com1ac83502012-02-28 17:06:02 +00001376#endif
1377
reed@google.coma1bfa212012-03-08 21:57:12 +00001378 const bool doBGR = SkToBool(fRec.fFlags & SkScalerContext::kLCD_BGROrder_Flag);
1379 const bool doVert = fLCDIsVert;
1380
reed@android.com8a1c16f2008-12-17 15:59:43 +00001381 switch ( fFace->glyph->format ) {
1382 case FT_GLYPH_FORMAT_OUTLINE: {
1383 FT_Outline* outline = &fFace->glyph->outline;
1384 FT_BBox bbox;
1385 FT_Bitmap target;
1386
senorblanco@chromium.org4526a842010-02-05 23:08:20 +00001387 if (fRec.fFlags & kEmbolden_Flag) {
1388 emboldenOutline(outline);
1389 }
1390
reed@android.com8a1c16f2008-12-17 15:59:43 +00001391 int dx = 0, dy = 0;
agl@chromium.orga2c71cb2010-06-17 20:49:17 +00001392 if (fRec.fFlags & SkScalerContext::kSubpixelPositioning_Flag) {
djsollen@google.comd8b599c2012-03-19 19:44:19 +00001393 dx = FixedToDot6(glyph.getSubXFixed());
1394 dy = FixedToDot6(glyph.getSubYFixed());
reed@android.com8a1c16f2008-12-17 15:59:43 +00001395 // negate dy since freetype-y-goes-up and skia-y-goes-down
1396 dy = -dy;
1397 }
1398 FT_Outline_Get_CBox(outline, &bbox);
1399 /*
1400 what we really want to do for subpixel is
1401 offset(dx, dy)
1402 compute_bounds
1403 offset(bbox & !63)
1404 but that is two calls to offset, so we do the following, which
1405 achieves the same thing with only one offset call.
1406 */
1407 FT_Outline_Translate(outline, dx - ((bbox.xMin + dx) & ~63),
1408 dy - ((bbox.yMin + dy) & ~63));
1409
reed@google.comea2333d2011-03-14 16:44:56 +00001410 if (SkMask::kLCD16_Format == glyph.fMaskFormat) {
reed@google.coma1bfa212012-03-08 21:57:12 +00001411 FT_Render_Glyph(fFace->glyph, doVert ? FT_RENDER_MODE_LCD_V : FT_RENDER_MODE_LCD);
1412 copyFT2LCD16(glyph, fFace->glyph->bitmap, doBGR, doVert,
reed@google.com1ac83502012-02-28 17:06:02 +00001413 tableR, tableG, tableB);
reed@google.comea2333d2011-03-14 16:44:56 +00001414 } else {
1415 target.width = glyph.fWidth;
1416 target.rows = glyph.fHeight;
1417 target.pitch = glyph.rowBytes();
1418 target.buffer = reinterpret_cast<uint8_t*>(glyph.fImage);
1419 target.pixel_mode = compute_pixel_mode(
1420 (SkMask::Format)fRec.fMaskFormat);
1421 target.num_grays = 256;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001422
reed@google.comea2333d2011-03-14 16:44:56 +00001423 memset(glyph.fImage, 0, glyph.rowBytes() * glyph.fHeight);
1424 FT_Outline_Get_Bitmap(gFTLibrary, outline, &target);
1425 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001426 } break;
1427
1428 case FT_GLYPH_FORMAT_BITMAP: {
agl@chromium.orge76073b2010-06-04 20:31:17 +00001429 if (fRec.fFlags & kEmbolden_Flag) {
1430 FT_GlyphSlot_Own_Bitmap(fFace->glyph);
1431 FT_Bitmap_Embolden(gFTLibrary, &fFace->glyph->bitmap, kBitmapEmboldenStrength, 0);
1432 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001433 SkASSERT_CONTINUE(glyph.fWidth == fFace->glyph->bitmap.width);
1434 SkASSERT_CONTINUE(glyph.fHeight == fFace->glyph->bitmap.rows);
1435 SkASSERT_CONTINUE(glyph.fTop == -fFace->glyph->bitmap_top);
1436 SkASSERT_CONTINUE(glyph.fLeft == fFace->glyph->bitmap_left);
1437
1438 const uint8_t* src = (const uint8_t*)fFace->glyph->bitmap.buffer;
1439 uint8_t* dst = (uint8_t*)glyph.fImage;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001440
agl@chromium.org558434a2009-08-11 17:22:38 +00001441 if (fFace->glyph->bitmap.pixel_mode == FT_PIXEL_MODE_GRAY ||
1442 (fFace->glyph->bitmap.pixel_mode == FT_PIXEL_MODE_MONO &&
1443 glyph.fMaskFormat == SkMask::kBW_Format)) {
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001444 unsigned srcRowBytes = fFace->glyph->bitmap.pitch;
1445 unsigned dstRowBytes = glyph.rowBytes();
1446 unsigned minRowBytes = SkMin32(srcRowBytes, dstRowBytes);
1447 unsigned extraRowBytes = dstRowBytes - minRowBytes;
1448
1449 for (int y = fFace->glyph->bitmap.rows - 1; y >= 0; --y) {
1450 memcpy(dst, src, minRowBytes);
1451 memset(dst + minRowBytes, 0, extraRowBytes);
1452 src += srcRowBytes;
1453 dst += dstRowBytes;
1454 }
agl@chromium.org558434a2009-08-11 17:22:38 +00001455 } else if (fFace->glyph->bitmap.pixel_mode == FT_PIXEL_MODE_MONO &&
reed@google.comeffc5012011-06-27 16:44:46 +00001456 glyph.fMaskFormat == SkMask::kA8_Format) {
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001457 for (int y = 0; y < fFace->glyph->bitmap.rows; ++y) {
1458 uint8_t byte = 0;
1459 int bits = 0;
1460 const uint8_t* src_row = src;
1461 uint8_t* dst_row = dst;
1462
1463 for (int x = 0; x < fFace->glyph->bitmap.width; ++x) {
1464 if (!bits) {
1465 byte = *src_row++;
1466 bits = 8;
1467 }
1468
1469 *dst_row++ = byte & 0x80 ? 0xff : 0;
1470 bits--;
1471 byte <<= 1;
1472 }
1473
1474 src += fFace->glyph->bitmap.pitch;
1475 dst += glyph.rowBytes();
1476 }
reed@google.com73824072011-06-23 13:17:30 +00001477 } else if (SkMask::kLCD16_Format == glyph.fMaskFormat) {
reed@google.coma1bfa212012-03-08 21:57:12 +00001478 copyFT2LCD16(glyph, fFace->glyph->bitmap, doBGR, doVert,
reed@google.com1ac83502012-02-28 17:06:02 +00001479 tableR, tableG, tableB);
agl@chromium.org558434a2009-08-11 17:22:38 +00001480 } else {
tomhudson@google.com0c00f212011-12-28 14:59:50 +00001481 SkDEBUGFAIL("unknown glyph bitmap transform needed");
reed@android.com8a1c16f2008-12-17 15:59:43 +00001482 }
1483 } break;
1484
1485 default:
tomhudson@google.com0c00f212011-12-28 14:59:50 +00001486 SkDEBUGFAIL("unknown glyph format");
reed@android.com8a1c16f2008-12-17 15:59:43 +00001487 goto ERROR;
1488 }
reed@google.com1f6b4ae2011-11-22 14:20:55 +00001489
reed@google.comd61b92b2012-03-02 16:02:07 +00001490// We used to always do this pre-USE_COLOR_LUMINANCE, but with colorlum,
1491// it is optional
1492#if defined(SK_GAMMA_APPLY_TO_A8) || !defined(SK_USE_COLOR_LUMINANCE)
reed@google.com1ac83502012-02-28 17:06:02 +00001493 if (SkMask::kA8_Format == glyph.fMaskFormat) {
1494 SkASSERT(tableR == tableG && tableR == tableB);
1495 const uint8_t* table = tableR;
1496 uint8_t* SK_RESTRICT dst = (uint8_t*)glyph.fImage;
1497 unsigned rowBytes = glyph.rowBytes();
1498
1499 for (int y = glyph.fHeight - 1; y >= 0; --y) {
1500 for (int x = glyph.fWidth - 1; x >= 0; --x) {
1501 dst[x] = table[dst[x]];
reed@google.com1f6b4ae2011-11-22 14:20:55 +00001502 }
reed@google.com1ac83502012-02-28 17:06:02 +00001503 dst += rowBytes;
reed@google.com1f6b4ae2011-11-22 14:20:55 +00001504 }
1505 }
reed@google.com1ac83502012-02-28 17:06:02 +00001506#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +00001507}
1508
1509///////////////////////////////////////////////////////////////////////////////
1510
djsollen@google.comd8b599c2012-03-19 19:44:19 +00001511#define ft2sk(x) SkFixedToScalar(Dot6ToFixed(x))
reed@android.com8a1c16f2008-12-17 15:59:43 +00001512
reed@android.com6f252972009-01-14 16:46:16 +00001513#if FREETYPE_MAJOR >= 2 && FREETYPE_MINOR >= 2
reed@android.com8a1c16f2008-12-17 15:59:43 +00001514 #define CONST_PARAM const
1515#else // older freetype doesn't use const here
1516 #define CONST_PARAM
1517#endif
1518
1519static int move_proc(CONST_PARAM FT_Vector* pt, void* ctx) {
1520 SkPath* path = (SkPath*)ctx;
1521 path->close(); // to close the previous contour (if any)
1522 path->moveTo(ft2sk(pt->x), -ft2sk(pt->y));
1523 return 0;
1524}
1525
1526static int line_proc(CONST_PARAM FT_Vector* pt, void* ctx) {
1527 SkPath* path = (SkPath*)ctx;
1528 path->lineTo(ft2sk(pt->x), -ft2sk(pt->y));
1529 return 0;
1530}
1531
1532static int quad_proc(CONST_PARAM FT_Vector* pt0, CONST_PARAM FT_Vector* pt1,
1533 void* ctx) {
1534 SkPath* path = (SkPath*)ctx;
1535 path->quadTo(ft2sk(pt0->x), -ft2sk(pt0->y), ft2sk(pt1->x), -ft2sk(pt1->y));
1536 return 0;
1537}
1538
1539static int cubic_proc(CONST_PARAM FT_Vector* pt0, CONST_PARAM FT_Vector* pt1,
1540 CONST_PARAM FT_Vector* pt2, void* ctx) {
1541 SkPath* path = (SkPath*)ctx;
1542 path->cubicTo(ft2sk(pt0->x), -ft2sk(pt0->y), ft2sk(pt1->x),
1543 -ft2sk(pt1->y), ft2sk(pt2->x), -ft2sk(pt2->y));
1544 return 0;
1545}
1546
1547void SkScalerContext_FreeType::generatePath(const SkGlyph& glyph,
1548 SkPath* path) {
1549 SkAutoMutexAcquire ac(gFTMutex);
1550
1551 SkASSERT(&glyph && path);
1552
1553 if (this->setupSize()) {
1554 path->reset();
1555 return;
1556 }
1557
1558 uint32_t flags = fLoadGlyphFlags;
1559 flags |= FT_LOAD_NO_BITMAP; // ignore embedded bitmaps so we're sure to get the outline
1560 flags &= ~FT_LOAD_RENDER; // don't scan convert (we just want the outline)
1561
1562 FT_Error err = FT_Load_Glyph( fFace, glyph.getGlyphID(fBaseGlyphCount), flags);
1563
1564 if (err != 0) {
1565 SkDEBUGF(("SkScalerContext_FreeType::generatePath: FT_Load_Glyph(glyph:%d flags:%d) returned 0x%x\n",
1566 glyph.getGlyphID(fBaseGlyphCount), flags, err));
1567 path->reset();
1568 return;
1569 }
1570
senorblanco@chromium.org4526a842010-02-05 23:08:20 +00001571 if (fRec.fFlags & kEmbolden_Flag) {
1572 emboldenOutline(&fFace->glyph->outline);
1573 }
1574
reed@android.com8a1c16f2008-12-17 15:59:43 +00001575 FT_Outline_Funcs funcs;
1576
1577 funcs.move_to = move_proc;
1578 funcs.line_to = line_proc;
1579 funcs.conic_to = quad_proc;
1580 funcs.cubic_to = cubic_proc;
1581 funcs.shift = 0;
1582 funcs.delta = 0;
1583
1584 err = FT_Outline_Decompose(&fFace->glyph->outline, &funcs, path);
1585
1586 if (err != 0) {
1587 SkDEBUGF(("SkScalerContext_FreeType::generatePath: FT_Load_Glyph(glyph:%d flags:%d) returned 0x%x\n",
1588 glyph.getGlyphID(fBaseGlyphCount), flags, err));
1589 path->reset();
1590 return;
1591 }
1592
1593 path->close();
1594}
1595
1596void SkScalerContext_FreeType::generateFontMetrics(SkPaint::FontMetrics* mx,
1597 SkPaint::FontMetrics* my) {
1598 if (NULL == mx && NULL == my) {
1599 return;
1600 }
1601
1602 SkAutoMutexAcquire ac(gFTMutex);
1603
1604 if (this->setupSize()) {
reed@android.coma8a8b8b2009-05-04 15:00:11 +00001605 ERROR:
reed@android.com8a1c16f2008-12-17 15:59:43 +00001606 if (mx) {
reed@android.com4516f472009-06-29 16:25:36 +00001607 sk_bzero(mx, sizeof(SkPaint::FontMetrics));
reed@android.com8a1c16f2008-12-17 15:59:43 +00001608 }
1609 if (my) {
reed@android.com4516f472009-06-29 16:25:36 +00001610 sk_bzero(my, sizeof(SkPaint::FontMetrics));
reed@android.com8a1c16f2008-12-17 15:59:43 +00001611 }
1612 return;
1613 }
1614
reed@android.coma8a8b8b2009-05-04 15:00:11 +00001615 FT_Face face = fFace;
1616 int upem = face->units_per_EM;
1617 if (upem <= 0) {
1618 goto ERROR;
1619 }
1620
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001621 SkPoint pts[6];
1622 SkFixed ys[6];
reed@android.com8a1c16f2008-12-17 15:59:43 +00001623 SkFixed scaleY = fScaleY;
1624 SkFixed mxy = fMatrix22.xy;
1625 SkFixed myy = fMatrix22.yy;
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001626 SkScalar xmin = SkIntToScalar(face->bbox.xMin) / upem;
1627 SkScalar xmax = SkIntToScalar(face->bbox.xMax) / upem;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001628
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001629 int leading = face->height - (face->ascender + -face->descender);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001630 if (leading < 0) {
1631 leading = 0;
1632 }
1633
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001634 // Try to get the OS/2 table from the font. This contains the specific
1635 // average font width metrics which Windows uses.
1636 TT_OS2* os2 = (TT_OS2*) FT_Get_Sfnt_Table(face, ft_sfnt_os2);
1637
reed@android.com8a1c16f2008-12-17 15:59:43 +00001638 ys[0] = -face->bbox.yMax;
1639 ys[1] = -face->ascender;
1640 ys[2] = -face->descender;
1641 ys[3] = -face->bbox.yMin;
1642 ys[4] = leading;
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001643 ys[5] = os2 ? os2->xAvgCharWidth : 0;
1644
1645 SkScalar x_height;
1646 if (os2 && os2->sxHeight) {
1647 x_height = SkFixedToScalar(SkMulDiv(fScaleX, os2->sxHeight, upem));
1648 } else {
1649 const FT_UInt x_glyph = FT_Get_Char_Index(fFace, 'x');
1650 if (x_glyph) {
1651 FT_BBox bbox;
1652 FT_Load_Glyph(fFace, x_glyph, fLoadGlyphFlags);
senorblanco@chromium.org4526a842010-02-05 23:08:20 +00001653 if (fRec.fFlags & kEmbolden_Flag) {
1654 emboldenOutline(&fFace->glyph->outline);
1655 }
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001656 FT_Outline_Get_CBox(&fFace->glyph->outline, &bbox);
epoger@google.comb90113d2012-01-18 19:20:39 +00001657 x_height = SkFixedToScalar(SkFDot6ToFixed(bbox.yMax));
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001658 } else {
1659 x_height = 0;
1660 }
1661 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001662
1663 // convert upem-y values into scalar points
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001664 for (int i = 0; i < 6; i++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001665 SkFixed y = SkMulDiv(scaleY, ys[i], upem);
1666 SkFixed x = SkFixedMul(mxy, y);
1667 y = SkFixedMul(myy, y);
1668 pts[i].set(SkFixedToScalar(x), SkFixedToScalar(y));
1669 }
1670
1671 if (mx) {
1672 mx->fTop = pts[0].fX;
1673 mx->fAscent = pts[1].fX;
1674 mx->fDescent = pts[2].fX;
1675 mx->fBottom = pts[3].fX;
1676 mx->fLeading = pts[4].fX;
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001677 mx->fAvgCharWidth = pts[5].fX;
1678 mx->fXMin = xmin;
1679 mx->fXMax = xmax;
1680 mx->fXHeight = x_height;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001681 }
1682 if (my) {
1683 my->fTop = pts[0].fY;
1684 my->fAscent = pts[1].fY;
1685 my->fDescent = pts[2].fY;
1686 my->fBottom = pts[3].fY;
1687 my->fLeading = pts[4].fY;
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001688 my->fAvgCharWidth = pts[5].fY;
1689 my->fXMin = xmin;
1690 my->fXMax = xmax;
1691 my->fXHeight = x_height;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001692 }
1693}
1694
1695////////////////////////////////////////////////////////////////////////
1696////////////////////////////////////////////////////////////////////////
1697
1698SkScalerContext* SkFontHost::CreateScalerContext(const SkDescriptor* desc) {
reed@android.com62900b42009-02-11 15:07:19 +00001699 SkScalerContext_FreeType* c = SkNEW_ARGS(SkScalerContext_FreeType, (desc));
1700 if (!c->success()) {
1701 SkDELETE(c);
1702 c = NULL;
1703 }
1704 return c;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001705}
1706
1707///////////////////////////////////////////////////////////////////////////////
1708
1709/* Export this so that other parts of our FonttHost port can make use of our
1710 ability to extract the name+style from a stream, using FreeType's api.
1711*/
djsollen@google.com4dc686d2012-02-15 21:03:45 +00001712bool find_name_and_attributes(SkStream* stream, SkString* name,
1713 SkTypeface::Style* style, bool* isFixedWidth) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001714 FT_Library library;
reed@android.combfbd4ff2009-07-23 17:44:41 +00001715 if (FT_Init_FreeType(&library)) {
djsollen@google.com4dc686d2012-02-15 21:03:45 +00001716 return false;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001717 }
1718
1719 FT_Open_Args args;
1720 memset(&args, 0, sizeof(args));
1721
1722 const void* memoryBase = stream->getMemoryBase();
1723 FT_StreamRec streamRec;
1724
1725 if (NULL != memoryBase) {
1726 args.flags = FT_OPEN_MEMORY;
1727 args.memory_base = (const FT_Byte*)memoryBase;
1728 args.memory_size = stream->getLength();
1729 } else {
1730 memset(&streamRec, 0, sizeof(streamRec));
1731 streamRec.size = stream->read(NULL, 0);
1732 streamRec.descriptor.pointer = stream;
1733 streamRec.read = sk_stream_read;
1734 streamRec.close = sk_stream_close;
1735
1736 args.flags = FT_OPEN_STREAM;
1737 args.stream = &streamRec;
1738 }
1739
1740 FT_Face face;
1741 if (FT_Open_Face(library, &args, 0, &face)) {
1742 FT_Done_FreeType(library);
djsollen@google.com4dc686d2012-02-15 21:03:45 +00001743 return false;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001744 }
1745
djsollen@google.com4dc686d2012-02-15 21:03:45 +00001746 int tempStyle = SkTypeface::kNormal;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001747 if (face->style_flags & FT_STYLE_FLAG_BOLD) {
djsollen@google.com4dc686d2012-02-15 21:03:45 +00001748 tempStyle |= SkTypeface::kBold;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001749 }
1750 if (face->style_flags & FT_STYLE_FLAG_ITALIC) {
djsollen@google.com4dc686d2012-02-15 21:03:45 +00001751 tempStyle |= SkTypeface::kItalic;
1752 }
1753
1754 if (name) {
1755 name->set(face->family_name);
1756 }
1757 if (style) {
1758 *style = (SkTypeface::Style) tempStyle;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001759 }
reed@google.com5b31b0f2011-02-23 14:41:42 +00001760 if (isFixedWidth) {
1761 *isFixedWidth = FT_IS_FIXED_WIDTH(face);
1762 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001763
1764 FT_Done_Face(face);
1765 FT_Done_FreeType(library);
djsollen@google.com4dc686d2012-02-15 21:03:45 +00001766 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001767}