-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest_rects.py
234 lines (162 loc) · 6.36 KB
/
test_rects.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
"""Test rects extraction."""
from __future__ import annotations
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from collections.abc import Sequence
from libpdf.apiobjects import ApiObjects, Chapter, Paragraph, Rect, Table
import libpdf
from tests.conftest import (
PDF_RECTS_EXTRACTION,
)
def find_chapter(objects: ApiObjects, chapter_name: str) -> Chapter:
"""
search for given chapter in the objects.
:return: found chapter
"""
chapters = objects.flattened.chapters
assert len(chapters) > 0
chapter = next((c for c in chapters if c.title == chapter_name), None)
assert chapter is not None
return chapter
def check_chapter_contains_text_paragraph(
chapter: Chapter, text: str
) -> [Paragraph | None]:
"""
check for text in chapter paragraphs.
:return: found paragraph
"""
assert chapter.content is not None
for content in chapter.content:
if content.type == "paragraph" and text in content.textbox.text:
return content
return None
def check_chapter_contains_text_rect(chapter: Chapter, text: str) -> [Paragraph | None]:
"""
check for text in chapter rects.
:return: found rect
"""
assert chapter.content is not None
for content in chapter.content:
if content.type == "rect" and text in content.textbox.text:
return content
return None
def check_chapter_rects_count(chapter: Chapter) -> int:
"""
check for number of rects in chapter.
:return: number of rects
"""
assert chapter.content is not None
count = 0
for content in chapter.content:
if content.type == "rect":
count += 1
return count
def check_content_color(content: Rect, color: Sequence[int]) -> bool:
"""
check rect color is equal given color.
:return: True if equal
"""
return content.non_stroking_color == color
def check_content_margins_equal(rect: Rect, paragraph: Paragraph) -> bool:
"""
check for text margins between rect and paragraph.
:return: True if x0 equals
"""
return paragraph.textbox.x0 == rect.textbox.x0
def check_content_margins_greater(
rect: Rect, paragraph: Paragraph, offset: int
) -> bool:
"""
check for text margins of given rect.x0 + offset is greater or equal paragraph x0.
:return: found paragraph
"""
return rect.textbox.x0 + offset >= paragraph.textbox.x0
def find_tables(chapter: Chapter) -> [Table]:
"""
check for number of rects in chapter.
:return: List of Tables
"""
assert chapter.content is not None
return [content for content in chapter.content if content.type == "table"]
def test_rects_extraction_code_block() -> None:
"""Test rect extraction of multiline codeblock."""
smart_page_crop = (
True # remove header and footers so rects IN chapters are left only.
)
objects = libpdf.load(PDF_RECTS_EXTRACTION, smart_page_crop=smart_page_crop)
assert objects.flattened.rects is not None
chapter = find_chapter(objects, "Code Block Highlighting")
assert chapter is not None
assert check_chapter_rects_count(chapter) == 1
paragraph = check_chapter_contains_text_paragraph(
chapter, "def decode_title(obj_bytes: bytes) -> str:"
)
assert paragraph is not None
rect = check_chapter_contains_text_rect(
chapter, "def decode_title(obj_bytes: bytes) -> str:"
)
assert rect is not None
assert check_content_color(rect, (0.941176, 0.941176, 0.941176))
assert check_content_margins_equal(paragraph, rect)
def test_rects_extraction_code_inline() -> None:
"""Test rect extraction of inline codeblock."""
smart_page_crop = (
True # remove header and footers so rects IN chapters are left only.
)
objects = libpdf.load(PDF_RECTS_EXTRACTION, smart_page_crop=smart_page_crop)
assert objects.flattened.rects is not None
chapter = find_chapter(objects, "Code Inline Highlighting")
# 2 inline code blocks, but the first one is broken in two lines
assert check_chapter_rects_count(chapter) == 1 * 3
paragraph = check_chapter_contains_text_paragraph(
chapter, "from pathlib import Path"
)
assert paragraph is not None
rect = check_chapter_contains_text_rect(chapter, "from pathlib import Path")
assert rect is not None
assert rect.textbox.text == "from pathlib import Path"
assert check_content_color(rect, (0.945098, 0.945098, 0.945098))
assert check_content_margins_greater(rect, paragraph, 234)
assert (
check_chapter_contains_text_rect(
chapter, "decode_title(obj_bytes: bytes) -> str"
)
is None
)
rect = check_chapter_contains_text_rect(chapter, "decode_title(obj_bytes: bytes)")
assert rect is not None
rect_str = check_chapter_contains_text_rect(chapter, "str ")
assert rect_str is not None
assert rect_str.textbox.x0 < rect.textbox.x0
def test_rects_extraction_adminition() -> None:
"""Test rect extraction of 3 admonitions."""
smart_page_crop = (
True # remove header and footers so rects IN chapters are left only.
)
objects = libpdf.load(PDF_RECTS_EXTRACTION, smart_page_crop=smart_page_crop)
assert objects.flattened.rects is not None
chapter = find_chapter(objects, "Adminition")
assert (
check_chapter_rects_count(chapter) == 3 * 2
) # 2 rects per admonition, 3 types of admonition
rect = check_chapter_contains_text_rect(chapter, "A very importing Adminition")
assert rect is not None
assert check_content_color(rect, (0.858824, 0.980392, 0.956863))
rect_inner = check_chapter_contains_text_rect(chapter, "Wichtig")
assert rect_inner is not None
assert check_content_color(rect, (0.858824, 0.980392, 0.956863))
def test_rects_extraction_table() -> None:
"""Test rect extraction of table colored cells."""
smart_page_crop = (
True # remove header and footers so rects IN chapters are left only.
)
objects = libpdf.load(PDF_RECTS_EXTRACTION, smart_page_crop=smart_page_crop)
assert objects.flattened.rects is not None
chapter = find_chapter(objects, "Tables")
tables = find_tables(chapter)
assert len(tables) == 1
table = tables[0]
assert table.columns_count == 1 * 3
assert table.rows_count == 1
# assert check_chapter_rects_count(chapter) == 1 * 5
assert check_chapter_rects_count(chapter) == 17