Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Support rendering 'Oblique' fonts #236

Merged
merged 3 commits into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading