PDFium Delphi Component Docs

AddShapedText method

Component: TPdf  ·  Units: PDFium, FPdfTextShaping

Syntax

function AddShapedText(const Text, Font: WString; FontSize: Single; X, Y: Double; out Shaped: TPdfShapedText; Direction: TPdfTextDirection = ptdAuto; Color: TColor = clBlack; Alpha: Byte = $FF): Boolean;

function AddShapedText(const Text, Font: WString; FontSize: Single; X, Y: Double; Direction: TPdfTextDirection; Color: TColor; Alpha: Byte; const Shaped: TPdfShapedText): Boolean; overload;

Description

Shapes Unicode text through the native Windows script engine, loads the selected system font once, and writes explicitly positioned glyphs through PDFium's CID Type 2 font API

ptdAuto resolves the paragraph base direction from the first strong character, while explicit left-to-right and right-to-left modes are available for application-controlled layout

Unicode semantics

Each glyph receives a bounded CID mapping and a generated ToUnicode entry, including cluster-aware mappings for ligatures and contextual forms; since v3.352.0 the CID-to-GID map is the identity over the font's own glyph count (CID k+1 stands for glyph k), so every call that uses the same font program emits the same map

The text object carries the original logical string in ActualText, so conforming consumers can recover logical reading order even when positioned glyph geometry is visual-order bidi content

TPdf.Text follows PDFium's physical bidi extraction order and may therefore differ from the original logical input order

Shaping result

TPdfShapedText returns font bytes, total width, glyph and logical-character counts, and visual runs containing bidi levels, glyph IDs, advances, offsets, and source clusters

Use ShapePdfText for shaping without modifying a document, or instantiate TPdfUniscribeTextShaper with TPdfTextShapingOptions when explicit character, glyph, and run budgets are required

Prepared shapes since v3.367.0

The overload that takes const Shaped emits a shape the caller produced, for example one TPdfTextShaper.ShapeSpan returned for a span inside a cursive-joining word, and does not shape Text again. The shape must describe the same text, font name and font size; a mismatch raises the usual EPdfProduction failure and the page is left unchanged

Safety and performance

Input, glyph, and run limits fail closed before unbounded allocation

Runs sharing one baseline are emitted as one text object and glyph positions are generated in linear time

The method requires the PDFium CID Type 2 font, character-code, position, marked-content, and content-regeneration exports

Resolved flow and font scopes

Since v3.339.0, ptdResolvedLeftToRight and ptdResolvedRightToLeft shape an already resolved visible run in one uniform direction. The original explicit directions continue to specify paragraph bases, and the configured backend supplies shaping

PDFium merges font resources by BaseFont and type, so two calls that supply different CID maps for one font program used to save shifted glyph IDs despite correct ActualText. Identical identity maps make that merge harmless for shaped text since v3.352.0, qualified for resolved LTR, resolved Arabic RTL and large supplementary-glyph fonts; two different font programs loaded under one BaseFont name still need separate names or resource scopes, and native rendering of the merged resource is outside this contract

See Also

AddText, GetStructuredText, GenerateLayout