Blame view

美国版/Food Labeling Management Platform/src/utils/previewRulerUnits.ts 2.68 KB
540ac0e3   杨鑫   前端修改bug
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
  import { unitToPx } from "./labelTemplateUnits";
  
  /** 预览区标尺显示单位(与模板 `template.unit` 独立,仅影响刻度与读数) */
  export type PreviewRulerDisplayUnit = "cm" | "mm" | "inch";
  
  /** 将长度从模板单位换算到预览标尺显示单位 */
  export function convertTemplateLengthToDisplay(
    value: number,
    templateUnit: "cm" | "inch",
    displayUnit: PreviewRulerDisplayUnit,
  ): number {
    if (!Number.isFinite(value)) return 0;
    const cm = templateUnit === "cm" ? value : value * 2.54;
    if (displayUnit === "cm") return cm;
    if (displayUnit === "mm") return cm * 10;
    return cm / 2.54;
  }
  
  /** 预览标尺显示单位 → 模板单位 */
  export function convertDisplayLengthToTemplate(
    displayValue: number,
    templateUnit: "cm" | "inch",
    displayUnit: PreviewRulerDisplayUnit,
  ): number {
    if (!Number.isFinite(displayValue)) return 0;
    let cm: number;
    if (displayUnit === "cm") cm = displayValue;
    else if (displayUnit === "mm") cm = displayValue / 10;
    else cm = displayValue * 2.54;
    if (templateUnit === "cm") return cm;
    return cm / 2.54;
  }
  
  export function elementPxToDisplayLength(
    lengthPx: number,
    basePaperPx: number,
    paperSizeTemplate: number,
    templateUnit: "cm" | "inch",
    displayUnit: PreviewRulerDisplayUnit,
  ): number {
    if (!Number.isFinite(lengthPx) || !Number.isFinite(basePaperPx) || basePaperPx <= 0) return 0;
    const inTemplate = (lengthPx / basePaperPx) * paperSizeTemplate;
    return convertTemplateLengthToDisplay(inTemplate, templateUnit, displayUnit);
  }
  
  export function displayLengthToElementPx(
    displayValue: number,
    basePaperPx: number,
    paperSizeTemplate: number,
    templateUnit: "cm" | "inch",
    displayUnit: PreviewRulerDisplayUnit,
  ): number {
    if (!Number.isFinite(basePaperPx) || basePaperPx <= 0 || !Number.isFinite(paperSizeTemplate) || paperSizeTemplate <= 0) {
      return 0;
    }
    const inTemplate = convertDisplayLengthToTemplate(displayValue, templateUnit, displayUnit);
    return (inTemplate / paperSizeTemplate) * basePaperPx;
  }
  
  export function formatPreviewRulerDisplayValue(
    value: number,
    displayUnit: PreviewRulerDisplayUnit,
  ): string {
    if (!Number.isFinite(value)) return "";
    if (displayUnit === "mm") return String(Math.round(value * 10) / 10);
    if (displayUnit === "inch") return value.toFixed(4);
    return (Math.round(value * 1000) / 1000).toFixed(3);
  }
  
  export function previewRulerUnitLabel(displayUnit: PreviewRulerDisplayUnit): string {
    return displayUnit;
  }
  
  export function templatePaperBasePx(
    width: number,
    height: number,
    unit: "cm" | "inch",
  ): { baseW: number; baseH: number } {
    return {
      baseW: unitToPx(Number(width) || 0, unit),
      baseH: unitToPx(Number(height) || 0, unit),
    };
  }