1 ///////////////////////////////////////////////////////////////////////////// 
   4 // Author:      Robert Roebling 
   6 // Copyright:   (c) 1998 Robert Roebling and Julian Smart 
   7 // Licence:     wxWindows licence 
   8 ///////////////////////////////////////////////////////////////////////////// 
  10 // ============================================================================ 
  12 // ============================================================================ 
  14 // ---------------------------------------------------------------------------- 
  16 // ---------------------------------------------------------------------------- 
  18 // For compilers that support precompilation, includes "wx.h". 
  19 #include "wx/wxprec.h" 
  22 #include "wx/fontutil.h" 
  23 #include "wx/cmndata.h" 
  26 #include "wx/gdicmn.h" 
  27 #include "wx/tokenzr.h" 
  28 #include "wx/settings.h" 
  32 #include "wx/gtk/private.h" 
  33 #include <gdk/gdkprivate.h> 
  35 // ---------------------------------------------------------------------------- 
  37 // ---------------------------------------------------------------------------- 
  39 // the default size (in points) for the fonts 
  40 static const int wxDEFAULT_FONT_SIZE 
= 12; 
  42 // ---------------------------------------------------------------------------- 
  43 // wxScaledFontList: maps the font sizes to the GDK fonts for the given font 
  44 // ---------------------------------------------------------------------------- 
  46 WX_DECLARE_HASH_MAP(int, GdkFont 
*, wxIntegerHash
, wxIntegerEqual
, 
  49 // ---------------------------------------------------------------------------- 
  51 // ---------------------------------------------------------------------------- 
  53 class wxFontRefData 
: public wxObjectRefData
 
  56     // from broken down font parameters, also default ctor 
  57     wxFontRefData(int size 
= -1, 
  58                   int family 
= wxFONTFAMILY_DEFAULT
, 
  59                   int style 
= wxFONTSTYLE_NORMAL
, 
  60                   int weight 
= wxFONTWEIGHT_NORMAL
, 
  61                   bool underlined 
= FALSE
, 
  62                   const wxString
& faceName 
= wxEmptyString
, 
  63                   wxFontEncoding encoding 
= wxFONTENCODING_DEFAULT
); 
  66     wxFontRefData(const wxString
& fontname
); 
  69     wxFontRefData( const wxFontRefData
& data 
); 
  71     virtual ~wxFontRefData(); 
  73     // do we have the native font info? 
  74     bool HasNativeFont() const 
  76         // we always have a Pango font description 
  80     // setters: all of them also take care to modify m_nativeFontInfo if we 
  81     // have it so as to not lose the information not carried by our fields 
  82     void SetPointSize(int pointSize
); 
  83     void SetFamily(int family
); 
  84     void SetStyle(int style
); 
  85     void SetWeight(int weight
); 
  86     void SetUnderlined(bool underlined
); 
  87     void SetFaceName(const wxString
& facename
); 
  88     void SetEncoding(wxFontEncoding encoding
); 
  90     void SetNoAntiAliasing( bool no 
= TRUE 
) { m_noAA 
= no
; } 
  91     bool GetNoAntiAliasing() const { return m_noAA
; } 
  93     // and this one also modifies all the other font data fields 
  94     void SetNativeFontInfo(const wxNativeFontInfo
& info
); 
  97     // common part of all ctors 
  98     void Init(int pointSize
, 
 103               const wxString
& faceName
, 
 104               wxFontEncoding encoding
); 
 106     // set all fields from (already initialized and valid) m_nativeFontInfo 
 107     void InitFromNative(); 
 110     // clear m_scaled_xfonts if any 
 111     void ClearGdkFonts(); 
 119     wxFontEncoding  m_encoding
;  // Unused under GTK 2.0 
 120     bool            m_noAA
;      // No anti-aliasing 
 122     // The native font info, basicly an XFLD under GTK 1.2 and 
 123     // the pango font description under GTK 2.0. 
 124     wxNativeFontInfo m_nativeFontInfo
; 
 129 // ---------------------------------------------------------------------------- 
 131 // ---------------------------------------------------------------------------- 
 133 void wxFontRefData::Init(int pointSize
, 
 138                          const wxString
& faceName
, 
 139                          wxFontEncoding encoding
) 
 141     m_family 
= family 
== wxFONTFAMILY_DEFAULT 
? wxFONTFAMILY_SWISS 
: family
; 
 143     m_faceName 
= faceName
; 
 145     // we accept both wxDEFAULT and wxNORMAL here - should we? 
 146     m_style 
= style 
== wxDEFAULT 
? wxFONTSTYLE_NORMAL 
: style
; 
 147     m_weight 
= weight 
== wxDEFAULT 
? wxFONTWEIGHT_NORMAL 
: weight
; 
 149     // and here, do we really want to forbid creation of the font of the size 
 150     // 90 (the value of wxDEFAULT)?? 
 151     m_pointSize 
= pointSize 
== wxDEFAULT 
|| pointSize 
== -1 
 152                     ? wxDEFAULT_FONT_SIZE
 
 155     m_underlined 
= underlined
; 
 156     m_encoding 
= encoding
; 
 160     // Create native font info 
 161     m_nativeFontInfo
.description 
= pango_font_description_new(); 
 163     // And set its values 
 164     if (!m_faceName
.empty()) 
 166        pango_font_description_set_family( m_nativeFontInfo
.description
, wxGTK_CONV(m_faceName
) ); 
 172             case wxFONTFAMILY_MODERN
: 
 173             case wxFONTFAMILY_TELETYPE
: 
 174                pango_font_description_set_family( m_nativeFontInfo
.description
, "monospace" ); 
 176             case wxFONTFAMILY_ROMAN
: 
 177                pango_font_description_set_family( m_nativeFontInfo
.description
, "serif" ); 
 179             case wxFONTFAMILY_SWISS
: 
 180                // SWISS = sans serif 
 182                pango_font_description_set_family( m_nativeFontInfo
.description
, "sans" ); 
 188     SetPointSize( m_pointSize 
); 
 189     SetWeight( m_weight 
); 
 192 void wxFontRefData::InitFromNative() 
 197     PangoFontDescription 
*desc 
= m_nativeFontInfo
.description
; 
 200     m_faceName 
= wxGTK_CONV_BACK( pango_font_description_get_family( desc 
) ); 
 202     // Pango sometimes needs to have a size 
 203     int pango_size 
= pango_font_description_get_size( desc 
); 
 205         m_nativeFontInfo
.SetPointSize(12); 
 207     m_pointSize 
= m_nativeFontInfo
.GetPointSize(); 
 208     m_style 
= m_nativeFontInfo
.GetStyle(); 
 209     m_weight 
= m_nativeFontInfo
.GetWeight(); 
 211     if (m_faceName 
== wxT("monospace")) 
 213         m_family 
= wxFONTFAMILY_TELETYPE
; 
 215     else if (m_faceName 
== wxT("sans")) 
 217         m_family 
= wxFONTFAMILY_SWISS
; 
 219     else if (m_faceName 
== wxT("serif")) 
 221         m_family 
= wxFONTFAMILY_ROMAN
; 
 225         m_family 
= wxFONTFAMILY_UNKNOWN
; 
 228     // Pango description are never underlined (?) 
 229     m_underlined 
= FALSE
; 
 231     // Cannot we choose that 
 232     m_encoding 
= wxFONTENCODING_SYSTEM
; 
 235 wxFontRefData::wxFontRefData( const wxFontRefData
& data 
) 
 238     m_pointSize 
= data
.m_pointSize
; 
 239     m_family 
= data
.m_family
; 
 240     m_style 
= data
.m_style
; 
 241     m_weight 
= data
.m_weight
; 
 243     m_underlined 
= data
.m_underlined
; 
 245     m_faceName 
= data
.m_faceName
; 
 246     m_encoding 
= data
.m_encoding
; 
 248     m_noAA 
= data
.m_noAA
; 
 250     // Forces a copy of the internal data.  wxNativeFontInfo should probably 
 251     // have a copy ctor and assignment operator to fix this properly but that 
 252     // would break binary compatibility... 
 253     m_nativeFontInfo
.FromString(data
.m_nativeFontInfo
.ToString()); 
 256 wxFontRefData::wxFontRefData(int size
, int family
, int style
, 
 257                              int weight
, bool underlined
, 
 258                              const wxString
& faceName
, 
 259                              wxFontEncoding encoding
) 
 261     Init(size
, family
, style
, weight
, underlined
, faceName
, encoding
); 
 264 wxFontRefData::wxFontRefData(const wxString
& fontname
) 
 266     m_nativeFontInfo
.FromString( fontname 
); 
 271 void wxFontRefData::ClearGdkFonts() 
 275 wxFontRefData::~wxFontRefData() 
 280 // ---------------------------------------------------------------------------- 
 281 // wxFontRefData SetXXX() 
 282 // ---------------------------------------------------------------------------- 
 284 void wxFontRefData::SetPointSize(int pointSize
) 
 286     m_pointSize 
= pointSize
; 
 288     m_nativeFontInfo
.SetPointSize(pointSize
); 
 291 void wxFontRefData::SetFamily(int family
) 
 295     // TODO: what are we supposed to do with m_nativeFontInfo here? 
 298 void wxFontRefData::SetStyle(int style
) 
 302     m_nativeFontInfo
.SetStyle((wxFontStyle
)style
); 
 305 void wxFontRefData::SetWeight(int weight
) 
 309     m_nativeFontInfo
.SetWeight((wxFontWeight
)weight
); 
 312 void wxFontRefData::SetUnderlined(bool underlined
) 
 314     m_underlined 
= underlined
; 
 316     // the XLFD doesn't have "underlined" field anyhow 
 319 void wxFontRefData::SetFaceName(const wxString
& facename
) 
 321     m_faceName 
= facename
; 
 323     m_nativeFontInfo
.SetFaceName(facename
); 
 326 void wxFontRefData::SetEncoding(wxFontEncoding encoding
) 
 328     m_encoding 
= encoding
; 
 331 void wxFontRefData::SetNativeFontInfo(const wxNativeFontInfo
& info
) 
 333     // previously cached fonts shouldn't be used 
 336     m_nativeFontInfo 
= info
; 
 338     // set all the other font parameters from the native font info 
 342 // ---------------------------------------------------------------------------- 
 344 // ---------------------------------------------------------------------------- 
 346 IMPLEMENT_DYNAMIC_CLASS(wxFont
, wxGDIObject
) 
 348 wxFont::wxFont(const wxNativeFontInfo
& info
) 
 350     Create( info
.GetPointSize(), 
 354             info
.GetUnderlined(), 
 356             info
.GetEncoding() ); 
 359 bool wxFont::Create( int pointSize
, 
 364                      const wxString
& face
, 
 365                      wxFontEncoding encoding
) 
 369     m_refData 
= new wxFontRefData(pointSize
, family
, style
, weight
, 
 370                                   underlined
, face
, encoding
); 
 375 bool wxFont::Create(const wxString
& fontname
) 
 377     // VZ: does this really happen? 
 378     if ( fontname
.empty() ) 
 380         *this = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
); 
 385     m_refData 
= new wxFontRefData(fontname
); 
 390 void wxFont::Unshare() 
 394         m_refData 
= new wxFontRefData(); 
 398         wxFontRefData
* ref 
= new wxFontRefData(*(wxFontRefData
*)m_refData
); 
 408 // ---------------------------------------------------------------------------- 
 410 // ---------------------------------------------------------------------------- 
 412 int wxFont::GetPointSize() const 
 414     wxCHECK_MSG( Ok(), 0, wxT("invalid font") ); 
 417     return M_FONTDATA
->HasNativeFont() ? M_FONTDATA
->m_nativeFontInfo
.GetPointSize() 
 418                                        : M_FONTDATA
->m_pointSize
; 
 420     return M_FONTDATA
->m_pointSize
; 
 424 wxString 
wxFont::GetFaceName() const 
 426     wxCHECK_MSG( Ok(), wxT(""), wxT("invalid font") ); 
 429     return M_FONTDATA
->HasNativeFont() ? M_FONTDATA
->m_nativeFontInfo
.GetFaceName() 
 430                                        : M_FONTDATA
->m_faceName
; 
 432     return M_FONTDATA
->m_faceName
; 
 436 int wxFont::GetFamily() const 
 438     wxCHECK_MSG( Ok(), 0, wxT("invalid font") ); 
 441     int ret 
= M_FONTDATA
->m_family
; 
 442     if (M_FONTDATA
->HasNativeFont()) 
 443         // wxNativeFontInfo::GetFamily is expensive, must not call more than once 
 444         ret 
= M_FONTDATA
->m_nativeFontInfo
.GetFamily(); 
 446     if (ret 
== wxFONTFAMILY_DEFAULT
) 
 447         ret 
= M_FONTDATA
->m_family
; 
 451     return M_FONTDATA
->m_family
; 
 455 int wxFont::GetStyle() const 
 457     wxCHECK_MSG( Ok(), 0, wxT("invalid font") ); 
 460     return M_FONTDATA
->HasNativeFont() ? M_FONTDATA
->m_nativeFontInfo
.GetStyle() 
 461                                        : M_FONTDATA
->m_style
; 
 463     return M_FONTDATA
->m_style
; 
 467 int wxFont::GetWeight() const 
 469     wxCHECK_MSG( Ok(), 0, wxT("invalid font") ); 
 472     return M_FONTDATA
->HasNativeFont() ? M_FONTDATA
->m_nativeFontInfo
.GetWeight() 
 473                                        : M_FONTDATA
->m_weight
; 
 475     return M_FONTDATA
->m_weight
; 
 479 bool wxFont::GetUnderlined() const 
 481     wxCHECK_MSG( Ok(), FALSE
, wxT("invalid font") ); 
 483     return M_FONTDATA
->m_underlined
; 
 486 wxFontEncoding 
wxFont::GetEncoding() const 
 488     wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT
, wxT("invalid font") ); 
 490     // m_encoding is unused in wxGTK2, return encoding that the user set. 
 491     return M_FONTDATA
->m_encoding
; 
 494 bool wxFont::GetNoAntiAliasing() const 
 496     wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT
, wxT("invalid font") ); 
 498     return M_FONTDATA
->m_noAA
; 
 501 const wxNativeFontInfo 
*wxFont::GetNativeFontInfo() const 
 503     wxCHECK_MSG( Ok(), (wxNativeFontInfo 
*)NULL
, wxT("invalid font") ); 
 505     return &(M_FONTDATA
->m_nativeFontInfo
); 
 508 bool wxFont::IsFixedWidth() const 
 510     wxCHECK_MSG( Ok(), FALSE
, wxT("invalid font") ); 
 512     return wxFontBase::IsFixedWidth(); 
 515 // ---------------------------------------------------------------------------- 
 516 // change font attributes 
 517 // ---------------------------------------------------------------------------- 
 519 void wxFont::SetPointSize(int pointSize
) 
 523     M_FONTDATA
->SetPointSize(pointSize
); 
 526 void wxFont::SetFamily(int family
) 
 530     M_FONTDATA
->SetFamily(family
); 
 533 void wxFont::SetStyle(int style
) 
 537     M_FONTDATA
->SetStyle(style
); 
 540 void wxFont::SetWeight(int weight
) 
 544     M_FONTDATA
->SetWeight(weight
); 
 547 void wxFont::SetFaceName(const wxString
& faceName
) 
 551     M_FONTDATA
->SetFaceName(faceName
); 
 554 void wxFont::SetUnderlined(bool underlined
) 
 558     M_FONTDATA
->SetUnderlined(underlined
); 
 561 void wxFont::SetEncoding(wxFontEncoding encoding
) 
 565     M_FONTDATA
->SetEncoding(encoding
); 
 568 void wxFont::DoSetNativeFontInfo( const wxNativeFontInfo
& info 
) 
 572     M_FONTDATA
->SetNativeFontInfo( info 
); 
 575 void wxFont::SetNoAntiAliasing( bool no 
) 
 579     M_FONTDATA
->SetNoAntiAliasing( no 
);