Skip to content

Commit d75bb9b

Browse files
authored
Fix font subfamily name detection (#134)
1 parent 5d475d6 commit d75bb9b

9 files changed

Lines changed: 106 additions & 6 deletions

File tree

‎src/FontProcessor.php‎

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Intervention\Image\Exceptions\FileNotReadableException;
1414
use Intervention\Image\Exceptions\StreamException;
1515
use Intervention\Image\Exceptions\InvalidArgumentException;
16+
use Intervention\Image\Exceptions\StateException;
1617
use Intervention\Image\Geometry\Rectangle;
1718
use Intervention\Image\Interfaces\ColorInterface;
1819
use Intervention\Image\Interfaces\FontInterface;
@@ -34,6 +35,7 @@ class FontProcessor extends AbstractFontProcessor
3435
* @throws FileNotFoundException
3536
* @throws FileNotReadableException
3637
* @throws StreamException
38+
* @throws StateException
3739
*/
3840
public function boxSize(string $text, FontInterface $font): SizeInterface
3941
{
@@ -59,17 +61,22 @@ public function boxSize(string $text, FontInterface $font): SizeInterface
5961
* @throws FileNotFoundException
6062
* @throws FileNotReadableException
6163
* @throws StreamException
64+
* @throws StateException
6265
*/
6366
public function textToVipsImage(
6467
string $text,
6568
FontInterface $font,
6669
ColorInterface $color = new Color(0, 0, 0),
6770
): VipsImage {
71+
if (!$font->hasFile()) {
72+
throw new StateException('No font file specified');
73+
}
74+
6875
return VipsImage::text(
6976
'<span ' . $this->pangoAttributes($font, $color) . '>' . htmlspecialchars($text) . '</span>',
7077
[
7178
'fontfile' => $font->filepath(),
72-
'font' => TrueTypeFont::fromPath($font->filepath())->familyName() . ' ' . $font->size(),
79+
'font' => $this->fontDescription($font),
7380
'dpi' => 72,
7481
'rgba' => true,
7582
'width' => $font->wrapWidth(),
@@ -84,6 +91,35 @@ public function textToVipsImage(
8491
);
8592
}
8693

94+
/**
95+
* Create vips font description.
96+
*
97+
* @throws InvalidArgumentException
98+
* @throws StreamException
99+
* @throws DriverException
100+
* @throws FileNotReadableException
101+
* @throws DirectoryNotFoundException
102+
* @throws FileNotFoundException
103+
*/
104+
private function fontDescription(FontInterface $font): string
105+
{
106+
$ttf = TrueTypeFont::fromPath($font->filepath());
107+
108+
try {
109+
$familyName = $ttf->typographicFamilyName();
110+
} catch (DriverException) {
111+
$familyName = $ttf->familyName();
112+
}
113+
114+
try {
115+
$subfamilyName = $ttf->typographicSubfamilyName();
116+
} catch (DriverException) {
117+
$subfamilyName = $ttf->subfamilyName();
118+
}
119+
120+
return $familyName . ' ' . $subfamilyName . ' ' . $font->size();
121+
}
122+
87123
/**
88124
* Return a pango markup attribute string based on the given font and color values
89125
*/

‎src/TrueTypeFont.php‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,36 @@ public function familyName(): string
2929
return $this->queryNameTable(1);
3030
}
3131

32+
/**
33+
* Return subfamily name of current font
34+
*
35+
* @throws DriverException
36+
*/
37+
public function subfamilyName(): string
38+
{
39+
return $this->queryNameTable(2);
40+
}
41+
42+
/**
43+
* Return typographic family name.
44+
*
45+
* @throws DriverException
46+
*/
47+
public function typographicFamilyName(): string
48+
{
49+
return $this->queryNameTable(16);
50+
}
51+
52+
/**
53+
* Return typographic subfamily name.
54+
*
55+
* @throws DriverException
56+
*/
57+
public function typographicSubfamilyName(): string
58+
{
59+
return $this->queryNameTable(17);
60+
}
61+
3262
/**
3363
* Query name table of current font file
3464
*

‎tests/Unit/TrueTypeFontTest.php‎

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,54 @@
44

55
namespace Intervention\Image\Drivers\Vips\Tests\Unit;
66

7+
use Generator;
78
use Intervention\Image\Drivers\Vips\Tests\BaseTestCase;
89
use Intervention\Image\Drivers\Vips\TrueTypeFont;
10+
use PHPUnit\Framework\Attributes\DataProvider;
911

1012
class TrueTypeFontTest extends BaseTestCase
1113
{
12-
protected TrueTypeFont $font;
14+
#[DataProvider('familyNamesProvider')]
15+
public function testFamilyNames(
16+
string $filename,
17+
string $family,
18+
string $subfamily,
19+
): void {
20+
$font = TrueTypeFont::fromPath($this->getTestResourcePath($filename));
21+
$this->assertEquals($family, $font->familyName());
22+
$this->assertEquals($subfamily, $font->subfamilyName());
23+
}
1324

14-
public function setUp(): void
25+
public static function familyNamesProvider(): Generator
1526
{
16-
$this->font = new TrueTypeFont($this->getTestResourceData('test.ttf'));
27+
yield ['test.ttf', 'Intervention Test', 'Regular'];
28+
yield ['test-bold.ttf', 'Intervention Test', 'Bold'];
29+
yield ['test-bolditalic.ttf', 'Intervention Test', 'Bold Italic'];
30+
yield ['test-italic.ttf', 'Intervention Test', 'Italic'];
31+
yield ['test-medium.ttf', 'Intervention Test', 'Medium'];
32+
yield ['test-semibold.ttf', 'Intervention Test', 'SemiBold'];
33+
yield ['test-custom.ttf', 'Intervention Test', 'Custom'];
34+
}
35+
36+
#[DataProvider('typographicFamilyNamesProvider')]
37+
public function testTypographicFamilyNames(
38+
string $filename,
39+
string $typographicFamily,
40+
string $typographicSubfamily,
41+
): void {
42+
$font = TrueTypeFont::fromPath($this->getTestResourcePath($filename));
43+
$this->assertEquals($typographicFamily, $font->typographicFamilyName());
44+
$this->assertEquals($typographicSubfamily, $font->typographicSubfamilyName());
1745
}
1846

19-
public function testFamilyName(): void
47+
public static function typographicFamilyNamesProvider(): Generator
2048
{
21-
$this->assertEquals('Intervention Test', $this->font->familyName());
49+
yield ['test.ttf', 'Intervention Test', 'Regular'];
50+
yield ['test-bold.ttf', 'Intervention Test', 'Bold'];
51+
yield ['test-bolditalic.ttf', 'Intervention Test', 'Bold Italic'];
52+
yield ['test-italic.ttf', 'Intervention Test', 'Italic'];
53+
yield ['test-medium.ttf', 'Intervention Test', 'Medium'];
54+
yield ['test-semibold.ttf', 'Intervention Test', 'SemiBold'];
55+
yield ['test-custom.ttf', 'Intervention Test', 'Custom'];
2256
}
2357
}

‎tests/resources/test-bold.ttf‎

1.74 KB
Binary file not shown.
1.8 KB
Binary file not shown.

‎tests/resources/test-custom.ttf‎

1.76 KB
Binary file not shown.

‎tests/resources/test-italic.ttf‎

1.76 KB
Binary file not shown.

‎tests/resources/test-medium.ttf‎

1.76 KB
Binary file not shown.

‎tests/resources/test-semibold.ttf‎

1.78 KB
Binary file not shown.

0 commit comments

Comments
 (0)