]>
Commit | Line | Data |
---|---|---|
7beba2fc | 1 | ///////////////////////////////////////////////////////////////////////////// |
55034339 | 2 | // Name: src/unix/fontutil.cpp |
7beba2fc VZ |
3 | // Purpose: Font helper functions for X11 (GDK/X) |
4 | // Author: Vadim Zeitlin | |
5 | // Modified by: | |
6 | // Created: 05.11.99 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Vadim Zeitlin | |
65571936 | 9 | // Licence: wxWindows licence |
7beba2fc VZ |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
7beba2fc VZ |
20 | // For compilers that support precompilation, includes "wx.h". |
21 | #include "wx/wxprec.h" | |
22 | ||
23 | #ifdef __BORLANDC__ | |
24 | #pragma hdrstop | |
25 | #endif | |
26 | ||
27 | #ifndef WX_PRECOMP | |
0f6858b6 | 28 | #include "wx/font.h" // wxFont enums |
e4ffab29 | 29 | #include "wx/encinfo.h" |
7beba2fc VZ |
30 | #endif // PCH |
31 | ||
db16cab4 RR |
32 | #include "wx/fontutil.h" |
33 | #include "wx/fontmap.h" | |
34 | #include "wx/tokenzr.h" | |
35 | #include "wx/hash.h" | |
36 | #include "wx/module.h" | |
37 | ||
2b5f62a0 VZ |
38 | #if wxUSE_PANGO |
39 | ||
40 | #include "pango/pango.h" | |
db16cab4 | 41 | |
2b5f62a0 | 42 | #ifdef __WXGTK20__ |
db16cab4 | 43 | #include "wx/gtk/private.h" |
38d446db | 44 | extern GtkWidget *wxGetRootWindow(); |
2b5f62a0 VZ |
45 | #else |
46 | #include "wx/x11/private.h" | |
47 | #endif | |
db16cab4 RR |
48 | |
49 | // ---------------------------------------------------------------------------- | |
50 | // wxNativeFontInfo | |
51 | // ---------------------------------------------------------------------------- | |
52 | ||
53 | void wxNativeFontInfo::Init() | |
54 | { | |
55 | description = NULL; | |
56 | } | |
57 | ||
23afe648 VZ |
58 | void |
59 | wxNativeFontInfo::Init(const wxNativeFontInfo& info) | |
fdf7514a VS |
60 | { |
61 | if (info.description) | |
62 | description = pango_font_description_copy(info.description); | |
63 | else | |
82680055 | 64 | description = NULL; |
fdf7514a VS |
65 | } |
66 | ||
82680055 | 67 | void wxNativeFontInfo::Free() |
fdf7514a VS |
68 | { |
69 | if (description) | |
70 | pango_font_description_free(description); | |
71 | } | |
72 | ||
db16cab4 RR |
73 | int wxNativeFontInfo::GetPointSize() const |
74 | { | |
75 | return pango_font_description_get_size( description ) / PANGO_SCALE; | |
76 | } | |
77 | ||
78 | wxFontStyle wxNativeFontInfo::GetStyle() const | |
79 | { | |
80 | wxFontStyle m_style = wxFONTSTYLE_NORMAL; | |
81 | ||
82 | switch (pango_font_description_get_style( description )) | |
83 | { | |
84 | case PANGO_STYLE_NORMAL: | |
85 | m_style = wxFONTSTYLE_NORMAL; | |
86 | break; | |
87 | case PANGO_STYLE_ITALIC: | |
88 | m_style = wxFONTSTYLE_ITALIC; | |
89 | break; | |
90 | case PANGO_STYLE_OBLIQUE: | |
91 | m_style = wxFONTSTYLE_SLANT; | |
92 | break; | |
93 | } | |
2b5f62a0 | 94 | |
db16cab4 RR |
95 | return m_style; |
96 | } | |
97 | ||
98 | wxFontWeight wxNativeFontInfo::GetWeight() const | |
99 | { | |
0f6858b6 RR |
100 | #if 0 |
101 | // We seem to currently initialize only by string. | |
102 | // In that case PANGO_FONT_MASK_WEIGHT is always set. | |
103 | if (!(pango_font_description_get_set_fields(description) & PANGO_FONT_MASK_WEIGHT)) | |
104 | return wxFONTWEIGHT_NORMAL; | |
105 | #endif | |
db16cab4 | 106 | |
0f6858b6 RR |
107 | PangoWeight pango_weight = pango_font_description_get_weight( description ); |
108 | ||
109 | // Until the API can be changed the following ranges of weight values are used: | |
110 | // wxFONTWEIGHT_LIGHT: 100 .. 349 - range of 250 | |
111 | // wxFONTWEIGHT_NORMAL: 350 .. 599 - range of 250 | |
112 | // wxFONTWEIGHT_BOLD: 600 .. 900 - range of 301 (600 is "semibold" already) | |
113 | ||
114 | if (pango_weight >= 600) | |
115 | return wxFONTWEIGHT_BOLD; | |
116 | ||
117 | if (pango_weight < 350) | |
118 | return wxFONTWEIGHT_LIGHT; | |
2b5f62a0 | 119 | |
0f6858b6 | 120 | return wxFONTWEIGHT_NORMAL; |
db16cab4 RR |
121 | } |
122 | ||
123 | bool wxNativeFontInfo::GetUnderlined() const | |
124 | { | |
55034339 | 125 | return false; |
db16cab4 RR |
126 | } |
127 | ||
128 | wxString wxNativeFontInfo::GetFaceName() const | |
129 | { | |
130 | wxString tmp = wxGTK_CONV_BACK( pango_font_description_get_family( description ) ); | |
2b5f62a0 | 131 | |
db16cab4 RR |
132 | return tmp; |
133 | } | |
134 | ||
135 | wxFontFamily wxNativeFontInfo::GetFamily() const | |
136 | { | |
b67d14be | 137 | wxFontFamily ret = wxFONTFAMILY_DEFAULT; |
34a35823 MW |
138 | // note: not passing -1 as the 2nd parameter to g_ascii_strdown to work |
139 | // around a bug in the 64-bit glib shipped with solaris 10, -1 causes it | |
140 | // to try to allocate 2^32 bytes. | |
141 | const char *family_name = pango_font_description_get_family( description ); | |
142 | char *family_text = g_ascii_strdown( family_name, family_name ? strlen( family_name ) : 0 ); | |
b67d14be MR |
143 | // Check for some common fonts, to salvage what we can from the current win32 centric wxFont API: |
144 | if (strncmp( family_text, "monospace", 9 ) == 0) | |
145 | ret = wxFONTFAMILY_TELETYPE; // begins with "Monospace" | |
146 | else if (strncmp( family_text, "courier", 7 ) == 0) | |
147 | ret = wxFONTFAMILY_TELETYPE; // begins with "Courier" | |
c42f011e | 148 | #if defined(__WXGTK24__) || defined(HAVE_PANGO_FONT_FAMILY_IS_MONOSPACE) |
b67d14be | 149 | else |
c42f011e MR |
150 | #ifdef __WXGTK24__ |
151 | if (!gtk_check_version(2,4,0)) | |
152 | #endif | |
b67d14be MR |
153 | { |
154 | PangoFontFamily **families; | |
b20cb045 | 155 | PangoFontFamily *family = NULL; |
b67d14be MR |
156 | int n_families; |
157 | pango_context_list_families( | |
38d446db | 158 | #ifdef __WXGTK20__ |
b67d14be | 159 | gtk_widget_get_pango_context( wxGetRootWindow() ), |
38d446db | 160 | #else |
b67d14be | 161 | wxTheApp->GetPangoContext(), |
38d446db | 162 | #endif |
b67d14be | 163 | &families, &n_families); |
38d446db | 164 | |
b67d14be | 165 | for (int i = 0;i < n_families;++i) |
38d446db | 166 | { |
b67d14be MR |
167 | if (g_ascii_strcasecmp(pango_font_family_get_name( families[i] ), pango_font_description_get_family( description )) == 0 ) |
168 | { | |
169 | family = families[i]; | |
170 | break; | |
171 | } | |
38d446db | 172 | } |
38d446db | 173 | |
b67d14be | 174 | g_free(families); |
38d446db | 175 | |
3c542a4d MR |
176 | // Some gtk+ systems might query for a non-existing font from wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT) |
177 | // on initialization, don't assert until wxSystemSettings::GetFont is checked for this - MR | |
178 | // wxASSERT_MSG( family, wxT("wxNativeFontInfo::GetFamily() - No appropriate PangoFontFamily found for ::description") ); | |
38d446db | 179 | |
b67d14be MR |
180 | //BCI: Cache the wxFontFamily inside the class. Validate cache with |
181 | //BCI: g_ascii_strcasecmp(pango_font_description_get_family(description), pango_font_family_get_name(family)) == 0 | |
38d446db | 182 | |
b20cb045 | 183 | if (family != NULL && pango_font_family_is_monospace( family )) |
b67d14be MR |
184 | ret = wxFONTFAMILY_TELETYPE; // is deemed a monospace font by pango |
185 | } | |
c42f011e | 186 | #endif // gtk24 || HAVE_PANGO_FONT_FAMILY_IS_MONOSPACE |
38d446db | 187 | |
b67d14be MR |
188 | if (ret == wxFONTFAMILY_DEFAULT) |
189 | { | |
190 | if (strstr( family_text, "sans" ) != NULL) // checked before serif, so that "* Sans Serif" fonts are detected correctly | |
191 | ret = wxFONTFAMILY_SWISS; // contains "Sans" | |
192 | else if (strstr( family_text, "serif" ) != NULL) | |
193 | ret = wxFONTFAMILY_ROMAN; // contains "Serif" | |
194 | else if (strncmp( family_text, "times", 5 ) == 0) | |
195 | ret = wxFONTFAMILY_ROMAN; // begins with "Times" | |
196 | else if (strncmp( family_text, "old", 3 ) == 0) | |
197 | ret = wxFONTFAMILY_DECORATIVE; // Begins with "Old" - "Old English", "Old Town" | |
198 | } | |
199 | ||
200 | free(family_text); | |
201 | return ret; | |
db16cab4 RR |
202 | } |
203 | ||
204 | wxFontEncoding wxNativeFontInfo::GetEncoding() const | |
205 | { | |
206 | return wxFONTENCODING_SYSTEM; | |
207 | } | |
208 | ||
3398cf2c | 209 | |
8a15e8ba | 210 | void wxNativeFontInfo::SetPointSize(int pointsize) |
3398cf2c | 211 | { |
8a15e8ba | 212 | pango_font_description_set_size( description, pointsize * PANGO_SCALE ); |
3398cf2c VZ |
213 | } |
214 | ||
7533ba25 | 215 | void wxNativeFontInfo::SetStyle(wxFontStyle style) |
3398cf2c | 216 | { |
7533ba25 MR |
217 | switch (style) |
218 | { | |
219 | case wxFONTSTYLE_ITALIC: | |
220 | pango_font_description_set_style( description, PANGO_STYLE_ITALIC ); | |
221 | break; | |
222 | case wxFONTSTYLE_SLANT: | |
223 | pango_font_description_set_style( description, PANGO_STYLE_OBLIQUE ); | |
224 | break; | |
225 | default: | |
226 | wxFAIL_MSG( _T("unknown font style") ); | |
227 | // fall through | |
228 | case wxFONTSTYLE_NORMAL: | |
229 | pango_font_description_set_style( description, PANGO_STYLE_NORMAL ); | |
230 | break; | |
231 | } | |
3398cf2c VZ |
232 | } |
233 | ||
7533ba25 | 234 | void wxNativeFontInfo::SetWeight(wxFontWeight weight) |
3398cf2c | 235 | { |
7533ba25 MR |
236 | switch (weight) |
237 | { | |
238 | case wxFONTWEIGHT_BOLD: | |
239 | pango_font_description_set_weight(description, PANGO_WEIGHT_BOLD); | |
240 | break; | |
241 | case wxFONTWEIGHT_LIGHT: | |
242 | pango_font_description_set_weight(description, PANGO_WEIGHT_LIGHT); | |
243 | break; | |
244 | default: | |
245 | wxFAIL_MSG( _T("unknown font weight") ); | |
246 | // fall through | |
247 | case wxFONTWEIGHT_NORMAL: | |
248 | pango_font_description_set_weight(description, PANGO_WEIGHT_NORMAL); | |
249 | } | |
3398cf2c VZ |
250 | } |
251 | ||
252 | void wxNativeFontInfo::SetUnderlined(bool WXUNUSED(underlined)) | |
253 | { | |
254 | wxFAIL_MSG( _T("not implemented") ); | |
255 | } | |
256 | ||
fbfb8bcc | 257 | void wxNativeFontInfo::SetFaceName(const wxString& facename) |
3398cf2c | 258 | { |
8a15e8ba | 259 | pango_font_description_set_family( description, wxGTK_CONV(facename) ); |
3398cf2c VZ |
260 | } |
261 | ||
262 | void wxNativeFontInfo::SetFamily(wxFontFamily WXUNUSED(family)) | |
263 | { | |
264 | wxFAIL_MSG( _T("not implemented") ); | |
265 | } | |
266 | ||
267 | void wxNativeFontInfo::SetEncoding(wxFontEncoding WXUNUSED(encoding)) | |
268 | { | |
269 | wxFAIL_MSG( _T("not implemented") ); | |
270 | } | |
271 | ||
272 | ||
273 | ||
db16cab4 RR |
274 | bool wxNativeFontInfo::FromString(const wxString& s) |
275 | { | |
276 | if (description) | |
277 | pango_font_description_free( description ); | |
278 | ||
279 | description = pango_font_description_from_string( wxGTK_CONV( s ) ); | |
280 | ||
55034339 | 281 | return true; |
db16cab4 RR |
282 | } |
283 | ||
284 | wxString wxNativeFontInfo::ToString() const | |
285 | { | |
61850501 RR |
286 | char *str = pango_font_description_to_string( description ); |
287 | wxString tmp = wxGTK_CONV_BACK( str ); | |
288 | g_free( str ); | |
db16cab4 RR |
289 | |
290 | return tmp; | |
291 | } | |
292 | ||
293 | bool wxNativeFontInfo::FromUserString(const wxString& s) | |
294 | { | |
295 | return FromString( s ); | |
296 | } | |
297 | ||
298 | wxString wxNativeFontInfo::ToUserString() const | |
299 | { | |
300 | return ToString(); | |
301 | } | |
302 | ||
303 | // ---------------------------------------------------------------------------- | |
304 | // wxNativeEncodingInfo | |
305 | // ---------------------------------------------------------------------------- | |
306 | ||
82359764 | 307 | bool wxNativeEncodingInfo::FromString(const wxString& WXUNUSED(s)) |
db16cab4 | 308 | { |
55034339 | 309 | return false; |
db16cab4 RR |
310 | } |
311 | ||
312 | wxString wxNativeEncodingInfo::ToString() const | |
313 | { | |
314 | return wxEmptyString; | |
315 | } | |
316 | ||
82359764 | 317 | bool wxTestFontEncoding(const wxNativeEncodingInfo& WXUNUSED(info)) |
db16cab4 | 318 | { |
55034339 | 319 | return true; |
db16cab4 RR |
320 | } |
321 | ||
322 | bool wxGetNativeFontEncoding(wxFontEncoding encoding, | |
323 | wxNativeEncodingInfo *info) | |
324 | { | |
82359764 VZ |
325 | info->facename.clear(); |
326 | ||
327 | switch ( encoding ) | |
f8b29974 | 328 | { |
82359764 VZ |
329 | // we *must* return true for default encodings as otherwise wxFontMapper |
330 | // considers that we can't load any font and aborts with wxLogFatalError! | |
331 | case wxFONTENCODING_DEFAULT: | |
332 | case wxFONTENCODING_SYSTEM: | |
333 | info->encoding = wxFONTENCODING_SYSTEM; | |
334 | return true; | |
f8b29974 | 335 | |
82359764 VZ |
336 | case wxFONTENCODING_UTF8: |
337 | info->encoding = wxFONTENCODING_UTF8; | |
338 | return true; | |
339 | ||
340 | default: | |
341 | // everything else must be converted to UTF-8 | |
342 | return false; | |
343 | } | |
db16cab4 RR |
344 | } |
345 | ||
2b5f62a0 | 346 | #else // GTK+ 1.x |
db16cab4 | 347 | |
79e4b627 | 348 | #ifdef __X__ |
4ea45e6b VZ |
349 | #ifdef __VMS__ |
350 | #pragma message disable nosimpint | |
351 | #endif | |
352 | ||
353 | #include <X11/Xlib.h> | |
354 | ||
355 | #ifdef __VMS__ | |
356 | #pragma message enable nosimpint | |
357 | #endif | |
79e4b627 VZ |
358 | |
359 | #include "wx/utils.h" // for wxGetDisplay() | |
360 | #elif defined(__WXGTK__) | |
4ea45e6b VZ |
361 | // we have to declare struct tm to avoid problems with first forward |
362 | // declaring it in C code (glib.h included from gdk.h does it) and then | |
363 | // defining it when time.h is included from the headers below - this is | |
364 | // known not to work at least with Sun CC 6.01 | |
365 | #include <time.h> | |
366 | ||
367 | #include <gdk/gdk.h> | |
79e4b627 VZ |
368 | #endif |
369 | ||
2e0e025e RR |
370 | |
371 | // ---------------------------------------------------------------------------- | |
372 | // private data | |
373 | // ---------------------------------------------------------------------------- | |
374 | ||
375 | static wxHashTable *g_fontHash = (wxHashTable*) NULL; | |
7beba2fc VZ |
376 | |
377 | // ---------------------------------------------------------------------------- | |
378 | // private functions | |
379 | // ---------------------------------------------------------------------------- | |
380 | ||
381 | // define the functions to create and destroy native fonts for this toolkit | |
382 | #ifdef __X__ | |
3fe34ed0 | 383 | wxNativeFont wxLoadFont(const wxString& fontSpec) |
7beba2fc VZ |
384 | { |
385 | return XLoadQueryFont((Display *)wxGetDisplay(), fontSpec); | |
386 | } | |
387 | ||
409d5a58 | 388 | inline void wxFreeFont(wxNativeFont font) |
7beba2fc | 389 | { |
a5fc62a1 | 390 | XFreeFont((Display *)wxGetDisplay(), (XFontStruct *)font); |
7beba2fc VZ |
391 | } |
392 | #elif defined(__WXGTK__) | |
3fe34ed0 | 393 | wxNativeFont wxLoadFont(const wxString& fontSpec) |
7beba2fc | 394 | { |
8fa3a431 VZ |
395 | // VZ: we should use gdk_fontset_load() instead of gdk_font_load() |
396 | // here to be able to display Japanese fonts correctly (at least | |
397 | // this is what people report) but unfortunately doing it results | |
398 | // in tons of warnings when using GTK with "normal" European | |
399 | // languages and so we can't always do it and I don't know enough | |
400 | // to determine when should this be done... (FIXME) | |
401 | return gdk_font_load( wxConvertWX2MB(fontSpec) ); | |
7beba2fc VZ |
402 | } |
403 | ||
409d5a58 | 404 | inline void wxFreeFont(wxNativeFont font) |
7beba2fc VZ |
405 | { |
406 | gdk_font_unref(font); | |
407 | } | |
408 | #else | |
409 | #error "Unknown GUI toolkit" | |
410 | #endif | |
411 | ||
412 | static bool wxTestFontSpec(const wxString& fontspec); | |
413 | ||
414 | static wxNativeFont wxLoadQueryFont(int pointSize, | |
415 | int family, | |
416 | int style, | |
417 | int weight, | |
418 | bool underlined, | |
419 | const wxString& facename, | |
420 | const wxString& xregistry, | |
30764ab5 VZ |
421 | const wxString& xencoding, |
422 | wxString* xFontName); | |
7beba2fc VZ |
423 | |
424 | // ============================================================================ | |
425 | // implementation | |
426 | // ============================================================================ | |
427 | ||
428 | // ---------------------------------------------------------------------------- | |
429 | // wxNativeEncodingInfo | |
430 | // ---------------------------------------------------------------------------- | |
431 | ||
432 | // convert to/from the string representation: format is | |
1e1d0be1 | 433 | // encodingid;registry;encoding[;facename] |
7beba2fc VZ |
434 | bool wxNativeEncodingInfo::FromString(const wxString& s) |
435 | { | |
6c49baf2 | 436 | // use ";", not "-" because it may be part of encoding name |
1e1d0be1 | 437 | wxStringTokenizer tokenizer(s, _T(";")); |
1e1d0be1 VS |
438 | |
439 | wxString encid = tokenizer.GetNextToken(); | |
440 | long enc; | |
441 | if ( !encid.ToLong(&enc) ) | |
55034339 | 442 | return false; |
1e1d0be1 | 443 | encoding = (wxFontEncoding)enc; |
7beba2fc VZ |
444 | |
445 | xregistry = tokenizer.GetNextToken(); | |
446 | if ( !xregistry ) | |
55034339 | 447 | return false; |
7beba2fc VZ |
448 | |
449 | xencoding = tokenizer.GetNextToken(); | |
450 | if ( !xencoding ) | |
55034339 | 451 | return false; |
7beba2fc VZ |
452 | |
453 | // ok even if empty | |
454 | facename = tokenizer.GetNextToken(); | |
455 | ||
55034339 | 456 | return true; |
7beba2fc VZ |
457 | } |
458 | ||
459 | wxString wxNativeEncodingInfo::ToString() const | |
460 | { | |
461 | wxString s; | |
1e1d0be1 | 462 | s << (long)encoding << _T(';') << xregistry << _T(';') << xencoding; |
55034339 | 463 | if ( !facename.empty() ) |
7beba2fc | 464 | { |
1e1d0be1 | 465 | s << _T(';') << facename; |
7beba2fc VZ |
466 | } |
467 | ||
468 | return s; | |
469 | } | |
470 | ||
ab5fe833 VZ |
471 | // ---------------------------------------------------------------------------- |
472 | // wxNativeFontInfo | |
473 | // ---------------------------------------------------------------------------- | |
474 | ||
475 | void wxNativeFontInfo::Init() | |
476 | { | |
55034339 | 477 | m_isDefault = true; |
ab5fe833 VZ |
478 | } |
479 | ||
480 | bool wxNativeFontInfo::FromString(const wxString& s) | |
481 | { | |
482 | wxStringTokenizer tokenizer(s, _T(";")); | |
483 | ||
484 | // check the version | |
485 | wxString token = tokenizer.GetNextToken(); | |
486 | if ( token != _T('0') ) | |
55034339 | 487 | return false; |
ab5fe833 VZ |
488 | |
489 | xFontName = tokenizer.GetNextToken(); | |
490 | ||
491 | // this should be the end | |
492 | if ( tokenizer.HasMoreTokens() ) | |
55034339 | 493 | return false; |
ab5fe833 VZ |
494 | |
495 | return FromXFontName(xFontName); | |
496 | } | |
497 | ||
498 | wxString wxNativeFontInfo::ToString() const | |
499 | { | |
500 | // 0 is the version | |
501 | return wxString::Format(_T("%d;%s"), 0, GetXFontName().c_str()); | |
502 | } | |
503 | ||
504 | bool wxNativeFontInfo::FromUserString(const wxString& s) | |
505 | { | |
506 | return FromXFontName(s); | |
507 | } | |
508 | ||
509 | wxString wxNativeFontInfo::ToUserString() const | |
510 | { | |
511 | return GetXFontName(); | |
512 | } | |
513 | ||
409d5a58 VZ |
514 | bool wxNativeFontInfo::HasElements() const |
515 | { | |
516 | // we suppose that the foundry is never empty, so if it is it means that we | |
517 | // had never parsed the XLFD | |
518 | return !fontElements[0].empty(); | |
519 | } | |
520 | ||
53f6aab7 VZ |
521 | wxString wxNativeFontInfo::GetXFontComponent(wxXLFDField field) const |
522 | { | |
55034339 | 523 | wxCHECK_MSG( field < wxXLFD_MAX, wxEmptyString, _T("invalid XLFD field") ); |
53f6aab7 | 524 | |
409d5a58 | 525 | if ( !HasElements() ) |
53f6aab7 VZ |
526 | { |
527 | // const_cast | |
528 | if ( !((wxNativeFontInfo *)this)->FromXFontName(xFontName) ) | |
55034339 | 529 | return wxEmptyString; |
53f6aab7 VZ |
530 | } |
531 | ||
532 | return fontElements[field]; | |
533 | } | |
534 | ||
295272bd | 535 | bool wxNativeFontInfo::FromXFontName(const wxString& fontname) |
ab5fe833 VZ |
536 | { |
537 | // TODO: we should be able to handle the font aliases here, but how? | |
409d5a58 VZ |
538 | wxStringTokenizer tokenizer(fontname, _T("-")); |
539 | ||
540 | // skip the leading, usually empty field (font name registry) | |
541 | if ( !tokenizer.HasMoreTokens() ) | |
55034339 | 542 | return false; |
409d5a58 VZ |
543 | |
544 | (void)tokenizer.GetNextToken(); | |
ab5fe833 VZ |
545 | |
546 | for ( size_t n = 0; n < WXSIZEOF(fontElements); n++ ) | |
547 | { | |
548 | if ( !tokenizer.HasMoreTokens() ) | |
549 | { | |
550 | // not enough elements in the XLFD - or maybe an alias | |
55034339 | 551 | return false; |
ab5fe833 VZ |
552 | } |
553 | ||
b4e4abb5 VZ |
554 | wxString field = tokenizer.GetNextToken(); |
555 | if ( !field.empty() && field != _T('*') ) | |
556 | { | |
557 | // we're really initialized now | |
55034339 | 558 | m_isDefault = false; |
b4e4abb5 VZ |
559 | } |
560 | ||
561 | fontElements[n] = field; | |
ab5fe833 VZ |
562 | } |
563 | ||
564 | // this should be all | |
011ba5ed | 565 | if ( tokenizer.HasMoreTokens() ) |
55034339 | 566 | return false; |
011ba5ed | 567 | |
55034339 | 568 | return true; |
ab5fe833 VZ |
569 | } |
570 | ||
571 | wxString wxNativeFontInfo::GetXFontName() const | |
572 | { | |
573 | if ( xFontName.empty() ) | |
574 | { | |
575 | for ( size_t n = 0; n < WXSIZEOF(fontElements); n++ ) | |
576 | { | |
577 | // replace the non specified elements with '*' except for the | |
578 | // additional style which is usually just omitted | |
579 | wxString elt = fontElements[n]; | |
409d5a58 | 580 | if ( elt.empty() && n != wxXLFD_ADDSTYLE ) |
ab5fe833 VZ |
581 | { |
582 | elt = _T('*'); | |
583 | } | |
584 | ||
585 | // const_cast | |
586 | ((wxNativeFontInfo *)this)->xFontName << _T('-') << elt; | |
587 | } | |
588 | } | |
589 | ||
590 | return xFontName; | |
591 | } | |
592 | ||
409d5a58 VZ |
593 | void |
594 | wxNativeFontInfo::SetXFontComponent(wxXLFDField field, const wxString& value) | |
595 | { | |
596 | wxCHECK_RET( field < wxXLFD_MAX, _T("invalid XLFD field") ); | |
597 | ||
598 | // this class should be initialized with a valid font spec first and only | |
599 | // then the fields may be modified! | |
600 | wxASSERT_MSG( !IsDefault(), _T("can't modify an uninitialized XLFD") ); | |
601 | ||
602 | if ( !HasElements() ) | |
603 | { | |
604 | // const_cast | |
605 | if ( !((wxNativeFontInfo *)this)->FromXFontName(xFontName) ) | |
606 | { | |
607 | wxFAIL_MSG( _T("can't set font element for invalid XLFD") ); | |
608 | ||
609 | return; | |
610 | } | |
611 | } | |
612 | ||
613 | fontElements[field] = value; | |
614 | ||
615 | // invalidate the XFLD, it doesn't correspond to the font elements any more | |
616 | xFontName.clear(); | |
617 | } | |
618 | ||
619 | void wxNativeFontInfo::SetXFontName(const wxString& xFontName_) | |
620 | { | |
621 | // invalidate the font elements, GetXFontComponent() will reparse the XLFD | |
622 | fontElements[0].clear(); | |
623 | ||
624 | xFontName = xFontName_; | |
625 | ||
55034339 | 626 | m_isDefault = false; |
409d5a58 VZ |
627 | } |
628 | ||
2b5f62a0 VZ |
629 | int wxNativeFontInfo::GetPointSize() const |
630 | { | |
631 | const wxString s = GetXFontComponent(wxXLFD_POINTSIZE); | |
632 | ||
633 | // return -1 to indicate that the size is unknown | |
634 | long l; | |
635 | return s.ToLong(&l) ? l : -1; | |
636 | } | |
637 | ||
638 | wxFontStyle wxNativeFontInfo::GetStyle() const | |
639 | { | |
640 | const wxString s = GetXFontComponent(wxXLFD_SLANT); | |
641 | ||
642 | if ( s.length() != 1 ) | |
643 | { | |
644 | // it is really unknown but we don't have any way to return it from | |
645 | // here | |
646 | return wxFONTSTYLE_NORMAL; | |
647 | } | |
648 | ||
649 | switch ( s[0] ) | |
650 | { | |
651 | default: | |
652 | // again, unknown but consider normal by default | |
653 | ||
654 | case _T('r'): | |
655 | return wxFONTSTYLE_NORMAL; | |
656 | ||
657 | case _T('i'): | |
658 | return wxFONTSTYLE_ITALIC; | |
659 | ||
660 | case _T('o'): | |
661 | return wxFONTSTYLE_SLANT; | |
662 | } | |
663 | } | |
664 | ||
665 | wxFontWeight wxNativeFontInfo::GetWeight() const | |
666 | { | |
667 | const wxString s = GetXFontComponent(wxXLFD_WEIGHT).MakeLower(); | |
668 | if ( s.find(_T("bold")) != wxString::npos || s == _T("black") ) | |
669 | return wxFONTWEIGHT_BOLD; | |
670 | else if ( s == _T("light") ) | |
671 | return wxFONTWEIGHT_LIGHT; | |
672 | ||
673 | return wxFONTWEIGHT_NORMAL; | |
674 | } | |
675 | ||
676 | bool wxNativeFontInfo::GetUnderlined() const | |
677 | { | |
678 | // X fonts are never underlined | |
55034339 | 679 | return false; |
2b5f62a0 VZ |
680 | } |
681 | ||
682 | wxString wxNativeFontInfo::GetFaceName() const | |
683 | { | |
77ffb593 | 684 | // wxWidgets facename probably more accurately corresponds to X family |
2b5f62a0 VZ |
685 | return GetXFontComponent(wxXLFD_FAMILY); |
686 | } | |
687 | ||
688 | wxFontFamily wxNativeFontInfo::GetFamily() const | |
689 | { | |
77ffb593 | 690 | // and wxWidgets family -- to X foundry, but we have to translate it to |
2b5f62a0 VZ |
691 | // wxFontFamily somehow... |
692 | wxFAIL_MSG(_T("not implemented")); // GetXFontComponent(wxXLFD_FOUNDRY); | |
693 | ||
694 | return wxFONTFAMILY_DEFAULT; | |
695 | } | |
696 | ||
697 | wxFontEncoding wxNativeFontInfo::GetEncoding() const | |
698 | { | |
699 | // we already have the code for this but need to refactor it first | |
700 | wxFAIL_MSG( _T("not implemented") ); | |
701 | ||
702 | return wxFONTENCODING_MAX; | |
703 | } | |
704 | ||
705 | void wxNativeFontInfo::SetPointSize(int pointsize) | |
706 | { | |
707 | SetXFontComponent(wxXLFD_POINTSIZE, wxString::Format(_T("%d"), pointsize)); | |
708 | } | |
709 | ||
710 | void wxNativeFontInfo::SetStyle(wxFontStyle style) | |
711 | { | |
712 | wxString s; | |
713 | switch ( style ) | |
714 | { | |
715 | case wxFONTSTYLE_ITALIC: | |
716 | s = _T('i'); | |
717 | break; | |
718 | ||
719 | case wxFONTSTYLE_SLANT: | |
720 | s = _T('o'); | |
721 | break; | |
722 | ||
723 | case wxFONTSTYLE_NORMAL: | |
724 | s = _T('r'); | |
725 | ||
726 | default: | |
727 | wxFAIL_MSG( _T("unknown wxFontStyle in wxNativeFontInfo::SetStyle") ); | |
728 | return; | |
729 | } | |
730 | ||
731 | SetXFontComponent(wxXLFD_SLANT, s); | |
732 | } | |
733 | ||
734 | void wxNativeFontInfo::SetWeight(wxFontWeight weight) | |
735 | { | |
736 | wxString s; | |
737 | switch ( weight ) | |
738 | { | |
739 | case wxFONTWEIGHT_BOLD: | |
740 | s = _T("bold"); | |
741 | break; | |
742 | ||
743 | case wxFONTWEIGHT_LIGHT: | |
744 | s = _T("light"); | |
745 | break; | |
746 | ||
747 | case wxFONTWEIGHT_NORMAL: | |
748 | s = _T("medium"); | |
749 | break; | |
750 | ||
751 | default: | |
752 | wxFAIL_MSG( _T("unknown wxFontWeight in wxNativeFontInfo::SetWeight") ); | |
753 | return; | |
754 | } | |
755 | ||
756 | SetXFontComponent(wxXLFD_WEIGHT, s); | |
757 | } | |
758 | ||
759 | void wxNativeFontInfo::SetUnderlined(bool WXUNUSED(underlined)) | |
760 | { | |
761 | // can't do this under X | |
762 | } | |
763 | ||
fbfb8bcc | 764 | void wxNativeFontInfo::SetFaceName(const wxString& facename) |
2b5f62a0 VZ |
765 | { |
766 | SetXFontComponent(wxXLFD_FAMILY, facename); | |
767 | } | |
768 | ||
55034339 | 769 | void wxNativeFontInfo::SetFamily(wxFontFamily WXUNUSED(family)) |
2b5f62a0 VZ |
770 | { |
771 | // wxFontFamily -> X foundry, anyone? | |
772 | wxFAIL_MSG( _T("not implemented") ); | |
773 | ||
774 | // SetXFontComponent(wxXLFD_FOUNDRY, ...); | |
775 | } | |
776 | ||
777 | void wxNativeFontInfo::SetEncoding(wxFontEncoding encoding) | |
778 | { | |
779 | wxNativeEncodingInfo info; | |
780 | if ( wxGetNativeFontEncoding(encoding, &info) ) | |
781 | { | |
782 | SetXFontComponent(wxXLFD_ENCODING, info.xencoding); | |
783 | SetXFontComponent(wxXLFD_REGISTRY, info.xregistry); | |
784 | } | |
785 | } | |
786 | ||
7beba2fc VZ |
787 | // ---------------------------------------------------------------------------- |
788 | // common functions | |
789 | // ---------------------------------------------------------------------------- | |
790 | ||
791 | bool wxGetNativeFontEncoding(wxFontEncoding encoding, | |
792 | wxNativeEncodingInfo *info) | |
793 | { | |
55034339 | 794 | wxCHECK_MSG( info, false, _T("bad pointer in wxGetNativeFontEncoding") ); |
7beba2fc VZ |
795 | |
796 | if ( encoding == wxFONTENCODING_DEFAULT ) | |
797 | { | |
798 | encoding = wxFont::GetDefaultEncoding(); | |
799 | } | |
800 | ||
801 | switch ( encoding ) | |
802 | { | |
803 | case wxFONTENCODING_ISO8859_1: | |
804 | case wxFONTENCODING_ISO8859_2: | |
805 | case wxFONTENCODING_ISO8859_3: | |
806 | case wxFONTENCODING_ISO8859_4: | |
807 | case wxFONTENCODING_ISO8859_5: | |
808 | case wxFONTENCODING_ISO8859_6: | |
809 | case wxFONTENCODING_ISO8859_7: | |
810 | case wxFONTENCODING_ISO8859_8: | |
811 | case wxFONTENCODING_ISO8859_9: | |
812 | case wxFONTENCODING_ISO8859_10: | |
813 | case wxFONTENCODING_ISO8859_11: | |
80a24267 | 814 | case wxFONTENCODING_ISO8859_12: |
7beba2fc VZ |
815 | case wxFONTENCODING_ISO8859_13: |
816 | case wxFONTENCODING_ISO8859_14: | |
817 | case wxFONTENCODING_ISO8859_15: | |
818 | { | |
819 | int cp = encoding - wxFONTENCODING_ISO8859_1 + 1; | |
820 | info->xregistry = wxT("iso8859"); | |
821 | info->xencoding.Printf(wxT("%d"), cp); | |
822 | } | |
823 | break; | |
824 | ||
bb84929e | 825 | case wxFONTENCODING_UTF8: |
5707316c | 826 | info->xregistry = wxT("iso10646"); |
bb84929e VZ |
827 | info->xencoding = wxT("*"); |
828 | break; | |
829 | ||
2b5f62a0 | 830 | case wxFONTENCODING_GB2312: |
82680055 | 831 | info->xregistry = wxT("GB2312"); // or the otherway round? |
2b5f62a0 VZ |
832 | info->xencoding = wxT("*"); |
833 | break; | |
834 | ||
7beba2fc | 835 | case wxFONTENCODING_KOI8: |
15ad38c3 | 836 | case wxFONTENCODING_KOI8_U: |
7beba2fc VZ |
837 | info->xregistry = wxT("koi8"); |
838 | ||
5707316c | 839 | // we don't make distinction between koi8-r, koi8-u and koi8-ru (so far) |
7beba2fc VZ |
840 | info->xencoding = wxT("*"); |
841 | break; | |
842 | ||
843 | case wxFONTENCODING_CP1250: | |
844 | case wxFONTENCODING_CP1251: | |
845 | case wxFONTENCODING_CP1252: | |
846 | case wxFONTENCODING_CP1253: | |
847 | case wxFONTENCODING_CP1254: | |
848 | case wxFONTENCODING_CP1255: | |
849 | case wxFONTENCODING_CP1256: | |
850 | case wxFONTENCODING_CP1257: | |
851 | { | |
852 | int cp = encoding - wxFONTENCODING_CP1250 + 1250; | |
853 | info->xregistry = wxT("microsoft"); | |
854 | info->xencoding.Printf(wxT("cp%d"), cp); | |
855 | } | |
856 | break; | |
857 | ||
d946915c MB |
858 | case wxFONTENCODING_EUC_JP: |
859 | case wxFONTENCODING_SHIFT_JIS: | |
860 | info->xregistry = "jis*"; | |
861 | info->xencoding = "*"; | |
862 | break; | |
863 | ||
7beba2fc VZ |
864 | case wxFONTENCODING_SYSTEM: |
865 | info->xregistry = | |
81c67e27 | 866 | info->xencoding = wxT("*"); |
7beba2fc VZ |
867 | break; |
868 | ||
869 | default: | |
870 | // don't know how to translate this encoding into X fontspec | |
55034339 | 871 | return false; |
7beba2fc VZ |
872 | } |
873 | ||
ab5fe833 | 874 | info->encoding = encoding; |
6b0eebc5 | 875 | |
55034339 | 876 | return true; |
7beba2fc VZ |
877 | } |
878 | ||
879 | bool wxTestFontEncoding(const wxNativeEncodingInfo& info) | |
880 | { | |
881 | wxString fontspec; | |
882 | fontspec.Printf(_T("-*-%s-*-*-*-*-*-*-*-*-*-*-%s-%s"), | |
883 | !info.facename ? _T("*") : info.facename.c_str(), | |
884 | info.xregistry.c_str(), | |
885 | info.xencoding.c_str()); | |
886 | ||
887 | return wxTestFontSpec(fontspec); | |
888 | } | |
889 | ||
890 | // ---------------------------------------------------------------------------- | |
891 | // X-specific functions | |
892 | // ---------------------------------------------------------------------------- | |
893 | ||
894 | wxNativeFont wxLoadQueryNearestFont(int pointSize, | |
895 | int family, | |
896 | int style, | |
897 | int weight, | |
898 | bool underlined, | |
899 | const wxString &facename, | |
30764ab5 VZ |
900 | wxFontEncoding encoding, |
901 | wxString* xFontName) | |
7beba2fc | 902 | { |
97d3f0ee VZ |
903 | if ( encoding == wxFONTENCODING_DEFAULT ) |
904 | { | |
905 | encoding = wxFont::GetDefaultEncoding(); | |
906 | } | |
907 | ||
7beba2fc VZ |
908 | // first determine the encoding - if the font doesn't exist at all in this |
909 | // encoding, it's useless to do all other approximations (i.e. size, | |
910 | // family &c don't matter much) | |
911 | wxNativeEncodingInfo info; | |
97d3f0ee | 912 | if ( encoding == wxFONTENCODING_SYSTEM ) |
81c67e27 RR |
913 | { |
914 | // This will always work so we don't test to save time | |
915 | wxGetNativeFontEncoding(wxFONTENCODING_SYSTEM, &info); | |
916 | } | |
917 | else | |
7beba2fc | 918 | { |
81c67e27 RR |
919 | if ( !wxGetNativeFontEncoding(encoding, &info) || |
920 | !wxTestFontEncoding(info) ) | |
7beba2fc | 921 | { |
1e6feb95 | 922 | #if wxUSE_FONTMAP |
142b3bc2 | 923 | if ( !wxFontMapper::Get()->GetAltForEncoding(encoding, &info) ) |
1e6feb95 | 924 | #endif // wxUSE_FONTMAP |
81c67e27 RR |
925 | { |
926 | // unspported encoding - replace it with the default | |
927 | // | |
928 | // NB: we can't just return 0 from here because wxGTK code doesn't | |
929 | // check for it (i.e. it supposes that we'll always succeed), | |
930 | // so it would provoke a crash | |
931 | wxGetNativeFontEncoding(wxFONTENCODING_SYSTEM, &info); | |
932 | } | |
97d3f0ee | 933 | } |
7beba2fc | 934 | } |
6c49baf2 | 935 | |
81c67e27 | 936 | // OK, we have the correct xregistry/xencoding in info structure |
30764ab5 VZ |
937 | wxNativeFont font = 0; |
938 | ||
939 | // if we already have the X font name, try to use it | |
55034339 | 940 | if( xFontName && !xFontName->empty() ) |
6c49baf2 VZ |
941 | { |
942 | // | |
943 | // Make sure point size is correct for scale factor. | |
944 | // | |
945 | wxStringTokenizer tokenizer(*xFontName, _T("-"), wxTOKEN_RET_DELIMS); | |
946 | wxString newFontName; | |
947 | ||
948 | for(int i = 0; i < 8; i++) | |
949 | newFontName += tokenizer.NextToken(); | |
950 | ||
951 | (void) tokenizer.NextToken(); | |
952 | ||
401eb3de | 953 | newFontName += wxString::Format(wxT("%d-"), pointSize); |
6c49baf2 VZ |
954 | |
955 | while(tokenizer.HasMoreTokens()) | |
956 | newFontName += tokenizer.GetNextToken(); | |
957 | ||
958 | font = wxLoadFont(newFontName); | |
959 | ||
960 | if(font) | |
961 | *xFontName = newFontName; | |
962 | } | |
30764ab5 | 963 | |
7beba2fc VZ |
964 | if ( !font ) |
965 | { | |
966 | // search up and down by stepsize 10 | |
967 | int max_size = pointSize + 20 * (1 + (pointSize/180)); | |
968 | int min_size = pointSize - 20 * (1 + (pointSize/180)); | |
969 | ||
34791896 | 970 | int i, round; // counters |
7beba2fc | 971 | |
34791896 RR |
972 | // first round: search for equal, then for smaller and for larger size with the given weight and style |
973 | int testweight = weight; | |
974 | int teststyle = style; | |
975 | ||
976 | for ( round = 0; round < 3; round++ ) | |
977 | { | |
978 | // second round: use normal weight | |
979 | if ( round == 1 ) | |
b12401e0 | 980 | { |
34791896 RR |
981 | if ( testweight != wxNORMAL ) |
982 | { | |
983 | testweight = wxNORMAL; | |
984 | } | |
985 | else | |
986 | { | |
987 | ++round; // fall through to third round | |
988 | } | |
989 | } | |
990 | ||
991 | // third round: ... and use normal style | |
992 | if ( round == 2 ) | |
993 | { | |
994 | if ( teststyle != wxNORMAL ) | |
995 | { | |
996 | teststyle = wxNORMAL; | |
997 | } | |
998 | else | |
999 | { | |
1000 | break; | |
1001 | } | |
1002 | } | |
1003 | // Search for equal or smaller size (approx.) | |
1004 | for ( i = pointSize; !font && i >= 10 && i >= min_size; i -= 10 ) | |
1005 | { | |
1006 | font = wxLoadQueryFont(i, family, teststyle, testweight, underlined, | |
30764ab5 VZ |
1007 | facename, info.xregistry, info.xencoding, |
1008 | xFontName); | |
b12401e0 | 1009 | } |
7beba2fc | 1010 | |
b12401e0 MB |
1011 | // Search for larger size (approx.) |
1012 | for ( i = pointSize + 10; !font && i <= max_size; i += 10 ) | |
1013 | { | |
34791896 | 1014 | font = wxLoadQueryFont(i, family, teststyle, testweight, underlined, |
30764ab5 VZ |
1015 | facename, info.xregistry, info.xencoding, |
1016 | xFontName); | |
34791896 | 1017 | } |
7beba2fc VZ |
1018 | } |
1019 | ||
1020 | // Try default family | |
1021 | if ( !font && family != wxDEFAULT ) | |
1022 | { | |
1023 | font = wxLoadQueryFont(pointSize, wxDEFAULT, style, weight, | |
1024 | underlined, facename, | |
30764ab5 VZ |
1025 | info.xregistry, info.xencoding, |
1026 | xFontName ); | |
7beba2fc VZ |
1027 | } |
1028 | ||
f139dfe8 VZ |
1029 | // ignore size, family, style and weight but try to find font with the |
1030 | // given facename and encoding | |
7beba2fc VZ |
1031 | if ( !font ) |
1032 | { | |
1033 | font = wxLoadQueryFont(120, wxDEFAULT, wxNORMAL, wxNORMAL, | |
1034 | underlined, facename, | |
30764ab5 VZ |
1035 | info.xregistry, info.xencoding, |
1036 | xFontName); | |
7beba2fc | 1037 | |
f139dfe8 VZ |
1038 | // ignore family as well |
1039 | if ( !font ) | |
1040 | { | |
1041 | font = wxLoadQueryFont(120, wxDEFAULT, wxNORMAL, wxNORMAL, | |
1042 | underlined, wxEmptyString, | |
1043 | info.xregistry, info.xencoding, | |
1044 | xFontName); | |
1045 | ||
1046 | // if it still failed, try to get the font of any size but | |
1047 | // with the requested encoding: this can happen if the | |
1048 | // encoding is only available in one size which happens to be | |
1049 | // different from 120 | |
1050 | if ( !font ) | |
1051 | { | |
1052 | font = wxLoadQueryFont(-1, wxDEFAULT, wxNORMAL, wxNORMAL, | |
55034339 | 1053 | false, wxEmptyString, |
f139dfe8 VZ |
1054 | info.xregistry, info.xencoding, |
1055 | xFontName); | |
1056 | ||
1057 | // this should never happen as we had tested for it in the | |
1058 | // very beginning, but if it does, do return something non | |
1059 | // NULL or we'd crash in wxFont code | |
1060 | if ( !font ) | |
1061 | { | |
1062 | wxFAIL_MSG( _T("this encoding should be available!") ); | |
1063 | ||
1064 | font = wxLoadQueryFont(-1, | |
1065 | wxDEFAULT, wxNORMAL, wxNORMAL, | |
55034339 | 1066 | false, wxEmptyString, |
f139dfe8 VZ |
1067 | _T("*"), _T("*"), |
1068 | xFontName); | |
1069 | } | |
1070 | } | |
1071 | } | |
7beba2fc VZ |
1072 | } |
1073 | } | |
1074 | ||
1075 | return font; | |
1076 | } | |
1077 | ||
1078 | // ---------------------------------------------------------------------------- | |
1079 | // private functions | |
1080 | // ---------------------------------------------------------------------------- | |
1081 | ||
55034339 | 1082 | // returns true if there are any fonts matching this font spec |
7beba2fc VZ |
1083 | static bool wxTestFontSpec(const wxString& fontspec) |
1084 | { | |
97d3f0ee VZ |
1085 | // some X servers will fail to load this font because there are too many |
1086 | // matches so we must test explicitly for this | |
1087 | if ( fontspec == _T("-*-*-*-*-*-*-*-*-*-*-*-*-*-*") ) | |
1088 | { | |
55034339 | 1089 | return true; |
97d3f0ee VZ |
1090 | } |
1091 | ||
2e0e025e RR |
1092 | wxNativeFont test = (wxNativeFont) g_fontHash->Get( fontspec ); |
1093 | if (test) | |
1094 | { | |
55034339 | 1095 | return true; |
2e0e025e RR |
1096 | } |
1097 | ||
1098 | test = wxLoadFont(fontspec); | |
1099 | g_fontHash->Put( fontspec, (wxObject*) test ); | |
6c49baf2 | 1100 | |
7beba2fc VZ |
1101 | if ( test ) |
1102 | { | |
1103 | wxFreeFont(test); | |
1104 | ||
55034339 | 1105 | return true; |
7beba2fc VZ |
1106 | } |
1107 | else | |
1108 | { | |
55034339 | 1109 | return false; |
7beba2fc VZ |
1110 | } |
1111 | } | |
1112 | ||
1113 | static wxNativeFont wxLoadQueryFont(int pointSize, | |
1114 | int family, | |
1115 | int style, | |
1116 | int weight, | |
1117 | bool WXUNUSED(underlined), | |
1118 | const wxString& facename, | |
1119 | const wxString& xregistry, | |
30764ab5 VZ |
1120 | const wxString& xencoding, |
1121 | wxString* xFontName) | |
7beba2fc VZ |
1122 | { |
1123 | wxString xfamily; | |
1124 | switch (family) | |
1125 | { | |
1126 | case wxDECORATIVE: xfamily = wxT("lucida"); break; | |
1127 | case wxROMAN: xfamily = wxT("times"); break; | |
1128 | case wxMODERN: xfamily = wxT("courier"); break; | |
1129 | case wxSWISS: xfamily = wxT("helvetica"); break; | |
1130 | case wxTELETYPE: xfamily = wxT("lucidatypewriter"); break; | |
1131 | case wxSCRIPT: xfamily = wxT("utopia"); break; | |
1132 | default: xfamily = wxT("*"); | |
1133 | } | |
16d865f7 | 1134 | #if wxUSE_NANOX |
c2ff68d3 JS |
1135 | int xweight; |
1136 | switch (weight) | |
1137 | { | |
1138 | case wxBOLD: | |
1139 | { | |
1140 | xweight = MWLF_WEIGHT_BOLD; | |
1141 | break; | |
1142 | } | |
1143 | case wxLIGHT: | |
1144 | { | |
1145 | xweight = MWLF_WEIGHT_LIGHT; | |
1146 | break; | |
1147 | } | |
1148 | case wxNORMAL: | |
1149 | { | |
1150 | xweight = MWLF_WEIGHT_NORMAL; | |
1151 | break; | |
1152 | } | |
1153 | ||
1154 | default: | |
1155 | { | |
1156 | xweight = MWLF_WEIGHT_DEFAULT; | |
1157 | break; | |
1158 | } | |
1159 | } | |
16d865f7 JS |
1160 | GR_SCREEN_INFO screenInfo; |
1161 | GrGetScreenInfo(& screenInfo); | |
1162 | ||
1163 | int yPixelsPerCM = screenInfo.ydpcm; | |
1164 | ||
0a68bc69 | 1165 | // A point is 1/72 of an inch. |
16d865f7 | 1166 | // An inch is 2.541 cm. |
0a68bc69 | 1167 | // So pixelHeight = (pointSize / 72) (inches) * 2.541 (for cm) * yPixelsPerCM (for pixels) |
c2ff68d3 JS |
1168 | // In fact pointSize is 10 * the normal point size so |
1169 | // divide by 10. | |
1170 | ||
15d5a947 | 1171 | int pixelHeight = (int) ( (((float)pointSize) / 720.0) * 2.541 * (float) yPixelsPerCM) ; |
c2ff68d3 JS |
1172 | |
1173 | // An alternative: assume that the screen is 72 dpi. | |
0a68bc69 JS |
1174 | //int pixelHeight = (int) (((float)pointSize / 720.0) * 72.0) ; |
1175 | //int pixelHeight = (int) ((float)pointSize / 10.0) ; | |
2b5f62a0 | 1176 | |
16d865f7 JS |
1177 | GR_LOGFONT logFont; |
1178 | logFont.lfHeight = pixelHeight; | |
1179 | logFont.lfWidth = 0; | |
1180 | logFont.lfEscapement = 0; | |
1181 | logFont.lfOrientation = 0; | |
c2ff68d3 | 1182 | logFont.lfWeight = xweight; |
16d865f7 JS |
1183 | logFont.lfItalic = (style == wxNORMAL ? 0 : 1) ; |
1184 | logFont.lfUnderline = 0; | |
1185 | logFont.lfStrikeOut = 0; | |
1186 | logFont.lfCharSet = MWLF_CHARSET_DEFAULT; // TODO: select appropriate one | |
1187 | logFont.lfOutPrecision = MWLF_TYPE_DEFAULT; | |
1188 | logFont.lfClipPrecision = 0; // Not used | |
1189 | logFont.lfRoman = (family == wxROMAN ? 1 : 0) ; | |
1190 | logFont.lfSerif = (family == wxSWISS ? 0 : 1) ; | |
1191 | logFont.lfSansSerif = !logFont.lfSerif ; | |
1192 | logFont.lfModern = (family == wxMODERN ? 1 : 0) ; | |
1193 | logFont.lfProportional = (family == wxTELETYPE ? 0 : 1) ; | |
1194 | logFont.lfOblique = 0; | |
1195 | logFont.lfSmallCaps = 0; | |
1196 | logFont.lfPitch = 0; // 0 = default | |
1197 | strcpy(logFont.lfFaceName, facename.c_str()); | |
1198 | ||
1199 | XFontStruct* fontInfo = (XFontStruct*) malloc(sizeof(XFontStruct)); | |
1200 | fontInfo->fid = GrCreateFont((GR_CHAR*) facename.c_str(), pixelHeight, & logFont); | |
1201 | GrGetFontInfo(fontInfo->fid, & fontInfo->info); | |
1202 | return (wxNativeFont) fontInfo; | |
2b5f62a0 | 1203 | |
16d865f7 | 1204 | #else |
7beba2fc | 1205 | wxString fontSpec; |
55034339 | 1206 | if (!facename.empty()) |
7beba2fc VZ |
1207 | { |
1208 | fontSpec.Printf(wxT("-*-%s-*-*-normal-*-*-*-*-*-*-*-*-*"), | |
1209 | facename.c_str()); | |
1210 | ||
1211 | if ( wxTestFontSpec(fontSpec) ) | |
1212 | { | |
1213 | xfamily = facename; | |
1214 | } | |
1215 | //else: no such family, use default one instead | |
1216 | } | |
1217 | ||
1218 | wxString xstyle; | |
1219 | switch (style) | |
1220 | { | |
f9dbf34f VZ |
1221 | case wxSLANT: |
1222 | fontSpec.Printf(wxT("-*-%s-*-o-*-*-*-*-*-*-*-*-*-*"), | |
1223 | xfamily.c_str()); | |
1224 | if ( wxTestFontSpec(fontSpec) ) | |
1225 | { | |
1226 | xstyle = wxT("o"); | |
1227 | break; | |
1228 | } | |
1229 | // fall through - try wxITALIC now | |
1230 | ||
1231 | case wxITALIC: | |
1232 | fontSpec.Printf(wxT("-*-%s-*-i-*-*-*-*-*-*-*-*-*-*"), | |
1233 | xfamily.c_str()); | |
1234 | if ( wxTestFontSpec(fontSpec) ) | |
1235 | { | |
1236 | xstyle = wxT("i"); | |
1237 | } | |
1238 | else if ( style == wxITALIC ) // and not wxSLANT | |
1239 | { | |
1240 | // try wxSLANT | |
1241 | fontSpec.Printf(wxT("-*-%s-*-o-*-*-*-*-*-*-*-*-*-*"), | |
1242 | xfamily.c_str()); | |
1243 | if ( wxTestFontSpec(fontSpec) ) | |
1244 | { | |
1245 | xstyle = wxT("o"); | |
1246 | } | |
1247 | else | |
1248 | { | |
1249 | // no italic, no slant - leave default | |
1250 | xstyle = wxT("*"); | |
1251 | } | |
1252 | } | |
1253 | break; | |
1254 | ||
1255 | default: | |
1256 | wxFAIL_MSG(_T("unknown font style")); | |
1257 | // fall back to normal | |
1258 | ||
1259 | case wxNORMAL: | |
1260 | xstyle = wxT("r"); | |
1261 | break; | |
7beba2fc VZ |
1262 | } |
1263 | ||
1264 | wxString xweight; | |
1265 | switch (weight) | |
1266 | { | |
1267 | case wxBOLD: | |
1268 | { | |
1269 | fontSpec.Printf(wxT("-*-%s-bold-*-*-*-*-*-*-*-*-*-*-*"), | |
1270 | xfamily.c_str()); | |
1271 | if ( wxTestFontSpec(fontSpec) ) | |
1272 | { | |
1273 | xweight = wxT("bold"); | |
1274 | break; | |
1275 | } | |
1276 | fontSpec.Printf(wxT("-*-%s-heavy-*-*-*-*-*-*-*-*-*-*-*"), | |
1277 | xfamily.c_str()); | |
1278 | if ( wxTestFontSpec(fontSpec) ) | |
1279 | { | |
1280 | xweight = wxT("heavy"); | |
1281 | break; | |
1282 | } | |
1283 | fontSpec.Printf(wxT("-*-%s-extrabold-*-*-*-*-*-*-*-*-*-*-*"), | |
1284 | xfamily.c_str()); | |
1285 | if ( wxTestFontSpec(fontSpec) ) | |
1286 | { | |
1287 | xweight = wxT("extrabold"); | |
1288 | break; | |
1289 | } | |
1290 | fontSpec.Printf(wxT("-*-%s-demibold-*-*-*-*-*-*-*-*-*-*-*"), | |
1291 | xfamily.c_str()); | |
1292 | if ( wxTestFontSpec(fontSpec) ) | |
1293 | { | |
1294 | xweight = wxT("demibold"); | |
1295 | break; | |
1296 | } | |
1297 | fontSpec.Printf(wxT("-*-%s-black-*-*-*-*-*-*-*-*-*-*-*"), | |
1298 | xfamily.c_str()); | |
1299 | if ( wxTestFontSpec(fontSpec) ) | |
1300 | { | |
1301 | xweight = wxT("black"); | |
1302 | break; | |
1303 | } | |
1304 | fontSpec.Printf(wxT("-*-%s-ultrablack-*-*-*-*-*-*-*-*-*-*-*"), | |
1305 | xfamily.c_str()); | |
1306 | if ( wxTestFontSpec(fontSpec) ) | |
1307 | { | |
1308 | xweight = wxT("ultrablack"); | |
1309 | break; | |
1310 | } | |
1311 | } | |
1312 | break; | |
1313 | case wxLIGHT: | |
1314 | { | |
1315 | fontSpec.Printf(wxT("-*-%s-light-*-*-*-*-*-*-*-*-*-*-*"), | |
1316 | xfamily.c_str()); | |
1317 | if ( wxTestFontSpec(fontSpec) ) | |
1318 | { | |
1319 | xweight = wxT("light"); | |
1320 | break; | |
1321 | } | |
1322 | fontSpec.Printf(wxT("-*-%s-thin-*-*-*-*-*-*-*-*-*-*-*"), | |
1323 | xfamily.c_str()); | |
1324 | if ( wxTestFontSpec(fontSpec) ) | |
1325 | { | |
1326 | xweight = wxT("thin"); | |
1327 | break; | |
1328 | } | |
1329 | } | |
1330 | break; | |
1331 | case wxNORMAL: | |
1332 | { | |
1333 | fontSpec.Printf(wxT("-*-%s-medium-*-*-*-*-*-*-*-*-*-*-*"), | |
1334 | xfamily.c_str()); | |
1335 | if ( wxTestFontSpec(fontSpec) ) | |
1336 | { | |
1337 | xweight = wxT("medium"); | |
1338 | break; | |
1339 | } | |
1340 | fontSpec.Printf(wxT("-*-%s-normal-*-*-*-*-*-*-*-*-*-*-*"), | |
1341 | xfamily.c_str()); | |
1342 | if ( wxTestFontSpec(fontSpec) ) | |
1343 | { | |
1344 | xweight = wxT("normal"); | |
1345 | break; | |
1346 | } | |
1347 | fontSpec.Printf(wxT("-*-%s-regular-*-*-*-*-*-*-*-*-*-*-*"), | |
1348 | xfamily.c_str()); | |
1349 | if ( wxTestFontSpec(fontSpec) ) | |
1350 | { | |
1351 | xweight = wxT("regular"); | |
1352 | break; | |
1353 | } | |
1354 | xweight = wxT("*"); | |
1355 | } | |
1356 | break; | |
1357 | default: xweight = wxT("*"); break; | |
1358 | } | |
1359 | ||
f139dfe8 VZ |
1360 | // if pointSize is -1, don't specify any |
1361 | wxString sizeSpec; | |
73b0423d | 1362 | if ( pointSize == -1 ) |
f139dfe8 VZ |
1363 | { |
1364 | sizeSpec = _T('*'); | |
1365 | } | |
1366 | else | |
1367 | { | |
1368 | sizeSpec.Printf(_T("%d"), pointSize); | |
1369 | } | |
1370 | ||
7beba2fc | 1371 | // construct the X font spec from our data |
f139dfe8 | 1372 | fontSpec.Printf(wxT("-*-%s-%s-%s-normal-*-*-%s-*-*-*-*-%s-%s"), |
7beba2fc | 1373 | xfamily.c_str(), xweight.c_str(), xstyle.c_str(), |
f139dfe8 | 1374 | sizeSpec.c_str(), xregistry.c_str(), xencoding.c_str()); |
7beba2fc | 1375 | |
30764ab5 VZ |
1376 | if( xFontName ) |
1377 | *xFontName = fontSpec; | |
1378 | ||
7beba2fc | 1379 | return wxLoadFont(fontSpec); |
16d865f7 JS |
1380 | #endif |
1381 | // wxUSE_NANOX | |
7beba2fc VZ |
1382 | } |
1383 | ||
2e0e025e RR |
1384 | // ---------------------------------------------------------------------------- |
1385 | // wxFontModule | |
1386 | // ---------------------------------------------------------------------------- | |
1387 | ||
1388 | class wxFontModule : public wxModule | |
1389 | { | |
1390 | public: | |
1391 | bool OnInit(); | |
1392 | void OnExit(); | |
1393 | ||
1394 | private: | |
1395 | DECLARE_DYNAMIC_CLASS(wxFontModule) | |
1396 | }; | |
1397 | ||
1398 | IMPLEMENT_DYNAMIC_CLASS(wxFontModule, wxModule) | |
1399 | ||
1400 | bool wxFontModule::OnInit() | |
1401 | { | |
1402 | g_fontHash = new wxHashTable( wxKEY_STRING ); | |
1403 | ||
55034339 | 1404 | return true; |
2e0e025e RR |
1405 | } |
1406 | ||
1407 | void wxFontModule::OnExit() | |
1408 | { | |
1409 | delete g_fontHash; | |
1410 | ||
1411 | g_fontHash = (wxHashTable *)NULL; | |
1412 | } | |
db16cab4 | 1413 | |
2b5f62a0 | 1414 | #endif // GTK 2.0/1.x |