Windows designer for Avery templates with Avery.com Word download, print, and PyInstaller build. Co-authored-by: Cursor <cursoragent@cursor.com>
84 lines
2.9 KiB
Python
84 lines
2.9 KiB
Python
"""Import settings and helpers for fitting artwork onto Avery 60504 labels."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from dataclasses import asdict, dataclass
|
|
from typing import Any, Literal
|
|
|
|
|
|
FitMode = Literal["contain", "cover", "stretch"]
|
|
# one_per_label: each PDF page → one label; auto-create Letter sheets (4/sheet)
|
|
# all_slots_per_page: each PDF page → a full sheet of 4 identical copies
|
|
FillMode = Literal["one_per_label", "all_slots_per_page", "all_slots", "one_per_slot"]
|
|
|
|
|
|
@dataclass
|
|
class ImportSettings:
|
|
"""How imported PDF/Word pages are placed onto 60504 label panels."""
|
|
|
|
fill_mode: FillMode = "one_per_label"
|
|
fit_mode: FitMode = "contain"
|
|
scale: float = 1.0
|
|
content_offset_x_in: float = 0.0
|
|
content_offset_y_in: float = 0.0
|
|
inset_in: float = 0.0
|
|
start_page: int = 1
|
|
# 0 = through end of document
|
|
end_page: int = 0
|
|
render_dpi: int = 300
|
|
|
|
def normalized_fill_mode(self) -> str:
|
|
"""Map legacy mode names to current ones."""
|
|
if self.fill_mode in ("one_per_slot", "one_per_label"):
|
|
return "one_per_label"
|
|
if self.fill_mode in ("all_slots", "all_slots_per_page"):
|
|
return "all_slots_per_page"
|
|
return "one_per_label"
|
|
|
|
def to_dict(self) -> dict[str, Any]:
|
|
return asdict(self)
|
|
|
|
@classmethod
|
|
def from_dict(cls, data: dict[str, Any]) -> ImportSettings:
|
|
return cls(
|
|
fill_mode=data.get("fill_mode", "one_per_label"), # type: ignore[arg-type]
|
|
fit_mode=data.get("fit_mode", "contain"), # type: ignore[arg-type]
|
|
scale=float(data.get("scale", 1.0)),
|
|
content_offset_x_in=float(data.get("content_offset_x_in", 0.0)),
|
|
content_offset_y_in=float(data.get("content_offset_y_in", 0.0)),
|
|
inset_in=float(data.get("inset_in", 0.0)),
|
|
start_page=max(1, int(data.get("start_page", 1))),
|
|
end_page=max(0, int(data.get("end_page", 0))),
|
|
render_dpi=max(72, int(data.get("render_dpi", 300))),
|
|
)
|
|
|
|
|
|
def fitted_rect(
|
|
src_w: float,
|
|
src_h: float,
|
|
label_w: float,
|
|
label_h: float,
|
|
settings: ImportSettings,
|
|
) -> tuple[float, float, float, float]:
|
|
"""Return (x, y, w, h) in inches for artwork inside a label panel."""
|
|
inset = max(0.0, settings.inset_in)
|
|
avail_w = max(0.01, label_w - 2 * inset)
|
|
avail_h = max(0.01, label_h - 2 * inset)
|
|
|
|
if src_w <= 0 or src_h <= 0:
|
|
return inset, inset, avail_w, avail_h
|
|
|
|
if settings.fit_mode == "stretch":
|
|
w, h = avail_w * settings.scale, avail_h * settings.scale
|
|
else:
|
|
scale_w = avail_w / src_w
|
|
scale_h = avail_h / src_h
|
|
scale = max(scale_w, scale_h) if settings.fit_mode == "cover" else min(scale_w, scale_h)
|
|
scale *= settings.scale
|
|
w = src_w * scale
|
|
h = src_h * scale
|
|
|
|
x = inset + (avail_w - w) / 2 + settings.content_offset_x_in
|
|
y = inset + (avail_h - h) / 2 + settings.content_offset_y_in
|
|
return x, y, w, h
|