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

Function test_2246

tests/test_general.py:510–567  ·  view source on GitHub ↗

Test / confirm identical text positions generated by * page.insert_text() versus * TextWriter.write_text() ... under varying situations as follows: 1. MediaBox does not start at (0, 0) 2. CropBox origin is different from that of MediaBox 3. Check for all 4 possible

()

Source from the content-addressed store, hash-verified

508
509
510def test_2246():
511 """
512 Test / confirm identical text positions generated by
513 * page.insert_text()
514 versus
515 * TextWriter.write_text()
516
517 ... under varying situations as follows:
518
519 1. MediaBox does not start at (0, 0)
520 2. CropBox origin is different from that of MediaBox
521 3. Check for all 4 possible page rotations
522
523 The test writes the same text at the same positions using `page.insert_text()`,
524 respectively `TextWriter.write_text()`.
525 Then extracts the text spans and confirms that they all occupy the same bbox.
526 This ensures coincidence of text positions of page.of insert_text()
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
565 assert bbox_count(90) == 1
566 assert bbox_count(180) == 1
567 assert bbox_count(270) == 1

Callers

nothing calls this directly

Calls 1

bbox_countFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…