0

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?

neydroydrec
  • 6,973
  • 9
  • 57
  • 89

1 Answers1

2

The QFontDatabase.families() function returns a list of fonts that support a specific Writing 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 enum for unicode script names and a scriptName function 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 QLabel using the unicodedata2 module from the answer above:

# -*- coding: utf8 -*-

import unicodedata2
from itertools import groupby
from PyQt4 import QtGui, QtCore

text = (u'چاچي चाची ćāćī (dim. of ćāćā, q.v.), '
        u's.f. A paternal aunt (=ćaćī, q.v.)')

markup = [
"""
<html>
<style type="text/css">
body {font-family: Sans Serif; font-size: 8pt}
span.arabic {font-family: ClearlyU Arabic; font-size: 18pt}
span.devanagari {font-family: ClearlyU Devanagari; font-size: 12pt}
</style>
<body>
"""
]

for script, group in groupby(text, unicodedata2.script):
    script = script.lower()
    chunk = ''.join(group)
    if script == 'common' or script == 'latin':
        markup.append(chunk)
    else:
        markup.append('<span class="%s">%s</span>' % (script, chunk))

markup.append(
"""
</body>
</html>
"""
)

class Window(QtGui.QWidget):
    def __init__(self):
        QtGui.QWidget.__init__(self)
        self.label = QtGui.QLabel(self)
        layout = QtGui.QVBoxLayout(self)
        layout.addWidget(self.label)
        self.label.setText(''.join(markup))

if __name__ == '__main__':

    import sys
    app = QtGui.QApplication(sys.argv)
    window = Window()
    window.show()
    sys.exit(app.exec_())
Community
  • 1
  • 1
ekhumoro
  • 115,249
  • 20
  • 229
  • 336
  • How to use this QFontDatabase? Will several scripts in a same string be displayed in a QLabel with their respective fonts and size thanks to this? – neydroydrec Aug 16 '12 at 20:18
  • @Benjamin. Only one font can be set at a time using `setFont`: the `QFontDatabase` just provides a way to find one that is appropriate for a given writing system. But it's not clear exactly what your asking for. It would help if you updated your question with a specific example, and also showed the code that you're currently using. – ekhumoro Aug 16 '12 at 20:40
  • @Benjamin. I've updated my answer with a work-around that may help to solve your problem. – ekhumoro Aug 18 '12 at 17:24