feat(print-area): detect die-cut and safe print area
Keep 22827 content on the sticker when Avery bleed guides or printer drift would otherwise print off the label. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
ffa4d0cf86
commit
e31c5f89bd
10 files changed
+730
-88
No files matched your search
@@ -0,0 +1,187 @@
|
||||
"""Print-area detection: die-cut vs bleed vs safe inset."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import unittest
|
||||
|
||||
from avery_studio.print_area import (
|
||||
DEFAULT_PRINT_TO_EDGE_INSET_IN,
|
||||
PrintAreaError,
|
||||
apply_known_product_hints,
|
||||
detect_label_layout,
|
||||
product_id_from_name,
|
||||
)
|
||||
from avery_studio.template_60504 import LabelTemplate
|
||||
|
||||
|
||||
def _grid(
|
||||
left: float,
|
||||
top: float,
|
||||
width: float,
|
||||
height: float,
|
||||
h_gap: float,
|
||||
v_gap: float,
|
||||
columns: int = 2,
|
||||
rows: int = 2,
|
||||
) -> list[tuple[float, float, float, float]]:
|
||||
rects = []
|
||||
for row in range(rows):
|
||||
for col in range(columns):
|
||||
rects.append(
|
||||
(
|
||||
left + col * (width + h_gap),
|
||||
top + row * (height + v_gap),
|
||||
width,
|
||||
height,
|
||||
)
|
||||
)
|
||||
return rects
|
||||
|
||||
|
||||
class DetectLabelLayoutTests(unittest.TestCase):
|
||||
def test_empty_rects_raise(self) -> None:
|
||||
with self.assertRaises(PrintAreaError):
|
||||
detect_label_layout([], 8.5, 11.0)
|
||||
|
||||
def test_page_border_only_raises(self) -> None:
|
||||
with self.assertRaises(PrintAreaError):
|
||||
detect_label_layout([(0.0, 0.0, 8.5, 11.0)], 8.5, 11.0)
|
||||
|
||||
def test_60504_guides_are_not_snapped_outward(self) -> None:
|
||||
rects = _grid(0.251, 1.001, 3.937, 3.937, 0.125, 1.126)
|
||||
layout = detect_label_layout(
|
||||
rects,
|
||||
8.5,
|
||||
11.0,
|
||||
marketed_width_in=4.0,
|
||||
marketed_height_in=4.0,
|
||||
print_to_edge=False,
|
||||
)
|
||||
self.assertAlmostEqual(layout.label_width_in, 3.937, places=3)
|
||||
self.assertAlmostEqual(layout.label_height_in, 3.937, places=3)
|
||||
self.assertEqual(layout.columns, 2)
|
||||
self.assertEqual(layout.rows, 2)
|
||||
self.assertFalse(layout.snapped_bleed)
|
||||
self.assertEqual(layout.safe_inset_in, 0.0)
|
||||
|
||||
def test_22827_picks_die_cut_and_nested_safe_inset(self) -> None:
|
||||
bleed = _grid(0.625, 0.50, 3.75, 5.00, 0.00, 0.25)
|
||||
die = _grid(0.75, 0.625, 3.50, 4.75, 0.00, 0.25)
|
||||
# Distinct from the 0.125" print-to-edge default so we know it was measured.
|
||||
safe = [(left + 0.1875, top + 0.1875, 3.125, 4.375) for left, top, _w, _h in die]
|
||||
junk = [(0.15, 0.12, 1.2, 0.55), (0.0, 0.0, 8.5, 11.0)]
|
||||
layout = detect_label_layout(
|
||||
bleed + die + safe + junk,
|
||||
8.5,
|
||||
11.0,
|
||||
marketed_width_in=3.5,
|
||||
marketed_height_in=4.75,
|
||||
print_to_edge=True,
|
||||
default_safe_inset_in=DEFAULT_PRINT_TO_EDGE_INSET_IN,
|
||||
)
|
||||
self.assertAlmostEqual(layout.label_width_in, 3.5, places=3)
|
||||
self.assertAlmostEqual(layout.label_height_in, 4.75, places=3)
|
||||
self.assertEqual((layout.columns, layout.rows), (2, 2))
|
||||
self.assertAlmostEqual(layout.left_margin_in, 0.75, places=3)
|
||||
self.assertAlmostEqual(layout.top_margin_in, 0.625, places=3)
|
||||
self.assertAlmostEqual(layout.horizontal_gap_in, 0.0, places=3)
|
||||
self.assertAlmostEqual(layout.vertical_gap_in, 0.25, places=3)
|
||||
self.assertAlmostEqual(layout.safe_inset_in, 0.1875, places=3)
|
||||
self.assertTrue(layout.print_to_edge)
|
||||
|
||||
def test_22827_bleed_only_snaps_to_marketed_and_uses_default_inset(self) -> None:
|
||||
bleed = _grid(0.625, 0.50, 3.75, 5.00, 0.00, 0.25)
|
||||
layout = detect_label_layout(
|
||||
bleed,
|
||||
8.5,
|
||||
11.0,
|
||||
marketed_width_in=3.5,
|
||||
marketed_height_in=4.75,
|
||||
print_to_edge=True,
|
||||
default_safe_inset_in=DEFAULT_PRINT_TO_EDGE_INSET_IN,
|
||||
)
|
||||
self.assertTrue(layout.snapped_bleed)
|
||||
self.assertAlmostEqual(layout.label_width_in, 3.5, places=3)
|
||||
self.assertAlmostEqual(layout.label_height_in, 4.75, places=3)
|
||||
self.assertAlmostEqual(layout.left_margin_in, 0.75, places=3)
|
||||
self.assertAlmostEqual(layout.top_margin_in, 0.625, places=3)
|
||||
# Adjacent 3.75" bleed boxes shrink toward their centers → 0.25" gutter.
|
||||
self.assertAlmostEqual(layout.horizontal_gap_in, 0.25, places=3)
|
||||
self.assertAlmostEqual(layout.vertical_gap_in, 0.50, places=3)
|
||||
self.assertAlmostEqual(layout.safe_inset_in, 0.125, places=3)
|
||||
|
||||
def test_22827_overlapping_bleed_keeps_zero_horizontal_gap(self) -> None:
|
||||
# Print-to-edge 3.5" labels that touch: bleed 3.75" at pitch 3.5" (overlap).
|
||||
bleed = _grid(0.625, 0.50, 3.75, 5.00, -0.25, 0.00)
|
||||
layout = detect_label_layout(
|
||||
bleed,
|
||||
8.5,
|
||||
11.0,
|
||||
marketed_width_in=3.5,
|
||||
marketed_height_in=4.75,
|
||||
print_to_edge=True,
|
||||
default_safe_inset_in=DEFAULT_PRINT_TO_EDGE_INSET_IN,
|
||||
)
|
||||
self.assertTrue(layout.snapped_bleed)
|
||||
self.assertAlmostEqual(layout.label_width_in, 3.5, places=3)
|
||||
self.assertAlmostEqual(layout.horizontal_gap_in, 0.0, places=3)
|
||||
self.assertAlmostEqual(layout.left_margin_in, 0.75, places=3)
|
||||
|
||||
def test_unknown_product_uses_larger_grid_not_inner_boxes(self) -> None:
|
||||
outer = _grid(0.75, 0.625, 3.50, 4.75, 0.00, 0.25)
|
||||
inner = [
|
||||
(left + 0.20, top + 0.20, 3.10, 4.35) for left, top, _w, _h in outer
|
||||
]
|
||||
layout = detect_label_layout(outer + inner, 8.5, 11.0)
|
||||
self.assertAlmostEqual(layout.label_width_in, 3.50, places=3)
|
||||
self.assertAlmostEqual(layout.label_height_in, 4.75, places=3)
|
||||
self.assertGreater(layout.safe_inset_in, 0.1)
|
||||
|
||||
|
||||
class ProductHintTests(unittest.TestCase):
|
||||
def test_product_id_from_filename(self) -> None:
|
||||
self.assertEqual(product_id_from_name("Avery22827_Blank.doc"), "22827")
|
||||
self.assertEqual(product_id_from_name("Avery60504UltraDuty.doc"), "60504")
|
||||
|
||||
def test_22827_json_gets_safe_inset_without_changing_near_die_cut(self) -> None:
|
||||
data = apply_known_product_hints(
|
||||
{
|
||||
"id": "22827",
|
||||
"label_width_in": 3.51,
|
||||
"label_height_in": 4.76,
|
||||
"left_margin_in": 0.74,
|
||||
}
|
||||
)
|
||||
self.assertTrue(data["print_to_edge"])
|
||||
self.assertEqual(data["safe_inset_in"], DEFAULT_PRINT_TO_EDGE_INSET_IN)
|
||||
self.assertEqual(data["label_width_in"], 3.51)
|
||||
self.assertEqual(data["left_margin_in"], 0.74)
|
||||
self.assertEqual(data["marketed_size"], '3.5" x 4.75"')
|
||||
|
||||
def test_22827_json_bleed_boxes_shrink_to_die_cut(self) -> None:
|
||||
data = apply_known_product_hints(
|
||||
{
|
||||
"id": "22827",
|
||||
"label_width_in": 3.75,
|
||||
"label_height_in": 5.00,
|
||||
"left_margin_in": 0.625,
|
||||
"top_margin_in": 0.50,
|
||||
"horizontal_gap_in": 0.0,
|
||||
"vertical_gap_in": 0.25,
|
||||
}
|
||||
)
|
||||
self.assertAlmostEqual(data["label_width_in"], 3.5)
|
||||
self.assertAlmostEqual(data["label_height_in"], 4.75)
|
||||
self.assertAlmostEqual(data["left_margin_in"], 0.75)
|
||||
self.assertAlmostEqual(data["top_margin_in"], 0.625)
|
||||
self.assertAlmostEqual(data["horizontal_gap_in"], 0.25)
|
||||
self.assertAlmostEqual(data["vertical_gap_in"], 0.50)
|
||||
|
||||
def test_label_template_from_dict_applies_22827_hints(self) -> None:
|
||||
tpl = LabelTemplate.from_dict({"id": "22827", "label_width_in": 3.5, "label_height_in": 4.75})
|
||||
self.assertTrue(tpl.print_to_edge)
|
||||
self.assertAlmostEqual(tpl.recommended_safe_inset_in(), 0.125)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user