7 changed files with 670 additions and 79 deletions
-
108src/main/java/com/gaotao/modules/base/controller/FontController.java
-
10src/main/java/com/gaotao/modules/base/entity/ReportLabelList.java
-
30src/main/java/com/gaotao/modules/base/service/FontService.java
-
267src/main/java/com/gaotao/modules/base/service/Impl/FontServiceImpl.java
-
147src/main/java/com/gaotao/modules/base/service/Impl/LabelDataProcessorServiceImpl.java
-
15src/main/java/com/gaotao/modules/base/service/LabelDataProcessorService.java
-
172src/main/java/com/gaotao/modules/base/utils/ZplGenerator.java
@ -0,0 +1,108 @@ |
|||
package com.gaotao.modules.base.controller; |
|||
|
|||
import com.gaotao.modules.base.service.FontService; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.springframework.beans.factory.annotation.Autowired; |
|||
import org.springframework.web.bind.annotation.*; |
|||
|
|||
import java.util.HashMap; |
|||
import java.util.List; |
|||
import java.util.Map; |
|||
|
|||
/** |
|||
* 字体管理控制器 |
|||
*/ |
|||
@Slf4j |
|||
@RestController |
|||
@RequestMapping("/font") |
|||
public class FontController { |
|||
|
|||
@Autowired |
|||
private FontService fontService; |
|||
|
|||
/** |
|||
* 获取系统可用字体列表 |
|||
*/ |
|||
@RequestMapping("/available") |
|||
public Map<String, Object> getAvailableFonts() { |
|||
Map<String, Object> result = new HashMap<>(); |
|||
|
|||
try { |
|||
List<Map<String, String>> fonts = fontService.getAvailableFonts(); |
|||
|
|||
result.put("success", true); |
|||
result.put("data", fonts); |
|||
result.put("total", fonts.size()); |
|||
result.put("message", "获取字体列表成功"); |
|||
|
|||
log.info("返回字体列表,共 {} 个字体", fonts.size()); |
|||
|
|||
} catch (Exception e) { |
|||
log.error("获取字体列表失败", e); |
|||
|
|||
result.put("success", false); |
|||
result.put("data", null); |
|||
result.put("message", "获取字体列表失败: " + e.getMessage()); |
|||
} |
|||
|
|||
return result; |
|||
} |
|||
|
|||
/** |
|||
* 检查字体是否可用 |
|||
*/ |
|||
@GetMapping("/check/{fontName}") |
|||
public Map<String, Object> checkFont(@PathVariable String fontName) { |
|||
Map<String, Object> result = new HashMap<>(); |
|||
|
|||
try { |
|||
boolean available = fontService.isFontAvailable(fontName); |
|||
String fontFile = fontService.getFontFilePath(fontName); |
|||
|
|||
result.put("success", true); |
|||
result.put("fontName", fontName); |
|||
result.put("available", available); |
|||
result.put("fontFile", fontFile); |
|||
result.put("supported", fontFile != null); |
|||
|
|||
} catch (Exception e) { |
|||
log.error("检查字体失败: {}", fontName, e); |
|||
|
|||
result.put("success", false); |
|||
result.put("message", "检查字体失败: " + e.getMessage()); |
|||
} |
|||
|
|||
return result; |
|||
} |
|||
|
|||
/** |
|||
* 获取字体分类 |
|||
*/ |
|||
@GetMapping("/categories") |
|||
public Map<String, Object> getFontCategories() { |
|||
Map<String, Object> result = new HashMap<>(); |
|||
|
|||
try { |
|||
List<Map<String, String>> fonts = fontService.getAvailableFonts(); |
|||
|
|||
// 按类别分组 |
|||
Map<String, Integer> categories = new HashMap<>(); |
|||
for (Map<String, String> font : fonts) { |
|||
String category = font.get("category"); |
|||
categories.put(category, categories.getOrDefault(category, 0) + 1); |
|||
} |
|||
|
|||
result.put("success", true); |
|||
result.put("categories", categories); |
|||
result.put("message", "获取字体分类成功"); |
|||
|
|||
} catch (Exception e) { |
|||
log.error("获取字体分类失败", e); |
|||
|
|||
result.put("success", false); |
|||
result.put("message", "获取字体分类失败: " + e.getMessage()); |
|||
} |
|||
|
|||
return result; |
|||
} |
|||
} |
|||
@ -0,0 +1,30 @@ |
|||
package com.gaotao.modules.base.service; |
|||
|
|||
import java.util.List; |
|||
import java.util.Map; |
|||
|
|||
/** |
|||
* 字体服务接口 |
|||
*/ |
|||
public interface FontService { |
|||
|
|||
/** |
|||
* 获取系统可用字体列表 |
|||
* @return 字体列表 |
|||
*/ |
|||
List<Map<String, String>> getAvailableFonts(); |
|||
|
|||
/** |
|||
* 获取字体文件路径 |
|||
* @param fontName 字体名称 |
|||
* @return 字体文件路径,如果不存在返回null |
|||
*/ |
|||
String getFontFilePath(String fontName); |
|||
|
|||
/** |
|||
* 检查字体是否可用 |
|||
* @param fontName 字体名称 |
|||
* @return 是否可用 |
|||
*/ |
|||
boolean isFontAvailable(String fontName); |
|||
} |
|||
@ -0,0 +1,267 @@ |
|||
package com.gaotao.modules.base.service.Impl; |
|||
|
|||
import com.gaotao.modules.base.service.FontService; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.springframework.stereotype.Service; |
|||
|
|||
import java.awt.*; |
|||
import java.util.*; |
|||
import java.util.List; |
|||
|
|||
/** |
|||
* 字体服务实现类 |
|||
*/ |
|||
@Slf4j |
|||
@Service |
|||
public class FontServiceImpl implements FontService { |
|||
|
|||
// 字体名称到ZPL字体文件的映射 |
|||
private static final Map<String, String> FONT_FILE_MAPPING = new HashMap<>(); |
|||
|
|||
static { |
|||
// 中文字体映射 |
|||
FONT_FILE_MAPPING.put("Microsoft YaHei", "MSYH.TTF"); |
|||
FONT_FILE_MAPPING.put("微软雅黑", "MSYH.TTF"); |
|||
FONT_FILE_MAPPING.put("SimSun", "SIMSUN.TTC"); |
|||
FONT_FILE_MAPPING.put("宋体", "SIMSUN.TTC"); |
|||
FONT_FILE_MAPPING.put("SimHei", "SIMHEI.TTF"); |
|||
FONT_FILE_MAPPING.put("黑体", "SIMHEI.TTF"); |
|||
FONT_FILE_MAPPING.put("KaiTi", "KAITI.TTF"); |
|||
FONT_FILE_MAPPING.put("楷体", "KAITI.TTF"); |
|||
FONT_FILE_MAPPING.put("FangSong", "SIMFANG.TTF"); |
|||
FONT_FILE_MAPPING.put("仿宋", "SIMFANG.TTF"); |
|||
|
|||
// 英文字体映射 |
|||
FONT_FILE_MAPPING.put("Arial", "ARIAL.TTF"); |
|||
FONT_FILE_MAPPING.put("Times New Roman", "TIMES.TTF"); |
|||
FONT_FILE_MAPPING.put("Courier New", "COUR.TTF"); |
|||
FONT_FILE_MAPPING.put("Helvetica", "HELV.TTF"); |
|||
FONT_FILE_MAPPING.put("Verdana", "VERDANA.TTF"); |
|||
FONT_FILE_MAPPING.put("Georgia", "GEORGIA.TTF"); |
|||
FONT_FILE_MAPPING.put("Tahoma", "TAHOMA.TTF"); |
|||
FONT_FILE_MAPPING.put("Trebuchet MS", "TREBUC.TTF"); |
|||
FONT_FILE_MAPPING.put("Lucida Console", "LUCON.TTF"); |
|||
FONT_FILE_MAPPING.put("Impact", "IMPACT.TTF"); |
|||
FONT_FILE_MAPPING.put("Comic Sans MS", "COMIC.TTF"); |
|||
FONT_FILE_MAPPING.put("Palatino", "PALA.TTF"); |
|||
FONT_FILE_MAPPING.put("Garamond", "GARA.TTF"); |
|||
FONT_FILE_MAPPING.put("Bookman", "BOOKOS.TTF"); |
|||
} |
|||
|
|||
@Override |
|||
public List<Map<String, String>> getAvailableFonts() { |
|||
List<Map<String, String>> fontList = new ArrayList<>(); |
|||
|
|||
try { |
|||
// 获取系统图形环境 |
|||
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); |
|||
|
|||
// 获取所有可用字体的名字 |
|||
String[] fontNames = ge.getAvailableFontFamilyNames(); |
|||
|
|||
log.info("系统检测到 {} 个可用字体", fontNames.length); |
|||
|
|||
// 处理系统字体 |
|||
Set<String> processedFonts = new HashSet<>(); |
|||
|
|||
for (String fontName : fontNames) { |
|||
// 避免重复添加 |
|||
if (processedFonts.contains(fontName)) { |
|||
continue; |
|||
} |
|||
processedFonts.add(fontName); |
|||
|
|||
Map<String, String> fontInfo = new HashMap<>(); |
|||
fontInfo.put("name", fontName); |
|||
fontInfo.put("value", fontName); |
|||
fontInfo.put("category", getFontCategory(fontName)); |
|||
fontInfo.put("description", getFontDescription(fontName)); |
|||
|
|||
// 检查是否有对应的ZPL字体文件 |
|||
String fontFile = getFontFilePath(fontName); |
|||
if (fontFile != null) { |
|||
fontInfo.put("zplFile", fontFile); |
|||
fontInfo.put("supported", "true"); |
|||
} else { |
|||
fontInfo.put("supported", "false"); |
|||
} |
|||
|
|||
fontList.add(fontInfo); |
|||
} |
|||
|
|||
// 按类别和名称排序 |
|||
fontList.sort((f1, f2) -> { |
|||
// 默认字体排在最前面 |
|||
if ("default".equals(f1.get("value"))) return -1; |
|||
if ("default".equals(f2.get("value"))) return 1; |
|||
|
|||
// 按类别排序 |
|||
int categoryCompare = f1.get("category").compareTo(f2.get("category")); |
|||
if (categoryCompare != 0) { |
|||
return categoryCompare; |
|||
} |
|||
|
|||
// 同类别按名称排序 |
|||
return f1.get("name").compareTo(f2.get("name")); |
|||
}); |
|||
|
|||
log.info("处理后可用字体数量: {}", fontList.size()); |
|||
|
|||
} catch (Exception e) { |
|||
log.error("获取系统字体失败", e); |
|||
|
|||
// 如果获取系统字体失败,返回基本字体列表 |
|||
fontList = getBasicFontList(); |
|||
} |
|||
|
|||
return fontList; |
|||
} |
|||
|
|||
@Override |
|||
public String getFontFilePath(String fontName) { |
|||
if (fontName == null || fontName.isEmpty()) { |
|||
return null; |
|||
} |
|||
|
|||
// 直接查找映射 |
|||
String fontFile = FONT_FILE_MAPPING.get(fontName); |
|||
if (fontFile != null) { |
|||
return fontFile; |
|||
} |
|||
|
|||
// 尝试模糊匹配 |
|||
for (Map.Entry<String, String> entry : FONT_FILE_MAPPING.entrySet()) { |
|||
if (fontName.toLowerCase().contains(entry.getKey().toLowerCase()) || |
|||
entry.getKey().toLowerCase().contains(fontName.toLowerCase())) { |
|||
return entry.getValue(); |
|||
} |
|||
} |
|||
|
|||
return null; |
|||
} |
|||
|
|||
@Override |
|||
public boolean isFontAvailable(String fontName) { |
|||
if (fontName == null || fontName.isEmpty() || "default".equals(fontName)) { |
|||
return true; |
|||
} |
|||
|
|||
try { |
|||
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); |
|||
String[] fontNames = ge.getAvailableFontFamilyNames(); |
|||
|
|||
for (String availableFont : fontNames) { |
|||
if (availableFont.equals(fontName)) { |
|||
return true; |
|||
} |
|||
} |
|||
} catch (Exception e) { |
|||
log.warn("检查字体可用性失败: {}", fontName, e); |
|||
} |
|||
|
|||
return false; |
|||
} |
|||
|
|||
/** |
|||
* 获取字体类别 |
|||
*/ |
|||
private String getFontCategory(String fontName) { |
|||
if (fontName == null) { |
|||
return "other"; |
|||
} |
|||
|
|||
String lowerName = fontName.toLowerCase(); |
|||
|
|||
// 中文字体 |
|||
if (lowerName.contains("微软雅黑") || lowerName.contains("microsoft yahei") || |
|||
lowerName.contains("宋体") || lowerName.contains("simsun") || |
|||
lowerName.contains("黑体") || lowerName.contains("simhei") || |
|||
lowerName.contains("楷体") || lowerName.contains("kaiti") || |
|||
lowerName.contains("仿宋") || lowerName.contains("fangsong")) { |
|||
return "chinese"; |
|||
} |
|||
|
|||
// 常用英文字体 |
|||
if (lowerName.contains("arial") || lowerName.contains("times") || |
|||
lowerName.contains("courier") || lowerName.contains("helvetica") || |
|||
lowerName.contains("verdana") || lowerName.contains("georgia") || |
|||
lowerName.contains("tahoma") || lowerName.contains("calibri")) { |
|||
return "english"; |
|||
} |
|||
|
|||
// 等宽字体 |
|||
if (lowerName.contains("mono") || lowerName.contains("courier") || |
|||
lowerName.contains("consolas") || lowerName.contains("lucida console")) { |
|||
return "monospace"; |
|||
} |
|||
|
|||
// 装饰字体 |
|||
if (lowerName.contains("comic") || lowerName.contains("impact") || |
|||
lowerName.contains("brush") || lowerName.contains("script")) { |
|||
return "decorative"; |
|||
} |
|||
|
|||
return "other"; |
|||
} |
|||
|
|||
/** |
|||
* 获取字体描述 |
|||
*/ |
|||
private String getFontDescription(String fontName) { |
|||
if (fontName == null) { |
|||
return ""; |
|||
} |
|||
|
|||
String category = getFontCategory(fontName); |
|||
switch (category) { |
|||
case "chinese": |
|||
return "中文字体"; |
|||
case "english": |
|||
return "英文字体"; |
|||
case "monospace": |
|||
return "等宽字体"; |
|||
case "decorative": |
|||
return "装饰字体"; |
|||
default: |
|||
return "系统字体"; |
|||
} |
|||
} |
|||
|
|||
/** |
|||
* 获取基本字体列表(备用方案) |
|||
*/ |
|||
private List<Map<String, String>> getBasicFontList() { |
|||
List<Map<String, String>> basicFonts = new ArrayList<>(); |
|||
|
|||
// 基本字体配置 |
|||
String[][] basicFontConfig = { |
|||
{"默认字体", "default", "system", "系统默认字体"}, |
|||
{"微软雅黑", "Microsoft YaHei", "chinese", "现代中文字体"}, |
|||
{"宋体", "SimSun", "chinese", "传统中文字体"}, |
|||
{"黑体", "SimHei", "chinese", "粗体中文字体"}, |
|||
{"Arial", "Arial", "english", "无衬线英文字体"}, |
|||
{"Times New Roman", "Times New Roman", "english", "衬线英文字体"}, |
|||
{"Courier New", "Courier New", "monospace", "等宽英文字体"} |
|||
}; |
|||
|
|||
for (String[] config : basicFontConfig) { |
|||
Map<String, String> fontInfo = new HashMap<>(); |
|||
fontInfo.put("name", config[0]); |
|||
fontInfo.put("value", config[1]); |
|||
fontInfo.put("category", config[2]); |
|||
fontInfo.put("description", config[3]); |
|||
|
|||
String fontFile = getFontFilePath(config[1]); |
|||
if (fontFile != null) { |
|||
fontInfo.put("zplFile", fontFile); |
|||
fontInfo.put("supported", "true"); |
|||
} else { |
|||
fontInfo.put("supported", "false"); |
|||
} |
|||
|
|||
basicFonts.add(fontInfo); |
|||
} |
|||
|
|||
return basicFonts; |
|||
} |
|||
} |
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue