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

Function JM_search_stext_page

src/__init__.py:21377–21447  ·  view source on GitHub ↗
(page, needle)

Source from the content-addressed store, hash-verified

21375
21376
21377def JM_search_stext_page(page, needle):
21378 if 1 or g_use_extra:
21379 return extra.JM_search_stext_page(page.m_internal, needle)
21380
21381 rect = mupdf.FzRect(page.m_internal.mediabox)
21382 if not needle:
21383 return
21384 quads = []
21385 class Hits:
21386 def __str__(self):
21387 return f'Hits(len={self.len} quads={self.quads} hfuzz={self.hfuzz} vfuzz={self.vfuzz}'
21388 hits = Hits()
21389 hits.len = 0
21390 hits.quads = quads
21391 hits.hfuzz = 0.2 # merge kerns but not large gaps
21392 hits.vfuzz = 0.1
21393
21394 buffer_ = JM_new_buffer_from_stext_page(page)
21395 haystack_string = mupdf.fz_string_from_buffer(buffer_)
21396 haystack = 0
21397 begin, end = find_string(haystack_string[haystack:], needle)
21398 if begin is None:
21399 #goto no_more_matches;
21400 return quads
21401
21402 begin += haystack
21403 end += haystack
21404 inside = 0
21405 i = 0
21406 for block in page:
21407 if block.m_internal.type != mupdf.FZ_STEXT_BLOCK_TEXT:
21408 continue
21409 for line in block:
21410 for ch in line:
21411 i += 1
21412 if not mupdf.fz_is_infinite_rect(rect):
21413 r = JM_char_bbox(line, ch)
21414 if not JM_rects_overlap(rect, r):
21415 #goto next_char;
21416 continue
21417 while 1:
21418 #try_new_match:
21419 if not inside:
21420 if haystack >= begin:
21421 inside = 1
21422 if inside:
21423 if haystack < end:
21424 on_highlight_char(hits, line, ch)
21425 break
21426 else:
21427 inside = 0
21428 begin, end = find_string(haystack_string[haystack:], needle)
21429 if begin is None:
21430 #goto no_more_matches;
21431 return quads
21432 else:
21433 #goto try_new_match;
21434 begin += haystack

Callers 1

searchMethod · 0.85

Calls 6

HitsClass · 0.85
find_stringFunction · 0.85
JM_char_bboxFunction · 0.85
JM_rects_overlapFunction · 0.85
on_highlight_charFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…