PDA

View Full Version : Getting superscript of arbitrary number to use in a QString



Durkin
12th May 2013, 22:08
Hello,

I have some QGraphicsItems onto which text is drawn from a QString, as well as some buttons whose title again is set from a QString. I 'd like that to include superscripts. Hard-coding it like this


QString text("σ²");

works, but I 'd like to superscript an arbitrary number. Ie say there was such a function Qsup then ideally I 'd do something like this:


QString text("σ" + Qsup(x));

What would be the best approach? Is it possible to use one of qt formats that employ html tags and convert that to a QString? If so a short example would be appreciated.

As an aside, it 'd be ideal to load the text data from a file. But I have trouble loading superscript characters from a plain *.txt file (which is what I 'm using now), no matter what encoding I choose. Any hints about that would also be great.

Thanks.

wysota
12th May 2013, 23:13
Not every digit contains a superscript version in Unicode so in a general case you can't do it like that.

ChrisW67
12th May 2013, 23:29
Unicode includes range of code points (http://www.unicode.org/charts/PDF/U2070.pdf) that should render as superscripted or subscripted 0-9 glyphs. Font support for these is patchy AFAICT. You should be able to trivially write a function to map normal Unicode digits 0-9 (U+0030-39) to their super- (U+2070-79) or subscripted (U+2080-89) equivalent.

This is not in any way the same thing as using HTML <sup> or <sub> markup, which causes normal glyphs to be rendered above or below the current baseline in HTML renderers. HTML renderers do not try to convert "&sigma;<sup>7</sup>" into the two Unicode characters U+03C3 and U+2077.


You need to know what encoding you are using your text file because generally you cannot infer it. It might be UTF-8, it might be ISO8859-1 or Windows 1252, it might be UTF16 (big or little endian)...

Durkin
13th May 2013, 09:53
Thanks. I wrote a small function myself then, that returns a QString with the superscripts 0-9. Though this is in the code -does qt have a way of converting any of the representations found here to the corresponding character?
-> http://www.fileformat.info/info/unicode/char/2075/index.htm

Anyhow, Arial seems to render them all fine for now.

ChrisW67
13th May 2013, 21:29
Yes. Qt can convert from several of those encodings...

#include <QtGui>

int main(int argc, char **argv)
{
QApplication app(argc, argv);

// UTF-8
const char s[] = {
0x78, // Lower case X
0xE2, 0x81, 0xB0, // superscript zero
0xC2, 0xB9, // superscript one
0xC2, 0xB2, // superscript two
0xE2, 0x82, 0x83, // subscript three
0xE2, 0x82, 0x84, // subscript four
0x00 // NUL terminator
};
QString str = QString::fromUtf8(s);

// UTF-16, BOM then same characters as above
const ushort s16[] = {
0xFEFF, // byte order mark
0x0078,
0x2070,
0x00B9,
0x00B2,
0x2083,
0x2084,
0x0
};
QString str16 = QString::fromUtf16(s16);

// str == str16 is true

QFont font("Arial", 36);
QLabel l(str16);
l.setFont(font);
l.show();

return app.exec();
}


You will probably see that the rendering of the superscript 1, 2 and 3 (U+00Bx) are different from 0 and 4-9 (U+207x).