diff --git a/src/Fonts/FontRegistry.php b/src/Fonts/FontRegistry.php index c6c5102..b1055f0 100644 --- a/src/Fonts/FontRegistry.php +++ b/src/Fonts/FontRegistry.php @@ -15,7 +15,7 @@ public function addFont(string $filePath): void $this->fontFiles[] = $ttfFile; } - public function findMatchingFont(?string $family, bool $italic, float $weight): ?FontFile + public function findMatchingFont(?string $family, ?string $style, float $weight): ?FontFile { if (empty($this->fontFiles)) { return null; @@ -32,9 +32,19 @@ public function findMatchingFont(?string $family, bool $italic, float $weight): } } - // Attempt to find the closest-weight match with correct family and italicness. - $match = $this->closestMatchBasedOnWeight(function (FontFile $font) use ($family, $anyFontFamily, $italic) { - return ($anyFontFamily || $font->getFamily() === $family) && $font->isItalic() === $italic; + // Attempt to find the closest-weight match with correct family and cursiveness. + $match = $this->closestMatchBasedOnWeight(function (FontFile $font) use ($family, $anyFontFamily, $style) { + $result = ($anyFontFamily || $font->getFamily() === $family); + $isItalic = $font->isItalic(); + $isOblique = $font->isOblique(); + switch ($style) { + case 'italic': + return $result && ($isItalic || $isOblique); + case 'oblique': + return $result && ($isOblique || $isItalic); + default: + return $result && !($isItalic || $isOblique); + } }, $weight); // Attempt to match just based on the font family. diff --git a/src/Fonts/TrueTypeFontFile.php b/src/Fonts/TrueTypeFontFile.php index 93586a3..e1a91e0 100644 --- a/src/Fonts/TrueTypeFontFile.php +++ b/src/Fonts/TrueTypeFontFile.php @@ -44,7 +44,11 @@ public function getFamily(): string public function getWeight(): float { - return $this->weightClass ?? ($this->subfamily === 'Bold' || $this->subfamily === 'Bold Italic' ? 700 : 400); + return $this->weightClass ?? ( + $this->subfamily === 'Bold' || + $this->subfamily === 'Bold Italic' || + $this->subfamily === 'Bold Oblique' ? 700 : 400 + ); } public function isItalic(): bool @@ -52,6 +56,11 @@ public function isItalic(): bool return $this->subfamily === 'Italic' || $this->subfamily === 'Bold Italic'; } + public function isOblique(): bool + { + return $this->subfamily === 'Oblique' || $this->subfamily === 'Bold Oblique'; + } + public function isMonospace(): bool { // TODO implement detection for monospace fonts diff --git a/src/Rasterization/Renderers/TextRenderer.php b/src/Rasterization/Renderers/TextRenderer.php index 886f03b..11e4c21 100644 --- a/src/Rasterization/Renderers/TextRenderer.php +++ b/src/Rasterization/Renderers/TextRenderer.php @@ -33,9 +33,8 @@ protected function prepareRenderParams(array $options, Transform $transform, ?Fo $fontPath = null; if (isset($fontRegistry)) { - $isItalic = $options['fontStyle'] === 'italic' || $options['fontStyle'] === 'oblique'; $weight = self::resolveFontWeight($options['fontWeight']); - $matchingFont = $fontRegistry->findMatchingFont($options['fontFamily'], $isItalic, $weight); + $matchingFont = $fontRegistry->findMatchingFont($options['fontFamily'], $options['fontStyle'], $weight); if ($matchingFont !== null) { $fontPath = $matchingFont->getPath(); }