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