mpcabd / python-arabic-reshaper

Reconstruct Arabic sentences to be used in applications that don't support Arabic
MIT License
395 stars 81 forks source link

Issue with Diacritics/Harakat positions and placement and tested on 6 fonts #95

Open bossxero opened 1 month ago

bossxero commented 1 month ago

Hello,

I have been trying to produce arabic text using the python reshaper but was unable to get the diacritics to show well in any of the text generated. I have tested with different fonts but it has not worked properly so far. I would be grateful if I could get some help from the community to resolve this issue. Please find the test code i have used and the generated image. I am using PIL to place the text on the image. As we can see in the image below, the diacritics often overlaps the letters or other symbols. The Shift_Harakat_Position did not solve the issue.

List of fonts which I tested are Amiri, Arabic Typesetting, Arial,NotoSansArabic, Traditional Arabic and Zekr Quran fonts.zip

textgeneration


# -*- coding: utf-8 -*-

from __future__ import unicode_literals

import os
import unittest
import sys
from arabic_reshaper import ArabicReshaper
from bidi.algorithm import get_display
from PIL import Image, ImageFont, ImageDraw, ImageEnhance

all_fonts = [i for i in os.listdir() if (i[i.find(".") + 1:]).lower() in ["ttf"]]

text_to_be_reshaped = u"هَذِهِ الأَفْكارِ المَغْلوطَةِ"

configuration = {
    'delete_harakat': False,
    'delete_tatweel': False,
    'support_ligatures': True,
    'support_zwj': True,
    'shift_harakat_position': False,
    'use_unshaped_instead_of_isolated': True
}

reshaper      = ArabicReshaper(configuration=configuration)
reshaped_text = reshaper.reshape(text_to_be_reshaped)       # Arabic Reshaper
reshaped_text = get_display(reshaped_text)                  # Bidi

img        = Image.new('RGBA', (1000,1000), "black")
draw_layer = ImageDraw.Draw(img)

text_h_sum   = 50
line_spacing = 16

for i, font in enumerate(all_fonts) :

    font = ImageFont.truetype(font, 50)

    space_taken_by_text = draw_layer.textbbox(xy = (0, 0), text = reshaped_text, font = font)

    text_w      = space_taken_by_text[0] + space_taken_by_text[2]
    text_h      = space_taken_by_text[1] + space_taken_by_text[3]

    if i != 0 :
        text_h_sum = text_h_sum + (text_h + (i * line_spacing))

    draw_layer.text(
        xy   = (50, text_h_sum),
        text = reshaped_text,
        fill = (255, 255, 255, 128),
        font = font
        )
img.show()
dotping-me commented 1 month ago

I wrote code to convert a string of Arabic characters into an image : https://github.com/dotping-me/arabic-word-to-image