1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxFont class
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "font.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
40 #include "wx/fontutil.h"
41 #include "wx/tokenzr.h"
43 #include "wx/msw/private.h"
44 #include "wx/tokenzr.h"
46 IMPLEMENT_DYNAMIC_CLASS(wxFont
, wxGDIObject
)
48 // ----------------------------------------------------------------------------
50 // ----------------------------------------------------------------------------
52 // the default font size in points
53 static const int wxDEFAULT_FONT_SIZE
= 12;
55 // ----------------------------------------------------------------------------
56 // wxFontRefData - the internal description of the font
57 // ----------------------------------------------------------------------------
59 class WXDLLEXPORT wxFontRefData
: public wxGDIRefData
61 friend class WXDLLEXPORT wxFont
;
66 Init(wxDEFAULT_FONT_SIZE
, wxDEFAULT
, wxNORMAL
, wxNORMAL
, FALSE
,
67 "", wxFONTENCODING_DEFAULT
);
70 wxFontRefData(const wxFontRefData
& data
)
72 Init(data
.m_pointSize
, data
.m_family
, data
.m_style
, data
.m_weight
,
73 data
.m_underlined
, data
.m_faceName
, data
.m_encoding
);
75 m_fontId
= data
.m_fontId
;
78 wxFontRefData(int size
,
83 const wxString
& faceName
,
84 wxFontEncoding encoding
)
86 Init(size
, family
, style
, weight
, underlined
, faceName
, encoding
);
89 wxFontRefData(const wxNativeFontInfo
& info
, WXHFONT hFont
= 0)
94 virtual ~wxFontRefData();
97 // common part of all ctors
103 const wxString
& faceName
,
104 wxFontEncoding encoding
);
106 void Init(const wxNativeFontInfo
& info
, WXHFONT hFont
= 0);
108 // If TRUE, the pointer to the actual font is temporary and SHOULD NOT BE
109 // DELETED by destructor
114 // font characterstics
121 wxFontEncoding m_encoding
;
123 // Windows font handle
127 wxNativeFontInfo m_nativeFontInfo
;
128 bool m_nativeFontInfoOk
;
131 // ============================================================================
133 // ============================================================================
135 // ----------------------------------------------------------------------------
137 // ----------------------------------------------------------------------------
139 void wxFontRefData::Init(int pointSize
,
144 const wxString
& faceName
,
145 wxFontEncoding encoding
)
148 m_pointSize
= pointSize
;
152 m_underlined
= underlined
;
153 m_faceName
= faceName
;
154 m_encoding
= encoding
;
161 m_nativeFontInfoOk
= FALSE
;
164 void wxFontRefData::Init(const wxNativeFontInfo
& info
, WXHFONT hFont
)
166 // extract family from pitch-and-family
167 int lfFamily
= info
.lf
.lfPitchAndFamily
;
168 if ( lfFamily
& FIXED_PITCH
)
169 lfFamily
-= FIXED_PITCH
;
170 if ( lfFamily
& VARIABLE_PITCH
)
171 lfFamily
-= VARIABLE_PITCH
;
192 m_family
= wxDECORATIVE
;
200 switch ( info
.lf
.lfWeight
)
216 m_style
= info
.lf
.lfItalic
? wxITALIC
: wxNORMAL
;
218 m_underlined
= info
.lf
.lfUnderline
!= 0;
220 m_faceName
= info
.lf
.lfFaceName
;
222 int height
= abs(info
.lf
.lfHeight
);
224 // remember that 1pt = 1/72inch
225 const int ppInch
= ::GetDeviceCaps(ScreenHDC(), LOGPIXELSY
);
226 m_pointSize
= (int) (((72.0*((double)height
))/(double) ppInch
) + 0.5);
228 m_encoding
= wxGetFontEncFromCharSet(info
.lf
.lfCharSet
);
233 // hFont may be zero, or it be passed in case we really want to
234 // use the exact font created in the underlying system
235 // (for example where we can't guarantee conversion from HFONT
236 // to LOGFONT back to HFONT)
239 m_nativeFontInfoOk
= TRUE
;
240 m_nativeFontInfo
= info
;
243 wxFontRefData::~wxFontRefData()
247 if ( !::DeleteObject((HFONT
) m_hFont
) )
249 wxLogLastError(wxT("DeleteObject(font)"));
255 // ----------------------------------------------------------------------------
257 // ----------------------------------------------------------------------------
259 bool wxNativeFontInfo::FromString(const wxString
& s
)
263 wxStringTokenizer
tokenizer(s
, _T(";"));
265 wxString token
= tokenizer
.GetNextToken();
267 // Ignore the version for now
270 token
= tokenizer
.GetNextToken();
271 if ( !token
.ToLong(&l
) )
275 token
= tokenizer
.GetNextToken();
276 if ( !token
.ToLong(&l
) )
280 token
= tokenizer
.GetNextToken();
281 if ( !token
.ToLong(&l
) )
285 token
= tokenizer
.GetNextToken();
286 if ( !token
.ToLong(&l
) )
288 lf
.lfOrientation
= l
;
290 token
= tokenizer
.GetNextToken();
291 if ( !token
.ToLong(&l
) )
295 token
= tokenizer
.GetNextToken();
296 if ( !token
.ToLong(&l
) )
298 lf
.lfItalic
= (BYTE
)l
;
300 token
= tokenizer
.GetNextToken();
301 if ( !token
.ToLong(&l
) )
303 lf
.lfUnderline
= (BYTE
)l
;
305 token
= tokenizer
.GetNextToken();
306 if ( !token
.ToLong(&l
) )
308 lf
.lfStrikeOut
= (BYTE
)l
;
310 token
= tokenizer
.GetNextToken();
311 if ( !token
.ToLong(&l
) )
313 lf
.lfCharSet
= (BYTE
)l
;
315 token
= tokenizer
.GetNextToken();
316 if ( !token
.ToLong(&l
) )
318 lf
.lfOutPrecision
= (BYTE
)l
;
320 token
= tokenizer
.GetNextToken();
321 if ( !token
.ToLong(&l
) )
323 lf
.lfClipPrecision
= (BYTE
)l
;
325 token
= tokenizer
.GetNextToken();
326 if ( !token
.ToLong(&l
) )
328 lf
.lfQuality
= (BYTE
)l
;
330 token
= tokenizer
.GetNextToken();
331 if ( !token
.ToLong(&l
) )
333 lf
.lfPitchAndFamily
= (BYTE
)l
;
335 token
= tokenizer
.GetNextToken();
338 wxStrcpy(lf
.lfFaceName
, token
.c_str());
343 wxString
wxNativeFontInfo::ToString() const
347 s
.Printf(_T("%d;%d;%d;%d;%d;%d;%d;%d;%d;%d;%d;%d;%d;%d;%s"),
348 0, // version, in case we want to change the format later
367 // ----------------------------------------------------------------------------
369 // ----------------------------------------------------------------------------
374 wxTheFontList
->Append(this);
377 bool wxFont::Create(const wxNativeFontInfo
& info
, WXHFONT hFont
)
381 m_refData
= new wxFontRefData(info
, hFont
);
388 wxFont::wxFont(const wxString
& fontdesc
)
390 wxNativeFontInfo info
;
391 if ( info
.FromString(fontdesc
) )
395 /* Constructor for a font. Note that the real construction is done
396 * in wxDC::SetFont, when information is available about scaling etc.
398 bool wxFont::Create(int pointSize
,
403 const wxString
& faceName
,
404 wxFontEncoding encoding
)
408 // wxDEFAULT is a valid value for the font size too so we must treat it
409 // specially here (otherwise the size would be 70 == wxDEFAULT value)
410 if ( pointSize
== wxDEFAULT
)
411 pointSize
= wxDEFAULT_FONT_SIZE
;
413 m_refData
= new wxFontRefData(pointSize
, family
, style
, weight
,
414 underlined
, faceName
, encoding
);
424 wxTheFontList
->DeleteObject(this);
427 // ----------------------------------------------------------------------------
428 // real implementation
429 // ----------------------------------------------------------------------------
431 bool wxFont::RealizeResource()
433 if ( GetResourceHandle() )
435 // VZ: the old code returned FALSE in this case, but it doesn't seem
436 // to make sense because the font _was_ created
440 if(!M_FONTDATA
->m_nativeFontInfoOk
)
442 wxFillLogFont(&M_FONTDATA
->m_nativeFontInfo
.lf
, this);
443 M_FONTDATA
->m_nativeFontInfoOk
= TRUE
;
446 M_FONTDATA
->m_hFont
= (WXHFONT
)::CreateFontIndirect(&M_FONTDATA
->m_nativeFontInfo
.lf
);
447 M_FONTDATA
->m_faceName
= M_FONTDATA
->m_nativeFontInfo
.lf
.lfFaceName
;
448 if ( !M_FONTDATA
->m_hFont
)
450 wxLogLastError(wxT("CreateFont"));
458 bool wxFont::FreeResource(bool WXUNUSED(force
))
460 if ( GetResourceHandle() )
462 if ( !::DeleteObject((HFONT
) M_FONTDATA
->m_hFont
) )
464 wxLogLastError(wxT("DeleteObject(font)"));
467 M_FONTDATA
->m_hFont
= 0;
474 WXHANDLE
wxFont::GetResourceHandle()
479 WXHFONT
wxFont::GetHFONT() const
484 return (WXHANDLE
)M_FONTDATA
->m_hFont
;
487 bool wxFont::IsFree() const
489 return (M_FONTDATA
&& (M_FONTDATA
->m_hFont
== 0));
492 void wxFont::Unshare()
494 // Don't change shared data
497 m_refData
= new wxFontRefData();
501 wxFontRefData
* ref
= new wxFontRefData(*M_FONTDATA
);
507 // ----------------------------------------------------------------------------
508 // change font attribute: we recreate font when doing it
509 // ----------------------------------------------------------------------------
511 void wxFont::SetPointSize(int pointSize
)
515 M_FONTDATA
->m_pointSize
= pointSize
;
516 M_FONTDATA
->m_nativeFontInfoOk
= FALSE
;
521 void wxFont::SetFamily(int family
)
525 M_FONTDATA
->m_family
= family
;
526 M_FONTDATA
->m_nativeFontInfoOk
= FALSE
;
531 void wxFont::SetStyle(int style
)
535 M_FONTDATA
->m_style
= style
;
536 M_FONTDATA
->m_nativeFontInfoOk
= FALSE
;
541 void wxFont::SetWeight(int weight
)
545 M_FONTDATA
->m_weight
= weight
;
546 M_FONTDATA
->m_nativeFontInfoOk
= FALSE
;
551 void wxFont::SetFaceName(const wxString
& faceName
)
555 M_FONTDATA
->m_faceName
= faceName
;
556 M_FONTDATA
->m_nativeFontInfoOk
= FALSE
;
561 void wxFont::SetUnderlined(bool underlined
)
565 M_FONTDATA
->m_underlined
= underlined
;
566 M_FONTDATA
->m_nativeFontInfoOk
= FALSE
;
571 void wxFont::SetEncoding(wxFontEncoding encoding
)
575 M_FONTDATA
->m_encoding
= encoding
;
576 M_FONTDATA
->m_nativeFontInfoOk
= FALSE
;
581 void wxFont::SetNativeFontInfo(const wxNativeFontInfo
& info
)
587 M_FONTDATA
->Init(info
);
592 // ----------------------------------------------------------------------------
594 // ----------------------------------------------------------------------------
596 int wxFont::GetPointSize() const
598 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
600 return M_FONTDATA
->m_pointSize
;
603 int wxFont::GetFamily() const
605 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
607 return M_FONTDATA
->m_family
;
610 int wxFont::GetFontId() const
612 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
614 return M_FONTDATA
->m_fontId
;
617 int wxFont::GetStyle() const
619 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
621 return M_FONTDATA
->m_style
;
624 int wxFont::GetWeight() const
626 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
628 return M_FONTDATA
->m_weight
;
631 bool wxFont::GetUnderlined() const
633 wxCHECK_MSG( Ok(), FALSE
, wxT("invalid font") );
635 return M_FONTDATA
->m_underlined
;
638 wxString
wxFont::GetFaceName() const
640 wxCHECK_MSG( Ok(), wxT(""), wxT("invalid font") );
642 return M_FONTDATA
->m_faceName
;
645 wxFontEncoding
wxFont::GetEncoding() const
647 wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT
, wxT("invalid font") );
649 return M_FONTDATA
->m_encoding
;
652 wxNativeFontInfo
*wxFont::GetNativeFontInfo() const
654 if( M_FONTDATA
->m_nativeFontInfoOk
)
655 return new wxNativeFontInfo(M_FONTDATA
->m_nativeFontInfo
);