Home | History | Annotate | Download | only in win
      1 /*
      2  * Copyright (C) 2007, 2008, 2009, 2010 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 "WOFFFileFormat.h"
     30 #include <ApplicationServices/ApplicationServices.h>
     31 #include <WebKitSystemInterface/WebKitSystemInterface.h>
     32 #include <wtf/RetainPtr.h>
     33 
     34 // From t2embapi.h, which is missing from the Microsoft Platform SDK.
     35 typedef unsigned long(WINAPIV *READEMBEDPROC) (void*, void*, unsigned long);
     36 struct TTLOADINFO;
     37 #define TTLOAD_PRIVATE 0x00000001
     38 #define LICENSE_PREVIEWPRINT 0x0004
     39 #define E_NONE 0x0000L
     40 
     41 namespace WebCore {
     42 
     43 using namespace std;
     44 
     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));
     49 
     50 FontCustomPlatformData::~FontCustomPlatformData()
     51 {
     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, FontOrientation, TextOrientation, FontWidthVariant, FontRenderingMode renderingMode)
     63 {
     64     ASSERT(m_fontReference);
     65     ASSERT(T2embedLibrary());
     66 
     67     LOGFONT& logFont = *static_cast<LOGFONT*>(malloc(sizeof(LOGFONT)));
     68     if (m_name.isNull())
     69         TTGetNewFontName(&m_fontReference, logFont.lfFaceName, LF_FACESIZE, 0, 0);
     70     else
     71         memcpy(logFont.lfFaceName, m_name.charactersWithNullTermination(), sizeof(logFont.lfFaceName[0]) * min(static_cast<size_t>(LF_FACESIZE), 1 + m_name.length()));
     72 
     73     logFont.lfHeight = -size;
     74     if (renderingMode == NormalRenderingMode)
     75         logFont.lfHeight *= 32;
     76     logFont.lfWidth = 0;
     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;
     87 
     88     HFONT hfont = CreateFontIndirect(&logFont);
     89 
     90     RetainPtr<CGFontRef> cgFont(AdoptCF, CGFontCreateWithPlatformFont(&logFont));
     91     return FontPlatformData(hfont, cgFont.get(), size, bold, italic, renderingMode == AlternateRenderingMode);
     92 }
     93 
     94 // Streams the concatenation of a header and font data.
     95 class EOTStream {
     96 public:
     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)
    103         , m_offset(0)
    104         , m_inHeader(true)
    105     {
    106     }
    107 
    108     size_t read(void* buffer, size_t count);
    109 
    110 private:
    111     const EOTHeader& m_eotHeader;
    112     const SharedBuffer* m_fontData;
    113     size_t m_overlayDst;
    114     size_t m_overlaySrc;
    115     size_t m_overlayLength;
    116     size_t m_offset;
    117     bool m_inHeader;
    118 };
    119 
    120 size_t EOTStream::read(void* buffer, size_t count)
    121 {
    122     size_t bytesToRead = count;
    123     if (m_inHeader) {
    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()) {
    129             m_inHeader = false;
    130             m_offset = 0;
    131         }
    132     }
    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);
    141         }
    142         m_offset += bytesFromData;
    143         bytesToRead -= bytesFromData;
    144     }
    145     return count - bytesToRead;
    146 }
    147 
    148 static unsigned long WINAPIV readEmbedProc(void* stream, void* buffer, unsigned long length)
    149 {
    150     return static_cast<EOTStream*>(stream)->read(buffer, length);
    151 }
    152 
    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()
    156 {
    157     GUID fontUuid;
    158     CoCreateGuid(&fontUuid);
    159 
    160     String fontName = base64Encode(reinterpret_cast<char*>(&fontUuid), sizeof(fontUuid));
    161     ASSERT(fontName.length() < LF_FACESIZE);
    162     return fontName;
    163 }
    164 
    165 FontCustomPlatformData* createFontCustomPlatformData(SharedBuffer* buffer)
    166 {
    167     ASSERT_ARG(buffer, buffer);
    168     ASSERT(T2embedLibrary());
    169 
    170     RefPtr<SharedBuffer> sfntBuffer;
    171     if (isWOFF(buffer)) {
    172         Vector<char> sfnt;
    173         if (!convertWOFFToSfnt(buffer, sfnt))
    174             return 0;
    175 
    176         sfntBuffer = SharedBuffer::adoptVector(sfnt);
    177         buffer = sfntBuffer.get();
    178     }
    179 
    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.
    184 
    185     String fontName = createUniqueFontName();
    186 
    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.
    189     EOTHeader eotHeader;
    190     size_t overlayDst;
    191     size_t overlaySrc;
    192     size_t overlayLength;
    193     if (!getEOTHeader(buffer, eotHeader, overlayDst, overlaySrc, overlayLength))
    194         return 0;
    195 
    196     HANDLE fontReference;
    197     ULONG privStatus;
    198     ULONG status;
    199     EOTStream eotStream(eotHeader, buffer, overlayDst, overlaySrc, overlayLength);
    200 
    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)
    203         fontName = String();
    204     else {
    205         fontReference = renameAndActivateFont(buffer, fontName);
    206         if (!fontReference)
    207             return 0;
    208     }
    209 
    210     return new FontCustomPlatformData(fontReference, fontName);
    211 }
    212 
    213 bool FontCustomPlatformData::supportsFormat(const String& format)
    214 {
    215     return equalIgnoringCase(format, "truetype") || equalIgnoringCase(format, "opentype") || equalIgnoringCase(format, "woff");
    216 }
    217 
    218 }
    219