Skip to content

Commit 8add3fd

Browse files
committed
Add support for themes service data
1 parent 11fe303 commit 8add3fd

8 files changed

Lines changed: 901 additions & 93 deletions

File tree

src/DiagramGenerator/Board.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use DiagramGenerator\Config;
66
use DiagramGenerator\Fen;
77
use DiagramGenerator\Image\Storage;
8+
use DiagramGenerator\Image\StorageLegacy;
89
use DiagramGenerator\Image\Image;
910

1011
/**
@@ -76,7 +77,11 @@ public function getImage()
7677
*/
7778
protected function generateImage()
7879
{
79-
$storage = new Storage($this->cacheDir, $this->pieceThemeUrl, $this->boardTextureUrl);
80+
if ($this->config->hasThemeUrls()) {
81+
$storage = new Storage($this->cacheDir, $this->pieceThemeUrl, $this->boardTextureUrl);
82+
} else {
83+
$storage = new StorageLegacy($this->cacheDir, $this->pieceThemeUrl, $this->boardTextureUrl);
84+
}
8085
$image = new Image($storage, $this->config);
8186
$topPadding = $storage->getMaxPieceHeight($this->fen, $this->config) - $this->config->getSize()->getCell();
8287

src/DiagramGenerator/Config.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,15 @@ class Config
136136
#[Range(min: 0, max: 100)]
137137
protected $compressionQualityJpg;
138138

139+
/**
140+
* Custom theme URLs object with board and piece URLs.
141+
* Structure: { board: boardUrl, wp: pieceUrl, bp: pieceUrl, ... }
142+
*
143+
* @var array|null
144+
*/
145+
#[Type('array')]
146+
protected $themeUrls;
147+
139148
/**
140149
* Gets the value of fen.
141150
*
@@ -454,6 +463,41 @@ public function setCompressionQualityJpg($compressionQualityJpg)
454463
return $this;
455464
}
456465

466+
/**
467+
* Gets the theme URLs object.
468+
*
469+
* @return array|null
470+
*/
471+
public function getThemeUrls()
472+
{
473+
return $this->themeUrls;
474+
}
475+
476+
/**
477+
* Sets the theme URLs object.
478+
* Structure: { board: boardUrl, wp: pieceUrl, bp: pieceUrl, ... }
479+
*
480+
* @param array|null $themeUrls
481+
*
482+
* @return self
483+
*/
484+
public function setThemeUrls($themeUrls)
485+
{
486+
$this->themeUrls = $themeUrls;
487+
488+
return $this;
489+
}
490+
491+
/**
492+
* Checks if custom theme URLs are configured.
493+
*
494+
* @return bool
495+
*/
496+
public function hasThemeUrls()
497+
{
498+
return !empty($this->themeUrls) && is_array($this->themeUrls);
499+
}
500+
457501
public function getBorderThickness()
458502
{
459503
return (int) round($this->getSize()->getCell() / 2);

src/DiagramGenerator/Image/Image.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,17 @@ class Image
1818
/** @var BaseImage */
1919
protected $image;
2020

21-
/** @var Storage */
21+
/** @var Storage|StorageLegacy */
2222
protected $storage;
2323

2424
/** @var Config */
2525
protected $config;
2626

27-
public function __construct(Storage $storage, Config $config)
27+
/**
28+
* @param Storage|StorageLegacy $storage
29+
* @param Config $config
30+
*/
31+
public function __construct($storage, Config $config)
2832
{
2933
$this->image = (new Decoder())->initFromGdResource(imagecreatetruecolor(1, 1));
3034
$this->storage = $storage;
@@ -133,13 +137,16 @@ public function drawBoardWithFigures(Fen $fen, $cellSize, $topPaddingOfCell)
133137
{
134138
$this->image = $this->drawBoard($this->storage->getBackgroundTextureImage($this->config), $cellSize, $topPaddingOfCell);
135139

140+
$boardHasTexture = !empty($this->config->getTexture()) ||
141+
($this->config->hasThemeUrls() && isset($this->config->getThemeUrls()['board']));
142+
136143
$this->drawCells(
137144
$this->config->getSize()->getCell(),
138145
$this->config->getDark(),
139146
$this->config->getLight(),
140147
$this->config->getHighlightSquaresColor(),
141148
$topPaddingOfCell,
142-
!empty($this->config->getTexture()),
149+
$boardHasTexture,
143150
$this->config->getHighlightSquares()
144151
);
145152

0 commit comments

Comments
 (0)