diff --git a/src/openparse/schemas.py b/src/openparse/schemas.py index 0476f87..73f8660 100644 --- a/src/openparse/schemas.py +++ b/src/openparse/schemas.py @@ -590,9 +590,12 @@ def reading_order(self) -> ReadingOrder: if self.coordinate_system == "bottom-left": y_position = -min(element.bbox.y0 for element in self.elements) + # Add support for top-left coordinate system for sorting + elif self.coordinate_system == "top-left": + y_position = min(element.bbox.y0 for element in self.elements) else: raise NotImplementedError( - "Only 'bottom-left' coordinate system is supported." + "Only 'top-left' and 'bottom-left' coordinate system is supported." ) return ReadingOrder(min_page=min_page, y_position=y_position, min_x0=min_x0) diff --git a/src/openparse/tables/parse.py b/src/openparse/tables/parse.py index 54ffbbb..e491073 100644 --- a/src/openparse/tables/parse.py +++ b/src/openparse/tables/parse.py @@ -68,18 +68,19 @@ def _ingest_with_pymupdf( if verbose: print(f"Page {page_num} - Table {i + 1}:\n{text}\n") - # Flip y-coordinates to match the top-left origin system bbox = pymupdf.combine_header_and_table_bboxes(tab.bbox, tab.header.bbox) - fy0 = page.rect.height - bbox[3] - fy1 = page.rect.height - bbox[1] + # No need for flipping coordinates, pymupdf already returns coordinates in top-left origin system and bottom-left is handled while sorting + # # Flip y-coordinates to match the top-left origin system + # fy0 = page.rect.height - bbox[3] + # fy1 = page.rect.height - bbox[1] table = TableElement( bbox=Bbox( page=page_num, x0=bbox[0], - y0=fy0, + y0=bbox[1], x1=bbox[2], - y1=fy1, + y1=bbox[3], page_width=page.rect.width, page_height=page.rect.height, ), @@ -131,18 +132,19 @@ def _ingest_with_table_transformers( elif args.table_output_format == "html": table_text = table.to_html_str() - # Flip y-coordinates to match the top-left origin system - # FIXME: incorporate padding into bbox - fy0 = page.rect.height - table_bbox.bbox[3] - fy1 = page.rect.height - table_bbox.bbox[1] + # No need for flipping coordinates, pymupdf already returns coordinates in top-left origin system and bottom-left is handled while sorting + # # Flip y-coordinates to match the top-left origin system + # # FIXME: incorporate padding into bbox + # fy0 = page.rect.height - table_bbox.bbox[3] + # fy1 = page.rect.height - table_bbox.bbox[1] table_elem = TableElement( bbox=Bbox( page=page_num, x0=table_bbox.bbox[0], - y0=fy0, + y0=table_bbox.bbox[1], x1=table_bbox.bbox[2], - y1=fy1, + y1=table_bbox.bbox[3], page_width=page.rect.width, page_height=page.rect.height, ), @@ -193,18 +195,19 @@ def _ingest_with_unitable( table_img = crop_img_with_padding(pdf_as_imgs[page_num], padded_bbox) table_str = table_img_to_html(table_img) - - # Flip y-coordinates to match the top-left origin system - fy0 = page.rect.height - padded_bbox[3] - fy1 = page.rect.height - padded_bbox[1] + + # No need for flipping coordinates, pymupdf already returns coordinates in top-left origin system and bottom-left is handled while sorting + # # Flip y-coordinates to match the top-left origin system + # fy0 = page.rect.height - padded_bbox[3] + # fy1 = page.rect.height - padded_bbox[1] table_elem = TableElement( bbox=Bbox( page=page_num, x0=padded_bbox[0], - y0=fy0, + y0=padded_bbox[1], x1=padded_bbox[2], - y1=fy1, + y1=padded_bbox[3], page_width=page.rect.width, page_height=page.rect.height, ), diff --git a/src/openparse/text/pymupdf/core.py b/src/openparse/text/pymupdf/core.py index 74abc0c..08c9379 100644 --- a/src/openparse/text/pymupdf/core.py +++ b/src/openparse/text/pymupdf/core.py @@ -80,17 +80,18 @@ def ingest( lines = _lines_from_ocr_output(node["lines"]) - # Flip y-coordinates to match the top-left origin system - fy0 = page.rect.height - node["bbox"][3] - fy1 = page.rect.height - node["bbox"][1] + # No need for flipping coordinates, pymupdf already returns coordinates in top-left origin system and bottom-left is handled while sorting + # # Flip y-coordinates to match the top-left origin system + # fy0 = page.rect.height - node["bbox"][3] + # fy1 = page.rect.height - node["bbox"][1] elements.append( TextElement( bbox=Bbox( x0=node["bbox"][0], - y0=fy0, + y0=node["bbox"][1], x1=node["bbox"][2], - y1=fy1, + y1=node["bbox"][3], page=page_num, page_width=page.rect.width, page_height=page.rect.height,