Skip to content

Commit

Permalink
Add zine mode for quarter-size letter paper.
Browse files Browse the repository at this point in the history
  • Loading branch information
donkirkby committed Dec 12, 2023
1 parent d1c82f3 commit 028bae1
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 9 deletions.
8 changes: 6 additions & 2 deletions diagram.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ def register_svg():
def __init__(self,
page_width: float,
page_height: float,
board_state: str):
board_state: str,
half_width: bool = True):
self.page_width = page_width
self.page_height = page_height
self.board_state = board_state
self.half_width = half_width

def build(self) -> SvgDiagram:
if self.board_state.startswith('type: '):
Expand Down Expand Up @@ -103,7 +105,9 @@ def build(self) -> SvgDiagram:
original_view_size = 390 # chess library always uses this size
view_width = original_view_size + (margins[0] + margins[2])*square_size
view_height = original_view_size + (margins[1] + margins[3])*square_size
x_aspect = self.page_width/2/view_width
x_aspect = self.page_width/view_width
if self.half_width:
x_aspect /= 2
y_aspect = self.page_height/view_height
aspect = min(x_aspect, y_aspect)
image_width = view_width * aspect
Expand Down
29 changes: 29 additions & 0 deletions footer.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,32 @@ def draw_canvas(self):
bottom,
"https://donkirkby.github.io/chess-kit")
self.restoreState()


class ZineCanvas(FooterCanvas):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.pages = []

def showPage(self):
self.pages.append(dict(self.__dict__))
self._startPage()

def save(self):
while len(self.pages) % 8 != 0:
self.showPage()
original_pages = self.pages[:]
reordered_pages = []
while original_pages:
reordered_pages.append(original_pages.pop(1))
reordered_pages.append(original_pages.pop(-2))
reordered_pages.append(original_pages.pop(2))
reordered_pages.append(original_pages.pop(-3))
reordered_pages.append(original_pages.pop(-1))
reordered_pages.append(original_pages.pop(0))
reordered_pages.append(original_pages.pop(-1))
reordered_pages.append(original_pages.pop(0))
for page in reordered_pages:
self.__dict__.update(page)
super().showPage()
super().save()
30 changes: 23 additions & 7 deletions publish_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
from diagram import Diagram
from diagram_differ import LiveSvg, DiagramDiffer
from font_set import register_fonts
from footer import FooterCanvas
from footer import FooterCanvas, ZineCanvas
from book_parser import parse, Styles

PAGE_HEIGHT = defaultPageSize[1]
Expand All @@ -46,6 +46,9 @@ def parse_args():
parser.add_argument('--booklet',
action='store_true',
help='Use smaller pages for a booklet.')
parser.add_argument('--zine',
action='store_true',
help='Use 1/4 letter pages for a zine.')
parser.add_argument('--no-merge',
action='store_true',
help="Don't write merged markdown, only PDF.")
Expand All @@ -54,7 +57,10 @@ def parse_args():
nargs='?',
default=default_markdown,
help='markdown source file to convert')
return parser.parse_args()
args = parser.parse_args()
if args.zine:
args.booklet = True
return args


class DiagramWriter:
Expand Down Expand Up @@ -189,7 +195,10 @@ def main():
images_path,
is_disabled=args.no_merge)
if args.booklet:
page_size = (4.25 * inch, 6.875 * inch)
if args.zine:
page_size = (4.25 * inch, 5.5 * inch)
else:
page_size = (4.25 * inch, 6.875 * inch)
vertical_margin = 0.3 * inch
side_margin = 0.5 * inch
else:
Expand Down Expand Up @@ -281,16 +290,19 @@ def main():
story.append(Spacer(0, page_size[1] * 0.15))
story.append(Paragraph('Don Kirkby', centred_style))
story.append(Spacer(0, page_size[1] * 0.15))
story.append(Paragraph('???-?-????-????-?', centred_style))
story.append(Paragraph('Imprint: Lulu.com', centred_style))
if not args.zine:
story.append(Paragraph('???-?-????-????-?', centred_style))
story.append(Paragraph('Imprint: Lulu.com', centred_style))
story.append(cc_drawing)
story.append(Paragraph(f'{datetime.now().year}', centred_style))
story.append(PageBreak())
continue
elif state.style == Styles.Diagram:
half_width_diagrams = not args.booklet
flowable = Diagram(doc.width,
doc.height,
state.text).build().to_reportlab()
state.text,
half_width_diagrams).build().to_reportlab()
if len(headings) < 2:
prefix = 'diagram'
else:
Expand Down Expand Up @@ -377,7 +389,11 @@ def main():
unknown_section_message = (f'Unknown section{suffix} in contents: ' +
', '.join(unlinked_section_names))
raise ValueError(unknown_section_message)
doc.multiBuild(story, canvasmaker=partial(FooterCanvas,
if args.zine:
canvas_class = ZineCanvas
else:
canvas_class = FooterCanvas
doc.multiBuild(story, canvasmaker=partial(canvas_class,
font_name='Body',
is_booklet=args.booklet))
if not args.no_merge:
Expand Down

0 comments on commit 028bae1

Please sign in to comment.