sprint 1-alpha
sprint/gtl/Font.h
Go to the documentation of this file.
00001 /******************************************************************************
00002  *  GTL: GUI Template Library 
00003  *    A Cross Platform C++ Wrapper for Win32 users
00004  *  Copyright (C) 2007-2011 Paolo Medici (www.pmx.it)
00005  *
00006  *  This library is free software; you can redistribute it and/or
00007  *  modify it under the terms of the GNU Lesser General Public
00008  *  License as published by the Free Software Foundation; either
00009  *  version 2.1 of the License, or (at your option) any later version.
00010  *
00011  *  This library is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  *  Lesser General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU Lesser General Public
00017  *  License along with this library; if not, write to the Free Software
00018  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00019  *
00020  *******************************************************************************/
00021 
00022 #ifndef _SPRINT_FONT_H
00023 #define _SPRINT_FONT_H
00024 
00028 #include <sprint/gtl/BaseTypes.h>
00029 
00030 namespace sprint {
00031         namespace gtl {
00032 
00034 class CLogFont : public LOGFONT
00035 {
00036 public:
00037         CLogFont()
00038         {
00039                 memset(this, 0, sizeof(LOGFONT));
00040         }
00041 
00042         CLogFont(const LOGFONT& lf)
00043         {
00044                 Copy(lf);
00045         }
00046 
00047         CLogFont(HFONT hFont)
00048         {
00049                 ::GetObject(hFont, sizeof(LOGFONT), (LOGFONT*)this);
00050         }
00051 
00053         HFONT CreateFontIndirect() const
00054         {
00055                 return ::CreateFontIndirect(this);
00056         }
00057 
00058         void SetBold()
00059         {
00060                 lfWeight = FW_BOLD;
00061         }
00062 
00063         bool IsBold() const
00064         {
00065                 return (lfWeight >= FW_BOLD);
00066         }
00067 
00068         void MakeBolder(int iScale = 1)
00069         {
00070                 lfWeight += FW_BOLD * iScale;
00071         }
00072 
00073         void MakeLarger(int iScale)
00074         {
00075                 if(lfHeight > 0)
00076                         lfHeight += iScale;
00077                 else
00078                         lfHeight -= iScale;
00079         }
00080 
00081         void SetHeight(LONG nPointSize, HDC hDC = NULL)
00082         {
00083                 // For MM_TEXT mapping mode
00084                 lfHeight = -::MulDiv(nPointSize, ::GetDeviceCaps(hDC, LOGPIXELSY), 72);
00085         }
00086 
00087         LONG GetHeight(HDC hDC = NULL) const
00088         {
00089                 // For MM_TEXT mapping mode
00090                 return ::MulDiv(-lfHeight, 72, ::GetDeviceCaps(hDC, LOGPIXELSY));
00091         }
00092 
00093         LONG GetDeciPointHeight(HDC hDC = NULL) const
00094         {
00095 #ifndef _WIN32_WCE
00096                 POINT ptOrg = { 0, 0 };
00097                 ::DPtoLP(hDC, &ptOrg, 1);
00098                 POINT pt = { 0, 0 };
00099                 pt.y = abs(lfHeight) + ptOrg.y;
00100                 ::LPtoDP(hDC,&pt,1);
00101                 return ::MulDiv(pt.y, 720, ::GetDeviceCaps(hDC, LOGPIXELSY));   // 72 points/inch, 10 decipoints/point
00102 #else // CE specific
00103                 // DP and LP are always the same on CE
00104                 return ::MulDiv(abs(lfHeight), 720, ::GetDeviceCaps(hDC, LOGPIXELSY));   // 72 points/inch, 10 decipoints/point
00105 #endif // !_WIN32_WCE
00106         }
00107 
00108         void SetHeightFromDeciPoint(LONG nDeciPtHeight, HDC hDC = NULL)
00109         {
00110 #ifndef _WIN32_WCE
00111                 POINT pt = { 0, 0 };
00112                 pt.y = ::MulDiv(::GetDeviceCaps(hDC, LOGPIXELSY), nDeciPtHeight, 720);   // 72 points/inch, 10 decipoints/point
00113                 ::DPtoLP(hDC, &pt, 1);
00114                 POINT ptOrg = { 0, 0 };
00115                 ::DPtoLP(hDC, &ptOrg, 1);
00116                 lfHeight = -abs(pt.y - ptOrg.y);
00117 #else // CE specific
00118                 // DP and LP are always the same on CE
00119                 lfHeight = -abs(::MulDiv(::GetDeviceCaps(hDC, LOGPIXELSY), nDeciPtHeight, 720));   // 72 points/inch, 10 decipoints/point
00120 #endif // !_WIN32_WCE
00121         }
00122 
00123         void Copy(const LOGFONT & LogFont)
00124         {
00125                 memcpy(this, &LogFont, sizeof(LOGFONT));
00126         }
00127 
00128         CLogFont& operator =(const CLogFont& src)
00129         {
00130                 Copy(src);
00131                 return *this;
00132         }
00133 
00134         CLogFont& operator =(const LOGFONT& src)
00135         {
00136                 Copy(src);
00137                 return *this;
00138         }
00139 
00140         CLogFont& operator =(HFONT hFont)
00141         {
00142                 ::GetObject(hFont, sizeof(LOGFONT), (LOGFONT*)this);
00143                 return *this;
00144         }
00145 
00146         bool operator ==(const LOGFONT& logfont) const
00147         {
00148                 return(logfont.lfHeight == lfHeight &&
00149                        logfont.lfWidth == lfWidth &&
00150                        logfont.lfEscapement == lfEscapement &&
00151                        logfont.lfOrientation == lfOrientation &&
00152                        logfont.lfWeight == lfWeight &&
00153                        logfont.lfItalic == lfItalic &&
00154                        logfont.lfUnderline == lfUnderline &&
00155                        logfont.lfStrikeOut == lfStrikeOut &&
00156                        logfont.lfCharSet == lfCharSet &&
00157                        logfont.lfOutPrecision == lfOutPrecision &&
00158                        logfont.lfClipPrecision == lfClipPrecision &&
00159                        logfont.lfQuality == lfQuality &&
00160                        logfont.lfPitchAndFamily == lfPitchAndFamily &&
00161                        lstrcmp(logfont.lfFaceName, lfFaceName) == 0);
00162         }
00163 };
00164 
00166 template <bool t_bManaged>
00167 class CFontT
00168 {
00169 public:
00170 // Data members
00171         HFONT m_hFont;
00172 
00173 // Constructor/destructor/operators
00174         CFontT(HFONT hFont = NULL) : m_hFont(hFont)
00175         { }
00176         
00177         ~CFontT()
00178         {
00179                 if(t_bManaged && m_hFont != NULL)
00180                         DeleteObject();
00181         }
00182 
00183         CFontT<t_bManaged>& operator =(HFONT hFont)
00184         {
00185                 Attach(hFont);
00186                 return *this;
00187         }
00188         
00189         inline bool IsNull() const { return (m_hFont == NULL); }
00190 
00191         void Attach(HFONT hFont)
00192         {
00193                 if(t_bManaged && m_hFont != NULL && m_hFont != hFont)
00194                         ::DeleteObject(m_hFont);
00195                 m_hFont = hFont;
00196         }
00197 
00198         HFONT Detach()
00199         {
00200                 HFONT hFont = m_hFont;
00201                 m_hFont = NULL;
00202                 return hFont;
00203         }
00204 
00205         inline operator HFONT() const { return m_hFont; }
00206 
00207 
00208 // Create methods
00209         inline HFONT CreateFontIndirect(const LOGFONT & LogFont)
00210         {
00211                 m_hFont = ::CreateFontIndirect(&LogFont);
00212                 return m_hFont;
00213         }
00214 
00215         CFontT(const LOGFONT & LogFont) : m_hFont(NULL)
00216         {
00217                 CreateFontIndirect(LogFont);
00218         }
00219 
00220         inline HFONT CreateFont(int nHeight, int nWidth, int nEscapement,
00221                         int nOrientation, int nWeight, BYTE bItalic, BYTE bUnderline,
00222                         BYTE cStrikeOut, BYTE nCharSet, BYTE nOutPrecision,
00223                         BYTE nClipPrecision, BYTE nQuality, BYTE nPitchAndFamily,
00224                         LPCTSTR lpszFacename)
00225         {
00226                 m_hFont = ::CreateFont(nHeight, nWidth, nEscapement,
00227                         nOrientation, nWeight, bItalic, bUnderline, cStrikeOut,
00228                         nCharSet, nOutPrecision, nClipPrecision, nQuality,
00229                         nPitchAndFamily, lpszFacename);
00230                 return m_hFont;
00231         }
00232 
00233         inline HFONT CreateFont(int nPointSize, LPCTSTR lpszFaceName, bool bBold = false, bool bItalic = false)
00234         {
00235                 LOGFONT logFont = { 0 };
00236                 logFont.lfCharSet = DEFAULT_CHARSET;
00237                 logFont.lfHeight = nPointSize;
00238                 lstrcpyn(logFont.lfFaceName, lpszFaceName, sizeof(logFont.lfFaceName) / sizeof(TCHAR));
00239 
00240                 if(bBold)
00241                         logFont.lfWeight = FW_BOLD;
00242                 if(bItalic)
00243                         logFont.lfItalic = (BYTE)TRUE;
00244                         
00245                 return CreateFontIndirect(logFont);
00246         }
00247 
00248         HFONT CreatePointFont(int nPointSize, LPCTSTR lpszFaceName, HDC hDC = NULL, bool bBold = false, bool bItalic = false)
00249         {
00250                 LOGFONT logFont = { 0 };
00251                 logFont.lfCharSet = DEFAULT_CHARSET;
00252                 logFont.lfHeight = nPointSize;
00253                 lstrcpyn(logFont.lfFaceName, lpszFaceName, sizeof(logFont.lfFaceName) / sizeof(TCHAR));
00254 
00255                 if(bBold)
00256                         logFont.lfWeight = FW_BOLD;
00257                 if(bItalic)
00258                         logFont.lfItalic = (BYTE)TRUE;
00259 
00260                 return CreatePointFontIndirect(logFont, hDC);
00261         }
00262 
00263         HFONT CreatePointFontIndirect(const LOGFONT & LogFont, HDC hDC = NULL)
00264         {
00265                 HDC hDC1 = (hDC != NULL) ? hDC : ::GetDC(NULL);
00266 
00267                 // convert nPointSize to logical units based on hDC
00268                 LOGFONT logFont = LogFont;
00269 #ifndef _WIN32_WCE
00270                 POINT pt = { 0, 0 };
00271                 pt.y = ::MulDiv(::GetDeviceCaps(hDC1, LOGPIXELSY), logFont.lfHeight, 720);   // 72 points/inch, 10 decipoints/point
00272                 ::DPtoLP(hDC1, &pt, 1);
00273                 POINT ptOrg = { 0, 0 };
00274                 ::DPtoLP(hDC1, &ptOrg, 1);
00275                 logFont.lfHeight = -abs(pt.y - ptOrg.y);
00276 #else // CE specific
00277                 // DP and LP are always the same on CE
00278                 logFont.lfHeight = -abs(::MulDiv(::GetDeviceCaps(hDC1, LOGPIXELSY), logFont.lfHeight, 720));   // 72 points/inch, 10 decipoints/point
00279 #endif // !_WIN32_WCE
00280 
00281                 if(hDC == NULL)
00282                         ::ReleaseDC(NULL, hDC1);
00283 
00284                 return CreateFontIndirect(logFont);
00285         }
00286 
00287         BOOL DeleteObject()
00288         {
00289                 BOOL bRet = ::DeleteObject(m_hFont);
00290                 if(bRet)
00291                         m_hFont = NULL;
00292                 return bRet;
00293         }
00294 
00295 // Attributes
00296         int GetLogFont(LOGFONT* pLogFont) const
00297         {
00298                 return ::GetObject(m_hFont, sizeof(LOGFONT), pLogFont);
00299         }
00300 
00301         bool GetLogFont(LOGFONT& LogFont) const
00302         {
00303                 return (::GetObject(m_hFont, sizeof(LOGFONT), &LogFont) == sizeof(LOGFONT));
00304         }
00305 };
00306 
00308 typedef CFontT<false>   CFontHandle;
00310 typedef CFontT<true>    CFont;
00311 
00312 } // gtl
00313 
00314 } // sprint
00315 
00316 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines