diff --git a/tests/test_app.py b/tests/test_app.py index 61fb083..f58fbfa 100644 --- a/tests/test_app.py +++ b/tests/test_app.py @@ -97,6 +97,10 @@ async def test_redaction_analyze(pdf_bytes: bytes): assert data["gap"]["w"] > 0 assert data["font"]["name"] assert data["font"]["size"] > 0 + assert "offset_x" in data + assert "offset_y" in data + assert isinstance(data["offset_x"], (int, float)) + assert isinstance(data["offset_y"], (int, float)) @pytest.mark.anyio diff --git a/unredact/app.py b/unredact/app.py index 1b1bbf9..68b750b 100644 --- a/unredact/app.py +++ b/unredact/app.py @@ -184,6 +184,15 @@ async def analyze_redaction(req: AnalyzeRequest): left_text = "".join(c.text for c in left_chars).rstrip() right_text = "".join(c.text for c in right_chars).lstrip() + # Compute initial offset guess: align end of left text with gap start + pil_font = font_match.to_pil_font() + if left_text: + left_rendered_width = pil_font.getlength(left_text) + offset_x = float(rx - left_rendered_width - best_line.x) + else: + offset_x = 0.0 + offset_y = 0.0 + if left_text: lx = left_chars[0].x lw = (left_chars[-1].x + left_chars[-1].w) - lx @@ -216,6 +225,8 @@ async def analyze_redaction(req: AnalyzeRequest): "text": best_line.text, }, "chars": chars_json, + "offset_x": round(offset_x, 1), + "offset_y": round(offset_y, 1), }