-
-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
49bccd4
commit 4c77c81
Showing
3 changed files
with
34 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
||
|
@@ -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); | ||
} | ||
} |