Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,20 @@ echo $output->render($chess);

See [dedicated documentation](docs/output_imagine.md) for detailed instructions.

### SVG Image

Pieces are displayed inside an SVG image.

```php
<?php
// use...
$chess = new Chess();
$output = new SvgOutput();
echo $output->render($chess);
```

<img src="https://private-user-images.githubusercontent.com/179866/457647250-34bae1e1-83c5-47b1-a997-0b5bf4041d2a.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3NTA1OTU5NTgsIm5iZiI6MTc1MDU5NTY1OCwicGF0aCI6Ii8xNzk4NjYvNDU3NjQ3MjUwLTM0YmFlMWUxLTgzYzUtNDdiMS1hOTk3LTBiNWJmNDA0MWQyYS5wbmc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjUwNjIyJTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI1MDYyMlQxMjM0MThaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT00ZTU3MjY5NzRlYzA3ZjNlZWQwOGNkYWNmNjVlNTA1OWY2MzNjMDg0Y2U1MjAwODNkZWIxZThhNzEyNTljMWM5JlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCJ9.J5khvbyPEh86N9no_JVcuWumIj-gVHEOrDjIvc-wBfw">

### HTML

Pieces are displayed inside an HTML table.
Expand Down
67 changes: 67 additions & 0 deletions src/Output/SvgOutput.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

declare(strict_types=1);

namespace PChess\Chess\Output;

use PChess\Chess\Board;
use PChess\Chess\Chess;

/**
* The SVG template was adapted from https://github.com/michael-i-f-george/FEN2SVG
* which is licensed under the GPL 3.0.
*/
final class SvgOutput implements OutputInterface
{
private int $boardSize;

private bool $coords;

private string $darkSquareColor;

private string $liteSquareColor;

public function __construct(
int $size = 626,
bool $coords = true,
string $darkSquareColor = 'LightSteelBlue',
string $liteSquareColor = 'Gainsboro'
) {
$this->boardSize = $size;
$this->coords = $coords;
$this->darkSquareColor = $darkSquareColor;
$this->liteSquareColor = $liteSquareColor;
}

public function render(Chess $chess): string
{
$reversed = $chess->board->isReversed();
$output = \file_get_contents(__DIR__.'/template.svg');
$pieces = '';
foreach ($chess->board as $i => $piece) {
if (null === $piece) {
continue;
}
if ($reversed) {
$x = ((7 - Board::file($i)) * 72) + 49;
$y = ((7 - Board::rank($i)) * 72) + 1;
} else {
$x = (Board::file($i) * 72) + 49;
$y = (Board::rank($i) * 72) + 1;
}
$pieces .= \sprintf('<use xlink:href="#%s_%s" x="%d" y="%d"/>', $piece->getColor(), $piece->getType(), $x, $y).PHP_EOL;
}

$coords = '';
if ($this->coords) {
$file = $reversed ? '/coordinates_reversed.svg' : '/coordinates.svg';
$coords = \file_get_contents(__DIR__.$file);
}

return \str_replace(
['{{ size }}', '{{ dark }}', '{{ light }}', '{{ coordinates }}', '{{ pieces }}'],
[$this->boardSize, $this->darkSquareColor, $this->liteSquareColor, $coords, $pieces],
$output
);
}
}
16 changes: 16 additions & 0 deletions src/Output/coordinates.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 16 additions & 0 deletions src/Output/coordinates_reversed.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading