MCPcopy Create free account
hub / github.com/pymupdf/PyMuPDF / bbox_count

Function bbox_count

tests/test_general.py:529–561  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

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

Callers 1

test_2246Function · 0.85

Calls 9

appendMethod · 0.95
write_textMethod · 0.95
new_pageMethod · 0.80
xref_get_keyMethod · 0.80
xref_set_keyMethod · 0.80
set_rotationMethod · 0.45
insert_textMethod · 0.45
get_textMethod · 0.45
addMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…