Fix charset process for fontName on win32 [ci build]

Since we use freetype, never needs create ttf font by win32 API
This commit is contained in:
halx99 2021-05-18 23:50:23 +08:00 committed by GitHub
parent 59ec137b7b
commit abc5f93c02
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 12 additions and 85 deletions

View File

@ -27,6 +27,9 @@ THE SOFTWARE.
#include "platform/CCFileUtils.h" #include "platform/CCFileUtils.h"
#include "platform/CCStdC.h" #include "platform/CCStdC.h"
#define NTCVT_CP_DEFAULT CP_UTF8
#include "win32-specific/ntcvt/ntcvt.hpp"
NS_CC_BEGIN NS_CC_BEGIN
int Device::getDPI() int Device::getDPI()
@ -74,67 +77,20 @@ public:
removeCustomFont(); removeCustomFont();
} }
wchar_t * utf8ToUtf16(const std::string& str) bool setFont(const char * pFontName, int nSize = 0, bool enableBold = false)
{
wchar_t * pwszBuffer = nullptr;
do
{
if (str.empty())
{
break;
}
// utf-8 to utf-16
int nLen = str.size();
int nBufLen = nLen + 1;
pwszBuffer = new wchar_t[nBufLen];
CC_BREAK_IF(!pwszBuffer);
memset(pwszBuffer, 0, nBufLen);
nLen = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), nLen, pwszBuffer, nBufLen);
pwszBuffer[nLen] = '\0';
} while (0);
return pwszBuffer;
}
bool setFont(const char * pFontName = nullptr, int nSize = 0, bool enableBold = false)
{ {
bool bRet = false; bool bRet = false;
do do
{ {
std::string fontName = pFontName; std::wstring fontName = ntcvt::from_chars(pFontName);
std::string fontPath;
HFONT hDefFont = (HFONT)GetStockObject(DEFAULT_GUI_FONT); HFONT hDefFont = (HFONT)GetStockObject(DEFAULT_GUI_FONT);
LOGFONTA tNewFont = { 0 }; LOGFONTW tNewFont = { 0 };
LOGFONTA tOldFont = { 0 }; LOGFONTW tOldFont = { 0 };
GetObjectA(hDefFont, sizeof(tNewFont), &tNewFont); GetObjectW(hDefFont, sizeof(tNewFont), &tNewFont);
if (!fontName.empty()) if (!fontName.empty())
{ {
// create font from ttf file
if (FileUtils::getInstance()->getFileExtension(fontName) == ".ttf")
{
fontPath = FileUtils::getInstance()->fullPathForFilename(fontName.c_str());
int nFindPos = fontName.rfind("/");
fontName = &fontName[nFindPos + 1];
nFindPos = fontName.rfind(".");
fontName = fontName.substr(0, nFindPos);
}
else
{
auto nFindPos = fontName.rfind("/");
if (nFindPos != fontName.npos)
{
if (fontName.length() == nFindPos + 1)
{
fontName = "";
}
else
{
fontName = &fontName[nFindPos + 1];
}
}
}
tNewFont.lfCharSet = DEFAULT_CHARSET; tNewFont.lfCharSet = DEFAULT_CHARSET;
strcpy_s(tNewFont.lfFaceName, LF_FACESIZE, fontName.c_str()); wcscpy_s(tNewFont.lfFaceName, LF_FACESIZE, fontName.c_str());
} }
if (nSize) if (nSize)
@ -151,11 +107,11 @@ public:
tNewFont.lfWeight = FW_NORMAL; tNewFont.lfWeight = FW_NORMAL;
} }
GetObjectA(_font, sizeof(tOldFont), &tOldFont); GetObjectW(_font, sizeof(tOldFont), &tOldFont);
if (tOldFont.lfHeight == tNewFont.lfHeight if (tOldFont.lfHeight == tNewFont.lfHeight
&& tOldFont.lfWeight == tNewFont.lfWeight && tOldFont.lfWeight == tNewFont.lfWeight
&& 0 == strcmp(tOldFont.lfFaceName, tNewFont.lfFaceName)) && 0 == wcscmp(tOldFont.lfFaceName, tNewFont.lfFaceName))
{ {
bRet = true; bRet = true;
break; break;
@ -164,28 +120,13 @@ public:
// delete old font // delete old font
removeCustomFont(); removeCustomFont();
if (!fontPath.empty())
{
_curFontPath = fontPath;
wchar_t * pwszBuffer = utf8ToUtf16(_curFontPath);
if (pwszBuffer)
{
if (AddFontResource(pwszBuffer))
{
PostMessage(_wnd, WM_FONTCHANGE, 0, 0);
}
delete[] pwszBuffer;
pwszBuffer = nullptr;
}
}
_font = nullptr; _font = nullptr;
// disable Cleartype // disable Cleartype
tNewFont.lfQuality = ANTIALIASED_QUALITY; tNewFont.lfQuality = ANTIALIASED_QUALITY;
// create new font // create new font
_font = CreateFontIndirectA(&tNewFont); _font = CreateFontIndirectW(&tNewFont);
if (!_font) if (!_font)
{ {
// create failed, use default font // create failed, use default font
@ -450,7 +391,6 @@ private:
friend class Image; friend class Image;
HFONT _font; HFONT _font;
HWND _wnd; HWND _wnd;
std::string _curFontPath;
void removeCustomFont() void removeCustomFont()
{ {
@ -460,19 +400,6 @@ private:
DeleteObject(_font); DeleteObject(_font);
_font = hDefFont; _font = hDefFont;
} }
// release temp font resource
if (!_curFontPath.empty())
{
wchar_t * pwszBuffer = utf8ToUtf16(_curFontPath);
if (pwszBuffer)
{
RemoveFontResource(pwszBuffer);
PostMessage(_wnd, WM_FONTCHANGE, 0, 0);
delete[] pwszBuffer;
pwszBuffer = nullptr;
}
_curFontPath.clear();
}
} }
}; };