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 #if !USE_SHARED_LIBRARIES
35 IMPLEMENT_DYNAMIC_CLASS(wxFont
, wxGDIObject
)
37 #if wxUSE_PORTABLE_FONTS_IN_MSW
38 IMPLEMENT_DYNAMIC_CLASS(wxFontNameDirectory
, wxObject
)
42 // ----------------------------------------------------------------------------
43 // wxFontRefData - the internal description of the font
44 // ----------------------------------------------------------------------------
46 class WXDLLEXPORT wxFontRefData
: public wxGDIRefData
48 friend class WXDLLEXPORT wxFont
;
53 Init(12, wxDEFAULT
, wxNORMAL
, wxNORMAL
, FALSE
,
54 "", wxFONTENCODING_DEFAULT
);
57 wxFontRefData(const wxFontRefData
& data
)
59 Init(data
.m_pointSize
, data
.m_family
, data
.m_style
, data
.m_weight
,
60 data
.m_underlined
, data
.m_faceName
, data
.m_encoding
);
62 m_fontId
= data
.m_fontId
;
65 wxFontRefData(int size
,
70 const wxString
& faceName
,
71 wxFontEncoding encoding
)
73 Init(size
, family
, style
, weight
, underlined
, faceName
, encoding
);
76 virtual ~wxFontRefData();
79 // common part of all ctors
85 const wxString
& faceName
,
86 wxFontEncoding encoding
);
88 // If TRUE, the pointer to the actual font is temporary and SHOULD NOT BE
89 // DELETED by destructor
94 // font characterstics
101 wxFontEncoding m_encoding
;
103 // Windows font handle
107 // ============================================================================
109 // ============================================================================
111 // ----------------------------------------------------------------------------
113 // ----------------------------------------------------------------------------
115 void wxFontRefData::Init(int pointSize
,
120 const wxString
& faceName
,
121 wxFontEncoding encoding
)
124 m_pointSize
= pointSize
;
128 m_underlined
= underlined
;
129 m_faceName
= faceName
;
130 m_encoding
= encoding
;
138 wxFontRefData::~wxFontRefData()
143 // if ( !::DeleteObject((HFONT) m_hFont) )
145 // wxLogLastError("DeleteObject(font)");
150 // ----------------------------------------------------------------------------
152 // ----------------------------------------------------------------------------
157 wxTheFontList
->Append(this);
160 /* Constructor for a font. Note that the real construction is done
161 * in wxDC::SetFont, when information is available about scaling etc.
163 bool wxFont::Create(int pointSize
,
168 const wxString
& faceName
,
169 wxFontEncoding encoding
)
172 m_refData
= new wxFontRefData(pointSize
, family
, style
, weight
,
173 underlined
, faceName
, encoding
);
183 wxTheFontList
->DeleteObject(this);
186 // ----------------------------------------------------------------------------
187 // real implementation
188 // ----------------------------------------------------------------------------
190 bool wxFont::RealizeResource()
192 if ( GetResourceHandle() )
194 // VZ: the old code returned FALSE in this case, but it doesn't seem
195 // to make sense because the font _was_ created
196 wxLogDebug(T("Calling wxFont::RealizeResource() twice"));
204 // OS/2 combines the family with styles to give a facename
206 switch ( M_FONTDATA
->m_family
)
209 // ff_family = FF_SCRIPT ;
210 ff_face
= T("Script") ;
214 // ff_family = FF_DECORATIVE;
218 // ff_family = FF_ROMAN;
219 ff_face
= T("Times New Roman") ;
224 // ff_family = FF_MODERN;
225 ff_face
= T("Courier New") ;
229 // ff_family = FF_SWISS;
230 ff_face
= T("Arial") ;
235 // ff_family = FF_SWISS;
236 ff_face
= T("Arial") ;
240 switch ( M_FONTDATA
->m_style
)
248 wxFAIL_MSG(T("unknown font slant"));
256 switch ( M_FONTDATA
->m_weight
)
259 wxFAIL_MSG(T("unknown font weight"));
263 // ff_weight = FW_NORMAL;
267 // ff_weight = FW_LIGHT;
271 // ff_weight = FW_BOLD;
275 const wxChar
* pzFace
;
276 if ( M_FONTDATA
->m_faceName
.IsEmpty() )
279 pzFace
= M_FONTDATA
->m_faceName
;
282 /* Always calculate fonts using the screen DC (is this the best strategy?)
283 * There may be confusion if a font is selected into a printer
284 * DC (say), because the height will be calculated very differently.
286 // What sort of display is it?
287 int technology
= ::GetDeviceCaps(dc
, TECHNOLOGY
);
291 if (technology
!= DT_RASDISPLAY
&& technology
!= DT_RASPRINTER
)
293 // Have to get screen DC Caps, because a metafile will return 0.
294 HDC dc2
= ::GetDC(NULL
);
295 nHeight
= M_FONTDATA
->m_pointSize
*GetDeviceCaps(dc2
, LOGPIXELSY
)/72;
296 ::ReleaseDC(NULL
, dc2
);
300 nHeight
= M_FONTDATA
->m_pointSize
*GetDeviceCaps(dc
, LOGPIXELSY
)/72;
305 // Have to get screen DC Caps, because a metafile will return 0.
306 HDC dc2
= ::GetDC(NULL
);
307 ppInch
= ::GetDeviceCaps(dc2
, LOGPIXELSY
);
308 ::ReleaseDC(NULL
, dc2
);
311 // New behaviour: apparently ppInch varies according to Large/Small Fonts
312 // setting in Windows. This messes up fonts. So, set ppInch to a constant
314 static const int ppInch
= 96;
316 #if wxFONT_SIZE_COMPATIBILITY
317 // Incorrect, but compatible with old wxWindows behaviour
318 int nHeight
= (M_FONTDATA
->m_pointSize
*ppInch
/72);
320 // Correct for Windows compatibility
321 int nHeight
= - (M_FONTDATA
->m_pointSize
*ppInch
/72);
324 BYTE ff_underline
= M_FONTDATA
->m_underlined
;
326 wxFontEncoding encoding
= M_FONTDATA
->m_encoding
;
327 if ( encoding
== wxFONTENCODING_DEFAULT
)
329 encoding
= wxFont::GetDefaultEncoding();
335 case wxFONTENCODING_ISO8859_1
:
336 case wxFONTENCODING_ISO8859_15
:
337 case wxFONTENCODING_CP1250
:
338 // charset = ANSI_CHARSET;
341 case wxFONTENCODING_ISO8859_2
:
342 case wxFONTENCODING_CP1252
:
343 // charset = EASTEUROPE_CHARSET;
346 case wxFONTENCODING_ISO8859_4
:
347 case wxFONTENCODING_ISO8859_10
:
348 // charset = BALTIC_CHARSET;
351 case wxFONTENCODING_ISO8859_5
:
352 case wxFONTENCODING_CP1251
:
353 // charset = RUSSIAN_CHARSET;
356 case wxFONTENCODING_ISO8859_6
:
357 // charset = ARABIC_CHARSET;
360 case wxFONTENCODING_ISO8859_7
:
361 // charset = GREEK_CHARSET;
364 case wxFONTENCODING_ISO8859_8
:
365 // charset = HEBREW_CHARSET;
368 case wxFONTENCODING_ISO8859_9
:
369 // charset = TURKISH_CHARSET;
372 case wxFONTENCODING_ISO8859_11
:
373 // charset = THAI_CHARSET;
376 case wxFONTENCODING_CP437
:
377 // charset = OEM_CHARSET;
381 wxFAIL_MSG(T("unsupported encoding"));
384 case wxFONTENCODING_SYSTEM
:
385 // charset = ANSI_CHARSET;
391 // HFONT hFont = ::CreateFont
393 // nHeight, // height
394 // 0, // width (choose best)
397 // ff_weight, // weight
398 // ff_italic, // italic?
399 // ff_underline, // underlined?
401 // charset, // charset
402 // OUT_DEFAULT_PRECIS, // precision
403 // CLIP_DEFAULT_PRECIS, // clip precision
404 // PROOF_QUALITY, // quality of match
405 // DEFAULT_PITCH | // fixed or variable
406 // ff_family, // family id
407 // pzFace // face name
410 M_FONTDATA
->m_hFont
= (WXHFONT
)hFont
;
413 wxLogLastError("CreateFont");
419 bool wxFont::FreeResource(bool force
)
421 if ( GetResourceHandle() )
424 // if ( !::DeleteObject((HFONT) M_FONTDATA->m_hFont) )
426 // wxLogLastError("DeleteObject(font)");
429 M_FONTDATA
->m_hFont
= 0;
436 WXHANDLE
wxFont::GetResourceHandle()
441 return (WXHANDLE
)M_FONTDATA
->m_hFont
;
444 bool wxFont::IsFree() const
446 return (M_FONTDATA
&& (M_FONTDATA
->m_hFont
== 0));
449 void wxFont::Unshare()
451 // Don't change shared data
454 m_refData
= new wxFontRefData();
458 wxFontRefData
* ref
= new wxFontRefData(*M_FONTDATA
);
464 // ----------------------------------------------------------------------------
465 // change font attribute: we recreate font when doing it
466 // ----------------------------------------------------------------------------
468 void wxFont::SetPointSize(int pointSize
)
472 M_FONTDATA
->m_pointSize
= pointSize
;
477 void wxFont::SetFamily(int family
)
481 M_FONTDATA
->m_family
= family
;
486 void wxFont::SetStyle(int style
)
490 M_FONTDATA
->m_style
= style
;
495 void wxFont::SetWeight(int weight
)
499 M_FONTDATA
->m_weight
= weight
;
504 void wxFont::SetFaceName(const wxString
& faceName
)
508 M_FONTDATA
->m_faceName
= faceName
;
513 void wxFont::SetUnderlined(bool underlined
)
517 M_FONTDATA
->m_underlined
= underlined
;
522 void wxFont::SetEncoding(wxFontEncoding encoding
)
526 M_FONTDATA
->m_encoding
= encoding
;
531 // ----------------------------------------------------------------------------
533 // ----------------------------------------------------------------------------
535 int wxFont::GetPointSize() const
537 return M_FONTDATA
->m_pointSize
;
540 int wxFont::GetFamily() const
542 return M_FONTDATA
->m_family
;
545 int wxFont::GetFontId() const
547 return M_FONTDATA
->m_fontId
;
550 int wxFont::GetStyle() const
552 return M_FONTDATA
->m_style
;
555 int wxFont::GetWeight() const
557 return M_FONTDATA
->m_weight
;
560 bool wxFont::GetUnderlined() const
562 return M_FONTDATA
->m_underlined
;
565 wxString
wxFont::GetFaceName() const
569 str
= M_FONTDATA
->m_faceName
;
573 wxFontEncoding
wxFont::GetEncoding() const
575 return M_FONTDATA
->m_encoding
;