Return rectangles of text lines between two points. Notes: The default of 'start' is top-left of 'clip'. The default of 'stop' is bottom-reight of 'clip'. Args: start: start point_like stop: end point_like, must be 'below' start clip: consider this r
(page, start: point_like =None, stop: point_like =None, clip: rect_like =None)
| 23803 | |
| 23804 | |
| 23805 | def get_highlight_selection(page, start: point_like =None, stop: point_like =None, clip: rect_like =None) -> list: |
| 23806 | """Return rectangles of text lines between two points. |
| 23807 | |
| 23808 | Notes: |
| 23809 | The default of 'start' is top-left of 'clip'. The default of 'stop' |
| 23810 | is bottom-reight of 'clip'. |
| 23811 | |
| 23812 | Args: |
| 23813 | start: start point_like |
| 23814 | stop: end point_like, must be 'below' start |
| 23815 | clip: consider this rect_like only, default is page rectangle |
| 23816 | Returns: |
| 23817 | List of line bbox intersections with the area established by the |
| 23818 | parameters. |
| 23819 | """ |
| 23820 | # validate and normalize arguments |
| 23821 | if clip is None: |
| 23822 | clip = page.rect |
| 23823 | clip = Rect(clip) |
| 23824 | if start is None: |
| 23825 | start = clip.tl |
| 23826 | if stop is None: |
| 23827 | stop = clip.br |
| 23828 | clip.y0 = start.y |
| 23829 | clip.y1 = stop.y |
| 23830 | if clip.is_empty or clip.is_infinite: |
| 23831 | return [] |
| 23832 | |
| 23833 | # extract text of page, clip only, no images, expand ligatures |
| 23834 | blocks = page.get_text( |
| 23835 | "dict", flags=0, clip=clip, |
| 23836 | )["blocks"] |
| 23837 | |
| 23838 | lines = [] # will return this list of rectangles |
| 23839 | for b in blocks: |
| 23840 | bbox = Rect(b["bbox"]) |
| 23841 | if bbox.is_infinite or bbox.is_empty: |
| 23842 | continue |
| 23843 | for line in b["lines"]: |
| 23844 | bbox = Rect(line["bbox"]) |
| 23845 | if bbox.is_infinite or bbox.is_empty: |
| 23846 | continue |
| 23847 | lines.append(bbox) |
| 23848 | |
| 23849 | if lines == []: # did not select anything |
| 23850 | return lines |
| 23851 | |
| 23852 | lines.sort(key=lambda bbox: bbox.y1) # sort by vertical positions |
| 23853 | |
| 23854 | # cut off prefix from first line if start point is close to its top |
| 23855 | bboxf = lines.pop(0) |
| 23856 | if bboxf.y0 - start.y <= 0.1 * bboxf.height: # close enough? |
| 23857 | r = Rect(start.x, bboxf.y0, bboxf.br) # intersection rectangle |
| 23858 | if not (r.is_empty or r.is_infinite): |
| 23859 | lines.insert(0, r) # insert again if not empty |
| 23860 | else: |
| 23861 | lines.insert(0, bboxf) # insert again |
| 23862 |
no test coverage detected
searching dependent graphs…