phenx/php-font-lib
Read and parse TrueType, OpenType (TT glyphs) and WOFF fonts in PHP. Extract basic/advanced metadata, metrics, glyph names and shapes, generate Adobe Font Metrics (AFM), and build font subsets. Used by dompdf for font handling.
Font, BinaryStream, AdobeFontMetrics) allows for custom extensions, such as adding support for additional font formats or integrating with Laravel’s service container for dependency injection. Hooks like setSubset() and reduce() enable fine-grained control over font processing pipelines, aligning well with Laravel’s service-layer patterns.php: ^7.1 || ^8.0, ext-mbstring) reduce conflict risk with Laravel’s core or third-party packages. The simplicity of the dependency graph ensures low maintenance overhead.getFontName(), getFontWeight()).setSubset(), reduce()).saveAdobeFontMetrics()).encode()).
This aligns well with Laravel’s service-layer patterns, allowing for easy wrapping in a FontService facade or service class.BinaryStream) to mitigate memory issues in high-throughput scenarios (e.g., batch PDF generation). This is critical for Laravel applications handling large-scale document generation.array vs. TypedArray handling in PHP 8.1+. This risk is mitigated by the library’s active maintenance and recent updates for PHP 8.4/8.5 compatibility.glyf tables or unsupported cmap formats). The library’s error handling improvements (e.g., fixes for cmap subtable format 2 in v0.5.5) suggest robustness, but custom validation may be needed for production use. This can be addressed by wrapping the library in a service class with try-catch blocks and fallback mechanisms.dispatch()) can mitigate this by offloading tasks to background workers. Additionally, caching subsets can reduce repeated processing.composer.json autoloader. No additional configuration is required beyond installing the package.// app/Providers/AppServiceProvider.php
public function register()
{
$this->app->bind(FontService::class, function ($app) {
return new FontService(new FontLib\Font());
});
}
Font facade to simplify usage in Blade templates or controllers. Example:
// app/Facades/Font.php
namespace App\Facades;
use Illuminate\Support\Facades\Facade;
class Font extends Facade
{
protected static function getFacadeAccessor()
{
return 'font.service';
}
}
php artisan font:subset path/to/font.ttf characters_to_subset
This provides a user-friendly interface for non-developers.dompdf/dompdf for seamless font embedding. Example workflow:
php-font-lib.Storage::disk()) to handle font file I/O, ensuring consistent path handling across environments.FontSubsetJob) for scalability, especially for high-throughput applications. Example:
// app/Jobs/FontSubsetJob.php
namespace App\Jobs;
use Illuminate\Bus\Queueable;
use FontLib\Font;
class FontSubsetJob implements Queueable
{
public function handle()
{
$font = Font::load(storage_path('fonts/source.ttf'));
$font->parse();
$font->setSubset("abc...");
$font->reduce();
$font->save(storage_path('fonts/subset.ttf'));
}
}
FontLib\Font class in unit tests to isolate business logic from font processing.Dependency Addition:
composer require dompdf/php-font-lib
ext-mbstring is enabled in php.ini by running:
php -m | grep mbstring
php.ini or use a Dockerfile to ensure the extension is installed in all environments.Service Registration:
FontService class to wrap the library’s functionality and handle error cases, caching, and resource management. Example:
// app/Services/FontService.php
How can I help you explore Laravel packages today?