XML中的Font节点添加id属性,支持font自定义,同时兼容过去的索引方式

This commit is contained in:
evilbeast 2019-06-10 01:56:07 +08:00
parent e817180f3c
commit d709e24707
11 changed files with 109 additions and 85 deletions

View File

@ -2,7 +2,7 @@
<Global> <Global>
<!--所有字体--> <!--所有字体-->
<!--0--> <!--0-->
<Font name="system" size="10"/> <Font name="system" size="12"/>
<!--1--> <!--1-->
<Font name="system" size="12"/> <Font name="system" size="12"/>
<!--2--> <!--2-->

View File

@ -225,7 +225,7 @@ void CheckBoxTemplate<InheritType>::PaintText(IRenderContext* pRender)
std::wstring newTextColor = m_dwSelectedTextColor.empty() ? this->m_textColorMap[kControlStateNormal] : m_dwSelectedTextColor; std::wstring newTextColor = m_dwSelectedTextColor.empty() ? this->m_textColorMap[kControlStateNormal] : m_dwSelectedTextColor;
DWORD dwTextColor = GlobalManager::GetTextColor(newTextColor); DWORD dwTextColor = GlobalManager::GetTextColor(newTextColor);
DWORD dwDisabledTextColor = GlobalManager::GetTextColor(this->m_textColorMap[kControlStateDisabled]); DWORD dwDisabledTextColor = GlobalManager::GetTextColor(this->m_textColorMap[kControlStateDisabled]);
pRender->DrawText(rc, this->GetText(), this->IsEnabled() ? dwTextColor : dwDisabledTextColor, this->m_iFont, this->m_uTextStyle); pRender->DrawText(rc, this->GetText(), this->IsEnabled() ? dwTextColor : dwDisabledTextColor, this->m_sFontId, this->m_uTextStyle);
} }
template<typename InheritType> template<typename InheritType>

View File

@ -56,14 +56,14 @@ public:
* @brief * @brief
* @return global.xml * @return global.xml
*/ */
int GetFont() const; std::wstring GetFont() const;
/** /**
* @brief * @brief
* @param[in] index global.xml * @param[in] index global.xml
* @return * @return
*/ */
void SetFont(int index); void SetFont(const std::wstring& strFontId);
/** /**
* @brief * @brief
@ -105,7 +105,7 @@ public:
void SetLineLimit(bool bLineLimit); void SetLineLimit(bool bLineLimit);
protected: protected:
int m_iFont; std::wstring m_sFontId;
UINT m_uTextStyle; UINT m_uTextStyle;
bool m_bSingleLine; bool m_bSingleLine;
bool m_bLineLimit; bool m_bLineLimit;
@ -119,7 +119,7 @@ protected:
template<typename InheritType> template<typename InheritType>
LabelTemplate<InheritType>::LabelTemplate() : LabelTemplate<InheritType>::LabelTemplate() :
m_iFont(1), m_sFontId(),
m_uTextStyle(DT_LEFT | DT_TOP | DT_END_ELLIPSIS | DT_NOCLIP | DT_SINGLELINE), m_uTextStyle(DT_LEFT | DT_TOP | DT_END_ELLIPSIS | DT_NOCLIP | DT_SINGLELINE),
m_bSingleLine(true), m_bSingleLine(true),
m_bLineLimit(false), m_bLineLimit(false),
@ -226,7 +226,7 @@ CSize LabelTemplate<InheritType>::EstimateText(CSize szAvailable, bool& bReEstim
CSize fixedSize; CSize fixedSize;
if (!GetText().empty()) { if (!GetText().empty()) {
auto pRender = this->m_pWindow->GetRenderContext(); auto pRender = this->m_pWindow->GetRenderContext();
UiRect rect = pRender->MeasureText(GetText(), m_iFont, m_uTextStyle, width); UiRect rect = pRender->MeasureText(GetText(), m_sFontId, m_uTextStyle, width);
if (this->GetFixedWidth() == DUI_LENGTH_AUTO) { if (this->GetFixedWidth() == DUI_LENGTH_AUTO) {
fixedSize.cx = rect.right - rect.left + m_rcTextPadding.left + m_rcTextPadding.right; fixedSize.cx = rect.right - rect.left + m_rcTextPadding.left + m_rcTextPadding.right;
} }
@ -239,7 +239,7 @@ CSize LabelTemplate<InheritType>::EstimateText(CSize szAvailable, bool& bReEstim
int maxWidth = szAvailable.cx - m_rcTextPadding.left - m_rcTextPadding.right; int maxWidth = szAvailable.cx - m_rcTextPadding.left - m_rcTextPadding.right;
if (estimateWidth > maxWidth) { if (estimateWidth > maxWidth) {
estimateWidth = maxWidth; estimateWidth = maxWidth;
UiRect newRect = pRender->MeasureText(GetText(), m_iFont, m_uTextStyle, estimateWidth); UiRect newRect = pRender->MeasureText(GetText(), m_sFontId, m_uTextStyle, estimateWidth);
estimateHeight = newRect.bottom - newRect.top; estimateHeight = newRect.bottom - newRect.top;
} }
} }
@ -288,7 +288,7 @@ void LabelTemplate<InheritType>::SetAttribute(const std::wstring& strName, const
else if (strName == _T("singleline")) SetSingleLine(strValue == _T("true")); else if (strName == _T("singleline")) SetSingleLine(strValue == _T("true"));
else if (strName == _T("text")) SetText(strValue); else if (strName == _T("text")) SetText(strValue);
else if (strName == _T("textid")) SetTextId(strValue); else if (strName == _T("textid")) SetTextId(strValue);
else if (strName == _T("font")) SetFont(_ttoi(strValue.c_str())); else if (strName == _T("font")) SetFont(strValue);
else if (strName == _T("normaltextcolor")) SetStateTextColor(kControlStateNormal, strValue); else if (strName == _T("normaltextcolor")) SetStateTextColor(kControlStateNormal, strValue);
else if (strName == _T("hottextcolor")) SetStateTextColor(kControlStateHot, strValue); else if (strName == _T("hottextcolor")) SetStateTextColor(kControlStateHot, strValue);
else if (strName == _T("pushedtextcolor")) SetStateTextColor(kControlStatePushed, strValue); else if (strName == _T("pushedtextcolor")) SetStateTextColor(kControlStatePushed, strValue);
@ -339,14 +339,14 @@ void LabelTemplate<InheritType>::PaintText(IRenderContext* pRender)
std::wstring clrColor = GetStateTextColor(kControlStateNormal); std::wstring clrColor = GetStateTextColor(kControlStateNormal);
if (!clrColor.empty()) { if (!clrColor.empty()) {
DWORD dwClrColor = GlobalManager::GetTextColor(clrColor); DWORD dwClrColor = GlobalManager::GetTextColor(clrColor);
pRender->DrawText(rc, GetText(), dwClrColor, m_iFont, m_uTextStyle, 255, m_bLineLimit); pRender->DrawText(rc, GetText(), dwClrColor, m_sFontId, m_uTextStyle, 255, m_bLineLimit);
} }
if (this->m_nHotAlpha > 0) { if (this->m_nHotAlpha > 0) {
std::wstring clrColor = GetStateTextColor(kControlStateHot); std::wstring clrColor = GetStateTextColor(kControlStateHot);
if (!clrColor.empty()) { if (!clrColor.empty()) {
DWORD dwClrColor = GlobalManager::GetTextColor(clrColor); DWORD dwClrColor = GlobalManager::GetTextColor(clrColor);
pRender->DrawText(rc, GetText(), dwClrColor, m_iFont, m_uTextStyle, (BYTE)this->m_nHotAlpha, m_bLineLimit); pRender->DrawText(rc, GetText(), dwClrColor, m_sFontId, m_uTextStyle, (BYTE)this->m_nHotAlpha, m_bLineLimit);
} }
} }
@ -354,7 +354,7 @@ void LabelTemplate<InheritType>::PaintText(IRenderContext* pRender)
} }
} }
pRender->DrawText(rc, GetText(), dwClrColor, m_iFont, m_uTextStyle, 255, m_bLineLimit); pRender->DrawText(rc, GetText(), dwClrColor, m_sFontId, m_uTextStyle, 255, m_bLineLimit);
} }
template<typename InheritType> template<typename InheritType>
@ -387,15 +387,15 @@ void LabelTemplate<InheritType>::SetStateTextColor(ControlStateType stateType, c
} }
template<typename InheritType> template<typename InheritType>
int LabelTemplate<InheritType>::GetFont() const std::wstring LabelTemplate<InheritType>::GetFont() const
{ {
return m_iFont; return m_sFontId;
} }
template<typename InheritType> template<typename InheritType>
void LabelTemplate<InheritType>::SetFont(int index) void LabelTemplate<InheritType>::SetFont(const std::wstring& strFontId)
{ {
m_iFont = index; m_sFontId = strFontId;
this->Invalidate(); this->Invalidate();
} }

View File

@ -1172,7 +1172,7 @@ RichEdit::RichEdit() :
m_iCaretPosY(0), m_iCaretPosY(0),
m_iCaretWidth(0), m_iCaretWidth(0),
m_iCaretHeight(0), m_iCaretHeight(0),
m_iFont(0), m_sFontId(0),
m_iLimitText(0), m_iLimitText(0),
m_lTwhStyle(ES_MULTILINE), m_lTwhStyle(ES_MULTILINE),
m_textVerAlignType(kVerAlignTop), m_textVerAlignType(kVerAlignTop),
@ -1274,16 +1274,16 @@ void RichEdit::SetWordWrap(bool bWordWrap)
if( m_pTwh ) m_pTwh->SetWordWrap(bWordWrap); if( m_pTwh ) m_pTwh->SetWordWrap(bWordWrap);
} }
int RichEdit::GetFont() std::wstring RichEdit::GetFont() const
{ {
return m_iFont; return m_sFontId;
} }
void RichEdit::SetFont(int index) void RichEdit::SetFont(const std::wstring& strFontId)
{ {
m_iFont = index; m_sFontId = strFontId;
if( m_pTwh ) { if( m_pTwh ) {
m_pTwh->SetFont(GlobalManager::GetFont(m_iFont)); m_pTwh->SetFont(GlobalManager::GetFont(m_sFontId));
} }
} }
@ -2745,7 +2745,7 @@ void RichEdit::SetAttribute(const std::wstring& strName, const std::wstring& str
else if (strName == _T("prompttext")) SetPromptText(strValue); else if (strName == _T("prompttext")) SetPromptText(strValue);
else if (strName == _T("prompttextid")) SetPromptTextId(strValue); else if (strName == _T("prompttextid")) SetPromptTextId(strValue);
else if (strName == _T("focusedimage")) SetFocusedImage(strValue); else if (strName == _T("focusedimage")) SetFocusedImage(strValue);
else if (strName == _T("font")) SetFont(_ttoi(strValue.c_str())); else if (strName == _T("font")) SetFont(strValue);
else if (strName == _T("text")) SetText(strValue.c_str()); else if (strName == _T("text")) SetText(strValue.c_str());
else if (strName == _T("textid")) SetTextId(strValue.c_str()); else if (strName == _T("textid")) SetTextId(strValue.c_str());
else if (strName == _T("wanttab")) SetWantTab(strValue == _T("true")); else if (strName == _T("wanttab")) SetWantTab(strValue == _T("true"));
@ -2899,7 +2899,7 @@ void RichEdit::PaintPromptText(IRenderContext* pRender)
DWORD dwClrColor = GlobalManager::GetTextColor(m_sPromptColor); DWORD dwClrColor = GlobalManager::GetTextColor(m_sPromptColor);
UINT dwStyle = DT_NOCLIP; UINT dwStyle = DT_NOCLIP;
pRender->DrawText(rc, strPrompt, dwClrColor, m_iFont, dwStyle); pRender->DrawText(rc, strPrompt, dwClrColor, m_sFontId, dwStyle);
} }
std::wstring RichEdit::GetFocusedImage() std::wstring RichEdit::GetFocusedImage()
@ -2995,23 +2995,23 @@ void RichEdit::AddLinkColorText(const std::wstring &str, const std::wstring &col
GetDefaultCharFormat(cf); GetDefaultCharFormat(cf);
SetSelectionCharFormat(cf); SetSelectionCharFormat(cf);
} }
void RichEdit::AddLinkColorTextEx(const std::wstring& str, const std::wstring &color, const std::wstring &linkInfo, int font) void RichEdit::AddLinkColorTextEx(const std::wstring& str, const std::wstring &color, const std::wstring &linkInfo, const std::wstring& strFontId)
{ {
if (!m_bRich || str.empty() || color.empty()) { if (!m_bRich || str.empty() || color.empty()) {
ASSERT(FALSE); ASSERT(FALSE);
return; return;
} }
int ifont = font >= 0 ? font : m_iFont;
std::string link; std::string link;
std::string text; std::string text;
std::string font_face; std::string font_face;
StringHelper::UnicodeToMBCS(linkInfo, link); StringHelper::UnicodeToMBCS(linkInfo, link);
StringHelper::UnicodeToMBCS(str, text); StringHelper::UnicodeToMBCS(str, text);
auto hFont = GlobalManager::GetFont(ifont); auto hFont = GlobalManager::GetFont(strFontId);
if (hFont == NULL) if (hFont == NULL)
hFont = GlobalManager::GetFont(m_iFont); hFont = GlobalManager::GetFont(m_sFontId);
if (hFont == NULL) if (hFont == NULL)
hFont = GlobalManager::GetFont(0); hFont = GlobalManager::GetFont(L"");
LOGFONT lf; LOGFONT lf;
::GetObject(hFont, sizeof(LOGFONT), &lf); ::GetObject(hFont, sizeof(LOGFONT), &lf);
StringHelper::UnicodeToMBCS(lf.lfFaceName, font_face); StringHelper::UnicodeToMBCS(lf.lfFaceName, font_face);

View File

@ -118,14 +118,14 @@ public:
* @brief * @brief
* @return global.xml * @return global.xml
*/ */
int GetFont(); std::wstring GetFont() const;
/** /**
* @brief * @brief
* @param[in] index global.xml * @param[in] index global.xml
* @return * @return
*/ */
void SetFont(int index); void SetFont(const std::wstring& strFontId);
void SetFont(HFONT font); void SetFont(HFONT font);
/** /**
* @brief * @brief
@ -875,7 +875,7 @@ public:
* @param[in] font * @param[in] font
* @return * @return
*/ */
void AddLinkColorTextEx(const std::wstring& str, const std::wstring &color, const std::wstring &linkInfo = L"", int font = -1); void AddLinkColorTextEx(const std::wstring& str, const std::wstring &color, const std::wstring &linkInfo = L"", const std::wstring& strFontId = L"");
/** /**
* @brief hittest * @brief hittest
@ -956,7 +956,7 @@ protected:
int m_iCaretPosY; int m_iCaretPosY;
int m_iCaretWidth; int m_iCaretWidth;
int m_iCaretHeight; int m_iCaretHeight;
int m_iFont; std::wstring m_sFontId;
int m_iLimitText; int m_iLimitText;
LONG m_lTwhStyle; LONG m_lTwhStyle;
VerAlignType m_textVerAlignType; VerAlignType m_textVerAlignType;

View File

@ -14,7 +14,7 @@ CreateControlCallback GlobalManager::m_createControlCallback;
GlobalManager::MapStringToImagePtr GlobalManager::m_mImageHash; GlobalManager::MapStringToImagePtr GlobalManager::m_mImageHash;
std::map<std::wstring, DWORD> GlobalManager::m_mapTextColor; std::map<std::wstring, DWORD> GlobalManager::m_mapTextColor;
std::map<std::wstring, std::wstring> GlobalManager::m_mGlobalClass; std::map<std::wstring, std::wstring> GlobalManager::m_mGlobalClass;
std::vector<TFontInfo*> GlobalManager::m_aCustomFonts; std::map<std::wstring, TFontInfo*> GlobalManager::m_mCustomFonts;
short GlobalManager::m_H = 180; short GlobalManager::m_H = 180;
short GlobalManager::m_S = 100; short GlobalManager::m_S = 100;
@ -366,8 +366,17 @@ void GlobalManager::RemoveAllImages()
m_mImageHash.clear(); m_mImageHash.clear();
} }
HFONT GlobalManager::AddFont(const std::wstring& strFontName, int nSize, bool bBold, bool bUnderline, bool bItalic) HFONT GlobalManager::AddFont(const std::wstring& strFontId, const std::wstring& strFontName, int nSize, bool bBold, bool bUnderline, bool bItalic)
{ {
std::wstring strNewFontId = strFontId;
if (strNewFontId.empty())
{
strNewFontId = std::to_wstring(m_mCustomFonts.size());
}
auto iter = m_mCustomFonts.find(strNewFontId);
ASSERT(iter == m_mCustomFonts.end());
static bool bOsOverXp = IsWindowsVistaOrGreater(); static bool bOsOverXp = IsWindowsVistaOrGreater();
std::wstring fontName = strFontName; std::wstring fontName = strFontName;
if ( fontName == L"system" ) { if ( fontName == L"system" ) {
@ -395,23 +404,32 @@ HFONT GlobalManager::AddFont(const std::wstring& strFontName, int nSize, bool bB
pFontInfo->bItalic = bItalic; pFontInfo->bItalic = bItalic;
::ZeroMemory(&pFontInfo->tm, sizeof(pFontInfo->tm)); ::ZeroMemory(&pFontInfo->tm, sizeof(pFontInfo->tm));
m_aCustomFonts.push_back(pFontInfo); m_mCustomFonts.insert(std::make_pair(strNewFontId, pFontInfo));
return hFont; return hFont;
} }
TFontInfo* GlobalManager::GetTFontInfo(std::size_t index) TFontInfo* GlobalManager::GetTFontInfo(const std::wstring& strFontId)
{ {
ASSERT(index >= 0 || index < m_aCustomFonts.size()); if (strFontId.empty())
if (index < 0 && index >= m_aCustomFonts.size()) {
for (auto it = m_mCustomFonts.begin(); it != m_mCustomFonts.end(); it++)
{
return it->second;
}
return NULL; return NULL;
TFontInfo* pFontInfo = static_cast<TFontInfo*>(m_aCustomFonts[index]); }
auto iter = m_mCustomFonts.find(strFontId);
ASSERT(iter != m_mCustomFonts.end());
TFontInfo* pFontInfo = static_cast<TFontInfo*>(iter->second);
return pFontInfo; return pFontInfo;
} }
HFONT GlobalManager::GetFont(std::size_t index) HFONT GlobalManager::GetFont(const std::wstring& strFontId)
{ {
TFontInfo* pFontInfo = GetTFontInfo(index); TFontInfo* pFontInfo = GetTFontInfo(strFontId);
if (pFontInfo) if (pFontInfo)
return pFontInfo->hFont; return pFontInfo->hFont;
return nullptr; return nullptr;
@ -419,8 +437,8 @@ HFONT GlobalManager::GetFont(std::size_t index)
HFONT GlobalManager::GetFont(const std::wstring& strFontName, int nSize, bool bBold, bool bUnderline, bool bItalic) HFONT GlobalManager::GetFont(const std::wstring& strFontName, int nSize, bool bBold, bool bUnderline, bool bItalic)
{ {
for( auto it = m_aCustomFonts.begin(); it != m_aCustomFonts.end(); it++ ) { for (auto it = m_mCustomFonts.begin(); it != m_mCustomFonts.end(); it++) {
auto pFontInfo = *it; auto pFontInfo = it->second;
if( pFontInfo->sFontName == strFontName && pFontInfo->iSize == nSize && if( pFontInfo->sFontName == strFontName && pFontInfo->iSize == nSize &&
pFontInfo->bBold == bBold && pFontInfo->bUnderline == bUnderline && pFontInfo->bItalic == bItalic) pFontInfo->bBold == bBold && pFontInfo->bUnderline == bUnderline && pFontInfo->bItalic == bItalic)
return pFontInfo->hFont; return pFontInfo->hFont;
@ -428,12 +446,9 @@ HFONT GlobalManager::GetFont(const std::wstring& strFontName, int nSize, bool bB
return NULL; return NULL;
} }
TFontInfo* GlobalManager::GetFontInfo(std::size_t index, HDC hDcPaint) TFontInfo* GlobalManager::GetFontInfo(const std::wstring& strFontId, HDC hDcPaint)
{ {
ASSERT(index >= 0 || index < m_aCustomFonts.size()); TFontInfo* pFontInfo = GetTFontInfo(strFontId);
if (index < 0 && index >= m_aCustomFonts.size())
return NULL;
TFontInfo* pFontInfo = static_cast<TFontInfo*>(m_aCustomFonts[index]);
if( pFontInfo->tm.tmHeight == 0 ) { if( pFontInfo->tm.tmHeight == 0 ) {
HFONT hOldFont = (HFONT) ::SelectObject(hDcPaint, pFontInfo->hFont); HFONT hOldFont = (HFONT) ::SelectObject(hDcPaint, pFontInfo->hFont);
::GetTextMetrics(hDcPaint, &pFontInfo->tm); ::GetTextMetrics(hDcPaint, &pFontInfo->tm);
@ -444,8 +459,8 @@ TFontInfo* GlobalManager::GetFontInfo(std::size_t index, HDC hDcPaint)
TFontInfo* GlobalManager::GetFontInfo(HFONT hFont, HDC hDcPaint) TFontInfo* GlobalManager::GetFontInfo(HFONT hFont, HDC hDcPaint)
{ {
for( auto it = m_aCustomFonts.begin(); it != m_aCustomFonts.end(); it++ ) { for( auto it = m_mCustomFonts.begin(); it != m_mCustomFonts.end(); it++ ) {
auto pFontInfo = *it; auto pFontInfo = it->second;
if( pFontInfo->hFont == hFont ) { if( pFontInfo->hFont == hFont ) {
if( pFontInfo->tm.tmHeight == 0 ) { if( pFontInfo->tm.tmHeight == 0 ) {
HFONT hOldFont = (HFONT) ::SelectObject(hDcPaint, pFontInfo->hFont); HFONT hOldFont = (HFONT) ::SelectObject(hDcPaint, pFontInfo->hFont);
@ -462,8 +477,8 @@ TFontInfo* GlobalManager::GetFontInfo(HFONT hFont, HDC hDcPaint)
bool GlobalManager::FindFont(HFONT hFont) bool GlobalManager::FindFont(HFONT hFont)
{ {
for( auto it = m_aCustomFonts.begin(); it != m_aCustomFonts.end(); it++ ) { for( auto it = m_mCustomFonts.begin(); it != m_mCustomFonts.end(); it++ ) {
auto pFontInfo = *it; auto pFontInfo = it->second;
if( pFontInfo->hFont == hFont ) if( pFontInfo->hFont == hFont )
return true; return true;
} }
@ -472,8 +487,8 @@ bool GlobalManager::FindFont(HFONT hFont)
bool GlobalManager::FindFont(const std::wstring& strFontName, int nSize, bool bBold, bool bUnderline, bool bItalic) bool GlobalManager::FindFont(const std::wstring& strFontName, int nSize, bool bBold, bool bUnderline, bool bItalic)
{ {
for( auto it = m_aCustomFonts.begin(); it != m_aCustomFonts.end(); it++ ) { for (auto it = m_mCustomFonts.begin(); it != m_mCustomFonts.end(); it++) {
auto pFontInfo = *it; auto pFontInfo = it->second;
if( pFontInfo->sFontName == strFontName && pFontInfo->iSize == nSize && if( pFontInfo->sFontName == strFontName && pFontInfo->iSize == nSize &&
pFontInfo->bBold == bBold && pFontInfo->bUnderline == bUnderline && pFontInfo->bItalic == bItalic) pFontInfo->bBold == bBold && pFontInfo->bUnderline == bUnderline && pFontInfo->bItalic == bItalic)
return true; return true;
@ -481,24 +496,28 @@ bool GlobalManager::FindFont(const std::wstring& strFontName, int nSize, bool bB
return false; return false;
} }
bool GlobalManager::RemoveFontAt(std::size_t index) bool GlobalManager::RemoveFontAt(const std::wstring& strFontId)
{ {
if( index < 0 || index >= m_aCustomFonts.size() ) return false; auto iter = m_mCustomFonts.find(strFontId);
TFontInfo* pFontInfo = static_cast<TFontInfo*>(m_aCustomFonts[index]); if (iter == m_mCustomFonts.end()) return false;
TFontInfo* pFontInfo = static_cast<TFontInfo*>(iter->second);
::DeleteObject(pFontInfo->hFont); ::DeleteObject(pFontInfo->hFont);
delete pFontInfo; delete pFontInfo;
m_aCustomFonts.erase(m_aCustomFonts.begin() + index);
m_mCustomFonts.erase(iter);
return true; return true;
} }
void GlobalManager::RemoveAllFonts() void GlobalManager::RemoveAllFonts()
{ {
for( auto it = m_aCustomFonts.begin(); it != m_aCustomFonts.end(); it++ ) { for (auto it = m_mCustomFonts.begin(); it != m_mCustomFonts.end(); it++) {
auto pFontInfo = *it; auto pFontInfo = it->second;
::DeleteObject(pFontInfo->hFont); ::DeleteObject(pFontInfo->hFont);
delete pFontInfo; delete pFontInfo;
} }
m_aCustomFonts.clear(); m_mCustomFonts.clear();
} }
std::wstring GlobalManager::GetDefaultDisabledTextColor() std::wstring GlobalManager::GetDefaultDisabledTextColor()

View File

@ -200,6 +200,7 @@ public:
/** /**
* @brief * @brief
* @param[in] strFontId ID标记
* @param[in] strFontName * @param[in] strFontName
* @param[in] nSize * @param[in] nSize
* @param[in] bBold * @param[in] bBold
@ -207,22 +208,21 @@ public:
* @param[in] bItalic * @param[in] bItalic
* @return HFONT * @return HFONT
*/ */
static HFONT AddFont(const std::wstring& strFontName, int nSize, bool bBold, bool bUnderline, bool bItalic); static HFONT AddFont(const std::wstring& strFontId, const std::wstring& strFontName, int nSize, bool bBold, bool bUnderline, bool bItalic);
/** /**
* @brief * @brief
* @param[in] index * @param[in] strFontId ID
* @return TFontInfo * @return TFontInfo
*/ */
static TFontInfo* GetTFontInfo(std::size_t index); static TFontInfo* GetTFontInfo(const std::wstring& strFontId);
/** /**
* @brief * @brief ID返回一个字体对
* @param[in] index * @param[in] strFontId ID
* @return HFONT * @return HFONT
*/ */
static HFONT GetFont(std::size_t index); static HFONT GetFont(const std::wstring& strFontId);
/** /**
* @brief * @brief
* @param[in] strFontName * @param[in] strFontName
@ -236,11 +236,11 @@ public:
/** /**
* @brief * @brief
* @param[in] index * @param[in] strFontId ID
* @param[in] hDcPaint * @param[in] hDcPaint
* @return TFontInfo * @return TFontInfo
*/ */
static TFontInfo* GetFontInfo(std::size_t index, HDC hDcPaint); static TFontInfo* GetFontInfo(const std::wstring& strFontId, HDC hDcPaint);
/** /**
* @brief * @brief
@ -274,12 +274,12 @@ public:
/** /**
* @brief * @brief
* @param[in] index * @param[in] strFontId ID
* @return * @return
* @retval true * @retval true
* @retval false * @retval false
*/ */
static bool RemoveFontAt(std::size_t index); static bool RemoveFontAt(const std::wstring& strFontId);
/** /**
* @brief * @brief
@ -457,7 +457,7 @@ private:
static MapStringToImagePtr m_mImageHash; static MapStringToImagePtr m_mImageHash;
static std::map<std::wstring, DWORD> m_mapTextColor; static std::map<std::wstring, DWORD> m_mapTextColor;
static std::map<std::wstring, std::wstring> m_mGlobalClass; static std::map<std::wstring, std::wstring> m_mGlobalClass;
static std::vector<TFontInfo*> m_aCustomFonts; static std::map<std::wstring, TFontInfo*> m_mCustomFonts;
static short m_H; static short m_H;
static short m_S; static short m_S;

View File

@ -193,6 +193,7 @@ Box* WindowBuilder::Create(CreateControlCallback pCallback, Window* pManager, Bo
} }
else if( strClass == _T("Font") ) { else if( strClass == _T("Font") ) {
nAttributes = node.GetAttributeCount(); nAttributes = node.GetAttributeCount();
std::wstring strFontId;
std::wstring strFontName; std::wstring strFontName;
int size = 12; int size = 12;
bool bold = false; bool bold = false;
@ -201,7 +202,11 @@ Box* WindowBuilder::Create(CreateControlCallback pCallback, Window* pManager, Bo
for( int i = 0; i < nAttributes; i++ ) { for( int i = 0; i < nAttributes; i++ ) {
strName = node.GetAttributeName(i); strName = node.GetAttributeName(i);
strValue = node.GetAttributeValue(i); strValue = node.GetAttributeValue(i);
if( strName == _T("name") ) { if (strName == _T("id"))
{
strFontId = strValue;
}
else if( strName == _T("name") ) {
strFontName = strValue; strFontName = strValue;
} }
else if( strName == _T("size") ) { else if( strName == _T("size") ) {
@ -221,7 +226,7 @@ Box* WindowBuilder::Create(CreateControlCallback pCallback, Window* pManager, Bo
} }
} }
if( !strFontName.empty() ) { if( !strFontName.empty() ) {
GlobalManager::AddFont(strFontName, size, bold, underline, italic); GlobalManager::AddFont(strFontId, strFontName, size, bold, underline, italic);
} }
} }
else if( strClass == _T("Class") ) { else if( strClass == _T("Class") ) {

View File

@ -204,12 +204,12 @@ public:
virtual void DrawLine(const IPen* pen, int x1, int y1, int x2, int y2) = 0; virtual void DrawLine(const IPen* pen, int x1, int y1, int x2, int y2) = 0;
virtual void DrawBezier(const IPen* pen, int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4) = 0; virtual void DrawBezier(const IPen* pen, int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4) = 0;
virtual void DrawRect(const UiRect& rc, int nSize, DWORD dwPenColor) = 0; virtual void DrawRect(const UiRect& rc, int nSize, DWORD dwPenColor) = 0;
virtual void DrawText(const UiRect& rc, const std::wstring& strText, DWORD dwTextColor, int iFont, UINT uStyle, BYTE uFade = 255, bool bLineLimit = false) = 0; virtual void DrawText(const UiRect& rc, const std::wstring& strText, DWORD dwTextColor, const std::wstring& strFontId, UINT uStyle, BYTE uFade = 255, bool bLineLimit = false) = 0;
virtual void DrawEllipse(const UiRect& rc, int nSize, DWORD dwColor) = 0; virtual void DrawEllipse(const UiRect& rc, int nSize, DWORD dwColor) = 0;
virtual void FillEllipse(const UiRect& rc, DWORD dwColor) = 0; virtual void FillEllipse(const UiRect& rc, DWORD dwColor) = 0;
virtual UiRect MeasureText(const std::wstring& strText, int iFont, UINT uStyle, int width = DUI_NOSET_VALUE) = 0; virtual UiRect MeasureText(const std::wstring& strText, const std::wstring& strFontId, UINT uStyle, int width = DUI_NOSET_VALUE) = 0;
virtual void DrawPath(const IPath* path, const IPen* pen) = 0; virtual void DrawPath(const IPath* path, const IPen* pen) = 0;
virtual void FillPath(const IPath* path, const IBrush* brush) = 0; virtual void FillPath(const IPath* path, const IBrush* brush) = 0;

View File

@ -470,13 +470,13 @@ void RenderContext_GdiPlus::DrawRect(const UiRect& rc, int nSize, DWORD dwPenCol
graphics.DrawRectangle(&pen, rc.left, rc.top, rc.GetWidth(), rc.GetHeight()); graphics.DrawRectangle(&pen, rc.left, rc.top, rc.GetWidth(), rc.GetHeight());
} }
void RenderContext_GdiPlus::DrawText(const UiRect& rc, const std::wstring& strText, DWORD dwTextColor, int iFont, UINT uStyle, BYTE uFade /*= 255*/, bool bLineLimit /*= false*/) void RenderContext_GdiPlus::DrawText(const UiRect& rc, const std::wstring& strText, DWORD dwTextColor, const std::wstring& strFontId, UINT uStyle, BYTE uFade /*= 255*/, bool bLineLimit /*= false*/)
{ {
ASSERT(::GetObjectType(m_hDC)==OBJ_DC || ::GetObjectType(m_hDC)==OBJ_MEMDC); ASSERT(::GetObjectType(m_hDC)==OBJ_DC || ::GetObjectType(m_hDC)==OBJ_MEMDC);
if( strText.empty() ) return; if( strText.empty() ) return;
Gdiplus::Graphics graphics( m_hDC ); Gdiplus::Graphics graphics( m_hDC );
Gdiplus::Font font(m_hDC, GlobalManager::GetFont(iFont)); Gdiplus::Font font(m_hDC, GlobalManager::GetFont(strFontId));
Gdiplus::RectF rcPaint((Gdiplus::REAL)rc.left, (Gdiplus::REAL)rc.top, (Gdiplus::REAL)(rc.right - rc.left), (Gdiplus::REAL)(rc.bottom - rc.top)); Gdiplus::RectF rcPaint((Gdiplus::REAL)rc.left, (Gdiplus::REAL)rc.top, (Gdiplus::REAL)(rc.right - rc.left), (Gdiplus::REAL)(rc.bottom - rc.top));
int alpha = dwTextColor >> 24; int alpha = dwTextColor >> 24;
uFade *= double(alpha) / 255; uFade *= double(alpha) / 255;
@ -519,7 +519,7 @@ void RenderContext_GdiPlus::DrawText(const UiRect& rc, const std::wstring& strTe
stringFormat.SetLineAlignment(Gdiplus::StringAlignmentNear); stringFormat.SetLineAlignment(Gdiplus::StringAlignmentNear);
} }
else if ((uStyle & DT_VCENTER) != 0) { else if ((uStyle & DT_VCENTER) != 0) {
TFontInfo* fontInfo = GlobalManager::GetTFontInfo(iFont); TFontInfo* fontInfo = GlobalManager::GetTFontInfo(strFontId);
if (fontInfo->sFontName == L"ÐÂËÎÌå") { if (fontInfo->sFontName == L"ÐÂËÎÌå") {
if (rcPaint.Height >= fontInfo->iSize + 2) { if (rcPaint.Height >= fontInfo->iSize + 2) {
rcPaint.Offset(0, 1); rcPaint.Offset(0, 1);
@ -567,10 +567,10 @@ void RenderContext_GdiPlus::FillPath(const IPath* path, const IBrush* brush)
graphics.FillPath(((Brush_Gdiplus*)brush)->GetBrush(), ((Path_Gdiplus*)path)->GetPath()); graphics.FillPath(((Brush_Gdiplus*)brush)->GetBrush(), ((Path_Gdiplus*)path)->GetPath());
} }
ui::UiRect RenderContext_GdiPlus::MeasureText(const std::wstring& strText, int iFont, UINT uStyle, int width /*= DUI_NOSET_VALUE*/) ui::UiRect RenderContext_GdiPlus::MeasureText(const std::wstring& strText, const std::wstring& strFontId, UINT uStyle, int width /*= DUI_NOSET_VALUE*/)
{ {
Gdiplus::Graphics graphics(m_hDC); Gdiplus::Graphics graphics(m_hDC);
Gdiplus::Font font(m_hDC, GlobalManager::GetFont(iFont)); Gdiplus::Font font(m_hDC, GlobalManager::GetFont(strFontId));
Gdiplus::RectF bounds; Gdiplus::RectF bounds;
Gdiplus::StringFormat stringFormat = Gdiplus::StringFormat::GenericTypographic(); Gdiplus::StringFormat stringFormat = Gdiplus::StringFormat::GenericTypographic();

View File

@ -51,12 +51,12 @@ public:
virtual void DrawLine(const IPen* pen, int x1, int y1, int x2, int y2) override; virtual void DrawLine(const IPen* pen, int x1, int y1, int x2, int y2) override;
virtual void DrawBezier(const IPen* pen, int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4) override; virtual void DrawBezier(const IPen* pen, int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4) override;
virtual void DrawRect(const UiRect& rc, int nSize, DWORD dwPenColor) override; virtual void DrawRect(const UiRect& rc, int nSize, DWORD dwPenColor) override;
virtual void DrawText(const UiRect& rc, const std::wstring& strText, DWORD dwTextColor, int iFont, UINT uStyle, BYTE uFade = 255, bool bLineLimit = false) override; virtual void DrawText(const UiRect& rc, const std::wstring& strText, DWORD dwTextColor, const std::wstring& strFontId, UINT uStyle, BYTE uFade = 255, bool bLineLimit = false) override;
virtual void DrawEllipse(const UiRect& rc, int nSize, DWORD dwColor) override; virtual void DrawEllipse(const UiRect& rc, int nSize, DWORD dwColor) override;
virtual void FillEllipse(const UiRect& rc, DWORD dwColor) override; virtual void FillEllipse(const UiRect& rc, DWORD dwColor) override;
virtual UiRect MeasureText(const std::wstring& strText, int iFont, UINT uStyle, int width = DUI_NOSET_VALUE) override; virtual UiRect MeasureText(const std::wstring& strText, const std::wstring& strFontId, UINT uStyle, int width = DUI_NOSET_VALUE) override;
virtual void DrawPath(const IPath* path, const IPen* pen) override; virtual void DrawPath(const IPath* path, const IPen* pen) override;
virtual void FillPath(const IPath* path, const IBrush* brush) override; virtual void FillPath(const IPath* path, const IBrush* brush) override;