home *** CD-ROM | disk | FTP | other *** search
- #include "stdafx.h"
-
- #include <Afxctl.h>
- #include <Atlbase.h>
-
- #include "NFont.h"
-
- //////////////////////////////////////////////////////////////////////
- // class CNFont
- CNFont::CNFont(LPPROPERTYNOTIFYSINK pNotifySink /*= NULL*/)
- :CFontHolder(pNotifySink)
- {
- // create a new default IFont
- InitializeFont(NULL, NULL);
- }
-
- //////////////////////////////////////////////////////////////////////
- CNFont::~CNFont()
- {
- }
-
- //////////////////////////////////////////////////////////////////////
- BOOL CNFont::SetDispatch(LPDISPATCH pDispatch)
- {
- InitializeFont(NULL, pDispatch);
- return TRUE;
- }
-
- //////////////////////////////////////////////////////////////////////
- BOOL CNFont::GetLogFont(LOGFONT& LogFont)
- {
- CComPtr<IFontDisp> pFontDisp;
-
- ZeroMemory(&LogFont, sizeof(LOGFONT));
-
- pFontDisp = GetFontDispatch();
- if (pFontDisp == NULL)
- return FALSE;
-
- CComQIPtr<IFont> pIFont(pFontDisp);
- if (pIFont == NULL)
- return FALSE;
-
- HFONT hFont;
- if (FAILED(pIFont->get_hFont(&hFont)))
- return FALSE;
-
- VERIFY(GetObject(hFont, sizeof(LogFont), (void*)&LogFont));
- return TRUE;
- }
-
- //////////////////////////////////////////////////////////////////////
- BOOL CNFont::SetLogFont(const LOGFONT& LogFont)
- {
- FONTDESC FontDesc;
- ZeroMemory(&FontDesc, sizeof(FONTDESC));
-
- FontDesc.cbSizeofstruct = sizeof(FONTDESC);
- FontDesc.lpstrName = (CString(LogFont.lfFaceName)).AllocSysString();
- FontDesc.cySize.Lo = (long)abs(int(LogFont.lfHeight * 3 / 4.0)) * 10000;
- FontDesc.cySize.Hi = 0;
- FontDesc.sWeight = (short)LogFont.lfWeight;
- FontDesc.sCharset = LogFont.lfCharSet;
- FontDesc.fItalic = LogFont.lfItalic;
- FontDesc.fUnderline = LogFont.lfUnderline;
- FontDesc.fStrikethrough = LogFont.lfStrikeOut;
-
- InitializeFont(&FontDesc);
-
- return TRUE;
- }
-
- //////////////////////////////////////////////////////////////////////
- void CNFont::GetTextMetrics(LPTEXTMETRIC pTextMetrics)
- {
- CDC dc;
- LOGFONT logFont;
- CFont font, *pOldFont;
-
- VERIFY(dc.CreateCompatibleDC(NULL));
- VERIFY(GetLogFont(logFont)); // Forgot to initialize the object ?
- VERIFY(font.CreateFontIndirect(&logFont));
-
- pOldFont = (CFont*)dc.SelectObject(&font);
- dc.GetTextMetrics(pTextMetrics);
- dc.SelectObject(pOldFont);
- }
-
- //////////////////////////////////////////////////////////////////////
- BOOL CNFont::IsTrueType()
- {
- TEXTMETRIC tm;
-
- GetTextMetrics(&tm);
-
- if (tm.tmPitchAndFamily & TMPF_TRUETYPE)
- return TRUE;
-
- return FALSE;
- }