Home | History | Annotate | Download | only in win
      1 /*
      2  * This file is part of the internal font implementation.  It should not be included by anyone other than
      3  * FontMac.cpp, FontWin.cpp and Font.cpp.
      4  *
      5  * Copyright (C) 2006, 2007, 2008, 2009 Apple Inc.
      6  *
      7  * This library is free software; you can redistribute it and/or
      8  * modify it under the terms of the GNU Library General Public
      9  * License as published by the Free Software Foundation; either
     10  * version 2 of the License, or (at your option) any later version.
     11  *
     12  * This library is distributed in the hope that it will be useful,
     13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     15  * Library General Public License for more details.
     16  *
     17  * You should have received a copy of the GNU Library General Public License
     18  * along with this library; see the file COPYING.LIB.  If not, write to
     19  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
     20  * Boston, MA 02110-1301, USA.
     21  *
     22  */
     23 
     24 #include "config.h"
     25 #include "FontPlatformData.h"
     26 
     27 #include "PlatformString.h"
     28 #include "StringHash.h"
     29 #include <ApplicationServices/ApplicationServices.h>
     30 #include <WebKitSystemInterface/WebKitSystemInterface.h>
     31 #include <wtf/HashMap.h>
     32 #include <wtf/RetainPtr.h>
     33 #include <wtf/Vector.h>
     34 
     35 using std::min;
     36 
     37 namespace WebCore {
     38 
     39 static inline USHORT readBigEndianWord(const BYTE* word) { return (word[0] << 8) | word[1]; }
     40 
     41 static CFStringRef getPostScriptName(CFStringRef faceName, HDC dc)
     42 {
     43     const DWORD cMaxNameTableSize = 1024 * 1024;
     44 
     45     static HashMap<String, RetainPtr<CFStringRef> > nameMap;
     46 
     47     // Check our hash first.
     48     String faceString(faceName);
     49     RetainPtr<CFStringRef> result = nameMap.get(faceString);
     50     if (result)
     51         return result.get();
     52 
     53     // We need to obtain the PostScript name from the name table and use it instead,.
     54     DWORD bufferSize = GetFontData(dc, 'eman', 0, NULL, 0); // "name" backwards
     55     if (bufferSize == 0 || bufferSize == GDI_ERROR || bufferSize > cMaxNameTableSize)
     56         return NULL;
     57 
     58     Vector<BYTE> bufferVector(bufferSize);
     59     BYTE* buffer = bufferVector.data();
     60     if (GetFontData(dc, 'eman', 0, buffer, bufferSize) == GDI_ERROR)
     61         return NULL;
     62 
     63     if (bufferSize < 6)
     64         return NULL;
     65 
     66     USHORT numberOfRecords = readBigEndianWord(buffer + 2);
     67     UINT stringsOffset = readBigEndianWord(buffer + 4);
     68     if (bufferSize < stringsOffset)
     69         return NULL;
     70 
     71     BYTE* strings = buffer + stringsOffset;
     72 
     73     // Now walk each name record looking for a Mac or Windows PostScript name.
     74     UINT offset = 6;
     75     for (int i = 0; i < numberOfRecords; i++) {
     76         if (bufferSize < offset + 12)
     77             return NULL;
     78 
     79         USHORT platformID = readBigEndianWord(buffer + offset);
     80         USHORT encodingID = readBigEndianWord(buffer + offset + 2);
     81         USHORT languageID = readBigEndianWord(buffer + offset + 4);
     82         USHORT nameID = readBigEndianWord(buffer + offset + 6);
     83         USHORT length = readBigEndianWord(buffer + offset + 8);
     84         USHORT nameOffset = readBigEndianWord(buffer + offset + 10);
     85 
     86         if (platformID == 3 && encodingID == 1 && languageID == 0x409 && nameID == 6) {
     87             // This is a Windows PostScript name and is therefore UTF-16.
     88             // Pass Big Endian as the encoding.
     89             if (bufferSize < stringsOffset + nameOffset + length)
     90                 return NULL;
     91             result.adoptCF(CFStringCreateWithBytes(NULL, strings + nameOffset, length, kCFStringEncodingUTF16BE, false));
     92             break;
     93         } else if (platformID == 1 && encodingID == 0 && languageID == 0 && nameID == 6) {
     94             // This is a Mac PostScript name and is therefore ASCII.
     95             // See http://developer.apple.com/textfonts/TTRefMan/RM06/Chap6name.html
     96             if (bufferSize < stringsOffset + nameOffset + length)
     97                 return NULL;
     98             result.adoptCF(CFStringCreateWithBytes(NULL, strings + nameOffset, length, kCFStringEncodingASCII, false));
     99             break;
    100         }
    101 
    102         offset += 12;
    103     }
    104 
    105     if (result)
    106         nameMap.set(faceString, result);
    107     return result.get();
    108 }
    109 
    110 void FontPlatformData::platformDataInit(HFONT font, float size, HDC hdc, WCHAR* faceName)
    111 {
    112     if (wkCanCreateCGFontWithLOGFONT()) {
    113         LOGFONT logfont;
    114         GetObject(font, sizeof(logfont), &logfont);
    115         m_cgFont.adoptCF(CGFontCreateWithPlatformFont(&logfont));
    116         return;
    117     }
    118 
    119     // Try the face name first.  Windows may end up localizing this name, and CG doesn't know about
    120     // the localization.  If the create fails, we'll try the PostScript name.
    121     RetainPtr<CFStringRef> fullName(AdoptCF, CFStringCreateWithCharacters(NULL, (const UniChar*)faceName, wcslen(faceName)));
    122     m_cgFont.adoptCF(CGFontCreateWithFontName(fullName.get()));
    123     if (!m_cgFont) {
    124         CFStringRef postScriptName = getPostScriptName(fullName.get(), hdc);
    125         if (postScriptName) {
    126             m_cgFont.adoptCF(CGFontCreateWithFontName(postScriptName));
    127             ASSERT(m_cgFont);
    128         }
    129     }
    130     if (m_useGDI) {
    131         LOGFONT* logfont = static_cast<LOGFONT*>(malloc(sizeof(LOGFONT)));
    132         GetObject(font, sizeof(*logfont), logfont);
    133         wkSetFontPlatformInfo(m_cgFont.get(), logfont, free);
    134     }
    135 }
    136 
    137 FontPlatformData::FontPlatformData(HFONT hfont, CGFontRef font, float size, bool bold, bool oblique, bool useGDI)
    138     : m_font(RefCountedHFONT::create(hfont))
    139     , m_size(size)
    140     , m_cgFont(font)
    141     , m_syntheticBold(bold)
    142     , m_syntheticOblique(oblique)
    143     , m_useGDI(useGDI)
    144 {
    145 }
    146 
    147 FontPlatformData::~FontPlatformData()
    148 {
    149 }
    150 
    151 }
    152