blob: 2e0b8b6ee371abc703bd1c5db8bb336cceef9eda [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
2 * Copyright 2006 The Android Open Source Project
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
commit-bot@chromium.orgba9354b2014-02-10 19:58:49 +00008#include "SkAdvancedTypefaceMetrics.h"
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"
bungeman41868fe2015-05-20 09:21:04 -070014#include "SkFontDescriptor.h"
george@mozilla.comc59b5da2012-08-23 00:39:08 +000015#include "SkFontHost_FreeType_common.h"
bungeman@google.combbe50132012-07-24 20:33:21 +000016#include "SkGlyph.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000017#include "SkMask.h"
bungeman@google.com97efada2012-07-30 20:40:50 +000018#include "SkMaskGamma.h"
bungeman@google.comd3fbd342014-04-15 15:52:07 +000019#include "SkMatrix22.h"
mtklein1b249332015-07-07 12:21:21 -070020#include "SkMutex.h"
bungeman@google.coma9802692013-08-07 02:45:25 +000021#include "SkOTUtils.h"
bungemand3ebb482015-08-05 13:57:49 -070022#include "SkPath.h"
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000023#include "SkScalerContext.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000024#include "SkStream.h"
25#include "SkString.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000026#include "SkTemplates.h"
bungeman9dc24682014-12-01 14:01:32 -080027#include "SkTypes.h"
mtklein5f939ab2016-03-16 10:28:35 -070028#include <memory>
reed@android.com8a1c16f2008-12-17 15:59:43 +000029
bungeman@google.comfd668cf2012-08-24 17:46:11 +000030#if defined(SK_CAN_USE_DLOPEN)
31#include <dlfcn.h>
32#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +000033#include <ft2build.h>
bungeman5ec443c2014-11-21 13:18:34 -080034#include FT_ADVANCES_H
35#include FT_BITMAP_H
reed@android.com8a1c16f2008-12-17 15:59:43 +000036#include FT_FREETYPE_H
bungeman5ec443c2014-11-21 13:18:34 -080037#include FT_LCD_FILTER_H
bungeman9dc24682014-12-01 14:01:32 -080038#include FT_MODULE_H
bungeman41868fe2015-05-20 09:21:04 -070039#include FT_MULTIPLE_MASTERS_H
reed@android.com8a1c16f2008-12-17 15:59:43 +000040#include FT_OUTLINE_H
41#include FT_SIZES_H
bungeman9dc24682014-12-01 14:01:32 -080042#include FT_SYSTEM_H
agl@chromium.orgcc3096b2009-04-22 22:09:04 +000043#include FT_TRUETYPE_TABLES_H
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000044#include FT_TYPE1_TABLES_H
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000045#include FT_XFREE86_H
agl@chromium.orgcc3096b2009-04-22 22:09:04 +000046
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +000047// FT_LOAD_COLOR and the corresponding FT_Pixel_Mode::FT_PIXEL_MODE_BGRA
48// were introduced in FreeType 2.5.0.
49// The following may be removed once FreeType 2.5.0 is required to build.
50#ifndef FT_LOAD_COLOR
51# define FT_LOAD_COLOR ( 1L << 20 )
52# define FT_PIXEL_MODE_BGRA 7
53#endif
54
reed@android.com8a1c16f2008-12-17 15:59:43 +000055//#define ENABLE_GLYPH_SPEW // for tracing calls
56//#define DUMP_STRIKE_CREATION
bungeman5ec443c2014-11-21 13:18:34 -080057//#define SK_FONTHOST_FREETYPE_USE_NORMAL_LCD_FILTER
58//#define SK_FONTHOST_FREETYPE_RUNTIME_VERSION
reed@google.com1ac83502012-02-28 17:06:02 +000059//#define SK_GAMMA_APPLY_TO_A8
reed@google.com1ac83502012-02-28 17:06:02 +000060
reed@google.comeffc5012011-06-27 16:44:46 +000061static bool isLCD(const SkScalerContext::Rec& rec) {
bungeman9dc24682014-12-01 14:01:32 -080062 return SkMask::kLCD16_Format == rec.fMaskFormat;
reed@google.comeffc5012011-06-27 16:44:46 +000063}
64
reed@android.com8a1c16f2008-12-17 15:59:43 +000065//////////////////////////////////////////////////////////////////////////
66
bungeman9dc24682014-12-01 14:01:32 -080067extern "C" {
68 static void* sk_ft_alloc(FT_Memory, long size) {
69 return sk_malloc_throw(size);
70 }
71 static void sk_ft_free(FT_Memory, void* block) {
72 sk_free(block);
73 }
74 static void* sk_ft_realloc(FT_Memory, long cur_size, long new_size, void* block) {
75 return sk_realloc_throw(block, new_size);
76 }
77};
halcanary96fcdcc2015-08-27 07:41:13 -070078FT_MemoryRec_ gFTMemory = { nullptr, sk_ft_alloc, sk_ft_free, sk_ft_realloc };
bungeman9dc24682014-12-01 14:01:32 -080079
80class FreeTypeLibrary : SkNoncopyable {
81public:
halcanary96fcdcc2015-08-27 07:41:13 -070082 FreeTypeLibrary() : fLibrary(nullptr), fIsLCDSupported(false), fLCDExtra(0) {
bungeman9dc24682014-12-01 14:01:32 -080083 if (FT_New_Library(&gFTMemory, &fLibrary)) {
84 return;
85 }
86 FT_Add_Default_Modules(fLibrary);
87
88 // Setup LCD filtering. This reduces color fringes for LCD smoothed glyphs.
89 // Default { 0x10, 0x40, 0x70, 0x40, 0x10 } adds up to 0x110, simulating ink spread.
90 // SetLcdFilter must be called before SetLcdFilterWeights.
91 if (FT_Library_SetLcdFilter(fLibrary, FT_LCD_FILTER_DEFAULT) == 0) {
92 fIsLCDSupported = true;
93 fLCDExtra = 2; //Using a filter adds one full pixel to each side.
94
95#ifdef SK_FONTHOST_FREETYPE_USE_NORMAL_LCD_FILTER
96 // Adds to 0x110 simulating ink spread, but provides better results than default.
97 static unsigned char gGaussianLikeHeavyWeights[] = { 0x1A, 0x43, 0x56, 0x43, 0x1A, };
98
99# if SK_FONTHOST_FREETYPE_RUNTIME_VERSION > 0x020400
100 FT_Library_SetLcdFilterWeights(fLibrary, gGaussianLikeHeavyWeights);
101# elif SK_CAN_USE_DLOPEN == 1
102 //The FreeType library is already loaded, so symbols are available in process.
halcanary96fcdcc2015-08-27 07:41:13 -0700103 void* self = dlopen(nullptr, RTLD_LAZY);
bungeman9dc24682014-12-01 14:01:32 -0800104 if (self) {
105 FT_Library_SetLcdFilterWeightsProc setLcdFilterWeights;
106 //The following cast is non-standard, but safe for POSIX.
107 *reinterpret_cast<void**>(&setLcdFilterWeights) =
108 dlsym(self, "FT_Library_SetLcdFilterWeights");
109 dlclose(self);
110
111 if (setLcdFilterWeights) {
112 setLcdFilterWeights(fLibrary, gGaussianLikeHeavyWeights);
113 }
114 }
115# endif
116#endif
117 }
118 }
119 ~FreeTypeLibrary() {
120 if (fLibrary) {
121 FT_Done_Library(fLibrary);
122 }
123 }
124
125 FT_Library library() { return fLibrary; }
126 bool isLCDSupported() { return fIsLCDSupported; }
127 int lcdExtra() { return fLCDExtra; }
128
129private:
130 FT_Library fLibrary;
131 bool fIsLCDSupported;
132 int fLCDExtra;
133
134 // FT_Library_SetLcdFilterWeights was introduced in FreeType 2.4.0.
135 // The following platforms provide FreeType of at least 2.4.0.
136 // Ubuntu >= 11.04 (previous deprecated April 2013)
137 // Debian >= 6.0 (good)
138 // OpenSuse >= 11.4 (previous deprecated January 2012 / Nov 2013 for Evergreen 11.2)
139 // Fedora >= 14 (good)
140 // Android >= Gingerbread (good)
141 typedef FT_Error (*FT_Library_SetLcdFilterWeightsProc)(FT_Library, unsigned char*);
142};
143
reed@android.com8a1c16f2008-12-17 15:59:43 +0000144struct SkFaceRec;
145
reed086eea92016-05-04 17:12:46 -0700146SK_DECLARE_STATIC_MUTEX(gFTMutex);
bungeman9dc24682014-12-01 14:01:32 -0800147static FreeTypeLibrary* gFTLibrary;
148static SkFaceRec* gFaceRecHead;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000149
bungemanaabd71c2016-03-01 15:15:09 -0800150// Private to ref_ft_library and unref_ft_library
bungeman9dc24682014-12-01 14:01:32 -0800151static int gFTCount;
bungeman@google.comfd668cf2012-08-24 17:46:11 +0000152
scroggo@google.com94bc60f2012-10-04 20:45:06 +0000153// Caller must lock gFTMutex before calling this function.
bungeman9dc24682014-12-01 14:01:32 -0800154static bool ref_ft_library() {
bungeman5ec443c2014-11-21 13:18:34 -0800155 gFTMutex.assertHeld();
bungeman9dc24682014-12-01 14:01:32 -0800156 SkASSERT(gFTCount >= 0);
bungeman5ec443c2014-11-21 13:18:34 -0800157
bungeman9dc24682014-12-01 14:01:32 -0800158 if (0 == gFTCount) {
halcanary96fcdcc2015-08-27 07:41:13 -0700159 SkASSERT(nullptr == gFTLibrary);
halcanary385fe4d2015-08-26 13:07:48 -0700160 gFTLibrary = new FreeTypeLibrary;
reed@google.comea2333d2011-03-14 16:44:56 +0000161 }
bungeman9dc24682014-12-01 14:01:32 -0800162 ++gFTCount;
163 return gFTLibrary->library();
agl@chromium.org309485b2009-07-21 17:41:32 +0000164}
165
bungeman9dc24682014-12-01 14:01:32 -0800166// Caller must lock gFTMutex before calling this function.
167static void unref_ft_library() {
168 gFTMutex.assertHeld();
169 SkASSERT(gFTCount > 0);
commit-bot@chromium.orgba9354b2014-02-10 19:58:49 +0000170
bungeman9dc24682014-12-01 14:01:32 -0800171 --gFTCount;
172 if (0 == gFTCount) {
bungemanaabd71c2016-03-01 15:15:09 -0800173 SkASSERT(nullptr == gFaceRecHead);
halcanary96fcdcc2015-08-27 07:41:13 -0700174 SkASSERT(nullptr != gFTLibrary);
halcanary385fe4d2015-08-26 13:07:48 -0700175 delete gFTLibrary;
halcanary96fcdcc2015-08-27 07:41:13 -0700176 SkDEBUGCODE(gFTLibrary = nullptr;)
bungeman9dc24682014-12-01 14:01:32 -0800177 }
reed@google.comfb2fdcc2012-10-17 15:49:36 +0000178}
179
george@mozilla.comc59b5da2012-08-23 00:39:08 +0000180class SkScalerContext_FreeType : public SkScalerContext_FreeType_Base {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000181public:
reeda9322c22016-04-12 06:47:05 -0700182 SkScalerContext_FreeType(SkTypeface*, const SkScalerContextEffects&, const SkDescriptor* desc);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000183 virtual ~SkScalerContext_FreeType();
agl@chromium.orgcc3096b2009-04-22 22:09:04 +0000184
reed@android.com62900b42009-02-11 15:07:19 +0000185 bool success() const {
halcanary96fcdcc2015-08-27 07:41:13 -0700186 return fFTSize != nullptr && fFace != nullptr;
reed@android.com62900b42009-02-11 15:07:19 +0000187 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000188
189protected:
mtklein36352bf2015-03-25 18:17:31 -0700190 unsigned generateGlyphCount() override;
191 uint16_t generateCharToGlyph(SkUnichar uni) override;
192 void generateAdvance(SkGlyph* glyph) override;
193 void generateMetrics(SkGlyph* glyph) override;
194 void generateImage(const SkGlyph& glyph) override;
195 void generatePath(const SkGlyph& glyph, SkPath* path) override;
196 void generateFontMetrics(SkPaint::FontMetrics*) override;
197 SkUnichar generateGlyphToChar(uint16_t glyph) override;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000198
199private:
reed@android.com8a1c16f2008-12-17 15:59:43 +0000200 FT_Face fFace; // reference to shared face in gFaceRecHead
201 FT_Size fFTSize; // our own copy
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +0000202 FT_Int fStrikeIndex;
benjaminwagner45345622016-02-19 15:30:20 -0800203 FT_F26Dot6 fScaleX, fScaleY;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000204 FT_Matrix fMatrix22;
205 uint32_t fLoadGlyphFlags;
reed@google.combdc99882011-11-21 14:36:57 +0000206 bool fDoLinearMetrics;
reed@google.coma1bfa212012-03-08 21:57:12 +0000207 bool fLCDIsVert;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000208
reed@google.comf073b332013-05-06 12:21:16 +0000209 // Need scalar versions for generateFontMetrics
210 SkVector fScale;
211 SkMatrix fMatrix22Scalar;
212
reed@android.com8a1c16f2008-12-17 15:59:43 +0000213 FT_Error setupSize();
djsollen@google.comd8b599c2012-03-19 19:44:19 +0000214 void getBBoxForCurrentGlyph(SkGlyph* glyph, FT_BBox* bbox,
215 bool snapToPixelBoundary = false);
bungeman@google.comcbe1b542013-12-16 17:02:39 +0000216 bool getCBoxForLetter(char letter, FT_BBox* bbox);
scroggo@google.com94bc60f2012-10-04 20:45:06 +0000217 // Caller must lock gFTMutex before calling this function.
djsollen@google.comd8b599c2012-03-19 19:44:19 +0000218 void updateGlyphIfLCD(SkGlyph* glyph);
commit-bot@chromium.org6fa81d72013-12-26 15:50:29 +0000219 // Caller must lock gFTMutex before calling this function.
220 // update FreeType2 glyph slot with glyph emboldened
221 void emboldenIfNeeded(FT_Face face, FT_GlyphSlot glyph);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000222};
223
224///////////////////////////////////////////////////////////////////////////
225///////////////////////////////////////////////////////////////////////////
226
reed@android.com8a1c16f2008-12-17 15:59:43 +0000227struct SkFaceRec {
bungeman52b64b42015-01-27 10:41:17 -0800228 SkFaceRec* fNext;
229 FT_Face fFace;
230 FT_StreamRec fFTStream;
231 SkAutoTDelete<SkStreamAsset> fSkStream;
232 uint32_t fRefCnt;
233 uint32_t fFontID;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000234
scroggoa1193e42015-01-21 12:09:53 -0800235 // assumes ownership of the stream, will delete when its done
bungeman52b64b42015-01-27 10:41:17 -0800236 SkFaceRec(SkStreamAsset* strm, uint32_t fontID);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000237};
238
239extern "C" {
bungeman52b64b42015-01-27 10:41:17 -0800240 static unsigned long sk_ft_stream_io(FT_Stream ftStream,
bungeman9dc24682014-12-01 14:01:32 -0800241 unsigned long offset,
242 unsigned char* buffer,
243 unsigned long count)
244 {
bungeman52b64b42015-01-27 10:41:17 -0800245 SkStreamAsset* stream = static_cast<SkStreamAsset*>(ftStream->descriptor.pointer);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000246
247 if (count) {
bungeman52b64b42015-01-27 10:41:17 -0800248 if (!stream->seek(offset)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000249 return 0;
bungeman9dc24682014-12-01 14:01:32 -0800250 }
bungeman52b64b42015-01-27 10:41:17 -0800251 count = stream->read(buffer, count);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000252 }
253 return count;
254 }
255
bungeman9dc24682014-12-01 14:01:32 -0800256 static void sk_ft_stream_close(FT_Stream) {}
reed@android.com8a1c16f2008-12-17 15:59:43 +0000257}
258
bungeman52b64b42015-01-27 10:41:17 -0800259SkFaceRec::SkFaceRec(SkStreamAsset* stream, uint32_t fontID)
halcanary96fcdcc2015-08-27 07:41:13 -0700260 : fNext(nullptr), fSkStream(stream), fRefCnt(1), fFontID(fontID)
bungeman52b64b42015-01-27 10:41:17 -0800261{
reed@android.com4516f472009-06-29 16:25:36 +0000262 sk_bzero(&fFTStream, sizeof(fFTStream));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000263 fFTStream.size = fSkStream->getLength();
264 fFTStream.descriptor.pointer = fSkStream;
bungeman9dc24682014-12-01 14:01:32 -0800265 fFTStream.read = sk_ft_stream_io;
266 fFTStream.close = sk_ft_stream_close;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000267}
268
bungeman41868fe2015-05-20 09:21:04 -0700269static void ft_face_setup_axes(FT_Face face, const SkFontData& data) {
270 if (!(face->face_flags & FT_FACE_FLAG_MULTIPLE_MASTERS)) {
271 return;
272 }
273
274 SkDEBUGCODE(
halcanary96fcdcc2015-08-27 07:41:13 -0700275 FT_MM_Var* variations = nullptr;
bungeman41868fe2015-05-20 09:21:04 -0700276 if (FT_Get_MM_Var(face, &variations)) {
277 SkDEBUGF(("INFO: font %s claims variations, but none found.\n", face->family_name));
278 return;
279 }
280 SkAutoFree autoFreeVariations(variations);
281
282 if (static_cast<FT_UInt>(data.getAxisCount()) != variations->num_axis) {
283 SkDEBUGF(("INFO: font %s has %d variations, but %d were specified.\n",
284 face->family_name, variations->num_axis, data.getAxisCount()));
285 return;
286 }
287 )
288
289 SkAutoSTMalloc<4, FT_Fixed> coords(data.getAxisCount());
290 for (int i = 0; i < data.getAxisCount(); ++i) {
291 coords[i] = data.getAxis()[i];
292 }
293 if (FT_Set_Var_Design_Coordinates(face, data.getAxisCount(), coords.get())) {
294 SkDEBUGF(("INFO: font %s has variations, but specified variations could not be set.\n",
295 face->family_name));
296 return;
297 }
298}
bungeman41868fe2015-05-20 09:21:04 -0700299
reed@android.com62900b42009-02-11 15:07:19 +0000300// Will return 0 on failure
scroggo@google.com94bc60f2012-10-04 20:45:06 +0000301// Caller must lock gFTMutex before calling this function.
bungeman13a007d2015-06-19 05:09:39 -0700302static FT_Face ref_ft_face(const SkTypeface* typeface) {
bungeman5ec443c2014-11-21 13:18:34 -0800303 gFTMutex.assertHeld();
304
reed@google.com2cdc6712013-03-21 18:22:00 +0000305 const SkFontID fontID = typeface->uniqueID();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000306 SkFaceRec* rec = gFaceRecHead;
307 while (rec) {
308 if (rec->fFontID == fontID) {
309 SkASSERT(rec->fFace);
310 rec->fRefCnt += 1;
bungeman13a007d2015-06-19 05:09:39 -0700311 return rec->fFace;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000312 }
313 rec = rec->fNext;
314 }
315
bungeman41868fe2015-05-20 09:21:04 -0700316 SkAutoTDelete<SkFontData> data(typeface->createFontData());
halcanary96fcdcc2015-08-27 07:41:13 -0700317 if (nullptr == data || !data->hasStream()) {
318 return nullptr;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000319 }
320
bungeman52b64b42015-01-27 10:41:17 -0800321 // this passes ownership of stream to the rec
halcanary385fe4d2015-08-26 13:07:48 -0700322 rec = new SkFaceRec(data->detachStream(), fontID);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000323
bungeman9dc24682014-12-01 14:01:32 -0800324 FT_Open_Args args;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000325 memset(&args, 0, sizeof(args));
bungeman41868fe2015-05-20 09:21:04 -0700326 const void* memoryBase = rec->fSkStream->getMemoryBase();
bsalomon49f085d2014-09-05 13:34:00 -0700327 if (memoryBase) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000328 args.flags = FT_OPEN_MEMORY;
329 args.memory_base = (const FT_Byte*)memoryBase;
bungeman41868fe2015-05-20 09:21:04 -0700330 args.memory_size = rec->fSkStream->getLength();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000331 } else {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000332 args.flags = FT_OPEN_STREAM;
333 args.stream = &rec->fFTStream;
334 }
335
bungeman41868fe2015-05-20 09:21:04 -0700336 FT_Error err = FT_Open_Face(gFTLibrary->library(), &args, data->getIndex(), &rec->fFace);
337 if (err) {
bungeman5ec443c2014-11-21 13:18:34 -0800338 SkDEBUGF(("ERROR: unable to open font '%x'\n", fontID));
halcanary385fe4d2015-08-26 13:07:48 -0700339 delete rec;
halcanary96fcdcc2015-08-27 07:41:13 -0700340 return nullptr;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000341 }
bungeman9dc24682014-12-01 14:01:32 -0800342 SkASSERT(rec->fFace);
bungeman41868fe2015-05-20 09:21:04 -0700343
bungeman41868fe2015-05-20 09:21:04 -0700344 ft_face_setup_axes(rec->fFace, *data);
bungeman41868fe2015-05-20 09:21:04 -0700345
bungeman726cf902015-06-05 13:38:12 -0700346 // FreeType will set the charmap to the "most unicode" cmap if it exists.
halcanary96fcdcc2015-08-27 07:41:13 -0700347 // If there are no unicode cmaps, the charmap is set to nullptr.
bungeman726cf902015-06-05 13:38:12 -0700348 // However, "symbol" cmaps should also be considered "fallback unicode" cmaps
349 // because they are effectively private use area only (even if they aren't).
350 // This is the last on the fallback list at
351 // https://developer.apple.com/fonts/TrueType-Reference-Manual/RM06/Chap6cmap.html
352 if (!rec->fFace->charmap) {
353 FT_Select_Charmap(rec->fFace, FT_ENCODING_MS_SYMBOL);
354 }
355
bungeman9dc24682014-12-01 14:01:32 -0800356 rec->fNext = gFaceRecHead;
357 gFaceRecHead = rec;
bungeman13a007d2015-06-19 05:09:39 -0700358 return rec->fFace;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000359}
360
scroggo@google.com94bc60f2012-10-04 20:45:06 +0000361// Caller must lock gFTMutex before calling this function.
caryclarkfe7ada72016-03-21 06:55:52 -0700362extern void unref_ft_face(FT_Face face);
363void unref_ft_face(FT_Face face) {
bungeman5ec443c2014-11-21 13:18:34 -0800364 gFTMutex.assertHeld();
365
reed@android.com8a1c16f2008-12-17 15:59:43 +0000366 SkFaceRec* rec = gFaceRecHead;
halcanary96fcdcc2015-08-27 07:41:13 -0700367 SkFaceRec* prev = nullptr;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000368 while (rec) {
369 SkFaceRec* next = rec->fNext;
370 if (rec->fFace == face) {
371 if (--rec->fRefCnt == 0) {
372 if (prev) {
373 prev->fNext = next;
374 } else {
375 gFaceRecHead = next;
376 }
377 FT_Done_Face(face);
halcanary385fe4d2015-08-26 13:07:48 -0700378 delete rec;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000379 }
380 return;
381 }
382 prev = rec;
383 rec = next;
384 }
tomhudson@google.com0c00f212011-12-28 14:59:50 +0000385 SkDEBUGFAIL("shouldn't get here, face not in list");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000386}
387
reed@google.comb4162b12013-07-02 16:32:29 +0000388class AutoFTAccess {
389public:
halcanary96fcdcc2015-08-27 07:41:13 -0700390 AutoFTAccess(const SkTypeface* tf) : fFace(nullptr) {
reed@google.comb4162b12013-07-02 16:32:29 +0000391 gFTMutex.acquire();
bungeman9dc24682014-12-01 14:01:32 -0800392 if (!ref_ft_library()) {
393 sk_throw();
reed@google.comb4162b12013-07-02 16:32:29 +0000394 }
bungeman13a007d2015-06-19 05:09:39 -0700395 fFace = ref_ft_face(tf);
reed@google.comb4162b12013-07-02 16:32:29 +0000396 }
397
398 ~AutoFTAccess() {
399 if (fFace) {
400 unref_ft_face(fFace);
401 }
bungeman9dc24682014-12-01 14:01:32 -0800402 unref_ft_library();
reed@google.comb4162b12013-07-02 16:32:29 +0000403 gFTMutex.release();
404 }
405
reed@google.comb4162b12013-07-02 16:32:29 +0000406 FT_Face face() { return fFace; }
407
408private:
reed@google.comb4162b12013-07-02 16:32:29 +0000409 FT_Face fFace;
410};
411
reed@android.com8a1c16f2008-12-17 15:59:43 +0000412///////////////////////////////////////////////////////////////////////////
413
vandebo@chromium.org16be6b82011-01-28 21:28:56 +0000414static bool canEmbed(FT_Face face) {
vandebo@chromium.org16be6b82011-01-28 21:28:56 +0000415 FT_UShort fsType = FT_Get_FSType_Flags(face);
416 return (fsType & (FT_FSTYPE_RESTRICTED_LICENSE_EMBEDDING |
417 FT_FSTYPE_BITMAP_EMBEDDING_ONLY)) == 0;
vandebo@chromium.org16be6b82011-01-28 21:28:56 +0000418}
419
vandebo0f9bad02014-06-19 11:05:39 -0700420static bool canSubset(FT_Face face) {
vandebo0f9bad02014-06-19 11:05:39 -0700421 FT_UShort fsType = FT_Get_FSType_Flags(face);
422 return (fsType & FT_FSTYPE_NO_SUBSETTING) == 0;
vandebo0f9bad02014-06-19 11:05:39 -0700423}
424
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000425static bool GetLetterCBox(FT_Face face, char letter, FT_BBox* bbox) {
426 const FT_UInt glyph_id = FT_Get_Char_Index(face, letter);
427 if (!glyph_id)
428 return false;
bungeman@google.comcbe1b542013-12-16 17:02:39 +0000429 if (FT_Load_Glyph(face, glyph_id, FT_LOAD_NO_SCALE) != 0)
430 return false;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000431 FT_Outline_Get_CBox(&face->glyph->outline, bbox);
432 return true;
433}
434
bungeman5ec443c2014-11-21 13:18:34 -0800435static void populate_glyph_to_unicode(FT_Face& face, SkTDArray<SkUnichar>* glyphToUnicode) {
halcanaryf8c74a12016-04-20 08:37:43 -0700436 FT_Long numGlyphs = face->num_glyphs;
437 glyphToUnicode->setCount(SkToInt(numGlyphs));
438 sk_bzero(glyphToUnicode->begin(), sizeof((*glyphToUnicode)[0]) * numGlyphs);
vandebo@chromium.org6744d492011-05-09 18:13:47 +0000439
bungeman726cf902015-06-05 13:38:12 -0700440 FT_UInt glyphIndex;
441 SkUnichar charCode = FT_Get_First_Char(face, &glyphIndex);
442 while (glyphIndex) {
halcanaryf8c74a12016-04-20 08:37:43 -0700443 SkASSERT(glyphIndex < SkToUInt(numGlyphs));
bungeman726cf902015-06-05 13:38:12 -0700444 (*glyphToUnicode)[glyphIndex] = charCode;
445 charCode = FT_Get_Next_Char(face, charCode, &glyphIndex);
vandebo@chromium.org6744d492011-05-09 18:13:47 +0000446 }
447}
448
reed@google.com2689f612013-03-20 20:01:47 +0000449SkAdvancedTypefaceMetrics* SkTypeface_FreeType::onGetAdvancedTypefaceMetrics(
reed39a9a502015-05-12 09:50:04 -0700450 PerGlyphInfo perGlyphInfo,
vandebo@chromium.org37ad8fb2011-08-18 02:38:50 +0000451 const uint32_t* glyphIDs,
reed@google.com2689f612013-03-20 20:01:47 +0000452 uint32_t glyphIDsCount) const {
djsollen@google.comda957722011-11-16 17:00:46 +0000453#if defined(SK_BUILD_FOR_MAC)
halcanary96fcdcc2015-08-27 07:41:13 -0700454 return nullptr;
reed@google.com8a5d6922011-03-14 15:08:03 +0000455#else
reed@google.comb4162b12013-07-02 16:32:29 +0000456 AutoFTAccess fta(this);
457 FT_Face face = fta.face();
458 if (!face) {
halcanary96fcdcc2015-08-27 07:41:13 -0700459 return nullptr;
reed@google.comb4162b12013-07-02 16:32:29 +0000460 }
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000461
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000462 SkAdvancedTypefaceMetrics* info = new SkAdvancedTypefaceMetrics;
463 info->fFontName.set(FT_Get_Postscript_Name(face));
vandebo0f9bad02014-06-19 11:05:39 -0700464 info->fFlags = SkAdvancedTypefaceMetrics::kEmpty_FontFlag;
465 if (FT_HAS_MULTIPLE_MASTERS(face)) {
466 info->fFlags = SkTBitOr<SkAdvancedTypefaceMetrics::FontFlags>(
467 info->fFlags, SkAdvancedTypefaceMetrics::kMultiMaster_FontFlag);
468 }
469 if (!canEmbed(face)) {
470 info->fFlags = SkTBitOr<SkAdvancedTypefaceMetrics::FontFlags>(
471 info->fFlags,
472 SkAdvancedTypefaceMetrics::kNotEmbeddable_FontFlag);
473 }
474 if (!canSubset(face)) {
475 info->fFlags = SkTBitOr<SkAdvancedTypefaceMetrics::FontFlags>(
476 info->fFlags,
477 SkAdvancedTypefaceMetrics::kNotSubsettable_FontFlag);
478 }
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000479 info->fLastGlyphID = face->num_glyphs - 1;
480 info->fEmSize = 1000;
481
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000482 bool cid = false;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000483 const char* fontType = FT_Get_X11_Font_Format(face);
vandebo@chromium.orgc3a2ae52011-02-03 21:48:23 +0000484 if (strcmp(fontType, "Type 1") == 0) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000485 info->fType = SkAdvancedTypefaceMetrics::kType1_Font;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000486 } else if (strcmp(fontType, "CID Type 1") == 0) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000487 info->fType = SkAdvancedTypefaceMetrics::kType1CID_Font;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000488 cid = true;
489 } else if (strcmp(fontType, "CFF") == 0) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000490 info->fType = SkAdvancedTypefaceMetrics::kCFF_Font;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000491 } else if (strcmp(fontType, "TrueType") == 0) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000492 info->fType = SkAdvancedTypefaceMetrics::kTrueType_Font;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000493 cid = true;
494 TT_Header* ttHeader;
495 if ((ttHeader = (TT_Header*)FT_Get_Sfnt_Table(face,
halcanary96fcdcc2015-08-27 07:41:13 -0700496 ft_sfnt_head)) != nullptr) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000497 info->fEmSize = ttHeader->Units_Per_EM;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000498 }
bungeman@google.com4d71db82013-12-02 19:10:02 +0000499 } else {
500 info->fType = SkAdvancedTypefaceMetrics::kOther_Font;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000501 }
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000502
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000503 info->fStyle = 0;
504 if (FT_IS_FIXED_WIDTH(face))
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000505 info->fStyle |= SkAdvancedTypefaceMetrics::kFixedPitch_Style;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000506 if (face->style_flags & FT_STYLE_FLAG_ITALIC)
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000507 info->fStyle |= SkAdvancedTypefaceMetrics::kItalic_Style;
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000508
509 PS_FontInfoRec ps_info;
510 TT_Postscript* tt_info;
511 if (FT_Get_PS_Font_Info(face, &ps_info) == 0) {
512 info->fItalicAngle = ps_info.italic_angle;
513 } else if ((tt_info =
514 (TT_Postscript*)FT_Get_Sfnt_Table(face,
halcanary96fcdcc2015-08-27 07:41:13 -0700515 ft_sfnt_post)) != nullptr) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000516 info->fItalicAngle = SkFixedToScalar(tt_info->italicAngle);
517 } else {
518 info->fItalicAngle = 0;
519 }
520
521 info->fAscent = face->ascender;
522 info->fDescent = face->descender;
523
524 // Figure out a good guess for StemV - Min width of i, I, !, 1.
525 // This probably isn't very good with an italic font.
526 int16_t min_width = SHRT_MAX;
vandebo@chromium.org6f72d1e2011-02-14 23:19:59 +0000527 info->fStemV = 0;
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000528 char stem_chars[] = {'i', 'I', '!', '1'};
529 for (size_t i = 0; i < SK_ARRAY_COUNT(stem_chars); i++) {
530 FT_BBox bbox;
531 if (GetLetterCBox(face, stem_chars[i], &bbox)) {
532 int16_t width = bbox.xMax - bbox.xMin;
533 if (width > 0 && width < min_width) {
534 min_width = width;
535 info->fStemV = min_width;
536 }
537 }
538 }
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000539
540 TT_PCLT* pclt_info;
541 TT_OS2* os2_table;
halcanary96fcdcc2015-08-27 07:41:13 -0700542 if ((pclt_info = (TT_PCLT*)FT_Get_Sfnt_Table(face, ft_sfnt_pclt)) != nullptr) {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000543 info->fCapHeight = pclt_info->CapHeight;
544 uint8_t serif_style = pclt_info->SerifStyle & 0x3F;
545 if (serif_style >= 2 && serif_style <= 6)
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000546 info->fStyle |= SkAdvancedTypefaceMetrics::kSerif_Style;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000547 else if (serif_style >= 9 && serif_style <= 12)
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000548 info->fStyle |= SkAdvancedTypefaceMetrics::kScript_Style;
halcanary96fcdcc2015-08-27 07:41:13 -0700549 } else if (((os2_table = (TT_OS2*)FT_Get_Sfnt_Table(face, ft_sfnt_os2)) != nullptr) &&
bungeman@google.comcbe1b542013-12-16 17:02:39 +0000550 // sCapHeight is available only when version 2 or later.
551 os2_table->version != 0xFFFF &&
552 os2_table->version >= 2) {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000553 info->fCapHeight = os2_table->sCapHeight;
554 } else {
555 // Figure out a good guess for CapHeight: average the height of M and X.
556 FT_BBox m_bbox, x_bbox;
557 bool got_m, got_x;
558 got_m = GetLetterCBox(face, 'M', &m_bbox);
559 got_x = GetLetterCBox(face, 'X', &x_bbox);
560 if (got_m && got_x) {
561 info->fCapHeight = (m_bbox.yMax - m_bbox.yMin + x_bbox.yMax -
562 x_bbox.yMin) / 2;
563 } else if (got_m && !got_x) {
564 info->fCapHeight = m_bbox.yMax - m_bbox.yMin;
565 } else if (!got_m && got_x) {
566 info->fCapHeight = x_bbox.yMax - x_bbox.yMin;
bungeman@google.com12bd4a02013-12-19 19:34:22 +0000567 } else {
568 // Last resort, use the ascent.
569 info->fCapHeight = info->fAscent;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000570 }
571 }
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000572
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000573 info->fBBox = SkIRect::MakeLTRB(face->bbox.xMin, face->bbox.yMax,
574 face->bbox.xMax, face->bbox.yMin);
575
vandebo0f9bad02014-06-19 11:05:39 -0700576 if (!FT_IS_SCALABLE(face)) {
reed39a9a502015-05-12 09:50:04 -0700577 perGlyphInfo = kNo_PerGlyphInfo;
vandebo@chromium.org325cb9a2011-03-30 18:36:29 +0000578 }
579
reed39a9a502015-05-12 09:50:04 -0700580 if (perGlyphInfo & kHAdvance_PerGlyphInfo) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000581 if (FT_IS_FIXED_WIDTH(face)) {
halcanarye20a8752016-05-08 18:47:16 -0700582 SkAdvancedTypefaceMetrics::WidthRange range(0);
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000583 int16_t advance = face->max_advance_width;
halcanarye20a8752016-05-08 18:47:16 -0700584 range.fAdvance.append(1, &advance);
585 SkAdvancedTypefaceMetrics::FinishRange(
586 &range, 0, SkAdvancedTypefaceMetrics::WidthRange::kDefault);
587 info->fGlyphWidths.emplace_back(std::move(range));
vandebo@chromium.org6f72d1e2011-02-14 23:19:59 +0000588 } else if (!cid) {
halcanarye20a8752016-05-08 18:47:16 -0700589 SkAdvancedTypefaceMetrics::WidthRange range(0);
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000590 // So as to not blow out the stack, get advances in batches.
591 for (int gID = 0; gID < face->num_glyphs; gID += 128) {
592 FT_Fixed advances[128];
593 int advanceCount = 128;
bungeman5ec443c2014-11-21 13:18:34 -0800594 if (gID + advanceCount > face->num_glyphs) {
bungeman@google.comb8aa4dd2013-10-15 18:50:00 +0000595 advanceCount = face->num_glyphs - gID;
bungeman5ec443c2014-11-21 13:18:34 -0800596 }
597 FT_Get_Advances(face, gID, advanceCount, FT_LOAD_NO_SCALE, advances);
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000598 for (int i = 0; i < advanceCount; i++) {
vandebo@chromium.orgce8a1952012-10-22 20:09:31 +0000599 int16_t advance = advances[i];
halcanarye20a8752016-05-08 18:47:16 -0700600 range.fAdvance.append(1, &advance);
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000601 }
602 }
halcanarye20a8752016-05-08 18:47:16 -0700603 SkAdvancedTypefaceMetrics::FinishRange(
604 &range, face->num_glyphs - 1,
605 SkAdvancedTypefaceMetrics::WidthRange::kRange);
606 info->fGlyphWidths.emplace_back(std::move(range));
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000607 } else {
halcanary57cd94a2016-05-09 06:34:39 -0700608 info->setGlyphWidths(
bungeman5a493cd2016-05-09 08:41:01 -0700609 face->num_glyphs,
610 glyphIDs,
611 glyphIDsCount,
612 SkAdvancedTypefaceMetrics::GetAdvance([face](int gId, int16_t* data) {
613 FT_Fixed advance = 0;
614 if (FT_Get_Advances(face, gId, 1, FT_LOAD_NO_SCALE, &advance)) {
615 return false;
616 }
617 SkASSERT(data);
618 *data = advance;
619 return true;
620 })
621 );
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000622 }
vandebo@chromium.org325cb9a2011-03-30 18:36:29 +0000623 }
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000624
reed39a9a502015-05-12 09:50:04 -0700625 if (perGlyphInfo & kVAdvance_PerGlyphInfo &&
vandebo@chromium.org325cb9a2011-03-30 18:36:29 +0000626 FT_HAS_VERTICAL(face)) {
627 SkASSERT(false); // Not implemented yet.
628 }
629
reed39a9a502015-05-12 09:50:04 -0700630 if (perGlyphInfo & kGlyphNames_PerGlyphInfo &&
vandebo@chromium.org325cb9a2011-03-30 18:36:29 +0000631 info->fType == SkAdvancedTypefaceMetrics::kType1_Font) {
632 // Postscript fonts may contain more than 255 glyphs, so we end up
633 // using multiple font descriptions with a glyph ordering. Record
634 // the name of each glyph.
635 info->fGlyphNames.reset(
636 new SkAutoTArray<SkString>(face->num_glyphs));
637 for (int gID = 0; gID < face->num_glyphs; gID++) {
638 char glyphName[128]; // PS limit for names is 127 bytes.
639 FT_Get_Glyph_Name(face, gID, glyphName, 128);
640 info->fGlyphNames->get()[gID].set(glyphName);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000641 }
642 }
643
reed39a9a502015-05-12 09:50:04 -0700644 if (perGlyphInfo & kToUnicode_PerGlyphInfo &&
vandebo@chromium.org6744d492011-05-09 18:13:47 +0000645 info->fType != SkAdvancedTypefaceMetrics::kType1_Font &&
646 face->num_charmaps) {
647 populate_glyph_to_unicode(face, &(info->fGlyphToUnicode));
648 }
649
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000650 return info;
reed@google.com8a5d6922011-03-14 15:08:03 +0000651#endif
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000652}
vandebo@chromium.org37ad8fb2011-08-18 02:38:50 +0000653
reed@google.com618ef5e2011-01-26 22:10:41 +0000654///////////////////////////////////////////////////////////////////////////
655
reed@google.com8ed436c2011-07-21 14:12:36 +0000656static bool bothZero(SkScalar a, SkScalar b) {
657 return 0 == a && 0 == b;
658}
659
660// returns false if there is any non-90-rotation or skew
661static bool isAxisAligned(const SkScalerContext::Rec& rec) {
662 return 0 == rec.fPreSkewX &&
663 (bothZero(rec.fPost2x2[0][1], rec.fPost2x2[1][0]) ||
664 bothZero(rec.fPost2x2[0][0], rec.fPost2x2[1][1]));
665}
666
reeda9322c22016-04-12 06:47:05 -0700667SkScalerContext* SkTypeface_FreeType::onCreateScalerContext(const SkScalerContextEffects& effects,
668 const SkDescriptor* desc) const {
halcanary385fe4d2015-08-26 13:07:48 -0700669 SkScalerContext_FreeType* c =
reeda9322c22016-04-12 06:47:05 -0700670 new SkScalerContext_FreeType(const_cast<SkTypeface_FreeType*>(this), effects, desc);
reed@google.com0da48612013-03-19 16:06:52 +0000671 if (!c->success()) {
halcanary385fe4d2015-08-26 13:07:48 -0700672 delete c;
halcanary96fcdcc2015-08-27 07:41:13 -0700673 c = nullptr;
reed@google.com0da48612013-03-19 16:06:52 +0000674 }
675 return c;
676}
677
678void SkTypeface_FreeType::onFilterRec(SkScalerContextRec* rec) const {
bungeman@google.com8cf32262012-04-02 14:34:30 +0000679 //BOGUS: http://code.google.com/p/chromium/issues/detail?id=121119
680 //Cap the requested size as larger sizes give bogus values.
681 //Remove when http://code.google.com/p/skia/issues/detail?id=554 is fixed.
bungemanaabd71c2016-03-01 15:15:09 -0800682 //Note that this also currently only protects against large text size requests,
683 //the total matrix is not taken into account here.
bungeman@google.com5582e632012-04-02 14:51:54 +0000684 if (rec->fTextSize > SkIntToScalar(1 << 14)) {
scroggo@google.com94bc60f2012-10-04 20:45:06 +0000685 rec->fTextSize = SkIntToScalar(1 << 14);
bungeman@google.com8cf32262012-04-02 14:34:30 +0000686 }
skia.committer@gmail.coma27096b2012-08-30 14:38:00 +0000687
bungemanec7e12f2015-01-21 11:55:16 -0800688 if (isLCD(*rec)) {
bungemand4742fa2015-01-21 11:19:22 -0800689 // TODO: re-work so that FreeType is set-up and selected by the SkFontMgr.
690 SkAutoMutexAcquire ama(gFTMutex);
691 ref_ft_library();
bungemanec7e12f2015-01-21 11:55:16 -0800692 if (!gFTLibrary->isLCDSupported()) {
bungemand4742fa2015-01-21 11:19:22 -0800693 // If the runtime Freetype library doesn't support LCD, disable it here.
694 rec->fMaskFormat = SkMask::kA8_Format;
695 }
696 unref_ft_library();
reed@google.com618ef5e2011-01-26 22:10:41 +0000697 }
reed@google.com5b31b0f2011-02-23 14:41:42 +0000698
reed@google.com618ef5e2011-01-26 22:10:41 +0000699 SkPaint::Hinting h = rec->getHinting();
reed@google.comeffc5012011-06-27 16:44:46 +0000700 if (SkPaint::kFull_Hinting == h && !isLCD(*rec)) {
reed@google.com618ef5e2011-01-26 22:10:41 +0000701 // collapse full->normal hinting if we're not doing LCD
702 h = SkPaint::kNormal_Hinting;
reed@google.com618ef5e2011-01-26 22:10:41 +0000703 }
bungeman@google.comf4f2b802012-03-08 19:19:51 +0000704 if ((rec->fFlags & SkScalerContext::kSubpixelPositioning_Flag)) {
reed@google.com1ac83502012-02-28 17:06:02 +0000705 if (SkPaint::kNo_Hinting != h) {
706 h = SkPaint::kSlight_Hinting;
707 }
708 }
709
reed@google.com8ed436c2011-07-21 14:12:36 +0000710 // rotated text looks bad with hinting, so we disable it as needed
711 if (!isAxisAligned(*rec)) {
712 h = SkPaint::kNo_Hinting;
713 }
reed@google.com618ef5e2011-01-26 22:10:41 +0000714 rec->setHinting(h);
reed@google.comffe49f52011-11-22 19:42:41 +0000715
bungeman@google.com97efada2012-07-30 20:40:50 +0000716#ifndef SK_GAMMA_APPLY_TO_A8
717 if (!isLCD(*rec)) {
brianosmana1e8f8d2016-04-08 06:47:54 -0700718 // SRGBTODO: Is this correct? Do we want contrast boost?
719 rec->ignorePreBlend();
reed@google.comffe49f52011-11-22 19:42:41 +0000720 }
reed@google.com1ac83502012-02-28 17:06:02 +0000721#endif
reed@google.com618ef5e2011-01-26 22:10:41 +0000722}
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000723
reed@google.com38c37dd2013-03-21 15:36:26 +0000724int SkTypeface_FreeType::onGetUPEM() const {
reed@google.comb4162b12013-07-02 16:32:29 +0000725 AutoFTAccess fta(this);
726 FT_Face face = fta.face();
727 return face ? face->units_per_EM : 0;
djsollen@google.comcd9d69b2011-03-14 20:30:14 +0000728}
djsollen@google.comcd9d69b2011-03-14 20:30:14 +0000729
reed@google.com35fe7372013-10-30 15:07:03 +0000730bool SkTypeface_FreeType::onGetKerningPairAdjustments(const uint16_t glyphs[],
731 int count, int32_t adjustments[]) const {
732 AutoFTAccess fta(this);
733 FT_Face face = fta.face();
734 if (!face || !FT_HAS_KERNING(face)) {
735 return false;
736 }
737
738 for (int i = 0; i < count - 1; ++i) {
739 FT_Vector delta;
740 FT_Error err = FT_Get_Kerning(face, glyphs[i], glyphs[i+1],
741 FT_KERNING_UNSCALED, &delta);
742 if (err) {
743 return false;
744 }
745 adjustments[i] = delta.x;
746 }
747 return true;
748}
749
benjaminwagner45345622016-02-19 15:30:20 -0800750static FT_Int chooseBitmapStrike(FT_Face face, FT_F26Dot6 scaleY) {
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +0000751 // early out if face is bad
halcanary96fcdcc2015-08-27 07:41:13 -0700752 if (face == nullptr) {
753 SkDEBUGF(("chooseBitmapStrike aborted due to nullptr face\n"));
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +0000754 return -1;
755 }
756 // determine target ppem
benjaminwagner45345622016-02-19 15:30:20 -0800757 FT_Pos targetPPEM = scaleY;
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +0000758 // find a bitmap strike equal to or just larger than the requested size
759 FT_Int chosenStrikeIndex = -1;
760 FT_Pos chosenPPEM = 0;
761 for (FT_Int strikeIndex = 0; strikeIndex < face->num_fixed_sizes; ++strikeIndex) {
762 FT_Pos thisPPEM = face->available_sizes[strikeIndex].y_ppem;
763 if (thisPPEM == targetPPEM) {
764 // exact match - our search stops here
765 chosenPPEM = thisPPEM;
766 chosenStrikeIndex = strikeIndex;
767 break;
768 } else if (chosenPPEM < targetPPEM) {
769 // attempt to increase chosenPPEM
770 if (thisPPEM > chosenPPEM) {
771 chosenPPEM = thisPPEM;
772 chosenStrikeIndex = strikeIndex;
773 }
774 } else {
775 // attempt to decrease chosenPPEM, but not below targetPPEM
776 if (thisPPEM < chosenPPEM && thisPPEM > targetPPEM) {
777 chosenPPEM = thisPPEM;
778 chosenStrikeIndex = strikeIndex;
779 }
780 }
781 }
782 if (chosenStrikeIndex != -1) {
783 // use the chosen strike
784 FT_Error err = FT_Select_Size(face, chosenStrikeIndex);
785 if (err != 0) {
786 SkDEBUGF(("FT_Select_Size(%s, %d) returned 0x%x\n", face->family_name,
787 chosenStrikeIndex, err));
788 chosenStrikeIndex = -1;
789 }
790 }
791 return chosenStrikeIndex;
792}
793
reeda9322c22016-04-12 06:47:05 -0700794SkScalerContext_FreeType::SkScalerContext_FreeType(SkTypeface* typeface,
795 const SkScalerContextEffects& effects,
796 const SkDescriptor* desc)
797 : SkScalerContext_FreeType_Base(typeface, effects, desc)
bungemanaabd71c2016-03-01 15:15:09 -0800798 , fFace(nullptr)
799 , fFTSize(nullptr)
800 , fStrikeIndex(-1)
bungeman13a007d2015-06-19 05:09:39 -0700801{
reed@android.com8a1c16f2008-12-17 15:59:43 +0000802 SkAutoMutexAcquire ac(gFTMutex);
803
bungeman9dc24682014-12-01 14:01:32 -0800804 if (!ref_ft_library()) {
805 sk_throw();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000806 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000807
808 // load the font file
bungemanaabd71c2016-03-01 15:15:09 -0800809 using UnrefFTFace = SkFunctionWrapper<void, skstd::remove_pointer_t<FT_Face>, unref_ft_face>;
mtklein5f939ab2016-03-16 10:28:35 -0700810 std::unique_ptr<skstd::remove_pointer_t<FT_Face>, UnrefFTFace> ftFace(ref_ft_face(typeface));
bungemanaabd71c2016-03-01 15:15:09 -0800811 if (nullptr == ftFace) {
812 SkDEBUGF(("Could not create FT_Face.\n"));
reed@android.com62900b42009-02-11 15:07:19 +0000813 return;
814 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000815
bungeman5f14c5e2014-12-05 12:26:44 -0800816 fRec.computeMatrices(SkScalerContextRec::kFull_PreMatrixScale, &fScale, &fMatrix22Scalar);
817 fMatrix22Scalar.setSkewX(-fMatrix22Scalar.getSkewX());
818 fMatrix22Scalar.setSkewY(-fMatrix22Scalar.getSkewY());
reed@android.com8a1c16f2008-12-17 15:59:43 +0000819
benjaminwagner45345622016-02-19 15:30:20 -0800820 fScaleX = SkScalarToFDot6(fScale.fX);
821 fScaleY = SkScalarToFDot6(fScale.fY);
bungeman@google.comd3fbd342014-04-15 15:52:07 +0000822 fMatrix22.xx = SkScalarToFixed(fMatrix22Scalar.getScaleX());
823 fMatrix22.xy = SkScalarToFixed(fMatrix22Scalar.getSkewX());
824 fMatrix22.yx = SkScalarToFixed(fMatrix22Scalar.getSkewY());
825 fMatrix22.yy = SkScalarToFixed(fMatrix22Scalar.getScaleY());
reed@android.com8a1c16f2008-12-17 15:59:43 +0000826
reed@google.coma1bfa212012-03-08 21:57:12 +0000827 fLCDIsVert = SkToBool(fRec.fFlags & SkScalerContext::kLCD_Vertical_Flag);
828
reed@android.com8a1c16f2008-12-17 15:59:43 +0000829 // compute the flags we send to Load_Glyph
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +0000830 bool linearMetrics = SkToBool(fRec.fFlags & SkScalerContext::kSubpixelPositioning_Flag);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000831 {
reed@android.come4d0bc02009-07-24 19:53:20 +0000832 FT_Int32 loadFlags = FT_LOAD_DEFAULT;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000833
agl@chromium.org70a303f2010-05-10 14:15:50 +0000834 if (SkMask::kBW_Format == fRec.fMaskFormat) {
835 // See http://code.google.com/p/chromium/issues/detail?id=43252#c24
836 loadFlags = FT_LOAD_TARGET_MONO;
reed@google.comeffc5012011-06-27 16:44:46 +0000837 if (fRec.getHinting() == SkPaint::kNo_Hinting) {
agl@chromium.org70a303f2010-05-10 14:15:50 +0000838 loadFlags = FT_LOAD_NO_HINTING;
reed@google.combdc99882011-11-21 14:36:57 +0000839 linearMetrics = true;
reed@google.comeffc5012011-06-27 16:44:46 +0000840 }
agl@chromium.org70a303f2010-05-10 14:15:50 +0000841 } else {
842 switch (fRec.getHinting()) {
843 case SkPaint::kNo_Hinting:
844 loadFlags = FT_LOAD_NO_HINTING;
reed@google.combdc99882011-11-21 14:36:57 +0000845 linearMetrics = true;
agl@chromium.org70a303f2010-05-10 14:15:50 +0000846 break;
847 case SkPaint::kSlight_Hinting:
848 loadFlags = FT_LOAD_TARGET_LIGHT; // This implies FORCE_AUTOHINT
849 break;
850 case SkPaint::kNormal_Hinting:
bungeman@google.comf6f56872014-01-23 19:01:36 +0000851 if (fRec.fFlags & SkScalerContext::kForceAutohinting_Flag) {
agl@chromium.orga2c71cb2010-06-17 20:49:17 +0000852 loadFlags = FT_LOAD_FORCE_AUTOHINT;
djsollen858a7892014-08-20 07:03:23 -0700853#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
854 } else {
855 loadFlags = FT_LOAD_NO_AUTOHINT;
856#endif
bungeman@google.comf6f56872014-01-23 19:01:36 +0000857 }
agl@chromium.org70a303f2010-05-10 14:15:50 +0000858 break;
859 case SkPaint::kFull_Hinting:
bungeman@google.comf6f56872014-01-23 19:01:36 +0000860 if (fRec.fFlags & SkScalerContext::kForceAutohinting_Flag) {
agl@chromium.orga2c71cb2010-06-17 20:49:17 +0000861 loadFlags = FT_LOAD_FORCE_AUTOHINT;
862 break;
863 }
agl@chromium.org70a303f2010-05-10 14:15:50 +0000864 loadFlags = FT_LOAD_TARGET_NORMAL;
reed@google.comeffc5012011-06-27 16:44:46 +0000865 if (isLCD(fRec)) {
reed@google.coma1bfa212012-03-08 21:57:12 +0000866 if (fLCDIsVert) {
reed@google.comeffc5012011-06-27 16:44:46 +0000867 loadFlags = FT_LOAD_TARGET_LCD_V;
868 } else {
869 loadFlags = FT_LOAD_TARGET_LCD;
870 }
reed@google.comea2333d2011-03-14 16:44:56 +0000871 }
agl@chromium.org70a303f2010-05-10 14:15:50 +0000872 break;
873 default:
874 SkDebugf("---------- UNKNOWN hinting %d\n", fRec.getHinting());
875 break;
876 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000877 }
878
reed@google.comeffc5012011-06-27 16:44:46 +0000879 if ((fRec.fFlags & SkScalerContext::kEmbeddedBitmapText_Flag) == 0) {
agl@chromium.orge0d08992009-08-07 19:19:23 +0000880 loadFlags |= FT_LOAD_NO_BITMAP;
reed@google.comeffc5012011-06-27 16:44:46 +0000881 }
agl@chromium.orge0d08992009-08-07 19:19:23 +0000882
reed@google.com96a9f7912011-05-06 11:49:30 +0000883 // Always using FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH to get correct
884 // advances, as fontconfig and cairo do.
885 // See http://code.google.com/p/skia/issues/detail?id=222.
886 loadFlags |= FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH;
887
bungeman@google.com8ff8a192012-09-25 20:38:28 +0000888 // Use vertical layout if requested.
889 if (fRec.fFlags & SkScalerContext::kVertical_Flag) {
890 loadFlags |= FT_LOAD_VERTICAL_LAYOUT;
891 }
892
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +0000893 loadFlags |= FT_LOAD_COLOR;
894
reed@android.come4d0bc02009-07-24 19:53:20 +0000895 fLoadGlyphFlags = loadFlags;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000896 }
897
bungemanaabd71c2016-03-01 15:15:09 -0800898 using DoneFTSize = SkFunctionWrapper<FT_Error, skstd::remove_pointer_t<FT_Size>, FT_Done_Size>;
mtklein5f939ab2016-03-16 10:28:35 -0700899 std::unique_ptr<skstd::remove_pointer_t<FT_Size>, DoneFTSize> ftSize([&ftFace]() -> FT_Size {
bungemanaabd71c2016-03-01 15:15:09 -0800900 FT_Size size;
901 FT_Error err = FT_New_Size(ftFace.get(), &size);
902 if (err != 0) {
903 SkDEBUGF(("FT_New_Size returned %x for face %s\n", err, ftFace->family_name));
904 return nullptr;
905 }
906 return size;
907 }());
908 if (nullptr == ftSize) {
909 SkDEBUGF(("Could not create FT_Size.\n"));
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +0000910 return;
911 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000912
bungemanaabd71c2016-03-01 15:15:09 -0800913 FT_Error err = FT_Activate_Size(ftSize.get());
914 if (err != 0) {
915 SkDEBUGF(("FT_Activate_Size(%08x, 0x%x, 0x%x) returned 0x%x\n",
916 ftFace.get(), fScaleX, fScaleY, err));
917 return;
918 }
919
920 if (FT_IS_SCALABLE(ftFace)) {
921 err = FT_Set_Char_Size(ftFace.get(), fScaleX, fScaleY, 72, 72);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000922 if (err != 0) {
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +0000923 SkDEBUGF(("FT_Set_CharSize(%08x, 0x%x, 0x%x) returned 0x%x\n",
bungemanaabd71c2016-03-01 15:15:09 -0800924 ftFace.get(), fScaleX, fScaleY, err));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000925 return;
926 }
bungemanaabd71c2016-03-01 15:15:09 -0800927 FT_Set_Transform(ftFace.get(), &fMatrix22, nullptr);
928 } else if (FT_HAS_FIXED_SIZES(ftFace)) {
929 fStrikeIndex = chooseBitmapStrike(ftFace.get(), fScaleY);
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +0000930 if (fStrikeIndex == -1) {
931 SkDEBUGF(("no glyphs for font \"%s\" size %f?\n",
bungemanaabd71c2016-03-01 15:15:09 -0800932 ftFace->family_name, SkFDot6ToScalar(fScaleY)));
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +0000933 } else {
934 // FreeType does no provide linear metrics for bitmap fonts.
935 linearMetrics = false;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000936
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +0000937 // FreeType documentation says:
938 // FT_LOAD_NO_BITMAP -- Ignore bitmap strikes when loading.
939 // Bitmap-only fonts ignore this flag.
940 //
941 // However, in FreeType 2.5.1 color bitmap only fonts do not ignore this flag.
942 // Force this flag off for bitmap only fonts.
943 fLoadGlyphFlags &= ~FT_LOAD_NO_BITMAP;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000944 }
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +0000945 } else {
946 SkDEBUGF(("unknown kind of font \"%s\" size %f?\n",
benjaminwagner45345622016-02-19 15:30:20 -0800947 fFace->family_name, SkFDot6ToScalar(fScaleY)));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000948 }
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +0000949
bungemanaabd71c2016-03-01 15:15:09 -0800950 fFTSize = ftSize.release();
951 fFace = ftFace.release();
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +0000952 fDoLinearMetrics = linearMetrics;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000953}
954
955SkScalerContext_FreeType::~SkScalerContext_FreeType() {
scroggo@google.com94bc60f2012-10-04 20:45:06 +0000956 SkAutoMutexAcquire ac(gFTMutex);
957
halcanary96fcdcc2015-08-27 07:41:13 -0700958 if (fFTSize != nullptr) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000959 FT_Done_Size(fFTSize);
960 }
961
halcanary96fcdcc2015-08-27 07:41:13 -0700962 if (fFace != nullptr) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000963 unref_ft_face(fFace);
964 }
bungeman9dc24682014-12-01 14:01:32 -0800965
966 unref_ft_library();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000967}
968
969/* We call this before each use of the fFace, since we may be sharing
970 this face with other context (at different sizes).
971*/
972FT_Error SkScalerContext_FreeType::setupSize() {
bungeman3f846ae2015-11-03 11:07:20 -0800973 gFTMutex.assertHeld();
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +0000974 FT_Error err = FT_Activate_Size(fFTSize);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000975 if (err != 0) {
bungeman13a007d2015-06-19 05:09:39 -0700976 SkDEBUGF(("SkScalerContext_FreeType::FT_Activate_Size(%s %s, 0x%x, 0x%x) returned 0x%x\n",
977 fFace->family_name, fFace->style_name, fScaleX, fScaleY, err));
halcanary96fcdcc2015-08-27 07:41:13 -0700978 fFTSize = nullptr;
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +0000979 return err;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000980 }
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +0000981
982 // seems we need to reset this every time (not sure why, but without it
983 // I get random italics from some other fFTSize)
halcanary96fcdcc2015-08-27 07:41:13 -0700984 FT_Set_Transform(fFace, &fMatrix22, nullptr);
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +0000985 return 0;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000986}
987
ctguil@chromium.org0bc7bf52011-03-04 19:04:57 +0000988unsigned SkScalerContext_FreeType::generateGlyphCount() {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000989 return fFace->num_glyphs;
990}
991
992uint16_t SkScalerContext_FreeType::generateCharToGlyph(SkUnichar uni) {
bungeman3f846ae2015-11-03 11:07:20 -0800993 SkAutoMutexAcquire ac(gFTMutex);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000994 return SkToU16(FT_Get_Char_Index( fFace, uni ));
995}
996
reed@android.com9d3a9852010-01-08 14:07:42 +0000997SkUnichar SkScalerContext_FreeType::generateGlyphToChar(uint16_t glyph) {
bungeman3f846ae2015-11-03 11:07:20 -0800998 SkAutoMutexAcquire ac(gFTMutex);
reed@android.com9d3a9852010-01-08 14:07:42 +0000999 // iterate through each cmap entry, looking for matching glyph indices
1000 FT_UInt glyphIndex;
1001 SkUnichar charCode = FT_Get_First_Char( fFace, &glyphIndex );
1002
1003 while (glyphIndex != 0) {
1004 if (glyphIndex == glyph) {
1005 return charCode;
1006 }
1007 charCode = FT_Get_Next_Char( fFace, charCode, &glyphIndex );
1008 }
1009
1010 return 0;
1011}
1012
benjaminwagner6b3eacb2016-03-24 19:07:58 -07001013static SkScalar SkFT_FixedToScalar(FT_Fixed x) {
1014 return SkFixedToScalar(x);
1015}
1016
reed@android.com8a1c16f2008-12-17 15:59:43 +00001017void SkScalerContext_FreeType::generateAdvance(SkGlyph* glyph) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001018 /* unhinted and light hinted text have linearly scaled advances
1019 * which are very cheap to compute with some font formats...
1020 */
reed@google.combdc99882011-11-21 14:36:57 +00001021 if (fDoLinearMetrics) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001022 SkAutoMutexAcquire ac(gFTMutex);
1023
1024 if (this->setupSize()) {
reed@android.com62900b42009-02-11 15:07:19 +00001025 glyph->zeroMetrics();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001026 return;
1027 }
1028
1029 FT_Error error;
1030 FT_Fixed advance;
1031
djsollen1b277042014-08-06 06:58:06 -07001032 error = FT_Get_Advance( fFace, glyph->getGlyphID(),
reed@android.com8a1c16f2008-12-17 15:59:43 +00001033 fLoadGlyphFlags | FT_ADVANCE_FLAG_FAST_ONLY,
1034 &advance );
1035 if (0 == error) {
1036 glyph->fRsbDelta = 0;
1037 glyph->fLsbDelta = 0;
benjaminwagner6b3eacb2016-03-24 19:07:58 -07001038 const SkScalar advanceScalar = SkFT_FixedToScalar(advance);
1039 glyph->fAdvanceX = SkScalarToFloat(fMatrix22Scalar.getScaleX() * advanceScalar);
1040 glyph->fAdvanceY = -SkScalarToFloat(fMatrix22Scalar.getSkewY() * advanceScalar);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001041 return;
1042 }
1043 }
bungeman5ec443c2014-11-21 13:18:34 -08001044
reed@android.com8a1c16f2008-12-17 15:59:43 +00001045 /* otherwise, we need to load/hint the glyph, which is slower */
1046 this->generateMetrics(glyph);
1047 return;
1048}
1049
djsollen@google.comd8b599c2012-03-19 19:44:19 +00001050void SkScalerContext_FreeType::getBBoxForCurrentGlyph(SkGlyph* glyph,
1051 FT_BBox* bbox,
1052 bool snapToPixelBoundary) {
1053
1054 FT_Outline_Get_CBox(&fFace->glyph->outline, bbox);
1055
1056 if (fRec.fFlags & SkScalerContext::kSubpixelPositioning_Flag) {
george@mozilla.comc59b5da2012-08-23 00:39:08 +00001057 int dx = SkFixedToFDot6(glyph->getSubXFixed());
1058 int dy = SkFixedToFDot6(glyph->getSubYFixed());
djsollen@google.comd8b599c2012-03-19 19:44:19 +00001059 // negate dy since freetype-y-goes-up and skia-y-goes-down
1060 bbox->xMin += dx;
1061 bbox->yMin -= dy;
1062 bbox->xMax += dx;
1063 bbox->yMax -= dy;
1064 }
1065
1066 // outset the box to integral boundaries
1067 if (snapToPixelBoundary) {
1068 bbox->xMin &= ~63;
1069 bbox->yMin &= ~63;
1070 bbox->xMax = (bbox->xMax + 63) & ~63;
1071 bbox->yMax = (bbox->yMax + 63) & ~63;
1072 }
bungeman@google.com8ff8a192012-09-25 20:38:28 +00001073
1074 // Must come after snapToPixelBoundary so that the width and height are
1075 // consistent. Otherwise asserts will fire later on when generating the
1076 // glyph image.
1077 if (fRec.fFlags & SkScalerContext::kVertical_Flag) {
1078 FT_Vector vector;
1079 vector.x = fFace->glyph->metrics.vertBearingX - fFace->glyph->metrics.horiBearingX;
1080 vector.y = -fFace->glyph->metrics.vertBearingY - fFace->glyph->metrics.horiBearingY;
1081 FT_Vector_Transform(&vector, &fMatrix22);
1082 bbox->xMin += vector.x;
1083 bbox->xMax += vector.x;
1084 bbox->yMin += vector.y;
1085 bbox->yMax += vector.y;
1086 }
djsollen@google.comd8b599c2012-03-19 19:44:19 +00001087}
1088
bungeman@google.comcbe1b542013-12-16 17:02:39 +00001089bool SkScalerContext_FreeType::getCBoxForLetter(char letter, FT_BBox* bbox) {
1090 const FT_UInt glyph_id = FT_Get_Char_Index(fFace, letter);
bungeman5ec443c2014-11-21 13:18:34 -08001091 if (!glyph_id) {
bungeman@google.comcbe1b542013-12-16 17:02:39 +00001092 return false;
bungeman5ec443c2014-11-21 13:18:34 -08001093 }
1094 if (FT_Load_Glyph(fFace, glyph_id, fLoadGlyphFlags) != 0) {
bungeman@google.comcbe1b542013-12-16 17:02:39 +00001095 return false;
bungeman5ec443c2014-11-21 13:18:34 -08001096 }
commit-bot@chromium.org6fa81d72013-12-26 15:50:29 +00001097 emboldenIfNeeded(fFace, fFace->glyph);
bungeman@google.comcbe1b542013-12-16 17:02:39 +00001098 FT_Outline_Get_CBox(&fFace->glyph->outline, bbox);
1099 return true;
1100}
1101
djsollen@google.comd8b599c2012-03-19 19:44:19 +00001102void SkScalerContext_FreeType::updateGlyphIfLCD(SkGlyph* glyph) {
1103 if (isLCD(fRec)) {
1104 if (fLCDIsVert) {
bungeman9dc24682014-12-01 14:01:32 -08001105 glyph->fHeight += gFTLibrary->lcdExtra();
1106 glyph->fTop -= gFTLibrary->lcdExtra() >> 1;
djsollen@google.comd8b599c2012-03-19 19:44:19 +00001107 } else {
bungeman9dc24682014-12-01 14:01:32 -08001108 glyph->fWidth += gFTLibrary->lcdExtra();
1109 glyph->fLeft -= gFTLibrary->lcdExtra() >> 1;
djsollen@google.comd8b599c2012-03-19 19:44:19 +00001110 }
1111 }
1112}
1113
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +00001114inline void scaleGlyphMetrics(SkGlyph& glyph, SkScalar scale) {
1115 glyph.fWidth *= scale;
1116 glyph.fHeight *= scale;
1117 glyph.fTop *= scale;
1118 glyph.fLeft *= scale;
1119
benjaminwagner6b3eacb2016-03-24 19:07:58 -07001120 float floatScale = SkScalarToFloat(scale);
1121 glyph.fAdvanceX *= floatScale;
1122 glyph.fAdvanceY *= floatScale;
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +00001123}
1124
reed@android.com8a1c16f2008-12-17 15:59:43 +00001125void SkScalerContext_FreeType::generateMetrics(SkGlyph* glyph) {
1126 SkAutoMutexAcquire ac(gFTMutex);
1127
1128 glyph->fRsbDelta = 0;
1129 glyph->fLsbDelta = 0;
1130
1131 FT_Error err;
1132
1133 if (this->setupSize()) {
bungeman13a007d2015-06-19 05:09:39 -07001134 glyph->zeroMetrics();
1135 return;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001136 }
1137
djsollen1b277042014-08-06 06:58:06 -07001138 err = FT_Load_Glyph( fFace, glyph->getGlyphID(), fLoadGlyphFlags );
reed@android.com8a1c16f2008-12-17 15:59:43 +00001139 if (err != 0) {
reed@android.com62900b42009-02-11 15:07:19 +00001140 glyph->zeroMetrics();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001141 return;
1142 }
commit-bot@chromium.org6fa81d72013-12-26 15:50:29 +00001143 emboldenIfNeeded(fFace, fFace->glyph);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001144
1145 switch ( fFace->glyph->format ) {
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +00001146 case FT_GLYPH_FORMAT_OUTLINE:
bungeman@google.com0f0c2882011-11-04 15:47:41 +00001147 if (0 == fFace->glyph->outline.n_contours) {
1148 glyph->fWidth = 0;
1149 glyph->fHeight = 0;
1150 glyph->fTop = 0;
1151 glyph->fLeft = 0;
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +00001152 } else {
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +00001153 FT_BBox bbox;
1154 getBBoxForCurrentGlyph(glyph, &bbox, true);
1155
1156 glyph->fWidth = SkToU16(SkFDot6Floor(bbox.xMax - bbox.xMin));
1157 glyph->fHeight = SkToU16(SkFDot6Floor(bbox.yMax - bbox.yMin));
1158 glyph->fTop = -SkToS16(SkFDot6Floor(bbox.yMax));
1159 glyph->fLeft = SkToS16(SkFDot6Floor(bbox.xMin));
1160
1161 updateGlyphIfLCD(glyph);
bungeman@google.com0f0c2882011-11-04 15:47:41 +00001162 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001163 break;
1164
1165 case FT_GLYPH_FORMAT_BITMAP:
bungeman@google.com8ff8a192012-09-25 20:38:28 +00001166 if (fRec.fFlags & SkScalerContext::kVertical_Flag) {
1167 FT_Vector vector;
1168 vector.x = fFace->glyph->metrics.vertBearingX - fFace->glyph->metrics.horiBearingX;
1169 vector.y = -fFace->glyph->metrics.vertBearingY - fFace->glyph->metrics.horiBearingY;
1170 FT_Vector_Transform(&vector, &fMatrix22);
1171 fFace->glyph->bitmap_left += SkFDot6Floor(vector.x);
1172 fFace->glyph->bitmap_top += SkFDot6Floor(vector.y);
1173 }
1174
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +00001175 if (fFace->glyph->bitmap.pixel_mode == FT_PIXEL_MODE_BGRA) {
1176 glyph->fMaskFormat = SkMask::kARGB32_Format;
1177 }
1178
reed@android.com8a1c16f2008-12-17 15:59:43 +00001179 glyph->fWidth = SkToU16(fFace->glyph->bitmap.width);
1180 glyph->fHeight = SkToU16(fFace->glyph->bitmap.rows);
1181 glyph->fTop = -SkToS16(fFace->glyph->bitmap_top);
1182 glyph->fLeft = SkToS16(fFace->glyph->bitmap_left);
1183 break;
1184
1185 default:
tomhudson@google.com0c00f212011-12-28 14:59:50 +00001186 SkDEBUGFAIL("unknown glyph format");
bungeman13a007d2015-06-19 05:09:39 -07001187 glyph->zeroMetrics();
1188 return;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001189 }
1190
bungeman@google.com8ff8a192012-09-25 20:38:28 +00001191 if (fRec.fFlags & SkScalerContext::kVertical_Flag) {
1192 if (fDoLinearMetrics) {
benjaminwagner6b3eacb2016-03-24 19:07:58 -07001193 const SkScalar advanceScalar = SkFT_FixedToScalar(fFace->glyph->linearVertAdvance);
1194 glyph->fAdvanceX = -SkScalarToFloat(fMatrix22Scalar.getSkewX() * advanceScalar);
1195 glyph->fAdvanceY = SkScalarToFloat(fMatrix22Scalar.getScaleY() * advanceScalar);
bungeman@google.com8ff8a192012-09-25 20:38:28 +00001196 } else {
benjaminwagner6b3eacb2016-03-24 19:07:58 -07001197 glyph->fAdvanceX = -SkFDot6ToFloat(fFace->glyph->advance.x);
1198 glyph->fAdvanceY = SkFDot6ToFloat(fFace->glyph->advance.y);
bungeman@google.com8ff8a192012-09-25 20:38:28 +00001199 }
bungeman@google.com34f10262012-03-23 18:11:47 +00001200 } else {
bungeman@google.com8ff8a192012-09-25 20:38:28 +00001201 if (fDoLinearMetrics) {
benjaminwagner6b3eacb2016-03-24 19:07:58 -07001202 const SkScalar advanceScalar = SkFT_FixedToScalar(fFace->glyph->linearHoriAdvance);
1203 glyph->fAdvanceX = SkScalarToFloat(fMatrix22Scalar.getScaleX() * advanceScalar);
1204 glyph->fAdvanceY = -SkScalarToFloat(fMatrix22Scalar.getSkewY() * advanceScalar);
bungeman@google.com8ff8a192012-09-25 20:38:28 +00001205 } else {
benjaminwagner6b3eacb2016-03-24 19:07:58 -07001206 glyph->fAdvanceX = SkFDot6ToFloat(fFace->glyph->advance.x);
1207 glyph->fAdvanceY = -SkFDot6ToFloat(fFace->glyph->advance.y);
bungeman@google.com34f10262012-03-23 18:11:47 +00001208
bungeman@google.com8ff8a192012-09-25 20:38:28 +00001209 if (fRec.fFlags & kDevKernText_Flag) {
1210 glyph->fRsbDelta = SkToS8(fFace->glyph->rsb_delta);
1211 glyph->fLsbDelta = SkToS8(fFace->glyph->lsb_delta);
djsollen@google.comd8b599c2012-03-19 19:44:19 +00001212 }
1213 }
djsollen@google.comd8b599c2012-03-19 19:44:19 +00001214 }
1215
bungemana85511a2014-09-22 12:24:41 -07001216 // If the font isn't scalable, scale the metrics from the non-scalable strike.
1217 // This means do not try to scale embedded bitmaps; only scale bitmaps in bitmap only fonts.
benjaminwagner45345622016-02-19 15:30:20 -08001218 if (!FT_IS_SCALABLE(fFace) && !SkScalarNearlyZero(fScale.fY) && fFace->size->metrics.y_ppem) {
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +00001219 // NOTE: both dimensions are scaled by y_ppem. this is WAI.
benjaminwagner45345622016-02-19 15:30:20 -08001220 scaleGlyphMetrics(*glyph, fScale.fY / fFace->size->metrics.y_ppem);
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +00001221 }
djsollen@google.comd8b599c2012-03-19 19:44:19 +00001222
reed@android.com8a1c16f2008-12-17 15:59:43 +00001223#ifdef ENABLE_GLYPH_SPEW
1224 SkDEBUGF(("FT_Set_Char_Size(this:%p sx:%x sy:%x ", this, fScaleX, fScaleY));
djsollen1b277042014-08-06 06:58:06 -07001225 SkDEBUGF(("Metrics(glyph:%d flags:0x%x) w:%d\n", glyph->getGlyphID(), fLoadGlyphFlags, glyph->fWidth));
reed@android.com8a1c16f2008-12-17 15:59:43 +00001226#endif
1227}
1228
bungeman5ec443c2014-11-21 13:18:34 -08001229static void clear_glyph_image(const SkGlyph& glyph) {
1230 sk_bzero(glyph.fImage, glyph.rowBytes() * glyph.fHeight);
1231}
reed@google.comea2333d2011-03-14 16:44:56 +00001232
bungeman@google.coma76de722012-10-26 19:35:54 +00001233void SkScalerContext_FreeType::generateImage(const SkGlyph& glyph) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001234 SkAutoMutexAcquire ac(gFTMutex);
1235
reed@android.com8a1c16f2008-12-17 15:59:43 +00001236 if (this->setupSize()) {
bungeman5ec443c2014-11-21 13:18:34 -08001237 clear_glyph_image(glyph);
1238 return;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001239 }
1240
bungeman5ec443c2014-11-21 13:18:34 -08001241 FT_Error err = FT_Load_Glyph(fFace, glyph.getGlyphID(), fLoadGlyphFlags);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001242 if (err != 0) {
1243 SkDEBUGF(("SkScalerContext_FreeType::generateImage: FT_Load_Glyph(glyph:%d width:%d height:%d rb:%d flags:%d) returned 0x%x\n",
bungeman5ec443c2014-11-21 13:18:34 -08001244 glyph.getGlyphID(), glyph.fWidth, glyph.fHeight, glyph.rowBytes(), fLoadGlyphFlags, err));
1245 clear_glyph_image(glyph);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001246 return;
1247 }
1248
commit-bot@chromium.org6fa81d72013-12-26 15:50:29 +00001249 emboldenIfNeeded(fFace, fFace->glyph);
bungeman@google.coma76de722012-10-26 19:35:54 +00001250 generateGlyphImage(fFace, glyph);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001251}
1252
reed@android.com8a1c16f2008-12-17 15:59:43 +00001253
bungeman5ec443c2014-11-21 13:18:34 -08001254void SkScalerContext_FreeType::generatePath(const SkGlyph& glyph, SkPath* path) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001255 SkAutoMutexAcquire ac(gFTMutex);
1256
caryclarka10742c2014-09-18 11:00:40 -07001257 SkASSERT(path);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001258
1259 if (this->setupSize()) {
1260 path->reset();
1261 return;
1262 }
1263
1264 uint32_t flags = fLoadGlyphFlags;
1265 flags |= FT_LOAD_NO_BITMAP; // ignore embedded bitmaps so we're sure to get the outline
1266 flags &= ~FT_LOAD_RENDER; // don't scan convert (we just want the outline)
1267
djsollen1b277042014-08-06 06:58:06 -07001268 FT_Error err = FT_Load_Glyph( fFace, glyph.getGlyphID(), flags);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001269
1270 if (err != 0) {
1271 SkDEBUGF(("SkScalerContext_FreeType::generatePath: FT_Load_Glyph(glyph:%d flags:%d) returned 0x%x\n",
djsollen1b277042014-08-06 06:58:06 -07001272 glyph.getGlyphID(), flags, err));
reed@android.com8a1c16f2008-12-17 15:59:43 +00001273 path->reset();
1274 return;
1275 }
commit-bot@chromium.org6fa81d72013-12-26 15:50:29 +00001276 emboldenIfNeeded(fFace, fFace->glyph);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001277
sugoi@google.com66a58ac2013-03-05 20:40:52 +00001278 generateGlyphPath(fFace, path);
bungeman@google.com8ff8a192012-09-25 20:38:28 +00001279
1280 // The path's origin from FreeType is always the horizontal layout origin.
1281 // Offset the path so that it is relative to the vertical origin if needed.
1282 if (fRec.fFlags & SkScalerContext::kVertical_Flag) {
1283 FT_Vector vector;
1284 vector.x = fFace->glyph->metrics.vertBearingX - fFace->glyph->metrics.horiBearingX;
1285 vector.y = -fFace->glyph->metrics.vertBearingY - fFace->glyph->metrics.horiBearingY;
1286 FT_Vector_Transform(&vector, &fMatrix22);
1287 path->offset(SkFDot6ToScalar(vector.x), -SkFDot6ToScalar(vector.y));
1288 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001289}
1290
bungeman41078062014-07-07 08:16:37 -07001291void SkScalerContext_FreeType::generateFontMetrics(SkPaint::FontMetrics* metrics) {
halcanary96fcdcc2015-08-27 07:41:13 -07001292 if (nullptr == metrics) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001293 return;
1294 }
1295
bungeman41078062014-07-07 08:16:37 -07001296 SkAutoMutexAcquire ac(gFTMutex);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001297
1298 if (this->setupSize()) {
bungeman41078062014-07-07 08:16:37 -07001299 sk_bzero(metrics, sizeof(*metrics));
reed@android.com8a1c16f2008-12-17 15:59:43 +00001300 return;
1301 }
1302
reed@android.coma8a8b8b2009-05-04 15:00:11 +00001303 FT_Face face = fFace;
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +00001304 SkScalar scaleX = fScale.x();
reed@google.comf073b332013-05-06 12:21:16 +00001305 SkScalar scaleY = fScale.y();
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +00001306 SkScalar mxy = fMatrix22Scalar.getSkewX() * scaleY;
1307 SkScalar myy = fMatrix22Scalar.getScaleY() * scaleY;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001308
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +00001309 // fetch units/EM from "head" table if needed (ie for bitmap fonts)
1310 SkScalar upem = SkIntToScalar(face->units_per_EM);
1311 if (!upem) {
1312 TT_Header* ttHeader = (TT_Header*)FT_Get_Sfnt_Table(face, ft_sfnt_head);
1313 if (ttHeader) {
1314 upem = SkIntToScalar(ttHeader->Units_Per_EM);
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001315 }
1316 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001317
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +00001318 // use the os/2 table as a source of reasonable defaults.
1319 SkScalar x_height = 0.0f;
1320 SkScalar avgCharWidth = 0.0f;
bungeman@google.comcbe1b542013-12-16 17:02:39 +00001321 SkScalar cap_height = 0.0f;
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +00001322 TT_OS2* os2 = (TT_OS2*) FT_Get_Sfnt_Table(face, ft_sfnt_os2);
1323 if (os2) {
1324 x_height = scaleX * SkIntToScalar(os2->sxHeight) / upem;
1325 avgCharWidth = SkIntToScalar(os2->xAvgCharWidth) / upem;
bungeman@google.comcbe1b542013-12-16 17:02:39 +00001326 if (os2->version != 0xFFFF && os2->version >= 2) {
1327 cap_height = scaleX * SkIntToScalar(os2->sCapHeight) / upem;
1328 }
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +00001329 }
1330
1331 // pull from format-specific metrics as needed
1332 SkScalar ascent, descent, leading, xmin, xmax, ymin, ymax;
commit-bot@chromium.org0bc406d2014-03-01 20:12:26 +00001333 SkScalar underlineThickness, underlinePosition;
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +00001334 if (face->face_flags & FT_FACE_FLAG_SCALABLE) { // scalable outline font
bungeman665b0382015-03-19 10:43:57 -07001335 // FreeType will always use HHEA metrics if they're not zero.
1336 // It completely ignores the OS/2 fsSelection::UseTypoMetrics bit.
1337 // It also ignores the VDMX tables, which are also of interest here
1338 // (and override everything else when they apply).
1339 static const int kUseTypoMetricsMask = (1 << 7);
1340 if (os2 && os2->version != 0xFFFF && (os2->fsSelection & kUseTypoMetricsMask)) {
1341 ascent = -SkIntToScalar(os2->sTypoAscender) / upem;
1342 descent = -SkIntToScalar(os2->sTypoDescender) / upem;
1343 leading = SkIntToScalar(os2->sTypoLineGap) / upem;
1344 } else {
1345 ascent = -SkIntToScalar(face->ascender) / upem;
1346 descent = -SkIntToScalar(face->descender) / upem;
1347 leading = SkIntToScalar(face->height + (face->descender - face->ascender)) / upem;
1348 }
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +00001349 xmin = SkIntToScalar(face->bbox.xMin) / upem;
1350 xmax = SkIntToScalar(face->bbox.xMax) / upem;
1351 ymin = -SkIntToScalar(face->bbox.yMin) / upem;
1352 ymax = -SkIntToScalar(face->bbox.yMax) / upem;
commit-bot@chromium.org0bc406d2014-03-01 20:12:26 +00001353 underlineThickness = SkIntToScalar(face->underline_thickness) / upem;
commit-bot@chromium.orgd3031aa2014-05-14 14:54:51 +00001354 underlinePosition = -SkIntToScalar(face->underline_position +
1355 face->underline_thickness / 2) / upem;
commit-bot@chromium.org0bc406d2014-03-01 20:12:26 +00001356
bungeman41078062014-07-07 08:16:37 -07001357 metrics->fFlags |= SkPaint::FontMetrics::kUnderlineThinknessIsValid_Flag;
1358 metrics->fFlags |= SkPaint::FontMetrics::kUnderlinePositionIsValid_Flag;
1359
bungeman@google.comcbe1b542013-12-16 17:02:39 +00001360 // we may be able to synthesize x_height and cap_height from outline
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +00001361 if (!x_height) {
bungeman@google.comcbe1b542013-12-16 17:02:39 +00001362 FT_BBox bbox;
1363 if (getCBoxForLetter('x', &bbox)) {
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +00001364 x_height = SkIntToScalar(bbox.yMax) / 64.0f;
1365 }
1366 }
bungeman@google.comcbe1b542013-12-16 17:02:39 +00001367 if (!cap_height) {
1368 FT_BBox bbox;
1369 if (getCBoxForLetter('H', &bbox)) {
1370 cap_height = SkIntToScalar(bbox.yMax) / 64.0f;
1371 }
1372 }
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +00001373 } else if (fStrikeIndex != -1) { // bitmap strike metrics
1374 SkScalar xppem = SkIntToScalar(face->size->metrics.x_ppem);
1375 SkScalar yppem = SkIntToScalar(face->size->metrics.y_ppem);
1376 ascent = -SkIntToScalar(face->size->metrics.ascender) / (yppem * 64.0f);
1377 descent = -SkIntToScalar(face->size->metrics.descender) / (yppem * 64.0f);
1378 leading = (SkIntToScalar(face->size->metrics.height) / (yppem * 64.0f))
1379 + ascent - descent;
1380 xmin = 0.0f;
1381 xmax = SkIntToScalar(face->available_sizes[fStrikeIndex].width) / xppem;
1382 ymin = descent + leading;
1383 ymax = ascent - descent;
commit-bot@chromium.org0bc406d2014-03-01 20:12:26 +00001384 underlineThickness = 0;
1385 underlinePosition = 0;
1386
bungeman41078062014-07-07 08:16:37 -07001387 metrics->fFlags &= ~SkPaint::FontMetrics::kUnderlineThinknessIsValid_Flag;
1388 metrics->fFlags &= ~SkPaint::FontMetrics::kUnderlinePositionIsValid_Flag;
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +00001389 } else {
caryclarkfe7ada72016-03-21 06:55:52 -07001390 sk_bzero(metrics, sizeof(*metrics));
1391 return;
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +00001392 }
1393
1394 // synthesize elements that were not provided by the os/2 table or format-specific metrics
1395 if (!x_height) {
1396 x_height = -ascent;
1397 }
1398 if (!avgCharWidth) {
1399 avgCharWidth = xmax - xmin;
1400 }
bungeman@google.comcbe1b542013-12-16 17:02:39 +00001401 if (!cap_height) {
1402 cap_height = -ascent;
1403 }
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +00001404
1405 // disallow negative linespacing
1406 if (leading < 0.0f) {
1407 leading = 0.0f;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001408 }
1409
bungeman41078062014-07-07 08:16:37 -07001410 SkScalar scale = myy;
1411 if (this->isVertical()) {
1412 scale = mxy;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001413 }
bungeman41078062014-07-07 08:16:37 -07001414 metrics->fTop = ymax * scale;
1415 metrics->fAscent = ascent * scale;
1416 metrics->fDescent = descent * scale;
1417 metrics->fBottom = ymin * scale;
1418 metrics->fLeading = leading * scale;
1419 metrics->fAvgCharWidth = avgCharWidth * scale;
reedc17c6582014-10-31 08:20:46 -07001420 metrics->fXMin = xmin * scale;
1421 metrics->fXMax = xmax * scale;
bungeman7316b102014-10-29 12:46:52 -07001422 metrics->fXHeight = x_height;
1423 metrics->fCapHeight = cap_height;
bungeman41078062014-07-07 08:16:37 -07001424 metrics->fUnderlineThickness = underlineThickness * scale;
1425 metrics->fUnderlinePosition = underlinePosition * scale;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001426}
1427
djsollenfcfea992015-01-09 08:18:13 -08001428///////////////////////////////////////////////////////////////////////////////
1429
1430// hand-tuned value to reduce outline embolden strength
1431#ifndef SK_OUTLINE_EMBOLDEN_DIVISOR
1432 #ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
1433 #define SK_OUTLINE_EMBOLDEN_DIVISOR 34
1434 #else
1435 #define SK_OUTLINE_EMBOLDEN_DIVISOR 24
1436 #endif
1437#endif
1438
1439///////////////////////////////////////////////////////////////////////////////
1440
commit-bot@chromium.org6fa81d72013-12-26 15:50:29 +00001441void SkScalerContext_FreeType::emboldenIfNeeded(FT_Face face, FT_GlyphSlot glyph)
1442{
commit-bot@chromium.org921d2b32014-04-01 19:03:07 +00001443 // check to see if the embolden bit is set
1444 if (0 == (fRec.fFlags & SkScalerContext::kEmbolden_Flag)) {
1445 return;
1446 }
1447
commit-bot@chromium.org921d2b32014-04-01 19:03:07 +00001448 switch (glyph->format) {
1449 case FT_GLYPH_FORMAT_OUTLINE:
1450 FT_Pos strength;
djsollenfcfea992015-01-09 08:18:13 -08001451 strength = FT_MulFix(face->units_per_EM, face->size->metrics.y_scale)
1452 / SK_OUTLINE_EMBOLDEN_DIVISOR;
commit-bot@chromium.org921d2b32014-04-01 19:03:07 +00001453 FT_Outline_Embolden(&glyph->outline, strength);
1454 break;
1455 case FT_GLYPH_FORMAT_BITMAP:
1456 FT_GlyphSlot_Own_Bitmap(glyph);
1457 FT_Bitmap_Embolden(glyph->library, &glyph->bitmap, kBitmapEmboldenStrength, 0);
1458 break;
1459 default:
1460 SkDEBUGFAIL("unknown glyph format");
commit-bot@chromium.org6fa81d72013-12-26 15:50:29 +00001461 }
1462}
1463
reed@google.comb4162b12013-07-02 16:32:29 +00001464///////////////////////////////////////////////////////////////////////////////
1465
1466#include "SkUtils.h"
1467
1468static SkUnichar next_utf8(const void** chars) {
1469 return SkUTF8_NextUnichar((const char**)chars);
1470}
1471
1472static SkUnichar next_utf16(const void** chars) {
1473 return SkUTF16_NextUnichar((const uint16_t**)chars);
1474}
1475
1476static SkUnichar next_utf32(const void** chars) {
1477 const SkUnichar** uniChars = (const SkUnichar**)chars;
1478 SkUnichar uni = **uniChars;
1479 *uniChars += 1;
1480 return uni;
1481}
1482
1483typedef SkUnichar (*EncodingProc)(const void**);
1484
1485static EncodingProc find_encoding_proc(SkTypeface::Encoding enc) {
1486 static const EncodingProc gProcs[] = {
1487 next_utf8, next_utf16, next_utf32
1488 };
1489 SkASSERT((size_t)enc < SK_ARRAY_COUNT(gProcs));
1490 return gProcs[enc];
1491}
1492
1493int SkTypeface_FreeType::onCharsToGlyphs(const void* chars, Encoding encoding,
bungeman726cf902015-06-05 13:38:12 -07001494 uint16_t glyphs[], int glyphCount) const
1495{
reed@google.comb4162b12013-07-02 16:32:29 +00001496 AutoFTAccess fta(this);
1497 FT_Face face = fta.face();
1498 if (!face) {
1499 if (glyphs) {
1500 sk_bzero(glyphs, glyphCount * sizeof(glyphs[0]));
1501 }
1502 return 0;
1503 }
1504
1505 EncodingProc next_uni_proc = find_encoding_proc(encoding);
1506
halcanary96fcdcc2015-08-27 07:41:13 -07001507 if (nullptr == glyphs) {
reed@google.comb4162b12013-07-02 16:32:29 +00001508 for (int i = 0; i < glyphCount; ++i) {
1509 if (0 == FT_Get_Char_Index(face, next_uni_proc(&chars))) {
1510 return i;
1511 }
1512 }
1513 return glyphCount;
1514 } else {
1515 int first = glyphCount;
1516 for (int i = 0; i < glyphCount; ++i) {
1517 unsigned id = FT_Get_Char_Index(face, next_uni_proc(&chars));
1518 glyphs[i] = SkToU16(id);
1519 if (0 == id && i < first) {
1520 first = i;
1521 }
1522 }
1523 return first;
1524 }
1525}
1526
1527int SkTypeface_FreeType::onCountGlyphs() const {
bungeman572f8792016-04-29 15:05:02 -07001528 AutoFTAccess fta(this);
1529 FT_Face face = fta.face();
1530 return face ? face->num_glyphs : 0;
reed@google.comb4162b12013-07-02 16:32:29 +00001531}
1532
bungeman@google.com839702b2013-08-07 17:09:22 +00001533SkTypeface::LocalizedStrings* SkTypeface_FreeType::onCreateFamilyNameIterator() const {
bungeman@google.coma9802692013-08-07 02:45:25 +00001534 SkTypeface::LocalizedStrings* nameIter =
1535 SkOTUtils::LocalizedStrings_NameTable::CreateForFamilyNames(*this);
halcanary96fcdcc2015-08-27 07:41:13 -07001536 if (nullptr == nameIter) {
bungeman@google.coma9802692013-08-07 02:45:25 +00001537 SkString familyName;
1538 this->getFamilyName(&familyName);
1539 SkString language("und"); //undetermined
1540 nameIter = new SkOTUtils::LocalizedStrings_SingleName(familyName, language);
1541 }
1542 return nameIter;
1543}
1544
bungeman@google.comddc218e2013-08-01 22:29:43 +00001545int SkTypeface_FreeType::onGetTableTags(SkFontTableTag tags[]) const {
1546 AutoFTAccess fta(this);
1547 FT_Face face = fta.face();
1548
1549 FT_ULong tableCount = 0;
1550 FT_Error error;
1551
halcanary96fcdcc2015-08-27 07:41:13 -07001552 // When 'tag' is nullptr, returns number of tables in 'length'.
1553 error = FT_Sfnt_Table_Info(face, 0, nullptr, &tableCount);
bungeman@google.comddc218e2013-08-01 22:29:43 +00001554 if (error) {
1555 return 0;
1556 }
1557
1558 if (tags) {
1559 for (FT_ULong tableIndex = 0; tableIndex < tableCount; ++tableIndex) {
1560 FT_ULong tableTag;
1561 FT_ULong tablelength;
1562 error = FT_Sfnt_Table_Info(face, tableIndex, &tableTag, &tablelength);
1563 if (error) {
1564 return 0;
1565 }
1566 tags[tableIndex] = static_cast<SkFontTableTag>(tableTag);
1567 }
1568 }
1569 return tableCount;
1570}
1571
1572size_t SkTypeface_FreeType::onGetTableData(SkFontTableTag tag, size_t offset,
1573 size_t length, void* data) const
1574{
1575 AutoFTAccess fta(this);
1576 FT_Face face = fta.face();
1577
1578 FT_ULong tableLength = 0;
1579 FT_Error error;
1580
1581 // When 'length' is 0 it is overwritten with the full table length; 'offset' is ignored.
halcanary96fcdcc2015-08-27 07:41:13 -07001582 error = FT_Load_Sfnt_Table(face, tag, 0, nullptr, &tableLength);
bungeman@google.comddc218e2013-08-01 22:29:43 +00001583 if (error) {
1584 return 0;
1585 }
1586
1587 if (offset > tableLength) {
1588 return 0;
1589 }
bungeman@google.com5ecd4fa2013-08-01 22:48:21 +00001590 FT_ULong size = SkTMin((FT_ULong)length, tableLength - (FT_ULong)offset);
bsalomon49f085d2014-09-05 13:34:00 -07001591 if (data) {
bungeman@google.comddc218e2013-08-01 22:29:43 +00001592 error = FT_Load_Sfnt_Table(face, tag, offset, reinterpret_cast<FT_Byte*>(data), &size);
1593 if (error) {
1594 return 0;
1595 }
1596 }
1597
1598 return size;
1599}
1600
reed@google.comb4162b12013-07-02 16:32:29 +00001601///////////////////////////////////////////////////////////////////////////////
1602///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +00001603
halcanary96fcdcc2015-08-27 07:41:13 -07001604SkTypeface_FreeType::Scanner::Scanner() : fLibrary(nullptr) {
bungeman9dc24682014-12-01 14:01:32 -08001605 if (FT_New_Library(&gFTMemory, &fLibrary)) {
1606 return;
bungeman14df8332014-10-28 15:07:23 -07001607 }
bungeman9dc24682014-12-01 14:01:32 -08001608 FT_Add_Default_Modules(fLibrary);
bungeman14df8332014-10-28 15:07:23 -07001609}
1610SkTypeface_FreeType::Scanner::~Scanner() {
bungeman9dc24682014-12-01 14:01:32 -08001611 if (fLibrary) {
1612 FT_Done_Library(fLibrary);
1613 }
bungeman14df8332014-10-28 15:07:23 -07001614}
1615
1616FT_Face SkTypeface_FreeType::Scanner::openFace(SkStream* stream, int ttcIndex,
1617 FT_Stream ftStream) const
bungeman32501a12014-10-28 12:03:55 -07001618{
halcanary96fcdcc2015-08-27 07:41:13 -07001619 if (fLibrary == nullptr) {
1620 return nullptr;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001621 }
1622
bungeman14df8332014-10-28 15:07:23 -07001623 FT_Open_Args args;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001624 memset(&args, 0, sizeof(args));
1625
1626 const void* memoryBase = stream->getMemoryBase();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001627
bsalomon49f085d2014-09-05 13:34:00 -07001628 if (memoryBase) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001629 args.flags = FT_OPEN_MEMORY;
1630 args.memory_base = (const FT_Byte*)memoryBase;
1631 args.memory_size = stream->getLength();
1632 } else {
bungeman14df8332014-10-28 15:07:23 -07001633 memset(ftStream, 0, sizeof(*ftStream));
1634 ftStream->size = stream->getLength();
1635 ftStream->descriptor.pointer = stream;
bungeman9dc24682014-12-01 14:01:32 -08001636 ftStream->read = sk_ft_stream_io;
1637 ftStream->close = sk_ft_stream_close;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001638
1639 args.flags = FT_OPEN_STREAM;
bungeman14df8332014-10-28 15:07:23 -07001640 args.stream = ftStream;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001641 }
1642
1643 FT_Face face;
bungeman14df8332014-10-28 15:07:23 -07001644 if (FT_Open_Face(fLibrary, &args, ttcIndex, &face)) {
halcanary96fcdcc2015-08-27 07:41:13 -07001645 return nullptr;
bungeman14df8332014-10-28 15:07:23 -07001646 }
1647 return face;
1648}
1649
1650bool SkTypeface_FreeType::Scanner::recognizedFont(SkStream* stream, int* numFaces) const {
1651 SkAutoMutexAcquire libraryLock(fLibraryMutex);
1652
1653 FT_StreamRec streamRec;
1654 FT_Face face = this->openFace(stream, -1, &streamRec);
halcanary96fcdcc2015-08-27 07:41:13 -07001655 if (nullptr == face) {
bungeman14df8332014-10-28 15:07:23 -07001656 return false;
1657 }
1658
1659 *numFaces = face->num_faces;
1660
1661 FT_Done_Face(face);
1662 return true;
1663}
1664
1665#include "SkTSearch.h"
1666bool SkTypeface_FreeType::Scanner::scanFont(
bungeman41868fe2015-05-20 09:21:04 -07001667 SkStream* stream, int ttcIndex,
1668 SkString* name, SkFontStyle* style, bool* isFixedPitch, AxisDefinitions* axes) const
bungeman14df8332014-10-28 15:07:23 -07001669{
1670 SkAutoMutexAcquire libraryLock(fLibraryMutex);
1671
1672 FT_StreamRec streamRec;
1673 FT_Face face = this->openFace(stream, ttcIndex, &streamRec);
halcanary96fcdcc2015-08-27 07:41:13 -07001674 if (nullptr == face) {
djsollen@google.com4dc686d2012-02-15 21:03:45 +00001675 return false;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001676 }
1677
bungemana4c4a2d2014-10-20 13:33:19 -07001678 int weight = SkFontStyle::kNormal_Weight;
1679 int width = SkFontStyle::kNormal_Width;
1680 SkFontStyle::Slant slant = SkFontStyle::kUpright_Slant;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001681 if (face->style_flags & FT_STYLE_FLAG_BOLD) {
bungemana4c4a2d2014-10-20 13:33:19 -07001682 weight = SkFontStyle::kBold_Weight;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001683 }
1684 if (face->style_flags & FT_STYLE_FLAG_ITALIC) {
bungemana4c4a2d2014-10-20 13:33:19 -07001685 slant = SkFontStyle::kItalic_Slant;
1686 }
1687
1688 PS_FontInfoRec psFontInfo;
1689 TT_OS2* os2 = static_cast<TT_OS2*>(FT_Get_Sfnt_Table(face, ft_sfnt_os2));
1690 if (os2 && os2->version != 0xffff) {
1691 weight = os2->usWeightClass;
1692 width = os2->usWidthClass;
bungemanb4bb7d82016-04-27 10:21:04 -07001693
1694 // OS/2::fsSelection bit 9 indicates oblique.
1695 if (SkToBool(os2->fsSelection & (1u << 9))) {
1696 slant = SkFontStyle::kOblique_Slant;
1697 }
bungemana4c4a2d2014-10-20 13:33:19 -07001698 } else if (0 == FT_Get_PS_Font_Info(face, &psFontInfo) && psFontInfo.weight) {
1699 static const struct {
1700 char const * const name;
1701 int const weight;
1702 } commonWeights [] = {
1703 // There are probably more common names, but these are known to exist.
bungemand803cda2015-04-16 14:22:46 -07001704 { "all", SkFontStyle::kNormal_Weight }, // Multiple Masters usually default to normal.
bungemana4c4a2d2014-10-20 13:33:19 -07001705 { "black", SkFontStyle::kBlack_Weight },
1706 { "bold", SkFontStyle::kBold_Weight },
1707 { "book", (SkFontStyle::kNormal_Weight + SkFontStyle::kLight_Weight)/2 },
1708 { "demi", SkFontStyle::kSemiBold_Weight },
1709 { "demibold", SkFontStyle::kSemiBold_Weight },
bungeman14df8332014-10-28 15:07:23 -07001710 { "extra", SkFontStyle::kExtraBold_Weight },
bungemana4c4a2d2014-10-20 13:33:19 -07001711 { "extrabold", SkFontStyle::kExtraBold_Weight },
1712 { "extralight", SkFontStyle::kExtraLight_Weight },
bungeman14df8332014-10-28 15:07:23 -07001713 { "hairline", SkFontStyle::kThin_Weight },
bungemana4c4a2d2014-10-20 13:33:19 -07001714 { "heavy", SkFontStyle::kBlack_Weight },
1715 { "light", SkFontStyle::kLight_Weight },
1716 { "medium", SkFontStyle::kMedium_Weight },
1717 { "normal", SkFontStyle::kNormal_Weight },
bungeman14df8332014-10-28 15:07:23 -07001718 { "plain", SkFontStyle::kNormal_Weight },
bungemana4c4a2d2014-10-20 13:33:19 -07001719 { "regular", SkFontStyle::kNormal_Weight },
bungeman14df8332014-10-28 15:07:23 -07001720 { "roman", SkFontStyle::kNormal_Weight },
bungemana4c4a2d2014-10-20 13:33:19 -07001721 { "semibold", SkFontStyle::kSemiBold_Weight },
bungeman14df8332014-10-28 15:07:23 -07001722 { "standard", SkFontStyle::kNormal_Weight },
bungemana4c4a2d2014-10-20 13:33:19 -07001723 { "thin", SkFontStyle::kThin_Weight },
1724 { "ultra", SkFontStyle::kExtraBold_Weight },
1725 { "ultrablack", 1000 },
1726 { "ultrabold", SkFontStyle::kExtraBold_Weight },
1727 { "ultraheavy", 1000 },
1728 { "ultralight", SkFontStyle::kExtraLight_Weight },
1729 };
1730 int const index = SkStrLCSearch(&commonWeights[0].name, SK_ARRAY_COUNT(commonWeights),
bungemand2ae7282014-10-22 08:25:44 -07001731 psFontInfo.weight, sizeof(commonWeights[0]));
bungemana4c4a2d2014-10-20 13:33:19 -07001732 if (index >= 0) {
1733 weight = commonWeights[index].weight;
1734 } else {
bungeman14df8332014-10-28 15:07:23 -07001735 SkDEBUGF(("Do not know weight for: %s (%s) \n", face->family_name, psFontInfo.weight));
bungemana4c4a2d2014-10-20 13:33:19 -07001736 }
djsollen@google.com4dc686d2012-02-15 21:03:45 +00001737 }
1738
1739 if (name) {
1740 name->set(face->family_name);
1741 }
1742 if (style) {
bungemana4c4a2d2014-10-20 13:33:19 -07001743 *style = SkFontStyle(weight, width, slant);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001744 }
bungeman@google.comfe747652013-03-25 19:36:11 +00001745 if (isFixedPitch) {
1746 *isFixedPitch = FT_IS_FIXED_WIDTH(face);
reed@google.com5b31b0f2011-02-23 14:41:42 +00001747 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001748
bungeman41868fe2015-05-20 09:21:04 -07001749 if (axes && face->face_flags & FT_FACE_FLAG_MULTIPLE_MASTERS) {
halcanary96fcdcc2015-08-27 07:41:13 -07001750 FT_MM_Var* variations = nullptr;
bungeman41868fe2015-05-20 09:21:04 -07001751 FT_Error err = FT_Get_MM_Var(face, &variations);
1752 if (err) {
1753 SkDEBUGF(("INFO: font %s claims to have variations, but none found.\n",
1754 face->family_name));
1755 return false;
1756 }
1757 SkAutoFree autoFreeVariations(variations);
1758
1759 axes->reset(variations->num_axis);
1760 for (FT_UInt i = 0; i < variations->num_axis; ++i) {
1761 const FT_Var_Axis& ftAxis = variations->axis[i];
1762 (*axes)[i].fTag = ftAxis.tag;
1763 (*axes)[i].fMinimum = ftAxis.minimum;
1764 (*axes)[i].fDefault = ftAxis.def;
1765 (*axes)[i].fMaximum = ftAxis.maximum;
1766 }
1767 }
bungeman41868fe2015-05-20 09:21:04 -07001768
reed@android.com8a1c16f2008-12-17 15:59:43 +00001769 FT_Done_Face(face);
djsollen@google.com4dc686d2012-02-15 21:03:45 +00001770 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001771}
bungemanf6c71072016-01-21 14:17:47 -08001772
1773/*static*/ void SkTypeface_FreeType::Scanner::computeAxisValues(
1774 AxisDefinitions axisDefinitions,
1775 const SkFontMgr::FontParameters::Axis* requestedAxes, int requestedAxisCount,
1776 SkFixed* axisValues,
1777 const SkString& name)
1778{
1779 for (int i = 0; i < axisDefinitions.count(); ++i) {
1780 const Scanner::AxisDefinition& axisDefinition = axisDefinitions[i];
benjaminwagner64a3c952016-02-25 12:20:40 -08001781 const SkScalar axisMin = SkFixedToScalar(axisDefinition.fMinimum);
1782 const SkScalar axisMax = SkFixedToScalar(axisDefinition.fMaximum);
bungemanf6c71072016-01-21 14:17:47 -08001783 axisValues[i] = axisDefinition.fDefault;
1784 for (int j = 0; j < requestedAxisCount; ++j) {
1785 const SkFontMgr::FontParameters::Axis& axisSpecified = requestedAxes[j];
1786 if (axisDefinition.fTag == axisSpecified.fTag) {
benjaminwagner64a3c952016-02-25 12:20:40 -08001787 const SkScalar axisValue = SkTPin(axisSpecified.fStyleValue, axisMin, axisMax);
1788 if (axisSpecified.fStyleValue != axisValue) {
bungemanf6c71072016-01-21 14:17:47 -08001789 SkDEBUGF(("Requested font axis value out of range: "
1790 "%s '%c%c%c%c' %f; pinned to %f.\n",
1791 name.c_str(),
1792 (axisDefinition.fTag >> 24) & 0xFF,
1793 (axisDefinition.fTag >> 16) & 0xFF,
1794 (axisDefinition.fTag >> 8) & 0xFF,
1795 (axisDefinition.fTag ) & 0xFF,
1796 SkScalarToDouble(axisSpecified.fStyleValue),
benjaminwagner64a3c952016-02-25 12:20:40 -08001797 SkScalarToDouble(axisValue)));
bungemanf6c71072016-01-21 14:17:47 -08001798 }
benjaminwagner64a3c952016-02-25 12:20:40 -08001799 axisValues[i] = SkScalarToFixed(axisValue);
bungemanf6c71072016-01-21 14:17:47 -08001800 break;
1801 }
1802 }
1803 // TODO: warn on defaulted axis?
1804 }
1805
1806 SkDEBUGCODE(
1807 // Check for axis specified, but not matched in font.
1808 for (int i = 0; i < requestedAxisCount; ++i) {
1809 SkFourByteTag skTag = requestedAxes[i].fTag;
1810 bool found = false;
1811 for (int j = 0; j < axisDefinitions.count(); ++j) {
1812 if (skTag == axisDefinitions[j].fTag) {
1813 found = true;
1814 break;
1815 }
1816 }
1817 if (!found) {
1818 SkDEBUGF(("Requested font axis not found: %s '%c%c%c%c'\n",
1819 name.c_str(),
1820 (skTag >> 24) & 0xFF,
1821 (skTag >> 16) & 0xFF,
1822 (skTag >> 8) & 0xFF,
1823 (skTag) & 0xFF));
1824 }
1825 }
1826 )
1827}