Skip to content

feat: Fixed char ordering in text lines #138

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 17 commits into from
Jun 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .github/workflows/wheels.yml
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,11 @@ jobs:
shell: pwsh
run: |
New-Item -Path 'C:\nasm' -ItemType Directory -Force
Invoke-WebRequest -Uri 'https://fossies.org/windows/misc/nasm-2.16.03-win64.zip/nasm-2.16.03/nasm.exe' -OutFile 'C:\nasm\nasm.exe'
Invoke-WebRequest -Uri 'https://www.nasm.us/pub/nasm/releasebuilds/2.16.03/win64/nasm-2.16.03-win64.zip' -OutFile 'C:\nasm-2.16.03-win64.zip'
Expand-Archive -Path 'C:\nasm-2.16.03-win64.zip' -DestinationPath 'C:\nasm-temp' -Force
Copy-Item -Path 'C:\nasm-temp\nasm-2.16.03\nasm.exe' -Destination 'C:\nasm\nasm.exe'
Remove-Item -Path 'C:\nasm-2.16.03-win64.zip' -Force
Remove-Item -Path 'C:\nasm-temp' -Recurse -Force
nasm -v

- name: Build wheels
Expand Down
51 changes: 42 additions & 9 deletions docling_parse/pdf_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,12 @@ def _to_table_of_contents(self, toc: dict) -> List[PdfTableOfContents]:
return result

def get_page(
self, page_no: int, create_words: bool = True, create_textlines: bool = True
self,
page_no: int,
*,
create_words: bool = True,
create_textlines: bool = True,
enforce_same_font: bool = True,
) -> SegmentedPdfPage:
if page_no in self._pages.keys():
return self._pages[page_no]
Expand All @@ -134,6 +139,7 @@ def get_page(
page=page["original"],
create_words=create_words,
create_textlines=create_textlines,
enforce_same_font=enforce_same_font,
) # put on cache
return self._pages[page_no]

Expand Down Expand Up @@ -315,7 +321,12 @@ def _to_lines(self, data: dict) -> List[PdfLine]:
return result

def _to_segmented_page(
self, page: dict, create_words: bool, create_textlines: bool
self,
page: dict,
*,
create_words: bool,
create_textlines: bool,
enforce_same_font: bool = True,
) -> SegmentedPdfPage:

char_cells = self._to_cells(page["cells"])
Expand All @@ -330,14 +341,22 @@ def _to_segmented_page(
)

if create_words:
self._create_word_cells(segmented_page)
self._create_word_cells(segmented_page, enforce_same_font=enforce_same_font)

if create_textlines:
self._create_textline_cells(segmented_page)
self._create_textline_cells(
segmented_page, enforce_same_font=enforce_same_font
)

return segmented_page

def _create_word_cells(
self, segmented_page: SegmentedPdfPage, _loglevel: str = "fatal"
self,
segmented_page: SegmentedPdfPage,
*,
space_width_factor_for_merge: float = 0.33,
enforce_same_font: bool = True,
_loglevel: str = "fatal",
):

if len(segmented_page.word_cells) > 0:
Expand All @@ -355,7 +374,11 @@ def _create_word_cells(

sanitizer.set_char_cells(data=char_data)

data = sanitizer.create_word_cells(space_width_factor_for_merge=0.33)
# data = sanitizer.create_word_cells(space_width_factor_for_merge=0.33)
data = sanitizer.create_word_cells(
space_width_factor_for_merge=space_width_factor_for_merge,
enforce_same_font=enforce_same_font,
)

segmented_page.word_cells = []
for item in data:
Expand All @@ -365,9 +388,14 @@ def _create_word_cells(
segmented_page.has_words = len(segmented_page.word_cells) > 0

def _create_textline_cells(
self, segmented_page: SegmentedPdfPage, _loglevel: str = "fatal"
self,
segmented_page: SegmentedPdfPage,
*,
space_width_factor_for_merge: float = 1.0,
space_width_factor_for_merge_with_space: float = 0.33,
enforce_same_font: bool = True,
_loglevel: str = "fatal",
):

if len(segmented_page.textline_cells) > 0:
return

Expand All @@ -387,7 +415,12 @@ def _create_textline_cells(

sanitizer.set_char_cells(data=char_data)

data = sanitizer.create_line_cells()
# data = sanitizer.create_line_cells()
data = sanitizer.create_line_cells(
space_width_factor_for_merge=space_width_factor_for_merge,
space_width_factor_for_merge_with_space=space_width_factor_for_merge_with_space,
enforce_same_font=enforce_same_font,
)

segmented_page.textline_cells = []
for item in data:
Expand Down
19 changes: 18 additions & 1 deletion docling_parse/visualize.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@ def parse_args():
help="Enable interactive mode (default: False)",
)

# Add an optional boolean argument for enforcing same-font
parser.add_argument(
"--enforce-same-font",
action="store_true",
help="Enable interactive mode (default: False)",
)

# Add an argument for the output directory, defaulting to "./tmp"
parser.add_argument(
"-o",
Expand Down Expand Up @@ -115,18 +122,21 @@ def parse_args():
int(args.page),
args.display_text,
args.log_text,
args.enforce_same_font,
args.page_boundary,
args.category,
)


def visualise_py(
*,
log_level: str,
pdf_path: str,
interactive: str,
output_dir: Path,
display_text: bool,
log_text: bool,
enforce_same_font: bool,
page_boundary: str = "crop_box", # media_box
category: str = "char", # "both", "sanitized", "original"
page_num: int = -1,
Expand All @@ -142,7 +152,12 @@ def visualise_py(
for page_no in page_nos:
print(f"parsing {pdf_path} on page: {page_no}")

pdf_page: SegmentedPdfPage = pdf_doc.get_page(page_no=page_no)
pdf_page: SegmentedPdfPage = pdf_doc.get_page(
page_no=page_no,
create_words=True,
create_textlines=True,
enforce_same_font=enforce_same_font,
)

if os.path.exists(str(output_dir)):
pdf_page.save_as_json(
Expand Down Expand Up @@ -234,6 +249,7 @@ def main():
page_num,
display_text,
log_text,
enforce_same_font,
page_boundary,
category,
) = parse_args()
Expand All @@ -247,6 +263,7 @@ def main():
output_dir=output_dir,
display_text=display_text,
log_text=log_text,
enforce_same_font=enforce_same_font,
page_boundary=page_boundary,
category=category,
page_num=page_num,
Expand Down
4 changes: 2 additions & 2 deletions src/pybind/docling_sanitizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ namespace docling
// do a deep copy
word_cells = char_cells;

LOG_S(INFO) << "#-wordcells: " << word_cells.size();
LOG_S(INFO) << "#-word cells: " << word_cells.size();

// remove all spaces
auto itr = word_cells.begin();
Expand All @@ -265,7 +265,7 @@ namespace docling
}
}

LOG_S(INFO) << "#-wordcells: " << word_cells.size();
LOG_S(INFO) << "#-word cells: " << word_cells.size();

// > space_width_factor_for_merge, so nothing gets merged with a space
double space_width_factor_for_merge_with_space = 2.0*space_width_factor_for_merge;
Expand Down
1 change: 1 addition & 0 deletions src/v2/pdf_resources/page_cells.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ namespace pdflib
itr_type end() { return cells.end(); }

itr_type erase(itr_type itr) { return cells.erase(itr); }
itr_type erase(itr_type itr_0, itr_type itr_1) { return cells.erase(itr_0, itr_1); }

pdf_resource<PAGE_CELL>& at(std::size_t i) { return cells.at(i); }

Expand Down
2 changes: 1 addition & 1 deletion src/v2/pdf_resources/page_font.h
Original file line number Diff line number Diff line change
Expand Up @@ -1555,7 +1555,7 @@ namespace pdflib
if(cmap_initialized) // we found a `ToUnicode` before. No need to go deeper!
{
LOG_S(WARNING) << "We found a `ToUnicode` before. No need to go deeper!";
// return;
return;
}
//else

Expand Down
Loading
Loading