mpcabd / python-arabic-reshaper

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

problem with text size after reshaping. #43

Closed ahmed4end closed 4 years ago

ahmed4end commented 4 years ago

for many fonts there are many words that when reshaping it appears to have wrong text size . when i try to get the text size for most of words i get it wrong as shown: image the bounding box shows the text size found by "[pillow object] draw.textsize(text, font)" there is extra space to the right that make it hard to use the reshaped result in any work !

mpcabd commented 4 years ago

Please post your code, the font you're using, and the text you're reshaping, so I can help you further.

ahmed4end commented 4 years ago

@mpcabd `from PIL import Image, ImageDraw, ImageFont from arabic_reshaper import ArabicReshaper from bidi.algorithm import get_display import os

size = (300,200) image = Image.new(mode = "RGBA" , size=size , color="white" ) draw = ImageDraw.Draw(image)

fnt = ImageFont.truetype(os.path.join(os.getcwd()+ "\TahaBold.ttf") , 100) text = "تجربة " configuration = { 'use_unshaped_instead_of_isolated': True, } reshaper = ArabicReshaper(configuration) reshaped_text = reshaper.reshape(text) bidi_text = get_display(reshaped_text)

text_size = draw.textsize(text, fnt) #

draw.text((size[0]-text_size[0],0), bidi_text, "black", font=fnt) draw.rectangle([size[0]-3, 0, size[0]-text_size[0], text_size[1]], fill=None, outline="black", width=3)

image.save(f"image.png");os.startfile("image.png")`

and the font is Taha bold and here is a link to download it:

https://gofile.io/d/w79bXZ

ahmed4end commented 4 years ago

oh my bad, i calculated the text size for the raw text instead of the reshaped one and that made the problem for me, never mind.