|
|
@@ -29,6 +29,7 @@ import java.awt.*;
|
|
|
import java.awt.image.BufferedImage;
|
|
|
import java.io.ByteArrayOutputStream;
|
|
|
import java.io.File;
|
|
|
+import java.io.InputStream;
|
|
|
import java.util.List;
|
|
|
import java.util.concurrent.CompletableFuture;
|
|
|
|
|
|
@@ -46,6 +47,9 @@ public class NumberController {
|
|
|
// 预览框尺寸(固定)
|
|
|
private static final int previewWidth = 600;
|
|
|
|
|
|
+ // 字体缓存,避免重复加载
|
|
|
+ private static Font simheiFont = null;
|
|
|
+
|
|
|
/**
|
|
|
*
|
|
|
* @return key:队伍名称 value:队伍运动员
|
|
|
@@ -412,6 +416,32 @@ public class NumberController {
|
|
|
/**
|
|
|
* 在模版上绘制赛事名称
|
|
|
*/
|
|
|
+ /**
|
|
|
+ * 加载黑体字体
|
|
|
+ * @return 加载的黑体字体,如果加载失败则返回系统默认字体
|
|
|
+ */
|
|
|
+ private Font loadCustomFont(String fontPath, int fontSize) {
|
|
|
+ try {
|
|
|
+ if (simheiFont == null) {
|
|
|
+ // 从资源目录加载字体文件
|
|
|
+ try (InputStream is = getClass().getClassLoader().getResourceAsStream("fonts/"+fontPath+".ttf")) {
|
|
|
+ if (is != null) {
|
|
|
+ simheiFont = Font.createFont(Font.TRUETYPE_FONT, is);
|
|
|
+ log.info("成功加载{}字体文件", fontPath);
|
|
|
+ } else {
|
|
|
+ log.warn("未找到字体文件: fonts/{}.ttf", fontPath);
|
|
|
+ return new Font("SimHei", Font.BOLD, fontSize);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 调整字体大小并返回
|
|
|
+ return simheiFont.deriveFont(Font.BOLD, fontSize);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("加载字体失败,使用系统默认字体", e);
|
|
|
+ return new Font("SimHei", Font.BOLD, fontSize);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
private void drawEventNameOnTemplate(Graphics2D g2d, String eventName, GenerateBibBo bibParam, int canvasWidth, int canvasHeight) {
|
|
|
try {
|
|
|
// 使用前端传递的像素坐标
|
|
|
@@ -421,14 +451,15 @@ public class NumberController {
|
|
|
// 使用前端传递的字体大小
|
|
|
int fontSize = bibParam.getEventFontSize() != null ? bibParam.getEventFontSize() : 36;
|
|
|
|
|
|
- // 使用前端传递的字体名称,否则使用默认字体 "SimHei"
|
|
|
+ // 使用前端传递的字体名称,否则使用默认字体
|
|
|
String fontName = bibParam.getFontName();
|
|
|
- if (fontName == null) {
|
|
|
- fontName = "SimHei";
|
|
|
- } else if ("simhei".equalsIgnoreCase(fontName)) {
|
|
|
- fontName = "SimHei";
|
|
|
+ Font font;
|
|
|
+ if (fontName == null || "simhei".equalsIgnoreCase(fontName) || "SimHei".equals(fontName)) {
|
|
|
+ // 如果指定了黑体或未指定字体,则使用自定义加载的黑体字体,确保线上环境字体一致
|
|
|
+ font = loadCustomFont("simhei", fontSize);
|
|
|
+ } else {
|
|
|
+ font = loadCustomFont(fontName, fontSize);
|
|
|
}
|
|
|
- Font font = new Font(fontName, Font.BOLD, fontSize);
|
|
|
g2d.setFont(font);
|
|
|
|
|
|
// 设置颜色
|