Skip to content

Commit

Permalink
Split crop marks calculation to improve readability
Browse files Browse the repository at this point in the history
  • Loading branch information
pablerass committed Nov 27, 2023
1 parent 22248a3 commit fc72a79
Show file tree
Hide file tree
Showing 4 changed files with 298 additions and 107 deletions.
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
166 changes: 104 additions & 62 deletions cartuli/sheet.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ def crop_marks_padding(self) -> float:
def __len__(self):
return len(self.__cards)

# TODO: Add minimum crop mark length
@property
def margin(self) -> float:
"""Return sheet margin to center content."""
Expand Down Expand Up @@ -146,72 +147,113 @@ def card_position(self, coordinates: Coordinates) -> Point:
coordinates.y * self.padding)
return Point(x, y)

def _calculate_border_horizontal_crop_marks(self) -> list[Line]:
crop_marks = []

for y in range(self.cards_per_page.height):
start_y_point = self.margin.height + y * (self.card_size.height + self.padding)
end_y_point = start_y_point + self.card_size.height

crop_marks.append(Line(
Point(self.print_margin, start_y_point),
Point(self.margin.width - self.crop_marks_padding, start_y_point)))

crop_marks.append(Line(
Point(self.print_margin, end_y_point),
Point(self.margin.width - self.crop_marks_padding, end_y_point)))

crop_marks.append(Line(
Point(self.size.width - self.margin.width + self.crop_marks_padding, start_y_point),
Point(self.size.width - self.print_margin, start_y_point)))

crop_marks.append(Line(
Point(self.size.width - self.margin.width + self.crop_marks_padding, end_y_point),
Point(self.size.width - self.print_margin, end_y_point)))

return crop_marks

def _calculate_middle_horizontal_crop_marks(self) -> list[Line]:
crop_marks = []

for y in range(self.cards_per_page.height):
start_y_point = self.margin.height + y * (self.card_size.height + self.padding)
end_y_point = start_y_point + self.card_size.height

if 2 * self.crop_marks_padding < self.padding:
for x in range(self.cards_per_page.width - 1):
crop_marks.append(Line(
Point(self.margin.width + (x + 1) * self.card_size.width + x * self.padding
+ self.crop_marks_padding, start_y_point),
Point(self.margin.width + (x + 1) * (self.card_size.width + self.padding)
- self.crop_marks_padding, start_y_point)))

crop_marks.append(Line(
Point(self.margin.width + (x + 1) * self.card_size.width + x * self.padding
+ self.crop_marks_padding, end_y_point),
Point(self.margin.width + (x + 1) * (self.card_size.width + self.padding)
- self.crop_marks_padding, end_y_point)))

return crop_marks

def _calculate_border_vertical_crop_marks(self) -> list[Line]:
crop_marks = []
for x in range(self.cards_per_page.width):
start_x_point = self.margin.width + x * (self.card_size.width + self.padding)
end_x_point = start_x_point + self.card_size.width

crop_marks.append(Line(
Point(start_x_point, self.print_margin),
Point(start_x_point, self.margin.height - self.crop_marks_padding)))

crop_marks.append(Line(
Point(end_x_point, self.print_margin),
Point(end_x_point, self.margin.height - self.crop_marks_padding)))

crop_marks.append(Line(
Point(start_x_point, self.size.height - self.margin.height + self.crop_marks_padding),
Point(start_x_point, self.size.height - self.print_margin)))

crop_marks.append(Line(
Point(end_x_point, self.size.height - self.margin.height + self.crop_marks_padding),
Point(end_x_point, self.size.height - self.print_margin)))

return crop_marks

def _calculate_middle_vertical_crop_marks(self) -> list[Line]:
crop_marks = []

for x in range(self.cards_per_page.width):
start_x_point = self.margin.width + x * (self.card_size.width + self.padding)
end_x_point = start_x_point + self.card_size.width

if 2 * self.crop_marks_padding < self.padding:
for y in range(self.cards_per_page.height - 1):
crop_marks.append(Line(
Point(start_x_point, self.margin.height + (y + 1) * self.card_size.height
+ y * self.padding + self.crop_marks_padding),
Point(start_x_point, self.margin.height +
(y + 1) * (self.card_size.height + self.padding) - self.crop_marks_padding)))

crop_marks.append(Line(
Point(end_x_point, self.margin.height + (y + 1) * self.card_size.height
+ y * self.padding + self.crop_marks_padding),
Point(end_x_point, self.margin.height + (y + 1) * (self.card_size.height + self.padding)
- self.crop_marks_padding)))

return crop_marks

# TODO: Extract crop marks calculation from sheet class,
# think again about it in the future, maybe it is not the best idea
@property
def crop_marks(self) -> list[Line]:
"""Return the crop marks to be drawn in each page."""
# TUNE: This is not manageable
if self.__crop_marks is None:
crop_marks = []

for x in range(self.cards_per_page.width):
start_x_point = self.margin.width + x * (self.card_size.width + self.padding)
end_x_point = start_x_point + self.card_size.width

crop_marks.append(Line(
Point(start_x_point, self.print_margin),
Point(start_x_point, self.margin.height - self.crop_marks_padding)))
crop_marks.append(Line(
Point(end_x_point, self.print_margin),
Point(end_x_point, self.margin.height - self.crop_marks_padding)))
if 2 * self.crop_marks_padding < self.padding:
for y in range(self.cards_per_page.height - 1):
crop_marks.append(Line(
Point(start_x_point, self.margin.height + (y + 1) * self.card_size.height
+ y * self.padding + self.crop_marks_padding),
Point(start_x_point, self.margin.height +
(y + 1) * (self.card_size.height + self.padding) - self.crop_marks_padding)))
crop_marks.append(Line(
Point(end_x_point, self.margin.height + (y + 1) * self.card_size.height
+ y * self.padding + self.crop_marks_padding),
Point(end_x_point, self.margin.height + (y + 1) * (self.card_size.height + self.padding)
- self.crop_marks_padding)))
crop_marks.append(Line(
Point(start_x_point, self.size.height - self.margin.height + self.crop_marks_padding),
Point(start_x_point, self.size.height - self.print_margin)))
crop_marks.append(Line(
Point(end_x_point, self.size.height - self.margin.height + self.crop_marks_padding),
Point(end_x_point, self.size.height - self.print_margin)))

for y in range(self.cards_per_page.height):
start_y_point = self.margin.height + y * (self.card_size.height + self.padding)
end_y_point = start_y_point + self.card_size.height

crop_marks.append(Line(
Point(self.print_margin, start_y_point),
Point(self.margin.width - self.crop_marks_padding, start_y_point)))
crop_marks.append(Line(
Point(self.print_margin, end_y_point),
Point(self.margin.width - self.crop_marks_padding, end_y_point)))
if 2 * self.crop_marks_padding < self.padding:
for x in range(self.cards_per_page.width - 1):
crop_marks.append(Line(
Point(self.margin.width + (x + 1) * self.card_size.width + x * self.padding
+ self.crop_marks_padding, start_y_point),
Point(self.margin.width + (x + 1) * (self.card_size.width + self.padding)
- self.crop_marks_padding, start_y_point)))
crop_marks.append(Line(
Point(self.margin.width + (x + 1) * self.card_size.width + x * self.padding
+ self.crop_marks_padding, end_y_point),
Point(self.margin.width + (x + 1) * (self.card_size.width + self.padding)
- self.crop_marks_padding, end_y_point)))
crop_marks.append(Line(
Point(self.size.width - self.margin.width + self.crop_marks_padding, start_y_point),
Point(self.size.width - self.print_margin, start_y_point)))
crop_marks.append(Line(
Point(self.size.width - self.margin.width + self.crop_marks_padding, end_y_point),
Point(self.size.width - self.print_margin, end_y_point)))

self.__crop_marks = crop_marks
self.__crop_marks = (
self._calculate_border_horizontal_crop_marks() +
self._calculate_middle_horizontal_crop_marks() +
self._calculate_border_vertical_crop_marks() +
self._calculate_middle_vertical_crop_marks()
)

return self.__crop_marks

Expand Down
9 changes: 8 additions & 1 deletion tests/test_measure.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pytest

from cartuli.measure import Size, mm, A4, MINI_USA
from cartuli.measure import Line, Point, Size, mm, A4, MINI_USA


def test_size():
Expand All @@ -13,3 +13,10 @@ def test_size():
Size.from_str("[3*mm, 4*mm]") == Size(3*mm, 4*mm)
with pytest.raises(ValueError):
Size.from_str("Size")


def test_line():
a = Point(0, 0)
b = Point(1, 1)

assert Line(a, b) == Line(b, a)
Loading

0 comments on commit fc72a79

Please sign in to comment.