]>
Commit | Line | Data |
---|---|---|
32b8ec41 VZ |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: font.cpp | |
3 | // Author: Vaclav Slavik | |
4 | // Id: $Id$ | |
8f7b34a8 | 5 | // Copyright: (c) 2001 SciTech Software, Inc. (www.scitechsoft.com) |
32b8ec41 VZ |
6 | // Licence: wxWindows licence |
7 | ///////////////////////////////////////////////////////////////////////////// | |
8 | ||
9 | // ============================================================================ | |
10 | // declarations | |
11 | // ============================================================================ | |
12 | ||
13 | // ---------------------------------------------------------------------------- | |
14 | // headers | |
15 | // ---------------------------------------------------------------------------- | |
16 | ||
17 | #ifdef __GNUG__ | |
18 | #pragma implementation "font.h" | |
19 | #endif | |
20 | ||
21 | #include "wx/font.h" | |
22 | #include "wx/fontutil.h" | |
23 | #include "wx/cmndata.h" | |
24 | #include "wx/utils.h" | |
25 | #include "wx/log.h" | |
26 | #include "wx/gdicmn.h" | |
27 | #include "wx/tokenzr.h" | |
28 | #include "wx/settings.h" | |
29 | ||
30 | #include <strings.h> | |
31 | ||
32 | // ---------------------------------------------------------------------------- | |
33 | // wxFontRefData | |
34 | // ---------------------------------------------------------------------------- | |
35 | ||
36 | class wxFontRefData : public wxObjectRefData | |
37 | { | |
38 | public: | |
39 | wxFontRefData(int size = wxDEFAULT, | |
40 | int family = wxDEFAULT, | |
41 | int style = wxDEFAULT, | |
42 | int weight = wxDEFAULT, | |
43 | bool underlined = FALSE, | |
44 | const wxString& faceName = wxEmptyString, | |
45 | wxFontEncoding encoding = wxFONTENCODING_DEFAULT); | |
46 | wxFontRefData(const wxFontRefData& data); | |
47 | virtual ~wxFontRefData(); | |
48 | ||
49 | protected: | |
50 | // common part of all ctors | |
51 | void Init(int pointSize, | |
52 | int family, | |
53 | int style, | |
54 | int weight, | |
55 | bool underlined, | |
56 | const wxString& faceName, | |
57 | wxFontEncoding encoding); | |
58 | ||
59 | private: | |
60 | int m_pointSize; | |
61 | int m_family, | |
62 | m_style, | |
63 | m_weight; | |
64 | bool m_underlined; | |
65 | wxString m_faceName; | |
66 | wxFontEncoding m_encoding; | |
67 | ||
68 | wxMGLFontLibrary *m_library; | |
69 | bool m_valid; | |
70 | ||
71 | friend class wxFont; | |
72 | }; | |
73 | ||
74 | // ============================================================================ | |
75 | // implementation | |
76 | // ============================================================================ | |
77 | ||
78 | // ---------------------------------------------------------------------------- | |
79 | // wxFontRefData | |
80 | // ---------------------------------------------------------------------------- | |
81 | ||
82 | void wxFontRefData::Init(int pointSize, | |
83 | int family, | |
84 | int style, | |
85 | int weight, | |
86 | bool underlined, | |
87 | const wxString& faceName, | |
88 | wxFontEncoding encoding) | |
89 | { | |
90 | if ( family == wxDEFAULT ) | |
91 | m_family = wxSWISS; | |
92 | else | |
93 | m_family = family; | |
94 | ||
95 | m_faceName = faceName; | |
96 | ||
97 | if ( style == wxDEFAULT ) | |
98 | m_style = wxNORMAL; | |
99 | else | |
100 | m_style = style; | |
101 | ||
102 | if ( weight == wxDEFAULT ) | |
103 | m_weight = wxNORMAL; | |
104 | else | |
105 | m_weight = weight; | |
106 | ||
107 | if ( pointSize == wxDEFAULT ) | |
108 | m_pointSize = 12; | |
109 | else | |
110 | m_pointSize = pointSize; | |
111 | ||
112 | m_underlined = underlined; | |
113 | m_encoding = encoding; | |
114 | ||
115 | m_library = NULL; | |
116 | m_valid = FALSE; | |
117 | } | |
118 | ||
119 | wxFontRefData::wxFontRefData(const wxFontRefData& data) | |
120 | { | |
121 | Init(data.m_pointSize, data.m_family, data.m_style, data.m_weight, | |
122 | data.m_underlined, data.m_faceName, data.m_encoding); | |
123 | ||
124 | m_library = data.m_library; | |
125 | m_valid = data.m_valid; | |
126 | if ( m_library ) | |
127 | m_library->IncRef(); | |
ef344ff8 | 128 | wxLogTrace("mgl_font", "created fntrefdata %p, library is %p", this, m_library); |
32b8ec41 VZ |
129 | } |
130 | ||
131 | wxFontRefData::wxFontRefData(int size, int family, int style, | |
132 | int weight, bool underlined, | |
133 | const wxString& faceName, | |
134 | wxFontEncoding encoding) | |
135 | { | |
136 | Init(size, family, style, weight, underlined, faceName, encoding); | |
ef344ff8 | 137 | wxLogTrace("mgl_font", "created fntrefdata %p, library is %p", this, m_library); |
32b8ec41 VZ |
138 | } |
139 | ||
140 | wxFontRefData::~wxFontRefData() | |
141 | { | |
ef344ff8 | 142 | wxLogTrace("mgl_font", "destructing fntrefdata %p, library is %p", this, m_library); |
32b8ec41 VZ |
143 | if ( m_library ) |
144 | m_library->DecRef(); | |
145 | } | |
146 | ||
147 | // ---------------------------------------------------------------------------- | |
148 | // wxFont | |
149 | // ---------------------------------------------------------------------------- | |
150 | ||
151 | IMPLEMENT_DYNAMIC_CLASS(wxFont, wxGDIObject) | |
152 | ||
32b8ec41 VZ |
153 | bool wxFont::Create(const wxNativeFontInfo& info) |
154 | { | |
155 | return Create(info.pointSize, info.family, info.style, info.weight, | |
156 | info.underlined, info.faceName, info.encoding); | |
157 | } | |
158 | ||
159 | bool wxFont::Create(int pointSize, | |
160 | int family, | |
161 | int style, | |
162 | int weight, | |
163 | bool underlined, | |
164 | const wxString& face, | |
165 | wxFontEncoding encoding) | |
166 | { | |
167 | m_refData = new wxFontRefData(pointSize, family, style, weight, | |
168 | underlined, face, encoding); | |
169 | return TRUE; | |
170 | } | |
171 | ||
172 | struct font_t *wxFont::GetMGLfont_t(float scale, bool antialiased) | |
173 | { | |
174 | if ( !M_FONTDATA->m_valid ) | |
175 | { | |
176 | wxMGLFontLibrary *old = M_FONTDATA->m_library; | |
177 | M_FONTDATA->m_library = wxTheFontsManager->GetFontLibrary(this); | |
178 | M_FONTDATA->m_library->IncRef(); | |
179 | if ( old ) | |
180 | old->DecRef(); | |
181 | } | |
182 | ||
183 | wxMGLFontInstance *instance = | |
184 | M_FONTDATA->m_library->GetFontInstance(this, scale, antialiased); | |
185 | ||
186 | return instance->GetMGLfont_t(); | |
187 | } | |
188 | ||
189 | void wxFont::Unshare() | |
190 | { | |
191 | if ( !m_refData ) | |
192 | { | |
193 | m_refData = new wxFontRefData(); | |
194 | } | |
195 | else | |
196 | { | |
197 | wxFontRefData* ref = new wxFontRefData(*(wxFontRefData*)m_refData); | |
198 | UnRef(); | |
199 | m_refData = ref; | |
200 | } | |
201 | } | |
202 | ||
32b8ec41 VZ |
203 | // ---------------------------------------------------------------------------- |
204 | // accessors | |
205 | // ---------------------------------------------------------------------------- | |
206 | ||
207 | int wxFont::GetPointSize() const | |
208 | { | |
209 | wxCHECK_MSG( Ok(), 0, wxT("invalid font") ); | |
210 | ||
211 | return M_FONTDATA->m_pointSize; | |
212 | } | |
213 | ||
214 | wxString wxFont::GetFaceName() const | |
215 | { | |
216 | wxCHECK_MSG( Ok(), wxT(""), wxT("invalid font") ); | |
217 | ||
218 | return M_FONTDATA->m_faceName; | |
219 | } | |
220 | ||
221 | int wxFont::GetFamily() const | |
222 | { | |
223 | wxCHECK_MSG( Ok(), 0, wxT("invalid font") ); | |
224 | ||
225 | return M_FONTDATA->m_family; | |
226 | } | |
227 | ||
228 | int wxFont::GetStyle() const | |
229 | { | |
230 | wxCHECK_MSG( Ok(), 0, wxT("invalid font") ); | |
231 | ||
232 | return M_FONTDATA->m_style; | |
233 | } | |
234 | ||
235 | int wxFont::GetWeight() const | |
236 | { | |
237 | wxCHECK_MSG( Ok(), 0, wxT("invalid font") ); | |
238 | ||
239 | return M_FONTDATA->m_weight; | |
240 | } | |
241 | ||
242 | bool wxFont::GetUnderlined() const | |
243 | { | |
244 | wxCHECK_MSG( Ok(), FALSE, wxT("invalid font") ); | |
245 | ||
246 | return M_FONTDATA->m_underlined; | |
247 | } | |
248 | ||
249 | ||
250 | wxFontEncoding wxFont::GetEncoding() const | |
251 | { | |
252 | wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT, wxT("invalid font") ); | |
253 | ||
254 | return M_FONTDATA->m_encoding; | |
255 | } | |
256 | ||
257 | ||
258 | // ---------------------------------------------------------------------------- | |
259 | // change font attributes | |
260 | // ---------------------------------------------------------------------------- | |
261 | ||
262 | void wxFont::SetPointSize(int pointSize) | |
263 | { | |
264 | Unshare(); | |
265 | ||
266 | M_FONTDATA->m_pointSize = pointSize; | |
267 | M_FONTDATA->m_valid = FALSE; | |
268 | } | |
269 | ||
270 | void wxFont::SetFamily(int family) | |
271 | { | |
272 | Unshare(); | |
273 | ||
274 | M_FONTDATA->m_family = family; | |
275 | M_FONTDATA->m_valid = FALSE; | |
276 | } | |
277 | ||
278 | void wxFont::SetStyle(int style) | |
279 | { | |
280 | Unshare(); | |
281 | ||
282 | M_FONTDATA->m_style = style; | |
283 | M_FONTDATA->m_valid = FALSE; | |
284 | } | |
285 | ||
286 | void wxFont::SetWeight(int weight) | |
287 | { | |
288 | Unshare(); | |
289 | ||
290 | M_FONTDATA->m_weight = weight; | |
291 | M_FONTDATA->m_valid = FALSE; | |
292 | } | |
293 | ||
294 | void wxFont::SetFaceName(const wxString& faceName) | |
295 | { | |
296 | Unshare(); | |
297 | ||
298 | M_FONTDATA->m_faceName = faceName; | |
299 | M_FONTDATA->m_valid = FALSE; | |
300 | } | |
301 | ||
302 | void wxFont::SetUnderlined(bool underlined) | |
303 | { | |
304 | Unshare(); | |
305 | ||
306 | M_FONTDATA->m_underlined = underlined; | |
307 | } | |
308 | ||
309 | void wxFont::SetEncoding(wxFontEncoding encoding) | |
310 | { | |
311 | Unshare(); | |
312 | ||
313 | M_FONTDATA->m_encoding = encoding; | |
314 | M_FONTDATA->m_valid = FALSE; | |
315 | } |