1 /*
2 * Copyright (C) 2006 Kevin Ollivier All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
14 * its contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #ifndef FontPlatformData_h
30 #define FontPlatformData_h
31
32 #include "FontDescription.h"
33 #include "FontWidthVariant.h"
34 #include "FontOrientation.h"
35 #include "StringImpl.h"
36 #include <wtf/Forward.h>
37 #include <wtf/RefPtr.h>
38 #include <wtf/text/AtomicString.h>
39 #include <wtf/text/CString.h>
40
41 #include <wx/defs.h>
42 #include <wx/font.h>
43 #include <wx/gdicmn.h>
44
45 #if OS(DARWIN)
46 #include <ApplicationServices/ApplicationServices.h>
47
48 #if __OBJC__
49 @class NSFont;
50 #else
51 class NSFont;
52 #endif
53
54 #ifndef BUILDING_ON_TIGER
toCTFontRef(NSFont * nsFont)55 inline CTFontRef toCTFontRef(NSFont *nsFont) { return reinterpret_cast<CTFontRef>(nsFont); }
56 #endif
57
58 #endif
59
60 namespace WebCore {
61
62 class FontHolder: public WTF::RefCounted<FontHolder>
63 {
64 public:
FontHolder()65 FontHolder()
66 : m_font(0)
67 {}
68
FontHolder(wxFont * font)69 FontHolder(wxFont* font)
70 : m_font(font)
71 {}
72
font()73 wxFont* font() { return m_font; }
74
75 private:
76 wxFont* m_font;
77 };
78
79 class FontPlatformData {
80 public:
81 enum FontState { UNINITIALIZED, DELETED, VALID };
82
FontPlatformData(WTF::HashTableDeletedValueType)83 FontPlatformData(WTF::HashTableDeletedValueType)
84 : m_fontState(DELETED)
85 , m_font(0)
86 , m_size(0)
87 #if OS(DARWIN)
88 , m_atsuFontID(0)
89 #endif
90 { }
91
92 ~FontPlatformData();
93
94 FontPlatformData(const FontDescription&, const AtomicString&);
95
FontPlatformData(float size,bool bold,bool italic)96 FontPlatformData(float size, bool bold, bool italic)
97 : m_fontState(UNINITIALIZED)
98 , m_font(0)
99 , m_size(size)
100 #if OS(DARWIN)
101 , m_atsuFontID(0)
102 #endif
103 {
104 }
105
FontPlatformData()106 FontPlatformData()
107 : m_fontState(UNINITIALIZED)
108 , m_font(0)
109 , m_size(0)
110 #if OS(DARWIN)
111 , m_atsuFontID(0)
112 #endif
113 {
114 }
115
font()116 wxFont* font() const {
117 return m_font->font();
118 }
119
hash()120 unsigned hash() const {
121 switch (m_fontState) {
122 case DELETED:
123 return -1;
124 case UNINITIALIZED:
125 return 0;
126 case VALID:
127 return computeHash();
128 }
129 }
130
131 unsigned computeHash() const;
132
size()133 float size() const { return m_size; }
134
135 bool operator==(const FontPlatformData& other) const
136 {
137 if (m_font && m_fontState == VALID && other.m_fontState == VALID && other.m_font) {
138 wxFont* thisFont = m_font->font();
139 wxFont* otherFont = other.m_font->font();
140 return thisFont->IsOk() && otherFont->IsOk() && thisFont->IsSameAs(*otherFont);
141 }
142 else
143 return m_fontState == other.m_fontState;
144 }
145
isHashTableDeletedValue()146 bool isHashTableDeletedValue() const { return m_fontState == DELETED; }
147
roundsGlyphAdvances()148 bool roundsGlyphAdvances() const { return false; }
149
allowsLigatures()150 bool allowsLigatures() const { return false; }
151
orientation()152 FontOrientation orientation() const { return Horizontal; } // FIXME: Implement.
setOrientation(FontOrientation)153 void setOrientation(FontOrientation) { } // FIXME: Implement.
154
155 // We don't support this yet, so just return the default value for now.
widthVariant()156 FontWidthVariant widthVariant() const { return RegularWidth; }
157
158 #if OS(WINDOWS)
159 bool useGDI() const;
160 HFONT hfont() const;
161 #endif
162
163 #if OS(DARWIN)
164 ATSUFontID m_atsuFontID;
165 CGFontRef cgFont() const;
nsFont()166 NSFont* nsFont() const { return m_nsFont; }
ctFont()167 CTFontRef ctFont() const { return reinterpret_cast<CTFontRef>(m_nsFont); }
168 void cacheNSFont();
169 #endif
170
171 float m_size;
172
173 #ifndef NDEBUG
174 String description() const;
175 #endif
176
177 private:
178 WTF::RefPtr<FontHolder> m_font;
179 FontState m_fontState;
180 #if OS(DARWIN)
181 NSFont* m_nsFont;
182 #endif
183 };
184
185 }
186
187 #endif
188