proxyscripts/sheet_layout.py
2023-07-13 01:06:17 -04:00

153 lines
5.8 KiB
Python

import dataclasses
import math
from typing import List, Tuple
from base_proxy import CARD_W_INCHES, CARD_H_INCHES, LEAVE_BLEED_INCHES
@dataclasses.dataclass
class CardImageTransfer:
topleft_dest: Tuple[int, int]
bleeds: List[int]
class SheetLayout:
def __init__(self, dpi: int, w_inches: float, h_inches: float, internal_bleed: bool):
self.dpi = dpi
self.card_w_inches = CARD_W_INCHES
self.card_h_inches = CARD_H_INCHES
self.card_w = self.in_to_px(self.card_w_inches)
self.card_h = self.in_to_px(self.card_h_inches)
self.w_inches = w_inches
self.h_inches = h_inches
self.w = self.in_to_px(self.w_inches)
self.h = self.in_to_px(self.h_inches)
self.leave_bleed = self.in_to_px(LEAVE_BLEED_INCHES)
self.spacing_inches = 0.05 if internal_bleed else 0
self.spacing = self.in_to_px(self.spacing_inches)
self.min_padding_inches = 0.12
self.min_padding = self.in_to_px(self.min_padding_inches)
paste_area_left_xs = []
paste_area_right_xs = []
card_area_left_xs = []
card_area_right_xs = []
cur_x = 0
x_index = 0
while True:
paste_area_left = cur_x
if x_index == 0 or internal_bleed:
# Left side bleed
card_area_left = cur_x + self.leave_bleed
else:
card_area_left = cur_x
card_area_right = card_area_left + self.card_w
if internal_bleed:
# Right side bleed (added to last card of row in break block)
paste_area_right = card_area_right + self.leave_bleed
else:
paste_area_right = card_area_right
if paste_area_right >= self.w - 2 * self.min_padding - (self.leave_bleed if not internal_bleed else 0):
if not internal_bleed:
# Right side bleed of previous card
paste_area_right_xs[-1] += self.leave_bleed
self.cards_per_row = x_index
self.total_w_content = paste_area_right_xs[-1]
# Center content horizontally
self.blank_space_w = (self.w - self.total_w_content) // 2
self.paste_area_left_xs = [x + self.blank_space_w for x in paste_area_left_xs]
self.paste_area_right_xs = [x + self.blank_space_w for x in paste_area_right_xs]
self.card_area_left_xs = [x + self.blank_space_w for x in card_area_left_xs]
self.card_area_right_xs = [x + self.blank_space_w for x in card_area_right_xs]
break
paste_area_left_xs.append(paste_area_left)
paste_area_right_xs.append(paste_area_right)
card_area_left_xs.append(card_area_left)
card_area_right_xs.append(card_area_right)
cur_x = paste_area_right + self.spacing
x_index += 1
paste_area_top_ys = []
paste_area_bottom_ys = []
card_area_top_ys = []
card_area_bottom_ys = []
cur_y = 0
y_index = 0
while True:
paste_area_top = cur_y
if y_index == 0 or internal_bleed:
# Top bleed
card_area_top = cur_y + self.leave_bleed
else:
card_area_top = cur_y
card_area_bottom = card_area_top + self.card_h
if internal_bleed:
# Bottom bleed (added to last card of column in break block)
paste_area_bottom = card_area_bottom + self.leave_bleed
else:
paste_area_bottom = card_area_bottom
if paste_area_bottom >= self.h - 2 * self.min_padding - (self.leave_bleed if not internal_bleed else 0):
if not internal_bleed:
# Bottom bleed of previous card
paste_area_bottom_ys[-1] += self.leave_bleed
self.cards_per_column = y_index
self.total_h_content = paste_area_bottom_ys[-1]
# Center content vertically
self.blank_space_h = (self.h - self.total_h_content) // 2
self.paste_area_top_ys = [y + self.blank_space_h for y in paste_area_top_ys]
self.paste_area_bottom_ys = [y + self.blank_space_h for y in paste_area_bottom_ys]
self.card_area_top_ys = [y + self.blank_space_h for y in card_area_top_ys]
self.card_area_bottom_ys = [y + self.blank_space_h for y in card_area_bottom_ys]
break
paste_area_top_ys.append(paste_area_top)
paste_area_bottom_ys.append(paste_area_bottom)
card_area_top_ys.append(card_area_top)
card_area_bottom_ys.append(card_area_bottom)
cur_y = paste_area_bottom + self.spacing
y_index += 1
self.cards_per_sheet = self.cards_per_row * self.cards_per_column
print(self.__dict__)
def in_to_px(self, inches: float) -> float:
return round(inches * self.dpi)
def create_card_image_transfer(self, y_index: int, x_index: int) -> CardImageTransfer:
top_row = y_index == 0
bottom_row = y_index == self.cards_per_column - 1
left_col = x_index == 0
right_col = x_index == self.cards_per_row - 1
paste_x = self.paste_area_left_xs[x_index]
paste_y = self.paste_area_top_ys[y_index]
bleeds = [
self.leave_bleed if top_row else 0,
self.leave_bleed if bottom_row else 0,
self.leave_bleed if left_col else 0,
self.leave_bleed if right_col else 0]
return CardImageTransfer((paste_x, paste_y), bleeds)
def letter_portrait_3x3_sheet_layout() -> SheetLayout:
return SheetLayout(1200, 8.5, 11, False)
def letter_landscape_2x4_sheet_layout() -> SheetLayout:
return SheetLayout(1200, 11, 8.5, False)