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 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "font.h"
25 #pragma message disable nosimpint
26 #include "wx/vms_x_fix.h"
30 #pragma message enable nosimpint
34 #include "wx/string.h"
36 #include "wx/gdicmn.h"
37 #include "wx/utils.h" // for wxGetDisplay()
38 #include "wx/fontutil.h" // for wxNativeFontInfo
39 #include "wx/tokenzr.h"
40 #include "wx/settings.h"
42 #include "wx/x11/private.h"
44 IMPLEMENT_DYNAMIC_CLASS(wxFont
, wxGDIObject
)
46 // ----------------------------------------------------------------------------
48 // ----------------------------------------------------------------------------
50 // the default size (in points) for the fonts
51 static const int wxDEFAULT_FONT_SIZE
= 12;
56 // ----------------------------------------------------------------------------
58 // ----------------------------------------------------------------------------
60 // For every wxFont, there must be a font for each display and scale requested.
61 // So these objects are stored in wxFontRefData::m_fonts
62 class wxXFont
: public wxObject
68 WXFontStructPtr m_fontStruct
; // XFontStruct
69 WXDisplay
* m_display
; // XDisplay
70 int m_scale
; // Scale * 100
75 m_fontStruct
= (WXFontStructPtr
) 0;
76 m_display
= (WXDisplay
*) 0;
82 // Freeing the font used to produce a segv, but
83 // appears to be OK now (bug fix in X11?)
84 XFontStruct
* fontStruct
= (XFontStruct
*) m_fontStruct
;
85 XFreeFont((Display
*) m_display
, fontStruct
);
89 // ----------------------------------------------------------------------------
91 // ----------------------------------------------------------------------------
93 class wxFontRefData
: public wxObjectRefData
98 wxFontRefData(int size
= wxDEFAULT
,
99 int family
= wxDEFAULT
,
100 int style
= wxDEFAULT
,
101 int weight
= wxDEFAULT
,
102 bool underlined
= FALSE
,
103 const wxString
& faceName
= wxEmptyString
,
104 wxFontEncoding encoding
= wxFONTENCODING_DEFAULT
);
107 wxFontRefData(const wxFontRefData
& data
);
110 wxFontRefData(const wxString
& fontname
);
113 virtual ~wxFontRefData();
115 // setters: all of them also take care to modify m_nativeFontInfo if we
116 // have it so as to not lose the information not carried by our fields
117 void SetPointSize(int pointSize
);
118 void SetFamily(int family
);
119 void SetStyle(int style
);
120 void SetWeight(int weight
);
121 void SetUnderlined(bool underlined
);
122 void SetFaceName(const wxString
& facename
);
123 void SetEncoding(wxFontEncoding encoding
);
125 void SetNoAntiAliasing( bool no
= TRUE
) { m_noAA
= no
; }
126 bool GetNoAntiAliasing() const { return m_noAA
; }
128 // and this one also modifies all the other font data fields
129 void SetNativeFontInfo(const wxNativeFontInfo
& info
);
132 // common part of all ctors
138 const wxString
& faceName
,
139 wxFontEncoding encoding
);
141 // set all fields from (already initialized and valid) m_nativeFontInfo
142 void InitFromNative();
151 wxFontEncoding m_encoding
; // Unused in Unicode mode
152 bool m_noAA
; // No anti-aliasing
154 wxNativeFontInfo m_nativeFontInfo
;
156 void ClearX11Fonts();
160 // A list of wxXFonts
165 // ----------------------------------------------------------------------------
167 // ----------------------------------------------------------------------------
169 void wxFontRefData::Init(int pointSize
,
174 const wxString
& faceName
,
175 wxFontEncoding encoding
)
177 m_family
= family
== wxFONTFAMILY_DEFAULT
? wxFONTFAMILY_SWISS
: family
;
179 m_faceName
= faceName
;
181 // we accept both wxDEFAULT and wxNORMAL here - should we?
182 m_style
= style
== wxDEFAULT
? wxFONTSTYLE_NORMAL
: style
;
183 m_weight
= weight
== wxDEFAULT
? wxFONTWEIGHT_NORMAL
: weight
;
185 // and here, do we really want to forbid creation of the font of the size
186 // 90 (the value of wxDEFAULT)??
187 m_pointSize
= pointSize
== wxDEFAULT
|| pointSize
== -1
188 ? wxDEFAULT_FONT_SIZE
191 m_underlined
= underlined
;
192 m_encoding
= encoding
;
195 // Create native font info
196 m_nativeFontInfo
.description
= pango_font_description_new();
198 // And set its values
201 case wxFONTFAMILY_MODERN
:
202 case wxFONTFAMILY_TELETYPE
:
203 pango_font_description_set_family( m_nativeFontInfo
.description
, "monospace" );
205 case wxFONTFAMILY_SWISS
:
206 pango_font_description_set_family( m_nativeFontInfo
.description
, "serif" );
209 pango_font_description_set_family( m_nativeFontInfo
.description
, "sans" );
213 SetPointSize( m_pointSize
);
214 SetWeight( m_weight
);
218 void wxFontRefData::InitFromNative()
224 PangoFontDescription
*desc
= m_nativeFontInfo
.description
;
227 m_faceName
= wxGTK_CONV_BACK( pango_font_description_get_family( desc
) );
229 m_pointSize
= pango_font_description_get_size( desc
) / PANGO_SCALE
;
231 switch (pango_font_description_get_style( desc
))
233 case PANGO_STYLE_NORMAL
:
234 m_style
= wxFONTSTYLE_NORMAL
;
236 case PANGO_STYLE_ITALIC
:
237 m_style
= wxFONTSTYLE_ITALIC
;
239 case PANGO_STYLE_OBLIQUE
:
240 m_style
= wxFONTSTYLE_SLANT
;
244 switch (pango_font_description_get_weight( desc
))
246 case PANGO_WEIGHT_ULTRALIGHT
:
247 m_weight
= wxFONTWEIGHT_LIGHT
;
249 case PANGO_WEIGHT_LIGHT
:
250 m_weight
= wxFONTWEIGHT_LIGHT
;
252 case PANGO_WEIGHT_NORMAL
:
253 m_weight
= wxFONTWEIGHT_NORMAL
;
255 case PANGO_WEIGHT_BOLD
:
256 m_weight
= wxFONTWEIGHT_BOLD
;
258 case PANGO_WEIGHT_ULTRABOLD
:
259 m_weight
= wxFONTWEIGHT_BOLD
;
261 case PANGO_WEIGHT_HEAVY
:
262 m_weight
= wxFONTWEIGHT_BOLD
;
266 if (m_faceName
== wxT("monospace"))
268 m_family
= wxFONTFAMILY_TELETYPE
;
270 else if (m_faceName
== wxT("sans"))
272 m_family
= wxFONTFAMILY_SWISS
;
276 m_family
= wxFONTFAMILY_UNKNOWN
;
279 // Pango description are never underlined (?)
280 m_underlined
= FALSE
;
282 // Cannot we choose that
283 m_encoding
= wxFONTENCODING_SYSTEM
;
285 // get the font parameters from the XLFD
286 // -------------------------------------
288 m_faceName
= m_nativeFontInfo
.GetXFontComponent(wxXLFD_FAMILY
);
290 m_weight
= wxFONTWEIGHT_NORMAL
;
292 wxString w
= m_nativeFontInfo
.GetXFontComponent(wxXLFD_WEIGHT
).Upper();
293 if ( !w
.empty() && w
!= _T('*') )
295 // the test below catches all of BOLD, EXTRABOLD, DEMIBOLD, ULTRABOLD
297 if ( ((w
[0u] == _T('B') && (!wxStrcmp(w
.c_str() + 1, wxT("OLD")) ||
298 !wxStrcmp(w
.c_str() + 1, wxT("LACK"))))) ||
299 wxStrstr(w
.c_str() + 1, _T("BOLD")) )
301 m_weight
= wxFONTWEIGHT_BOLD
;
303 else if ( w
== _T("LIGHT") || w
== _T("THIN") )
305 m_weight
= wxFONTWEIGHT_LIGHT
;
309 switch ( wxToupper(*m_nativeFontInfo
.
310 GetXFontComponent(wxXLFD_SLANT
).c_str()) )
312 case _T('I'): // italique
313 m_style
= wxFONTSTYLE_ITALIC
;
316 case _T('O'): // oblique
317 m_style
= wxFONTSTYLE_SLANT
;
321 m_style
= wxFONTSTYLE_NORMAL
;
325 if ( m_nativeFontInfo
.GetXFontComponent(wxXLFD_POINTSIZE
).ToLong(&ptSize
) )
327 // size in XLFD is in 10 point units
328 m_pointSize
= (int)(ptSize
/ 10);
332 m_pointSize
= wxDEFAULT_FONT_SIZE
;
335 // examine the spacing: if the font is monospaced, assume wxTELETYPE
336 // family for compatibility with the old code which used it instead of
338 if ( m_nativeFontInfo
.GetXFontComponent(wxXLFD_SPACING
).Upper() == _T('M') )
340 m_family
= wxFONTFAMILY_TELETYPE
;
342 else // not monospaceed
344 // don't even try guessing it, it doesn't work for too many fonts
346 m_family
= wxFONTFAMILY_UNKNOWN
;
349 // X fonts are never underlined...
350 m_underlined
= FALSE
;
352 // deal with font encoding
354 registry
= m_nativeFontInfo
.GetXFontComponent(wxXLFD_REGISTRY
).Upper(),
355 encoding
= m_nativeFontInfo
.GetXFontComponent(wxXLFD_ENCODING
).Upper();
357 if ( registry
== _T("ISO8859") )
360 if ( wxSscanf(encoding
, wxT("%d"), &cp
) == 1 )
362 m_encoding
= (wxFontEncoding
)(wxFONTENCODING_ISO8859_1
+ cp
- 1);
365 else if ( registry
== _T("MICROSOFT") )
368 if ( wxSscanf(encoding
, wxT("cp125%d"), &cp
) == 1 )
370 m_encoding
= (wxFontEncoding
)(wxFONTENCODING_CP1250
+ cp
);
373 else if ( registry
== _T("KOI8") )
375 m_encoding
= wxFONTENCODING_KOI8
;
377 else // unknown encoding
379 // may be give a warning here? or use wxFontMapper?
380 m_encoding
= wxFONTENCODING_SYSTEM
;
385 wxFontRefData::wxFontRefData( const wxFontRefData
& data
)
388 m_pointSize
= data
.m_pointSize
;
389 m_family
= data
.m_family
;
390 m_style
= data
.m_style
;
391 m_weight
= data
.m_weight
;
393 m_underlined
= data
.m_underlined
;
395 m_faceName
= data
.m_faceName
;
396 m_encoding
= data
.m_encoding
;
398 m_noAA
= data
.m_noAA
;
400 m_nativeFontInfo
= data
.m_nativeFontInfo
;
403 wxFontRefData::wxFontRefData(int size
, int family
, int style
,
404 int weight
, bool underlined
,
405 const wxString
& faceName
,
406 wxFontEncoding encoding
)
408 Init(size
, family
, style
, weight
, underlined
, faceName
, encoding
);
411 wxFontRefData::wxFontRefData(const wxString
& fontname
)
413 // VZ: FromString() should really work in both cases, doesn't it?
415 m_nativeFontInfo
.FromString( fontname
);
417 m_nativeFontInfo
.SetXFontName(fontname
);
423 void wxFontRefData::ClearX11Fonts()
427 wxList::compatibility_iterator node
= m_fonts
.GetFirst();
430 wxXFont
* f
= (wxXFont
*) node
->GetData();
432 node
= node
->GetNext();
438 wxFontRefData::~wxFontRefData()
443 // ----------------------------------------------------------------------------
444 // wxFontRefData SetXXX()
445 // ----------------------------------------------------------------------------
447 void wxFontRefData::SetPointSize(int pointSize
)
449 m_pointSize
= pointSize
;
453 PangoFontDescription
*desc
= m_nativeFontInfo
.description
;
455 pango_font_description_set_size( desc
, m_pointSize
* PANGO_SCALE
);
459 void wxFontRefData::SetFamily(int family
)
463 // TODO: what are we supposed to do with m_nativeFontInfo here?
466 void wxFontRefData::SetStyle(int style
)
472 PangoFontDescription
*desc
= m_nativeFontInfo
.description
;
476 case wxFONTSTYLE_ITALIC
:
477 pango_font_description_set_style( desc
, PANGO_STYLE_ITALIC
);
479 case wxFONTSTYLE_SLANT
:
480 pango_font_description_set_style( desc
, PANGO_STYLE_OBLIQUE
);
483 wxFAIL_MSG( _T("unknown font style") );
485 case wxFONTSTYLE_NORMAL
:
486 pango_font_description_set_style( desc
, PANGO_STYLE_NORMAL
);
492 void wxFontRefData::SetWeight(int weight
)
497 void wxFontRefData::SetUnderlined(bool underlined
)
499 m_underlined
= underlined
;
501 // the XLFD doesn't have "underlined" field anyhow
504 void wxFontRefData::SetFaceName(const wxString
& facename
)
506 m_faceName
= facename
;
509 void wxFontRefData::SetEncoding(wxFontEncoding encoding
)
511 m_encoding
= encoding
;
514 void wxFontRefData::SetNativeFontInfo(const wxNativeFontInfo
& info
)
516 // previously cached fonts shouldn't be used
519 m_nativeFontInfo
= info
;
521 // set all the other font parameters from the native font info
525 // ----------------------------------------------------------------------------
527 // ----------------------------------------------------------------------------
533 wxFont::wxFont(const wxNativeFontInfo
& info
)
538 Create( info
.GetPointSize(),
542 info
.GetUnderlined(),
544 info
.GetEncoding() );
546 (void) Create(info
.GetXFontName());
550 bool wxFont::Create(int pointSize
,
555 const wxString
& faceName
,
556 wxFontEncoding encoding
)
560 m_refData
= new wxFontRefData(pointSize
, family
, style
, weight
,
561 underlined
, faceName
, encoding
);
568 bool wxFont::Create(const wxString
& fontname
, wxFontEncoding enc
)
572 *this = wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT
);
576 m_refData
= new wxFontRefData();
578 M_FONTDATA
->m_nativeFontInfo
.SetXFontName(fontname
); // X font name
582 wxStringTokenizer
tn( fontname
, wxT("-") );
584 tn
.GetNextToken(); // skip initial empty token
585 tn
.GetNextToken(); // foundry
588 M_FONTDATA
->m_faceName
= tn
.GetNextToken(); // family
590 tmp
= tn
.GetNextToken().MakeUpper(); // weight
591 if (tmp
== wxT("BOLD")) M_FONTDATA
->m_weight
= wxBOLD
;
592 if (tmp
== wxT("BLACK")) M_FONTDATA
->m_weight
= wxBOLD
;
593 if (tmp
== wxT("EXTRABOLD")) M_FONTDATA
->m_weight
= wxBOLD
;
594 if (tmp
== wxT("DEMIBOLD")) M_FONTDATA
->m_weight
= wxBOLD
;
595 if (tmp
== wxT("ULTRABOLD")) M_FONTDATA
->m_weight
= wxBOLD
;
597 if (tmp
== wxT("LIGHT")) M_FONTDATA
->m_weight
= wxLIGHT
;
598 if (tmp
== wxT("THIN")) M_FONTDATA
->m_weight
= wxLIGHT
;
600 tmp
= tn
.GetNextToken().MakeUpper(); // slant
601 if (tmp
== wxT("I")) M_FONTDATA
->m_style
= wxITALIC
;
602 if (tmp
== wxT("O")) M_FONTDATA
->m_style
= wxITALIC
;
604 tn
.GetNextToken(); // set width
605 tn
.GetNextToken(); // add. style
606 tn
.GetNextToken(); // pixel size
608 tmp
= tn
.GetNextToken(); // pointsize
611 long num
= wxStrtol (tmp
.c_str(), (wxChar
**) NULL
, 10);
612 M_FONTDATA
->m_pointSize
= (int)(num
/ 10);
615 tn
.GetNextToken(); // x-res
616 tn
.GetNextToken(); // y-res
618 tmp
= tn
.GetNextToken().MakeUpper(); // spacing
621 M_FONTDATA
->m_family
= wxMODERN
;
622 else if (M_FONTDATA
->m_faceName
== wxT("TIMES"))
623 M_FONTDATA
->m_family
= wxROMAN
;
624 else if (M_FONTDATA
->m_faceName
== wxT("HELVETICA"))
625 M_FONTDATA
->m_family
= wxSWISS
;
626 else if (M_FONTDATA
->m_faceName
== wxT("LUCIDATYPEWRITER"))
627 M_FONTDATA
->m_family
= wxTELETYPE
;
628 else if (M_FONTDATA
->m_faceName
== wxT("LUCIDA"))
629 M_FONTDATA
->m_family
= wxDECORATIVE
;
630 else if (M_FONTDATA
->m_faceName
== wxT("UTOPIA"))
631 M_FONTDATA
->m_family
= wxSCRIPT
;
633 tn
.GetNextToken(); // avg width
635 // deal with font encoding
636 M_FONTDATA
->m_encoding
= enc
;
637 if ( M_FONTDATA
->m_encoding
== wxFONTENCODING_SYSTEM
)
639 wxString registry
= tn
.GetNextToken().MakeUpper(),
640 encoding
= tn
.GetNextToken().MakeUpper();
642 if ( registry
== _T("ISO8859") )
645 if ( wxSscanf(encoding
, wxT("%d"), &cp
) == 1 )
647 M_FONTDATA
->m_encoding
=
648 (wxFontEncoding
)(wxFONTENCODING_ISO8859_1
+ cp
- 1);
651 else if ( registry
== _T("MICROSOFT") )
654 if ( wxSscanf(encoding
, wxT("cp125%d"), &cp
) == 1 )
656 M_FONTDATA
->m_encoding
=
657 (wxFontEncoding
)(wxFONTENCODING_CP1250
+ cp
);
660 else if ( registry
== _T("KOI8") )
662 M_FONTDATA
->m_encoding
= wxFONTENCODING_KOI8
;
664 //else: unknown encoding - may be give a warning here?
670 #endif // !wxUSE_UNICODE
676 // ----------------------------------------------------------------------------
677 // change the font attributes
678 // ----------------------------------------------------------------------------
680 void wxFont::Unshare()
682 // Don't change shared data
685 m_refData
= new wxFontRefData();
689 wxFontRefData
* ref
= new wxFontRefData(*(wxFontRefData
*)m_refData
);
695 // ----------------------------------------------------------------------------
697 // ----------------------------------------------------------------------------
699 int wxFont::GetPointSize() const
701 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
703 return M_FONTDATA
->m_pointSize
;
706 wxString
wxFont::GetFaceName() const
708 wxCHECK_MSG( Ok(), wxT(""), wxT("invalid font") );
710 return M_FONTDATA
->m_faceName
;
713 int wxFont::GetFamily() const
715 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
717 return M_FONTDATA
->m_family
;
720 int wxFont::GetStyle() const
722 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
724 return M_FONTDATA
->m_style
;
727 int wxFont::GetWeight() const
729 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
731 return M_FONTDATA
->m_weight
;
734 bool wxFont::GetUnderlined() const
736 wxCHECK_MSG( Ok(), FALSE
, wxT("invalid font") );
738 return M_FONTDATA
->m_underlined
;
741 wxFontEncoding
wxFont::GetEncoding() const
743 wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT
, wxT("invalid font") );
745 return M_FONTDATA
->m_encoding
;
748 bool wxFont::GetNoAntiAliasing() const
750 wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT
, wxT("invalid font") );
752 return M_FONTDATA
->m_noAA
;
755 const wxNativeFontInfo
*wxFont::GetNativeFontInfo() const
757 wxCHECK_MSG( Ok(), (wxNativeFontInfo
*)NULL
, wxT("invalid font") );
761 if ( M_FONTDATA
->m_nativeFontInfo
.GetXFontName().empty() )
765 return &(M_FONTDATA
->m_nativeFontInfo
);
768 bool wxFont::IsFixedWidth() const
770 wxCHECK_MSG( Ok(), FALSE
, wxT("invalid font") );
773 return wxFontBase::IsFixedWidth();
775 // Robert, is this right? HasNativeFont doesn't exist.
777 // if ( M_FONTDATA->HasNativeFont() )
779 // the monospace fonts are supposed to have "M" in the spacing field
780 wxString spacing
= M_FONTDATA
->
781 m_nativeFontInfo
.GetXFontComponent(wxXLFD_SPACING
);
783 return spacing
.Upper() == _T('M');
785 // Unreaceable code for now
786 // return wxFontBase::IsFixedWidth();
791 // ----------------------------------------------------------------------------
792 // change font attributes
793 // ----------------------------------------------------------------------------
795 void wxFont::SetPointSize(int pointSize
)
799 M_FONTDATA
->SetPointSize(pointSize
);
802 void wxFont::SetFamily(int family
)
806 M_FONTDATA
->SetFamily(family
);
809 void wxFont::SetStyle(int style
)
813 M_FONTDATA
->SetStyle(style
);
816 void wxFont::SetWeight(int weight
)
820 M_FONTDATA
->SetWeight(weight
);
823 void wxFont::SetFaceName(const wxString
& faceName
)
827 M_FONTDATA
->SetFaceName(faceName
);
830 void wxFont::SetUnderlined(bool underlined
)
834 M_FONTDATA
->SetUnderlined(underlined
);
837 void wxFont::SetEncoding(wxFontEncoding encoding
)
841 M_FONTDATA
->SetEncoding(encoding
);
844 void wxFont::DoSetNativeFontInfo( const wxNativeFontInfo
& info
)
848 M_FONTDATA
->SetNativeFontInfo( info
);
851 void wxFont::SetNoAntiAliasing( bool no
)
855 M_FONTDATA
->SetNoAntiAliasing( no
);
861 // ----------------------------------------------------------------------------
862 // X11 implementation
863 // ----------------------------------------------------------------------------
865 // Find an existing, or create a new, XFontStruct
866 // based on this wxFont and the given scale. Append the
867 // font to list in the private data for future reference.
868 wxXFont
* wxFont::GetInternalFont(double scale
, WXDisplay
* display
) const
871 return (wxXFont
*)NULL
;
873 long intScale
= long(scale
* 100.0 + 0.5); // key for wxXFont
874 int pointSize
= (M_FONTDATA
->m_pointSize
* 10 * intScale
) / 100;
876 // search existing fonts first
877 wxList::compatibility_iterator node
= M_FONTDATA
->m_fonts
.GetFirst();
880 wxXFont
* f
= (wxXFont
*) node
->GetData();
881 if ((!display
|| (f
->m_display
== display
)) && (f
->m_scale
== intScale
))
883 node
= node
->GetNext();
886 // not found, create a new one
887 XFontStruct
*font
= (XFontStruct
*)
888 wxLoadQueryNearestFont(pointSize
,
889 M_FONTDATA
->m_family
,
891 M_FONTDATA
->m_weight
,
892 M_FONTDATA
->m_underlined
,
894 M_FONTDATA
->m_encoding
);
898 wxFAIL_MSG( wxT("Could not allocate even a default font -- something is wrong.") );
900 return (wxXFont
*) NULL
;
903 wxXFont
* f
= new wxXFont
;
904 f
->m_fontStruct
= (WXFontStructPtr
)font
;
905 f
->m_display
= ( display
? display
: wxGetDisplay() );
906 f
->m_scale
= intScale
;
907 M_FONTDATA
->m_fonts
.Append(f
);
912 WXFontStructPtr
wxFont::GetFontStruct(double scale
, WXDisplay
* display
) const
914 wxXFont
* f
= GetInternalFont(scale
, display
);
916 return (f
? f
->m_fontStruct
: (WXFontStructPtr
) 0);