Skip to content

Commit

Permalink
Merge pull request #184 from mCapricorn/master
Browse files Browse the repository at this point in the history
font/字体 支持 strikeout/删除线
  • Loading branch information
qdtroy authored Oct 1, 2024
2 parents 59e87b3 + 7227345 commit 1d30923
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 24 deletions.
8 changes: 6 additions & 2 deletions DuiLib/Core/UIDlgBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ namespace DuiLib {
bool italic = false;
bool defaultfont = false;
bool shared = false;
bool strikeout = false;
for( int i = 0; i < nAttributes; i++ ) {
pstrName = node.GetAttributeName(i);
pstrValue = node.GetAttributeValue(i);
Expand All @@ -124,6 +125,9 @@ namespace DuiLib {
else if( _tcsicmp(pstrName, _T("italic")) == 0 ) {
italic = (_tcsicmp(pstrValue, _T("true")) == 0);
}
else if (_tcsicmp(pstrName, _T("strikeout")) == 0) {
strikeout = (_tcsicmp(pstrValue, _T("true")) == 0);
}
else if( _tcsicmp(pstrName, _T("default")) == 0 ) {
defaultfont = (_tcsicmp(pstrValue, _T("true")) == 0);
}
Expand All @@ -132,8 +136,8 @@ namespace DuiLib {
}
}
if( id >= 0 ) {
pManager->AddFont(id, pFontName, size, bold, underline, italic, shared);
if( defaultfont ) pManager->SetDefaultFont(pFontName, size, bold, underline, italic, shared);
pManager->AddFont(id, pFontName, size, bold, underline, italic, strikeout, shared);
if( defaultfont ) pManager->SetDefaultFont(pFontName, size, bold, underline, italic, strikeout, shared);
}
}
else if( _tcsicmp(pstrClass, _T("Default")) == 0 ) {
Expand Down
25 changes: 17 additions & 8 deletions DuiLib/Core/UIManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2788,7 +2788,7 @@ namespace DuiLib {
}
}

void CPaintManagerUI::SetDefaultFont(LPCTSTR pStrFontName, int nSize, bool bBold, bool bUnderline, bool bItalic, bool bShared)
void CPaintManagerUI::SetDefaultFont(LPCTSTR pStrFontName, int nSize, bool bBold, bool bUnderline, bool bItalic, bool bStrikeout, bool bShared)
{
LOGFONT lf = { 0 };
::GetObject(::GetStockObject(DEFAULT_GUI_FONT), sizeof(LOGFONT), &lf);
Expand All @@ -2802,6 +2802,7 @@ namespace DuiLib {
if( bBold ) lf.lfWeight += FW_BOLD;
if( bUnderline ) lf.lfUnderline = TRUE;
if( bItalic ) lf.lfItalic = TRUE;
if( bStrikeout ) lf.lfStrikeOut = TRUE;

HFONT hFont = ::CreateFontIndirect(&lf);
if( hFont == NULL ) return;
Expand All @@ -2815,6 +2816,7 @@ namespace DuiLib {
m_SharedResInfo.m_DefaultFontInfo.bBold = bBold;
m_SharedResInfo.m_DefaultFontInfo.bUnderline = bUnderline;
m_SharedResInfo.m_DefaultFontInfo.bItalic = bItalic;
m_SharedResInfo.m_DefaultFontInfo.bStrikeout = bStrikeout;
::ZeroMemory(&m_SharedResInfo.m_DefaultFontInfo.tm, sizeof(m_SharedResInfo.m_DefaultFontInfo.tm));
if( m_hDcPaint ) {
HFONT hOldFont = (HFONT) ::SelectObject(m_hDcPaint, hFont);
Expand All @@ -2831,6 +2833,7 @@ namespace DuiLib {
m_ResInfo.m_DefaultFontInfo.bBold = bBold;
m_ResInfo.m_DefaultFontInfo.bUnderline = bUnderline;
m_ResInfo.m_DefaultFontInfo.bItalic = bItalic;
m_ResInfo.m_DefaultFontInfo.bStrikeout = bStrikeout;
::ZeroMemory(&m_ResInfo.m_DefaultFontInfo.tm, sizeof(m_ResInfo.m_DefaultFontInfo.tm));
if( m_hDcPaint ) {
HFONT hOldFont = (HFONT) ::SelectObject(m_hDcPaint, hFont);
Expand All @@ -2848,7 +2851,7 @@ namespace DuiLib {
return m_ResInfo.m_CustomFonts.GetSize();
}

HFONT CPaintManagerUI::AddFont(int id, LPCTSTR pStrFontName, int nSize, bool bBold, bool bUnderline, bool bItalic, bool bShared)
HFONT CPaintManagerUI::AddFont(int id, LPCTSTR pStrFontName, int nSize, bool bBold, bool bUnderline, bool bItalic, bool bStrikeout, bool bShared)
{
LOGFONT lf = { 0 };
::GetObject(::GetStockObject(DEFAULT_GUI_FONT), sizeof(LOGFONT), &lf);
Expand All @@ -2862,6 +2865,7 @@ namespace DuiLib {
if( bBold ) lf.lfWeight = FW_BOLD;
if( bUnderline ) lf.lfUnderline = TRUE;
if( bItalic ) lf.lfItalic = TRUE;
if( bStrikeout ) lf.lfStrikeOut = TRUE;
HFONT hFont = ::CreateFontIndirect(&lf);
if( hFont == NULL ) return NULL;

Expand All @@ -2874,6 +2878,7 @@ namespace DuiLib {
pFontInfo->bBold = bBold;
pFontInfo->bUnderline = bUnderline;
pFontInfo->bItalic = bItalic;
pFontInfo->bStrikeout = bStrikeout;
if( m_hDcPaint ) {
HFONT hOldFont = (HFONT) ::SelectObject(m_hDcPaint, hFont);
::GetTextMetrics(m_hDcPaint, &pFontInfo->tm);
Expand Down Expand Up @@ -3017,22 +3022,24 @@ namespace DuiLib {
return pFontInfo->hFont;
}

HFONT CPaintManagerUI::GetFont(LPCTSTR pStrFontName, int nSize, bool bBold, bool bUnderline, bool bItalic)
HFONT CPaintManagerUI::GetFont(LPCTSTR pStrFontName, int nSize, bool bBold, bool bUnderline, bool bItalic, bool bStrikeout)
{
TFontInfo* pFontInfo = NULL;
for( int i = 0; i< m_ResInfo.m_CustomFonts.GetSize(); i++ ) {
if(LPCTSTR key = m_ResInfo.m_CustomFonts.GetAt(i)) {
pFontInfo = static_cast<TFontInfo*>(m_ResInfo.m_CustomFonts.Find(key));
if (pFontInfo && pFontInfo->sFontName == pStrFontName && pFontInfo->iSize == nSize &&
pFontInfo->bBold == bBold && pFontInfo->bUnderline == bUnderline && pFontInfo->bItalic == bItalic)
pFontInfo->bBold == bBold && pFontInfo->bUnderline == bUnderline && pFontInfo->bItalic == bItalic
&& pFontInfo->bStrikeout == bStrikeout)
return pFontInfo->hFont;
}
}
for( int i = 0; i< m_SharedResInfo.m_CustomFonts.GetSize(); i++ ) {
if(LPCTSTR key = m_SharedResInfo.m_CustomFonts.GetAt(i)) {
pFontInfo = static_cast<TFontInfo*>(m_SharedResInfo.m_CustomFonts.Find(key));
if (pFontInfo && pFontInfo->sFontName == pStrFontName && pFontInfo->iSize == nSize &&
pFontInfo->bBold == bBold && pFontInfo->bUnderline == bUnderline && pFontInfo->bItalic == bItalic)
pFontInfo->bBold == bBold && pFontInfo->bUnderline == bUnderline && pFontInfo->bItalic == bItalic
&& pFontInfo->bStrikeout == bStrikeout)
return pFontInfo->hFont;
}
}
Expand Down Expand Up @@ -3065,7 +3072,7 @@ namespace DuiLib {
return -1;
}

int CPaintManagerUI::GetFontIndex(LPCTSTR pStrFontName, int nSize, bool bBold, bool bUnderline, bool bItalic, bool bShared)
int CPaintManagerUI::GetFontIndex(LPCTSTR pStrFontName, int nSize, bool bBold, bool bUnderline, bool bItalic, bool bStrikeout, bool bShared)
{
TFontInfo* pFontInfo = NULL;
if (bShared)
Expand All @@ -3074,7 +3081,8 @@ namespace DuiLib {
if(LPCTSTR key = m_SharedResInfo.m_CustomFonts.GetAt(i)) {
pFontInfo = static_cast<TFontInfo*>(m_SharedResInfo.m_CustomFonts.Find(key));
if (pFontInfo && pFontInfo->sFontName == pStrFontName && pFontInfo->iSize == nSize &&
pFontInfo->bBold == bBold && pFontInfo->bUnderline == bUnderline && pFontInfo->bItalic == bItalic)
pFontInfo->bBold == bBold && pFontInfo->bUnderline == bUnderline && pFontInfo->bItalic == bItalic
&& pFontInfo->bStrikeout == bStrikeout)
return _ttoi(key);
}
}
Expand All @@ -3085,7 +3093,8 @@ namespace DuiLib {
if(LPCTSTR key = m_ResInfo.m_CustomFonts.GetAt(i)) {
pFontInfo = static_cast<TFontInfo*>(m_ResInfo.m_CustomFonts.Find(key));
if (pFontInfo && pFontInfo->sFontName == pStrFontName && pFontInfo->iSize == nSize &&
pFontInfo->bBold == bBold && pFontInfo->bUnderline == bUnderline && pFontInfo->bItalic == bItalic)
pFontInfo->bBold == bBold && pFontInfo->bUnderline == bUnderline && pFontInfo->bItalic == bItalic
&& pFontInfo->bStrikeout == bStrikeout)
return _ttoi(key);
}
}
Expand Down
9 changes: 5 additions & 4 deletions DuiLib/Core/UIManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ namespace DuiLib {
bool bBold;
bool bUnderline;
bool bItalic;
bool bStrikeout;
TEXTMETRIC tm;
} TFontInfo;

Expand Down Expand Up @@ -322,14 +323,14 @@ namespace DuiLib {
DWORD GetDefaultSelectedBkColor() const;
void SetDefaultSelectedBkColor(DWORD dwColor, bool bShared = false);
TFontInfo* GetDefaultFontInfo();
void SetDefaultFont(LPCTSTR pStrFontName, int nSize, bool bBold, bool bUnderline, bool bItalic, bool bShared = false);
void SetDefaultFont(LPCTSTR pStrFontName, int nSize, bool bBold, bool bUnderline, bool bItalic, bool bStrikeout, bool bShared = false);
DWORD GetCustomFontCount(bool bShared = false) const;
void AddFontArray(LPCTSTR pstrPath);
HFONT AddFont(int id, LPCTSTR pStrFontName, int nSize, bool bBold, bool bUnderline, bool bItalic, bool bShared = false);
HFONT AddFont(int id, LPCTSTR pStrFontName, int nSize, bool bBold, bool bUnderline, bool bItalic, bool bStrikeout, bool bShared = false);
HFONT GetFont(int id);
HFONT GetFont(LPCTSTR pStrFontName, int nSize, bool bBold, bool bUnderline, bool bItalic);
HFONT GetFont(LPCTSTR pStrFontName, int nSize, bool bBold, bool bUnderline, bool bItalic, bool bStrikeout);
int GetFontIndex(HFONT hFont, bool bShared = false);
int GetFontIndex(LPCTSTR pStrFontName, int nSize, bool bBold, bool bUnderline, bool bItalic, bool bShared = false);
int GetFontIndex(LPCTSTR pStrFontName, int nSize, bool bBold, bool bUnderline, bool bItalic, bool bStrikeout, bool bShared = false);
void RemoveFont(HFONT hFont, bool bShared = false);
void RemoveFont(int id, bool bShared = false);
void RemoveAllFonts(bool bShared = false);
Expand Down
22 changes: 12 additions & 10 deletions DuiLib/Core/UIRender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1914,8 +1914,8 @@ namespace DuiLib {
TFontInfo* pFontInfo = pDefFontInfo;
if( aFontArray.GetSize() > 0 ) pFontInfo = (TFontInfo*)aFontArray.GetAt(aFontArray.GetSize() - 1);
if( pFontInfo->bUnderline == false ) {
HFONT hFont = pManager->GetFont(pFontInfo->sFontName, pFontInfo->iSize, pFontInfo->bBold, true, pFontInfo->bItalic);
if( hFont == NULL ) hFont = pManager->AddFont(g_iFontID, pFontInfo->sFontName, pFontInfo->iSize, pFontInfo->bBold, true, pFontInfo->bItalic);
HFONT hFont = pManager->GetFont(pFontInfo->sFontName, pFontInfo->iSize, pFontInfo->bBold, true, pFontInfo->bItalic, pFontInfo->bStrikeout);
if( hFont == NULL ) hFont = pManager->AddFont(g_iFontID, pFontInfo->sFontName, pFontInfo->iSize, pFontInfo->bBold, true, pFontInfo->bItalic, pFontInfo->bStrikeout);
pFontInfo = pManager->GetFontInfo(hFont);
aFontArray.Add(pFontInfo);
pTm = &pFontInfo->tm;
Expand All @@ -1932,8 +1932,8 @@ namespace DuiLib {
TFontInfo* pFontInfo = pDefFontInfo;
if( aFontArray.GetSize() > 0 ) pFontInfo = (TFontInfo*)aFontArray.GetAt(aFontArray.GetSize() - 1);
if( pFontInfo->bBold == false ) {
HFONT hFont = pManager->GetFont(pFontInfo->sFontName, pFontInfo->iSize, true, pFontInfo->bUnderline, pFontInfo->bItalic);
if( hFont == NULL ) hFont = pManager->AddFont(g_iFontID, pFontInfo->sFontName, pFontInfo->iSize, true, pFontInfo->bUnderline, pFontInfo->bItalic);
HFONT hFont = pManager->GetFont(pFontInfo->sFontName, pFontInfo->iSize, true, pFontInfo->bUnderline, pFontInfo->bItalic, pFontInfo->bStrikeout);
if( hFont == NULL ) hFont = pManager->AddFont(g_iFontID, pFontInfo->sFontName, pFontInfo->iSize, true, pFontInfo->bUnderline, pFontInfo->bItalic, pFontInfo->bStrikeout);
pFontInfo = pManager->GetFontInfo(hFont);
aFontArray.Add(pFontInfo);
pTm = &pFontInfo->tm;
Expand Down Expand Up @@ -1971,6 +1971,7 @@ namespace DuiLib {
bool bBold = false;
bool bUnderline = false;
bool bItalic = false;
bool bStrikeout = false;
while( *pstrText != _T('\0') && *pstrText != _T('>') && *pstrText != _T('}') && *pstrText != _T(' ') ) {
pstrTemp = ::CharNext(pstrText);
while( pstrText < pstrTemp) {
Expand All @@ -1992,8 +1993,9 @@ namespace DuiLib {
if( sFontAttr.Find(_T("bold")) >= 0 ) bBold = true;
if( sFontAttr.Find(_T("underline")) >= 0 ) bUnderline = true;
if( sFontAttr.Find(_T("italic")) >= 0 ) bItalic = true;
HFONT hFont = pManager->GetFont(sFontName, iFontSize, bBold, bUnderline, bItalic);
if( hFont == NULL ) hFont = pManager->AddFont(g_iFontID, sFontName, iFontSize, bBold, bUnderline, bItalic);
if( sFontAttr.Find(_T("strikeout")) >= 0 ) bStrikeout = true;
HFONT hFont = pManager->GetFont(sFontName, iFontSize, bBold, bUnderline, bItalic, bStrikeout);
if( hFont == NULL ) hFont = pManager->AddFont(g_iFontID, sFontName, iFontSize, bBold, bUnderline, bItalic, bStrikeout);
TFontInfo* pFontInfo = pManager->GetFontInfo(hFont);
aFontArray.Add(pFontInfo);
pTm = &pFontInfo->tm;
Expand Down Expand Up @@ -2023,8 +2025,8 @@ namespace DuiLib {
TFontInfo* pFontInfo = pDefFontInfo;
if( aFontArray.GetSize() > 0 ) pFontInfo = (TFontInfo*)aFontArray.GetAt(aFontArray.GetSize() - 1);
if( pFontInfo->bItalic == false ) {
HFONT hFont = pManager->GetFont(pFontInfo->sFontName, pFontInfo->iSize, pFontInfo->bBold, pFontInfo->bUnderline, true);
if( hFont == NULL ) hFont = pManager->AddFont(g_iFontID, pFontInfo->sFontName, pFontInfo->iSize, pFontInfo->bBold, pFontInfo->bUnderline, true);
HFONT hFont = pManager->GetFont(pFontInfo->sFontName, pFontInfo->iSize, pFontInfo->bBold, pFontInfo->bUnderline, pFontInfo->bStrikeout, true);
if( hFont == NULL ) hFont = pManager->AddFont(g_iFontID, pFontInfo->sFontName, pFontInfo->iSize, pFontInfo->bBold, pFontInfo->bUnderline, pFontInfo->bStrikeout, true);
pFontInfo = pManager->GetFontInfo(hFont);
aFontArray.Add(pFontInfo);
pTm = &pFontInfo->tm;
Expand Down Expand Up @@ -2156,8 +2158,8 @@ namespace DuiLib {
TFontInfo* pFontInfo = pDefFontInfo;
if( aFontArray.GetSize() > 0 ) pFontInfo = (TFontInfo*)aFontArray.GetAt(aFontArray.GetSize() - 1);
if( pFontInfo->bUnderline == false ) {
HFONT hFont = pManager->GetFont(pFontInfo->sFontName, pFontInfo->iSize, pFontInfo->bBold, true, pFontInfo->bItalic);
if( hFont == NULL ) hFont = pManager->AddFont(g_iFontID, pFontInfo->sFontName, pFontInfo->iSize, pFontInfo->bBold, true, pFontInfo->bItalic);
HFONT hFont = pManager->GetFont(pFontInfo->sFontName, pFontInfo->iSize, pFontInfo->bBold, true, pFontInfo->bItalic, pFontInfo->bStrikeout);
if( hFont == NULL ) hFont = pManager->AddFont(g_iFontID, pFontInfo->sFontName, pFontInfo->iSize, pFontInfo->bBold, true, pFontInfo->bItalic, pFontInfo->bStrikeout);
pFontInfo = pManager->GetFontInfo(hFont);
aFontArray.Add(pFontInfo);
pTm = &pFontInfo->tm;
Expand Down

0 comments on commit 1d30923

Please sign in to comment.