Skip to content

Commit

Permalink
feat: Support rendering 'Oblique' fonts (#236)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tontyna authored Jul 12, 2024
1 parent dc2ef9c commit efb03d8
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
18 changes: 14 additions & 4 deletions src/Fonts/FontRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand Down
11 changes: 10 additions & 1 deletion src/Fonts/TrueTypeFontFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,23 @@ 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
{
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
Expand Down
3 changes: 1 addition & 2 deletions src/Rasterization/Renderers/TextRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down

0 comments on commit efb03d8

Please sign in to comment.