TPdfFontDataResolver = function(const FontName: WideString;
out FontData: TBytes): Boolean of object;
EPdfHarfBuzzError = class(Exception);
TPdfHarfBuzzTextShaper = class(TPdfTextShaper)
constructor Create(const Resolver: TPdfFontDataResolver);
destructor Destroy; override;
function Shape(const Text, FontName: WideString; FontSize: Single;
const Options: TPdfTextShapingOptions;
out Shaped: TPdfShapedText): Boolean; override;
function SupportsVerticalShaping: Boolean; override;
end;
function HarfBuzzAvailable: Boolean;
function HarfBuzzVersionString: string;
FPdfTextShapingHb is the counterpart of the Uniscribe backend in FPdfTextShaping, for targets that have no usp10.dll. The interface is compiled under Free Pascal only, because Delphi targets Windows, where the Uniscribe backend applies
The two backends divide the work differently. Uniscribe itemises, shapes, places and reorders in one pass, whilst HarfBuzz shapes a single run whose direction and script are already known. This unit supplies the rest: FPdfBidi resolves the embedding levels, the Unicode script of each character segments the text into runs, and the runs are laid out in the visual order UAX #9 rule L2 gives. Common, inherited and unknown script characters continue the run they are in, so a digit or a space does not split a word
libharfbuzz is bound at run time through DynLibs, so it is not a build dependency and its absence is reported rather than fatal. HarfBuzzAvailable reports whether the library was found and the required entry points resolved, and HarfBuzzVersionString returns the version of the loaded library
The shaper does not look fonts up. Uniscribe reads the font binary out of a GDI device context, which has no portable equivalent, so the host passes a TPdfFontDataResolver to Create that maps a font name to the TrueType or OpenType bytes. That keeps font enumeration - fontconfig, CoreText, or an application's own font folder - outside a unit that has no business making that policy
| Function | Description |
|---|---|
HarfBuzzAvailable | True when libharfbuzz was found and the required entry points resolved; the first call performs the binding |
HarfBuzzVersionString | Version of the loaded library as "major.minor.micro", or an empty string when the library is unavailable |
| Type | Description |
|---|---|
TPdfFontDataResolver | Method reference that maps FontName to the font file bytes in FontData; returning False makes the shaper fail the request the same way an unreadable GDI font does on Windows |
EPdfHarfBuzzError | Exception raised when no resolver is configured, when libharfbuzz is unavailable, or when a HarfBuzz blob, face, font or buffer cannot be created |
TPdfHarfBuzzTextShaper | TPdfTextShaper implementation backed by HarfBuzz, with Create, Destroy and Shape |
| Member | Description |
|---|---|
Create(Resolver) | Creates the shaper and stores the TPdfFontDataResolver it will use for every font lookup |
Destroy | Releases the cached HarfBuzz face and blob, then the inherited destructor |
Shape(Text, FontName, FontSize, Options, Shaped) | Resolves embedding levels for ordinary base-direction modes, or uses uniform levels for already resolved modes; segments by level/script and shapes through HarfBuzz, retaining logical run-array order with visual positions in VisualX; False when the text produces no glyphs |
SupportsVerticalShaping | Returns True: HarfBuzz reports vertical advances and vertical glyph forms for ptdTopToBottom, so this backend serves TPdf.AddVerticalShapedText and RenderVerticalParagraph |
Since v3.339.0, ptdResolvedLeftToRight and ptdResolvedRightToLeft bypass substring bidi resolution and use uniform levels 0 and 1. Resolved input validation rejects malformed UTF-16 and direction/layout controls before calling the font resolver; a missing hb_buffer_set_direction export rejects the resolved request
HarfBuzzAvailable call probes libharfbuzz-0.dll and harfbuzz.dll on Windows, libharfbuzz.0.dylib and libharfbuzz.dylib on macOS, and libharfbuzz.so.0 and libharfbuzz.so elsewhereShape raises EPdfTextShapingError for empty text, an empty font name, a non-positive font size, invalid Options limits, text over MaxCharacters, glyphs over MaxGlyphs, or font data the resolver could not supplyShape raises EPdfHarfBuzzError when no TPdfFontDataResolver is configured, when libharfbuzz is unavailable, or when a HarfBuzz object cannot be createdOptions.MaxRuns, and Options.Direction maps ptdLeftToRight and ptdRightToLeft onto a fixed paragraph direction, with anything else resolved automaticallyShaped.FontData holds the bytes the resolver returned, which is what TPdf.AddShapedText needs to build a CID font