Skip to content

Commit 5553aee

Browse files
authored
Bidi text - Add option to force paragraph direction (#1114)
* bidi paragraph direction * add changelog line
1 parent 1bd4548 commit 5553aee

11 files changed

+156
-34
lines changed

CHANGELOG.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ This can also be enabled programmatically with `warnings.simplefilter('default',
1818

1919
## [2.7.9] - Not released yet
2020
### Added
21-
21+
* support for overriding paragraph direction on bidirectional text
2222
### Fixed
2323

2424
### Changed

docs/TextShaping.md

+25-1
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,28 @@ Direction can be `ltr` (left to right) or `rtl` (right to left). The `ttb` (top
8585

8686
[Valid OpenType script tags](https://learn.microsoft.com/en-us/typography/opentype/spec/scripttags)
8787

88-
[Valid OpenType language codes](https://learn.microsoft.com/en-us/typography/opentype/spec/languagetags)
88+
[Valid OpenType language codes](https://learn.microsoft.com/en-us/typography/opentype/spec/languagetags)
89+
90+
# Bidirectional Text #
91+
92+
_New in [:octicons-tag-24: 2.7.8](https://github.com/py-pdf/fpdf2/blob/master/CHANGELOG.md)_
93+
94+
Bidirectional text refers to text containing both left-to-right (LTR) and right-to-left (RTL) language scripts. Languages such as Arabic, Hebrew, and Persian are written from right to left, whereas languages like English, Spanish, and French are written from left to right. The Unicode Bidirectional Algorithm is a set of rules defined by the Unicode Consortium to properly display mixed-directional text. This algorithm ensures that characters are shown in their correct order, preserving the logical sequence of the text.
95+
96+
## Unicode Bidirectional Algorithm ##
97+
The Unicode Bidirectional Algorithm, often abbreviated as the *Bidi* Algorithm, is essential for displaying text containing both RTL and LTR scripts. It determines the directionality of characters and arranges them in a visually correct order. This algorithm takes into account the inherent directionality of characters (such as those in Arabic or Hebrew being inherently RTL) and the surrounding context to decide how text should be displayed.
98+
99+
## Paragraph direction ##
100+
101+
![](bidi_paragraph.svg)
102+
103+
## Bidirectional text in fpdf2 ##
104+
fpdf2 will automatically apply the unicode bidirectional algorithm if text shaping is enabled.
105+
106+
If no `direction` parameter is provided - or `direction` is `None` - paragraph direction will be set according to the first directional character present on the text.
107+
108+
If there is a need to explicitly set the direction of a paragraph, regardless of the content, you can force the paragraph direction to either RTL or LTR.
109+
110+
```python
111+
fpdf.set_text_shaping(use_shaping_engine=True, direction="rtl")
112+
```

docs/bidi_paragraph.svg

+1
Loading

0 commit comments

Comments
 (0)