|
|
|
@ -444,6 +444,176 @@ void MsgHandler::GtstrLength(int iVal, char *p)
|
|
|
|
memcpy(p, szTemp.c_str(), 4);
|
|
|
|
memcpy(p, szTemp.c_str(), 4);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 判断是否为中文字符(包括中文标点)
|
|
|
|
|
|
|
|
bool MsgHandler::is_chinese_char(const std::string& str, size_t start_byte) {
|
|
|
|
|
|
|
|
if (start_byte >= str.length()) return false;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
uint8_t c = static_cast<uint8_t>(str[start_byte]);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ASCII字符不是中文
|
|
|
|
|
|
|
|
if (c < 0x80) return false;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 获取Unicode码点
|
|
|
|
|
|
|
|
uint32_t code_point = 0;
|
|
|
|
|
|
|
|
int bytes = 0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ((c & 0xF8) == 0xF0 && start_byte + 3 < str.length()) {
|
|
|
|
|
|
|
|
code_point = ((c & 0x07) << 18) |
|
|
|
|
|
|
|
|
((static_cast<uint8_t>(str[start_byte+1]) & 0x3F) << 12) |
|
|
|
|
|
|
|
|
((static_cast<uint8_t>(str[start_byte+2]) & 0x3F) << 6) |
|
|
|
|
|
|
|
|
(static_cast<uint8_t>(str[start_byte+3]) & 0x3F);
|
|
|
|
|
|
|
|
bytes = 4;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
else if ((c & 0xF0) == 0xE0 && start_byte + 2 < str.length()) {
|
|
|
|
|
|
|
|
code_point = ((c & 0x0F) << 12) |
|
|
|
|
|
|
|
|
((static_cast<uint8_t>(str[start_byte+1]) & 0x3F) << 6) |
|
|
|
|
|
|
|
|
(static_cast<uint8_t>(str[start_byte+2]) & 0x3F);
|
|
|
|
|
|
|
|
bytes = 3;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
else if ((c & 0xE0) == 0xC0 && start_byte + 1 < str.length()) {
|
|
|
|
|
|
|
|
code_point = ((c & 0x1F) << 6) |
|
|
|
|
|
|
|
|
(static_cast<uint8_t>(str[start_byte+1]) & 0x3F);
|
|
|
|
|
|
|
|
bytes = 2;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (bytes == 0) return false;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 中文汉字范围
|
|
|
|
|
|
|
|
if ((code_point >= 0x4E00 && code_point <= 0x9FFF) ||
|
|
|
|
|
|
|
|
(code_point >= 0x3400 && code_point <= 0x4DBF) ||
|
|
|
|
|
|
|
|
(code_point >= 0x20000 && code_point <= 0x2A6DF) ||
|
|
|
|
|
|
|
|
(code_point >= 0x2A700 && code_point <= 0x2B73F) ||
|
|
|
|
|
|
|
|
(code_point >= 0x2B740 && code_point <= 0x2B81F) ||
|
|
|
|
|
|
|
|
(code_point >= 0x2B820 && code_point <= 0x2CEAF) ||
|
|
|
|
|
|
|
|
(code_point >= 0x2CEB0 && code_point <= 0x2EBEF) ||
|
|
|
|
|
|
|
|
(code_point >= 0x30000 && code_point <= 0x3134F)) {
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 中文标点范围
|
|
|
|
|
|
|
|
if ((code_point >= 0x3000 && code_point <= 0x303F) ||
|
|
|
|
|
|
|
|
(code_point >= 0xFF00 && code_point <= 0xFFEF)) {
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 获取单个字符的显示宽度
|
|
|
|
|
|
|
|
int MsgHandler::get_utf8_char_width(const std::string& str, size_t start_byte) {
|
|
|
|
|
|
|
|
if (start_byte >= str.length()) return 1;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
uint8_t c = static_cast<uint8_t>(str[start_byte]);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ASCII字符占1个位置
|
|
|
|
|
|
|
|
if (c < 0x80) {
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 判断是否为中文字符
|
|
|
|
|
|
|
|
if (is_chinese_char(str, start_byte)) {
|
|
|
|
|
|
|
|
return 2; // 中文字符占2个位置
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 其他宽字符(如全角字符、emoji等)
|
|
|
|
|
|
|
|
return 2; // 默认按2个位置处理
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 获取整个字符串的显示宽度
|
|
|
|
|
|
|
|
size_t MsgHandler::get_utf8_display_width(const std::string& str) {
|
|
|
|
|
|
|
|
size_t width = 0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (size_t i = 0; i < str.length(); ) {
|
|
|
|
|
|
|
|
uint8_t c = static_cast<uint8_t>(str[i]);
|
|
|
|
|
|
|
|
size_t char_len = 1;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ((c & 0x80) == 0x00) {
|
|
|
|
|
|
|
|
char_len = 1;
|
|
|
|
|
|
|
|
} else if ((c & 0xE0) == 0xC0) {
|
|
|
|
|
|
|
|
if (i + 1 >= str.length()) break;
|
|
|
|
|
|
|
|
char_len = 2;
|
|
|
|
|
|
|
|
} else if ((c & 0xF0) == 0xE0) {
|
|
|
|
|
|
|
|
if (i + 2 >= str.length()) break;
|
|
|
|
|
|
|
|
char_len = 3;
|
|
|
|
|
|
|
|
} else if ((c & 0xF8) == 0xF0) {
|
|
|
|
|
|
|
|
if (i + 3 >= str.length()) break;
|
|
|
|
|
|
|
|
char_len = 4;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
width += get_utf8_char_width(str, i);
|
|
|
|
|
|
|
|
i += char_len;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return width;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 按显示宽度分割字符串
|
|
|
|
|
|
|
|
std::vector<std::string> MsgHandler::split_utf8_by_width(const std::string& str, size_t max_width) {
|
|
|
|
|
|
|
|
std::vector<std::string> result;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (str.empty() || max_width == 0) return result;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
size_t byte_pos = 0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
while (byte_pos < str.length()) {
|
|
|
|
|
|
|
|
size_t line_start = byte_pos;
|
|
|
|
|
|
|
|
size_t line_width = 0;
|
|
|
|
|
|
|
|
size_t last_valid_pos = byte_pos;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 收集一行的字符
|
|
|
|
|
|
|
|
while (byte_pos < str.length() && line_width < max_width) {
|
|
|
|
|
|
|
|
uint8_t c = static_cast<uint8_t>(str[byte_pos]);
|
|
|
|
|
|
|
|
size_t char_len = 1;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ((c & 0x80) == 0x00) {
|
|
|
|
|
|
|
|
char_len = 1;
|
|
|
|
|
|
|
|
} else if ((c & 0xE0) == 0xC0) {
|
|
|
|
|
|
|
|
if (byte_pos + 1 >= str.length()) break;
|
|
|
|
|
|
|
|
char_len = 2;
|
|
|
|
|
|
|
|
} else if ((c & 0xF0) == 0xE0) {
|
|
|
|
|
|
|
|
if (byte_pos + 2 >= str.length()) break;
|
|
|
|
|
|
|
|
char_len = 3;
|
|
|
|
|
|
|
|
} else if ((c & 0xF8) == 0xF0) {
|
|
|
|
|
|
|
|
if (byte_pos + 3 >= str.length()) break;
|
|
|
|
|
|
|
|
char_len = 4;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int char_width = get_utf8_char_width(str, byte_pos);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 检查是否还能放下这个字符
|
|
|
|
|
|
|
|
if (line_width + char_width > max_width) {
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
last_valid_pos = byte_pos + char_len - 1;
|
|
|
|
|
|
|
|
byte_pos += char_len;
|
|
|
|
|
|
|
|
line_width += char_width;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 确保至少有一个字符
|
|
|
|
|
|
|
|
if (last_valid_pos < line_start && byte_pos < str.length()) {
|
|
|
|
|
|
|
|
uint8_t c = static_cast<uint8_t>(str[byte_pos]);
|
|
|
|
|
|
|
|
size_t char_len = 1;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ((c & 0xE0) == 0xC0) char_len = 2;
|
|
|
|
|
|
|
|
else if ((c & 0xF0) == 0xE0) char_len = 3;
|
|
|
|
|
|
|
|
else if ((c & 0xF8) == 0xF0) char_len = 4;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
last_valid_pos = byte_pos + char_len - 1;
|
|
|
|
|
|
|
|
byte_pos = last_valid_pos + 1;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 提取这一行
|
|
|
|
|
|
|
|
if (last_valid_pos >= line_start) {
|
|
|
|
|
|
|
|
result.push_back(str.substr(line_start, last_valid_pos - line_start + 1));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
byte_pos = last_valid_pos + 1;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//HS-K33打印通用称重
|
|
|
|
//HS-K33打印通用称重
|
|
|
|
void MsgHandler::Print1_3(const std::map<std::string, std::string>& msg)
|
|
|
|
void MsgHandler::Print1_3(const std::map<std::string, std::string>& msg)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
@ -1101,20 +1271,62 @@ void MsgHandler::Print1_3(const std::map<std::string, std::string>& msg)
|
|
|
|
|
|
|
|
|
|
|
|
//=========备注=====================
|
|
|
|
//=========备注=====================
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const size_t MAX_DISPLAY_WIDTH = 32; // 每行最多32个显示位置
|
|
|
|
|
|
|
|
|
|
|
|
m_newObj.clear();
|
|
|
|
m_newObj.clear();
|
|
|
|
m_newObj[1] = ("│");
|
|
|
|
m_newObj[1] = ("│");
|
|
|
|
m_newObj[26] = ("提示");
|
|
|
|
m_newObj[26] = ("提示");
|
|
|
|
m_newObj[120] = ("│");
|
|
|
|
m_newObj[120] = ("│");
|
|
|
|
|
|
|
|
|
|
|
|
it = msg.find("Warning");
|
|
|
|
it = msg.find("Warning");
|
|
|
|
if(it != msg.end())
|
|
|
|
if(it != msg.end())
|
|
|
|
{
|
|
|
|
{
|
|
|
|
|
|
|
|
// 检查显示宽度是否超过限制
|
|
|
|
|
|
|
|
size_t display_width = get_utf8_display_width(it->second);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (display_width > MAX_DISPLAY_WIDTH) {
|
|
|
|
|
|
|
|
// 需要分割成多行显示
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 第一行:显示前32个宽度的内容
|
|
|
|
|
|
|
|
std::vector<std::string> lines = split_utf8_by_width(it->second, MAX_DISPLAY_WIDTH);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 输出第一行(包含前缀)
|
|
|
|
|
|
|
|
m_newObj[145] = lines[0];
|
|
|
|
|
|
|
|
m_newObj[528] = ("│");
|
|
|
|
|
|
|
|
blTemp = BuildStringLine(m_newObj);
|
|
|
|
|
|
|
|
m_PrintData.insert(m_PrintData.end(), blTemp.begin(), blTemp.end());
|
|
|
|
|
|
|
|
m_PrintData.insert(m_PrintData.end(), {0x0A});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 输出剩余行(不包含前缀,但有边框)
|
|
|
|
|
|
|
|
for (size_t i = 1; i < lines.size(); i++) {
|
|
|
|
|
|
|
|
m_newObj.clear();
|
|
|
|
|
|
|
|
m_newObj[1] = ("│");
|
|
|
|
|
|
|
|
m_newObj[120] = ("│");
|
|
|
|
|
|
|
|
m_newObj[145] = lines[i];
|
|
|
|
|
|
|
|
m_newObj[528] = ("│");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
blTemp = BuildStringLine(m_newObj);
|
|
|
|
|
|
|
|
m_PrintData.insert(m_PrintData.end(), blTemp.begin(), blTemp.end());
|
|
|
|
|
|
|
|
m_PrintData.insert(m_PrintData.end(), {0x0A});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
else {
|
|
|
|
|
|
|
|
// 宽度不超过限制,正常显示
|
|
|
|
m_newObj[145] = it->second;
|
|
|
|
m_newObj[145] = it->second;
|
|
|
|
|
|
|
|
m_newObj[528] = ("│");
|
|
|
|
|
|
|
|
blTemp = BuildStringLine(m_newObj);
|
|
|
|
|
|
|
|
m_PrintData.insert(m_PrintData.end(), blTemp.begin(), blTemp.end());
|
|
|
|
|
|
|
|
m_PrintData.insert(m_PrintData.end(), {0x0A});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
else {
|
|
|
|
|
|
|
|
// 没有找到Warning,显示空内容
|
|
|
|
m_newObj[528] = ("│");
|
|
|
|
m_newObj[528] = ("│");
|
|
|
|
|
|
|
|
m_newObj[145] = "";
|
|
|
|
blTemp = BuildStringLine(m_newObj);
|
|
|
|
blTemp = BuildStringLine(m_newObj);
|
|
|
|
m_PrintData.insert(m_PrintData.end(), blTemp.begin(), blTemp.end());
|
|
|
|
m_PrintData.insert(m_PrintData.end(), blTemp.begin(), blTemp.end());
|
|
|
|
m_PrintData.insert(m_PrintData.end(), {0x0A});
|
|
|
|
m_PrintData.insert(m_PrintData.end(), {0x0A});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
m_newObj.clear();
|
|
|
|
m_newObj.clear();
|
|
|
|
m_newObj[1] = ("└");
|
|
|
|
m_newObj[1] = ("└");
|
|
|
|
|