I need to display several international scripts within a same string in a QLabel.
For instance the QLabel could display:
چاچي चाची ćāćī (dim. of ćāćā, q.v.), s.f. A paternal aunt (=ćaćī, q.v.)
The above string includes Latin script, Perso-Arabic script and Devanāgarī script (which belongs to he family of Indic scripts).
Each script requires different font families and sizes. For instance, the first word in the string is Urdu, and requires a font that is able to display ے. Moreover, I may want to use a certain type of calligraphy (Urdu traditionally uses “Nastaleeq” script). Besides, calligraphies may require an increases in font size to be readable (not all scripts fall into regular square shapes like Mandarin pictograms, if you have little idea of the diversity of international scripts, take a look at Omniglot.) In conclusion I want each script to be displayed with a particular font and size.
Currently I feed the QLabel with a complex HTML string which I compose bit by bit by specifying the font and size to use for specific sections of the string.
<font family="SomeFamilyforUrdu" size="10>اردو</font> <font family="SomeFamilyforDevanagari" size="8">हिन्दी</font> <font family="FontforLatin" size=5>English</font>
Qt4 also offers QLabel.setFont(QFont), but so far I did not see any means to specify font families and sizes according to script families.
Is it possible to set conditions to the QFont so it applies different families and sizes depending on the script it paints into the QLabel?
The
QFontDatabase.families()function returns a list of fonts that support a specificWriting System.EDIT
Fundamentally, your question seems to be about unicode and it’s support for assignment of script names to unicode code points. Theoretically, Qt could use the unicode database to determine the script of each text character that it renders and potentially use that information to apply a different format to it.
However, there doesn’t appear to be any obvious APIs providing support for this.
For instance, it would seem plausible that the QChar class might have an
enumfor unicode script names and ascriptNamefunction that would return the appropriate value from it for a particular unicode character. But there’s no such thing in either Qt4 or Qt5.So I’m going stick my neck out here, and guess that html/css is the only way to achieve what you want at this point in time.
UPDATE
It seems that python’s unicodedata module doesn’t have support for script names either.
However, this answer provides a workaround to that which could potentially allow a home-grown solution to be developed.
Here’s a demo script which auto-generates html for a
QLabelusing the unicodedata2 module from the answer above: