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.
5
* Copyright (C) 2006, 2007, 2008, 2009 Apple Inc.
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.
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.
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.
25
#include "FontPlatformData.h"
27
#include <ApplicationServices/ApplicationServices.h>
28
#include <WebKitSystemInterface/WebKitSystemInterface.h>
29
#include <wtf/HashMap.h>
30
#include <wtf/RetainPtr.h>
31
#include <wtf/Vector.h>
32
#include <wtf/text/StringHash.h>
33
#include <wtf/text/WTFString.h>
39
static inline USHORT readBigEndianWord(const BYTE* word) { return (word[0] << 8) | word[1]; }
41
static CFStringRef getPostScriptName(CFStringRef faceName, HDC dc)
43
const DWORD cMaxNameTableSize = 1024 * 1024;
45
static HashMap<String, RetainPtr<CFStringRef> > nameMap;
47
// Check our hash first.
48
String faceString(faceName);
49
RetainPtr<CFStringRef> result = nameMap.get(faceString);
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)
58
Vector<BYTE> bufferVector(bufferSize);
59
BYTE* buffer = bufferVector.data();
60
if (GetFontData(dc, 'eman', 0, buffer, bufferSize) == GDI_ERROR)
66
USHORT numberOfRecords = readBigEndianWord(buffer + 2);
67
UINT stringsOffset = readBigEndianWord(buffer + 4);
68
if (bufferSize < stringsOffset)
71
BYTE* strings = buffer + stringsOffset;
73
// Now walk each name record looking for a Mac or Windows PostScript name.
75
for (int i = 0; i < numberOfRecords; i++) {
76
if (bufferSize < offset + 12)
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);
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)
91
result.adoptCF(CFStringCreateWithBytes(NULL, strings + nameOffset, length, kCFStringEncodingUTF16BE, false));
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)
98
result.adoptCF(CFStringCreateWithBytes(NULL, strings + nameOffset, length, kCFStringEncodingASCII, false));
106
nameMap.set(faceString, result);
110
void FontPlatformData::platformDataInit(HFONT font, float size, HDC hdc, WCHAR* faceName)
113
GetObject(font, sizeof(logfont), &logfont);
114
m_cgFont.adoptCF(CGFontCreateWithPlatformFont(&logfont));
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)
123
, m_widthVariant(RegularWidth)
124
, m_font(RefCountedGDIHandle<HFONT>::create(hfont))
126
, m_isColorBitmapFont(false)
131
FontPlatformData::~FontPlatformData()
135
void FontPlatformData::platformDataInit(const FontPlatformData& source)
137
m_font = source.m_font;
138
m_cgFont = source.m_cgFont;
139
m_useGDI = source.m_useGDI;
142
const FontPlatformData& FontPlatformData::platformDataAssign(const FontPlatformData& other)
144
m_font = other.m_font;
145
m_cgFont = other.m_cgFont;
146
m_useGDI = other.m_useGDI;
151
bool FontPlatformData::platformIsEqual(const FontPlatformData& other) const
153
return m_font == other.m_font
154
&& m_cgFont == other.m_cgFont
155
&& m_useGDI == other.m_useGDI;