]>
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 | ||
153 | void wxFont::Init() | |
154 | { | |
155 | if (wxTheFontList) | |
156 | wxTheFontList->Append(this); | |
157 | } | |
158 | ||
159 | bool wxFont::Create(const wxNativeFontInfo& info) | |
160 | { | |
161 | return Create(info.pointSize, info.family, info.style, info.weight, | |
162 | info.underlined, info.faceName, info.encoding); | |
163 | } | |
164 | ||
165 | bool wxFont::Create(int pointSize, | |
166 | int family, | |
167 | int style, | |
168 | int weight, | |
169 | bool underlined, | |
170 | const wxString& face, | |
171 | wxFontEncoding encoding) | |
172 | { | |
173 | m_refData = new wxFontRefData(pointSize, family, style, weight, | |
174 | underlined, face, encoding); | |
175 | return TRUE; | |
176 | } | |
177 | ||
178 | struct font_t *wxFont::GetMGLfont_t(float scale, bool antialiased) | |
179 | { | |
1ac162ad | 180 | // FIXME_MGL -- no antialiasing for fonts smaller than certain treshold! |
32b8ec41 VZ |
181 | if ( !M_FONTDATA->m_valid ) |
182 | { | |
183 | wxMGLFontLibrary *old = M_FONTDATA->m_library; | |
184 | M_FONTDATA->m_library = wxTheFontsManager->GetFontLibrary(this); | |
185 | M_FONTDATA->m_library->IncRef(); | |
186 | if ( old ) | |
187 | old->DecRef(); | |
188 | } | |
189 | ||
190 | wxMGLFontInstance *instance = | |
191 | M_FONTDATA->m_library->GetFontInstance(this, scale, antialiased); | |
192 | ||
193 | return instance->GetMGLfont_t(); | |
194 | } | |
195 | ||
196 | void wxFont::Unshare() | |
197 | { | |
198 | if ( !m_refData ) | |
199 | { | |
200 | m_refData = new wxFontRefData(); | |
201 | } | |
202 | else | |
203 | { | |
204 | wxFontRefData* ref = new wxFontRefData(*(wxFontRefData*)m_refData); | |
205 | UnRef(); | |
206 | m_refData = ref; | |
207 | } | |
208 | } | |
209 | ||
210 | wxFont::~wxFont() | |
211 | { | |
212 | if (wxTheFontList) | |
213 | wxTheFontList->DeleteObject(this); | |
214 | } | |
215 | ||
216 | // ---------------------------------------------------------------------------- | |
217 | // accessors | |
218 | // ---------------------------------------------------------------------------- | |
219 | ||
220 | int wxFont::GetPointSize() const | |
221 | { | |
222 | wxCHECK_MSG( Ok(), 0, wxT("invalid font") ); | |
223 | ||
224 | return M_FONTDATA->m_pointSize; | |
225 | } | |
226 | ||
227 | wxString wxFont::GetFaceName() const | |
228 | { | |
229 | wxCHECK_MSG( Ok(), wxT(""), wxT("invalid font") ); | |
230 | ||
231 | return M_FONTDATA->m_faceName; | |
232 | } | |
233 | ||
234 | int wxFont::GetFamily() const | |
235 | { | |
236 | wxCHECK_MSG( Ok(), 0, wxT("invalid font") ); | |
237 | ||
238 | return M_FONTDATA->m_family; | |
239 | } | |
240 | ||
241 | int wxFont::GetStyle() const | |
242 | { | |
243 | wxCHECK_MSG( Ok(), 0, wxT("invalid font") ); | |
244 | ||
245 | return M_FONTDATA->m_style; | |
246 | } | |
247 | ||
248 | int wxFont::GetWeight() const | |
249 | { | |
250 | wxCHECK_MSG( Ok(), 0, wxT("invalid font") ); | |
251 | ||
252 | return M_FONTDATA->m_weight; | |
253 | } | |
254 | ||
255 | bool wxFont::GetUnderlined() const | |
256 | { | |
257 | wxCHECK_MSG( Ok(), FALSE, wxT("invalid font") ); | |
258 | ||
259 | return M_FONTDATA->m_underlined; | |
260 | } | |
261 | ||
262 | ||
263 | wxFontEncoding wxFont::GetEncoding() const | |
264 | { | |
265 | wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT, wxT("invalid font") ); | |
266 | ||
267 | return M_FONTDATA->m_encoding; | |
268 | } | |
269 | ||
270 | ||
271 | // ---------------------------------------------------------------------------- | |
272 | // change font attributes | |
273 | // ---------------------------------------------------------------------------- | |
274 | ||
275 | void wxFont::SetPointSize(int pointSize) | |
276 | { | |
277 | Unshare(); | |
278 | ||
279 | M_FONTDATA->m_pointSize = pointSize; | |
280 | M_FONTDATA->m_valid = FALSE; | |
281 | } | |
282 | ||
283 | void wxFont::SetFamily(int family) | |
284 | { | |
285 | Unshare(); | |
286 | ||
287 | M_FONTDATA->m_family = family; | |
288 | M_FONTDATA->m_valid = FALSE; | |
289 | } | |
290 | ||
291 | void wxFont::SetStyle(int style) | |
292 | { | |
293 | Unshare(); | |
294 | ||
295 | M_FONTDATA->m_style = style; | |
296 | M_FONTDATA->m_valid = FALSE; | |
297 | } | |
298 | ||
299 | void wxFont::SetWeight(int weight) | |
300 | { | |
301 | Unshare(); | |
302 | ||
303 | M_FONTDATA->m_weight = weight; | |
304 | M_FONTDATA->m_valid = FALSE; | |
305 | } | |
306 | ||
307 | void wxFont::SetFaceName(const wxString& faceName) | |
308 | { | |
309 | Unshare(); | |
310 | ||
311 | M_FONTDATA->m_faceName = faceName; | |
312 | M_FONTDATA->m_valid = FALSE; | |
313 | } | |
314 | ||
315 | void wxFont::SetUnderlined(bool underlined) | |
316 | { | |
317 | Unshare(); | |
318 | ||
319 | M_FONTDATA->m_underlined = underlined; | |
320 | } | |
321 | ||
322 | void wxFont::SetEncoding(wxFontEncoding encoding) | |
323 | { | |
324 | Unshare(); | |
325 | ||
326 | M_FONTDATA->m_encoding = encoding; | |
327 | M_FONTDATA->m_valid = FALSE; | |
328 | } |