""" Compare the old and new page geometry against the driver's real media. The PT-E560BT driver reports 24 x 65 mm (landscape). This shows what the driver receives in each configuration and whether it has to rescale or rotate. """ import os import sys sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) os.chdir(os.path.dirname(os.path.abspath(__file__))) from label_config import load_config from ptouch_label import LabelConfig, PtouchLabelRenderer, compute_geometry from reportlab.lib.units import mm from pypdf import PdfReader # What the installed driver advertises (queried earlier from the Windows spooler). DRIVER_W, DRIVER_H = 65.0, 24.0 def describe(label, cfg, inventory="INV-1"): renderer = PtouchLabelRenderer(cfg) path = os.path.join("pdf_backup", f"_geom_{label}.pdf") os.makedirs("pdf_backup", exist_ok=True) renderer.render_pdf(inventory, path) page = PdfReader(path).pages[0] pw = float(page.mediabox.width) / mm ph = float(page.mediabox.height) / mm ox, oy = cfg.origin_mm print(f"\n--- {label} ---") print(f" match_driver_media : {cfg.match_driver_media}") print(f" PDF page sent : {pw:.2f} x {ph:.2f} mm") print(f" label content : {cfg.page_width_mm:.2f} x {cfg.page_height_mm:.2f} mm") print(f" content origin : ({ox:.2f}, {oy:.2f}) mm") page_rotated = abs(pw - DRIVER_H) < 0.5 and abs(ph - DRIVER_W) < 0.5 exact_match = abs(pw - DRIVER_W) < 0.5 and abs(ph - DRIVER_H) < 0.5 print(f" driver media : {DRIVER_W:.2f} x {DRIVER_H:.2f} mm") if exact_match: print(" driver action : NONE (exact size match)") elif page_rotated: print(" driver action : rotate 90 deg, then fit") else: print(" driver action : RESCALE + reposition (distorts geometry)") x_scale = DRIVER_W / pw if not exact_match else 1.0 y_scale = DRIVER_H / ph if not exact_match else 1.0 if not exact_match: print(f" implied scale : x{x_scale:.3f} by y{y_scale:.3f}") os.remove(path) return exact_match def main() -> int: saved = load_config() # Use the saved settings so this reflects what will really be printed. print("=" * 70) print("PRINT GEOMETRY CHECK") print("=" * 70) print(f"Saved settings: match_driver_media={saved.match_driver_media}") print(f" logo_path={saved.logo_path or '(none)'}") print(f" static_lines={saved.static_lines}") old = LabelConfig() old.match_driver_media = False old_ok = describe("OLD (page 63x20, no media match)", old) new = saved new.match_driver_media = True new_ok = describe("NEW (page 65x24 matching driver media)", new) print("\n" + "=" * 70) print(f"OLD config needs driver rescaling : {not old_ok}") print(f"NEW config is an exact size match : {new_ok}") print("=" * 70) if new_ok: cfg = saved geo = compute_geometry(cfg) ox, oy = cfg.origin_mm print("\nContent placement on the 65 x 24 mm page:") print(f" label occupies x = {ox:.2f} .. {ox + cfg.page_width_mm:.2f} mm") print(f" y = {oy:.2f} .. {oy + cfg.page_height_mm:.2f} mm") print(f" slack left/right : {ox:.2f} mm each side") print(f" slack top/bottom : {oy:.2f} mm each side") print(f" expected printed : {cfg.page_width_mm:.0f} wide x " f"{cfg.page_height_mm:.0f} mm long") return 0 if new_ok else 1 if __name__ == "__main__": raise SystemExit(main())