illuscape/scripts/gen_templates.py

72 lines
3 KiB
Python

#!/usr/bin/env python3
"""Generate Illuscape document templates."""
from pathlib import Path
TEMPLATES_DIR = Path("config/templates")
def mm_to_px(mm: float) -> float:
return mm * 3.7795275591 # 96 dpi
def in_to_px(inches: float) -> float:
return inches * 96.0
def svg(name: str, width_px: float, height_px: float,
units: str, bleed_mm: float = 0.0, note: str = "") -> str:
import math
bleed_px = mm_to_px(bleed_mm)
guides = ""
if bleed_mm > 0:
guides = f"""
<sodipodi:guide position="{-bleed_px:.4f},{bleed_px:.4f}" orientation="1,0" />
<sodipodi:guide position="{width_px + bleed_px:.4f},{bleed_px:.4f}" orientation="1,0" />
<sodipodi:guide position="{bleed_px:.4f},{-bleed_px:.4f}" orientation="0,1" />
<sodipodi:guide position="{bleed_px:.4f},{height_px + bleed_px:.4f}" orientation="0,1" />"""
comment = f"<!-- {note} -->" if note else ""
return f"""<?xml version="1.0" encoding="UTF-8"?>
{comment}
<svg
xmlns="http://www.w3.org/2000/svg"
xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.0.dtd"
xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
width="{width_px:.4f}"
height="{height_px:.4f}"
viewBox="0 0 {width_px:.4f} {height_px:.4f}"
version="1.1"
id="svg-{name.lower().replace(' ', '-')}">
<sodipodi:namedview
id="namedview1"
pagecolor="#ffffff"
bordercolor="#666666"
borderopacity="1.0"
inkscape:showpageshadow="2"
inkscape:pageopacity="1.0"
inkscape:pagecheckerboard="0"
inkscape:document-units="{units}"
showgrid="false"
inkscape:snap-global="true"
inkscape:snap-nodes="true"
inkscape:snap-bbox="true"
inkscape:snap-to-guides="true"
inkscape:guide-bbox="true"
units="{units}">{guides}
</sodipodi:namedview>
<title>{name}</title>
<g inkscape:label="Background" inkscape:groupmode="layer" id="layer-background" style="display:inline" />
<g inkscape:label="Guides" inkscape:groupmode="layer" id="layer-guides" style="display:inline" />
<g inkscape:label="Artwork" inkscape:groupmode="layer" id="layer-artwork" style="display:inline" />
</svg>
"""
specs = [
("Letter", in_to_px(8.5), in_to_px(11), "in", 0, "US Letter portrait"),
("A4", mm_to_px(210), mm_to_px(297), "mm", 0, "A4 portrait"),
("Web-1920x1080", 1920, 1080, "px", 0, "HD web canvas"),
("Web-1280x720", 1280, 720, "px", 0, "HD web canvas (smaller)"),
("Print-CMYK-Letter", in_to_px(8.5), in_to_px(11), "in", 3, "US Letter with 3mm bleed. Note: Inkscape is RGB-native; set CMYK values manually."),
("Print-CMYK-A4", mm_to_px(210), mm_to_px(297), "mm", 3, "A4 with 3mm bleed. Note: Inkscape is RGB-native; set CMYK values manually."),
]
for name, w, h, units, bleed, note in specs:
path = TEMPLATES_DIR / f"{name}.svg"
path.write_text(svg(name, w, h, units, bleed, note))
print(f"{path}")