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

Function recover_line_quad

src_classic/utils.py:5044–5086  ·  view source on GitHub ↗

Calculate the line quad for 'dict' / 'rawdict' text extractions. The lower quad points are those of the first, resp. last span quad. The upper points are determined by the maximum span quad height. From this, compute a rect with bottom-left in (0, 0), convert this to a quad and rota

(line: dict, spans: list = None)

Source from the content-addressed store, hash-verified

5042
5043
5044def recover_line_quad(line: dict, spans: list = None) -> Quad:
5045 """Calculate the line quad for 'dict' / 'rawdict' text extractions.
5046
5047 The lower quad points are those of the first, resp. last span quad.
5048 The upper points are determined by the maximum span quad height.
5049 From this, compute a rect with bottom-left in (0, 0), convert this to a
5050 quad and rotate and shift back to cover the text of the spans.
5051
5052 Args:
5053 spans: (list, optional) sub-list of spans to consider.
5054 Returns:
5055 Quad covering selected spans.
5056 """
5057 if spans == None: # no sub-selection
5058 spans = line["spans"] # all spans
5059 if len(spans) == 0:
5060 raise ValueError("bad span list")
5061 line_dir = line["dir"] # text direction
5062 cos, sin = line_dir
5063 q0 = recover_quad(line_dir, spans[0]) # quad of first span
5064 if len(spans) > 1: # get quad of last span
5065 q1 = recover_quad(line_dir, spans[-1])
5066 else:
5067 q1 = q0 # last = first
5068
5069 line_ll = q0.ll # lower-left of line quad
5070 line_lr = q1.lr # lower-right of line quad
5071
5072 mat0 = planish_line(line_ll, line_lr)
5073
5074 # map base line to x-axis such that line_ll goes to (0, 0)
5075 x_lr = line_lr * mat0
5076
5077 small = TOOLS.set_small_glyph_heights() # small glyph heights?
5078
5079 h = max(
5080 [s["size"] * (1 if small else (s["ascender"] - s["descender"])) for s in spans]
5081 )
5082
5083 line_rect = Rect(0, -h, x_lr.x, 0) # line rectangle
5084 line_quad = line_rect.quad # make it a quad and:
5085 line_quad *= ~mat0
5086 return line_quad
5087
5088
5089def recover_span_quad(line_dir: tuple, span: dict, chars: list = None) -> Quad:

Callers

nothing calls this directly

Calls 4

planish_lineFunction · 0.85
RectClass · 0.85
recover_quadFunction · 0.70

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…