diff --git a/src/utils/zplGenerator.js b/src/utils/zplGenerator.js index 58991e7..85fab95 100644 --- a/src/utils/zplGenerator.js +++ b/src/utils/zplGenerator.js @@ -1,5 +1,8 @@ import { CoordinateTransformer } from './coordinateTransform.js' +const DEFAULT_FONT_FILE = 'ARIAL UNICODE MS.TTF' +const DEFAULT_FONT_LETTER = 'J' + /** * ZPL代码生成器 * 统一处理不同打印方向的ZPL代码生成 @@ -331,6 +334,8 @@ export class ZPLGenerator { '新細明體': 'P', 'DFKai-SB': 'Q', '標楷體': 'Q', + 'Arial Unicode MS': 'J', + 'Arial Unicode': 'J', 'Arial': 'A', 'Times New Roman': 'S', 'Courier New': 'T', @@ -364,14 +369,16 @@ export class ZPLGenerator { 'Marker Felt': 'V', 'Zapfino': 'W', 'Snell Roundhand': 'X', - 'default': 'A', - 'Default': 'A' + 'default': 'J', + 'Default': 'J', + 'ARIAL UNICODE MS.TTF': 'J', + 'ARIALUNI.TTF': 'J' } // 根据字体族选择字体文件和字母 const fontFamily = element.fontFamily - let fontLetter = 'A' - let fontFile = 'ARIAL.TTF' + let fontLetter = DEFAULT_FONT_LETTER + let fontFile = DEFAULT_FONT_FILE if (fontFamily && fontFamily !== 'default') { fontFile = this.mapFontFamilyToFile(fontFamily) // 判断是否为中文字体 @@ -382,7 +389,7 @@ export class ZPLGenerator { if (chineseFonts.includes(fontFamily)) { fontLetter = 'J' } else { - fontLetter = fontLetterMapping[fontFamily] || 'A' + fontLetter = fontLetterMapping[fontFamily] || 'J' if (!fontLetterMapping[fontFamily]) { // 尝试模糊匹配 const lowerFontFamily = fontFamily.toLowerCase() @@ -397,9 +404,9 @@ export class ZPLGenerator { fontCmd += ` ^CW${fontLetter},E:${fontFile}` console.log(`ZPL字体映射: ${fontFamily} -> ${fontFile}, 字母: ${fontLetter}`) } else { - fontCmd += ' ^CWJ,E:ARIAL.TTF' - fontLetter = 'J' - console.log('使用默认字体: ARIAL.TTF, 字母: J') + fontCmd += ` ^CW${DEFAULT_FONT_LETTER},E:${DEFAULT_FONT_FILE}` + fontLetter = DEFAULT_FONT_LETTER + console.log(`使用默认字体: ${DEFAULT_FONT_FILE}, 字母: ${DEFAULT_FONT_LETTER}`) } // 设置字体大小 @@ -437,6 +444,10 @@ export class ZPLGenerator { '標楷體': 'DFKAI.TTF', // 英文字体 + 'Arial Unicode MS': DEFAULT_FONT_FILE, + 'Arial Unicode': DEFAULT_FONT_FILE, + 'ARIAL UNICODE MS.TTF': DEFAULT_FONT_FILE, + 'ARIALUNI.TTF': DEFAULT_FONT_FILE, 'Arial': 'ARIAL.TTF', 'Times New Roman': 'TIMES.TTF', 'Courier New': 'COUR.TTF', @@ -476,8 +487,8 @@ export class ZPLGenerator { 'Snell Roundhand': 'SNELLROUND.TTF', // 默认字体 - 'default': 'ARIAL.TTF', - 'Default': 'ARIAL.TTF' + 'default': DEFAULT_FONT_FILE, + 'Default': DEFAULT_FONT_FILE } // 如果找不到精确匹配,尝试模糊匹配 @@ -490,7 +501,7 @@ export class ZPLGenerator { } } - return fontMapping[fontFamily] || 'ARIAL.TTF' // 默认返回ARIAL + return fontMapping[fontFamily] || DEFAULT_FONT_FILE // 默认返回Arial Unicode MS } /** @@ -620,7 +631,7 @@ export class ZPLGenerator { const fontSize = 15 // 根据二维码大小调整字体大小 // 设置中文字体支持 - zpl += `\n^CI28^CWJ,E:ARIAL.TTF^CFJ,${fontSize}` + zpl += `\n^CI28^CWJ,E:${DEFAULT_FONT_FILE}^CFJ,${fontSize}` // 计算文本宽度以实现居中对齐 const estimatedTextWidth = this.estimateTextWidth(data, fontSize) diff --git a/src/views/modules/labelSetting/components/PropertyForm.vue b/src/views/modules/labelSetting/components/PropertyForm.vue index 9ada75a..ac9a8b3 100644 --- a/src/views/modules/labelSetting/components/PropertyForm.vue +++ b/src/views/modules/labelSetting/components/PropertyForm.vue @@ -648,6 +648,14 @@ import comShowLabelSerialInfo from "../com_show_label_serial_info"; import { availableFont,getParentLabelInfo ,getParentSerialElements} from '@/api/labelSetting/label_setting.js' +const DEFAULT_FONT_OPTION = { + name: '默认字体', + value: 'default', + category: 'system', + description: 'ARIAL UNICODE MS.TTF', + supported: true +} + export default { name: 'PropertyForm', props: { @@ -659,7 +667,8 @@ export default { emits: ['data-source', 'image-upload', 'element-combination'], data() { return { - availableFonts: [], + // 下拉框必须始终可选default,避免后端字体列表不含default时出现空白/不可回退 + availableFonts: [{ ...DEFAULT_FONT_OPTION }], fontLoading: false, fontsLoaded: false, parentSerialElements: [] // 父标签的流水号元素列表 @@ -667,11 +676,12 @@ export default { }, computed: { groupedFonts() { - if (!this.availableFonts.length) { + const normalizedFonts = this.ensureDefaultFontOption(this.availableFonts) + if (!normalizedFonts.length) { return [] } const groups = {} - this.availableFonts.forEach(font => { + normalizedFonts.forEach(font => { const category = font.category || 'other' if (!groups[category]) { groups[category] = { @@ -1093,7 +1103,7 @@ export default { try { const response = await availableFont({}) if (response.data.success) { - this.availableFonts = response.data.data || [] + this.availableFonts = this.ensureDefaultFontOption(response.data.data || []) this.fontsLoaded = true console.log('加载字体列表成功,共', this.availableFonts.length, '个字体') } else { @@ -1126,7 +1136,7 @@ export default { // 获取默认字体列表(备用方案) getDefaultFonts() { return [ - { name: '默认字体', value: 'default', category: 'system', description: '系统默认', supported: true }, + { ...DEFAULT_FONT_OPTION }, { name: '微软雅黑', value: 'Microsoft YaHei', category: 'chinese', description: '中文字体', supported: true }, { name: '宋体', value: 'SimSun', category: 'chinese', description: '中文字体', supported: true }, { name: '黑体', value: 'SimHei', category: 'chinese', description: '中文字体', supported: true }, @@ -1136,6 +1146,27 @@ export default { ] }, + ensureDefaultFontOption(fonts) { + const safeFonts = Array.isArray(fonts) ? fonts.filter(item => item && item.value) : [] + const hasDefault = safeFonts.some(font => String(font.value).toLowerCase() === 'default') + const normalizedFonts = safeFonts.map(font => { + if (String(font.value).toLowerCase() !== 'default') { + return font + } + return { + ...font, + value: 'default', + category: font.category || DEFAULT_FONT_OPTION.category, + description: font.description || DEFAULT_FONT_OPTION.description, + supported: true + } + }) + if (hasDefault) { + return normalizedFonts + } + return [{ ...DEFAULT_FONT_OPTION }, ...normalizedFonts] + }, + // 字体族变化处理 onFontFamilyChange(value) { console.log('字体族变化:', value)