Browse Source

纸张选择bug

master
han\hanst 5 months ago
parent
commit
248e46643c
  1. 56
      src/utils/zplGenerator.js
  2. 144
      src/views/modules/labelSetting/components/PaperSelector.vue

56
src/utils/zplGenerator.js

@ -119,20 +119,20 @@ export class ZPLGenerator {
*/
generateTextZPL(element, x, y) {
const zpl = []
// 生成字体命令
const fontCommand = this.generateFontCommand(element)
zpl.push(fontCommand)
if (element.isChecked) {
zpl.push(`^FO${x-60},${y}^GB45,45,2,B,0^FS ^FO${x-50},${y+10}^AJN,35,35^FD√^FS `)
}
const lineWidth = Math.max(1, Math.round((element.lineWidth || 200) * this.dpi / 25.4))
// 处理文本对齐
const alignmentParam = this.getTextAlignmentParam(element.textAlign)
// 基础文本
const textCommand = element.newline
? `^FO${x},${y}^FB${lineWidth},${element.lineRows},0,${alignmentParam}^CFJ,${element.fontSize}^FD${element.data}^FS`
@ -170,7 +170,7 @@ export class ZPLGenerator {
*/
generateFontCommand(element) {
let fontCmd = '^CI28' // 设置字符编码
// 根据字体族选择字体文件
const fontFamily = element.fontFamily
if (fontFamily && fontFamily !== 'default') {
@ -179,22 +179,22 @@ export class ZPLGenerator {
fontCmd += ` ^CWJ,E:${fontFile}`
console.log(`ZPL字体映射: ${fontFamily} -> ${fontFile}`)
} else {
fontCmd += ' ^CWJ,E:MSYH.TTF' // 默认字体
fontCmd += ' ^CWJ,E:ARIAL.TTF' // 默认字体
console.warn(`未找到字体映射: ${fontFamily},使用默认字体`)
}
} else {
fontCmd += ' ^CWJ,E:MSYH.TTF' // 默认字体
console.log('使用默认字体: MSYH.TTF')
fontCmd += ' ^CWJ,E:ARIAL.TTF' // 默认字体
console.log('使用默认字体: ARIAL.TTF')
}
// 设置字体大小
const fontSize = element.fontSize || 30
fontCmd += ` ^CFJ,${fontSize}`
// 添加调试信息
console.log(`生成字体命令: ${fontCmd}`)
console.log(`字体族: ${fontFamily}, 字体大小: ${fontSize}`)
return fontCmd
}
@ -220,7 +220,7 @@ export class ZPLGenerator {
'新細明體': 'PMINGLIU.TTF',
'DFKai-SB': 'DFKAI.TTF',
'標楷體': 'DFKAI.TTF',
// 英文字体
'Arial': 'ARIAL.TTF',
'Times New Roman': 'TIMES.TTF',
@ -243,7 +243,7 @@ export class ZPLGenerator {
'Futura': 'FUTURA.TTF',
'Bodoni': 'BODONI.TTF',
'Didot': 'DIDOT.TTF',
// 等宽字体
'Consolas': 'CONSOLAS.TTF',
'Monaco': 'MONACO.TTF',
@ -251,7 +251,7 @@ export class ZPLGenerator {
'Source Code Pro': 'SOURCECODE.TTF',
'Fira Code': 'FIRACODE.TTF',
'JetBrains Mono': 'JETBRAINS.TTF',
// 装饰字体
'Brush Script MT': 'BRUSHSCRIPT.TTF',
'Papyrus': 'PAPYRUS.TTF',
@ -259,12 +259,12 @@ export class ZPLGenerator {
'Marker Felt': 'MARKERFELT.TTF',
'Zapfino': 'ZAPFINO.TTF',
'Snell Roundhand': 'SNELLROUND.TTF',
// 默认字体
'default': 'MSYH.TTF',
'Default': 'MSYH.TTF'
'default': 'ARIAL.TTF',
'Default': 'ARIAL.TTF'
}
// 如果找不到精确匹配,尝试模糊匹配
if (!fontMapping[fontFamily]) {
const lowerFontFamily = fontFamily.toLowerCase()
@ -274,8 +274,8 @@ export class ZPLGenerator {
}
}
}
return fontMapping[fontFamily] || 'MSYH.TTF' // 默认返回微软雅黑
return fontMapping[fontFamily] || 'ARIAL.TTF' // 默认返回ARIAL
}
/**
@ -295,11 +295,11 @@ export class ZPLGenerator {
*/
estimateTextWidth(text, fontSize) {
if (!text) return 0
// 简单估算:中文字符按字体大小计算,英文字符按字体大小的0.6倍计算
let chineseCount = 0
let englishCount = 0
for (let i = 0; i < text.length; i++) {
const char = text.charAt(i)
if (char.match(/[\u4e00-\u9fff]/)) {
@ -308,7 +308,7 @@ export class ZPLGenerator {
englishCount++
}
}
return Math.round(chineseCount * fontSize + englishCount * fontSize * 0.6)
}
@ -436,22 +436,22 @@ export class ZPLGenerator {
*/
generateSerialNumberZPL(element, x, y) {
const zpl = []
// 生成字体命令
const fontCommand = this.generateFontCommand(element)
zpl.push(fontCommand)
const bold = element.bold || false
const base = `^FO${x},${y}^FD${element.data}^FS`
zpl.push(base)
// 可选:加粗效果(重复打印)
if (bold) {
zpl.push(`^FO${x + 1},${y}^FD${element.data}^FS`)
zpl.push(`^FO${x},${y + 1}^FD${element.data}^FS`)
zpl.push(`^FO${x + 1},${y + 1}^FD${element.data}^FS`)
}
return zpl.join('\n')
}
}

144
src/views/modules/labelSetting/components/PaperSelector.vue

@ -11,12 +11,13 @@
<!-- 纸张类型选择 -->
<div class="paper-type-select">
<el-select
:value="selectedPaper"
:value="normalizedSelectedPaper"
@change="handlePaperChange"
placeholder="选择纸张类型"
size="small"
:loading="loading"
:style="horizontalMode ? 'width: 120px;' : 'width: 100%;'"
filterable
>
<el-option
v-for="option in paperOptions"
@ -31,74 +32,6 @@
</el-option>
</el-select>
</div>
<!-- 打印方向选择 -->
<!-- <div class="orientation-select" :class="{ 'horizontal-inline': horizontalMode }">
<el-radio-group
:value="orientation"
@change="handleOrientationChange"
size="small"
>
<el-radio label="portrait">纵向</el-radio>
<el-radio label="landscape">横向</el-radio>
</el-radio-group>
</div>-->
<!-- 纸张预览 -->
<div class="paper-preview">
<div class="preview-container">
<div
class="preview-paper"
:style="previewStyle"
:class="{ landscape: orientation === 'landscape' }"
>
<div class="preview-label">
{{ currentSize.width }}×{{ currentSize.height }}
</div>
</div>
</div>
</div>
<!-- 当前画布信息 -->
<div class="canvas-info">
<div class="info-item">
<span class="label">画布尺寸:</span>
<span class="value">{{ currentSize.width }} × {{ currentSize.height }}px</span>
</div>
<div class="info-item">
<span class="label">打印方向:</span>
<span class="value">
{{ orientationText }}
<i :class="orientationIcon" style="margin-left: 4px;"></i>
</span>
</div>
<div v-if="originalPaperInfo" class="info-item">
<span class="label">纸张规格:</span>
<span class="value">{{ originalPaperInfo }}</span>
</div>
<div v-if="currentSize.description" class="info-item description">
<span class="label">说明:</span>
<span class="value">{{ currentSize.description }}</span>
</div>
</div>
<!-- 快速尺寸按钮 -->
<div class="quick-sizes">
<div class="quick-title">常用尺寸</div>
<div class="quick-buttons">
<el-button
v-for="size in quickSizes"
:key="size.value"
:type="selectedPaper === size.value ? 'primary' : ''"
size="mini"
@click="handlePaperChange(size.value)"
>
{{ size.label }}
</el-button>
</div>
</div>
</div>
</template>
@ -163,33 +96,18 @@ export default {
paper: paper
}))
},
currentSize() {
// ID
if (typeof this.selectedPaper === 'number') {
const paper = this.dynamicPapers.find(p => p.id === this.selectedPaper)
if (paper) {
return dynamicPaperConfig.calculatePixelSize(paper, 203, this.orientation)
}
}
//
if (typeof this.selectedPaper === 'string') {
const legacyPaper = dynamicPaperConfig.findPaperByLegacyType(this.selectedPaper)
if (legacyPaper) {
return dynamicPaperConfig.calculatePixelSize(legacyPaper, 203, this.orientation)
}
//
return getCanvasSize(this.selectedPaper, this.orientation)
// ID
normalizedSelectedPaper() {
if (this.selectedPaper === null || this.selectedPaper === undefined) {
return null
}
//
if (this.dynamicPapers.length > 0) {
const defaultPaper = this.dynamicPapers[0]
return dynamicPaperConfig.calculatePixelSize(defaultPaper, 203, this.orientation)
//
if (typeof this.selectedPaper === 'string' && /^\d+$/.test(this.selectedPaper)) {
return parseInt(this.selectedPaper, 10)
}
return { width: 400, height: 300, name: '默认', description: '默认尺寸' }
return this.selectedPaper
},
orientationText() {
return this.orientation === 'portrait' ? '纵向' : '横向'
},
@ -200,41 +118,10 @@ export default {
label: paper.name.replace('英寸', '').replace('×', '×')
}))
},
//
originalPaperInfo() {
if (typeof this.selectedPaper === 'number') {
const paper = this.dynamicPapers.find(p => p.id === this.selectedPaper)
return paper ? `${paper.name} (${paper.widthMm}×${paper.heightMm}mm)` : ''
}
//
const paper = PAPER_SIZES[this.selectedPaper]
return paper ? `${paper.name} (${paper.width}×${paper.height}px)` : ''
},
//
orientationIcon() {
return this.orientation === 'portrait' ? 'el-icon-mobile-phone' : 'el-icon-monitor'
},
//
previewStyle() {
const { width, height } = this.currentSize
const maxWidth = 80
const maxHeight = 50
//
const scaleX = maxWidth / width
const scaleY = maxHeight / height
const scale = Math.min(scaleX, scaleY, 1)
const previewWidth = width * scale
const previewHeight = height * scale
return {
width: `${previewWidth}px`,
height: `${previewHeight}px`,
backgroundColor: this.orientation === 'landscape' ? '#e3f2fd' : '#f3e5f5'
}
}
},
methods: {
//
@ -243,14 +130,14 @@ export default {
this.loading = true
await dynamicPaperConfig.loadPapers()
this.dynamicPapers = dynamicPaperConfig.getActivePapers()
// ID
if (this.dynamicPapers.length > 0 && !this.defaultPaperId) {
// 4×2
const defaultPaper = this.dynamicPapers.find(p => p.name.includes('4×2')) || this.dynamicPapers[0]
this.defaultPaperId = defaultPaper.id
}
console.log('纸张选择器加载完成:', this.dynamicPapers.length, '个纸张')
} catch (error) {
console.error('加载纸张数据失败:', error)
@ -276,8 +163,9 @@ export default {
//
getCurrentPaper() {
if (typeof this.selectedPaper === 'number') {
return this.dynamicPapers.find(p => p.id === this.selectedPaper)
const paperId = this.normalizedSelectedPaper
if (typeof paperId === 'number') {
return this.dynamicPapers.find(p => p.id === paperId)
}
return null
}

Loading…
Cancel
Save