Esmaill1 commited on
Commit
dd497ca
·
1 Parent(s): 1979f00

Fix Arabic text reversal by using python-bidi instead of manual reversal

Browse files
Files changed (1) hide show
  1. 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 get connected glyphs
159
  reshaped_text = arabic_reshaper.reshape(text)
160
-
161
- # 2. Manually reverse the string characters.
162
- # In a pure LTR engine like Pillow (without Raqm), the reshaped glyphs
163
- # must be provided in reverse order so that the first logical character
164
- # ends up on the right side of the word visually.
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)