|
|
|
@ -18,7 +18,7 @@ export class CoordinateTransformer { |
|
|
|
*/ |
|
|
|
getConfig() { |
|
|
|
let canvasSize |
|
|
|
|
|
|
|
this.paperId = localStorage.getItem('paperId') |
|
|
|
// 优先使用纸张ID
|
|
|
|
if (this.paperId) { |
|
|
|
try { |
|
|
|
@ -26,13 +26,21 @@ export class CoordinateTransformer { |
|
|
|
const { default: dynamicPaperConfig } = require('./paperConfigDynamic.js') |
|
|
|
const paper = dynamicPaperConfig.getPaperById(this.paperId) |
|
|
|
if (paper) { |
|
|
|
const pixelSize = dynamicPaperConfig.calculatePixelSize(paper, 203, this.orientation) |
|
|
|
const pixelSize = dynamicPaperConfig.calculatePixelSize(paper, 300, this.orientation) |
|
|
|
canvasSize = { |
|
|
|
width: pixelSize.width, |
|
|
|
height: pixelSize.height, |
|
|
|
name: paper.name, |
|
|
|
description: paper.description || '' |
|
|
|
} |
|
|
|
console.log(`坐标转换器 - 纸张配置:`, { |
|
|
|
paperId: this.paperId, |
|
|
|
paperName: paper.name, |
|
|
|
orientation: this.orientation, |
|
|
|
originalSize: `${paper.widthMm}×${paper.heightMm}mm`, |
|
|
|
pixelSize: `${pixelSize.width}×${pixelSize.height}px`, |
|
|
|
dpi: 203 |
|
|
|
}) |
|
|
|
} |
|
|
|
} catch (error) { |
|
|
|
console.warn('获取动态纸张配置失败,使用降级方案:', error) |
|
|
|
@ -50,8 +58,14 @@ export class CoordinateTransformer { |
|
|
|
name: '自定义', |
|
|
|
description: `自定义尺寸 (${this.orientation === 'landscape' ? '横向' : '纵向'})` |
|
|
|
} |
|
|
|
console.log(`坐标转换器 - 自定义尺寸:`, canvasSize) |
|
|
|
} else { |
|
|
|
canvasSize = getCanvasSize(this.paperType, this.orientation) |
|
|
|
console.log(`坐标转换器 - 降级方案:`, { |
|
|
|
paperType: this.paperType, |
|
|
|
orientation: this.orientation, |
|
|
|
canvasSize |
|
|
|
}) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@ -67,7 +81,7 @@ export class CoordinateTransformer { |
|
|
|
canvasSize, |
|
|
|
scaleX: 1, |
|
|
|
scaleY: 1, |
|
|
|
offsetX: Math.max(750, canvasSize.width * 0.8), |
|
|
|
offsetX: 0, |
|
|
|
offsetY: 0 |
|
|
|
} |
|
|
|
} |
|
|
|
@ -101,16 +115,19 @@ export class CoordinateTransformer { |
|
|
|
*/ |
|
|
|
toZPL(canvasX, canvasY) { |
|
|
|
if (this.orientation === 'portrait') { |
|
|
|
return { |
|
|
|
// 纵向打印:直接映射
|
|
|
|
const result = { |
|
|
|
x: Math.round(canvasX * this.config.scaleX), |
|
|
|
y: Math.round(canvasY * this.config.scaleY) |
|
|
|
} |
|
|
|
}; |
|
|
|
return result; |
|
|
|
} else { |
|
|
|
// 横向打印需要坐标旋转变换
|
|
|
|
return { |
|
|
|
x: Math.round(canvasX * this.config.scaleX), |
|
|
|
y: Math.round(canvasY * this.config.scaleY) |
|
|
|
} |
|
|
|
const { width, height } = this.config.canvasSize; |
|
|
|
const result = { |
|
|
|
x: Math.round(canvasY * this.config.scaleX), |
|
|
|
y: Math.round((width - canvasX-100) * this.config.scaleY) |
|
|
|
}; |
|
|
|
return result; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@ -122,15 +139,18 @@ export class CoordinateTransformer { |
|
|
|
*/ |
|
|
|
toCanvas(zplX, zplY) { |
|
|
|
if (this.orientation === 'portrait') { |
|
|
|
// 纵向打印:直接映射
|
|
|
|
return { |
|
|
|
x: Math.round(zplX / this.config.scaleX), |
|
|
|
y: Math.round(zplY / this.config.scaleY) |
|
|
|
} |
|
|
|
} else { |
|
|
|
// 横向打印的逆变换
|
|
|
|
// 与修正后的 toZPL 方法对应的逆转换
|
|
|
|
const { width, height } = this.config.canvasSize |
|
|
|
return { |
|
|
|
x: Math.round(this.config.offsetX - (zplY / this.config.scaleY)), |
|
|
|
y: Math.round(zplX / this.config.scaleX) |
|
|
|
x: Math.round(zplY / this.config.scaleX), |
|
|
|
y: Math.round((height - zplX) / this.config.scaleY) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|