Browse Source

默认字体 ARIAL UNICODE MS.TTF

master
han\hanst 1 month ago
parent
commit
ea69819036
  1. 35
      src/utils/zplGenerator.js
  2. 41
      src/views/modules/labelSetting/components/PropertyForm.vue

35
src/utils/zplGenerator.js

@ -1,5 +1,8 @@
import { CoordinateTransformer } from './coordinateTransform.js' import { CoordinateTransformer } from './coordinateTransform.js'
const DEFAULT_FONT_FILE = 'ARIAL UNICODE MS.TTF'
const DEFAULT_FONT_LETTER = 'J'
/** /**
* ZPL代码生成器 * ZPL代码生成器
* 统一处理不同打印方向的ZPL代码生成 * 统一处理不同打印方向的ZPL代码生成
@ -331,6 +334,8 @@ export class ZPLGenerator {
'新細明體': 'P', '新細明體': 'P',
'DFKai-SB': 'Q', 'DFKai-SB': 'Q',
'標楷體': 'Q', '標楷體': 'Q',
'Arial Unicode MS': 'J',
'Arial Unicode': 'J',
'Arial': 'A', 'Arial': 'A',
'Times New Roman': 'S', 'Times New Roman': 'S',
'Courier New': 'T', 'Courier New': 'T',
@ -364,14 +369,16 @@ export class ZPLGenerator {
'Marker Felt': 'V', 'Marker Felt': 'V',
'Zapfino': 'W', 'Zapfino': 'W',
'Snell Roundhand': 'X', 'Snell Roundhand': 'X',
'default': 'A',
'Default': 'A'
'default': 'J',
'Default': 'J',
'ARIAL UNICODE MS.TTF': 'J',
'ARIALUNI.TTF': 'J'
} }
// 根据字体族选择字体文件和字母 // 根据字体族选择字体文件和字母
const fontFamily = element.fontFamily 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') { if (fontFamily && fontFamily !== 'default') {
fontFile = this.mapFontFamilyToFile(fontFamily) fontFile = this.mapFontFamilyToFile(fontFamily)
// 判断是否为中文字体 // 判断是否为中文字体
@ -382,7 +389,7 @@ export class ZPLGenerator {
if (chineseFonts.includes(fontFamily)) { if (chineseFonts.includes(fontFamily)) {
fontLetter = 'J' fontLetter = 'J'
} else { } else {
fontLetter = fontLetterMapping[fontFamily] || 'A'
fontLetter = fontLetterMapping[fontFamily] || 'J'
if (!fontLetterMapping[fontFamily]) { if (!fontLetterMapping[fontFamily]) {
// 尝试模糊匹配 // 尝试模糊匹配
const lowerFontFamily = fontFamily.toLowerCase() const lowerFontFamily = fontFamily.toLowerCase()
@ -397,9 +404,9 @@ export class ZPLGenerator {
fontCmd += ` ^CW${fontLetter},E:${fontFile}` fontCmd += ` ^CW${fontLetter},E:${fontFile}`
console.log(`ZPL字体映射: ${fontFamily} -> ${fontFile}, 字母: ${fontLetter}`) console.log(`ZPL字体映射: ${fontFamily} -> ${fontFile}, 字母: ${fontLetter}`)
} else { } 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', '標楷體': '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', 'Arial': 'ARIAL.TTF',
'Times New Roman': 'TIMES.TTF', 'Times New Roman': 'TIMES.TTF',
'Courier New': 'COUR.TTF', 'Courier New': 'COUR.TTF',
@ -476,8 +487,8 @@ export class ZPLGenerator {
'Snell Roundhand': 'SNELLROUND.TTF', '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 // 根据二维码大小调整字体大小 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) const estimatedTextWidth = this.estimateTextWidth(data, fontSize)

41
src/views/modules/labelSetting/components/PropertyForm.vue

@ -648,6 +648,14 @@
import comShowLabelSerialInfo from "../com_show_label_serial_info"; import comShowLabelSerialInfo from "../com_show_label_serial_info";
import { availableFont,getParentLabelInfo ,getParentSerialElements} from '@/api/labelSetting/label_setting.js' 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 { export default {
name: 'PropertyForm', name: 'PropertyForm',
props: { props: {
@ -659,7 +667,8 @@ export default {
emits: ['data-source', 'image-upload', 'element-combination'], emits: ['data-source', 'image-upload', 'element-combination'],
data() { data() {
return { return {
availableFonts: [],
// defaultdefault/退
availableFonts: [{ ...DEFAULT_FONT_OPTION }],
fontLoading: false, fontLoading: false,
fontsLoaded: false, fontsLoaded: false,
parentSerialElements: [] // parentSerialElements: [] //
@ -667,11 +676,12 @@ export default {
}, },
computed: { computed: {
groupedFonts() { groupedFonts() {
if (!this.availableFonts.length) {
const normalizedFonts = this.ensureDefaultFontOption(this.availableFonts)
if (!normalizedFonts.length) {
return [] return []
} }
const groups = {} const groups = {}
this.availableFonts.forEach(font => {
normalizedFonts.forEach(font => {
const category = font.category || 'other' const category = font.category || 'other'
if (!groups[category]) { if (!groups[category]) {
groups[category] = { groups[category] = {
@ -1093,7 +1103,7 @@ export default {
try { try {
const response = await availableFont({}) const response = await availableFont({})
if (response.data.success) { if (response.data.success) {
this.availableFonts = response.data.data || []
this.availableFonts = this.ensureDefaultFontOption(response.data.data || [])
this.fontsLoaded = true this.fontsLoaded = true
console.log('加载字体列表成功,共', this.availableFonts.length, '个字体') console.log('加载字体列表成功,共', this.availableFonts.length, '个字体')
} else { } else {
@ -1126,7 +1136,7 @@ export default {
// //
getDefaultFonts() { getDefaultFonts() {
return [ return [
{ name: '默认字体', value: 'default', category: 'system', description: '系统默认', supported: true },
{ ...DEFAULT_FONT_OPTION },
{ name: '微软雅黑', value: 'Microsoft YaHei', category: 'chinese', description: '中文字体', supported: true }, { name: '微软雅黑', value: 'Microsoft YaHei', category: 'chinese', description: '中文字体', supported: true },
{ name: '宋体', value: 'SimSun', category: 'chinese', description: '中文字体', supported: true }, { name: '宋体', value: 'SimSun', category: 'chinese', description: '中文字体', supported: true },
{ name: '黑体', value: 'SimHei', 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) { onFontFamilyChange(value) {
console.log('字体族变化:', value) console.log('字体族变化:', value)

Loading…
Cancel
Save