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

Function make_textline

src/__main__.py:687–738  ·  view source on GitHub ↗

Produce the text of one output line. Args: left: (float) left most coordinate used on page slot: (float) avg width of one character in any font in use. minslot: (float) min width for the characters in this line. chars: (list[tuple]) characters

(left, slot, minslot, lchars)

Source from the content-addressed store, hash-verified

685
686 # --------------------------------------------------------------------
687 def make_textline(left, slot, minslot, lchars):
688 """Produce the text of one output line.
689
690 Args:
691 left: (float) left most coordinate used on page
692 slot: (float) avg width of one character in any font in use.
693 minslot: (float) min width for the characters in this line.
694 chars: (list[tuple]) characters of this line.
695 Returns:
696 text: (str) text string for this line
697 """
698 text = "" # we output this
699 old_char = ""
700 old_x1 = 0 # end coordinate of last char
701 old_ox = 0 # x-origin of last char
702 if minslot <= pymupdf.EPSILON:
703 raise RuntimeError(f"program error: minslot too small = {minslot:g}")
704
705 for c in lchars: # loop over characters
706 char, ox, _, cwidth = c
707 ox = ox - left # its (relative) start coordinate
708 x1 = ox + cwidth # ending coordinate
709
710 # eliminate overprint effect
711 if old_char == char and ox - old_ox <= cwidth * 0.2:
712 continue
713
714 # omit spaces overlapping previous char
715 if char == " " and (old_x1 - ox) / cwidth > 0.8:
716 continue
717
718 old_char = char
719 # close enough to previous?
720 if ox < old_x1 + minslot: # assume char adjacent to previous
721 text += char # append to output
722 old_x1 = x1 # new end coord
723 old_ox = ox # new origin.x
724 continue
725
726 # else next char starts after some gap:
727 # fill in right number of spaces, so char is positioned
728 # in the right slot of the line
729 if char == " ": # rest relevant for non-space only
730 continue
731 delta = int(ox / slot) - len(text)
732 if ox > old_x1 and delta > 1:
733 text += " " * delta
734 # now append char
735 text += char
736 old_x1 = x1 # new end coordinate
737 old_ox = ox # new origin
738 return text.rstrip()
739
740 # extract page text by single characters ("rawdict")
741 blocks = page.get_text("rawdict", flags=flags)["blocks"]

Callers 1

page_layoutFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…