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