Skip to content

Warn that pre-shaping corrupts text under the Raqm layout engine - #9925

Open
Syamjith-NK wants to merge 1 commit into
python-pillow:mainfrom
Syamjith-NK:docs/raqm-preshaping-warning
Open

Warn that pre-shaping corrupts text under the Raqm layout engine#9925
Syamjith-NK wants to merge 1 commit into
python-pillow:mainfrom
Syamjith-NK:docs/raqm-preshaping-warning

Conversation

@Syamjith-NK

@Syamjith-NK Syamjith-NK commented Aug 29, 2026

Copy link
Copy Markdown

Closes #9907. Alternative to #9914

Running a string through arabic_reshaper + python-bidi before drawing is the standard advice in most tutorials for Arabic with Pillow, and it is correct for Layout.BASIC. Under Layout.RAQM it double-shapes the string: Pillow already shapes and reorders, so the text still renders but is wrong.

Because the engine is chosen by how Pillow was built, the same code is correct on one machine and wrong on another — which is why the pattern persists in projects that no longer need it, and why the docs seemed the right place for it.

Two details that made this feel worth documenting rather than leaving to user code:

  • Applications commonly store the reshaped string, so the corruption lands in the database rather than in a single render, and survives every later fix.
  • It cannot be undone by mapping the presentation forms back, because lam-alef collapses two characters into one glyph — a naive inverse turns السلام into السالم.

Documentation only, no code change. Placed in the Layout section rather than on ImageDraw.text because the behaviour is a property of the engine rather than of one drawing call.

As @akx noted on the issue, #9913 (linking FreeType against the bundled HarfBuzz in the wheels) would widen who is affected — pipelines that are correct today because their Pillow is not shaping would begin to double-shape — so this warning becomes more useful if that lands, not less.

Running a string through arabic_reshaper + python-bidi before drawing is the
standard advice in most tutorials for Arabic with Pillow, and it is correct for
Layout.BASIC. Under Layout.RAQM it double-shapes: Pillow already shapes and
reorders, so the text renders but is wrong.

Which engine is used depends on how Pillow was built, so the same code can be
correct on one machine and wrong on another - which is why the pattern persists
in projects that no longer need it.

Placed in the Layout section rather than on ImageDraw.text because the behaviour
is a property of the engine.

Refs python-pillow#9907
the reshaped string, so the corruption is written to the database and
outlives any later fix. It also cannot be undone by mapping the
presentation forms back, because lam-alef ligatures collapse two
characters into one glyph.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This paragraph doesn't seem optimal to me.

I start reading it and think

Damage? Pillow is doing damage to things beyond the image? What security problem is this?

but you're actually talking about reshaping that has occurred before Pillow enters the picture.

Are you sure we need to spell the problem out in this much detail for users? We've clearly said that they need to do. If they haven't things through with regards to a database, isn't that outside of Pillow's remit?

While I'm no expert on 'mapping the presentation forms back', that's not Pillow functionality. At some point, doesn't this documentation belong in arabic-reshaper or python-bidi instead?

@radarhere radarhere changed the title Docs: warn that pre-shaping corrupts text under the Raqm layout engine Warn that pre-shaping corrupts text under the Raqm layout engine Aug 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Warn that pre-applying arabic_reshaper + python-bidi corrupts text on a Raqm build

2 participants