53625df7acf4c45d75713cee0967c13c77a4eb87
[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 "FontPlatformData.h"
26 #include "SharedBuffer.h"
27 #include <cairo-ft.h>
28 #include <cairo.h>
29 #include <ft2build.h>
30 #include FT_MODULE_H
31
32 namespace WebCore {
33
34 static void releaseCustomFontData(void* data)
35 {
36     static_cast<SharedBuffer*>(data)->deref();
37 }
38
39 FontCustomPlatformData::FontCustomPlatformData(FT_Face freeTypeFace, SharedBuffer& buffer)
40     : m_fontFace(cairo_ft_font_face_create_for_ft_face(freeTypeFace, FT_LOAD_DEFAULT))
41 {
42     buffer.ref(); // This is balanced by the buffer->deref() in releaseCustomFontData.
43     static cairo_user_data_key_t bufferKey;
44     cairo_font_face_set_user_data(m_fontFace, &bufferKey, &buffer,
45          static_cast<cairo_destroy_func_t>(releaseCustomFontData));
46
47     // Cairo doesn't do FreeType reference counting, so we need to ensure that when
48     // this cairo_font_face_t is destroyed, it cleans up the FreeType face as well.
49     static cairo_user_data_key_t freeTypeFaceKey;
50     cairo_font_face_set_user_data(m_fontFace, &freeTypeFaceKey, freeTypeFace,
51         reinterpret_cast<cairo_destroy_func_t>(reinterpret_cast<void(*)(void)>(FT_Done_Face)));
52 }
53
54 FontCustomPlatformData::~FontCustomPlatformData()
55 {
56     cairo_font_face_destroy(m_fontFace);
57 }
58
59 FontPlatformData FontCustomPlatformData::fontPlatformData(const FontDescription& description, bool bold, bool italic)
60 {
61     return FontPlatformData(m_fontFace, description, bold, italic);
62 }
63
64 static bool initializeFreeTypeLibrary(FT_Library& library)
65 {
66     // https://www.freetype.org/freetype2/docs/design/design-4.html
67     // https://lists.nongnu.org/archive/html/freetype-devel/2004-10/msg00022.html
68
69     FT_Memory memory = bitwise_cast<FT_Memory>(ft_smalloc(sizeof(*memory)));
70     if (!memory)
71         return false;
72
73     memory->user = nullptr;
74     memory->alloc = [](FT_Memory, long size) -> void* {
75         return fastMalloc(size);
76     };
77     memory->free = [](FT_Memory, void* block) -> void {
78         fastFree(block);
79     };
80     memory->realloc = [](FT_Memory, long, long newSize, void* block) -> void* {
81         return fastRealloc(block, newSize);
82     };
83
84     if (FT_New_Library(memory, &library)) {
85         ft_sfree(memory);
86         return false;
87     }
88
89     FT_Add_Default_Modules(library);
90     return true;
91 }
92
93 std::unique_ptr<FontCustomPlatformData> createFontCustomPlatformData(SharedBuffer& buffer, const String&)
94 {
95     static FT_Library library;
96     if (!library && !initializeFreeTypeLibrary(library)) {
97         library = nullptr;
98         return nullptr;
99     }
100
101     FT_Face freeTypeFace;
102     if (FT_New_Memory_Face(library, reinterpret_cast<const FT_Byte*>(buffer.data()), buffer.size(), 0, &freeTypeFace))
103         return nullptr;
104     return std::make_unique<FontCustomPlatformData>(freeTypeFace, buffer);
105 }
106
107 bool FontCustomPlatformData::supportsFormat(const String& format)
108 {
109     return equalLettersIgnoringASCIICase(format, "truetype")
110         || equalLettersIgnoringASCIICase(format, "opentype")
111 #if USE(WOFF2)
112         || equalLettersIgnoringASCIICase(format, "woff2")
113 #endif
114         || equalLettersIgnoringASCIICase(format, "woff");
115 }
116
117 }