1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/unix/fontutil.cpp
3 // Purpose: Font helper functions for wxX11, wxGTK, wxMotif
4 // Author: Vadim Zeitlin
8 // Copyright: (c) Vadim Zeitlin
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
27 #include "wx/fontutil.h"
31 #include "wx/font.h" // wxFont enums
32 #include "wx/encinfo.h"
34 #include "wx/utils.h" // for wxGetDisplay()
35 #include "wx/module.h"
38 #include "wx/fontmap.h"
39 #include "wx/tokenzr.h"
40 #include "wx/fontenum.h"
44 #include "pango/pango.h"
47 #include "wx/gtk/private.h"
48 extern GtkWidget
*wxGetRootWindow();
50 #define wxPANGO_CONV wxGTK_CONV_SYS
51 #define wxPANGO_CONV_BACK wxGTK_CONV_BACK_SYS
53 #include "wx/x11/private.h"
54 #include "wx/gtk/private/string.h"
56 #define wxPANGO_CONV(s) (wxConvUTF8.cWX2MB((s)))
57 #define wxPANGO_CONV_BACK(s) (wxConvUTF8.cMB2WX((s)))
60 // ----------------------------------------------------------------------------
62 // ----------------------------------------------------------------------------
64 void wxNativeFontInfo::Init()
69 void wxNativeFontInfo::Init(const wxNativeFontInfo
& info
)
72 description
= pango_font_description_copy(info
.description
);
77 void wxNativeFontInfo::Free()
80 pango_font_description_free(description
);
83 int wxNativeFontInfo::GetPointSize() const
85 return pango_font_description_get_size( description
) / PANGO_SCALE
;
88 wxFontStyle
wxNativeFontInfo::GetStyle() const
90 wxFontStyle m_style
= wxFONTSTYLE_NORMAL
;
92 switch (pango_font_description_get_style( description
))
94 case PANGO_STYLE_NORMAL
:
95 m_style
= wxFONTSTYLE_NORMAL
;
97 case PANGO_STYLE_ITALIC
:
98 m_style
= wxFONTSTYLE_ITALIC
;
100 case PANGO_STYLE_OBLIQUE
:
101 m_style
= wxFONTSTYLE_SLANT
;
108 wxFontWeight
wxNativeFontInfo::GetWeight() const
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;
115 PangoWeight pango_weight
= pango_font_description_get_weight( description
);
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)
122 if (pango_weight
>= 600)
123 return wxFONTWEIGHT_BOLD
;
125 if (pango_weight
< 350)
126 return wxFONTWEIGHT_LIGHT
;
128 return wxFONTWEIGHT_NORMAL
;
131 bool wxNativeFontInfo::GetUnderlined() const
136 wxString
wxNativeFontInfo::GetFaceName() const
138 // the Pango "family" is the wx "face name"
139 return wxPANGO_CONV_BACK(pango_font_description_get_family(description
));
142 wxFontFamily
wxNativeFontInfo::GetFamily() const
144 wxFontFamily ret
= wxFONTFAMILY_UNKNOWN
;
146 const char *family_name
= pango_font_description_get_family( description
);
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.
153 wxGtkString
family_text(g_ascii_strdown(family_name
, strlen(family_name
)));
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)
164 if (!gtk_check_version(2,4,0))
167 PangoFontFamily
**families
;
168 PangoFontFamily
*family
= NULL
;
170 pango_context_list_families(
172 gtk_widget_get_pango_context( wxGetRootWindow() ),
174 wxTheApp
->GetPangoContext(),
176 &families
, &n_families
);
178 for (int i
= 0; i
< n_families
; ++i
)
180 if (g_ascii_strcasecmp(pango_font_family_get_name( families
[i
] ),
181 pango_font_description_get_family( description
)) == 0 )
183 family
= families
[i
];
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" );
195 if (family
!= NULL
&& pango_font_family_is_monospace( family
))
196 ret
= wxFONTFAMILY_TELETYPE
; // is deemed a monospace font by pango
198 #endif // GTK+ 2 || HAVE_PANGO_FONT_FAMILY_IS_MONOSPACE
200 if (ret
== wxFONTFAMILY_UNKNOWN
)
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"
216 wxFontEncoding
wxNativeFontInfo::GetEncoding() const
218 return wxFONTENCODING_SYSTEM
;
221 void wxNativeFontInfo::SetPointSize(int pointsize
)
223 pango_font_description_set_size( description
, pointsize
* PANGO_SCALE
);
226 void wxNativeFontInfo::SetStyle(wxFontStyle style
)
230 case wxFONTSTYLE_ITALIC
:
231 pango_font_description_set_style( description
, PANGO_STYLE_ITALIC
);
233 case wxFONTSTYLE_SLANT
:
234 pango_font_description_set_style( description
, PANGO_STYLE_OBLIQUE
);
237 wxFAIL_MSG( "unknown font style" );
239 case wxFONTSTYLE_NORMAL
:
240 pango_font_description_set_style( description
, PANGO_STYLE_NORMAL
);
245 void wxNativeFontInfo::SetWeight(wxFontWeight weight
)
249 case wxFONTWEIGHT_BOLD
:
250 pango_font_description_set_weight(description
, PANGO_WEIGHT_BOLD
);
252 case wxFONTWEIGHT_LIGHT
:
253 pango_font_description_set_weight(description
, PANGO_WEIGHT_LIGHT
);
256 wxFAIL_MSG( "unknown font weight" );
258 case wxFONTWEIGHT_NORMAL
:
259 pango_font_description_set_weight(description
, PANGO_WEIGHT_NORMAL
);
263 void wxNativeFontInfo::SetUnderlined(bool WXUNUSED(underlined
))
265 // wxWindowDCImpl::DoDrawText will take care of rendering font with
266 // the underline attribute!
267 wxFAIL_MSG( "not implemented" );
270 bool wxNativeFontInfo::SetFaceName(const wxString
& facename
)
272 pango_font_description_set_family(description
, wxPANGO_CONV(facename
));
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
280 void wxNativeFontInfo::SetFamily(wxFontFamily family
)
282 wxArrayString facename
;
284 // the list of fonts associated with a family was partially
285 // taken from http://www.codestyle.org/css/font-family
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"));
295 case wxFONTFAMILY_DECORATIVE
:
296 // corresponds to the fantasy font family in the page linked above
297 facename
.Add(wxS("Impact"));
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"));
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"));
324 case wxFONTFAMILY_SWISS
:
325 case wxFONTFAMILY_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"));
338 SetFaceName(facename
);
341 void wxNativeFontInfo::SetEncoding(wxFontEncoding
WXUNUSED(encoding
))
343 wxFAIL_MSG( "not implemented: Pango encoding is always UTF8" );
346 bool wxNativeFontInfo::FromString(const wxString
& s
)
349 pango_font_description_free( description
);
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 !!
359 const size_t pos
= str
.find_last_of(wxS(" "));
361 if ( pos
!= wxString::npos
&& wxString(str
, pos
+ 1).ToDouble(&size
) )
366 else if ( size
>= 1E6
)
367 sizeStr
= wxS("1E6");
369 if ( !sizeStr
.empty() )
371 // replace the old size with the adjusted one
372 str
= wxString(s
, 0, pos
) + sizeStr
;
376 description
= pango_font_description_from_string(wxPANGO_CONV(str
));
379 // ensure a valid facename is selected
380 if (!wxFontEnumerator::IsValidFacename(GetFaceName()))
381 SetFaceName(wxNORMAL_FONT
->GetFaceName());
382 #endif // wxUSE_FONTENUM
387 wxString
wxNativeFontInfo::ToString() const
389 wxGtkString
str(pango_font_description_to_string( description
));
391 return wxPANGO_CONV_BACK(str
);
394 bool wxNativeFontInfo::FromUserString(const wxString
& s
)
396 return FromString( s
);
399 wxString
wxNativeFontInfo::ToUserString() const
408 #pragma message disable nosimpint
411 #include <X11/Xlib.h>
414 #pragma message enable nosimpint
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
428 // ----------------------------------------------------------------------------
430 // ----------------------------------------------------------------------------
432 static wxHashTable
*g_fontHash
= NULL
;
434 // ----------------------------------------------------------------------------
436 // ----------------------------------------------------------------------------
438 // define the functions to create and destroy native fonts for this toolkit
440 wxNativeFont
wxLoadFont(const wxString
& fontSpec
)
442 return XLoadQueryFont((Display
*)wxGetDisplay(), fontSpec
);
445 inline void wxFreeFont(wxNativeFont font
)
447 XFreeFont((Display
*)wxGetDisplay(), (XFontStruct
*)font
);
449 #elif defined(__WXGTK__)
450 wxNativeFont
wxLoadFont(const wxString
& fontSpec
)
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
) );
461 inline void wxFreeFont(wxNativeFont font
)
463 gdk_font_unref(font
);
466 #error "Unknown GUI toolkit"
469 static bool wxTestFontSpec(const wxString
& fontspec
);
471 static wxNativeFont
wxLoadQueryFont(int pointSize
,
476 const wxString
& facename
,
477 const wxString
& xregistry
,
478 const wxString
& xencoding
,
479 wxString
* xFontName
);
481 // ============================================================================
483 // ============================================================================
485 // ----------------------------------------------------------------------------
486 // wxNativeEncodingInfo
487 // ----------------------------------------------------------------------------
489 // convert to/from the string representation: format is
490 // encodingid;registry;encoding[;facename]
491 bool wxNativeEncodingInfo::FromString(const wxString
& s
)
493 // use ";", not "-" because it may be part of encoding name
494 wxStringTokenizer
tokenizer(s
, wxT(";"));
496 wxString encid
= tokenizer
.GetNextToken();
498 if ( !encid
.ToLong(&enc
) )
500 encoding
= (wxFontEncoding
)enc
;
502 xregistry
= tokenizer
.GetNextToken();
506 xencoding
= tokenizer
.GetNextToken();
511 facename
= tokenizer
.GetNextToken();
516 wxString
wxNativeEncodingInfo::ToString() const
519 s
<< (long)encoding
<< wxT(';') << xregistry
<< wxT(';') << xencoding
;
520 if ( !facename
.empty() )
522 s
<< wxT(';') << facename
;
528 // ----------------------------------------------------------------------------
530 // ----------------------------------------------------------------------------
532 void wxNativeFontInfo::Init()
537 bool wxNativeFontInfo::FromString(const wxString
& s
)
539 wxStringTokenizer
tokenizer(s
, wxT(";"));
542 wxString token
= tokenizer
.GetNextToken();
543 if ( token
!= wxT('0') )
546 xFontName
= tokenizer
.GetNextToken();
548 // this should be the end
549 if ( tokenizer
.HasMoreTokens() )
552 return FromXFontName(xFontName
);
555 wxString
wxNativeFontInfo::ToString() const
558 return wxString::Format(wxT("%d;%s"), 0, GetXFontName().c_str());
561 bool wxNativeFontInfo::FromUserString(const wxString
& s
)
563 return FromXFontName(s
);
566 wxString
wxNativeFontInfo::ToUserString() const
568 return GetXFontName();
571 bool wxNativeFontInfo::HasElements() const
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();
578 wxString
wxNativeFontInfo::GetXFontComponent(wxXLFDField field
) const
580 wxCHECK_MSG( field
< wxXLFD_MAX
, wxEmptyString
, wxT("invalid XLFD field") );
582 if ( !HasElements() )
585 if ( !((wxNativeFontInfo
*)this)->FromXFontName(xFontName
) )
586 return wxEmptyString
;
589 return fontElements
[field
];
592 bool wxNativeFontInfo::FromXFontName(const wxString
& fontname
)
594 // TODO: we should be able to handle the font aliases here, but how?
595 wxStringTokenizer
tokenizer(fontname
, wxT("-"));
597 // skip the leading, usually empty field (font name registry)
598 if ( !tokenizer
.HasMoreTokens() )
601 (void)tokenizer
.GetNextToken();
603 for ( size_t n
= 0; n
< WXSIZEOF(fontElements
); n
++ )
605 if ( !tokenizer
.HasMoreTokens() )
607 // not enough elements in the XLFD - or maybe an alias
611 wxString field
= tokenizer
.GetNextToken();
612 if ( !field
.empty() && field
!= wxT('*') )
614 // we're really initialized now
618 fontElements
[n
] = field
;
621 // this should be all
622 if ( tokenizer
.HasMoreTokens() )
628 wxString
wxNativeFontInfo::GetXFontName() const
630 if ( xFontName
.empty() )
632 for ( size_t n
= 0; n
< WXSIZEOF(fontElements
); n
++ )
634 // replace the non specified elements with '*' except for the
635 // additional style which is usually just omitted
636 wxString elt
= fontElements
[n
];
637 if ( elt
.empty() && n
!= wxXLFD_ADDSTYLE
)
643 ((wxNativeFontInfo
*)this)->xFontName
<< wxT('-') << elt
;
651 wxNativeFontInfo::SetXFontComponent(wxXLFDField field
, const wxString
& value
)
653 wxCHECK_RET( field
< wxXLFD_MAX
, wxT("invalid XLFD field") );
655 // this class should be initialized with a valid font spec first and only
656 // then the fields may be modified!
657 wxASSERT_MSG( !IsDefault(), wxT("can't modify an uninitialized XLFD") );
659 if ( !HasElements() )
662 if ( !((wxNativeFontInfo
*)this)->FromXFontName(xFontName
) )
664 wxFAIL_MSG( wxT("can't set font element for invalid XLFD") );
670 fontElements
[field
] = value
;
672 // invalidate the XFLD, it doesn't correspond to the font elements any more
676 void wxNativeFontInfo::SetXFontName(const wxString
& xFontName_
)
678 // invalidate the font elements, GetXFontComponent() will reparse the XLFD
679 fontElements
[0].clear();
681 xFontName
= xFontName_
;
686 int wxNativeFontInfo::GetPointSize() const
688 const wxString s
= GetXFontComponent(wxXLFD_POINTSIZE
);
690 // return -1 to indicate that the size is unknown
692 return s
.ToLong(&l
) ? l
: -1;
695 wxFontStyle
wxNativeFontInfo::GetStyle() const
697 const wxString s
= GetXFontComponent(wxXLFD_SLANT
);
699 if ( s
.length() != 1 )
701 // it is really unknown but we don't have any way to return it from
703 return wxFONTSTYLE_NORMAL
;
706 switch ( s
[0].GetValue() )
709 // again, unknown but consider normal by default
712 return wxFONTSTYLE_NORMAL
;
715 return wxFONTSTYLE_ITALIC
;
718 return wxFONTSTYLE_SLANT
;
722 wxFontWeight
wxNativeFontInfo::GetWeight() const
724 const wxString s
= GetXFontComponent(wxXLFD_WEIGHT
).MakeLower();
725 if ( s
.find(wxT("bold")) != wxString::npos
|| s
== wxT("black") )
726 return wxFONTWEIGHT_BOLD
;
727 else if ( s
== wxT("light") )
728 return wxFONTWEIGHT_LIGHT
;
730 return wxFONTWEIGHT_NORMAL
;
733 bool wxNativeFontInfo::GetUnderlined() const
735 // X fonts are never underlined
739 wxString
wxNativeFontInfo::GetFaceName() const
741 // wxWidgets facename probably more accurately corresponds to X family
742 return GetXFontComponent(wxXLFD_FAMILY
);
745 wxFontFamily
wxNativeFontInfo::GetFamily() const
747 // and wxWidgets family -- to X foundry, but we have to translate it to
748 // wxFontFamily somehow...
749 wxFAIL_MSG(wxT("not implemented")); // GetXFontComponent(wxXLFD_FOUNDRY);
751 return wxFONTFAMILY_DEFAULT
;
754 wxFontEncoding
wxNativeFontInfo::GetEncoding() const
756 // we already have the code for this but need to refactor it first
757 wxFAIL_MSG( wxT("not implemented") );
759 return wxFONTENCODING_MAX
;
762 void wxNativeFontInfo::SetPointSize(int pointsize
)
764 SetXFontComponent(wxXLFD_POINTSIZE
, wxString::Format(wxT("%d"), pointsize
));
767 void wxNativeFontInfo::SetStyle(wxFontStyle style
)
772 case wxFONTSTYLE_ITALIC
:
776 case wxFONTSTYLE_SLANT
:
780 case wxFONTSTYLE_NORMAL
:
784 wxFAIL_MSG( wxT("unknown wxFontStyle in wxNativeFontInfo::SetStyle") );
788 SetXFontComponent(wxXLFD_SLANT
, s
);
791 void wxNativeFontInfo::SetWeight(wxFontWeight weight
)
796 case wxFONTWEIGHT_BOLD
:
800 case wxFONTWEIGHT_LIGHT
:
804 case wxFONTWEIGHT_NORMAL
:
809 wxFAIL_MSG( wxT("unknown wxFontWeight in wxNativeFontInfo::SetWeight") );
813 SetXFontComponent(wxXLFD_WEIGHT
, s
);
816 void wxNativeFontInfo::SetUnderlined(bool WXUNUSED(underlined
))
818 // can't do this under X
821 bool wxNativeFontInfo::SetFaceName(const wxString
& facename
)
823 SetXFontComponent(wxXLFD_FAMILY
, facename
);
827 void wxNativeFontInfo::SetFamily(wxFontFamily
WXUNUSED(family
))
829 // wxFontFamily -> X foundry, anyone?
830 wxFAIL_MSG( wxT("not implemented") );
832 // SetXFontComponent(wxXLFD_FOUNDRY, ...);
835 void wxNativeFontInfo::SetEncoding(wxFontEncoding encoding
)
837 wxNativeEncodingInfo info
;
838 if ( wxGetNativeFontEncoding(encoding
, &info
) )
840 SetXFontComponent(wxXLFD_ENCODING
, info
.xencoding
);
841 SetXFontComponent(wxXLFD_REGISTRY
, info
.xregistry
);
845 // ----------------------------------------------------------------------------
847 // ----------------------------------------------------------------------------
849 bool wxGetNativeFontEncoding(wxFontEncoding encoding
,
850 wxNativeEncodingInfo
*info
)
852 wxCHECK_MSG( info
, false, wxT("bad pointer in wxGetNativeFontEncoding") );
854 if ( encoding
== wxFONTENCODING_DEFAULT
)
856 encoding
= wxFont::GetDefaultEncoding();
861 case wxFONTENCODING_ISO8859_1
:
862 case wxFONTENCODING_ISO8859_2
:
863 case wxFONTENCODING_ISO8859_3
:
864 case wxFONTENCODING_ISO8859_4
:
865 case wxFONTENCODING_ISO8859_5
:
866 case wxFONTENCODING_ISO8859_6
:
867 case wxFONTENCODING_ISO8859_7
:
868 case wxFONTENCODING_ISO8859_8
:
869 case wxFONTENCODING_ISO8859_9
:
870 case wxFONTENCODING_ISO8859_10
:
871 case wxFONTENCODING_ISO8859_11
:
872 case wxFONTENCODING_ISO8859_12
:
873 case wxFONTENCODING_ISO8859_13
:
874 case wxFONTENCODING_ISO8859_14
:
875 case wxFONTENCODING_ISO8859_15
:
877 int cp
= encoding
- wxFONTENCODING_ISO8859_1
+ 1;
878 info
->xregistry
= wxT("iso8859");
879 info
->xencoding
.Printf(wxT("%d"), cp
);
883 case wxFONTENCODING_UTF8
:
884 info
->xregistry
= wxT("iso10646");
885 info
->xencoding
= wxT("*");
888 case wxFONTENCODING_GB2312
:
889 info
->xregistry
= wxT("GB2312"); // or the otherway round?
890 info
->xencoding
= wxT("*");
893 case wxFONTENCODING_KOI8
:
894 case wxFONTENCODING_KOI8_U
:
895 info
->xregistry
= wxT("koi8");
897 // we don't make distinction between koi8-r, koi8-u and koi8-ru (so far)
898 info
->xencoding
= wxT("*");
901 case wxFONTENCODING_CP1250
:
902 case wxFONTENCODING_CP1251
:
903 case wxFONTENCODING_CP1252
:
904 case wxFONTENCODING_CP1253
:
905 case wxFONTENCODING_CP1254
:
906 case wxFONTENCODING_CP1255
:
907 case wxFONTENCODING_CP1256
:
908 case wxFONTENCODING_CP1257
:
910 int cp
= encoding
- wxFONTENCODING_CP1250
+ 1250;
911 info
->xregistry
= wxT("microsoft");
912 info
->xencoding
.Printf(wxT("cp%d"), cp
);
916 case wxFONTENCODING_EUC_JP
:
917 case wxFONTENCODING_SHIFT_JIS
:
918 info
->xregistry
= "jis*";
919 info
->xencoding
= "*";
922 case wxFONTENCODING_SYSTEM
:
924 info
->xencoding
= wxT("*");
928 // don't know how to translate this encoding into X fontspec
932 info
->encoding
= encoding
;
937 bool wxTestFontEncoding(const wxNativeEncodingInfo
& info
)
940 fontspec
.Printf(wxT("-*-%s-*-*-*-*-*-*-*-*-*-*-%s-%s"),
941 !info
.facename
? wxT("*") : info
.facename
.c_str(),
942 info
.xregistry
.c_str(),
943 info
.xencoding
.c_str());
945 return wxTestFontSpec(fontspec
);
948 // ----------------------------------------------------------------------------
949 // X-specific functions
950 // ----------------------------------------------------------------------------
952 wxNativeFont
wxLoadQueryNearestFont(int pointSize
,
957 const wxString
&facename
,
958 wxFontEncoding encoding
,
961 if ( encoding
== wxFONTENCODING_DEFAULT
)
963 encoding
= wxFont::GetDefaultEncoding();
966 // first determine the encoding - if the font doesn't exist at all in this
967 // encoding, it's useless to do all other approximations (i.e. size,
968 // family &c don't matter much)
969 wxNativeEncodingInfo info
;
970 if ( encoding
== wxFONTENCODING_SYSTEM
)
972 // This will always work so we don't test to save time
973 wxGetNativeFontEncoding(wxFONTENCODING_SYSTEM
, &info
);
977 if ( !wxGetNativeFontEncoding(encoding
, &info
) ||
978 !wxTestFontEncoding(info
) )
981 if ( !wxFontMapper::Get()->GetAltForEncoding(encoding
, &info
) )
982 #endif // wxUSE_FONTMAP
984 // unspported encoding - replace it with the default
986 // NB: we can't just return 0 from here because wxGTK code doesn't
987 // check for it (i.e. it supposes that we'll always succeed),
988 // so it would provoke a crash
989 wxGetNativeFontEncoding(wxFONTENCODING_SYSTEM
, &info
);
994 // OK, we have the correct xregistry/xencoding in info structure
995 wxNativeFont font
= 0;
997 // if we already have the X font name, try to use it
998 if( xFontName
&& !xFontName
->empty() )
1001 // Make sure point size is correct for scale factor.
1003 wxStringTokenizer
tokenizer(*xFontName
, wxT("-"), wxTOKEN_RET_DELIMS
);
1004 wxString newFontName
;
1006 for(int i
= 0; i
< 8; i
++)
1007 newFontName
+= tokenizer
.NextToken();
1009 (void) tokenizer
.NextToken();
1011 newFontName
+= wxString::Format(wxT("%d-"), pointSize
);
1013 while(tokenizer
.HasMoreTokens())
1014 newFontName
+= tokenizer
.GetNextToken();
1016 font
= wxLoadFont(newFontName
);
1019 *xFontName
= newFontName
;
1024 // search up and down by stepsize 10
1025 int max_size
= pointSize
+ 20 * (1 + (pointSize
/180));
1026 int min_size
= pointSize
- 20 * (1 + (pointSize
/180));
1028 int i
, round
; // counters
1030 // first round: search for equal, then for smaller and for larger size
1031 // with the given weight and style
1032 int testweight
= weight
;
1033 int teststyle
= style
;
1035 for ( round
= 0; round
< 3; round
++ )
1037 // second round: use normal weight
1040 if ( testweight
!= wxNORMAL
)
1042 testweight
= wxNORMAL
;
1046 ++round
; // fall through to third round
1050 // third round: ... and use normal style
1053 if ( teststyle
!= wxNORMAL
)
1055 teststyle
= wxNORMAL
;
1062 // Search for equal or smaller size (approx.)
1063 for ( i
= pointSize
; !font
&& i
>= 10 && i
>= min_size
; i
-= 10 )
1065 font
= wxLoadQueryFont(i
, family
, teststyle
, testweight
, underlined
,
1066 facename
, info
.xregistry
, info
.xencoding
,
1070 // Search for larger size (approx.)
1071 for ( i
= pointSize
+ 10; !font
&& i
<= max_size
; i
+= 10 )
1073 font
= wxLoadQueryFont(i
, family
, teststyle
, testweight
, underlined
,
1074 facename
, info
.xregistry
, info
.xencoding
,
1079 // Try default family
1080 if ( !font
&& family
!= wxDEFAULT
)
1082 font
= wxLoadQueryFont(pointSize
, wxDEFAULT
, style
, weight
,
1083 underlined
, facename
,
1084 info
.xregistry
, info
.xencoding
,
1088 // ignore size, family, style and weight but try to find font with the
1089 // given facename and encoding
1092 font
= wxLoadQueryFont(120, wxDEFAULT
, wxNORMAL
, wxNORMAL
,
1093 underlined
, facename
,
1094 info
.xregistry
, info
.xencoding
,
1097 // ignore family as well
1100 font
= wxLoadQueryFont(120, wxDEFAULT
, wxNORMAL
, wxNORMAL
,
1101 underlined
, wxEmptyString
,
1102 info
.xregistry
, info
.xencoding
,
1105 // if it still failed, try to get the font of any size but
1106 // with the requested encoding: this can happen if the
1107 // encoding is only available in one size which happens to be
1108 // different from 120
1111 font
= wxLoadQueryFont(-1, wxDEFAULT
, wxNORMAL
, wxNORMAL
,
1112 false, wxEmptyString
,
1113 info
.xregistry
, info
.xencoding
,
1116 // this should never happen as we had tested for it in the
1117 // very beginning, but if it does, do return something non
1118 // NULL or we'd crash in wxFont code
1121 wxFAIL_MSG( wxT("this encoding should be available!") );
1123 font
= wxLoadQueryFont(-1,
1124 wxDEFAULT
, wxNORMAL
, wxNORMAL
,
1125 false, wxEmptyString
,
1137 // ----------------------------------------------------------------------------
1138 // private functions
1139 // ----------------------------------------------------------------------------
1141 // returns true if there are any fonts matching this font spec
1142 static bool wxTestFontSpec(const wxString
& fontspec
)
1144 // some X servers will fail to load this font because there are too many
1145 // matches so we must test explicitly for this
1146 if ( fontspec
== wxT("-*-*-*-*-*-*-*-*-*-*-*-*-*-*") )
1151 wxNativeFont test
= (wxNativeFont
) g_fontHash
->Get( fontspec
);
1157 test
= wxLoadFont(fontspec
);
1158 g_fontHash
->Put( fontspec
, (wxObject
*) test
);
1172 static wxNativeFont
wxLoadQueryFont(int pointSize
,
1176 bool WXUNUSED(underlined
),
1177 const wxString
& facename
,
1178 const wxString
& xregistry
,
1179 const wxString
& xencoding
,
1180 wxString
* xFontName
)
1185 case wxDECORATIVE
: xfamily
= wxT("lucida"); break;
1186 case wxROMAN
: xfamily
= wxT("times"); break;
1187 case wxMODERN
: xfamily
= wxT("courier"); break;
1188 case wxSWISS
: xfamily
= wxT("helvetica"); break;
1189 case wxTELETYPE
: xfamily
= wxT("lucidatypewriter"); break;
1190 case wxSCRIPT
: xfamily
= wxT("utopia"); break;
1191 default: xfamily
= wxT("*");
1199 xweight
= MWLF_WEIGHT_BOLD
;
1204 xweight
= MWLF_WEIGHT_LIGHT
;
1209 xweight
= MWLF_WEIGHT_NORMAL
;
1215 xweight
= MWLF_WEIGHT_DEFAULT
;
1219 GR_SCREEN_INFO screenInfo
;
1220 GrGetScreenInfo(& screenInfo
);
1222 int yPixelsPerCM
= screenInfo
.ydpcm
;
1224 // A point is 1/72 of an inch.
1225 // An inch is 2.541 cm.
1226 // So pixelHeight = (pointSize / 72) (inches) * 2.541 (for cm) * yPixelsPerCM (for pixels)
1227 // In fact pointSize is 10 * the normal point size so
1230 int pixelHeight
= (int) ( (((float)pointSize
) / 720.0) * 2.541 * (float) yPixelsPerCM
) ;
1232 // An alternative: assume that the screen is 72 dpi.
1233 //int pixelHeight = (int) (((float)pointSize / 720.0) * 72.0) ;
1234 //int pixelHeight = (int) ((float)pointSize / 10.0) ;
1237 logFont
.lfHeight
= pixelHeight
;
1238 logFont
.lfWidth
= 0;
1239 logFont
.lfEscapement
= 0;
1240 logFont
.lfOrientation
= 0;
1241 logFont
.lfWeight
= xweight
;
1242 logFont
.lfItalic
= (style
== wxNORMAL
? 0 : 1) ;
1243 logFont
.lfUnderline
= 0;
1244 logFont
.lfStrikeOut
= 0;
1245 logFont
.lfCharSet
= MWLF_CHARSET_DEFAULT
; // TODO: select appropriate one
1246 logFont
.lfOutPrecision
= MWLF_TYPE_DEFAULT
;
1247 logFont
.lfClipPrecision
= 0; // Not used
1248 logFont
.lfRoman
= (family
== wxROMAN
? 1 : 0) ;
1249 logFont
.lfSerif
= (family
== wxSWISS
? 0 : 1) ;
1250 logFont
.lfSansSerif
= !logFont
.lfSerif
;
1251 logFont
.lfModern
= (family
== wxMODERN
? 1 : 0) ;
1252 logFont
.lfProportional
= (family
== wxTELETYPE
? 0 : 1) ;
1253 logFont
.lfOblique
= 0;
1254 logFont
.lfSmallCaps
= 0;
1255 logFont
.lfPitch
= 0; // 0 = default
1256 strcpy(logFont
.lfFaceName
, facename
.c_str());
1258 XFontStruct
* fontInfo
= (XFontStruct
*) malloc(sizeof(XFontStruct
));
1259 fontInfo
->fid
= GrCreateFont((GR_CHAR
*) facename
.c_str(), pixelHeight
, & logFont
);
1260 GrGetFontInfo(fontInfo
->fid
, & fontInfo
->info
);
1261 return (wxNativeFont
) fontInfo
;
1265 if (!facename
.empty())
1267 fontSpec
.Printf(wxT("-*-%s-*-*-normal-*-*-*-*-*-*-*-*-*"),
1270 if ( wxTestFontSpec(fontSpec
) )
1274 //else: no such family, use default one instead
1281 fontSpec
.Printf(wxT("-*-%s-*-o-*-*-*-*-*-*-*-*-*-*"),
1283 if ( wxTestFontSpec(fontSpec
) )
1288 // fall through - try wxITALIC now
1291 fontSpec
.Printf(wxT("-*-%s-*-i-*-*-*-*-*-*-*-*-*-*"),
1293 if ( wxTestFontSpec(fontSpec
) )
1297 else if ( style
== wxITALIC
) // and not wxSLANT
1300 fontSpec
.Printf(wxT("-*-%s-*-o-*-*-*-*-*-*-*-*-*-*"),
1302 if ( wxTestFontSpec(fontSpec
) )
1308 // no italic, no slant - leave default
1315 wxFAIL_MSG(wxT("unknown font style"));
1316 // fall back to normal
1328 fontSpec
.Printf(wxT("-*-%s-bold-*-*-*-*-*-*-*-*-*-*-*"),
1330 if ( wxTestFontSpec(fontSpec
) )
1332 xweight
= wxT("bold");
1335 fontSpec
.Printf(wxT("-*-%s-heavy-*-*-*-*-*-*-*-*-*-*-*"),
1337 if ( wxTestFontSpec(fontSpec
) )
1339 xweight
= wxT("heavy");
1342 fontSpec
.Printf(wxT("-*-%s-extrabold-*-*-*-*-*-*-*-*-*-*-*"),
1344 if ( wxTestFontSpec(fontSpec
) )
1346 xweight
= wxT("extrabold");
1349 fontSpec
.Printf(wxT("-*-%s-demibold-*-*-*-*-*-*-*-*-*-*-*"),
1351 if ( wxTestFontSpec(fontSpec
) )
1353 xweight
= wxT("demibold");
1356 fontSpec
.Printf(wxT("-*-%s-black-*-*-*-*-*-*-*-*-*-*-*"),
1358 if ( wxTestFontSpec(fontSpec
) )
1360 xweight
= wxT("black");
1363 fontSpec
.Printf(wxT("-*-%s-ultrablack-*-*-*-*-*-*-*-*-*-*-*"),
1365 if ( wxTestFontSpec(fontSpec
) )
1367 xweight
= wxT("ultrablack");
1374 fontSpec
.Printf(wxT("-*-%s-light-*-*-*-*-*-*-*-*-*-*-*"),
1376 if ( wxTestFontSpec(fontSpec
) )
1378 xweight
= wxT("light");
1381 fontSpec
.Printf(wxT("-*-%s-thin-*-*-*-*-*-*-*-*-*-*-*"),
1383 if ( wxTestFontSpec(fontSpec
) )
1385 xweight
= wxT("thin");
1392 fontSpec
.Printf(wxT("-*-%s-medium-*-*-*-*-*-*-*-*-*-*-*"),
1394 if ( wxTestFontSpec(fontSpec
) )
1396 xweight
= wxT("medium");
1399 fontSpec
.Printf(wxT("-*-%s-normal-*-*-*-*-*-*-*-*-*-*-*"),
1401 if ( wxTestFontSpec(fontSpec
) )
1403 xweight
= wxT("normal");
1406 fontSpec
.Printf(wxT("-*-%s-regular-*-*-*-*-*-*-*-*-*-*-*"),
1408 if ( wxTestFontSpec(fontSpec
) )
1410 xweight
= wxT("regular");
1416 default: xweight
= wxT("*"); break;
1419 // if pointSize is -1, don't specify any
1421 if ( pointSize
== -1 )
1423 sizeSpec
= wxT('*');
1427 sizeSpec
.Printf(wxT("%d"), pointSize
);
1430 // construct the X font spec from our data
1431 fontSpec
.Printf(wxT("-*-%s-%s-%s-normal-*-*-%s-*-*-*-*-%s-%s"),
1432 xfamily
.c_str(), xweight
.c_str(), xstyle
.c_str(),
1433 sizeSpec
.c_str(), xregistry
.c_str(), xencoding
.c_str());
1436 *xFontName
= fontSpec
;
1438 return wxLoadFont(fontSpec
);
1443 // ----------------------------------------------------------------------------
1445 // ----------------------------------------------------------------------------
1447 class wxFontModule
: public wxModule
1454 DECLARE_DYNAMIC_CLASS(wxFontModule
)
1457 IMPLEMENT_DYNAMIC_CLASS(wxFontModule
, wxModule
)
1459 bool wxFontModule::OnInit()
1461 g_fontHash
= new wxHashTable( wxKEY_STRING
);
1466 void wxFontModule::OnExit()
1473 #endif // GTK 2.0/1.x