Skip to content

Commit

Permalink
Initial implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
antonkomarev committed Oct 16, 2023
1 parent 20a14af commit 914708c
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 154 deletions.
27 changes: 26 additions & 1 deletion src/Font.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
namespace Cog\SvgFont;

use Cog\Unicode\Character;
use Cog\Unicode\UnicodeString;

final class Font
{
private const UNICODE_CODE_POINT_LINE_FEED = 10;

private string $id;

private int $horizontalAdvance;
Expand Down Expand Up @@ -47,7 +50,7 @@ public function id(): string
return $this->id;
}

public function computeWidth(
public function computeCharacterWidth(
Character $character,
int $size,
float $letterSpacing = 0.0
Expand All @@ -62,6 +65,28 @@ public function computeWidth(
return $glyphWidth + $letterSpacingWidth;
}

public function computeStringWidth(
UnicodeString $string,
int $size,
float $letterSpacing = 0.0
): float {
$maxLineWidth = $lineWidth = 0;

$characterList = $string->characterList();

foreach ($characterList as $character) {
if ($character->toDecimal() === self::UNICODE_CODE_POINT_LINE_FEED) {
$maxLineWidth = max($maxLineWidth, $lineWidth);
$lineWidth = 0;
continue;
}

$lineWidth += $this->computeCharacterWidth($character, $size, $letterSpacing);
}

return max($maxLineWidth, $lineWidth);
}

private function getGlyphHorizontalAdvance(
Character $character
): int {
Expand Down
149 changes: 0 additions & 149 deletions src/Parser/XmlReaderSvgFontFileParser.php

This file was deleted.

39 changes: 35 additions & 4 deletions test/Unit/SvgFontTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@
use Cog\SvgFont\Font;
use Cog\SvgFont\Parser\SimpleXmlSvgFontFileParser;
use Cog\Unicode\Character;
use Cog\Unicode\UnicodeString;
use PHPUnit\Framework\TestCase;

final class SvgFontTest extends TestCase
{
/** @dataProvider provideItCanComputeWidth */
public function testItCanComputeTextWidth(
/** @dataProvider provideItCanComputeCharacterWidth */
public function testItCanComputeCharacterWidth(
float $expectedWidth,
string $characters,
int $fontSize,
Expand All @@ -22,15 +23,34 @@ public function testItCanComputeTextWidth(

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

public static function provideItCanComputeWidth(): array
/** @dataProvider provideItCanComputeStringWidth */
public function testItCanComputeStringWidth(
float $expectedWidth,
string $string,
int $fontSize,
float $letterSpacing
): void {
$font = $this->getFontById('Bagnard');

$this->assertSame(
$expectedWidth,
$font->computeStringWidth(
UnicodeString::of($string),
$fontSize,
$letterSpacing,
),
);
}

public static function provideItCanComputeCharacterWidth(): array
{
return [
[0, '@', 0, 0.0],
Expand All @@ -42,6 +62,17 @@ public static function provideItCanComputeWidth(): array
];
}

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],
[86.688, 'world', 32, 0.0],
];
}

protected function getFontById(
string $fontId
): Font {
Expand Down

0 comments on commit 914708c

Please sign in to comment.