blob: f892438ca3929ed78ffca7f2b7f3e3d924a3fb1d [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"
bungeman7cfd46a2016-10-20 16:06:52 -040017#include "SkMakeUnique.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000018#include "SkMask.h"
bungeman@google.com97efada2012-07-30 20:40:50 +000019#include "SkMaskGamma.h"
bungeman@google.comd3fbd342014-04-15 15:52:07 +000020#include "SkMatrix22.h"
Herb Derbyb549cc32017-03-27 13:35:15 -040021#include "SkMalloc.h"
mtklein1b249332015-07-07 12:21:21 -070022#include "SkMutex.h"
bungeman@google.coma9802692013-08-07 02:45:25 +000023#include "SkOTUtils.h"
bungemand3ebb482015-08-05 13:57:49 -070024#include "SkPath.h"
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000025#include "SkScalerContext.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000026#include "SkStream.h"
27#include "SkString.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000028#include "SkTemplates.h"
mtklein5f939ab2016-03-16 10:28:35 -070029#include <memory>
reed@android.com8a1c16f2008-12-17 15:59:43 +000030
31#include <ft2build.h>
bungeman5ec443c2014-11-21 13:18:34 -080032#include FT_ADVANCES_H
33#include FT_BITMAP_H
reed@android.com8a1c16f2008-12-17 15:59:43 +000034#include FT_FREETYPE_H
bungeman5ec443c2014-11-21 13:18:34 -080035#include FT_LCD_FILTER_H
bungeman9dc24682014-12-01 14:01:32 -080036#include FT_MODULE_H
bungeman41868fe2015-05-20 09:21:04 -070037#include FT_MULTIPLE_MASTERS_H
reed@android.com8a1c16f2008-12-17 15:59:43 +000038#include FT_OUTLINE_H
39#include FT_SIZES_H
bungeman9dc24682014-12-01 14:01:32 -080040#include FT_SYSTEM_H
agl@chromium.orgcc3096b2009-04-22 22:09:04 +000041#include FT_TRUETYPE_TABLES_H
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000042#include FT_TYPE1_TABLES_H
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000043#include FT_XFREE86_H
agl@chromium.orgcc3096b2009-04-22 22:09:04 +000044
Ben Wagnerfc497342017-02-24 11:15:26 -050045// SK_FREETYPE_MINIMUM_RUNTIME_VERSION 0x<major><minor><patch><flags>
46// Flag SK_FREETYPE_DLOPEN: also try dlopen to get newer features.
47#define SK_FREETYPE_DLOPEN (0x1)
48#ifndef SK_FREETYPE_MINIMUM_RUNTIME_VERSION
49# if defined(SK_BUILD_FOR_ANDROID_FRAMEWORK) || defined (GOOGLE3)
50# define SK_FREETYPE_MINIMUM_RUNTIME_VERSION (((FREETYPE_MAJOR) << 24) | ((FREETYPE_MINOR) << 16) | ((FREETYPE_PATCH) << 8))
51# else
52# define SK_FREETYPE_MINIMUM_RUNTIME_VERSION ((2 << 24) | (3 << 16) | (11 << 8) | (SK_FREETYPE_DLOPEN))
53# endif
54#endif
55#if SK_FREETYPE_MINIMUM_RUNTIME_VERSION & SK_FREETYPE_DLOPEN
56# include <dlfcn.h>
57#endif
58
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +000059// FT_LOAD_COLOR and the corresponding FT_Pixel_Mode::FT_PIXEL_MODE_BGRA
60// were introduced in FreeType 2.5.0.
61// The following may be removed once FreeType 2.5.0 is required to build.
62#ifndef FT_LOAD_COLOR
63# define FT_LOAD_COLOR ( 1L << 20 )
64# define FT_PIXEL_MODE_BGRA 7
65#endif
66
Seigo Nonaka52ab2f52016-12-05 02:41:53 +090067// FT_LOAD_BITMAP_METRICS_ONLY was introduced in FreeType 2.7.1
68// The following may be removed once FreeType 2.7.1 is required to build.
69#ifndef FT_LOAD_BITMAP_METRICS_ONLY
70# define FT_LOAD_BITMAP_METRICS_ONLY ( 1L << 22 )
71#endif
72
reed@android.com8a1c16f2008-12-17 15:59:43 +000073//#define ENABLE_GLYPH_SPEW // for tracing calls
74//#define DUMP_STRIKE_CREATION
bungeman5ec443c2014-11-21 13:18:34 -080075//#define SK_FONTHOST_FREETYPE_RUNTIME_VERSION
reed@google.com1ac83502012-02-28 17:06:02 +000076//#define SK_GAMMA_APPLY_TO_A8
reed@google.com1ac83502012-02-28 17:06:02 +000077
reed@google.comeffc5012011-06-27 16:44:46 +000078static bool isLCD(const SkScalerContext::Rec& rec) {
bungeman9dc24682014-12-01 14:01:32 -080079 return SkMask::kLCD16_Format == rec.fMaskFormat;
reed@google.comeffc5012011-06-27 16:44:46 +000080}
81
reed@android.com8a1c16f2008-12-17 15:59:43 +000082//////////////////////////////////////////////////////////////////////////
83
bungeman9dc24682014-12-01 14:01:32 -080084extern "C" {
85 static void* sk_ft_alloc(FT_Memory, long size) {
86 return sk_malloc_throw(size);
87 }
88 static void sk_ft_free(FT_Memory, void* block) {
89 sk_free(block);
90 }
91 static void* sk_ft_realloc(FT_Memory, long cur_size, long new_size, void* block) {
92 return sk_realloc_throw(block, new_size);
93 }
94};
halcanary96fcdcc2015-08-27 07:41:13 -070095FT_MemoryRec_ gFTMemory = { nullptr, sk_ft_alloc, sk_ft_free, sk_ft_realloc };
bungeman9dc24682014-12-01 14:01:32 -080096
97class FreeTypeLibrary : SkNoncopyable {
98public:
Ben Wagnerfc497342017-02-24 11:15:26 -050099 FreeTypeLibrary()
100 : fGetVarDesignCoordinates(nullptr)
101 , fLibrary(nullptr)
102 , fIsLCDSupported(false)
103 , fLCDExtra(0)
104 {
bungeman9dc24682014-12-01 14:01:32 -0800105 if (FT_New_Library(&gFTMemory, &fLibrary)) {
106 return;
107 }
108 FT_Add_Default_Modules(fLibrary);
109
Ben Wagner2a098d02017-03-01 13:00:53 -0500110 // When using dlsym
111 // *(void**)(&procPtr) = dlsym(self, "proc");
112 // is non-standard, but safe for POSIX. Cannot write
113 // *reinterpret_cast<void**>(&procPtr) = dlsym(self, "proc");
114 // because clang has not implemented DR573. See http://clang.llvm.org/cxx_dr_status.html .
115
116 FT_Int major, minor, patch;
117 FT_Library_Version(fLibrary, &major, &minor, &patch);
118
Ben Wagnerfc497342017-02-24 11:15:26 -0500119#if SK_FREETYPE_MINIMUM_RUNTIME_VERSION >= 0x02070100
120 fGetVarDesignCoordinates = FT_Get_Var_Design_Coordinates;
121#elif SK_FREETYPE_MINIMUM_RUNTIME_VERSION & SK_FREETYPE_DLOPEN
Ben Wagner2a098d02017-03-01 13:00:53 -0500122 if (major > 2 || ((major == 2 && minor > 7) || (major == 2 && minor == 7 && patch >= 0))) {
Ben Wagnerfc497342017-02-24 11:15:26 -0500123 //The FreeType library is already loaded, so symbols are available in process.
124 void* self = dlopen(nullptr, RTLD_LAZY);
125 if (self) {
Ben Wagnerfc497342017-02-24 11:15:26 -0500126 *(void**)(&fGetVarDesignCoordinates) = dlsym(self, "FT_Get_Var_Design_Coordinates");
127 dlclose(self);
128 }
129 }
130#endif
131
Ben Wagner2a098d02017-03-01 13:00:53 -0500132#if SK_FREETYPE_MINIMUM_RUNTIME_VERSION >= 0x02070200
133 FT_Set_Default_Properties(fLibrary);
134#elif SK_FREETYPE_MINIMUM_RUNTIME_VERSION & SK_FREETYPE_DLOPEN
135 if (major > 2 || ((major == 2 && minor > 7) || (major == 2 && minor == 7 && patch >= 1))) {
bungeman9dc24682014-12-01 14:01:32 -0800136 //The FreeType library is already loaded, so symbols are available in process.
halcanary96fcdcc2015-08-27 07:41:13 -0700137 void* self = dlopen(nullptr, RTLD_LAZY);
bungeman9dc24682014-12-01 14:01:32 -0800138 if (self) {
Ben Wagner2a098d02017-03-01 13:00:53 -0500139 FT_Set_Default_PropertiesProc setDefaultProperties;
140 *(void**)(&setDefaultProperties) = dlsym(self, "FT_Set_Default_Properties");
bungeman9dc24682014-12-01 14:01:32 -0800141 dlclose(self);
142
Ben Wagner2a098d02017-03-01 13:00:53 -0500143 if (setDefaultProperties) {
144 setDefaultProperties(fLibrary);
bungeman9dc24682014-12-01 14:01:32 -0800145 }
146 }
Ben Wagner2a098d02017-03-01 13:00:53 -0500147 }
bungeman9dc24682014-12-01 14:01:32 -0800148#endif
Ben Wagner2a098d02017-03-01 13:00:53 -0500149
150 // Setup LCD filtering. This reduces color fringes for LCD smoothed glyphs.
151 // The default has changed over time, so this doesn't mean the same thing to all users.
152 if (FT_Library_SetLcdFilter(fLibrary, FT_LCD_FILTER_DEFAULT) == 0) {
153 fIsLCDSupported = true;
154 fLCDExtra = 2; //Using a filter adds one full pixel to each side.
bungeman9dc24682014-12-01 14:01:32 -0800155 }
156 }
157 ~FreeTypeLibrary() {
158 if (fLibrary) {
159 FT_Done_Library(fLibrary);
160 }
161 }
162
163 FT_Library library() { return fLibrary; }
164 bool isLCDSupported() { return fIsLCDSupported; }
165 int lcdExtra() { return fLCDExtra; }
166
Ben Wagnerfc497342017-02-24 11:15:26 -0500167 // FT_Get_{MM,Var}_{Blend,Design}_Coordinates were added in FreeType 2.7.1.
168 // Prior to this there was no way to get the coordinates out of the FT_Face.
169 // This wasn't too bad because you needed to specify them anyway, and the clamp was provided.
170 // However, this doesn't work when face_index specifies named variations as introduced in 2.6.1.
171 using FT_Get_Var_Blend_CoordinatesProc = FT_Error (*)(FT_Face, FT_UInt, FT_Fixed*);
172 FT_Get_Var_Blend_CoordinatesProc fGetVarDesignCoordinates;
173
bungeman9dc24682014-12-01 14:01:32 -0800174private:
175 FT_Library fLibrary;
176 bool fIsLCDSupported;
177 int fLCDExtra;
178
179 // FT_Library_SetLcdFilterWeights was introduced in FreeType 2.4.0.
180 // The following platforms provide FreeType of at least 2.4.0.
181 // Ubuntu >= 11.04 (previous deprecated April 2013)
182 // Debian >= 6.0 (good)
183 // OpenSuse >= 11.4 (previous deprecated January 2012 / Nov 2013 for Evergreen 11.2)
184 // Fedora >= 14 (good)
185 // Android >= Gingerbread (good)
Ben Wagnerfc497342017-02-24 11:15:26 -0500186 // RHEL >= 7 (6 has 2.3.11, EOL Nov 2020, Phase 3 May 2017)
187 using FT_Library_SetLcdFilterWeightsProc = FT_Error (*)(FT_Library, unsigned char*);
Ben Wagner2a098d02017-03-01 13:00:53 -0500188
189 // FreeType added the ability to read global properties in 2.7.0. After 2.7.1 a means for users
190 // of FT_New_Library to request these global properties to be read was added.
191 using FT_Set_Default_PropertiesProc = void (*)(FT_Library);
bungeman9dc24682014-12-01 14:01:32 -0800192};
193
reed@android.com8a1c16f2008-12-17 15:59:43 +0000194struct SkFaceRec;
195
reed086eea92016-05-04 17:12:46 -0700196SK_DECLARE_STATIC_MUTEX(gFTMutex);
bungeman9dc24682014-12-01 14:01:32 -0800197static FreeTypeLibrary* gFTLibrary;
198static SkFaceRec* gFaceRecHead;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000199
bungemanaabd71c2016-03-01 15:15:09 -0800200// Private to ref_ft_library and unref_ft_library
bungeman9dc24682014-12-01 14:01:32 -0800201static int gFTCount;
bungeman@google.comfd668cf2012-08-24 17:46:11 +0000202
scroggo@google.com94bc60f2012-10-04 20:45:06 +0000203// Caller must lock gFTMutex before calling this function.
bungeman9dc24682014-12-01 14:01:32 -0800204static bool ref_ft_library() {
bungeman5ec443c2014-11-21 13:18:34 -0800205 gFTMutex.assertHeld();
bungeman9dc24682014-12-01 14:01:32 -0800206 SkASSERT(gFTCount >= 0);
bungeman5ec443c2014-11-21 13:18:34 -0800207
bungeman9dc24682014-12-01 14:01:32 -0800208 if (0 == gFTCount) {
halcanary96fcdcc2015-08-27 07:41:13 -0700209 SkASSERT(nullptr == gFTLibrary);
halcanary385fe4d2015-08-26 13:07:48 -0700210 gFTLibrary = new FreeTypeLibrary;
reed@google.comea2333d2011-03-14 16:44:56 +0000211 }
bungeman9dc24682014-12-01 14:01:32 -0800212 ++gFTCount;
213 return gFTLibrary->library();
agl@chromium.org309485b2009-07-21 17:41:32 +0000214}
215
bungeman9dc24682014-12-01 14:01:32 -0800216// Caller must lock gFTMutex before calling this function.
217static void unref_ft_library() {
218 gFTMutex.assertHeld();
219 SkASSERT(gFTCount > 0);
commit-bot@chromium.orgba9354b2014-02-10 19:58:49 +0000220
bungeman9dc24682014-12-01 14:01:32 -0800221 --gFTCount;
222 if (0 == gFTCount) {
bungemanaabd71c2016-03-01 15:15:09 -0800223 SkASSERT(nullptr == gFaceRecHead);
halcanary96fcdcc2015-08-27 07:41:13 -0700224 SkASSERT(nullptr != gFTLibrary);
halcanary385fe4d2015-08-26 13:07:48 -0700225 delete gFTLibrary;
halcanary96fcdcc2015-08-27 07:41:13 -0700226 SkDEBUGCODE(gFTLibrary = nullptr;)
bungeman9dc24682014-12-01 14:01:32 -0800227 }
reed@google.comfb2fdcc2012-10-17 15:49:36 +0000228}
229
Ben Wagnerfc497342017-02-24 11:15:26 -0500230///////////////////////////////////////////////////////////////////////////
231
232struct SkFaceRec {
233 SkFaceRec* fNext;
234 std::unique_ptr<FT_FaceRec, SkFunctionWrapper<FT_Error, FT_FaceRec, FT_Done_Face>> fFace;
235 FT_StreamRec fFTStream;
236 std::unique_ptr<SkStreamAsset> fSkStream;
237 uint32_t fRefCnt;
238 uint32_t fFontID;
239
240 // FreeType prior to 2.7.1 does not implement retreiving variation design metrics.
241 // Cache the variation design metrics used to create the font if the user specifies them.
242 SkAutoSTMalloc<4, SkFixed> fAxes;
243 int fAxesCount;
244
245 // FreeType from 2.6.1 (14d6b5d7) until 2.7.0 (ee3f36f6b38) uses font_index for both font index
246 // and named variation index on input, but masks the named variation index part on output.
247 // Manually keep track of when a named variation is requested for 2.6.1 until 2.7.1.
248 bool fNamedVariationSpecified;
249
250 SkFaceRec(std::unique_ptr<SkStreamAsset> stream, uint32_t fontID);
251};
252
253extern "C" {
254 static unsigned long sk_ft_stream_io(FT_Stream ftStream,
255 unsigned long offset,
256 unsigned char* buffer,
257 unsigned long count)
258 {
259 SkStreamAsset* stream = static_cast<SkStreamAsset*>(ftStream->descriptor.pointer);
260
261 if (count) {
262 if (!stream->seek(offset)) {
263 return 0;
264 }
265 count = stream->read(buffer, count);
266 }
267 return count;
268 }
269
270 static void sk_ft_stream_close(FT_Stream) {}
271}
272
273SkFaceRec::SkFaceRec(std::unique_ptr<SkStreamAsset> stream, uint32_t fontID)
274 : fNext(nullptr), fSkStream(std::move(stream)), fRefCnt(1), fFontID(fontID)
275 , fAxesCount(0), fNamedVariationSpecified(false)
276{
277 sk_bzero(&fFTStream, sizeof(fFTStream));
278 fFTStream.size = fSkStream->getLength();
279 fFTStream.descriptor.pointer = fSkStream.get();
280 fFTStream.read = sk_ft_stream_io;
281 fFTStream.close = sk_ft_stream_close;
282}
283
284static void ft_face_setup_axes(SkFaceRec* rec, const SkFontData& data) {
285 if (!(rec->fFace->face_flags & FT_FACE_FLAG_MULTIPLE_MASTERS)) {
286 return;
287 }
288
289 // If a named variation is requested, don't overwrite the named variation's position.
290 if (data.getIndex() > 0xFFFF) {
291 rec->fNamedVariationSpecified = true;
292 return;
293 }
294
295 SkDEBUGCODE(
296 FT_MM_Var* variations = nullptr;
297 if (FT_Get_MM_Var(rec->fFace.get(), &variations)) {
298 SkDEBUGF(("INFO: font %s claims variations, but none found.\n",
299 rec->fFace->family_name));
300 return;
301 }
302 SkAutoFree autoFreeVariations(variations);
303
304 if (static_cast<FT_UInt>(data.getAxisCount()) != variations->num_axis) {
305 SkDEBUGF(("INFO: font %s has %d variations, but %d were specified.\n",
306 rec->fFace->family_name, variations->num_axis, data.getAxisCount()));
307 return;
308 }
309 )
310
311 SkAutoSTMalloc<4, FT_Fixed> coords(data.getAxisCount());
312 for (int i = 0; i < data.getAxisCount(); ++i) {
313 coords[i] = data.getAxis()[i];
314 }
315 if (FT_Set_Var_Design_Coordinates(rec->fFace.get(), data.getAxisCount(), coords.get())) {
316 SkDEBUGF(("INFO: font %s has variations, but specified variations could not be set.\n",
317 rec->fFace->family_name));
318 return;
319 }
320
321 rec->fAxesCount = data.getAxisCount();
322 rec->fAxes.reset(rec->fAxesCount);
323 for (int i = 0; i < rec->fAxesCount; ++i) {
324 rec->fAxes[i] = data.getAxis()[i];
325 }
326}
327
328// Will return nullptr on failure
329// Caller must lock gFTMutex before calling this function.
330static SkFaceRec* ref_ft_face(const SkTypeface* typeface) {
331 gFTMutex.assertHeld();
332
333 const SkFontID fontID = typeface->uniqueID();
334 SkFaceRec* cachedRec = gFaceRecHead;
335 while (cachedRec) {
336 if (cachedRec->fFontID == fontID) {
337 SkASSERT(cachedRec->fFace);
338 cachedRec->fRefCnt += 1;
339 return cachedRec;
340 }
341 cachedRec = cachedRec->fNext;
342 }
343
344 std::unique_ptr<SkFontData> data = typeface->makeFontData();
345 if (nullptr == data || !data->hasStream()) {
346 return nullptr;
347 }
348
349 std::unique_ptr<SkFaceRec> rec(new SkFaceRec(data->detachStream(), fontID));
350
351 FT_Open_Args args;
352 memset(&args, 0, sizeof(args));
353 const void* memoryBase = rec->fSkStream->getMemoryBase();
354 if (memoryBase) {
355 args.flags = FT_OPEN_MEMORY;
356 args.memory_base = (const FT_Byte*)memoryBase;
357 args.memory_size = rec->fSkStream->getLength();
358 } else {
359 args.flags = FT_OPEN_STREAM;
360 args.stream = &rec->fFTStream;
361 }
362
363 {
364 FT_Face rawFace;
365 FT_Error err = FT_Open_Face(gFTLibrary->library(), &args, data->getIndex(), &rawFace);
366 if (err) {
367 SkDEBUGF(("ERROR: unable to open font '%x'\n", fontID));
368 return nullptr;
369 }
370 rec->fFace.reset(rawFace);
371 }
372 SkASSERT(rec->fFace);
373
374 ft_face_setup_axes(rec.get(), *data);
375
376 // FreeType will set the charmap to the "most unicode" cmap if it exists.
377 // If there are no unicode cmaps, the charmap is set to nullptr.
378 // However, "symbol" cmaps should also be considered "fallback unicode" cmaps
379 // because they are effectively private use area only (even if they aren't).
380 // This is the last on the fallback list at
381 // https://developer.apple.com/fonts/TrueType-Reference-Manual/RM06/Chap6cmap.html
382 if (!rec->fFace->charmap) {
383 FT_Select_Charmap(rec->fFace.get(), FT_ENCODING_MS_SYMBOL);
384 }
385
386 rec->fNext = gFaceRecHead;
387 gFaceRecHead = rec.get();
388 return rec.release();
389}
390
391// Caller must lock gFTMutex before calling this function.
Ben Wagnerf1b61af2017-03-09 15:12:09 -0500392// Marked extern because vc++ does not support internal linkage template parameters.
393extern /*static*/ void unref_ft_face(SkFaceRec* faceRec) {
Ben Wagnerfc497342017-02-24 11:15:26 -0500394 gFTMutex.assertHeld();
395
396 SkFaceRec* rec = gFaceRecHead;
397 SkFaceRec* prev = nullptr;
398 while (rec) {
399 SkFaceRec* next = rec->fNext;
400 if (rec->fFace == faceRec->fFace) {
401 if (--rec->fRefCnt == 0) {
402 if (prev) {
403 prev->fNext = next;
404 } else {
405 gFaceRecHead = next;
406 }
407 delete rec;
408 }
409 return;
410 }
411 prev = rec;
412 rec = next;
413 }
414 SkDEBUGFAIL("shouldn't get here, face not in list");
415}
416
417class AutoFTAccess {
418public:
419 AutoFTAccess(const SkTypeface* tf) : fFaceRec(nullptr) {
420 gFTMutex.acquire();
Ben Wagner7ca9a742017-08-17 14:05:04 -0400421 SkASSERT_RELEASE(ref_ft_library());
Ben Wagnerfc497342017-02-24 11:15:26 -0500422 fFaceRec = ref_ft_face(tf);
423 }
424
425 ~AutoFTAccess() {
426 if (fFaceRec) {
427 unref_ft_face(fFaceRec);
428 }
429 unref_ft_library();
430 gFTMutex.release();
431 }
432
433 FT_Face face() { return fFaceRec ? fFaceRec->fFace.get() : nullptr; }
434 int getAxesCount() { return fFaceRec ? fFaceRec->fAxesCount : 0; }
435 SkFixed* getAxes() { return fFaceRec ? fFaceRec->fAxes.get() : nullptr; }
436 bool isNamedVariationSpecified() {
437 return fFaceRec ? fFaceRec->fNamedVariationSpecified : false;
438 }
439
440private:
441 SkFaceRec* fFaceRec;
442};
443
444///////////////////////////////////////////////////////////////////////////
445
george@mozilla.comc59b5da2012-08-23 00:39:08 +0000446class SkScalerContext_FreeType : public SkScalerContext_FreeType_Base {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000447public:
bungeman7cfd46a2016-10-20 16:06:52 -0400448 SkScalerContext_FreeType(sk_sp<SkTypeface>,
449 const SkScalerContextEffects&,
450 const SkDescriptor* desc);
Brian Salomond3b65972017-03-22 12:05:03 -0400451 ~SkScalerContext_FreeType() override;
agl@chromium.orgcc3096b2009-04-22 22:09:04 +0000452
reed@android.com62900b42009-02-11 15:07:19 +0000453 bool success() const {
halcanary96fcdcc2015-08-27 07:41:13 -0700454 return fFTSize != nullptr && fFace != nullptr;
reed@android.com62900b42009-02-11 15:07:19 +0000455 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000456
457protected:
mtklein36352bf2015-03-25 18:17:31 -0700458 unsigned generateGlyphCount() override;
459 uint16_t generateCharToGlyph(SkUnichar uni) override;
460 void generateAdvance(SkGlyph* glyph) override;
461 void generateMetrics(SkGlyph* glyph) override;
462 void generateImage(const SkGlyph& glyph) override;
Ben Wagner6e9ac122016-11-11 14:31:06 -0500463 void generatePath(SkGlyphID glyphID, SkPath* path) override;
mtklein36352bf2015-03-25 18:17:31 -0700464 void generateFontMetrics(SkPaint::FontMetrics*) override;
465 SkUnichar generateGlyphToChar(uint16_t glyph) override;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000466
467private:
Ben Wagnerfc497342017-02-24 11:15:26 -0500468 using UnrefFTFace = SkFunctionWrapper<void, SkFaceRec, unref_ft_face>;
469 std::unique_ptr<SkFaceRec, UnrefFTFace> fFaceRec;
470
471 FT_Face fFace; // Borrowed face from gFaceRecHead.
bungeman401ae2d2016-07-18 15:46:27 -0700472 FT_Size fFTSize; // The size on the fFace for this scaler.
473 FT_Int fStrikeIndex;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000474
bungeman401ae2d2016-07-18 15:46:27 -0700475 /** The rest of the matrix after FreeType handles the size.
476 * With outline font rasterization this is handled by FreeType with FT_Set_Transform.
477 * With bitmap only fonts this matrix must be applied to scale the bitmap.
478 */
479 SkMatrix fMatrix22Scalar;
480 /** Same as fMatrix22Scalar, but in FreeType units and space. */
481 FT_Matrix fMatrix22;
482 /** The actual size requested. */
483 SkVector fScale;
484
485 uint32_t fLoadGlyphFlags;
486 bool fDoLinearMetrics;
487 bool fLCDIsVert;
reed@google.comf073b332013-05-06 12:21:16 +0000488
reed@android.com8a1c16f2008-12-17 15:59:43 +0000489 FT_Error setupSize();
djsollen@google.comd8b599c2012-03-19 19:44:19 +0000490 void getBBoxForCurrentGlyph(SkGlyph* glyph, FT_BBox* bbox,
491 bool snapToPixelBoundary = false);
bungeman@google.comcbe1b542013-12-16 17:02:39 +0000492 bool getCBoxForLetter(char letter, FT_BBox* bbox);
scroggo@google.com94bc60f2012-10-04 20:45:06 +0000493 // Caller must lock gFTMutex before calling this function.
djsollen@google.comd8b599c2012-03-19 19:44:19 +0000494 void updateGlyphIfLCD(SkGlyph* glyph);
commit-bot@chromium.org6fa81d72013-12-26 15:50:29 +0000495 // Caller must lock gFTMutex before calling this function.
496 // update FreeType2 glyph slot with glyph emboldened
Ben Wagnerc3aef182017-06-26 12:47:33 -0400497 void emboldenIfNeeded(FT_Face face, FT_GlyphSlot glyph, SkGlyphID gid);
bungeman401ae2d2016-07-18 15:46:27 -0700498 bool shouldSubpixelBitmap(const SkGlyph&, const SkMatrix&);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000499};
500
501///////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +0000502
vandebo@chromium.org16be6b82011-01-28 21:28:56 +0000503static bool canEmbed(FT_Face face) {
vandebo@chromium.org16be6b82011-01-28 21:28:56 +0000504 FT_UShort fsType = FT_Get_FSType_Flags(face);
505 return (fsType & (FT_FSTYPE_RESTRICTED_LICENSE_EMBEDDING |
506 FT_FSTYPE_BITMAP_EMBEDDING_ONLY)) == 0;
vandebo@chromium.org16be6b82011-01-28 21:28:56 +0000507}
508
vandebo0f9bad02014-06-19 11:05:39 -0700509static bool canSubset(FT_Face face) {
vandebo0f9bad02014-06-19 11:05:39 -0700510 FT_UShort fsType = FT_Get_FSType_Flags(face);
511 return (fsType & FT_FSTYPE_NO_SUBSETTING) == 0;
vandebo0f9bad02014-06-19 11:05:39 -0700512}
513
bungeman5ec443c2014-11-21 13:18:34 -0800514static void populate_glyph_to_unicode(FT_Face& face, SkTDArray<SkUnichar>* glyphToUnicode) {
halcanaryf8c74a12016-04-20 08:37:43 -0700515 FT_Long numGlyphs = face->num_glyphs;
516 glyphToUnicode->setCount(SkToInt(numGlyphs));
517 sk_bzero(glyphToUnicode->begin(), sizeof((*glyphToUnicode)[0]) * numGlyphs);
vandebo@chromium.org6744d492011-05-09 18:13:47 +0000518
bungeman726cf902015-06-05 13:38:12 -0700519 FT_UInt glyphIndex;
520 SkUnichar charCode = FT_Get_First_Char(face, &glyphIndex);
521 while (glyphIndex) {
halcanaryf8c74a12016-04-20 08:37:43 -0700522 SkASSERT(glyphIndex < SkToUInt(numGlyphs));
halcanary5f1d0f62016-09-13 08:08:38 -0700523 // Use the first character that maps to this glyphID. https://crbug.com/359065
524 if (0 == (*glyphToUnicode)[glyphIndex]) {
525 (*glyphToUnicode)[glyphIndex] = charCode;
526 }
bungeman726cf902015-06-05 13:38:12 -0700527 charCode = FT_Get_Next_Char(face, charCode, &glyphIndex);
vandebo@chromium.org6744d492011-05-09 18:13:47 +0000528 }
529}
530
Hal Canary209e4b12017-05-04 14:23:55 -0400531std::unique_ptr<SkAdvancedTypefaceMetrics> SkTypeface_FreeType::onGetAdvancedMetrics() const {
reed@google.comb4162b12013-07-02 16:32:29 +0000532 AutoFTAccess fta(this);
533 FT_Face face = fta.face();
534 if (!face) {
halcanary96fcdcc2015-08-27 07:41:13 -0700535 return nullptr;
reed@google.comb4162b12013-07-02 16:32:29 +0000536 }
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000537
Hal Canary209e4b12017-05-04 14:23:55 -0400538 std::unique_ptr<SkAdvancedTypefaceMetrics> info(new SkAdvancedTypefaceMetrics);
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000539 info->fFontName.set(FT_Get_Postscript_Name(face));
halcanary32875882016-08-16 09:36:23 -0700540
vandebo0f9bad02014-06-19 11:05:39 -0700541 if (FT_HAS_MULTIPLE_MASTERS(face)) {
halcanary32875882016-08-16 09:36:23 -0700542 info->fFlags |= SkAdvancedTypefaceMetrics::kMultiMaster_FontFlag;
vandebo0f9bad02014-06-19 11:05:39 -0700543 }
544 if (!canEmbed(face)) {
halcanary32875882016-08-16 09:36:23 -0700545 info->fFlags |= SkAdvancedTypefaceMetrics::kNotEmbeddable_FontFlag;
vandebo0f9bad02014-06-19 11:05:39 -0700546 }
547 if (!canSubset(face)) {
halcanary32875882016-08-16 09:36:23 -0700548 info->fFlags |= SkAdvancedTypefaceMetrics::kNotSubsettable_FontFlag;
vandebo0f9bad02014-06-19 11:05:39 -0700549 }
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000550
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000551 const char* fontType = FT_Get_X11_Font_Format(face);
vandebo@chromium.orgc3a2ae52011-02-03 21:48:23 +0000552 if (strcmp(fontType, "Type 1") == 0) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000553 info->fType = SkAdvancedTypefaceMetrics::kType1_Font;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000554 } else if (strcmp(fontType, "CID Type 1") == 0) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000555 info->fType = SkAdvancedTypefaceMetrics::kType1CID_Font;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000556 } else if (strcmp(fontType, "CFF") == 0) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000557 info->fType = SkAdvancedTypefaceMetrics::kCFF_Font;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000558 } else if (strcmp(fontType, "TrueType") == 0) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000559 info->fType = SkAdvancedTypefaceMetrics::kTrueType_Font;
bungeman@google.com4d71db82013-12-02 19:10:02 +0000560 } else {
561 info->fType = SkAdvancedTypefaceMetrics::kOther_Font;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000562 }
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000563
halcanary32875882016-08-16 09:36:23 -0700564 info->fStyle = (SkAdvancedTypefaceMetrics::StyleFlags)0;
bungemanf1491692016-07-22 11:19:24 -0700565 if (FT_IS_FIXED_WIDTH(face)) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000566 info->fStyle |= SkAdvancedTypefaceMetrics::kFixedPitch_Style;
bungemanf1491692016-07-22 11:19:24 -0700567 }
568 if (face->style_flags & FT_STYLE_FLAG_ITALIC) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000569 info->fStyle |= SkAdvancedTypefaceMetrics::kItalic_Style;
bungemanf1491692016-07-22 11:19:24 -0700570 }
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000571
bungemanf1491692016-07-22 11:19:24 -0700572 PS_FontInfoRec psFontInfo;
573 TT_Postscript* postTable;
574 if (FT_Get_PS_Font_Info(face, &psFontInfo) == 0) {
575 info->fItalicAngle = psFontInfo.italic_angle;
576 } else if ((postTable = (TT_Postscript*)FT_Get_Sfnt_Table(face, ft_sfnt_post)) != nullptr) {
577 info->fItalicAngle = SkFixedToScalar(postTable->italicAngle);
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000578 } else {
579 info->fItalicAngle = 0;
580 }
581
582 info->fAscent = face->ascender;
583 info->fDescent = face->descender;
584
bungemanf1491692016-07-22 11:19:24 -0700585 TT_PCLT* pcltTable;
586 TT_OS2* os2Table;
587 if ((pcltTable = (TT_PCLT*)FT_Get_Sfnt_Table(face, ft_sfnt_pclt)) != nullptr) {
588 info->fCapHeight = pcltTable->CapHeight;
589 uint8_t serif_style = pcltTable->SerifStyle & 0x3F;
590 if (2 <= serif_style && serif_style <= 6) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000591 info->fStyle |= SkAdvancedTypefaceMetrics::kSerif_Style;
bungemanf1491692016-07-22 11:19:24 -0700592 } else if (9 <= serif_style && serif_style <= 12) {
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000593 info->fStyle |= SkAdvancedTypefaceMetrics::kScript_Style;
bungemanf1491692016-07-22 11:19:24 -0700594 }
595 } else if (((os2Table = (TT_OS2*)FT_Get_Sfnt_Table(face, ft_sfnt_os2)) != nullptr) &&
bungeman@google.comcbe1b542013-12-16 17:02:39 +0000596 // sCapHeight is available only when version 2 or later.
bungemanf1491692016-07-22 11:19:24 -0700597 os2Table->version != 0xFFFF &&
598 os2Table->version >= 2)
599 {
600 info->fCapHeight = os2Table->sCapHeight;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000601 }
vandebo@chromium.orgc48b2b32011-02-02 02:11:22 +0000602 info->fBBox = SkIRect::MakeLTRB(face->bbox.xMin, face->bbox.yMax,
603 face->bbox.xMax, face->bbox.yMin);
604
Hal Canary209e4b12017-05-04 14:23:55 -0400605 bool perGlyphInfo = FT_IS_SCALABLE(face);
vandebo@chromium.org325cb9a2011-03-30 18:36:29 +0000606
Hal Canary209e4b12017-05-04 14:23:55 -0400607 if (perGlyphInfo && info->fType == SkAdvancedTypefaceMetrics::kType1_Font) {
vandebo@chromium.org325cb9a2011-03-30 18:36:29 +0000608 // Postscript fonts may contain more than 255 glyphs, so we end up
609 // using multiple font descriptions with a glyph ordering. Record
610 // the name of each glyph.
halcanary8b1d32c2016-08-08 09:09:59 -0700611 info->fGlyphNames.reset(face->num_glyphs);
vandebo@chromium.org325cb9a2011-03-30 18:36:29 +0000612 for (int gID = 0; gID < face->num_glyphs; gID++) {
613 char glyphName[128]; // PS limit for names is 127 bytes.
614 FT_Get_Glyph_Name(face, gID, glyphName, 128);
halcanary8b1d32c2016-08-08 09:09:59 -0700615 info->fGlyphNames[gID].set(glyphName);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000616 }
617 }
618
Hal Canary209e4b12017-05-04 14:23:55 -0400619 if (perGlyphInfo &&
bungemanf1491692016-07-22 11:19:24 -0700620 info->fType != SkAdvancedTypefaceMetrics::kType1_Font &&
621 face->num_charmaps)
622 {
vandebo@chromium.org6744d492011-05-09 18:13:47 +0000623 populate_glyph_to_unicode(face, &(info->fGlyphToUnicode));
624 }
625
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000626 return info;
627}
vandebo@chromium.org37ad8fb2011-08-18 02:38:50 +0000628
reed@google.com618ef5e2011-01-26 22:10:41 +0000629///////////////////////////////////////////////////////////////////////////
630
reed@google.com8ed436c2011-07-21 14:12:36 +0000631static bool bothZero(SkScalar a, SkScalar b) {
632 return 0 == a && 0 == b;
633}
634
635// returns false if there is any non-90-rotation or skew
636static bool isAxisAligned(const SkScalerContext::Rec& rec) {
637 return 0 == rec.fPreSkewX &&
638 (bothZero(rec.fPost2x2[0][1], rec.fPost2x2[1][0]) ||
639 bothZero(rec.fPost2x2[0][0], rec.fPost2x2[1][1]));
640}
641
reeda9322c22016-04-12 06:47:05 -0700642SkScalerContext* SkTypeface_FreeType::onCreateScalerContext(const SkScalerContextEffects& effects,
643 const SkDescriptor* desc) const {
bungeman7cfd46a2016-10-20 16:06:52 -0400644 auto c = skstd::make_unique<SkScalerContext_FreeType>(
645 sk_ref_sp(const_cast<SkTypeface_FreeType*>(this)), effects, desc);
reed@google.com0da48612013-03-19 16:06:52 +0000646 if (!c->success()) {
Ben Wagnerc05b2bf2016-11-03 16:51:26 -0400647 return nullptr;
reed@google.com0da48612013-03-19 16:06:52 +0000648 }
bungeman7cfd46a2016-10-20 16:06:52 -0400649 return c.release();
reed@google.com0da48612013-03-19 16:06:52 +0000650}
651
652void SkTypeface_FreeType::onFilterRec(SkScalerContextRec* rec) const {
bungeman@google.com8cf32262012-04-02 14:34:30 +0000653 //BOGUS: http://code.google.com/p/chromium/issues/detail?id=121119
654 //Cap the requested size as larger sizes give bogus values.
655 //Remove when http://code.google.com/p/skia/issues/detail?id=554 is fixed.
bungemanaabd71c2016-03-01 15:15:09 -0800656 //Note that this also currently only protects against large text size requests,
657 //the total matrix is not taken into account here.
bungeman@google.com5582e632012-04-02 14:51:54 +0000658 if (rec->fTextSize > SkIntToScalar(1 << 14)) {
scroggo@google.com94bc60f2012-10-04 20:45:06 +0000659 rec->fTextSize = SkIntToScalar(1 << 14);
bungeman@google.com8cf32262012-04-02 14:34:30 +0000660 }
skia.committer@gmail.coma27096b2012-08-30 14:38:00 +0000661
bungemanec7e12f2015-01-21 11:55:16 -0800662 if (isLCD(*rec)) {
bungemand4742fa2015-01-21 11:19:22 -0800663 // TODO: re-work so that FreeType is set-up and selected by the SkFontMgr.
664 SkAutoMutexAcquire ama(gFTMutex);
665 ref_ft_library();
bungemanec7e12f2015-01-21 11:55:16 -0800666 if (!gFTLibrary->isLCDSupported()) {
bungemand4742fa2015-01-21 11:19:22 -0800667 // If the runtime Freetype library doesn't support LCD, disable it here.
668 rec->fMaskFormat = SkMask::kA8_Format;
669 }
670 unref_ft_library();
reed@google.com618ef5e2011-01-26 22:10:41 +0000671 }
reed@google.com5b31b0f2011-02-23 14:41:42 +0000672
reed@google.com618ef5e2011-01-26 22:10:41 +0000673 SkPaint::Hinting h = rec->getHinting();
reed@google.comeffc5012011-06-27 16:44:46 +0000674 if (SkPaint::kFull_Hinting == h && !isLCD(*rec)) {
reed@google.com618ef5e2011-01-26 22:10:41 +0000675 // collapse full->normal hinting if we're not doing LCD
676 h = SkPaint::kNormal_Hinting;
reed@google.com618ef5e2011-01-26 22:10:41 +0000677 }
Ben Wagner423d1632017-08-11 16:25:30 +0000678 if ((rec->fFlags & SkScalerContext::kSubpixelPositioning_Flag)) {
679 if (SkPaint::kNo_Hinting != h) {
680 h = SkPaint::kSlight_Hinting;
681 }
682 }
reed@google.com1ac83502012-02-28 17:06:02 +0000683
reed@google.com8ed436c2011-07-21 14:12:36 +0000684 // rotated text looks bad with hinting, so we disable it as needed
685 if (!isAxisAligned(*rec)) {
686 h = SkPaint::kNo_Hinting;
687 }
reed@google.com618ef5e2011-01-26 22:10:41 +0000688 rec->setHinting(h);
reed@google.comffe49f52011-11-22 19:42:41 +0000689
bungeman@google.com97efada2012-07-30 20:40:50 +0000690#ifndef SK_GAMMA_APPLY_TO_A8
691 if (!isLCD(*rec)) {
brianosmana1e8f8d2016-04-08 06:47:54 -0700692 // SRGBTODO: Is this correct? Do we want contrast boost?
693 rec->ignorePreBlend();
reed@google.comffe49f52011-11-22 19:42:41 +0000694 }
reed@google.com1ac83502012-02-28 17:06:02 +0000695#endif
reed@google.com618ef5e2011-01-26 22:10:41 +0000696}
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000697
reed@google.com38c37dd2013-03-21 15:36:26 +0000698int SkTypeface_FreeType::onGetUPEM() const {
reed@google.comb4162b12013-07-02 16:32:29 +0000699 AutoFTAccess fta(this);
700 FT_Face face = fta.face();
701 return face ? face->units_per_EM : 0;
djsollen@google.comcd9d69b2011-03-14 20:30:14 +0000702}
djsollen@google.comcd9d69b2011-03-14 20:30:14 +0000703
reed@google.com35fe7372013-10-30 15:07:03 +0000704bool SkTypeface_FreeType::onGetKerningPairAdjustments(const uint16_t glyphs[],
705 int count, int32_t adjustments[]) const {
706 AutoFTAccess fta(this);
707 FT_Face face = fta.face();
708 if (!face || !FT_HAS_KERNING(face)) {
709 return false;
710 }
711
712 for (int i = 0; i < count - 1; ++i) {
713 FT_Vector delta;
714 FT_Error err = FT_Get_Kerning(face, glyphs[i], glyphs[i+1],
715 FT_KERNING_UNSCALED, &delta);
716 if (err) {
717 return false;
718 }
719 adjustments[i] = delta.x;
720 }
721 return true;
722}
723
bungeman401ae2d2016-07-18 15:46:27 -0700724/** Returns the bitmap strike equal to or just larger than the requested size. */
benjaminwagner45345622016-02-19 15:30:20 -0800725static FT_Int chooseBitmapStrike(FT_Face face, FT_F26Dot6 scaleY) {
halcanary96fcdcc2015-08-27 07:41:13 -0700726 if (face == nullptr) {
bungeman401ae2d2016-07-18 15:46:27 -0700727 SkDEBUGF(("chooseBitmapStrike aborted due to nullptr face.\n"));
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +0000728 return -1;
729 }
bungeman401ae2d2016-07-18 15:46:27 -0700730
731 FT_Pos requestedPPEM = scaleY; // FT_Bitmap_Size::y_ppem is in 26.6 format.
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +0000732 FT_Int chosenStrikeIndex = -1;
733 FT_Pos chosenPPEM = 0;
734 for (FT_Int strikeIndex = 0; strikeIndex < face->num_fixed_sizes; ++strikeIndex) {
bungeman401ae2d2016-07-18 15:46:27 -0700735 FT_Pos strikePPEM = face->available_sizes[strikeIndex].y_ppem;
736 if (strikePPEM == requestedPPEM) {
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +0000737 // exact match - our search stops here
bungeman401ae2d2016-07-18 15:46:27 -0700738 return strikeIndex;
739 } else if (chosenPPEM < requestedPPEM) {
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +0000740 // attempt to increase chosenPPEM
bungeman401ae2d2016-07-18 15:46:27 -0700741 if (chosenPPEM < strikePPEM) {
742 chosenPPEM = strikePPEM;
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +0000743 chosenStrikeIndex = strikeIndex;
744 }
745 } else {
bungeman401ae2d2016-07-18 15:46:27 -0700746 // attempt to decrease chosenPPEM, but not below requestedPPEM
747 if (requestedPPEM < strikePPEM && strikePPEM < chosenPPEM) {
748 chosenPPEM = strikePPEM;
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +0000749 chosenStrikeIndex = strikeIndex;
750 }
751 }
752 }
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +0000753 return chosenStrikeIndex;
754}
755
bungeman7cfd46a2016-10-20 16:06:52 -0400756SkScalerContext_FreeType::SkScalerContext_FreeType(sk_sp<SkTypeface> typeface,
reeda9322c22016-04-12 06:47:05 -0700757 const SkScalerContextEffects& effects,
758 const SkDescriptor* desc)
bungeman7cfd46a2016-10-20 16:06:52 -0400759 : SkScalerContext_FreeType_Base(std::move(typeface), effects, desc)
bungemanaabd71c2016-03-01 15:15:09 -0800760 , fFace(nullptr)
761 , fFTSize(nullptr)
762 , fStrikeIndex(-1)
bungeman13a007d2015-06-19 05:09:39 -0700763{
reed@android.com8a1c16f2008-12-17 15:59:43 +0000764 SkAutoMutexAcquire ac(gFTMutex);
Ben Wagner7ca9a742017-08-17 14:05:04 -0400765 SkASSERT_RELEASE(ref_ft_library());
reed@android.com8a1c16f2008-12-17 15:59:43 +0000766
Ben Wagnerfc497342017-02-24 11:15:26 -0500767 fFaceRec.reset(ref_ft_face(this->getTypeface()));
768
reed@android.com8a1c16f2008-12-17 15:59:43 +0000769 // load the font file
Ben Wagnerfc497342017-02-24 11:15:26 -0500770 if (nullptr == fFaceRec) {
bungemanaabd71c2016-03-01 15:15:09 -0800771 SkDEBUGF(("Could not create FT_Face.\n"));
reed@android.com62900b42009-02-11 15:07:19 +0000772 return;
773 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000774
bungeman5f14c5e2014-12-05 12:26:44 -0800775 fRec.computeMatrices(SkScalerContextRec::kFull_PreMatrixScale, &fScale, &fMatrix22Scalar);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000776
bungeman401ae2d2016-07-18 15:46:27 -0700777 FT_F26Dot6 scaleX = SkScalarToFDot6(fScale.fX);
778 FT_F26Dot6 scaleY = SkScalarToFDot6(fScale.fY);
bungeman@google.comd3fbd342014-04-15 15:52:07 +0000779 fMatrix22.xx = SkScalarToFixed(fMatrix22Scalar.getScaleX());
bungeman401ae2d2016-07-18 15:46:27 -0700780 fMatrix22.xy = SkScalarToFixed(-fMatrix22Scalar.getSkewX());
781 fMatrix22.yx = SkScalarToFixed(-fMatrix22Scalar.getSkewY());
bungeman@google.comd3fbd342014-04-15 15:52:07 +0000782 fMatrix22.yy = SkScalarToFixed(fMatrix22Scalar.getScaleY());
reed@android.com8a1c16f2008-12-17 15:59:43 +0000783
reed@google.coma1bfa212012-03-08 21:57:12 +0000784 fLCDIsVert = SkToBool(fRec.fFlags & SkScalerContext::kLCD_Vertical_Flag);
785
reed@android.com8a1c16f2008-12-17 15:59:43 +0000786 // compute the flags we send to Load_Glyph
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +0000787 bool linearMetrics = SkToBool(fRec.fFlags & SkScalerContext::kSubpixelPositioning_Flag);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000788 {
reed@android.come4d0bc02009-07-24 19:53:20 +0000789 FT_Int32 loadFlags = FT_LOAD_DEFAULT;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000790
agl@chromium.org70a303f2010-05-10 14:15:50 +0000791 if (SkMask::kBW_Format == fRec.fMaskFormat) {
792 // See http://code.google.com/p/chromium/issues/detail?id=43252#c24
793 loadFlags = FT_LOAD_TARGET_MONO;
reed@google.comeffc5012011-06-27 16:44:46 +0000794 if (fRec.getHinting() == SkPaint::kNo_Hinting) {
agl@chromium.org70a303f2010-05-10 14:15:50 +0000795 loadFlags = FT_LOAD_NO_HINTING;
reed@google.combdc99882011-11-21 14:36:57 +0000796 linearMetrics = true;
reed@google.comeffc5012011-06-27 16:44:46 +0000797 }
agl@chromium.org70a303f2010-05-10 14:15:50 +0000798 } else {
799 switch (fRec.getHinting()) {
800 case SkPaint::kNo_Hinting:
801 loadFlags = FT_LOAD_NO_HINTING;
reed@google.combdc99882011-11-21 14:36:57 +0000802 linearMetrics = true;
agl@chromium.org70a303f2010-05-10 14:15:50 +0000803 break;
804 case SkPaint::kSlight_Hinting:
805 loadFlags = FT_LOAD_TARGET_LIGHT; // This implies FORCE_AUTOHINT
806 break;
807 case SkPaint::kNormal_Hinting:
bungeman@google.comf6f56872014-01-23 19:01:36 +0000808 if (fRec.fFlags & SkScalerContext::kForceAutohinting_Flag) {
agl@chromium.orga2c71cb2010-06-17 20:49:17 +0000809 loadFlags = FT_LOAD_FORCE_AUTOHINT;
djsollen858a7892014-08-20 07:03:23 -0700810#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
811 } else {
812 loadFlags = FT_LOAD_NO_AUTOHINT;
813#endif
bungeman@google.comf6f56872014-01-23 19:01:36 +0000814 }
agl@chromium.org70a303f2010-05-10 14:15:50 +0000815 break;
816 case SkPaint::kFull_Hinting:
bungeman@google.comf6f56872014-01-23 19:01:36 +0000817 if (fRec.fFlags & SkScalerContext::kForceAutohinting_Flag) {
agl@chromium.orga2c71cb2010-06-17 20:49:17 +0000818 loadFlags = FT_LOAD_FORCE_AUTOHINT;
819 break;
820 }
agl@chromium.org70a303f2010-05-10 14:15:50 +0000821 loadFlags = FT_LOAD_TARGET_NORMAL;
reed@google.comeffc5012011-06-27 16:44:46 +0000822 if (isLCD(fRec)) {
reed@google.coma1bfa212012-03-08 21:57:12 +0000823 if (fLCDIsVert) {
reed@google.comeffc5012011-06-27 16:44:46 +0000824 loadFlags = FT_LOAD_TARGET_LCD_V;
825 } else {
826 loadFlags = FT_LOAD_TARGET_LCD;
827 }
reed@google.comea2333d2011-03-14 16:44:56 +0000828 }
agl@chromium.org70a303f2010-05-10 14:15:50 +0000829 break;
830 default:
831 SkDebugf("---------- UNKNOWN hinting %d\n", fRec.getHinting());
832 break;
833 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000834 }
835
reed@google.comeffc5012011-06-27 16:44:46 +0000836 if ((fRec.fFlags & SkScalerContext::kEmbeddedBitmapText_Flag) == 0) {
agl@chromium.orge0d08992009-08-07 19:19:23 +0000837 loadFlags |= FT_LOAD_NO_BITMAP;
reed@google.comeffc5012011-06-27 16:44:46 +0000838 }
agl@chromium.orge0d08992009-08-07 19:19:23 +0000839
reed@google.com96a9f7912011-05-06 11:49:30 +0000840 // Always using FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH to get correct
841 // advances, as fontconfig and cairo do.
842 // See http://code.google.com/p/skia/issues/detail?id=222.
843 loadFlags |= FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH;
844
bungeman@google.com8ff8a192012-09-25 20:38:28 +0000845 // Use vertical layout if requested.
846 if (fRec.fFlags & SkScalerContext::kVertical_Flag) {
847 loadFlags |= FT_LOAD_VERTICAL_LAYOUT;
848 }
849
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +0000850 loadFlags |= FT_LOAD_COLOR;
851
reed@android.come4d0bc02009-07-24 19:53:20 +0000852 fLoadGlyphFlags = loadFlags;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000853 }
854
bungemanaabd71c2016-03-01 15:15:09 -0800855 using DoneFTSize = SkFunctionWrapper<FT_Error, skstd::remove_pointer_t<FT_Size>, FT_Done_Size>;
Ben Wagnerfc497342017-02-24 11:15:26 -0500856 std::unique_ptr<skstd::remove_pointer_t<FT_Size>, DoneFTSize> ftSize([this]() -> FT_Size {
bungemanaabd71c2016-03-01 15:15:09 -0800857 FT_Size size;
Ben Wagnerfc497342017-02-24 11:15:26 -0500858 FT_Error err = FT_New_Size(fFaceRec->fFace.get(), &size);
bungemanaabd71c2016-03-01 15:15:09 -0800859 if (err != 0) {
Ben Wagnerfc497342017-02-24 11:15:26 -0500860 SkDEBUGF(("FT_New_Size(%s) returned 0x%x.\n", fFaceRec->fFace->family_name, err));
bungemanaabd71c2016-03-01 15:15:09 -0800861 return nullptr;
862 }
863 return size;
864 }());
865 if (nullptr == ftSize) {
866 SkDEBUGF(("Could not create FT_Size.\n"));
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +0000867 return;
868 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000869
bungemanaabd71c2016-03-01 15:15:09 -0800870 FT_Error err = FT_Activate_Size(ftSize.get());
871 if (err != 0) {
Ben Wagnerfc497342017-02-24 11:15:26 -0500872 SkDEBUGF(("FT_Activate_Size(%s) returned 0x%x.\n", fFaceRec->fFace->family_name, err));
bungemanaabd71c2016-03-01 15:15:09 -0800873 return;
874 }
875
Ben Wagnerfc497342017-02-24 11:15:26 -0500876 if (FT_IS_SCALABLE(fFaceRec->fFace)) {
877 err = FT_Set_Char_Size(fFaceRec->fFace.get(), scaleX, scaleY, 72, 72);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000878 if (err != 0) {
bungeman401ae2d2016-07-18 15:46:27 -0700879 SkDEBUGF(("FT_Set_CharSize(%s, %f, %f) returned 0x%x.\n",
Ben Wagnerfc497342017-02-24 11:15:26 -0500880 fFaceRec->fFace->family_name, fScale.fX, fScale.fY, err));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000881 return;
882 }
Ben Wagnerfc497342017-02-24 11:15:26 -0500883 } else if (FT_HAS_FIXED_SIZES(fFaceRec->fFace)) {
884 fStrikeIndex = chooseBitmapStrike(fFaceRec->fFace.get(), scaleY);
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +0000885 if (fStrikeIndex == -1) {
Ben Wagnerfc497342017-02-24 11:15:26 -0500886 SkDEBUGF(("No glyphs for font \"%s\" size %f.\n",
887 fFaceRec->fFace->family_name, fScale.fY));
bungeman401ae2d2016-07-18 15:46:27 -0700888 return;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000889 }
bungeman401ae2d2016-07-18 15:46:27 -0700890
Ben Wagnerfc497342017-02-24 11:15:26 -0500891 err = FT_Select_Size(fFaceRec->fFace.get(), fStrikeIndex);
bungeman401ae2d2016-07-18 15:46:27 -0700892 if (err != 0) {
893 SkDEBUGF(("FT_Select_Size(%s, %d) returned 0x%x.\n",
Ben Wagnerfc497342017-02-24 11:15:26 -0500894 fFaceRec->fFace->family_name, fStrikeIndex, err));
bungeman401ae2d2016-07-18 15:46:27 -0700895 fStrikeIndex = -1;
896 return;
897 }
898
899 // A non-ideal size was picked, so recompute the matrix.
900 // This adjusts for the difference between FT_Set_Char_Size and FT_Select_Size.
Ben Wagnerfc497342017-02-24 11:15:26 -0500901 fMatrix22Scalar.preScale(fScale.x() / fFaceRec->fFace->size->metrics.x_ppem,
902 fScale.y() / fFaceRec->fFace->size->metrics.y_ppem);
bungeman401ae2d2016-07-18 15:46:27 -0700903 fMatrix22.xx = SkScalarToFixed(fMatrix22Scalar.getScaleX());
904 fMatrix22.xy = SkScalarToFixed(-fMatrix22Scalar.getSkewX());
905 fMatrix22.yx = SkScalarToFixed(-fMatrix22Scalar.getSkewY());
906 fMatrix22.yy = SkScalarToFixed(fMatrix22Scalar.getScaleY());
907
908 // FreeType does not provide linear metrics for bitmap fonts.
909 linearMetrics = false;
910
911 // FreeType documentation says:
912 // FT_LOAD_NO_BITMAP -- Ignore bitmap strikes when loading.
913 // Bitmap-only fonts ignore this flag.
914 //
915 // However, in FreeType 2.5.1 color bitmap only fonts do not ignore this flag.
916 // Force this flag off for bitmap only fonts.
917 fLoadGlyphFlags &= ~FT_LOAD_NO_BITMAP;
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +0000918 } else {
Ben Wagnerfc497342017-02-24 11:15:26 -0500919 SkDEBUGF(("Unknown kind of font \"%s\" size %f.\n", fFaceRec->fFace->family_name, fScale.fY));
bungeman401ae2d2016-07-18 15:46:27 -0700920 return;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000921 }
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +0000922
bungemanaabd71c2016-03-01 15:15:09 -0800923 fFTSize = ftSize.release();
Ben Wagnerfc497342017-02-24 11:15:26 -0500924 fFace = fFaceRec->fFace.get();
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +0000925 fDoLinearMetrics = linearMetrics;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000926}
927
928SkScalerContext_FreeType::~SkScalerContext_FreeType() {
scroggo@google.com94bc60f2012-10-04 20:45:06 +0000929 SkAutoMutexAcquire ac(gFTMutex);
930
halcanary96fcdcc2015-08-27 07:41:13 -0700931 if (fFTSize != nullptr) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000932 FT_Done_Size(fFTSize);
933 }
934
Ben Wagnerfc497342017-02-24 11:15:26 -0500935 fFaceRec = nullptr;
bungeman9dc24682014-12-01 14:01:32 -0800936
937 unref_ft_library();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000938}
939
940/* We call this before each use of the fFace, since we may be sharing
941 this face with other context (at different sizes).
942*/
943FT_Error SkScalerContext_FreeType::setupSize() {
bungeman3f846ae2015-11-03 11:07:20 -0800944 gFTMutex.assertHeld();
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +0000945 FT_Error err = FT_Activate_Size(fFTSize);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000946 if (err != 0) {
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +0000947 return err;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000948 }
halcanary96fcdcc2015-08-27 07:41:13 -0700949 FT_Set_Transform(fFace, &fMatrix22, nullptr);
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +0000950 return 0;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000951}
952
ctguil@chromium.org0bc7bf52011-03-04 19:04:57 +0000953unsigned SkScalerContext_FreeType::generateGlyphCount() {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000954 return fFace->num_glyphs;
955}
956
957uint16_t SkScalerContext_FreeType::generateCharToGlyph(SkUnichar uni) {
bungeman3f846ae2015-11-03 11:07:20 -0800958 SkAutoMutexAcquire ac(gFTMutex);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000959 return SkToU16(FT_Get_Char_Index( fFace, uni ));
960}
961
reed@android.com9d3a9852010-01-08 14:07:42 +0000962SkUnichar SkScalerContext_FreeType::generateGlyphToChar(uint16_t glyph) {
bungeman3f846ae2015-11-03 11:07:20 -0800963 SkAutoMutexAcquire ac(gFTMutex);
reed@android.com9d3a9852010-01-08 14:07:42 +0000964 // iterate through each cmap entry, looking for matching glyph indices
965 FT_UInt glyphIndex;
966 SkUnichar charCode = FT_Get_First_Char( fFace, &glyphIndex );
967
968 while (glyphIndex != 0) {
969 if (glyphIndex == glyph) {
970 return charCode;
971 }
972 charCode = FT_Get_Next_Char( fFace, charCode, &glyphIndex );
973 }
974
975 return 0;
976}
977
benjaminwagner6b3eacb2016-03-24 19:07:58 -0700978static SkScalar SkFT_FixedToScalar(FT_Fixed x) {
979 return SkFixedToScalar(x);
980}
981
reed@android.com8a1c16f2008-12-17 15:59:43 +0000982void SkScalerContext_FreeType::generateAdvance(SkGlyph* glyph) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000983 /* unhinted and light hinted text have linearly scaled advances
984 * which are very cheap to compute with some font formats...
985 */
reed@google.combdc99882011-11-21 14:36:57 +0000986 if (fDoLinearMetrics) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000987 SkAutoMutexAcquire ac(gFTMutex);
988
989 if (this->setupSize()) {
reed@android.com62900b42009-02-11 15:07:19 +0000990 glyph->zeroMetrics();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000991 return;
992 }
993
994 FT_Error error;
995 FT_Fixed advance;
996
djsollen1b277042014-08-06 06:58:06 -0700997 error = FT_Get_Advance( fFace, glyph->getGlyphID(),
reed@android.com8a1c16f2008-12-17 15:59:43 +0000998 fLoadGlyphFlags | FT_ADVANCE_FLAG_FAST_ONLY,
999 &advance );
1000 if (0 == error) {
1001 glyph->fRsbDelta = 0;
1002 glyph->fLsbDelta = 0;
benjaminwagner6b3eacb2016-03-24 19:07:58 -07001003 const SkScalar advanceScalar = SkFT_FixedToScalar(advance);
1004 glyph->fAdvanceX = SkScalarToFloat(fMatrix22Scalar.getScaleX() * advanceScalar);
bungeman401ae2d2016-07-18 15:46:27 -07001005 glyph->fAdvanceY = SkScalarToFloat(fMatrix22Scalar.getSkewY() * advanceScalar);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001006 return;
1007 }
1008 }
bungeman5ec443c2014-11-21 13:18:34 -08001009
reed@android.com8a1c16f2008-12-17 15:59:43 +00001010 /* otherwise, we need to load/hint the glyph, which is slower */
1011 this->generateMetrics(glyph);
1012 return;
1013}
1014
djsollen@google.comd8b599c2012-03-19 19:44:19 +00001015void SkScalerContext_FreeType::getBBoxForCurrentGlyph(SkGlyph* glyph,
1016 FT_BBox* bbox,
1017 bool snapToPixelBoundary) {
1018
1019 FT_Outline_Get_CBox(&fFace->glyph->outline, bbox);
1020
1021 if (fRec.fFlags & SkScalerContext::kSubpixelPositioning_Flag) {
george@mozilla.comc59b5da2012-08-23 00:39:08 +00001022 int dx = SkFixedToFDot6(glyph->getSubXFixed());
1023 int dy = SkFixedToFDot6(glyph->getSubYFixed());
djsollen@google.comd8b599c2012-03-19 19:44:19 +00001024 // negate dy since freetype-y-goes-up and skia-y-goes-down
1025 bbox->xMin += dx;
1026 bbox->yMin -= dy;
1027 bbox->xMax += dx;
1028 bbox->yMax -= dy;
1029 }
1030
1031 // outset the box to integral boundaries
1032 if (snapToPixelBoundary) {
1033 bbox->xMin &= ~63;
1034 bbox->yMin &= ~63;
1035 bbox->xMax = (bbox->xMax + 63) & ~63;
1036 bbox->yMax = (bbox->yMax + 63) & ~63;
1037 }
bungeman@google.com8ff8a192012-09-25 20:38:28 +00001038
1039 // Must come after snapToPixelBoundary so that the width and height are
1040 // consistent. Otherwise asserts will fire later on when generating the
1041 // glyph image.
1042 if (fRec.fFlags & SkScalerContext::kVertical_Flag) {
1043 FT_Vector vector;
1044 vector.x = fFace->glyph->metrics.vertBearingX - fFace->glyph->metrics.horiBearingX;
1045 vector.y = -fFace->glyph->metrics.vertBearingY - fFace->glyph->metrics.horiBearingY;
1046 FT_Vector_Transform(&vector, &fMatrix22);
1047 bbox->xMin += vector.x;
1048 bbox->xMax += vector.x;
1049 bbox->yMin += vector.y;
1050 bbox->yMax += vector.y;
1051 }
djsollen@google.comd8b599c2012-03-19 19:44:19 +00001052}
1053
bungeman@google.comcbe1b542013-12-16 17:02:39 +00001054bool SkScalerContext_FreeType::getCBoxForLetter(char letter, FT_BBox* bbox) {
1055 const FT_UInt glyph_id = FT_Get_Char_Index(fFace, letter);
bungeman5ec443c2014-11-21 13:18:34 -08001056 if (!glyph_id) {
bungeman@google.comcbe1b542013-12-16 17:02:39 +00001057 return false;
bungeman5ec443c2014-11-21 13:18:34 -08001058 }
1059 if (FT_Load_Glyph(fFace, glyph_id, fLoadGlyphFlags) != 0) {
bungeman@google.comcbe1b542013-12-16 17:02:39 +00001060 return false;
bungeman5ec443c2014-11-21 13:18:34 -08001061 }
Ben Wagnerc3aef182017-06-26 12:47:33 -04001062 emboldenIfNeeded(fFace, fFace->glyph, SkTo<SkGlyphID>(glyph_id));
bungeman@google.comcbe1b542013-12-16 17:02:39 +00001063 FT_Outline_Get_CBox(&fFace->glyph->outline, bbox);
1064 return true;
1065}
1066
djsollen@google.comd8b599c2012-03-19 19:44:19 +00001067void SkScalerContext_FreeType::updateGlyphIfLCD(SkGlyph* glyph) {
1068 if (isLCD(fRec)) {
1069 if (fLCDIsVert) {
bungeman9dc24682014-12-01 14:01:32 -08001070 glyph->fHeight += gFTLibrary->lcdExtra();
1071 glyph->fTop -= gFTLibrary->lcdExtra() >> 1;
djsollen@google.comd8b599c2012-03-19 19:44:19 +00001072 } else {
bungeman9dc24682014-12-01 14:01:32 -08001073 glyph->fWidth += gFTLibrary->lcdExtra();
1074 glyph->fLeft -= gFTLibrary->lcdExtra() >> 1;
djsollen@google.comd8b599c2012-03-19 19:44:19 +00001075 }
1076 }
1077}
1078
bungeman401ae2d2016-07-18 15:46:27 -07001079bool SkScalerContext_FreeType::shouldSubpixelBitmap(const SkGlyph& glyph, const SkMatrix& matrix) {
1080 // If subpixel rendering of a bitmap *can* be done.
1081 bool mechanism = fFace->glyph->format == FT_GLYPH_FORMAT_BITMAP &&
1082 fRec.fFlags & SkScalerContext::kSubpixelPositioning_Flag &&
1083 (glyph.getSubXFixed() || glyph.getSubYFixed());
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +00001084
bungeman401ae2d2016-07-18 15:46:27 -07001085 // If subpixel rendering of a bitmap *should* be done.
1086 // 1. If the face is not scalable then always allow subpixel rendering.
1087 // Otherwise, if the font has an 8ppem strike 7 will subpixel render but 8 won't.
1088 // 2. If the matrix is already not identity the bitmap will already be resampled,
1089 // so resampling slightly differently shouldn't make much difference.
1090 bool policy = !FT_IS_SCALABLE(fFace) || !matrix.isIdentity();
1091
1092 return mechanism && policy;
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +00001093}
1094
reed@android.com8a1c16f2008-12-17 15:59:43 +00001095void SkScalerContext_FreeType::generateMetrics(SkGlyph* glyph) {
1096 SkAutoMutexAcquire ac(gFTMutex);
1097
1098 glyph->fRsbDelta = 0;
1099 glyph->fLsbDelta = 0;
1100
1101 FT_Error err;
1102
1103 if (this->setupSize()) {
bungeman13a007d2015-06-19 05:09:39 -07001104 glyph->zeroMetrics();
1105 return;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001106 }
1107
Seigo Nonaka52ab2f52016-12-05 02:41:53 +09001108 err = FT_Load_Glyph( fFace, glyph->getGlyphID(),
1109 fLoadGlyphFlags | FT_LOAD_BITMAP_METRICS_ONLY );
reed@android.com8a1c16f2008-12-17 15:59:43 +00001110 if (err != 0) {
reed@android.com62900b42009-02-11 15:07:19 +00001111 glyph->zeroMetrics();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001112 return;
1113 }
Ben Wagnerc3aef182017-06-26 12:47:33 -04001114 emboldenIfNeeded(fFace, fFace->glyph, glyph->getGlyphID());
reed@android.com8a1c16f2008-12-17 15:59:43 +00001115
1116 switch ( fFace->glyph->format ) {
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +00001117 case FT_GLYPH_FORMAT_OUTLINE:
bungeman@google.com0f0c2882011-11-04 15:47:41 +00001118 if (0 == fFace->glyph->outline.n_contours) {
1119 glyph->fWidth = 0;
1120 glyph->fHeight = 0;
1121 glyph->fTop = 0;
1122 glyph->fLeft = 0;
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +00001123 } else {
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +00001124 FT_BBox bbox;
1125 getBBoxForCurrentGlyph(glyph, &bbox, true);
1126
1127 glyph->fWidth = SkToU16(SkFDot6Floor(bbox.xMax - bbox.xMin));
1128 glyph->fHeight = SkToU16(SkFDot6Floor(bbox.yMax - bbox.yMin));
1129 glyph->fTop = -SkToS16(SkFDot6Floor(bbox.yMax));
1130 glyph->fLeft = SkToS16(SkFDot6Floor(bbox.xMin));
1131
1132 updateGlyphIfLCD(glyph);
bungeman@google.com0f0c2882011-11-04 15:47:41 +00001133 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001134 break;
1135
1136 case FT_GLYPH_FORMAT_BITMAP:
bungeman@google.com8ff8a192012-09-25 20:38:28 +00001137 if (fRec.fFlags & SkScalerContext::kVertical_Flag) {
1138 FT_Vector vector;
1139 vector.x = fFace->glyph->metrics.vertBearingX - fFace->glyph->metrics.horiBearingX;
1140 vector.y = -fFace->glyph->metrics.vertBearingY - fFace->glyph->metrics.horiBearingY;
1141 FT_Vector_Transform(&vector, &fMatrix22);
1142 fFace->glyph->bitmap_left += SkFDot6Floor(vector.x);
1143 fFace->glyph->bitmap_top += SkFDot6Floor(vector.y);
1144 }
1145
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +00001146 if (fFace->glyph->bitmap.pixel_mode == FT_PIXEL_MODE_BGRA) {
1147 glyph->fMaskFormat = SkMask::kARGB32_Format;
1148 }
1149
bungeman401ae2d2016-07-18 15:46:27 -07001150 {
1151 SkRect rect = SkRect::MakeXYWH(SkIntToScalar(fFace->glyph->bitmap_left),
1152 -SkIntToScalar(fFace->glyph->bitmap_top),
1153 SkIntToScalar(fFace->glyph->bitmap.width),
1154 SkIntToScalar(fFace->glyph->bitmap.rows));
1155 fMatrix22Scalar.mapRect(&rect);
1156 if (this->shouldSubpixelBitmap(*glyph, fMatrix22Scalar)) {
1157 rect.offset(SkFixedToScalar(glyph->getSubXFixed()),
1158 SkFixedToScalar(glyph->getSubYFixed()));
1159 }
1160 SkIRect irect = rect.roundOut();
1161 glyph->fWidth = SkToU16(irect.width());
1162 glyph->fHeight = SkToU16(irect.height());
1163 glyph->fTop = SkToS16(irect.top());
1164 glyph->fLeft = SkToS16(irect.left());
1165 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001166 break;
1167
1168 default:
tomhudson@google.com0c00f212011-12-28 14:59:50 +00001169 SkDEBUGFAIL("unknown glyph format");
bungeman13a007d2015-06-19 05:09:39 -07001170 glyph->zeroMetrics();
1171 return;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001172 }
1173
bungeman@google.com8ff8a192012-09-25 20:38:28 +00001174 if (fRec.fFlags & SkScalerContext::kVertical_Flag) {
1175 if (fDoLinearMetrics) {
benjaminwagner6b3eacb2016-03-24 19:07:58 -07001176 const SkScalar advanceScalar = SkFT_FixedToScalar(fFace->glyph->linearVertAdvance);
bungeman401ae2d2016-07-18 15:46:27 -07001177 glyph->fAdvanceX = SkScalarToFloat(fMatrix22Scalar.getSkewX() * advanceScalar);
benjaminwagner6b3eacb2016-03-24 19:07:58 -07001178 glyph->fAdvanceY = SkScalarToFloat(fMatrix22Scalar.getScaleY() * advanceScalar);
bungeman@google.com8ff8a192012-09-25 20:38:28 +00001179 } else {
benjaminwagner6b3eacb2016-03-24 19:07:58 -07001180 glyph->fAdvanceX = -SkFDot6ToFloat(fFace->glyph->advance.x);
1181 glyph->fAdvanceY = SkFDot6ToFloat(fFace->glyph->advance.y);
bungeman@google.com8ff8a192012-09-25 20:38:28 +00001182 }
bungeman@google.com34f10262012-03-23 18:11:47 +00001183 } else {
bungeman@google.com8ff8a192012-09-25 20:38:28 +00001184 if (fDoLinearMetrics) {
benjaminwagner6b3eacb2016-03-24 19:07:58 -07001185 const SkScalar advanceScalar = SkFT_FixedToScalar(fFace->glyph->linearHoriAdvance);
1186 glyph->fAdvanceX = SkScalarToFloat(fMatrix22Scalar.getScaleX() * advanceScalar);
bungeman401ae2d2016-07-18 15:46:27 -07001187 glyph->fAdvanceY = SkScalarToFloat(fMatrix22Scalar.getSkewY() * advanceScalar);
bungeman@google.com8ff8a192012-09-25 20:38:28 +00001188 } else {
benjaminwagner6b3eacb2016-03-24 19:07:58 -07001189 glyph->fAdvanceX = SkFDot6ToFloat(fFace->glyph->advance.x);
1190 glyph->fAdvanceY = -SkFDot6ToFloat(fFace->glyph->advance.y);
bungeman@google.com34f10262012-03-23 18:11:47 +00001191
bungeman@google.com8ff8a192012-09-25 20:38:28 +00001192 if (fRec.fFlags & kDevKernText_Flag) {
1193 glyph->fRsbDelta = SkToS8(fFace->glyph->rsb_delta);
1194 glyph->fLsbDelta = SkToS8(fFace->glyph->lsb_delta);
djsollen@google.comd8b599c2012-03-19 19:44:19 +00001195 }
1196 }
djsollen@google.comd8b599c2012-03-19 19:44:19 +00001197 }
1198
reed@android.com8a1c16f2008-12-17 15:59:43 +00001199#ifdef ENABLE_GLYPH_SPEW
djsollen1b277042014-08-06 06:58:06 -07001200 SkDEBUGF(("Metrics(glyph:%d flags:0x%x) w:%d\n", glyph->getGlyphID(), fLoadGlyphFlags, glyph->fWidth));
reed@android.com8a1c16f2008-12-17 15:59:43 +00001201#endif
1202}
1203
bungeman5ec443c2014-11-21 13:18:34 -08001204static void clear_glyph_image(const SkGlyph& glyph) {
1205 sk_bzero(glyph.fImage, glyph.rowBytes() * glyph.fHeight);
1206}
reed@google.comea2333d2011-03-14 16:44:56 +00001207
bungeman@google.coma76de722012-10-26 19:35:54 +00001208void SkScalerContext_FreeType::generateImage(const SkGlyph& glyph) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001209 SkAutoMutexAcquire ac(gFTMutex);
1210
reed@android.com8a1c16f2008-12-17 15:59:43 +00001211 if (this->setupSize()) {
bungeman5ec443c2014-11-21 13:18:34 -08001212 clear_glyph_image(glyph);
1213 return;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001214 }
1215
bungeman5ec443c2014-11-21 13:18:34 -08001216 FT_Error err = FT_Load_Glyph(fFace, glyph.getGlyphID(), fLoadGlyphFlags);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001217 if (err != 0) {
1218 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 -08001219 glyph.getGlyphID(), glyph.fWidth, glyph.fHeight, glyph.rowBytes(), fLoadGlyphFlags, err));
1220 clear_glyph_image(glyph);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001221 return;
1222 }
1223
Ben Wagnerc3aef182017-06-26 12:47:33 -04001224 emboldenIfNeeded(fFace, fFace->glyph, glyph.getGlyphID());
bungeman401ae2d2016-07-18 15:46:27 -07001225 SkMatrix* bitmapMatrix = &fMatrix22Scalar;
1226 SkMatrix subpixelBitmapMatrix;
1227 if (this->shouldSubpixelBitmap(glyph, *bitmapMatrix)) {
1228 subpixelBitmapMatrix = fMatrix22Scalar;
1229 subpixelBitmapMatrix.postTranslate(SkFixedToScalar(glyph.getSubXFixed()),
1230 SkFixedToScalar(glyph.getSubYFixed()));
1231 bitmapMatrix = &subpixelBitmapMatrix;
1232 }
1233 generateGlyphImage(fFace, glyph, *bitmapMatrix);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001234}
1235
reed@android.com8a1c16f2008-12-17 15:59:43 +00001236
Ben Wagner6e9ac122016-11-11 14:31:06 -05001237void SkScalerContext_FreeType::generatePath(SkGlyphID glyphID, SkPath* path) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001238 SkAutoMutexAcquire ac(gFTMutex);
1239
caryclarka10742c2014-09-18 11:00:40 -07001240 SkASSERT(path);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001241
1242 if (this->setupSize()) {
1243 path->reset();
1244 return;
1245 }
1246
1247 uint32_t flags = fLoadGlyphFlags;
1248 flags |= FT_LOAD_NO_BITMAP; // ignore embedded bitmaps so we're sure to get the outline
1249 flags &= ~FT_LOAD_RENDER; // don't scan convert (we just want the outline)
1250
Ben Wagner6e9ac122016-11-11 14:31:06 -05001251 FT_Error err = FT_Load_Glyph(fFace, glyphID, flags);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001252
1253 if (err != 0) {
1254 SkDEBUGF(("SkScalerContext_FreeType::generatePath: FT_Load_Glyph(glyph:%d flags:%d) returned 0x%x\n",
Ben Wagner6e9ac122016-11-11 14:31:06 -05001255 glyphID, flags, err));
reed@android.com8a1c16f2008-12-17 15:59:43 +00001256 path->reset();
1257 return;
1258 }
Ben Wagnerc3aef182017-06-26 12:47:33 -04001259 emboldenIfNeeded(fFace, fFace->glyph, glyphID);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001260
sugoi@google.com66a58ac2013-03-05 20:40:52 +00001261 generateGlyphPath(fFace, path);
bungeman@google.com8ff8a192012-09-25 20:38:28 +00001262
1263 // The path's origin from FreeType is always the horizontal layout origin.
1264 // Offset the path so that it is relative to the vertical origin if needed.
1265 if (fRec.fFlags & SkScalerContext::kVertical_Flag) {
1266 FT_Vector vector;
1267 vector.x = fFace->glyph->metrics.vertBearingX - fFace->glyph->metrics.horiBearingX;
1268 vector.y = -fFace->glyph->metrics.vertBearingY - fFace->glyph->metrics.horiBearingY;
1269 FT_Vector_Transform(&vector, &fMatrix22);
1270 path->offset(SkFDot6ToScalar(vector.x), -SkFDot6ToScalar(vector.y));
1271 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001272}
1273
bungeman41078062014-07-07 08:16:37 -07001274void SkScalerContext_FreeType::generateFontMetrics(SkPaint::FontMetrics* metrics) {
halcanary96fcdcc2015-08-27 07:41:13 -07001275 if (nullptr == metrics) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001276 return;
1277 }
1278
bungeman41078062014-07-07 08:16:37 -07001279 SkAutoMutexAcquire ac(gFTMutex);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001280
1281 if (this->setupSize()) {
bungeman41078062014-07-07 08:16:37 -07001282 sk_bzero(metrics, sizeof(*metrics));
reed@android.com8a1c16f2008-12-17 15:59:43 +00001283 return;
1284 }
1285
reed@android.coma8a8b8b2009-05-04 15:00:11 +00001286 FT_Face face = fFace;
Ben Wagner219f3622017-07-17 15:32:25 -04001287 metrics->fFlags = 0;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001288
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +00001289 // fetch units/EM from "head" table if needed (ie for bitmap fonts)
1290 SkScalar upem = SkIntToScalar(face->units_per_EM);
1291 if (!upem) {
1292 TT_Header* ttHeader = (TT_Header*)FT_Get_Sfnt_Table(face, ft_sfnt_head);
1293 if (ttHeader) {
1294 upem = SkIntToScalar(ttHeader->Units_Per_EM);
agl@chromium.orgcc3096b2009-04-22 22:09:04 +00001295 }
1296 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001297
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +00001298 // use the os/2 table as a source of reasonable defaults.
1299 SkScalar x_height = 0.0f;
1300 SkScalar avgCharWidth = 0.0f;
bungeman@google.comcbe1b542013-12-16 17:02:39 +00001301 SkScalar cap_height = 0.0f;
Ben Wagner219f3622017-07-17 15:32:25 -04001302 SkScalar strikeoutThickness, strikeoutPosition;
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +00001303 TT_OS2* os2 = (TT_OS2*) FT_Get_Sfnt_Table(face, ft_sfnt_os2);
1304 if (os2) {
bungeman53790512016-07-21 13:32:09 -07001305 x_height = SkIntToScalar(os2->sxHeight) / upem * fScale.y();
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +00001306 avgCharWidth = SkIntToScalar(os2->xAvgCharWidth) / upem;
Ben Wagner219f3622017-07-17 15:32:25 -04001307 strikeoutThickness = SkIntToScalar(os2->yStrikeoutSize) / upem;
1308 strikeoutPosition = -SkIntToScalar(os2->yStrikeoutPosition) / upem;
1309 metrics->fFlags |= SkPaint::FontMetrics::kStrikeoutThicknessIsValid_Flag;
1310 metrics->fFlags |= SkPaint::FontMetrics::kStrikeoutPositionIsValid_Flag;
bungeman@google.comcbe1b542013-12-16 17:02:39 +00001311 if (os2->version != 0xFFFF && os2->version >= 2) {
bungeman53790512016-07-21 13:32:09 -07001312 cap_height = SkIntToScalar(os2->sCapHeight) / upem * fScale.y();
bungeman@google.comcbe1b542013-12-16 17:02:39 +00001313 }
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +00001314 }
1315
1316 // pull from format-specific metrics as needed
1317 SkScalar ascent, descent, leading, xmin, xmax, ymin, ymax;
commit-bot@chromium.org0bc406d2014-03-01 20:12:26 +00001318 SkScalar underlineThickness, underlinePosition;
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +00001319 if (face->face_flags & FT_FACE_FLAG_SCALABLE) { // scalable outline font
bungeman665b0382015-03-19 10:43:57 -07001320 // FreeType will always use HHEA metrics if they're not zero.
1321 // It completely ignores the OS/2 fsSelection::UseTypoMetrics bit.
1322 // It also ignores the VDMX tables, which are also of interest here
1323 // (and override everything else when they apply).
1324 static const int kUseTypoMetricsMask = (1 << 7);
1325 if (os2 && os2->version != 0xFFFF && (os2->fsSelection & kUseTypoMetricsMask)) {
1326 ascent = -SkIntToScalar(os2->sTypoAscender) / upem;
1327 descent = -SkIntToScalar(os2->sTypoDescender) / upem;
1328 leading = SkIntToScalar(os2->sTypoLineGap) / upem;
1329 } else {
1330 ascent = -SkIntToScalar(face->ascender) / upem;
1331 descent = -SkIntToScalar(face->descender) / upem;
1332 leading = SkIntToScalar(face->height + (face->descender - face->ascender)) / upem;
1333 }
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +00001334 xmin = SkIntToScalar(face->bbox.xMin) / upem;
1335 xmax = SkIntToScalar(face->bbox.xMax) / upem;
1336 ymin = -SkIntToScalar(face->bbox.yMin) / upem;
1337 ymax = -SkIntToScalar(face->bbox.yMax) / upem;
commit-bot@chromium.org0bc406d2014-03-01 20:12:26 +00001338 underlineThickness = SkIntToScalar(face->underline_thickness) / upem;
commit-bot@chromium.orgd3031aa2014-05-14 14:54:51 +00001339 underlinePosition = -SkIntToScalar(face->underline_position +
1340 face->underline_thickness / 2) / upem;
commit-bot@chromium.org0bc406d2014-03-01 20:12:26 +00001341
Ben Wagner3318da52017-03-23 14:01:22 -04001342 metrics->fFlags |= SkPaint::FontMetrics::kUnderlineThicknessIsValid_Flag;
bungeman41078062014-07-07 08:16:37 -07001343 metrics->fFlags |= SkPaint::FontMetrics::kUnderlinePositionIsValid_Flag;
1344
bungeman@google.comcbe1b542013-12-16 17:02:39 +00001345 // we may be able to synthesize x_height and cap_height from outline
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +00001346 if (!x_height) {
bungeman@google.comcbe1b542013-12-16 17:02:39 +00001347 FT_BBox bbox;
1348 if (getCBoxForLetter('x', &bbox)) {
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +00001349 x_height = SkIntToScalar(bbox.yMax) / 64.0f;
1350 }
1351 }
bungeman@google.comcbe1b542013-12-16 17:02:39 +00001352 if (!cap_height) {
1353 FT_BBox bbox;
1354 if (getCBoxForLetter('H', &bbox)) {
1355 cap_height = SkIntToScalar(bbox.yMax) / 64.0f;
1356 }
1357 }
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +00001358 } else if (fStrikeIndex != -1) { // bitmap strike metrics
1359 SkScalar xppem = SkIntToScalar(face->size->metrics.x_ppem);
1360 SkScalar yppem = SkIntToScalar(face->size->metrics.y_ppem);
1361 ascent = -SkIntToScalar(face->size->metrics.ascender) / (yppem * 64.0f);
1362 descent = -SkIntToScalar(face->size->metrics.descender) / (yppem * 64.0f);
bungeman53790512016-07-21 13:32:09 -07001363 leading = (SkIntToScalar(face->size->metrics.height) / (yppem * 64.0f)) + ascent - descent;
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +00001364 xmin = 0.0f;
1365 xmax = SkIntToScalar(face->available_sizes[fStrikeIndex].width) / xppem;
1366 ymin = descent + leading;
1367 ymax = ascent - descent;
commit-bot@chromium.org0bc406d2014-03-01 20:12:26 +00001368 underlineThickness = 0;
1369 underlinePosition = 0;
1370
Ben Wagner3318da52017-03-23 14:01:22 -04001371 metrics->fFlags &= ~SkPaint::FontMetrics::kUnderlineThicknessIsValid_Flag;
bungeman41078062014-07-07 08:16:37 -07001372 metrics->fFlags &= ~SkPaint::FontMetrics::kUnderlinePositionIsValid_Flag;
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +00001373 } else {
caryclarkfe7ada72016-03-21 06:55:52 -07001374 sk_bzero(metrics, sizeof(*metrics));
1375 return;
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +00001376 }
1377
1378 // synthesize elements that were not provided by the os/2 table or format-specific metrics
1379 if (!x_height) {
bungeman53790512016-07-21 13:32:09 -07001380 x_height = -ascent * fScale.y();
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +00001381 }
1382 if (!avgCharWidth) {
1383 avgCharWidth = xmax - xmin;
1384 }
bungeman@google.comcbe1b542013-12-16 17:02:39 +00001385 if (!cap_height) {
bungeman53790512016-07-21 13:32:09 -07001386 cap_height = -ascent * fScale.y();
bungeman@google.comcbe1b542013-12-16 17:02:39 +00001387 }
bungeman@google.comc9a8a7e2013-12-10 18:09:36 +00001388
1389 // disallow negative linespacing
1390 if (leading < 0.0f) {
1391 leading = 0.0f;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001392 }
1393
bungeman53790512016-07-21 13:32:09 -07001394 metrics->fTop = ymax * fScale.y();
1395 metrics->fAscent = ascent * fScale.y();
1396 metrics->fDescent = descent * fScale.y();
1397 metrics->fBottom = ymin * fScale.y();
1398 metrics->fLeading = leading * fScale.y();
1399 metrics->fAvgCharWidth = avgCharWidth * fScale.y();
1400 metrics->fXMin = xmin * fScale.y();
1401 metrics->fXMax = xmax * fScale.y();
Ben Wagner219f3622017-07-17 15:32:25 -04001402 metrics->fMaxCharWidth = metrics->fXMax - metrics->fXMin;
bungeman7316b102014-10-29 12:46:52 -07001403 metrics->fXHeight = x_height;
1404 metrics->fCapHeight = cap_height;
bungeman53790512016-07-21 13:32:09 -07001405 metrics->fUnderlineThickness = underlineThickness * fScale.y();
1406 metrics->fUnderlinePosition = underlinePosition * fScale.y();
Ben Wagner219f3622017-07-17 15:32:25 -04001407 metrics->fStrikeoutThickness = strikeoutThickness * fScale.y();
1408 metrics->fStrikeoutPosition = strikeoutPosition * fScale.y();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001409}
1410
djsollenfcfea992015-01-09 08:18:13 -08001411///////////////////////////////////////////////////////////////////////////////
1412
1413// hand-tuned value to reduce outline embolden strength
1414#ifndef SK_OUTLINE_EMBOLDEN_DIVISOR
1415 #ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
1416 #define SK_OUTLINE_EMBOLDEN_DIVISOR 34
1417 #else
1418 #define SK_OUTLINE_EMBOLDEN_DIVISOR 24
1419 #endif
1420#endif
1421
1422///////////////////////////////////////////////////////////////////////////////
1423
Ben Wagnerc3aef182017-06-26 12:47:33 -04001424void SkScalerContext_FreeType::emboldenIfNeeded(FT_Face face, FT_GlyphSlot glyph, SkGlyphID gid) {
commit-bot@chromium.org921d2b32014-04-01 19:03:07 +00001425 // check to see if the embolden bit is set
1426 if (0 == (fRec.fFlags & SkScalerContext::kEmbolden_Flag)) {
1427 return;
1428 }
1429
commit-bot@chromium.org921d2b32014-04-01 19:03:07 +00001430 switch (glyph->format) {
1431 case FT_GLYPH_FORMAT_OUTLINE:
1432 FT_Pos strength;
djsollenfcfea992015-01-09 08:18:13 -08001433 strength = FT_MulFix(face->units_per_EM, face->size->metrics.y_scale)
1434 / SK_OUTLINE_EMBOLDEN_DIVISOR;
commit-bot@chromium.org921d2b32014-04-01 19:03:07 +00001435 FT_Outline_Embolden(&glyph->outline, strength);
1436 break;
1437 case FT_GLYPH_FORMAT_BITMAP:
Ben Wagnerc3aef182017-06-26 12:47:33 -04001438 if (!fFace->glyph->bitmap.buffer) {
1439 FT_Load_Glyph(fFace, gid, fLoadGlyphFlags);
1440 }
commit-bot@chromium.org921d2b32014-04-01 19:03:07 +00001441 FT_GlyphSlot_Own_Bitmap(glyph);
1442 FT_Bitmap_Embolden(glyph->library, &glyph->bitmap, kBitmapEmboldenStrength, 0);
1443 break;
1444 default:
1445 SkDEBUGFAIL("unknown glyph format");
commit-bot@chromium.org6fa81d72013-12-26 15:50:29 +00001446 }
1447}
1448
reed@google.comb4162b12013-07-02 16:32:29 +00001449///////////////////////////////////////////////////////////////////////////////
1450
1451#include "SkUtils.h"
1452
1453static SkUnichar next_utf8(const void** chars) {
1454 return SkUTF8_NextUnichar((const char**)chars);
1455}
1456
1457static SkUnichar next_utf16(const void** chars) {
1458 return SkUTF16_NextUnichar((const uint16_t**)chars);
1459}
1460
1461static SkUnichar next_utf32(const void** chars) {
1462 const SkUnichar** uniChars = (const SkUnichar**)chars;
1463 SkUnichar uni = **uniChars;
1464 *uniChars += 1;
1465 return uni;
1466}
1467
1468typedef SkUnichar (*EncodingProc)(const void**);
1469
1470static EncodingProc find_encoding_proc(SkTypeface::Encoding enc) {
1471 static const EncodingProc gProcs[] = {
1472 next_utf8, next_utf16, next_utf32
1473 };
1474 SkASSERT((size_t)enc < SK_ARRAY_COUNT(gProcs));
1475 return gProcs[enc];
1476}
1477
1478int SkTypeface_FreeType::onCharsToGlyphs(const void* chars, Encoding encoding,
bungeman726cf902015-06-05 13:38:12 -07001479 uint16_t glyphs[], int glyphCount) const
1480{
reed@google.comb4162b12013-07-02 16:32:29 +00001481 AutoFTAccess fta(this);
1482 FT_Face face = fta.face();
1483 if (!face) {
1484 if (glyphs) {
1485 sk_bzero(glyphs, glyphCount * sizeof(glyphs[0]));
1486 }
1487 return 0;
1488 }
1489
1490 EncodingProc next_uni_proc = find_encoding_proc(encoding);
1491
halcanary96fcdcc2015-08-27 07:41:13 -07001492 if (nullptr == glyphs) {
reed@google.comb4162b12013-07-02 16:32:29 +00001493 for (int i = 0; i < glyphCount; ++i) {
1494 if (0 == FT_Get_Char_Index(face, next_uni_proc(&chars))) {
1495 return i;
1496 }
1497 }
1498 return glyphCount;
1499 } else {
1500 int first = glyphCount;
1501 for (int i = 0; i < glyphCount; ++i) {
1502 unsigned id = FT_Get_Char_Index(face, next_uni_proc(&chars));
1503 glyphs[i] = SkToU16(id);
1504 if (0 == id && i < first) {
1505 first = i;
1506 }
1507 }
1508 return first;
1509 }
1510}
1511
1512int SkTypeface_FreeType::onCountGlyphs() const {
bungeman572f8792016-04-29 15:05:02 -07001513 AutoFTAccess fta(this);
1514 FT_Face face = fta.face();
1515 return face ? face->num_glyphs : 0;
reed@google.comb4162b12013-07-02 16:32:29 +00001516}
1517
bungeman@google.com839702b2013-08-07 17:09:22 +00001518SkTypeface::LocalizedStrings* SkTypeface_FreeType::onCreateFamilyNameIterator() const {
bungeman@google.coma9802692013-08-07 02:45:25 +00001519 SkTypeface::LocalizedStrings* nameIter =
1520 SkOTUtils::LocalizedStrings_NameTable::CreateForFamilyNames(*this);
halcanary96fcdcc2015-08-27 07:41:13 -07001521 if (nullptr == nameIter) {
bungeman@google.coma9802692013-08-07 02:45:25 +00001522 SkString familyName;
1523 this->getFamilyName(&familyName);
1524 SkString language("und"); //undetermined
1525 nameIter = new SkOTUtils::LocalizedStrings_SingleName(familyName, language);
1526 }
1527 return nameIter;
1528}
1529
Ben Wagnerfc497342017-02-24 11:15:26 -05001530int SkTypeface_FreeType::onGetVariationDesignPosition(
1531 SkFontArguments::VariationPosition::Coordinate coordinates[], int coordinateCount) const
1532{
1533 AutoFTAccess fta(this);
1534 FT_Face face = fta.face();
1535
Ben Wagnere7e54992017-03-02 11:48:38 -05001536 if (!face || !(face->face_flags & FT_FACE_FLAG_MULTIPLE_MASTERS)) {
Ben Wagnerfc497342017-02-24 11:15:26 -05001537 return 0;
1538 }
1539
1540 FT_MM_Var* variations = nullptr;
1541 if (FT_Get_MM_Var(face, &variations)) {
1542 return 0;
1543 }
1544 SkAutoFree autoFreeVariations(variations);
1545
1546 if (!coordinates || coordinateCount < SkToInt(variations->num_axis)) {
1547 return variations->num_axis;
1548 }
1549
1550 SkAutoSTMalloc<4, FT_Fixed> coords(variations->num_axis);
1551 // FT_Get_{MM,Var}_{Blend,Design}_Coordinates were added in FreeType 2.7.1.
1552 if (gFTLibrary->fGetVarDesignCoordinates &&
1553 !gFTLibrary->fGetVarDesignCoordinates(face, variations->num_axis, coords.get()))
1554 {
1555 for (FT_UInt i = 0; i < variations->num_axis; ++i) {
1556 coordinates[i].axis = variations->axis[i].tag;
1557 coordinates[i].value = SkFixedToScalar(coords[i]);
1558 }
1559 } else if (static_cast<FT_UInt>(fta.getAxesCount()) == variations->num_axis) {
1560 for (FT_UInt i = 0; i < variations->num_axis; ++i) {
1561 coordinates[i].axis = variations->axis[i].tag;
1562 coordinates[i].value = SkFixedToScalar(fta.getAxes()[i]);
1563 }
1564 } else if (fta.isNamedVariationSpecified()) {
1565 // The font has axes, they cannot be retrieved, and some named axis was specified.
1566 return -1;
1567 } else {
1568 // The font has axes, they cannot be retrieved, but no named instance was specified.
1569 return 0;
1570 }
1571
1572 return variations->num_axis;
1573}
1574
bungeman@google.comddc218e2013-08-01 22:29:43 +00001575int SkTypeface_FreeType::onGetTableTags(SkFontTableTag tags[]) const {
1576 AutoFTAccess fta(this);
1577 FT_Face face = fta.face();
1578
1579 FT_ULong tableCount = 0;
1580 FT_Error error;
1581
halcanary96fcdcc2015-08-27 07:41:13 -07001582 // When 'tag' is nullptr, returns number of tables in 'length'.
1583 error = FT_Sfnt_Table_Info(face, 0, nullptr, &tableCount);
bungeman@google.comddc218e2013-08-01 22:29:43 +00001584 if (error) {
1585 return 0;
1586 }
1587
1588 if (tags) {
1589 for (FT_ULong tableIndex = 0; tableIndex < tableCount; ++tableIndex) {
1590 FT_ULong tableTag;
1591 FT_ULong tablelength;
1592 error = FT_Sfnt_Table_Info(face, tableIndex, &tableTag, &tablelength);
1593 if (error) {
1594 return 0;
1595 }
1596 tags[tableIndex] = static_cast<SkFontTableTag>(tableTag);
1597 }
1598 }
1599 return tableCount;
1600}
1601
1602size_t SkTypeface_FreeType::onGetTableData(SkFontTableTag tag, size_t offset,
1603 size_t length, void* data) const
1604{
1605 AutoFTAccess fta(this);
1606 FT_Face face = fta.face();
1607
1608 FT_ULong tableLength = 0;
1609 FT_Error error;
1610
1611 // When 'length' is 0 it is overwritten with the full table length; 'offset' is ignored.
halcanary96fcdcc2015-08-27 07:41:13 -07001612 error = FT_Load_Sfnt_Table(face, tag, 0, nullptr, &tableLength);
bungeman@google.comddc218e2013-08-01 22:29:43 +00001613 if (error) {
1614 return 0;
1615 }
1616
1617 if (offset > tableLength) {
1618 return 0;
1619 }
bungeman@google.com5ecd4fa2013-08-01 22:48:21 +00001620 FT_ULong size = SkTMin((FT_ULong)length, tableLength - (FT_ULong)offset);
bsalomon49f085d2014-09-05 13:34:00 -07001621 if (data) {
bungeman@google.comddc218e2013-08-01 22:29:43 +00001622 error = FT_Load_Sfnt_Table(face, tag, offset, reinterpret_cast<FT_Byte*>(data), &size);
1623 if (error) {
1624 return 0;
1625 }
1626 }
1627
1628 return size;
1629}
1630
reed@google.comb4162b12013-07-02 16:32:29 +00001631///////////////////////////////////////////////////////////////////////////////
1632///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +00001633
halcanary96fcdcc2015-08-27 07:41:13 -07001634SkTypeface_FreeType::Scanner::Scanner() : fLibrary(nullptr) {
bungeman9dc24682014-12-01 14:01:32 -08001635 if (FT_New_Library(&gFTMemory, &fLibrary)) {
1636 return;
bungeman14df8332014-10-28 15:07:23 -07001637 }
bungeman9dc24682014-12-01 14:01:32 -08001638 FT_Add_Default_Modules(fLibrary);
bungeman14df8332014-10-28 15:07:23 -07001639}
1640SkTypeface_FreeType::Scanner::~Scanner() {
bungeman9dc24682014-12-01 14:01:32 -08001641 if (fLibrary) {
1642 FT_Done_Library(fLibrary);
1643 }
bungeman14df8332014-10-28 15:07:23 -07001644}
1645
bungemanf93d7112016-09-16 06:24:20 -07001646FT_Face SkTypeface_FreeType::Scanner::openFace(SkStreamAsset* stream, int ttcIndex,
bungeman14df8332014-10-28 15:07:23 -07001647 FT_Stream ftStream) const
bungeman32501a12014-10-28 12:03:55 -07001648{
halcanary96fcdcc2015-08-27 07:41:13 -07001649 if (fLibrary == nullptr) {
1650 return nullptr;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001651 }
1652
bungeman14df8332014-10-28 15:07:23 -07001653 FT_Open_Args args;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001654 memset(&args, 0, sizeof(args));
1655
1656 const void* memoryBase = stream->getMemoryBase();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001657
bsalomon49f085d2014-09-05 13:34:00 -07001658 if (memoryBase) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001659 args.flags = FT_OPEN_MEMORY;
1660 args.memory_base = (const FT_Byte*)memoryBase;
1661 args.memory_size = stream->getLength();
1662 } else {
bungeman14df8332014-10-28 15:07:23 -07001663 memset(ftStream, 0, sizeof(*ftStream));
1664 ftStream->size = stream->getLength();
1665 ftStream->descriptor.pointer = stream;
bungeman9dc24682014-12-01 14:01:32 -08001666 ftStream->read = sk_ft_stream_io;
1667 ftStream->close = sk_ft_stream_close;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001668
1669 args.flags = FT_OPEN_STREAM;
bungeman14df8332014-10-28 15:07:23 -07001670 args.stream = ftStream;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001671 }
1672
1673 FT_Face face;
bungeman14df8332014-10-28 15:07:23 -07001674 if (FT_Open_Face(fLibrary, &args, ttcIndex, &face)) {
halcanary96fcdcc2015-08-27 07:41:13 -07001675 return nullptr;
bungeman14df8332014-10-28 15:07:23 -07001676 }
1677 return face;
1678}
1679
bungemanf93d7112016-09-16 06:24:20 -07001680bool SkTypeface_FreeType::Scanner::recognizedFont(SkStreamAsset* stream, int* numFaces) const {
bungeman14df8332014-10-28 15:07:23 -07001681 SkAutoMutexAcquire libraryLock(fLibraryMutex);
1682
1683 FT_StreamRec streamRec;
1684 FT_Face face = this->openFace(stream, -1, &streamRec);
halcanary96fcdcc2015-08-27 07:41:13 -07001685 if (nullptr == face) {
bungeman14df8332014-10-28 15:07:23 -07001686 return false;
1687 }
1688
1689 *numFaces = face->num_faces;
1690
1691 FT_Done_Face(face);
1692 return true;
1693}
1694
1695#include "SkTSearch.h"
1696bool SkTypeface_FreeType::Scanner::scanFont(
bungemanf93d7112016-09-16 06:24:20 -07001697 SkStreamAsset* stream, int ttcIndex,
bungeman41868fe2015-05-20 09:21:04 -07001698 SkString* name, SkFontStyle* style, bool* isFixedPitch, AxisDefinitions* axes) const
bungeman14df8332014-10-28 15:07:23 -07001699{
1700 SkAutoMutexAcquire libraryLock(fLibraryMutex);
1701
1702 FT_StreamRec streamRec;
1703 FT_Face face = this->openFace(stream, ttcIndex, &streamRec);
halcanary96fcdcc2015-08-27 07:41:13 -07001704 if (nullptr == face) {
djsollen@google.com4dc686d2012-02-15 21:03:45 +00001705 return false;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001706 }
1707
bungemana4c4a2d2014-10-20 13:33:19 -07001708 int weight = SkFontStyle::kNormal_Weight;
1709 int width = SkFontStyle::kNormal_Width;
1710 SkFontStyle::Slant slant = SkFontStyle::kUpright_Slant;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001711 if (face->style_flags & FT_STYLE_FLAG_BOLD) {
bungemana4c4a2d2014-10-20 13:33:19 -07001712 weight = SkFontStyle::kBold_Weight;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001713 }
1714 if (face->style_flags & FT_STYLE_FLAG_ITALIC) {
bungemana4c4a2d2014-10-20 13:33:19 -07001715 slant = SkFontStyle::kItalic_Slant;
1716 }
1717
1718 PS_FontInfoRec psFontInfo;
1719 TT_OS2* os2 = static_cast<TT_OS2*>(FT_Get_Sfnt_Table(face, ft_sfnt_os2));
1720 if (os2 && os2->version != 0xffff) {
1721 weight = os2->usWeightClass;
1722 width = os2->usWidthClass;
bungemanb4bb7d82016-04-27 10:21:04 -07001723
1724 // OS/2::fsSelection bit 9 indicates oblique.
1725 if (SkToBool(os2->fsSelection & (1u << 9))) {
1726 slant = SkFontStyle::kOblique_Slant;
1727 }
bungemana4c4a2d2014-10-20 13:33:19 -07001728 } else if (0 == FT_Get_PS_Font_Info(face, &psFontInfo) && psFontInfo.weight) {
1729 static const struct {
1730 char const * const name;
1731 int const weight;
1732 } commonWeights [] = {
1733 // There are probably more common names, but these are known to exist.
bungemand803cda2015-04-16 14:22:46 -07001734 { "all", SkFontStyle::kNormal_Weight }, // Multiple Masters usually default to normal.
bungemana4c4a2d2014-10-20 13:33:19 -07001735 { "black", SkFontStyle::kBlack_Weight },
1736 { "bold", SkFontStyle::kBold_Weight },
1737 { "book", (SkFontStyle::kNormal_Weight + SkFontStyle::kLight_Weight)/2 },
1738 { "demi", SkFontStyle::kSemiBold_Weight },
1739 { "demibold", SkFontStyle::kSemiBold_Weight },
bungeman14df8332014-10-28 15:07:23 -07001740 { "extra", SkFontStyle::kExtraBold_Weight },
bungemana4c4a2d2014-10-20 13:33:19 -07001741 { "extrabold", SkFontStyle::kExtraBold_Weight },
1742 { "extralight", SkFontStyle::kExtraLight_Weight },
bungeman14df8332014-10-28 15:07:23 -07001743 { "hairline", SkFontStyle::kThin_Weight },
bungemana4c4a2d2014-10-20 13:33:19 -07001744 { "heavy", SkFontStyle::kBlack_Weight },
1745 { "light", SkFontStyle::kLight_Weight },
1746 { "medium", SkFontStyle::kMedium_Weight },
1747 { "normal", SkFontStyle::kNormal_Weight },
bungeman14df8332014-10-28 15:07:23 -07001748 { "plain", SkFontStyle::kNormal_Weight },
bungemana4c4a2d2014-10-20 13:33:19 -07001749 { "regular", SkFontStyle::kNormal_Weight },
bungeman14df8332014-10-28 15:07:23 -07001750 { "roman", SkFontStyle::kNormal_Weight },
bungemana4c4a2d2014-10-20 13:33:19 -07001751 { "semibold", SkFontStyle::kSemiBold_Weight },
bungeman14df8332014-10-28 15:07:23 -07001752 { "standard", SkFontStyle::kNormal_Weight },
bungemana4c4a2d2014-10-20 13:33:19 -07001753 { "thin", SkFontStyle::kThin_Weight },
1754 { "ultra", SkFontStyle::kExtraBold_Weight },
bungeman6e45bda2016-07-25 15:11:49 -07001755 { "ultrablack", SkFontStyle::kExtraBlack_Weight },
bungemana4c4a2d2014-10-20 13:33:19 -07001756 { "ultrabold", SkFontStyle::kExtraBold_Weight },
bungeman6e45bda2016-07-25 15:11:49 -07001757 { "ultraheavy", SkFontStyle::kExtraBlack_Weight },
bungemana4c4a2d2014-10-20 13:33:19 -07001758 { "ultralight", SkFontStyle::kExtraLight_Weight },
1759 };
1760 int const index = SkStrLCSearch(&commonWeights[0].name, SK_ARRAY_COUNT(commonWeights),
bungemand2ae7282014-10-22 08:25:44 -07001761 psFontInfo.weight, sizeof(commonWeights[0]));
bungemana4c4a2d2014-10-20 13:33:19 -07001762 if (index >= 0) {
1763 weight = commonWeights[index].weight;
1764 } else {
bungeman14df8332014-10-28 15:07:23 -07001765 SkDEBUGF(("Do not know weight for: %s (%s) \n", face->family_name, psFontInfo.weight));
bungemana4c4a2d2014-10-20 13:33:19 -07001766 }
djsollen@google.com4dc686d2012-02-15 21:03:45 +00001767 }
1768
1769 if (name) {
1770 name->set(face->family_name);
1771 }
1772 if (style) {
bungemana4c4a2d2014-10-20 13:33:19 -07001773 *style = SkFontStyle(weight, width, slant);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001774 }
bungeman@google.comfe747652013-03-25 19:36:11 +00001775 if (isFixedPitch) {
1776 *isFixedPitch = FT_IS_FIXED_WIDTH(face);
reed@google.com5b31b0f2011-02-23 14:41:42 +00001777 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001778
bungeman41868fe2015-05-20 09:21:04 -07001779 if (axes && face->face_flags & FT_FACE_FLAG_MULTIPLE_MASTERS) {
halcanary96fcdcc2015-08-27 07:41:13 -07001780 FT_MM_Var* variations = nullptr;
bungeman41868fe2015-05-20 09:21:04 -07001781 FT_Error err = FT_Get_MM_Var(face, &variations);
1782 if (err) {
1783 SkDEBUGF(("INFO: font %s claims to have variations, but none found.\n",
1784 face->family_name));
1785 return false;
1786 }
1787 SkAutoFree autoFreeVariations(variations);
1788
1789 axes->reset(variations->num_axis);
1790 for (FT_UInt i = 0; i < variations->num_axis; ++i) {
1791 const FT_Var_Axis& ftAxis = variations->axis[i];
1792 (*axes)[i].fTag = ftAxis.tag;
1793 (*axes)[i].fMinimum = ftAxis.minimum;
1794 (*axes)[i].fDefault = ftAxis.def;
1795 (*axes)[i].fMaximum = ftAxis.maximum;
1796 }
1797 }
bungeman41868fe2015-05-20 09:21:04 -07001798
reed@android.com8a1c16f2008-12-17 15:59:43 +00001799 FT_Done_Face(face);
djsollen@google.com4dc686d2012-02-15 21:03:45 +00001800 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001801}
bungemanf6c71072016-01-21 14:17:47 -08001802
1803/*static*/ void SkTypeface_FreeType::Scanner::computeAxisValues(
1804 AxisDefinitions axisDefinitions,
Ben Wagnerfc497342017-02-24 11:15:26 -05001805 const SkFontArguments::VariationPosition position,
bungemanf6c71072016-01-21 14:17:47 -08001806 SkFixed* axisValues,
1807 const SkString& name)
1808{
1809 for (int i = 0; i < axisDefinitions.count(); ++i) {
1810 const Scanner::AxisDefinition& axisDefinition = axisDefinitions[i];
benjaminwagner64a3c952016-02-25 12:20:40 -08001811 const SkScalar axisMin = SkFixedToScalar(axisDefinition.fMinimum);
1812 const SkScalar axisMax = SkFixedToScalar(axisDefinition.fMaximum);
bungemanf6c71072016-01-21 14:17:47 -08001813 axisValues[i] = axisDefinition.fDefault;
bungeman9aec8942017-03-29 13:38:53 -04001814 // The position may be over specified. If there are multiple values for a given axis,
1815 // use the last one since that's what css-fonts-4 requires.
1816 for (int j = position.coordinateCount; j --> 0;) {
Ben Wagnerfc497342017-02-24 11:15:26 -05001817 const auto& coordinate = position.coordinates[j];
1818 if (axisDefinition.fTag == coordinate.axis) {
1819 const SkScalar axisValue = SkTPin(coordinate.value, axisMin, axisMax);
1820 if (coordinate.value != axisValue) {
bungemanf6c71072016-01-21 14:17:47 -08001821 SkDEBUGF(("Requested font axis value out of range: "
1822 "%s '%c%c%c%c' %f; pinned to %f.\n",
1823 name.c_str(),
1824 (axisDefinition.fTag >> 24) & 0xFF,
1825 (axisDefinition.fTag >> 16) & 0xFF,
1826 (axisDefinition.fTag >> 8) & 0xFF,
1827 (axisDefinition.fTag ) & 0xFF,
Ben Wagnerfc497342017-02-24 11:15:26 -05001828 SkScalarToDouble(coordinate.value),
benjaminwagner64a3c952016-02-25 12:20:40 -08001829 SkScalarToDouble(axisValue)));
bungemanf6c71072016-01-21 14:17:47 -08001830 }
benjaminwagner64a3c952016-02-25 12:20:40 -08001831 axisValues[i] = SkScalarToFixed(axisValue);
bungemanf6c71072016-01-21 14:17:47 -08001832 break;
1833 }
1834 }
1835 // TODO: warn on defaulted axis?
1836 }
1837
1838 SkDEBUGCODE(
1839 // Check for axis specified, but not matched in font.
Ben Wagnerfc497342017-02-24 11:15:26 -05001840 for (int i = 0; i < position.coordinateCount; ++i) {
1841 SkFourByteTag skTag = position.coordinates[i].axis;
bungemanf6c71072016-01-21 14:17:47 -08001842 bool found = false;
1843 for (int j = 0; j < axisDefinitions.count(); ++j) {
1844 if (skTag == axisDefinitions[j].fTag) {
1845 found = true;
1846 break;
1847 }
1848 }
1849 if (!found) {
1850 SkDEBUGF(("Requested font axis not found: %s '%c%c%c%c'\n",
1851 name.c_str(),
1852 (skTag >> 24) & 0xFF,
1853 (skTag >> 16) & 0xFF,
1854 (skTag >> 8) & 0xFF,
1855 (skTag) & 0xFF));
1856 }
1857 }
1858 )
1859}