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 // ----------------------------------------------------------------------------
21 #pragma message disable nosimpint
22 #include "wx/vms_x_fix.h"
26 #pragma message enable nosimpint
30 #include "wx/string.h"
32 #include "wx/gdicmn.h"
33 #include "wx/utils.h" // for wxGetDisplay()
34 #include "wx/fontutil.h" // for wxNativeFontInfo
35 #include "wx/tokenzr.h"
36 #include "wx/settings.h"
38 #include "wx/x11/private.h"
40 IMPLEMENT_DYNAMIC_CLASS(wxFont
, wxGDIObject
)
42 // ----------------------------------------------------------------------------
44 // ----------------------------------------------------------------------------
46 // the default size (in points) for the fonts
47 static const int wxDEFAULT_FONT_SIZE
= 12;
52 // ----------------------------------------------------------------------------
54 // ----------------------------------------------------------------------------
56 // For every wxFont, there must be a font for each display and scale requested.
57 // So these objects are stored in wxFontRefData::m_fonts
58 class wxXFont
: public wxObject
64 WXFontStructPtr m_fontStruct
; // XFontStruct
65 WXDisplay
* m_display
; // XDisplay
66 int m_scale
; // Scale * 100
71 m_fontStruct
= (WXFontStructPtr
) 0;
72 m_display
= (WXDisplay
*) 0;
78 // Freeing the font used to produce a segv, but
79 // appears to be OK now (bug fix in X11?)
80 XFontStruct
* fontStruct
= (XFontStruct
*) m_fontStruct
;
81 XFreeFont((Display
*) m_display
, fontStruct
);
85 // ----------------------------------------------------------------------------
87 // ----------------------------------------------------------------------------
89 class wxFontRefData
: public wxObjectRefData
94 wxFontRefData(int size
= wxDEFAULT
,
95 int family
= wxDEFAULT
,
96 int style
= wxDEFAULT
,
97 int weight
= wxDEFAULT
,
98 bool underlined
= FALSE
,
99 const wxString
& faceName
= wxEmptyString
,
100 wxFontEncoding encoding
= wxFONTENCODING_DEFAULT
);
103 wxFontRefData(const wxFontRefData
& data
);
106 wxFontRefData(const wxString
& fontname
);
109 virtual ~wxFontRefData();
111 // setters: all of them also take care to modify m_nativeFontInfo if we
112 // have it so as to not lose the information not carried by our fields
113 void SetPointSize(int pointSize
);
114 void SetFamily(int family
);
115 void SetStyle(int style
);
116 void SetWeight(int weight
);
117 void SetUnderlined(bool underlined
);
118 void SetFaceName(const wxString
& facename
);
119 void SetEncoding(wxFontEncoding encoding
);
121 void SetNoAntiAliasing( bool no
= TRUE
) { m_noAA
= no
; }
122 bool GetNoAntiAliasing() const { return m_noAA
; }
124 // and this one also modifies all the other font data fields
125 void SetNativeFontInfo(const wxNativeFontInfo
& info
);
128 // common part of all ctors
134 const wxString
& faceName
,
135 wxFontEncoding encoding
);
137 // set all fields from (already initialized and valid) m_nativeFontInfo
138 void InitFromNative();
147 wxFontEncoding m_encoding
; // Unused in Unicode mode
148 bool m_noAA
; // No anti-aliasing
150 wxNativeFontInfo m_nativeFontInfo
;
152 void ClearX11Fonts();
156 // A list of wxXFonts
161 // ----------------------------------------------------------------------------
163 // ----------------------------------------------------------------------------
165 void wxFontRefData::Init(int pointSize
,
170 const wxString
& faceName
,
171 wxFontEncoding encoding
)
173 m_family
= family
== wxFONTFAMILY_DEFAULT
? wxFONTFAMILY_SWISS
: family
;
175 m_faceName
= faceName
;
177 // we accept both wxDEFAULT and wxNORMAL here - should we?
178 m_style
= style
== wxDEFAULT
? wxFONTSTYLE_NORMAL
: style
;
179 m_weight
= weight
== wxDEFAULT
? wxFONTWEIGHT_NORMAL
: weight
;
181 // and here, do we really want to forbid creation of the font of the size
182 // 90 (the value of wxDEFAULT)??
183 m_pointSize
= pointSize
== wxDEFAULT
|| pointSize
== -1
184 ? wxDEFAULT_FONT_SIZE
187 m_underlined
= underlined
;
188 m_encoding
= encoding
;
191 // Create native font info
192 m_nativeFontInfo
.description
= pango_font_description_new();
194 // And set its values
197 case wxFONTFAMILY_MODERN
:
198 case wxFONTFAMILY_TELETYPE
:
199 pango_font_description_set_family( m_nativeFontInfo
.description
, "monospace" );
201 case wxFONTFAMILY_ROMAN
:
202 pango_font_description_set_family( m_nativeFontInfo
.description
, "serif" );
205 pango_font_description_set_family( m_nativeFontInfo
.description
, "sans" );
209 SetPointSize( m_pointSize
);
210 SetWeight( m_weight
);
214 void wxFontRefData::InitFromNative()
220 PangoFontDescription
*desc
= m_nativeFontInfo
.description
;
223 m_faceName
= wxGTK_CONV_BACK( pango_font_description_get_family( desc
) );
225 m_pointSize
= pango_font_description_get_size( desc
) / PANGO_SCALE
;
227 switch (pango_font_description_get_style( desc
))
229 case PANGO_STYLE_NORMAL
:
230 m_style
= wxFONTSTYLE_NORMAL
;
232 case PANGO_STYLE_ITALIC
:
233 m_style
= wxFONTSTYLE_ITALIC
;
235 case PANGO_STYLE_OBLIQUE
:
236 m_style
= wxFONTSTYLE_SLANT
;
240 switch (pango_font_description_get_weight( desc
))
242 case PANGO_WEIGHT_ULTRALIGHT
:
243 m_weight
= wxFONTWEIGHT_LIGHT
;
245 case PANGO_WEIGHT_LIGHT
:
246 m_weight
= wxFONTWEIGHT_LIGHT
;
248 case PANGO_WEIGHT_NORMAL
:
249 m_weight
= wxFONTWEIGHT_NORMAL
;
251 case PANGO_WEIGHT_BOLD
:
252 m_weight
= wxFONTWEIGHT_BOLD
;
254 case PANGO_WEIGHT_ULTRABOLD
:
255 m_weight
= wxFONTWEIGHT_BOLD
;
257 case PANGO_WEIGHT_HEAVY
:
258 m_weight
= wxFONTWEIGHT_BOLD
;
262 if (m_faceName
== wxT("monospace"))
264 m_family
= wxFONTFAMILY_TELETYPE
;
266 else if (m_faceName
== wxT("sans"))
268 m_family
= wxFONTFAMILY_SWISS
;
272 m_family
= wxFONTFAMILY_UNKNOWN
;
275 // Pango description are never underlined (?)
276 m_underlined
= FALSE
;
278 // Cannot we choose that
279 m_encoding
= wxFONTENCODING_SYSTEM
;
281 // get the font parameters from the XLFD
282 // -------------------------------------
284 m_faceName
= m_nativeFontInfo
.GetXFontComponent(wxXLFD_FAMILY
);
286 m_weight
= wxFONTWEIGHT_NORMAL
;
288 wxString w
= m_nativeFontInfo
.GetXFontComponent(wxXLFD_WEIGHT
).Upper();
289 if ( !w
.empty() && w
!= _T('*') )
291 // the test below catches all of BOLD, EXTRABOLD, DEMIBOLD, ULTRABOLD
293 if ( ((w
[0u] == _T('B') && (!wxStrcmp(w
.c_str() + 1, wxT("OLD")) ||
294 !wxStrcmp(w
.c_str() + 1, wxT("LACK"))))) ||
295 wxStrstr(w
.c_str() + 1, _T("BOLD")) )
297 m_weight
= wxFONTWEIGHT_BOLD
;
299 else if ( w
== _T("LIGHT") || w
== _T("THIN") )
301 m_weight
= wxFONTWEIGHT_LIGHT
;
305 switch ( wxToupper(*m_nativeFontInfo
.
306 GetXFontComponent(wxXLFD_SLANT
).c_str()) )
308 case _T('I'): // italique
309 m_style
= wxFONTSTYLE_ITALIC
;
312 case _T('O'): // oblique
313 m_style
= wxFONTSTYLE_SLANT
;
317 m_style
= wxFONTSTYLE_NORMAL
;
321 if ( m_nativeFontInfo
.GetXFontComponent(wxXLFD_POINTSIZE
).ToLong(&ptSize
) )
323 // size in XLFD is in 10 point units
324 m_pointSize
= (int)(ptSize
/ 10);
328 m_pointSize
= wxDEFAULT_FONT_SIZE
;
331 // examine the spacing: if the font is monospaced, assume wxTELETYPE
332 // family for compatibility with the old code which used it instead of
334 if ( m_nativeFontInfo
.GetXFontComponent(wxXLFD_SPACING
).Upper() == _T('M') )
336 m_family
= wxFONTFAMILY_TELETYPE
;
338 else // not monospaceed
340 // don't even try guessing it, it doesn't work for too many fonts
342 m_family
= wxFONTFAMILY_UNKNOWN
;
345 // X fonts are never underlined...
346 m_underlined
= FALSE
;
348 // deal with font encoding
350 registry
= m_nativeFontInfo
.GetXFontComponent(wxXLFD_REGISTRY
).Upper(),
351 encoding
= m_nativeFontInfo
.GetXFontComponent(wxXLFD_ENCODING
).Upper();
353 if ( registry
== _T("ISO8859") )
356 if ( wxSscanf(encoding
, wxT("%d"), &cp
) == 1 )
358 m_encoding
= (wxFontEncoding
)(wxFONTENCODING_ISO8859_1
+ cp
- 1);
361 else if ( registry
== _T("MICROSOFT") )
364 if ( wxSscanf(encoding
, wxT("cp125%d"), &cp
) == 1 )
366 m_encoding
= (wxFontEncoding
)(wxFONTENCODING_CP1250
+ cp
);
369 else if ( registry
== _T("KOI8") )
371 m_encoding
= wxFONTENCODING_KOI8
;
373 else // unknown encoding
375 // may be give a warning here? or use wxFontMapper?
376 m_encoding
= wxFONTENCODING_SYSTEM
;
381 wxFontRefData::wxFontRefData( const wxFontRefData
& data
)
384 m_pointSize
= data
.m_pointSize
;
385 m_family
= data
.m_family
;
386 m_style
= data
.m_style
;
387 m_weight
= data
.m_weight
;
389 m_underlined
= data
.m_underlined
;
391 m_faceName
= data
.m_faceName
;
392 m_encoding
= data
.m_encoding
;
394 m_noAA
= data
.m_noAA
;
396 m_nativeFontInfo
= data
.m_nativeFontInfo
;
399 wxFontRefData::wxFontRefData(int size
, int family
, int style
,
400 int weight
, bool underlined
,
401 const wxString
& faceName
,
402 wxFontEncoding encoding
)
404 Init(size
, family
, style
, weight
, underlined
, faceName
, encoding
);
407 wxFontRefData::wxFontRefData(const wxString
& fontname
)
409 // VZ: FromString() should really work in both cases, doesn't it?
411 m_nativeFontInfo
.FromString( fontname
);
413 m_nativeFontInfo
.SetXFontName(fontname
);
419 void wxFontRefData::ClearX11Fonts()
423 wxList::compatibility_iterator node
= m_fonts
.GetFirst();
426 wxXFont
* f
= (wxXFont
*) node
->GetData();
428 node
= node
->GetNext();
434 wxFontRefData::~wxFontRefData()
439 // ----------------------------------------------------------------------------
440 // wxFontRefData SetXXX()
441 // ----------------------------------------------------------------------------
443 void wxFontRefData::SetPointSize(int pointSize
)
445 m_pointSize
= pointSize
;
449 PangoFontDescription
*desc
= m_nativeFontInfo
.description
;
451 pango_font_description_set_size( desc
, m_pointSize
* PANGO_SCALE
);
455 void wxFontRefData::SetFamily(int family
)
459 // TODO: what are we supposed to do with m_nativeFontInfo here?
462 void wxFontRefData::SetStyle(int style
)
468 PangoFontDescription
*desc
= m_nativeFontInfo
.description
;
472 case wxFONTSTYLE_ITALIC
:
473 pango_font_description_set_style( desc
, PANGO_STYLE_ITALIC
);
475 case wxFONTSTYLE_SLANT
:
476 pango_font_description_set_style( desc
, PANGO_STYLE_OBLIQUE
);
479 wxFAIL_MSG( _T("unknown font style") );
481 case wxFONTSTYLE_NORMAL
:
482 pango_font_description_set_style( desc
, PANGO_STYLE_NORMAL
);
488 void wxFontRefData::SetWeight(int weight
)
493 void wxFontRefData::SetUnderlined(bool underlined
)
495 m_underlined
= underlined
;
497 // the XLFD doesn't have "underlined" field anyhow
500 void wxFontRefData::SetFaceName(const wxString
& facename
)
502 m_faceName
= facename
;
505 void wxFontRefData::SetEncoding(wxFontEncoding encoding
)
507 m_encoding
= encoding
;
510 void wxFontRefData::SetNativeFontInfo(const wxNativeFontInfo
& info
)
512 // previously cached fonts shouldn't be used
515 m_nativeFontInfo
= info
;
517 // set all the other font parameters from the native font info
521 // ----------------------------------------------------------------------------
523 // ----------------------------------------------------------------------------
529 wxFont::wxFont(const wxNativeFontInfo
& info
)
534 Create( info
.GetPointSize(),
538 info
.GetUnderlined(),
540 info
.GetEncoding() );
542 (void) Create(info
.GetXFontName());
546 bool wxFont::Create(int pointSize
,
551 const wxString
& faceName
,
552 wxFontEncoding encoding
)
556 m_refData
= new wxFontRefData(pointSize
, family
, style
, weight
,
557 underlined
, faceName
, encoding
);
564 bool wxFont::Create(const wxString
& fontname
, wxFontEncoding enc
)
568 *this = wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT
);
572 m_refData
= new wxFontRefData();
574 M_FONTDATA
->m_nativeFontInfo
.SetXFontName(fontname
); // X font name
578 wxStringTokenizer
tn( fontname
, wxT("-") );
580 tn
.GetNextToken(); // skip initial empty token
581 tn
.GetNextToken(); // foundry
584 M_FONTDATA
->m_faceName
= tn
.GetNextToken(); // family
586 tmp
= tn
.GetNextToken().MakeUpper(); // weight
587 if (tmp
== wxT("BOLD")) M_FONTDATA
->m_weight
= wxBOLD
;
588 if (tmp
== wxT("BLACK")) M_FONTDATA
->m_weight
= wxBOLD
;
589 if (tmp
== wxT("EXTRABOLD")) M_FONTDATA
->m_weight
= wxBOLD
;
590 if (tmp
== wxT("DEMIBOLD")) M_FONTDATA
->m_weight
= wxBOLD
;
591 if (tmp
== wxT("ULTRABOLD")) M_FONTDATA
->m_weight
= wxBOLD
;
593 if (tmp
== wxT("LIGHT")) M_FONTDATA
->m_weight
= wxLIGHT
;
594 if (tmp
== wxT("THIN")) M_FONTDATA
->m_weight
= wxLIGHT
;
596 tmp
= tn
.GetNextToken().MakeUpper(); // slant
597 if (tmp
== wxT("I")) M_FONTDATA
->m_style
= wxITALIC
;
598 if (tmp
== wxT("O")) M_FONTDATA
->m_style
= wxITALIC
;
600 tn
.GetNextToken(); // set width
601 tn
.GetNextToken(); // add. style
602 tn
.GetNextToken(); // pixel size
604 tmp
= tn
.GetNextToken(); // pointsize
607 long num
= wxStrtol (tmp
.c_str(), (wxChar
**) NULL
, 10);
608 M_FONTDATA
->m_pointSize
= (int)(num
/ 10);
611 tn
.GetNextToken(); // x-res
612 tn
.GetNextToken(); // y-res
614 tmp
= tn
.GetNextToken().MakeUpper(); // spacing
617 M_FONTDATA
->m_family
= wxMODERN
;
618 else if (M_FONTDATA
->m_faceName
== wxT("TIMES"))
619 M_FONTDATA
->m_family
= wxROMAN
;
620 else if (M_FONTDATA
->m_faceName
== wxT("HELVETICA"))
621 M_FONTDATA
->m_family
= wxSWISS
;
622 else if (M_FONTDATA
->m_faceName
== wxT("LUCIDATYPEWRITER"))
623 M_FONTDATA
->m_family
= wxTELETYPE
;
624 else if (M_FONTDATA
->m_faceName
== wxT("LUCIDA"))
625 M_FONTDATA
->m_family
= wxDECORATIVE
;
626 else if (M_FONTDATA
->m_faceName
== wxT("UTOPIA"))
627 M_FONTDATA
->m_family
= wxSCRIPT
;
629 tn
.GetNextToken(); // avg width
631 // deal with font encoding
632 M_FONTDATA
->m_encoding
= enc
;
633 if ( M_FONTDATA
->m_encoding
== wxFONTENCODING_SYSTEM
)
635 wxString registry
= tn
.GetNextToken().MakeUpper(),
636 encoding
= tn
.GetNextToken().MakeUpper();
638 if ( registry
== _T("ISO8859") )
641 if ( wxSscanf(encoding
, wxT("%d"), &cp
) == 1 )
643 M_FONTDATA
->m_encoding
=
644 (wxFontEncoding
)(wxFONTENCODING_ISO8859_1
+ cp
- 1);
647 else if ( registry
== _T("MICROSOFT") )
650 if ( wxSscanf(encoding
, wxT("cp125%d"), &cp
) == 1 )
652 M_FONTDATA
->m_encoding
=
653 (wxFontEncoding
)(wxFONTENCODING_CP1250
+ cp
);
656 else if ( registry
== _T("KOI8") )
658 M_FONTDATA
->m_encoding
= wxFONTENCODING_KOI8
;
660 //else: unknown encoding - may be give a warning here?
666 #endif // !wxUSE_UNICODE
672 // ----------------------------------------------------------------------------
673 // change the font attributes
674 // ----------------------------------------------------------------------------
676 void wxFont::Unshare()
678 // Don't change shared data
681 m_refData
= new wxFontRefData();
685 wxFontRefData
* ref
= new wxFontRefData(*(wxFontRefData
*)m_refData
);
691 // ----------------------------------------------------------------------------
693 // ----------------------------------------------------------------------------
695 int wxFont::GetPointSize() const
697 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
699 return M_FONTDATA
->m_pointSize
;
702 wxString
wxFont::GetFaceName() const
704 wxCHECK_MSG( Ok(), wxT(""), wxT("invalid font") );
706 return M_FONTDATA
->m_faceName
;
709 int wxFont::GetFamily() const
711 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
713 return M_FONTDATA
->m_family
;
716 int wxFont::GetStyle() const
718 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
720 return M_FONTDATA
->m_style
;
723 int wxFont::GetWeight() const
725 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
727 return M_FONTDATA
->m_weight
;
730 bool wxFont::GetUnderlined() const
732 wxCHECK_MSG( Ok(), FALSE
, wxT("invalid font") );
734 return M_FONTDATA
->m_underlined
;
737 wxFontEncoding
wxFont::GetEncoding() const
739 wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT
, wxT("invalid font") );
741 return M_FONTDATA
->m_encoding
;
744 bool wxFont::GetNoAntiAliasing() const
746 wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT
, wxT("invalid font") );
748 return M_FONTDATA
->m_noAA
;
751 const wxNativeFontInfo
*wxFont::GetNativeFontInfo() const
753 wxCHECK_MSG( Ok(), (wxNativeFontInfo
*)NULL
, wxT("invalid font") );
757 if ( M_FONTDATA
->m_nativeFontInfo
.GetXFontName().empty() )
761 return &(M_FONTDATA
->m_nativeFontInfo
);
764 bool wxFont::IsFixedWidth() const
766 wxCHECK_MSG( Ok(), FALSE
, wxT("invalid font") );
769 return wxFontBase::IsFixedWidth();
771 // Robert, is this right? HasNativeFont doesn't exist.
773 // if ( M_FONTDATA->HasNativeFont() )
775 // the monospace fonts are supposed to have "M" in the spacing field
776 wxString spacing
= M_FONTDATA
->
777 m_nativeFontInfo
.GetXFontComponent(wxXLFD_SPACING
);
779 return spacing
.Upper() == _T('M');
781 // Unreaceable code for now
782 // return wxFontBase::IsFixedWidth();
787 // ----------------------------------------------------------------------------
788 // change font attributes
789 // ----------------------------------------------------------------------------
791 void wxFont::SetPointSize(int pointSize
)
795 M_FONTDATA
->SetPointSize(pointSize
);
798 void wxFont::SetFamily(int family
)
802 M_FONTDATA
->SetFamily(family
);
805 void wxFont::SetStyle(int style
)
809 M_FONTDATA
->SetStyle(style
);
812 void wxFont::SetWeight(int weight
)
816 M_FONTDATA
->SetWeight(weight
);
819 void wxFont::SetFaceName(const wxString
& faceName
)
823 M_FONTDATA
->SetFaceName(faceName
);
826 void wxFont::SetUnderlined(bool underlined
)
830 M_FONTDATA
->SetUnderlined(underlined
);
833 void wxFont::SetEncoding(wxFontEncoding encoding
)
837 M_FONTDATA
->SetEncoding(encoding
);
840 void wxFont::DoSetNativeFontInfo( const wxNativeFontInfo
& info
)
844 M_FONTDATA
->SetNativeFontInfo( info
);
847 void wxFont::SetNoAntiAliasing( bool no
)
851 M_FONTDATA
->SetNoAntiAliasing( no
);
857 // ----------------------------------------------------------------------------
858 // X11 implementation
859 // ----------------------------------------------------------------------------
861 // Find an existing, or create a new, XFontStruct
862 // based on this wxFont and the given scale. Append the
863 // font to list in the private data for future reference.
864 wxXFont
* wxFont::GetInternalFont(double scale
, WXDisplay
* display
) const
867 return (wxXFont
*)NULL
;
869 long intScale
= long(scale
* 100.0 + 0.5); // key for wxXFont
870 int pointSize
= (M_FONTDATA
->m_pointSize
* 10 * intScale
) / 100;
872 // search existing fonts first
873 wxList::compatibility_iterator node
= M_FONTDATA
->m_fonts
.GetFirst();
876 wxXFont
* f
= (wxXFont
*) node
->GetData();
877 if ((!display
|| (f
->m_display
== display
)) && (f
->m_scale
== intScale
))
879 node
= node
->GetNext();
882 wxString xFontName
= M_FONTDATA
->m_nativeFontInfo
.GetXFontName();
883 if (xFontName
== "-*-*-*-*-*--*-*-*-*-*-*-*-*")
884 // wxFont constructor not called with native font info parameter => take M_FONTDATA values
887 // not found, create a new one
888 XFontStruct
*font
= (XFontStruct
*)
889 wxLoadQueryNearestFont(pointSize
,
890 M_FONTDATA
->m_family
,
892 M_FONTDATA
->m_weight
,
893 M_FONTDATA
->m_underlined
,
895 M_FONTDATA
->m_encoding
,
900 wxFAIL_MSG( wxT("Could not allocate even a default font -- something is wrong.") );
902 return (wxXFont
*) NULL
;
905 wxXFont
* f
= new wxXFont
;
906 f
->m_fontStruct
= (WXFontStructPtr
)font
;
907 f
->m_display
= ( display
? display
: wxGetDisplay() );
908 f
->m_scale
= intScale
;
909 M_FONTDATA
->m_fonts
.Append(f
);
914 WXFontStructPtr
wxFont::GetFontStruct(double scale
, WXDisplay
* display
) const
916 wxXFont
* f
= GetInternalFont(scale
, display
);
918 return (f
? f
->m_fontStruct
: (WXFontStructPtr
) 0);