Write HTML-styled text into a rect with different rotations. The text is styled and contains a link. Then extract the text again, and - assert that text was written in the 4 different angles, - assert that text properties are correct (bold, italic, color), - assert that the link
()
| 157 | |
| 158 | |
| 159 | def test_htmlbox1(): |
| 160 | """Write HTML-styled text into a rect with different rotations. |
| 161 | |
| 162 | The text is styled and contains a link. |
| 163 | Then extract the text again, and |
| 164 | - assert that text was written in the 4 different angles, |
| 165 | - assert that text properties are correct (bold, italic, color), |
| 166 | - assert that the link has been correctly inserted. |
| 167 | |
| 168 | We try to insert into a rectangle that is too small, setting |
| 169 | scale=False and confirming we have a negative return code. |
| 170 | """ |
| 171 | if not hasattr(pymupdf, "mupdf"): |
| 172 | print("'test_htmlbox1' not executed in classic.") |
| 173 | return |
| 174 | |
| 175 | rect = pymupdf.Rect(100, 100, 200, 200) # this only works with scale=True |
| 176 | |
| 177 | base_text = """Lorem ipsum dolor sit amet, consectetur adipisici elit, sed eiusmod tempor incidunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquid ex ea commodi consequat. Quis aute iure reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint obcaecat cupiditat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.""" |
| 178 | |
| 179 | text = """Lorem ipsum dolor sit amet, consectetur adipisici elit, sed eiusmod tempor incidunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation <b>ullamco</b> <i>laboris</i> nisi ut aliquid ex ea commodi consequat. Quis aute iure reprehenderit in <span style="color: #0f0;font-weight:bold;">voluptate</span> velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint obcaecat cupiditat non proident, sunt in culpa qui <a href="https://www.artifex.com">officia</a> deserunt mollit anim id est laborum.""" |
| 180 | |
| 181 | doc = pymupdf.Document() |
| 182 | |
| 183 | for rot in (0, 90, 180, 270): |
| 184 | wdirs = ((1, 0), (0, -1), (-1, 0), (0, 1)) # all writing directions |
| 185 | page = doc.new_page() |
| 186 | spare_height, scale = page.insert_htmlbox(rect, text, rotate=rot, scale_low=1) |
| 187 | assert spare_height < 0 |
| 188 | assert scale == 1 |
| 189 | spare_height, scale = page.insert_htmlbox(rect, text, rotate=rot, scale_low=0) |
| 190 | page.draw_rect(rect, (1, 0, 0)) |
| 191 | doc.save(os.path.normpath(f'{__file__}/../../tests/test_htmlbox1.pdf')) |
| 192 | assert abs(spare_height - 3.8507) < 0.001 |
| 193 | assert 0 < scale < 1 |
| 194 | page = doc.reload_page(page) |
| 195 | link = page.get_links()[0] # extracts the links on the page |
| 196 | |
| 197 | assert link["uri"] == "https://www.artifex.com" |
| 198 | |
| 199 | # Assert plain text is complete. |
| 200 | # We must remove line breaks and any ligatures for this. |
| 201 | assert base_text == page.get_text(flags=0)[:-1].replace("\n", " ") |
| 202 | |
| 203 | encounters = 0 # counts the words with selected properties |
| 204 | for b in page.get_text("dict")["blocks"]: |
| 205 | for l in b["lines"]: |
| 206 | wdir = l["dir"] # writing direction |
| 207 | assert wdir == wdirs[page.number] |
| 208 | for s in l["spans"]: |
| 209 | stext = s["text"] |
| 210 | color = pymupdf.sRGB_to_pdf(s["color"]) |
| 211 | bold = bool(s["flags"] & 16) |
| 212 | italic = bool(s["flags"] & 2) |
| 213 | if stext in ("ullamco", "laboris", "voluptate"): |
| 214 | encounters += 1 |
| 215 | if stext == "ullamco": |
| 216 | assert bold is True |
nothing calls this directly
no test coverage detected
searching dependent graphs…