php font lib

网站数据 2025-07-24

PHP Font Lib

This library can be used to:

  • Read TrueType, OpenType (with TrueType glyphs), WOFF font files
  • Extract basic info (name, style, etc)
  • Extract advanced info (horizontal metrics, glyph names, glyph shapes, etc)
  • Make an Adobe Font Metrics (AFM) file from a font

This project was initiated by the need to read font files in the DOMPDF project.

Usage Example

Base font information

$font = FontLibFont::load('fontfile.ttf');
$font->parse();  // for getFontWeight() to work this call must be done first!
echo $font->getFontName() .'
'
; echo $font->getFontSubfamily() .'
'
; echo $font->getFontSubfamilyID() .'
'
; echo $font->getFontFullName() .'
'
; echo $font->getFontVersion() .'
'
; echo $font->getFontWeight() .'
'
; echo $font->getFontPostscriptName() .'
'
; $font->close();

Font Metrics Generation

$font = FontLibFont::load('fontfile.ttf');
$font->parse();
$font->saveAdobeFontMetrics('fontfile.ufm');

Create a font subset

reduce(); touch('fontfile.subset.ttf'); $font->open('fontfile.subset.ttf', FontLibBinaryStream::modeReadWrite); $font->encode(array("OS/2")); $font->close();">
$font = FontLibFont::load('fontfile.ttf');
$font->parse();
$font->setSubset("abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ.:,;' (!?)+-*/== 1234567890"); // characters to include
$font->reduce();
touch('fontfile.subset.ttf');
$font->open('fontfile.subset.ttf', FontLibBinaryStream::modeReadWrite);
$font->encode(array("OS/2"));
$font->close();
下载源码

通过命令行克隆项目:

git clone https://github.com/dompdf/php-font-lib.git