[WTF] Add makeUnique<T>, which ensures T is fast-allocated, makeUnique / makeUniqueWi...
[WebKit-https.git] / Source / WebCore / platform / graphics / freetype / FontCustomPlatformDataFreeType.cpp
1 /*
2  * Copyright (C) 2008 Alp Toker <alp@atoker.com>
3  * Copyright (C) 2010 Igalia S.L.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public License
16  * along with this library; see the file COPYING.LIB.  If not, write to
17  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  *
20  */
21
22 #include "config.h"
23 #include "FontCustomPlatformData.h"
24
25 #include "CairoUtilities.h"
26 #include "FontCacheFreeType.h"
27 #include "FontDescription.h"
28 #include "FontPlatformData.h"
29 #include "SharedBuffer.h"
30 #include <cairo-ft.h>
31 #include <cairo.h>
32 #include <ft2build.h>
33 #include FT_MODULE_H
34 #include <mutex>
35
36 namespace WebCore {
37
38 static void releaseCustomFontData(void* data)
39 {
40     static_cast<SharedBuffer*>(data)->deref();
41 }
42
43 static cairo_user_data_key_t freeTypeFaceKey;
44
45 FontCustomPlatformData::FontCustomPlatformData(FT_Face freeTypeFace, SharedBuffer& buffer)
46     : m_fontFace(adoptRef(cairo_ft_font_face_create_for_ft_face(freeTypeFace, FT_LOAD_DEFAULT)))
47 {
48     buffer.ref(); // This is balanced by the buffer->deref() in releaseCustomFontData.
49     static cairo_user_data_key_t bufferKey;
50     cairo_font_face_set_user_data(m_fontFace.get(), &bufferKey, &buffer,
51          static_cast<cairo_destroy_func_t>(releaseCustomFontData));
52
53     // Cairo doesn't do FreeType reference counting, so we need to ensure that when
54     // this cairo_font_face_t is destroyed, it cleans up the FreeType face as well.
55     cairo_font_face_set_user_data(m_fontFace.get(), &freeTypeFaceKey, freeTypeFace,
56         reinterpret_cast<cairo_destroy_func_t>(reinterpret_cast<void(*)(void)>(FT_Done_Face)));
57 }
58
59 static FcPattern* defaultFontconfigOptions()
60 {
61     // Get some generic default settings from fontconfig for web fonts. Strategy
62     // from Behdad Esfahbod in https://code.google.com/p/chromium/issues/detail?id=173207#c35
63     // For web fonts, the hint style is overridden in FontCustomPlatformData::FontCustomPlatformData
64     // so Fontconfig will not affect the hint style, but it may disable hinting completely.
65     static FcPattern* pattern = nullptr;
66     static std::once_flag flag;
67     std::call_once(flag, [](FcPattern*) {
68         pattern = FcPatternCreate();
69         FcConfigSubstitute(nullptr, pattern, FcMatchPattern);
70         cairo_ft_font_options_substitute(getDefaultCairoFontOptions(), pattern);
71         FcDefaultSubstitute(pattern);
72         FcPatternDel(pattern, FC_FAMILY);
73         FcConfigSubstitute(nullptr, pattern, FcMatchFont);
74     }, pattern);
75     return pattern;
76 }
77
78 FontPlatformData FontCustomPlatformData::fontPlatformData(const FontDescription& description, bool bold, bool italic, const FontFeatureSettings&, const FontVariantSettings&, FontSelectionSpecifiedCapabilities)
79 {
80     auto* freeTypeFace = static_cast<FT_Face>(cairo_font_face_get_user_data(m_fontFace.get(), &freeTypeFaceKey));
81     ASSERT(freeTypeFace);
82     RefPtr<FcPattern> pattern = defaultFontconfigOptions();
83 #if ENABLE(VARIATION_FONTS)
84     auto variants = buildVariationSettings(freeTypeFace, description);
85     if (!variants.isEmpty()) {
86         pattern = adoptRef(FcPatternDuplicate(pattern.get()));
87         FcPatternAddString(pattern.get(), FC_FONT_VARIATIONS, reinterpret_cast<const FcChar8*>(variants.utf8().data()));
88     }
89 #endif
90     return FontPlatformData(m_fontFace.get(), pattern.get(), description.computedPixelSize(), freeTypeFace->face_flags & FT_FACE_FLAG_FIXED_WIDTH, bold, italic, description.orientation());
91 }
92
93 static bool initializeFreeTypeLibrary(FT_Library& library)
94 {
95     // https://www.freetype.org/freetype2/docs/design/design-4.html
96     // https://lists.nongnu.org/archive/html/freetype-devel/2004-10/msg00022.html
97
98     FT_Memory memory = bitwise_cast<FT_Memory>(ft_smalloc(sizeof(*memory)));
99     if (!memory)
100         return false;
101
102     memory->user = nullptr;
103     memory->alloc = [](FT_Memory, long size) -> void* {
104         return fastMalloc(size);
105     };
106     memory->free = [](FT_Memory, void* block) -> void {
107         fastFree(block);
108     };
109     memory->realloc = [](FT_Memory, long, long newSize, void* block) -> void* {
110         return fastRealloc(block, newSize);
111     };
112
113     if (FT_New_Library(memory, &library)) {
114         ft_sfree(memory);
115         return false;
116     }
117
118     FT_Add_Default_Modules(library);
119     return true;
120 }
121
122 std::unique_ptr<FontCustomPlatformData> createFontCustomPlatformData(SharedBuffer& buffer, const String&)
123 {
124     static FT_Library library;
125     if (!library && !initializeFreeTypeLibrary(library)) {
126         library = nullptr;
127         return nullptr;
128     }
129
130     FT_Face freeTypeFace;
131     if (FT_New_Memory_Face(library, reinterpret_cast<const FT_Byte*>(buffer.data()), buffer.size(), 0, &freeTypeFace))
132         return nullptr;
133     return makeUnique<FontCustomPlatformData>(freeTypeFace, buffer);
134 }
135
136 bool FontCustomPlatformData::supportsFormat(const String& format)
137 {
138     return equalLettersIgnoringASCIICase(format, "truetype")
139         || equalLettersIgnoringASCIICase(format, "opentype")
140 #if USE(WOFF2)
141         || equalLettersIgnoringASCIICase(format, "woff2")
142 #if ENABLE(VARIATION_FONTS)
143         || equalLettersIgnoringASCIICase(format, "woff2-variations")
144 #endif
145 #endif
146 #if ENABLE(VARIATION_FONTS)
147         || equalLettersIgnoringASCIICase(format, "woff-variations")
148         || equalLettersIgnoringASCIICase(format, "truetype-variations")
149         || equalLettersIgnoringASCIICase(format, "opentype-variations")
150 #endif
151         || equalLettersIgnoringASCIICase(format, "woff");
152 }
153
154 }