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

Function page_layout

src/__main__.py:581–803  ·  view source on GitHub ↗
(page, textout, GRID, fontsize, noformfeed, skip_empty, flags)

Source from the content-addressed store, hash-verified

579
580
581def page_layout(page, textout, GRID, fontsize, noformfeed, skip_empty, flags):
582 eop = b"\n" if noformfeed else bytes([12])
583
584 # --------------------------------------------------------------------
585 def find_line_index(values: List[int], value: int) -> int:
586 """Find the right row coordinate.
587
588 Args:
589 values: (list) y-coordinates of rows.
590 value: (int) lookup for this value (y-origin of char).
591 Returns:
592 y-ccordinate of appropriate line for value.
593 """
594 i = bisect.bisect_right(values, value)
595 if i:
596 return values[i - 1]
597 raise RuntimeError(f"Line for {value:g} not found in {values}")
598
599 # --------------------------------------------------------------------
600 def curate_rows(rows: Set[int], GRID) -> List:
601 rows = list(rows)
602 rows.sort() # sort ascending
603 nrows = [rows[0]]
604 for h in rows[1:]:
605 if h >= nrows[-1] + GRID: # only keep significant differences
606 nrows.append(h)
607 return nrows # curated list of line bottom coordinates
608
609 def process_blocks(blocks: List[Dict], page: pymupdf.Page):
610 rows = set()
611 page_width = page.rect.width
612 page_height = page.rect.height
613 rowheight = page_height
614 left = page_width
615 right = 0
616 chars = []
617 for block in blocks:
618 for line in block["lines"]:
619 if line["dir"] != (1, 0): # ignore non-horizontal text
620 continue
621 x0, y0, x1, y1 = line["bbox"]
622 if y1 < 0 or y0 > page.rect.height: # ignore if outside CropBox
623 continue
624 # upd row height
625 height = y1 - y0
626
627 if rowheight > height:
628 rowheight = height
629 for span in line["spans"]:
630 if span["size"] <= fontsize:
631 continue
632 for c in span["chars"]:
633 x0, _, x1, _ = c["bbox"]
634 cwidth = x1 - x0
635 ox, oy = c["origin"]
636 oy = int(round(oy))
637 rows.add(oy)
638 ch = c["c"]

Callers

nothing calls this directly

Calls 8

process_blocksFunction · 0.70
curate_rowsFunction · 0.70
find_line_indexFunction · 0.70
make_textlineFunction · 0.70
get_textMethod · 0.45
writeMethod · 0.45
getMethod · 0.45
appendMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…