diff --git a/core/pdf_exporter.py b/core/pdf_exporter.py index c1461c9..eeaa729 100644 --- a/core/pdf_exporter.py +++ b/core/pdf_exporter.py @@ -375,6 +375,8 @@ def export_wireframe_to_pdf( w_geom = max_x - min_x h_geom = max_y - min_y + center_x = (min_x + max_x) / 2.0 + center_y = (min_y + max_y) / 2.0 L = max(w_geom, h_geom) if L > 1e-6: @@ -387,10 +389,17 @@ def export_wireframe_to_pdf( minor_step = main_step / 10.0 if len(edges_to_draw) > 0 or len(edges_2d) > 0: - grid_min_x = math.floor(min_x / main_step) * main_step - grid_max_x = math.ceil(max_x / main_step) * main_step - grid_min_y = math.floor(min_y / main_step) * main_step - grid_max_y = math.ceil(max_y / main_step) * main_step + half_span_x = math.ceil(max(abs(max_x - center_x), abs(min_x - center_x)) / main_step) * main_step + half_span_y = math.ceil(max(abs(max_y - center_y), abs(min_y - center_y)) / main_step) * main_step + if half_span_x == 0: + half_span_x = main_step + if half_span_y == 0: + half_span_y = main_step + + grid_min_x = math.floor((center_x - half_span_x) / main_step) * main_step + grid_max_x = math.ceil((center_x + half_span_x) / main_step) * main_step + grid_min_y = math.floor((center_y - half_span_y) / main_step) * main_step + grid_max_y = math.ceil((center_y + half_span_y) / main_step) * main_step if grid_min_x == grid_max_x: grid_max_x += main_step @@ -403,8 +412,11 @@ def export_wireframe_to_pdf( w_grid_mm = grid_max_x - grid_min_x h_grid_mm = grid_max_y - grid_min_y - w_total_mm = w_grid_mm + 2.0 * margin_mm - h_total_mm = h_grid_mm + 2.0 * margin_mm + # Paper margin: user margin_mm + 2x smaller grid step in all directions + total_margin_mm = margin_mm + 2.0 * minor_step + + w_total_mm = w_grid_mm + 2.0 * total_margin_mm + h_total_mm = h_grid_mm + 2.0 * total_margin_mm page_w_pt = w_total_mm * MM_TO_PT page_h_pt = h_total_mm * MM_TO_PT @@ -412,8 +424,8 @@ def export_wireframe_to_pdf( line_width_pt = line_width_mm * MM_TO_PT def to_pdf_pt(pt_mm): - x_pt = (pt_mm[:, 0] - grid_min_x + margin_mm) * MM_TO_PT - y_pt = (pt_mm[:, 1] - grid_min_y + margin_mm) * MM_TO_PT + x_pt = (pt_mm[:, 0] - grid_min_x + total_margin_mm) * MM_TO_PT + y_pt = (pt_mm[:, 1] - grid_min_y + total_margin_mm) * MM_TO_PT return np.column_stack([x_pt, y_pt]) max_grid_steps = 2000 diff --git a/tests/test_pdf_exporter.py b/tests/test_pdf_exporter.py index 99192b8..9a96d17 100644 --- a/tests/test_pdf_exporter.py +++ b/tests/test_pdf_exporter.py @@ -143,8 +143,10 @@ class TestPdfExporter(unittest.TestCase): with open(pdf_path, "rb") as f: content = f.read() - expected_w = (100.0 + 2 * margin_mm) * MM_TO_PT - expected_h = (100.0 + 2 * margin_mm) * MM_TO_PT + minor_step = 1.0 # main_step 10.0 / 10 + total_margin_mm = margin_mm + 2.0 * minor_step + expected_w = (100.0 + 2 * total_margin_mm) * MM_TO_PT + expected_h = (100.0 + 2 * total_margin_mm) * MM_TO_PT mediabox_str = f"/MediaBox [0 0 {expected_w:.2f} {expected_h:.2f}]".encode("utf-8") self.assertTrue(content.startswith(b"%PDF-1.4"))