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;
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
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
| Function | Description |
|---|---|
PdfBidiClassOf | Returns the TPdfBidiClass of a single code point, using the range table and the UAX #9 defaults for code points the database does not assign |
PdfBidiIsRtlCodePoint | Returns True when the code point is a strong right-to-left character, that is bcR or bcAL |
PdfResolveBidiLevels | Resolves 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 |
PdfResolveBidiParagraphLevels | Resolves a complete valid UTF-16 paragraph before line resets, preserving context for wrapping |
PdfBidiLineLevels | Copies a scalar-aligned paragraph range and applies L1 without repeating the paragraph analysis |
PdfBidiVisualOrder | Applies the L2 reordering to Levels and returns the logical code-unit index for each visual position, keeping surrogate pairs intact |
PdfBidiParagraphIsRtl | Returns True when Text resolves to a right-to-left paragraph level under P2-P3 |
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
| Function | Purpose |
|---|---|
PdfBidiIsFormattingControl | Classifies the exact twelve formatting controls, independently of shaping joiners |
PdfBidiRenderText | Builds glyph input and scalar-safe original UTF-16 offsets for an already resolved logical span, with cleared failure outputs |
| Type | Purpose |
|---|---|
EPdfBidiError | Exception class the unit raises for bidi failures; derives from Exception |
TPdfBidiClass | Bidi_Class of a code point, in the order UAX #9 Table 4 lists the values |
TPdfBidiParagraphDirection | Selects between P2-P3 detection and a forced paragraph direction |
TPdfBidiLevels | array of Byte holding one resolved embedding level per UTF-16 code unit |
TPdfBidiOrder | array of Integer holding logical code-unit indices in visual order |
| Constant | Value and purpose |
|---|---|
PdfBidiMaxDepth | 125, the maximum explicit nesting depth X1 allows; embeddings and isolates past it are counted as overflow instead of raising the level |
PdfBidiMaxResolvedLevel | 126, the maximum level after implicit resolution |
| Value | Meaning |
|---|---|
pbdAuto | Derives the paragraph level from the first strong character, per P2-P3 |
pbdLeftToRight | Forces paragraph level 0 |
pbdRightToLeft | Forces paragraph level 1 |
| Value | Bidi_Class and category |
|---|---|
bcL | Left_To_Right, strong |
bcR | Right_To_Left, strong |
bcAL | Arabic_Letter, strong; becomes bcR under W3 |
bcEN | European_Number, weak |
bcES | European_Separator, weak |
bcET | European_Terminator, weak |
bcAN | Arabic_Number, weak |
bcCS | Common_Separator, weak |
bcNSM | Nonspacing_Mark, weak; takes the class of the preceding character under W1 |
bcBN | Boundary_Neutral, weak; removed by X9 |
bcB | Paragraph_Separator, neutral; terminates every embedding under X8 |
bcS | Segment_Separator, neutral |
bcWS | White_Space, neutral |
bcON | Other_Neutral, neutral; the class brackets carry into N0 |
bcLRE | Left_To_Right_Embedding, explicit formatting |
bcLRO | Left_To_Right_Override, explicit formatting |
bcRLE | Right_To_Left_Embedding, explicit formatting |
bcRLO | Right_To_Left_Override, explicit formatting |
bcPDF | Pop_Directional_Format, explicit formatting; pops an embedding under X7 |
bcLRI | Left_To_Right_Isolate, isolate initiator |
bcRLI | Right_To_Left_Isolate, isolate initiator |
bcFSI | First_Strong_Isolate, isolate initiator whose direction comes from the first strong character up to its matching bcPDI |
bcPDI | Pop_Directional_Isolate, terminates an isolate under X6a |
bcB separator inside Text resets every open embedding under X8, so text holding several paragraphs should be split before resolutionLength(Levels) equals Length(Text) and a surrogate pair occupies two entries carrying the same levelPdfBidiVisualOrder sees no gap in the level arrayPdfBidiVisualOrder returns the identity order when no odd level is present, because logical order is already visual order in that caseTPdfBidiLevels and TPdfBidiOrder are dynamic arrays owned by the caller and released by the usual reference counting