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
35 #include "wx/string.h"
36 #include "wx/utils.h" // for wxGetDisplay()
37 #include "wx/settings.h"
38 #include "wx/gdicmn.h"
41 #include "wx/fontutil.h" // for wxNativeFontInfo
42 #include "wx/tokenzr.h"
44 #include "wx/x11/private.h"
46 IMPLEMENT_DYNAMIC_CLASS(wxFont
, wxGDIObject
)
48 // ----------------------------------------------------------------------------
50 // ----------------------------------------------------------------------------
52 // the default size (in points) for the fonts
53 static const int wxDEFAULT_FONT_SIZE
= 12;
58 // ----------------------------------------------------------------------------
60 // ----------------------------------------------------------------------------
62 // For every wxFont, there must be a font for each display and scale requested.
63 // So these objects are stored in wxFontRefData::m_fonts
64 class wxXFont
: public wxObject
70 WXFontStructPtr m_fontStruct
; // XFontStruct
71 WXDisplay
* m_display
; // XDisplay
72 int m_scale
; // Scale * 100
77 m_fontStruct
= (WXFontStructPtr
) 0;
78 m_display
= (WXDisplay
*) 0;
84 // Freeing the font used to produce a segv, but
85 // appears to be OK now (bug fix in X11?)
86 XFontStruct
* fontStruct
= (XFontStruct
*) m_fontStruct
;
87 XFreeFont((Display
*) m_display
, fontStruct
);
91 // ----------------------------------------------------------------------------
93 // ----------------------------------------------------------------------------
95 class wxFontRefData
: public wxObjectRefData
100 wxFontRefData(int size
= wxDEFAULT
,
101 int family
= wxDEFAULT
,
102 int style
= wxDEFAULT
,
103 int weight
= wxDEFAULT
,
104 bool underlined
= false,
105 const wxString
& faceName
= wxEmptyString
,
106 wxFontEncoding encoding
= wxFONTENCODING_DEFAULT
);
109 wxFontRefData(const wxFontRefData
& data
);
112 wxFontRefData(const wxString
& fontname
);
115 virtual ~wxFontRefData();
117 // setters: all of them also take care to modify m_nativeFontInfo if we
118 // have it so as to not lose the information not carried by our fields
119 void SetPointSize(int pointSize
);
120 void SetFamily(int family
);
121 void SetStyle(int style
);
122 void SetWeight(int weight
);
123 void SetUnderlined(bool underlined
);
124 bool SetFaceName(const wxString
& facename
);
125 void SetEncoding(wxFontEncoding encoding
);
127 void SetNoAntiAliasing( bool no
= true ) { m_noAA
= no
; }
128 bool GetNoAntiAliasing() const { return m_noAA
; }
130 // and this one also modifies all the other font data fields
131 void SetNativeFontInfo(const wxNativeFontInfo
& info
);
134 // common part of all ctors
140 const wxString
& faceName
,
141 wxFontEncoding encoding
);
143 // set all fields from (already initialized and valid) m_nativeFontInfo
144 void InitFromNative();
153 wxFontEncoding m_encoding
; // Unused in Unicode mode
154 bool m_noAA
; // No anti-aliasing
156 wxNativeFontInfo m_nativeFontInfo
;
158 void ClearX11Fonts();
162 // A list of wxXFonts
167 // ----------------------------------------------------------------------------
169 // ----------------------------------------------------------------------------
171 void wxFontRefData::Init(int pointSize
,
176 const wxString
& faceName
,
177 wxFontEncoding encoding
)
179 m_family
= family
== wxFONTFAMILY_DEFAULT
? wxFONTFAMILY_SWISS
: family
;
181 m_faceName
= faceName
;
183 // we accept both wxDEFAULT and wxNORMAL here - should we?
184 m_style
= style
== wxDEFAULT
? wxFONTSTYLE_NORMAL
: style
;
185 m_weight
= weight
== wxDEFAULT
? wxFONTWEIGHT_NORMAL
: weight
;
187 // and here, do we really want to forbid creation of the font of the size
188 // 90 (the value of wxDEFAULT)??
189 m_pointSize
= pointSize
== wxDEFAULT
|| pointSize
== -1
190 ? wxDEFAULT_FONT_SIZE
193 m_underlined
= underlined
;
194 m_encoding
= encoding
;
197 // Create native font info
198 m_nativeFontInfo
.description
= pango_font_description_new();
200 // And set its values
203 case wxFONTFAMILY_MODERN
:
204 case wxFONTFAMILY_TELETYPE
:
205 pango_font_description_set_family( m_nativeFontInfo
.description
, "monospace" );
207 case wxFONTFAMILY_ROMAN
:
208 pango_font_description_set_family( m_nativeFontInfo
.description
, "serif" );
211 pango_font_description_set_family( m_nativeFontInfo
.description
, "sans" );
215 SetPointSize( m_pointSize
);
216 SetWeight( m_weight
);
220 void wxFontRefData::InitFromNative()
226 PangoFontDescription
*desc
= m_nativeFontInfo
.description
;
229 m_faceName
= wxGTK_CONV_BACK( pango_font_description_get_family( desc
) );
231 m_pointSize
= pango_font_description_get_size( desc
) / PANGO_SCALE
;
233 switch (pango_font_description_get_style( desc
))
235 case PANGO_STYLE_NORMAL
:
236 m_style
= wxFONTSTYLE_NORMAL
;
238 case PANGO_STYLE_ITALIC
:
239 m_style
= wxFONTSTYLE_ITALIC
;
241 case PANGO_STYLE_OBLIQUE
:
242 m_style
= wxFONTSTYLE_SLANT
;
246 switch (pango_font_description_get_weight( desc
))
248 case PANGO_WEIGHT_ULTRALIGHT
:
249 case PANGO_WEIGHT_LIGHT
:
250 m_weight
= wxFONTWEIGHT_LIGHT
;
254 wxFAIL_MSG(_T("unknown Pango font weight"));
257 case PANGO_WEIGHT_NORMAL
:
258 m_weight
= wxFONTWEIGHT_NORMAL
;
261 case PANGO_WEIGHT_SEMIBOLD
:
262 case PANGO_WEIGHT_BOLD
:
263 case PANGO_WEIGHT_ULTRABOLD
:
264 case PANGO_WEIGHT_HEAVY
:
265 m_weight
= wxFONTWEIGHT_BOLD
;
269 if (m_faceName
== wxT("monospace"))
271 m_family
= wxFONTFAMILY_TELETYPE
;
273 else if (m_faceName
== wxT("sans"))
275 m_family
= wxFONTFAMILY_SWISS
;
279 m_family
= wxFONTFAMILY_UNKNOWN
;
282 // Pango description are never underlined (?)
283 m_underlined
= false;
285 // Cannot we choose that
286 m_encoding
= wxFONTENCODING_SYSTEM
;
288 // get the font parameters from the XLFD
289 // -------------------------------------
291 m_faceName
= m_nativeFontInfo
.GetXFontComponent(wxXLFD_FAMILY
);
293 m_weight
= wxFONTWEIGHT_NORMAL
;
295 wxString w
= m_nativeFontInfo
.GetXFontComponent(wxXLFD_WEIGHT
).Upper();
296 if ( !w
.empty() && w
!= _T('*') )
298 // the test below catches all of BOLD, EXTRABOLD, DEMIBOLD, ULTRABOLD
300 if ( ((w
[0u] == _T('B') && (!wxStrcmp(w
.c_str() + 1, wxT("OLD")) ||
301 !wxStrcmp(w
.c_str() + 1, wxT("LACK"))))) ||
302 wxStrstr(w
.c_str() + 1, _T("BOLD")) )
304 m_weight
= wxFONTWEIGHT_BOLD
;
306 else if ( w
== _T("LIGHT") || w
== _T("THIN") )
308 m_weight
= wxFONTWEIGHT_LIGHT
;
312 switch ( wxToupper(*m_nativeFontInfo
.
313 GetXFontComponent(wxXLFD_SLANT
).c_str()) )
315 case _T('I'): // italique
316 m_style
= wxFONTSTYLE_ITALIC
;
319 case _T('O'): // oblique
320 m_style
= wxFONTSTYLE_SLANT
;
324 m_style
= wxFONTSTYLE_NORMAL
;
328 if ( m_nativeFontInfo
.GetXFontComponent(wxXLFD_POINTSIZE
).ToLong(&ptSize
) )
330 // size in XLFD is in 10 point units
331 m_pointSize
= (int)(ptSize
/ 10);
335 m_pointSize
= wxDEFAULT_FONT_SIZE
;
338 // examine the spacing: if the font is monospaced, assume wxTELETYPE
339 // family for compatibility with the old code which used it instead of
341 if ( m_nativeFontInfo
.GetXFontComponent(wxXLFD_SPACING
).Upper() == _T('M') )
343 m_family
= wxFONTFAMILY_TELETYPE
;
345 else // not monospaceed
347 // don't even try guessing it, it doesn't work for too many fonts
349 m_family
= wxFONTFAMILY_UNKNOWN
;
352 // X fonts are never underlined...
353 m_underlined
= false;
355 // deal with font encoding
357 registry
= m_nativeFontInfo
.GetXFontComponent(wxXLFD_REGISTRY
).Upper(),
358 encoding
= m_nativeFontInfo
.GetXFontComponent(wxXLFD_ENCODING
).Upper();
360 if ( registry
== _T("ISO8859") )
363 if ( wxSscanf(encoding
, wxT("%d"), &cp
) == 1 )
365 m_encoding
= (wxFontEncoding
)(wxFONTENCODING_ISO8859_1
+ cp
- 1);
368 else if ( registry
== _T("MICROSOFT") )
371 if ( wxSscanf(encoding
, wxT("cp125%d"), &cp
) == 1 )
373 m_encoding
= (wxFontEncoding
)(wxFONTENCODING_CP1250
+ cp
);
376 else if ( registry
== _T("KOI8") )
378 m_encoding
= wxFONTENCODING_KOI8
;
380 else // unknown encoding
382 // may be give a warning here? or use wxFontMapper?
383 m_encoding
= wxFONTENCODING_SYSTEM
;
388 wxFontRefData::wxFontRefData( const wxFontRefData
& data
)
391 m_pointSize
= data
.m_pointSize
;
392 m_family
= data
.m_family
;
393 m_style
= data
.m_style
;
394 m_weight
= data
.m_weight
;
396 m_underlined
= data
.m_underlined
;
398 m_faceName
= data
.m_faceName
;
399 m_encoding
= data
.m_encoding
;
401 m_noAA
= data
.m_noAA
;
403 m_nativeFontInfo
= data
.m_nativeFontInfo
;
406 wxFontRefData::wxFontRefData(int size
, int family
, int style
,
407 int weight
, bool underlined
,
408 const wxString
& faceName
,
409 wxFontEncoding encoding
)
411 Init(size
, family
, style
, weight
, underlined
, faceName
, encoding
);
414 wxFontRefData::wxFontRefData(const wxString
& fontname
)
416 // VZ: FromString() should really work in both cases, doesn't it?
418 m_nativeFontInfo
.FromString( fontname
);
420 m_nativeFontInfo
.SetXFontName(fontname
);
426 void wxFontRefData::ClearX11Fonts()
430 wxList::compatibility_iterator node
= m_fonts
.GetFirst();
433 wxXFont
* f
= (wxXFont
*) node
->GetData();
435 node
= node
->GetNext();
441 wxFontRefData::~wxFontRefData()
446 // ----------------------------------------------------------------------------
447 // wxFontRefData SetXXX()
448 // ----------------------------------------------------------------------------
450 void wxFontRefData::SetPointSize(int pointSize
)
452 m_pointSize
= pointSize
;
456 PangoFontDescription
*desc
= m_nativeFontInfo
.description
;
458 pango_font_description_set_size( desc
, m_pointSize
* PANGO_SCALE
);
462 void wxFontRefData::SetFamily(int family
)
466 // TODO: what are we supposed to do with m_nativeFontInfo here?
469 void wxFontRefData::SetStyle(int style
)
475 PangoFontDescription
*desc
= m_nativeFontInfo
.description
;
479 case wxFONTSTYLE_ITALIC
:
480 pango_font_description_set_style( desc
, PANGO_STYLE_ITALIC
);
482 case wxFONTSTYLE_SLANT
:
483 pango_font_description_set_style( desc
, PANGO_STYLE_OBLIQUE
);
486 wxFAIL_MSG( _T("unknown font style") );
488 case wxFONTSTYLE_NORMAL
:
489 pango_font_description_set_style( desc
, PANGO_STYLE_NORMAL
);
495 void wxFontRefData::SetWeight(int weight
)
500 void wxFontRefData::SetUnderlined(bool underlined
)
502 m_underlined
= underlined
;
504 // the XLFD doesn't have "underlined" field anyhow
507 bool wxFontRefData::SetFaceName(const wxString
& facename
)
509 m_faceName
= facename
;
513 void wxFontRefData::SetEncoding(wxFontEncoding encoding
)
515 m_encoding
= encoding
;
518 void wxFontRefData::SetNativeFontInfo(const wxNativeFontInfo
& info
)
520 // previously cached fonts shouldn't be used
523 m_nativeFontInfo
= info
;
525 // set all the other font parameters from the native font info
529 // ----------------------------------------------------------------------------
531 // ----------------------------------------------------------------------------
533 wxFont::wxFont(const wxNativeFontInfo
& info
)
536 Create( info
.GetPointSize(),
540 info
.GetUnderlined(),
542 info
.GetEncoding() );
544 (void) Create(info
.GetXFontName());
548 bool wxFont::Create(int pointSize
,
553 const wxString
& faceName
,
554 wxFontEncoding encoding
)
558 m_refData
= new wxFontRefData(pointSize
, family
, style
, weight
,
559 underlined
, faceName
, encoding
);
566 bool wxFont::Create(const wxString
& fontname
, wxFontEncoding enc
)
570 *this = wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT
);
574 m_refData
= new wxFontRefData();
576 M_FONTDATA
->m_nativeFontInfo
.SetXFontName(fontname
); // X font name
580 wxStringTokenizer
tn( fontname
, wxT("-") );
582 tn
.GetNextToken(); // skip initial empty token
583 tn
.GetNextToken(); // foundry
586 M_FONTDATA
->m_faceName
= tn
.GetNextToken(); // family
588 tmp
= tn
.GetNextToken().MakeUpper(); // weight
589 if (tmp
== wxT("BOLD")) M_FONTDATA
->m_weight
= wxBOLD
;
590 if (tmp
== wxT("BLACK")) M_FONTDATA
->m_weight
= wxBOLD
;
591 if (tmp
== wxT("EXTRABOLD")) M_FONTDATA
->m_weight
= wxBOLD
;
592 if (tmp
== wxT("DEMIBOLD")) M_FONTDATA
->m_weight
= wxBOLD
;
593 if (tmp
== wxT("ULTRABOLD")) M_FONTDATA
->m_weight
= wxBOLD
;
595 if (tmp
== wxT("LIGHT")) M_FONTDATA
->m_weight
= wxLIGHT
;
596 if (tmp
== wxT("THIN")) M_FONTDATA
->m_weight
= wxLIGHT
;
598 tmp
= tn
.GetNextToken().MakeUpper(); // slant
599 if (tmp
== wxT("I")) M_FONTDATA
->m_style
= wxITALIC
;
600 if (tmp
== wxT("O")) M_FONTDATA
->m_style
= wxITALIC
;
602 tn
.GetNextToken(); // set width
603 tn
.GetNextToken(); // add. style
604 tn
.GetNextToken(); // pixel size
606 tmp
= tn
.GetNextToken(); // pointsize
609 long num
= wxStrtol (tmp
.c_str(), (wxChar
**) NULL
, 10);
610 M_FONTDATA
->m_pointSize
= (int)(num
/ 10);
613 tn
.GetNextToken(); // x-res
614 tn
.GetNextToken(); // y-res
616 tmp
= tn
.GetNextToken().MakeUpper(); // spacing
619 M_FONTDATA
->m_family
= wxMODERN
;
620 else if (M_FONTDATA
->m_faceName
== wxT("TIMES"))
621 M_FONTDATA
->m_family
= wxROMAN
;
622 else if (M_FONTDATA
->m_faceName
== wxT("HELVETICA"))
623 M_FONTDATA
->m_family
= wxSWISS
;
624 else if (M_FONTDATA
->m_faceName
== wxT("LUCIDATYPEWRITER"))
625 M_FONTDATA
->m_family
= wxTELETYPE
;
626 else if (M_FONTDATA
->m_faceName
== wxT("LUCIDA"))
627 M_FONTDATA
->m_family
= wxDECORATIVE
;
628 else if (M_FONTDATA
->m_faceName
== wxT("UTOPIA"))
629 M_FONTDATA
->m_family
= wxSCRIPT
;
631 tn
.GetNextToken(); // avg width
633 // deal with font encoding
634 M_FONTDATA
->m_encoding
= enc
;
635 if ( M_FONTDATA
->m_encoding
== wxFONTENCODING_SYSTEM
)
637 wxString registry
= tn
.GetNextToken().MakeUpper(),
638 encoding
= tn
.GetNextToken().MakeUpper();
640 if ( registry
== _T("ISO8859") )
643 if ( wxSscanf(encoding
, wxT("%d"), &cp
) == 1 )
645 M_FONTDATA
->m_encoding
=
646 (wxFontEncoding
)(wxFONTENCODING_ISO8859_1
+ cp
- 1);
649 else if ( registry
== _T("MICROSOFT") )
652 if ( wxSscanf(encoding
, wxT("cp125%d"), &cp
) == 1 )
654 M_FONTDATA
->m_encoding
=
655 (wxFontEncoding
)(wxFONTENCODING_CP1250
+ cp
);
658 else if ( registry
== _T("KOI8") )
660 M_FONTDATA
->m_encoding
= wxFONTENCODING_KOI8
;
662 //else: unknown encoding - may be give a warning here?
668 #endif // !wxUSE_UNICODE
674 // ----------------------------------------------------------------------------
675 // change the font attributes
676 // ----------------------------------------------------------------------------
678 void wxFont::Unshare()
680 // Don't change shared data
683 m_refData
= new wxFontRefData();
687 wxFontRefData
* ref
= new wxFontRefData(*(wxFontRefData
*)m_refData
);
693 // ----------------------------------------------------------------------------
695 // ----------------------------------------------------------------------------
697 int wxFont::GetPointSize() const
699 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
701 return M_FONTDATA
->m_pointSize
;
704 wxString
wxFont::GetFaceName() const
706 wxCHECK_MSG( Ok(), wxEmptyString
, wxT("invalid font") );
708 return M_FONTDATA
->m_faceName
;
711 int wxFont::GetFamily() const
713 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
715 return M_FONTDATA
->m_family
;
718 int wxFont::GetStyle() const
720 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
722 return M_FONTDATA
->m_style
;
725 int wxFont::GetWeight() const
727 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
729 return M_FONTDATA
->m_weight
;
732 bool wxFont::GetUnderlined() const
734 wxCHECK_MSG( Ok(), false, wxT("invalid font") );
736 return M_FONTDATA
->m_underlined
;
739 wxFontEncoding
wxFont::GetEncoding() const
741 wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT
, wxT("invalid font") );
743 return M_FONTDATA
->m_encoding
;
746 bool wxFont::GetNoAntiAliasing() const
748 wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT
, wxT("invalid font") );
750 return M_FONTDATA
->m_noAA
;
753 const wxNativeFontInfo
*wxFont::GetNativeFontInfo() const
755 wxCHECK_MSG( Ok(), (wxNativeFontInfo
*)NULL
, wxT("invalid font") );
759 if ( M_FONTDATA
->m_nativeFontInfo
.GetXFontName().empty() )
763 return &(M_FONTDATA
->m_nativeFontInfo
);
766 bool wxFont::IsFixedWidth() const
768 wxCHECK_MSG( Ok(), false, wxT("invalid font") );
771 return wxFontBase::IsFixedWidth();
773 // Robert, is this right? HasNativeFont doesn't exist.
775 // if ( M_FONTDATA->HasNativeFont() )
777 // the monospace fonts are supposed to have "M" in the spacing field
778 wxString spacing
= M_FONTDATA
->
779 m_nativeFontInfo
.GetXFontComponent(wxXLFD_SPACING
);
781 return spacing
.Upper() == _T('M');
783 // Unreaceable code for now
784 // return wxFontBase::IsFixedWidth();
789 // ----------------------------------------------------------------------------
790 // change font attributes
791 // ----------------------------------------------------------------------------
793 void wxFont::SetPointSize(int pointSize
)
797 M_FONTDATA
->SetPointSize(pointSize
);
800 void wxFont::SetFamily(int family
)
804 M_FONTDATA
->SetFamily(family
);
807 void wxFont::SetStyle(int style
)
811 M_FONTDATA
->SetStyle(style
);
814 void wxFont::SetWeight(int weight
)
818 M_FONTDATA
->SetWeight(weight
);
821 bool wxFont::SetFaceName(const wxString
& faceName
)
825 return M_FONTDATA
->SetFaceName(faceName
) &&
826 wxFontBase::SetFaceName(faceName
);
829 void wxFont::SetUnderlined(bool underlined
)
833 M_FONTDATA
->SetUnderlined(underlined
);
836 void wxFont::SetEncoding(wxFontEncoding encoding
)
840 M_FONTDATA
->SetEncoding(encoding
);
843 void wxFont::DoSetNativeFontInfo( const wxNativeFontInfo
& info
)
847 M_FONTDATA
->SetNativeFontInfo( info
);
850 void wxFont::SetNoAntiAliasing( bool no
)
854 M_FONTDATA
->SetNoAntiAliasing( no
);
860 // ----------------------------------------------------------------------------
861 // X11 implementation
862 // ----------------------------------------------------------------------------
864 // Find an existing, or create a new, XFontStruct
865 // based on this wxFont and the given scale. Append the
866 // font to list in the private data for future reference.
867 wxXFont
* wxFont::GetInternalFont(double scale
, WXDisplay
* display
) const
870 return (wxXFont
*)NULL
;
872 long intScale
= long(scale
* 100.0 + 0.5); // key for wxXFont
873 int pointSize
= (M_FONTDATA
->m_pointSize
* 10 * intScale
) / 100;
875 // search existing fonts first
876 wxList::compatibility_iterator node
= M_FONTDATA
->m_fonts
.GetFirst();
879 wxXFont
* f
= (wxXFont
*) node
->GetData();
880 if ((!display
|| (f
->m_display
== display
)) && (f
->m_scale
== intScale
))
882 node
= node
->GetNext();
885 wxString xFontName
= M_FONTDATA
->m_nativeFontInfo
.GetXFontName();
886 if (xFontName
== "-*-*-*-*-*--*-*-*-*-*-*-*-*")
887 // wxFont constructor not called with native font info parameter => take M_FONTDATA values
890 // not found, create a new one
891 XFontStruct
*font
= (XFontStruct
*)
892 wxLoadQueryNearestFont(pointSize
,
893 M_FONTDATA
->m_family
,
895 M_FONTDATA
->m_weight
,
896 M_FONTDATA
->m_underlined
,
898 M_FONTDATA
->m_encoding
,
903 wxFAIL_MSG( wxT("Could not allocate even a default font -- something is wrong.") );
905 return (wxXFont
*) NULL
;
908 wxXFont
* f
= new wxXFont
;
909 f
->m_fontStruct
= (WXFontStructPtr
)font
;
910 f
->m_display
= ( display
? display
: wxGetDisplay() );
911 f
->m_scale
= intScale
;
912 M_FONTDATA
->m_fonts
.Append(f
);
917 WXFontStructPtr
wxFont::GetFontStruct(double scale
, WXDisplay
* display
) const
919 wxXFont
* f
= GetInternalFont(scale
, display
);
921 return (f
? f
->m_fontStruct
: (WXFontStructPtr
) 0);