Skip to content

Commit

Permalink
Merge pull request #8 from pablerass/feature/improve-cut-lines
Browse files Browse the repository at this point in the history
Improve cut lines readability and add them to front pages to facilitate align of printer dispacement
  • Loading branch information
pablerass authored Nov 27, 2023
2 parents fc6562c + fc72a79 commit 5ccbe7a
Show file tree
Hide file tree
Showing 7 changed files with 415 additions and 214 deletions.
3 changes: 2 additions & 1 deletion cartuli/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from pathlib import Path

from .definition import Definition
from .output import sheet_pdf_output


def parse_args(args: list[str] = None) -> argparse.Namespace:
Expand Down Expand Up @@ -73,7 +74,7 @@ def main(args=None):
sheet_dir.mkdir(exist_ok=True)
sheet_file = sheet_dir / f"{'_'.join(deck_names)}.pdf"
logger.debug(f'Creating sheet {sheet_file}')
sheet.create_pdf(sheet_file)
sheet_pdf_output(sheet, sheet_file)

if tracer:
trace_output(tracer, args.trace_output)
Expand Down
7 changes: 3 additions & 4 deletions cartuli/definition.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,12 +154,11 @@ def sheets(self) -> dict[tuple[str], Sheet]:
self.__sheets[deck_names] = Sheet(
cards,
size=Size.from_str(sheet_definition.get('size', str(Sheet.DEFAULT_SIZE))),
margin=from_str(sheet_definition.get('margin', str(Sheet.DEFAULT_MARGIN))),
print_margin=from_str(sheet_definition.get('print_margin',
str(Sheet.DEFAULT_PRINT_MARGIN))),
padding=from_str(sheet_definition.get('padding', str(Sheet.DEFAULT_PADDING))),
crop_marks_padding=from_str(
sheet_definition.get('crop_marks_padding', str(Sheet.DEFAULT_CROP_MARKS_PADDING))),
print_margin=from_str(sheet_definition.get('print_margin',
str(Sheet.DEFAULT_PRINT_MARGIN)))
sheet_definition.get('crop_marks_padding', str(Sheet.DEFAULT_CROP_MARKS_PADDING)))
)

return self.__sheets
Expand Down
10 changes: 7 additions & 3 deletions cartuli/measure.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
]


isclose = partial(isclose, rel_tol=1e-04)
measure_is_close = partial(isclose, abs_tol=0.001*mm)


def from_str(measure: str | int | float) -> float:
Expand All @@ -36,7 +36,7 @@ class Size:
height: float | int

def __eq__(self, other):
return isclose(self.width, other.width) and isclose(self.height, other.height)
return measure_is_close(self.width, other.width) and measure_is_close(self.height, other.height)

def __str__(self):
return f"({self.width}, {self.height})"
Expand Down Expand Up @@ -72,7 +72,7 @@ class Point:
y: float | int

def __eq__(self, other):
return isclose(self.x, other.x) and isclose(self.y, other.y)
return measure_is_close(self.x, other.x) and measure_is_close(self.y, other.y)

def __str__(self):
return f"({self.x}, {self.y})"
Expand All @@ -88,6 +88,10 @@ class Line:
a: Point
b: Point

def __eq__(self, other):
return ((self.a == other.a) and (self.b == other.b) or
(self.a == other.b) and (self.b == other.a))

def __str__(self):
return f"{self.a} <-> {self.b}"

Expand Down
70 changes: 70 additions & 0 deletions cartuli/output.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
"""Sheet module."""
import logging

from pathlib import Path
from reportlab.lib.utils import ImageReader
from reportlab.pdfgen import canvas

from .sheet import Sheet


def sheet_pdf_output(sheet: Sheet, output_path: Path | str) -> None:
"""Create a PDF document containing all sheet content."""
logger = logging.getLogger('cartuli.output.sheet_pdf_output')

# TODO: Add title to PDF document
c = canvas.Canvas(str(output_path), pagesize=tuple(sheet.size))
for page in range(1, sheet.pages + 1):
# Front
for line in sheet.crop_marks:
c.setLineWidth(0.5)
c.line(*list(line))

for i, card in enumerate(sheet.page_cards(page)):
num_card = i + 1
card_image = card.front.image
card_coordinates = sheet.card_coordinates(num_card)
card_position = sheet.card_position(card_coordinates)
logger.debug(f"Adding card {num_card} '{card}' front image to page {page} at {card_coordinates}")
c.drawImage(ImageReader(card_image),
card_position.x - card.front.bleed, card_position.y - card.front.bleed,
card.front.image_size.width, card.front.image_size.height)

# Back
if sheet.two_sided:
c.showPage()
for i, card in enumerate(sheet.page_cards(page)):
num_card = i + 1
card_image = card.back.image
card_coordinates = sheet.card_coordinates(num_card, back=True)
card_position = sheet.card_position(card_coordinates)
logger.debug(f"Adding {num_card} card {card} back image to page {page} at {card_coordinates}")
c.drawImage(ImageReader(card_image),
card_position.x - card.back.bleed, card_position.y - card.back.bleed,
card.back.image_size.width, card.back.image_size.height)

for line in sheet.crop_marks:
c.setLineWidth(0.5)
c.line(*list(line))

c.showPage()
logger.debug(f"Created {output_path} page {page}")

c.save()
logger.info(f"Created {output_path}")


def sheet_output(sheet: Sheet, output_path: Path | str):
if isinstance(output_path, str):
output_path = Path(output_path)

output_path = output_path.expanduser()

if not output_path.is_absolute():
output_path = Path.cwd() / output_path

match output_path:
case Path(suffix='.pdf'):
sheet_pdf_output(sheet, output_path)
case _:
raise ValueError('Unable to identify output format in output_path')
Loading

0 comments on commit 5ccbe7a

Please sign in to comment.