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

Function process_blocks

src/__main__.py:609–659  ·  view source on GitHub ↗
(blocks: List[Dict], page: pymupdf.Page)

Source from the content-addressed store, hash-verified

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"]
639 if left > ox and ch != " ":
640 left = ox # update left coordinate
641 if right < x1:
642 right = x1 # update right coordinate
643 # handle ligatures:
644 if cwidth == 0 and chars != []: # potential ligature
645 old_ch, old_ox, old_oy, old_cwidth = chars[-1]
646 if old_oy == oy: # ligature
647 if old_ch != chr(0xFB00): # previous "ff" char lig?
648 lig = joinligature(old_ch + ch) # no
649 # convert to one of the 3-char ligatures:
650 elif ch == "i":
651 lig = chr(0xFB03) # "ffi"
652 elif ch == "l":
653 lig = chr(0xFB04) # "ffl"
654 else: # something wrong, leave old char in place
655 lig = old_ch
656 chars[-1] = (lig, old_ox, old_oy, old_cwidth)
657 continue
658 chars.append((ch, ox, oy, cwidth)) # all chars on page
659 return chars, rows, left, right, rowheight
660
661 def joinligature(lig: str) -> str:
662 """Return ligature character for a given pair / triple of characters.

Callers 1

page_layoutFunction · 0.70

Calls 3

joinligatureFunction · 0.70
addMethod · 0.45
appendMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…