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