Skip to content

Commit 7fa1f18

Browse files
author
Lukas Malte Monnerjahn
committed
SVG: Align pieces, check mark, selected squares and arrows correctly when rendering a border #1103
1 parent 7299216 commit 7fa1f18

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

chess/svg.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,7 @@ def board(board: Optional[chess.BaseBoard] = None, *,
283283
inner_border = 1 if borders and coordinates else 0
284284
outer_border = 1 if borders else 0
285285
margin = 15 if coordinates else 0
286+
board_offset = inner_border + margin + outer_border
286287
full_size = 2 * outer_border + 2 * margin + 2 * inner_border + 8 * SQUARE_SIZE
287288
svg = _svg(full_size, size)
288289

@@ -351,12 +352,12 @@ def board(board: Optional[chess.BaseBoard] = None, *,
351352
if coordinates:
352353
coord_color, coord_opacity = _select_color(colors, "coord")
353354
for file_index, file_name in enumerate(chess.FILE_NAMES):
354-
x = (file_index if orientation else 7 - file_index) * SQUARE_SIZE + inner_border + margin + outer_border
355+
x = (file_index if orientation else 7 - file_index) * SQUARE_SIZE + board_offset
355356
# Keep some padding here to separate the ascender from the border
356357
svg.append(_coord(file_name, x, 1, SQUARE_SIZE, margin, True, margin, color=coord_color, opacity=coord_opacity))
357358
svg.append(_coord(file_name, x, full_size - outer_border - margin, SQUARE_SIZE, margin, True, margin, color=coord_color, opacity=coord_opacity))
358359
for rank_index, rank_name in enumerate(chess.RANK_NAMES):
359-
y = (7 - rank_index if orientation else rank_index) * SQUARE_SIZE + inner_border + margin + outer_border
360+
y = (7 - rank_index if orientation else rank_index) * SQUARE_SIZE + board_offset
360361
svg.append(_coord(rank_name, 0, y, margin, SQUARE_SIZE, False, margin, color=coord_color, opacity=coord_opacity))
361362
svg.append(_coord(rank_name, full_size - outer_border - margin, y, margin, SQUARE_SIZE, False, margin, color=coord_color, opacity=coord_opacity))
362363

@@ -365,8 +366,8 @@ def board(board: Optional[chess.BaseBoard] = None, *,
365366
file_index = chess.square_file(square)
366367
rank_index = chess.square_rank(square)
367368

368-
x = (file_index if orientation else 7 - file_index) * SQUARE_SIZE + inner_border + margin + outer_border
369-
y = (7 - rank_index if orientation else rank_index) * SQUARE_SIZE + inner_border + margin + outer_border
369+
x = (file_index if orientation else 7 - file_index) * SQUARE_SIZE + board_offset
370+
y = (7 - rank_index if orientation else rank_index) * SQUARE_SIZE + board_offset
370371

371372
cls = ["square", "light" if chess.BB_LIGHT_SQUARES & bb else "dark"]
372373
if lastmove and square in [lastmove.from_square, lastmove.to_square]:
@@ -406,8 +407,8 @@ def board(board: Optional[chess.BaseBoard] = None, *,
406407
file_index = chess.square_file(check)
407408
rank_index = chess.square_rank(check)
408409

409-
x = (file_index if orientation else 7 - file_index) * SQUARE_SIZE + margin
410-
y = (7 - rank_index if orientation else rank_index) * SQUARE_SIZE + margin
410+
x = (file_index if orientation else 7 - file_index) * SQUARE_SIZE + board_offset
411+
y = (7 - rank_index if orientation else rank_index) * SQUARE_SIZE + board_offset
411412

412413
ET.SubElement(svg, "rect", _attrs({
413414
"x": x,
@@ -423,8 +424,8 @@ def board(board: Optional[chess.BaseBoard] = None, *,
423424
file_index = chess.square_file(square)
424425
rank_index = chess.square_rank(square)
425426

426-
x = (file_index if orientation else 7 - file_index) * SQUARE_SIZE + margin
427-
y = (7 - rank_index if orientation else rank_index) * SQUARE_SIZE + margin
427+
x = (file_index if orientation else 7 - file_index) * SQUARE_SIZE + board_offset
428+
y = (7 - rank_index if orientation else rank_index) * SQUARE_SIZE + board_offset
428429

429430
if board is not None:
430431
piece = board.piece_at(square)
@@ -463,10 +464,10 @@ def board(board: Optional[chess.BaseBoard] = None, *,
463464
head_file = chess.square_file(head)
464465
head_rank = chess.square_rank(head)
465466

466-
xtail = outer_border + margin + inner_border + (tail_file + 0.5 if orientation else 7.5 - tail_file) * SQUARE_SIZE
467-
ytail = outer_border + margin + inner_border + (7.5 - tail_rank if orientation else tail_rank + 0.5) * SQUARE_SIZE
468-
xhead = outer_border + margin + inner_border + (head_file + 0.5 if orientation else 7.5 - head_file) * SQUARE_SIZE
469-
yhead = outer_border + margin + inner_border + (7.5 - head_rank if orientation else head_rank + 0.5) * SQUARE_SIZE
467+
xtail = board_offset + (tail_file + 0.5 if orientation else 7.5 - tail_file) * SQUARE_SIZE
468+
ytail = board_offset + (7.5 - tail_rank if orientation else tail_rank + 0.5) * SQUARE_SIZE
469+
xhead = board_offset + (head_file + 0.5 if orientation else 7.5 - head_file) * SQUARE_SIZE
470+
yhead = board_offset + (7.5 - head_rank if orientation else head_rank + 0.5) * SQUARE_SIZE
470471

471472
if (head_file, head_rank) == (tail_file, tail_rank):
472473
ET.SubElement(svg, "circle", _attrs({

0 commit comments

Comments
 (0)