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 <ApplicationServices/ApplicationServices.h> 29 #include <WebKitSystemInterface/WebKitSystemInterface.h> 30 #include <wtf/HashMap.h> 31 #include <wtf/RetainPtr.h> 32 #include <wtf/Vector.h> 33 #include <wtf/text/StringHash.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 LOGFONT logfont; 113 GetObject(font, sizeof(logfont), &logfont); 114 m_cgFont.adoptCF(CGFontCreateWithPlatformFont(&logfont)); 115 } 116 117 FontPlatformData::FontPlatformData(HFONT hfont, CGFontRef font, float size, bool bold, bool oblique, bool useGDI) 118 : m_syntheticBold(bold) 119 , m_syntheticOblique(oblique) 120 , m_orientation(Horizontal) 121 , m_textOrientation(TextOrientationVerticalRight) 122 , m_size(size) 123 , m_widthVariant(RegularWidth) 124 , m_font(RefCountedGDIHandle<HFONT>::create(hfont)) 125 , m_cgFont(font) 126 , m_isColorBitmapFont(false) 127 , m_useGDI(useGDI) 128 { 129 } 130 131 FontPlatformData::~FontPlatformData() 132 { 133 } 134 135 void FontPlatformData::platformDataInit(const FontPlatformData& source) 136 { 137 m_font = source.m_font; 138 m_cgFont = source.m_cgFont; 139 m_useGDI = source.m_useGDI; 140 } 141 142 const FontPlatformData& FontPlatformData::platformDataAssign(const FontPlatformData& other) 143 { 144 m_font = other.m_font; 145 m_cgFont = other.m_cgFont; 146 m_useGDI = other.m_useGDI; 147 148 return *this; 149 } 150 151 bool FontPlatformData::platformIsEqual(const FontPlatformData& other) const 152 { 153 return m_font == other.m_font 154 && m_cgFont == other.m_cgFont 155 && m_useGDI == other.m_useGDI; 156 } 157 158 } 159