Skip to content

Commit

Permalink
Add getFontFamilyName and getCurrentFontKey functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolaasuni committed Sep 14, 2024
1 parent 5d4f0a8 commit 479a66f
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 4 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.5.1
2.6.0
56 changes: 53 additions & 3 deletions src/Stack.php
Original file line number Diff line number Diff line change
Expand Up @@ -287,8 +287,20 @@ public function cloneFont(
);
}

/**
* Returns the current font key.
*
* @return string
*/
public function getCurrentFontKey(): string
{
return $this->stack[$this->index]['key'];
}

/**
* Returns the current font type (i.e.: Core, TrueType, TrueTypeUnicode, Type1).
*
* @return string
*/
public function getCurrentFontType(): string
{
Expand All @@ -297,6 +309,8 @@ public function getCurrentFontType(): string

/**
* Returns the PDF code to use the current font.
*
* @return string
*/
public function getOutCurrentFont(): string
{
Expand All @@ -305,6 +319,8 @@ public function getOutCurrentFont(): string

/**
* Returns true if the current font type is Core, TrueType or Type1.
*
* @return bool
*/
public function isCurrentByteFont(): bool
{
Expand All @@ -314,6 +330,8 @@ public function isCurrentByteFont(): bool

/**
* Returns true if the current font type is TrueTypeUnicode or cidfont0.
*
* @return bool
*/
public function isCurrentUnicodeFont(): bool
{
Expand Down Expand Up @@ -375,6 +393,8 @@ public function replaceMissingChars(array $uniarr, array $subs = []): array
* Returns true if the specified unicode value is defined in the current font
*
* @param int $ord Unicode character value to convert
*
* @return bool
*/
public function isCharDefined(int $ord): bool
{
Expand All @@ -386,6 +406,8 @@ public function isCharDefined(int $ord): bool
* Returns the width of the specified character
*
* @param int $ord Unicode character value.
*
* @return float
*/
public function getCharWidth(int $ord): float
{
Expand All @@ -403,6 +425,8 @@ public function getCharWidth(int $ord): float
* Returns the lenght of the string specified using an array of codepoints.
*
* @param array<int, int> $uniarr Array of character codepoints.
*
* @return float
*/
public function getOrdArrWidth(array $uniarr): float
{
Expand Down Expand Up @@ -597,7 +621,7 @@ protected function getFontMetric(int $idx): array
*
* @param ?int $size Font size in points (set to null to inherit the last font size).
*
* return float
* @return float
*/
protected function getInputSize(?int $size = null): float
{
Expand All @@ -617,7 +641,8 @@ protected function getInputSize(?int $size = null): float
* Normalize the input spacing
*
* @param ?float $spacing Extra spacing between characters.
* return float
*
* @return float
*/
protected function getInputSpacing(?float $spacing = null): float
{
Expand All @@ -637,7 +662,8 @@ protected function getInputSpacing(?float $spacing = null): float
* Normalize the input stretching
*
* @param ?float $stretching Horizontal character stretching ratio.
* return float
*
* @return float
*/
protected function getInputStretching(?float $stretching = null): float
{
Expand Down Expand Up @@ -705,4 +731,28 @@ protected function getNormalizedFontKeys(string $fontfamily): array

return $keys;
}

/**
* Returns the nomalized font family name or the current font name (key.
*
* @param string $fontfamily Raw font family name.
*
* @return string
*/
public function getFontFamilyName(string $fontfamily): string
{
$fkeys = $this->getNormalizedFontKeys($fontfamily);
foreach ($fkeys as $fkey) {
if ($this->isValidKey($fkey)) {
return $fkey;
}

$pdfakey = 'pdfa' . $fkey;
if ($this->isValidKey($pdfakey)) {
return $pdfakey;
}
}

return $this->getCurrentFontKey();
}
}
22 changes: 22 additions & 0 deletions test/StackTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ public function testStack(): void
$this->bcAssertEqualsWithDelta(4.6704, $bfont['missingwidth'], 0.0001);
$this->bcAssertEqualsWithDelta([-1.092, -3.08, 18.5976, 13.342], $bfont['fbbox'], 0.0001);

$fkey = $stack->getCurrentFontKey();
$this->assertEquals('pdfahelveticaBI', $fkey);

$font = $stack->getCurrentFont();
$this->assertEquals($bfont, $font);

Expand Down Expand Up @@ -107,6 +110,9 @@ public function testStack(): void
$font = $stack->getCurrentFont();
$this->assertEquals($afont, $font);

$fkey = $stack->getCurrentFontKey();
$this->assertEquals('pdfatimes', $fkey);

$type = $stack->getCurrentFontType();
$this->assertEquals('Type1', $type);

Expand Down Expand Up @@ -145,6 +151,22 @@ public function testStack(): void
$this->assertEquals(17, $font['size']);
$this->assertEquals(0.7, $font['spacing']);
$this->assertEquals(1.3, $font['stretching']);

$fname = $stack->getFontFamilyName('unknown');
$this->assertEquals('freesansBI', $fname);

new \Com\Tecnick\Pdf\Font\Import($indir . 'pdfa/pfb/PDFACourier.pfb');
$bfont = $stack->insert($objnum, 'courier', '', null, null, null, '', null);
$this->assertNotEmpty($bfont);

$fname = $stack->getFontFamilyName('freesans');
$this->assertEquals('freesans', $fname);

$fname = $stack->getFontFamilyName('cursive');
$this->assertEquals('pdfatimes', $fname);

$fname = $stack->getFontFamilyName('unknown');
$this->assertEquals('pdfacourier', $fname);
}

public function testEmptyStack(): void
Expand Down

0 comments on commit 479a66f

Please sign in to comment.