blob: 3a32ea40c4270ccced1ee2d013222a98230ef729 [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.com62566f32012-10-13 03:21:53 +000089/////////////////////////////////////////////////////////////////////////
90
bungeman@google.comfd668cf2012-08-24 17:46:11 +000091// FT_Library_SetLcdFilterWeights was introduced in FreeType 2.4.0.
92// The following platforms provide FreeType of at least 2.4.0.
93// Ubuntu >= 11.04 (previous deprecated April 2013)
94// Debian >= 6.0 (good)
95// OpenSuse >= 11.4 (previous deprecated January 2012 / Nov 2013 for Evergreen 11.2)
96// Fedora >= 14 (good)
97// Android >= Gingerbread (good)
98typedef FT_Error (*FT_Library_SetLcdFilterWeightsProc)(FT_Library, unsigned char*);
99
scroggo@google.com94bc60f2012-10-04 20:45:06 +0000100// Caller must lock gFTMutex before calling this function.
bungeman@google.comfd668cf2012-08-24 17:46:11 +0000101static bool InitFreetype() {
agl@chromium.org309485b2009-07-21 17:41:32 +0000102 FT_Error err = FT_Init_FreeType(&gFTLibrary);
reed@google.comea2333d2011-03-14 16:44:56 +0000103 if (err) {
agl@chromium.org309485b2009-07-21 17:41:32 +0000104 return false;
reed@google.comea2333d2011-03-14 16:44:56 +0000105 }
agl@chromium.org309485b2009-07-21 17:41:32 +0000106
bungeman@google.comfd668cf2012-08-24 17:46:11 +0000107 // Setup LCD filtering. This reduces color fringes for LCD smoothed glyphs.
epoger@google.comb371ed12011-06-29 21:20:52 +0000108#ifdef FT_LCD_FILTER_H
bungeman@google.com62566f32012-10-13 03:21:53 +0000109 // Use default { 0x10, 0x40, 0x70, 0x40, 0x10 }, as it adds up to 0x110, simulating ink spread.
110 // SetLcdFilter must be called before SetLcdFilterWeights.
111 err = FT_Library_SetLcdFilter(gFTLibrary, FT_LCD_FILTER_DEFAULT);
bungeman@google.comfd668cf2012-08-24 17:46:11 +0000112 if (0 == err) {
113 gLCDSupport = true;
114 gLCDExtra = 2; //Using a filter adds one full pixel to each side.
115
bungeman@google.com62566f32012-10-13 03:21:53 +0000116#ifdef SK_FONTHOST_FREETYPE_USE_NORMAL_LCD_FILTER
117 // This also adds to 0x110 simulating ink spread, but provides better results than default.
118 static unsigned char gGaussianLikeHeavyWeights[] = { 0x1A, 0x43, 0x56, 0x43, 0x1A, };
bungeman@google.comfd668cf2012-08-24 17:46:11 +0000119
120#if defined(SK_FONTHOST_FREETYPE_RUNTIME_VERSION) && \
121 SK_FONTHOST_FREETYPE_RUNTIME_VERSION > 0x020400
bungeman@google.com62566f32012-10-13 03:21:53 +0000122 err = FT_Library_SetLcdFilterWeights(gFTLibrary, gGaussianLikeHeavyWeights);
bungeman@google.comfd668cf2012-08-24 17:46:11 +0000123#elif defined(SK_CAN_USE_DLOPEN) && SK_CAN_USE_DLOPEN == 1
124 //The FreeType library is already loaded, so symbols are available in process.
125 void* self = dlopen(NULL, RTLD_LAZY);
126 if (NULL != self) {
127 FT_Library_SetLcdFilterWeightsProc setLcdFilterWeights;
128 //The following cast is non-standard, but safe for POSIX.
129 *reinterpret_cast<void**>(&setLcdFilterWeights) = dlsym(self, "FT_Library_SetLcdFilterWeights");
130 dlclose(self);
131
132 if (NULL != setLcdFilterWeights) {
bungeman@google.com62566f32012-10-13 03:21:53 +0000133 err = setLcdFilterWeights(gFTLibrary, gGaussianLikeHeavyWeights);
bungeman@google.comfd668cf2012-08-24 17:46:11 +0000134 }
135 }
136#endif
bungeman@google.com62566f32012-10-13 03:21:53 +0000137#endif
reed@google.coma1c32562012-03-01 19:38:23 +0000138 }
epoger@google.com5070d792011-06-29 20:43:14 +0000139#else
140 gLCDSupport = false;
141#endif
reed@android.com61608aa2009-07-31 14:52:54 +0000142 gLCDSupportValid = true;
agl@chromium.org309485b2009-07-21 17:41:32 +0000143
144 return true;
145}
146
reed@google.comfb2fdcc2012-10-17 15:49:36 +0000147// Lazy, once, wrapper to ask the FreeType Library if it can support LCD text
148static bool is_lcd_supported() {
149 if (!gLCDSupportValid) {
150 SkAutoMutexAcquire ac(gFTMutex);
151
152 if (!gLCDSupportValid) {
153 InitFreetype();
154 FT_Done_FreeType(gFTLibrary);
155 }
156 }
157 return gLCDSupport;
158}
159
george@mozilla.comc59b5da2012-08-23 00:39:08 +0000160class SkScalerContext_FreeType : public SkScalerContext_FreeType_Base {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000161public:
reed@google.com0da48612013-03-19 16:06:52 +0000162 SkScalerContext_FreeType(SkTypeface*, const SkDescriptor* desc);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000163 virtual ~SkScalerContext_FreeType();
agl@chromium.orgcc3096b2009-04-22 22:09:04 +0000164
reed@android.com62900b42009-02-11 15:07:19 +0000165 bool success() const {
reed@android.coma0f5d152009-06-22 17:38:10 +0000166 return fFaceRec != NULL &&
167 fFTSize != NULL &&
168 fFace != NULL;
reed@android.com62900b42009-02-11 15:07:19 +0000169 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000170
171protected:
bungeman@google.coma76de722012-10-26 19:35:54 +0000172 virtual unsigned generateGlyphCount() SK_OVERRIDE;
173 virtual uint16_t generateCharToGlyph(SkUnichar uni) SK_OVERRIDE;
174 virtual void generateAdvance(SkGlyph* glyph) SK_OVERRIDE;
175 virtual void generateMetrics(SkGlyph* glyph) SK_OVERRIDE;
176 virtual void generateImage(const SkGlyph& glyph) SK_OVERRIDE;
177 virtual void generatePath(const SkGlyph& glyph, SkPath* path) SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000178 virtual void generateFontMetrics(SkPaint::FontMetrics* mx,
bungeman@google.coma76de722012-10-26 19:35:54 +0000179 SkPaint::FontMetrics* my) SK_OVERRIDE;
180 virtual SkUnichar generateGlyphToChar(uint16_t glyph) SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000181
182private:
183 SkFaceRec* fFaceRec;
184 FT_Face fFace; // reference to shared face in gFaceRecHead
185 FT_Size fFTSize; // our own copy
186 SkFixed fScaleX, fScaleY;
187 FT_Matrix fMatrix22;
188 uint32_t fLoadGlyphFlags;
reed@google.combdc99882011-11-21 14:36:57 +0000189 bool fDoLinearMetrics;
reed@google.coma1bfa212012-03-08 21:57:12 +0000190 bool fLCDIsVert;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000191
reed@google.comf073b332013-05-06 12:21:16 +0000192 // Need scalar versions for generateFontMetrics
193 SkVector fScale;
194 SkMatrix fMatrix22Scalar;
195
reed@android.com8a1c16f2008-12-17 15:59:43 +0000196 FT_Error setupSize();
djsollen@google.comd8b599c2012-03-19 19:44:19 +0000197 void getBBoxForCurrentGlyph(SkGlyph* glyph, FT_BBox* bbox,
198 bool snapToPixelBoundary = false);
scroggo@google.com94bc60f2012-10-04 20:45:06 +0000199 // Caller must lock gFTMutex before calling this function.
djsollen@google.comd8b599c2012-03-19 19:44:19 +0000200 void updateGlyphIfLCD(SkGlyph* glyph);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000201};
202
203///////////////////////////////////////////////////////////////////////////
204///////////////////////////////////////////////////////////////////////////
205
reed@android.com8a1c16f2008-12-17 15:59:43 +0000206struct SkFaceRec {
207 SkFaceRec* fNext;
208 FT_Face fFace;
209 FT_StreamRec fFTStream;
210 SkStream* fSkStream;
211 uint32_t fRefCnt;
212 uint32_t fFontID;
213
reed@android.comb1d9d2e2009-03-04 17:37:51 +0000214 // assumes ownership of the stream, will call unref() when its done
reed@android.com8a1c16f2008-12-17 15:59:43 +0000215 SkFaceRec(SkStream* strm, uint32_t fontID);
216 ~SkFaceRec() {
reed@android.comb1d9d2e2009-03-04 17:37:51 +0000217 fSkStream->unref();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000218 }
219};
220
221extern "C" {
222 static unsigned long sk_stream_read(FT_Stream stream,
223 unsigned long offset,
224 unsigned char* buffer,
225 unsigned long count ) {
226 SkStream* str = (SkStream*)stream->descriptor.pointer;
227
228 if (count) {
229 if (!str->rewind()) {
230 return 0;
231 } else {
232 unsigned long ret;
233 if (offset) {
234 ret = str->read(NULL, offset);
235 if (ret != offset) {
236 return 0;
237 }
238 }
239 ret = str->read(buffer, count);
240 if (ret != count) {
241 return 0;
242 }
243 count = ret;
244 }
245 }
246 return count;
247 }
248
sugoi@google.com66a58ac2013-03-05 20:40:52 +0000249 static void sk_stream_close(FT_Stream) {}
reed@android.com8a1c16f2008-12-17 15:59:43 +0000250}
251
252SkFaceRec::SkFaceRec(SkStream* strm, uint32_t fontID)
vandebo@chromium.org9af25f32012-03-28 21:24:27 +0000253 : fNext(NULL), fSkStream(strm), fRefCnt(1), fFontID(fontID) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000254// SkDEBUGF(("SkFaceRec: opening %s (%p)\n", key.c_str(), strm));
255
reed@android.com4516f472009-06-29 16:25:36 +0000256 sk_bzero(&fFTStream, sizeof(fFTStream));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000257 fFTStream.size = fSkStream->getLength();
258 fFTStream.descriptor.pointer = fSkStream;
259 fFTStream.read = sk_stream_read;
260 fFTStream.close = sk_stream_close;
261}
262
reed@android.com62900b42009-02-11 15:07:19 +0000263// Will return 0 on failure
scroggo@google.com94bc60f2012-10-04 20:45:06 +0000264// Caller must lock gFTMutex before calling this function.
reed@google.com2cdc6712013-03-21 18:22:00 +0000265static SkFaceRec* ref_ft_face(const SkTypeface* typeface) {
266 const SkFontID fontID = typeface->uniqueID();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000267 SkFaceRec* rec = gFaceRecHead;
268 while (rec) {
269 if (rec->fFontID == fontID) {
270 SkASSERT(rec->fFace);
271 rec->fRefCnt += 1;
272 return rec;
273 }
274 rec = rec->fNext;
275 }
276
reed@google.com2cdc6712013-03-21 18:22:00 +0000277 int face_index;
278 SkStream* strm = typeface->openStream(&face_index);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000279 if (NULL == strm) {
reed@google.com2cdc6712013-03-21 18:22:00 +0000280 return NULL;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000281 }
282
283 // this passes ownership of strm to the rec
284 rec = SkNEW_ARGS(SkFaceRec, (strm, fontID));
285
286 FT_Open_Args args;
287 memset(&args, 0, sizeof(args));
288 const void* memoryBase = strm->getMemoryBase();
289
290 if (NULL != memoryBase) {
291//printf("mmap(%s)\n", keyString.c_str());
292 args.flags = FT_OPEN_MEMORY;
293 args.memory_base = (const FT_Byte*)memoryBase;
294 args.memory_size = strm->getLength();
295 } else {
296//printf("fopen(%s)\n", keyString.c_str());
297 args.flags = FT_OPEN_STREAM;
298 args.stream = &rec->fFTStream;
299 }
300
reed@google.com2cdc6712013-03-21 18:22:00 +0000301 FT_Error err = FT_Open_Face(gFTLibrary, &args, face_index, &rec->fFace);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000302 if (err) { // bad filename, try the default font
303 fprintf(stderr, "ERROR: unable to open font '%x'\n", fontID);
304 SkDELETE(rec);
reed@google.com2cdc6712013-03-21 18:22:00 +0000305 return NULL;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000306 } else {
307 SkASSERT(rec->fFace);
308 //fprintf(stderr, "Opened font '%s'\n", filename.c_str());
309 rec->fNext = gFaceRecHead;
310 gFaceRecHead = rec;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000311 return rec;
312 }
313}
314
scroggo@google.com94bc60f2012-10-04 20:45:06 +0000315// Caller must lock gFTMutex before calling this function.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000316static void unref_ft_face(FT_Face face) {
317 SkFaceRec* rec = gFaceRecHead;
318 SkFaceRec* prev = NULL;
319 while (rec) {
320 SkFaceRec* next = rec->fNext;
321 if (rec->fFace == face) {
322 if (--rec->fRefCnt == 0) {
323 if (prev) {
324 prev->fNext = next;
325 } else {
326 gFaceRecHead = next;
327 }
328 FT_Done_Face(face);
329 SkDELETE(rec);
330 }
331 return;
332 }
333 prev = rec;
334 rec = next;
335 }
tomhudson@google.com0c00f212011-12-28 14:59:50 +0000336 SkDEBUGFAIL("shouldn't get here, face not in list");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000337}
338
reed@google.comb4162b12013-07-02 16:32:29 +0000339class AutoFTAccess {
340public:
341 AutoFTAccess(const SkTypeface* tf) : fRec(NULL), fFace(NULL) {
342 gFTMutex.acquire();
343 if (1 == ++gFTCount) {
344 if (!InitFreetype()) {
345 sk_throw();
346 }
347 }
348 fRec = ref_ft_face(tf);
349 if (fRec) {
350 fFace = fRec->fFace;
351 }
352 }
353
354 ~AutoFTAccess() {
355 if (fFace) {
356 unref_ft_face(fFace);
357 }
358 if (0 == --gFTCount) {
359 FT_Done_FreeType(gFTLibrary);
360 }
361 gFTMutex.release();
362 }
363
364 SkFaceRec* rec() { return fRec; }
365 FT_Face face() { return fFace; }
366
367private:
368 SkFaceRec* fRec;
369 FT_Face fFace;
370};
371
reed@android.com8a1c16f2008-12-17 15:59:43 +0000372///////////////////////////////////////////////////////////////////////////
373
vandebo@chromium.org16be6b82011-01-28 21:28:56 +0000374// Work around for old versions of freetype.
375static FT_Error getAdvances(FT_Face face, FT_UInt start, FT_UInt count,
376 FT_Int32 loadFlags, FT_Fixed* advances) {
377#ifdef FT_ADVANCES_H
378 return FT_Get_Advances(face, start, count, loadFlags, advances);
379#else
380 if (!face || start >= face->num_glyphs ||
381 start + count > face->num_glyphs || loadFlags != FT_LOAD_NO_SCALE) {
382 return 6; // "Invalid argument."
383 }
384 if (count == 0)
385 return 0;
386
387 for (int i = 0; i < count; i++) {
388 FT_Error err = FT_Load_Glyph(face, start + i, FT_LOAD_NO_SCALE);
389 if (err)
390 return err;
391 advances[i] = face->glyph->advance.x;
392 }
393
394 return 0;
395#endif
396}
397
398static bool canEmbed(FT_Face face) {
djsollen@google.comfa394d42012-01-09 20:40:25 +0000399#ifdef FT_FSTYPE_RESTRICTED_LICENSE_EMBEDDING
vandebo@chromium.org16be6b82011-01-28 21:28:56 +0000400 FT_UShort fsType = FT_Get_FSType_Flags(face);
401 return (fsType & (FT_FSTYPE_RESTRICTED_LICENSE_EMBEDDING |
402 FT_FSTYPE_BITMAP_EMBEDDING_ONLY)) == 0;
403#else
404 // No embedding is 0x2 and bitmap embedding only is 0x200.
405 TT_OS2* os2_table;
406 if ((os2_table = (TT_OS2*)FT_Get_Sfnt_Table(face, ft_sfnt_os2)) != NULL) {
407 return (os2_table->fsType & 0x202) == 0;
408 }
409 return false; // We tried, fail safe.
410#endif
411}
412
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000413static bool GetLetterCBox(FT_Face face, char letter, FT_BBox* bbox) {
414 const FT_UInt glyph_id = FT_Get_Char_Index(face, letter);
415 if (!glyph_id)
416 return false;
417 FT_Load_Glyph(face, glyph_id, FT_LOAD_NO_SCALE);
418 FT_Outline_Get_CBox(&face->glyph->outline, bbox);
419 return true;
420}
421
vandebo@chromium.org6f72d1e2011-02-14 23:19:59 +0000422static bool getWidthAdvance(FT_Face face, int gId, int16_t* data) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000423 FT_Fixed advance = 0;
vandebo@chromium.org6f72d1e2011-02-14 23:19:59 +0000424 if (getAdvances(face, gId, 1, FT_LOAD_NO_SCALE, &advance)) {
425 return false;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000426 }
vandebo@chromium.org6f72d1e2011-02-14 23:19:59 +0000427 SkASSERT(data);
428 *data = advance;
429 return true;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000430}
431
vandebo@chromium.org6744d492011-05-09 18:13:47 +0000432static void populate_glyph_to_unicode(FT_Face& face,
433 SkTDArray<SkUnichar>* glyphToUnicode) {
434 // Check and see if we have Unicode cmaps.
435 for (int i = 0; i < face->num_charmaps; ++i) {
436 // CMaps known to support Unicode:
437 // Platform ID Encoding ID Name
438 // ----------- ----------- -----------------------------------
439 // 0 0,1 Apple Unicode
440 // 0 3 Apple Unicode 2.0 (preferred)
441 // 3 1 Microsoft Unicode UCS-2
442 // 3 10 Microsoft Unicode UCS-4 (preferred)
443 //
444 // See Apple TrueType Reference Manual
445 // http://developer.apple.com/fonts/TTRefMan/RM06/Chap6cmap.html
446 // http://developer.apple.com/fonts/TTRefMan/RM06/Chap6name.html#ID
447 // Microsoft OpenType Specification
448 // http://www.microsoft.com/typography/otspec/cmap.htm
449
450 FT_UShort platformId = face->charmaps[i]->platform_id;
451 FT_UShort encodingId = face->charmaps[i]->encoding_id;
452
453 if (platformId != 0 && platformId != 3) {
454 continue;
455 }
456 if (platformId == 3 && encodingId != 1 && encodingId != 10) {
457 continue;
458 }
459 bool preferredMap = ((platformId == 3 && encodingId == 10) ||
460 (platformId == 0 && encodingId == 3));
461
462 FT_Set_Charmap(face, face->charmaps[i]);
463 if (glyphToUnicode->isEmpty()) {
464 glyphToUnicode->setCount(face->num_glyphs);
465 memset(glyphToUnicode->begin(), 0,
466 sizeof(SkUnichar) * face->num_glyphs);
467 }
468
469 // Iterate through each cmap entry.
470 FT_UInt glyphIndex;
471 for (SkUnichar charCode = FT_Get_First_Char(face, &glyphIndex);
472 glyphIndex != 0;
473 charCode = FT_Get_Next_Char(face, charCode, &glyphIndex)) {
474 if (charCode &&
475 ((*glyphToUnicode)[glyphIndex] == 0 || preferredMap)) {
476 (*glyphToUnicode)[glyphIndex] = charCode;
477 }
478 }
479 }
480}
481
reed@google.com2689f612013-03-20 20:01:47 +0000482SkAdvancedTypefaceMetrics* SkTypeface_FreeType::onGetAdvancedTypefaceMetrics(
vandebo@chromium.org37ad8fb2011-08-18 02:38:50 +0000483 SkAdvancedTypefaceMetrics::PerGlyphInfo perGlyphInfo,
484 const uint32_t* glyphIDs,
reed@google.com2689f612013-03-20 20:01:47 +0000485 uint32_t glyphIDsCount) const {
djsollen@google.comda957722011-11-16 17:00:46 +0000486#if defined(SK_BUILD_FOR_MAC)
reed@google.com8a5d6922011-03-14 15:08:03 +0000487 return NULL;
488#else
reed@google.comb4162b12013-07-02 16:32:29 +0000489 AutoFTAccess fta(this);
490 FT_Face face = fta.face();
491 if (!face) {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000492 return NULL;
reed@google.comb4162b12013-07-02 16:32:29 +0000493 }
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000494
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000495 SkAdvancedTypefaceMetrics* info = new SkAdvancedTypefaceMetrics;
496 info->fFontName.set(FT_Get_Postscript_Name(face));
497 info->fMultiMaster = FT_HAS_MULTIPLE_MASTERS(face);
498 info->fLastGlyphID = face->num_glyphs - 1;
499 info->fEmSize = 1000;
500
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000501 bool cid = false;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000502 const char* fontType = FT_Get_X11_Font_Format(face);
vandebo@chromium.orgc3a2ae52011-02-03 21:48:23 +0000503 if (strcmp(fontType, "Type 1") == 0) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000504 info->fType = SkAdvancedTypefaceMetrics::kType1_Font;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000505 } else if (strcmp(fontType, "CID Type 1") == 0) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000506 info->fType = SkAdvancedTypefaceMetrics::kType1CID_Font;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000507 cid = true;
508 } else if (strcmp(fontType, "CFF") == 0) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000509 info->fType = SkAdvancedTypefaceMetrics::kCFF_Font;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000510 } else if (strcmp(fontType, "TrueType") == 0) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000511 info->fType = SkAdvancedTypefaceMetrics::kTrueType_Font;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000512 cid = true;
513 TT_Header* ttHeader;
514 if ((ttHeader = (TT_Header*)FT_Get_Sfnt_Table(face,
515 ft_sfnt_head)) != NULL) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000516 info->fEmSize = ttHeader->Units_Per_EM;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000517 }
518 }
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000519
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000520 info->fStyle = 0;
521 if (FT_IS_FIXED_WIDTH(face))
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000522 info->fStyle |= SkAdvancedTypefaceMetrics::kFixedPitch_Style;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000523 if (face->style_flags & FT_STYLE_FLAG_ITALIC)
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000524 info->fStyle |= SkAdvancedTypefaceMetrics::kItalic_Style;
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000525
526 PS_FontInfoRec ps_info;
527 TT_Postscript* tt_info;
528 if (FT_Get_PS_Font_Info(face, &ps_info) == 0) {
529 info->fItalicAngle = ps_info.italic_angle;
530 } else if ((tt_info =
531 (TT_Postscript*)FT_Get_Sfnt_Table(face,
532 ft_sfnt_post)) != NULL) {
533 info->fItalicAngle = SkFixedToScalar(tt_info->italicAngle);
534 } else {
535 info->fItalicAngle = 0;
536 }
537
538 info->fAscent = face->ascender;
539 info->fDescent = face->descender;
540
541 // Figure out a good guess for StemV - Min width of i, I, !, 1.
542 // This probably isn't very good with an italic font.
543 int16_t min_width = SHRT_MAX;
vandebo@chromium.org6f72d1e2011-02-14 23:19:59 +0000544 info->fStemV = 0;
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000545 char stem_chars[] = {'i', 'I', '!', '1'};
546 for (size_t i = 0; i < SK_ARRAY_COUNT(stem_chars); i++) {
547 FT_BBox bbox;
548 if (GetLetterCBox(face, stem_chars[i], &bbox)) {
549 int16_t width = bbox.xMax - bbox.xMin;
550 if (width > 0 && width < min_width) {
551 min_width = width;
552 info->fStemV = min_width;
553 }
554 }
555 }
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000556
557 TT_PCLT* pclt_info;
558 TT_OS2* os2_table;
559 if ((pclt_info = (TT_PCLT*)FT_Get_Sfnt_Table(face, ft_sfnt_pclt)) != NULL) {
560 info->fCapHeight = pclt_info->CapHeight;
561 uint8_t serif_style = pclt_info->SerifStyle & 0x3F;
562 if (serif_style >= 2 && serif_style <= 6)
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000563 info->fStyle |= SkAdvancedTypefaceMetrics::kSerif_Style;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000564 else if (serif_style >= 9 && serif_style <= 12)
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000565 info->fStyle |= SkAdvancedTypefaceMetrics::kScript_Style;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000566 } else if ((os2_table =
567 (TT_OS2*)FT_Get_Sfnt_Table(face, ft_sfnt_os2)) != NULL) {
568 info->fCapHeight = os2_table->sCapHeight;
569 } else {
570 // Figure out a good guess for CapHeight: average the height of M and X.
571 FT_BBox m_bbox, x_bbox;
572 bool got_m, got_x;
573 got_m = GetLetterCBox(face, 'M', &m_bbox);
574 got_x = GetLetterCBox(face, 'X', &x_bbox);
575 if (got_m && got_x) {
576 info->fCapHeight = (m_bbox.yMax - m_bbox.yMin + x_bbox.yMax -
577 x_bbox.yMin) / 2;
578 } else if (got_m && !got_x) {
579 info->fCapHeight = m_bbox.yMax - m_bbox.yMin;
580 } else if (!got_m && got_x) {
581 info->fCapHeight = x_bbox.yMax - x_bbox.yMin;
582 }
583 }
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000584
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000585 info->fBBox = SkIRect::MakeLTRB(face->bbox.xMin, face->bbox.yMax,
586 face->bbox.xMax, face->bbox.yMin);
587
vandebo@chromium.org37ad8fb2011-08-18 02:38:50 +0000588 if (!canEmbed(face) || !FT_IS_SCALABLE(face) ||
vandebo@chromium.org325cb9a2011-03-30 18:36:29 +0000589 info->fType == SkAdvancedTypefaceMetrics::kOther_Font) {
590 perGlyphInfo = SkAdvancedTypefaceMetrics::kNo_PerGlyphInfo;
591 }
592
593 if (perGlyphInfo & SkAdvancedTypefaceMetrics::kHAdvance_PerGlyphInfo) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000594 if (FT_IS_FIXED_WIDTH(face)) {
595 appendRange(&info->fGlyphWidths, 0);
596 int16_t advance = face->max_advance_width;
597 info->fGlyphWidths->fAdvance.append(1, &advance);
598 finishRange(info->fGlyphWidths.get(), 0,
599 SkAdvancedTypefaceMetrics::WidthRange::kDefault);
vandebo@chromium.org6f72d1e2011-02-14 23:19:59 +0000600 } else if (!cid) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000601 appendRange(&info->fGlyphWidths, 0);
602 // So as to not blow out the stack, get advances in batches.
603 for (int gID = 0; gID < face->num_glyphs; gID += 128) {
604 FT_Fixed advances[128];
605 int advanceCount = 128;
606 if (gID + advanceCount > face->num_glyphs)
607 advanceCount = face->num_glyphs - gID + 1;
608 getAdvances(face, gID, advanceCount, FT_LOAD_NO_SCALE,
609 advances);
610 for (int i = 0; i < advanceCount; i++) {
vandebo@chromium.orgce8a1952012-10-22 20:09:31 +0000611 int16_t advance = advances[i];
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000612 info->fGlyphWidths->fAdvance.append(1, &advance);
613 }
614 }
615 finishRange(info->fGlyphWidths.get(), face->num_glyphs - 1,
616 SkAdvancedTypefaceMetrics::WidthRange::kRange);
617 } else {
vandebo@chromium.org6f72d1e2011-02-14 23:19:59 +0000618 info->fGlyphWidths.reset(
vandebo@chromium.org37ad8fb2011-08-18 02:38:50 +0000619 getAdvanceData(face,
620 face->num_glyphs,
621 glyphIDs,
622 glyphIDsCount,
623 &getWidthAdvance));
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000624 }
vandebo@chromium.org325cb9a2011-03-30 18:36:29 +0000625 }
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000626
vandebo@chromium.org325cb9a2011-03-30 18:36:29 +0000627 if (perGlyphInfo & SkAdvancedTypefaceMetrics::kVAdvance_PerGlyphInfo &&
628 FT_HAS_VERTICAL(face)) {
629 SkASSERT(false); // Not implemented yet.
630 }
631
632 if (perGlyphInfo & SkAdvancedTypefaceMetrics::kGlyphNames_PerGlyphInfo &&
633 info->fType == SkAdvancedTypefaceMetrics::kType1_Font) {
634 // Postscript fonts may contain more than 255 glyphs, so we end up
635 // using multiple font descriptions with a glyph ordering. Record
636 // the name of each glyph.
637 info->fGlyphNames.reset(
638 new SkAutoTArray<SkString>(face->num_glyphs));
639 for (int gID = 0; gID < face->num_glyphs; gID++) {
640 char glyphName[128]; // PS limit for names is 127 bytes.
641 FT_Get_Glyph_Name(face, gID, glyphName, 128);
642 info->fGlyphNames->get()[gID].set(glyphName);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000643 }
644 }
645
vandebo@chromium.org6744d492011-05-09 18:13:47 +0000646 if (perGlyphInfo & SkAdvancedTypefaceMetrics::kToUnicode_PerGlyphInfo &&
647 info->fType != SkAdvancedTypefaceMetrics::kType1_Font &&
648 face->num_charmaps) {
649 populate_glyph_to_unicode(face, &(info->fGlyphToUnicode));
650 }
651
vandebo@chromium.orgc3a2ae52011-02-03 21:48:23 +0000652 if (!canEmbed(face))
653 info->fType = SkAdvancedTypefaceMetrics::kNotEmbeddable_Font;
654
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000655 return info;
reed@google.com8a5d6922011-03-14 15:08:03 +0000656#endif
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000657}
vandebo@chromium.org37ad8fb2011-08-18 02:38:50 +0000658
reed@google.com618ef5e2011-01-26 22:10:41 +0000659///////////////////////////////////////////////////////////////////////////
660
reed@google.comffe49f52011-11-22 19:42:41 +0000661#define BLACK_LUMINANCE_LIMIT 0x40
662#define WHITE_LUMINANCE_LIMIT 0xA0
663
reed@google.com8ed436c2011-07-21 14:12:36 +0000664static bool bothZero(SkScalar a, SkScalar b) {
665 return 0 == a && 0 == b;
666}
667
668// returns false if there is any non-90-rotation or skew
669static bool isAxisAligned(const SkScalerContext::Rec& rec) {
670 return 0 == rec.fPreSkewX &&
671 (bothZero(rec.fPost2x2[0][1], rec.fPost2x2[1][0]) ||
672 bothZero(rec.fPost2x2[0][0], rec.fPost2x2[1][1]));
673}
674
reed@google.com0da48612013-03-19 16:06:52 +0000675SkScalerContext* SkTypeface_FreeType::onCreateScalerContext(
676 const SkDescriptor* desc) const {
677 SkScalerContext_FreeType* c = SkNEW_ARGS(SkScalerContext_FreeType,
678 (const_cast<SkTypeface_FreeType*>(this),
679 desc));
680 if (!c->success()) {
681 SkDELETE(c);
682 c = NULL;
683 }
684 return c;
685}
686
687void SkTypeface_FreeType::onFilterRec(SkScalerContextRec* rec) const {
bungeman@google.com8cf32262012-04-02 14:34:30 +0000688 //BOGUS: http://code.google.com/p/chromium/issues/detail?id=121119
689 //Cap the requested size as larger sizes give bogus values.
690 //Remove when http://code.google.com/p/skia/issues/detail?id=554 is fixed.
bungeman@google.com5582e632012-04-02 14:51:54 +0000691 if (rec->fTextSize > SkIntToScalar(1 << 14)) {
scroggo@google.com94bc60f2012-10-04 20:45:06 +0000692 rec->fTextSize = SkIntToScalar(1 << 14);
bungeman@google.com8cf32262012-04-02 14:34:30 +0000693 }
skia.committer@gmail.coma27096b2012-08-30 14:38:00 +0000694
reed@google.comfb2fdcc2012-10-17 15:49:36 +0000695 if (!is_lcd_supported() && isLCD(*rec)) {
reed@google.com618ef5e2011-01-26 22:10:41 +0000696 // If the runtime Freetype library doesn't support LCD mode, we disable
697 // it here.
698 rec->fMaskFormat = SkMask::kA8_Format;
699 }
reed@google.com5b31b0f2011-02-23 14:41:42 +0000700
reed@google.com618ef5e2011-01-26 22:10:41 +0000701 SkPaint::Hinting h = rec->getHinting();
reed@google.comeffc5012011-06-27 16:44:46 +0000702 if (SkPaint::kFull_Hinting == h && !isLCD(*rec)) {
reed@google.com618ef5e2011-01-26 22:10:41 +0000703 // collapse full->normal hinting if we're not doing LCD
704 h = SkPaint::kNormal_Hinting;
reed@google.com618ef5e2011-01-26 22:10:41 +0000705 }
bungeman@google.comf4f2b802012-03-08 19:19:51 +0000706 if ((rec->fFlags & SkScalerContext::kSubpixelPositioning_Flag)) {
reed@google.com1ac83502012-02-28 17:06:02 +0000707 if (SkPaint::kNo_Hinting != h) {
708 h = SkPaint::kSlight_Hinting;
709 }
710 }
711
reed@google.com8ed436c2011-07-21 14:12:36 +0000712 // rotated text looks bad with hinting, so we disable it as needed
713 if (!isAxisAligned(*rec)) {
714 h = SkPaint::kNo_Hinting;
715 }
reed@google.com618ef5e2011-01-26 22:10:41 +0000716 rec->setHinting(h);
reed@google.comffe49f52011-11-22 19:42:41 +0000717
bungeman@google.com97efada2012-07-30 20:40:50 +0000718#ifndef SK_GAMMA_APPLY_TO_A8
719 if (!isLCD(*rec)) {
720 rec->ignorePreBlend();
reed@google.comffe49f52011-11-22 19:42:41 +0000721 }
reed@google.com1ac83502012-02-28 17:06:02 +0000722#endif
reed@google.com618ef5e2011-01-26 22:10:41 +0000723}
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000724
reed@google.com38c37dd2013-03-21 15:36:26 +0000725int SkTypeface_FreeType::onGetUPEM() const {
reed@google.comb4162b12013-07-02 16:32:29 +0000726 AutoFTAccess fta(this);
727 FT_Face face = fta.face();
728 return face ? face->units_per_EM : 0;
djsollen@google.comcd9d69b2011-03-14 20:30:14 +0000729}
djsollen@google.comcd9d69b2011-03-14 20:30:14 +0000730
reed@google.com0da48612013-03-19 16:06:52 +0000731SkScalerContext_FreeType::SkScalerContext_FreeType(SkTypeface* typeface,
732 const SkDescriptor* desc)
733 : SkScalerContext_FreeType_Base(typeface, desc) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000734 SkAutoMutexAcquire ac(gFTMutex);
735
reed@android.com8a1c16f2008-12-17 15:59:43 +0000736 if (gFTCount == 0) {
reed@android.com659aaf92009-07-23 15:20:21 +0000737 if (!InitFreetype()) {
738 sk_throw();
739 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000740 }
741 ++gFTCount;
742
743 // load the font file
reed@android.com62900b42009-02-11 15:07:19 +0000744 fFTSize = NULL;
745 fFace = NULL;
reed@google.com2cdc6712013-03-21 18:22:00 +0000746 fFaceRec = ref_ft_face(typeface);
reed@android.com62900b42009-02-11 15:07:19 +0000747 if (NULL == fFaceRec) {
748 return;
749 }
750 fFace = fFaceRec->fFace;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000751
752 // compute our factors from the record
753
754 SkMatrix m;
755
756 fRec.getSingleMatrix(&m);
757
758#ifdef DUMP_STRIKE_CREATION
759 SkString keyString;
760 SkFontHost::GetDescriptorKeyString(desc, &keyString);
761 printf("========== strike [%g %g %g] [%g %g %g %g] hints %d format %d %s\n", SkScalarToFloat(fRec.fTextSize),
762 SkScalarToFloat(fRec.fPreScaleX), SkScalarToFloat(fRec.fPreSkewX),
763 SkScalarToFloat(fRec.fPost2x2[0][0]), SkScalarToFloat(fRec.fPost2x2[0][1]),
764 SkScalarToFloat(fRec.fPost2x2[1][0]), SkScalarToFloat(fRec.fPost2x2[1][1]),
agl@chromium.org309485b2009-07-21 17:41:32 +0000765 fRec.getHinting(), fRec.fMaskFormat, keyString.c_str());
reed@android.com8a1c16f2008-12-17 15:59:43 +0000766#endif
767
768 // now compute our scale factors
769 SkScalar sx = m.getScaleX();
770 SkScalar sy = m.getScaleY();
771
reed@google.comf073b332013-05-06 12:21:16 +0000772 fMatrix22Scalar.reset();
773
reed@android.com8a1c16f2008-12-17 15:59:43 +0000774 if (m.getSkewX() || m.getSkewY() || sx < 0 || sy < 0) {
775 // sort of give up on hinting
776 sx = SkMaxScalar(SkScalarAbs(sx), SkScalarAbs(m.getSkewX()));
777 sy = SkMaxScalar(SkScalarAbs(m.getSkewY()), SkScalarAbs(sy));
778 sx = sy = SkScalarAve(sx, sy);
779
780 SkScalar inv = SkScalarInvert(sx);
781
782 // flip the skew elements to go from our Y-down system to FreeType's
783 fMatrix22.xx = SkScalarToFixed(SkScalarMul(m.getScaleX(), inv));
784 fMatrix22.xy = -SkScalarToFixed(SkScalarMul(m.getSkewX(), inv));
785 fMatrix22.yx = -SkScalarToFixed(SkScalarMul(m.getSkewY(), inv));
786 fMatrix22.yy = SkScalarToFixed(SkScalarMul(m.getScaleY(), inv));
skia.committer@gmail.come944de72013-05-07 07:01:03 +0000787
reed@google.comf073b332013-05-06 12:21:16 +0000788 fMatrix22Scalar.setScaleX(SkScalarMul(m.getScaleX(), inv));
789 fMatrix22Scalar.setSkewX(-SkScalarMul(m.getSkewX(), inv));
790 fMatrix22Scalar.setSkewY(-SkScalarMul(m.getSkewY(), inv));
791 fMatrix22Scalar.setScaleY(SkScalarMul(m.getScaleY(), inv));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000792 } else {
793 fMatrix22.xx = fMatrix22.yy = SK_Fixed1;
794 fMatrix22.xy = fMatrix22.yx = 0;
795 }
796
reed@google.com1f1543f2012-09-12 21:08:33 +0000797#ifdef SK_SUPPORT_HINTING_SCALE_FACTOR
reed@google.com9548be32012-09-14 16:00:01 +0000798 if (fRec.getHinting() == SkPaint::kNo_Hinting) {
reed@google.comf073b332013-05-06 12:21:16 +0000799 fScale.set(sx, sy);
reed@google.com9548be32012-09-14 16:00:01 +0000800 fScaleX = SkScalarToFixed(sx);
801 fScaleY = SkScalarToFixed(sy);
802 } else {
803 SkScalar hintingScaleFactor = fRec.fHintingScaleFactor;
reed@google.com1f1543f2012-09-12 21:08:33 +0000804
reed@google.comf073b332013-05-06 12:21:16 +0000805 fScale.set(sx / hintingScaleFactor, sy / hintingScaleFactor);
806 fScaleX = SkScalarToFixed(fScale.fX);
807 fScaleY = SkScalarToFixed(fScale.fY);
reed@google.com1f1543f2012-09-12 21:08:33 +0000808
reed@google.com9548be32012-09-14 16:00:01 +0000809 fMatrix22.xx *= hintingScaleFactor;
810 fMatrix22.xy *= hintingScaleFactor;
811 fMatrix22.yx *= hintingScaleFactor;
812 fMatrix22.yy *= hintingScaleFactor;
reed@google.comf073b332013-05-06 12:21:16 +0000813
814 fMatrix22Scalar.setScaleX(fMatrix22Scalar.getScaleX() * hintingScaleFactor);
815 fMatrix22Scalar.setSkewX(fMatrix22Scalar..getSkewX() * hintingScaleFactor);
816 fMatrix22Scalar.setSkewY(fMatrix22Scalar..getSkewY() * hintingScaleFactor);
817 fMatrix22Scalar.setScaleY(fMatrix22Scalar..getScaleY() * hintingScaleFactor);
reed@google.com9548be32012-09-14 16:00:01 +0000818 }
reed@google.com1f1543f2012-09-12 21:08:33 +0000819#else
reed@google.comf073b332013-05-06 12:21:16 +0000820 fScale.set(sx, sy);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000821 fScaleX = SkScalarToFixed(sx);
822 fScaleY = SkScalarToFixed(sy);
reed@google.com1f1543f2012-09-12 21:08:33 +0000823#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000824
reed@google.coma1bfa212012-03-08 21:57:12 +0000825 fLCDIsVert = SkToBool(fRec.fFlags & SkScalerContext::kLCD_Vertical_Flag);
826
reed@android.com8a1c16f2008-12-17 15:59:43 +0000827 // compute the flags we send to Load_Glyph
828 {
reed@android.come4d0bc02009-07-24 19:53:20 +0000829 FT_Int32 loadFlags = FT_LOAD_DEFAULT;
bungeman@google.com34f10262012-03-23 18:11:47 +0000830 bool linearMetrics = SkToBool(fRec.fFlags & SkScalerContext::kSubpixelPositioning_Flag);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000831
agl@chromium.org70a303f2010-05-10 14:15:50 +0000832 if (SkMask::kBW_Format == fRec.fMaskFormat) {
833 // See http://code.google.com/p/chromium/issues/detail?id=43252#c24
834 loadFlags = FT_LOAD_TARGET_MONO;
reed@google.comeffc5012011-06-27 16:44:46 +0000835 if (fRec.getHinting() == SkPaint::kNo_Hinting) {
agl@chromium.org70a303f2010-05-10 14:15:50 +0000836 loadFlags = FT_LOAD_NO_HINTING;
reed@google.combdc99882011-11-21 14:36:57 +0000837 linearMetrics = true;
reed@google.comeffc5012011-06-27 16:44:46 +0000838 }
agl@chromium.org70a303f2010-05-10 14:15:50 +0000839 } else {
840 switch (fRec.getHinting()) {
841 case SkPaint::kNo_Hinting:
842 loadFlags = FT_LOAD_NO_HINTING;
reed@google.combdc99882011-11-21 14:36:57 +0000843 linearMetrics = true;
agl@chromium.org70a303f2010-05-10 14:15:50 +0000844 break;
845 case SkPaint::kSlight_Hinting:
846 loadFlags = FT_LOAD_TARGET_LIGHT; // This implies FORCE_AUTOHINT
847 break;
848 case SkPaint::kNormal_Hinting:
agl@chromium.orga2c71cb2010-06-17 20:49:17 +0000849 if (fRec.fFlags & SkScalerContext::kAutohinting_Flag)
850 loadFlags = FT_LOAD_FORCE_AUTOHINT;
851 else
852 loadFlags = FT_LOAD_NO_AUTOHINT;
agl@chromium.org70a303f2010-05-10 14:15:50 +0000853 break;
854 case SkPaint::kFull_Hinting:
agl@chromium.orga2c71cb2010-06-17 20:49:17 +0000855 if (fRec.fFlags & SkScalerContext::kAutohinting_Flag) {
856 loadFlags = FT_LOAD_FORCE_AUTOHINT;
857 break;
858 }
agl@chromium.org70a303f2010-05-10 14:15:50 +0000859 loadFlags = FT_LOAD_TARGET_NORMAL;
reed@google.comeffc5012011-06-27 16:44:46 +0000860 if (isLCD(fRec)) {
reed@google.coma1bfa212012-03-08 21:57:12 +0000861 if (fLCDIsVert) {
reed@google.comeffc5012011-06-27 16:44:46 +0000862 loadFlags = FT_LOAD_TARGET_LCD_V;
863 } else {
864 loadFlags = FT_LOAD_TARGET_LCD;
865 }
reed@google.comea2333d2011-03-14 16:44:56 +0000866 }
agl@chromium.org70a303f2010-05-10 14:15:50 +0000867 break;
868 default:
869 SkDebugf("---------- UNKNOWN hinting %d\n", fRec.getHinting());
870 break;
871 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000872 }
873
reed@google.comeffc5012011-06-27 16:44:46 +0000874 if ((fRec.fFlags & SkScalerContext::kEmbeddedBitmapText_Flag) == 0) {
agl@chromium.orge0d08992009-08-07 19:19:23 +0000875 loadFlags |= FT_LOAD_NO_BITMAP;
reed@google.comeffc5012011-06-27 16:44:46 +0000876 }
agl@chromium.orge0d08992009-08-07 19:19:23 +0000877
reed@google.com96a9f7912011-05-06 11:49:30 +0000878 // Always using FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH to get correct
879 // advances, as fontconfig and cairo do.
880 // See http://code.google.com/p/skia/issues/detail?id=222.
881 loadFlags |= FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH;
882
bungeman@google.com8ff8a192012-09-25 20:38:28 +0000883 // Use vertical layout if requested.
884 if (fRec.fFlags & SkScalerContext::kVertical_Flag) {
885 loadFlags |= FT_LOAD_VERTICAL_LAYOUT;
886 }
887
reed@android.come4d0bc02009-07-24 19:53:20 +0000888 fLoadGlyphFlags = loadFlags;
reed@google.combdc99882011-11-21 14:36:57 +0000889 fDoLinearMetrics = linearMetrics;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000890 }
891
892 // now create the FT_Size
893
894 {
895 FT_Error err;
896
897 err = FT_New_Size(fFace, &fFTSize);
898 if (err != 0) {
899 SkDEBUGF(("SkScalerContext_FreeType::FT_New_Size(%x): FT_Set_Char_Size(0x%x, 0x%x) returned 0x%x\n",
900 fFaceRec->fFontID, fScaleX, fScaleY, err));
901 fFace = NULL;
902 return;
903 }
904
905 err = FT_Activate_Size(fFTSize);
906 if (err != 0) {
907 SkDEBUGF(("SkScalerContext_FreeType::FT_Activate_Size(%x, 0x%x, 0x%x) returned 0x%x\n",
908 fFaceRec->fFontID, fScaleX, fScaleY, err));
909 fFTSize = NULL;
910 }
911
912 err = FT_Set_Char_Size( fFace,
913 SkFixedToFDot6(fScaleX), SkFixedToFDot6(fScaleY),
914 72, 72);
915 if (err != 0) {
916 SkDEBUGF(("SkScalerContext_FreeType::FT_Set_Char_Size(%x, 0x%x, 0x%x) returned 0x%x\n",
917 fFaceRec->fFontID, fScaleX, fScaleY, err));
918 fFace = NULL;
919 return;
920 }
921
922 FT_Set_Transform( fFace, &fMatrix22, NULL);
923 }
924}
925
926SkScalerContext_FreeType::~SkScalerContext_FreeType() {
scroggo@google.com94bc60f2012-10-04 20:45:06 +0000927 SkAutoMutexAcquire ac(gFTMutex);
928
reed@android.com8a1c16f2008-12-17 15:59:43 +0000929 if (fFTSize != NULL) {
930 FT_Done_Size(fFTSize);
931 }
932
reed@android.com8a1c16f2008-12-17 15:59:43 +0000933 if (fFace != NULL) {
934 unref_ft_face(fFace);
935 }
936 if (--gFTCount == 0) {
937// SkDEBUGF(("FT_Done_FreeType\n"));
938 FT_Done_FreeType(gFTLibrary);
939 SkDEBUGCODE(gFTLibrary = NULL;)
940 }
941}
942
943/* We call this before each use of the fFace, since we may be sharing
944 this face with other context (at different sizes).
945*/
946FT_Error SkScalerContext_FreeType::setupSize() {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000947 FT_Error err = FT_Activate_Size(fFTSize);
948
949 if (err != 0) {
950 SkDEBUGF(("SkScalerContext_FreeType::FT_Activate_Size(%x, 0x%x, 0x%x) returned 0x%x\n",
951 fFaceRec->fFontID, fScaleX, fScaleY, err));
952 fFTSize = NULL;
953 } else {
954 // seems we need to reset this every time (not sure why, but without it
955 // I get random italics from some other fFTSize)
956 FT_Set_Transform( fFace, &fMatrix22, NULL);
957 }
958 return err;
959}
960
ctguil@chromium.org0bc7bf52011-03-04 19:04:57 +0000961unsigned SkScalerContext_FreeType::generateGlyphCount() {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000962 return fFace->num_glyphs;
963}
964
965uint16_t SkScalerContext_FreeType::generateCharToGlyph(SkUnichar uni) {
966 return SkToU16(FT_Get_Char_Index( fFace, uni ));
967}
968
reed@android.com9d3a9852010-01-08 14:07:42 +0000969SkUnichar SkScalerContext_FreeType::generateGlyphToChar(uint16_t glyph) {
970 // iterate through each cmap entry, looking for matching glyph indices
971 FT_UInt glyphIndex;
972 SkUnichar charCode = FT_Get_First_Char( fFace, &glyphIndex );
973
974 while (glyphIndex != 0) {
975 if (glyphIndex == glyph) {
976 return charCode;
977 }
978 charCode = FT_Get_Next_Char( fFace, charCode, &glyphIndex );
979 }
980
981 return 0;
982}
983
reed@android.com8a1c16f2008-12-17 15:59:43 +0000984void SkScalerContext_FreeType::generateAdvance(SkGlyph* glyph) {
985#ifdef FT_ADVANCES_H
986 /* unhinted and light hinted text have linearly scaled advances
987 * which are very cheap to compute with some font formats...
988 */
reed@google.combdc99882011-11-21 14:36:57 +0000989 if (fDoLinearMetrics) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000990 SkAutoMutexAcquire ac(gFTMutex);
991
992 if (this->setupSize()) {
reed@android.com62900b42009-02-11 15:07:19 +0000993 glyph->zeroMetrics();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000994 return;
995 }
996
997 FT_Error error;
998 FT_Fixed advance;
999
1000 error = FT_Get_Advance( fFace, glyph->getGlyphID(fBaseGlyphCount),
1001 fLoadGlyphFlags | FT_ADVANCE_FLAG_FAST_ONLY,
1002 &advance );
1003 if (0 == error) {
1004 glyph->fRsbDelta = 0;
1005 glyph->fLsbDelta = 0;
reed@google.comd074c372012-07-18 13:45:58 +00001006 glyph->fAdvanceX = SkFixedMul(fMatrix22.xx, advance);
1007 glyph->fAdvanceY = - SkFixedMul(fMatrix22.yx, advance);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001008 return;
1009 }
1010 }
1011#endif /* FT_ADVANCES_H */
1012 /* otherwise, we need to load/hint the glyph, which is slower */
1013 this->generateMetrics(glyph);
1014 return;
1015}
1016
djsollen@google.comd8b599c2012-03-19 19:44:19 +00001017void SkScalerContext_FreeType::getBBoxForCurrentGlyph(SkGlyph* glyph,
1018 FT_BBox* bbox,
1019 bool snapToPixelBoundary) {
1020
1021 FT_Outline_Get_CBox(&fFace->glyph->outline, bbox);
1022
1023 if (fRec.fFlags & SkScalerContext::kSubpixelPositioning_Flag) {
george@mozilla.comc59b5da2012-08-23 00:39:08 +00001024 int dx = SkFixedToFDot6(glyph->getSubXFixed());
1025 int dy = SkFixedToFDot6(glyph->getSubYFixed());
djsollen@google.comd8b599c2012-03-19 19:44:19 +00001026 // negate dy since freetype-y-goes-up and skia-y-goes-down
1027 bbox->xMin += dx;
1028 bbox->yMin -= dy;
1029 bbox->xMax += dx;
1030 bbox->yMax -= dy;
1031 }
1032
1033 // outset the box to integral boundaries
1034 if (snapToPixelBoundary) {
1035 bbox->xMin &= ~63;
1036 bbox->yMin &= ~63;
1037 bbox->xMax = (bbox->xMax + 63) & ~63;
1038 bbox->yMax = (bbox->yMax + 63) & ~63;
1039 }
bungeman@google.com8ff8a192012-09-25 20:38:28 +00001040
1041 // Must come after snapToPixelBoundary so that the width and height are
1042 // consistent. Otherwise asserts will fire later on when generating the
1043 // glyph image.
1044 if (fRec.fFlags & SkScalerContext::kVertical_Flag) {
1045 FT_Vector vector;
1046 vector.x = fFace->glyph->metrics.vertBearingX - fFace->glyph->metrics.horiBearingX;
1047 vector.y = -fFace->glyph->metrics.vertBearingY - fFace->glyph->metrics.horiBearingY;
1048 FT_Vector_Transform(&vector, &fMatrix22);
1049 bbox->xMin += vector.x;
1050 bbox->xMax += vector.x;
1051 bbox->yMin += vector.y;
1052 bbox->yMax += vector.y;
1053 }
djsollen@google.comd8b599c2012-03-19 19:44:19 +00001054}
1055
1056void SkScalerContext_FreeType::updateGlyphIfLCD(SkGlyph* glyph) {
1057 if (isLCD(fRec)) {
1058 if (fLCDIsVert) {
1059 glyph->fHeight += gLCDExtra;
1060 glyph->fTop -= gLCDExtra >> 1;
1061 } else {
1062 glyph->fWidth += gLCDExtra;
1063 glyph->fLeft -= gLCDExtra >> 1;
1064 }
1065 }
1066}
1067
reed@android.com8a1c16f2008-12-17 15:59:43 +00001068void SkScalerContext_FreeType::generateMetrics(SkGlyph* glyph) {
1069 SkAutoMutexAcquire ac(gFTMutex);
1070
1071 glyph->fRsbDelta = 0;
1072 glyph->fLsbDelta = 0;
1073
1074 FT_Error err;
1075
1076 if (this->setupSize()) {
1077 goto ERROR;
1078 }
1079
1080 err = FT_Load_Glyph( fFace, glyph->getGlyphID(fBaseGlyphCount), fLoadGlyphFlags );
1081 if (err != 0) {
mike@reedtribe.org7a722f02012-11-15 02:12:14 +00001082#if 0
1083 SkDEBUGF(("SkScalerContext_FreeType::generateMetrics(%x): FT_Load_Glyph(glyph:%d flags:%x) returned 0x%x\n",
reed@android.com8a1c16f2008-12-17 15:59:43 +00001084 fFaceRec->fFontID, glyph->getGlyphID(fBaseGlyphCount), fLoadGlyphFlags, err));
mike@reedtribe.org7a722f02012-11-15 02:12:14 +00001085#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +00001086 ERROR:
reed@android.com62900b42009-02-11 15:07:19 +00001087 glyph->zeroMetrics();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001088 return;
1089 }
1090
1091 switch ( fFace->glyph->format ) {
thakis@chromium.org598b8592011-05-24 05:42:55 +00001092 case FT_GLYPH_FORMAT_OUTLINE: {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001093 FT_BBox bbox;
1094
bungeman@google.com0f0c2882011-11-04 15:47:41 +00001095 if (0 == fFace->glyph->outline.n_contours) {
1096 glyph->fWidth = 0;
1097 glyph->fHeight = 0;
1098 glyph->fTop = 0;
1099 glyph->fLeft = 0;
1100 break;
1101 }
1102
senorblanco@chromium.org4526a842010-02-05 23:08:20 +00001103 if (fRec.fFlags & kEmbolden_Flag) {
george@mozilla.comc59b5da2012-08-23 00:39:08 +00001104 emboldenOutline(fFace, &fFace->glyph->outline);
senorblanco@chromium.org4526a842010-02-05 23:08:20 +00001105 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001106
djsollen@google.comd8b599c2012-03-19 19:44:19 +00001107 getBBoxForCurrentGlyph(glyph, &bbox, true);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001108
bungeman@google.com8ff8a192012-09-25 20:38:28 +00001109 glyph->fWidth = SkToU16(SkFDot6Floor(bbox.xMax - bbox.xMin));
1110 glyph->fHeight = SkToU16(SkFDot6Floor(bbox.yMax - bbox.yMin));
1111 glyph->fTop = -SkToS16(SkFDot6Floor(bbox.yMax));
1112 glyph->fLeft = SkToS16(SkFDot6Floor(bbox.xMin));
djsollen@google.comd8b599c2012-03-19 19:44:19 +00001113
1114 updateGlyphIfLCD(glyph);
1115
reed@android.com8a1c16f2008-12-17 15:59:43 +00001116 break;
thakis@chromium.org598b8592011-05-24 05:42:55 +00001117 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001118
1119 case FT_GLYPH_FORMAT_BITMAP:
agl@chromium.orge76073b2010-06-04 20:31:17 +00001120 if (fRec.fFlags & kEmbolden_Flag) {
1121 FT_GlyphSlot_Own_Bitmap(fFace->glyph);
1122 FT_Bitmap_Embolden(gFTLibrary, &fFace->glyph->bitmap, kBitmapEmboldenStrength, 0);
1123 }
bungeman@google.com8ff8a192012-09-25 20:38:28 +00001124
1125 if (fRec.fFlags & SkScalerContext::kVertical_Flag) {
1126 FT_Vector vector;
1127 vector.x = fFace->glyph->metrics.vertBearingX - fFace->glyph->metrics.horiBearingX;
1128 vector.y = -fFace->glyph->metrics.vertBearingY - fFace->glyph->metrics.horiBearingY;
1129 FT_Vector_Transform(&vector, &fMatrix22);
1130 fFace->glyph->bitmap_left += SkFDot6Floor(vector.x);
1131 fFace->glyph->bitmap_top += SkFDot6Floor(vector.y);
1132 }
1133
reed@android.com8a1c16f2008-12-17 15:59:43 +00001134 glyph->fWidth = SkToU16(fFace->glyph->bitmap.width);
1135 glyph->fHeight = SkToU16(fFace->glyph->bitmap.rows);
1136 glyph->fTop = -SkToS16(fFace->glyph->bitmap_top);
1137 glyph->fLeft = SkToS16(fFace->glyph->bitmap_left);
1138 break;
1139
1140 default:
tomhudson@google.com0c00f212011-12-28 14:59:50 +00001141 SkDEBUGFAIL("unknown glyph format");
reed@android.com8a1c16f2008-12-17 15:59:43 +00001142 goto ERROR;
1143 }
1144
bungeman@google.com8ff8a192012-09-25 20:38:28 +00001145 if (fRec.fFlags & SkScalerContext::kVertical_Flag) {
1146 if (fDoLinearMetrics) {
1147 glyph->fAdvanceX = -SkFixedMul(fMatrix22.xy, fFace->glyph->linearVertAdvance);
1148 glyph->fAdvanceY = SkFixedMul(fMatrix22.yy, fFace->glyph->linearVertAdvance);
1149 } else {
1150 glyph->fAdvanceX = -SkFDot6ToFixed(fFace->glyph->advance.x);
1151 glyph->fAdvanceY = SkFDot6ToFixed(fFace->glyph->advance.y);
1152 }
bungeman@google.com34f10262012-03-23 18:11:47 +00001153 } else {
bungeman@google.com8ff8a192012-09-25 20:38:28 +00001154 if (fDoLinearMetrics) {
1155 glyph->fAdvanceX = SkFixedMul(fMatrix22.xx, fFace->glyph->linearHoriAdvance);
1156 glyph->fAdvanceY = -SkFixedMul(fMatrix22.yx, fFace->glyph->linearHoriAdvance);
1157 } else {
1158 glyph->fAdvanceX = SkFDot6ToFixed(fFace->glyph->advance.x);
1159 glyph->fAdvanceY = -SkFDot6ToFixed(fFace->glyph->advance.y);
bungeman@google.com34f10262012-03-23 18:11:47 +00001160
bungeman@google.com8ff8a192012-09-25 20:38:28 +00001161 if (fRec.fFlags & kDevKernText_Flag) {
1162 glyph->fRsbDelta = SkToS8(fFace->glyph->rsb_delta);
1163 glyph->fLsbDelta = SkToS8(fFace->glyph->lsb_delta);
djsollen@google.comd8b599c2012-03-19 19:44:19 +00001164 }
1165 }
djsollen@google.comd8b599c2012-03-19 19:44:19 +00001166 }
1167
1168
reed@android.com8a1c16f2008-12-17 15:59:43 +00001169#ifdef ENABLE_GLYPH_SPEW
1170 SkDEBUGF(("FT_Set_Char_Size(this:%p sx:%x sy:%x ", this, fScaleX, fScaleY));
1171 SkDEBUGF(("Metrics(glyph:%d flags:0x%x) w:%d\n", glyph->getGlyphID(fBaseGlyphCount), fLoadGlyphFlags, glyph->fWidth));
1172#endif
1173}
1174
reed@google.comea2333d2011-03-14 16:44:56 +00001175
bungeman@google.coma76de722012-10-26 19:35:54 +00001176void SkScalerContext_FreeType::generateImage(const SkGlyph& glyph) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001177 SkAutoMutexAcquire ac(gFTMutex);
1178
1179 FT_Error err;
1180
1181 if (this->setupSize()) {
1182 goto ERROR;
1183 }
1184
1185 err = FT_Load_Glyph( fFace, glyph.getGlyphID(fBaseGlyphCount), fLoadGlyphFlags);
1186 if (err != 0) {
1187 SkDEBUGF(("SkScalerContext_FreeType::generateImage: FT_Load_Glyph(glyph:%d width:%d height:%d rb:%d flags:%d) returned 0x%x\n",
1188 glyph.getGlyphID(fBaseGlyphCount), glyph.fWidth, glyph.fHeight, glyph.rowBytes(), fLoadGlyphFlags, err));
1189 ERROR:
1190 memset(glyph.fImage, 0, glyph.rowBytes() * glyph.fHeight);
1191 return;
1192 }
1193
bungeman@google.coma76de722012-10-26 19:35:54 +00001194 generateGlyphImage(fFace, glyph);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001195}
1196
reed@android.com8a1c16f2008-12-17 15:59:43 +00001197
1198void SkScalerContext_FreeType::generatePath(const SkGlyph& glyph,
1199 SkPath* path) {
1200 SkAutoMutexAcquire ac(gFTMutex);
1201
1202 SkASSERT(&glyph && path);
1203
1204 if (this->setupSize()) {
1205 path->reset();
1206 return;
1207 }
1208
1209 uint32_t flags = fLoadGlyphFlags;
1210 flags |= FT_LOAD_NO_BITMAP; // ignore embedded bitmaps so we're sure to get the outline
1211 flags &= ~FT_LOAD_RENDER; // don't scan convert (we just want the outline)
1212
1213 FT_Error err = FT_Load_Glyph( fFace, glyph.getGlyphID(fBaseGlyphCount), flags);
1214
1215 if (err != 0) {
1216 SkDEBUGF(("SkScalerContext_FreeType::generatePath: FT_Load_Glyph(glyph:%d flags:%d) returned 0x%x\n",
1217 glyph.getGlyphID(fBaseGlyphCount), flags, err));
1218 path->reset();
1219 return;
1220 }
1221
sugoi@google.com66a58ac2013-03-05 20:40:52 +00001222 generateGlyphPath(fFace, path);
bungeman@google.com8ff8a192012-09-25 20:38:28 +00001223
1224 // The path's origin from FreeType is always the horizontal layout origin.
1225 // Offset the path so that it is relative to the vertical origin if needed.
1226 if (fRec.fFlags & SkScalerContext::kVertical_Flag) {
1227 FT_Vector vector;
1228 vector.x = fFace->glyph->metrics.vertBearingX - fFace->glyph->metrics.horiBearingX;
1229 vector.y = -fFace->glyph->metrics.vertBearingY - fFace->glyph->metrics.horiBearingY;
1230 FT_Vector_Transform(&vector, &fMatrix22);
1231 path->offset(SkFDot6ToScalar(vector.x), -SkFDot6ToScalar(vector.y));
1232 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001233}
1234
1235void SkScalerContext_FreeType::generateFontMetrics(SkPaint::FontMetrics* mx,
1236 SkPaint::FontMetrics* my) {
1237 if (NULL == mx && NULL == my) {
1238 return;
1239 }
1240
1241 SkAutoMutexAcquire ac(gFTMutex);
1242
1243 if (this->setupSize()) {
reed@android.coma8a8b8b2009-05-04 15:00:11 +00001244 ERROR:
reed@android.com8a1c16f2008-12-17 15:59:43 +00001245 if (mx) {
reed@android.com4516f472009-06-29 16:25:36 +00001246 sk_bzero(mx, sizeof(SkPaint::FontMetrics));
reed@android.com8a1c16f2008-12-17 15:59:43 +00001247 }
1248 if (my) {
reed@android.com4516f472009-06-29 16:25:36 +00001249 sk_bzero(my, sizeof(SkPaint::FontMetrics));
reed@android.com8a1c16f2008-12-17 15:59:43 +00001250 }
1251 return;
1252 }
1253
reed@android.coma8a8b8b2009-05-04 15:00:11 +00001254 FT_Face face = fFace;
1255 int upem = face->units_per_EM;
1256 if (upem <= 0) {
1257 goto ERROR;
1258 }
1259
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001260 SkPoint pts[6];
1261 SkFixed ys[6];
reed@google.comf073b332013-05-06 12:21:16 +00001262 SkScalar scaleY = fScale.y();
1263 SkScalar mxy = fMatrix22Scalar.getSkewX();
1264 SkScalar myy = fMatrix22Scalar.getScaleY();
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001265 SkScalar xmin = SkIntToScalar(face->bbox.xMin) / upem;
1266 SkScalar xmax = SkIntToScalar(face->bbox.xMax) / upem;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001267
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001268 int leading = face->height - (face->ascender + -face->descender);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001269 if (leading < 0) {
1270 leading = 0;
1271 }
1272
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001273 // Try to get the OS/2 table from the font. This contains the specific
1274 // average font width metrics which Windows uses.
1275 TT_OS2* os2 = (TT_OS2*) FT_Get_Sfnt_Table(face, ft_sfnt_os2);
1276
reed@android.com8a1c16f2008-12-17 15:59:43 +00001277 ys[0] = -face->bbox.yMax;
1278 ys[1] = -face->ascender;
1279 ys[2] = -face->descender;
1280 ys[3] = -face->bbox.yMin;
1281 ys[4] = leading;
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001282 ys[5] = os2 ? os2->xAvgCharWidth : 0;
1283
1284 SkScalar x_height;
1285 if (os2 && os2->sxHeight) {
reed@google.comf073b332013-05-06 12:21:16 +00001286 x_height = fScale.x() * os2->sxHeight / upem;
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001287 } else {
1288 const FT_UInt x_glyph = FT_Get_Char_Index(fFace, 'x');
1289 if (x_glyph) {
1290 FT_BBox bbox;
1291 FT_Load_Glyph(fFace, x_glyph, fLoadGlyphFlags);
senorblanco@chromium.org4526a842010-02-05 23:08:20 +00001292 if (fRec.fFlags & kEmbolden_Flag) {
george@mozilla.comc59b5da2012-08-23 00:39:08 +00001293 emboldenOutline(fFace, &fFace->glyph->outline);
senorblanco@chromium.org4526a842010-02-05 23:08:20 +00001294 }
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001295 FT_Outline_Get_CBox(&fFace->glyph->outline, &bbox);
reed@google.comf073b332013-05-06 12:21:16 +00001296 x_height = bbox.yMax / 64.0f;
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001297 } else {
1298 x_height = 0;
1299 }
1300 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001301
1302 // convert upem-y values into scalar points
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001303 for (int i = 0; i < 6; i++) {
reed@google.comf073b332013-05-06 12:21:16 +00001304 SkScalar y = scaleY * ys[i] / upem;
1305 pts[i].set(y * mxy, y * myy);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001306 }
1307
1308 if (mx) {
1309 mx->fTop = pts[0].fX;
1310 mx->fAscent = pts[1].fX;
1311 mx->fDescent = pts[2].fX;
1312 mx->fBottom = pts[3].fX;
1313 mx->fLeading = pts[4].fX;
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001314 mx->fAvgCharWidth = pts[5].fX;
1315 mx->fXMin = xmin;
1316 mx->fXMax = xmax;
1317 mx->fXHeight = x_height;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001318 }
1319 if (my) {
1320 my->fTop = pts[0].fY;
1321 my->fAscent = pts[1].fY;
1322 my->fDescent = pts[2].fY;
1323 my->fBottom = pts[3].fY;
1324 my->fLeading = pts[4].fY;
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001325 my->fAvgCharWidth = pts[5].fY;
1326 my->fXMin = xmin;
1327 my->fXMax = xmax;
1328 my->fXHeight = x_height;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001329 }
1330}
1331
reed@google.comb4162b12013-07-02 16:32:29 +00001332///////////////////////////////////////////////////////////////////////////////
1333
1334#include "SkUtils.h"
1335
1336static SkUnichar next_utf8(const void** chars) {
1337 return SkUTF8_NextUnichar((const char**)chars);
1338}
1339
1340static SkUnichar next_utf16(const void** chars) {
1341 return SkUTF16_NextUnichar((const uint16_t**)chars);
1342}
1343
1344static SkUnichar next_utf32(const void** chars) {
1345 const SkUnichar** uniChars = (const SkUnichar**)chars;
1346 SkUnichar uni = **uniChars;
1347 *uniChars += 1;
1348 return uni;
1349}
1350
1351typedef SkUnichar (*EncodingProc)(const void**);
1352
1353static EncodingProc find_encoding_proc(SkTypeface::Encoding enc) {
1354 static const EncodingProc gProcs[] = {
1355 next_utf8, next_utf16, next_utf32
1356 };
1357 SkASSERT((size_t)enc < SK_ARRAY_COUNT(gProcs));
1358 return gProcs[enc];
1359}
1360
1361int SkTypeface_FreeType::onCharsToGlyphs(const void* chars, Encoding encoding,
1362 uint16_t glyphs[], int glyphCount) const {
1363 AutoFTAccess fta(this);
1364 FT_Face face = fta.face();
1365 if (!face) {
1366 if (glyphs) {
1367 sk_bzero(glyphs, glyphCount * sizeof(glyphs[0]));
1368 }
1369 return 0;
1370 }
1371
1372 EncodingProc next_uni_proc = find_encoding_proc(encoding);
1373
1374 if (NULL == glyphs) {
1375 for (int i = 0; i < glyphCount; ++i) {
1376 if (0 == FT_Get_Char_Index(face, next_uni_proc(&chars))) {
1377 return i;
1378 }
1379 }
1380 return glyphCount;
1381 } else {
1382 int first = glyphCount;
1383 for (int i = 0; i < glyphCount; ++i) {
1384 unsigned id = FT_Get_Char_Index(face, next_uni_proc(&chars));
1385 glyphs[i] = SkToU16(id);
1386 if (0 == id && i < first) {
1387 first = i;
1388 }
1389 }
1390 return first;
1391 }
1392}
1393
1394int SkTypeface_FreeType::onCountGlyphs() const {
1395 // we cache this value, using -1 as a sentinel for "not computed"
1396 if (fGlyphCount < 0) {
1397 AutoFTAccess fta(this);
1398 FT_Face face = fta.face();
1399 // if the face failed, we still assign a non-negative value
1400 fGlyphCount = face ? face->num_glyphs : 0;
1401 }
1402 return fGlyphCount;
1403}
1404
bungeman@google.comddc218e2013-08-01 22:29:43 +00001405int SkTypeface_FreeType::onGetTableTags(SkFontTableTag tags[]) const {
1406 AutoFTAccess fta(this);
1407 FT_Face face = fta.face();
1408
1409 FT_ULong tableCount = 0;
1410 FT_Error error;
1411
1412 // When 'tag' is NULL, returns number of tables in 'length'.
1413 error = FT_Sfnt_Table_Info(face, 0, NULL, &tableCount);
1414 if (error) {
1415 return 0;
1416 }
1417
1418 if (tags) {
1419 for (FT_ULong tableIndex = 0; tableIndex < tableCount; ++tableIndex) {
1420 FT_ULong tableTag;
1421 FT_ULong tablelength;
1422 error = FT_Sfnt_Table_Info(face, tableIndex, &tableTag, &tablelength);
1423 if (error) {
1424 return 0;
1425 }
1426 tags[tableIndex] = static_cast<SkFontTableTag>(tableTag);
1427 }
1428 }
1429 return tableCount;
1430}
1431
1432size_t SkTypeface_FreeType::onGetTableData(SkFontTableTag tag, size_t offset,
1433 size_t length, void* data) const
1434{
1435 AutoFTAccess fta(this);
1436 FT_Face face = fta.face();
1437
1438 FT_ULong tableLength = 0;
1439 FT_Error error;
1440
1441 // When 'length' is 0 it is overwritten with the full table length; 'offset' is ignored.
1442 error = FT_Load_Sfnt_Table(face, tag, 0, NULL, &tableLength);
1443 if (error) {
1444 return 0;
1445 }
1446
1447 if (offset > tableLength) {
1448 return 0;
1449 }
bungeman@google.com5ecd4fa2013-08-01 22:48:21 +00001450 FT_ULong size = SkTMin((FT_ULong)length, tableLength - (FT_ULong)offset);
bungeman@google.comddc218e2013-08-01 22:29:43 +00001451 if (NULL != data) {
1452 error = FT_Load_Sfnt_Table(face, tag, offset, reinterpret_cast<FT_Byte*>(data), &size);
1453 if (error) {
1454 return 0;
1455 }
1456 }
1457
1458 return size;
1459}
1460
reed@google.comb4162b12013-07-02 16:32:29 +00001461///////////////////////////////////////////////////////////////////////////////
1462///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +00001463
reed@android.com8a1c16f2008-12-17 15:59:43 +00001464/* Export this so that other parts of our FonttHost port can make use of our
1465 ability to extract the name+style from a stream, using FreeType's api.
1466*/
djsollen@google.com4dc686d2012-02-15 21:03:45 +00001467bool find_name_and_attributes(SkStream* stream, SkString* name,
bungeman@google.comfe747652013-03-25 19:36:11 +00001468 SkTypeface::Style* style, bool* isFixedPitch) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001469 FT_Library library;
reed@android.combfbd4ff2009-07-23 17:44:41 +00001470 if (FT_Init_FreeType(&library)) {
djsollen@google.com4dc686d2012-02-15 21:03:45 +00001471 return false;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001472 }
1473
1474 FT_Open_Args args;
1475 memset(&args, 0, sizeof(args));
1476
1477 const void* memoryBase = stream->getMemoryBase();
1478 FT_StreamRec streamRec;
1479
1480 if (NULL != memoryBase) {
1481 args.flags = FT_OPEN_MEMORY;
1482 args.memory_base = (const FT_Byte*)memoryBase;
1483 args.memory_size = stream->getLength();
1484 } else {
1485 memset(&streamRec, 0, sizeof(streamRec));
djsollen@google.com5dd45022013-03-21 13:30:54 +00001486 streamRec.size = stream->getLength();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001487 streamRec.descriptor.pointer = stream;
1488 streamRec.read = sk_stream_read;
1489 streamRec.close = sk_stream_close;
1490
1491 args.flags = FT_OPEN_STREAM;
1492 args.stream = &streamRec;
1493 }
1494
1495 FT_Face face;
1496 if (FT_Open_Face(library, &args, 0, &face)) {
1497 FT_Done_FreeType(library);
djsollen@google.com4dc686d2012-02-15 21:03:45 +00001498 return false;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001499 }
1500
djsollen@google.com4dc686d2012-02-15 21:03:45 +00001501 int tempStyle = SkTypeface::kNormal;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001502 if (face->style_flags & FT_STYLE_FLAG_BOLD) {
djsollen@google.com4dc686d2012-02-15 21:03:45 +00001503 tempStyle |= SkTypeface::kBold;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001504 }
1505 if (face->style_flags & FT_STYLE_FLAG_ITALIC) {
djsollen@google.com4dc686d2012-02-15 21:03:45 +00001506 tempStyle |= SkTypeface::kItalic;
1507 }
1508
1509 if (name) {
1510 name->set(face->family_name);
1511 }
1512 if (style) {
1513 *style = (SkTypeface::Style) tempStyle;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001514 }
bungeman@google.comfe747652013-03-25 19:36:11 +00001515 if (isFixedPitch) {
1516 *isFixedPitch = FT_IS_FIXED_WIDTH(face);
reed@google.com5b31b0f2011-02-23 14:41:42 +00001517 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001518
1519 FT_Done_Face(face);
1520 FT_Done_FreeType(library);
djsollen@google.com4dc686d2012-02-15 21:03:45 +00001521 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001522}