2 * Copyright (C) 2007, 2008, 2009, 2010 Apple Inc. All rights reserved.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public License
15 * along with this library; see the file COPYING.LIB. If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
22 #include "FontCustomPlatformData.h"
25 #include "FontPlatformData.h"
26 #include "OpenTypeUtilities.h"
27 #include "SharedBuffer.h"
28 #include "SoftLinking.h"
29 #include "WOFFFileFormat.h"
30 #include <ApplicationServices/ApplicationServices.h>
31 #include <WebKitSystemInterface/WebKitSystemInterface.h>
32 #include <wtf/RetainPtr.h>
34 // From t2embapi.h, which is missing from the Microsoft Platform SDK.
35 typedef unsigned long(WINAPIV *READEMBEDPROC) (void*, void*, unsigned long);
37 #define TTLOAD_PRIVATE 0x00000001
38 #define LICENSE_PREVIEWPRINT 0x0004
39 #define E_NONE 0x0000L
45 SOFT_LINK_LIBRARY(T2embed);
46 SOFT_LINK(T2embed, TTLoadEmbeddedFont, LONG, __stdcall, (HANDLE* phFontReference, ULONG ulFlags, ULONG* pulPrivStatus, ULONG ulPrivs, ULONG* pulStatus, READEMBEDPROC lpfnReadFromStream, LPVOID lpvReadStream, LPWSTR szWinFamilyName, LPSTR szMacFamilyName, TTLOADINFO* pTTLoadInfo), (phFontReference, ulFlags,pulPrivStatus, ulPrivs, pulStatus, lpfnReadFromStream, lpvReadStream, szWinFamilyName, szMacFamilyName, pTTLoadInfo));
47 SOFT_LINK(T2embed, TTGetNewFontName, LONG, __stdcall, (HANDLE* phFontReference, LPWSTR szWinFamilyName, long cchMaxWinName, LPSTR szMacFamilyName, long cchMaxMacName), (phFontReference, szWinFamilyName, cchMaxWinName, szMacFamilyName, cchMaxMacName));
48 SOFT_LINK(T2embed, TTDeleteEmbeddedFont, LONG, __stdcall, (HANDLE hFontReference, ULONG ulFlags, ULONG* pulStatus), (hFontReference, ulFlags, pulStatus));
50 FontCustomPlatformData::~FontCustomPlatformData()
52 if (m_fontReference) {
53 if (m_name.isNull()) {
54 ASSERT(T2embedLibrary());
56 TTDeleteEmbeddedFont(m_fontReference, 0, &status);
58 RemoveFontMemResourceEx(m_fontReference);
62 FontPlatformData FontCustomPlatformData::fontPlatformData(int size, bool bold, bool italic, FontOrientation, TextOrientation, FontWidthVariant, FontRenderingMode renderingMode)
64 ASSERT(m_fontReference);
65 ASSERT(T2embedLibrary());
67 LOGFONT& logFont = *static_cast<LOGFONT*>(malloc(sizeof(LOGFONT)));
69 TTGetNewFontName(&m_fontReference, logFont.lfFaceName, LF_FACESIZE, 0, 0);
71 memcpy(logFont.lfFaceName, m_name.charactersWithNullTermination(), sizeof(logFont.lfFaceName[0]) * min(static_cast<size_t>(LF_FACESIZE), 1 + m_name.length()));
73 logFont.lfHeight = -size;
74 if (renderingMode == NormalRenderingMode)
75 logFont.lfHeight *= 32;
77 logFont.lfEscapement = 0;
78 logFont.lfOrientation = 0;
79 logFont.lfUnderline = false;
80 logFont.lfStrikeOut = false;
81 logFont.lfCharSet = DEFAULT_CHARSET;
82 logFont.lfOutPrecision = OUT_TT_ONLY_PRECIS;
83 logFont.lfQuality = CLEARTYPE_QUALITY;
84 logFont.lfPitchAndFamily = DEFAULT_PITCH | FF_DONTCARE;
85 logFont.lfItalic = italic;
86 logFont.lfWeight = bold ? 700 : 400;
88 HFONT hfont = CreateFontIndirect(&logFont);
90 RetainPtr<CGFontRef> cgFont(AdoptCF, CGFontCreateWithPlatformFont(&logFont));
91 return FontPlatformData(hfont, cgFont.get(), size, bold, italic, renderingMode == AlternateRenderingMode);
94 // Streams the concatenation of a header and font data.
97 EOTStream(const EOTHeader& eotHeader, const SharedBuffer* fontData, size_t overlayDst, size_t overlaySrc, size_t overlayLength)
98 : m_eotHeader(eotHeader)
99 , m_fontData(fontData)
100 , m_overlayDst(overlayDst)
101 , m_overlaySrc(overlaySrc)
102 , m_overlayLength(overlayLength)
108 size_t read(void* buffer, size_t count);
111 const EOTHeader& m_eotHeader;
112 const SharedBuffer* m_fontData;
115 size_t m_overlayLength;
120 size_t EOTStream::read(void* buffer, size_t count)
122 size_t bytesToRead = count;
124 size_t bytesFromHeader = min(m_eotHeader.size() - m_offset, count);
125 memcpy(buffer, m_eotHeader.data() + m_offset, bytesFromHeader);
126 m_offset += bytesFromHeader;
127 bytesToRead -= bytesFromHeader;
128 if (m_offset == m_eotHeader.size()) {
133 if (bytesToRead && !m_inHeader) {
134 size_t bytesFromData = min(m_fontData->size() - m_offset, bytesToRead);
135 memcpy(buffer, m_fontData->data() + m_offset, bytesFromData);
136 if (m_offset < m_overlayDst + m_overlayLength && m_offset + bytesFromData >= m_overlayDst) {
137 size_t dstOffset = max<int>(m_overlayDst - m_offset, 0);
138 size_t srcOffset = max<int>(0, m_offset - m_overlayDst);
139 size_t bytesToCopy = min(bytesFromData - dstOffset, m_overlayLength - srcOffset);
140 memcpy(reinterpret_cast<char*>(buffer) + dstOffset, m_fontData->data() + m_overlaySrc + srcOffset, bytesToCopy);
142 m_offset += bytesFromData;
143 bytesToRead -= bytesFromData;
145 return count - bytesToRead;
148 static unsigned long WINAPIV readEmbedProc(void* stream, void* buffer, unsigned long length)
150 return static_cast<EOTStream*>(stream)->read(buffer, length);
153 // Creates a unique and unpredictable font name, in order to avoid collisions and to
154 // not allow access from CSS.
155 static String createUniqueFontName()
158 CoCreateGuid(&fontUuid);
160 String fontName = base64Encode(reinterpret_cast<char*>(&fontUuid), sizeof(fontUuid));
161 ASSERT(fontName.length() < LF_FACESIZE);
165 FontCustomPlatformData* createFontCustomPlatformData(SharedBuffer* buffer)
167 ASSERT_ARG(buffer, buffer);
168 ASSERT(T2embedLibrary());
170 RefPtr<SharedBuffer> sfntBuffer;
171 if (isWOFF(buffer)) {
173 if (!convertWOFFToSfnt(buffer, sfnt))
176 sfntBuffer = SharedBuffer::adoptVector(sfnt);
177 buffer = sfntBuffer.get();
180 // Introduce the font to GDI. AddFontMemResourceEx cannot be used, because it will pollute the process's
181 // font namespace (Windows has no API for creating an HFONT from data without exposing the font to the
182 // entire process first). TTLoadEmbeddedFont lets us override the font family name, so using a unique name
183 // we avoid namespace collisions.
185 String fontName = createUniqueFontName();
187 // TTLoadEmbeddedFont works only with Embedded OpenType (.eot) data, so we need to create an EOT header
188 // and prepend it to the font data.
192 size_t overlayLength;
193 if (!getEOTHeader(buffer, eotHeader, overlayDst, overlaySrc, overlayLength))
196 HANDLE fontReference;
199 EOTStream eotStream(eotHeader, buffer, overlayDst, overlaySrc, overlayLength);
201 LONG loadEmbeddedFontResult = TTLoadEmbeddedFont(&fontReference, TTLOAD_PRIVATE, &privStatus, LICENSE_PREVIEWPRINT, &status, readEmbedProc, &eotStream, const_cast<LPWSTR>(fontName.charactersWithNullTermination()), 0, 0);
202 if (loadEmbeddedFontResult == E_NONE)
205 fontReference = renameAndActivateFont(buffer, fontName);
210 return new FontCustomPlatformData(fontReference, fontName);
213 bool FontCustomPlatformData::supportsFormat(const String& format)
215 return equalIgnoringCase(format, "truetype") || equalIgnoringCase(format, "opentype") || equalIgnoringCase(format, "woff");