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/msw/private.h"
42 #if !USE_SHARED_LIBRARIES
43 IMPLEMENT_DYNAMIC_CLASS(wxFont
, wxGDIObject
)
45 #if wxUSE_PORTABLE_FONTS_IN_MSW
46 IMPLEMENT_DYNAMIC_CLASS(wxFontNameDirectory
, wxObject
)
50 // ----------------------------------------------------------------------------
51 // wxFontRefData - the internal description of the font
52 // ----------------------------------------------------------------------------
54 class WXDLLEXPORT wxFontRefData
: public wxGDIRefData
56 friend class WXDLLEXPORT wxFont
;
61 Init(12, wxDEFAULT
, wxNORMAL
, wxNORMAL
, FALSE
,
62 "", wxFONTENCODING_DEFAULT
);
65 wxFontRefData(const wxFontRefData
& data
)
67 Init(data
.m_pointSize
, data
.m_family
, data
.m_style
, data
.m_weight
,
68 data
.m_underlined
, data
.m_faceName
, data
.m_encoding
);
70 m_fontId
= data
.m_fontId
;
73 wxFontRefData(int size
,
78 const wxString
& faceName
,
79 wxFontEncoding encoding
)
81 Init(size
, family
, style
, weight
, underlined
, faceName
, encoding
);
84 virtual ~wxFontRefData();
87 // common part of all ctors
93 const wxString
& faceName
,
94 wxFontEncoding encoding
);
96 // If TRUE, the pointer to the actual font is temporary and SHOULD NOT BE
97 // DELETED by destructor
102 // font characterstics
109 wxFontEncoding m_encoding
;
111 // Windows font handle
115 // ============================================================================
117 // ============================================================================
119 // ----------------------------------------------------------------------------
121 // ----------------------------------------------------------------------------
123 void wxFontRefData::Init(int pointSize
,
128 const wxString
& faceName
,
129 wxFontEncoding encoding
)
132 m_pointSize
= pointSize
;
136 m_underlined
= underlined
;
137 m_faceName
= faceName
;
138 m_encoding
= encoding
;
146 wxFontRefData::~wxFontRefData()
150 if ( !::DeleteObject((HFONT
) m_hFont
) )
152 wxLogLastError("DeleteObject(font)");
157 // ----------------------------------------------------------------------------
159 // ----------------------------------------------------------------------------
164 wxTheFontList
->Append(this);
167 /* Constructor for a font. Note that the real construction is done
168 * in wxDC::SetFont, when information is available about scaling etc.
170 bool wxFont::Create(int pointSize
,
175 const wxString
& faceName
,
176 wxFontEncoding encoding
)
179 m_refData
= new wxFontRefData(pointSize
, family
, style
, weight
,
180 underlined
, faceName
, encoding
);
190 wxTheFontList
->DeleteObject(this);
193 // ----------------------------------------------------------------------------
194 // real implementation
195 // ----------------------------------------------------------------------------
197 bool wxFont::RealizeResource()
199 if ( GetResourceHandle() )
201 // VZ: the old code returned FALSE in this case, but it doesn't seem
202 // to make sense because the font _was_ created
203 wxLogDebug(_T("Calling wxFont::RealizeResource() twice"));
213 switch (M_FONTDATA
->m_family
)
215 case wxSCRIPT
: ff_family
= FF_SCRIPT
;
216 ff_face
= _T("Script") ;
218 case wxDECORATIVE
: ff_family
= FF_DECORATIVE
;
220 case wxROMAN
: ff_family
= FF_ROMAN
;
221 ff_face
= _T("Times New Roman") ;
224 case wxMODERN
: ff_family
= FF_MODERN
;
225 ff_face
= _T("Courier New") ;
227 case wxSWISS
: ff_family
= FF_SWISS
;
228 ff_face
= _T("Arial") ;
231 default: ff_family
= FF_SWISS
;
232 ff_face
= _T("Arial") ;
235 if (M_FONTDATA
->m_style
== wxITALIC
|| M_FONTDATA
->m_style
== wxSLANT
)
240 if (M_FONTDATA
->m_weight
== wxNORMAL
)
241 ff_weight
= FW_NORMAL
;
242 else if (M_FONTDATA
->m_weight
== wxLIGHT
)
243 ff_weight
= FW_LIGHT
;
244 else if (M_FONTDATA
->m_weight
== wxBOLD
)
247 const wxChar
* pzFace
= (const wxChar
*) ff_face
;
248 if (!M_FONTDATA
->m_faceName
.IsNull())
249 pzFace
= (const wxChar
*) M_FONTDATA
->m_faceName
;
251 /* Always calculate fonts using the screen DC (is this the best strategy?)
252 * There may be confusion if a font is selected into a printer
253 * DC (say), because the height will be calculated very differently.
254 // What sort of display is it?
255 int technology = ::GetDeviceCaps(dc, TECHNOLOGY);
259 if (technology != DT_RASDISPLAY && technology != DT_RASPRINTER)
261 // Have to get screen DC Caps, because a metafile will return 0.
262 HDC dc2 = ::GetDC(NULL);
263 nHeight = M_FONTDATA->m_pointSize*GetDeviceCaps(dc2, LOGPIXELSY)/72;
264 ::ReleaseDC(NULL, dc2);
268 nHeight = M_FONTDATA->m_pointSize*GetDeviceCaps(dc, LOGPIXELSY)/72;
271 // Have to get screen DC Caps, because a metafile will return 0.
272 HDC dc2
= ::GetDC(NULL
);
273 int ppInch
= ::GetDeviceCaps(dc2
, LOGPIXELSY
);
274 ::ReleaseDC(NULL
, dc2
);
276 // New behaviour: apparently ppInch varies according to
277 // Large/Small Fonts setting in Windows. This messes
278 // up fonts. So, set ppInch to a constant 96 dpi.
281 #if wxFONT_SIZE_COMPATIBILITY
282 // Incorrect, but compatible with old wxWindows behaviour
283 int nHeight
= (M_FONTDATA
->m_pointSize
*ppInch
/72);
285 // Correct for Windows compatibility
286 int nHeight
= - (M_FONTDATA
->m_pointSize
*ppInch
/72);
289 bool ff_underline
= M_FONTDATA
->m_underlined
;
291 M_FONTDATA
->m_hFont
= (WXHFONT
) CreateFont(nHeight
, 0, 0, 0,ff_weight
,ff_italic
,(BYTE
)ff_underline
,
292 0, ANSI_CHARSET
, OUT_DEFAULT_PRECIS
, CLIP_DEFAULT_PRECIS
,
293 PROOF_QUALITY
, DEFAULT_PITCH
| ff_family
, pzFace
);
294 #ifdef WXDEBUG_CREATE
295 if (m_hFont
==NULL
) wxError(_T("Cannot create font"),_T("Internal Error")) ;
297 return (M_FONTDATA
->m_hFont
!= (WXHFONT
) NULL
);
300 bool wxFont::FreeResource(bool force
)
302 if ( GetResourceHandle() )
304 if ( !::DeleteObject((HFONT
) M_FONTDATA
->m_hFont
) )
306 wxLogLastError("DeleteObject(font)");
309 M_FONTDATA
->m_hFont
= 0;
316 WXHANDLE
wxFont::GetResourceHandle()
321 return (WXHANDLE
)M_FONTDATA
->m_hFont
;
324 bool wxFont::IsFree() const
326 return (M_FONTDATA
&& (M_FONTDATA
->m_hFont
== 0));
329 void wxFont::Unshare()
331 // Don't change shared data
334 m_refData
= new wxFontRefData();
338 wxFontRefData
* ref
= new wxFontRefData(*M_FONTDATA
);
344 // ----------------------------------------------------------------------------
345 // change font attribute: we recreate font when doing it
346 // ----------------------------------------------------------------------------
348 void wxFont::SetPointSize(int pointSize
)
352 M_FONTDATA
->m_pointSize
= pointSize
;
357 void wxFont::SetFamily(int family
)
361 M_FONTDATA
->m_family
= family
;
366 void wxFont::SetStyle(int style
)
370 M_FONTDATA
->m_style
= style
;
375 void wxFont::SetWeight(int weight
)
379 M_FONTDATA
->m_weight
= weight
;
384 void wxFont::SetFaceName(const wxString
& faceName
)
388 M_FONTDATA
->m_faceName
= faceName
;
393 void wxFont::SetUnderlined(bool underlined
)
397 M_FONTDATA
->m_underlined
= underlined
;
402 void wxFont::SetEncoding(wxFontEncoding encoding
)
406 M_FONTDATA
->m_encoding
= encoding
;
411 // ----------------------------------------------------------------------------
413 // ----------------------------------------------------------------------------
415 int wxFont::GetPointSize() const
417 return M_FONTDATA
->m_pointSize
;
420 int wxFont::GetFamily() const
422 return M_FONTDATA
->m_family
;
425 int wxFont::GetFontId() const
427 return M_FONTDATA
->m_fontId
;
430 int wxFont::GetStyle() const
432 return M_FONTDATA
->m_style
;
435 int wxFont::GetWeight() const
437 return M_FONTDATA
->m_weight
;
440 bool wxFont::GetUnderlined() const
442 return M_FONTDATA
->m_underlined
;
445 wxString
wxFont::GetFaceName() const
449 str
= M_FONTDATA
->m_faceName
;
453 wxFontEncoding
wxFont::GetEncoding() const
455 return M_FONTDATA
->m_encoding
;