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

Function line_text

src/utils.py:196–223  ·  view source on GitHub ↗

Create the string of one text line. We are trying to simulate some horizontal layout here, too. Args: clip: (pymupdf.Rect) the area from which all text is being read. line: (list) word tuples (rect, text) contained in the line Returns: Te

(clip, line)

Source from the content-addressed store, hash-verified

194 """
195
196 def line_text(clip, line):
197 """Create the string of one text line.
198
199 We are trying to simulate some horizontal layout here, too.
200
201 Args:
202 clip: (pymupdf.Rect) the area from which all text is being read.
203 line: (list) word tuples (rect, text) contained in the line
204 Returns:
205 Text in this line. Generated from words in 'line'. Distance from
206 predecessor is translated to multiple spaces, thus simulating
207 text indentations and large horizontal distances.
208 """
209 line.sort(key=lambda w: w[0].x0)
210 ltext = "" # text in the line
211 x1 = clip.x0 # end coordinate of ltext
212 lrect = pymupdf.EMPTY_RECT() # bbox of this line
213 for r, t in line:
214 lrect |= r # update line bbox
215 # convert distance to previous word to multiple spaces
216 dist = max(
217 int(round((r.x0 - x1) / r.width * len(t))),
218 0 if (x1 == clip.x0 or r.x0 <= x1) else 1,
219 ) # number of space characters
220
221 ltext += " " * dist + t # append word string
222 x1 = r.x1 # update new end position
223 return ltext
224
225 # Extract words in correct sequence first.
226 words = [

Callers 1

get_sorted_textFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…