Esmaill1 commited on
Commit ·
dd497ca
1
Parent(s): 1979f00
Fix Arabic text reversal by using python-bidi instead of manual reversal
Browse files- core/layout_engine.py +6 -8
core/layout_engine.py
CHANGED
|
@@ -155,18 +155,16 @@ def _arabic_font(size: int) -> ImageFont.FreeTypeFont:
|
|
| 155 |
def _reshape_arabic(text: str) -> str:
|
| 156 |
if not text: return ""
|
| 157 |
try:
|
| 158 |
-
# 1. Reshape the text to
|
| 159 |
reshaped_text = arabic_reshaper.reshape(text)
|
| 160 |
-
|
| 161 |
-
# 2.
|
| 162 |
-
#
|
| 163 |
-
#
|
| 164 |
-
|
| 165 |
-
return "".join(reversed(reshaped_text))
|
| 166 |
except Exception as e:
|
| 167 |
print(f"DEBUG: Arabic Shaping Error: {e}")
|
| 168 |
return text
|
| 169 |
-
|
| 170 |
def _resize_to_fit(img: Image.Image, max_w: int, max_h: int) -> Image.Image:
|
| 171 |
w, h = img.size
|
| 172 |
scale = min(max_w / w, max_h / h)
|
|
|
|
| 155 |
def _reshape_arabic(text: str) -> str:
|
| 156 |
if not text: return ""
|
| 157 |
try:
|
| 158 |
+
# 1. Reshape the text to handle ligatures and character connections
|
| 159 |
reshaped_text = arabic_reshaper.reshape(text)
|
| 160 |
+
|
| 161 |
+
# 2. Apply the Bidi algorithm to handle Right-to-Left reordering properly.
|
| 162 |
+
# Using get_display is the robust way to ensure text flows correctly
|
| 163 |
+
# in environments without native complex script support (like Pillow without Raqm).
|
| 164 |
+
return get_display(reshaped_text)
|
|
|
|
| 165 |
except Exception as e:
|
| 166 |
print(f"DEBUG: Arabic Shaping Error: {e}")
|
| 167 |
return text
|
|
|
|
| 168 |
def _resize_to_fit(img: Image.Image, max_w: int, max_h: int) -> Image.Image:
|
| 169 |
w, h = img.size
|
| 170 |
scale = min(max_w / w, max_h / h)
|