"""Mise en page d'une page de PDF : ordre de lecture par colonnes, rangées de tableau.""" from __future__ import annotations from app.infrastructure.pdf_layout import ( CELL_SEPARATOR, LayoutBlock, LayoutLine, place_lines, reading_order, split_cells, without_overprints, ) WIDTH, HEIGHT = 600.0, 800.0 PARAGRAPH = "Texte d'un paragraphe assez long pour dessiner une colonne. " * 4 def line(x0: float, y0: float, x1: float, text: str, size: float = 10.0) -> LayoutLine: return LayoutLine(x0, y0, x1, y0 + size, text, size) def block(x0: float, y0: float, x1: float, y1: float, *lines: LayoutLine) -> LayoutBlock: return LayoutBlock(x0, y0, x1, y1, list(lines)) def paragraph(x0: float, y0: float, x1: float, y1: float, text: str = PARAGRAPH) -> LayoutBlock: return block(x0, y0, x1, y1, line(x0, y0, x1, text)) def texts(blocks: list[LayoutBlock]) -> list[str]: return [b.lines[0].text[:12] for b in blocks] # --- ordre de lecture ------------------------------------------------------------------------ def _two_columns_out_of_order() -> list[LayoutBlock]: """Deux colonnes nettes, dessinées dans le désordre : bas à droite, bas à gauche, haut à gauche, haut à droite (vu dans un vrai livre).""" return [ paragraph(310, 400, 560, 700, "D bas droite " + PARAGRAPH), paragraph(40, 400, 290, 700, "B bas gauche " + PARAGRAPH), paragraph(40, 100, 290, 380, "A haut gauche " + PARAGRAPH), paragraph(310, 100, 560, 380, "C haut droite " + PARAGRAPH), ] def test_a_page_drawn_out_of_order_is_read_column_by_column(): ordered = reading_order(_two_columns_out_of_order(), WIDTH, HEIGHT) assert [b.lines[0].text[0] for b in ordered] == ["A", "B", "C", "D"] def test_a_page_already_in_order_is_left_untouched(): blocks = [paragraph(40, 100, 290, 380), paragraph(40, 400, 290, 700), paragraph(310, 100, 560, 380), paragraph(310, 400, 560, 700)] assert reading_order(blocks, WIDTH, HEIGHT) is blocks def test_a_full_width_block_separates_two_bands(): blocks = [ paragraph(310, 450, 560, 700, "E droite, sous le titre " + PARAGRAPH), paragraph(40, 450, 290, 700, "D gauche, sous le titre " + PARAGRAPH), paragraph(40, 400, 560, 430, "C titre pleine largeur " + PARAGRAPH), paragraph(310, 100, 560, 380, "B droite, au-dessus " + PARAGRAPH), paragraph(40, 100, 290, 380, "A gauche, au-dessus " + PARAGRAPH), ] assert [b.lines[0].text[0] for b in reading_order(blocks, WIDTH, HEIGHT)] == ["A", "B", "C", "D", "E"] def test_two_stacked_bands_of_columns_are_read_one_after_the_other(): """Sans bloc pleine largeur entre elles : c'est le grand blanc horizontal qui les sépare. Lue colonne entière par colonne entière, la page coupait une phrase en deux.""" blocks = [ paragraph(310, 460, 560, 700, "D bas droite " + PARAGRAPH), paragraph(40, 460, 290, 700, "C bas gauche " + PARAGRAPH), paragraph(40, 100, 290, 380, "A haut gauche " + PARAGRAPH), paragraph(310, 100, 560, 380, "B haut droite " + PARAGRAPH), ] assert [b.lines[0].text[0] for b in reading_order(blocks, WIDTH, HEIGHT)] == ["A", "B", "C", "D"] def test_a_block_straddling_two_columns_keeps_the_order_of_the_pdf(): """Double page à quatre colonnes : un petit titre à cheval faisait entremêler deux colonnes.""" blocks = _two_columns_out_of_order() + [block(200, 385, 400, 398, line(200, 385, 400, "Titre à cheval"))] assert reading_order(blocks, WIDTH, HEIGHT) is blocks def test_a_block_outside_every_column_keeps_the_order_of_the_pdf(): blocks = _two_columns_out_of_order() + [block(570, 300, 598, 320, line(570, 300, 598, "note de marge"))] assert reading_order(blocks, WIDTH, HEIGHT) is blocks def test_a_footer_drawn_first_does_not_reorder_the_page(): footer = block(40, 770, 200, 785, line(40, 770, 200, "30 | Hell Comes to Bogtown, pied de page répété")) body = [paragraph(40, 100, 290, 700), paragraph(310, 100, 560, 700)] blocks = [footer, *body] assert reading_order(blocks, WIDTH, HEIGHT) is blocks # --- rangées de tableau ------------------------------------------------------------------------ def test_cells_at_the_same_height_in_a_block_are_one_row(): row = block(40, 100, 560, 110, line(40, 100, 90, "Dagger"), line(150, 100, 260, "1d4+DEX piercing"), line(300, 100, 380, "Light, Thrown 4"), line(520, 100, 560, "3 gp")) placed = place_lines([row], WIDTH, HEIGHT) assert [(p.text, p.cells) for p in placed] == [("Dagger | 1d4+DEX piercing | Light, Thrown 4 | 3 gp", 4)] def test_cells_of_a_row_spread_over_several_blocks_are_gathered(): blocks = [ paragraph(40, 100, 560, 300), # dessine la colonne block(40, 400, 120, 410, line(40, 400, 120, "Wooden Buckler")), block(300, 400, 320, 410, line(300, 400, 320, "+2")), block(500, 400, 540, 410, line(500, 400, 540, "5 gp")), ] placed = place_lines(blocks, WIDTH, HEIGHT) assert placed[-1].text == "Wooden Buckler | +2 | 5 gp" and placed[-1].cells == 3 def test_lines_of_a_paragraph_are_never_pulled_into_a_neighbouring_row(): """Un encadré à la hauteur d'un tableau y laissait des bouts de phrase.""" sidebar = block(330, 395, 560, 430, line(330, 395, 560, "Deflect! Heroes proficient with shields"), line(330, 410, 560, "may reduce the damage of an attack.")) blocks = [ paragraph(40, 100, 560, 300), block(40, 395, 120, 405, line(40, 395, 120, "Wooden Buckler")), block(200, 395, 220, 405, line(200, 395, 220, "+2")), sidebar, ] placed = place_lines(blocks, WIDTH, HEIGHT) assert "Wooden Buckler | +2" in [p.text for p in placed] assert "Deflect! Heroes proficient with shields" in [p.text for p in placed] def test_a_bullet_and_its_text_are_an_ordinary_line(): item = block(40, 100, 560, 110, line(40, 100, 46, "•"), line(60, 100, 560, "2-handed. Can be held in a single hand.")) placed = place_lines([item], WIDTH, HEIGHT) assert [(p.text, p.cells) for p in placed] == [("• 2-handed. Can be held in a single hand.", 1)] def test_two_columns_of_text_at_the_same_height_are_not_a_table(): blocks = [block(40, 100, 290, 130, line(40, 100, 290, "Le combat se joue en tours et chaque héros " * 2), line(40, 115, 290, "agit une fois. " * 6)), block(310, 100, 560, 130, line(310, 100, 560, "La magie coûte du mana au lanceur de sorts. " * 2), line(310, 115, 560, "Un sort rate sur un 1. " * 4))] assert all(p.cells == 1 for p in place_lines(blocks, WIDTH, HEIGHT)) def test_lines_at_the_top_and_bottom_of_the_page_are_flagged(): blocks = [block(40, 20, 300, 32, line(40, 20, 300, "NIMBLE — RÈGLES")), paragraph(40, 100, 560, 300), block(280, 770, 300, 782, line(280, 770, 300, "12"))] assert [p.edge for p in place_lines(blocks, WIDTH, HEIGHT)] == [True, False, True] def test_a_title_drawn_twice_at_the_same_place_is_read_once(): title = line(50, 100, 120, "Notes", 18.0) twice = block(50, 100, 300, 140, title, LayoutLine(50.2, 100.1, 120.1, 118.0, "Notes", 18.0), line(50, 120, 300, "Avant de jouer cette aventure, lisez-la en entier.")) placed = place_lines([twice], WIDTH, HEIGHT) assert [p.text for p in placed] == ["Notes", "Avant de jouer cette aventure, lisez-la en entier."] # Un bloc qui ne contient que la copie disparaît ; un bloc intact reste le même objet. copy = block(50, 100, 120, 118, title) intact = paragraph(50, 200, 300, 240) assert without_overprints([block(50, 100, 120, 118, title), copy, intact])[1:] == [intact] def test_a_chapter_tab_in_the_side_margin_is_not_text(): text = paragraph(60, 100, 540, 140) tab = block(578, 60, 590, 260, LayoutLine(578, 60, 590, 80, "1", 18.0), LayoutLine(578, 90, 590, 260, "Les Passagers du destin", 9.0)) left_tab = block(8, 60, 20, 260, LayoutLine(8, 90, 20, 260, "Les Passagers du destin", 9.0)) placed = place_lines([tab, text, left_tab], WIDTH, HEIGHT) assert [p.text for p in placed] == [PARAGRAPH] # Une ligne qui dépasse de la marge reste du texte. wide = block(8, 300, 300, 312, line(8, 300, 300, "Une note qui commence dans la marge.")) assert len(place_lines([text, wide], WIDTH, HEIGHT)) == 2 def test_the_same_words_elsewhere_on_the_page_are_kept(): rows = block(50, 100, 300, 140, line(50, 100, 120, "1d6"), line(50, 112, 120, "1d6"), line(200, 100, 270, "1d6")) assert len(without_overprints([rows])[0].lines) == 3 def test_no_text_no_lines(): assert place_lines([], WIDTH, HEIGHT) == [] assert place_lines([LayoutBlock(0, 0, 10, 10, [])], WIDTH, HEIGHT) == [] # --- cellules d'une ligne ------------------------------------------------------------------------ def test_a_wide_gap_in_a_line_separates_two_cells(): spans = [(40.0, 80.0, "Dagger"), (150.0, 260.0, "1d4+DEX "), (260.0, 300.0, "piercing"), (520.0, 560.0, "3 gp")] assert split_cells(spans, 10.0) == ["Dagger", "1d4+DEX piercing", "3 gp"] def test_words_of_a_sentence_are_one_cell(): spans = [(40.0, 120.0, "Le combat "), (122.0, 200.0, "se joue "), (203.0, 300.0, "en tours.")] assert split_cells(spans, 10.0) == ["Le combat se joue en tours."] def test_a_number_and_its_text_are_one_cell(): assert split_cells([(40.0, 52.0, "10"), (80.0, 400.0, "Londar s'est fait sauter.")], 10.0) == [ "10 Londar s'est fait sauter."] assert split_cells([(40.0, 46.0, "•"), (70.0, 400.0, "Portée 8")], 10.0) == ["• Portée 8"] def test_the_cell_separator_is_the_one_the_structure_splitter_expects(): from app.application.pdf_structure import CELL_SEPARATOR as expected assert CELL_SEPARATOR == expected