Skip to content

Commit 2181e24

Browse files
fix(render): resolve border corner crossing issues by using butt caps and adjusted tiling math
1 parent 8f2899b commit 2181e24

2 files changed

Lines changed: 22 additions & 7 deletions

File tree

example/coverage.pdf

148 Bytes
Binary file not shown.

src/pypdfpatra/render.py

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -252,11 +252,21 @@ def _draw_borders(
252252
border_box_h: float,
253253
) -> None:
254254
"""Paints element borders with correct styles: solid, dashed, dotted, double."""
255+
# Ensure lines don't cross each other at the corners (the "math crossing" fix)
256+
# Style 0 J = butt caps (ends exactly at coordinates)
257+
# Style 2 J = projecting square (default, extends half-width past)
258+
# Since set_line_cap_style isn't consistently available in all fpdf2 versions,
259+
# we use the raw PDF operator via _out.
260+
pdf._out("0 J")
261+
255262
def half(w):
256263
return w / 2.0
257264

258-
for edge, b_w, line_x1, line_y1, line_x2, line_y2 in [
259-
# Lines are centered on their coordinate, so offset by b_w/2 to hug the border-box edge
265+
# Draw order: Top, Bottom, Left, Right
266+
# To avoid "crossing", we adjust the start/end of vertical lines
267+
# so they sit perfectly between the top and bottom lines.
268+
edges = [
269+
# edge, b_w, x1, y1, x2, y2
260270
(
261271
"top",
262272
border_top,
@@ -277,19 +287,21 @@ def half(w):
277287
"left",
278288
border_left,
279289
border_box_x + half(border_left),
280-
border_box_y,
290+
border_box_y + border_top,
281291
border_box_x + half(border_left),
282-
border_box_y + border_box_h,
292+
border_box_y + border_box_h - border_bottom,
283293
),
284294
(
285295
"right",
286296
border_right,
287297
border_box_x + border_box_w - half(border_right),
288-
border_box_y,
298+
border_box_y + border_top,
289299
border_box_x + border_box_w - half(border_right),
290-
border_box_y + border_box_h,
300+
border_box_y + border_box_h - border_bottom,
291301
),
292-
]:
302+
]
303+
304+
for edge, b_w, line_x1, line_y1, line_x2, line_y2 in edges:
293305
border_style = style.get(
294306
f"border-{edge}-style", style.get("border-style", "solid")
295307
)
@@ -363,6 +375,9 @@ def half(w):
363375
pdf.set_line_width(0.2)
364376
pdf.set_draw_color(0, 0, 0)
365377

378+
# Restore default line cap (2 J = projecting square)
379+
pdf._out("2 J")
380+
366381

367382
def _draw_text(
368383
pdf: fpdf.FPDF,

0 commit comments

Comments
 (0)