1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxFont class
4 // Author: David Webster
8 // Copyright: (c) David Webster
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11 #define DEBUG_PRINTF(NAME) { static int raz=0; \
12 printf( #NAME " %i\n",raz); fflush(stdout); \
23 if (_HEAPOK
!= (rc
= _heapchk()))
28 puts("The heap has not been initialized.");
31 puts("A memory node is corrupted or the heap is damaged.");
34 puts("The heap specified is not valid.");
42 // ============================================================================
44 // ============================================================================
46 // ----------------------------------------------------------------------------
48 // ----------------------------------------------------------------------------
51 // For compilers that support precompilation, includes "wx.h".
52 #include "wx/wxprec.h"
63 #include "wx/os2/private.h"
65 IMPLEMENT_DYNAMIC_CLASS(wxFont
, wxGDIObject
)
67 #if wxUSE_PORTABLE_FONTS_IN_MSW
68 IMPLEMENT_DYNAMIC_CLASS(wxFontNameDirectory
, wxObject
)
71 // ----------------------------------------------------------------------------
72 // wxFontRefData - the internal description of the font
73 // ----------------------------------------------------------------------------
75 class WXDLLEXPORT wxFontRefData
: public wxGDIRefData
77 friend class WXDLLEXPORT wxFont
;
82 Init(12, wxDEFAULT
, wxNORMAL
, wxNORMAL
, FALSE
,
83 "", wxFONTENCODING_DEFAULT
);
86 wxFontRefData(const wxFontRefData
& data
)
88 Init(data
.m_pointSize
, data
.m_family
, data
.m_style
, data
.m_weight
,
89 data
.m_underlined
, data
.m_faceName
, data
.m_encoding
);
91 m_fontId
= data
.m_fontId
;
94 wxFontRefData(int size
,
99 const wxString
& faceName
,
100 wxFontEncoding encoding
)
102 Init(size
, family
, style
, weight
, underlined
, faceName
, encoding
);
105 virtual ~wxFontRefData();
108 // common part of all ctors
114 const wxString
& faceName
,
115 wxFontEncoding encoding
);
117 // If TRUE, the pointer to the actual font is temporary and SHOULD NOT BE
118 // DELETED by destructor
123 // font characterstics
130 wxFontEncoding m_encoding
;
132 // Windows font handle
136 // ============================================================================
138 // ============================================================================
140 // ----------------------------------------------------------------------------
142 // ----------------------------------------------------------------------------
144 void wxFontRefData::Init(int pointSize
,
149 const wxString
& faceName
,
150 wxFontEncoding encoding
)
153 m_pointSize
= pointSize
;
157 m_underlined
= underlined
;
158 m_faceName
= faceName
;
159 m_encoding
= encoding
;
167 wxFontRefData::~wxFontRefData()
169 DEBUG_PRINTF(wxFontRefData::~wxFontRefData
!!!)
173 // if ( !::DeleteObject((HFONT) m_hFont) )
175 // wxLogLastError("DeleteObject(font)");
180 // ----------------------------------------------------------------------------
182 // ----------------------------------------------------------------------------
187 DEBUG_PRINTF(wxFontRefData::~wxFontRefData
!!!)
190 wxTheFontList
->Append(this);
193 /* Constructor for a font. Note that the real construction is done
194 * in wxDC::SetFont, when information is available about scaling etc.
196 bool wxFont::Create(int pointSize
,
201 const wxString
& faceName
,
202 wxFontEncoding encoding
)
205 DEBUG_PRINTF(wxFontRefData::~wxFontRefData
!!!)
206 m_refData
= new wxFontRefData(pointSize
, family
, style
, weight
,
207 underlined
, faceName
, encoding
);
222 wxTheFontList
->DeleteObject(this);
228 // ----------------------------------------------------------------------------
229 // real implementation
230 // Boris' Kovalenko comments:
231 // Because OS/2 fonts are associated with PS we can not create the font
232 // here, but we may check that font definition is true
233 // ----------------------------------------------------------------------------
235 bool wxFont::RealizeResource()
237 DEBUG_PRINTF(wxFont::RealizeResource
)
238 if ( GetResourceHandle() )
240 // VZ: the old code returned FALSE in this case, but it doesn't seem
241 // to make sense because the font _was_ created
242 wxLogDebug(wxT("Calling wxFont::RealizeResource() twice"));
252 fAttrs
.usRecordLength
= sizeof(FATTRS
);
253 fAttrs
.fsFontUse
= FATTR_FONTUSE_OUTLINE
| // only outline fonts allowed
254 FATTR_FONTUSE_TRANSFORMABLE
; // may be transformed
256 fAttrs
.lMaxBaselineExt
= fAttrs
.lAveCharWidth
= 0;
257 fAttrs
.idRegistry
= 0;
260 fName
.usSize
= sizeof(FACENAMEDESC
);
261 fName
.usWidthClass
= FWIDTH_NORMAL
;
262 fName
.usReserved
= 0;
267 // OS/2 combines the family with styles to give a facename
269 switch ( M_FONTDATA
->m_family
)
274 ff_face
= wxT("Times New Roman") ;
279 ff_face
= wxT("Courier") ;
285 ff_face
= wxT("Helvetica") ;
288 switch ( M_FONTDATA
->m_style
)
292 fAttrs
.fsSelection
= FATTR_SEL_ITALIC
;
296 wxFAIL_MSG(wxT("unknown font slant"));
300 fAttrs
.fsSelection
= 0;
303 switch ( M_FONTDATA
->m_weight
)
306 wxFAIL_MSG(wxT("unknown font weight"));
310 fName
.usWeightClass
= FWEIGHT_NORMAL
;
314 fName
.usWeightClass
= FWEIGHT_LIGHT
;
318 fName
.usWeightClass
= FWEIGHT_BOLD
;
322 if( M_FONTDATA
->m_underlined
)
323 fAttrs
.fsSelection
|= FATTR_SEL_UNDERSCORE
;
325 wxFontEncoding encoding
= M_FONTDATA
->m_encoding
;
326 if ( encoding
== wxFONTENCODING_DEFAULT
)
328 encoding
= wxFont::GetDefaultEncoding();
333 case wxFONTENCODING_ISO8859_1
:
334 case wxFONTENCODING_ISO8859_15
:
335 case wxFONTENCODING_CP1250
:
336 fAttrs
.usCodePage
= 1250;
339 case wxFONTENCODING_ISO8859_2
:
340 case wxFONTENCODING_CP1252
:
341 fAttrs
.usCodePage
= 1252;
344 case wxFONTENCODING_ISO8859_4
:
345 case wxFONTENCODING_ISO8859_10
:
346 fAttrs
.usCodePage
= 850; // what is baltic?
349 case wxFONTENCODING_ISO8859_5
:
350 case wxFONTENCODING_CP1251
:
351 fAttrs
.usCodePage
= 1251;
354 case wxFONTENCODING_ISO8859_6
:
355 fAttrs
.usCodePage
= 850; // what is arabic?
358 case wxFONTENCODING_ISO8859_7
:
359 fAttrs
.usCodePage
= 850; // what is greek
362 case wxFONTENCODING_ISO8859_8
:
363 fAttrs
.usCodePage
= 850; // what is hebrew?
366 case wxFONTENCODING_ISO8859_9
:
367 fAttrs
.usCodePage
= 857;
370 case wxFONTENCODING_ISO8859_11
:
371 fAttrs
.usCodePage
= 850; // what is thai
374 case wxFONTENCODING_CP437
:
375 fAttrs
.usCodePage
= 437;
379 wxFAIL_MSG(wxT("unsupported encoding"));
382 case wxFONTENCODING_SYSTEM
:
383 fAttrs
.usCodePage
= 850; // what is ANSI?
389 hps
= ::WinGetPS( HWND_DESKTOP
);
391 long numLids
= ::GpiQueryNumberSetIds( hps
);
394 // First we should generate unique id
401 if( !GpiQuerySetIds(hps
, numLids
, Types
, Names
, lIds
) )
403 ::WinReleasePS( hps
);
407 for(unsigned long LCNum
= 0; LCNum
< numLids
; LCNum
++)
408 if(lIds
[LCNum
] == fLid
)
410 if(fLid
> 254) // wow, no id available!
412 ::WinReleasePS( hps
);
417 // now building facestring
418 if(::GpiQueryFaceString(hps
, ff_face
.c_str(), &fName
, FACESIZE
, fAttrs
.szFacename
) == GPI_ERROR
)
420 ::WinReleasePS( hps
);
425 WXHFONT hFont
= (WXHFONT
)0;
427 if(::GpiCreateLogFont(hps
, NULL
, fLid
, &fAttrs
) != GPI_ERROR
)
428 M_FONTDATA
->m_hFont
= hFont
= (WXHFONT
)1;
431 ::GpiDeleteSetId(hps
, fLid
);
433 ::WinReleasePS( hps
);
437 wxLogLastError("CreateFont");
443 bool wxFont::FreeResource(bool force
)
445 if ( GetResourceHandle() )
448 // if ( !::DeleteObject((HFONT) M_FONTDATA->m_hFont) )
450 // wxLogLastError("DeleteObject(font)");
453 M_FONTDATA
->m_hFont
= 0;
460 WXHANDLE
wxFont::GetResourceHandle()
465 return (WXHANDLE
)M_FONTDATA
->m_hFont
;
468 bool wxFont::IsFree() const
470 return (M_FONTDATA
&& (M_FONTDATA
->m_hFont
== 0));
473 void wxFont::Unshare()
475 // Don't change shared data
478 m_refData
= new wxFontRefData();
482 wxFontRefData
* ref
= new wxFontRefData(*M_FONTDATA
);
488 // ----------------------------------------------------------------------------
489 // change font attribute: we recreate font when doing it
490 // ----------------------------------------------------------------------------
492 void wxFont::SetPointSize(int pointSize
)
496 M_FONTDATA
->m_pointSize
= pointSize
;
501 void wxFont::SetFamily(int family
)
505 M_FONTDATA
->m_family
= family
;
510 void wxFont::SetStyle(int style
)
514 M_FONTDATA
->m_style
= style
;
519 void wxFont::SetWeight(int weight
)
523 M_FONTDATA
->m_weight
= weight
;
528 void wxFont::SetFaceName(const wxString
& faceName
)
532 M_FONTDATA
->m_faceName
= faceName
;
537 void wxFont::SetUnderlined(bool underlined
)
541 M_FONTDATA
->m_underlined
= underlined
;
546 void wxFont::SetEncoding(wxFontEncoding encoding
)
550 M_FONTDATA
->m_encoding
= encoding
;
555 // ----------------------------------------------------------------------------
557 // ----------------------------------------------------------------------------
559 int wxFont::GetPointSize() const
561 DEBUG_PRINTF(wxFont::GetPointSize
)
566 return pTmp
->m_pointSize
;
571 int wxFont::GetFamily() const
573 return M_FONTDATA
->m_family
;
576 int wxFont::GetFontId() const
578 return M_FONTDATA
->m_fontId
;
581 int wxFont::GetStyle() const
583 return M_FONTDATA
->m_style
;
586 int wxFont::GetWeight() const
588 return M_FONTDATA
->m_weight
;
591 bool wxFont::GetUnderlined() const
593 return M_FONTDATA
->m_underlined
;
596 wxString
wxFont::GetFaceName() const
600 str
= M_FONTDATA
->m_faceName
;
604 wxFontEncoding
wxFont::GetEncoding() const
606 return M_FONTDATA
->m_encoding
;