text alignment #3022
Replies: 8 comments 18 replies
|
This is a Discussions item. So transferring it for further communication. |
|
What method to write text did you use? |
|
RTL is not working when we use then as per RTL it show have been but it actually writes |
True, the algorithm is a bit more complex, because you first have to take back the word splitting for any two adjacent RTL words. Hope, the following does this: def is_rtl(word): # check if a word contains characters from an RTL alphabet
r0 = range(0x0590, 0x0780) # Hebrew / Arabic / Persian Unicode range
if any([ord(c) in r0 for c in word]):
return True
return False
words = text.split(" ")
new_words = [words[0]]
for w in words:
w0 = new_words[-1] # previous word
if is_rtl(w0) and is_rtl(w): # both words are RTL
new_words[-1] = w0 + " " + w # replace
continue
new_words.append(w)
rtl_text = " ".join(reversed(new_words)) |
|
A hint from the MuPDF colleagues: import fitz
r1 = (100, 100, 120, 120) # very small rectangles of equal size
r2 = (100, 130, 120, 150)
text1 = "short treatment" # some word fill not fit in rect width
text2 = "short treat­ment" # offer optional word break via soft hyphen
doc = fitz.open()
page = doc.new_page()
page.insert_htmlbox(r1, text1, css="* {overflow-wrap: break-word;}")
page.insert_htmlbox(r2, text2)
page.draw_rect(r1, color=(1, 0, 0))
page.draw_rect(r2, color=(1, 0, 0))
doc.save(__file__.replace(".py", ".pdf")) |
|
Goodnews I think: The MuPDF team has developed a solution for the issue discussed here. With one of the next PyMuPDF versions, you will be able to choose between 3 different ways to solve this: import fitz
RED = fitz.pdfcolor["red"]
r1 = (100, 100, 120, 120)
r2 = (100, 130, 120, 150)
r3 = (100, 160, 120, 180)
text1 = "short treatment"
text2 = "short treat­ment"
text3 = text1
doc = fitz.open()
page = doc.new_page()
page.insert_htmlbox(r1, text1, css="* {overflow-wrap: break-word;}")
page.insert_htmlbox(r2, text2)
page.insert_htmlbox(r3, text3) # NEW behavior
page.draw_rect(r1, color=RED)
page.draw_rect(r2, color=RED)
page.draw_rect(r3, color=RED)
doc.save(__file__.replace(".py", ".pdf")) |
|
The Arabic language issue has been resolved. Are there any updates in the latest version? |





Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Hi, I have been working with PyMuPDF to write translated text in Arabic to the pdf but the alignment of the text needs to be to the right of the bbox. I have tried many ways to do it but nothing seems to work . Please help me to understand why is the text not getting right aligned even after using right-align and matrix. Even the paragraph text goes out of the bbox or overlaps sometimes. The red outline in test_ar.pdf shows the bbox to which the text need to be right aligned.
test.pdf
test_ar.pdf
All reactions