1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxFont class
4 // Author: David Webster
8 // Copyright: (c) David Webster
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
32 #include "wx/os2/private.h"
34 IMPLEMENT_DYNAMIC_CLASS(wxFont
, wxGDIObject
)
36 #if wxUSE_PORTABLE_FONTS_IN_MSW
37 IMPLEMENT_DYNAMIC_CLASS(wxFontNameDirectory
, wxObject
)
40 // ----------------------------------------------------------------------------
41 // wxFontRefData - the internal description of the font
42 // ----------------------------------------------------------------------------
44 class WXDLLEXPORT wxFontRefData
: public wxGDIRefData
46 friend class WXDLLEXPORT wxFont
;
51 Init(12, wxDEFAULT
, wxNORMAL
, wxNORMAL
, FALSE
,
52 "", wxFONTENCODING_DEFAULT
);
55 wxFontRefData(const wxFontRefData
& data
)
57 Init(data
.m_pointSize
, data
.m_family
, data
.m_style
, data
.m_weight
,
58 data
.m_underlined
, data
.m_faceName
, data
.m_encoding
);
60 m_fontId
= data
.m_fontId
;
63 wxFontRefData(int size
,
68 const wxString
& faceName
,
69 wxFontEncoding encoding
)
71 Init(size
, family
, style
, weight
, underlined
, faceName
, encoding
);
74 virtual ~wxFontRefData();
77 // common part of all ctors
83 const wxString
& faceName
,
84 wxFontEncoding encoding
);
86 // If TRUE, the pointer to the actual font is temporary and SHOULD NOT BE
87 // DELETED by destructor
92 // font characterstics
99 wxFontEncoding m_encoding
;
101 // Windows font handle
105 // ============================================================================
107 // ============================================================================
109 // ----------------------------------------------------------------------------
111 // ----------------------------------------------------------------------------
113 void wxFontRefData::Init(int pointSize
,
118 const wxString
& faceName
,
119 wxFontEncoding encoding
)
122 m_pointSize
= pointSize
;
126 m_underlined
= underlined
;
127 m_faceName
= faceName
;
128 m_encoding
= encoding
;
136 wxFontRefData::~wxFontRefData()
141 // if ( !::DeleteObject((HFONT) m_hFont) )
143 // wxLogLastError("DeleteObject(font)");
148 // ----------------------------------------------------------------------------
150 // ----------------------------------------------------------------------------
155 wxTheFontList
->Append(this);
158 /* Constructor for a font. Note that the real construction is done
159 * in wxDC::SetFont, when information is available about scaling etc.
161 bool wxFont::Create(int pointSize
,
166 const wxString
& faceName
,
167 wxFontEncoding encoding
)
170 m_refData
= new wxFontRefData(pointSize
, family
, style
, weight
,
171 underlined
, faceName
, encoding
);
181 wxTheFontList
->DeleteObject(this);
184 // ----------------------------------------------------------------------------
185 // real implementation
186 // ----------------------------------------------------------------------------
188 bool wxFont::RealizeResource()
190 if ( GetResourceHandle() )
192 // VZ: the old code returned FALSE in this case, but it doesn't seem
193 // to make sense because the font _was_ created
194 wxLogDebug(wxT("Calling wxFont::RealizeResource() twice"));
202 // OS/2 combines the family with styles to give a facename
204 switch ( M_FONTDATA
->m_family
)
207 // ff_family = FF_SCRIPT ;
208 ff_face
= wxT("Script") ;
212 // ff_family = FF_DECORATIVE;
216 // ff_family = FF_ROMAN;
217 ff_face
= wxT("Times New Roman") ;
222 // ff_family = FF_MODERN;
223 ff_face
= wxT("Courier New") ;
227 // ff_family = FF_SWISS;
228 ff_face
= wxT("Arial") ;
233 // ff_family = FF_SWISS;
234 ff_face
= wxT("Arial") ;
238 switch ( M_FONTDATA
->m_style
)
246 wxFAIL_MSG(wxT("unknown font slant"));
254 switch ( M_FONTDATA
->m_weight
)
257 wxFAIL_MSG(wxT("unknown font weight"));
261 // ff_weight = FW_NORMAL;
265 // ff_weight = FW_LIGHT;
269 // ff_weight = FW_BOLD;
273 const wxChar
* pzFace
;
274 if ( M_FONTDATA
->m_faceName
.IsEmpty() )
277 pzFace
= M_FONTDATA
->m_faceName
;
280 /* Always calculate fonts using the screen DC (is this the best strategy?)
281 * There may be confusion if a font is selected into a printer
282 * DC (say), because the height will be calculated very differently.
284 // What sort of display is it?
285 int technology
= ::GetDeviceCaps(dc
, TECHNOLOGY
);
289 if (technology
!= DT_RASDISPLAY
&& technology
!= DT_RASPRINTER
)
291 // Have to get screen DC Caps, because a metafile will return 0.
292 HDC dc2
= ::GetDC(NULL
);
293 nHeight
= M_FONTDATA
->m_pointSize
*GetDeviceCaps(dc2
, LOGPIXELSY
)/72;
294 ::ReleaseDC(NULL
, dc2
);
298 nHeight
= M_FONTDATA
->m_pointSize
*GetDeviceCaps(dc
, LOGPIXELSY
)/72;
303 // Have to get screen DC Caps, because a metafile will return 0.
304 HDC dc2
= ::GetDC(NULL
);
305 ppInch
= ::GetDeviceCaps(dc2
, LOGPIXELSY
);
306 ::ReleaseDC(NULL
, dc2
);
309 // New behaviour: apparently ppInch varies according to Large/Small Fonts
310 // setting in Windows. This messes up fonts. So, set ppInch to a constant
312 static const int ppInch
= 96;
314 #if wxFONT_SIZE_COMPATIBILITY
315 // Incorrect, but compatible with old wxWindows behaviour
316 int nHeight
= (M_FONTDATA
->m_pointSize
*ppInch
/72);
318 // Correct for Windows compatibility
319 int nHeight
= - (M_FONTDATA
->m_pointSize
*ppInch
/72);
322 BYTE ff_underline
= M_FONTDATA
->m_underlined
;
324 wxFontEncoding encoding
= M_FONTDATA
->m_encoding
;
325 if ( encoding
== wxFONTENCODING_DEFAULT
)
327 encoding
= wxFont::GetDefaultEncoding();
333 case wxFONTENCODING_ISO8859_1
:
334 case wxFONTENCODING_ISO8859_15
:
335 case wxFONTENCODING_CP1250
:
336 // charset = ANSI_CHARSET;
339 case wxFONTENCODING_ISO8859_2
:
340 case wxFONTENCODING_CP1252
:
341 // charset = EASTEUROPE_CHARSET;
344 case wxFONTENCODING_ISO8859_4
:
345 case wxFONTENCODING_ISO8859_10
:
346 // charset = BALTIC_CHARSET;
349 case wxFONTENCODING_ISO8859_5
:
350 case wxFONTENCODING_CP1251
:
351 // charset = RUSSIAN_CHARSET;
354 case wxFONTENCODING_ISO8859_6
:
355 // charset = ARABIC_CHARSET;
358 case wxFONTENCODING_ISO8859_7
:
359 // charset = GREEK_CHARSET;
362 case wxFONTENCODING_ISO8859_8
:
363 // charset = HEBREW_CHARSET;
366 case wxFONTENCODING_ISO8859_9
:
367 // charset = TURKISH_CHARSET;
370 case wxFONTENCODING_ISO8859_11
:
371 // charset = THAI_CHARSET;
374 case wxFONTENCODING_CP437
:
375 // charset = OEM_CHARSET;
379 wxFAIL_MSG(wxT("unsupported encoding"));
382 case wxFONTENCODING_SYSTEM
:
383 // charset = ANSI_CHARSET;
389 // HFONT hFont = ::CreateFont
391 // nHeight, // height
392 // 0, // width (choose best)
395 // ff_weight, // weight
396 // ff_italic, // italic?
397 // ff_underline, // underlined?
399 // charset, // charset
400 // OUT_DEFAULT_PRECIS, // precision
401 // CLIP_DEFAULT_PRECIS, // clip precision
402 // PROOF_QUALITY, // quality of match
403 // DEFAULT_PITCH | // fixed or variable
404 // ff_family, // family id
405 // pzFace // face name
408 M_FONTDATA
->m_hFont
= (WXHFONT
)hFont
;
411 wxLogLastError("CreateFont");
417 bool wxFont::FreeResource(bool force
)
419 if ( GetResourceHandle() )
422 // if ( !::DeleteObject((HFONT) M_FONTDATA->m_hFont) )
424 // wxLogLastError("DeleteObject(font)");
427 M_FONTDATA
->m_hFont
= 0;
434 WXHANDLE
wxFont::GetResourceHandle()
439 return (WXHANDLE
)M_FONTDATA
->m_hFont
;
442 bool wxFont::IsFree() const
444 return (M_FONTDATA
&& (M_FONTDATA
->m_hFont
== 0));
447 void wxFont::Unshare()
449 // Don't change shared data
452 m_refData
= new wxFontRefData();
456 wxFontRefData
* ref
= new wxFontRefData(*M_FONTDATA
);
462 // ----------------------------------------------------------------------------
463 // change font attribute: we recreate font when doing it
464 // ----------------------------------------------------------------------------
466 void wxFont::SetPointSize(int pointSize
)
470 M_FONTDATA
->m_pointSize
= pointSize
;
475 void wxFont::SetFamily(int family
)
479 M_FONTDATA
->m_family
= family
;
484 void wxFont::SetStyle(int style
)
488 M_FONTDATA
->m_style
= style
;
493 void wxFont::SetWeight(int weight
)
497 M_FONTDATA
->m_weight
= weight
;
502 void wxFont::SetFaceName(const wxString
& faceName
)
506 M_FONTDATA
->m_faceName
= faceName
;
511 void wxFont::SetUnderlined(bool underlined
)
515 M_FONTDATA
->m_underlined
= underlined
;
520 void wxFont::SetEncoding(wxFontEncoding encoding
)
524 M_FONTDATA
->m_encoding
= encoding
;
529 // ----------------------------------------------------------------------------
531 // ----------------------------------------------------------------------------
533 int wxFont::GetPointSize() const
535 return M_FONTDATA
->m_pointSize
;
538 int wxFont::GetFamily() const
540 return M_FONTDATA
->m_family
;
543 int wxFont::GetFontId() const
545 return M_FONTDATA
->m_fontId
;
548 int wxFont::GetStyle() const
550 return M_FONTDATA
->m_style
;
553 int wxFont::GetWeight() const
555 return M_FONTDATA
->m_weight
;
558 bool wxFont::GetUnderlined() const
560 return M_FONTDATA
->m_underlined
;
563 wxString
wxFont::GetFaceName() const
567 str
= M_FONTDATA
->m_faceName
;
571 wxFontEncoding
wxFont::GetEncoding() const
573 return M_FONTDATA
->m_encoding
;