s-u / Cairo

R graphics device using cairo graphics library for creating high-quality output
12 stars 10 forks source link

Right to left fonts like Arabic and Persian are disjointed and reversed #23

Closed m-hoseini closed 2 years ago

m-hoseini commented 5 years ago

Hello,

When I save a plot containing rtl font as cairo_pdf, characters are disjointed and reversed. Example:

library(Cairo) cairo_pdf("test.pdf") plot(0, main="سلام علیکم") dev.off()

The PDF output looks like this. Instead of "سلام علیکم" it prints "م‌ک‌ی‌ل‌ع م‌ا‌ل‌س" image

s-u commented 2 years ago

The internal rendering in the cairographics library does not support anything other than LTR text, unfortunately.

I have now started as experimental branch which uses Harfbuzz and ICU to split text by language/script and renders them separately as glyphs: https://github.com/s-u/Cairo/tree/feature/harfbuzz

This seems to work:

Cairo::Cairo(dpi=100)
Cairo::CairoFonts("Tahoma")
plot(1, 1, xlab="سلام علیکم", ylab="ABC אבג DEF",ty="n")
text(1, 1 ,"قرأ Wikipedia™‎ طوال اليوم.",cex=2)
dev.off()

plot

Please note that cairo_pdf is NOT from the Cairo package but rather the built-in R functions. You have to use one of the Cairo functions instead (e.g. CairoPDF for PDF). Finally, the fonts used must support all the glyphs rendered and only few fonts do so some trial and error may be necessary.

Also note that this will only work if the system has all libraries necessary, you should see at installation time:

checking whether Harfbuzz layout is desired... auto
checking whether Harfbuzz + ICU works... yes

If it shows no then something is not available and you'd have to check config.log for details.

s-u commented 2 years ago

Should be now supported in Cairo 1.6-0 with Harfbuzz back-end.