blob: 30e5fb340f4ddeb154b42a1e1e77da71ae7f3ddb [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
vandebo@chromium.org6f72d1e2011-02-14 23:19:59 +0000435static bool getWidthAdvance(FT_Face face, int gId, int16_t* data) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000436 FT_Fixed advance = 0;
bungeman5ec443c2014-11-21 13:18:34 -0800437 if (FT_Get_Advances(face, gId, 1, FT_LOAD_NO_SCALE, &advance)) {
vandebo@chromium.org6f72d1e2011-02-14 23:19:59 +0000438 return false;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000439 }
vandebo@chromium.org6f72d1e2011-02-14 23:19:59 +0000440 SkASSERT(data);
441 *data = advance;
442 return true;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000443}
444
bungeman5ec443c2014-11-21 13:18:34 -0800445static void populate_glyph_to_unicode(FT_Face& face, SkTDArray<SkUnichar>* glyphToUnicode) {
halcanaryf8c74a12016-04-20 08:37:43 -0700446 FT_Long numGlyphs = face->num_glyphs;
447 glyphToUnicode->setCount(SkToInt(numGlyphs));
448 sk_bzero(glyphToUnicode->begin(), sizeof((*glyphToUnicode)[0]) * numGlyphs);
vandebo@chromium.org6744d492011-05-09 18:13:47 +0000449
bungeman726cf902015-06-05 13:38:12 -0700450 FT_UInt glyphIndex;
451 SkUnichar charCode = FT_Get_First_Char(face, &glyphIndex);
452 while (glyphIndex) {
halcanaryf8c74a12016-04-20 08:37:43 -0700453 SkASSERT(glyphIndex < SkToUInt(numGlyphs));
bungeman726cf902015-06-05 13:38:12 -0700454 (*glyphToUnicode)[glyphIndex] = charCode;
455 charCode = FT_Get_Next_Char(face, charCode, &glyphIndex);
vandebo@chromium.org6744d492011-05-09 18:13:47 +0000456 }
457}
458
reed@google.com2689f612013-03-20 20:01:47 +0000459SkAdvancedTypefaceMetrics* SkTypeface_FreeType::onGetAdvancedTypefaceMetrics(
reed39a9a502015-05-12 09:50:04 -0700460 PerGlyphInfo perGlyphInfo,
vandebo@chromium.org37ad8fb2011-08-18 02:38:50 +0000461 const uint32_t* glyphIDs,
reed@google.com2689f612013-03-20 20:01:47 +0000462 uint32_t glyphIDsCount) const {
djsollen@google.comda957722011-11-16 17:00:46 +0000463#if defined(SK_BUILD_FOR_MAC)
halcanary96fcdcc2015-08-27 07:41:13 -0700464 return nullptr;
reed@google.com8a5d6922011-03-14 15:08:03 +0000465#else
reed@google.comb4162b12013-07-02 16:32:29 +0000466 AutoFTAccess fta(this);
467 FT_Face face = fta.face();
468 if (!face) {
halcanary96fcdcc2015-08-27 07:41:13 -0700469 return nullptr;
reed@google.comb4162b12013-07-02 16:32:29 +0000470 }
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000471
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000472 SkAdvancedTypefaceMetrics* info = new SkAdvancedTypefaceMetrics;
473 info->fFontName.set(FT_Get_Postscript_Name(face));
vandebo0f9bad02014-06-19 11:05:39 -0700474 info->fFlags = SkAdvancedTypefaceMetrics::kEmpty_FontFlag;
475 if (FT_HAS_MULTIPLE_MASTERS(face)) {
476 info->fFlags = SkTBitOr<SkAdvancedTypefaceMetrics::FontFlags>(
477 info->fFlags, SkAdvancedTypefaceMetrics::kMultiMaster_FontFlag);
478 }
479 if (!canEmbed(face)) {
480 info->fFlags = SkTBitOr<SkAdvancedTypefaceMetrics::FontFlags>(
481 info->fFlags,
482 SkAdvancedTypefaceMetrics::kNotEmbeddable_FontFlag);
483 }
484 if (!canSubset(face)) {
485 info->fFlags = SkTBitOr<SkAdvancedTypefaceMetrics::FontFlags>(
486 info->fFlags,
487 SkAdvancedTypefaceMetrics::kNotSubsettable_FontFlag);
488 }
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000489 info->fLastGlyphID = face->num_glyphs - 1;
490 info->fEmSize = 1000;
491
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000492 bool cid = false;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000493 const char* fontType = FT_Get_X11_Font_Format(face);
vandebo@chromium.orgc3a2ae52011-02-03 21:48:23 +0000494 if (strcmp(fontType, "Type 1") == 0) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000495 info->fType = SkAdvancedTypefaceMetrics::kType1_Font;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000496 } else if (strcmp(fontType, "CID Type 1") == 0) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000497 info->fType = SkAdvancedTypefaceMetrics::kType1CID_Font;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000498 cid = true;
499 } else if (strcmp(fontType, "CFF") == 0) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000500 info->fType = SkAdvancedTypefaceMetrics::kCFF_Font;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000501 } else if (strcmp(fontType, "TrueType") == 0) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000502 info->fType = SkAdvancedTypefaceMetrics::kTrueType_Font;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000503 cid = true;
504 TT_Header* ttHeader;
505 if ((ttHeader = (TT_Header*)FT_Get_Sfnt_Table(face,
halcanary96fcdcc2015-08-27 07:41:13 -0700506 ft_sfnt_head)) != nullptr) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000507 info->fEmSize = ttHeader->Units_Per_EM;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000508 }
bungeman@google.com4d71db82013-12-02 19:10:02 +0000509 } else {
510 info->fType = SkAdvancedTypefaceMetrics::kOther_Font;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000511 }
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000512
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000513 info->fStyle = 0;
514 if (FT_IS_FIXED_WIDTH(face))
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000515 info->fStyle |= SkAdvancedTypefaceMetrics::kFixedPitch_Style;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000516 if (face->style_flags & FT_STYLE_FLAG_ITALIC)
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000517 info->fStyle |= SkAdvancedTypefaceMetrics::kItalic_Style;
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000518
519 PS_FontInfoRec ps_info;
520 TT_Postscript* tt_info;
521 if (FT_Get_PS_Font_Info(face, &ps_info) == 0) {
522 info->fItalicAngle = ps_info.italic_angle;
523 } else if ((tt_info =
524 (TT_Postscript*)FT_Get_Sfnt_Table(face,
halcanary96fcdcc2015-08-27 07:41:13 -0700525 ft_sfnt_post)) != nullptr) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000526 info->fItalicAngle = SkFixedToScalar(tt_info->italicAngle);
527 } else {
528 info->fItalicAngle = 0;
529 }
530
531 info->fAscent = face->ascender;
532 info->fDescent = face->descender;
533
534 // Figure out a good guess for StemV - Min width of i, I, !, 1.
535 // This probably isn't very good with an italic font.
536 int16_t min_width = SHRT_MAX;
vandebo@chromium.org6f72d1e2011-02-14 23:19:59 +0000537 info->fStemV = 0;
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000538 char stem_chars[] = {'i', 'I', '!', '1'};
539 for (size_t i = 0; i < SK_ARRAY_COUNT(stem_chars); i++) {
540 FT_BBox bbox;
541 if (GetLetterCBox(face, stem_chars[i], &bbox)) {
542 int16_t width = bbox.xMax - bbox.xMin;
543 if (width > 0 && width < min_width) {
544 min_width = width;
545 info->fStemV = min_width;
546 }
547 }
548 }
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000549
550 TT_PCLT* pclt_info;
551 TT_OS2* os2_table;
halcanary96fcdcc2015-08-27 07:41:13 -0700552 if ((pclt_info = (TT_PCLT*)FT_Get_Sfnt_Table(face, ft_sfnt_pclt)) != nullptr) {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000553 info->fCapHeight = pclt_info->CapHeight;
554 uint8_t serif_style = pclt_info->SerifStyle & 0x3F;
555 if (serif_style >= 2 && serif_style <= 6)
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000556 info->fStyle |= SkAdvancedTypefaceMetrics::kSerif_Style;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000557 else if (serif_style >= 9 && serif_style <= 12)
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000558 info->fStyle |= SkAdvancedTypefaceMetrics::kScript_Style;
halcanary96fcdcc2015-08-27 07:41:13 -0700559 } else if (((os2_table = (TT_OS2*)FT_Get_Sfnt_Table(face, ft_sfnt_os2)) != nullptr) &&
bungeman@google.comcbe1b542013-12-16 17:02:39 +0000560 // sCapHeight is available only when version 2 or later.
561 os2_table->version != 0xFFFF &&
562 os2_table->version >= 2) {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000563 info->fCapHeight = os2_table->sCapHeight;
564 } else {
565 // Figure out a good guess for CapHeight: average the height of M and X.
566 FT_BBox m_bbox, x_bbox;
567 bool got_m, got_x;
568 got_m = GetLetterCBox(face, 'M', &m_bbox);
569 got_x = GetLetterCBox(face, 'X', &x_bbox);
570 if (got_m && got_x) {
571 info->fCapHeight = (m_bbox.yMax - m_bbox.yMin + x_bbox.yMax -
572 x_bbox.yMin) / 2;
573 } else if (got_m && !got_x) {
574 info->fCapHeight = m_bbox.yMax - m_bbox.yMin;
575 } else if (!got_m && got_x) {
576 info->fCapHeight = x_bbox.yMax - x_bbox.yMin;
bungeman@google.com12bd4a02013-12-19 19:34:22 +0000577 } else {
578 // Last resort, use the ascent.
579 info->fCapHeight = info->fAscent;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000580 }
581 }
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000582
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000583 info->fBBox = SkIRect::MakeLTRB(face->bbox.xMin, face->bbox.yMax,
584 face->bbox.xMax, face->bbox.yMin);
585
vandebo0f9bad02014-06-19 11:05:39 -0700586 if (!FT_IS_SCALABLE(face)) {
reed39a9a502015-05-12 09:50:04 -0700587 perGlyphInfo = kNo_PerGlyphInfo;
vandebo@chromium.org325cb9a2011-03-30 18:36:29 +0000588 }
589
reed39a9a502015-05-12 09:50:04 -0700590 if (perGlyphInfo & kHAdvance_PerGlyphInfo) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000591 if (FT_IS_FIXED_WIDTH(face)) {
halcanarye20a8752016-05-08 18:47:16 -0700592 SkAdvancedTypefaceMetrics::WidthRange range(0);
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000593 int16_t advance = face->max_advance_width;
halcanarye20a8752016-05-08 18:47:16 -0700594 range.fAdvance.append(1, &advance);
595 SkAdvancedTypefaceMetrics::FinishRange(
596 &range, 0, SkAdvancedTypefaceMetrics::WidthRange::kDefault);
597 info->fGlyphWidths.emplace_back(std::move(range));
vandebo@chromium.org6f72d1e2011-02-14 23:19:59 +0000598 } else if (!cid) {
halcanarye20a8752016-05-08 18:47:16 -0700599 SkAdvancedTypefaceMetrics::WidthRange range(0);
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000600 // So as to not blow out the stack, get advances in batches.
601 for (int gID = 0; gID < face->num_glyphs; gID += 128) {
602 FT_Fixed advances[128];
603 int advanceCount = 128;
bungeman5ec443c2014-11-21 13:18:34 -0800604 if (gID + advanceCount > face->num_glyphs) {
bungeman@google.comb8aa4dd2013-10-15 18:50:00 +0000605 advanceCount = face->num_glyphs - gID;
bungeman5ec443c2014-11-21 13:18:34 -0800606 }
607 FT_Get_Advances(face, gID, advanceCount, FT_LOAD_NO_SCALE, advances);
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000608 for (int i = 0; i < advanceCount; i++) {
vandebo@chromium.orgce8a1952012-10-22 20:09:31 +0000609 int16_t advance = advances[i];
halcanarye20a8752016-05-08 18:47:16 -0700610 range.fAdvance.append(1, &advance);
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000611 }
612 }
halcanarye20a8752016-05-08 18:47:16 -0700613 SkAdvancedTypefaceMetrics::FinishRange(
614 &range, face->num_glyphs - 1,
615 SkAdvancedTypefaceMetrics::WidthRange::kRange);
616 info->fGlyphWidths.emplace_back(std::move(range));
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000617 } else {
halcanarye20a8752016-05-08 18:47:16 -0700618 info->setGlyphWidths(face, face->num_glyphs, glyphIDs,
619 glyphIDsCount, &getWidthAdvance);
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000620 }
vandebo@chromium.org325cb9a2011-03-30 18:36:29 +0000621 }
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000622
reed39a9a502015-05-12 09:50:04 -0700623 if (perGlyphInfo & kVAdvance_PerGlyphInfo &&
vandebo@chromium.org325cb9a2011-03-30 18:36:29 +0000624 FT_HAS_VERTICAL(face)) {
625 SkASSERT(false); // Not implemented yet.
626 }
627
reed39a9a502015-05-12 09:50:04 -0700628 if (perGlyphInfo & kGlyphNames_PerGlyphInfo &&
vandebo@chromium.org325cb9a2011-03-30 18:36:29 +0000629 info->fType == SkAdvancedTypefaceMetrics::kType1_Font) {
630 // Postscript fonts may contain more than 255 glyphs, so we end up
631 // using multiple font descriptions with a glyph ordering. Record
632 // the name of each glyph.
633 info->fGlyphNames.reset(
634 new SkAutoTArray<SkString>(face->num_glyphs));
635 for (int gID = 0; gID < face->num_glyphs; gID++) {
636 char glyphName[128]; // PS limit for names is 127 bytes.
637 FT_Get_Glyph_Name(face, gID, glyphName, 128);
638 info->fGlyphNames->get()[gID].set(glyphName);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000639 }
640 }
641
reed39a9a502015-05-12 09:50:04 -0700642 if (perGlyphInfo & kToUnicode_PerGlyphInfo &&
vandebo@chromium.org6744d492011-05-09 18:13:47 +0000643 info->fType != SkAdvancedTypefaceMetrics::kType1_Font &&
644 face->num_charmaps) {
645 populate_glyph_to_unicode(face, &(info->fGlyphToUnicode));
646 }
647
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000648 return info;
reed@google.com8a5d6922011-03-14 15:08:03 +0000649#endif
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000650}
vandebo@chromium.org37ad8fb2011-08-18 02:38:50 +0000651
reed@google.com618ef5e2011-01-26 22:10:41 +0000652///////////////////////////////////////////////////////////////////////////
653
reed@google.com8ed436c2011-07-21 14:12:36 +0000654static bool bothZero(SkScalar a, SkScalar b) {
655 return 0 == a && 0 == b;
656}
657
658// returns false if there is any non-90-rotation or skew
659static bool isAxisAligned(const SkScalerContext::Rec& rec) {
660 return 0 == rec.fPreSkewX &&
661 (bothZero(rec.fPost2x2[0][1], rec.fPost2x2[1][0]) ||
662 bothZero(rec.fPost2x2[0][0], rec.fPost2x2[1][1]));
663}
664
reeda9322c22016-04-12 06:47:05 -0700665SkScalerContext* SkTypeface_FreeType::onCreateScalerContext(const SkScalerContextEffects& effects,
666 const SkDescriptor* desc) const {
halcanary385fe4d2015-08-26 13:07:48 -0700667 SkScalerContext_FreeType* c =
reeda9322c22016-04-12 06:47:05 -0700668 new SkScalerContext_FreeType(const_cast<SkTypeface_FreeType*>(this), effects, desc);
reed@google.com0da48612013-03-19 16:06:52 +0000669 if (!c->success()) {
halcanary385fe4d2015-08-26 13:07:48 -0700670 delete c;
halcanary96fcdcc2015-08-27 07:41:13 -0700671 c = nullptr;
reed@google.com0da48612013-03-19 16:06:52 +0000672 }
673 return c;
674}
675
676void SkTypeface_FreeType::onFilterRec(SkScalerContextRec* rec) const {
bungeman@google.com8cf32262012-04-02 14:34:30 +0000677 //BOGUS: http://code.google.com/p/chromium/issues/detail?id=121119
678 //Cap the requested size as larger sizes give bogus values.
679 //Remove when http://code.google.com/p/skia/issues/detail?id=554 is fixed.
bungemanaabd71c2016-03-01 15:15:09 -0800680 //Note that this also currently only protects against large text size requests,
681 //the total matrix is not taken into account here.
bungeman@google.com5582e632012-04-02 14:51:54 +0000682 if (rec->fTextSize > SkIntToScalar(1 << 14)) {
scroggo@google.com94bc60f2012-10-04 20:45:06 +0000683 rec->fTextSize = SkIntToScalar(1 << 14);
bungeman@google.com8cf32262012-04-02 14:34:30 +0000684 }
skia.committer@gmail.coma27096b2012-08-30 14:38:00 +0000685
bungemanec7e12f2015-01-21 11:55:16 -0800686 if (isLCD(*rec)) {
bungemand4742fa2015-01-21 11:19:22 -0800687 // TODO: re-work so that FreeType is set-up and selected by the SkFontMgr.
688 SkAutoMutexAcquire ama(gFTMutex);
689 ref_ft_library();
bungemanec7e12f2015-01-21 11:55:16 -0800690 if (!gFTLibrary->isLCDSupported()) {
bungemand4742fa2015-01-21 11:19:22 -0800691 // If the runtime Freetype library doesn't support LCD, disable it here.
692 rec->fMaskFormat = SkMask::kA8_Format;
693 }
694 unref_ft_library();
reed@google.com618ef5e2011-01-26 22:10:41 +0000695 }
reed@google.com5b31b0f2011-02-23 14:41:42 +0000696
reed@google.com618ef5e2011-01-26 22:10:41 +0000697 SkPaint::Hinting h = rec->getHinting();
reed@google.comeffc5012011-06-27 16:44:46 +0000698 if (SkPaint::kFull_Hinting == h && !isLCD(*rec)) {
reed@google.com618ef5e2011-01-26 22:10:41 +0000699 // collapse full->normal hinting if we're not doing LCD
700 h = SkPaint::kNormal_Hinting;
reed@google.com618ef5e2011-01-26 22:10:41 +0000701 }
bungeman@google.comf4f2b802012-03-08 19:19:51 +0000702 if ((rec->fFlags & SkScalerContext::kSubpixelPositioning_Flag)) {
reed@google.com1ac83502012-02-28 17:06:02 +0000703 if (SkPaint::kNo_Hinting != h) {
704 h = SkPaint::kSlight_Hinting;
705 }
706 }
707
reed@google.com8ed436c2011-07-21 14:12:36 +0000708 // rotated text looks bad with hinting, so we disable it as needed
709 if (!isAxisAligned(*rec)) {
710 h = SkPaint::kNo_Hinting;
711 }
reed@google.com618ef5e2011-01-26 22:10:41 +0000712 rec->setHinting(h);
reed@google.comffe49f52011-11-22 19:42:41 +0000713
bungeman@google.com97efada2012-07-30 20:40:50 +0000714#ifndef SK_GAMMA_APPLY_TO_A8
715 if (!isLCD(*rec)) {
brianosmana1e8f8d2016-04-08 06:47:54 -0700716 // SRGBTODO: Is this correct? Do we want contrast boost?
717 rec->ignorePreBlend();
reed@google.comffe49f52011-11-22 19:42:41 +0000718 }
reed@google.com1ac83502012-02-28 17:06:02 +0000719#endif
reed@google.com618ef5e2011-01-26 22:10:41 +0000720}
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000721
reed@google.com38c37dd2013-03-21 15:36:26 +0000722int SkTypeface_FreeType::onGetUPEM() const {
reed@google.comb4162b12013-07-02 16:32:29 +0000723 AutoFTAccess fta(this);
724 FT_Face face = fta.face();
725 return face ? face->units_per_EM : 0;
djsollen@google.comcd9d69b2011-03-14 20:30:14 +0000726}
djsollen@google.comcd9d69b2011-03-14 20:30:14 +0000727
reed@google.com35fe7372013-10-30 15:07:03 +0000728bool SkTypeface_FreeType::onGetKerningPairAdjustments(const uint16_t glyphs[],
729 int count, int32_t adjustments[]) const {
730 AutoFTAccess fta(this);
731 FT_Face face = fta.face();
732 if (!face || !FT_HAS_KERNING(face)) {
733 return false;
734 }
735
736 for (int i = 0; i < count - 1; ++i) {
737 FT_Vector delta;
738 FT_Error err = FT_Get_Kerning(face, glyphs[i], glyphs[i+1],
739 FT_KERNING_UNSCALED, &delta);
740 if (err) {
741 return false;
742 }
743 adjustments[i] = delta.x;
744 }
745 return true;
746}
747
benjaminwagner45345622016-02-19 15:30:20 -0800748static FT_Int chooseBitmapStrike(FT_Face face, FT_F26Dot6 scaleY) {
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +0000749 // early out if face is bad
halcanary96fcdcc2015-08-27 07:41:13 -0700750 if (face == nullptr) {
751 SkDEBUGF(("chooseBitmapStrike aborted due to nullptr face\n"));
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +0000752 return -1;
753 }
754 // determine target ppem
benjaminwagner45345622016-02-19 15:30:20 -0800755 FT_Pos targetPPEM = scaleY;
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +0000756 // find a bitmap strike equal to or just larger than the requested size
757 FT_Int chosenStrikeIndex = -1;
758 FT_Pos chosenPPEM = 0;
759 for (FT_Int strikeIndex = 0; strikeIndex < face->num_fixed_sizes; ++strikeIndex) {
760 FT_Pos thisPPEM = face->available_sizes[strikeIndex].y_ppem;
761 if (thisPPEM == targetPPEM) {
762 // exact match - our search stops here
763 chosenPPEM = thisPPEM;
764 chosenStrikeIndex = strikeIndex;
765 break;
766 } else if (chosenPPEM < targetPPEM) {
767 // attempt to increase chosenPPEM
768 if (thisPPEM > chosenPPEM) {
769 chosenPPEM = thisPPEM;
770 chosenStrikeIndex = strikeIndex;
771 }
772 } else {
773 // attempt to decrease chosenPPEM, but not below targetPPEM
774 if (thisPPEM < chosenPPEM && thisPPEM > targetPPEM) {
775 chosenPPEM = thisPPEM;
776 chosenStrikeIndex = strikeIndex;
777 }
778 }
779 }
780 if (chosenStrikeIndex != -1) {
781 // use the chosen strike
782 FT_Error err = FT_Select_Size(face, chosenStrikeIndex);
783 if (err != 0) {
784 SkDEBUGF(("FT_Select_Size(%s, %d) returned 0x%x\n", face->family_name,
785 chosenStrikeIndex, err));
786 chosenStrikeIndex = -1;
787 }
788 }
789 return chosenStrikeIndex;
790}
791
reeda9322c22016-04-12 06:47:05 -0700792SkScalerContext_FreeType::SkScalerContext_FreeType(SkTypeface* typeface,
793 const SkScalerContextEffects& effects,
794 const SkDescriptor* desc)
795 : SkScalerContext_FreeType_Base(typeface, effects, desc)
bungemanaabd71c2016-03-01 15:15:09 -0800796 , fFace(nullptr)
797 , fFTSize(nullptr)
798 , fStrikeIndex(-1)
bungeman13a007d2015-06-19 05:09:39 -0700799{
reed@android.com8a1c16f2008-12-17 15:59:43 +0000800 SkAutoMutexAcquire ac(gFTMutex);
801
bungeman9dc24682014-12-01 14:01:32 -0800802 if (!ref_ft_library()) {
803 sk_throw();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000804 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000805
806 // load the font file
bungemanaabd71c2016-03-01 15:15:09 -0800807 using UnrefFTFace = SkFunctionWrapper<void, skstd::remove_pointer_t<FT_Face>, unref_ft_face>;
mtklein5f939ab2016-03-16 10:28:35 -0700808 std::unique_ptr<skstd::remove_pointer_t<FT_Face>, UnrefFTFace> ftFace(ref_ft_face(typeface));
bungemanaabd71c2016-03-01 15:15:09 -0800809 if (nullptr == ftFace) {
810 SkDEBUGF(("Could not create FT_Face.\n"));
reed@android.com62900b42009-02-11 15:07:19 +0000811 return;
812 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000813
bungeman5f14c5e2014-12-05 12:26:44 -0800814 fRec.computeMatrices(SkScalerContextRec::kFull_PreMatrixScale, &fScale, &fMatrix22Scalar);
815 fMatrix22Scalar.setSkewX(-fMatrix22Scalar.getSkewX());
816 fMatrix22Scalar.setSkewY(-fMatrix22Scalar.getSkewY());
reed@android.com8a1c16f2008-12-17 15:59:43 +0000817
benjaminwagner45345622016-02-19 15:30:20 -0800818 fScaleX = SkScalarToFDot6(fScale.fX);
819 fScaleY = SkScalarToFDot6(fScale.fY);
bungeman@google.comd3fbd342014-04-15 15:52:07 +0000820 fMatrix22.xx = SkScalarToFixed(fMatrix22Scalar.getScaleX());
821 fMatrix22.xy = SkScalarToFixed(fMatrix22Scalar.getSkewX());
822 fMatrix22.yx = SkScalarToFixed(fMatrix22Scalar.getSkewY());
823 fMatrix22.yy = SkScalarToFixed(fMatrix22Scalar.getScaleY());
reed@android.com8a1c16f2008-12-17 15:59:43 +0000824
reed@google.coma1bfa212012-03-08 21:57:12 +0000825 fLCDIsVert = SkToBool(fRec.fFlags & SkScalerContext::kLCD_Vertical_Flag);
826
reed@android.com8a1c16f2008-12-17 15:59:43 +0000827 // compute the flags we send to Load_Glyph
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +0000828 bool linearMetrics = SkToBool(fRec.fFlags & SkScalerContext::kSubpixelPositioning_Flag);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000829 {
reed@android.come4d0bc02009-07-24 19:53:20 +0000830 FT_Int32 loadFlags = FT_LOAD_DEFAULT;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000831
agl@chromium.org70a303f2010-05-10 14:15:50 +0000832 if (SkMask::kBW_Format == fRec.fMaskFormat) {
833 // See http://code.google.com/p/chromium/issues/detail?id=43252#c24
834 loadFlags = FT_LOAD_TARGET_MONO;
reed@google.comeffc5012011-06-27 16:44:46 +0000835 if (fRec.getHinting() == SkPaint::kNo_Hinting) {
agl@chromium.org70a303f2010-05-10 14:15:50 +0000836 loadFlags = FT_LOAD_NO_HINTING;
reed@google.combdc99882011-11-21 14:36:57 +0000837 linearMetrics = true;
reed@google.comeffc5012011-06-27 16:44:46 +0000838 }
agl@chromium.org70a303f2010-05-10 14:15:50 +0000839 } else {
840 switch (fRec.getHinting()) {
841 case SkPaint::kNo_Hinting:
842 loadFlags = FT_LOAD_NO_HINTING;
reed@google.combdc99882011-11-21 14:36:57 +0000843 linearMetrics = true;
agl@chromium.org70a303f2010-05-10 14:15:50 +0000844 break;
845 case SkPaint::kSlight_Hinting:
846 loadFlags = FT_LOAD_TARGET_LIGHT; // This implies FORCE_AUTOHINT
847 break;
848 case SkPaint::kNormal_Hinting:
bungeman@google.comf6f56872014-01-23 19:01:36 +0000849 if (fRec.fFlags & SkScalerContext::kForceAutohinting_Flag) {
agl@chromium.orga2c71cb2010-06-17 20:49:17 +0000850 loadFlags = FT_LOAD_FORCE_AUTOHINT;
djsollen858a7892014-08-20 07:03:23 -0700851#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
852 } else {
853 loadFlags = FT_LOAD_NO_AUTOHINT;
854#endif
bungeman@google.comf6f56872014-01-23 19:01:36 +0000855 }
agl@chromium.org70a303f2010-05-10 14:15:50 +0000856 break;
857 case SkPaint::kFull_Hinting:
bungeman@google.comf6f56872014-01-23 19:01:36 +0000858 if (fRec.fFlags & SkScalerContext::kForceAutohinting_Flag) {
agl@chromium.orga2c71cb2010-06-17 20:49:17 +0000859 loadFlags = FT_LOAD_FORCE_AUTOHINT;
860 break;
861 }
agl@chromium.org70a303f2010-05-10 14:15:50 +0000862 loadFlags = FT_LOAD_TARGET_NORMAL;
reed@google.comeffc5012011-06-27 16:44:46 +0000863 if (isLCD(fRec)) {
reed@google.coma1bfa212012-03-08 21:57:12 +0000864 if (fLCDIsVert) {
reed@google.comeffc5012011-06-27 16:44:46 +0000865 loadFlags = FT_LOAD_TARGET_LCD_V;
866 } else {
867 loadFlags = FT_LOAD_TARGET_LCD;
868 }
reed@google.comea2333d2011-03-14 16:44:56 +0000869 }
agl@chromium.org70a303f2010-05-10 14:15:50 +0000870 break;
871 default:
872 SkDebugf("---------- UNKNOWN hinting %d\n", fRec.getHinting());
873 break;
874 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000875 }
876
reed@google.comeffc5012011-06-27 16:44:46 +0000877 if ((fRec.fFlags & SkScalerContext::kEmbeddedBitmapText_Flag) == 0) {
agl@chromium.orge0d08992009-08-07 19:19:23 +0000878 loadFlags |= FT_LOAD_NO_BITMAP;
reed@google.comeffc5012011-06-27 16:44:46 +0000879 }
agl@chromium.orge0d08992009-08-07 19:19:23 +0000880
reed@google.com96a9f7912011-05-06 11:49:30 +0000881 // Always using FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH to get correct
882 // advances, as fontconfig and cairo do.
883 // See http://code.google.com/p/skia/issues/detail?id=222.
884 loadFlags |= FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH;
885
bungeman@google.com8ff8a192012-09-25 20:38:28 +0000886 // Use vertical layout if requested.
887 if (fRec.fFlags & SkScalerContext::kVertical_Flag) {
888 loadFlags |= FT_LOAD_VERTICAL_LAYOUT;
889 }
890
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +0000891 loadFlags |= FT_LOAD_COLOR;
892
reed@android.come4d0bc02009-07-24 19:53:20 +0000893 fLoadGlyphFlags = loadFlags;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000894 }
895
bungemanaabd71c2016-03-01 15:15:09 -0800896 using DoneFTSize = SkFunctionWrapper<FT_Error, skstd::remove_pointer_t<FT_Size>, FT_Done_Size>;
mtklein5f939ab2016-03-16 10:28:35 -0700897 std::unique_ptr<skstd::remove_pointer_t<FT_Size>, DoneFTSize> ftSize([&ftFace]() -> FT_Size {
bungemanaabd71c2016-03-01 15:15:09 -0800898 FT_Size size;
899 FT_Error err = FT_New_Size(ftFace.get(), &size);
900 if (err != 0) {
901 SkDEBUGF(("FT_New_Size returned %x for face %s\n", err, ftFace->family_name));
902 return nullptr;
903 }
904 return size;
905 }());
906 if (nullptr == ftSize) {
907 SkDEBUGF(("Could not create FT_Size.\n"));
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +0000908 return;
909 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000910
bungemanaabd71c2016-03-01 15:15:09 -0800911 FT_Error err = FT_Activate_Size(ftSize.get());
912 if (err != 0) {
913 SkDEBUGF(("FT_Activate_Size(%08x, 0x%x, 0x%x) returned 0x%x\n",
914 ftFace.get(), fScaleX, fScaleY, err));
915 return;
916 }
917
918 if (FT_IS_SCALABLE(ftFace)) {
919 err = FT_Set_Char_Size(ftFace.get(), fScaleX, fScaleY, 72, 72);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000920 if (err != 0) {
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +0000921 SkDEBUGF(("FT_Set_CharSize(%08x, 0x%x, 0x%x) returned 0x%x\n",
bungemanaabd71c2016-03-01 15:15:09 -0800922 ftFace.get(), fScaleX, fScaleY, err));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000923 return;
924 }
bungemanaabd71c2016-03-01 15:15:09 -0800925 FT_Set_Transform(ftFace.get(), &fMatrix22, nullptr);
926 } else if (FT_HAS_FIXED_SIZES(ftFace)) {
927 fStrikeIndex = chooseBitmapStrike(ftFace.get(), fScaleY);
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +0000928 if (fStrikeIndex == -1) {
929 SkDEBUGF(("no glyphs for font \"%s\" size %f?\n",
bungemanaabd71c2016-03-01 15:15:09 -0800930 ftFace->family_name, SkFDot6ToScalar(fScaleY)));
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +0000931 } else {
932 // FreeType does no provide linear metrics for bitmap fonts.
933 linearMetrics = false;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000934
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +0000935 // FreeType documentation says:
936 // FT_LOAD_NO_BITMAP -- Ignore bitmap strikes when loading.
937 // Bitmap-only fonts ignore this flag.
938 //
939 // However, in FreeType 2.5.1 color bitmap only fonts do not ignore this flag.
940 // Force this flag off for bitmap only fonts.
941 fLoadGlyphFlags &= ~FT_LOAD_NO_BITMAP;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000942 }
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +0000943 } else {
944 SkDEBUGF(("unknown kind of font \"%s\" size %f?\n",
benjaminwagner45345622016-02-19 15:30:20 -0800945 fFace->family_name, SkFDot6ToScalar(fScaleY)));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000946 }
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +0000947
bungemanaabd71c2016-03-01 15:15:09 -0800948 fFTSize = ftSize.release();
949 fFace = ftFace.release();
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +0000950 fDoLinearMetrics = linearMetrics;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000951}
952
953SkScalerContext_FreeType::~SkScalerContext_FreeType() {
scroggo@google.com94bc60f2012-10-04 20:45:06 +0000954 SkAutoMutexAcquire ac(gFTMutex);
955
halcanary96fcdcc2015-08-27 07:41:13 -0700956 if (fFTSize != nullptr) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000957 FT_Done_Size(fFTSize);
958 }
959
halcanary96fcdcc2015-08-27 07:41:13 -0700960 if (fFace != nullptr) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000961 unref_ft_face(fFace);
962 }
bungeman9dc24682014-12-01 14:01:32 -0800963
964 unref_ft_library();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000965}
966
967/* We call this before each use of the fFace, since we may be sharing
968 this face with other context (at different sizes).
969*/
970FT_Error SkScalerContext_FreeType::setupSize() {
bungeman3f846ae2015-11-03 11:07:20 -0800971 gFTMutex.assertHeld();
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +0000972 FT_Error err = FT_Activate_Size(fFTSize);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000973 if (err != 0) {
bungeman13a007d2015-06-19 05:09:39 -0700974 SkDEBUGF(("SkScalerContext_FreeType::FT_Activate_Size(%s %s, 0x%x, 0x%x) returned 0x%x\n",
975 fFace->family_name, fFace->style_name, fScaleX, fScaleY, err));
halcanary96fcdcc2015-08-27 07:41:13 -0700976 fFTSize = nullptr;
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +0000977 return err;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000978 }
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +0000979
980 // seems we need to reset this every time (not sure why, but without it
981 // I get random italics from some other fFTSize)
halcanary96fcdcc2015-08-27 07:41:13 -0700982 FT_Set_Transform(fFace, &fMatrix22, nullptr);
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +0000983 return 0;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000984}
985
ctguil@chromium.org0bc7bf52011-03-04 19:04:57 +0000986unsigned SkScalerContext_FreeType::generateGlyphCount() {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000987 return fFace->num_glyphs;
988}
989
990uint16_t SkScalerContext_FreeType::generateCharToGlyph(SkUnichar uni) {
bungeman3f846ae2015-11-03 11:07:20 -0800991 SkAutoMutexAcquire ac(gFTMutex);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000992 return SkToU16(FT_Get_Char_Index( fFace, uni ));
993}
994
reed@android.com9d3a9852010-01-08 14:07:42 +0000995SkUnichar SkScalerContext_FreeType::generateGlyphToChar(uint16_t glyph) {
bungeman3f846ae2015-11-03 11:07:20 -0800996 SkAutoMutexAcquire ac(gFTMutex);
reed@android.com9d3a9852010-01-08 14:07:42 +0000997 // iterate through each cmap entry, looking for matching glyph indices
998 FT_UInt glyphIndex;
999 SkUnichar charCode = FT_Get_First_Char( fFace, &glyphIndex );
1000
1001 while (glyphIndex != 0) {
1002 if (glyphIndex == glyph) {
1003 return charCode;
1004 }
1005 charCode = FT_Get_Next_Char( fFace, charCode, &glyphIndex );
1006 }
1007
1008 return 0;
1009}
1010
benjaminwagner6b3eacb2016-03-24 19:07:58 -07001011static SkScalar SkFT_FixedToScalar(FT_Fixed x) {
1012 return SkFixedToScalar(x);
1013}
1014
reed@android.com8a1c16f2008-12-17 15:59:43 +00001015void SkScalerContext_FreeType::generateAdvance(SkGlyph* glyph) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001016 /* unhinted and light hinted text have linearly scaled advances
1017 * which are very cheap to compute with some font formats...
1018 */
reed@google.combdc99882011-11-21 14:36:57 +00001019 if (fDoLinearMetrics) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001020 SkAutoMutexAcquire ac(gFTMutex);
1021
1022 if (this->setupSize()) {
reed@android.com62900b42009-02-11 15:07:19 +00001023 glyph->zeroMetrics();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001024 return;
1025 }
1026
1027 FT_Error error;
1028 FT_Fixed advance;
1029
djsollen1b277042014-08-06 06:58:06 -07001030 error = FT_Get_Advance( fFace, glyph->getGlyphID(),
reed@android.com8a1c16f2008-12-17 15:59:43 +00001031 fLoadGlyphFlags | FT_ADVANCE_FLAG_FAST_ONLY,
1032 &advance );
1033 if (0 == error) {
1034 glyph->fRsbDelta = 0;
1035 glyph->fLsbDelta = 0;
benjaminwagner6b3eacb2016-03-24 19:07:58 -07001036 const SkScalar advanceScalar = SkFT_FixedToScalar(advance);
1037 glyph->fAdvanceX = SkScalarToFloat(fMatrix22Scalar.getScaleX() * advanceScalar);
1038 glyph->fAdvanceY = -SkScalarToFloat(fMatrix22Scalar.getSkewY() * advanceScalar);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001039 return;
1040 }
1041 }
bungeman5ec443c2014-11-21 13:18:34 -08001042
reed@android.com8a1c16f2008-12-17 15:59:43 +00001043 /* otherwise, we need to load/hint the glyph, which is slower */
1044 this->generateMetrics(glyph);
1045 return;
1046}
1047
djsollen@google.comd8b599c2012-03-19 19:44:19 +00001048void SkScalerContext_FreeType::getBBoxForCurrentGlyph(SkGlyph* glyph,
1049 FT_BBox* bbox,
1050 bool snapToPixelBoundary) {
1051
1052 FT_Outline_Get_CBox(&fFace->glyph->outline, bbox);
1053
1054 if (fRec.fFlags & SkScalerContext::kSubpixelPositioning_Flag) {
george@mozilla.comc59b5da2012-08-23 00:39:08 +00001055 int dx = SkFixedToFDot6(glyph->getSubXFixed());
1056 int dy = SkFixedToFDot6(glyph->getSubYFixed());
djsollen@google.comd8b599c2012-03-19 19:44:19 +00001057 // negate dy since freetype-y-goes-up and skia-y-goes-down
1058 bbox->xMin += dx;
1059 bbox->yMin -= dy;
1060 bbox->xMax += dx;
1061 bbox->yMax -= dy;
1062 }
1063
1064 // outset the box to integral boundaries
1065 if (snapToPixelBoundary) {
1066 bbox->xMin &= ~63;
1067 bbox->yMin &= ~63;
1068 bbox->xMax = (bbox->xMax + 63) & ~63;
1069 bbox->yMax = (bbox->yMax + 63) & ~63;
1070 }
bungeman@google.com8ff8a192012-09-25 20:38:28 +00001071
1072 // Must come after snapToPixelBoundary so that the width and height are
1073 // consistent. Otherwise asserts will fire later on when generating the
1074 // glyph image.
1075 if (fRec.fFlags & SkScalerContext::kVertical_Flag) {
1076 FT_Vector vector;
1077 vector.x = fFace->glyph->metrics.vertBearingX - fFace->glyph->metrics.horiBearingX;
1078 vector.y = -fFace->glyph->metrics.vertBearingY - fFace->glyph->metrics.horiBearingY;
1079 FT_Vector_Transform(&vector, &fMatrix22);
1080 bbox->xMin += vector.x;
1081 bbox->xMax += vector.x;
1082 bbox->yMin += vector.y;
1083 bbox->yMax += vector.y;
1084 }
djsollen@google.comd8b599c2012-03-19 19:44:19 +00001085}
1086
bungeman@google.comcbe1b542013-12-16 17:02:39 +00001087bool SkScalerContext_FreeType::getCBoxForLetter(char letter, FT_BBox* bbox) {
1088 const FT_UInt glyph_id = FT_Get_Char_Index(fFace, letter);
bungeman5ec443c2014-11-21 13:18:34 -08001089 if (!glyph_id) {
bungeman@google.comcbe1b542013-12-16 17:02:39 +00001090 return false;
bungeman5ec443c2014-11-21 13:18:34 -08001091 }
1092 if (FT_Load_Glyph(fFace, glyph_id, fLoadGlyphFlags) != 0) {
bungeman@google.comcbe1b542013-12-16 17:02:39 +00001093 return false;
bungeman5ec443c2014-11-21 13:18:34 -08001094 }
commit-bot@chromium.org6fa81d72013-12-26 15:50:29 +00001095 emboldenIfNeeded(fFace, fFace->glyph);
bungeman@google.comcbe1b542013-12-16 17:02:39 +00001096 FT_Outline_Get_CBox(&fFace->glyph->outline, bbox);
1097 return true;
1098}
1099
djsollen@google.comd8b599c2012-03-19 19:44:19 +00001100void SkScalerContext_FreeType::updateGlyphIfLCD(SkGlyph* glyph) {
1101 if (isLCD(fRec)) {
1102 if (fLCDIsVert) {
bungeman9dc24682014-12-01 14:01:32 -08001103 glyph->fHeight += gFTLibrary->lcdExtra();
1104 glyph->fTop -= gFTLibrary->lcdExtra() >> 1;
djsollen@google.comd8b599c2012-03-19 19:44:19 +00001105 } else {
bungeman9dc24682014-12-01 14:01:32 -08001106 glyph->fWidth += gFTLibrary->lcdExtra();
1107 glyph->fLeft -= gFTLibrary->lcdExtra() >> 1;
djsollen@google.comd8b599c2012-03-19 19:44:19 +00001108 }
1109 }
1110}
1111
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +00001112inline void scaleGlyphMetrics(SkGlyph& glyph, SkScalar scale) {
1113 glyph.fWidth *= scale;
1114 glyph.fHeight *= scale;
1115 glyph.fTop *= scale;
1116 glyph.fLeft *= scale;
1117
benjaminwagner6b3eacb2016-03-24 19:07:58 -07001118 float floatScale = SkScalarToFloat(scale);
1119 glyph.fAdvanceX *= floatScale;
1120 glyph.fAdvanceY *= floatScale;
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +00001121}
1122
reed@android.com8a1c16f2008-12-17 15:59:43 +00001123void SkScalerContext_FreeType::generateMetrics(SkGlyph* glyph) {
1124 SkAutoMutexAcquire ac(gFTMutex);
1125
1126 glyph->fRsbDelta = 0;
1127 glyph->fLsbDelta = 0;
1128
1129 FT_Error err;
1130
1131 if (this->setupSize()) {
bungeman13a007d2015-06-19 05:09:39 -07001132 glyph->zeroMetrics();
1133 return;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001134 }
1135
djsollen1b277042014-08-06 06:58:06 -07001136 err = FT_Load_Glyph( fFace, glyph->getGlyphID(), fLoadGlyphFlags );
reed@android.com8a1c16f2008-12-17 15:59:43 +00001137 if (err != 0) {
reed@android.com62900b42009-02-11 15:07:19 +00001138 glyph->zeroMetrics();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001139 return;
1140 }
commit-bot@chromium.org6fa81d72013-12-26 15:50:29 +00001141 emboldenIfNeeded(fFace, fFace->glyph);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001142
1143 switch ( fFace->glyph->format ) {
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +00001144 case FT_GLYPH_FORMAT_OUTLINE:
bungeman@google.com0f0c2882011-11-04 15:47:41 +00001145 if (0 == fFace->glyph->outline.n_contours) {
1146 glyph->fWidth = 0;
1147 glyph->fHeight = 0;
1148 glyph->fTop = 0;
1149 glyph->fLeft = 0;
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +00001150 } else {
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +00001151 FT_BBox bbox;
1152 getBBoxForCurrentGlyph(glyph, &bbox, true);
1153
1154 glyph->fWidth = SkToU16(SkFDot6Floor(bbox.xMax - bbox.xMin));
1155 glyph->fHeight = SkToU16(SkFDot6Floor(bbox.yMax - bbox.yMin));
1156 glyph->fTop = -SkToS16(SkFDot6Floor(bbox.yMax));
1157 glyph->fLeft = SkToS16(SkFDot6Floor(bbox.xMin));
1158
1159 updateGlyphIfLCD(glyph);
bungeman@google.com0f0c2882011-11-04 15:47:41 +00001160 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001161 break;
1162
1163 case FT_GLYPH_FORMAT_BITMAP:
bungeman@google.com8ff8a192012-09-25 20:38:28 +00001164 if (fRec.fFlags & SkScalerContext::kVertical_Flag) {
1165 FT_Vector vector;
1166 vector.x = fFace->glyph->metrics.vertBearingX - fFace->glyph->metrics.horiBearingX;
1167 vector.y = -fFace->glyph->metrics.vertBearingY - fFace->glyph->metrics.horiBearingY;
1168 FT_Vector_Transform(&vector, &fMatrix22);
1169 fFace->glyph->bitmap_left += SkFDot6Floor(vector.x);
1170 fFace->glyph->bitmap_top += SkFDot6Floor(vector.y);
1171 }
1172
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +00001173 if (fFace->glyph->bitmap.pixel_mode == FT_PIXEL_MODE_BGRA) {
1174 glyph->fMaskFormat = SkMask::kARGB32_Format;
1175 }
1176
reed@android.com8a1c16f2008-12-17 15:59:43 +00001177 glyph->fWidth = SkToU16(fFace->glyph->bitmap.width);
1178 glyph->fHeight = SkToU16(fFace->glyph->bitmap.rows);
1179 glyph->fTop = -SkToS16(fFace->glyph->bitmap_top);
1180 glyph->fLeft = SkToS16(fFace->glyph->bitmap_left);
1181 break;
1182
1183 default:
tomhudson@google.com0c00f212011-12-28 14:59:50 +00001184 SkDEBUGFAIL("unknown glyph format");
bungeman13a007d2015-06-19 05:09:39 -07001185 glyph->zeroMetrics();
1186 return;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001187 }
1188
bungeman@google.com8ff8a192012-09-25 20:38:28 +00001189 if (fRec.fFlags & SkScalerContext::kVertical_Flag) {
1190 if (fDoLinearMetrics) {
benjaminwagner6b3eacb2016-03-24 19:07:58 -07001191 const SkScalar advanceScalar = SkFT_FixedToScalar(fFace->glyph->linearVertAdvance);
1192 glyph->fAdvanceX = -SkScalarToFloat(fMatrix22Scalar.getSkewX() * advanceScalar);
1193 glyph->fAdvanceY = SkScalarToFloat(fMatrix22Scalar.getScaleY() * advanceScalar);
bungeman@google.com8ff8a192012-09-25 20:38:28 +00001194 } else {
benjaminwagner6b3eacb2016-03-24 19:07:58 -07001195 glyph->fAdvanceX = -SkFDot6ToFloat(fFace->glyph->advance.x);
1196 glyph->fAdvanceY = SkFDot6ToFloat(fFace->glyph->advance.y);
bungeman@google.com8ff8a192012-09-25 20:38:28 +00001197 }
bungeman@google.com34f10262012-03-23 18:11:47 +00001198 } else {
bungeman@google.com8ff8a192012-09-25 20:38:28 +00001199 if (fDoLinearMetrics) {
benjaminwagner6b3eacb2016-03-24 19:07:58 -07001200 const SkScalar advanceScalar = SkFT_FixedToScalar(fFace->glyph->linearHoriAdvance);
1201 glyph->fAdvanceX = SkScalarToFloat(fMatrix22Scalar.getScaleX() * advanceScalar);
1202 glyph->fAdvanceY = -SkScalarToFloat(fMatrix22Scalar.getSkewY() * advanceScalar);
bungeman@google.com8ff8a192012-09-25 20:38:28 +00001203 } else {
benjaminwagner6b3eacb2016-03-24 19:07:58 -07001204 glyph->fAdvanceX = SkFDot6ToFloat(fFace->glyph->advance.x);
1205 glyph->fAdvanceY = -SkFDot6ToFloat(fFace->glyph->advance.y);
bungeman@google.com34f10262012-03-23 18:11:47 +00001206
bungeman@google.com8ff8a192012-09-25 20:38:28 +00001207 if (fRec.fFlags & kDevKernText_Flag) {
1208 glyph->fRsbDelta = SkToS8(fFace->glyph->rsb_delta);
1209 glyph->fLsbDelta = SkToS8(fFace->glyph->lsb_delta);
djsollen@google.comd8b599c2012-03-19 19:44:19 +00001210 }
1211 }
djsollen@google.comd8b599c2012-03-19 19:44:19 +00001212 }
1213
bungemana85511a2014-09-22 12:24:41 -07001214 // If the font isn't scalable, scale the metrics from the non-scalable strike.
1215 // This means do not try to scale embedded bitmaps; only scale bitmaps in bitmap only fonts.
benjaminwagner45345622016-02-19 15:30:20 -08001216 if (!FT_IS_SCALABLE(fFace) && !SkScalarNearlyZero(fScale.fY) && fFace->size->metrics.y_ppem) {
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +00001217 // NOTE: both dimensions are scaled by y_ppem. this is WAI.
benjaminwagner45345622016-02-19 15:30:20 -08001218 scaleGlyphMetrics(*glyph, fScale.fY / fFace->size->metrics.y_ppem);
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +00001219 }
djsollen@google.comd8b599c2012-03-19 19:44:19 +00001220
reed@android.com8a1c16f2008-12-17 15:59:43 +00001221#ifdef ENABLE_GLYPH_SPEW
1222 SkDEBUGF(("FT_Set_Char_Size(this:%p sx:%x sy:%x ", this, fScaleX, fScaleY));
djsollen1b277042014-08-06 06:58:06 -07001223 SkDEBUGF(("Metrics(glyph:%d flags:0x%x) w:%d\n", glyph->getGlyphID(), fLoadGlyphFlags, glyph->fWidth));
reed@android.com8a1c16f2008-12-17 15:59:43 +00001224#endif
1225}
1226
bungeman5ec443c2014-11-21 13:18:34 -08001227static void clear_glyph_image(const SkGlyph& glyph) {
1228 sk_bzero(glyph.fImage, glyph.rowBytes() * glyph.fHeight);
1229}
reed@google.comea2333d2011-03-14 16:44:56 +00001230
bungeman@google.coma76de722012-10-26 19:35:54 +00001231void SkScalerContext_FreeType::generateImage(const SkGlyph& glyph) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001232 SkAutoMutexAcquire ac(gFTMutex);
1233
reed@android.com8a1c16f2008-12-17 15:59:43 +00001234 if (this->setupSize()) {
bungeman5ec443c2014-11-21 13:18:34 -08001235 clear_glyph_image(glyph);
1236 return;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001237 }
1238
bungeman5ec443c2014-11-21 13:18:34 -08001239 FT_Error err = FT_Load_Glyph(fFace, glyph.getGlyphID(), fLoadGlyphFlags);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001240 if (err != 0) {
1241 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 -08001242 glyph.getGlyphID(), glyph.fWidth, glyph.fHeight, glyph.rowBytes(), fLoadGlyphFlags, err));
1243 clear_glyph_image(glyph);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001244 return;
1245 }
1246
commit-bot@chromium.org6fa81d72013-12-26 15:50:29 +00001247 emboldenIfNeeded(fFace, fFace->glyph);
bungeman@google.coma76de722012-10-26 19:35:54 +00001248 generateGlyphImage(fFace, glyph);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001249}
1250
reed@android.com8a1c16f2008-12-17 15:59:43 +00001251
bungeman5ec443c2014-11-21 13:18:34 -08001252void SkScalerContext_FreeType::generatePath(const SkGlyph& glyph, SkPath* path) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001253 SkAutoMutexAcquire ac(gFTMutex);
1254
caryclarka10742c2014-09-18 11:00:40 -07001255 SkASSERT(path);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001256
1257 if (this->setupSize()) {
1258 path->reset();
1259 return;
1260 }
1261
1262 uint32_t flags = fLoadGlyphFlags;
1263 flags |= FT_LOAD_NO_BITMAP; // ignore embedded bitmaps so we're sure to get the outline
1264 flags &= ~FT_LOAD_RENDER; // don't scan convert (we just want the outline)
1265
djsollen1b277042014-08-06 06:58:06 -07001266 FT_Error err = FT_Load_Glyph( fFace, glyph.getGlyphID(), flags);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001267
1268 if (err != 0) {
1269 SkDEBUGF(("SkScalerContext_FreeType::generatePath: FT_Load_Glyph(glyph:%d flags:%d) returned 0x%x\n",
djsollen1b277042014-08-06 06:58:06 -07001270 glyph.getGlyphID(), flags, err));
reed@android.com8a1c16f2008-12-17 15:59:43 +00001271 path->reset();
1272 return;
1273 }
commit-bot@chromium.org6fa81d72013-12-26 15:50:29 +00001274 emboldenIfNeeded(fFace, fFace->glyph);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001275
sugoi@google.com66a58ac2013-03-05 20:40:52 +00001276 generateGlyphPath(fFace, path);
bungeman@google.com8ff8a192012-09-25 20:38:28 +00001277
1278 // The path's origin from FreeType is always the horizontal layout origin.
1279 // Offset the path so that it is relative to the vertical origin if needed.
1280 if (fRec.fFlags & SkScalerContext::kVertical_Flag) {
1281 FT_Vector vector;
1282 vector.x = fFace->glyph->metrics.vertBearingX - fFace->glyph->metrics.horiBearingX;
1283 vector.y = -fFace->glyph->metrics.vertBearingY - fFace->glyph->metrics.horiBearingY;
1284 FT_Vector_Transform(&vector, &fMatrix22);
1285 path->offset(SkFDot6ToScalar(vector.x), -SkFDot6ToScalar(vector.y));
1286 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001287}
1288
bungeman41078062014-07-07 08:16:37 -07001289void SkScalerContext_FreeType::generateFontMetrics(SkPaint::FontMetrics* metrics) {
halcanary96fcdcc2015-08-27 07:41:13 -07001290 if (nullptr == metrics) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001291 return;
1292 }
1293
bungeman41078062014-07-07 08:16:37 -07001294 SkAutoMutexAcquire ac(gFTMutex);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001295
1296 if (this->setupSize()) {
bungeman41078062014-07-07 08:16:37 -07001297 sk_bzero(metrics, sizeof(*metrics));
reed@android.com8a1c16f2008-12-17 15:59:43 +00001298 return;
1299 }
1300
reed@android.coma8a8b8b2009-05-04 15:00:11 +00001301 FT_Face face = fFace;
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +00001302 SkScalar scaleX = fScale.x();
reed@google.comf073b332013-05-06 12:21:16 +00001303 SkScalar scaleY = fScale.y();
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +00001304 SkScalar mxy = fMatrix22Scalar.getSkewX() * scaleY;
1305 SkScalar myy = fMatrix22Scalar.getScaleY() * scaleY;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001306
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +00001307 // fetch units/EM from "head" table if needed (ie for bitmap fonts)
1308 SkScalar upem = SkIntToScalar(face->units_per_EM);
1309 if (!upem) {
1310 TT_Header* ttHeader = (TT_Header*)FT_Get_Sfnt_Table(face, ft_sfnt_head);
1311 if (ttHeader) {
1312 upem = SkIntToScalar(ttHeader->Units_Per_EM);
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001313 }
1314 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001315
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +00001316 // use the os/2 table as a source of reasonable defaults.
1317 SkScalar x_height = 0.0f;
1318 SkScalar avgCharWidth = 0.0f;
bungeman@google.comcbe1b542013-12-16 17:02:39 +00001319 SkScalar cap_height = 0.0f;
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +00001320 TT_OS2* os2 = (TT_OS2*) FT_Get_Sfnt_Table(face, ft_sfnt_os2);
1321 if (os2) {
1322 x_height = scaleX * SkIntToScalar(os2->sxHeight) / upem;
1323 avgCharWidth = SkIntToScalar(os2->xAvgCharWidth) / upem;
bungeman@google.comcbe1b542013-12-16 17:02:39 +00001324 if (os2->version != 0xFFFF && os2->version >= 2) {
1325 cap_height = scaleX * SkIntToScalar(os2->sCapHeight) / upem;
1326 }
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +00001327 }
1328
1329 // pull from format-specific metrics as needed
1330 SkScalar ascent, descent, leading, xmin, xmax, ymin, ymax;
commit-bot@chromium.org0bc406d2014-03-01 20:12:26 +00001331 SkScalar underlineThickness, underlinePosition;
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +00001332 if (face->face_flags & FT_FACE_FLAG_SCALABLE) { // scalable outline font
bungeman665b0382015-03-19 10:43:57 -07001333 // FreeType will always use HHEA metrics if they're not zero.
1334 // It completely ignores the OS/2 fsSelection::UseTypoMetrics bit.
1335 // It also ignores the VDMX tables, which are also of interest here
1336 // (and override everything else when they apply).
1337 static const int kUseTypoMetricsMask = (1 << 7);
1338 if (os2 && os2->version != 0xFFFF && (os2->fsSelection & kUseTypoMetricsMask)) {
1339 ascent = -SkIntToScalar(os2->sTypoAscender) / upem;
1340 descent = -SkIntToScalar(os2->sTypoDescender) / upem;
1341 leading = SkIntToScalar(os2->sTypoLineGap) / upem;
1342 } else {
1343 ascent = -SkIntToScalar(face->ascender) / upem;
1344 descent = -SkIntToScalar(face->descender) / upem;
1345 leading = SkIntToScalar(face->height + (face->descender - face->ascender)) / upem;
1346 }
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +00001347 xmin = SkIntToScalar(face->bbox.xMin) / upem;
1348 xmax = SkIntToScalar(face->bbox.xMax) / upem;
1349 ymin = -SkIntToScalar(face->bbox.yMin) / upem;
1350 ymax = -SkIntToScalar(face->bbox.yMax) / upem;
commit-bot@chromium.org0bc406d2014-03-01 20:12:26 +00001351 underlineThickness = SkIntToScalar(face->underline_thickness) / upem;
commit-bot@chromium.orgd3031aa2014-05-14 14:54:51 +00001352 underlinePosition = -SkIntToScalar(face->underline_position +
1353 face->underline_thickness / 2) / upem;
commit-bot@chromium.org0bc406d2014-03-01 20:12:26 +00001354
bungeman41078062014-07-07 08:16:37 -07001355 metrics->fFlags |= SkPaint::FontMetrics::kUnderlineThinknessIsValid_Flag;
1356 metrics->fFlags |= SkPaint::FontMetrics::kUnderlinePositionIsValid_Flag;
1357
bungeman@google.comcbe1b542013-12-16 17:02:39 +00001358 // we may be able to synthesize x_height and cap_height from outline
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +00001359 if (!x_height) {
bungeman@google.comcbe1b542013-12-16 17:02:39 +00001360 FT_BBox bbox;
1361 if (getCBoxForLetter('x', &bbox)) {
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +00001362 x_height = SkIntToScalar(bbox.yMax) / 64.0f;
1363 }
1364 }
bungeman@google.comcbe1b542013-12-16 17:02:39 +00001365 if (!cap_height) {
1366 FT_BBox bbox;
1367 if (getCBoxForLetter('H', &bbox)) {
1368 cap_height = SkIntToScalar(bbox.yMax) / 64.0f;
1369 }
1370 }
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +00001371 } else if (fStrikeIndex != -1) { // bitmap strike metrics
1372 SkScalar xppem = SkIntToScalar(face->size->metrics.x_ppem);
1373 SkScalar yppem = SkIntToScalar(face->size->metrics.y_ppem);
1374 ascent = -SkIntToScalar(face->size->metrics.ascender) / (yppem * 64.0f);
1375 descent = -SkIntToScalar(face->size->metrics.descender) / (yppem * 64.0f);
1376 leading = (SkIntToScalar(face->size->metrics.height) / (yppem * 64.0f))
1377 + ascent - descent;
1378 xmin = 0.0f;
1379 xmax = SkIntToScalar(face->available_sizes[fStrikeIndex].width) / xppem;
1380 ymin = descent + leading;
1381 ymax = ascent - descent;
commit-bot@chromium.org0bc406d2014-03-01 20:12:26 +00001382 underlineThickness = 0;
1383 underlinePosition = 0;
1384
bungeman41078062014-07-07 08:16:37 -07001385 metrics->fFlags &= ~SkPaint::FontMetrics::kUnderlineThinknessIsValid_Flag;
1386 metrics->fFlags &= ~SkPaint::FontMetrics::kUnderlinePositionIsValid_Flag;
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +00001387 } else {
caryclarkfe7ada72016-03-21 06:55:52 -07001388 sk_bzero(metrics, sizeof(*metrics));
1389 return;
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +00001390 }
1391
1392 // synthesize elements that were not provided by the os/2 table or format-specific metrics
1393 if (!x_height) {
1394 x_height = -ascent;
1395 }
1396 if (!avgCharWidth) {
1397 avgCharWidth = xmax - xmin;
1398 }
bungeman@google.comcbe1b542013-12-16 17:02:39 +00001399 if (!cap_height) {
1400 cap_height = -ascent;
1401 }
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +00001402
1403 // disallow negative linespacing
1404 if (leading < 0.0f) {
1405 leading = 0.0f;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001406 }
1407
bungeman41078062014-07-07 08:16:37 -07001408 SkScalar scale = myy;
1409 if (this->isVertical()) {
1410 scale = mxy;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001411 }
bungeman41078062014-07-07 08:16:37 -07001412 metrics->fTop = ymax * scale;
1413 metrics->fAscent = ascent * scale;
1414 metrics->fDescent = descent * scale;
1415 metrics->fBottom = ymin * scale;
1416 metrics->fLeading = leading * scale;
1417 metrics->fAvgCharWidth = avgCharWidth * scale;
reedc17c6582014-10-31 08:20:46 -07001418 metrics->fXMin = xmin * scale;
1419 metrics->fXMax = xmax * scale;
bungeman7316b102014-10-29 12:46:52 -07001420 metrics->fXHeight = x_height;
1421 metrics->fCapHeight = cap_height;
bungeman41078062014-07-07 08:16:37 -07001422 metrics->fUnderlineThickness = underlineThickness * scale;
1423 metrics->fUnderlinePosition = underlinePosition * scale;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001424}
1425
djsollenfcfea992015-01-09 08:18:13 -08001426///////////////////////////////////////////////////////////////////////////////
1427
1428// hand-tuned value to reduce outline embolden strength
1429#ifndef SK_OUTLINE_EMBOLDEN_DIVISOR
1430 #ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
1431 #define SK_OUTLINE_EMBOLDEN_DIVISOR 34
1432 #else
1433 #define SK_OUTLINE_EMBOLDEN_DIVISOR 24
1434 #endif
1435#endif
1436
1437///////////////////////////////////////////////////////////////////////////////
1438
commit-bot@chromium.org6fa81d72013-12-26 15:50:29 +00001439void SkScalerContext_FreeType::emboldenIfNeeded(FT_Face face, FT_GlyphSlot glyph)
1440{
commit-bot@chromium.org921d2b32014-04-01 19:03:07 +00001441 // check to see if the embolden bit is set
1442 if (0 == (fRec.fFlags & SkScalerContext::kEmbolden_Flag)) {
1443 return;
1444 }
1445
commit-bot@chromium.org921d2b32014-04-01 19:03:07 +00001446 switch (glyph->format) {
1447 case FT_GLYPH_FORMAT_OUTLINE:
1448 FT_Pos strength;
djsollenfcfea992015-01-09 08:18:13 -08001449 strength = FT_MulFix(face->units_per_EM, face->size->metrics.y_scale)
1450 / SK_OUTLINE_EMBOLDEN_DIVISOR;
commit-bot@chromium.org921d2b32014-04-01 19:03:07 +00001451 FT_Outline_Embolden(&glyph->outline, strength);
1452 break;
1453 case FT_GLYPH_FORMAT_BITMAP:
1454 FT_GlyphSlot_Own_Bitmap(glyph);
1455 FT_Bitmap_Embolden(glyph->library, &glyph->bitmap, kBitmapEmboldenStrength, 0);
1456 break;
1457 default:
1458 SkDEBUGFAIL("unknown glyph format");
commit-bot@chromium.org6fa81d72013-12-26 15:50:29 +00001459 }
1460}
1461
reed@google.comb4162b12013-07-02 16:32:29 +00001462///////////////////////////////////////////////////////////////////////////////
1463
1464#include "SkUtils.h"
1465
1466static SkUnichar next_utf8(const void** chars) {
1467 return SkUTF8_NextUnichar((const char**)chars);
1468}
1469
1470static SkUnichar next_utf16(const void** chars) {
1471 return SkUTF16_NextUnichar((const uint16_t**)chars);
1472}
1473
1474static SkUnichar next_utf32(const void** chars) {
1475 const SkUnichar** uniChars = (const SkUnichar**)chars;
1476 SkUnichar uni = **uniChars;
1477 *uniChars += 1;
1478 return uni;
1479}
1480
1481typedef SkUnichar (*EncodingProc)(const void**);
1482
1483static EncodingProc find_encoding_proc(SkTypeface::Encoding enc) {
1484 static const EncodingProc gProcs[] = {
1485 next_utf8, next_utf16, next_utf32
1486 };
1487 SkASSERT((size_t)enc < SK_ARRAY_COUNT(gProcs));
1488 return gProcs[enc];
1489}
1490
1491int SkTypeface_FreeType::onCharsToGlyphs(const void* chars, Encoding encoding,
bungeman726cf902015-06-05 13:38:12 -07001492 uint16_t glyphs[], int glyphCount) const
1493{
reed@google.comb4162b12013-07-02 16:32:29 +00001494 AutoFTAccess fta(this);
1495 FT_Face face = fta.face();
1496 if (!face) {
1497 if (glyphs) {
1498 sk_bzero(glyphs, glyphCount * sizeof(glyphs[0]));
1499 }
1500 return 0;
1501 }
1502
1503 EncodingProc next_uni_proc = find_encoding_proc(encoding);
1504
halcanary96fcdcc2015-08-27 07:41:13 -07001505 if (nullptr == glyphs) {
reed@google.comb4162b12013-07-02 16:32:29 +00001506 for (int i = 0; i < glyphCount; ++i) {
1507 if (0 == FT_Get_Char_Index(face, next_uni_proc(&chars))) {
1508 return i;
1509 }
1510 }
1511 return glyphCount;
1512 } else {
1513 int first = glyphCount;
1514 for (int i = 0; i < glyphCount; ++i) {
1515 unsigned id = FT_Get_Char_Index(face, next_uni_proc(&chars));
1516 glyphs[i] = SkToU16(id);
1517 if (0 == id && i < first) {
1518 first = i;
1519 }
1520 }
1521 return first;
1522 }
1523}
1524
1525int SkTypeface_FreeType::onCountGlyphs() const {
bungeman572f8792016-04-29 15:05:02 -07001526 AutoFTAccess fta(this);
1527 FT_Face face = fta.face();
1528 return face ? face->num_glyphs : 0;
reed@google.comb4162b12013-07-02 16:32:29 +00001529}
1530
bungeman@google.com839702b2013-08-07 17:09:22 +00001531SkTypeface::LocalizedStrings* SkTypeface_FreeType::onCreateFamilyNameIterator() const {
bungeman@google.coma9802692013-08-07 02:45:25 +00001532 SkTypeface::LocalizedStrings* nameIter =
1533 SkOTUtils::LocalizedStrings_NameTable::CreateForFamilyNames(*this);
halcanary96fcdcc2015-08-27 07:41:13 -07001534 if (nullptr == nameIter) {
bungeman@google.coma9802692013-08-07 02:45:25 +00001535 SkString familyName;
1536 this->getFamilyName(&familyName);
1537 SkString language("und"); //undetermined
1538 nameIter = new SkOTUtils::LocalizedStrings_SingleName(familyName, language);
1539 }
1540 return nameIter;
1541}
1542
bungeman@google.comddc218e2013-08-01 22:29:43 +00001543int SkTypeface_FreeType::onGetTableTags(SkFontTableTag tags[]) const {
1544 AutoFTAccess fta(this);
1545 FT_Face face = fta.face();
1546
1547 FT_ULong tableCount = 0;
1548 FT_Error error;
1549
halcanary96fcdcc2015-08-27 07:41:13 -07001550 // When 'tag' is nullptr, returns number of tables in 'length'.
1551 error = FT_Sfnt_Table_Info(face, 0, nullptr, &tableCount);
bungeman@google.comddc218e2013-08-01 22:29:43 +00001552 if (error) {
1553 return 0;
1554 }
1555
1556 if (tags) {
1557 for (FT_ULong tableIndex = 0; tableIndex < tableCount; ++tableIndex) {
1558 FT_ULong tableTag;
1559 FT_ULong tablelength;
1560 error = FT_Sfnt_Table_Info(face, tableIndex, &tableTag, &tablelength);
1561 if (error) {
1562 return 0;
1563 }
1564 tags[tableIndex] = static_cast<SkFontTableTag>(tableTag);
1565 }
1566 }
1567 return tableCount;
1568}
1569
1570size_t SkTypeface_FreeType::onGetTableData(SkFontTableTag tag, size_t offset,
1571 size_t length, void* data) const
1572{
1573 AutoFTAccess fta(this);
1574 FT_Face face = fta.face();
1575
1576 FT_ULong tableLength = 0;
1577 FT_Error error;
1578
1579 // When 'length' is 0 it is overwritten with the full table length; 'offset' is ignored.
halcanary96fcdcc2015-08-27 07:41:13 -07001580 error = FT_Load_Sfnt_Table(face, tag, 0, nullptr, &tableLength);
bungeman@google.comddc218e2013-08-01 22:29:43 +00001581 if (error) {
1582 return 0;
1583 }
1584
1585 if (offset > tableLength) {
1586 return 0;
1587 }
bungeman@google.com5ecd4fa2013-08-01 22:48:21 +00001588 FT_ULong size = SkTMin((FT_ULong)length, tableLength - (FT_ULong)offset);
bsalomon49f085d2014-09-05 13:34:00 -07001589 if (data) {
bungeman@google.comddc218e2013-08-01 22:29:43 +00001590 error = FT_Load_Sfnt_Table(face, tag, offset, reinterpret_cast<FT_Byte*>(data), &size);
1591 if (error) {
1592 return 0;
1593 }
1594 }
1595
1596 return size;
1597}
1598
reed@google.comb4162b12013-07-02 16:32:29 +00001599///////////////////////////////////////////////////////////////////////////////
1600///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +00001601
halcanary96fcdcc2015-08-27 07:41:13 -07001602SkTypeface_FreeType::Scanner::Scanner() : fLibrary(nullptr) {
bungeman9dc24682014-12-01 14:01:32 -08001603 if (FT_New_Library(&gFTMemory, &fLibrary)) {
1604 return;
bungeman14df8332014-10-28 15:07:23 -07001605 }
bungeman9dc24682014-12-01 14:01:32 -08001606 FT_Add_Default_Modules(fLibrary);
bungeman14df8332014-10-28 15:07:23 -07001607}
1608SkTypeface_FreeType::Scanner::~Scanner() {
bungeman9dc24682014-12-01 14:01:32 -08001609 if (fLibrary) {
1610 FT_Done_Library(fLibrary);
1611 }
bungeman14df8332014-10-28 15:07:23 -07001612}
1613
1614FT_Face SkTypeface_FreeType::Scanner::openFace(SkStream* stream, int ttcIndex,
1615 FT_Stream ftStream) const
bungeman32501a12014-10-28 12:03:55 -07001616{
halcanary96fcdcc2015-08-27 07:41:13 -07001617 if (fLibrary == nullptr) {
1618 return nullptr;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001619 }
1620
bungeman14df8332014-10-28 15:07:23 -07001621 FT_Open_Args args;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001622 memset(&args, 0, sizeof(args));
1623
1624 const void* memoryBase = stream->getMemoryBase();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001625
bsalomon49f085d2014-09-05 13:34:00 -07001626 if (memoryBase) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001627 args.flags = FT_OPEN_MEMORY;
1628 args.memory_base = (const FT_Byte*)memoryBase;
1629 args.memory_size = stream->getLength();
1630 } else {
bungeman14df8332014-10-28 15:07:23 -07001631 memset(ftStream, 0, sizeof(*ftStream));
1632 ftStream->size = stream->getLength();
1633 ftStream->descriptor.pointer = stream;
bungeman9dc24682014-12-01 14:01:32 -08001634 ftStream->read = sk_ft_stream_io;
1635 ftStream->close = sk_ft_stream_close;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001636
1637 args.flags = FT_OPEN_STREAM;
bungeman14df8332014-10-28 15:07:23 -07001638 args.stream = ftStream;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001639 }
1640
1641 FT_Face face;
bungeman14df8332014-10-28 15:07:23 -07001642 if (FT_Open_Face(fLibrary, &args, ttcIndex, &face)) {
halcanary96fcdcc2015-08-27 07:41:13 -07001643 return nullptr;
bungeman14df8332014-10-28 15:07:23 -07001644 }
1645 return face;
1646}
1647
1648bool SkTypeface_FreeType::Scanner::recognizedFont(SkStream* stream, int* numFaces) const {
1649 SkAutoMutexAcquire libraryLock(fLibraryMutex);
1650
1651 FT_StreamRec streamRec;
1652 FT_Face face = this->openFace(stream, -1, &streamRec);
halcanary96fcdcc2015-08-27 07:41:13 -07001653 if (nullptr == face) {
bungeman14df8332014-10-28 15:07:23 -07001654 return false;
1655 }
1656
1657 *numFaces = face->num_faces;
1658
1659 FT_Done_Face(face);
1660 return true;
1661}
1662
1663#include "SkTSearch.h"
1664bool SkTypeface_FreeType::Scanner::scanFont(
bungeman41868fe2015-05-20 09:21:04 -07001665 SkStream* stream, int ttcIndex,
1666 SkString* name, SkFontStyle* style, bool* isFixedPitch, AxisDefinitions* axes) const
bungeman14df8332014-10-28 15:07:23 -07001667{
1668 SkAutoMutexAcquire libraryLock(fLibraryMutex);
1669
1670 FT_StreamRec streamRec;
1671 FT_Face face = this->openFace(stream, ttcIndex, &streamRec);
halcanary96fcdcc2015-08-27 07:41:13 -07001672 if (nullptr == face) {
djsollen@google.com4dc686d2012-02-15 21:03:45 +00001673 return false;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001674 }
1675
bungemana4c4a2d2014-10-20 13:33:19 -07001676 int weight = SkFontStyle::kNormal_Weight;
1677 int width = SkFontStyle::kNormal_Width;
1678 SkFontStyle::Slant slant = SkFontStyle::kUpright_Slant;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001679 if (face->style_flags & FT_STYLE_FLAG_BOLD) {
bungemana4c4a2d2014-10-20 13:33:19 -07001680 weight = SkFontStyle::kBold_Weight;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001681 }
1682 if (face->style_flags & FT_STYLE_FLAG_ITALIC) {
bungemana4c4a2d2014-10-20 13:33:19 -07001683 slant = SkFontStyle::kItalic_Slant;
1684 }
1685
1686 PS_FontInfoRec psFontInfo;
1687 TT_OS2* os2 = static_cast<TT_OS2*>(FT_Get_Sfnt_Table(face, ft_sfnt_os2));
1688 if (os2 && os2->version != 0xffff) {
1689 weight = os2->usWeightClass;
1690 width = os2->usWidthClass;
bungemanb4bb7d82016-04-27 10:21:04 -07001691
1692 // OS/2::fsSelection bit 9 indicates oblique.
1693 if (SkToBool(os2->fsSelection & (1u << 9))) {
1694 slant = SkFontStyle::kOblique_Slant;
1695 }
bungemana4c4a2d2014-10-20 13:33:19 -07001696 } else if (0 == FT_Get_PS_Font_Info(face, &psFontInfo) && psFontInfo.weight) {
1697 static const struct {
1698 char const * const name;
1699 int const weight;
1700 } commonWeights [] = {
1701 // There are probably more common names, but these are known to exist.
bungemand803cda2015-04-16 14:22:46 -07001702 { "all", SkFontStyle::kNormal_Weight }, // Multiple Masters usually default to normal.
bungemana4c4a2d2014-10-20 13:33:19 -07001703 { "black", SkFontStyle::kBlack_Weight },
1704 { "bold", SkFontStyle::kBold_Weight },
1705 { "book", (SkFontStyle::kNormal_Weight + SkFontStyle::kLight_Weight)/2 },
1706 { "demi", SkFontStyle::kSemiBold_Weight },
1707 { "demibold", SkFontStyle::kSemiBold_Weight },
bungeman14df8332014-10-28 15:07:23 -07001708 { "extra", SkFontStyle::kExtraBold_Weight },
bungemana4c4a2d2014-10-20 13:33:19 -07001709 { "extrabold", SkFontStyle::kExtraBold_Weight },
1710 { "extralight", SkFontStyle::kExtraLight_Weight },
bungeman14df8332014-10-28 15:07:23 -07001711 { "hairline", SkFontStyle::kThin_Weight },
bungemana4c4a2d2014-10-20 13:33:19 -07001712 { "heavy", SkFontStyle::kBlack_Weight },
1713 { "light", SkFontStyle::kLight_Weight },
1714 { "medium", SkFontStyle::kMedium_Weight },
1715 { "normal", SkFontStyle::kNormal_Weight },
bungeman14df8332014-10-28 15:07:23 -07001716 { "plain", SkFontStyle::kNormal_Weight },
bungemana4c4a2d2014-10-20 13:33:19 -07001717 { "regular", SkFontStyle::kNormal_Weight },
bungeman14df8332014-10-28 15:07:23 -07001718 { "roman", SkFontStyle::kNormal_Weight },
bungemana4c4a2d2014-10-20 13:33:19 -07001719 { "semibold", SkFontStyle::kSemiBold_Weight },
bungeman14df8332014-10-28 15:07:23 -07001720 { "standard", SkFontStyle::kNormal_Weight },
bungemana4c4a2d2014-10-20 13:33:19 -07001721 { "thin", SkFontStyle::kThin_Weight },
1722 { "ultra", SkFontStyle::kExtraBold_Weight },
1723 { "ultrablack", 1000 },
1724 { "ultrabold", SkFontStyle::kExtraBold_Weight },
1725 { "ultraheavy", 1000 },
1726 { "ultralight", SkFontStyle::kExtraLight_Weight },
1727 };
1728 int const index = SkStrLCSearch(&commonWeights[0].name, SK_ARRAY_COUNT(commonWeights),
bungemand2ae7282014-10-22 08:25:44 -07001729 psFontInfo.weight, sizeof(commonWeights[0]));
bungemana4c4a2d2014-10-20 13:33:19 -07001730 if (index >= 0) {
1731 weight = commonWeights[index].weight;
1732 } else {
bungeman14df8332014-10-28 15:07:23 -07001733 SkDEBUGF(("Do not know weight for: %s (%s) \n", face->family_name, psFontInfo.weight));
bungemana4c4a2d2014-10-20 13:33:19 -07001734 }
djsollen@google.com4dc686d2012-02-15 21:03:45 +00001735 }
1736
1737 if (name) {
1738 name->set(face->family_name);
1739 }
1740 if (style) {
bungemana4c4a2d2014-10-20 13:33:19 -07001741 *style = SkFontStyle(weight, width, slant);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001742 }
bungeman@google.comfe747652013-03-25 19:36:11 +00001743 if (isFixedPitch) {
1744 *isFixedPitch = FT_IS_FIXED_WIDTH(face);
reed@google.com5b31b0f2011-02-23 14:41:42 +00001745 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001746
bungeman41868fe2015-05-20 09:21:04 -07001747 if (axes && face->face_flags & FT_FACE_FLAG_MULTIPLE_MASTERS) {
halcanary96fcdcc2015-08-27 07:41:13 -07001748 FT_MM_Var* variations = nullptr;
bungeman41868fe2015-05-20 09:21:04 -07001749 FT_Error err = FT_Get_MM_Var(face, &variations);
1750 if (err) {
1751 SkDEBUGF(("INFO: font %s claims to have variations, but none found.\n",
1752 face->family_name));
1753 return false;
1754 }
1755 SkAutoFree autoFreeVariations(variations);
1756
1757 axes->reset(variations->num_axis);
1758 for (FT_UInt i = 0; i < variations->num_axis; ++i) {
1759 const FT_Var_Axis& ftAxis = variations->axis[i];
1760 (*axes)[i].fTag = ftAxis.tag;
1761 (*axes)[i].fMinimum = ftAxis.minimum;
1762 (*axes)[i].fDefault = ftAxis.def;
1763 (*axes)[i].fMaximum = ftAxis.maximum;
1764 }
1765 }
bungeman41868fe2015-05-20 09:21:04 -07001766
reed@android.com8a1c16f2008-12-17 15:59:43 +00001767 FT_Done_Face(face);
djsollen@google.com4dc686d2012-02-15 21:03:45 +00001768 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001769}
bungemanf6c71072016-01-21 14:17:47 -08001770
1771/*static*/ void SkTypeface_FreeType::Scanner::computeAxisValues(
1772 AxisDefinitions axisDefinitions,
1773 const SkFontMgr::FontParameters::Axis* requestedAxes, int requestedAxisCount,
1774 SkFixed* axisValues,
1775 const SkString& name)
1776{
1777 for (int i = 0; i < axisDefinitions.count(); ++i) {
1778 const Scanner::AxisDefinition& axisDefinition = axisDefinitions[i];
benjaminwagner64a3c952016-02-25 12:20:40 -08001779 const SkScalar axisMin = SkFixedToScalar(axisDefinition.fMinimum);
1780 const SkScalar axisMax = SkFixedToScalar(axisDefinition.fMaximum);
bungemanf6c71072016-01-21 14:17:47 -08001781 axisValues[i] = axisDefinition.fDefault;
1782 for (int j = 0; j < requestedAxisCount; ++j) {
1783 const SkFontMgr::FontParameters::Axis& axisSpecified = requestedAxes[j];
1784 if (axisDefinition.fTag == axisSpecified.fTag) {
benjaminwagner64a3c952016-02-25 12:20:40 -08001785 const SkScalar axisValue = SkTPin(axisSpecified.fStyleValue, axisMin, axisMax);
1786 if (axisSpecified.fStyleValue != axisValue) {
bungemanf6c71072016-01-21 14:17:47 -08001787 SkDEBUGF(("Requested font axis value out of range: "
1788 "%s '%c%c%c%c' %f; pinned to %f.\n",
1789 name.c_str(),
1790 (axisDefinition.fTag >> 24) & 0xFF,
1791 (axisDefinition.fTag >> 16) & 0xFF,
1792 (axisDefinition.fTag >> 8) & 0xFF,
1793 (axisDefinition.fTag ) & 0xFF,
1794 SkScalarToDouble(axisSpecified.fStyleValue),
benjaminwagner64a3c952016-02-25 12:20:40 -08001795 SkScalarToDouble(axisValue)));
bungemanf6c71072016-01-21 14:17:47 -08001796 }
benjaminwagner64a3c952016-02-25 12:20:40 -08001797 axisValues[i] = SkScalarToFixed(axisValue);
bungemanf6c71072016-01-21 14:17:47 -08001798 break;
1799 }
1800 }
1801 // TODO: warn on defaulted axis?
1802 }
1803
1804 SkDEBUGCODE(
1805 // Check for axis specified, but not matched in font.
1806 for (int i = 0; i < requestedAxisCount; ++i) {
1807 SkFourByteTag skTag = requestedAxes[i].fTag;
1808 bool found = false;
1809 for (int j = 0; j < axisDefinitions.count(); ++j) {
1810 if (skTag == axisDefinitions[j].fTag) {
1811 found = true;
1812 break;
1813 }
1814 }
1815 if (!found) {
1816 SkDEBUGF(("Requested font axis not found: %s '%c%c%c%c'\n",
1817 name.c_str(),
1818 (skTag >> 24) & 0xFF,
1819 (skTag >> 16) & 0xFF,
1820 (skTag >> 8) & 0xFF,
1821 (skTag) & 0xFF));
1822 }
1823 }
1824 )
1825}