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