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

Function recover_span_quad

src_classic/utils.py:5089–5131  ·  view source on GitHub ↗

Calculate the span quad for 'dict' / 'rawdict' text extractions. Notes: There are two execution paths: 1. For the full span quad, the result of 'recover_quad' is returned. 2. For the quad of a sub-list of characters, the char quads are computed and joined. Thi

(line_dir: tuple, span: dict, chars: list = None)

Source from the content-addressed store, hash-verified

5087
5088
5089def recover_span_quad(line_dir: tuple, span: dict, chars: list = None) -> Quad:
5090 """Calculate the span quad for 'dict' / 'rawdict' text extractions.
5091
5092 Notes:
5093 There are two execution paths:
5094 1. For the full span quad, the result of 'recover_quad' is returned.
5095 2. For the quad of a sub-list of characters, the char quads are
5096 computed and joined. This is only supported for the "rawdict"
5097 extraction option.
5098
5099 Args:
5100 line_dir: (tuple) 'line["dir"]' of the owning line.
5101 span: (dict) the span.
5102 chars: (list, optional) sub-list of characters to consider.
5103 Returns:
5104 Quad covering selected characters.
5105 """
5106 if line_dir == None: # must be a span from get_texttrace()
5107 line_dir = span["dir"]
5108 if chars == None: # no sub-selection
5109 return recover_quad(line_dir, span)
5110 if not "chars" in span.keys():
5111 raise ValueError("need 'rawdict' option to sub-select chars")
5112
5113 q0 = recover_char_quad(line_dir, span, chars[0]) # quad of first char
5114 if len(chars) > 1: # get quad of last char
5115 q1 = recover_char_quad(line_dir, span, chars[-1])
5116 else:
5117 q1 = q0 # last = first
5118
5119 span_ll = q0.ll # lower-left of span quad
5120 span_lr = q1.lr # lower-right of span quad
5121 mat0 = planish_line(span_ll, span_lr)
5122 # map base line to x-axis such that span_ll goes to (0, 0)
5123 x_lr = span_lr * mat0
5124
5125 small = TOOLS.set_small_glyph_heights() # small glyph heights?
5126 h = span["size"] * (1 if small else (span["ascender"] - span["descender"]))
5127
5128 span_rect = Rect(0, -h, x_lr.x, 0) # line rectangle
5129 span_quad = span_rect.quad # make it a quad and:
5130 span_quad *= ~mat0 # rotate back and shift back
5131 return span_quad
5132
5133
5134def recover_char_quad(line_dir: tuple, span: dict, char: dict) -> Quad:

Callers

nothing calls this directly

Calls 5

planish_lineFunction · 0.85
RectClass · 0.85
recover_quadFunction · 0.70
recover_char_quadFunction · 0.70

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…