blob: 3d6b5746164b522bc43a2becc814878b7ec869b2 [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
bungeman@google.comfd668cf2012-08-24 17:46:11 +000027#if defined(SK_CAN_USE_DLOPEN)
28#include <dlfcn.h>
29#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +000030#include <ft2build.h>
31#include FT_FREETYPE_H
32#include FT_OUTLINE_H
33#include FT_SIZES_H
agl@chromium.orgcc3096b2009-04-22 22:09:04 +000034#include FT_TRUETYPE_TABLES_H
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000035#include FT_TYPE1_TABLES_H
agl@chromium.orge76073b2010-06-04 20:31:17 +000036#include FT_BITMAP_H
agl@chromium.org36bb6972010-06-04 20:57:16 +000037// In the past, FT_GlyphSlot_Own_Bitmap was defined in this header file.
38#include FT_SYNTHESIS_H
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000039#include FT_XFREE86_H
epoger@google.com5070d792011-06-29 20:43:14 +000040#ifdef FT_LCD_FILTER_H
agl@chromium.org309485b2009-07-21 17:41:32 +000041#include FT_LCD_FILTER_H
epoger@google.com5070d792011-06-29 20:43:14 +000042#endif
agl@chromium.org309485b2009-07-21 17:41:32 +000043
reed@android.com8a1c16f2008-12-17 15:59:43 +000044#ifdef FT_ADVANCES_H
45#include FT_ADVANCES_H
46#endif
47
agl@chromium.orgcc3096b2009-04-22 22:09:04 +000048#if 0
49// Also include the files by name for build tools which require this.
50#include <freetype/freetype.h>
51#include <freetype/ftoutln.h>
52#include <freetype/ftsizes.h>
53#include <freetype/tttables.h>
54#include <freetype/ftadvanc.h>
agl@chromium.org309485b2009-07-21 17:41:32 +000055#include <freetype/ftlcdfil.h>
agl@chromium.orge76073b2010-06-04 20:31:17 +000056#include <freetype/ftbitmap.h>
agl@chromium.org36bb6972010-06-04 20:57:16 +000057#include <freetype/ftsynth.h>
agl@chromium.orgcc3096b2009-04-22 22:09:04 +000058#endif
59
reed@android.com8a1c16f2008-12-17 15:59:43 +000060//#define ENABLE_GLYPH_SPEW // for tracing calls
61//#define DUMP_STRIKE_CREATION
62
reed@google.com1ac83502012-02-28 17:06:02 +000063//#define SK_GAMMA_APPLY_TO_A8
reed@google.com1ac83502012-02-28 17:06:02 +000064
vandebo@chromium.org6f72d1e2011-02-14 23:19:59 +000065using namespace skia_advanced_typeface_metrics_utils;
66
reed@google.comeffc5012011-06-27 16:44:46 +000067static bool isLCD(const SkScalerContext::Rec& rec) {
68 switch (rec.fMaskFormat) {
69 case SkMask::kLCD16_Format:
70 case SkMask::kLCD32_Format:
71 return true;
72 default:
73 return false;
74 }
75}
76
reed@android.com8a1c16f2008-12-17 15:59:43 +000077//////////////////////////////////////////////////////////////////////////
78
79struct SkFaceRec;
80
digit@google.com1771cbf2012-01-26 21:26:40 +000081SK_DECLARE_STATIC_MUTEX(gFTMutex);
reed@android.com8a1c16f2008-12-17 15:59:43 +000082static int gFTCount;
83static FT_Library gFTLibrary;
84static SkFaceRec* gFaceRecHead;
agl@chromium.orgf18d8762009-07-28 18:38:08 +000085static bool gLCDSupportValid; // true iff |gLCDSupport| has been set.
86static bool gLCDSupport; // true iff LCD is supported by the runtime.
reed@google.coma1c32562012-03-01 19:38:23 +000087static int gLCDExtra; // number of extra pixels for filtering.
reed@android.com8a1c16f2008-12-17 15:59:43 +000088
bungeman@google.comfd668cf2012-08-24 17:46:11 +000089// FT_Library_SetLcdFilterWeights was introduced in FreeType 2.4.0.
90// The following platforms provide FreeType of at least 2.4.0.
91// Ubuntu >= 11.04 (previous deprecated April 2013)
92// Debian >= 6.0 (good)
93// OpenSuse >= 11.4 (previous deprecated January 2012 / Nov 2013 for Evergreen 11.2)
94// Fedora >= 14 (good)
95// Android >= Gingerbread (good)
96typedef FT_Error (*FT_Library_SetLcdFilterWeightsProc)(FT_Library, unsigned char*);
97
reed@android.com8a1c16f2008-12-17 15:59:43 +000098/////////////////////////////////////////////////////////////////////////
99
bungeman@google.comfd668cf2012-08-24 17:46:11 +0000100static bool InitFreetype() {
agl@chromium.org309485b2009-07-21 17:41:32 +0000101 FT_Error err = FT_Init_FreeType(&gFTLibrary);
reed@google.comea2333d2011-03-14 16:44:56 +0000102 if (err) {
agl@chromium.org309485b2009-07-21 17:41:32 +0000103 return false;
reed@google.comea2333d2011-03-14 16:44:56 +0000104 }
agl@chromium.org309485b2009-07-21 17:41:32 +0000105
bungeman@google.comfd668cf2012-08-24 17:46:11 +0000106 // Setup LCD filtering. This reduces color fringes for LCD smoothed glyphs.
epoger@google.comb371ed12011-06-29 21:20:52 +0000107#ifdef FT_LCD_FILTER_H
bungeman@google.comfd668cf2012-08-24 17:46:11 +0000108 //Use light as default, as FT_LCD_FILTER_DEFAULT adds up to 0x110.
109 err = FT_Library_SetLcdFilter(gFTLibrary, FT_LCD_FILTER_LIGHT);
110 if (0 == err) {
111 gLCDSupport = true;
112 gLCDExtra = 2; //Using a filter adds one full pixel to each side.
113
114 static unsigned char gaussianLikeWeights[] = { 0x17, 0x40, 0x52, 0x40, 0x17 };
115 //static unsigned char triangleLikeWeights[] = { 0x1C, 0x39, 0x56, 0x39, 0x1C };
116
117#if defined(SK_FONTHOST_FREETYPE_RUNTIME_VERSION) && \
118 SK_FONTHOST_FREETYPE_RUNTIME_VERSION > 0x020400
119 err = FT_Library_SetLcdFilterWeights(gFTLibrary, gaussianLikeWeights);
120#elif defined(SK_CAN_USE_DLOPEN) && SK_CAN_USE_DLOPEN == 1
121 //The FreeType library is already loaded, so symbols are available in process.
122 void* self = dlopen(NULL, RTLD_LAZY);
123 if (NULL != self) {
124 FT_Library_SetLcdFilterWeightsProc setLcdFilterWeights;
125 //The following cast is non-standard, but safe for POSIX.
126 *reinterpret_cast<void**>(&setLcdFilterWeights) = dlsym(self, "FT_Library_SetLcdFilterWeights");
127 dlclose(self);
128
129 if (NULL != setLcdFilterWeights) {
130 err = setLcdFilterWeights(gFTLibrary, gaussianLikeWeights);
131 }
132 }
133#endif
reed@google.coma1c32562012-03-01 19:38:23 +0000134 }
epoger@google.com5070d792011-06-29 20:43:14 +0000135#else
136 gLCDSupport = false;
137#endif
reed@android.com61608aa2009-07-31 14:52:54 +0000138 gLCDSupportValid = true;
agl@chromium.org309485b2009-07-21 17:41:32 +0000139
140 return true;
141}
142
george@mozilla.comc59b5da2012-08-23 00:39:08 +0000143class SkScalerContext_FreeType : public SkScalerContext_FreeType_Base {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000144public:
145 SkScalerContext_FreeType(const SkDescriptor* desc);
146 virtual ~SkScalerContext_FreeType();
agl@chromium.orgcc3096b2009-04-22 22:09:04 +0000147
reed@android.com62900b42009-02-11 15:07:19 +0000148 bool success() const {
reed@android.coma0f5d152009-06-22 17:38:10 +0000149 return fFaceRec != NULL &&
150 fFTSize != NULL &&
151 fFace != NULL;
reed@android.com62900b42009-02-11 15:07:19 +0000152 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000153
154protected:
ctguil@chromium.org0bc7bf52011-03-04 19:04:57 +0000155 virtual unsigned generateGlyphCount();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000156 virtual uint16_t generateCharToGlyph(SkUnichar uni);
157 virtual void generateAdvance(SkGlyph* glyph);
158 virtual void generateMetrics(SkGlyph* glyph);
bungeman@google.com97efada2012-07-30 20:40:50 +0000159 virtual void generateImage(const SkGlyph& glyph, SkMaskGamma::PreBlend* maskPreBlend);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000160 virtual void generatePath(const SkGlyph& glyph, SkPath* path);
161 virtual void generateFontMetrics(SkPaint::FontMetrics* mx,
162 SkPaint::FontMetrics* my);
reed@android.com9d3a9852010-01-08 14:07:42 +0000163 virtual SkUnichar generateGlyphToChar(uint16_t glyph);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000164
165private:
166 SkFaceRec* fFaceRec;
167 FT_Face fFace; // reference to shared face in gFaceRecHead
168 FT_Size fFTSize; // our own copy
169 SkFixed fScaleX, fScaleY;
170 FT_Matrix fMatrix22;
171 uint32_t fLoadGlyphFlags;
reed@google.combdc99882011-11-21 14:36:57 +0000172 bool fDoLinearMetrics;
reed@google.coma1bfa212012-03-08 21:57:12 +0000173 bool fLCDIsVert;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000174
175 FT_Error setupSize();
djsollen@google.comd8b599c2012-03-19 19:44:19 +0000176 void getBBoxForCurrentGlyph(SkGlyph* glyph, FT_BBox* bbox,
177 bool snapToPixelBoundary = false);
178 void updateGlyphIfLCD(SkGlyph* glyph);
bungeman@google.com875eb982012-04-12 15:53:23 +0000179 void updateGlyphPosIfLCD(SkGlyph* glyph);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000180};
181
182///////////////////////////////////////////////////////////////////////////
183///////////////////////////////////////////////////////////////////////////
184
185#include "SkStream.h"
186
187struct SkFaceRec {
188 SkFaceRec* fNext;
189 FT_Face fFace;
190 FT_StreamRec fFTStream;
191 SkStream* fSkStream;
192 uint32_t fRefCnt;
193 uint32_t fFontID;
194
reed@android.comb1d9d2e2009-03-04 17:37:51 +0000195 // assumes ownership of the stream, will call unref() when its done
reed@android.com8a1c16f2008-12-17 15:59:43 +0000196 SkFaceRec(SkStream* strm, uint32_t fontID);
197 ~SkFaceRec() {
reed@android.comb1d9d2e2009-03-04 17:37:51 +0000198 fSkStream->unref();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000199 }
200};
201
202extern "C" {
203 static unsigned long sk_stream_read(FT_Stream stream,
204 unsigned long offset,
205 unsigned char* buffer,
206 unsigned long count ) {
207 SkStream* str = (SkStream*)stream->descriptor.pointer;
208
209 if (count) {
210 if (!str->rewind()) {
211 return 0;
212 } else {
213 unsigned long ret;
214 if (offset) {
215 ret = str->read(NULL, offset);
216 if (ret != offset) {
217 return 0;
218 }
219 }
220 ret = str->read(buffer, count);
221 if (ret != count) {
222 return 0;
223 }
224 count = ret;
225 }
226 }
227 return count;
228 }
229
230 static void sk_stream_close( FT_Stream stream) {}
231}
232
233SkFaceRec::SkFaceRec(SkStream* strm, uint32_t fontID)
vandebo@chromium.org9af25f32012-03-28 21:24:27 +0000234 : fNext(NULL), fSkStream(strm), fRefCnt(1), fFontID(fontID) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000235// SkDEBUGF(("SkFaceRec: opening %s (%p)\n", key.c_str(), strm));
236
reed@android.com4516f472009-06-29 16:25:36 +0000237 sk_bzero(&fFTStream, sizeof(fFTStream));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000238 fFTStream.size = fSkStream->getLength();
239 fFTStream.descriptor.pointer = fSkStream;
240 fFTStream.read = sk_stream_read;
241 fFTStream.close = sk_stream_close;
242}
243
reed@android.com62900b42009-02-11 15:07:19 +0000244// Will return 0 on failure
reed@android.com8a1c16f2008-12-17 15:59:43 +0000245static SkFaceRec* ref_ft_face(uint32_t fontID) {
246 SkFaceRec* rec = gFaceRecHead;
247 while (rec) {
248 if (rec->fFontID == fontID) {
249 SkASSERT(rec->fFace);
250 rec->fRefCnt += 1;
251 return rec;
252 }
253 rec = rec->fNext;
254 }
255
256 SkStream* strm = SkFontHost::OpenStream(fontID);
257 if (NULL == strm) {
258 SkDEBUGF(("SkFontHost::OpenStream failed opening %x\n", fontID));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000259 return 0;
260 }
261
262 // this passes ownership of strm to the rec
263 rec = SkNEW_ARGS(SkFaceRec, (strm, fontID));
264
265 FT_Open_Args args;
266 memset(&args, 0, sizeof(args));
267 const void* memoryBase = strm->getMemoryBase();
268
269 if (NULL != memoryBase) {
270//printf("mmap(%s)\n", keyString.c_str());
271 args.flags = FT_OPEN_MEMORY;
272 args.memory_base = (const FT_Byte*)memoryBase;
273 args.memory_size = strm->getLength();
274 } else {
275//printf("fopen(%s)\n", keyString.c_str());
276 args.flags = FT_OPEN_STREAM;
277 args.stream = &rec->fFTStream;
278 }
279
agl@chromium.org61a678a2010-08-06 18:08:18 +0000280 int face_index;
281 int length = SkFontHost::GetFileName(fontID, NULL, 0, &face_index);
282 FT_Error err = FT_Open_Face(gFTLibrary, &args, length ? face_index : 0,
283 &rec->fFace);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000284
285 if (err) { // bad filename, try the default font
286 fprintf(stderr, "ERROR: unable to open font '%x'\n", fontID);
287 SkDELETE(rec);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000288 return 0;
289 } else {
290 SkASSERT(rec->fFace);
291 //fprintf(stderr, "Opened font '%s'\n", filename.c_str());
292 rec->fNext = gFaceRecHead;
293 gFaceRecHead = rec;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000294 return rec;
295 }
296}
297
298static void unref_ft_face(FT_Face face) {
299 SkFaceRec* rec = gFaceRecHead;
300 SkFaceRec* prev = NULL;
301 while (rec) {
302 SkFaceRec* next = rec->fNext;
303 if (rec->fFace == face) {
304 if (--rec->fRefCnt == 0) {
305 if (prev) {
306 prev->fNext = next;
307 } else {
308 gFaceRecHead = next;
309 }
310 FT_Done_Face(face);
311 SkDELETE(rec);
312 }
313 return;
314 }
315 prev = rec;
316 rec = next;
317 }
tomhudson@google.com0c00f212011-12-28 14:59:50 +0000318 SkDEBUGFAIL("shouldn't get here, face not in list");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000319}
320
321///////////////////////////////////////////////////////////////////////////
322
vandebo@chromium.org16be6b82011-01-28 21:28:56 +0000323// Work around for old versions of freetype.
324static FT_Error getAdvances(FT_Face face, FT_UInt start, FT_UInt count,
325 FT_Int32 loadFlags, FT_Fixed* advances) {
326#ifdef FT_ADVANCES_H
327 return FT_Get_Advances(face, start, count, loadFlags, advances);
328#else
329 if (!face || start >= face->num_glyphs ||
330 start + count > face->num_glyphs || loadFlags != FT_LOAD_NO_SCALE) {
331 return 6; // "Invalid argument."
332 }
333 if (count == 0)
334 return 0;
335
336 for (int i = 0; i < count; i++) {
337 FT_Error err = FT_Load_Glyph(face, start + i, FT_LOAD_NO_SCALE);
338 if (err)
339 return err;
340 advances[i] = face->glyph->advance.x;
341 }
342
343 return 0;
344#endif
345}
346
347static bool canEmbed(FT_Face face) {
djsollen@google.comfa394d42012-01-09 20:40:25 +0000348#ifdef FT_FSTYPE_RESTRICTED_LICENSE_EMBEDDING
vandebo@chromium.org16be6b82011-01-28 21:28:56 +0000349 FT_UShort fsType = FT_Get_FSType_Flags(face);
350 return (fsType & (FT_FSTYPE_RESTRICTED_LICENSE_EMBEDDING |
351 FT_FSTYPE_BITMAP_EMBEDDING_ONLY)) == 0;
352#else
353 // No embedding is 0x2 and bitmap embedding only is 0x200.
354 TT_OS2* os2_table;
355 if ((os2_table = (TT_OS2*)FT_Get_Sfnt_Table(face, ft_sfnt_os2)) != NULL) {
356 return (os2_table->fsType & 0x202) == 0;
357 }
358 return false; // We tried, fail safe.
359#endif
360}
361
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000362static bool GetLetterCBox(FT_Face face, char letter, FT_BBox* bbox) {
363 const FT_UInt glyph_id = FT_Get_Char_Index(face, letter);
364 if (!glyph_id)
365 return false;
366 FT_Load_Glyph(face, glyph_id, FT_LOAD_NO_SCALE);
367 FT_Outline_Get_CBox(&face->glyph->outline, bbox);
368 return true;
369}
370
vandebo@chromium.org6f72d1e2011-02-14 23:19:59 +0000371static bool getWidthAdvance(FT_Face face, int gId, int16_t* data) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000372 FT_Fixed advance = 0;
vandebo@chromium.org6f72d1e2011-02-14 23:19:59 +0000373 if (getAdvances(face, gId, 1, FT_LOAD_NO_SCALE, &advance)) {
374 return false;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000375 }
vandebo@chromium.org6f72d1e2011-02-14 23:19:59 +0000376 SkASSERT(data);
377 *data = advance;
378 return true;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000379}
380
vandebo@chromium.org6744d492011-05-09 18:13:47 +0000381static void populate_glyph_to_unicode(FT_Face& face,
382 SkTDArray<SkUnichar>* glyphToUnicode) {
383 // Check and see if we have Unicode cmaps.
384 for (int i = 0; i < face->num_charmaps; ++i) {
385 // CMaps known to support Unicode:
386 // Platform ID Encoding ID Name
387 // ----------- ----------- -----------------------------------
388 // 0 0,1 Apple Unicode
389 // 0 3 Apple Unicode 2.0 (preferred)
390 // 3 1 Microsoft Unicode UCS-2
391 // 3 10 Microsoft Unicode UCS-4 (preferred)
392 //
393 // See Apple TrueType Reference Manual
394 // http://developer.apple.com/fonts/TTRefMan/RM06/Chap6cmap.html
395 // http://developer.apple.com/fonts/TTRefMan/RM06/Chap6name.html#ID
396 // Microsoft OpenType Specification
397 // http://www.microsoft.com/typography/otspec/cmap.htm
398
399 FT_UShort platformId = face->charmaps[i]->platform_id;
400 FT_UShort encodingId = face->charmaps[i]->encoding_id;
401
402 if (platformId != 0 && platformId != 3) {
403 continue;
404 }
405 if (platformId == 3 && encodingId != 1 && encodingId != 10) {
406 continue;
407 }
408 bool preferredMap = ((platformId == 3 && encodingId == 10) ||
409 (platformId == 0 && encodingId == 3));
410
411 FT_Set_Charmap(face, face->charmaps[i]);
412 if (glyphToUnicode->isEmpty()) {
413 glyphToUnicode->setCount(face->num_glyphs);
414 memset(glyphToUnicode->begin(), 0,
415 sizeof(SkUnichar) * face->num_glyphs);
416 }
417
418 // Iterate through each cmap entry.
419 FT_UInt glyphIndex;
420 for (SkUnichar charCode = FT_Get_First_Char(face, &glyphIndex);
421 glyphIndex != 0;
422 charCode = FT_Get_Next_Char(face, charCode, &glyphIndex)) {
423 if (charCode &&
424 ((*glyphToUnicode)[glyphIndex] == 0 || preferredMap)) {
425 (*glyphToUnicode)[glyphIndex] = charCode;
426 }
427 }
428 }
429}
430
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000431// static
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000432SkAdvancedTypefaceMetrics* SkFontHost::GetAdvancedTypefaceMetrics(
vandebo@chromium.org325cb9a2011-03-30 18:36:29 +0000433 uint32_t fontID,
vandebo@chromium.org37ad8fb2011-08-18 02:38:50 +0000434 SkAdvancedTypefaceMetrics::PerGlyphInfo perGlyphInfo,
435 const uint32_t* glyphIDs,
436 uint32_t glyphIDsCount) {
djsollen@google.comda957722011-11-16 17:00:46 +0000437#if defined(SK_BUILD_FOR_MAC)
reed@google.com8a5d6922011-03-14 15:08:03 +0000438 return NULL;
439#else
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000440 SkAutoMutexAcquire ac(gFTMutex);
441 FT_Library libInit = NULL;
442 if (gFTCount == 0) {
443 if (!InitFreetype())
444 sk_throw();
445 libInit = gFTLibrary;
446 }
447 SkAutoTCallIProc<struct FT_LibraryRec_, FT_Done_FreeType> ftLib(libInit);
448 SkFaceRec* rec = ref_ft_face(fontID);
449 if (NULL == rec)
450 return NULL;
451 FT_Face face = rec->fFace;
452
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000453 SkAdvancedTypefaceMetrics* info = new SkAdvancedTypefaceMetrics;
454 info->fFontName.set(FT_Get_Postscript_Name(face));
455 info->fMultiMaster = FT_HAS_MULTIPLE_MASTERS(face);
456 info->fLastGlyphID = face->num_glyphs - 1;
457 info->fEmSize = 1000;
458
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000459 bool cid = false;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000460 const char* fontType = FT_Get_X11_Font_Format(face);
vandebo@chromium.orgc3a2ae52011-02-03 21:48:23 +0000461 if (strcmp(fontType, "Type 1") == 0) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000462 info->fType = SkAdvancedTypefaceMetrics::kType1_Font;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000463 } else if (strcmp(fontType, "CID Type 1") == 0) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000464 info->fType = SkAdvancedTypefaceMetrics::kType1CID_Font;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000465 cid = true;
466 } else if (strcmp(fontType, "CFF") == 0) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000467 info->fType = SkAdvancedTypefaceMetrics::kCFF_Font;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000468 } else if (strcmp(fontType, "TrueType") == 0) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000469 info->fType = SkAdvancedTypefaceMetrics::kTrueType_Font;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000470 cid = true;
471 TT_Header* ttHeader;
472 if ((ttHeader = (TT_Header*)FT_Get_Sfnt_Table(face,
473 ft_sfnt_head)) != NULL) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000474 info->fEmSize = ttHeader->Units_Per_EM;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000475 }
476 }
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000477
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000478 info->fStyle = 0;
479 if (FT_IS_FIXED_WIDTH(face))
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000480 info->fStyle |= SkAdvancedTypefaceMetrics::kFixedPitch_Style;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000481 if (face->style_flags & FT_STYLE_FLAG_ITALIC)
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000482 info->fStyle |= SkAdvancedTypefaceMetrics::kItalic_Style;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000483 // We should set either Symbolic or Nonsymbolic; Nonsymbolic if the font's
484 // character set is a subset of 'Adobe standard Latin.'
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000485 info->fStyle |= SkAdvancedTypefaceMetrics::kSymbolic_Style;
486
487 PS_FontInfoRec ps_info;
488 TT_Postscript* tt_info;
489 if (FT_Get_PS_Font_Info(face, &ps_info) == 0) {
490 info->fItalicAngle = ps_info.italic_angle;
491 } else if ((tt_info =
492 (TT_Postscript*)FT_Get_Sfnt_Table(face,
493 ft_sfnt_post)) != NULL) {
494 info->fItalicAngle = SkFixedToScalar(tt_info->italicAngle);
495 } else {
496 info->fItalicAngle = 0;
497 }
498
499 info->fAscent = face->ascender;
500 info->fDescent = face->descender;
501
502 // Figure out a good guess for StemV - Min width of i, I, !, 1.
503 // This probably isn't very good with an italic font.
504 int16_t min_width = SHRT_MAX;
vandebo@chromium.org6f72d1e2011-02-14 23:19:59 +0000505 info->fStemV = 0;
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000506 char stem_chars[] = {'i', 'I', '!', '1'};
507 for (size_t i = 0; i < SK_ARRAY_COUNT(stem_chars); i++) {
508 FT_BBox bbox;
509 if (GetLetterCBox(face, stem_chars[i], &bbox)) {
510 int16_t width = bbox.xMax - bbox.xMin;
511 if (width > 0 && width < min_width) {
512 min_width = width;
513 info->fStemV = min_width;
514 }
515 }
516 }
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000517
518 TT_PCLT* pclt_info;
519 TT_OS2* os2_table;
520 if ((pclt_info = (TT_PCLT*)FT_Get_Sfnt_Table(face, ft_sfnt_pclt)) != NULL) {
521 info->fCapHeight = pclt_info->CapHeight;
522 uint8_t serif_style = pclt_info->SerifStyle & 0x3F;
523 if (serif_style >= 2 && serif_style <= 6)
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000524 info->fStyle |= SkAdvancedTypefaceMetrics::kSerif_Style;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000525 else if (serif_style >= 9 && serif_style <= 12)
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000526 info->fStyle |= SkAdvancedTypefaceMetrics::kScript_Style;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000527 } else if ((os2_table =
528 (TT_OS2*)FT_Get_Sfnt_Table(face, ft_sfnt_os2)) != NULL) {
529 info->fCapHeight = os2_table->sCapHeight;
530 } else {
531 // Figure out a good guess for CapHeight: average the height of M and X.
532 FT_BBox m_bbox, x_bbox;
533 bool got_m, got_x;
534 got_m = GetLetterCBox(face, 'M', &m_bbox);
535 got_x = GetLetterCBox(face, 'X', &x_bbox);
536 if (got_m && got_x) {
537 info->fCapHeight = (m_bbox.yMax - m_bbox.yMin + x_bbox.yMax -
538 x_bbox.yMin) / 2;
539 } else if (got_m && !got_x) {
540 info->fCapHeight = m_bbox.yMax - m_bbox.yMin;
541 } else if (!got_m && got_x) {
542 info->fCapHeight = x_bbox.yMax - x_bbox.yMin;
543 }
544 }
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000545
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000546 info->fBBox = SkIRect::MakeLTRB(face->bbox.xMin, face->bbox.yMax,
547 face->bbox.xMax, face->bbox.yMin);
548
vandebo@chromium.org37ad8fb2011-08-18 02:38:50 +0000549 if (!canEmbed(face) || !FT_IS_SCALABLE(face) ||
vandebo@chromium.org325cb9a2011-03-30 18:36:29 +0000550 info->fType == SkAdvancedTypefaceMetrics::kOther_Font) {
551 perGlyphInfo = SkAdvancedTypefaceMetrics::kNo_PerGlyphInfo;
552 }
553
554 if (perGlyphInfo & SkAdvancedTypefaceMetrics::kHAdvance_PerGlyphInfo) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000555 if (FT_IS_FIXED_WIDTH(face)) {
556 appendRange(&info->fGlyphWidths, 0);
557 int16_t advance = face->max_advance_width;
558 info->fGlyphWidths->fAdvance.append(1, &advance);
559 finishRange(info->fGlyphWidths.get(), 0,
560 SkAdvancedTypefaceMetrics::WidthRange::kDefault);
vandebo@chromium.org6f72d1e2011-02-14 23:19:59 +0000561 } else if (!cid) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000562 appendRange(&info->fGlyphWidths, 0);
563 // So as to not blow out the stack, get advances in batches.
564 for (int gID = 0; gID < face->num_glyphs; gID += 128) {
565 FT_Fixed advances[128];
566 int advanceCount = 128;
567 if (gID + advanceCount > face->num_glyphs)
568 advanceCount = face->num_glyphs - gID + 1;
569 getAdvances(face, gID, advanceCount, FT_LOAD_NO_SCALE,
570 advances);
571 for (int i = 0; i < advanceCount; i++) {
572 int16_t advance = advances[gID + i];
573 info->fGlyphWidths->fAdvance.append(1, &advance);
574 }
575 }
576 finishRange(info->fGlyphWidths.get(), face->num_glyphs - 1,
577 SkAdvancedTypefaceMetrics::WidthRange::kRange);
578 } else {
vandebo@chromium.org6f72d1e2011-02-14 23:19:59 +0000579 info->fGlyphWidths.reset(
vandebo@chromium.org37ad8fb2011-08-18 02:38:50 +0000580 getAdvanceData(face,
581 face->num_glyphs,
582 glyphIDs,
583 glyphIDsCount,
584 &getWidthAdvance));
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000585 }
vandebo@chromium.org325cb9a2011-03-30 18:36:29 +0000586 }
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000587
vandebo@chromium.org325cb9a2011-03-30 18:36:29 +0000588 if (perGlyphInfo & SkAdvancedTypefaceMetrics::kVAdvance_PerGlyphInfo &&
589 FT_HAS_VERTICAL(face)) {
590 SkASSERT(false); // Not implemented yet.
591 }
592
593 if (perGlyphInfo & SkAdvancedTypefaceMetrics::kGlyphNames_PerGlyphInfo &&
594 info->fType == SkAdvancedTypefaceMetrics::kType1_Font) {
595 // Postscript fonts may contain more than 255 glyphs, so we end up
596 // using multiple font descriptions with a glyph ordering. Record
597 // the name of each glyph.
598 info->fGlyphNames.reset(
599 new SkAutoTArray<SkString>(face->num_glyphs));
600 for (int gID = 0; gID < face->num_glyphs; gID++) {
601 char glyphName[128]; // PS limit for names is 127 bytes.
602 FT_Get_Glyph_Name(face, gID, glyphName, 128);
603 info->fGlyphNames->get()[gID].set(glyphName);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000604 }
605 }
606
vandebo@chromium.org6744d492011-05-09 18:13:47 +0000607 if (perGlyphInfo & SkAdvancedTypefaceMetrics::kToUnicode_PerGlyphInfo &&
608 info->fType != SkAdvancedTypefaceMetrics::kType1_Font &&
609 face->num_charmaps) {
610 populate_glyph_to_unicode(face, &(info->fGlyphToUnicode));
611 }
612
vandebo@chromium.orgc3a2ae52011-02-03 21:48:23 +0000613 if (!canEmbed(face))
614 info->fType = SkAdvancedTypefaceMetrics::kNotEmbeddable_Font;
615
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000616 unref_ft_face(face);
617 return info;
reed@google.com8a5d6922011-03-14 15:08:03 +0000618#endif
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000619}
vandebo@chromium.org37ad8fb2011-08-18 02:38:50 +0000620
reed@google.com618ef5e2011-01-26 22:10:41 +0000621///////////////////////////////////////////////////////////////////////////
622
reed@google.comffe49f52011-11-22 19:42:41 +0000623#define BLACK_LUMINANCE_LIMIT 0x40
624#define WHITE_LUMINANCE_LIMIT 0xA0
625
reed@google.com8ed436c2011-07-21 14:12:36 +0000626static bool bothZero(SkScalar a, SkScalar b) {
627 return 0 == a && 0 == b;
628}
629
630// returns false if there is any non-90-rotation or skew
631static bool isAxisAligned(const SkScalerContext::Rec& rec) {
632 return 0 == rec.fPreSkewX &&
633 (bothZero(rec.fPost2x2[0][1], rec.fPost2x2[1][0]) ||
634 bothZero(rec.fPost2x2[0][0], rec.fPost2x2[1][1]));
635}
636
reed@google.com618ef5e2011-01-26 22:10:41 +0000637void SkFontHost::FilterRec(SkScalerContext::Rec* rec) {
bungeman@google.com8cf32262012-04-02 14:34:30 +0000638 //BOGUS: http://code.google.com/p/chromium/issues/detail?id=121119
639 //Cap the requested size as larger sizes give bogus values.
640 //Remove when http://code.google.com/p/skia/issues/detail?id=554 is fixed.
bungeman@google.com5582e632012-04-02 14:51:54 +0000641 if (rec->fTextSize > SkIntToScalar(1 << 14)) {
642 rec->fTextSize = SkIntToScalar(1 << 14);
bungeman@google.com8cf32262012-04-02 14:34:30 +0000643 }
bungeman@google.comfd668cf2012-08-24 17:46:11 +0000644
reed@google.com618ef5e2011-01-26 22:10:41 +0000645 if (!gLCDSupportValid) {
646 InitFreetype();
647 FT_Done_FreeType(gFTLibrary);
648 }
reed@google.com5b31b0f2011-02-23 14:41:42 +0000649
reed@google.comeffc5012011-06-27 16:44:46 +0000650 if (!gLCDSupport && isLCD(*rec)) {
reed@google.com618ef5e2011-01-26 22:10:41 +0000651 // If the runtime Freetype library doesn't support LCD mode, we disable
652 // it here.
653 rec->fMaskFormat = SkMask::kA8_Format;
654 }
reed@google.com5b31b0f2011-02-23 14:41:42 +0000655
reed@google.com618ef5e2011-01-26 22:10:41 +0000656 SkPaint::Hinting h = rec->getHinting();
reed@google.comeffc5012011-06-27 16:44:46 +0000657 if (SkPaint::kFull_Hinting == h && !isLCD(*rec)) {
reed@google.com618ef5e2011-01-26 22:10:41 +0000658 // collapse full->normal hinting if we're not doing LCD
659 h = SkPaint::kNormal_Hinting;
reed@google.com618ef5e2011-01-26 22:10:41 +0000660 }
bungeman@google.comf4f2b802012-03-08 19:19:51 +0000661 if ((rec->fFlags & SkScalerContext::kSubpixelPositioning_Flag)) {
reed@google.com1ac83502012-02-28 17:06:02 +0000662 if (SkPaint::kNo_Hinting != h) {
663 h = SkPaint::kSlight_Hinting;
664 }
665 }
666
vandebo@chromium.org37ad8fb2011-08-18 02:38:50 +0000667#ifndef SK_IGNORE_ROTATED_FREETYPE_FIX
reed@google.com8ed436c2011-07-21 14:12:36 +0000668 // rotated text looks bad with hinting, so we disable it as needed
669 if (!isAxisAligned(*rec)) {
670 h = SkPaint::kNo_Hinting;
671 }
bsalomon@google.com0e35ca82011-07-22 17:56:19 +0000672#endif
reed@google.com618ef5e2011-01-26 22:10:41 +0000673 rec->setHinting(h);
reed@google.comffe49f52011-11-22 19:42:41 +0000674
bungeman@google.com97efada2012-07-30 20:40:50 +0000675#ifndef SK_GAMMA_APPLY_TO_A8
676 if (!isLCD(*rec)) {
677 rec->ignorePreBlend();
reed@google.comffe49f52011-11-22 19:42:41 +0000678 }
reed@google.com1ac83502012-02-28 17:06:02 +0000679#endif
reed@google.com618ef5e2011-01-26 22:10:41 +0000680}
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000681
djsollen@google.comda957722011-11-16 17:00:46 +0000682#ifdef SK_BUILD_FOR_ANDROID
djsollen@google.comcd9d69b2011-03-14 20:30:14 +0000683uint32_t SkFontHost::GetUnitsPerEm(SkFontID fontID) {
684 SkAutoMutexAcquire ac(gFTMutex);
685 SkFaceRec *rec = ref_ft_face(fontID);
686 uint16_t unitsPerEm = 0;
687
688 if (rec != NULL && rec->fFace != NULL) {
689 unitsPerEm = rec->fFace->units_per_EM;
690 unref_ft_face(rec->fFace);
691 }
692
693 return (uint32_t)unitsPerEm;
694}
695#endif
696
reed@android.com8a1c16f2008-12-17 15:59:43 +0000697SkScalerContext_FreeType::SkScalerContext_FreeType(const SkDescriptor* desc)
george@mozilla.comc59b5da2012-08-23 00:39:08 +0000698 : SkScalerContext_FreeType_Base(desc) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000699 SkAutoMutexAcquire ac(gFTMutex);
700
reed@android.com8a1c16f2008-12-17 15:59:43 +0000701 if (gFTCount == 0) {
reed@android.com659aaf92009-07-23 15:20:21 +0000702 if (!InitFreetype()) {
703 sk_throw();
704 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000705 }
706 ++gFTCount;
707
708 // load the font file
reed@android.com62900b42009-02-11 15:07:19 +0000709 fFTSize = NULL;
710 fFace = NULL;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000711 fFaceRec = ref_ft_face(fRec.fFontID);
reed@android.com62900b42009-02-11 15:07:19 +0000712 if (NULL == fFaceRec) {
713 return;
714 }
715 fFace = fFaceRec->fFace;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000716
717 // compute our factors from the record
718
719 SkMatrix m;
720
721 fRec.getSingleMatrix(&m);
722
723#ifdef DUMP_STRIKE_CREATION
724 SkString keyString;
725 SkFontHost::GetDescriptorKeyString(desc, &keyString);
726 printf("========== strike [%g %g %g] [%g %g %g %g] hints %d format %d %s\n", SkScalarToFloat(fRec.fTextSize),
727 SkScalarToFloat(fRec.fPreScaleX), SkScalarToFloat(fRec.fPreSkewX),
728 SkScalarToFloat(fRec.fPost2x2[0][0]), SkScalarToFloat(fRec.fPost2x2[0][1]),
729 SkScalarToFloat(fRec.fPost2x2[1][0]), SkScalarToFloat(fRec.fPost2x2[1][1]),
agl@chromium.org309485b2009-07-21 17:41:32 +0000730 fRec.getHinting(), fRec.fMaskFormat, keyString.c_str());
reed@android.com8a1c16f2008-12-17 15:59:43 +0000731#endif
732
733 // now compute our scale factors
734 SkScalar sx = m.getScaleX();
735 SkScalar sy = m.getScaleY();
736
737 if (m.getSkewX() || m.getSkewY() || sx < 0 || sy < 0) {
738 // sort of give up on hinting
739 sx = SkMaxScalar(SkScalarAbs(sx), SkScalarAbs(m.getSkewX()));
740 sy = SkMaxScalar(SkScalarAbs(m.getSkewY()), SkScalarAbs(sy));
741 sx = sy = SkScalarAve(sx, sy);
742
743 SkScalar inv = SkScalarInvert(sx);
744
745 // flip the skew elements to go from our Y-down system to FreeType's
746 fMatrix22.xx = SkScalarToFixed(SkScalarMul(m.getScaleX(), inv));
747 fMatrix22.xy = -SkScalarToFixed(SkScalarMul(m.getSkewX(), inv));
748 fMatrix22.yx = -SkScalarToFixed(SkScalarMul(m.getSkewY(), inv));
749 fMatrix22.yy = SkScalarToFixed(SkScalarMul(m.getScaleY(), inv));
750 } else {
751 fMatrix22.xx = fMatrix22.yy = SK_Fixed1;
752 fMatrix22.xy = fMatrix22.yx = 0;
753 }
754
755 fScaleX = SkScalarToFixed(sx);
756 fScaleY = SkScalarToFixed(sy);
757
reed@google.coma1bfa212012-03-08 21:57:12 +0000758 fLCDIsVert = SkToBool(fRec.fFlags & SkScalerContext::kLCD_Vertical_Flag);
759
reed@android.com8a1c16f2008-12-17 15:59:43 +0000760 // compute the flags we send to Load_Glyph
761 {
reed@android.come4d0bc02009-07-24 19:53:20 +0000762 FT_Int32 loadFlags = FT_LOAD_DEFAULT;
bungeman@google.com34f10262012-03-23 18:11:47 +0000763 bool linearMetrics = SkToBool(fRec.fFlags & SkScalerContext::kSubpixelPositioning_Flag);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000764
agl@chromium.org70a303f2010-05-10 14:15:50 +0000765 if (SkMask::kBW_Format == fRec.fMaskFormat) {
766 // See http://code.google.com/p/chromium/issues/detail?id=43252#c24
767 loadFlags = FT_LOAD_TARGET_MONO;
reed@google.comeffc5012011-06-27 16:44:46 +0000768 if (fRec.getHinting() == SkPaint::kNo_Hinting) {
agl@chromium.org70a303f2010-05-10 14:15:50 +0000769 loadFlags = FT_LOAD_NO_HINTING;
reed@google.combdc99882011-11-21 14:36:57 +0000770 linearMetrics = true;
reed@google.comeffc5012011-06-27 16:44:46 +0000771 }
agl@chromium.org70a303f2010-05-10 14:15:50 +0000772 } else {
773 switch (fRec.getHinting()) {
774 case SkPaint::kNo_Hinting:
775 loadFlags = FT_LOAD_NO_HINTING;
reed@google.combdc99882011-11-21 14:36:57 +0000776 linearMetrics = true;
agl@chromium.org70a303f2010-05-10 14:15:50 +0000777 break;
778 case SkPaint::kSlight_Hinting:
779 loadFlags = FT_LOAD_TARGET_LIGHT; // This implies FORCE_AUTOHINT
780 break;
781 case SkPaint::kNormal_Hinting:
agl@chromium.orga2c71cb2010-06-17 20:49:17 +0000782 if (fRec.fFlags & SkScalerContext::kAutohinting_Flag)
783 loadFlags = FT_LOAD_FORCE_AUTOHINT;
784 else
785 loadFlags = FT_LOAD_NO_AUTOHINT;
agl@chromium.org70a303f2010-05-10 14:15:50 +0000786 break;
787 case SkPaint::kFull_Hinting:
agl@chromium.orga2c71cb2010-06-17 20:49:17 +0000788 if (fRec.fFlags & SkScalerContext::kAutohinting_Flag) {
789 loadFlags = FT_LOAD_FORCE_AUTOHINT;
790 break;
791 }
agl@chromium.org70a303f2010-05-10 14:15:50 +0000792 loadFlags = FT_LOAD_TARGET_NORMAL;
reed@google.comeffc5012011-06-27 16:44:46 +0000793 if (isLCD(fRec)) {
reed@google.coma1bfa212012-03-08 21:57:12 +0000794 if (fLCDIsVert) {
reed@google.comeffc5012011-06-27 16:44:46 +0000795 loadFlags = FT_LOAD_TARGET_LCD_V;
796 } else {
797 loadFlags = FT_LOAD_TARGET_LCD;
798 }
reed@google.comea2333d2011-03-14 16:44:56 +0000799 }
agl@chromium.org70a303f2010-05-10 14:15:50 +0000800 break;
801 default:
802 SkDebugf("---------- UNKNOWN hinting %d\n", fRec.getHinting());
803 break;
804 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000805 }
806
reed@google.comeffc5012011-06-27 16:44:46 +0000807 if ((fRec.fFlags & SkScalerContext::kEmbeddedBitmapText_Flag) == 0) {
agl@chromium.orge0d08992009-08-07 19:19:23 +0000808 loadFlags |= FT_LOAD_NO_BITMAP;
reed@google.comeffc5012011-06-27 16:44:46 +0000809 }
agl@chromium.orge0d08992009-08-07 19:19:23 +0000810
reed@google.com96a9f7912011-05-06 11:49:30 +0000811 // Always using FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH to get correct
812 // advances, as fontconfig and cairo do.
813 // See http://code.google.com/p/skia/issues/detail?id=222.
814 loadFlags |= FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH;
815
reed@android.come4d0bc02009-07-24 19:53:20 +0000816 fLoadGlyphFlags = loadFlags;
reed@google.combdc99882011-11-21 14:36:57 +0000817 fDoLinearMetrics = linearMetrics;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000818 }
819
820 // now create the FT_Size
821
822 {
823 FT_Error err;
824
825 err = FT_New_Size(fFace, &fFTSize);
826 if (err != 0) {
827 SkDEBUGF(("SkScalerContext_FreeType::FT_New_Size(%x): FT_Set_Char_Size(0x%x, 0x%x) returned 0x%x\n",
828 fFaceRec->fFontID, fScaleX, fScaleY, err));
829 fFace = NULL;
830 return;
831 }
832
833 err = FT_Activate_Size(fFTSize);
834 if (err != 0) {
835 SkDEBUGF(("SkScalerContext_FreeType::FT_Activate_Size(%x, 0x%x, 0x%x) returned 0x%x\n",
836 fFaceRec->fFontID, fScaleX, fScaleY, err));
837 fFTSize = NULL;
838 }
839
840 err = FT_Set_Char_Size( fFace,
841 SkFixedToFDot6(fScaleX), SkFixedToFDot6(fScaleY),
842 72, 72);
843 if (err != 0) {
844 SkDEBUGF(("SkScalerContext_FreeType::FT_Set_Char_Size(%x, 0x%x, 0x%x) returned 0x%x\n",
845 fFaceRec->fFontID, fScaleX, fScaleY, err));
846 fFace = NULL;
847 return;
848 }
849
850 FT_Set_Transform( fFace, &fMatrix22, NULL);
851 }
852}
853
854SkScalerContext_FreeType::~SkScalerContext_FreeType() {
855 if (fFTSize != NULL) {
856 FT_Done_Size(fFTSize);
857 }
858
859 SkAutoMutexAcquire ac(gFTMutex);
860
861 if (fFace != NULL) {
862 unref_ft_face(fFace);
863 }
864 if (--gFTCount == 0) {
865// SkDEBUGF(("FT_Done_FreeType\n"));
866 FT_Done_FreeType(gFTLibrary);
867 SkDEBUGCODE(gFTLibrary = NULL;)
868 }
869}
870
871/* We call this before each use of the fFace, since we may be sharing
872 this face with other context (at different sizes).
873*/
874FT_Error SkScalerContext_FreeType::setupSize() {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000875 FT_Error err = FT_Activate_Size(fFTSize);
876
877 if (err != 0) {
878 SkDEBUGF(("SkScalerContext_FreeType::FT_Activate_Size(%x, 0x%x, 0x%x) returned 0x%x\n",
879 fFaceRec->fFontID, fScaleX, fScaleY, err));
880 fFTSize = NULL;
881 } else {
882 // seems we need to reset this every time (not sure why, but without it
883 // I get random italics from some other fFTSize)
884 FT_Set_Transform( fFace, &fMatrix22, NULL);
885 }
886 return err;
887}
888
ctguil@chromium.org0bc7bf52011-03-04 19:04:57 +0000889unsigned SkScalerContext_FreeType::generateGlyphCount() {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000890 return fFace->num_glyphs;
891}
892
893uint16_t SkScalerContext_FreeType::generateCharToGlyph(SkUnichar uni) {
894 return SkToU16(FT_Get_Char_Index( fFace, uni ));
895}
896
reed@android.com9d3a9852010-01-08 14:07:42 +0000897SkUnichar SkScalerContext_FreeType::generateGlyphToChar(uint16_t glyph) {
898 // iterate through each cmap entry, looking for matching glyph indices
899 FT_UInt glyphIndex;
900 SkUnichar charCode = FT_Get_First_Char( fFace, &glyphIndex );
901
902 while (glyphIndex != 0) {
903 if (glyphIndex == glyph) {
904 return charCode;
905 }
906 charCode = FT_Get_Next_Char( fFace, charCode, &glyphIndex );
907 }
908
909 return 0;
910}
911
reed@android.com8a1c16f2008-12-17 15:59:43 +0000912void SkScalerContext_FreeType::generateAdvance(SkGlyph* glyph) {
913#ifdef FT_ADVANCES_H
914 /* unhinted and light hinted text have linearly scaled advances
915 * which are very cheap to compute with some font formats...
916 */
reed@google.combdc99882011-11-21 14:36:57 +0000917 if (fDoLinearMetrics) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000918 SkAutoMutexAcquire ac(gFTMutex);
919
920 if (this->setupSize()) {
reed@android.com62900b42009-02-11 15:07:19 +0000921 glyph->zeroMetrics();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000922 return;
923 }
924
925 FT_Error error;
926 FT_Fixed advance;
927
928 error = FT_Get_Advance( fFace, glyph->getGlyphID(fBaseGlyphCount),
929 fLoadGlyphFlags | FT_ADVANCE_FLAG_FAST_ONLY,
930 &advance );
931 if (0 == error) {
932 glyph->fRsbDelta = 0;
933 glyph->fLsbDelta = 0;
reed@google.comd074c372012-07-18 13:45:58 +0000934 glyph->fAdvanceX = SkFixedMul(fMatrix22.xx, advance);
935 glyph->fAdvanceY = - SkFixedMul(fMatrix22.yx, advance);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000936 return;
937 }
938 }
939#endif /* FT_ADVANCES_H */
940 /* otherwise, we need to load/hint the glyph, which is slower */
941 this->generateMetrics(glyph);
942 return;
943}
944
djsollen@google.comd8b599c2012-03-19 19:44:19 +0000945void SkScalerContext_FreeType::getBBoxForCurrentGlyph(SkGlyph* glyph,
946 FT_BBox* bbox,
947 bool snapToPixelBoundary) {
948
949 FT_Outline_Get_CBox(&fFace->glyph->outline, bbox);
950
951 if (fRec.fFlags & SkScalerContext::kSubpixelPositioning_Flag) {
george@mozilla.comc59b5da2012-08-23 00:39:08 +0000952 int dx = SkFixedToFDot6(glyph->getSubXFixed());
953 int dy = SkFixedToFDot6(glyph->getSubYFixed());
djsollen@google.comd8b599c2012-03-19 19:44:19 +0000954 // negate dy since freetype-y-goes-up and skia-y-goes-down
955 bbox->xMin += dx;
956 bbox->yMin -= dy;
957 bbox->xMax += dx;
958 bbox->yMax -= dy;
959 }
960
961 // outset the box to integral boundaries
962 if (snapToPixelBoundary) {
963 bbox->xMin &= ~63;
964 bbox->yMin &= ~63;
965 bbox->xMax = (bbox->xMax + 63) & ~63;
966 bbox->yMax = (bbox->yMax + 63) & ~63;
967 }
968}
969
970void SkScalerContext_FreeType::updateGlyphIfLCD(SkGlyph* glyph) {
971 if (isLCD(fRec)) {
972 if (fLCDIsVert) {
973 glyph->fHeight += gLCDExtra;
974 glyph->fTop -= gLCDExtra >> 1;
975 } else {
976 glyph->fWidth += gLCDExtra;
977 glyph->fLeft -= gLCDExtra >> 1;
978 }
979 }
980}
bungeman@google.com875eb982012-04-12 15:53:23 +0000981void SkScalerContext_FreeType::updateGlyphPosIfLCD(SkGlyph* glyph) {
982 if (isLCD(fRec)) {
983 if (fLCDIsVert) {
984 glyph->fTop -= gLCDExtra >> 1;
985 } else {
986 glyph->fLeft -= gLCDExtra >> 1;
987 }
988 }
989}
djsollen@google.comd8b599c2012-03-19 19:44:19 +0000990
reed@android.com8a1c16f2008-12-17 15:59:43 +0000991void SkScalerContext_FreeType::generateMetrics(SkGlyph* glyph) {
992 SkAutoMutexAcquire ac(gFTMutex);
993
994 glyph->fRsbDelta = 0;
995 glyph->fLsbDelta = 0;
996
997 FT_Error err;
998
999 if (this->setupSize()) {
1000 goto ERROR;
1001 }
1002
1003 err = FT_Load_Glyph( fFace, glyph->getGlyphID(fBaseGlyphCount), fLoadGlyphFlags );
1004 if (err != 0) {
1005 SkDEBUGF(("SkScalerContext_FreeType::generateMetrics(%x): FT_Load_Glyph(glyph:%d flags:%d) returned 0x%x\n",
1006 fFaceRec->fFontID, glyph->getGlyphID(fBaseGlyphCount), fLoadGlyphFlags, err));
1007 ERROR:
reed@android.com62900b42009-02-11 15:07:19 +00001008 glyph->zeroMetrics();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001009 return;
1010 }
1011
vandebo@chromium.org6390c722012-03-28 21:03:22 +00001012 SkFixed vLeft = 0, vTop = 0;
djsollen@google.comd8b599c2012-03-19 19:44:19 +00001013
reed@android.com8a1c16f2008-12-17 15:59:43 +00001014 switch ( fFace->glyph->format ) {
thakis@chromium.org598b8592011-05-24 05:42:55 +00001015 case FT_GLYPH_FORMAT_OUTLINE: {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001016 FT_BBox bbox;
1017
bungeman@google.com0f0c2882011-11-04 15:47:41 +00001018 if (0 == fFace->glyph->outline.n_contours) {
1019 glyph->fWidth = 0;
1020 glyph->fHeight = 0;
1021 glyph->fTop = 0;
1022 glyph->fLeft = 0;
1023 break;
1024 }
1025
senorblanco@chromium.org4526a842010-02-05 23:08:20 +00001026 if (fRec.fFlags & kEmbolden_Flag) {
george@mozilla.comc59b5da2012-08-23 00:39:08 +00001027 emboldenOutline(fFace, &fFace->glyph->outline);
senorblanco@chromium.org4526a842010-02-05 23:08:20 +00001028 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001029
djsollen@google.comd8b599c2012-03-19 19:44:19 +00001030 getBBoxForCurrentGlyph(glyph, &bbox, true);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001031
1032 glyph->fWidth = SkToU16((bbox.xMax - bbox.xMin) >> 6);
1033 glyph->fHeight = SkToU16((bbox.yMax - bbox.yMin) >> 6);
1034 glyph->fTop = -SkToS16(bbox.yMax >> 6);
1035 glyph->fLeft = SkToS16(bbox.xMin >> 6);
reed@google.coma1c32562012-03-01 19:38:23 +00001036
djsollen@google.comd8b599c2012-03-19 19:44:19 +00001037 if ((fRec.fFlags & SkScalerContext::kVertical_Flag)) {
george@mozilla.comc59b5da2012-08-23 00:39:08 +00001038 vLeft = SkFDot6ToFixed(bbox.xMin);
1039 vTop = SkFDot6ToFixed(bbox.yMax);
reed@google.coma1c32562012-03-01 19:38:23 +00001040 }
djsollen@google.comd8b599c2012-03-19 19:44:19 +00001041
1042 updateGlyphIfLCD(glyph);
1043
reed@android.com8a1c16f2008-12-17 15:59:43 +00001044 break;
thakis@chromium.org598b8592011-05-24 05:42:55 +00001045 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001046
1047 case FT_GLYPH_FORMAT_BITMAP:
agl@chromium.orge76073b2010-06-04 20:31:17 +00001048 if (fRec.fFlags & kEmbolden_Flag) {
1049 FT_GlyphSlot_Own_Bitmap(fFace->glyph);
1050 FT_Bitmap_Embolden(gFTLibrary, &fFace->glyph->bitmap, kBitmapEmboldenStrength, 0);
1051 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001052 glyph->fWidth = SkToU16(fFace->glyph->bitmap.width);
1053 glyph->fHeight = SkToU16(fFace->glyph->bitmap.rows);
1054 glyph->fTop = -SkToS16(fFace->glyph->bitmap_top);
1055 glyph->fLeft = SkToS16(fFace->glyph->bitmap_left);
1056 break;
1057
1058 default:
tomhudson@google.com0c00f212011-12-28 14:59:50 +00001059 SkDEBUGFAIL("unknown glyph format");
reed@android.com8a1c16f2008-12-17 15:59:43 +00001060 goto ERROR;
1061 }
1062
bungeman@google.com34f10262012-03-23 18:11:47 +00001063 if (fDoLinearMetrics) {
1064 glyph->fAdvanceX = SkFixedMul(fMatrix22.xx, fFace->glyph->linearHoriAdvance);
1065 glyph->fAdvanceY = -SkFixedMul(fMatrix22.yx, fFace->glyph->linearHoriAdvance);
1066 } else {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001067 glyph->fAdvanceX = SkFDot6ToFixed(fFace->glyph->advance.x);
1068 glyph->fAdvanceY = -SkFDot6ToFixed(fFace->glyph->advance.y);
bungeman@google.com34f10262012-03-23 18:11:47 +00001069
reed@android.com8a1c16f2008-12-17 15:59:43 +00001070 if (fRec.fFlags & kDevKernText_Flag) {
1071 glyph->fRsbDelta = SkToS8(fFace->glyph->rsb_delta);
1072 glyph->fLsbDelta = SkToS8(fFace->glyph->lsb_delta);
1073 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001074 }
1075
djsollen@google.comd8b599c2012-03-19 19:44:19 +00001076 if ((fRec.fFlags & SkScalerContext::kVertical_Flag)
1077 && fFace->glyph->format == FT_GLYPH_FORMAT_OUTLINE) {
1078
1079 //TODO: do we need to specially handle SubpixelPositioning and Kerning?
1080
1081 FT_Matrix identityMatrix;
1082 identityMatrix.xx = identityMatrix.yy = SK_Fixed1;
1083 identityMatrix.xy = identityMatrix.yx = 0;
1084
1085 // if the matrix is not the identity matrix then we need to re-load the
1086 // glyph with the identity matrix to get the necessary bounding box
1087 if (memcmp(&fMatrix22, &identityMatrix, sizeof(FT_Matrix)) != 0) {
1088
1089 FT_Set_Transform(fFace, &identityMatrix, NULL);
1090
1091 err = FT_Load_Glyph( fFace, glyph->getGlyphID(fBaseGlyphCount), fLoadGlyphFlags );
1092 if (err != 0) {
1093 SkDEBUGF(("SkScalerContext_FreeType::generateMetrics(%x): FT_Load_Glyph(glyph:%d flags:%d) returned 0x%x\n",
1094 fFaceRec->fFontID, glyph->getGlyphID(fBaseGlyphCount), fLoadGlyphFlags, err));
1095 goto ERROR;
1096 }
1097
1098 if (fRec.fFlags & kEmbolden_Flag) {
george@mozilla.comc59b5da2012-08-23 00:39:08 +00001099 emboldenOutline(fFace, &fFace->glyph->outline);
djsollen@google.comd8b599c2012-03-19 19:44:19 +00001100 }
1101 }
1102
1103 // bounding box of the unskewed and unscaled glyph
vandebo@chromium.org6390c722012-03-28 21:03:22 +00001104 FT_BBox bbox = {0, 0, 0, 0}; // Suppress Coverity warning.
djsollen@google.comd8b599c2012-03-19 19:44:19 +00001105 getBBoxForCurrentGlyph(glyph, &bbox);
1106
1107 // compute the vertical gap above and below the glyph if the glyph were
1108 // centered within the linearVertAdvance
george@mozilla.comc59b5da2012-08-23 00:39:08 +00001109 SkFixed vGap = (fFace->glyph->linearVertAdvance - SkFDot6ToFixed(bbox.yMax - bbox.yMin)) / 2;
djsollen@google.comd8b599c2012-03-19 19:44:19 +00001110
1111 // the origin point of the glyph when rendered vertically
1112 FT_Vector vOrigin;
1113 vOrigin.x = fFace->glyph->linearHoriAdvance / 2;
george@mozilla.comc59b5da2012-08-23 00:39:08 +00001114 vOrigin.y = vGap + SkFDot6ToFixed(bbox.yMax);
djsollen@google.comd8b599c2012-03-19 19:44:19 +00001115
1116 // transform the vertical origin based on the matrix of the actual glyph
1117 FT_Vector_Transform(&vOrigin, &fMatrix22);
1118
1119 // compute a new offset vector for the glyph by subtracting the vertical
1120 // origin from the original horizontal offset vector
1121 glyph->fLeft = SkFixedRoundToInt(vLeft - vOrigin.x);
1122 glyph->fTop = -SkFixedRoundToInt(vTop - vOrigin.y);
1123
bungeman@google.com875eb982012-04-12 15:53:23 +00001124 updateGlyphPosIfLCD(glyph);
djsollen@google.comd8b599c2012-03-19 19:44:19 +00001125
1126 // use the vertical advance values computed by freetype
1127 glyph->fAdvanceX = -SkFixedMul(fMatrix22.xy, fFace->glyph->linearVertAdvance);
1128 glyph->fAdvanceY = SkFixedMul(fMatrix22.yy, fFace->glyph->linearVertAdvance);
1129 }
1130
1131
reed@android.com8a1c16f2008-12-17 15:59:43 +00001132#ifdef ENABLE_GLYPH_SPEW
1133 SkDEBUGF(("FT_Set_Char_Size(this:%p sx:%x sy:%x ", this, fScaleX, fScaleY));
1134 SkDEBUGF(("Metrics(glyph:%d flags:0x%x) w:%d\n", glyph->getGlyphID(fBaseGlyphCount), fLoadGlyphFlags, glyph->fWidth));
1135#endif
1136}
1137
reed@google.comea2333d2011-03-14 16:44:56 +00001138
bungeman@google.com97efada2012-07-30 20:40:50 +00001139void SkScalerContext_FreeType::generateImage(const SkGlyph& glyph, SkMaskGamma::PreBlend* maskPreBlend) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001140 SkAutoMutexAcquire ac(gFTMutex);
1141
1142 FT_Error err;
1143
1144 if (this->setupSize()) {
1145 goto ERROR;
1146 }
1147
1148 err = FT_Load_Glyph( fFace, glyph.getGlyphID(fBaseGlyphCount), fLoadGlyphFlags);
1149 if (err != 0) {
1150 SkDEBUGF(("SkScalerContext_FreeType::generateImage: FT_Load_Glyph(glyph:%d width:%d height:%d rb:%d flags:%d) returned 0x%x\n",
1151 glyph.getGlyphID(fBaseGlyphCount), glyph.fWidth, glyph.fHeight, glyph.rowBytes(), fLoadGlyphFlags, err));
1152 ERROR:
1153 memset(glyph.fImage, 0, glyph.rowBytes() * glyph.fHeight);
1154 return;
1155 }
1156
george@mozilla.comc59b5da2012-08-23 00:39:08 +00001157 generateGlyphImage(fFace, glyph, maskPreBlend);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001158}
1159
reed@android.com8a1c16f2008-12-17 15:59:43 +00001160
1161void SkScalerContext_FreeType::generatePath(const SkGlyph& glyph,
1162 SkPath* path) {
1163 SkAutoMutexAcquire ac(gFTMutex);
1164
1165 SkASSERT(&glyph && path);
1166
1167 if (this->setupSize()) {
1168 path->reset();
1169 return;
1170 }
1171
1172 uint32_t flags = fLoadGlyphFlags;
1173 flags |= FT_LOAD_NO_BITMAP; // ignore embedded bitmaps so we're sure to get the outline
1174 flags &= ~FT_LOAD_RENDER; // don't scan convert (we just want the outline)
1175
1176 FT_Error err = FT_Load_Glyph( fFace, glyph.getGlyphID(fBaseGlyphCount), flags);
1177
1178 if (err != 0) {
1179 SkDEBUGF(("SkScalerContext_FreeType::generatePath: FT_Load_Glyph(glyph:%d flags:%d) returned 0x%x\n",
1180 glyph.getGlyphID(fBaseGlyphCount), flags, err));
1181 path->reset();
1182 return;
1183 }
1184
george@mozilla.comc59b5da2012-08-23 00:39:08 +00001185 generateGlyphPath(fFace, glyph, path);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001186}
1187
1188void SkScalerContext_FreeType::generateFontMetrics(SkPaint::FontMetrics* mx,
1189 SkPaint::FontMetrics* my) {
1190 if (NULL == mx && NULL == my) {
1191 return;
1192 }
1193
1194 SkAutoMutexAcquire ac(gFTMutex);
1195
1196 if (this->setupSize()) {
reed@android.coma8a8b8b2009-05-04 15:00:11 +00001197 ERROR:
reed@android.com8a1c16f2008-12-17 15:59:43 +00001198 if (mx) {
reed@android.com4516f472009-06-29 16:25:36 +00001199 sk_bzero(mx, sizeof(SkPaint::FontMetrics));
reed@android.com8a1c16f2008-12-17 15:59:43 +00001200 }
1201 if (my) {
reed@android.com4516f472009-06-29 16:25:36 +00001202 sk_bzero(my, sizeof(SkPaint::FontMetrics));
reed@android.com8a1c16f2008-12-17 15:59:43 +00001203 }
1204 return;
1205 }
1206
reed@android.coma8a8b8b2009-05-04 15:00:11 +00001207 FT_Face face = fFace;
1208 int upem = face->units_per_EM;
1209 if (upem <= 0) {
1210 goto ERROR;
1211 }
1212
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001213 SkPoint pts[6];
1214 SkFixed ys[6];
reed@android.com8a1c16f2008-12-17 15:59:43 +00001215 SkFixed scaleY = fScaleY;
1216 SkFixed mxy = fMatrix22.xy;
1217 SkFixed myy = fMatrix22.yy;
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001218 SkScalar xmin = SkIntToScalar(face->bbox.xMin) / upem;
1219 SkScalar xmax = SkIntToScalar(face->bbox.xMax) / upem;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001220
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001221 int leading = face->height - (face->ascender + -face->descender);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001222 if (leading < 0) {
1223 leading = 0;
1224 }
1225
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001226 // Try to get the OS/2 table from the font. This contains the specific
1227 // average font width metrics which Windows uses.
1228 TT_OS2* os2 = (TT_OS2*) FT_Get_Sfnt_Table(face, ft_sfnt_os2);
1229
reed@android.com8a1c16f2008-12-17 15:59:43 +00001230 ys[0] = -face->bbox.yMax;
1231 ys[1] = -face->ascender;
1232 ys[2] = -face->descender;
1233 ys[3] = -face->bbox.yMin;
1234 ys[4] = leading;
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001235 ys[5] = os2 ? os2->xAvgCharWidth : 0;
1236
1237 SkScalar x_height;
1238 if (os2 && os2->sxHeight) {
1239 x_height = SkFixedToScalar(SkMulDiv(fScaleX, os2->sxHeight, upem));
1240 } else {
1241 const FT_UInt x_glyph = FT_Get_Char_Index(fFace, 'x');
1242 if (x_glyph) {
1243 FT_BBox bbox;
1244 FT_Load_Glyph(fFace, x_glyph, fLoadGlyphFlags);
senorblanco@chromium.org4526a842010-02-05 23:08:20 +00001245 if (fRec.fFlags & kEmbolden_Flag) {
george@mozilla.comc59b5da2012-08-23 00:39:08 +00001246 emboldenOutline(fFace, &fFace->glyph->outline);
senorblanco@chromium.org4526a842010-02-05 23:08:20 +00001247 }
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001248 FT_Outline_Get_CBox(&fFace->glyph->outline, &bbox);
epoger@google.comb90113d2012-01-18 19:20:39 +00001249 x_height = SkFixedToScalar(SkFDot6ToFixed(bbox.yMax));
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001250 } else {
1251 x_height = 0;
1252 }
1253 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001254
1255 // convert upem-y values into scalar points
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001256 for (int i = 0; i < 6; i++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001257 SkFixed y = SkMulDiv(scaleY, ys[i], upem);
1258 SkFixed x = SkFixedMul(mxy, y);
1259 y = SkFixedMul(myy, y);
1260 pts[i].set(SkFixedToScalar(x), SkFixedToScalar(y));
1261 }
1262
1263 if (mx) {
1264 mx->fTop = pts[0].fX;
1265 mx->fAscent = pts[1].fX;
1266 mx->fDescent = pts[2].fX;
1267 mx->fBottom = pts[3].fX;
1268 mx->fLeading = pts[4].fX;
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001269 mx->fAvgCharWidth = pts[5].fX;
1270 mx->fXMin = xmin;
1271 mx->fXMax = xmax;
1272 mx->fXHeight = x_height;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001273 }
1274 if (my) {
1275 my->fTop = pts[0].fY;
1276 my->fAscent = pts[1].fY;
1277 my->fDescent = pts[2].fY;
1278 my->fBottom = pts[3].fY;
1279 my->fLeading = pts[4].fY;
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001280 my->fAvgCharWidth = pts[5].fY;
1281 my->fXMin = xmin;
1282 my->fXMax = xmax;
1283 my->fXHeight = x_height;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001284 }
1285}
1286
1287////////////////////////////////////////////////////////////////////////
1288////////////////////////////////////////////////////////////////////////
1289
1290SkScalerContext* SkFontHost::CreateScalerContext(const SkDescriptor* desc) {
reed@android.com62900b42009-02-11 15:07:19 +00001291 SkScalerContext_FreeType* c = SkNEW_ARGS(SkScalerContext_FreeType, (desc));
1292 if (!c->success()) {
1293 SkDELETE(c);
1294 c = NULL;
1295 }
1296 return c;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001297}
1298
1299///////////////////////////////////////////////////////////////////////////////
1300
1301/* Export this so that other parts of our FonttHost port can make use of our
1302 ability to extract the name+style from a stream, using FreeType's api.
1303*/
djsollen@google.com4dc686d2012-02-15 21:03:45 +00001304bool find_name_and_attributes(SkStream* stream, SkString* name,
1305 SkTypeface::Style* style, bool* isFixedWidth) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001306 FT_Library library;
reed@android.combfbd4ff2009-07-23 17:44:41 +00001307 if (FT_Init_FreeType(&library)) {
djsollen@google.com4dc686d2012-02-15 21:03:45 +00001308 return false;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001309 }
1310
1311 FT_Open_Args args;
1312 memset(&args, 0, sizeof(args));
1313
1314 const void* memoryBase = stream->getMemoryBase();
1315 FT_StreamRec streamRec;
1316
1317 if (NULL != memoryBase) {
1318 args.flags = FT_OPEN_MEMORY;
1319 args.memory_base = (const FT_Byte*)memoryBase;
1320 args.memory_size = stream->getLength();
1321 } else {
1322 memset(&streamRec, 0, sizeof(streamRec));
1323 streamRec.size = stream->read(NULL, 0);
1324 streamRec.descriptor.pointer = stream;
1325 streamRec.read = sk_stream_read;
1326 streamRec.close = sk_stream_close;
1327
1328 args.flags = FT_OPEN_STREAM;
1329 args.stream = &streamRec;
1330 }
1331
1332 FT_Face face;
1333 if (FT_Open_Face(library, &args, 0, &face)) {
1334 FT_Done_FreeType(library);
djsollen@google.com4dc686d2012-02-15 21:03:45 +00001335 return false;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001336 }
1337
djsollen@google.com4dc686d2012-02-15 21:03:45 +00001338 int tempStyle = SkTypeface::kNormal;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001339 if (face->style_flags & FT_STYLE_FLAG_BOLD) {
djsollen@google.com4dc686d2012-02-15 21:03:45 +00001340 tempStyle |= SkTypeface::kBold;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001341 }
1342 if (face->style_flags & FT_STYLE_FLAG_ITALIC) {
djsollen@google.com4dc686d2012-02-15 21:03:45 +00001343 tempStyle |= SkTypeface::kItalic;
1344 }
1345
1346 if (name) {
1347 name->set(face->family_name);
1348 }
1349 if (style) {
1350 *style = (SkTypeface::Style) tempStyle;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001351 }
reed@google.com5b31b0f2011-02-23 14:41:42 +00001352 if (isFixedWidth) {
1353 *isFixedWidth = FT_IS_FIXED_WIDTH(face);
1354 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001355
1356 FT_Done_Face(face);
1357 FT_Done_FreeType(library);
djsollen@google.com4dc686d2012-02-15 21:03:45 +00001358 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001359}