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