]>
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(void)
61 ::DeleteObject((HFONT
) m_hFont
);
67 wxTheFontList
->Append(this);
70 /* Constructor for a font. Note that the real construction is done
71 * in wxDC::SetFont, when information is available about scaling etc.
73 wxFont::wxFont(int PointSize
, int Family
, int Style
, int Weight
, bool Underlined
, const wxString
& Face
)
75 Create(PointSize
, Family
, Style
, Weight
, Underlined
, Face
);
78 wxTheFontList
->Append(this);
81 bool wxFont::Create(int PointSize
, int Family
, int Style
, int Weight
, bool Underlined
, const wxString
& Face
)
84 m_refData
= new wxFontRefData
;
86 M_FONTDATA
->m_family
= Family
;
87 M_FONTDATA
->m_style
= Style
;
88 M_FONTDATA
->m_weight
= Weight
;
89 M_FONTDATA
->m_pointSize
= PointSize
;
90 M_FONTDATA
->m_underlined
= Underlined
;
91 M_FONTDATA
->m_faceName
= Face
;
92 M_FONTDATA
->m_temporary
= FALSE
;
93 M_FONTDATA
->m_hFont
= 0;
103 wxTheFontList
->DeleteObject(this);
106 bool wxFont::RealizeResource(void)
108 if (M_FONTDATA
&& !M_FONTDATA
->m_hFont
)
113 wxString
ff_face("");
115 switch (M_FONTDATA
->m_family
)
117 case wxSCRIPT
: ff_family
= FF_SCRIPT
;
120 case wxDECORATIVE
: ff_family
= FF_DECORATIVE
;
122 case wxROMAN
: ff_family
= FF_ROMAN
;
123 ff_face
= "Times New Roman" ;
126 case wxMODERN
: ff_family
= FF_MODERN
;
127 ff_face
= "Courier New" ;
129 case wxSWISS
: ff_family
= FF_SWISS
;
133 default: ff_family
= FF_SWISS
;
137 if (M_FONTDATA
->m_style
== wxITALIC
|| M_FONTDATA
->m_style
== wxSLANT
)
142 if (M_FONTDATA
->m_weight
== wxNORMAL
)
143 ff_weight
= FW_NORMAL
;
144 else if (M_FONTDATA
->m_weight
== wxLIGHT
)
145 ff_weight
= FW_LIGHT
;
146 else if (M_FONTDATA
->m_weight
== wxBOLD
)
149 #if defined(__X__) || (defined(__WINDOWS__) && USE_PORTABLE_FONTS_IN_MSW)
150 ff_face
= wxTheFontNameDirectory
.GetScreenName(M_FONTDATA
->m_family
, M_FONTDATA
->m_weight
, M_FONTDATA
->m_style
);
152 ff_face
= M_FONTDATA
->m_faceName
;
153 if ( ff_face
.IsNull() )
157 /* Always calculate fonts using the screen DC (is this the best strategy?)
158 * There may be confusion if a font is selected into a printer
159 * DC (say), because the height will be calculated very differently.
160 // What sort of display is it?
161 int technology = ::GetDeviceCaps(dc, TECHNOLOGY);
165 if (technology != DT_RASDISPLAY && technology != DT_RASPRINTER)
167 // Have to get screen DC Caps, because a metafile will return 0.
168 HDC dc2 = ::GetDC(NULL);
169 nHeight = M_FONTDATA->m_pointSize*GetDeviceCaps(dc2, LOGPIXELSY)/72;
170 ::ReleaseDC(NULL, dc2);
174 nHeight = M_FONTDATA->m_pointSize*GetDeviceCaps(dc, LOGPIXELSY)/72;
177 // Have to get screen DC Caps, because a metafile will return 0.
178 HDC dc2
= ::GetDC(NULL
);
179 int ppInch
= ::GetDeviceCaps(dc2
, LOGPIXELSY
);
180 ::ReleaseDC(NULL
, dc2
);
182 // New behaviour: apparently ppInch varies according to
183 // Large/Small Fonts setting in Windows. This messes
184 // up fonts. So, set ppInch to a constant 96 dpi.
187 #if FONT_SIZE_COMPATIBILITY
188 // Incorrect, but compatible with old wxWindows behaviour
189 int nHeight
= (M_FONTDATA
->m_pointSize
*ppInch
/72);
191 // Correct for Windows compatibility
192 int nHeight
= - (M_FONTDATA
->m_pointSize
*ppInch
/72);
195 bool ff_underline
= M_FONTDATA
->m_underlined
;
197 M_FONTDATA
->m_hFont
= (WXHFONT
) CreateFont(nHeight
, 0, 0, 0,ff_weight
,ff_italic
,(BYTE
)ff_underline
,
198 0, ANSI_CHARSET
, OUT_DEFAULT_PRECIS
, CLIP_DEFAULT_PRECIS
,
199 PROOF_QUALITY
, DEFAULT_PITCH
| ff_family
, (ff_face
== "" ? NULL
: (const char *)ff_face
));
201 if (m_hFont
==NULL
) wxError("Cannot create font","Internal Error") ;
203 return (M_FONTDATA
->m_hFont
!= (WXHFONT
) NULL
);
208 bool wxFont::FreeResource(bool force
)
210 if (M_FONTDATA
&& M_FONTDATA
->m_hFont
)
212 ::DeleteObject((HFONT
) M_FONTDATA
->m_hFont
);
213 M_FONTDATA
->m_hFont
= 0;
220 bool wxFont::UseResource(void)
222 IncrementResourceUsage();
226 bool wxFont::ReleaseResource(void)
228 DecrementResourceUsage();
233 WXHANDLE
wxFont::GetResourceHandle(void)
238 return (WXHANDLE
)M_FONTDATA
->m_hFont
;
241 bool wxFont::IsFree(void)
243 return (M_FONTDATA
&& (M_FONTDATA
->m_hFont
== 0));
246 void wxFont::SetPointSize(int pointSize
)
249 m_refData
= new wxFontRefData
;
250 M_FONTDATA
->m_pointSize
= pointSize
;
253 void wxFont::SetFamily(int family
)
256 m_refData
= new wxFontRefData
;
257 M_FONTDATA
->m_family
= family
;
260 void wxFont::SetStyle(int style
)
263 m_refData
= new wxFontRefData
;
264 M_FONTDATA
->m_style
= style
;
267 void wxFont::SetWeight(int weight
)
270 m_refData
= new wxFontRefData
;
271 M_FONTDATA
->m_weight
= weight
;
274 void wxFont::SetFaceName(const wxString
& faceName
)
277 m_refData
= new wxFontRefData
;
278 M_FONTDATA
->m_faceName
= faceName
;
281 void wxFont::SetUnderlined(bool underlined
)
284 m_refData
= new wxFontRefData
;
285 M_FONTDATA
->m_underlined
= underlined
;
288 wxString
wxFont::GetFamilyString(void) const
294 fam
= "wxDECORATIVE";
318 /* New font system */
319 wxString
wxFont::GetFaceName(void) const
323 str
= M_FONTDATA
->m_faceName
;
327 wxString
wxFont::GetStyleString(void) const
345 wxString
wxFont::GetWeightString(void) const