2 * Copyright (C) 2007, 2008, 2009 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 <ApplicationServices/ApplicationServices.h>
30 #include <WebKitSystemInterface/WebKitSystemInterface.h>
31 #include <wtf/RetainPtr.h>
33 // From t2embapi.h, which is missing from the Microsoft Platform SDK.
34 typedef unsigned long(WINAPIV *READEMBEDPROC) (void*, void*, unsigned long);
36 #define TTLOAD_PRIVATE 0x00000001
37 #define LICENSE_PREVIEWPRINT 0x0004
38 #define E_NONE 0x0000L
44 SOFT_LINK_LIBRARY(T2embed);
45 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));
46 SOFT_LINK(T2embed, TTGetNewFontName, LONG, __stdcall, (HANDLE* phFontReference, LPWSTR szWinFamilyName, long cchMaxWinName, LPSTR szMacFamilyName, long cchMaxMacName), (phFontReference, szWinFamilyName, cchMaxWinName, szMacFamilyName, cchMaxMacName));
47 SOFT_LINK(T2embed, TTDeleteEmbeddedFont, LONG, __stdcall, (HANDLE hFontReference, ULONG ulFlags, ULONG* pulStatus), (hFontReference, ulFlags, pulStatus));
49 FontCustomPlatformData::~FontCustomPlatformData()
51 CGFontRelease(m_cgFont);
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, FontRenderingMode renderingMode)
65 ASSERT(m_fontReference);
66 ASSERT(T2embedLibrary());
68 LOGFONT& logFont = *static_cast<LOGFONT*>(malloc(sizeof(LOGFONT)));
70 TTGetNewFontName(&m_fontReference, logFont.lfFaceName, LF_FACESIZE, 0, 0);
72 memcpy(logFont.lfFaceName, m_name.charactersWithNullTermination(), sizeof(logFont.lfFaceName[0]) * min(static_cast<size_t>(LF_FACESIZE), 1 + m_name.length()));
74 logFont.lfHeight = -size;
75 if (renderingMode == NormalRenderingMode)
76 logFont.lfHeight *= 32;
78 logFont.lfEscapement = 0;
79 logFont.lfOrientation = 0;
80 logFont.lfUnderline = false;
81 logFont.lfStrikeOut = false;
82 logFont.lfCharSet = DEFAULT_CHARSET;
83 logFont.lfOutPrecision = OUT_TT_ONLY_PRECIS;
84 logFont.lfQuality = CLEARTYPE_QUALITY;
85 logFont.lfPitchAndFamily = DEFAULT_PITCH | FF_DONTCARE;
86 logFont.lfItalic = italic;
87 logFont.lfWeight = bold ? 700 : 400;
89 HFONT hfont = CreateFontIndirect(&logFont);
90 wkSetFontPlatformInfo(m_cgFont, &logFont, free);
91 return FontPlatformData(hfont, m_cgFont, size, bold, italic, renderingMode == AlternateRenderingMode);
94 const void* getData(void* info)
96 SharedBuffer* buffer = static_cast<SharedBuffer*>(info);
98 return (void*)buffer->data();
101 void releaseData(void* info, const void* data)
103 static_cast<SharedBuffer*>(info)->deref();
106 size_t getBytesWithOffset(void *info, void* buffer, size_t offset, size_t count)
108 SharedBuffer* sharedBuffer = static_cast<SharedBuffer*>(info);
109 size_t availBytes = count;
110 if (offset + count > sharedBuffer->size())
111 availBytes -= (offset + count) - sharedBuffer->size();
112 memcpy(buffer, sharedBuffer->data() + offset, availBytes);
116 // Streams the concatenation of a header and font data.
119 EOTStream(const Vector<uint8_t, 512>& eotHeader, const SharedBuffer* fontData, size_t overlayDst, size_t overlaySrc, size_t overlayLength)
120 : m_eotHeader(eotHeader)
121 , m_fontData(fontData)
122 , m_overlayDst(overlayDst)
123 , m_overlaySrc(overlaySrc)
124 , m_overlayLength(overlayLength)
130 size_t read(void* buffer, size_t count);
133 const Vector<uint8_t, 512>& m_eotHeader;
134 const SharedBuffer* m_fontData;
137 size_t m_overlayLength;
142 size_t EOTStream::read(void* buffer, size_t count)
144 size_t bytesToRead = count;
146 size_t bytesFromHeader = min(m_eotHeader.size() - m_offset, count);
147 memcpy(buffer, m_eotHeader.data() + m_offset, bytesFromHeader);
148 m_offset += bytesFromHeader;
149 bytesToRead -= bytesFromHeader;
150 if (m_offset == m_eotHeader.size()) {
155 if (bytesToRead && !m_inHeader) {
156 size_t bytesFromData = min(m_fontData->size() - m_offset, bytesToRead);
157 memcpy(buffer, m_fontData->data() + m_offset, bytesFromData);
158 if (m_offset < m_overlayDst + m_overlayLength && m_offset + bytesFromData >= m_overlayDst) {
159 size_t dstOffset = max<int>(m_overlayDst - m_offset, 0);
160 size_t srcOffset = max<int>(0, m_offset - m_overlayDst);
161 size_t bytesToCopy = min(bytesFromData - dstOffset, m_overlayLength - srcOffset);
162 memcpy(reinterpret_cast<char*>(buffer) + dstOffset, m_fontData->data() + m_overlaySrc + srcOffset, bytesToCopy);
164 m_offset += bytesFromData;
165 bytesToRead -= bytesFromData;
167 return count - bytesToRead;
170 static unsigned long WINAPIV readEmbedProc(void* stream, void* buffer, unsigned long length)
172 return static_cast<EOTStream*>(stream)->read(buffer, length);
175 // Creates a unique and unpredictable font name, in order to avoid collisions and to
176 // not allow access from CSS.
177 static String createUniqueFontName()
179 Vector<char> fontUuid(sizeof(GUID));
180 CoCreateGuid(reinterpret_cast<GUID*>(fontUuid.data()));
182 Vector<char> fontNameVector;
183 base64Encode(fontUuid, fontNameVector);
184 ASSERT(fontNameVector.size() < LF_FACESIZE);
185 return String(fontNameVector.data(), fontNameVector.size());
188 FontCustomPlatformData* createFontCustomPlatformData(SharedBuffer* buffer)
190 ASSERT_ARG(buffer, buffer);
191 ASSERT(T2embedLibrary());
193 // Get CG to create the font.
194 CGDataProviderDirectAccessCallbacks callbacks = { &getData, &releaseData, &getBytesWithOffset, NULL };
195 RetainPtr<CGDataProviderRef> dataProvider(AdoptCF, CGDataProviderCreateDirectAccess(buffer, buffer->size(), &callbacks));
196 CGFontRef cgFont = CGFontCreateWithDataProvider(dataProvider.get());
200 // Introduce the font to GDI. AddFontMemResourceEx cannot be used, because it will pollute the process's
201 // font namespace (Windows has no API for creating an HFONT from data without exposing the font to the
202 // entire process first). TTLoadEmbeddedFont lets us override the font family name, so using a unique name
203 // we avoid namespace collisions.
205 String fontName = createUniqueFontName();
207 // TTLoadEmbeddedFont works only with Embedded OpenType (.eot) data, so we need to create an EOT header
208 // and prepend it to the font data.
209 Vector<uint8_t, 512> eotHeader;
212 size_t overlayLength;
213 if (!getEOTHeader(buffer, eotHeader, overlayDst, overlaySrc, overlayLength)) {
214 CGFontRelease(cgFont);
218 HANDLE fontReference;
221 EOTStream eotStream(eotHeader, buffer, overlayDst, overlaySrc, overlayLength);
223 LONG loadEmbeddedFontResult = TTLoadEmbeddedFont(&fontReference, TTLOAD_PRIVATE, &privStatus, LICENSE_PREVIEWPRINT, &status, readEmbedProc, &eotStream, const_cast<LPWSTR>(fontName.charactersWithNullTermination()), 0, 0);
224 if (loadEmbeddedFontResult == E_NONE)
227 fontReference = renameAndActivateFont(buffer, fontName);
228 if (!fontReference) {
229 CGFontRelease(cgFont);
234 return new FontCustomPlatformData(cgFont, fontReference, fontName);