package com.zbooksoft.gdmis.common;
|
|
import java.util.ArrayList;
|
import java.util.List;
|
|
/**
|
* @Description:
|
* @Author: zhai
|
* @Date: 2025/8/12
|
**/
|
public class TscUtil {
|
public static List<String> wrapText(String text, int maxWidth, int size) {
|
List<String> lines = new ArrayList<>();
|
String[] words = text.split("");
|
StringBuilder currentLine = new StringBuilder();
|
int currentWidth = 0;
|
|
for (String word : words) {
|
// 简单估算字符宽度(中文字符宽度为2,英文字符宽度为1)
|
int wordWidth = word.matches("[\\u4e00-\\u9fa5]") ? 2 : 1;
|
if (currentWidth + wordWidth > maxWidth) {
|
lines.add(currentLine.toString());
|
currentLine = new StringBuilder(word);
|
currentWidth = wordWidth;
|
} else {
|
currentLine.append(word);
|
currentWidth += wordWidth;
|
}
|
}
|
|
if (currentLine.length() > 0) {
|
lines.add(currentLine.toString());
|
}
|
if (lines.size() > size) {
|
return lines.subList(0, size);
|
}
|
|
return lines;
|
}
|
|
public static int getTextLong(String text) {
|
String[] words = text.split("");
|
int currentWidth = 0;
|
for (String word : words) {
|
// 简单估算字符宽度(中文字符宽度为2,英文字符宽度为1)
|
currentWidth += word.matches("[\\u4e00-\\u9fa5]") ? 2 : 1;
|
}
|
return currentWidth;
|
}
|
|
public static String replaceEnglishQuotesWithChinese(String text) {
|
if (text == null || text.isEmpty()) {
|
return text;
|
}
|
|
StringBuilder result = new StringBuilder();
|
boolean isOpenQuote = true; // 标记当前应该是开引号还是闭引号
|
|
for (char c : text.toCharArray()) {
|
if (c == '"') {
|
// 根据标记决定使用开引号还是闭引号
|
result.append(isOpenQuote ? '“' : '”');
|
// 切换状态
|
isOpenQuote = !isOpenQuote;
|
} else {
|
result.append(c);
|
}
|
}
|
return result.toString();
|
}
|
}
|