Home | History | Annotate | Download | only in win
      1 /*
      2  * Copyright (C) 2007, 2008, 2009 Apple Inc. All rights reserved.
      3  *
      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.
      8  *
      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.
     13  *
     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.
     18  *
     19  */
     20 
     21 #include "config.h"
     22 #include "FontCustomPlatformData.h"
     23 
     24 #include "Base64.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>
     32 
     33 // From t2embapi.h, which is missing from the Microsoft Platform SDK.
     34 typedef unsigned long(WINAPIV *READEMBEDPROC) (void*, void*, unsigned long);
     35 struct TTLOADINFO;
     36 #define TTLOAD_PRIVATE 0x00000001
     37 #define LICENSE_PREVIEWPRINT 0x0004
     38 #define E_NONE 0x0000L
     39 
     40 namespace WebCore {
     41 
     42 using namespace std;
     43 
     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));
     48 
     49 FontCustomPlatformData::~FontCustomPlatformData()
     50 {
     51     CGFontRelease(m_cgFont);
     52     if (m_fontReference) {
     53         if (m_name.isNull()) {
     54             ASSERT(T2embedLibrary());
     55             ULONG status;
     56             TTDeleteEmbeddedFont(m_fontReference, 0, &status);
     57         } else
     58             RemoveFontMemResourceEx(m_fontReference);
     59     }
     60 }
     61 
     62 FontPlatformData FontCustomPlatformData::fontPlatformData(int size, bool bold, bool italic, FontRenderingMode renderingMode)
     63 {
     64     ASSERT(wkCanCreateCGFontWithLOGFONT() || m_cgFont);
     65     ASSERT(m_fontReference);
     66     ASSERT(T2embedLibrary());
     67 
     68     LOGFONT& logFont = *static_cast<LOGFONT*>(malloc(sizeof(LOGFONT)));
     69     if (m_name.isNull())
     70         TTGetNewFontName(&m_fontReference, logFont.lfFaceName, LF_FACESIZE, 0, 0);
     71     else
     72         memcpy(logFont.lfFaceName, m_name.charactersWithNullTermination(), sizeof(logFont.lfFaceName[0]) * min(static_cast<size_t>(LF_FACESIZE), 1 + m_name.length()));
     73 
     74     logFont.lfHeight = -size;
     75     if (renderingMode == NormalRenderingMode)
     76         logFont.lfHeight *= 32;
     77     logFont.lfWidth = 0;
     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;
     88 
     89     HFONT hfont = CreateFontIndirect(&logFont);
     90 
     91     if (wkCanCreateCGFontWithLOGFONT()) {
     92         RetainPtr<CGFontRef> cgFont(AdoptCF, CGFontCreateWithPlatformFont(&logFont));
     93         return FontPlatformData(hfont, cgFont.get(), size, bold, italic, renderingMode == AlternateRenderingMode);
     94     }
     95 
     96     wkSetFontPlatformInfo(m_cgFont, &logFont, free);
     97     return FontPlatformData(hfont, m_cgFont, size, bold, italic, renderingMode == AlternateRenderingMode);
     98 }
     99 
    100 const void* getData(void* info)
    101 {
    102     SharedBuffer* buffer = static_cast<SharedBuffer*>(info);
    103     buffer->ref();
    104     return (void*)buffer->data();
    105 }
    106 
    107 void releaseData(void* info, const void* data)
    108 {
    109     static_cast<SharedBuffer*>(info)->deref();
    110 }
    111 
    112 size_t getBytesWithOffset(void *info, void* buffer, size_t offset, size_t count)
    113 {
    114     SharedBuffer* sharedBuffer = static_cast<SharedBuffer*>(info);
    115     size_t availBytes = count;
    116     if (offset + count > sharedBuffer->size())
    117         availBytes -= (offset + count) - sharedBuffer->size();
    118     memcpy(buffer, sharedBuffer->data() + offset, availBytes);
    119     return availBytes;
    120 }
    121 
    122 // Streams the concatenation of a header and font data.
    123 class EOTStream {
    124 public:
    125     EOTStream(const EOTHeader& eotHeader, const SharedBuffer* fontData, size_t overlayDst, size_t overlaySrc, size_t overlayLength)
    126         : m_eotHeader(eotHeader)
    127         , m_fontData(fontData)
    128         , m_overlayDst(overlayDst)
    129         , m_overlaySrc(overlaySrc)
    130         , m_overlayLength(overlayLength)
    131         , m_offset(0)
    132         , m_inHeader(true)
    133     {
    134     }
    135 
    136     size_t read(void* buffer, size_t count);
    137 
    138 private:
    139     const EOTHeader& m_eotHeader;
    140     const SharedBuffer* m_fontData;
    141     size_t m_overlayDst;
    142     size_t m_overlaySrc;
    143     size_t m_overlayLength;
    144     size_t m_offset;
    145     bool m_inHeader;
    146 };
    147 
    148 size_t EOTStream::read(void* buffer, size_t count)
    149 {
    150     size_t bytesToRead = count;
    151     if (m_inHeader) {
    152         size_t bytesFromHeader = min(m_eotHeader.size() - m_offset, count);
    153         memcpy(buffer, m_eotHeader.data() + m_offset, bytesFromHeader);
    154         m_offset += bytesFromHeader;
    155         bytesToRead -= bytesFromHeader;
    156         if (m_offset == m_eotHeader.size()) {
    157             m_inHeader = false;
    158             m_offset = 0;
    159         }
    160     }
    161     if (bytesToRead && !m_inHeader) {
    162         size_t bytesFromData = min(m_fontData->size() - m_offset, bytesToRead);
    163         memcpy(buffer, m_fontData->data() + m_offset, bytesFromData);
    164         if (m_offset < m_overlayDst + m_overlayLength && m_offset + bytesFromData >= m_overlayDst) {
    165             size_t dstOffset = max<int>(m_overlayDst - m_offset, 0);
    166             size_t srcOffset = max<int>(0, m_offset - m_overlayDst);
    167             size_t bytesToCopy = min(bytesFromData - dstOffset, m_overlayLength - srcOffset);
    168             memcpy(reinterpret_cast<char*>(buffer) + dstOffset, m_fontData->data() + m_overlaySrc + srcOffset, bytesToCopy);
    169         }
    170         m_offset += bytesFromData;
    171         bytesToRead -= bytesFromData;
    172     }
    173     return count - bytesToRead;
    174 }
    175 
    176 static unsigned long WINAPIV readEmbedProc(void* stream, void* buffer, unsigned long length)
    177 {
    178     return static_cast<EOTStream*>(stream)->read(buffer, length);
    179 }
    180 
    181 // Creates a unique and unpredictable font name, in order to avoid collisions and to
    182 // not allow access from CSS.
    183 static String createUniqueFontName()
    184 {
    185     Vector<char> fontUuid(sizeof(GUID));
    186     CoCreateGuid(reinterpret_cast<GUID*>(fontUuid.data()));
    187 
    188     Vector<char> fontNameVector;
    189     base64Encode(fontUuid, fontNameVector);
    190     ASSERT(fontNameVector.size() < LF_FACESIZE);
    191     return String(fontNameVector.data(), fontNameVector.size());
    192 }
    193 
    194 FontCustomPlatformData* createFontCustomPlatformData(SharedBuffer* buffer)
    195 {
    196     ASSERT_ARG(buffer, buffer);
    197     ASSERT(T2embedLibrary());
    198 
    199     RetainPtr<CGFontRef> cgFont;
    200     if (!wkCanCreateCGFontWithLOGFONT()) {
    201         // Get CG to create the font.
    202         CGDataProviderDirectAccessCallbacks callbacks = { &getData, &releaseData, &getBytesWithOffset, NULL };
    203         RetainPtr<CGDataProviderRef> dataProvider(AdoptCF, CGDataProviderCreateDirectAccess(buffer, buffer->size(), &callbacks));
    204         cgFont.adoptCF(CGFontCreateWithDataProvider(dataProvider.get()));
    205         if (!cgFont)
    206             return 0;
    207     }
    208 
    209     // Introduce the font to GDI. AddFontMemResourceEx cannot be used, because it will pollute the process's
    210     // font namespace (Windows has no API for creating an HFONT from data without exposing the font to the
    211     // entire process first). TTLoadEmbeddedFont lets us override the font family name, so using a unique name
    212     // we avoid namespace collisions.
    213 
    214     String fontName = createUniqueFontName();
    215 
    216     // TTLoadEmbeddedFont works only with Embedded OpenType (.eot) data, so we need to create an EOT header
    217     // and prepend it to the font data.
    218     EOTHeader eotHeader;
    219     size_t overlayDst;
    220     size_t overlaySrc;
    221     size_t overlayLength;
    222     if (!getEOTHeader(buffer, eotHeader, overlayDst, overlaySrc, overlayLength))
    223         return 0;
    224 
    225     HANDLE fontReference;
    226     ULONG privStatus;
    227     ULONG status;
    228     EOTStream eotStream(eotHeader, buffer, overlayDst, overlaySrc, overlayLength);
    229 
    230     LONG loadEmbeddedFontResult = TTLoadEmbeddedFont(&fontReference, TTLOAD_PRIVATE, &privStatus, LICENSE_PREVIEWPRINT, &status, readEmbedProc, &eotStream, const_cast<LPWSTR>(fontName.charactersWithNullTermination()), 0, 0);
    231     if (loadEmbeddedFontResult == E_NONE)
    232         fontName = String();
    233     else {
    234         fontReference = renameAndActivateFont(buffer, fontName);
    235         if (!fontReference)
    236             return 0;
    237     }
    238 
    239     return new FontCustomPlatformData(cgFont.releaseRef(), fontReference, fontName);
    240 }
    241 
    242 }
    243