epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "include/core/SkBitmap.h" |
| 9 | #include "include/core/SkCanvas.h" |
| 10 | #include "include/core/SkFontMetrics.h" |
| 11 | #include "include/core/SkPath.h" |
| 12 | #include "include/core/SkStream.h" |
| 13 | #include "include/core/SkString.h" |
| 14 | #include "include/private/SkColorData.h" |
| 15 | #include "include/private/SkMalloc.h" |
| 16 | #include "include/private/SkMutex.h" |
| 17 | #include "include/private/SkTemplates.h" |
| 18 | #include "include/private/SkTo.h" |
| 19 | #include "src/core/SkAdvancedTypefaceMetrics.h" |
| 20 | #include "src/core/SkDescriptor.h" |
| 21 | #include "src/core/SkFDot6.h" |
| 22 | #include "src/core/SkFontDescriptor.h" |
| 23 | #include "src/core/SkGlyph.h" |
| 24 | #include "src/core/SkMakeUnique.h" |
| 25 | #include "src/core/SkMask.h" |
| 26 | #include "src/core/SkMaskGamma.h" |
| 27 | #include "src/core/SkScalerContext.h" |
| 28 | #include "src/ports/SkFontHost_FreeType_common.h" |
| 29 | #include "src/sfnt/SkOTUtils.h" |
| 30 | #include "src/utils/SkCallableTraits.h" |
| 31 | #include "src/utils/SkMatrix22.h" |
Hal Canary | c640d0d | 2018-06-13 09:59:02 -0400 | [diff] [blame] | 32 | |
mtklein | 5f939ab | 2016-03-16 10:28:35 -0700 | [diff] [blame] | 33 | #include <memory> |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 34 | |
| 35 | #include <ft2build.h> |
bungeman | 5ec443c | 2014-11-21 13:18:34 -0800 | [diff] [blame] | 36 | #include FT_ADVANCES_H |
| 37 | #include FT_BITMAP_H |
Bruce Wang | f3ca1c6 | 2018-07-09 10:08:36 -0400 | [diff] [blame] | 38 | #ifdef FT_COLOR_H |
| 39 | # include FT_COLOR_H |
| 40 | #endif |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 41 | #include FT_FREETYPE_H |
bungeman | 5ec443c | 2014-11-21 13:18:34 -0800 | [diff] [blame] | 42 | #include FT_LCD_FILTER_H |
bungeman | 9dc2468 | 2014-12-01 14:01:32 -0800 | [diff] [blame] | 43 | #include FT_MODULE_H |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 44 | #include FT_MULTIPLE_MASTERS_H |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 45 | #include FT_OUTLINE_H |
| 46 | #include FT_SIZES_H |
bungeman | 9dc2468 | 2014-12-01 14:01:32 -0800 | [diff] [blame] | 47 | #include FT_SYSTEM_H |
agl@chromium.org | cc3096b | 2009-04-22 22:09:04 +0000 | [diff] [blame] | 48 | #include FT_TRUETYPE_TABLES_H |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 49 | #include FT_TYPE1_TABLES_H |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 50 | #include FT_XFREE86_H |
agl@chromium.org | cc3096b | 2009-04-22 22:09:04 +0000 | [diff] [blame] | 51 | |
Ben Wagner | fc49734 | 2017-02-24 11:15:26 -0500 | [diff] [blame] | 52 | // SK_FREETYPE_MINIMUM_RUNTIME_VERSION 0x<major><minor><patch><flags> |
| 53 | // Flag SK_FREETYPE_DLOPEN: also try dlopen to get newer features. |
| 54 | #define SK_FREETYPE_DLOPEN (0x1) |
| 55 | #ifndef SK_FREETYPE_MINIMUM_RUNTIME_VERSION |
Mike Klein | 6613cc5 | 2017-12-19 09:09:33 -0500 | [diff] [blame] | 56 | # if defined(SK_BUILD_FOR_ANDROID_FRAMEWORK) || defined (SK_BUILD_FOR_GOOGLE3) |
Ben Wagner | fc49734 | 2017-02-24 11:15:26 -0500 | [diff] [blame] | 57 | # define SK_FREETYPE_MINIMUM_RUNTIME_VERSION (((FREETYPE_MAJOR) << 24) | ((FREETYPE_MINOR) << 16) | ((FREETYPE_PATCH) << 8)) |
| 58 | # else |
| 59 | # define SK_FREETYPE_MINIMUM_RUNTIME_VERSION ((2 << 24) | (3 << 16) | (11 << 8) | (SK_FREETYPE_DLOPEN)) |
| 60 | # endif |
| 61 | #endif |
| 62 | #if SK_FREETYPE_MINIMUM_RUNTIME_VERSION & SK_FREETYPE_DLOPEN |
| 63 | # include <dlfcn.h> |
| 64 | #endif |
| 65 | |
bungeman@google.com | c9a8a7e | 2013-12-10 18:09:36 +0000 | [diff] [blame] | 66 | // FT_LOAD_COLOR and the corresponding FT_Pixel_Mode::FT_PIXEL_MODE_BGRA |
| 67 | // were introduced in FreeType 2.5.0. |
| 68 | // The following may be removed once FreeType 2.5.0 is required to build. |
| 69 | #ifndef FT_LOAD_COLOR |
| 70 | # define FT_LOAD_COLOR ( 1L << 20 ) |
| 71 | # define FT_PIXEL_MODE_BGRA 7 |
| 72 | #endif |
| 73 | |
Seigo Nonaka | 52ab2f5 | 2016-12-05 02:41:53 +0900 | [diff] [blame] | 74 | // FT_LOAD_BITMAP_METRICS_ONLY was introduced in FreeType 2.7.1 |
| 75 | // The following may be removed once FreeType 2.7.1 is required to build. |
| 76 | #ifndef FT_LOAD_BITMAP_METRICS_ONLY |
| 77 | # define FT_LOAD_BITMAP_METRICS_ONLY ( 1L << 22 ) |
| 78 | #endif |
| 79 | |
Ben Wagner | e346b1e | 2018-06-26 11:22:37 -0400 | [diff] [blame] | 80 | // FT_VAR_AXIS_FLAG_HIDDEN was introduced in FreeType 2.8.1 |
| 81 | // The variation axis should not be exposed to user interfaces. |
| 82 | #ifndef FT_VAR_AXIS_FLAG_HIDDEN |
| 83 | # define FT_VAR_AXIS_FLAG_HIDDEN 1 |
| 84 | #endif |
| 85 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 86 | //#define ENABLE_GLYPH_SPEW // for tracing calls |
| 87 | //#define DUMP_STRIKE_CREATION |
bungeman | 5ec443c | 2014-11-21 13:18:34 -0800 | [diff] [blame] | 88 | //#define SK_FONTHOST_FREETYPE_RUNTIME_VERSION |
reed@google.com | 1ac8350 | 2012-02-28 17:06:02 +0000 | [diff] [blame] | 89 | //#define SK_GAMMA_APPLY_TO_A8 |
reed@google.com | 1ac8350 | 2012-02-28 17:06:02 +0000 | [diff] [blame] | 90 | |
Mike Klein | 3e0141a | 2019-03-26 10:41:18 -0500 | [diff] [blame] | 91 | #if 1 |
| 92 | #define LOG_INFO(...) |
| 93 | #else |
| 94 | #define LOG_INFO SkDEBUGF |
| 95 | #endif |
| 96 | |
Ben Wagner | 3e45a2f | 2017-11-01 11:47:39 -0400 | [diff] [blame] | 97 | static bool isLCD(const SkScalerContextRec& rec) { |
bungeman | 9dc2468 | 2014-12-01 14:01:32 -0800 | [diff] [blame] | 98 | return SkMask::kLCD16_Format == rec.fMaskFormat; |
reed@google.com | effc501 | 2011-06-27 16:44:46 +0000 | [diff] [blame] | 99 | } |
| 100 | |
Ben Wagner | 082a7a7 | 2018-06-08 14:15:47 -0400 | [diff] [blame] | 101 | static SkScalar SkFT_FixedToScalar(FT_Fixed x) { |
| 102 | return SkFixedToScalar(x); |
| 103 | } |
| 104 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 105 | ////////////////////////////////////////////////////////////////////////// |
| 106 | |
Ben Wagner | 230a71b | 2018-10-01 14:54:01 -0400 | [diff] [blame] | 107 | using FT_Alloc_size_t = SkCallableTraits<FT_Alloc_Func>::argument<1>::type; |
| 108 | static_assert(std::is_same<FT_Alloc_size_t, long >::value || |
| 109 | std::is_same<FT_Alloc_size_t, size_t>::value,""); |
| 110 | |
bungeman | 9dc2468 | 2014-12-01 14:01:32 -0800 | [diff] [blame] | 111 | extern "C" { |
Ben Wagner | 230a71b | 2018-10-01 14:54:01 -0400 | [diff] [blame] | 112 | static void* sk_ft_alloc(FT_Memory, FT_Alloc_size_t size) { |
bungeman | 9dc2468 | 2014-12-01 14:01:32 -0800 | [diff] [blame] | 113 | return sk_malloc_throw(size); |
| 114 | } |
| 115 | static void sk_ft_free(FT_Memory, void* block) { |
| 116 | sk_free(block); |
| 117 | } |
Ben Wagner | 230a71b | 2018-10-01 14:54:01 -0400 | [diff] [blame] | 118 | static void* sk_ft_realloc(FT_Memory, FT_Alloc_size_t cur_size, |
| 119 | FT_Alloc_size_t new_size, void* block) { |
bungeman | 9dc2468 | 2014-12-01 14:01:32 -0800 | [diff] [blame] | 120 | return sk_realloc_throw(block, new_size); |
| 121 | } |
| 122 | }; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 123 | FT_MemoryRec_ gFTMemory = { nullptr, sk_ft_alloc, sk_ft_free, sk_ft_realloc }; |
bungeman | 9dc2468 | 2014-12-01 14:01:32 -0800 | [diff] [blame] | 124 | |
| 125 | class FreeTypeLibrary : SkNoncopyable { |
| 126 | public: |
Ben Wagner | fc49734 | 2017-02-24 11:15:26 -0500 | [diff] [blame] | 127 | FreeTypeLibrary() |
| 128 | : fGetVarDesignCoordinates(nullptr) |
Ben Wagner | e346b1e | 2018-06-26 11:22:37 -0400 | [diff] [blame] | 129 | , fGetVarAxisFlags(nullptr) |
Ben Wagner | fc49734 | 2017-02-24 11:15:26 -0500 | [diff] [blame] | 130 | , fLibrary(nullptr) |
| 131 | , fIsLCDSupported(false) |
| 132 | , fLCDExtra(0) |
| 133 | { |
bungeman | 9dc2468 | 2014-12-01 14:01:32 -0800 | [diff] [blame] | 134 | if (FT_New_Library(&gFTMemory, &fLibrary)) { |
| 135 | return; |
| 136 | } |
| 137 | FT_Add_Default_Modules(fLibrary); |
| 138 | |
Ben Wagner | 2a098d0 | 2017-03-01 13:00:53 -0500 | [diff] [blame] | 139 | // When using dlsym |
| 140 | // *(void**)(&procPtr) = dlsym(self, "proc"); |
| 141 | // is non-standard, but safe for POSIX. Cannot write |
| 142 | // *reinterpret_cast<void**>(&procPtr) = dlsym(self, "proc"); |
| 143 | // because clang has not implemented DR573. See http://clang.llvm.org/cxx_dr_status.html . |
| 144 | |
| 145 | FT_Int major, minor, patch; |
| 146 | FT_Library_Version(fLibrary, &major, &minor, &patch); |
| 147 | |
Ben Wagner | fc49734 | 2017-02-24 11:15:26 -0500 | [diff] [blame] | 148 | #if SK_FREETYPE_MINIMUM_RUNTIME_VERSION >= 0x02070100 |
| 149 | fGetVarDesignCoordinates = FT_Get_Var_Design_Coordinates; |
| 150 | #elif SK_FREETYPE_MINIMUM_RUNTIME_VERSION & SK_FREETYPE_DLOPEN |
Ben Wagner | 2a098d0 | 2017-03-01 13:00:53 -0500 | [diff] [blame] | 151 | if (major > 2 || ((major == 2 && minor > 7) || (major == 2 && minor == 7 && patch >= 0))) { |
Ben Wagner | fc49734 | 2017-02-24 11:15:26 -0500 | [diff] [blame] | 152 | //The FreeType library is already loaded, so symbols are available in process. |
| 153 | void* self = dlopen(nullptr, RTLD_LAZY); |
| 154 | if (self) { |
Ben Wagner | fc49734 | 2017-02-24 11:15:26 -0500 | [diff] [blame] | 155 | *(void**)(&fGetVarDesignCoordinates) = dlsym(self, "FT_Get_Var_Design_Coordinates"); |
| 156 | dlclose(self); |
| 157 | } |
| 158 | } |
| 159 | #endif |
| 160 | |
Ben Wagner | 2a098d0 | 2017-03-01 13:00:53 -0500 | [diff] [blame] | 161 | #if SK_FREETYPE_MINIMUM_RUNTIME_VERSION >= 0x02070200 |
| 162 | FT_Set_Default_Properties(fLibrary); |
| 163 | #elif SK_FREETYPE_MINIMUM_RUNTIME_VERSION & SK_FREETYPE_DLOPEN |
| 164 | if (major > 2 || ((major == 2 && minor > 7) || (major == 2 && minor == 7 && patch >= 1))) { |
bungeman | 9dc2468 | 2014-12-01 14:01:32 -0800 | [diff] [blame] | 165 | //The FreeType library is already loaded, so symbols are available in process. |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 166 | void* self = dlopen(nullptr, RTLD_LAZY); |
bungeman | 9dc2468 | 2014-12-01 14:01:32 -0800 | [diff] [blame] | 167 | if (self) { |
Ben Wagner | 2a098d0 | 2017-03-01 13:00:53 -0500 | [diff] [blame] | 168 | FT_Set_Default_PropertiesProc setDefaultProperties; |
| 169 | *(void**)(&setDefaultProperties) = dlsym(self, "FT_Set_Default_Properties"); |
bungeman | 9dc2468 | 2014-12-01 14:01:32 -0800 | [diff] [blame] | 170 | dlclose(self); |
| 171 | |
Ben Wagner | 2a098d0 | 2017-03-01 13:00:53 -0500 | [diff] [blame] | 172 | if (setDefaultProperties) { |
| 173 | setDefaultProperties(fLibrary); |
bungeman | 9dc2468 | 2014-12-01 14:01:32 -0800 | [diff] [blame] | 174 | } |
| 175 | } |
Ben Wagner | 2a098d0 | 2017-03-01 13:00:53 -0500 | [diff] [blame] | 176 | } |
bungeman | 9dc2468 | 2014-12-01 14:01:32 -0800 | [diff] [blame] | 177 | #endif |
Ben Wagner | 2a098d0 | 2017-03-01 13:00:53 -0500 | [diff] [blame] | 178 | |
Ben Wagner | e346b1e | 2018-06-26 11:22:37 -0400 | [diff] [blame] | 179 | #if SK_FREETYPE_MINIMUM_RUNTIME_VERSION >= 0x02080100 |
| 180 | fGetVarAxisFlags = FT_Get_Var_Axis_Flags; |
| 181 | #elif SK_FREETYPE_MINIMUM_RUNTIME_VERSION & SK_FREETYPE_DLOPEN |
| 182 | if (major > 2 || ((major == 2 && minor > 7) || (major == 2 && minor == 7 && patch >= 0))) { |
| 183 | //The FreeType library is already loaded, so symbols are available in process. |
| 184 | void* self = dlopen(nullptr, RTLD_LAZY); |
| 185 | if (self) { |
| 186 | *(void**)(&fGetVarAxisFlags) = dlsym(self, "FT_Get_Var_Axis_Flags"); |
| 187 | dlclose(self); |
| 188 | } |
| 189 | } |
| 190 | #endif |
| 191 | |
Ben Wagner | 2a098d0 | 2017-03-01 13:00:53 -0500 | [diff] [blame] | 192 | // Setup LCD filtering. This reduces color fringes for LCD smoothed glyphs. |
| 193 | // The default has changed over time, so this doesn't mean the same thing to all users. |
| 194 | if (FT_Library_SetLcdFilter(fLibrary, FT_LCD_FILTER_DEFAULT) == 0) { |
| 195 | fIsLCDSupported = true; |
| 196 | fLCDExtra = 2; //Using a filter adds one full pixel to each side. |
bungeman | 9dc2468 | 2014-12-01 14:01:32 -0800 | [diff] [blame] | 197 | } |
| 198 | } |
| 199 | ~FreeTypeLibrary() { |
| 200 | if (fLibrary) { |
| 201 | FT_Done_Library(fLibrary); |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | FT_Library library() { return fLibrary; } |
| 206 | bool isLCDSupported() { return fIsLCDSupported; } |
| 207 | int lcdExtra() { return fLCDExtra; } |
| 208 | |
Ben Wagner | fc49734 | 2017-02-24 11:15:26 -0500 | [diff] [blame] | 209 | // FT_Get_{MM,Var}_{Blend,Design}_Coordinates were added in FreeType 2.7.1. |
| 210 | // Prior to this there was no way to get the coordinates out of the FT_Face. |
| 211 | // This wasn't too bad because you needed to specify them anyway, and the clamp was provided. |
| 212 | // However, this doesn't work when face_index specifies named variations as introduced in 2.6.1. |
| 213 | using FT_Get_Var_Blend_CoordinatesProc = FT_Error (*)(FT_Face, FT_UInt, FT_Fixed*); |
| 214 | FT_Get_Var_Blend_CoordinatesProc fGetVarDesignCoordinates; |
| 215 | |
Ben Wagner | e346b1e | 2018-06-26 11:22:37 -0400 | [diff] [blame] | 216 | // FT_Get_Var_Axis_Flags was introduced in FreeType 2.8.1 |
| 217 | // Get the ‘flags’ field of an OpenType Variation Axis Record. |
| 218 | // Not meaningful for Adobe MM fonts (‘*flags’ is always zero). |
| 219 | using FT_Get_Var_Axis_FlagsProc = FT_Error (*)(FT_MM_Var*, FT_UInt, FT_UInt*); |
| 220 | FT_Get_Var_Axis_FlagsProc fGetVarAxisFlags; |
| 221 | |
bungeman | 9dc2468 | 2014-12-01 14:01:32 -0800 | [diff] [blame] | 222 | private: |
| 223 | FT_Library fLibrary; |
| 224 | bool fIsLCDSupported; |
| 225 | int fLCDExtra; |
| 226 | |
| 227 | // FT_Library_SetLcdFilterWeights was introduced in FreeType 2.4.0. |
| 228 | // The following platforms provide FreeType of at least 2.4.0. |
| 229 | // Ubuntu >= 11.04 (previous deprecated April 2013) |
| 230 | // Debian >= 6.0 (good) |
| 231 | // OpenSuse >= 11.4 (previous deprecated January 2012 / Nov 2013 for Evergreen 11.2) |
| 232 | // Fedora >= 14 (good) |
| 233 | // Android >= Gingerbread (good) |
Ben Wagner | fc49734 | 2017-02-24 11:15:26 -0500 | [diff] [blame] | 234 | // RHEL >= 7 (6 has 2.3.11, EOL Nov 2020, Phase 3 May 2017) |
| 235 | using FT_Library_SetLcdFilterWeightsProc = FT_Error (*)(FT_Library, unsigned char*); |
Ben Wagner | 2a098d0 | 2017-03-01 13:00:53 -0500 | [diff] [blame] | 236 | |
| 237 | // FreeType added the ability to read global properties in 2.7.0. After 2.7.1 a means for users |
| 238 | // of FT_New_Library to request these global properties to be read was added. |
| 239 | using FT_Set_Default_PropertiesProc = void (*)(FT_Library); |
bungeman | 9dc2468 | 2014-12-01 14:01:32 -0800 | [diff] [blame] | 240 | }; |
| 241 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 242 | struct SkFaceRec; |
| 243 | |
reed | 086eea9 | 2016-05-04 17:12:46 -0700 | [diff] [blame] | 244 | SK_DECLARE_STATIC_MUTEX(gFTMutex); |
bungeman | 9dc2468 | 2014-12-01 14:01:32 -0800 | [diff] [blame] | 245 | static FreeTypeLibrary* gFTLibrary; |
| 246 | static SkFaceRec* gFaceRecHead; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 247 | |
bungeman | aabd71c | 2016-03-01 15:15:09 -0800 | [diff] [blame] | 248 | // Private to ref_ft_library and unref_ft_library |
bungeman | 9dc2468 | 2014-12-01 14:01:32 -0800 | [diff] [blame] | 249 | static int gFTCount; |
bungeman@google.com | fd668cf | 2012-08-24 17:46:11 +0000 | [diff] [blame] | 250 | |
scroggo@google.com | 94bc60f | 2012-10-04 20:45:06 +0000 | [diff] [blame] | 251 | // Caller must lock gFTMutex before calling this function. |
bungeman | 9dc2468 | 2014-12-01 14:01:32 -0800 | [diff] [blame] | 252 | static bool ref_ft_library() { |
bungeman | 5ec443c | 2014-11-21 13:18:34 -0800 | [diff] [blame] | 253 | gFTMutex.assertHeld(); |
bungeman | 9dc2468 | 2014-12-01 14:01:32 -0800 | [diff] [blame] | 254 | SkASSERT(gFTCount >= 0); |
bungeman | 5ec443c | 2014-11-21 13:18:34 -0800 | [diff] [blame] | 255 | |
bungeman | 9dc2468 | 2014-12-01 14:01:32 -0800 | [diff] [blame] | 256 | if (0 == gFTCount) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 257 | SkASSERT(nullptr == gFTLibrary); |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 258 | gFTLibrary = new FreeTypeLibrary; |
reed@google.com | ea2333d | 2011-03-14 16:44:56 +0000 | [diff] [blame] | 259 | } |
bungeman | 9dc2468 | 2014-12-01 14:01:32 -0800 | [diff] [blame] | 260 | ++gFTCount; |
| 261 | return gFTLibrary->library(); |
agl@chromium.org | 309485b | 2009-07-21 17:41:32 +0000 | [diff] [blame] | 262 | } |
| 263 | |
bungeman | 9dc2468 | 2014-12-01 14:01:32 -0800 | [diff] [blame] | 264 | // Caller must lock gFTMutex before calling this function. |
| 265 | static void unref_ft_library() { |
| 266 | gFTMutex.assertHeld(); |
| 267 | SkASSERT(gFTCount > 0); |
commit-bot@chromium.org | ba9354b | 2014-02-10 19:58:49 +0000 | [diff] [blame] | 268 | |
bungeman | 9dc2468 | 2014-12-01 14:01:32 -0800 | [diff] [blame] | 269 | --gFTCount; |
| 270 | if (0 == gFTCount) { |
bungeman | aabd71c | 2016-03-01 15:15:09 -0800 | [diff] [blame] | 271 | SkASSERT(nullptr == gFaceRecHead); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 272 | SkASSERT(nullptr != gFTLibrary); |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 273 | delete gFTLibrary; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 274 | SkDEBUGCODE(gFTLibrary = nullptr;) |
bungeman | 9dc2468 | 2014-12-01 14:01:32 -0800 | [diff] [blame] | 275 | } |
reed@google.com | fb2fdcc | 2012-10-17 15:49:36 +0000 | [diff] [blame] | 276 | } |
| 277 | |
Ben Wagner | fc49734 | 2017-02-24 11:15:26 -0500 | [diff] [blame] | 278 | /////////////////////////////////////////////////////////////////////////// |
| 279 | |
| 280 | struct SkFaceRec { |
| 281 | SkFaceRec* fNext; |
| 282 | std::unique_ptr<FT_FaceRec, SkFunctionWrapper<FT_Error, FT_FaceRec, FT_Done_Face>> fFace; |
| 283 | FT_StreamRec fFTStream; |
| 284 | std::unique_ptr<SkStreamAsset> fSkStream; |
| 285 | uint32_t fRefCnt; |
| 286 | uint32_t fFontID; |
| 287 | |
| 288 | // FreeType prior to 2.7.1 does not implement retreiving variation design metrics. |
| 289 | // Cache the variation design metrics used to create the font if the user specifies them. |
| 290 | SkAutoSTMalloc<4, SkFixed> fAxes; |
| 291 | int fAxesCount; |
| 292 | |
| 293 | // FreeType from 2.6.1 (14d6b5d7) until 2.7.0 (ee3f36f6b38) uses font_index for both font index |
| 294 | // and named variation index on input, but masks the named variation index part on output. |
| 295 | // Manually keep track of when a named variation is requested for 2.6.1 until 2.7.1. |
| 296 | bool fNamedVariationSpecified; |
| 297 | |
| 298 | SkFaceRec(std::unique_ptr<SkStreamAsset> stream, uint32_t fontID); |
| 299 | }; |
| 300 | |
| 301 | extern "C" { |
| 302 | static unsigned long sk_ft_stream_io(FT_Stream ftStream, |
| 303 | unsigned long offset, |
| 304 | unsigned char* buffer, |
| 305 | unsigned long count) |
| 306 | { |
| 307 | SkStreamAsset* stream = static_cast<SkStreamAsset*>(ftStream->descriptor.pointer); |
| 308 | |
| 309 | if (count) { |
| 310 | if (!stream->seek(offset)) { |
| 311 | return 0; |
| 312 | } |
| 313 | count = stream->read(buffer, count); |
| 314 | } |
| 315 | return count; |
| 316 | } |
| 317 | |
| 318 | static void sk_ft_stream_close(FT_Stream) {} |
| 319 | } |
| 320 | |
| 321 | SkFaceRec::SkFaceRec(std::unique_ptr<SkStreamAsset> stream, uint32_t fontID) |
| 322 | : fNext(nullptr), fSkStream(std::move(stream)), fRefCnt(1), fFontID(fontID) |
| 323 | , fAxesCount(0), fNamedVariationSpecified(false) |
| 324 | { |
| 325 | sk_bzero(&fFTStream, sizeof(fFTStream)); |
| 326 | fFTStream.size = fSkStream->getLength(); |
| 327 | fFTStream.descriptor.pointer = fSkStream.get(); |
| 328 | fFTStream.read = sk_ft_stream_io; |
| 329 | fFTStream.close = sk_ft_stream_close; |
| 330 | } |
| 331 | |
| 332 | static void ft_face_setup_axes(SkFaceRec* rec, const SkFontData& data) { |
| 333 | if (!(rec->fFace->face_flags & FT_FACE_FLAG_MULTIPLE_MASTERS)) { |
| 334 | return; |
| 335 | } |
| 336 | |
| 337 | // If a named variation is requested, don't overwrite the named variation's position. |
| 338 | if (data.getIndex() > 0xFFFF) { |
| 339 | rec->fNamedVariationSpecified = true; |
| 340 | return; |
| 341 | } |
| 342 | |
| 343 | SkDEBUGCODE( |
| 344 | FT_MM_Var* variations = nullptr; |
| 345 | if (FT_Get_MM_Var(rec->fFace.get(), &variations)) { |
Mike Klein | 3e0141a | 2019-03-26 10:41:18 -0500 | [diff] [blame] | 346 | LOG_INFO("INFO: font %s claims variations, but none found.\n", |
Hal Canary | 2b0e6cd | 2018-07-09 12:43:39 -0400 | [diff] [blame] | 347 | rec->fFace->family_name); |
Ben Wagner | fc49734 | 2017-02-24 11:15:26 -0500 | [diff] [blame] | 348 | return; |
| 349 | } |
| 350 | SkAutoFree autoFreeVariations(variations); |
| 351 | |
| 352 | if (static_cast<FT_UInt>(data.getAxisCount()) != variations->num_axis) { |
Mike Klein | 3e0141a | 2019-03-26 10:41:18 -0500 | [diff] [blame] | 353 | LOG_INFO("INFO: font %s has %d variations, but %d were specified.\n", |
Hal Canary | 2b0e6cd | 2018-07-09 12:43:39 -0400 | [diff] [blame] | 354 | rec->fFace->family_name, variations->num_axis, data.getAxisCount()); |
Ben Wagner | fc49734 | 2017-02-24 11:15:26 -0500 | [diff] [blame] | 355 | return; |
| 356 | } |
| 357 | ) |
| 358 | |
| 359 | SkAutoSTMalloc<4, FT_Fixed> coords(data.getAxisCount()); |
| 360 | for (int i = 0; i < data.getAxisCount(); ++i) { |
| 361 | coords[i] = data.getAxis()[i]; |
| 362 | } |
| 363 | if (FT_Set_Var_Design_Coordinates(rec->fFace.get(), data.getAxisCount(), coords.get())) { |
Mike Klein | 3e0141a | 2019-03-26 10:41:18 -0500 | [diff] [blame] | 364 | LOG_INFO("INFO: font %s has variations, but specified variations could not be set.\n", |
Hal Canary | 2b0e6cd | 2018-07-09 12:43:39 -0400 | [diff] [blame] | 365 | rec->fFace->family_name); |
Ben Wagner | fc49734 | 2017-02-24 11:15:26 -0500 | [diff] [blame] | 366 | return; |
| 367 | } |
| 368 | |
| 369 | rec->fAxesCount = data.getAxisCount(); |
| 370 | rec->fAxes.reset(rec->fAxesCount); |
| 371 | for (int i = 0; i < rec->fAxesCount; ++i) { |
| 372 | rec->fAxes[i] = data.getAxis()[i]; |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | // Will return nullptr on failure |
| 377 | // Caller must lock gFTMutex before calling this function. |
| 378 | static SkFaceRec* ref_ft_face(const SkTypeface* typeface) { |
| 379 | gFTMutex.assertHeld(); |
| 380 | |
| 381 | const SkFontID fontID = typeface->uniqueID(); |
| 382 | SkFaceRec* cachedRec = gFaceRecHead; |
| 383 | while (cachedRec) { |
| 384 | if (cachedRec->fFontID == fontID) { |
| 385 | SkASSERT(cachedRec->fFace); |
| 386 | cachedRec->fRefCnt += 1; |
| 387 | return cachedRec; |
| 388 | } |
| 389 | cachedRec = cachedRec->fNext; |
| 390 | } |
| 391 | |
| 392 | std::unique_ptr<SkFontData> data = typeface->makeFontData(); |
| 393 | if (nullptr == data || !data->hasStream()) { |
| 394 | return nullptr; |
| 395 | } |
| 396 | |
| 397 | std::unique_ptr<SkFaceRec> rec(new SkFaceRec(data->detachStream(), fontID)); |
| 398 | |
| 399 | FT_Open_Args args; |
| 400 | memset(&args, 0, sizeof(args)); |
| 401 | const void* memoryBase = rec->fSkStream->getMemoryBase(); |
| 402 | if (memoryBase) { |
| 403 | args.flags = FT_OPEN_MEMORY; |
| 404 | args.memory_base = (const FT_Byte*)memoryBase; |
| 405 | args.memory_size = rec->fSkStream->getLength(); |
| 406 | } else { |
| 407 | args.flags = FT_OPEN_STREAM; |
| 408 | args.stream = &rec->fFTStream; |
| 409 | } |
| 410 | |
| 411 | { |
| 412 | FT_Face rawFace; |
| 413 | FT_Error err = FT_Open_Face(gFTLibrary->library(), &args, data->getIndex(), &rawFace); |
| 414 | if (err) { |
Hal Canary | 2bcd843 | 2018-01-26 10:35:07 -0500 | [diff] [blame] | 415 | SK_TRACEFTR(err, "unable to open font '%x'", fontID); |
Ben Wagner | fc49734 | 2017-02-24 11:15:26 -0500 | [diff] [blame] | 416 | return nullptr; |
| 417 | } |
| 418 | rec->fFace.reset(rawFace); |
| 419 | } |
| 420 | SkASSERT(rec->fFace); |
| 421 | |
| 422 | ft_face_setup_axes(rec.get(), *data); |
| 423 | |
| 424 | // FreeType will set the charmap to the "most unicode" cmap if it exists. |
| 425 | // If there are no unicode cmaps, the charmap is set to nullptr. |
| 426 | // However, "symbol" cmaps should also be considered "fallback unicode" cmaps |
| 427 | // because they are effectively private use area only (even if they aren't). |
| 428 | // This is the last on the fallback list at |
| 429 | // https://developer.apple.com/fonts/TrueType-Reference-Manual/RM06/Chap6cmap.html |
| 430 | if (!rec->fFace->charmap) { |
| 431 | FT_Select_Charmap(rec->fFace.get(), FT_ENCODING_MS_SYMBOL); |
| 432 | } |
| 433 | |
| 434 | rec->fNext = gFaceRecHead; |
| 435 | gFaceRecHead = rec.get(); |
| 436 | return rec.release(); |
| 437 | } |
| 438 | |
| 439 | // Caller must lock gFTMutex before calling this function. |
Ben Wagner | f1b61af | 2017-03-09 15:12:09 -0500 | [diff] [blame] | 440 | // Marked extern because vc++ does not support internal linkage template parameters. |
| 441 | extern /*static*/ void unref_ft_face(SkFaceRec* faceRec) { |
Ben Wagner | fc49734 | 2017-02-24 11:15:26 -0500 | [diff] [blame] | 442 | gFTMutex.assertHeld(); |
| 443 | |
| 444 | SkFaceRec* rec = gFaceRecHead; |
| 445 | SkFaceRec* prev = nullptr; |
| 446 | while (rec) { |
| 447 | SkFaceRec* next = rec->fNext; |
| 448 | if (rec->fFace == faceRec->fFace) { |
| 449 | if (--rec->fRefCnt == 0) { |
| 450 | if (prev) { |
| 451 | prev->fNext = next; |
| 452 | } else { |
| 453 | gFaceRecHead = next; |
| 454 | } |
| 455 | delete rec; |
| 456 | } |
| 457 | return; |
| 458 | } |
| 459 | prev = rec; |
| 460 | rec = next; |
| 461 | } |
| 462 | SkDEBUGFAIL("shouldn't get here, face not in list"); |
| 463 | } |
| 464 | |
| 465 | class AutoFTAccess { |
| 466 | public: |
| 467 | AutoFTAccess(const SkTypeface* tf) : fFaceRec(nullptr) { |
| 468 | gFTMutex.acquire(); |
Ben Wagner | 7ca9a74 | 2017-08-17 14:05:04 -0400 | [diff] [blame] | 469 | SkASSERT_RELEASE(ref_ft_library()); |
Ben Wagner | fc49734 | 2017-02-24 11:15:26 -0500 | [diff] [blame] | 470 | fFaceRec = ref_ft_face(tf); |
| 471 | } |
| 472 | |
| 473 | ~AutoFTAccess() { |
| 474 | if (fFaceRec) { |
| 475 | unref_ft_face(fFaceRec); |
| 476 | } |
| 477 | unref_ft_library(); |
| 478 | gFTMutex.release(); |
| 479 | } |
| 480 | |
| 481 | FT_Face face() { return fFaceRec ? fFaceRec->fFace.get() : nullptr; } |
| 482 | int getAxesCount() { return fFaceRec ? fFaceRec->fAxesCount : 0; } |
| 483 | SkFixed* getAxes() { return fFaceRec ? fFaceRec->fAxes.get() : nullptr; } |
| 484 | bool isNamedVariationSpecified() { |
| 485 | return fFaceRec ? fFaceRec->fNamedVariationSpecified : false; |
| 486 | } |
| 487 | |
| 488 | private: |
| 489 | SkFaceRec* fFaceRec; |
| 490 | }; |
| 491 | |
| 492 | /////////////////////////////////////////////////////////////////////////// |
| 493 | |
george@mozilla.com | c59b5da | 2012-08-23 00:39:08 +0000 | [diff] [blame] | 494 | class SkScalerContext_FreeType : public SkScalerContext_FreeType_Base { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 495 | public: |
bungeman | 7cfd46a | 2016-10-20 16:06:52 -0400 | [diff] [blame] | 496 | SkScalerContext_FreeType(sk_sp<SkTypeface>, |
| 497 | const SkScalerContextEffects&, |
| 498 | const SkDescriptor* desc); |
Brian Salomon | d3b6597 | 2017-03-22 12:05:03 -0400 | [diff] [blame] | 499 | ~SkScalerContext_FreeType() override; |
agl@chromium.org | cc3096b | 2009-04-22 22:09:04 +0000 | [diff] [blame] | 500 | |
reed@android.com | 62900b4 | 2009-02-11 15:07:19 +0000 | [diff] [blame] | 501 | bool success() const { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 502 | return fFTSize != nullptr && fFace != nullptr; |
reed@android.com | 62900b4 | 2009-02-11 15:07:19 +0000 | [diff] [blame] | 503 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 504 | |
| 505 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 506 | unsigned generateGlyphCount() override; |
Ben Wagner | e541645 | 2018-08-09 14:03:42 -0400 | [diff] [blame] | 507 | bool generateAdvance(SkGlyph* glyph) override; |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 508 | void generateMetrics(SkGlyph* glyph) override; |
| 509 | void generateImage(const SkGlyph& glyph) override; |
Ben Wagner | 5ddb308 | 2018-03-29 11:18:06 -0400 | [diff] [blame] | 510 | bool generatePath(SkGlyphID glyphID, SkPath* path) override; |
Mike Reed | b5784ac | 2018-11-12 09:35:15 -0500 | [diff] [blame] | 511 | void generateFontMetrics(SkFontMetrics*) override; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 512 | |
| 513 | private: |
Ben Wagner | fc49734 | 2017-02-24 11:15:26 -0500 | [diff] [blame] | 514 | using UnrefFTFace = SkFunctionWrapper<void, SkFaceRec, unref_ft_face>; |
| 515 | std::unique_ptr<SkFaceRec, UnrefFTFace> fFaceRec; |
| 516 | |
| 517 | FT_Face fFace; // Borrowed face from gFaceRecHead. |
bungeman | 401ae2d | 2016-07-18 15:46:27 -0700 | [diff] [blame] | 518 | FT_Size fFTSize; // The size on the fFace for this scaler. |
| 519 | FT_Int fStrikeIndex; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 520 | |
bungeman | 401ae2d | 2016-07-18 15:46:27 -0700 | [diff] [blame] | 521 | /** The rest of the matrix after FreeType handles the size. |
| 522 | * With outline font rasterization this is handled by FreeType with FT_Set_Transform. |
| 523 | * With bitmap only fonts this matrix must be applied to scale the bitmap. |
| 524 | */ |
| 525 | SkMatrix fMatrix22Scalar; |
| 526 | /** Same as fMatrix22Scalar, but in FreeType units and space. */ |
| 527 | FT_Matrix fMatrix22; |
| 528 | /** The actual size requested. */ |
| 529 | SkVector fScale; |
| 530 | |
| 531 | uint32_t fLoadGlyphFlags; |
| 532 | bool fDoLinearMetrics; |
| 533 | bool fLCDIsVert; |
reed@google.com | f073b33 | 2013-05-06 12:21:16 +0000 | [diff] [blame] | 534 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 535 | FT_Error setupSize(); |
Bruce Wang | f3ca1c6 | 2018-07-09 10:08:36 -0400 | [diff] [blame] | 536 | void getBBoxForCurrentGlyph(const SkGlyph* glyph, FT_BBox* bbox, |
djsollen@google.com | d8b599c | 2012-03-19 19:44:19 +0000 | [diff] [blame] | 537 | bool snapToPixelBoundary = false); |
bungeman@google.com | cbe1b54 | 2013-12-16 17:02:39 +0000 | [diff] [blame] | 538 | bool getCBoxForLetter(char letter, FT_BBox* bbox); |
scroggo@google.com | 94bc60f | 2012-10-04 20:45:06 +0000 | [diff] [blame] | 539 | // Caller must lock gFTMutex before calling this function. |
djsollen@google.com | d8b599c | 2012-03-19 19:44:19 +0000 | [diff] [blame] | 540 | void updateGlyphIfLCD(SkGlyph* glyph); |
commit-bot@chromium.org | 6fa81d7 | 2013-12-26 15:50:29 +0000 | [diff] [blame] | 541 | // Caller must lock gFTMutex before calling this function. |
| 542 | // update FreeType2 glyph slot with glyph emboldened |
Ben Wagner | c3aef18 | 2017-06-26 12:47:33 -0400 | [diff] [blame] | 543 | void emboldenIfNeeded(FT_Face face, FT_GlyphSlot glyph, SkGlyphID gid); |
bungeman | 401ae2d | 2016-07-18 15:46:27 -0700 | [diff] [blame] | 544 | bool shouldSubpixelBitmap(const SkGlyph&, const SkMatrix&); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 545 | }; |
| 546 | |
| 547 | /////////////////////////////////////////////////////////////////////////// |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 548 | |
vandebo@chromium.org | 16be6b8 | 2011-01-28 21:28:56 +0000 | [diff] [blame] | 549 | static bool canEmbed(FT_Face face) { |
vandebo@chromium.org | 16be6b8 | 2011-01-28 21:28:56 +0000 | [diff] [blame] | 550 | FT_UShort fsType = FT_Get_FSType_Flags(face); |
| 551 | return (fsType & (FT_FSTYPE_RESTRICTED_LICENSE_EMBEDDING | |
| 552 | FT_FSTYPE_BITMAP_EMBEDDING_ONLY)) == 0; |
vandebo@chromium.org | 16be6b8 | 2011-01-28 21:28:56 +0000 | [diff] [blame] | 553 | } |
| 554 | |
vandebo | 0f9bad0 | 2014-06-19 11:05:39 -0700 | [diff] [blame] | 555 | static bool canSubset(FT_Face face) { |
vandebo | 0f9bad0 | 2014-06-19 11:05:39 -0700 | [diff] [blame] | 556 | FT_UShort fsType = FT_Get_FSType_Flags(face); |
| 557 | return (fsType & FT_FSTYPE_NO_SUBSETTING) == 0; |
vandebo | 0f9bad0 | 2014-06-19 11:05:39 -0700 | [diff] [blame] | 558 | } |
| 559 | |
Hal Canary | 5bdc4d5 | 2018-04-10 11:13:24 -0400 | [diff] [blame] | 560 | static SkAdvancedTypefaceMetrics::FontType get_font_type(FT_Face face) { |
| 561 | const char* fontType = FT_Get_X11_Font_Format(face); |
| 562 | static struct { const char* s; SkAdvancedTypefaceMetrics::FontType t; } values[] = { |
| 563 | { "Type 1", SkAdvancedTypefaceMetrics::kType1_Font }, |
| 564 | { "CID Type 1", SkAdvancedTypefaceMetrics::kType1CID_Font }, |
| 565 | { "CFF", SkAdvancedTypefaceMetrics::kCFF_Font }, |
| 566 | { "TrueType", SkAdvancedTypefaceMetrics::kTrueType_Font }, |
| 567 | }; |
| 568 | for(const auto& v : values) { if (strcmp(fontType, v.s) == 0) { return v.t; } } |
| 569 | return SkAdvancedTypefaceMetrics::kOther_Font; |
| 570 | } |
| 571 | |
Hal Canary | 209e4b1 | 2017-05-04 14:23:55 -0400 | [diff] [blame] | 572 | std::unique_ptr<SkAdvancedTypefaceMetrics> SkTypeface_FreeType::onGetAdvancedMetrics() const { |
reed@google.com | b4162b1 | 2013-07-02 16:32:29 +0000 | [diff] [blame] | 573 | AutoFTAccess fta(this); |
| 574 | FT_Face face = fta.face(); |
| 575 | if (!face) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 576 | return nullptr; |
reed@google.com | b4162b1 | 2013-07-02 16:32:29 +0000 | [diff] [blame] | 577 | } |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 578 | |
Hal Canary | 209e4b1 | 2017-05-04 14:23:55 -0400 | [diff] [blame] | 579 | std::unique_ptr<SkAdvancedTypefaceMetrics> info(new SkAdvancedTypefaceMetrics); |
Hal Canary | 8031b32 | 2018-03-29 13:20:30 -0700 | [diff] [blame] | 580 | info->fPostScriptName.set(FT_Get_Postscript_Name(face)); |
| 581 | info->fFontName = info->fPostScriptName; |
halcanary | 3287588 | 2016-08-16 09:36:23 -0700 | [diff] [blame] | 582 | |
vandebo | 0f9bad0 | 2014-06-19 11:05:39 -0700 | [diff] [blame] | 583 | if (FT_HAS_MULTIPLE_MASTERS(face)) { |
halcanary | 3287588 | 2016-08-16 09:36:23 -0700 | [diff] [blame] | 584 | info->fFlags |= SkAdvancedTypefaceMetrics::kMultiMaster_FontFlag; |
vandebo | 0f9bad0 | 2014-06-19 11:05:39 -0700 | [diff] [blame] | 585 | } |
| 586 | if (!canEmbed(face)) { |
halcanary | 3287588 | 2016-08-16 09:36:23 -0700 | [diff] [blame] | 587 | info->fFlags |= SkAdvancedTypefaceMetrics::kNotEmbeddable_FontFlag; |
vandebo | 0f9bad0 | 2014-06-19 11:05:39 -0700 | [diff] [blame] | 588 | } |
| 589 | if (!canSubset(face)) { |
halcanary | 3287588 | 2016-08-16 09:36:23 -0700 | [diff] [blame] | 590 | info->fFlags |= SkAdvancedTypefaceMetrics::kNotSubsettable_FontFlag; |
vandebo | 0f9bad0 | 2014-06-19 11:05:39 -0700 | [diff] [blame] | 591 | } |
vandebo@chromium.org | c48b2b3 | 2011-02-02 02:11:22 +0000 | [diff] [blame] | 592 | |
Hal Canary | 5bdc4d5 | 2018-04-10 11:13:24 -0400 | [diff] [blame] | 593 | info->fType = get_font_type(face); |
halcanary | 3287588 | 2016-08-16 09:36:23 -0700 | [diff] [blame] | 594 | info->fStyle = (SkAdvancedTypefaceMetrics::StyleFlags)0; |
bungeman | f149169 | 2016-07-22 11:19:24 -0700 | [diff] [blame] | 595 | if (FT_IS_FIXED_WIDTH(face)) { |
vandebo@chromium.org | c48b2b3 | 2011-02-02 02:11:22 +0000 | [diff] [blame] | 596 | info->fStyle |= SkAdvancedTypefaceMetrics::kFixedPitch_Style; |
bungeman | f149169 | 2016-07-22 11:19:24 -0700 | [diff] [blame] | 597 | } |
| 598 | if (face->style_flags & FT_STYLE_FLAG_ITALIC) { |
vandebo@chromium.org | c48b2b3 | 2011-02-02 02:11:22 +0000 | [diff] [blame] | 599 | info->fStyle |= SkAdvancedTypefaceMetrics::kItalic_Style; |
bungeman | f149169 | 2016-07-22 11:19:24 -0700 | [diff] [blame] | 600 | } |
vandebo@chromium.org | c48b2b3 | 2011-02-02 02:11:22 +0000 | [diff] [blame] | 601 | |
bungeman | f149169 | 2016-07-22 11:19:24 -0700 | [diff] [blame] | 602 | PS_FontInfoRec psFontInfo; |
| 603 | TT_Postscript* postTable; |
| 604 | if (FT_Get_PS_Font_Info(face, &psFontInfo) == 0) { |
| 605 | info->fItalicAngle = psFontInfo.italic_angle; |
| 606 | } else if ((postTable = (TT_Postscript*)FT_Get_Sfnt_Table(face, ft_sfnt_post)) != nullptr) { |
Ben Wagner | 4f6fc83 | 2017-09-15 16:00:40 -0400 | [diff] [blame] | 607 | info->fItalicAngle = SkFixedFloorToInt(postTable->italicAngle); |
vandebo@chromium.org | c48b2b3 | 2011-02-02 02:11:22 +0000 | [diff] [blame] | 608 | } else { |
| 609 | info->fItalicAngle = 0; |
| 610 | } |
| 611 | |
| 612 | info->fAscent = face->ascender; |
| 613 | info->fDescent = face->descender; |
| 614 | |
bungeman | f149169 | 2016-07-22 11:19:24 -0700 | [diff] [blame] | 615 | TT_PCLT* pcltTable; |
| 616 | TT_OS2* os2Table; |
| 617 | if ((pcltTable = (TT_PCLT*)FT_Get_Sfnt_Table(face, ft_sfnt_pclt)) != nullptr) { |
| 618 | info->fCapHeight = pcltTable->CapHeight; |
| 619 | uint8_t serif_style = pcltTable->SerifStyle & 0x3F; |
| 620 | if (2 <= serif_style && serif_style <= 6) { |
vandebo@chromium.org | c48b2b3 | 2011-02-02 02:11:22 +0000 | [diff] [blame] | 621 | info->fStyle |= SkAdvancedTypefaceMetrics::kSerif_Style; |
bungeman | f149169 | 2016-07-22 11:19:24 -0700 | [diff] [blame] | 622 | } else if (9 <= serif_style && serif_style <= 12) { |
vandebo@chromium.org | c48b2b3 | 2011-02-02 02:11:22 +0000 | [diff] [blame] | 623 | info->fStyle |= SkAdvancedTypefaceMetrics::kScript_Style; |
bungeman | f149169 | 2016-07-22 11:19:24 -0700 | [diff] [blame] | 624 | } |
| 625 | } else if (((os2Table = (TT_OS2*)FT_Get_Sfnt_Table(face, ft_sfnt_os2)) != nullptr) && |
bungeman@google.com | cbe1b54 | 2013-12-16 17:02:39 +0000 | [diff] [blame] | 626 | // sCapHeight is available only when version 2 or later. |
bungeman | f149169 | 2016-07-22 11:19:24 -0700 | [diff] [blame] | 627 | os2Table->version != 0xFFFF && |
| 628 | os2Table->version >= 2) |
| 629 | { |
| 630 | info->fCapHeight = os2Table->sCapHeight; |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 631 | } |
vandebo@chromium.org | c48b2b3 | 2011-02-02 02:11:22 +0000 | [diff] [blame] | 632 | info->fBBox = SkIRect::MakeLTRB(face->bbox.xMin, face->bbox.yMax, |
| 633 | face->bbox.xMax, face->bbox.yMin); |
Greg Daniel | 97c1108 | 2018-05-09 15:35:54 +0000 | [diff] [blame] | 634 | return info; |
Hal Canary | 1c2bcd8 | 2018-04-10 11:27:48 -0400 | [diff] [blame] | 635 | } |
| 636 | |
Hal Canary | 46cc3da | 2018-05-09 11:50:34 -0400 | [diff] [blame] | 637 | void SkTypeface_FreeType::getGlyphToUnicodeMap(SkUnichar* dstArray) const { |
| 638 | SkASSERT(dstArray); |
| 639 | AutoFTAccess fta(this); |
| 640 | FT_Face face = fta.face(); |
| 641 | FT_Long numGlyphs = face->num_glyphs; |
| 642 | sk_bzero(dstArray, sizeof(SkUnichar) * numGlyphs); |
| 643 | |
| 644 | FT_UInt glyphIndex; |
| 645 | SkUnichar charCode = FT_Get_First_Char(face, &glyphIndex); |
| 646 | while (glyphIndex) { |
| 647 | SkASSERT(glyphIndex < SkToUInt(numGlyphs)); |
| 648 | // Use the first character that maps to this glyphID. https://crbug.com/359065 |
| 649 | if (0 == dstArray[glyphIndex]) { |
| 650 | dstArray[glyphIndex] = charCode; |
| 651 | } |
| 652 | charCode = FT_Get_Next_Char(face, charCode, &glyphIndex); |
| 653 | } |
| 654 | } |
| 655 | |
Hal Canary | 5bdc4d5 | 2018-04-10 11:13:24 -0400 | [diff] [blame] | 656 | void SkTypeface_FreeType::getPostScriptGlyphNames(SkString* dstArray) const { |
| 657 | SkASSERT(dstArray); |
| 658 | AutoFTAccess fta(this); |
| 659 | FT_Face face = fta.face(); |
| 660 | if (face && FT_HAS_GLYPH_NAMES(face)) { |
| 661 | for (int gID = 0; gID < face->num_glyphs; gID++) { |
| 662 | char glyphName[128]; // PS limit for names is 127 bytes. |
| 663 | FT_Get_Glyph_Name(face, gID, glyphName, 128); |
| 664 | dstArray[gID] = glyphName; |
| 665 | } |
| 666 | } |
| 667 | } |
| 668 | |
reed@google.com | 618ef5e | 2011-01-26 22:10:41 +0000 | [diff] [blame] | 669 | /////////////////////////////////////////////////////////////////////////// |
| 670 | |
reed@google.com | 8ed436c | 2011-07-21 14:12:36 +0000 | [diff] [blame] | 671 | static bool bothZero(SkScalar a, SkScalar b) { |
| 672 | return 0 == a && 0 == b; |
| 673 | } |
| 674 | |
| 675 | // returns false if there is any non-90-rotation or skew |
Ben Wagner | 3e45a2f | 2017-11-01 11:47:39 -0400 | [diff] [blame] | 676 | static bool isAxisAligned(const SkScalerContextRec& rec) { |
reed@google.com | 8ed436c | 2011-07-21 14:12:36 +0000 | [diff] [blame] | 677 | return 0 == rec.fPreSkewX && |
| 678 | (bothZero(rec.fPost2x2[0][1], rec.fPost2x2[1][0]) || |
| 679 | bothZero(rec.fPost2x2[0][0], rec.fPost2x2[1][1])); |
| 680 | } |
| 681 | |
reed | a9322c2 | 2016-04-12 06:47:05 -0700 | [diff] [blame] | 682 | SkScalerContext* SkTypeface_FreeType::onCreateScalerContext(const SkScalerContextEffects& effects, |
| 683 | const SkDescriptor* desc) const { |
bungeman | 7cfd46a | 2016-10-20 16:06:52 -0400 | [diff] [blame] | 684 | auto c = skstd::make_unique<SkScalerContext_FreeType>( |
| 685 | sk_ref_sp(const_cast<SkTypeface_FreeType*>(this)), effects, desc); |
reed@google.com | 0da4861 | 2013-03-19 16:06:52 +0000 | [diff] [blame] | 686 | if (!c->success()) { |
Ben Wagner | c05b2bf | 2016-11-03 16:51:26 -0400 | [diff] [blame] | 687 | return nullptr; |
reed@google.com | 0da4861 | 2013-03-19 16:06:52 +0000 | [diff] [blame] | 688 | } |
bungeman | 7cfd46a | 2016-10-20 16:06:52 -0400 | [diff] [blame] | 689 | return c.release(); |
reed@google.com | 0da4861 | 2013-03-19 16:06:52 +0000 | [diff] [blame] | 690 | } |
| 691 | |
Bruce Wang | ebf0cf5 | 2018-06-18 14:04:19 -0400 | [diff] [blame] | 692 | std::unique_ptr<SkFontData> SkTypeface_FreeType::cloneFontData( |
| 693 | const SkFontArguments& args) const { |
| 694 | SkString name; |
| 695 | AutoFTAccess fta(this); |
| 696 | FT_Face face = fta.face(); |
| 697 | Scanner::AxisDefinitions axisDefinitions; |
| 698 | |
| 699 | if (!Scanner::GetAxes(face, &axisDefinitions)) { |
| 700 | return nullptr; |
| 701 | } |
| 702 | SkAutoSTMalloc<4, SkFixed> axisValues(axisDefinitions.count()); |
| 703 | Scanner::computeAxisValues(axisDefinitions, args.getVariationDesignPosition(), |
| 704 | axisValues, name); |
| 705 | int ttcIndex; |
Ben Wagner | ff84d8a | 2019-02-26 15:39:41 -0500 | [diff] [blame] | 706 | std::unique_ptr<SkStreamAsset> stream = this->openStream(&ttcIndex); |
Bruce Wang | ebf0cf5 | 2018-06-18 14:04:19 -0400 | [diff] [blame] | 707 | return skstd::make_unique<SkFontData>(std::move(stream), ttcIndex, axisValues.get(), |
| 708 | axisDefinitions.count()); |
| 709 | } |
| 710 | |
reed@google.com | 0da4861 | 2013-03-19 16:06:52 +0000 | [diff] [blame] | 711 | void SkTypeface_FreeType::onFilterRec(SkScalerContextRec* rec) const { |
bungeman@google.com | 8cf3226 | 2012-04-02 14:34:30 +0000 | [diff] [blame] | 712 | //BOGUS: http://code.google.com/p/chromium/issues/detail?id=121119 |
| 713 | //Cap the requested size as larger sizes give bogus values. |
| 714 | //Remove when http://code.google.com/p/skia/issues/detail?id=554 is fixed. |
bungeman | aabd71c | 2016-03-01 15:15:09 -0800 | [diff] [blame] | 715 | //Note that this also currently only protects against large text size requests, |
| 716 | //the total matrix is not taken into account here. |
bungeman@google.com | 5582e63 | 2012-04-02 14:51:54 +0000 | [diff] [blame] | 717 | if (rec->fTextSize > SkIntToScalar(1 << 14)) { |
scroggo@google.com | 94bc60f | 2012-10-04 20:45:06 +0000 | [diff] [blame] | 718 | rec->fTextSize = SkIntToScalar(1 << 14); |
bungeman@google.com | 8cf3226 | 2012-04-02 14:34:30 +0000 | [diff] [blame] | 719 | } |
skia.committer@gmail.com | a27096b | 2012-08-30 14:38:00 +0000 | [diff] [blame] | 720 | |
bungeman | ec7e12f | 2015-01-21 11:55:16 -0800 | [diff] [blame] | 721 | if (isLCD(*rec)) { |
bungeman | d4742fa | 2015-01-21 11:19:22 -0800 | [diff] [blame] | 722 | // TODO: re-work so that FreeType is set-up and selected by the SkFontMgr. |
| 723 | SkAutoMutexAcquire ama(gFTMutex); |
| 724 | ref_ft_library(); |
bungeman | ec7e12f | 2015-01-21 11:55:16 -0800 | [diff] [blame] | 725 | if (!gFTLibrary->isLCDSupported()) { |
bungeman | d4742fa | 2015-01-21 11:19:22 -0800 | [diff] [blame] | 726 | // If the runtime Freetype library doesn't support LCD, disable it here. |
| 727 | rec->fMaskFormat = SkMask::kA8_Format; |
| 728 | } |
| 729 | unref_ft_library(); |
reed@google.com | 618ef5e | 2011-01-26 22:10:41 +0000 | [diff] [blame] | 730 | } |
reed@google.com | 5b31b0f | 2011-02-23 14:41:42 +0000 | [diff] [blame] | 731 | |
Mike Reed | 04346d5 | 2018-11-05 12:45:32 -0500 | [diff] [blame] | 732 | SkFontHinting h = rec->getHinting(); |
| 733 | if (kFull_SkFontHinting == h && !isLCD(*rec)) { |
reed@google.com | 618ef5e | 2011-01-26 22:10:41 +0000 | [diff] [blame] | 734 | // collapse full->normal hinting if we're not doing LCD |
Mike Reed | 04346d5 | 2018-11-05 12:45:32 -0500 | [diff] [blame] | 735 | h = kNormal_SkFontHinting; |
reed@google.com | 618ef5e | 2011-01-26 22:10:41 +0000 | [diff] [blame] | 736 | } |
reed@google.com | 1ac8350 | 2012-02-28 17:06:02 +0000 | [diff] [blame] | 737 | |
reed@google.com | 8ed436c | 2011-07-21 14:12:36 +0000 | [diff] [blame] | 738 | // rotated text looks bad with hinting, so we disable it as needed |
| 739 | if (!isAxisAligned(*rec)) { |
Mike Reed | 04346d5 | 2018-11-05 12:45:32 -0500 | [diff] [blame] | 740 | h = kNo_SkFontHinting; |
reed@google.com | 8ed436c | 2011-07-21 14:12:36 +0000 | [diff] [blame] | 741 | } |
reed@google.com | 618ef5e | 2011-01-26 22:10:41 +0000 | [diff] [blame] | 742 | rec->setHinting(h); |
reed@google.com | ffe49f5 | 2011-11-22 19:42:41 +0000 | [diff] [blame] | 743 | |
bungeman@google.com | 97efada | 2012-07-30 20:40:50 +0000 | [diff] [blame] | 744 | #ifndef SK_GAMMA_APPLY_TO_A8 |
| 745 | if (!isLCD(*rec)) { |
brianosman | a1e8f8d | 2016-04-08 06:47:54 -0700 | [diff] [blame] | 746 | // SRGBTODO: Is this correct? Do we want contrast boost? |
| 747 | rec->ignorePreBlend(); |
reed@google.com | ffe49f5 | 2011-11-22 19:42:41 +0000 | [diff] [blame] | 748 | } |
reed@google.com | 1ac8350 | 2012-02-28 17:06:02 +0000 | [diff] [blame] | 749 | #endif |
reed@google.com | 618ef5e | 2011-01-26 22:10:41 +0000 | [diff] [blame] | 750 | } |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 751 | |
Ben Wagner | 26a005a | 2018-08-31 17:41:45 -0400 | [diff] [blame] | 752 | int SkTypeface_FreeType::GetUnitsPerEm(FT_Face face) { |
| 753 | if (!face) { |
| 754 | return 0; |
| 755 | } |
| 756 | |
| 757 | SkScalar upem = SkIntToScalar(face->units_per_EM); |
| 758 | // At least some versions of FreeType set face->units_per_EM to 0 for bitmap only fonts. |
| 759 | if (upem == 0) { |
| 760 | TT_Header* ttHeader = (TT_Header*)FT_Get_Sfnt_Table(face, ft_sfnt_head); |
| 761 | if (ttHeader) { |
| 762 | upem = SkIntToScalar(ttHeader->Units_Per_EM); |
| 763 | } |
| 764 | } |
| 765 | return upem; |
| 766 | } |
| 767 | |
reed@google.com | 38c37dd | 2013-03-21 15:36:26 +0000 | [diff] [blame] | 768 | int SkTypeface_FreeType::onGetUPEM() const { |
reed@google.com | b4162b1 | 2013-07-02 16:32:29 +0000 | [diff] [blame] | 769 | AutoFTAccess fta(this); |
| 770 | FT_Face face = fta.face(); |
Ben Wagner | 26a005a | 2018-08-31 17:41:45 -0400 | [diff] [blame] | 771 | return GetUnitsPerEm(face); |
djsollen@google.com | cd9d69b | 2011-03-14 20:30:14 +0000 | [diff] [blame] | 772 | } |
djsollen@google.com | cd9d69b | 2011-03-14 20:30:14 +0000 | [diff] [blame] | 773 | |
reed@google.com | 35fe737 | 2013-10-30 15:07:03 +0000 | [diff] [blame] | 774 | bool SkTypeface_FreeType::onGetKerningPairAdjustments(const uint16_t glyphs[], |
| 775 | int count, int32_t adjustments[]) const { |
| 776 | AutoFTAccess fta(this); |
| 777 | FT_Face face = fta.face(); |
| 778 | if (!face || !FT_HAS_KERNING(face)) { |
| 779 | return false; |
| 780 | } |
| 781 | |
| 782 | for (int i = 0; i < count - 1; ++i) { |
| 783 | FT_Vector delta; |
| 784 | FT_Error err = FT_Get_Kerning(face, glyphs[i], glyphs[i+1], |
| 785 | FT_KERNING_UNSCALED, &delta); |
| 786 | if (err) { |
| 787 | return false; |
| 788 | } |
| 789 | adjustments[i] = delta.x; |
| 790 | } |
| 791 | return true; |
| 792 | } |
| 793 | |
bungeman | 401ae2d | 2016-07-18 15:46:27 -0700 | [diff] [blame] | 794 | /** Returns the bitmap strike equal to or just larger than the requested size. */ |
benjaminwagner | 4534562 | 2016-02-19 15:30:20 -0800 | [diff] [blame] | 795 | static FT_Int chooseBitmapStrike(FT_Face face, FT_F26Dot6 scaleY) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 796 | if (face == nullptr) { |
Mike Klein | 3e0141a | 2019-03-26 10:41:18 -0500 | [diff] [blame] | 797 | LOG_INFO("chooseBitmapStrike aborted due to nullptr face.\n"); |
bungeman@google.com | c9a8a7e | 2013-12-10 18:09:36 +0000 | [diff] [blame] | 798 | return -1; |
| 799 | } |
bungeman | 401ae2d | 2016-07-18 15:46:27 -0700 | [diff] [blame] | 800 | |
| 801 | FT_Pos requestedPPEM = scaleY; // FT_Bitmap_Size::y_ppem is in 26.6 format. |
bungeman@google.com | c9a8a7e | 2013-12-10 18:09:36 +0000 | [diff] [blame] | 802 | FT_Int chosenStrikeIndex = -1; |
| 803 | FT_Pos chosenPPEM = 0; |
| 804 | for (FT_Int strikeIndex = 0; strikeIndex < face->num_fixed_sizes; ++strikeIndex) { |
bungeman | 401ae2d | 2016-07-18 15:46:27 -0700 | [diff] [blame] | 805 | FT_Pos strikePPEM = face->available_sizes[strikeIndex].y_ppem; |
| 806 | if (strikePPEM == requestedPPEM) { |
bungeman@google.com | c9a8a7e | 2013-12-10 18:09:36 +0000 | [diff] [blame] | 807 | // exact match - our search stops here |
bungeman | 401ae2d | 2016-07-18 15:46:27 -0700 | [diff] [blame] | 808 | return strikeIndex; |
| 809 | } else if (chosenPPEM < requestedPPEM) { |
bungeman@google.com | c9a8a7e | 2013-12-10 18:09:36 +0000 | [diff] [blame] | 810 | // attempt to increase chosenPPEM |
bungeman | 401ae2d | 2016-07-18 15:46:27 -0700 | [diff] [blame] | 811 | if (chosenPPEM < strikePPEM) { |
| 812 | chosenPPEM = strikePPEM; |
bungeman@google.com | c9a8a7e | 2013-12-10 18:09:36 +0000 | [diff] [blame] | 813 | chosenStrikeIndex = strikeIndex; |
| 814 | } |
| 815 | } else { |
bungeman | 401ae2d | 2016-07-18 15:46:27 -0700 | [diff] [blame] | 816 | // attempt to decrease chosenPPEM, but not below requestedPPEM |
| 817 | if (requestedPPEM < strikePPEM && strikePPEM < chosenPPEM) { |
| 818 | chosenPPEM = strikePPEM; |
bungeman@google.com | c9a8a7e | 2013-12-10 18:09:36 +0000 | [diff] [blame] | 819 | chosenStrikeIndex = strikeIndex; |
| 820 | } |
| 821 | } |
| 822 | } |
bungeman@google.com | c9a8a7e | 2013-12-10 18:09:36 +0000 | [diff] [blame] | 823 | return chosenStrikeIndex; |
| 824 | } |
| 825 | |
bungeman | 7cfd46a | 2016-10-20 16:06:52 -0400 | [diff] [blame] | 826 | SkScalerContext_FreeType::SkScalerContext_FreeType(sk_sp<SkTypeface> typeface, |
reed | a9322c2 | 2016-04-12 06:47:05 -0700 | [diff] [blame] | 827 | const SkScalerContextEffects& effects, |
| 828 | const SkDescriptor* desc) |
bungeman | 7cfd46a | 2016-10-20 16:06:52 -0400 | [diff] [blame] | 829 | : SkScalerContext_FreeType_Base(std::move(typeface), effects, desc) |
bungeman | aabd71c | 2016-03-01 15:15:09 -0800 | [diff] [blame] | 830 | , fFace(nullptr) |
| 831 | , fFTSize(nullptr) |
| 832 | , fStrikeIndex(-1) |
bungeman | 13a007d | 2015-06-19 05:09:39 -0700 | [diff] [blame] | 833 | { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 834 | SkAutoMutexAcquire ac(gFTMutex); |
Ben Wagner | 7ca9a74 | 2017-08-17 14:05:04 -0400 | [diff] [blame] | 835 | SkASSERT_RELEASE(ref_ft_library()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 836 | |
Ben Wagner | fc49734 | 2017-02-24 11:15:26 -0500 | [diff] [blame] | 837 | fFaceRec.reset(ref_ft_face(this->getTypeface())); |
| 838 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 839 | // load the font file |
Ben Wagner | fc49734 | 2017-02-24 11:15:26 -0500 | [diff] [blame] | 840 | if (nullptr == fFaceRec) { |
Mike Klein | 3e0141a | 2019-03-26 10:41:18 -0500 | [diff] [blame] | 841 | LOG_INFO("Could not create FT_Face.\n"); |
reed@android.com | 62900b4 | 2009-02-11 15:07:19 +0000 | [diff] [blame] | 842 | return; |
| 843 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 844 | |
reed@google.com | a1bfa21 | 2012-03-08 21:57:12 +0000 | [diff] [blame] | 845 | fLCDIsVert = SkToBool(fRec.fFlags & SkScalerContext::kLCD_Vertical_Flag); |
| 846 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 847 | // compute the flags we send to Load_Glyph |
Ben Wagner | aa166bd | 2018-08-14 14:58:18 -0400 | [diff] [blame] | 848 | bool linearMetrics = this->isSubpixel(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 849 | { |
reed@android.com | e4d0bc0 | 2009-07-24 19:53:20 +0000 | [diff] [blame] | 850 | FT_Int32 loadFlags = FT_LOAD_DEFAULT; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 851 | |
agl@chromium.org | 70a303f | 2010-05-10 14:15:50 +0000 | [diff] [blame] | 852 | if (SkMask::kBW_Format == fRec.fMaskFormat) { |
| 853 | // See http://code.google.com/p/chromium/issues/detail?id=43252#c24 |
| 854 | loadFlags = FT_LOAD_TARGET_MONO; |
Mike Reed | 04346d5 | 2018-11-05 12:45:32 -0500 | [diff] [blame] | 855 | if (fRec.getHinting() == kNo_SkFontHinting) { |
agl@chromium.org | 70a303f | 2010-05-10 14:15:50 +0000 | [diff] [blame] | 856 | loadFlags = FT_LOAD_NO_HINTING; |
reed@google.com | bdc9988 | 2011-11-21 14:36:57 +0000 | [diff] [blame] | 857 | linearMetrics = true; |
reed@google.com | effc501 | 2011-06-27 16:44:46 +0000 | [diff] [blame] | 858 | } |
agl@chromium.org | 70a303f | 2010-05-10 14:15:50 +0000 | [diff] [blame] | 859 | } else { |
| 860 | switch (fRec.getHinting()) { |
Mike Reed | 04346d5 | 2018-11-05 12:45:32 -0500 | [diff] [blame] | 861 | case kNo_SkFontHinting: |
agl@chromium.org | 70a303f | 2010-05-10 14:15:50 +0000 | [diff] [blame] | 862 | loadFlags = FT_LOAD_NO_HINTING; |
reed@google.com | bdc9988 | 2011-11-21 14:36:57 +0000 | [diff] [blame] | 863 | linearMetrics = true; |
agl@chromium.org | 70a303f | 2010-05-10 14:15:50 +0000 | [diff] [blame] | 864 | break; |
Mike Reed | 04346d5 | 2018-11-05 12:45:32 -0500 | [diff] [blame] | 865 | case kSlight_SkFontHinting: |
agl@chromium.org | 70a303f | 2010-05-10 14:15:50 +0000 | [diff] [blame] | 866 | loadFlags = FT_LOAD_TARGET_LIGHT; // This implies FORCE_AUTOHINT |
| 867 | break; |
Mike Reed | 04346d5 | 2018-11-05 12:45:32 -0500 | [diff] [blame] | 868 | case kNormal_SkFontHinting: |
Ben Wagner | d34b8a8 | 2018-06-07 15:02:18 -0400 | [diff] [blame] | 869 | loadFlags = FT_LOAD_TARGET_NORMAL; |
agl@chromium.org | 70a303f | 2010-05-10 14:15:50 +0000 | [diff] [blame] | 870 | break; |
Mike Reed | 04346d5 | 2018-11-05 12:45:32 -0500 | [diff] [blame] | 871 | case kFull_SkFontHinting: |
agl@chromium.org | 70a303f | 2010-05-10 14:15:50 +0000 | [diff] [blame] | 872 | loadFlags = FT_LOAD_TARGET_NORMAL; |
reed@google.com | effc501 | 2011-06-27 16:44:46 +0000 | [diff] [blame] | 873 | if (isLCD(fRec)) { |
reed@google.com | a1bfa21 | 2012-03-08 21:57:12 +0000 | [diff] [blame] | 874 | if (fLCDIsVert) { |
reed@google.com | effc501 | 2011-06-27 16:44:46 +0000 | [diff] [blame] | 875 | loadFlags = FT_LOAD_TARGET_LCD_V; |
| 876 | } else { |
| 877 | loadFlags = FT_LOAD_TARGET_LCD; |
| 878 | } |
reed@google.com | ea2333d | 2011-03-14 16:44:56 +0000 | [diff] [blame] | 879 | } |
agl@chromium.org | 70a303f | 2010-05-10 14:15:50 +0000 | [diff] [blame] | 880 | break; |
| 881 | default: |
Mike Klein | 3e0141a | 2019-03-26 10:41:18 -0500 | [diff] [blame] | 882 | LOG_INFO("---------- UNKNOWN hinting %d\n", fRec.getHinting()); |
agl@chromium.org | 70a303f | 2010-05-10 14:15:50 +0000 | [diff] [blame] | 883 | break; |
| 884 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 885 | } |
| 886 | |
Ben Wagner | d34b8a8 | 2018-06-07 15:02:18 -0400 | [diff] [blame] | 887 | if (fRec.fFlags & SkScalerContext::kForceAutohinting_Flag) { |
| 888 | loadFlags |= FT_LOAD_FORCE_AUTOHINT; |
| 889 | #ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK |
| 890 | } else { |
| 891 | loadFlags |= FT_LOAD_NO_AUTOHINT; |
| 892 | #endif |
| 893 | } |
| 894 | |
reed@google.com | effc501 | 2011-06-27 16:44:46 +0000 | [diff] [blame] | 895 | if ((fRec.fFlags & SkScalerContext::kEmbeddedBitmapText_Flag) == 0) { |
agl@chromium.org | e0d0899 | 2009-08-07 19:19:23 +0000 | [diff] [blame] | 896 | loadFlags |= FT_LOAD_NO_BITMAP; |
reed@google.com | effc501 | 2011-06-27 16:44:46 +0000 | [diff] [blame] | 897 | } |
agl@chromium.org | e0d0899 | 2009-08-07 19:19:23 +0000 | [diff] [blame] | 898 | |
reed@google.com | 96a9f791 | 2011-05-06 11:49:30 +0000 | [diff] [blame] | 899 | // Always using FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH to get correct |
| 900 | // advances, as fontconfig and cairo do. |
| 901 | // See http://code.google.com/p/skia/issues/detail?id=222. |
| 902 | loadFlags |= FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH; |
| 903 | |
bungeman@google.com | 8ff8a19 | 2012-09-25 20:38:28 +0000 | [diff] [blame] | 904 | // Use vertical layout if requested. |
Ben Wagner | aa166bd | 2018-08-14 14:58:18 -0400 | [diff] [blame] | 905 | if (this->isVertical()) { |
bungeman@google.com | 8ff8a19 | 2012-09-25 20:38:28 +0000 | [diff] [blame] | 906 | loadFlags |= FT_LOAD_VERTICAL_LAYOUT; |
| 907 | } |
| 908 | |
bungeman@google.com | c9a8a7e | 2013-12-10 18:09:36 +0000 | [diff] [blame] | 909 | loadFlags |= FT_LOAD_COLOR; |
| 910 | |
reed@android.com | e4d0bc0 | 2009-07-24 19:53:20 +0000 | [diff] [blame] | 911 | fLoadGlyphFlags = loadFlags; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 912 | } |
| 913 | |
bungeman | aabd71c | 2016-03-01 15:15:09 -0800 | [diff] [blame] | 914 | using DoneFTSize = SkFunctionWrapper<FT_Error, skstd::remove_pointer_t<FT_Size>, FT_Done_Size>; |
Ben Wagner | fc49734 | 2017-02-24 11:15:26 -0500 | [diff] [blame] | 915 | std::unique_ptr<skstd::remove_pointer_t<FT_Size>, DoneFTSize> ftSize([this]() -> FT_Size { |
bungeman | aabd71c | 2016-03-01 15:15:09 -0800 | [diff] [blame] | 916 | FT_Size size; |
Ben Wagner | fc49734 | 2017-02-24 11:15:26 -0500 | [diff] [blame] | 917 | FT_Error err = FT_New_Size(fFaceRec->fFace.get(), &size); |
bungeman | aabd71c | 2016-03-01 15:15:09 -0800 | [diff] [blame] | 918 | if (err != 0) { |
Hal Canary | 2bcd843 | 2018-01-26 10:35:07 -0500 | [diff] [blame] | 919 | SK_TRACEFTR(err, "FT_New_Size(%s) failed.", fFaceRec->fFace->family_name); |
bungeman | aabd71c | 2016-03-01 15:15:09 -0800 | [diff] [blame] | 920 | return nullptr; |
| 921 | } |
| 922 | return size; |
| 923 | }()); |
| 924 | if (nullptr == ftSize) { |
Mike Klein | 3e0141a | 2019-03-26 10:41:18 -0500 | [diff] [blame] | 925 | LOG_INFO("Could not create FT_Size.\n"); |
bungeman@google.com | c9a8a7e | 2013-12-10 18:09:36 +0000 | [diff] [blame] | 926 | return; |
| 927 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 928 | |
bungeman | aabd71c | 2016-03-01 15:15:09 -0800 | [diff] [blame] | 929 | FT_Error err = FT_Activate_Size(ftSize.get()); |
| 930 | if (err != 0) { |
Hal Canary | 2bcd843 | 2018-01-26 10:35:07 -0500 | [diff] [blame] | 931 | SK_TRACEFTR(err, "FT_Activate_Size(%s) failed.", fFaceRec->fFace->family_name); |
bungeman | aabd71c | 2016-03-01 15:15:09 -0800 | [diff] [blame] | 932 | return; |
| 933 | } |
| 934 | |
Ben Wagner | 082a7a7 | 2018-06-08 14:15:47 -0400 | [diff] [blame] | 935 | fRec.computeMatrices(SkScalerContextRec::kFull_PreMatrixScale, &fScale, &fMatrix22Scalar); |
| 936 | FT_F26Dot6 scaleX = SkScalarToFDot6(fScale.fX); |
| 937 | FT_F26Dot6 scaleY = SkScalarToFDot6(fScale.fY); |
| 938 | |
Ben Wagner | fc49734 | 2017-02-24 11:15:26 -0500 | [diff] [blame] | 939 | if (FT_IS_SCALABLE(fFaceRec->fFace)) { |
| 940 | err = FT_Set_Char_Size(fFaceRec->fFace.get(), scaleX, scaleY, 72, 72); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 941 | if (err != 0) { |
Hal Canary | 2bcd843 | 2018-01-26 10:35:07 -0500 | [diff] [blame] | 942 | SK_TRACEFTR(err, "FT_Set_CharSize(%s, %f, %f) failed.", |
Ben Wagner | 082a7a7 | 2018-06-08 14:15:47 -0400 | [diff] [blame] | 943 | fFaceRec->fFace->family_name, fScale.fX, fScale.fY); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 944 | return; |
| 945 | } |
Ben Wagner | 082a7a7 | 2018-06-08 14:15:47 -0400 | [diff] [blame] | 946 | |
Ben Wagner | 082a7a7 | 2018-06-08 14:15:47 -0400 | [diff] [blame] | 947 | // Adjust the matrix to reflect the actually chosen scale. |
| 948 | // FreeType currently does not allow requesting sizes less than 1, this allow for scaling. |
| 949 | // Don't do this at all sizes as that will interfere with hinting. |
| 950 | if (fScale.fX < 1 || fScale.fY < 1) { |
| 951 | SkScalar upem = fFaceRec->fFace->units_per_EM; |
| 952 | FT_Size_Metrics& ftmetrics = fFaceRec->fFace->size->metrics; |
| 953 | SkScalar x_ppem = upem * SkFT_FixedToScalar(ftmetrics.x_scale) / 64.0f; |
| 954 | SkScalar y_ppem = upem * SkFT_FixedToScalar(ftmetrics.y_scale) / 64.0f; |
| 955 | fMatrix22Scalar.preScale(fScale.x() / x_ppem, fScale.y() / y_ppem); |
| 956 | } |
Ben Wagner | 082a7a7 | 2018-06-08 14:15:47 -0400 | [diff] [blame] | 957 | |
Ben Wagner | fc49734 | 2017-02-24 11:15:26 -0500 | [diff] [blame] | 958 | } else if (FT_HAS_FIXED_SIZES(fFaceRec->fFace)) { |
| 959 | fStrikeIndex = chooseBitmapStrike(fFaceRec->fFace.get(), scaleY); |
bungeman@google.com | c9a8a7e | 2013-12-10 18:09:36 +0000 | [diff] [blame] | 960 | if (fStrikeIndex == -1) { |
Mike Klein | 3e0141a | 2019-03-26 10:41:18 -0500 | [diff] [blame] | 961 | LOG_INFO("No glyphs for font \"%s\" size %f.\n", |
Hal Canary | 2b0e6cd | 2018-07-09 12:43:39 -0400 | [diff] [blame] | 962 | fFaceRec->fFace->family_name, fScale.fY); |
bungeman | 401ae2d | 2016-07-18 15:46:27 -0700 | [diff] [blame] | 963 | return; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 964 | } |
bungeman | 401ae2d | 2016-07-18 15:46:27 -0700 | [diff] [blame] | 965 | |
Ben Wagner | fc49734 | 2017-02-24 11:15:26 -0500 | [diff] [blame] | 966 | err = FT_Select_Size(fFaceRec->fFace.get(), fStrikeIndex); |
bungeman | 401ae2d | 2016-07-18 15:46:27 -0700 | [diff] [blame] | 967 | if (err != 0) { |
Hal Canary | 2bcd843 | 2018-01-26 10:35:07 -0500 | [diff] [blame] | 968 | SK_TRACEFTR(err, "FT_Select_Size(%s, %d) failed.", |
Ben Wagner | 082a7a7 | 2018-06-08 14:15:47 -0400 | [diff] [blame] | 969 | fFaceRec->fFace->family_name, fStrikeIndex); |
bungeman | 401ae2d | 2016-07-18 15:46:27 -0700 | [diff] [blame] | 970 | fStrikeIndex = -1; |
| 971 | return; |
| 972 | } |
| 973 | |
Ben Wagner | 082a7a7 | 2018-06-08 14:15:47 -0400 | [diff] [blame] | 974 | // Adjust the matrix to reflect the actually chosen scale. |
| 975 | // It is likely that the ppem chosen was not the one requested, this allows for scaling. |
Ben Wagner | fc49734 | 2017-02-24 11:15:26 -0500 | [diff] [blame] | 976 | fMatrix22Scalar.preScale(fScale.x() / fFaceRec->fFace->size->metrics.x_ppem, |
| 977 | fScale.y() / fFaceRec->fFace->size->metrics.y_ppem); |
bungeman | 401ae2d | 2016-07-18 15:46:27 -0700 | [diff] [blame] | 978 | |
| 979 | // FreeType does not provide linear metrics for bitmap fonts. |
| 980 | linearMetrics = false; |
| 981 | |
| 982 | // FreeType documentation says: |
| 983 | // FT_LOAD_NO_BITMAP -- Ignore bitmap strikes when loading. |
| 984 | // Bitmap-only fonts ignore this flag. |
| 985 | // |
| 986 | // However, in FreeType 2.5.1 color bitmap only fonts do not ignore this flag. |
| 987 | // Force this flag off for bitmap only fonts. |
| 988 | fLoadGlyphFlags &= ~FT_LOAD_NO_BITMAP; |
bungeman@google.com | c9a8a7e | 2013-12-10 18:09:36 +0000 | [diff] [blame] | 989 | } else { |
Mike Klein | 3e0141a | 2019-03-26 10:41:18 -0500 | [diff] [blame] | 990 | LOG_INFO("Unknown kind of font \"%s\" size %f.\n", fFaceRec->fFace->family_name, fScale.fY); |
bungeman | 401ae2d | 2016-07-18 15:46:27 -0700 | [diff] [blame] | 991 | return; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 992 | } |
bungeman@google.com | c9a8a7e | 2013-12-10 18:09:36 +0000 | [diff] [blame] | 993 | |
Ben Wagner | 082a7a7 | 2018-06-08 14:15:47 -0400 | [diff] [blame] | 994 | fMatrix22.xx = SkScalarToFixed(fMatrix22Scalar.getScaleX()); |
| 995 | fMatrix22.xy = SkScalarToFixed(-fMatrix22Scalar.getSkewX()); |
| 996 | fMatrix22.yx = SkScalarToFixed(-fMatrix22Scalar.getSkewY()); |
| 997 | fMatrix22.yy = SkScalarToFixed(fMatrix22Scalar.getScaleY()); |
| 998 | |
Bruce Wang | f3ca1c6 | 2018-07-09 10:08:36 -0400 | [diff] [blame] | 999 | #ifdef FT_COLOR_H |
| 1000 | FT_Palette_Select(fFaceRec->fFace.get(), 0, nullptr); |
| 1001 | #endif |
| 1002 | |
bungeman | aabd71c | 2016-03-01 15:15:09 -0800 | [diff] [blame] | 1003 | fFTSize = ftSize.release(); |
Ben Wagner | fc49734 | 2017-02-24 11:15:26 -0500 | [diff] [blame] | 1004 | fFace = fFaceRec->fFace.get(); |
bungeman@google.com | c9a8a7e | 2013-12-10 18:09:36 +0000 | [diff] [blame] | 1005 | fDoLinearMetrics = linearMetrics; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1006 | } |
| 1007 | |
| 1008 | SkScalerContext_FreeType::~SkScalerContext_FreeType() { |
scroggo@google.com | 94bc60f | 2012-10-04 20:45:06 +0000 | [diff] [blame] | 1009 | SkAutoMutexAcquire ac(gFTMutex); |
| 1010 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1011 | if (fFTSize != nullptr) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1012 | FT_Done_Size(fFTSize); |
| 1013 | } |
| 1014 | |
Ben Wagner | fc49734 | 2017-02-24 11:15:26 -0500 | [diff] [blame] | 1015 | fFaceRec = nullptr; |
bungeman | 9dc2468 | 2014-12-01 14:01:32 -0800 | [diff] [blame] | 1016 | |
| 1017 | unref_ft_library(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1018 | } |
| 1019 | |
| 1020 | /* We call this before each use of the fFace, since we may be sharing |
| 1021 | this face with other context (at different sizes). |
| 1022 | */ |
| 1023 | FT_Error SkScalerContext_FreeType::setupSize() { |
bungeman | 3f846ae | 2015-11-03 11:07:20 -0800 | [diff] [blame] | 1024 | gFTMutex.assertHeld(); |
bungeman@google.com | c9a8a7e | 2013-12-10 18:09:36 +0000 | [diff] [blame] | 1025 | FT_Error err = FT_Activate_Size(fFTSize); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1026 | if (err != 0) { |
bungeman@google.com | c9a8a7e | 2013-12-10 18:09:36 +0000 | [diff] [blame] | 1027 | return err; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1028 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1029 | FT_Set_Transform(fFace, &fMatrix22, nullptr); |
bungeman@google.com | c9a8a7e | 2013-12-10 18:09:36 +0000 | [diff] [blame] | 1030 | return 0; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1031 | } |
| 1032 | |
ctguil@chromium.org | 0bc7bf5 | 2011-03-04 19:04:57 +0000 | [diff] [blame] | 1033 | unsigned SkScalerContext_FreeType::generateGlyphCount() { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1034 | return fFace->num_glyphs; |
| 1035 | } |
| 1036 | |
Ben Wagner | e541645 | 2018-08-09 14:03:42 -0400 | [diff] [blame] | 1037 | bool SkScalerContext_FreeType::generateAdvance(SkGlyph* glyph) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1038 | /* unhinted and light hinted text have linearly scaled advances |
| 1039 | * which are very cheap to compute with some font formats... |
| 1040 | */ |
Ben Wagner | e541645 | 2018-08-09 14:03:42 -0400 | [diff] [blame] | 1041 | if (!fDoLinearMetrics) { |
| 1042 | return false; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1043 | } |
bungeman | 5ec443c | 2014-11-21 13:18:34 -0800 | [diff] [blame] | 1044 | |
Ben Wagner | e541645 | 2018-08-09 14:03:42 -0400 | [diff] [blame] | 1045 | SkAutoMutexAcquire ac(gFTMutex); |
| 1046 | |
| 1047 | if (this->setupSize()) { |
| 1048 | glyph->zeroMetrics(); |
| 1049 | return true; |
| 1050 | } |
| 1051 | |
| 1052 | FT_Error error; |
| 1053 | FT_Fixed advance; |
| 1054 | |
| 1055 | error = FT_Get_Advance( fFace, glyph->getGlyphID(), |
| 1056 | fLoadGlyphFlags | FT_ADVANCE_FLAG_FAST_ONLY, |
| 1057 | &advance ); |
| 1058 | |
| 1059 | if (error != 0) { |
| 1060 | return false; |
| 1061 | } |
| 1062 | |
| 1063 | const SkScalar advanceScalar = SkFT_FixedToScalar(advance); |
| 1064 | glyph->fAdvanceX = SkScalarToFloat(fMatrix22Scalar.getScaleX() * advanceScalar); |
| 1065 | glyph->fAdvanceY = SkScalarToFloat(fMatrix22Scalar.getSkewY() * advanceScalar); |
| 1066 | return true; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1067 | } |
| 1068 | |
Bruce Wang | f3ca1c6 | 2018-07-09 10:08:36 -0400 | [diff] [blame] | 1069 | void SkScalerContext_FreeType::getBBoxForCurrentGlyph(const SkGlyph* glyph, |
djsollen@google.com | d8b599c | 2012-03-19 19:44:19 +0000 | [diff] [blame] | 1070 | FT_BBox* bbox, |
| 1071 | bool snapToPixelBoundary) { |
| 1072 | |
| 1073 | FT_Outline_Get_CBox(&fFace->glyph->outline, bbox); |
| 1074 | |
Ben Wagner | aa166bd | 2018-08-14 14:58:18 -0400 | [diff] [blame] | 1075 | if (this->isSubpixel()) { |
george@mozilla.com | c59b5da | 2012-08-23 00:39:08 +0000 | [diff] [blame] | 1076 | int dx = SkFixedToFDot6(glyph->getSubXFixed()); |
| 1077 | int dy = SkFixedToFDot6(glyph->getSubYFixed()); |
djsollen@google.com | d8b599c | 2012-03-19 19:44:19 +0000 | [diff] [blame] | 1078 | // negate dy since freetype-y-goes-up and skia-y-goes-down |
| 1079 | bbox->xMin += dx; |
| 1080 | bbox->yMin -= dy; |
| 1081 | bbox->xMax += dx; |
| 1082 | bbox->yMax -= dy; |
| 1083 | } |
| 1084 | |
| 1085 | // outset the box to integral boundaries |
| 1086 | if (snapToPixelBoundary) { |
| 1087 | bbox->xMin &= ~63; |
| 1088 | bbox->yMin &= ~63; |
| 1089 | bbox->xMax = (bbox->xMax + 63) & ~63; |
| 1090 | bbox->yMax = (bbox->yMax + 63) & ~63; |
| 1091 | } |
bungeman@google.com | 8ff8a19 | 2012-09-25 20:38:28 +0000 | [diff] [blame] | 1092 | |
| 1093 | // Must come after snapToPixelBoundary so that the width and height are |
| 1094 | // consistent. Otherwise asserts will fire later on when generating the |
| 1095 | // glyph image. |
Ben Wagner | aa166bd | 2018-08-14 14:58:18 -0400 | [diff] [blame] | 1096 | if (this->isVertical()) { |
bungeman@google.com | 8ff8a19 | 2012-09-25 20:38:28 +0000 | [diff] [blame] | 1097 | FT_Vector vector; |
| 1098 | vector.x = fFace->glyph->metrics.vertBearingX - fFace->glyph->metrics.horiBearingX; |
| 1099 | vector.y = -fFace->glyph->metrics.vertBearingY - fFace->glyph->metrics.horiBearingY; |
| 1100 | FT_Vector_Transform(&vector, &fMatrix22); |
| 1101 | bbox->xMin += vector.x; |
| 1102 | bbox->xMax += vector.x; |
| 1103 | bbox->yMin += vector.y; |
| 1104 | bbox->yMax += vector.y; |
| 1105 | } |
djsollen@google.com | d8b599c | 2012-03-19 19:44:19 +0000 | [diff] [blame] | 1106 | } |
| 1107 | |
bungeman@google.com | cbe1b54 | 2013-12-16 17:02:39 +0000 | [diff] [blame] | 1108 | bool SkScalerContext_FreeType::getCBoxForLetter(char letter, FT_BBox* bbox) { |
| 1109 | const FT_UInt glyph_id = FT_Get_Char_Index(fFace, letter); |
bungeman | 5ec443c | 2014-11-21 13:18:34 -0800 | [diff] [blame] | 1110 | if (!glyph_id) { |
bungeman@google.com | cbe1b54 | 2013-12-16 17:02:39 +0000 | [diff] [blame] | 1111 | return false; |
bungeman | 5ec443c | 2014-11-21 13:18:34 -0800 | [diff] [blame] | 1112 | } |
| 1113 | if (FT_Load_Glyph(fFace, glyph_id, fLoadGlyphFlags) != 0) { |
bungeman@google.com | cbe1b54 | 2013-12-16 17:02:39 +0000 | [diff] [blame] | 1114 | return false; |
bungeman | 5ec443c | 2014-11-21 13:18:34 -0800 | [diff] [blame] | 1115 | } |
Ben Wagner | c3aef18 | 2017-06-26 12:47:33 -0400 | [diff] [blame] | 1116 | emboldenIfNeeded(fFace, fFace->glyph, SkTo<SkGlyphID>(glyph_id)); |
bungeman@google.com | cbe1b54 | 2013-12-16 17:02:39 +0000 | [diff] [blame] | 1117 | FT_Outline_Get_CBox(&fFace->glyph->outline, bbox); |
| 1118 | return true; |
| 1119 | } |
| 1120 | |
djsollen@google.com | d8b599c | 2012-03-19 19:44:19 +0000 | [diff] [blame] | 1121 | void SkScalerContext_FreeType::updateGlyphIfLCD(SkGlyph* glyph) { |
Ben Wagner | e541645 | 2018-08-09 14:03:42 -0400 | [diff] [blame] | 1122 | if (glyph->fMaskFormat == SkMask::kLCD16_Format) { |
djsollen@google.com | d8b599c | 2012-03-19 19:44:19 +0000 | [diff] [blame] | 1123 | if (fLCDIsVert) { |
bungeman | 9dc2468 | 2014-12-01 14:01:32 -0800 | [diff] [blame] | 1124 | glyph->fHeight += gFTLibrary->lcdExtra(); |
| 1125 | glyph->fTop -= gFTLibrary->lcdExtra() >> 1; |
djsollen@google.com | d8b599c | 2012-03-19 19:44:19 +0000 | [diff] [blame] | 1126 | } else { |
bungeman | 9dc2468 | 2014-12-01 14:01:32 -0800 | [diff] [blame] | 1127 | glyph->fWidth += gFTLibrary->lcdExtra(); |
| 1128 | glyph->fLeft -= gFTLibrary->lcdExtra() >> 1; |
djsollen@google.com | d8b599c | 2012-03-19 19:44:19 +0000 | [diff] [blame] | 1129 | } |
| 1130 | } |
| 1131 | } |
| 1132 | |
bungeman | 401ae2d | 2016-07-18 15:46:27 -0700 | [diff] [blame] | 1133 | bool SkScalerContext_FreeType::shouldSubpixelBitmap(const SkGlyph& glyph, const SkMatrix& matrix) { |
| 1134 | // If subpixel rendering of a bitmap *can* be done. |
| 1135 | bool mechanism = fFace->glyph->format == FT_GLYPH_FORMAT_BITMAP && |
Ben Wagner | aa166bd | 2018-08-14 14:58:18 -0400 | [diff] [blame] | 1136 | this->isSubpixel() && |
bungeman | 401ae2d | 2016-07-18 15:46:27 -0700 | [diff] [blame] | 1137 | (glyph.getSubXFixed() || glyph.getSubYFixed()); |
bungeman@google.com | c9a8a7e | 2013-12-10 18:09:36 +0000 | [diff] [blame] | 1138 | |
bungeman | 401ae2d | 2016-07-18 15:46:27 -0700 | [diff] [blame] | 1139 | // If subpixel rendering of a bitmap *should* be done. |
| 1140 | // 1. If the face is not scalable then always allow subpixel rendering. |
| 1141 | // Otherwise, if the font has an 8ppem strike 7 will subpixel render but 8 won't. |
| 1142 | // 2. If the matrix is already not identity the bitmap will already be resampled, |
| 1143 | // so resampling slightly differently shouldn't make much difference. |
| 1144 | bool policy = !FT_IS_SCALABLE(fFace) || !matrix.isIdentity(); |
| 1145 | |
| 1146 | return mechanism && policy; |
bungeman@google.com | c9a8a7e | 2013-12-10 18:09:36 +0000 | [diff] [blame] | 1147 | } |
| 1148 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1149 | void SkScalerContext_FreeType::generateMetrics(SkGlyph* glyph) { |
| 1150 | SkAutoMutexAcquire ac(gFTMutex); |
| 1151 | |
Ben Wagner | e541645 | 2018-08-09 14:03:42 -0400 | [diff] [blame] | 1152 | glyph->fMaskFormat = fRec.fMaskFormat; |
| 1153 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1154 | if (this->setupSize()) { |
bungeman | 13a007d | 2015-06-19 05:09:39 -0700 | [diff] [blame] | 1155 | glyph->zeroMetrics(); |
| 1156 | return; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1157 | } |
| 1158 | |
Bruce Wang | f3ca1c6 | 2018-07-09 10:08:36 -0400 | [diff] [blame] | 1159 | FT_Error err; |
Seigo Nonaka | 52ab2f5 | 2016-12-05 02:41:53 +0900 | [diff] [blame] | 1160 | err = FT_Load_Glyph( fFace, glyph->getGlyphID(), |
| 1161 | fLoadGlyphFlags | FT_LOAD_BITMAP_METRICS_ONLY ); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1162 | if (err != 0) { |
reed@android.com | 62900b4 | 2009-02-11 15:07:19 +0000 | [diff] [blame] | 1163 | glyph->zeroMetrics(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1164 | return; |
| 1165 | } |
Ben Wagner | c3aef18 | 2017-06-26 12:47:33 -0400 | [diff] [blame] | 1166 | emboldenIfNeeded(fFace, fFace->glyph, glyph->getGlyphID()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1167 | |
Ben Wagner | 094b3ea | 2018-09-06 18:09:29 -0400 | [diff] [blame] | 1168 | if (fFace->glyph->format == FT_GLYPH_FORMAT_OUTLINE) { |
| 1169 | using FT_PosLimits = std::numeric_limits<FT_Pos>; |
| 1170 | FT_BBox bounds = { FT_PosLimits::max(), FT_PosLimits::max(), |
| 1171 | FT_PosLimits::min(), FT_PosLimits::min() }; |
Bruce Wang | f3ca1c6 | 2018-07-09 10:08:36 -0400 | [diff] [blame] | 1172 | #ifdef FT_COLOR_H |
Ben Wagner | 094b3ea | 2018-09-06 18:09:29 -0400 | [diff] [blame] | 1173 | FT_Bool haveLayers = false; |
| 1174 | FT_LayerIterator layerIterator = { 0, 0, nullptr }; |
| 1175 | FT_UInt layerGlyphIndex; |
| 1176 | FT_UInt layerColorIndex; |
| 1177 | while (FT_Get_Color_Glyph_Layer(fFace, glyph->getGlyphID(), |
| 1178 | &layerGlyphIndex, &layerColorIndex, &layerIterator)) |
| 1179 | { |
| 1180 | haveLayers = true; |
| 1181 | err = FT_Load_Glyph(fFace, layerGlyphIndex, |
| 1182 | fLoadGlyphFlags | FT_LOAD_BITMAP_METRICS_ONLY); |
| 1183 | if (err != 0) { |
| 1184 | glyph->zeroMetrics(); |
| 1185 | return; |
Bruce Wang | f3ca1c6 | 2018-07-09 10:08:36 -0400 | [diff] [blame] | 1186 | } |
Ben Wagner | 094b3ea | 2018-09-06 18:09:29 -0400 | [diff] [blame] | 1187 | emboldenIfNeeded(fFace, fFace->glyph, layerGlyphIndex); |
Bruce Wang | f3ca1c6 | 2018-07-09 10:08:36 -0400 | [diff] [blame] | 1188 | |
Ben Wagner | 094b3ea | 2018-09-06 18:09:29 -0400 | [diff] [blame] | 1189 | if (0 < fFace->glyph->outline.n_contours) { |
| 1190 | FT_BBox bbox; |
Bruce Wang | f3ca1c6 | 2018-07-09 10:08:36 -0400 | [diff] [blame] | 1191 | getBBoxForCurrentGlyph(glyph, &bbox, true); |
| 1192 | |
Ben Wagner | 094b3ea | 2018-09-06 18:09:29 -0400 | [diff] [blame] | 1193 | // Union |
| 1194 | bounds.xMin = std::min(bbox.xMin, bounds.xMin); |
| 1195 | bounds.yMin = std::min(bbox.yMin, bounds.yMin); |
| 1196 | bounds.xMax = std::max(bbox.xMax, bounds.xMax); |
| 1197 | bounds.yMax = std::max(bbox.yMax, bounds.yMax); |
Bruce Wang | f3ca1c6 | 2018-07-09 10:08:36 -0400 | [diff] [blame] | 1198 | } |
bungeman@google.com | 0f0c288 | 2011-11-04 15:47:41 +0000 | [diff] [blame] | 1199 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1200 | |
Ben Wagner | 094b3ea | 2018-09-06 18:09:29 -0400 | [diff] [blame] | 1201 | if (haveLayers) { |
| 1202 | glyph->fMaskFormat = SkMask::kARGB32_Format; |
| 1203 | if (!(bounds.xMin < bounds.xMax && bounds.yMin < bounds.yMax)) { |
| 1204 | bounds = { 0, 0, 0, 0 }; |
| 1205 | } |
| 1206 | } else { |
| 1207 | #endif |
| 1208 | if (0 < fFace->glyph->outline.n_contours) { |
| 1209 | getBBoxForCurrentGlyph(glyph, &bounds, true); |
| 1210 | } else { |
| 1211 | bounds = { 0, 0, 0, 0 }; |
| 1212 | } |
| 1213 | #ifdef FT_COLOR_H |
| 1214 | } |
| 1215 | #endif |
| 1216 | // Round out, no longer dot6. |
| 1217 | bounds.xMin = SkFDot6Floor(bounds.xMin); |
| 1218 | bounds.yMin = SkFDot6Floor(bounds.yMin); |
| 1219 | bounds.xMax = SkFDot6Ceil (bounds.xMax); |
| 1220 | bounds.yMax = SkFDot6Ceil (bounds.yMax); |
| 1221 | |
| 1222 | FT_Pos width = bounds.xMax - bounds.xMin; |
| 1223 | FT_Pos height = bounds.yMax - bounds.yMin; |
| 1224 | FT_Pos top = -bounds.yMax; // Freetype y-up, Skia y-down. |
| 1225 | FT_Pos left = bounds.xMin; |
| 1226 | if (!SkTFitsIn<decltype(glyph->fWidth )>(width ) || |
| 1227 | !SkTFitsIn<decltype(glyph->fHeight)>(height) || |
| 1228 | !SkTFitsIn<decltype(glyph->fTop )>(top ) || |
| 1229 | !SkTFitsIn<decltype(glyph->fLeft )>(left ) ) |
| 1230 | { |
| 1231 | width = height = top = left = 0; |
| 1232 | } |
| 1233 | |
| 1234 | glyph->fWidth = SkToU16(width ); |
| 1235 | glyph->fHeight = SkToU16(height); |
| 1236 | glyph->fTop = SkToS16(top ); |
| 1237 | glyph->fLeft = SkToS16(left ); |
| 1238 | updateGlyphIfLCD(glyph); |
| 1239 | |
| 1240 | } else if (fFace->glyph->format == FT_GLYPH_FORMAT_BITMAP) { |
Ben Wagner | aa166bd | 2018-08-14 14:58:18 -0400 | [diff] [blame] | 1241 | if (this->isVertical()) { |
bungeman@google.com | 8ff8a19 | 2012-09-25 20:38:28 +0000 | [diff] [blame] | 1242 | FT_Vector vector; |
| 1243 | vector.x = fFace->glyph->metrics.vertBearingX - fFace->glyph->metrics.horiBearingX; |
| 1244 | vector.y = -fFace->glyph->metrics.vertBearingY - fFace->glyph->metrics.horiBearingY; |
| 1245 | FT_Vector_Transform(&vector, &fMatrix22); |
| 1246 | fFace->glyph->bitmap_left += SkFDot6Floor(vector.x); |
| 1247 | fFace->glyph->bitmap_top += SkFDot6Floor(vector.y); |
| 1248 | } |
| 1249 | |
bungeman@google.com | c9a8a7e | 2013-12-10 18:09:36 +0000 | [diff] [blame] | 1250 | if (fFace->glyph->bitmap.pixel_mode == FT_PIXEL_MODE_BGRA) { |
| 1251 | glyph->fMaskFormat = SkMask::kARGB32_Format; |
| 1252 | } |
| 1253 | |
bungeman | 401ae2d | 2016-07-18 15:46:27 -0700 | [diff] [blame] | 1254 | { |
| 1255 | SkRect rect = SkRect::MakeXYWH(SkIntToScalar(fFace->glyph->bitmap_left), |
| 1256 | -SkIntToScalar(fFace->glyph->bitmap_top), |
| 1257 | SkIntToScalar(fFace->glyph->bitmap.width), |
| 1258 | SkIntToScalar(fFace->glyph->bitmap.rows)); |
| 1259 | fMatrix22Scalar.mapRect(&rect); |
| 1260 | if (this->shouldSubpixelBitmap(*glyph, fMatrix22Scalar)) { |
| 1261 | rect.offset(SkFixedToScalar(glyph->getSubXFixed()), |
| 1262 | SkFixedToScalar(glyph->getSubYFixed())); |
| 1263 | } |
| 1264 | SkIRect irect = rect.roundOut(); |
| 1265 | glyph->fWidth = SkToU16(irect.width()); |
| 1266 | glyph->fHeight = SkToU16(irect.height()); |
| 1267 | glyph->fTop = SkToS16(irect.top()); |
| 1268 | glyph->fLeft = SkToS16(irect.left()); |
| 1269 | } |
Ben Wagner | 094b3ea | 2018-09-06 18:09:29 -0400 | [diff] [blame] | 1270 | } else { |
tomhudson@google.com | 0c00f21 | 2011-12-28 14:59:50 +0000 | [diff] [blame] | 1271 | SkDEBUGFAIL("unknown glyph format"); |
bungeman | 13a007d | 2015-06-19 05:09:39 -0700 | [diff] [blame] | 1272 | glyph->zeroMetrics(); |
| 1273 | return; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1274 | } |
| 1275 | |
Ben Wagner | aa166bd | 2018-08-14 14:58:18 -0400 | [diff] [blame] | 1276 | if (this->isVertical()) { |
bungeman@google.com | 8ff8a19 | 2012-09-25 20:38:28 +0000 | [diff] [blame] | 1277 | if (fDoLinearMetrics) { |
benjaminwagner | 6b3eacb | 2016-03-24 19:07:58 -0700 | [diff] [blame] | 1278 | const SkScalar advanceScalar = SkFT_FixedToScalar(fFace->glyph->linearVertAdvance); |
bungeman | 401ae2d | 2016-07-18 15:46:27 -0700 | [diff] [blame] | 1279 | glyph->fAdvanceX = SkScalarToFloat(fMatrix22Scalar.getSkewX() * advanceScalar); |
benjaminwagner | 6b3eacb | 2016-03-24 19:07:58 -0700 | [diff] [blame] | 1280 | glyph->fAdvanceY = SkScalarToFloat(fMatrix22Scalar.getScaleY() * advanceScalar); |
bungeman@google.com | 8ff8a19 | 2012-09-25 20:38:28 +0000 | [diff] [blame] | 1281 | } else { |
benjaminwagner | 6b3eacb | 2016-03-24 19:07:58 -0700 | [diff] [blame] | 1282 | glyph->fAdvanceX = -SkFDot6ToFloat(fFace->glyph->advance.x); |
| 1283 | glyph->fAdvanceY = SkFDot6ToFloat(fFace->glyph->advance.y); |
bungeman@google.com | 8ff8a19 | 2012-09-25 20:38:28 +0000 | [diff] [blame] | 1284 | } |
bungeman@google.com | 34f1026 | 2012-03-23 18:11:47 +0000 | [diff] [blame] | 1285 | } else { |
bungeman@google.com | 8ff8a19 | 2012-09-25 20:38:28 +0000 | [diff] [blame] | 1286 | if (fDoLinearMetrics) { |
benjaminwagner | 6b3eacb | 2016-03-24 19:07:58 -0700 | [diff] [blame] | 1287 | const SkScalar advanceScalar = SkFT_FixedToScalar(fFace->glyph->linearHoriAdvance); |
| 1288 | glyph->fAdvanceX = SkScalarToFloat(fMatrix22Scalar.getScaleX() * advanceScalar); |
bungeman | 401ae2d | 2016-07-18 15:46:27 -0700 | [diff] [blame] | 1289 | glyph->fAdvanceY = SkScalarToFloat(fMatrix22Scalar.getSkewY() * advanceScalar); |
bungeman@google.com | 8ff8a19 | 2012-09-25 20:38:28 +0000 | [diff] [blame] | 1290 | } else { |
benjaminwagner | 6b3eacb | 2016-03-24 19:07:58 -0700 | [diff] [blame] | 1291 | glyph->fAdvanceX = SkFDot6ToFloat(fFace->glyph->advance.x); |
| 1292 | glyph->fAdvanceY = -SkFDot6ToFloat(fFace->glyph->advance.y); |
djsollen@google.com | d8b599c | 2012-03-19 19:44:19 +0000 | [diff] [blame] | 1293 | } |
djsollen@google.com | d8b599c | 2012-03-19 19:44:19 +0000 | [diff] [blame] | 1294 | } |
| 1295 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1296 | #ifdef ENABLE_GLYPH_SPEW |
Mike Klein | 3e0141a | 2019-03-26 10:41:18 -0500 | [diff] [blame] | 1297 | LOG_INFO("Metrics(glyph:%d flags:0x%x) w:%d\n", glyph->getGlyphID(), fLoadGlyphFlags, glyph->fWidth); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1298 | #endif |
| 1299 | } |
| 1300 | |
bungeman | 5ec443c | 2014-11-21 13:18:34 -0800 | [diff] [blame] | 1301 | static void clear_glyph_image(const SkGlyph& glyph) { |
| 1302 | sk_bzero(glyph.fImage, glyph.rowBytes() * glyph.fHeight); |
| 1303 | } |
reed@google.com | ea2333d | 2011-03-14 16:44:56 +0000 | [diff] [blame] | 1304 | |
bungeman@google.com | a76de72 | 2012-10-26 19:35:54 +0000 | [diff] [blame] | 1305 | void SkScalerContext_FreeType::generateImage(const SkGlyph& glyph) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1306 | SkAutoMutexAcquire ac(gFTMutex); |
| 1307 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1308 | if (this->setupSize()) { |
bungeman | 5ec443c | 2014-11-21 13:18:34 -0800 | [diff] [blame] | 1309 | clear_glyph_image(glyph); |
| 1310 | return; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1311 | } |
| 1312 | |
bungeman | 5ec443c | 2014-11-21 13:18:34 -0800 | [diff] [blame] | 1313 | FT_Error err = FT_Load_Glyph(fFace, glyph.getGlyphID(), fLoadGlyphFlags); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1314 | if (err != 0) { |
Hal Canary | 2bcd843 | 2018-01-26 10:35:07 -0500 | [diff] [blame] | 1315 | SK_TRACEFTR(err, "SkScalerContext_FreeType::generateImage: FT_Load_Glyph(glyph:%d " |
| 1316 | "width:%d height:%d rb:%d flags:%d) failed.", |
| 1317 | glyph.getGlyphID(), glyph.fWidth, glyph.fHeight, glyph.rowBytes(), |
| 1318 | fLoadGlyphFlags); |
bungeman | 5ec443c | 2014-11-21 13:18:34 -0800 | [diff] [blame] | 1319 | clear_glyph_image(glyph); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1320 | return; |
| 1321 | } |
| 1322 | |
Ben Wagner | c3aef18 | 2017-06-26 12:47:33 -0400 | [diff] [blame] | 1323 | emboldenIfNeeded(fFace, fFace->glyph, glyph.getGlyphID()); |
bungeman | 401ae2d | 2016-07-18 15:46:27 -0700 | [diff] [blame] | 1324 | SkMatrix* bitmapMatrix = &fMatrix22Scalar; |
| 1325 | SkMatrix subpixelBitmapMatrix; |
| 1326 | if (this->shouldSubpixelBitmap(glyph, *bitmapMatrix)) { |
| 1327 | subpixelBitmapMatrix = fMatrix22Scalar; |
| 1328 | subpixelBitmapMatrix.postTranslate(SkFixedToScalar(glyph.getSubXFixed()), |
| 1329 | SkFixedToScalar(glyph.getSubYFixed())); |
| 1330 | bitmapMatrix = &subpixelBitmapMatrix; |
| 1331 | } |
| 1332 | generateGlyphImage(fFace, glyph, *bitmapMatrix); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1333 | } |
| 1334 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1335 | |
Ben Wagner | 5ddb308 | 2018-03-29 11:18:06 -0400 | [diff] [blame] | 1336 | bool SkScalerContext_FreeType::generatePath(SkGlyphID glyphID, SkPath* path) { |
caryclark | a10742c | 2014-09-18 11:00:40 -0700 | [diff] [blame] | 1337 | SkASSERT(path); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1338 | |
Ben Wagner | 5ddb308 | 2018-03-29 11:18:06 -0400 | [diff] [blame] | 1339 | SkAutoMutexAcquire ac(gFTMutex); |
| 1340 | |
Ben Wagner | d6931bb | 2018-09-18 14:38:32 -0400 | [diff] [blame] | 1341 | // FT_IS_SCALABLE is documented to mean the face contains outline glyphs. |
| 1342 | if (!FT_IS_SCALABLE(fFace) || this->setupSize()) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1343 | path->reset(); |
Ben Wagner | 5ddb308 | 2018-03-29 11:18:06 -0400 | [diff] [blame] | 1344 | return false; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1345 | } |
| 1346 | |
| 1347 | uint32_t flags = fLoadGlyphFlags; |
| 1348 | flags |= FT_LOAD_NO_BITMAP; // ignore embedded bitmaps so we're sure to get the outline |
| 1349 | flags &= ~FT_LOAD_RENDER; // don't scan convert (we just want the outline) |
| 1350 | |
Ben Wagner | 6e9ac12 | 2016-11-11 14:31:06 -0500 | [diff] [blame] | 1351 | FT_Error err = FT_Load_Glyph(fFace, glyphID, flags); |
Ben Wagner | d6931bb | 2018-09-18 14:38:32 -0400 | [diff] [blame] | 1352 | if (err != 0 || fFace->glyph->format != FT_GLYPH_FORMAT_OUTLINE) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1353 | path->reset(); |
Ben Wagner | 5ddb308 | 2018-03-29 11:18:06 -0400 | [diff] [blame] | 1354 | return false; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1355 | } |
Ben Wagner | c3aef18 | 2017-06-26 12:47:33 -0400 | [diff] [blame] | 1356 | emboldenIfNeeded(fFace, fFace->glyph, glyphID); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1357 | |
Ben Wagner | 5ddb308 | 2018-03-29 11:18:06 -0400 | [diff] [blame] | 1358 | if (!generateGlyphPath(fFace, path)) { |
| 1359 | path->reset(); |
| 1360 | return false; |
| 1361 | } |
bungeman@google.com | 8ff8a19 | 2012-09-25 20:38:28 +0000 | [diff] [blame] | 1362 | |
| 1363 | // The path's origin from FreeType is always the horizontal layout origin. |
| 1364 | // Offset the path so that it is relative to the vertical origin if needed. |
Ben Wagner | aa166bd | 2018-08-14 14:58:18 -0400 | [diff] [blame] | 1365 | if (this->isVertical()) { |
bungeman@google.com | 8ff8a19 | 2012-09-25 20:38:28 +0000 | [diff] [blame] | 1366 | FT_Vector vector; |
| 1367 | vector.x = fFace->glyph->metrics.vertBearingX - fFace->glyph->metrics.horiBearingX; |
| 1368 | vector.y = -fFace->glyph->metrics.vertBearingY - fFace->glyph->metrics.horiBearingY; |
| 1369 | FT_Vector_Transform(&vector, &fMatrix22); |
| 1370 | path->offset(SkFDot6ToScalar(vector.x), -SkFDot6ToScalar(vector.y)); |
| 1371 | } |
Ben Wagner | 5ddb308 | 2018-03-29 11:18:06 -0400 | [diff] [blame] | 1372 | return true; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1373 | } |
| 1374 | |
Mike Reed | b5784ac | 2018-11-12 09:35:15 -0500 | [diff] [blame] | 1375 | void SkScalerContext_FreeType::generateFontMetrics(SkFontMetrics* metrics) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1376 | if (nullptr == metrics) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1377 | return; |
| 1378 | } |
| 1379 | |
bungeman | 4107806 | 2014-07-07 08:16:37 -0700 | [diff] [blame] | 1380 | SkAutoMutexAcquire ac(gFTMutex); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1381 | |
| 1382 | if (this->setupSize()) { |
bungeman | 4107806 | 2014-07-07 08:16:37 -0700 | [diff] [blame] | 1383 | sk_bzero(metrics, sizeof(*metrics)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1384 | return; |
| 1385 | } |
| 1386 | |
reed@android.com | a8a8b8b | 2009-05-04 15:00:11 +0000 | [diff] [blame] | 1387 | FT_Face face = fFace; |
Ben Wagner | 219f362 | 2017-07-17 15:32:25 -0400 | [diff] [blame] | 1388 | metrics->fFlags = 0; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1389 | |
Ben Wagner | 26a005a | 2018-08-31 17:41:45 -0400 | [diff] [blame] | 1390 | SkScalar upem = SkIntToScalar(SkTypeface_FreeType::GetUnitsPerEm(face)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1391 | |
bungeman@google.com | c9a8a7e | 2013-12-10 18:09:36 +0000 | [diff] [blame] | 1392 | // use the os/2 table as a source of reasonable defaults. |
| 1393 | SkScalar x_height = 0.0f; |
| 1394 | SkScalar avgCharWidth = 0.0f; |
bungeman@google.com | cbe1b54 | 2013-12-16 17:02:39 +0000 | [diff] [blame] | 1395 | SkScalar cap_height = 0.0f; |
Kevin Lubick | 4284613 | 2018-01-05 10:11:11 -0500 | [diff] [blame] | 1396 | SkScalar strikeoutThickness = 0.0f, strikeoutPosition = 0.0f; |
bungeman@google.com | c9a8a7e | 2013-12-10 18:09:36 +0000 | [diff] [blame] | 1397 | TT_OS2* os2 = (TT_OS2*) FT_Get_Sfnt_Table(face, ft_sfnt_os2); |
| 1398 | if (os2) { |
bungeman | 5379051 | 2016-07-21 13:32:09 -0700 | [diff] [blame] | 1399 | x_height = SkIntToScalar(os2->sxHeight) / upem * fScale.y(); |
bungeman@google.com | c9a8a7e | 2013-12-10 18:09:36 +0000 | [diff] [blame] | 1400 | avgCharWidth = SkIntToScalar(os2->xAvgCharWidth) / upem; |
Ben Wagner | 219f362 | 2017-07-17 15:32:25 -0400 | [diff] [blame] | 1401 | strikeoutThickness = SkIntToScalar(os2->yStrikeoutSize) / upem; |
| 1402 | strikeoutPosition = -SkIntToScalar(os2->yStrikeoutPosition) / upem; |
Mike Reed | b5784ac | 2018-11-12 09:35:15 -0500 | [diff] [blame] | 1403 | metrics->fFlags |= SkFontMetrics::kStrikeoutThicknessIsValid_Flag; |
| 1404 | metrics->fFlags |= SkFontMetrics::kStrikeoutPositionIsValid_Flag; |
bungeman@google.com | cbe1b54 | 2013-12-16 17:02:39 +0000 | [diff] [blame] | 1405 | if (os2->version != 0xFFFF && os2->version >= 2) { |
bungeman | 5379051 | 2016-07-21 13:32:09 -0700 | [diff] [blame] | 1406 | cap_height = SkIntToScalar(os2->sCapHeight) / upem * fScale.y(); |
bungeman@google.com | cbe1b54 | 2013-12-16 17:02:39 +0000 | [diff] [blame] | 1407 | } |
bungeman@google.com | c9a8a7e | 2013-12-10 18:09:36 +0000 | [diff] [blame] | 1408 | } |
| 1409 | |
| 1410 | // pull from format-specific metrics as needed |
| 1411 | SkScalar ascent, descent, leading, xmin, xmax, ymin, ymax; |
commit-bot@chromium.org | 0bc406d | 2014-03-01 20:12:26 +0000 | [diff] [blame] | 1412 | SkScalar underlineThickness, underlinePosition; |
bungeman@google.com | c9a8a7e | 2013-12-10 18:09:36 +0000 | [diff] [blame] | 1413 | if (face->face_flags & FT_FACE_FLAG_SCALABLE) { // scalable outline font |
bungeman | 665b038 | 2015-03-19 10:43:57 -0700 | [diff] [blame] | 1414 | // FreeType will always use HHEA metrics if they're not zero. |
| 1415 | // It completely ignores the OS/2 fsSelection::UseTypoMetrics bit. |
| 1416 | // It also ignores the VDMX tables, which are also of interest here |
| 1417 | // (and override everything else when they apply). |
| 1418 | static const int kUseTypoMetricsMask = (1 << 7); |
| 1419 | if (os2 && os2->version != 0xFFFF && (os2->fsSelection & kUseTypoMetricsMask)) { |
| 1420 | ascent = -SkIntToScalar(os2->sTypoAscender) / upem; |
| 1421 | descent = -SkIntToScalar(os2->sTypoDescender) / upem; |
| 1422 | leading = SkIntToScalar(os2->sTypoLineGap) / upem; |
| 1423 | } else { |
| 1424 | ascent = -SkIntToScalar(face->ascender) / upem; |
| 1425 | descent = -SkIntToScalar(face->descender) / upem; |
| 1426 | leading = SkIntToScalar(face->height + (face->descender - face->ascender)) / upem; |
| 1427 | } |
bungeman@google.com | c9a8a7e | 2013-12-10 18:09:36 +0000 | [diff] [blame] | 1428 | xmin = SkIntToScalar(face->bbox.xMin) / upem; |
| 1429 | xmax = SkIntToScalar(face->bbox.xMax) / upem; |
| 1430 | ymin = -SkIntToScalar(face->bbox.yMin) / upem; |
| 1431 | ymax = -SkIntToScalar(face->bbox.yMax) / upem; |
commit-bot@chromium.org | 0bc406d | 2014-03-01 20:12:26 +0000 | [diff] [blame] | 1432 | underlineThickness = SkIntToScalar(face->underline_thickness) / upem; |
commit-bot@chromium.org | d3031aa | 2014-05-14 14:54:51 +0000 | [diff] [blame] | 1433 | underlinePosition = -SkIntToScalar(face->underline_position + |
| 1434 | face->underline_thickness / 2) / upem; |
commit-bot@chromium.org | 0bc406d | 2014-03-01 20:12:26 +0000 | [diff] [blame] | 1435 | |
Mike Reed | b5784ac | 2018-11-12 09:35:15 -0500 | [diff] [blame] | 1436 | metrics->fFlags |= SkFontMetrics::kUnderlineThicknessIsValid_Flag; |
| 1437 | metrics->fFlags |= SkFontMetrics::kUnderlinePositionIsValid_Flag; |
bungeman | 4107806 | 2014-07-07 08:16:37 -0700 | [diff] [blame] | 1438 | |
bungeman@google.com | cbe1b54 | 2013-12-16 17:02:39 +0000 | [diff] [blame] | 1439 | // we may be able to synthesize x_height and cap_height from outline |
bungeman@google.com | c9a8a7e | 2013-12-10 18:09:36 +0000 | [diff] [blame] | 1440 | if (!x_height) { |
bungeman@google.com | cbe1b54 | 2013-12-16 17:02:39 +0000 | [diff] [blame] | 1441 | FT_BBox bbox; |
| 1442 | if (getCBoxForLetter('x', &bbox)) { |
bungeman@google.com | c9a8a7e | 2013-12-10 18:09:36 +0000 | [diff] [blame] | 1443 | x_height = SkIntToScalar(bbox.yMax) / 64.0f; |
| 1444 | } |
| 1445 | } |
bungeman@google.com | cbe1b54 | 2013-12-16 17:02:39 +0000 | [diff] [blame] | 1446 | if (!cap_height) { |
| 1447 | FT_BBox bbox; |
| 1448 | if (getCBoxForLetter('H', &bbox)) { |
| 1449 | cap_height = SkIntToScalar(bbox.yMax) / 64.0f; |
| 1450 | } |
| 1451 | } |
bungeman@google.com | c9a8a7e | 2013-12-10 18:09:36 +0000 | [diff] [blame] | 1452 | } else if (fStrikeIndex != -1) { // bitmap strike metrics |
| 1453 | SkScalar xppem = SkIntToScalar(face->size->metrics.x_ppem); |
| 1454 | SkScalar yppem = SkIntToScalar(face->size->metrics.y_ppem); |
| 1455 | ascent = -SkIntToScalar(face->size->metrics.ascender) / (yppem * 64.0f); |
| 1456 | descent = -SkIntToScalar(face->size->metrics.descender) / (yppem * 64.0f); |
bungeman | 5379051 | 2016-07-21 13:32:09 -0700 | [diff] [blame] | 1457 | leading = (SkIntToScalar(face->size->metrics.height) / (yppem * 64.0f)) + ascent - descent; |
bungeman@google.com | c9a8a7e | 2013-12-10 18:09:36 +0000 | [diff] [blame] | 1458 | xmin = 0.0f; |
| 1459 | xmax = SkIntToScalar(face->available_sizes[fStrikeIndex].width) / xppem; |
Ben Wagner | 5423f1f | 2018-02-20 09:57:58 -0500 | [diff] [blame] | 1460 | ymin = descent; |
| 1461 | ymax = ascent; |
commit-bot@chromium.org | 0bc406d | 2014-03-01 20:12:26 +0000 | [diff] [blame] | 1462 | underlineThickness = 0; |
| 1463 | underlinePosition = 0; |
Mike Reed | b5784ac | 2018-11-12 09:35:15 -0500 | [diff] [blame] | 1464 | metrics->fFlags &= ~SkFontMetrics::kUnderlineThicknessIsValid_Flag; |
| 1465 | metrics->fFlags &= ~SkFontMetrics::kUnderlinePositionIsValid_Flag; |
Ben Wagner | 5423f1f | 2018-02-20 09:57:58 -0500 | [diff] [blame] | 1466 | |
| 1467 | TT_Postscript* post = (TT_Postscript*) FT_Get_Sfnt_Table(face, ft_sfnt_post); |
| 1468 | if (post) { |
| 1469 | underlineThickness = SkIntToScalar(post->underlineThickness) / upem; |
| 1470 | underlinePosition = -SkIntToScalar(post->underlinePosition) / upem; |
Mike Reed | b5784ac | 2018-11-12 09:35:15 -0500 | [diff] [blame] | 1471 | metrics->fFlags |= SkFontMetrics::kUnderlineThicknessIsValid_Flag; |
| 1472 | metrics->fFlags |= SkFontMetrics::kUnderlinePositionIsValid_Flag; |
Ben Wagner | 5423f1f | 2018-02-20 09:57:58 -0500 | [diff] [blame] | 1473 | } |
bungeman@google.com | c9a8a7e | 2013-12-10 18:09:36 +0000 | [diff] [blame] | 1474 | } else { |
caryclark | fe7ada7 | 2016-03-21 06:55:52 -0700 | [diff] [blame] | 1475 | sk_bzero(metrics, sizeof(*metrics)); |
| 1476 | return; |
bungeman@google.com | c9a8a7e | 2013-12-10 18:09:36 +0000 | [diff] [blame] | 1477 | } |
| 1478 | |
| 1479 | // synthesize elements that were not provided by the os/2 table or format-specific metrics |
| 1480 | if (!x_height) { |
bungeman | 5379051 | 2016-07-21 13:32:09 -0700 | [diff] [blame] | 1481 | x_height = -ascent * fScale.y(); |
bungeman@google.com | c9a8a7e | 2013-12-10 18:09:36 +0000 | [diff] [blame] | 1482 | } |
| 1483 | if (!avgCharWidth) { |
| 1484 | avgCharWidth = xmax - xmin; |
| 1485 | } |
bungeman@google.com | cbe1b54 | 2013-12-16 17:02:39 +0000 | [diff] [blame] | 1486 | if (!cap_height) { |
bungeman | 5379051 | 2016-07-21 13:32:09 -0700 | [diff] [blame] | 1487 | cap_height = -ascent * fScale.y(); |
bungeman@google.com | cbe1b54 | 2013-12-16 17:02:39 +0000 | [diff] [blame] | 1488 | } |
bungeman@google.com | c9a8a7e | 2013-12-10 18:09:36 +0000 | [diff] [blame] | 1489 | |
| 1490 | // disallow negative linespacing |
| 1491 | if (leading < 0.0f) { |
| 1492 | leading = 0.0f; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1493 | } |
| 1494 | |
bungeman | 5379051 | 2016-07-21 13:32:09 -0700 | [diff] [blame] | 1495 | metrics->fTop = ymax * fScale.y(); |
| 1496 | metrics->fAscent = ascent * fScale.y(); |
| 1497 | metrics->fDescent = descent * fScale.y(); |
| 1498 | metrics->fBottom = ymin * fScale.y(); |
| 1499 | metrics->fLeading = leading * fScale.y(); |
| 1500 | metrics->fAvgCharWidth = avgCharWidth * fScale.y(); |
| 1501 | metrics->fXMin = xmin * fScale.y(); |
| 1502 | metrics->fXMax = xmax * fScale.y(); |
Ben Wagner | 219f362 | 2017-07-17 15:32:25 -0400 | [diff] [blame] | 1503 | metrics->fMaxCharWidth = metrics->fXMax - metrics->fXMin; |
bungeman | 7316b10 | 2014-10-29 12:46:52 -0700 | [diff] [blame] | 1504 | metrics->fXHeight = x_height; |
| 1505 | metrics->fCapHeight = cap_height; |
bungeman | 5379051 | 2016-07-21 13:32:09 -0700 | [diff] [blame] | 1506 | metrics->fUnderlineThickness = underlineThickness * fScale.y(); |
| 1507 | metrics->fUnderlinePosition = underlinePosition * fScale.y(); |
Ben Wagner | 219f362 | 2017-07-17 15:32:25 -0400 | [diff] [blame] | 1508 | metrics->fStrikeoutThickness = strikeoutThickness * fScale.y(); |
| 1509 | metrics->fStrikeoutPosition = strikeoutPosition * fScale.y(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1510 | } |
| 1511 | |
djsollen | fcfea99 | 2015-01-09 08:18:13 -0800 | [diff] [blame] | 1512 | /////////////////////////////////////////////////////////////////////////////// |
| 1513 | |
| 1514 | // hand-tuned value to reduce outline embolden strength |
| 1515 | #ifndef SK_OUTLINE_EMBOLDEN_DIVISOR |
| 1516 | #ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK |
| 1517 | #define SK_OUTLINE_EMBOLDEN_DIVISOR 34 |
| 1518 | #else |
| 1519 | #define SK_OUTLINE_EMBOLDEN_DIVISOR 24 |
| 1520 | #endif |
| 1521 | #endif |
| 1522 | |
| 1523 | /////////////////////////////////////////////////////////////////////////////// |
| 1524 | |
Ben Wagner | c3aef18 | 2017-06-26 12:47:33 -0400 | [diff] [blame] | 1525 | void SkScalerContext_FreeType::emboldenIfNeeded(FT_Face face, FT_GlyphSlot glyph, SkGlyphID gid) { |
commit-bot@chromium.org | 921d2b3 | 2014-04-01 19:03:07 +0000 | [diff] [blame] | 1526 | // check to see if the embolden bit is set |
| 1527 | if (0 == (fRec.fFlags & SkScalerContext::kEmbolden_Flag)) { |
| 1528 | return; |
| 1529 | } |
| 1530 | |
commit-bot@chromium.org | 921d2b3 | 2014-04-01 19:03:07 +0000 | [diff] [blame] | 1531 | switch (glyph->format) { |
| 1532 | case FT_GLYPH_FORMAT_OUTLINE: |
| 1533 | FT_Pos strength; |
djsollen | fcfea99 | 2015-01-09 08:18:13 -0800 | [diff] [blame] | 1534 | strength = FT_MulFix(face->units_per_EM, face->size->metrics.y_scale) |
| 1535 | / SK_OUTLINE_EMBOLDEN_DIVISOR; |
commit-bot@chromium.org | 921d2b3 | 2014-04-01 19:03:07 +0000 | [diff] [blame] | 1536 | FT_Outline_Embolden(&glyph->outline, strength); |
| 1537 | break; |
| 1538 | case FT_GLYPH_FORMAT_BITMAP: |
Ben Wagner | c3aef18 | 2017-06-26 12:47:33 -0400 | [diff] [blame] | 1539 | if (!fFace->glyph->bitmap.buffer) { |
| 1540 | FT_Load_Glyph(fFace, gid, fLoadGlyphFlags); |
| 1541 | } |
commit-bot@chromium.org | 921d2b3 | 2014-04-01 19:03:07 +0000 | [diff] [blame] | 1542 | FT_GlyphSlot_Own_Bitmap(glyph); |
| 1543 | FT_Bitmap_Embolden(glyph->library, &glyph->bitmap, kBitmapEmboldenStrength, 0); |
| 1544 | break; |
| 1545 | default: |
| 1546 | SkDEBUGFAIL("unknown glyph format"); |
commit-bot@chromium.org | 6fa81d7 | 2013-12-26 15:50:29 +0000 | [diff] [blame] | 1547 | } |
| 1548 | } |
| 1549 | |
reed@google.com | b4162b1 | 2013-07-02 16:32:29 +0000 | [diff] [blame] | 1550 | /////////////////////////////////////////////////////////////////////////////// |
| 1551 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 1552 | #include "src/core/SkUtils.h" |
reed@google.com | b4162b1 | 2013-07-02 16:32:29 +0000 | [diff] [blame] | 1553 | |
Mike Reed | b07e9a8 | 2019-04-19 10:02:10 -0400 | [diff] [blame] | 1554 | // Just made up, so we don't end up storing 1000s of entries |
| 1555 | constexpr int kMaxC2GCacheCount = 512; |
| 1556 | |
Mike Reed | 64670cb | 2019-04-16 11:37:38 -0700 | [diff] [blame] | 1557 | void SkTypeface_FreeType::onCharsToGlyphs(const SkUnichar uni[], int count, |
| 1558 | SkGlyphID glyphs[]) const { |
Mike Reed | b07e9a8 | 2019-04-19 10:02:10 -0400 | [diff] [blame] | 1559 | // Try the cache first, *before* accessing freetype lib/face, as that |
| 1560 | // can be very slow. If we do need to compute a new glyphID, then |
| 1561 | // access those freetype objects and continue the loop. |
| 1562 | |
| 1563 | SkAutoMutexAcquire ama(fC2GCacheMutex); |
| 1564 | |
| 1565 | int i; |
| 1566 | for (i = 0; i < count; ++i) { |
| 1567 | int index = fC2GCache.findGlyphIndex(uni[i]); |
| 1568 | if (index < 0) { |
| 1569 | break; |
| 1570 | } |
| 1571 | glyphs[i] = SkToU16(index); |
| 1572 | } |
| 1573 | if (i == count) { |
| 1574 | // we're done, no need to access the freetype objects |
| 1575 | return; |
| 1576 | } |
| 1577 | |
reed@google.com | b4162b1 | 2013-07-02 16:32:29 +0000 | [diff] [blame] | 1578 | AutoFTAccess fta(this); |
| 1579 | FT_Face face = fta.face(); |
| 1580 | if (!face) { |
Mike Reed | 64670cb | 2019-04-16 11:37:38 -0700 | [diff] [blame] | 1581 | sk_bzero(glyphs, count * sizeof(glyphs[0])); |
| 1582 | return; |
reed@google.com | b4162b1 | 2013-07-02 16:32:29 +0000 | [diff] [blame] | 1583 | } |
| 1584 | |
Mike Reed | b07e9a8 | 2019-04-19 10:02:10 -0400 | [diff] [blame] | 1585 | for (; i < count; ++i) { |
| 1586 | SkUnichar c = uni[i]; |
| 1587 | int index = fC2GCache.findGlyphIndex(c); |
| 1588 | if (index >= 0) { |
| 1589 | glyphs[i] = SkToU16(index); |
| 1590 | } else { |
| 1591 | glyphs[i] = SkToU16(FT_Get_Char_Index(face, c)); |
| 1592 | fC2GCache.insertCharAndGlyph(~index, c, glyphs[i]); |
| 1593 | } |
| 1594 | } |
| 1595 | |
| 1596 | if (fC2GCache.count() > kMaxC2GCacheCount) { |
| 1597 | fC2GCache.reset(); |
reed@google.com | b4162b1 | 2013-07-02 16:32:29 +0000 | [diff] [blame] | 1598 | } |
| 1599 | } |
| 1600 | |
| 1601 | int SkTypeface_FreeType::onCountGlyphs() const { |
bungeman | 572f879 | 2016-04-29 15:05:02 -0700 | [diff] [blame] | 1602 | AutoFTAccess fta(this); |
| 1603 | FT_Face face = fta.face(); |
| 1604 | return face ? face->num_glyphs : 0; |
reed@google.com | b4162b1 | 2013-07-02 16:32:29 +0000 | [diff] [blame] | 1605 | } |
| 1606 | |
bungeman@google.com | 839702b | 2013-08-07 17:09:22 +0000 | [diff] [blame] | 1607 | SkTypeface::LocalizedStrings* SkTypeface_FreeType::onCreateFamilyNameIterator() const { |
Ben Wagner | ad031f5 | 2018-08-20 13:45:57 -0400 | [diff] [blame] | 1608 | sk_sp<SkTypeface::LocalizedStrings> nameIter = |
| 1609 | SkOTUtils::LocalizedStrings_NameTable::MakeForFamilyNames(*this); |
| 1610 | if (!nameIter) { |
bungeman@google.com | a980269 | 2013-08-07 02:45:25 +0000 | [diff] [blame] | 1611 | SkString familyName; |
| 1612 | this->getFamilyName(&familyName); |
| 1613 | SkString language("und"); //undetermined |
Ben Wagner | ad031f5 | 2018-08-20 13:45:57 -0400 | [diff] [blame] | 1614 | nameIter = sk_make_sp<SkOTUtils::LocalizedStrings_SingleName>(familyName, language); |
bungeman@google.com | a980269 | 2013-08-07 02:45:25 +0000 | [diff] [blame] | 1615 | } |
Ben Wagner | ad031f5 | 2018-08-20 13:45:57 -0400 | [diff] [blame] | 1616 | return nameIter.release(); |
bungeman@google.com | a980269 | 2013-08-07 02:45:25 +0000 | [diff] [blame] | 1617 | } |
| 1618 | |
Ben Wagner | fc49734 | 2017-02-24 11:15:26 -0500 | [diff] [blame] | 1619 | int SkTypeface_FreeType::onGetVariationDesignPosition( |
Ben Wagner | e346b1e | 2018-06-26 11:22:37 -0400 | [diff] [blame] | 1620 | SkFontArguments::VariationPosition::Coordinate coordinates[], int coordinateCount) const |
Ben Wagner | fc49734 | 2017-02-24 11:15:26 -0500 | [diff] [blame] | 1621 | { |
| 1622 | AutoFTAccess fta(this); |
| 1623 | FT_Face face = fta.face(); |
Ben Wagner | e346b1e | 2018-06-26 11:22:37 -0400 | [diff] [blame] | 1624 | if (!face) { |
| 1625 | return -1; |
| 1626 | } |
Ben Wagner | fc49734 | 2017-02-24 11:15:26 -0500 | [diff] [blame] | 1627 | |
Ben Wagner | e346b1e | 2018-06-26 11:22:37 -0400 | [diff] [blame] | 1628 | if (!(face->face_flags & FT_FACE_FLAG_MULTIPLE_MASTERS)) { |
Ben Wagner | fc49734 | 2017-02-24 11:15:26 -0500 | [diff] [blame] | 1629 | return 0; |
| 1630 | } |
| 1631 | |
| 1632 | FT_MM_Var* variations = nullptr; |
| 1633 | if (FT_Get_MM_Var(face, &variations)) { |
Ben Wagner | e346b1e | 2018-06-26 11:22:37 -0400 | [diff] [blame] | 1634 | return -1; |
Ben Wagner | fc49734 | 2017-02-24 11:15:26 -0500 | [diff] [blame] | 1635 | } |
| 1636 | SkAutoFree autoFreeVariations(variations); |
| 1637 | |
| 1638 | if (!coordinates || coordinateCount < SkToInt(variations->num_axis)) { |
| 1639 | return variations->num_axis; |
| 1640 | } |
| 1641 | |
| 1642 | SkAutoSTMalloc<4, FT_Fixed> coords(variations->num_axis); |
| 1643 | // FT_Get_{MM,Var}_{Blend,Design}_Coordinates were added in FreeType 2.7.1. |
| 1644 | if (gFTLibrary->fGetVarDesignCoordinates && |
| 1645 | !gFTLibrary->fGetVarDesignCoordinates(face, variations->num_axis, coords.get())) |
| 1646 | { |
| 1647 | for (FT_UInt i = 0; i < variations->num_axis; ++i) { |
| 1648 | coordinates[i].axis = variations->axis[i].tag; |
| 1649 | coordinates[i].value = SkFixedToScalar(coords[i]); |
| 1650 | } |
| 1651 | } else if (static_cast<FT_UInt>(fta.getAxesCount()) == variations->num_axis) { |
| 1652 | for (FT_UInt i = 0; i < variations->num_axis; ++i) { |
| 1653 | coordinates[i].axis = variations->axis[i].tag; |
| 1654 | coordinates[i].value = SkFixedToScalar(fta.getAxes()[i]); |
| 1655 | } |
| 1656 | } else if (fta.isNamedVariationSpecified()) { |
| 1657 | // The font has axes, they cannot be retrieved, and some named axis was specified. |
| 1658 | return -1; |
| 1659 | } else { |
| 1660 | // The font has axes, they cannot be retrieved, but no named instance was specified. |
| 1661 | return 0; |
| 1662 | } |
| 1663 | |
| 1664 | return variations->num_axis; |
| 1665 | } |
| 1666 | |
Ben Wagner | e346b1e | 2018-06-26 11:22:37 -0400 | [diff] [blame] | 1667 | int SkTypeface_FreeType::onGetVariationDesignParameters( |
| 1668 | SkFontParameters::Variation::Axis parameters[], int parameterCount) const |
| 1669 | { |
| 1670 | AutoFTAccess fta(this); |
| 1671 | FT_Face face = fta.face(); |
| 1672 | if (!face) { |
| 1673 | return -1; |
| 1674 | } |
| 1675 | |
| 1676 | if (!(face->face_flags & FT_FACE_FLAG_MULTIPLE_MASTERS)) { |
| 1677 | return 0; |
| 1678 | } |
| 1679 | |
| 1680 | FT_MM_Var* variations = nullptr; |
| 1681 | if (FT_Get_MM_Var(face, &variations)) { |
| 1682 | return -1; |
| 1683 | } |
| 1684 | SkAutoFree autoFreeVariations(variations); |
| 1685 | |
| 1686 | if (!parameters || parameterCount < SkToInt(variations->num_axis)) { |
| 1687 | return variations->num_axis; |
| 1688 | } |
| 1689 | |
| 1690 | for (FT_UInt i = 0; i < variations->num_axis; ++i) { |
| 1691 | parameters[i].tag = variations->axis[i].tag; |
| 1692 | parameters[i].min = SkFixedToScalar(variations->axis[i].minimum); |
| 1693 | parameters[i].def = SkFixedToScalar(variations->axis[i].def); |
| 1694 | parameters[i].max = SkFixedToScalar(variations->axis[i].maximum); |
| 1695 | FT_UInt flags = 0; |
| 1696 | bool hidden = gFTLibrary->fGetVarAxisFlags && |
| 1697 | !gFTLibrary->fGetVarAxisFlags(variations, i, &flags) && |
| 1698 | (flags & FT_VAR_AXIS_FLAG_HIDDEN); |
| 1699 | parameters[i].setHidden(hidden); |
| 1700 | } |
| 1701 | |
| 1702 | return variations->num_axis; |
| 1703 | } |
| 1704 | |
bungeman@google.com | ddc218e | 2013-08-01 22:29:43 +0000 | [diff] [blame] | 1705 | int SkTypeface_FreeType::onGetTableTags(SkFontTableTag tags[]) const { |
| 1706 | AutoFTAccess fta(this); |
| 1707 | FT_Face face = fta.face(); |
| 1708 | |
| 1709 | FT_ULong tableCount = 0; |
| 1710 | FT_Error error; |
| 1711 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1712 | // When 'tag' is nullptr, returns number of tables in 'length'. |
| 1713 | error = FT_Sfnt_Table_Info(face, 0, nullptr, &tableCount); |
bungeman@google.com | ddc218e | 2013-08-01 22:29:43 +0000 | [diff] [blame] | 1714 | if (error) { |
| 1715 | return 0; |
| 1716 | } |
| 1717 | |
| 1718 | if (tags) { |
| 1719 | for (FT_ULong tableIndex = 0; tableIndex < tableCount; ++tableIndex) { |
| 1720 | FT_ULong tableTag; |
| 1721 | FT_ULong tablelength; |
| 1722 | error = FT_Sfnt_Table_Info(face, tableIndex, &tableTag, &tablelength); |
| 1723 | if (error) { |
| 1724 | return 0; |
| 1725 | } |
| 1726 | tags[tableIndex] = static_cast<SkFontTableTag>(tableTag); |
| 1727 | } |
| 1728 | } |
| 1729 | return tableCount; |
| 1730 | } |
| 1731 | |
| 1732 | size_t SkTypeface_FreeType::onGetTableData(SkFontTableTag tag, size_t offset, |
| 1733 | size_t length, void* data) const |
| 1734 | { |
| 1735 | AutoFTAccess fta(this); |
| 1736 | FT_Face face = fta.face(); |
| 1737 | |
| 1738 | FT_ULong tableLength = 0; |
| 1739 | FT_Error error; |
| 1740 | |
| 1741 | // When 'length' is 0 it is overwritten with the full table length; 'offset' is ignored. |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1742 | error = FT_Load_Sfnt_Table(face, tag, 0, nullptr, &tableLength); |
bungeman@google.com | ddc218e | 2013-08-01 22:29:43 +0000 | [diff] [blame] | 1743 | if (error) { |
| 1744 | return 0; |
| 1745 | } |
| 1746 | |
| 1747 | if (offset > tableLength) { |
| 1748 | return 0; |
| 1749 | } |
bungeman@google.com | 5ecd4fa | 2013-08-01 22:48:21 +0000 | [diff] [blame] | 1750 | FT_ULong size = SkTMin((FT_ULong)length, tableLength - (FT_ULong)offset); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 1751 | if (data) { |
bungeman@google.com | ddc218e | 2013-08-01 22:29:43 +0000 | [diff] [blame] | 1752 | error = FT_Load_Sfnt_Table(face, tag, offset, reinterpret_cast<FT_Byte*>(data), &size); |
| 1753 | if (error) { |
| 1754 | return 0; |
| 1755 | } |
| 1756 | } |
| 1757 | |
| 1758 | return size; |
| 1759 | } |
| 1760 | |
reed@google.com | b4162b1 | 2013-07-02 16:32:29 +0000 | [diff] [blame] | 1761 | /////////////////////////////////////////////////////////////////////////////// |
| 1762 | /////////////////////////////////////////////////////////////////////////////// |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1763 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1764 | SkTypeface_FreeType::Scanner::Scanner() : fLibrary(nullptr) { |
bungeman | 9dc2468 | 2014-12-01 14:01:32 -0800 | [diff] [blame] | 1765 | if (FT_New_Library(&gFTMemory, &fLibrary)) { |
| 1766 | return; |
bungeman | 14df833 | 2014-10-28 15:07:23 -0700 | [diff] [blame] | 1767 | } |
bungeman | 9dc2468 | 2014-12-01 14:01:32 -0800 | [diff] [blame] | 1768 | FT_Add_Default_Modules(fLibrary); |
bungeman | 14df833 | 2014-10-28 15:07:23 -0700 | [diff] [blame] | 1769 | } |
| 1770 | SkTypeface_FreeType::Scanner::~Scanner() { |
bungeman | 9dc2468 | 2014-12-01 14:01:32 -0800 | [diff] [blame] | 1771 | if (fLibrary) { |
| 1772 | FT_Done_Library(fLibrary); |
| 1773 | } |
bungeman | 14df833 | 2014-10-28 15:07:23 -0700 | [diff] [blame] | 1774 | } |
| 1775 | |
bungeman | f93d711 | 2016-09-16 06:24:20 -0700 | [diff] [blame] | 1776 | FT_Face SkTypeface_FreeType::Scanner::openFace(SkStreamAsset* stream, int ttcIndex, |
bungeman | 14df833 | 2014-10-28 15:07:23 -0700 | [diff] [blame] | 1777 | FT_Stream ftStream) const |
bungeman | 32501a1 | 2014-10-28 12:03:55 -0700 | [diff] [blame] | 1778 | { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1779 | if (fLibrary == nullptr) { |
| 1780 | return nullptr; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1781 | } |
| 1782 | |
bungeman | 14df833 | 2014-10-28 15:07:23 -0700 | [diff] [blame] | 1783 | FT_Open_Args args; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1784 | memset(&args, 0, sizeof(args)); |
| 1785 | |
| 1786 | const void* memoryBase = stream->getMemoryBase(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1787 | |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 1788 | if (memoryBase) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1789 | args.flags = FT_OPEN_MEMORY; |
| 1790 | args.memory_base = (const FT_Byte*)memoryBase; |
| 1791 | args.memory_size = stream->getLength(); |
| 1792 | } else { |
bungeman | 14df833 | 2014-10-28 15:07:23 -0700 | [diff] [blame] | 1793 | memset(ftStream, 0, sizeof(*ftStream)); |
| 1794 | ftStream->size = stream->getLength(); |
| 1795 | ftStream->descriptor.pointer = stream; |
bungeman | 9dc2468 | 2014-12-01 14:01:32 -0800 | [diff] [blame] | 1796 | ftStream->read = sk_ft_stream_io; |
| 1797 | ftStream->close = sk_ft_stream_close; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1798 | |
| 1799 | args.flags = FT_OPEN_STREAM; |
bungeman | 14df833 | 2014-10-28 15:07:23 -0700 | [diff] [blame] | 1800 | args.stream = ftStream; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1801 | } |
| 1802 | |
| 1803 | FT_Face face; |
bungeman | 14df833 | 2014-10-28 15:07:23 -0700 | [diff] [blame] | 1804 | if (FT_Open_Face(fLibrary, &args, ttcIndex, &face)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1805 | return nullptr; |
bungeman | 14df833 | 2014-10-28 15:07:23 -0700 | [diff] [blame] | 1806 | } |
| 1807 | return face; |
| 1808 | } |
| 1809 | |
bungeman | f93d711 | 2016-09-16 06:24:20 -0700 | [diff] [blame] | 1810 | bool SkTypeface_FreeType::Scanner::recognizedFont(SkStreamAsset* stream, int* numFaces) const { |
bungeman | 14df833 | 2014-10-28 15:07:23 -0700 | [diff] [blame] | 1811 | SkAutoMutexAcquire libraryLock(fLibraryMutex); |
| 1812 | |
| 1813 | FT_StreamRec streamRec; |
| 1814 | FT_Face face = this->openFace(stream, -1, &streamRec); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1815 | if (nullptr == face) { |
bungeman | 14df833 | 2014-10-28 15:07:23 -0700 | [diff] [blame] | 1816 | return false; |
| 1817 | } |
| 1818 | |
| 1819 | *numFaces = face->num_faces; |
| 1820 | |
| 1821 | FT_Done_Face(face); |
| 1822 | return true; |
| 1823 | } |
| 1824 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 1825 | #include "include/private/SkTSearch.h" |
bungeman | 14df833 | 2014-10-28 15:07:23 -0700 | [diff] [blame] | 1826 | bool SkTypeface_FreeType::Scanner::scanFont( |
bungeman | f93d711 | 2016-09-16 06:24:20 -0700 | [diff] [blame] | 1827 | SkStreamAsset* stream, int ttcIndex, |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 1828 | SkString* name, SkFontStyle* style, bool* isFixedPitch, AxisDefinitions* axes) const |
bungeman | 14df833 | 2014-10-28 15:07:23 -0700 | [diff] [blame] | 1829 | { |
| 1830 | SkAutoMutexAcquire libraryLock(fLibraryMutex); |
| 1831 | |
| 1832 | FT_StreamRec streamRec; |
| 1833 | FT_Face face = this->openFace(stream, ttcIndex, &streamRec); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1834 | if (nullptr == face) { |
djsollen@google.com | 4dc686d | 2012-02-15 21:03:45 +0000 | [diff] [blame] | 1835 | return false; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1836 | } |
| 1837 | |
bungeman | a4c4a2d | 2014-10-20 13:33:19 -0700 | [diff] [blame] | 1838 | int weight = SkFontStyle::kNormal_Weight; |
| 1839 | int width = SkFontStyle::kNormal_Width; |
| 1840 | SkFontStyle::Slant slant = SkFontStyle::kUpright_Slant; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1841 | if (face->style_flags & FT_STYLE_FLAG_BOLD) { |
bungeman | a4c4a2d | 2014-10-20 13:33:19 -0700 | [diff] [blame] | 1842 | weight = SkFontStyle::kBold_Weight; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1843 | } |
| 1844 | if (face->style_flags & FT_STYLE_FLAG_ITALIC) { |
bungeman | a4c4a2d | 2014-10-20 13:33:19 -0700 | [diff] [blame] | 1845 | slant = SkFontStyle::kItalic_Slant; |
| 1846 | } |
| 1847 | |
| 1848 | PS_FontInfoRec psFontInfo; |
| 1849 | TT_OS2* os2 = static_cast<TT_OS2*>(FT_Get_Sfnt_Table(face, ft_sfnt_os2)); |
| 1850 | if (os2 && os2->version != 0xffff) { |
| 1851 | weight = os2->usWeightClass; |
| 1852 | width = os2->usWidthClass; |
bungeman | b4bb7d8 | 2016-04-27 10:21:04 -0700 | [diff] [blame] | 1853 | |
| 1854 | // OS/2::fsSelection bit 9 indicates oblique. |
| 1855 | if (SkToBool(os2->fsSelection & (1u << 9))) { |
| 1856 | slant = SkFontStyle::kOblique_Slant; |
| 1857 | } |
bungeman | a4c4a2d | 2014-10-20 13:33:19 -0700 | [diff] [blame] | 1858 | } else if (0 == FT_Get_PS_Font_Info(face, &psFontInfo) && psFontInfo.weight) { |
| 1859 | static const struct { |
| 1860 | char const * const name; |
| 1861 | int const weight; |
| 1862 | } commonWeights [] = { |
| 1863 | // There are probably more common names, but these are known to exist. |
bungeman | d803cda | 2015-04-16 14:22:46 -0700 | [diff] [blame] | 1864 | { "all", SkFontStyle::kNormal_Weight }, // Multiple Masters usually default to normal. |
bungeman | a4c4a2d | 2014-10-20 13:33:19 -0700 | [diff] [blame] | 1865 | { "black", SkFontStyle::kBlack_Weight }, |
| 1866 | { "bold", SkFontStyle::kBold_Weight }, |
| 1867 | { "book", (SkFontStyle::kNormal_Weight + SkFontStyle::kLight_Weight)/2 }, |
| 1868 | { "demi", SkFontStyle::kSemiBold_Weight }, |
| 1869 | { "demibold", SkFontStyle::kSemiBold_Weight }, |
bungeman | 14df833 | 2014-10-28 15:07:23 -0700 | [diff] [blame] | 1870 | { "extra", SkFontStyle::kExtraBold_Weight }, |
bungeman | a4c4a2d | 2014-10-20 13:33:19 -0700 | [diff] [blame] | 1871 | { "extrabold", SkFontStyle::kExtraBold_Weight }, |
| 1872 | { "extralight", SkFontStyle::kExtraLight_Weight }, |
bungeman | 14df833 | 2014-10-28 15:07:23 -0700 | [diff] [blame] | 1873 | { "hairline", SkFontStyle::kThin_Weight }, |
bungeman | a4c4a2d | 2014-10-20 13:33:19 -0700 | [diff] [blame] | 1874 | { "heavy", SkFontStyle::kBlack_Weight }, |
| 1875 | { "light", SkFontStyle::kLight_Weight }, |
| 1876 | { "medium", SkFontStyle::kMedium_Weight }, |
| 1877 | { "normal", SkFontStyle::kNormal_Weight }, |
bungeman | 14df833 | 2014-10-28 15:07:23 -0700 | [diff] [blame] | 1878 | { "plain", SkFontStyle::kNormal_Weight }, |
bungeman | a4c4a2d | 2014-10-20 13:33:19 -0700 | [diff] [blame] | 1879 | { "regular", SkFontStyle::kNormal_Weight }, |
bungeman | 14df833 | 2014-10-28 15:07:23 -0700 | [diff] [blame] | 1880 | { "roman", SkFontStyle::kNormal_Weight }, |
bungeman | a4c4a2d | 2014-10-20 13:33:19 -0700 | [diff] [blame] | 1881 | { "semibold", SkFontStyle::kSemiBold_Weight }, |
bungeman | 14df833 | 2014-10-28 15:07:23 -0700 | [diff] [blame] | 1882 | { "standard", SkFontStyle::kNormal_Weight }, |
bungeman | a4c4a2d | 2014-10-20 13:33:19 -0700 | [diff] [blame] | 1883 | { "thin", SkFontStyle::kThin_Weight }, |
| 1884 | { "ultra", SkFontStyle::kExtraBold_Weight }, |
bungeman | 6e45bda | 2016-07-25 15:11:49 -0700 | [diff] [blame] | 1885 | { "ultrablack", SkFontStyle::kExtraBlack_Weight }, |
bungeman | a4c4a2d | 2014-10-20 13:33:19 -0700 | [diff] [blame] | 1886 | { "ultrabold", SkFontStyle::kExtraBold_Weight }, |
bungeman | 6e45bda | 2016-07-25 15:11:49 -0700 | [diff] [blame] | 1887 | { "ultraheavy", SkFontStyle::kExtraBlack_Weight }, |
bungeman | a4c4a2d | 2014-10-20 13:33:19 -0700 | [diff] [blame] | 1888 | { "ultralight", SkFontStyle::kExtraLight_Weight }, |
| 1889 | }; |
| 1890 | int const index = SkStrLCSearch(&commonWeights[0].name, SK_ARRAY_COUNT(commonWeights), |
bungeman | d2ae728 | 2014-10-22 08:25:44 -0700 | [diff] [blame] | 1891 | psFontInfo.weight, sizeof(commonWeights[0])); |
bungeman | a4c4a2d | 2014-10-20 13:33:19 -0700 | [diff] [blame] | 1892 | if (index >= 0) { |
| 1893 | weight = commonWeights[index].weight; |
| 1894 | } else { |
Mike Klein | 3e0141a | 2019-03-26 10:41:18 -0500 | [diff] [blame] | 1895 | LOG_INFO("Do not know weight for: %s (%s) \n", face->family_name, psFontInfo.weight); |
bungeman | a4c4a2d | 2014-10-20 13:33:19 -0700 | [diff] [blame] | 1896 | } |
djsollen@google.com | 4dc686d | 2012-02-15 21:03:45 +0000 | [diff] [blame] | 1897 | } |
| 1898 | |
| 1899 | if (name) { |
| 1900 | name->set(face->family_name); |
| 1901 | } |
| 1902 | if (style) { |
bungeman | a4c4a2d | 2014-10-20 13:33:19 -0700 | [diff] [blame] | 1903 | *style = SkFontStyle(weight, width, slant); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1904 | } |
bungeman@google.com | fe74765 | 2013-03-25 19:36:11 +0000 | [diff] [blame] | 1905 | if (isFixedPitch) { |
| 1906 | *isFixedPitch = FT_IS_FIXED_WIDTH(face); |
reed@google.com | 5b31b0f | 2011-02-23 14:41:42 +0000 | [diff] [blame] | 1907 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1908 | |
Bruce Wang | ebf0cf5 | 2018-06-18 14:04:19 -0400 | [diff] [blame] | 1909 | bool success = GetAxes(face, axes); |
| 1910 | FT_Done_Face(face); |
| 1911 | return success; |
| 1912 | } |
| 1913 | |
| 1914 | bool SkTypeface_FreeType::Scanner::GetAxes(FT_Face face, AxisDefinitions* axes) { |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 1915 | if (axes && face->face_flags & FT_FACE_FLAG_MULTIPLE_MASTERS) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1916 | FT_MM_Var* variations = nullptr; |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 1917 | FT_Error err = FT_Get_MM_Var(face, &variations); |
| 1918 | if (err) { |
Mike Klein | 3e0141a | 2019-03-26 10:41:18 -0500 | [diff] [blame] | 1919 | LOG_INFO("INFO: font %s claims to have variations, but none found.\n", |
Hal Canary | 2b0e6cd | 2018-07-09 12:43:39 -0400 | [diff] [blame] | 1920 | face->family_name); |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 1921 | return false; |
| 1922 | } |
| 1923 | SkAutoFree autoFreeVariations(variations); |
| 1924 | |
| 1925 | axes->reset(variations->num_axis); |
| 1926 | for (FT_UInt i = 0; i < variations->num_axis; ++i) { |
| 1927 | const FT_Var_Axis& ftAxis = variations->axis[i]; |
| 1928 | (*axes)[i].fTag = ftAxis.tag; |
| 1929 | (*axes)[i].fMinimum = ftAxis.minimum; |
| 1930 | (*axes)[i].fDefault = ftAxis.def; |
| 1931 | (*axes)[i].fMaximum = ftAxis.maximum; |
| 1932 | } |
| 1933 | } |
djsollen@google.com | 4dc686d | 2012-02-15 21:03:45 +0000 | [diff] [blame] | 1934 | return true; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1935 | } |
bungeman | f6c7107 | 2016-01-21 14:17:47 -0800 | [diff] [blame] | 1936 | |
| 1937 | /*static*/ void SkTypeface_FreeType::Scanner::computeAxisValues( |
| 1938 | AxisDefinitions axisDefinitions, |
Ben Wagner | fc49734 | 2017-02-24 11:15:26 -0500 | [diff] [blame] | 1939 | const SkFontArguments::VariationPosition position, |
bungeman | f6c7107 | 2016-01-21 14:17:47 -0800 | [diff] [blame] | 1940 | SkFixed* axisValues, |
| 1941 | const SkString& name) |
| 1942 | { |
| 1943 | for (int i = 0; i < axisDefinitions.count(); ++i) { |
| 1944 | const Scanner::AxisDefinition& axisDefinition = axisDefinitions[i]; |
benjaminwagner | 64a3c95 | 2016-02-25 12:20:40 -0800 | [diff] [blame] | 1945 | const SkScalar axisMin = SkFixedToScalar(axisDefinition.fMinimum); |
| 1946 | const SkScalar axisMax = SkFixedToScalar(axisDefinition.fMaximum); |
bungeman | f6c7107 | 2016-01-21 14:17:47 -0800 | [diff] [blame] | 1947 | axisValues[i] = axisDefinition.fDefault; |
bungeman | 9aec894 | 2017-03-29 13:38:53 -0400 | [diff] [blame] | 1948 | // The position may be over specified. If there are multiple values for a given axis, |
| 1949 | // use the last one since that's what css-fonts-4 requires. |
| 1950 | for (int j = position.coordinateCount; j --> 0;) { |
Ben Wagner | fc49734 | 2017-02-24 11:15:26 -0500 | [diff] [blame] | 1951 | const auto& coordinate = position.coordinates[j]; |
| 1952 | if (axisDefinition.fTag == coordinate.axis) { |
| 1953 | const SkScalar axisValue = SkTPin(coordinate.value, axisMin, axisMax); |
| 1954 | if (coordinate.value != axisValue) { |
Mike Klein | 3e0141a | 2019-03-26 10:41:18 -0500 | [diff] [blame] | 1955 | LOG_INFO("Requested font axis value out of range: " |
Hal Canary | 2b0e6cd | 2018-07-09 12:43:39 -0400 | [diff] [blame] | 1956 | "%s '%c%c%c%c' %f; pinned to %f.\n", |
| 1957 | name.c_str(), |
| 1958 | (axisDefinition.fTag >> 24) & 0xFF, |
| 1959 | (axisDefinition.fTag >> 16) & 0xFF, |
| 1960 | (axisDefinition.fTag >> 8) & 0xFF, |
| 1961 | (axisDefinition.fTag ) & 0xFF, |
| 1962 | SkScalarToDouble(coordinate.value), |
| 1963 | SkScalarToDouble(axisValue)); |
bungeman | f6c7107 | 2016-01-21 14:17:47 -0800 | [diff] [blame] | 1964 | } |
benjaminwagner | 64a3c95 | 2016-02-25 12:20:40 -0800 | [diff] [blame] | 1965 | axisValues[i] = SkScalarToFixed(axisValue); |
bungeman | f6c7107 | 2016-01-21 14:17:47 -0800 | [diff] [blame] | 1966 | break; |
| 1967 | } |
| 1968 | } |
| 1969 | // TODO: warn on defaulted axis? |
| 1970 | } |
| 1971 | |
| 1972 | SkDEBUGCODE( |
| 1973 | // Check for axis specified, but not matched in font. |
Ben Wagner | fc49734 | 2017-02-24 11:15:26 -0500 | [diff] [blame] | 1974 | for (int i = 0; i < position.coordinateCount; ++i) { |
| 1975 | SkFourByteTag skTag = position.coordinates[i].axis; |
bungeman | f6c7107 | 2016-01-21 14:17:47 -0800 | [diff] [blame] | 1976 | bool found = false; |
| 1977 | for (int j = 0; j < axisDefinitions.count(); ++j) { |
| 1978 | if (skTag == axisDefinitions[j].fTag) { |
| 1979 | found = true; |
| 1980 | break; |
| 1981 | } |
| 1982 | } |
| 1983 | if (!found) { |
Mike Klein | 3e0141a | 2019-03-26 10:41:18 -0500 | [diff] [blame] | 1984 | LOG_INFO("Requested font axis not found: %s '%c%c%c%c'\n", |
Hal Canary | 2b0e6cd | 2018-07-09 12:43:39 -0400 | [diff] [blame] | 1985 | name.c_str(), |
| 1986 | (skTag >> 24) & 0xFF, |
| 1987 | (skTag >> 16) & 0xFF, |
| 1988 | (skTag >> 8) & 0xFF, |
| 1989 | (skTag) & 0xFF); |
bungeman | f6c7107 | 2016-01-21 14:17:47 -0800 | [diff] [blame] | 1990 | } |
| 1991 | } |
| 1992 | ) |
| 1993 | } |