fix(rtl): les textes centrs en persan s'affichaient du mauvais cote

- Word: sous w:bidi, w:jc est logique (left=dbut=droite visuelle,
  right=fin=gauche visuelle) ; forcer jc=right alignait donc les
  paragraphes a gauche et crasait le centrage hrit du style.
  Dsormais le RTL pose w:bidi et w:rtl sans jamais toucher w:jc,
  comme le fait Word lui-mme pour un document RTL.
- PowerPoint: algn n'est plus crit quand le paragraphe n'en dfinit
  pas, afin de respecter l'alignement hrit du masque (titres centrs) ;
  algn=l explicite reste converti en r (algn est visuel en DrawingML).
- tests: centrage par style Word prserv, aucun w:jic crit, algn
  absent non cras côté PowerPoint
This commit is contained in:
2026-09-01 20:45:05 +02:00
parent f22f645fab
commit ffbd85a7b6
3 changed files with 70 additions and 19 deletions

View File

@@ -73,9 +73,15 @@ def _set_pptx_paragraph_rtl(paragraph) -> None:
Enable RTL mode on a PowerPoint paragraph.
Sets rtl="1" on the <a:pPr> element, which controls the paragraph
text direction. The horizontal alignment (algn) is only forced to
"r" when the paragraph has no explicit alignment or is aligned
"l" (left) — a centered or justified title keeps its alignment.
text direction; with rtl="1" and no explicit algn, PowerPoint
renders the paragraph from its start edge — the right edge.
algn is only rewritten when the paragraph explicitly says "l": in
DrawingML algn is visual (not logical like Word's w:jc), so an
explicit left would keep RTL text on the left edge. When algn is
absent, the inherited layout/master alignment (often centered
titles) must keep playing — writing "r" here would override it,
which is the centered-text mis-rendering reported for Persian.
"""
p_elem = paragraph._p
tag_pPr = f"{{{_NS_A}}}pPr"
@@ -84,7 +90,7 @@ def _set_pptx_paragraph_rtl(paragraph) -> None:
pPr = etree.Element(tag_pPr)
p_elem.insert(0, pPr)
pPr.set("rtl", "1")
if pPr.get("algn") in (None, "", "l"):
if pPr.get("algn") == "l":
pPr.set("algn", "r")

View File

@@ -153,8 +153,6 @@ _PPR_BIDI_SUCCESSORS = (
qn("w:sectPr"),
qn("w:pPrChange"),
)
# Elements that must come AFTER w:jc inside w:pPr (w:jc itself excluded).
_PPR_JC_SUCCESSORS = _PPR_BIDI_SUCCESSORS[_PPR_BIDI_SUCCESSORS.index(qn("w:jc")) + 1 :]
# Elements that must come AFTER w:rtl inside w:rPr.
_RPR_RTL_SUCCESSORS = (
qn("w:cs"),
@@ -205,11 +203,18 @@ def _set_paragraph_rtl(paragraph: Paragraph) -> None:
Sets:
- w:pPr/w:bidi → paragraph text direction = RTL
- w:pPr/w:jc → mirrored alignment (left→right), ONLY when the
paragraph has no explicit alignment — centered/justified titles
must not be forced right-aligned.
- w:rPr/w:rtl → run-level RTL marker for each run
w:jc is deliberately left untouched. Under w:bidi, Word interprets
w:jc LOGICALLY (start/end), not visually: "left" means the start —
the RIGHT edge for an RTL paragraph — and "right" means the end, the
LEFT edge. Forcing jc="right" therefore aligns paragraphs to the
visual left and overrides style-level centering, which is exactly
the mis-rendering reported for centered Persian text. With bidi
alone: unaligned paragraphs start at the right edge, "left" resolves
to the right edge, and center/justify stay centered/justified — the
same markup Word itself writes for RTL documents.
Every element is inserted at its schema position inside w:pPr/w:rPr
(OOXML requires a fixed child order; appended-at-the-end elements
make Word flag the file for repair).
@@ -219,14 +224,6 @@ def _set_paragraph_rtl(paragraph: Paragraph) -> None:
if pPr.find(qn("w:bidi")) is None:
_insert_ordered(pPr, OxmlElement("w:bidi"), _PPR_BIDI_SUCCESSORS)
jc = pPr.find(qn("w:jc"))
explicit_alignment = jc is not None and jc.get(qn("w:val")) not in (None, "", "left")
if not explicit_alignment:
if jc is None:
jc = OxmlElement("w:jc")
_insert_ordered(pPr, jc, _PPR_JC_SUCCESSORS)
jc.set(qn("w:val"), "right")
for run in paragraph.runs:
rPr = run._r.get_or_add_rPr()
if rPr.find(qn("w:rtl")) is None: