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