1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/x11/font.cpp
3 // Purpose: wxFont class
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // for compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
15 // ============================================================================
17 // ============================================================================
19 // ----------------------------------------------------------------------------
21 // ----------------------------------------------------------------------------
24 #pragma message disable nosimpint
25 #include "wx/vms_x_fix.h"
29 #pragma message enable nosimpint
32 #include "wx/string.h"
34 #include "wx/gdicmn.h"
35 #include "wx/utils.h" // for wxGetDisplay()
36 #include "wx/fontutil.h" // for wxNativeFontInfo
37 #include "wx/tokenzr.h"
38 #include "wx/settings.h"
40 #include "wx/x11/private.h"
42 IMPLEMENT_DYNAMIC_CLASS(wxFont
, wxGDIObject
)
44 // ----------------------------------------------------------------------------
46 // ----------------------------------------------------------------------------
48 // the default size (in points) for the fonts
49 static const int wxDEFAULT_FONT_SIZE
= 12;
54 // ----------------------------------------------------------------------------
56 // ----------------------------------------------------------------------------
58 // For every wxFont, there must be a font for each display and scale requested.
59 // So these objects are stored in wxFontRefData::m_fonts
60 class wxXFont
: public wxObject
66 WXFontStructPtr m_fontStruct
; // XFontStruct
67 WXDisplay
* m_display
; // XDisplay
68 int m_scale
; // Scale * 100
73 m_fontStruct
= (WXFontStructPtr
) 0;
74 m_display
= (WXDisplay
*) 0;
80 // Freeing the font used to produce a segv, but
81 // appears to be OK now (bug fix in X11?)
82 XFontStruct
* fontStruct
= (XFontStruct
*) m_fontStruct
;
83 XFreeFont((Display
*) m_display
, fontStruct
);
87 // ----------------------------------------------------------------------------
89 // ----------------------------------------------------------------------------
91 class wxFontRefData
: public wxObjectRefData
96 wxFontRefData(int size
= wxDEFAULT
,
97 int family
= wxDEFAULT
,
98 int style
= wxDEFAULT
,
99 int weight
= wxDEFAULT
,
100 bool underlined
= false,
101 const wxString
& faceName
= wxEmptyString
,
102 wxFontEncoding encoding
= wxFONTENCODING_DEFAULT
);
105 wxFontRefData(const wxFontRefData
& data
);
108 wxFontRefData(const wxString
& fontname
);
111 virtual ~wxFontRefData();
113 // setters: all of them also take care to modify m_nativeFontInfo if we
114 // have it so as to not lose the information not carried by our fields
115 void SetPointSize(int pointSize
);
116 void SetFamily(int family
);
117 void SetStyle(int style
);
118 void SetWeight(int weight
);
119 void SetUnderlined(bool underlined
);
120 void SetFaceName(const wxString
& facename
);
121 void SetEncoding(wxFontEncoding encoding
);
123 void SetNoAntiAliasing( bool no
= true ) { m_noAA
= no
; }
124 bool GetNoAntiAliasing() const { return m_noAA
; }
126 // and this one also modifies all the other font data fields
127 void SetNativeFontInfo(const wxNativeFontInfo
& info
);
130 // common part of all ctors
136 const wxString
& faceName
,
137 wxFontEncoding encoding
);
139 // set all fields from (already initialized and valid) m_nativeFontInfo
140 void InitFromNative();
149 wxFontEncoding m_encoding
; // Unused in Unicode mode
150 bool m_noAA
; // No anti-aliasing
152 wxNativeFontInfo m_nativeFontInfo
;
154 void ClearX11Fonts();
158 // A list of wxXFonts
163 // ----------------------------------------------------------------------------
165 // ----------------------------------------------------------------------------
167 void wxFontRefData::Init(int pointSize
,
172 const wxString
& faceName
,
173 wxFontEncoding encoding
)
175 m_family
= family
== wxFONTFAMILY_DEFAULT
? wxFONTFAMILY_SWISS
: family
;
177 m_faceName
= faceName
;
179 // we accept both wxDEFAULT and wxNORMAL here - should we?
180 m_style
= style
== wxDEFAULT
? wxFONTSTYLE_NORMAL
: style
;
181 m_weight
= weight
== wxDEFAULT
? wxFONTWEIGHT_NORMAL
: weight
;
183 // and here, do we really want to forbid creation of the font of the size
184 // 90 (the value of wxDEFAULT)??
185 m_pointSize
= pointSize
== wxDEFAULT
|| pointSize
== -1
186 ? wxDEFAULT_FONT_SIZE
189 m_underlined
= underlined
;
190 m_encoding
= encoding
;
193 // Create native font info
194 m_nativeFontInfo
.description
= pango_font_description_new();
196 // And set its values
199 case wxFONTFAMILY_MODERN
:
200 case wxFONTFAMILY_TELETYPE
:
201 pango_font_description_set_family( m_nativeFontInfo
.description
, "monospace" );
203 case wxFONTFAMILY_ROMAN
:
204 pango_font_description_set_family( m_nativeFontInfo
.description
, "serif" );
207 pango_font_description_set_family( m_nativeFontInfo
.description
, "sans" );
211 SetPointSize( m_pointSize
);
212 SetWeight( m_weight
);
216 void wxFontRefData::InitFromNative()
222 PangoFontDescription
*desc
= m_nativeFontInfo
.description
;
225 m_faceName
= wxGTK_CONV_BACK( pango_font_description_get_family( desc
) );
227 m_pointSize
= pango_font_description_get_size( desc
) / PANGO_SCALE
;
229 switch (pango_font_description_get_style( desc
))
231 case PANGO_STYLE_NORMAL
:
232 m_style
= wxFONTSTYLE_NORMAL
;
234 case PANGO_STYLE_ITALIC
:
235 m_style
= wxFONTSTYLE_ITALIC
;
237 case PANGO_STYLE_OBLIQUE
:
238 m_style
= wxFONTSTYLE_SLANT
;
242 switch (pango_font_description_get_weight( desc
))
244 case PANGO_WEIGHT_ULTRALIGHT
:
245 m_weight
= wxFONTWEIGHT_LIGHT
;
247 case PANGO_WEIGHT_LIGHT
:
248 m_weight
= wxFONTWEIGHT_LIGHT
;
250 case PANGO_WEIGHT_NORMAL
:
251 m_weight
= wxFONTWEIGHT_NORMAL
;
253 case PANGO_WEIGHT_BOLD
:
254 m_weight
= wxFONTWEIGHT_BOLD
;
256 case PANGO_WEIGHT_ULTRABOLD
:
257 m_weight
= wxFONTWEIGHT_BOLD
;
259 case PANGO_WEIGHT_HEAVY
:
260 m_weight
= wxFONTWEIGHT_BOLD
;
264 if (m_faceName
== wxT("monospace"))
266 m_family
= wxFONTFAMILY_TELETYPE
;
268 else if (m_faceName
== wxT("sans"))
270 m_family
= wxFONTFAMILY_SWISS
;
274 m_family
= wxFONTFAMILY_UNKNOWN
;
277 // Pango description are never underlined (?)
278 m_underlined
= false;
280 // Cannot we choose that
281 m_encoding
= wxFONTENCODING_SYSTEM
;
283 // get the font parameters from the XLFD
284 // -------------------------------------
286 m_faceName
= m_nativeFontInfo
.GetXFontComponent(wxXLFD_FAMILY
);
288 m_weight
= wxFONTWEIGHT_NORMAL
;
290 wxString w
= m_nativeFontInfo
.GetXFontComponent(wxXLFD_WEIGHT
).Upper();
291 if ( !w
.empty() && w
!= _T('*') )
293 // the test below catches all of BOLD, EXTRABOLD, DEMIBOLD, ULTRABOLD
295 if ( ((w
[0u] == _T('B') && (!wxStrcmp(w
.c_str() + 1, wxT("OLD")) ||
296 !wxStrcmp(w
.c_str() + 1, wxT("LACK"))))) ||
297 wxStrstr(w
.c_str() + 1, _T("BOLD")) )
299 m_weight
= wxFONTWEIGHT_BOLD
;
301 else if ( w
== _T("LIGHT") || w
== _T("THIN") )
303 m_weight
= wxFONTWEIGHT_LIGHT
;
307 switch ( wxToupper(*m_nativeFontInfo
.
308 GetXFontComponent(wxXLFD_SLANT
).c_str()) )
310 case _T('I'): // italique
311 m_style
= wxFONTSTYLE_ITALIC
;
314 case _T('O'): // oblique
315 m_style
= wxFONTSTYLE_SLANT
;
319 m_style
= wxFONTSTYLE_NORMAL
;
323 if ( m_nativeFontInfo
.GetXFontComponent(wxXLFD_POINTSIZE
).ToLong(&ptSize
) )
325 // size in XLFD is in 10 point units
326 m_pointSize
= (int)(ptSize
/ 10);
330 m_pointSize
= wxDEFAULT_FONT_SIZE
;
333 // examine the spacing: if the font is monospaced, assume wxTELETYPE
334 // family for compatibility with the old code which used it instead of
336 if ( m_nativeFontInfo
.GetXFontComponent(wxXLFD_SPACING
).Upper() == _T('M') )
338 m_family
= wxFONTFAMILY_TELETYPE
;
340 else // not monospaceed
342 // don't even try guessing it, it doesn't work for too many fonts
344 m_family
= wxFONTFAMILY_UNKNOWN
;
347 // X fonts are never underlined...
348 m_underlined
= false;
350 // deal with font encoding
352 registry
= m_nativeFontInfo
.GetXFontComponent(wxXLFD_REGISTRY
).Upper(),
353 encoding
= m_nativeFontInfo
.GetXFontComponent(wxXLFD_ENCODING
).Upper();
355 if ( registry
== _T("ISO8859") )
358 if ( wxSscanf(encoding
, wxT("%d"), &cp
) == 1 )
360 m_encoding
= (wxFontEncoding
)(wxFONTENCODING_ISO8859_1
+ cp
- 1);
363 else if ( registry
== _T("MICROSOFT") )
366 if ( wxSscanf(encoding
, wxT("cp125%d"), &cp
) == 1 )
368 m_encoding
= (wxFontEncoding
)(wxFONTENCODING_CP1250
+ cp
);
371 else if ( registry
== _T("KOI8") )
373 m_encoding
= wxFONTENCODING_KOI8
;
375 else // unknown encoding
377 // may be give a warning here? or use wxFontMapper?
378 m_encoding
= wxFONTENCODING_SYSTEM
;
383 wxFontRefData::wxFontRefData( const wxFontRefData
& data
)
386 m_pointSize
= data
.m_pointSize
;
387 m_family
= data
.m_family
;
388 m_style
= data
.m_style
;
389 m_weight
= data
.m_weight
;
391 m_underlined
= data
.m_underlined
;
393 m_faceName
= data
.m_faceName
;
394 m_encoding
= data
.m_encoding
;
396 m_noAA
= data
.m_noAA
;
398 m_nativeFontInfo
= data
.m_nativeFontInfo
;
401 wxFontRefData::wxFontRefData(int size
, int family
, int style
,
402 int weight
, bool underlined
,
403 const wxString
& faceName
,
404 wxFontEncoding encoding
)
406 Init(size
, family
, style
, weight
, underlined
, faceName
, encoding
);
409 wxFontRefData::wxFontRefData(const wxString
& fontname
)
411 // VZ: FromString() should really work in both cases, doesn't it?
413 m_nativeFontInfo
.FromString( fontname
);
415 m_nativeFontInfo
.SetXFontName(fontname
);
421 void wxFontRefData::ClearX11Fonts()
425 wxList::compatibility_iterator node
= m_fonts
.GetFirst();
428 wxXFont
* f
= (wxXFont
*) node
->GetData();
430 node
= node
->GetNext();
436 wxFontRefData::~wxFontRefData()
441 // ----------------------------------------------------------------------------
442 // wxFontRefData SetXXX()
443 // ----------------------------------------------------------------------------
445 void wxFontRefData::SetPointSize(int pointSize
)
447 m_pointSize
= pointSize
;
451 PangoFontDescription
*desc
= m_nativeFontInfo
.description
;
453 pango_font_description_set_size( desc
, m_pointSize
* PANGO_SCALE
);
457 void wxFontRefData::SetFamily(int family
)
461 // TODO: what are we supposed to do with m_nativeFontInfo here?
464 void wxFontRefData::SetStyle(int style
)
470 PangoFontDescription
*desc
= m_nativeFontInfo
.description
;
474 case wxFONTSTYLE_ITALIC
:
475 pango_font_description_set_style( desc
, PANGO_STYLE_ITALIC
);
477 case wxFONTSTYLE_SLANT
:
478 pango_font_description_set_style( desc
, PANGO_STYLE_OBLIQUE
);
481 wxFAIL_MSG( _T("unknown font style") );
483 case wxFONTSTYLE_NORMAL
:
484 pango_font_description_set_style( desc
, PANGO_STYLE_NORMAL
);
490 void wxFontRefData::SetWeight(int weight
)
495 void wxFontRefData::SetUnderlined(bool underlined
)
497 m_underlined
= underlined
;
499 // the XLFD doesn't have "underlined" field anyhow
502 void wxFontRefData::SetFaceName(const wxString
& facename
)
504 m_faceName
= facename
;
507 void wxFontRefData::SetEncoding(wxFontEncoding encoding
)
509 m_encoding
= encoding
;
512 void wxFontRefData::SetNativeFontInfo(const wxNativeFontInfo
& info
)
514 // previously cached fonts shouldn't be used
517 m_nativeFontInfo
= info
;
519 // set all the other font parameters from the native font info
523 // ----------------------------------------------------------------------------
525 // ----------------------------------------------------------------------------
527 wxFont::wxFont(const wxNativeFontInfo
& info
)
530 Create( info
.GetPointSize(),
534 info
.GetUnderlined(),
536 info
.GetEncoding() );
538 (void) Create(info
.GetXFontName());
542 bool wxFont::Create(int pointSize
,
547 const wxString
& faceName
,
548 wxFontEncoding encoding
)
552 m_refData
= new wxFontRefData(pointSize
, family
, style
, weight
,
553 underlined
, faceName
, encoding
);
560 bool wxFont::Create(const wxString
& fontname
, wxFontEncoding enc
)
564 *this = wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT
);
568 m_refData
= new wxFontRefData();
570 M_FONTDATA
->m_nativeFontInfo
.SetXFontName(fontname
); // X font name
574 wxStringTokenizer
tn( fontname
, wxT("-") );
576 tn
.GetNextToken(); // skip initial empty token
577 tn
.GetNextToken(); // foundry
580 M_FONTDATA
->m_faceName
= tn
.GetNextToken(); // family
582 tmp
= tn
.GetNextToken().MakeUpper(); // weight
583 if (tmp
== wxT("BOLD")) M_FONTDATA
->m_weight
= wxBOLD
;
584 if (tmp
== wxT("BLACK")) M_FONTDATA
->m_weight
= wxBOLD
;
585 if (tmp
== wxT("EXTRABOLD")) M_FONTDATA
->m_weight
= wxBOLD
;
586 if (tmp
== wxT("DEMIBOLD")) M_FONTDATA
->m_weight
= wxBOLD
;
587 if (tmp
== wxT("ULTRABOLD")) M_FONTDATA
->m_weight
= wxBOLD
;
589 if (tmp
== wxT("LIGHT")) M_FONTDATA
->m_weight
= wxLIGHT
;
590 if (tmp
== wxT("THIN")) M_FONTDATA
->m_weight
= wxLIGHT
;
592 tmp
= tn
.GetNextToken().MakeUpper(); // slant
593 if (tmp
== wxT("I")) M_FONTDATA
->m_style
= wxITALIC
;
594 if (tmp
== wxT("O")) M_FONTDATA
->m_style
= wxITALIC
;
596 tn
.GetNextToken(); // set width
597 tn
.GetNextToken(); // add. style
598 tn
.GetNextToken(); // pixel size
600 tmp
= tn
.GetNextToken(); // pointsize
603 long num
= wxStrtol (tmp
.c_str(), (wxChar
**) NULL
, 10);
604 M_FONTDATA
->m_pointSize
= (int)(num
/ 10);
607 tn
.GetNextToken(); // x-res
608 tn
.GetNextToken(); // y-res
610 tmp
= tn
.GetNextToken().MakeUpper(); // spacing
613 M_FONTDATA
->m_family
= wxMODERN
;
614 else if (M_FONTDATA
->m_faceName
== wxT("TIMES"))
615 M_FONTDATA
->m_family
= wxROMAN
;
616 else if (M_FONTDATA
->m_faceName
== wxT("HELVETICA"))
617 M_FONTDATA
->m_family
= wxSWISS
;
618 else if (M_FONTDATA
->m_faceName
== wxT("LUCIDATYPEWRITER"))
619 M_FONTDATA
->m_family
= wxTELETYPE
;
620 else if (M_FONTDATA
->m_faceName
== wxT("LUCIDA"))
621 M_FONTDATA
->m_family
= wxDECORATIVE
;
622 else if (M_FONTDATA
->m_faceName
== wxT("UTOPIA"))
623 M_FONTDATA
->m_family
= wxSCRIPT
;
625 tn
.GetNextToken(); // avg width
627 // deal with font encoding
628 M_FONTDATA
->m_encoding
= enc
;
629 if ( M_FONTDATA
->m_encoding
== wxFONTENCODING_SYSTEM
)
631 wxString registry
= tn
.GetNextToken().MakeUpper(),
632 encoding
= tn
.GetNextToken().MakeUpper();
634 if ( registry
== _T("ISO8859") )
637 if ( wxSscanf(encoding
, wxT("%d"), &cp
) == 1 )
639 M_FONTDATA
->m_encoding
=
640 (wxFontEncoding
)(wxFONTENCODING_ISO8859_1
+ cp
- 1);
643 else if ( registry
== _T("MICROSOFT") )
646 if ( wxSscanf(encoding
, wxT("cp125%d"), &cp
) == 1 )
648 M_FONTDATA
->m_encoding
=
649 (wxFontEncoding
)(wxFONTENCODING_CP1250
+ cp
);
652 else if ( registry
== _T("KOI8") )
654 M_FONTDATA
->m_encoding
= wxFONTENCODING_KOI8
;
656 //else: unknown encoding - may be give a warning here?
662 #endif // !wxUSE_UNICODE
668 // ----------------------------------------------------------------------------
669 // change the font attributes
670 // ----------------------------------------------------------------------------
672 void wxFont::Unshare()
674 // Don't change shared data
677 m_refData
= new wxFontRefData();
681 wxFontRefData
* ref
= new wxFontRefData(*(wxFontRefData
*)m_refData
);
687 // ----------------------------------------------------------------------------
689 // ----------------------------------------------------------------------------
691 int wxFont::GetPointSize() const
693 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
695 return M_FONTDATA
->m_pointSize
;
698 wxString
wxFont::GetFaceName() const
700 wxCHECK_MSG( Ok(), wxEmptyString
, wxT("invalid font") );
702 return M_FONTDATA
->m_faceName
;
705 int wxFont::GetFamily() const
707 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
709 return M_FONTDATA
->m_family
;
712 int wxFont::GetStyle() const
714 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
716 return M_FONTDATA
->m_style
;
719 int wxFont::GetWeight() const
721 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
723 return M_FONTDATA
->m_weight
;
726 bool wxFont::GetUnderlined() const
728 wxCHECK_MSG( Ok(), false, wxT("invalid font") );
730 return M_FONTDATA
->m_underlined
;
733 wxFontEncoding
wxFont::GetEncoding() const
735 wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT
, wxT("invalid font") );
737 return M_FONTDATA
->m_encoding
;
740 bool wxFont::GetNoAntiAliasing() const
742 wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT
, wxT("invalid font") );
744 return M_FONTDATA
->m_noAA
;
747 const wxNativeFontInfo
*wxFont::GetNativeFontInfo() const
749 wxCHECK_MSG( Ok(), (wxNativeFontInfo
*)NULL
, wxT("invalid font") );
753 if ( M_FONTDATA
->m_nativeFontInfo
.GetXFontName().empty() )
757 return &(M_FONTDATA
->m_nativeFontInfo
);
760 bool wxFont::IsFixedWidth() const
762 wxCHECK_MSG( Ok(), false, wxT("invalid font") );
765 return wxFontBase::IsFixedWidth();
767 // Robert, is this right? HasNativeFont doesn't exist.
769 // if ( M_FONTDATA->HasNativeFont() )
771 // the monospace fonts are supposed to have "M" in the spacing field
772 wxString spacing
= M_FONTDATA
->
773 m_nativeFontInfo
.GetXFontComponent(wxXLFD_SPACING
);
775 return spacing
.Upper() == _T('M');
777 // Unreaceable code for now
778 // return wxFontBase::IsFixedWidth();
783 // ----------------------------------------------------------------------------
784 // change font attributes
785 // ----------------------------------------------------------------------------
787 void wxFont::SetPointSize(int pointSize
)
791 M_FONTDATA
->SetPointSize(pointSize
);
794 void wxFont::SetFamily(int family
)
798 M_FONTDATA
->SetFamily(family
);
801 void wxFont::SetStyle(int style
)
805 M_FONTDATA
->SetStyle(style
);
808 void wxFont::SetWeight(int weight
)
812 M_FONTDATA
->SetWeight(weight
);
815 void wxFont::SetFaceName(const wxString
& faceName
)
819 M_FONTDATA
->SetFaceName(faceName
);
822 void wxFont::SetUnderlined(bool underlined
)
826 M_FONTDATA
->SetUnderlined(underlined
);
829 void wxFont::SetEncoding(wxFontEncoding encoding
)
833 M_FONTDATA
->SetEncoding(encoding
);
836 void wxFont::DoSetNativeFontInfo( const wxNativeFontInfo
& info
)
840 M_FONTDATA
->SetNativeFontInfo( info
);
843 void wxFont::SetNoAntiAliasing( bool no
)
847 M_FONTDATA
->SetNoAntiAliasing( no
);
853 // ----------------------------------------------------------------------------
854 // X11 implementation
855 // ----------------------------------------------------------------------------
857 // Find an existing, or create a new, XFontStruct
858 // based on this wxFont and the given scale. Append the
859 // font to list in the private data for future reference.
860 wxXFont
* wxFont::GetInternalFont(double scale
, WXDisplay
* display
) const
863 return (wxXFont
*)NULL
;
865 long intScale
= long(scale
* 100.0 + 0.5); // key for wxXFont
866 int pointSize
= (M_FONTDATA
->m_pointSize
* 10 * intScale
) / 100;
868 // search existing fonts first
869 wxList::compatibility_iterator node
= M_FONTDATA
->m_fonts
.GetFirst();
872 wxXFont
* f
= (wxXFont
*) node
->GetData();
873 if ((!display
|| (f
->m_display
== display
)) && (f
->m_scale
== intScale
))
875 node
= node
->GetNext();
878 wxString xFontName
= M_FONTDATA
->m_nativeFontInfo
.GetXFontName();
879 if (xFontName
== "-*-*-*-*-*--*-*-*-*-*-*-*-*")
880 // wxFont constructor not called with native font info parameter => take M_FONTDATA values
883 // not found, create a new one
884 XFontStruct
*font
= (XFontStruct
*)
885 wxLoadQueryNearestFont(pointSize
,
886 M_FONTDATA
->m_family
,
888 M_FONTDATA
->m_weight
,
889 M_FONTDATA
->m_underlined
,
891 M_FONTDATA
->m_encoding
,
896 wxFAIL_MSG( wxT("Could not allocate even a default font -- something is wrong.") );
898 return (wxXFont
*) NULL
;
901 wxXFont
* f
= new wxXFont
;
902 f
->m_fontStruct
= (WXFontStructPtr
)font
;
903 f
->m_display
= ( display
? display
: wxGetDisplay() );
904 f
->m_scale
= intScale
;
905 M_FONTDATA
->m_fonts
.Append(f
);
910 WXFontStructPtr
wxFont::GetFontStruct(double scale
, WXDisplay
* display
) const
912 wxXFont
* f
= GetInternalFont(scale
, display
);
914 return (f
? f
->m_fontStruct
: (WXFontStructPtr
) 0);