|
| 1 | +"""Test catalog extraction.""" |
| 2 | + |
| 3 | +import libpdf |
| 4 | +from tests.conftest import PDF_COLOR_STYLE |
| 5 | + |
| 6 | + |
| 7 | +def test_colors_0() -> None: |
| 8 | + """Test word colors in given chapter paragraph.""" |
| 9 | + objects = libpdf.load(PDF_COLOR_STYLE) |
| 10 | + assert objects is not None |
| 11 | + assert objects.flattened.chapters |
| 12 | + |
| 13 | + for chapter in objects.flattened.chapters: |
| 14 | + if chapter.title == "Color in Text and Heading": |
| 15 | + assert chapter.textbox.ncolor == (1, 0, 0) |
| 16 | + |
| 17 | + |
| 18 | +def test_colors_1() -> None: |
| 19 | + """Test word colors in given chapter paragraph.""" |
| 20 | + objects = libpdf.load(PDF_COLOR_STYLE) |
| 21 | + assert objects is not None |
| 22 | + assert objects.flattened.chapters |
| 23 | + |
| 24 | + for chapter in objects.flattened.chapters: |
| 25 | + if chapter.title == "HorizontalLine": |
| 26 | + for content in chapter.content: |
| 27 | + if ( |
| 28 | + content.type == "paragraph" |
| 29 | + and "Paragraph text is blue" in content.textbox.text |
| 30 | + ): |
| 31 | + assert content.textbox.ncolor == (0, 0, 1) |
| 32 | + if ( |
| 33 | + content.type == "paragraph" |
| 34 | + and "This chapter is for" in content.textbox.text |
| 35 | + ): |
| 36 | + assert content.textbox.ncolor == (0, 0, 0) |
| 37 | + |
| 38 | + |
| 39 | +def test_colors_2() -> None: |
| 40 | + """Test word colors in given chapter paragraph.""" |
| 41 | + objects = libpdf.load(PDF_COLOR_STYLE) |
| 42 | + assert objects is not None |
| 43 | + assert objects.flattened.chapters |
| 44 | + |
| 45 | + for chapter in objects.flattened.chapters: |
| 46 | + if chapter.title == "HorizontalBox": |
| 47 | + for content in chapter.content: |
| 48 | + if content.type == "paragraph": |
| 49 | + assert content.textbox.ncolor == (0, 1, 0) |
| 50 | + elif chapter.title == "UncoloredHorizontalbox": |
| 51 | + for content in chapter.content: |
| 52 | + if content.type == "paragraph": |
| 53 | + assert content.textbox.ncolor is None |
| 54 | + for line in content.textbox.lines: |
| 55 | + assert line.ncolor is not None |
| 56 | + |
| 57 | + |
| 58 | +def test_colors_3() -> None: |
| 59 | + """Test word colors in given chapter paragraph.""" |
| 60 | + objects = libpdf.load(PDF_COLOR_STYLE) |
| 61 | + assert objects is not None |
| 62 | + assert objects.flattened.chapters |
| 63 | + |
| 64 | + for chapter in objects.flattened.chapters: |
| 65 | + if "Words" in chapter.title: |
| 66 | + for content in chapter.content: |
| 67 | + if ( |
| 68 | + content.type == "paragraph" |
| 69 | + and "This line has no color" in content.textbox.text |
| 70 | + ): |
| 71 | + assert content.textbox.ncolor is None |
| 72 | + |
| 73 | + for word in content.textbox.words: |
| 74 | + if word.text == "has": |
| 75 | + assert word.ncolor == (0, 0, 1) |
| 76 | + elif word.text == "color": |
| 77 | + assert word.ncolor in [(0, 1, 0), (0, 0, 0)] |
| 78 | + elif word.text == "changes": |
| 79 | + assert word.ncolor == (1, 0, 0) |
| 80 | + elif word.text == "words": |
| 81 | + assert word.ncolor == (0, 0, 1) |
| 82 | + |
| 83 | + |
| 84 | +def test_colors_4() -> None: |
| 85 | + """Test word colors in given chapter paragraph.""" |
| 86 | + objects = libpdf.load(PDF_COLOR_STYLE) |
| 87 | + assert objects is not None |
| 88 | + assert objects.flattened.chapters |
| 89 | + |
| 90 | + for chapter in objects.flattened.chapters: |
| 91 | + if "Words" in chapter.title: |
| 92 | + for content in chapter.content: |
| 93 | + if "This words have no color" in content.textbox.text: |
| 94 | + assert content.textbox.ncolor is None |
| 95 | + |
| 96 | + for word in content.textbox.words: |
| 97 | + assert word.ncolor is None or word.ncolor == (0, 0, 0) |
| 98 | + |
| 99 | + |
| 100 | +def test_colors_5() -> None: |
| 101 | + """Test word colors in given chapter paragraph.""" |
| 102 | + objects = libpdf.load(PDF_COLOR_STYLE) |
| 103 | + assert objects is not None |
| 104 | + assert objects.flattened.chapters |
| 105 | + |
| 106 | + for chapter in objects.flattened.chapters: |
| 107 | + if "Words" in chapter.title: |
| 108 | + for content in chapter.content: |
| 109 | + if "These words are printed" in content.textbox.text: |
| 110 | + assert content.textbox.ncolor is None |
| 111 | + |
| 112 | + for word in content.textbox.words: |
| 113 | + if word.text in ["words", "but"]: |
| 114 | + assert word.ncolor == (0, 1, 0) |
| 115 | + elif word.text == "printed": |
| 116 | + assert word.ncolor == (0, 0, 1) |
| 117 | + elif word.text == "background": |
| 118 | + assert word.ncolor == (1, 0, 0) |
| 119 | + |
| 120 | + |
| 121 | +def test_colors_6() -> None: |
| 122 | + """Test word colors in given chapter paragraph.""" |
| 123 | + objects = libpdf.load(PDF_COLOR_STYLE) |
| 124 | + assert objects is not None |
| 125 | + assert objects.flattened.chapters |
| 126 | + |
| 127 | + for chapter in objects.flattened.chapters: |
| 128 | + if "Styled Text" in chapter.title: |
| 129 | + for content in chapter.content: |
| 130 | + if "bold text format" in content.textbox.text: |
| 131 | + for word in content.textbox.words: |
| 132 | + if word.text == "bold": |
| 133 | + assert "Bold" in word.fontname |
| 134 | + else: |
| 135 | + assert "Bold" not in word.fontname |
| 136 | + elif "italic text format" in content.textbox.text: |
| 137 | + if word.text == "italic": |
| 138 | + assert "Italic" in word.fontname |
| 139 | + else: |
| 140 | + assert "Italic" not in word.fontname |
| 141 | + elif "underline text format" in content.textbox.text: |
| 142 | + # this seems to be exracted as rect |
| 143 | + pass |
0 commit comments