Make a page and insert identical text via different methods. Desired page rotation is a parameter. MediaBox and CropBox are chosen to be "awkward": MediaBox does not start at (0,0) and CropBox is a true subset of MediaBox.
(rot)
| 527 | (which is assumed correct) and TextWriter.write_text(). |
| 528 | """ |
| 529 | def bbox_count(rot): |
| 530 | """Make a page and insert identical text via different methods. |
| 531 | |
| 532 | Desired page rotation is a parameter. MediaBox and CropBox are chosen |
| 533 | to be "awkward": MediaBox does not start at (0,0) and CropBox is a |
| 534 | true subset of MediaBox. |
| 535 | """ |
| 536 | # bboxes of spans on page: same text positions are represented by ONE bbox |
| 537 | bboxes = set() |
| 538 | doc = pymupdf.open() |
| 539 | # prepare a page with desired MediaBox / CropBox peculiarities |
| 540 | mediabox = pymupdf.paper_rect("letter") |
| 541 | page = doc.new_page(width=mediabox.width, height=mediabox.height) |
| 542 | xref = page.xref |
| 543 | newmbox = list(map(float, doc.xref_get_key(xref, "MediaBox")[1][1:-1].split())) |
| 544 | newmbox = pymupdf.Rect(newmbox) |
| 545 | mbox = newmbox + (10, 20, 10, 20) |
| 546 | cbox = mbox + (10, 10, -10, -10) |
| 547 | doc.xref_set_key(xref, "MediaBox", "[%g %g %g %g]" % tuple(mbox)) |
| 548 | doc.xref_set_key(xref, "CrobBox", "[%g %g %g %g]" % tuple(cbox)) |
| 549 | # set page to desired rotation |
| 550 | page.set_rotation(rot) |
| 551 | page.insert_text((50, 50), "Text inserted at (50,50)") |
| 552 | tw = pymupdf.TextWriter(page.rect) |
| 553 | tw.append((50, 50), "Text inserted at (50,50)") |
| 554 | tw.write_text(page) |
| 555 | blocks = page.get_text("dict")["blocks"] |
| 556 | for b in blocks: |
| 557 | for l in b["lines"]: |
| 558 | for s in l["spans"]: |
| 559 | # store bbox rounded to 3 decimal places |
| 560 | bboxes.add(pymupdf.Rect(pymupdf.JM_TUPLE3(s["bbox"]))) |
| 561 | return len(bboxes) # should be 1! |
| 562 | |
| 563 | # the following tests must all pass |
| 564 | assert bbox_count(0) == 1 |
no test coverage detected
searching dependent graphs…