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