-
Notifications
You must be signed in to change notification settings - Fork 132
Added fixes to properly align paragraphs when loading RTL documents #2352
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
caio-pizzol
merged 9 commits into
superdoc-dev:main
from
claudiu-ior:rtl-initial-load-support
Mar 23, 2026
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
36824e4
Added fixes to properly align paragraphs when loading RTL documents
claudiu-ior 9795ed1
P2 Mirror tab decorations with RTL segment positioning.
claudiu-ior 5bca37e
addressing pr feedback
claudiu-ior d04acad
New module: features/rtl-paragraph/ following the same pattern as par…
claudiu-ior d15ac88
Merge branch 'main' into rtl-initial-load-support
claudiu-ior ccc6dc5
fix: fixed tests
claudiu-ior bac46a1
Update packages/layout-engine/painters/dom/src/features/rtl-paragraph…
claudiu-ior 58ab84e
Update packages/layout-engine/painters/dom/src/features/rtl-paragraph…
claudiu-ior 386b95e
fix: removed unused import
claudiu-ior File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
packages/layout-engine/painters/dom/src/features/rtl-paragraph/index.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| /** | ||
| * RTL Paragraph — rendering feature module | ||
| * | ||
| * Centralises all right-to-left paragraph logic used by DomPainter: | ||
| * - Detecting whether a paragraph is RTL | ||
| * - Applying dir="rtl" and the correct text-align to an element | ||
| * - Resolving text-align for RTL vs LTR (justify → right/left) | ||
| * - Deciding whether segment-based (absolute) positioning is safe | ||
| * | ||
| * @ooxml w:pPr/w:bidi — paragraph bidirectional flag | ||
| * @ooxml w:rPr/w:rtl — run-level right-to-left flag | ||
| * @spec ECMA-376 §17.3.1.1 (bidi), §17.3.2.30 (rtl) | ||
| */ | ||
|
|
||
| export { applyRtlStyles, shouldUseSegmentPositioning } from './rtl-styles.js'; |
67 changes: 67 additions & 0 deletions
67
packages/layout-engine/painters/dom/src/features/rtl-paragraph/rtl-styles.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,67 @@ | ||||||||||
| /** | ||||||||||
| * RTL paragraph style helpers for DomPainter. | ||||||||||
| * | ||||||||||
| * All RTL-aware rendering decisions live here so the main renderer | ||||||||||
| * doesn't need to re-derive direction in multiple places. | ||||||||||
| * | ||||||||||
| * @ooxml w:pPr/w:bidi — paragraph bidirectional flag | ||||||||||
| * @spec ECMA-376 §17.3.1.1 (bidi) | ||||||||||
| */ | ||||||||||
| import type { ParagraphAttrs } from '@superdoc/contracts'; | ||||||||||
|
|
||||||||||
| /** | ||||||||||
| * Returns true when the paragraph attributes indicate right-to-left direction. | ||||||||||
| * Checks both the `direction` string and the legacy `rtl` boolean flag. | ||||||||||
| */ | ||||||||||
| export const isRtlParagraph = (attrs: ParagraphAttrs | undefined): boolean => | ||||||||||
| attrs?.direction === 'rtl' || attrs?.rtl === true; | ||||||||||
|
|
||||||||||
| /** | ||||||||||
| * Compute the effective CSS text-align for a paragraph. | ||||||||||
| * | ||||||||||
| * DomPainter handles justify via per-line word-spacing, so 'justify' | ||||||||||
| * becomes 'left' (LTR) or 'right' (RTL) to align the last line correctly. | ||||||||||
| * When no explicit alignment is set the default follows the paragraph direction. | ||||||||||
| */ | ||||||||||
| export const resolveTextAlign = (alignment: ParagraphAttrs['alignment'], isRtl: boolean): string => { | ||||||||||
| switch (alignment) { | ||||||||||
| case 'center': | ||||||||||
| case 'right': | ||||||||||
| case 'left': | ||||||||||
| return alignment; | ||||||||||
| case 'justify': | ||||||||||
| return isRtl ? 'right' : 'left'; | ||||||||||
| default: | ||||||||||
| case 'justify': | ||||||||||
| default: | ||||||||||
| return isRtl ? 'right' : 'left'; | ||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. these two cases do the same thing — combine them?
Suggested change
claudiu-ior marked this conversation as resolved.
|
||||||||||
| } | ||||||||||
| }; | ||||||||||
|
|
||||||||||
| /** | ||||||||||
| * Apply `dir` and `text-align` to an element based on paragraph attributes. | ||||||||||
| * Used by both `renderLine` (line elements) and `applyParagraphBlockStyles` | ||||||||||
| * (fragment wrappers) so the logic stays in one place. | ||||||||||
| */ | ||||||||||
| export const applyRtlStyles = (element: HTMLElement, attrs: ParagraphAttrs | undefined): boolean => { | ||||||||||
| const rtl = isRtlParagraph(attrs); | ||||||||||
| if (rtl) { | ||||||||||
| element.setAttribute('dir', 'rtl'); | ||||||||||
| element.style.direction = 'rtl'; | ||||||||||
| } | ||||||||||
| element.style.textAlign = resolveTextAlign(attrs?.alignment, rtl); | ||||||||||
| return rtl; | ||||||||||
| }; | ||||||||||
|
|
||||||||||
| /** | ||||||||||
| * Whether the renderer should use absolute-positioned segment layout for a line. | ||||||||||
| * | ||||||||||
| * Returns false for RTL paragraphs: the layout engine computes tab X positions | ||||||||||
| * in LTR order, so for RTL we fall through to inline-flow rendering where the | ||||||||||
| * browser's native bidi algorithm handles tab positioning via dir="rtl". | ||||||||||
| */ | ||||||||||
| export const shouldUseSegmentPositioning = ( | ||||||||||
| hasExplicitPositioning: boolean, | ||||||||||
| hasSegments: boolean, | ||||||||||
| isRtl: boolean, | ||||||||||
| ): boolean => hasExplicitPositioning && hasSegments && !isRtl; | ||||||||||
|
claudiu-ior marked this conversation as resolved.
|
||||||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.