]>
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 USE_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 #if defined(__X__) || (defined(__WXMSW__) && USE_PORTABLE_FONTS_IN_MSW)
162 ff_face
= wxTheFontNameDirectory
.GetScreenName(M_FONTDATA
->m_family
, M_FONTDATA
->m_weight
, M_FONTDATA
->m_style
);
164 ff_face
= M_FONTDATA
->m_faceName
;
165 if ( ff_face
.IsNull() )
169 /* Always calculate fonts using the screen DC (is this the best strategy?)
170 * There may be confusion if a font is selected into a printer
171 * DC (say), because the height will be calculated very differently.
172 // What sort of display is it?
173 int technology = ::GetDeviceCaps(dc, TECHNOLOGY);
177 if (technology != DT_RASDISPLAY && technology != DT_RASPRINTER)
179 // Have to get screen DC Caps, because a metafile will return 0.
180 HDC dc2 = ::GetDC(NULL);
181 nHeight = M_FONTDATA->m_pointSize*GetDeviceCaps(dc2, LOGPIXELSY)/72;
182 ::ReleaseDC(NULL, dc2);
186 nHeight = M_FONTDATA->m_pointSize*GetDeviceCaps(dc, LOGPIXELSY)/72;
189 // Have to get screen DC Caps, because a metafile will return 0.
190 HDC dc2
= ::GetDC(NULL
);
191 int ppInch
= ::GetDeviceCaps(dc2
, LOGPIXELSY
);
192 ::ReleaseDC(NULL
, dc2
);
194 // New behaviour: apparently ppInch varies according to
195 // Large/Small Fonts setting in Windows. This messes
196 // up fonts. So, set ppInch to a constant 96 dpi.
199 #if FONT_SIZE_COMPATIBILITY
200 // Incorrect, but compatible with old wxWindows behaviour
201 int nHeight
= (M_FONTDATA
->m_pointSize
*ppInch
/72);
203 // Correct for Windows compatibility
204 int nHeight
= - (M_FONTDATA
->m_pointSize
*ppInch
/72);
207 bool ff_underline
= M_FONTDATA
->m_underlined
;
209 M_FONTDATA
->m_hFont
= (WXHFONT
) CreateFont(nHeight
, 0, 0, 0,ff_weight
,ff_italic
,(BYTE
)ff_underline
,
210 0, ANSI_CHARSET
, OUT_DEFAULT_PRECIS
, CLIP_DEFAULT_PRECIS
,
211 PROOF_QUALITY
, DEFAULT_PITCH
| ff_family
, (ff_face
== "" ? NULL
: (const char *)ff_face
));
212 #ifdef WXDEBUG_CREATE
213 if (m_hFont
==NULL
) wxError("Cannot create font","Internal Error") ;
215 return (M_FONTDATA
->m_hFont
!= (WXHFONT
) NULL
);
220 bool wxFont::FreeResource(bool force
)
222 if (M_FONTDATA
&& M_FONTDATA
->m_hFont
)
224 ::DeleteObject((HFONT
) M_FONTDATA
->m_hFont
);
225 M_FONTDATA
->m_hFont
= 0;
231 WXHANDLE
wxFont::GetResourceHandle()
236 return (WXHANDLE
)M_FONTDATA
->m_hFont
;
239 bool wxFont::IsFree()
241 return (M_FONTDATA
&& (M_FONTDATA
->m_hFont
== 0));
244 void wxFont::Unshare()
246 // Don't change shared data
249 m_refData
= new wxFontRefData();
253 wxFontRefData
* ref
= new wxFontRefData(*(wxFontRefData
*)m_refData
);
259 void wxFont::SetPointSize(int pointSize
)
263 M_FONTDATA
->m_pointSize
= pointSize
;
268 void wxFont::SetFamily(int family
)
272 M_FONTDATA
->m_family
= family
;
277 void wxFont::SetStyle(int style
)
281 M_FONTDATA
->m_style
= style
;
286 void wxFont::SetWeight(int weight
)
290 M_FONTDATA
->m_weight
= weight
;
295 void wxFont::SetFaceName(const wxString
& faceName
)
299 M_FONTDATA
->m_faceName
= faceName
;
304 void wxFont::SetUnderlined(bool underlined
)
308 M_FONTDATA
->m_underlined
= underlined
;
313 wxString
wxFont::GetFamilyString(void) const
319 fam
= "wxDECORATIVE";
343 wxString
wxFont::GetFaceName(void) const
347 str
= M_FONTDATA
->m_faceName
;
351 wxString
wxFont::GetStyleString(void) const
369 wxString
wxFont::GetWeightString(void) const