"""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, hoist_titles, 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")), block(40, 415, 120, 425, line(40, 415, 120, "Iron Shield")), block(300, 415, 320, 425, line(300, 415, 320, "+3")), block(500, 415, 540, 425, line(500, 415, 540, "20 gp")), ] placed = place_lines(blocks, WIDTH, HEIGHT) assert [p.text for p in placed[-2:]] == ["Wooden Buckler | +2 | 5 gp", "Iron Shield | +3 | 20 gp"] assert placed[-1].cells == 3 def test_two_lone_labels_at_the_same_height_are_not_a_table_row(): """Double colonne de sorts : « CANTRIP » ouvre un sort à gauche, « TIER 4 » un autre à droite.""" blocks = [ paragraph(40, 100, 560, 300), block(40, 400, 80, 408, line(40, 400, 80, "CANTRIP", 8.0)), block(40, 410, 260, 450, line(40, 410, 260, "Rebuke : la cible subit 1d8 dégâts radiants,"), line(40, 422, 260, "et ne peut plus attaquer ce tour.")), block(300, 400, 335, 408, line(300, 400, 335, "TIER 4", 8.0)), block(300, 410, 540, 450, line(300, 410, 540, "Shield of Justice : vos attaquants sont aveuglés"), line(300, 422, 540, "jusqu'à la fin de leur prochain tour.")), block(40, 520, 80, 528, line(40, 520, 80, "TIER 1", 8.0)), block(300, 520, 335, 528, line(300, 520, 335, "TIER 6", 8.0)), ] read = [p.text for p in place_lines(blocks, WIDTH, HEIGHT)] assert "CANTRIP" in read and "TIER 4" in read and "TIER 1" in read and "TIER 6" in read assert not any(" | " in text for text in read) 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")), block(40, 410, 120, 420, line(40, 410, 120, "Iron Shield")), block(200, 410, 220, 420, line(200, 410, 220, "+3")), 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] # --- grands titres dessinés après leur texte --------------------------------------------------- def _spell(x0: float, y0: float, name: str) -> LayoutBlock: return block(x0, y0, x0 + 160, y0 + 60, LayoutLine(x0, y0, x0 + 160, y0 + 12, name, 12.0), line(x0, y0 + 14, x0 + 160, "Portée 8. Dégâts 2d12, puis la cible est entravée jusqu'à la fin.")) def _big_title(x0: float, y0: float, text: str) -> LayoutBlock: return block(x0, y0, x0 + 150, y0 + 30, LayoutLine(x0, y0, x0 + 150, y0 + 30, text, 25.0)) def test_a_big_title_drawn_after_the_text_it_heads_is_put_back_above_it(): """Double page d'un livre de sorts : chaque moitié a deux colonnes de sorts sous son titre, mais le PDF trace les sorts d'abord, le titre de l'école ensuite.""" width, height = 840.0, 600.0 radiant = [_spell(30, 80, "Rebuke"), _spell(30, 200, "Heal"), _spell(220, 80, "Vengeance")] necrotic = [block(450, 100, 475, 110, line(450, 100, 475, "CANTRIP", 7.5)), _spell(450, 115, "Entice"), _spell(640, 115, "Vigor Mortis")] radiant_title, necrotic_title = _big_title(150, 30, "Radiant Spells"), _big_title(570, 60, "Necrotic Spells") redeem = _spell(80, 520, "Redeem") drawn = [*radiant, radiant_title, redeem, *necrotic, necrotic_title] ordered = hoist_titles(drawn, width, height) assert ordered == [radiant_title, *radiant, redeem, necrotic_title, *necrotic] # Par place_lines, le texte suit : le titre de l'école précède ses sorts. read = [placed.text for placed in place_lines(drawn, width, height)] assert read.index("Radiant Spells") < read.index("Rebuke") < read.index("Redeem") assert read.index("Redeem") < read.index("Necrotic Spells") < read.index("CANTRIP") < read.index("Entice") def test_a_label_brushing_the_bottom_of_a_big_title_is_still_below_it(): """La boîte d'un titre de 25 points descend sous ses lettres : l'étiquette « CANTRIP » du premier sort la frôle, sans être au-dessus du titre.""" title = _big_title(150, 30, "Radiant Spells") # boîte de 30 à 60 label = block(30, 57, 60, 66, line(30, 57, 60, "CANTRIP", 7.5)) spell = _spell(30, 70, "Rebuke") assert hoist_titles([label, spell, title], 840.0, 600.0) == [title, label, spell] def test_two_subheadings_at_the_same_height_in_two_columns_are_not_a_table_row(): left = block(30, 200, 190, 212, LayoutLine(30, 200, 190, 212, "Heal | 1 Action", 12.0, 2)) right = block(220, 200, 380, 212, LayoutLine(220, 200, 380, 212, "Sacrifice | 1 Action", 12.0, 2)) texts_ = [paragraph(30, 216, 190, 260), paragraph(220, 216, 380, 260)] read = [placed.text for placed in place_lines([left, texts_[0], right, texts_[1]], 840.0, 600.0)] assert "Heal | 1 Action" in read and "Sacrifice | 1 Action" in read # Des cellules à la taille du texte courant restent une rangée. cells = [block(30, 300, 80, 310, line(30, 300, 80, "Épée")), block(110, 300, 160, 310, line(110, 300, 160, "1d8")), block(30, 314, 80, 324, line(30, 314, 80, "Dague")), block(110, 314, 160, 324, line(110, 314, 160, "1d4"))] assert "Épée | 1d8" in [placed.text for placed in place_lines([*texts_, *cells], 840.0, 600.0)] def test_a_title_already_in_place_or_heading_another_column_does_not_move(): # Page simple, deux colonnes : le titre en tête de la seconde ne passe pas devant la première. first = [paragraph(40, 60, 280, 300), paragraph(40, 320, 280, 560)] title = _big_title(320, 60, "Magie") second = [paragraph(320, 100, 560, 300)] drawn = [*first, title, *second] assert hoist_titles(drawn, WIDTH, HEIGHT) == drawn # Un titre placé plus bas que ce qui le précède reste où il est. lower = [paragraph(40, 60, 560, 200), _big_title(40, 240, "Combat"), paragraph(40, 290, 560, 400)] assert hoist_titles(lower, WIDTH, HEIGHT) == lower def test_a_title_does_not_climb_over_a_block_that_spans_the_page(): banner = paragraph(40, 200, 560, 240) # barre la page, sous le titre below = paragraph(40, 260, 280, 400) title = _big_title(40, 150, "Rituels") drawn = [paragraph(40, 60, 560, 120), banner, below, title] # Le titre remonte devant le bloc qu'il coiffe, pas au-delà de la bande qui barre la page. assert hoist_titles(drawn, WIDTH, HEIGHT) == [drawn[0], banner, title, below] 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