PDFium Delphi Component Docs

FPdfTextShapingHb unit

Unit: FPdfTextShapingHb
HarfBuzz shaping backend for Free Pascal targets, binding libharfbuzz at run time and taking the font bytes from a host-supplied resolver

Syntax

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;

Description

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

Functions

FunctionDescription
HarfBuzzAvailableTrue when libharfbuzz was found and the required entry points resolved; the first call performs the binding
HarfBuzzVersionStringVersion of the loaded library as "major.minor.micro", or an empty string when the library is unavailable

Types

TypeDescription
TPdfFontDataResolverMethod 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
EPdfHarfBuzzErrorException raised when no resolver is configured, when libharfbuzz is unavailable, or when a HarfBuzz blob, face, font or buffer cannot be created
TPdfHarfBuzzTextShaperTPdfTextShaper implementation backed by HarfBuzz, with Create, Destroy and Shape

Members of TPdfHarfBuzzTextShaper

MemberDescription
Create(Resolver)Creates the shaper and stores the TPdfFontDataResolver it will use for every font lookup
DestroyReleases 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
SupportsVerticalShapingReturns True: HarfBuzz reports vertical advances and vertical glyph forms for ptdTopToBottom, so this backend serves TPdf.AddVerticalShapedText and RenderVerticalParagraph

Remarks

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

See Also

FPdfTextShaping, TPdf.AddShapedText, TPdf.AddCidType2Text, ConfigureSystemFontProvider