Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
antonkomarev committed Oct 16, 2023
1 parent 49bccd4 commit 4c77c81
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 6 deletions.
15 changes: 15 additions & 0 deletions spec/PUGX/Poser/Calculator/GDTextSizeCalculatorSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace spec\PUGX\Poser\Calculator;

use PhpSpec\ObjectBehavior;

class GDTextSizeCalculatorSpec extends ObjectBehavior
{
public function it_should_compute_text_width(): void
{
$this->calculateWidth('MIT', 8)->shouldBeLike(24);
$this->calculateWidth('MIT', 10)->shouldBeLike(29);
$this->calculateWidth('MIT', 14)->shouldBeLike(34);
}
}
15 changes: 15 additions & 0 deletions spec/PUGX/Poser/Calculator/SvgTextSizeCalculatorSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace spec\PUGX\Poser\Calculator;

use PhpSpec\ObjectBehavior;

class SvgTextSizeCalculatorSpec extends ObjectBehavior
{
public function it_should_compute_text_width(): void
{
$this->calculateWidth('MIT', 8)->shouldBeLike(25);
$this->calculateWidth('MIT', 10)->shouldBeLike(29);
$this->calculateWidth('MIT', 14)->shouldBeLike(36);
}
}
10 changes: 4 additions & 6 deletions src/Calculator/SvgTextSizeCalculator.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,22 @@

namespace PUGX\Poser\Calculator;

use Cog\SvgFont\Parser\SimpleXmlSvgFontFileParser;
use Cog\SvgFont\FontList;
use Cog\Unicode\UnicodeString;

/**
* @author Anton Komarev <[email protected]>
*/
class SvgTextSizeCalculator implements TextSizeCalculatorInterface
{
private const SHIELD_PADDING = 12;
private const SHIELD_PADDING_X = 11;

/**
* Calculate the width of the text box.
*/
public function calculateWidth(string $text, int $size = self::TEXT_SIZE): float
{
$font = (new SimpleXmlSvgFontFileParser())
->parseFile(__DIR__ . '/DejaVuSans.svg')
->getById('DejaVuSansBook');
$font = FontList::ofFile(__DIR__ . '/Font/DejaVuSans.svg')->getById('DejaVuSansBook');

$letterSpacing = 0.0;

Expand All @@ -38,6 +36,6 @@ public function calculateWidth(string $text, int $size = self::TEXT_SIZE): float
$letterSpacing,
);

return \round($width + self::SHIELD_PADDING, 1);
return \round($width + self::SHIELD_PADDING_X, 0);
}
}

0 comments on commit 4c77c81

Please sign in to comment.