Extract text as "rawdict" to fill CHARS.
(page, clip=None)
| 2179 | # Extract all page characters to fill the CHARS list |
| 2180 | # ----------------------------------------------------------------------------- |
| 2181 | def make_chars(page, clip=None): |
| 2182 | """Extract text as "rawdict" to fill CHARS.""" |
| 2183 | page_number = page.number + 1 |
| 2184 | page_height = page.rect.height |
| 2185 | ctm = page.transformation_matrix |
| 2186 | TEXTPAGE = page.get_textpage(clip=clip, flags=FLAGS) |
| 2187 | blocks = page.get_text("rawdict", textpage=TEXTPAGE)["blocks"] |
| 2188 | doctop_base = page_height * page.number |
| 2189 | for block in blocks: |
| 2190 | for line in block["lines"]: |
| 2191 | ldir = line["dir"] # = (cosine, sine) of angle |
| 2192 | ldir = (round(ldir[0], 4), round(ldir[1], 4)) |
| 2193 | matrix = pymupdf.Matrix(ldir[0], -ldir[1], ldir[1], ldir[0], 0, 0) |
| 2194 | if ldir[1] == 0: |
| 2195 | upright = True |
| 2196 | else: |
| 2197 | upright = False |
| 2198 | for span in sorted(line["spans"], key=lambda s: s["bbox"][0]): |
| 2199 | fontname = span["font"] |
| 2200 | fontsize = span["size"] |
| 2201 | span_bold = bool( |
| 2202 | span["flags"] & pymupdf.TEXT_FONT_BOLD or span["char_flags"] & 8 |
| 2203 | ) |
| 2204 | color = pymupdf.sRGB_to_pdf(span["color"]) |
| 2205 | for char in sorted(span["chars"], key=lambda c: c["bbox"][0]): |
| 2206 | bbox = pymupdf.Rect(char["bbox"]) |
| 2207 | bbox_ctm = bbox * ctm |
| 2208 | origin = pymupdf.Point(char["origin"]) * ctm |
| 2209 | matrix.e = origin.x |
| 2210 | matrix.f = origin.y |
| 2211 | text = char["c"] |
| 2212 | char_dict = { |
| 2213 | "adv": bbox.x1 - bbox.x0 if upright else bbox.y1 - bbox.y0, |
| 2214 | "bottom": bbox.y1, |
| 2215 | "doctop": bbox.y0 + doctop_base, |
| 2216 | "fontname": fontname, |
| 2217 | "height": bbox.y1 - bbox.y0, |
| 2218 | "matrix": tuple(matrix), |
| 2219 | "ncs": "DeviceRGB", |
| 2220 | "non_stroking_color": color, |
| 2221 | "non_stroking_pattern": None, |
| 2222 | "object_type": "char", |
| 2223 | "page_number": page_number, |
| 2224 | "size": fontsize if upright else bbox.y1 - bbox.y0, |
| 2225 | "stroking_color": color, |
| 2226 | "stroking_pattern": None, |
| 2227 | "bold": span_bold, |
| 2228 | "text": text, |
| 2229 | "top": bbox.y0, |
| 2230 | "upright": upright, |
| 2231 | "width": bbox.x1 - bbox.x0, |
| 2232 | "x0": bbox.x0, |
| 2233 | "x1": bbox.x1, |
| 2234 | "y0": bbox_ctm.y0, |
| 2235 | "y1": bbox_ctm.y1, |
| 2236 | } |
| 2237 | CHARS.append(char_dict) |
| 2238 | return TEXTPAGE |
no test coverage detected
searching dependent graphs…