Skip to content

Commit

Permalink
Close Font::computeCharacterWidth method
Browse files Browse the repository at this point in the history
  • Loading branch information
antonkomarev committed Dec 11, 2023
1 parent 8e29dae commit df79793
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 45 deletions.
32 changes: 16 additions & 16 deletions src/Font.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,6 @@ public function __construct(
}
}

public function computeCharacterWidth(
Character $character,
int $size,
float $letterSpacing = 0.0,
): float {
$size = $size / $this->fontFace->unitsPerEm;

$glyphHorizontalAdvance = $this->getGlyphHorizontalAdvance($character);

$glyphWidth = $glyphHorizontalAdvance * $size;
$letterSpacingWidth = $this->fontFace->unitsPerEm * $letterSpacing * $size;

return $glyphWidth + $letterSpacingWidth;
}

public function computeStringWidth(
UnicodeString $string,
int $size,
Expand All @@ -65,7 +50,22 @@ public function computeStringWidth(
return max($maxLineWidth, $lineWidth);
}

private function getGlyphHorizontalAdvance(
private function computeCharacterWidth(
Character $character,
int $size,
float $letterSpacing = 0.0,
): float {
$size = $size / $this->fontFace->unitsPerEm;

$glyphHorizontalAdvance = $this->resolveGlyphHorizontalAdvance($character);

$glyphWidth = $glyphHorizontalAdvance * $size;
$letterSpacingWidth = $this->fontFace->unitsPerEm * $letterSpacing * $size;

return $glyphWidth + $letterSpacingWidth;
}

private function resolveGlyphHorizontalAdvance(
Character $character,
): int {
return isset($this->glyphs[strval($character)])
Expand Down
31 changes: 2 additions & 29 deletions test/Unit/SvgFontTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,11 @@

use Cog\SvgFont\Font;
use Cog\SvgFont\FontList;
use Cog\Unicode\Character;
use Cog\Unicode\UnicodeString;
use PHPUnit\Framework\TestCase;

final class SvgFontTest extends TestCase
{
/** @dataProvider provideItCanComputeCharacterWidth */
public function testItCanComputeCharacterWidth(
float $expectedWidth,
string $characters,
int $fontSize,
float $letterSpacing,
): void {
$font = $this->getFontById('Bagnard');

$this->assertSame(
$expectedWidth,
$font->computeCharacterWidth(
Character::of($characters),
$fontSize,
$letterSpacing,
),
);
}

/** @dataProvider provideItCanComputeStringWidth */
public function testItCanComputeStringWidth(
float $expectedWidth,
Expand All @@ -50,22 +30,15 @@ public function testItCanComputeStringWidth(
);
}

public static function provideItCanComputeCharacterWidth(): array
public static function provideItCanComputeStringWidth(): array
{
return [
[0, '@', 0, 0.0],
[0, 'Zero-width', 0, 0.0],
[8.192, 'a', 16, 0.0],
[9.712, 'b', 16, 0.0],
[9.44, '4', 16, 0.0],
[4.816, '.', 16, 0.0],
[4.816, ',', 16, 0.0],
];
}

public static function provideItCanComputeStringWidth(): array
{
return [
[0, 'Zero-width', 0, 0.0],
[41.072, 'Hello', 16, 0.0],
[43.344, 'world', 16, 0.0],
[82.144, 'Hello', 32, 0.0],
Expand Down

0 comments on commit df79793

Please sign in to comment.