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 "SharedGDIObject.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#include <wtf/text/WTFString.h>
35
36using std::min;
37
38namespace WebCore {
39
40static inline USHORT readBigEndianWord(const BYTE* word) { return (word[0] << 8) | word[1]; }
41
42static CFStringRef getPostScriptName(CFStringRef faceName, HDC dc)
43{
44    const DWORD cMaxNameTableSize = 1024 * 1024;
45
46    static HashMap<String, RetainPtr<CFStringRef> > nameMap;
47
48    // Check our hash first.
49    String faceString(faceName);
50    RetainPtr<CFStringRef> result = nameMap.get(faceString);
51    if (result)
52        return result.get();
53
54    // We need to obtain the PostScript name from the name table and use it instead,.
55    DWORD bufferSize = GetFontData(dc, 'eman', 0, NULL, 0); // "name" backwards
56    if (bufferSize == 0 || bufferSize == GDI_ERROR || bufferSize > cMaxNameTableSize)
57        return NULL;
58
59    Vector<BYTE> bufferVector(bufferSize);
60    BYTE* buffer = bufferVector.data();
61    if (GetFontData(dc, 'eman', 0, buffer, bufferSize) == GDI_ERROR)
62        return NULL;
63
64    if (bufferSize < 6)
65        return NULL;
66
67    USHORT numberOfRecords = readBigEndianWord(buffer + 2);
68    UINT stringsOffset = readBigEndianWord(buffer + 4);
69    if (bufferSize < stringsOffset)
70        return NULL;
71
72    BYTE* strings = buffer + stringsOffset;
73
74    // Now walk each name record looking for a Mac or Windows PostScript name.
75    UINT offset = 6;
76    for (int i = 0; i < numberOfRecords; i++) {
77        if (bufferSize < offset + 12)
78            return NULL;
79
80        USHORT platformID = readBigEndianWord(buffer + offset);
81        USHORT encodingID = readBigEndianWord(buffer + offset + 2);
82        USHORT languageID = readBigEndianWord(buffer + offset + 4);
83        USHORT nameID = readBigEndianWord(buffer + offset + 6);
84        USHORT length = readBigEndianWord(buffer + offset + 8);
85        USHORT nameOffset = readBigEndianWord(buffer + offset + 10);
86
87        if (platformID == 3 && encodingID == 1 && languageID == 0x409 && nameID == 6) {
88            // This is a Windows PostScript name and is therefore UTF-16.
89            // Pass Big Endian as the encoding.
90            if (bufferSize < stringsOffset + nameOffset + length)
91                return NULL;
92            result = adoptCF(CFStringCreateWithBytes(NULL, strings + nameOffset, length, kCFStringEncodingUTF16BE, false));
93            break;
94        } else if (platformID == 1 && encodingID == 0 && languageID == 0 && nameID == 6) {
95            // This is a Mac PostScript name and is therefore ASCII.
96            // See http://developer.apple.com/textfonts/TTRefMan/RM06/Chap6name.html
97            if (bufferSize < stringsOffset + nameOffset + length)
98                return NULL;
99            result = adoptCF(CFStringCreateWithBytes(NULL, strings + nameOffset, length, kCFStringEncodingASCII, false));
100            break;
101        }
102
103        offset += 12;
104    }
105
106    if (result)
107        nameMap.set(faceString, result);
108    return result.get();
109}
110
111void FontPlatformData::platformDataInit(HFONT font, float size, HDC hdc, WCHAR* faceName)
112{
113    LOGFONT logfont;
114    GetObject(font, sizeof(logfont), &logfont);
115    m_cgFont = adoptCF(CGFontCreateWithPlatformFont(&logfont));
116}
117
118FontPlatformData::FontPlatformData(GDIObject<HFONT> hfont, CGFontRef font, float size, bool bold, bool oblique, bool useGDI)
119    : m_syntheticBold(bold)
120    , m_syntheticOblique(oblique)
121    , m_orientation(Horizontal)
122    , m_size(size)
123    , m_widthVariant(RegularWidth)
124    , m_font(SharedGDIObject<HFONT>::create(WTF::move(hfont)))
125    , m_cgFont(font)
126    , m_isColorBitmapFont(false)
127    , m_isCompositeFontReference(false)
128    , m_useGDI(useGDI)
129{
130}
131
132FontPlatformData::~FontPlatformData()
133{
134}
135
136void FontPlatformData::platformDataInit(const FontPlatformData& source)
137{
138    m_font = source.m_font;
139    m_cgFont = source.m_cgFont;
140    m_useGDI = source.m_useGDI;
141}
142
143const FontPlatformData& FontPlatformData::platformDataAssign(const FontPlatformData& other)
144{
145    m_font = other.m_font;
146    m_cgFont = other.m_cgFont;
147    m_useGDI = other.m_useGDI;
148
149    return *this;
150}
151
152bool FontPlatformData::platformIsEqual(const FontPlatformData& other) const
153{
154    return m_font == other.m_font
155        && m_cgFont == other.m_cgFont
156        && m_useGDI == other.m_useGDI;
157}
158
159}
160