Warn that pre-shaping corrupts text under the Raqm layout engine - #9925
Warn that pre-shaping corrupts text under the Raqm layout engine#9925Syamjith-NK wants to merge 2 commits into
Conversation
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. |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Fair, and I agree it is out of scope - dropped that paragraph. The warning now stops at the run-time check.
For what it is worth on where the rest belongs: the reshaping half is filed upstream at mpcabd/python-arabic-reshaper#102. What I think does belong here is only what is left - that under Layout.RAQM Pillow shapes the text itself, and that the engine is decided at build time, so the same code can be correct on one machine and wrong on another.
Closes #9907. Alternative to #9914
Running a string through
arabic_reshaper+python-bidibefore drawing is the standard advice in most tutorials for Arabic with Pillow, and it is correct forLayout.BASIC. UnderLayout.RAQMit 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:
السلامintoالسالم.Documentation only, no code change. Placed in the
Layoutsection rather than onImageDraw.textbecause 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.