mirror of
https://github.com/cocos/cocos4.git
synced 2026-11-21 16:12:40 +08:00
fix windows font size bug (#14448)
This commit is contained in:
@@ -224,19 +224,25 @@ void CanvasRenderingContext2D::setFont(const ccstd::string &font) {
|
||||
ccstd::string fontName = "Arial";
|
||||
ccstd::string fontSizeStr = "30";
|
||||
|
||||
// support get font name from `60px American` or `60px "American abc-abc_abc"`
|
||||
std::regex re("(bold)?\\s*((\\d+)([\\.]\\d+)?)px\\s+([\\w-]+|\"[\\w -]+\"$)");
|
||||
std::regex re(R"(\s*((\d+)([\.]\d+)?)px\s+([^\r\n]*))");
|
||||
std::match_results<ccstd::string::const_iterator> results;
|
||||
if (std::regex_search(_font.cbegin(), _font.cend(), results, re)) {
|
||||
boldStr = results[1].str();
|
||||
fontSizeStr = results[2].str();
|
||||
fontName = results[5].str();
|
||||
// support get font name from `60px American` or `60px "American abc-abc_abc"`
|
||||
// support get font name contain space,example `times new roman`
|
||||
// if regex rule that does not conform to the rules,such as Chinese,it defaults to Arial
|
||||
std::match_results<ccstd::string::const_iterator> fontResults;
|
||||
std::regex fontRe(R"(([\w\s-]+|"[\w\s-]+"$))");
|
||||
ccstd::string tmp(results[4].str());
|
||||
if (std::regex_match(tmp, fontResults, fontRe)) {
|
||||
fontName = results[4].str();
|
||||
}
|
||||
}
|
||||
bool isItalic = font.find("italic", 0) != ccstd::string::npos || font.find("Italic", 0) != ccstd::string::npos;
|
||||
|
||||
auto fontSize = static_cast<float>(atof(fontSizeStr.c_str()));
|
||||
bool isBold = !boldStr.empty() || font.find("bold", 0) != ccstd::string::npos || font.find("Bold", 0) != ccstd::string::npos;
|
||||
//SE_LOGD("CanvasRenderingContext2D::set_font: %s, Size: %f, isBold: %b\n", fontName.c_str(), fontSize, isBold);
|
||||
_delegate->updateFont(fontName, fontSize, isBold, isItalic, false, false);
|
||||
bool isItalic = font.find("italic", 0) != ccstd::string::npos || font.find("Italic", 0) != ccstd::string::npos;
|
||||
_delegate->updateFont(fontName, static_cast<float>(fontSize), isBold, isItalic, false, false);
|
||||
}
|
||||
#elif CC_PLATFORM == CC_PLATFORM_QNX
|
||||
if (_font != font) {
|
||||
|
||||
Reference in New Issue
Block a user