PDFium Delphi Component Docs

FPdfBidi unit

Unit: FPdfBidi
Unicode Bidirectional Algorithm (UAX #9) implementation that resolves embedding levels and visual order for mixed-direction text

Syntax

type EPdfBidiError = class(Exception);
type TPdfBidiClass = (bcL, bcR, bcAL, bcEN, bcES, bcET, bcAN, bcCS, bcNSM, bcBN, bcB, bcS, bcWS, bcON, bcLRE, bcLRO, bcRLE, bcRLO, bcPDF, bcLRI, bcRLI, bcFSI, bcPDI);
type TPdfBidiParagraphDirection = (pbdAuto, pbdLeftToRight, pbdRightToLeft);
type TPdfBidiLevels = array of Byte;
type TPdfBidiOrder = array of Integer;
const PdfBidiMaxDepth = 125;
const PdfBidiMaxResolvedLevel = 126;
function PdfBidiClassOf(CodePoint: Cardinal): TPdfBidiClass;
function PdfBidiIsFormattingControl(CodePoint: Cardinal): Boolean;
function PdfBidiRenderText(const Text: WideString; LogicalStart: Integer; var RenderedText: WideString; out LogicalUnits: TPdfBidiOrder): Boolean;
function PdfBidiIsRtlCodePoint(CodePoint: Cardinal): Boolean;
function PdfResolveBidiLevels(const Text: WideString; Direction: TPdfBidiParagraphDirection; out Levels: TPdfBidiLevels; out ParagraphLevel: Byte): Boolean;
function PdfResolveBidiParagraphLevels(const Text: WideString; Direction: TPdfBidiParagraphDirection; out Levels: TPdfBidiLevels; out ParagraphLevel: Byte): Boolean;
function PdfBidiLineLevels(const Text: WideString; const ParagraphLevels: TPdfBidiLevels; Start, Count: Integer; ParagraphLevel: Byte; var Levels: TPdfBidiLevels): Boolean;
function PdfBidiVisualOrder(const Text: WideString; const Levels: TPdfBidiLevels): TPdfBidiOrder;
function PdfBidiParagraphIsRtl(const Text: WideString): Boolean;

Description

FPdfBidi implements the Unicode Bidirectional Algorithm defined by UAX #9, which decides the direction and the visual order of mixed-direction text before it is measured or drawn. A shaping stage works on a run whose direction and script are already settled, so the caller has to resolve that direction first

The unit covers the rules P2-P3 for the paragraph level, X1-X10 for explicit levels and isolating run sequences, W1-W7 for weak types, N0-N2 for bracket pairs and neutrals, I1-I2 for implicit levels, and L1-L2 for the level reset and the reordering, including isolate initiators and BD16 bracket pairs

Resolution runs over code points rather than UTF-16 code units, and the resolved levels are expanded back onto the code units at the end, so both halves of a surrogate pair carry one level each

PdfResolveBidiLevels retains its one-shot contract: Direction selects the P2-P3 scan through pbdAuto or forces the paragraph level, Levels receives one embedding level per code unit of Text including the final whitespace reset, and ParagraphLevel receives the level the paragraph resolved to. The result is False only for empty input, and Levels is always sized to Length(Text)

PdfBidiVisualOrder applies L2 to those levels and returns the logical code-unit index that belongs at each visual position, so element zero of the result is the code unit that renders leftmost. Text is needed to keep a surrogate pair together, because reversing its two code units would turn the pair into two unpaired surrogates

PdfBidiClassOf exposes the Bidi_Class lookup itself. The table is derived from the Unicode Character Database and folded into ranges; code points the database does not assign fall back to the default ranges UAX #9 defines for them, which are the R, AL, ET and BN blocks, and L everywhere else

PdfBidiIsRtlCodePoint and PdfBidiParagraphIsRtl answer the two questions a caller usually asks before layout: whether one code point is strongly right-to-left, and whether a whole paragraph resolves right-to-left under P2-P3

Paragraph resolution and line layout since v3.338.0

For wrapping, call PdfResolveBidiParagraphLevels on the complete paragraph before measuring its lines. It performs the paragraph resolution phases without the line-specific L1 reset, preserving bracket, neutral and number context across proposed line breaks

Once a line range is known, PdfBidiLineLevels slices those existing levels and applies L1 to the original character classes on that line. Pass its result to PdfBidiVisualOrder for L2; resolving each substring as a new paragraph loses cross-line context

The paragraph helper accepts nonempty valid UTF-16 containing one paragraph, with an optional terminal paragraph separator or CRLF pair. Empty, malformed UTF-16, invalid directions and internal paragraph separators return False with empty levels and paragraph level zero

The line helper uses zero-based UTF-16 Start/Count, requires a matching source level-array length, paragraph level 0 or 1 and selected resolved values up to 126, and rejects empty/out-of-range or surrogate-splitting spans and inconsistent surrogate-pair levels. Failure clears Levels; the var output supports aliasing the source array through a retained input reference

These low-level helpers expose level processing for rendering engines. Paragraph editing retains its separate text-admission, font, style and resource contracts; bidi-control rendering and complete Unicode conformance remain separate requirements

Functions

FunctionDescription
PdfBidiClassOfReturns the TPdfBidiClass of a single code point, using the range table and the UAX #9 defaults for code points the database does not assign
PdfBidiIsRtlCodePointReturns True when the code point is a strong right-to-left character, that is bcR or bcAL
PdfResolveBidiLevelsResolves one embedding level per UTF-16 code unit of Text into Levels and reports the paragraph level in ParagraphLevel; returns False only for empty input
PdfResolveBidiParagraphLevelsResolves a complete valid UTF-16 paragraph before line resets, preserving context for wrapping
PdfBidiLineLevelsCopies a scalar-aligned paragraph range and applies L1 without repeating the paragraph analysis
PdfBidiVisualOrderApplies the L2 reordering to Levels and returns the logical code-unit index for each visual position, keeping surrogate pairs intact
PdfBidiParagraphIsRtlReturns True when Text resolves to a right-to-left paragraph level under P2-P3

Logical and rendered text since v3.340.0

PdfBidiIsFormattingControl recognises the twelve Unicode Bidi_Control characters: U+061C, U+200E-U+200F, U+202A-U+202E and U+2066-U+2069. Shaping joiners U+200C-U+200D and other boundary neutrals are distinct and are not removed by this API

After resolving the original paragraph and its line levels, PdfBidiRenderText removes these controls from one logical shaping span and returns one original zero-based UTF-16 offset per retained UTF-16 unit in LogicalUnits. Offsets include LogicalStart; supplementary scalars retain both adjacent source-unit offsets

Empty and control-only strings succeed with empty rendered text and maps. Malformed UTF-16, negative or overflowing offset ranges, C0/C1 controls, unsupported line/paragraph separators and other boundary neutrals return False and clear both outputs. The rendered string may alias the input; the helper retains the input before clearing outputs

Keep the original logical string for bidi analysis and semantic ownership. Removing all controls before paragraph analysis or L2 changes isolate boundaries and directional context; the paragraph pipeline removes only X9 embedding/override/PDF entries before L2 and suppresses all twelve controls only at glyph emission

The Unicode property list defines this finite control set, while UAX #9 defines paragraph resolution, X9, line reordering and shaping phases. The helper does not establish complete Unicode conformance or replace a shaping backend

FunctionPurpose
PdfBidiIsFormattingControlClassifies the exact twelve formatting controls, independently of shaping joiners
PdfBidiRenderTextBuilds glyph input and scalar-safe original UTF-16 offsets for an already resolved logical span, with cleared failure outputs

Types

TypePurpose
EPdfBidiErrorException class the unit raises for bidi failures; derives from Exception
TPdfBidiClassBidi_Class of a code point, in the order UAX #9 Table 4 lists the values
TPdfBidiParagraphDirectionSelects between P2-P3 detection and a forced paragraph direction
TPdfBidiLevelsarray of Byte holding one resolved embedding level per UTF-16 code unit
TPdfBidiOrderarray of Integer holding logical code-unit indices in visual order

Constants

ConstantValue and purpose
PdfBidiMaxDepth125, the maximum explicit nesting depth X1 allows; embeddings and isolates past it are counted as overflow instead of raising the level
PdfBidiMaxResolvedLevel126, the maximum level after implicit resolution

TPdfBidiParagraphDirection values

ValueMeaning
pbdAutoDerives the paragraph level from the first strong character, per P2-P3
pbdLeftToRightForces paragraph level 0
pbdRightToLeftForces paragraph level 1

TPdfBidiClass values

ValueBidi_Class and category
bcLLeft_To_Right, strong
bcRRight_To_Left, strong
bcALArabic_Letter, strong; becomes bcR under W3
bcENEuropean_Number, weak
bcESEuropean_Separator, weak
bcETEuropean_Terminator, weak
bcANArabic_Number, weak
bcCSCommon_Separator, weak
bcNSMNonspacing_Mark, weak; takes the class of the preceding character under W1
bcBNBoundary_Neutral, weak; removed by X9
bcBParagraph_Separator, neutral; terminates every embedding under X8
bcSSegment_Separator, neutral
bcWSWhite_Space, neutral
bcONOther_Neutral, neutral; the class brackets carry into N0
bcLRELeft_To_Right_Embedding, explicit formatting
bcLROLeft_To_Right_Override, explicit formatting
bcRLERight_To_Left_Embedding, explicit formatting
bcRLORight_To_Left_Override, explicit formatting
bcPDFPop_Directional_Format, explicit formatting; pops an embedding under X7
bcLRILeft_To_Right_Isolate, isolate initiator
bcRLIRight_To_Left_Isolate, isolate initiator
bcFSIFirst_Strong_Isolate, isolate initiator whose direction comes from the first strong character up to its matching bcPDI
bcPDIPop_Directional_Isolate, terminates an isolate under X6a

Remarks

See Also

FPdfTextShaping, TPdf.AddShapedText, TPdf.FocusedFormFieldTextDirection