]>
git.saurik.com Git - wxWidgets.git/blob - src/msw/font.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxFont class
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "font.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
32 #include "wx/msw/private.h"
35 #if !USE_SHARED_LIBRARIES
36 IMPLEMENT_DYNAMIC_CLASS(wxFont
, wxGDIObject
)
38 #if wxUSE_PORTABLE_FONTS_IN_MSW
39 IMPLEMENT_DYNAMIC_CLASS(wxFontNameDirectory
, wxObject
)
44 wxFontRefData::wxFontRefData(void)
58 wxFontRefData::wxFontRefData(const wxFontRefData
& data
)
60 m_style
= data
.m_style
;
62 m_pointSize
= data
.m_pointSize
;
63 m_family
= data
.m_family
;
64 m_fontId
= data
.m_fontId
;
65 m_style
= data
.m_style
;
66 m_weight
= data
.m_weight
;
67 m_underlined
= data
.m_underlined
;
68 m_faceName
= data
.m_faceName
;
72 wxFontRefData::~wxFontRefData(void)
75 ::DeleteObject((HFONT
) m_hFont
);
81 wxTheFontList
->Append(this);
84 /* Constructor for a font. Note that the real construction is done
85 * in wxDC::SetFont, when information is available about scaling etc.
87 wxFont::wxFont(int pointSize
, int family
, int style
, int weight
, bool underlined
, const wxString
& faceName
)
89 Create(pointSize
, family
, style
, weight
, underlined
, faceName
);
92 wxTheFontList
->Append(this);
95 bool wxFont::Create(int pointSize
, int family
, int style
, int weight
, bool underlined
, const wxString
& faceName
)
98 m_refData
= new wxFontRefData
;
100 M_FONTDATA
->m_family
= family
;
101 M_FONTDATA
->m_style
= style
;
102 M_FONTDATA
->m_weight
= weight
;
103 M_FONTDATA
->m_pointSize
= pointSize
;
104 M_FONTDATA
->m_underlined
= underlined
;
105 M_FONTDATA
->m_faceName
= faceName
;
115 wxTheFontList
->DeleteObject(this);
118 bool wxFont::RealizeResource(void)
120 if (M_FONTDATA
&& !M_FONTDATA
->m_hFont
)
125 wxString
ff_face("");
127 switch (M_FONTDATA
->m_family
)
129 case wxSCRIPT
: ff_family
= FF_SCRIPT
;
132 case wxDECORATIVE
: ff_family
= FF_DECORATIVE
;
134 case wxROMAN
: ff_family
= FF_ROMAN
;
135 ff_face
= "Times New Roman" ;
138 case wxMODERN
: ff_family
= FF_MODERN
;
139 ff_face
= "Courier New" ;
141 case wxSWISS
: ff_family
= FF_SWISS
;
145 default: ff_family
= FF_SWISS
;
149 if (M_FONTDATA
->m_style
== wxITALIC
|| M_FONTDATA
->m_style
== wxSLANT
)
154 if (M_FONTDATA
->m_weight
== wxNORMAL
)
155 ff_weight
= FW_NORMAL
;
156 else if (M_FONTDATA
->m_weight
== wxLIGHT
)
157 ff_weight
= FW_LIGHT
;
158 else if (M_FONTDATA
->m_weight
== wxBOLD
)
161 const wxChar
* pzFace
= (const wxChar
*) ff_face
;
162 if (!M_FONTDATA
->m_faceName
.IsNull())
163 pzFace
= (const wxChar
*) M_FONTDATA
->m_faceName
;
165 /* Always calculate fonts using the screen DC (is this the best strategy?)
166 * There may be confusion if a font is selected into a printer
167 * DC (say), because the height will be calculated very differently.
168 // What sort of display is it?
169 int technology = ::GetDeviceCaps(dc, TECHNOLOGY);
173 if (technology != DT_RASDISPLAY && technology != DT_RASPRINTER)
175 // Have to get screen DC Caps, because a metafile will return 0.
176 HDC dc2 = ::GetDC(NULL);
177 nHeight = M_FONTDATA->m_pointSize*GetDeviceCaps(dc2, LOGPIXELSY)/72;
178 ::ReleaseDC(NULL, dc2);
182 nHeight = M_FONTDATA->m_pointSize*GetDeviceCaps(dc, LOGPIXELSY)/72;
185 // Have to get screen DC Caps, because a metafile will return 0.
186 HDC dc2
= ::GetDC(NULL
);
187 int ppInch
= ::GetDeviceCaps(dc2
, LOGPIXELSY
);
188 ::ReleaseDC(NULL
, dc2
);
190 // New behaviour: apparently ppInch varies according to
191 // Large/Small Fonts setting in Windows. This messes
192 // up fonts. So, set ppInch to a constant 96 dpi.
195 #if wxFONT_SIZE_COMPATIBILITY
196 // Incorrect, but compatible with old wxWindows behaviour
197 int nHeight
= (M_FONTDATA
->m_pointSize
*ppInch
/72);
199 // Correct for Windows compatibility
200 int nHeight
= - (M_FONTDATA
->m_pointSize
*ppInch
/72);
203 bool ff_underline
= M_FONTDATA
->m_underlined
;
205 M_FONTDATA
->m_hFont
= (WXHFONT
) CreateFont(nHeight
, 0, 0, 0,ff_weight
,ff_italic
,(BYTE
)ff_underline
,
206 0, ANSI_CHARSET
, OUT_DEFAULT_PRECIS
, CLIP_DEFAULT_PRECIS
,
207 PROOF_QUALITY
, DEFAULT_PITCH
| ff_family
, pzFace
);
208 #ifdef WXDEBUG_CREATE
209 if (m_hFont
==NULL
) wxError("Cannot create font","Internal Error") ;
211 return (M_FONTDATA
->m_hFont
!= (WXHFONT
) NULL
);
216 bool wxFont::FreeResource(bool force
)
218 if (M_FONTDATA
&& M_FONTDATA
->m_hFont
)
220 ::DeleteObject((HFONT
) M_FONTDATA
->m_hFont
);
221 M_FONTDATA
->m_hFont
= 0;
227 WXHANDLE
wxFont::GetResourceHandle()
232 return (WXHANDLE
)M_FONTDATA
->m_hFont
;
235 bool wxFont::IsFree() const
237 return (M_FONTDATA
&& (M_FONTDATA
->m_hFont
== 0));
240 void wxFont::Unshare()
242 // Don't change shared data
245 m_refData
= new wxFontRefData();
249 wxFontRefData
* ref
= new wxFontRefData(*(wxFontRefData
*)m_refData
);
255 void wxFont::SetPointSize(int pointSize
)
259 M_FONTDATA
->m_pointSize
= pointSize
;
264 void wxFont::SetFamily(int family
)
268 M_FONTDATA
->m_family
= family
;
273 void wxFont::SetStyle(int style
)
277 M_FONTDATA
->m_style
= style
;
282 void wxFont::SetWeight(int weight
)
286 M_FONTDATA
->m_weight
= weight
;
291 void wxFont::SetFaceName(const wxString
& faceName
)
295 M_FONTDATA
->m_faceName
= faceName
;
300 void wxFont::SetUnderlined(bool underlined
)
304 M_FONTDATA
->m_underlined
= underlined
;
309 wxString
wxFont::GetFamilyString(void) const
315 fam
= "wxDECORATIVE";
339 wxString
wxFont::GetFaceName(void) const
343 str
= M_FONTDATA
->m_faceName
;
347 wxString
wxFont::GetStyleString(void) const
365 wxString
wxFont::GetWeightString(void) const