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