Download the PHP package tecnickcom/tc-lib-pdf-font without Composer
On this page you can find all versions of the php package tecnickcom/tc-lib-pdf-font. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download tecnickcom/tc-lib-pdf-font
More information about tecnickcom/tc-lib-pdf-font
Files in tecnickcom/tc-lib-pdf-font
Package tc-lib-pdf-font
Short Description PHP library containing PDF font methods and utilities
License LGPL-3.0-or-later
Homepage https://tcpdf.org
Informations about the package tc-lib-pdf-font
tc-lib-pdf-font
Font import, metrics, and stack management utilities for PDF generation.
💖 Part of the tc-lib-pdf / TCPDF ecosystem (100M+ installs). Sponsor its maintenance →
Overview
tc-lib-pdf-font provides font import and runtime font-stack services used by PDF composition engines.
It bridges static font assets and runtime document composition by handling metrics, encodings, and font program references in a PDF-friendly way. This modular design lets applications evolve font workflows independently from the rest of the rendering stack.
| Namespace | \Com\Tecnick\Pdf\Font |
| Author | Nicola Asuni [email protected] |
| License | GNU LGPL v3 - see LICENSE |
| API docs | https://tcpdf.org/docs/srcdoc/tc-lib-pdf-font |
| Packagist | https://packagist.org/packages/tecnickcom/tc-lib-pdf-font |
Features
Font Processing
- Import support for core, Type1, and TrueType sources
- Font metadata extraction and normalization
- Utilities for subset and output dictionary generation
- Optional injectable cache to reuse computed TrueType font subsets
Runtime Font Stack
- Font stack insertion and switching
- Glyph width/bounding-box helpers
- Character replacement and fallback handling
- Glyph index encoding of composite fonts, with support for the supplementary planes
Requirements
- PHP 8.2 or later
- Extensions:
json,zlib - Composer
Installation
Quick Start
For larger examples, refer to test/OutputTest.php and the conversion tooling in this repository.
Character Encoding of Composite Fonts
A TrueTypeUnicode font is emitted as a composite (Type0) font with the Identity-H encoding, so every character code in a content stream is 2 bytes wide.
Since version 4.0 the character code is the glyph index of the font (CID == GID): the font dictionary declares /CIDToGIDMap /Identity, the /W array is keyed by glyph index, and a /ToUnicode CMap is generated for the glyphs used by the document so that the text stays searchable and extractable. Earlier versions used the Unicode codepoint as the character code and embedded a 131072-byte CIDToGIDMap stream, which limited the addressable characters to the Basic Multilingual Plane.
Encoding a string
The consumer of this library must encode composite text through the font stack, so that the glyphs used are recorded for the /W array and the /ToUnicode CMap:
The text object written to the page must also select the font the string was encoded with, otherwise the glyph indices are resolved against a different font. This does not apply to the fonts that are not GID encoded, whose character codes mean the same thing in every font.
Relevant methods:
Stack::isCurrentGidEncoded(): true when the current font encodes text as glyph indices.Stack::ordArrToGidStr(array $uniarr): encodes codepoints as 2-byte glyph indices and records them.Stack::getGidForOrd(int $ord): glyph index of a codepoint,0when the font has no glyph for it.Buffer::addUsedGid(string $key, int $gid, int $ord): records a glyph and the codepoint it was encoded from.
Font definition files
The glyph index of a codepoint is read at runtime from the .ctg.z artifact of the font, so definition files generated by earlier versions keep working for the whole Basic Multilingual Plane without being regenerated. The file must remain next to the .json definition file.
Codepoints above U+FFFF do not fit that table and are stored in the definition file under the ctgu key:
That key is only written when the font is converted with --encoding_id=10, which selects the format 12 cmap subtable. A definition file carrying ctgu is still readable by older versions of the library, which ignore it.
Subset Caching
TrueType font subsetting is computational and memory intensive. When the same
fonts and character sets are embedded repeatedly (for example across many
generated documents), you can inject an optional cache into
\Com\Tecnick\Pdf\Font\Output to reuse previously computed subset programs.
Provide any object implementing
\Com\Tecnick\Pdf\Font\FontSubsetCacheInterface:
Pass it as the last constructor argument:
When no cache is supplied (the default), behavior is unchanged and every subset is recomputed. The library never evicts entries — the injected backend owns expiration and size limits. The cache key already accounts for the font program bytes, the cmap-selection metrics, and the requested subset characters, so distinct inputs never collide.
Using a PSR-16 cache
No PSR-16 adapter is bundled, because the interface above is intentionally trivial to wrap around any backend (PSR-16/PSR-6, Symfony Cache, Laravel, Redis, APCu, …). A correct PSR-16 wrapper is a few lines:
The normalize() step matters: the library's raw key is human-readable and
works with permissive backends, but stricter PSR-16 implementations may reject
or mangle it. Hashing it keeps the wrapper portable. Note that hashing discards
the readable namespace, so scope the underlying PSR-16 pool to this library if
you share it with other consumers.
Converting Existing Fonts
Use the CLI utilities in util/ to convert existing font files into the JSON/Z format consumed by this library.
Convert One or More Fonts
Run util/convert.php and pass one or more input files with --fonts:
The command writes generated font definition files to --outpath.
Common options:
--type: Explicit font type (TrueTypeUnicode,TrueType,Type1,CID0JP,CID0KR,CID0CS,CID0CT). Leave empty for autodetect.--encoding: Encoding table (for examplecp1252for many non-Unicode Type1/Core cases). Omit for Unicode and symbolic fonts.--flags: PDF descriptor flags. Default is32(non-symbolic).--platform_idand--encoding_id: CMAP selection for TrueType Unicode imports (defaults:3and1). Use--encoding_id=10to read the format 12 subtable, which is required for the characters above U+FFFF. It is a safe default: a font without that subtable falls back to the BMP one.--linked: Link to system font file instead of embedding/copying it (not transportable).
To see full usage help:
Bulk Conversion
For batch generation from the mirrored font set:
This installs util dependencies and runs bulk_convert.php, which scans the mirror package and writes converted fonts under target/fonts/.
Notes:
bulk_convert.phpalso attempts OTF conversion via FontForge (fontforge -script otf2ttf.ff ...) before import.- If you run bulk conversion directly, customize destination with
php util/bulk_convert.php --outpath=/your/path/.
Upgrading from 3.x to 4.0
Version 4.0 changes the character codes emitted for TrueTypeUnicode fonts from Unicode codepoints to glyph indices, as described in Character Encoding of Composite Fonts.
A consumer of this library must be updated together with it:
- Encode composite text with
Stack::ordArrToGidStr()instead of writing UTF-16BE codepoints, and make the text object select the font the string was encoded with. A consumer that keeps writing codepoints produces PDFs whose text renders as unrelated glyphs, with no error raised. - A
TrueTypeUnicodefont emits one PDF object less, since theCIDToGIDMapstream is not embedded any more. Baselines that compare PDF output byte by byte, or that assert on object numbers, must be regenerated. - The
TFontDatashape gained thectgu,gidencandusedgidkeys. Code that builds that array must add them.
Unchanged:
- No method was removed or changed signature. The new members are additions.
- Font definition files and
.ctg.zartifacts generated by 3.x keep working for the whole Basic Multilingual Plane. Regenerate a font only to gain the characters above U+FFFF, with--encoding_id=10. - The output of
Core,TrueType,Type1andcidfont0fonts is unchanged.
Development
Font generation helpers are also available through Make targets such as fonts.
Packaging
For system packages, bootstrap with:
Contributing
Contributions are welcome. Please review SECURITY.md.
All versions of tc-lib-pdf-font with dependencies
ext-hash Version *
ext-json Version *
ext-pcre Version *
ext-zlib Version *
tecnickcom/tc-lib-file Version ^3.9
tecnickcom/tc-lib-unicode-data Version ^3.0
tecnickcom/tc-lib-pdf-encrypt Version ^2.11