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