From 028bae1bd959e16856bc2421371ad619dcf45142 Mon Sep 17 00:00:00 2001 From: Don Kirkby Date: Mon, 11 Dec 2023 22:34:13 -0800 Subject: [PATCH] Add zine mode for quarter-size letter paper. --- diagram.py | 8 ++++++-- footer.py | 29 +++++++++++++++++++++++++++++ publish_rules.py | 30 +++++++++++++++++++++++------- 3 files changed, 58 insertions(+), 9 deletions(-) diff --git a/diagram.py b/diagram.py index ef6380f..6630c9e 100644 --- a/diagram.py +++ b/diagram.py @@ -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: '): @@ -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 diff --git a/footer.py b/footer.py index 0a92bee..cb767e9 100644 --- a/footer.py +++ b/footer.py @@ -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() diff --git a/publish_rules.py b/publish_rules.py index 1a64455..c86b0cf 100644 --- a/publish_rules.py +++ b/publish_rules.py @@ -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] @@ -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.") @@ -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: @@ -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: @@ -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: @@ -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: