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