Skip to content

Commit

Permalink
feat: Add debug rectangle drawing for PDF characters
Browse files Browse the repository at this point in the history
- Implement _draw_chars_rectangle method in add_debug_information
- Draw red rectangles with thin line width around each character
- Collect characters from various page components (paragraphs, lines, formulas)
- Support debug visualization for character bounding boxes
  • Loading branch information
awwaawwa committed Feb 20, 2025
1 parent 75ee660 commit 85b1601
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
7 changes: 6 additions & 1 deletion babeldoc/document_il/backend/pdf_creater.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,12 @@ def _debug_render_rectangle(
draw_op.append(
rectangle.graphic_state.passthrough_per_char_instruction.encode(),
) # Green stroke
draw_op.append(b" 1 w ") # Line width
if rectangle.linewidth is not None:
draw_op.append(
f" {rectangle.linewidth} w ".encode(),
)
else:
draw_op.append(b" 1 w ") # Line width

# Draw four lines manually
# Bottom line
Expand Down
33 changes: 32 additions & 1 deletion babeldoc/document_il/midend/add_debug_information.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from babeldoc.document_il import GraphicState
from babeldoc.document_il.utils.style_helper import BLUE
from babeldoc.document_il.utils.style_helper import ORANGE
from babeldoc.document_il.utils.style_helper import RED
from babeldoc.document_il.utils.style_helper import YELLOW
from babeldoc.translation_config import TranslationConfig

Expand All @@ -24,11 +25,17 @@ def process(self, docs: il_version_1.Document):
for page in docs.page:
self.process_page(page)

def _create_rectangle(self, box: il_version_1.Box, color: GraphicState):
def _create_rectangle(
self,
box: il_version_1.Box,
color: GraphicState,
line_width: float = 1,
):
rect = il_version_1.PdfRectangle(
box=box,
graphic_state=color,
debug_info=True,
linewidth=line_width,
)
return rect

Expand Down Expand Up @@ -62,6 +69,7 @@ def _create_text(self, text: str, color: GraphicState, box: il_version_1.Box):
)

def process_page(self, page: il_version_1.Page):
self._draw_chars_rectangle(page)
# Add page number text at top-left corner
page_width = page.cropbox.box.x2 - page.cropbox.box.x
page_height = page.cropbox.box.y2 - page.cropbox.box.y
Expand Down Expand Up @@ -136,3 +144,26 @@ def process_page(self, page: il_version_1.Page):
)

page.pdf_paragraph.extend(new_paragraphs)

def _draw_chars_rectangle(self, page: il_version_1.Page):
chars = []
chars.extend(page.pdf_character)

for para in page.pdf_paragraph:
for comp in para.pdf_paragraph_composition:
if comp.pdf_line:
chars.extend(comp.pdf_line.pdf_character)
elif comp.pdf_same_style_characters:
chars.extend(comp.pdf_same_style_characters.pdf_character)
elif comp.pdf_formula:
chars.extend(comp.pdf_formula.pdf_character)
elif comp.pdf_character:
chars.append(comp.pdf_character)
elif comp.pdf_same_style_unicode_characters:
continue
else:
raise ValueError(f"Unknown composition type: {comp}")

for char in chars:
rect = self._create_rectangle(char.box, RED, line_width=0.1)
page.pdf_rectangle.append(rect)

0 comments on commit 85b1601

Please sign in to comment.