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 // Boris' Kovalenko comments:
187 // Because OS/2 fonts are associated with PS we can not create the font
188 // here, but we may check that font definition is true
189 // ----------------------------------------------------------------------------
191 bool wxFont::RealizeResource()
193 if ( GetResourceHandle() )
195 // VZ: the old code returned FALSE in this case, but it doesn't seem
196 // to make sense because the font _was_ created
197 wxLogDebug(wxT("Calling wxFont::RealizeResource() twice"));
207 fAttrs
.usRecordLength
= sizeof(FATTRS
);
208 fAttrs
.fsFontUse
= FATTR_FONTUSE_OUTLINE
| // only outline fonts allowed
209 FATTR_FONTUSE_TRANSFORMABLE
; // may be transformed
211 fAttrs
.lMaxBaselineExt
= fAttrs
.lAveCharWidth
= 0;
212 fAttrs
.idRegistry
= 0;
215 fName
.usSize
= sizeof(FACENAMEDESC
);
216 fName
.usWidthClass
= FWIDTH_NORMAL
;
217 fName
.usReserved
= 0;
222 // OS/2 combines the family with styles to give a facename
224 switch ( M_FONTDATA
->m_family
)
229 ff_face
= wxT("Times New Roman") ;
234 ff_face
= wxT("Courier") ;
240 ff_face
= wxT("Helvetica") ;
243 switch ( M_FONTDATA
->m_style
)
247 fAttrs
.fsSelection
= FATTR_SEL_ITALIC
;
251 wxFAIL_MSG(wxT("unknown font slant"));
255 fAttrs
.fsSelection
= 0;
258 switch ( M_FONTDATA
->m_weight
)
261 wxFAIL_MSG(wxT("unknown font weight"));
265 fName
.usWeightClass
= FWEIGHT_NORMAL
;
269 fName
.usWeightClass
= FWEIGHT_LIGHT
;
273 fName
.usWeightClass
= FWEIGHT_BOLD
;
277 if( M_FONTDATA
->m_underlined
)
278 fAttrs
.fsSelection
|= FATTR_SEL_UNDERSCORE
;
280 wxFontEncoding encoding
= M_FONTDATA
->m_encoding
;
281 if ( encoding
== wxFONTENCODING_DEFAULT
)
283 encoding
= wxFont::GetDefaultEncoding();
288 case wxFONTENCODING_ISO8859_1
:
289 case wxFONTENCODING_ISO8859_15
:
290 case wxFONTENCODING_CP1250
:
291 fAttrs
.usCodePage
= 1250;
294 case wxFONTENCODING_ISO8859_2
:
295 case wxFONTENCODING_CP1252
:
296 fAttrs
.usCodePage
= 1252;
299 case wxFONTENCODING_ISO8859_4
:
300 case wxFONTENCODING_ISO8859_10
:
301 fAttrs
.usCodePage
= 850; // what is baltic?
304 case wxFONTENCODING_ISO8859_5
:
305 case wxFONTENCODING_CP1251
:
306 fAttrs
.usCodePage
= 1251;
309 case wxFONTENCODING_ISO8859_6
:
310 fAttrs
.usCodePage
= 850; // what is arabic?
313 case wxFONTENCODING_ISO8859_7
:
314 fAttrs
.usCodePage
= 850; // what is greek
317 case wxFONTENCODING_ISO8859_8
:
318 fAttrs
.usCodePage
= 850; // what is hebrew?
321 case wxFONTENCODING_ISO8859_9
:
322 fAttrs
.usCodePage
= 857;
325 case wxFONTENCODING_ISO8859_11
:
326 fAttrs
.usCodePage
= 850; // what is thai
329 case wxFONTENCODING_CP437
:
330 fAttrs
.usCodePage
= 437;
334 wxFAIL_MSG(wxT("unsupported encoding"));
337 case wxFONTENCODING_SYSTEM
:
338 fAttrs
.usCodePage
= 850; // what is ANSI?
344 hps
= ::WinGetPS( HWND_DESKTOP
);
346 long numLids
= ::GpiQueryNumberSetIds( hps
);
349 // First we should generate unique id
356 if( !GpiQuerySetIds(hps
, numLids
, Types
, Names
, lIds
) )
358 ::WinReleasePS( hps
);
362 for(unsigned long LCNum
= 0; LCNum
< numLids
; LCNum
++)
363 if(lIds
[LCNum
] == fLid
)
365 if(fLid
> 254) // wow, no id available!
367 ::WinReleasePS( hps
);
372 // now building facestring
373 if(::GpiQueryFaceString(hps
, ff_face
.c_str(), &fName
, FACESIZE
, fAttrs
.szFacename
) == GPI_ERROR
)
375 ::WinReleasePS( hps
);
380 WXHFONT hFont
= (WXHFONT
)0;
382 if(::GpiCreateLogFont(hps
, NULL
, fLid
, &fAttrs
) != GPI_ERROR
)
383 M_FONTDATA
->m_hFont
= hFont
= (WXHFONT
)1;
386 ::GpiDeleteSetId(hps
, fLid
);
388 ::WinReleasePS( hps
);
392 wxLogLastError("CreateFont");
398 bool wxFont::FreeResource(bool force
)
400 if ( GetResourceHandle() )
403 // if ( !::DeleteObject((HFONT) M_FONTDATA->m_hFont) )
405 // wxLogLastError("DeleteObject(font)");
408 M_FONTDATA
->m_hFont
= 0;
415 WXHANDLE
wxFont::GetResourceHandle()
420 return (WXHANDLE
)M_FONTDATA
->m_hFont
;
423 bool wxFont::IsFree() const
425 return (M_FONTDATA
&& (M_FONTDATA
->m_hFont
== 0));
428 void wxFont::Unshare()
430 // Don't change shared data
433 m_refData
= new wxFontRefData();
437 wxFontRefData
* ref
= new wxFontRefData(*M_FONTDATA
);
443 // ----------------------------------------------------------------------------
444 // change font attribute: we recreate font when doing it
445 // ----------------------------------------------------------------------------
447 void wxFont::SetPointSize(int pointSize
)
451 M_FONTDATA
->m_pointSize
= pointSize
;
456 void wxFont::SetFamily(int family
)
460 M_FONTDATA
->m_family
= family
;
465 void wxFont::SetStyle(int style
)
469 M_FONTDATA
->m_style
= style
;
474 void wxFont::SetWeight(int weight
)
478 M_FONTDATA
->m_weight
= weight
;
483 void wxFont::SetFaceName(const wxString
& faceName
)
487 M_FONTDATA
->m_faceName
= faceName
;
492 void wxFont::SetUnderlined(bool underlined
)
496 M_FONTDATA
->m_underlined
= underlined
;
501 void wxFont::SetEncoding(wxFontEncoding encoding
)
505 M_FONTDATA
->m_encoding
= encoding
;
510 // ----------------------------------------------------------------------------
512 // ----------------------------------------------------------------------------
514 int wxFont::GetPointSize() const
516 return M_FONTDATA
->m_pointSize
;
519 int wxFont::GetFamily() const
521 return M_FONTDATA
->m_family
;
524 int wxFont::GetFontId() const
526 return M_FONTDATA
->m_fontId
;
529 int wxFont::GetStyle() const
531 return M_FONTDATA
->m_style
;
534 int wxFont::GetWeight() const
536 return M_FONTDATA
->m_weight
;
539 bool wxFont::GetUnderlined() const
541 return M_FONTDATA
->m_underlined
;
544 wxString
wxFont::GetFaceName() const
548 str
= M_FONTDATA
->m_faceName
;
552 wxFontEncoding
wxFont::GetEncoding() const
554 return M_FONTDATA
->m_encoding
;