blob: 45ae7ecffca0049a97411496cf660fd148d732c9 [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#include "SkBitmap.h"
10#include "SkCanvas.h"
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000011#include "SkColorPriv.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000012#include "SkDescriptor.h"
13#include "SkFDot6.h"
bungeman@google.com3aacb412012-03-13 14:55:12 +000014#include "SkFloatingPoint.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000015#include "SkFontHost.h"
george@mozilla.comc59b5da2012-08-23 00:39:08 +000016#include "SkFontHost_FreeType_common.h"
bungeman@google.combbe50132012-07-24 20:33:21 +000017#include "SkGlyph.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000018#include "SkMask.h"
bungeman@google.com97efada2012-07-30 20:40:50 +000019#include "SkMaskGamma.h"
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +000020#include "SkAdvancedTypefaceMetrics.h"
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000021#include "SkScalerContext.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000022#include "SkStream.h"
23#include "SkString.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000024#include "SkTemplates.h"
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000025#include "SkThread.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000026
27#include <ft2build.h>
28#include FT_FREETYPE_H
29#include FT_OUTLINE_H
30#include FT_SIZES_H
agl@chromium.orgcc3096b2009-04-22 22:09:04 +000031#include FT_TRUETYPE_TABLES_H
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000032#include FT_TYPE1_TABLES_H
agl@chromium.orge76073b2010-06-04 20:31:17 +000033#include FT_BITMAP_H
agl@chromium.org36bb6972010-06-04 20:57:16 +000034// In the past, FT_GlyphSlot_Own_Bitmap was defined in this header file.
35#include FT_SYNTHESIS_H
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000036#include FT_XFREE86_H
epoger@google.com5070d792011-06-29 20:43:14 +000037#ifdef FT_LCD_FILTER_H
agl@chromium.org309485b2009-07-21 17:41:32 +000038#include FT_LCD_FILTER_H
epoger@google.com5070d792011-06-29 20:43:14 +000039#endif
agl@chromium.org309485b2009-07-21 17:41:32 +000040
reed@android.com8a1c16f2008-12-17 15:59:43 +000041#ifdef FT_ADVANCES_H
42#include FT_ADVANCES_H
43#endif
44
agl@chromium.orgcc3096b2009-04-22 22:09:04 +000045#if 0
46// Also include the files by name for build tools which require this.
47#include <freetype/freetype.h>
48#include <freetype/ftoutln.h>
49#include <freetype/ftsizes.h>
50#include <freetype/tttables.h>
51#include <freetype/ftadvanc.h>
agl@chromium.org309485b2009-07-21 17:41:32 +000052#include <freetype/ftlcdfil.h>
agl@chromium.orge76073b2010-06-04 20:31:17 +000053#include <freetype/ftbitmap.h>
agl@chromium.org36bb6972010-06-04 20:57:16 +000054#include <freetype/ftsynth.h>
agl@chromium.orgcc3096b2009-04-22 22:09:04 +000055#endif
56
reed@android.com8a1c16f2008-12-17 15:59:43 +000057//#define ENABLE_GLYPH_SPEW // for tracing calls
58//#define DUMP_STRIKE_CREATION
59
reed@google.com1ac83502012-02-28 17:06:02 +000060//#define SK_GAMMA_APPLY_TO_A8
reed@google.com1ac83502012-02-28 17:06:02 +000061
vandebo@chromium.org6f72d1e2011-02-14 23:19:59 +000062using namespace skia_advanced_typeface_metrics_utils;
63
reed@google.comeffc5012011-06-27 16:44:46 +000064static bool isLCD(const SkScalerContext::Rec& rec) {
65 switch (rec.fMaskFormat) {
66 case SkMask::kLCD16_Format:
67 case SkMask::kLCD32_Format:
68 return true;
69 default:
70 return false;
71 }
72}
73
reed@android.com8a1c16f2008-12-17 15:59:43 +000074//////////////////////////////////////////////////////////////////////////
75
76struct SkFaceRec;
77
digit@google.com1771cbf2012-01-26 21:26:40 +000078SK_DECLARE_STATIC_MUTEX(gFTMutex);
reed@android.com8a1c16f2008-12-17 15:59:43 +000079static int gFTCount;
80static FT_Library gFTLibrary;
81static SkFaceRec* gFaceRecHead;
agl@chromium.orgf18d8762009-07-28 18:38:08 +000082static bool gLCDSupportValid; // true iff |gLCDSupport| has been set.
83static bool gLCDSupport; // true iff LCD is supported by the runtime.
reed@google.coma1c32562012-03-01 19:38:23 +000084static int gLCDExtra; // number of extra pixels for filtering.
reed@android.com8a1c16f2008-12-17 15:59:43 +000085
86/////////////////////////////////////////////////////////////////////////
87
agl@chromium.org309485b2009-07-21 17:41:32 +000088static bool
89InitFreetype() {
90 FT_Error err = FT_Init_FreeType(&gFTLibrary);
reed@google.comea2333d2011-03-14 16:44:56 +000091 if (err) {
agl@chromium.org309485b2009-07-21 17:41:32 +000092 return false;
reed@google.comea2333d2011-03-14 16:44:56 +000093 }
agl@chromium.org309485b2009-07-21 17:41:32 +000094
agl@chromium.org309485b2009-07-21 17:41:32 +000095 // Setup LCD filtering. This reduces colour fringes for LCD rendered
96 // glyphs.
epoger@google.comb371ed12011-06-29 21:20:52 +000097#ifdef FT_LCD_FILTER_H
bungeman@google.comd2dae962012-03-09 20:31:17 +000098 err = FT_Library_SetLcdFilter(gFTLibrary, FT_LCD_FILTER_DEFAULT);
99// err = FT_Library_SetLcdFilter(gFTLibrary, FT_LCD_FILTER_LIGHT);
agl@chromium.orgf18d8762009-07-28 18:38:08 +0000100 gLCDSupport = err == 0;
reed@google.coma1c32562012-03-01 19:38:23 +0000101 if (gLCDSupport) {
102 gLCDExtra = 2; //DEFAULT and LIGHT add one pixel to each side.
103 }
epoger@google.com5070d792011-06-29 20:43:14 +0000104#else
105 gLCDSupport = false;
106#endif
reed@android.com61608aa2009-07-31 14:52:54 +0000107 gLCDSupportValid = true;
agl@chromium.org309485b2009-07-21 17:41:32 +0000108
109 return true;
110}
111
george@mozilla.comc59b5da2012-08-23 00:39:08 +0000112class SkScalerContext_FreeType : public SkScalerContext_FreeType_Base {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000113public:
114 SkScalerContext_FreeType(const SkDescriptor* desc);
115 virtual ~SkScalerContext_FreeType();
agl@chromium.orgcc3096b2009-04-22 22:09:04 +0000116
reed@android.com62900b42009-02-11 15:07:19 +0000117 bool success() const {
reed@android.coma0f5d152009-06-22 17:38:10 +0000118 return fFaceRec != NULL &&
119 fFTSize != NULL &&
120 fFace != NULL;
reed@android.com62900b42009-02-11 15:07:19 +0000121 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000122
123protected:
ctguil@chromium.org0bc7bf52011-03-04 19:04:57 +0000124 virtual unsigned generateGlyphCount();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000125 virtual uint16_t generateCharToGlyph(SkUnichar uni);
126 virtual void generateAdvance(SkGlyph* glyph);
127 virtual void generateMetrics(SkGlyph* glyph);
bungeman@google.com97efada2012-07-30 20:40:50 +0000128 virtual void generateImage(const SkGlyph& glyph, SkMaskGamma::PreBlend* maskPreBlend);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000129 virtual void generatePath(const SkGlyph& glyph, SkPath* path);
130 virtual void generateFontMetrics(SkPaint::FontMetrics* mx,
131 SkPaint::FontMetrics* my);
reed@android.com9d3a9852010-01-08 14:07:42 +0000132 virtual SkUnichar generateGlyphToChar(uint16_t glyph);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000133
134private:
135 SkFaceRec* fFaceRec;
136 FT_Face fFace; // reference to shared face in gFaceRecHead
137 FT_Size fFTSize; // our own copy
138 SkFixed fScaleX, fScaleY;
139 FT_Matrix fMatrix22;
140 uint32_t fLoadGlyphFlags;
reed@google.combdc99882011-11-21 14:36:57 +0000141 bool fDoLinearMetrics;
reed@google.coma1bfa212012-03-08 21:57:12 +0000142 bool fLCDIsVert;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000143
144 FT_Error setupSize();
djsollen@google.comd8b599c2012-03-19 19:44:19 +0000145 void getBBoxForCurrentGlyph(SkGlyph* glyph, FT_BBox* bbox,
146 bool snapToPixelBoundary = false);
147 void updateGlyphIfLCD(SkGlyph* glyph);
bungeman@google.com875eb982012-04-12 15:53:23 +0000148 void updateGlyphPosIfLCD(SkGlyph* glyph);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000149};
150
151///////////////////////////////////////////////////////////////////////////
152///////////////////////////////////////////////////////////////////////////
153
154#include "SkStream.h"
155
156struct SkFaceRec {
157 SkFaceRec* fNext;
158 FT_Face fFace;
159 FT_StreamRec fFTStream;
160 SkStream* fSkStream;
161 uint32_t fRefCnt;
162 uint32_t fFontID;
163
reed@android.comb1d9d2e2009-03-04 17:37:51 +0000164 // assumes ownership of the stream, will call unref() when its done
reed@android.com8a1c16f2008-12-17 15:59:43 +0000165 SkFaceRec(SkStream* strm, uint32_t fontID);
166 ~SkFaceRec() {
reed@android.comb1d9d2e2009-03-04 17:37:51 +0000167 fSkStream->unref();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000168 }
169};
170
171extern "C" {
172 static unsigned long sk_stream_read(FT_Stream stream,
173 unsigned long offset,
174 unsigned char* buffer,
175 unsigned long count ) {
176 SkStream* str = (SkStream*)stream->descriptor.pointer;
177
178 if (count) {
179 if (!str->rewind()) {
180 return 0;
181 } else {
182 unsigned long ret;
183 if (offset) {
184 ret = str->read(NULL, offset);
185 if (ret != offset) {
186 return 0;
187 }
188 }
189 ret = str->read(buffer, count);
190 if (ret != count) {
191 return 0;
192 }
193 count = ret;
194 }
195 }
196 return count;
197 }
198
199 static void sk_stream_close( FT_Stream stream) {}
200}
201
202SkFaceRec::SkFaceRec(SkStream* strm, uint32_t fontID)
vandebo@chromium.org9af25f32012-03-28 21:24:27 +0000203 : fNext(NULL), fSkStream(strm), fRefCnt(1), fFontID(fontID) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000204// SkDEBUGF(("SkFaceRec: opening %s (%p)\n", key.c_str(), strm));
205
reed@android.com4516f472009-06-29 16:25:36 +0000206 sk_bzero(&fFTStream, sizeof(fFTStream));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000207 fFTStream.size = fSkStream->getLength();
208 fFTStream.descriptor.pointer = fSkStream;
209 fFTStream.read = sk_stream_read;
210 fFTStream.close = sk_stream_close;
211}
212
reed@android.com62900b42009-02-11 15:07:19 +0000213// Will return 0 on failure
reed@android.com8a1c16f2008-12-17 15:59:43 +0000214static SkFaceRec* ref_ft_face(uint32_t fontID) {
215 SkFaceRec* rec = gFaceRecHead;
216 while (rec) {
217 if (rec->fFontID == fontID) {
218 SkASSERT(rec->fFace);
219 rec->fRefCnt += 1;
220 return rec;
221 }
222 rec = rec->fNext;
223 }
224
225 SkStream* strm = SkFontHost::OpenStream(fontID);
226 if (NULL == strm) {
227 SkDEBUGF(("SkFontHost::OpenStream failed opening %x\n", fontID));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000228 return 0;
229 }
230
231 // this passes ownership of strm to the rec
232 rec = SkNEW_ARGS(SkFaceRec, (strm, fontID));
233
234 FT_Open_Args args;
235 memset(&args, 0, sizeof(args));
236 const void* memoryBase = strm->getMemoryBase();
237
238 if (NULL != memoryBase) {
239//printf("mmap(%s)\n", keyString.c_str());
240 args.flags = FT_OPEN_MEMORY;
241 args.memory_base = (const FT_Byte*)memoryBase;
242 args.memory_size = strm->getLength();
243 } else {
244//printf("fopen(%s)\n", keyString.c_str());
245 args.flags = FT_OPEN_STREAM;
246 args.stream = &rec->fFTStream;
247 }
248
agl@chromium.org61a678a2010-08-06 18:08:18 +0000249 int face_index;
250 int length = SkFontHost::GetFileName(fontID, NULL, 0, &face_index);
251 FT_Error err = FT_Open_Face(gFTLibrary, &args, length ? face_index : 0,
252 &rec->fFace);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000253
254 if (err) { // bad filename, try the default font
255 fprintf(stderr, "ERROR: unable to open font '%x'\n", fontID);
256 SkDELETE(rec);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000257 return 0;
258 } else {
259 SkASSERT(rec->fFace);
260 //fprintf(stderr, "Opened font '%s'\n", filename.c_str());
261 rec->fNext = gFaceRecHead;
262 gFaceRecHead = rec;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000263 return rec;
264 }
265}
266
267static void unref_ft_face(FT_Face face) {
268 SkFaceRec* rec = gFaceRecHead;
269 SkFaceRec* prev = NULL;
270 while (rec) {
271 SkFaceRec* next = rec->fNext;
272 if (rec->fFace == face) {
273 if (--rec->fRefCnt == 0) {
274 if (prev) {
275 prev->fNext = next;
276 } else {
277 gFaceRecHead = next;
278 }
279 FT_Done_Face(face);
280 SkDELETE(rec);
281 }
282 return;
283 }
284 prev = rec;
285 rec = next;
286 }
tomhudson@google.com0c00f212011-12-28 14:59:50 +0000287 SkDEBUGFAIL("shouldn't get here, face not in list");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000288}
289
290///////////////////////////////////////////////////////////////////////////
291
vandebo@chromium.org16be6b82011-01-28 21:28:56 +0000292// Work around for old versions of freetype.
293static FT_Error getAdvances(FT_Face face, FT_UInt start, FT_UInt count,
294 FT_Int32 loadFlags, FT_Fixed* advances) {
295#ifdef FT_ADVANCES_H
296 return FT_Get_Advances(face, start, count, loadFlags, advances);
297#else
298 if (!face || start >= face->num_glyphs ||
299 start + count > face->num_glyphs || loadFlags != FT_LOAD_NO_SCALE) {
300 return 6; // "Invalid argument."
301 }
302 if (count == 0)
303 return 0;
304
305 for (int i = 0; i < count; i++) {
306 FT_Error err = FT_Load_Glyph(face, start + i, FT_LOAD_NO_SCALE);
307 if (err)
308 return err;
309 advances[i] = face->glyph->advance.x;
310 }
311
312 return 0;
313#endif
314}
315
316static bool canEmbed(FT_Face face) {
djsollen@google.comfa394d42012-01-09 20:40:25 +0000317#ifdef FT_FSTYPE_RESTRICTED_LICENSE_EMBEDDING
vandebo@chromium.org16be6b82011-01-28 21:28:56 +0000318 FT_UShort fsType = FT_Get_FSType_Flags(face);
319 return (fsType & (FT_FSTYPE_RESTRICTED_LICENSE_EMBEDDING |
320 FT_FSTYPE_BITMAP_EMBEDDING_ONLY)) == 0;
321#else
322 // No embedding is 0x2 and bitmap embedding only is 0x200.
323 TT_OS2* os2_table;
324 if ((os2_table = (TT_OS2*)FT_Get_Sfnt_Table(face, ft_sfnt_os2)) != NULL) {
325 return (os2_table->fsType & 0x202) == 0;
326 }
327 return false; // We tried, fail safe.
328#endif
329}
330
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000331static bool GetLetterCBox(FT_Face face, char letter, FT_BBox* bbox) {
332 const FT_UInt glyph_id = FT_Get_Char_Index(face, letter);
333 if (!glyph_id)
334 return false;
335 FT_Load_Glyph(face, glyph_id, FT_LOAD_NO_SCALE);
336 FT_Outline_Get_CBox(&face->glyph->outline, bbox);
337 return true;
338}
339
vandebo@chromium.org6f72d1e2011-02-14 23:19:59 +0000340static bool getWidthAdvance(FT_Face face, int gId, int16_t* data) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000341 FT_Fixed advance = 0;
vandebo@chromium.org6f72d1e2011-02-14 23:19:59 +0000342 if (getAdvances(face, gId, 1, FT_LOAD_NO_SCALE, &advance)) {
343 return false;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000344 }
vandebo@chromium.org6f72d1e2011-02-14 23:19:59 +0000345 SkASSERT(data);
346 *data = advance;
347 return true;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000348}
349
vandebo@chromium.org6744d492011-05-09 18:13:47 +0000350static void populate_glyph_to_unicode(FT_Face& face,
351 SkTDArray<SkUnichar>* glyphToUnicode) {
352 // Check and see if we have Unicode cmaps.
353 for (int i = 0; i < face->num_charmaps; ++i) {
354 // CMaps known to support Unicode:
355 // Platform ID Encoding ID Name
356 // ----------- ----------- -----------------------------------
357 // 0 0,1 Apple Unicode
358 // 0 3 Apple Unicode 2.0 (preferred)
359 // 3 1 Microsoft Unicode UCS-2
360 // 3 10 Microsoft Unicode UCS-4 (preferred)
361 //
362 // See Apple TrueType Reference Manual
363 // http://developer.apple.com/fonts/TTRefMan/RM06/Chap6cmap.html
364 // http://developer.apple.com/fonts/TTRefMan/RM06/Chap6name.html#ID
365 // Microsoft OpenType Specification
366 // http://www.microsoft.com/typography/otspec/cmap.htm
367
368 FT_UShort platformId = face->charmaps[i]->platform_id;
369 FT_UShort encodingId = face->charmaps[i]->encoding_id;
370
371 if (platformId != 0 && platformId != 3) {
372 continue;
373 }
374 if (platformId == 3 && encodingId != 1 && encodingId != 10) {
375 continue;
376 }
377 bool preferredMap = ((platformId == 3 && encodingId == 10) ||
378 (platformId == 0 && encodingId == 3));
379
380 FT_Set_Charmap(face, face->charmaps[i]);
381 if (glyphToUnicode->isEmpty()) {
382 glyphToUnicode->setCount(face->num_glyphs);
383 memset(glyphToUnicode->begin(), 0,
384 sizeof(SkUnichar) * face->num_glyphs);
385 }
386
387 // Iterate through each cmap entry.
388 FT_UInt glyphIndex;
389 for (SkUnichar charCode = FT_Get_First_Char(face, &glyphIndex);
390 glyphIndex != 0;
391 charCode = FT_Get_Next_Char(face, charCode, &glyphIndex)) {
392 if (charCode &&
393 ((*glyphToUnicode)[glyphIndex] == 0 || preferredMap)) {
394 (*glyphToUnicode)[glyphIndex] = charCode;
395 }
396 }
397 }
398}
399
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000400// static
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000401SkAdvancedTypefaceMetrics* SkFontHost::GetAdvancedTypefaceMetrics(
vandebo@chromium.org325cb9a2011-03-30 18:36:29 +0000402 uint32_t fontID,
vandebo@chromium.org37ad8fb2011-08-18 02:38:50 +0000403 SkAdvancedTypefaceMetrics::PerGlyphInfo perGlyphInfo,
404 const uint32_t* glyphIDs,
405 uint32_t glyphIDsCount) {
djsollen@google.comda957722011-11-16 17:00:46 +0000406#if defined(SK_BUILD_FOR_MAC)
reed@google.com8a5d6922011-03-14 15:08:03 +0000407 return NULL;
408#else
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000409 SkAutoMutexAcquire ac(gFTMutex);
410 FT_Library libInit = NULL;
411 if (gFTCount == 0) {
412 if (!InitFreetype())
413 sk_throw();
414 libInit = gFTLibrary;
415 }
416 SkAutoTCallIProc<struct FT_LibraryRec_, FT_Done_FreeType> ftLib(libInit);
417 SkFaceRec* rec = ref_ft_face(fontID);
418 if (NULL == rec)
419 return NULL;
420 FT_Face face = rec->fFace;
421
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000422 SkAdvancedTypefaceMetrics* info = new SkAdvancedTypefaceMetrics;
423 info->fFontName.set(FT_Get_Postscript_Name(face));
424 info->fMultiMaster = FT_HAS_MULTIPLE_MASTERS(face);
425 info->fLastGlyphID = face->num_glyphs - 1;
426 info->fEmSize = 1000;
427
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000428 bool cid = false;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000429 const char* fontType = FT_Get_X11_Font_Format(face);
vandebo@chromium.orgc3a2ae52011-02-03 21:48:23 +0000430 if (strcmp(fontType, "Type 1") == 0) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000431 info->fType = SkAdvancedTypefaceMetrics::kType1_Font;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000432 } else if (strcmp(fontType, "CID Type 1") == 0) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000433 info->fType = SkAdvancedTypefaceMetrics::kType1CID_Font;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000434 cid = true;
435 } else if (strcmp(fontType, "CFF") == 0) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000436 info->fType = SkAdvancedTypefaceMetrics::kCFF_Font;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000437 } else if (strcmp(fontType, "TrueType") == 0) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000438 info->fType = SkAdvancedTypefaceMetrics::kTrueType_Font;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000439 cid = true;
440 TT_Header* ttHeader;
441 if ((ttHeader = (TT_Header*)FT_Get_Sfnt_Table(face,
442 ft_sfnt_head)) != NULL) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000443 info->fEmSize = ttHeader->Units_Per_EM;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000444 }
445 }
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000446
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000447 info->fStyle = 0;
448 if (FT_IS_FIXED_WIDTH(face))
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000449 info->fStyle |= SkAdvancedTypefaceMetrics::kFixedPitch_Style;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000450 if (face->style_flags & FT_STYLE_FLAG_ITALIC)
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000451 info->fStyle |= SkAdvancedTypefaceMetrics::kItalic_Style;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000452 // We should set either Symbolic or Nonsymbolic; Nonsymbolic if the font's
453 // character set is a subset of 'Adobe standard Latin.'
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000454 info->fStyle |= SkAdvancedTypefaceMetrics::kSymbolic_Style;
455
456 PS_FontInfoRec ps_info;
457 TT_Postscript* tt_info;
458 if (FT_Get_PS_Font_Info(face, &ps_info) == 0) {
459 info->fItalicAngle = ps_info.italic_angle;
460 } else if ((tt_info =
461 (TT_Postscript*)FT_Get_Sfnt_Table(face,
462 ft_sfnt_post)) != NULL) {
463 info->fItalicAngle = SkFixedToScalar(tt_info->italicAngle);
464 } else {
465 info->fItalicAngle = 0;
466 }
467
468 info->fAscent = face->ascender;
469 info->fDescent = face->descender;
470
471 // Figure out a good guess for StemV - Min width of i, I, !, 1.
472 // This probably isn't very good with an italic font.
473 int16_t min_width = SHRT_MAX;
vandebo@chromium.org6f72d1e2011-02-14 23:19:59 +0000474 info->fStemV = 0;
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000475 char stem_chars[] = {'i', 'I', '!', '1'};
476 for (size_t i = 0; i < SK_ARRAY_COUNT(stem_chars); i++) {
477 FT_BBox bbox;
478 if (GetLetterCBox(face, stem_chars[i], &bbox)) {
479 int16_t width = bbox.xMax - bbox.xMin;
480 if (width > 0 && width < min_width) {
481 min_width = width;
482 info->fStemV = min_width;
483 }
484 }
485 }
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000486
487 TT_PCLT* pclt_info;
488 TT_OS2* os2_table;
489 if ((pclt_info = (TT_PCLT*)FT_Get_Sfnt_Table(face, ft_sfnt_pclt)) != NULL) {
490 info->fCapHeight = pclt_info->CapHeight;
491 uint8_t serif_style = pclt_info->SerifStyle & 0x3F;
492 if (serif_style >= 2 && serif_style <= 6)
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000493 info->fStyle |= SkAdvancedTypefaceMetrics::kSerif_Style;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000494 else if (serif_style >= 9 && serif_style <= 12)
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000495 info->fStyle |= SkAdvancedTypefaceMetrics::kScript_Style;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000496 } else if ((os2_table =
497 (TT_OS2*)FT_Get_Sfnt_Table(face, ft_sfnt_os2)) != NULL) {
498 info->fCapHeight = os2_table->sCapHeight;
499 } else {
500 // Figure out a good guess for CapHeight: average the height of M and X.
501 FT_BBox m_bbox, x_bbox;
502 bool got_m, got_x;
503 got_m = GetLetterCBox(face, 'M', &m_bbox);
504 got_x = GetLetterCBox(face, 'X', &x_bbox);
505 if (got_m && got_x) {
506 info->fCapHeight = (m_bbox.yMax - m_bbox.yMin + x_bbox.yMax -
507 x_bbox.yMin) / 2;
508 } else if (got_m && !got_x) {
509 info->fCapHeight = m_bbox.yMax - m_bbox.yMin;
510 } else if (!got_m && got_x) {
511 info->fCapHeight = x_bbox.yMax - x_bbox.yMin;
512 }
513 }
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000514
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000515 info->fBBox = SkIRect::MakeLTRB(face->bbox.xMin, face->bbox.yMax,
516 face->bbox.xMax, face->bbox.yMin);
517
vandebo@chromium.org37ad8fb2011-08-18 02:38:50 +0000518 if (!canEmbed(face) || !FT_IS_SCALABLE(face) ||
vandebo@chromium.org325cb9a2011-03-30 18:36:29 +0000519 info->fType == SkAdvancedTypefaceMetrics::kOther_Font) {
520 perGlyphInfo = SkAdvancedTypefaceMetrics::kNo_PerGlyphInfo;
521 }
522
523 if (perGlyphInfo & SkAdvancedTypefaceMetrics::kHAdvance_PerGlyphInfo) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000524 if (FT_IS_FIXED_WIDTH(face)) {
525 appendRange(&info->fGlyphWidths, 0);
526 int16_t advance = face->max_advance_width;
527 info->fGlyphWidths->fAdvance.append(1, &advance);
528 finishRange(info->fGlyphWidths.get(), 0,
529 SkAdvancedTypefaceMetrics::WidthRange::kDefault);
vandebo@chromium.org6f72d1e2011-02-14 23:19:59 +0000530 } else if (!cid) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000531 appendRange(&info->fGlyphWidths, 0);
532 // So as to not blow out the stack, get advances in batches.
533 for (int gID = 0; gID < face->num_glyphs; gID += 128) {
534 FT_Fixed advances[128];
535 int advanceCount = 128;
536 if (gID + advanceCount > face->num_glyphs)
537 advanceCount = face->num_glyphs - gID + 1;
538 getAdvances(face, gID, advanceCount, FT_LOAD_NO_SCALE,
539 advances);
540 for (int i = 0; i < advanceCount; i++) {
541 int16_t advance = advances[gID + i];
542 info->fGlyphWidths->fAdvance.append(1, &advance);
543 }
544 }
545 finishRange(info->fGlyphWidths.get(), face->num_glyphs - 1,
546 SkAdvancedTypefaceMetrics::WidthRange::kRange);
547 } else {
vandebo@chromium.org6f72d1e2011-02-14 23:19:59 +0000548 info->fGlyphWidths.reset(
vandebo@chromium.org37ad8fb2011-08-18 02:38:50 +0000549 getAdvanceData(face,
550 face->num_glyphs,
551 glyphIDs,
552 glyphIDsCount,
553 &getWidthAdvance));
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000554 }
vandebo@chromium.org325cb9a2011-03-30 18:36:29 +0000555 }
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000556
vandebo@chromium.org325cb9a2011-03-30 18:36:29 +0000557 if (perGlyphInfo & SkAdvancedTypefaceMetrics::kVAdvance_PerGlyphInfo &&
558 FT_HAS_VERTICAL(face)) {
559 SkASSERT(false); // Not implemented yet.
560 }
561
562 if (perGlyphInfo & SkAdvancedTypefaceMetrics::kGlyphNames_PerGlyphInfo &&
563 info->fType == SkAdvancedTypefaceMetrics::kType1_Font) {
564 // Postscript fonts may contain more than 255 glyphs, so we end up
565 // using multiple font descriptions with a glyph ordering. Record
566 // the name of each glyph.
567 info->fGlyphNames.reset(
568 new SkAutoTArray<SkString>(face->num_glyphs));
569 for (int gID = 0; gID < face->num_glyphs; gID++) {
570 char glyphName[128]; // PS limit for names is 127 bytes.
571 FT_Get_Glyph_Name(face, gID, glyphName, 128);
572 info->fGlyphNames->get()[gID].set(glyphName);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000573 }
574 }
575
vandebo@chromium.org6744d492011-05-09 18:13:47 +0000576 if (perGlyphInfo & SkAdvancedTypefaceMetrics::kToUnicode_PerGlyphInfo &&
577 info->fType != SkAdvancedTypefaceMetrics::kType1_Font &&
578 face->num_charmaps) {
579 populate_glyph_to_unicode(face, &(info->fGlyphToUnicode));
580 }
581
vandebo@chromium.orgc3a2ae52011-02-03 21:48:23 +0000582 if (!canEmbed(face))
583 info->fType = SkAdvancedTypefaceMetrics::kNotEmbeddable_Font;
584
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000585 unref_ft_face(face);
586 return info;
reed@google.com8a5d6922011-03-14 15:08:03 +0000587#endif
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000588}
vandebo@chromium.org37ad8fb2011-08-18 02:38:50 +0000589
reed@google.com618ef5e2011-01-26 22:10:41 +0000590///////////////////////////////////////////////////////////////////////////
591
reed@google.comffe49f52011-11-22 19:42:41 +0000592#define BLACK_LUMINANCE_LIMIT 0x40
593#define WHITE_LUMINANCE_LIMIT 0xA0
594
reed@google.com8ed436c2011-07-21 14:12:36 +0000595static bool bothZero(SkScalar a, SkScalar b) {
596 return 0 == a && 0 == b;
597}
598
599// returns false if there is any non-90-rotation or skew
600static bool isAxisAligned(const SkScalerContext::Rec& rec) {
601 return 0 == rec.fPreSkewX &&
602 (bothZero(rec.fPost2x2[0][1], rec.fPost2x2[1][0]) ||
603 bothZero(rec.fPost2x2[0][0], rec.fPost2x2[1][1]));
604}
605
reed@google.com618ef5e2011-01-26 22:10:41 +0000606void SkFontHost::FilterRec(SkScalerContext::Rec* rec) {
bungeman@google.com8cf32262012-04-02 14:34:30 +0000607 //BOGUS: http://code.google.com/p/chromium/issues/detail?id=121119
608 //Cap the requested size as larger sizes give bogus values.
609 //Remove when http://code.google.com/p/skia/issues/detail?id=554 is fixed.
bungeman@google.com5582e632012-04-02 14:51:54 +0000610 if (rec->fTextSize > SkIntToScalar(1 << 14)) {
611 rec->fTextSize = SkIntToScalar(1 << 14);
bungeman@google.com8cf32262012-04-02 14:34:30 +0000612 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000613
reed@google.com618ef5e2011-01-26 22:10:41 +0000614 if (!gLCDSupportValid) {
615 InitFreetype();
616 FT_Done_FreeType(gFTLibrary);
617 }
reed@google.com5b31b0f2011-02-23 14:41:42 +0000618
reed@google.comeffc5012011-06-27 16:44:46 +0000619 if (!gLCDSupport && isLCD(*rec)) {
reed@google.com618ef5e2011-01-26 22:10:41 +0000620 // If the runtime Freetype library doesn't support LCD mode, we disable
621 // it here.
622 rec->fMaskFormat = SkMask::kA8_Format;
623 }
reed@google.com5b31b0f2011-02-23 14:41:42 +0000624
reed@google.com618ef5e2011-01-26 22:10:41 +0000625 SkPaint::Hinting h = rec->getHinting();
reed@google.comeffc5012011-06-27 16:44:46 +0000626 if (SkPaint::kFull_Hinting == h && !isLCD(*rec)) {
reed@google.com618ef5e2011-01-26 22:10:41 +0000627 // collapse full->normal hinting if we're not doing LCD
628 h = SkPaint::kNormal_Hinting;
reed@google.com618ef5e2011-01-26 22:10:41 +0000629 }
bungeman@google.comf4f2b802012-03-08 19:19:51 +0000630 if ((rec->fFlags & SkScalerContext::kSubpixelPositioning_Flag)) {
reed@google.com1ac83502012-02-28 17:06:02 +0000631 if (SkPaint::kNo_Hinting != h) {
632 h = SkPaint::kSlight_Hinting;
633 }
634 }
635
vandebo@chromium.org37ad8fb2011-08-18 02:38:50 +0000636#ifndef SK_IGNORE_ROTATED_FREETYPE_FIX
reed@google.com8ed436c2011-07-21 14:12:36 +0000637 // rotated text looks bad with hinting, so we disable it as needed
638 if (!isAxisAligned(*rec)) {
639 h = SkPaint::kNo_Hinting;
640 }
bsalomon@google.com0e35ca82011-07-22 17:56:19 +0000641#endif
reed@google.com618ef5e2011-01-26 22:10:41 +0000642 rec->setHinting(h);
reed@google.comffe49f52011-11-22 19:42:41 +0000643
bungeman@google.com97efada2012-07-30 20:40:50 +0000644#ifndef SK_GAMMA_APPLY_TO_A8
645 if (!isLCD(*rec)) {
646 rec->ignorePreBlend();
reed@google.comffe49f52011-11-22 19:42:41 +0000647 }
reed@google.com1ac83502012-02-28 17:06:02 +0000648#endif
reed@google.com618ef5e2011-01-26 22:10:41 +0000649}
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000650
djsollen@google.comda957722011-11-16 17:00:46 +0000651#ifdef SK_BUILD_FOR_ANDROID
djsollen@google.comcd9d69b2011-03-14 20:30:14 +0000652uint32_t SkFontHost::GetUnitsPerEm(SkFontID fontID) {
653 SkAutoMutexAcquire ac(gFTMutex);
654 SkFaceRec *rec = ref_ft_face(fontID);
655 uint16_t unitsPerEm = 0;
656
657 if (rec != NULL && rec->fFace != NULL) {
658 unitsPerEm = rec->fFace->units_per_EM;
659 unref_ft_face(rec->fFace);
660 }
661
662 return (uint32_t)unitsPerEm;
663}
664#endif
665
reed@android.com8a1c16f2008-12-17 15:59:43 +0000666SkScalerContext_FreeType::SkScalerContext_FreeType(const SkDescriptor* desc)
george@mozilla.comc59b5da2012-08-23 00:39:08 +0000667 : SkScalerContext_FreeType_Base(desc) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000668 SkAutoMutexAcquire ac(gFTMutex);
669
reed@android.com8a1c16f2008-12-17 15:59:43 +0000670 if (gFTCount == 0) {
reed@android.com659aaf92009-07-23 15:20:21 +0000671 if (!InitFreetype()) {
672 sk_throw();
673 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000674 }
675 ++gFTCount;
676
677 // load the font file
reed@android.com62900b42009-02-11 15:07:19 +0000678 fFTSize = NULL;
679 fFace = NULL;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000680 fFaceRec = ref_ft_face(fRec.fFontID);
reed@android.com62900b42009-02-11 15:07:19 +0000681 if (NULL == fFaceRec) {
682 return;
683 }
684 fFace = fFaceRec->fFace;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000685
686 // compute our factors from the record
687
688 SkMatrix m;
689
690 fRec.getSingleMatrix(&m);
691
692#ifdef DUMP_STRIKE_CREATION
693 SkString keyString;
694 SkFontHost::GetDescriptorKeyString(desc, &keyString);
695 printf("========== strike [%g %g %g] [%g %g %g %g] hints %d format %d %s\n", SkScalarToFloat(fRec.fTextSize),
696 SkScalarToFloat(fRec.fPreScaleX), SkScalarToFloat(fRec.fPreSkewX),
697 SkScalarToFloat(fRec.fPost2x2[0][0]), SkScalarToFloat(fRec.fPost2x2[0][1]),
698 SkScalarToFloat(fRec.fPost2x2[1][0]), SkScalarToFloat(fRec.fPost2x2[1][1]),
agl@chromium.org309485b2009-07-21 17:41:32 +0000699 fRec.getHinting(), fRec.fMaskFormat, keyString.c_str());
reed@android.com8a1c16f2008-12-17 15:59:43 +0000700#endif
701
702 // now compute our scale factors
703 SkScalar sx = m.getScaleX();
704 SkScalar sy = m.getScaleY();
705
706 if (m.getSkewX() || m.getSkewY() || sx < 0 || sy < 0) {
707 // sort of give up on hinting
708 sx = SkMaxScalar(SkScalarAbs(sx), SkScalarAbs(m.getSkewX()));
709 sy = SkMaxScalar(SkScalarAbs(m.getSkewY()), SkScalarAbs(sy));
710 sx = sy = SkScalarAve(sx, sy);
711
712 SkScalar inv = SkScalarInvert(sx);
713
714 // flip the skew elements to go from our Y-down system to FreeType's
715 fMatrix22.xx = SkScalarToFixed(SkScalarMul(m.getScaleX(), inv));
716 fMatrix22.xy = -SkScalarToFixed(SkScalarMul(m.getSkewX(), inv));
717 fMatrix22.yx = -SkScalarToFixed(SkScalarMul(m.getSkewY(), inv));
718 fMatrix22.yy = SkScalarToFixed(SkScalarMul(m.getScaleY(), inv));
719 } else {
720 fMatrix22.xx = fMatrix22.yy = SK_Fixed1;
721 fMatrix22.xy = fMatrix22.yx = 0;
722 }
723
724 fScaleX = SkScalarToFixed(sx);
725 fScaleY = SkScalarToFixed(sy);
726
reed@google.coma1bfa212012-03-08 21:57:12 +0000727 fLCDIsVert = SkToBool(fRec.fFlags & SkScalerContext::kLCD_Vertical_Flag);
728
reed@android.com8a1c16f2008-12-17 15:59:43 +0000729 // compute the flags we send to Load_Glyph
730 {
reed@android.come4d0bc02009-07-24 19:53:20 +0000731 FT_Int32 loadFlags = FT_LOAD_DEFAULT;
bungeman@google.com34f10262012-03-23 18:11:47 +0000732 bool linearMetrics = SkToBool(fRec.fFlags & SkScalerContext::kSubpixelPositioning_Flag);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000733
agl@chromium.org70a303f2010-05-10 14:15:50 +0000734 if (SkMask::kBW_Format == fRec.fMaskFormat) {
735 // See http://code.google.com/p/chromium/issues/detail?id=43252#c24
736 loadFlags = FT_LOAD_TARGET_MONO;
reed@google.comeffc5012011-06-27 16:44:46 +0000737 if (fRec.getHinting() == SkPaint::kNo_Hinting) {
agl@chromium.org70a303f2010-05-10 14:15:50 +0000738 loadFlags = FT_LOAD_NO_HINTING;
reed@google.combdc99882011-11-21 14:36:57 +0000739 linearMetrics = true;
reed@google.comeffc5012011-06-27 16:44:46 +0000740 }
agl@chromium.org70a303f2010-05-10 14:15:50 +0000741 } else {
742 switch (fRec.getHinting()) {
743 case SkPaint::kNo_Hinting:
744 loadFlags = FT_LOAD_NO_HINTING;
reed@google.combdc99882011-11-21 14:36:57 +0000745 linearMetrics = true;
agl@chromium.org70a303f2010-05-10 14:15:50 +0000746 break;
747 case SkPaint::kSlight_Hinting:
748 loadFlags = FT_LOAD_TARGET_LIGHT; // This implies FORCE_AUTOHINT
749 break;
750 case SkPaint::kNormal_Hinting:
agl@chromium.orga2c71cb2010-06-17 20:49:17 +0000751 if (fRec.fFlags & SkScalerContext::kAutohinting_Flag)
752 loadFlags = FT_LOAD_FORCE_AUTOHINT;
753 else
754 loadFlags = FT_LOAD_NO_AUTOHINT;
agl@chromium.org70a303f2010-05-10 14:15:50 +0000755 break;
756 case SkPaint::kFull_Hinting:
agl@chromium.orga2c71cb2010-06-17 20:49:17 +0000757 if (fRec.fFlags & SkScalerContext::kAutohinting_Flag) {
758 loadFlags = FT_LOAD_FORCE_AUTOHINT;
759 break;
760 }
agl@chromium.org70a303f2010-05-10 14:15:50 +0000761 loadFlags = FT_LOAD_TARGET_NORMAL;
reed@google.comeffc5012011-06-27 16:44:46 +0000762 if (isLCD(fRec)) {
reed@google.coma1bfa212012-03-08 21:57:12 +0000763 if (fLCDIsVert) {
reed@google.comeffc5012011-06-27 16:44:46 +0000764 loadFlags = FT_LOAD_TARGET_LCD_V;
765 } else {
766 loadFlags = FT_LOAD_TARGET_LCD;
767 }
reed@google.comea2333d2011-03-14 16:44:56 +0000768 }
agl@chromium.org70a303f2010-05-10 14:15:50 +0000769 break;
770 default:
771 SkDebugf("---------- UNKNOWN hinting %d\n", fRec.getHinting());
772 break;
773 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000774 }
775
reed@google.comeffc5012011-06-27 16:44:46 +0000776 if ((fRec.fFlags & SkScalerContext::kEmbeddedBitmapText_Flag) == 0) {
agl@chromium.orge0d08992009-08-07 19:19:23 +0000777 loadFlags |= FT_LOAD_NO_BITMAP;
reed@google.comeffc5012011-06-27 16:44:46 +0000778 }
agl@chromium.orge0d08992009-08-07 19:19:23 +0000779
reed@google.com96a9f7912011-05-06 11:49:30 +0000780 // Always using FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH to get correct
781 // advances, as fontconfig and cairo do.
782 // See http://code.google.com/p/skia/issues/detail?id=222.
783 loadFlags |= FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH;
784
reed@android.come4d0bc02009-07-24 19:53:20 +0000785 fLoadGlyphFlags = loadFlags;
reed@google.combdc99882011-11-21 14:36:57 +0000786 fDoLinearMetrics = linearMetrics;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000787 }
788
789 // now create the FT_Size
790
791 {
792 FT_Error err;
793
794 err = FT_New_Size(fFace, &fFTSize);
795 if (err != 0) {
796 SkDEBUGF(("SkScalerContext_FreeType::FT_New_Size(%x): FT_Set_Char_Size(0x%x, 0x%x) returned 0x%x\n",
797 fFaceRec->fFontID, fScaleX, fScaleY, err));
798 fFace = NULL;
799 return;
800 }
801
802 err = FT_Activate_Size(fFTSize);
803 if (err != 0) {
804 SkDEBUGF(("SkScalerContext_FreeType::FT_Activate_Size(%x, 0x%x, 0x%x) returned 0x%x\n",
805 fFaceRec->fFontID, fScaleX, fScaleY, err));
806 fFTSize = NULL;
807 }
808
809 err = FT_Set_Char_Size( fFace,
810 SkFixedToFDot6(fScaleX), SkFixedToFDot6(fScaleY),
811 72, 72);
812 if (err != 0) {
813 SkDEBUGF(("SkScalerContext_FreeType::FT_Set_Char_Size(%x, 0x%x, 0x%x) returned 0x%x\n",
814 fFaceRec->fFontID, fScaleX, fScaleY, err));
815 fFace = NULL;
816 return;
817 }
818
819 FT_Set_Transform( fFace, &fMatrix22, NULL);
820 }
821}
822
823SkScalerContext_FreeType::~SkScalerContext_FreeType() {
824 if (fFTSize != NULL) {
825 FT_Done_Size(fFTSize);
826 }
827
828 SkAutoMutexAcquire ac(gFTMutex);
829
830 if (fFace != NULL) {
831 unref_ft_face(fFace);
832 }
833 if (--gFTCount == 0) {
834// SkDEBUGF(("FT_Done_FreeType\n"));
835 FT_Done_FreeType(gFTLibrary);
836 SkDEBUGCODE(gFTLibrary = NULL;)
837 }
838}
839
840/* We call this before each use of the fFace, since we may be sharing
841 this face with other context (at different sizes).
842*/
843FT_Error SkScalerContext_FreeType::setupSize() {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000844 FT_Error err = FT_Activate_Size(fFTSize);
845
846 if (err != 0) {
847 SkDEBUGF(("SkScalerContext_FreeType::FT_Activate_Size(%x, 0x%x, 0x%x) returned 0x%x\n",
848 fFaceRec->fFontID, fScaleX, fScaleY, err));
849 fFTSize = NULL;
850 } else {
851 // seems we need to reset this every time (not sure why, but without it
852 // I get random italics from some other fFTSize)
853 FT_Set_Transform( fFace, &fMatrix22, NULL);
854 }
855 return err;
856}
857
ctguil@chromium.org0bc7bf52011-03-04 19:04:57 +0000858unsigned SkScalerContext_FreeType::generateGlyphCount() {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000859 return fFace->num_glyphs;
860}
861
862uint16_t SkScalerContext_FreeType::generateCharToGlyph(SkUnichar uni) {
863 return SkToU16(FT_Get_Char_Index( fFace, uni ));
864}
865
reed@android.com9d3a9852010-01-08 14:07:42 +0000866SkUnichar SkScalerContext_FreeType::generateGlyphToChar(uint16_t glyph) {
867 // iterate through each cmap entry, looking for matching glyph indices
868 FT_UInt glyphIndex;
869 SkUnichar charCode = FT_Get_First_Char( fFace, &glyphIndex );
870
871 while (glyphIndex != 0) {
872 if (glyphIndex == glyph) {
873 return charCode;
874 }
875 charCode = FT_Get_Next_Char( fFace, charCode, &glyphIndex );
876 }
877
878 return 0;
879}
880
reed@android.com8a1c16f2008-12-17 15:59:43 +0000881void SkScalerContext_FreeType::generateAdvance(SkGlyph* glyph) {
882#ifdef FT_ADVANCES_H
883 /* unhinted and light hinted text have linearly scaled advances
884 * which are very cheap to compute with some font formats...
885 */
reed@google.combdc99882011-11-21 14:36:57 +0000886 if (fDoLinearMetrics) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000887 SkAutoMutexAcquire ac(gFTMutex);
888
889 if (this->setupSize()) {
reed@android.com62900b42009-02-11 15:07:19 +0000890 glyph->zeroMetrics();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000891 return;
892 }
893
894 FT_Error error;
895 FT_Fixed advance;
896
897 error = FT_Get_Advance( fFace, glyph->getGlyphID(fBaseGlyphCount),
898 fLoadGlyphFlags | FT_ADVANCE_FLAG_FAST_ONLY,
899 &advance );
900 if (0 == error) {
901 glyph->fRsbDelta = 0;
902 glyph->fLsbDelta = 0;
reed@google.comd074c372012-07-18 13:45:58 +0000903 glyph->fAdvanceX = SkFixedMul(fMatrix22.xx, advance);
904 glyph->fAdvanceY = - SkFixedMul(fMatrix22.yx, advance);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000905 return;
906 }
907 }
908#endif /* FT_ADVANCES_H */
909 /* otherwise, we need to load/hint the glyph, which is slower */
910 this->generateMetrics(glyph);
911 return;
912}
913
djsollen@google.comd8b599c2012-03-19 19:44:19 +0000914void SkScalerContext_FreeType::getBBoxForCurrentGlyph(SkGlyph* glyph,
915 FT_BBox* bbox,
916 bool snapToPixelBoundary) {
917
918 FT_Outline_Get_CBox(&fFace->glyph->outline, bbox);
919
920 if (fRec.fFlags & SkScalerContext::kSubpixelPositioning_Flag) {
george@mozilla.comc59b5da2012-08-23 00:39:08 +0000921 int dx = SkFixedToFDot6(glyph->getSubXFixed());
922 int dy = SkFixedToFDot6(glyph->getSubYFixed());
djsollen@google.comd8b599c2012-03-19 19:44:19 +0000923 // negate dy since freetype-y-goes-up and skia-y-goes-down
924 bbox->xMin += dx;
925 bbox->yMin -= dy;
926 bbox->xMax += dx;
927 bbox->yMax -= dy;
928 }
929
930 // outset the box to integral boundaries
931 if (snapToPixelBoundary) {
932 bbox->xMin &= ~63;
933 bbox->yMin &= ~63;
934 bbox->xMax = (bbox->xMax + 63) & ~63;
935 bbox->yMax = (bbox->yMax + 63) & ~63;
936 }
937}
938
939void SkScalerContext_FreeType::updateGlyphIfLCD(SkGlyph* glyph) {
940 if (isLCD(fRec)) {
941 if (fLCDIsVert) {
942 glyph->fHeight += gLCDExtra;
943 glyph->fTop -= gLCDExtra >> 1;
944 } else {
945 glyph->fWidth += gLCDExtra;
946 glyph->fLeft -= gLCDExtra >> 1;
947 }
948 }
949}
bungeman@google.com875eb982012-04-12 15:53:23 +0000950void SkScalerContext_FreeType::updateGlyphPosIfLCD(SkGlyph* glyph) {
951 if (isLCD(fRec)) {
952 if (fLCDIsVert) {
953 glyph->fTop -= gLCDExtra >> 1;
954 } else {
955 glyph->fLeft -= gLCDExtra >> 1;
956 }
957 }
958}
djsollen@google.comd8b599c2012-03-19 19:44:19 +0000959
reed@android.com8a1c16f2008-12-17 15:59:43 +0000960void SkScalerContext_FreeType::generateMetrics(SkGlyph* glyph) {
961 SkAutoMutexAcquire ac(gFTMutex);
962
963 glyph->fRsbDelta = 0;
964 glyph->fLsbDelta = 0;
965
966 FT_Error err;
967
968 if (this->setupSize()) {
969 goto ERROR;
970 }
971
972 err = FT_Load_Glyph( fFace, glyph->getGlyphID(fBaseGlyphCount), fLoadGlyphFlags );
973 if (err != 0) {
974 SkDEBUGF(("SkScalerContext_FreeType::generateMetrics(%x): FT_Load_Glyph(glyph:%d flags:%d) returned 0x%x\n",
975 fFaceRec->fFontID, glyph->getGlyphID(fBaseGlyphCount), fLoadGlyphFlags, err));
976 ERROR:
reed@android.com62900b42009-02-11 15:07:19 +0000977 glyph->zeroMetrics();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000978 return;
979 }
980
vandebo@chromium.org6390c722012-03-28 21:03:22 +0000981 SkFixed vLeft = 0, vTop = 0;
djsollen@google.comd8b599c2012-03-19 19:44:19 +0000982
reed@android.com8a1c16f2008-12-17 15:59:43 +0000983 switch ( fFace->glyph->format ) {
thakis@chromium.org598b8592011-05-24 05:42:55 +0000984 case FT_GLYPH_FORMAT_OUTLINE: {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000985 FT_BBox bbox;
986
bungeman@google.com0f0c2882011-11-04 15:47:41 +0000987 if (0 == fFace->glyph->outline.n_contours) {
988 glyph->fWidth = 0;
989 glyph->fHeight = 0;
990 glyph->fTop = 0;
991 glyph->fLeft = 0;
992 break;
993 }
994
senorblanco@chromium.org4526a842010-02-05 23:08:20 +0000995 if (fRec.fFlags & kEmbolden_Flag) {
george@mozilla.comc59b5da2012-08-23 00:39:08 +0000996 emboldenOutline(fFace, &fFace->glyph->outline);
senorblanco@chromium.org4526a842010-02-05 23:08:20 +0000997 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000998
djsollen@google.comd8b599c2012-03-19 19:44:19 +0000999 getBBoxForCurrentGlyph(glyph, &bbox, true);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001000
1001 glyph->fWidth = SkToU16((bbox.xMax - bbox.xMin) >> 6);
1002 glyph->fHeight = SkToU16((bbox.yMax - bbox.yMin) >> 6);
1003 glyph->fTop = -SkToS16(bbox.yMax >> 6);
1004 glyph->fLeft = SkToS16(bbox.xMin >> 6);
reed@google.coma1c32562012-03-01 19:38:23 +00001005
djsollen@google.comd8b599c2012-03-19 19:44:19 +00001006 if ((fRec.fFlags & SkScalerContext::kVertical_Flag)) {
george@mozilla.comc59b5da2012-08-23 00:39:08 +00001007 vLeft = SkFDot6ToFixed(bbox.xMin);
1008 vTop = SkFDot6ToFixed(bbox.yMax);
reed@google.coma1c32562012-03-01 19:38:23 +00001009 }
djsollen@google.comd8b599c2012-03-19 19:44:19 +00001010
1011 updateGlyphIfLCD(glyph);
1012
reed@android.com8a1c16f2008-12-17 15:59:43 +00001013 break;
thakis@chromium.org598b8592011-05-24 05:42:55 +00001014 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001015
1016 case FT_GLYPH_FORMAT_BITMAP:
agl@chromium.orge76073b2010-06-04 20:31:17 +00001017 if (fRec.fFlags & kEmbolden_Flag) {
1018 FT_GlyphSlot_Own_Bitmap(fFace->glyph);
1019 FT_Bitmap_Embolden(gFTLibrary, &fFace->glyph->bitmap, kBitmapEmboldenStrength, 0);
1020 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001021 glyph->fWidth = SkToU16(fFace->glyph->bitmap.width);
1022 glyph->fHeight = SkToU16(fFace->glyph->bitmap.rows);
1023 glyph->fTop = -SkToS16(fFace->glyph->bitmap_top);
1024 glyph->fLeft = SkToS16(fFace->glyph->bitmap_left);
1025 break;
1026
1027 default:
tomhudson@google.com0c00f212011-12-28 14:59:50 +00001028 SkDEBUGFAIL("unknown glyph format");
reed@android.com8a1c16f2008-12-17 15:59:43 +00001029 goto ERROR;
1030 }
1031
bungeman@google.com34f10262012-03-23 18:11:47 +00001032 if (fDoLinearMetrics) {
1033 glyph->fAdvanceX = SkFixedMul(fMatrix22.xx, fFace->glyph->linearHoriAdvance);
1034 glyph->fAdvanceY = -SkFixedMul(fMatrix22.yx, fFace->glyph->linearHoriAdvance);
1035 } else {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001036 glyph->fAdvanceX = SkFDot6ToFixed(fFace->glyph->advance.x);
1037 glyph->fAdvanceY = -SkFDot6ToFixed(fFace->glyph->advance.y);
bungeman@google.com34f10262012-03-23 18:11:47 +00001038
reed@android.com8a1c16f2008-12-17 15:59:43 +00001039 if (fRec.fFlags & kDevKernText_Flag) {
1040 glyph->fRsbDelta = SkToS8(fFace->glyph->rsb_delta);
1041 glyph->fLsbDelta = SkToS8(fFace->glyph->lsb_delta);
1042 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001043 }
1044
djsollen@google.comd8b599c2012-03-19 19:44:19 +00001045 if ((fRec.fFlags & SkScalerContext::kVertical_Flag)
1046 && fFace->glyph->format == FT_GLYPH_FORMAT_OUTLINE) {
1047
1048 //TODO: do we need to specially handle SubpixelPositioning and Kerning?
1049
1050 FT_Matrix identityMatrix;
1051 identityMatrix.xx = identityMatrix.yy = SK_Fixed1;
1052 identityMatrix.xy = identityMatrix.yx = 0;
1053
1054 // if the matrix is not the identity matrix then we need to re-load the
1055 // glyph with the identity matrix to get the necessary bounding box
1056 if (memcmp(&fMatrix22, &identityMatrix, sizeof(FT_Matrix)) != 0) {
1057
1058 FT_Set_Transform(fFace, &identityMatrix, NULL);
1059
1060 err = FT_Load_Glyph( fFace, glyph->getGlyphID(fBaseGlyphCount), fLoadGlyphFlags );
1061 if (err != 0) {
1062 SkDEBUGF(("SkScalerContext_FreeType::generateMetrics(%x): FT_Load_Glyph(glyph:%d flags:%d) returned 0x%x\n",
1063 fFaceRec->fFontID, glyph->getGlyphID(fBaseGlyphCount), fLoadGlyphFlags, err));
1064 goto ERROR;
1065 }
1066
1067 if (fRec.fFlags & kEmbolden_Flag) {
george@mozilla.comc59b5da2012-08-23 00:39:08 +00001068 emboldenOutline(fFace, &fFace->glyph->outline);
djsollen@google.comd8b599c2012-03-19 19:44:19 +00001069 }
1070 }
1071
1072 // bounding box of the unskewed and unscaled glyph
vandebo@chromium.org6390c722012-03-28 21:03:22 +00001073 FT_BBox bbox = {0, 0, 0, 0}; // Suppress Coverity warning.
djsollen@google.comd8b599c2012-03-19 19:44:19 +00001074 getBBoxForCurrentGlyph(glyph, &bbox);
1075
1076 // compute the vertical gap above and below the glyph if the glyph were
1077 // centered within the linearVertAdvance
george@mozilla.comc59b5da2012-08-23 00:39:08 +00001078 SkFixed vGap = (fFace->glyph->linearVertAdvance - SkFDot6ToFixed(bbox.yMax - bbox.yMin)) / 2;
djsollen@google.comd8b599c2012-03-19 19:44:19 +00001079
1080 // the origin point of the glyph when rendered vertically
1081 FT_Vector vOrigin;
1082 vOrigin.x = fFace->glyph->linearHoriAdvance / 2;
george@mozilla.comc59b5da2012-08-23 00:39:08 +00001083 vOrigin.y = vGap + SkFDot6ToFixed(bbox.yMax);
djsollen@google.comd8b599c2012-03-19 19:44:19 +00001084
1085 // transform the vertical origin based on the matrix of the actual glyph
1086 FT_Vector_Transform(&vOrigin, &fMatrix22);
1087
1088 // compute a new offset vector for the glyph by subtracting the vertical
1089 // origin from the original horizontal offset vector
1090 glyph->fLeft = SkFixedRoundToInt(vLeft - vOrigin.x);
1091 glyph->fTop = -SkFixedRoundToInt(vTop - vOrigin.y);
1092
bungeman@google.com875eb982012-04-12 15:53:23 +00001093 updateGlyphPosIfLCD(glyph);
djsollen@google.comd8b599c2012-03-19 19:44:19 +00001094
1095 // use the vertical advance values computed by freetype
1096 glyph->fAdvanceX = -SkFixedMul(fMatrix22.xy, fFace->glyph->linearVertAdvance);
1097 glyph->fAdvanceY = SkFixedMul(fMatrix22.yy, fFace->glyph->linearVertAdvance);
1098 }
1099
1100
reed@android.com8a1c16f2008-12-17 15:59:43 +00001101#ifdef ENABLE_GLYPH_SPEW
1102 SkDEBUGF(("FT_Set_Char_Size(this:%p sx:%x sy:%x ", this, fScaleX, fScaleY));
1103 SkDEBUGF(("Metrics(glyph:%d flags:0x%x) w:%d\n", glyph->getGlyphID(fBaseGlyphCount), fLoadGlyphFlags, glyph->fWidth));
1104#endif
1105}
1106
reed@google.comea2333d2011-03-14 16:44:56 +00001107
bungeman@google.com97efada2012-07-30 20:40:50 +00001108void SkScalerContext_FreeType::generateImage(const SkGlyph& glyph, SkMaskGamma::PreBlend* maskPreBlend) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001109 SkAutoMutexAcquire ac(gFTMutex);
1110
1111 FT_Error err;
1112
1113 if (this->setupSize()) {
1114 goto ERROR;
1115 }
1116
1117 err = FT_Load_Glyph( fFace, glyph.getGlyphID(fBaseGlyphCount), fLoadGlyphFlags);
1118 if (err != 0) {
1119 SkDEBUGF(("SkScalerContext_FreeType::generateImage: FT_Load_Glyph(glyph:%d width:%d height:%d rb:%d flags:%d) returned 0x%x\n",
1120 glyph.getGlyphID(fBaseGlyphCount), glyph.fWidth, glyph.fHeight, glyph.rowBytes(), fLoadGlyphFlags, err));
1121 ERROR:
1122 memset(glyph.fImage, 0, glyph.rowBytes() * glyph.fHeight);
1123 return;
1124 }
1125
george@mozilla.comc59b5da2012-08-23 00:39:08 +00001126 generateGlyphImage(fFace, glyph, maskPreBlend);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001127}
1128
reed@android.com8a1c16f2008-12-17 15:59:43 +00001129
1130void SkScalerContext_FreeType::generatePath(const SkGlyph& glyph,
1131 SkPath* path) {
1132 SkAutoMutexAcquire ac(gFTMutex);
1133
1134 SkASSERT(&glyph && path);
1135
1136 if (this->setupSize()) {
1137 path->reset();
1138 return;
1139 }
1140
1141 uint32_t flags = fLoadGlyphFlags;
1142 flags |= FT_LOAD_NO_BITMAP; // ignore embedded bitmaps so we're sure to get the outline
1143 flags &= ~FT_LOAD_RENDER; // don't scan convert (we just want the outline)
1144
1145 FT_Error err = FT_Load_Glyph( fFace, glyph.getGlyphID(fBaseGlyphCount), flags);
1146
1147 if (err != 0) {
1148 SkDEBUGF(("SkScalerContext_FreeType::generatePath: FT_Load_Glyph(glyph:%d flags:%d) returned 0x%x\n",
1149 glyph.getGlyphID(fBaseGlyphCount), flags, err));
1150 path->reset();
1151 return;
1152 }
1153
george@mozilla.comc59b5da2012-08-23 00:39:08 +00001154 generateGlyphPath(fFace, glyph, path);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001155}
1156
1157void SkScalerContext_FreeType::generateFontMetrics(SkPaint::FontMetrics* mx,
1158 SkPaint::FontMetrics* my) {
1159 if (NULL == mx && NULL == my) {
1160 return;
1161 }
1162
1163 SkAutoMutexAcquire ac(gFTMutex);
1164
1165 if (this->setupSize()) {
reed@android.coma8a8b8b2009-05-04 15:00:11 +00001166 ERROR:
reed@android.com8a1c16f2008-12-17 15:59:43 +00001167 if (mx) {
reed@android.com4516f472009-06-29 16:25:36 +00001168 sk_bzero(mx, sizeof(SkPaint::FontMetrics));
reed@android.com8a1c16f2008-12-17 15:59:43 +00001169 }
1170 if (my) {
reed@android.com4516f472009-06-29 16:25:36 +00001171 sk_bzero(my, sizeof(SkPaint::FontMetrics));
reed@android.com8a1c16f2008-12-17 15:59:43 +00001172 }
1173 return;
1174 }
1175
reed@android.coma8a8b8b2009-05-04 15:00:11 +00001176 FT_Face face = fFace;
1177 int upem = face->units_per_EM;
1178 if (upem <= 0) {
1179 goto ERROR;
1180 }
1181
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001182 SkPoint pts[6];
1183 SkFixed ys[6];
reed@android.com8a1c16f2008-12-17 15:59:43 +00001184 SkFixed scaleY = fScaleY;
1185 SkFixed mxy = fMatrix22.xy;
1186 SkFixed myy = fMatrix22.yy;
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001187 SkScalar xmin = SkIntToScalar(face->bbox.xMin) / upem;
1188 SkScalar xmax = SkIntToScalar(face->bbox.xMax) / upem;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001189
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001190 int leading = face->height - (face->ascender + -face->descender);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001191 if (leading < 0) {
1192 leading = 0;
1193 }
1194
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001195 // Try to get the OS/2 table from the font. This contains the specific
1196 // average font width metrics which Windows uses.
1197 TT_OS2* os2 = (TT_OS2*) FT_Get_Sfnt_Table(face, ft_sfnt_os2);
1198
reed@android.com8a1c16f2008-12-17 15:59:43 +00001199 ys[0] = -face->bbox.yMax;
1200 ys[1] = -face->ascender;
1201 ys[2] = -face->descender;
1202 ys[3] = -face->bbox.yMin;
1203 ys[4] = leading;
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001204 ys[5] = os2 ? os2->xAvgCharWidth : 0;
1205
1206 SkScalar x_height;
1207 if (os2 && os2->sxHeight) {
1208 x_height = SkFixedToScalar(SkMulDiv(fScaleX, os2->sxHeight, upem));
1209 } else {
1210 const FT_UInt x_glyph = FT_Get_Char_Index(fFace, 'x');
1211 if (x_glyph) {
1212 FT_BBox bbox;
1213 FT_Load_Glyph(fFace, x_glyph, fLoadGlyphFlags);
senorblanco@chromium.org4526a842010-02-05 23:08:20 +00001214 if (fRec.fFlags & kEmbolden_Flag) {
george@mozilla.comc59b5da2012-08-23 00:39:08 +00001215 emboldenOutline(fFace, &fFace->glyph->outline);
senorblanco@chromium.org4526a842010-02-05 23:08:20 +00001216 }
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001217 FT_Outline_Get_CBox(&fFace->glyph->outline, &bbox);
epoger@google.comb90113d2012-01-18 19:20:39 +00001218 x_height = SkFixedToScalar(SkFDot6ToFixed(bbox.yMax));
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001219 } else {
1220 x_height = 0;
1221 }
1222 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001223
1224 // convert upem-y values into scalar points
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001225 for (int i = 0; i < 6; i++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001226 SkFixed y = SkMulDiv(scaleY, ys[i], upem);
1227 SkFixed x = SkFixedMul(mxy, y);
1228 y = SkFixedMul(myy, y);
1229 pts[i].set(SkFixedToScalar(x), SkFixedToScalar(y));
1230 }
1231
1232 if (mx) {
1233 mx->fTop = pts[0].fX;
1234 mx->fAscent = pts[1].fX;
1235 mx->fDescent = pts[2].fX;
1236 mx->fBottom = pts[3].fX;
1237 mx->fLeading = pts[4].fX;
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001238 mx->fAvgCharWidth = pts[5].fX;
1239 mx->fXMin = xmin;
1240 mx->fXMax = xmax;
1241 mx->fXHeight = x_height;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001242 }
1243 if (my) {
1244 my->fTop = pts[0].fY;
1245 my->fAscent = pts[1].fY;
1246 my->fDescent = pts[2].fY;
1247 my->fBottom = pts[3].fY;
1248 my->fLeading = pts[4].fY;
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001249 my->fAvgCharWidth = pts[5].fY;
1250 my->fXMin = xmin;
1251 my->fXMax = xmax;
1252 my->fXHeight = x_height;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001253 }
1254}
1255
1256////////////////////////////////////////////////////////////////////////
1257////////////////////////////////////////////////////////////////////////
1258
1259SkScalerContext* SkFontHost::CreateScalerContext(const SkDescriptor* desc) {
reed@android.com62900b42009-02-11 15:07:19 +00001260 SkScalerContext_FreeType* c = SkNEW_ARGS(SkScalerContext_FreeType, (desc));
1261 if (!c->success()) {
1262 SkDELETE(c);
1263 c = NULL;
1264 }
1265 return c;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001266}
1267
1268///////////////////////////////////////////////////////////////////////////////
1269
1270/* Export this so that other parts of our FonttHost port can make use of our
1271 ability to extract the name+style from a stream, using FreeType's api.
1272*/
djsollen@google.com4dc686d2012-02-15 21:03:45 +00001273bool find_name_and_attributes(SkStream* stream, SkString* name,
1274 SkTypeface::Style* style, bool* isFixedWidth) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001275 FT_Library library;
reed@android.combfbd4ff2009-07-23 17:44:41 +00001276 if (FT_Init_FreeType(&library)) {
djsollen@google.com4dc686d2012-02-15 21:03:45 +00001277 return false;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001278 }
1279
1280 FT_Open_Args args;
1281 memset(&args, 0, sizeof(args));
1282
1283 const void* memoryBase = stream->getMemoryBase();
1284 FT_StreamRec streamRec;
1285
1286 if (NULL != memoryBase) {
1287 args.flags = FT_OPEN_MEMORY;
1288 args.memory_base = (const FT_Byte*)memoryBase;
1289 args.memory_size = stream->getLength();
1290 } else {
1291 memset(&streamRec, 0, sizeof(streamRec));
1292 streamRec.size = stream->read(NULL, 0);
1293 streamRec.descriptor.pointer = stream;
1294 streamRec.read = sk_stream_read;
1295 streamRec.close = sk_stream_close;
1296
1297 args.flags = FT_OPEN_STREAM;
1298 args.stream = &streamRec;
1299 }
1300
1301 FT_Face face;
1302 if (FT_Open_Face(library, &args, 0, &face)) {
1303 FT_Done_FreeType(library);
djsollen@google.com4dc686d2012-02-15 21:03:45 +00001304 return false;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001305 }
1306
djsollen@google.com4dc686d2012-02-15 21:03:45 +00001307 int tempStyle = SkTypeface::kNormal;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001308 if (face->style_flags & FT_STYLE_FLAG_BOLD) {
djsollen@google.com4dc686d2012-02-15 21:03:45 +00001309 tempStyle |= SkTypeface::kBold;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001310 }
1311 if (face->style_flags & FT_STYLE_FLAG_ITALIC) {
djsollen@google.com4dc686d2012-02-15 21:03:45 +00001312 tempStyle |= SkTypeface::kItalic;
1313 }
1314
1315 if (name) {
1316 name->set(face->family_name);
1317 }
1318 if (style) {
1319 *style = (SkTypeface::Style) tempStyle;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001320 }
reed@google.com5b31b0f2011-02-23 14:41:42 +00001321 if (isFixedWidth) {
1322 *isFixedWidth = FT_IS_FIXED_WIDTH(face);
1323 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001324
1325 FT_Done_Face(face);
1326 FT_Done_FreeType(library);
djsollen@google.com4dc686d2012-02-15 21:03:45 +00001327 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001328}