Skip to content

Support page number alias substitution with bidi / RTL text shaping #1925

Description

@andersonhc

PR #1894 added initial support for page-count alias substitution ({nb} / alias_nb_pages()) when text shaping is enabled.

There is still a remaining compatibility gap with bidirectional and RTL text. When the alias appears inside text processed by the bidi pipeline, it can be reordered as normal text before alias substitution runs. For example, {nb} may become }nb{, so the alias is no longer recognized and is rendered literally instead of being replaced by the total page count.

Example:

from fpdf import FPDF

pdf = FPDF()
pdf.add_font("SBL_Hbrw", fname="test/text_shaping/SBL_Hbrw.ttf")
pdf.set_font("SBL_Hbrw", size=18)
pdf.set_text_shaping(True, direction="rtl")

pdf.add_page()
pdf.cell(text="אבג {nb} דהו")

pdf.output("rtl-nb-alias.pdf")

The {nb} alias should be replaced by the total page count, consistently with LTR shaped text and regular non-shaped text, but right now the alias can be reordered by bidi processing and emitted literally, so the generated PDF may contain the alias text instead of the page count.

We likely need to detect page-count aliases before bidi reordering and preserve them as special atomic / unbreakable fragments. This may require changes in bidi.py or in the preprocessing path that creates bidi fragments, so aliases are not treated as ordinary reorderable text.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions