labelSheetLayout.ts 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // Custom label sheets (#3296): the form's draft, and the same "does it fit"
  2. // check the server makes in backend.app.services.label_renderer.SheetLayout,
  3. // so the dialog can explain a problem while the user types instead of after
  4. // a 422.
  5. import type { LabelPageSize, LabelSheetLayout } from '../api/client';
  6. // Mirrors of the renderer's limits (label_renderer.py), number for number, so
  7. // the dialog and the server never disagree about a layout.
  8. export const PAGE_SIZES_MM: Record<LabelPageSize, [number, number]> = {
  9. a4: [210, 297],
  10. letter: [215.9, 279.4],
  11. };
  12. // What the label layout can fill: the full layout from 20 mm tall, and room
  13. // for a four-digit spool ID beside the swatch and QR from 40 mm wide.
  14. export const MIN_LABEL_W_MM = 40;
  15. export const MIN_LABEL_H_MM = 20;
  16. export const MAX_LENGTH_MM = 300;
  17. export const MAX_COLS = 20;
  18. export const MAX_ROWS = 40;
  19. const FIT_TOLERANCE_MM = 0.01;
  20. // PNG export of sheets is capped at pages × dpi (MAX_PNG_SHEET_PAGES_X_DPI in labels.py).
  21. export const MAX_PNG_SHEET_PAGES_X_DPI = 15000;
  22. /** Pages ``labelCount`` labels take on a sheet of ``capacity``, from ``startingPosition``. */
  23. export function sheetPageCount(capacity: number, labelCount: number, startingPosition = 1): number {
  24. return Math.ceil((startingPosition - 1 + labelCount) / capacity);
  25. }
  26. /** A length in millimetres, written the way the reader's language writes numbers. */
  27. export function formatMm(mm: number, language: string): string {
  28. return new Intl.NumberFormat(language, { maximumFractionDigits: 2 }).format(mm);
  29. }
  30. export const LAYOUT_NUMBER_FIELDS = [
  31. 'label_w_mm',
  32. 'label_h_mm',
  33. 'cols',
  34. 'rows',
  35. 'top_mm',
  36. 'left_mm',
  37. 'col_gap_mm',
  38. 'row_gap_mm',
  39. ] as const;
  40. export type LayoutNumberField = (typeof LAYOUT_NUMBER_FIELDS)[number];
  41. /** The form as typed: numbers stay strings so a half-typed "3." isn't lost. */
  42. export type LayoutDraft = { page: LabelPageSize } & Record<LayoutNumberField, string>;
  43. /** A reasonable starting point: a 3 × 7 grid on A4 (the Avery L7160). */
  44. export const DEFAULT_LAYOUT: LabelSheetLayout = {
  45. page: 'a4',
  46. label_w_mm: 63.5,
  47. label_h_mm: 38.1,
  48. cols: 3,
  49. rows: 7,
  50. top_mm: 15.15,
  51. left_mm: 7,
  52. col_gap_mm: 2.5,
  53. row_gap_mm: 0,
  54. };
  55. export function draftFromLayout(layout: LabelSheetLayout): LayoutDraft {
  56. const draft = { page: layout.page } as LayoutDraft;
  57. for (const field of LAYOUT_NUMBER_FIELDS) draft[field] = String(layout[field]);
  58. return draft;
  59. }
  60. /** The layout a draft describes, or null while a field is blank or not a number. */
  61. export function parseDraft(draft: LayoutDraft): LabelSheetLayout | null {
  62. const layout = { page: draft.page } as LabelSheetLayout;
  63. for (const field of LAYOUT_NUMBER_FIELDS) {
  64. const text = draft[field].trim().replace(',', '.');
  65. const value = Number(text);
  66. if (text === '' || !Number.isFinite(value)) return null;
  67. layout[field] = value;
  68. }
  69. return layout;
  70. }
  71. export function isLayoutDraft(value: unknown): value is LayoutDraft {
  72. if (!value || typeof value !== 'object') return false;
  73. const v = value as Record<string, unknown>;
  74. return (
  75. (v.page === 'a4' || v.page === 'letter') && LAYOUT_NUMBER_FIELDS.every((f) => typeof v[f] === 'string')
  76. );
  77. }
  78. export interface LayoutProblem {
  79. key: string;
  80. fallback: string;
  81. /** Lengths in mm and counts; the dialog formats them for the reader's language. */
  82. params?: Record<string, number>;
  83. }
  84. /** Width and height the grid takes up, margins included. */
  85. export function gridExtentMm(layout: LabelSheetLayout): [number, number] {
  86. return [
  87. layout.left_mm + layout.cols * layout.label_w_mm + (layout.cols - 1) * layout.col_gap_mm,
  88. layout.top_mm + layout.rows * layout.label_h_mm + (layout.rows - 1) * layout.row_gap_mm,
  89. ];
  90. }
  91. /** Why a draft can't print, or null when it can. Same rules as the server. */
  92. export function layoutProblem(draft: LayoutDraft): LayoutProblem | null {
  93. const layout = parseDraft(draft);
  94. if (!layout) {
  95. return { key: 'incomplete', fallback: 'Fill in every field with a number.' };
  96. }
  97. // Same order as SheetLayout.problem(), so both name the same problem first.
  98. if (layout.label_w_mm < MIN_LABEL_W_MM || layout.label_h_mm < MIN_LABEL_H_MM) {
  99. return {
  100. key: 'tooSmall',
  101. fallback: 'Labels must be at least {{minWidth}} mm wide and {{minHeight}} mm tall, or the text doesn’t fit.',
  102. params: { minWidth: MIN_LABEL_W_MM, minHeight: MIN_LABEL_H_MM },
  103. };
  104. }
  105. const lengths = [layout.label_w_mm, layout.label_h_mm, layout.top_mm, layout.left_mm, layout.col_gap_mm, layout.row_gap_mm];
  106. if (Math.max(...lengths) > MAX_LENGTH_MM) {
  107. return {
  108. key: 'tooLong',
  109. fallback: 'Sizes, margins and gaps can be at most {{max}} mm.',
  110. params: { max: MAX_LENGTH_MM },
  111. };
  112. }
  113. if (!Number.isInteger(layout.cols) || layout.cols < 1 || layout.cols > MAX_COLS) {
  114. return { key: 'columns', fallback: 'Columns must be a whole number from 1 to {{max}}.', params: { max: MAX_COLS } };
  115. }
  116. if (!Number.isInteger(layout.rows) || layout.rows < 1 || layout.rows > MAX_ROWS) {
  117. return { key: 'rows', fallback: 'Rows must be a whole number from 1 to {{max}}.', params: { max: MAX_ROWS } };
  118. }
  119. if (Math.min(layout.top_mm, layout.left_mm, layout.col_gap_mm, layout.row_gap_mm) < 0) {
  120. return { key: 'negative', fallback: 'Margins and gaps can’t be negative.' };
  121. }
  122. const [pageW, pageH] = PAGE_SIZES_MM[layout.page];
  123. const [gridW, gridH] = gridExtentMm(layout);
  124. if (gridW > pageW + FIT_TOLERANCE_MM) {
  125. return {
  126. key: 'tooWide',
  127. fallback: 'The labels are {{size}} mm wide with the left margin, but the page is {{page}} mm.',
  128. params: { size: gridW, page: pageW },
  129. };
  130. }
  131. if (gridH > pageH + FIT_TOLERANCE_MM) {
  132. return {
  133. key: 'tooTall',
  134. fallback: 'The labels are {{size}} mm tall with the top margin, but the page is {{page}} mm.',
  135. params: { size: gridH, page: pageH },
  136. };
  137. }
  138. return null;
  139. }