(blocks: List[Dict], page: fitz.Page)
| 603 | return nrows # curated list of line bottom coordinates |
| 604 | |
| 605 | def process_blocks(blocks: List[Dict], page: fitz.Page): |
| 606 | rows = set() |
| 607 | page_width = page.rect.width |
| 608 | page_height = page.rect.height |
| 609 | rowheight = page_height |
| 610 | left = page_width |
| 611 | right = 0 |
| 612 | chars = [] |
| 613 | for block in blocks: |
| 614 | for line in block["lines"]: |
| 615 | if line["dir"] != (1, 0): # ignore non-horizontal text |
| 616 | continue |
| 617 | x0, y0, x1, y1 = line["bbox"] |
| 618 | if y1 < 0 or y0 > page.rect.height: # ignore if outside CropBox |
| 619 | continue |
| 620 | # upd row height |
| 621 | height = y1 - y0 |
| 622 | |
| 623 | if rowheight > height: |
| 624 | rowheight = height |
| 625 | for span in line["spans"]: |
| 626 | if span["size"] <= fontsize: |
| 627 | continue |
| 628 | for c in span["chars"]: |
| 629 | x0, _, x1, _ = c["bbox"] |
| 630 | cwidth = x1 - x0 |
| 631 | ox, oy = c["origin"] |
| 632 | oy = int(round(oy)) |
| 633 | rows.add(oy) |
| 634 | ch = c["c"] |
| 635 | if left > ox and ch != " ": |
| 636 | left = ox # update left coordinate |
| 637 | if right < x1: |
| 638 | right = x1 # update right coordinate |
| 639 | # handle ligatures: |
| 640 | if cwidth == 0 and chars != []: # potential ligature |
| 641 | old_ch, old_ox, old_oy, old_cwidth = chars[-1] |
| 642 | if old_oy == oy: # ligature |
| 643 | if old_ch != chr(0xFB00): # previous "ff" char lig? |
| 644 | lig = joinligature(old_ch + ch) # no |
| 645 | # convert to one of the 3-char ligatures: |
| 646 | elif ch == "i": |
| 647 | lig = chr(0xFB03) # "ffi" |
| 648 | elif ch == "l": |
| 649 | lig = chr(0xFB04) # "ffl" |
| 650 | else: # something wrong, leave old char in place |
| 651 | lig = old_ch |
| 652 | chars[-1] = (lig, old_ox, old_oy, old_cwidth) |
| 653 | continue |
| 654 | chars.append((ch, ox, oy, cwidth)) # all chars on page |
| 655 | return chars, rows, left, right, rowheight |
| 656 | |
| 657 | def joinligature(lig: str) -> str: |
| 658 | """Return ligature character for a given pair / triple of characters. |
no test coverage detected
searching dependent graphs…