Qt-jambi-interest Archive, September 2007
Drawing unicode text using QPainter on Windows
Message 1 in thread
Hi,
I'm struggeling with drawing unicode text using the QPainter on Windows.
Drawing text like "ââââââââââââââââ" only shows ?????????? on the screen.
Here is some observations:
1. I can see the lines in Notepad meaning that the Windows (Vista) has a
unicode font.
2. I can see the lines in my IntelliJ Java text editor and they shows up
nice there.
3. I can paint the on linux without any problems.
4. Thre is no difference if I use QPainter or the AWT's Graphics object to
draw the text, same result....
Any ideas ?
-=BÃrge
Message 2 in thread
BÃrge Austvold wrote:
> Hi,
>
> I'm struggeling with drawing unicode text using the QPainter on Windows.
> Drawing text like "ââââââââââââââââ" only shows ?????????? on the screen.
>
> Here is some observations:
>
> 1. I can see the lines in Notepad meaning that the Windows (Vista) has a
> unicode font.
> 2. I can see the lines in my IntelliJ Java text editor and they shows up
> nice there.
> 3. I can paint the on linux without any problems.
> 4. Thre is no difference if I use QPainter or the AWT's Graphics object
> to draw the text, same result....
>
> Any ideas ?
Hi BÃrge,
There are a lot of dependencies in font drawing, unfortunatly. The
system may not have unicode fonts installed or they may be only partial,
e.g. that certain sections of the unicode tables are left as blank
glyphs.
Based on the input it gets Qt tries its best to find a unicode font that
can render the glyphs you specify, but this is sometimes out of our
control. I've attached a small showcase that draws the 2500-258f table
of the unicode in a few fonts which could be used to draw the "box"-es
you have above.
When putting unicode symbols in string litterals you need to make sure
that you have the right data in your strings and that you have right
encoding when compiling the source files. The default encoding used by
javac is the system encoding which is probably norwegian or some other
latin1 derivative. If you specify the string above directly in the
source file it will be interpreted as characters in the range 007f-00ff.
To properly address the unicode table 2500-258f you need to escape
each character using "\u2500" or construct chars like:
char c = 0x2500;
and work with those. Alternativly, you can use some funky input method
that can create the above string using unicode, save the file as utf-8
and compile the source file using
javac -encoding utf-8 SourceFile.java
Which should also preserve your unicode characters.
Hope this helps,
Gunnar
package com.trolltech.tests;
package com.trolltech.tests;
import com.trolltech.qt.gui.*;
public class Unicode extends QWidget {
protected void paintEvent(QPaintEvent e) {
QPainter p = new QPainter(this);
p.translate(0, 20);
renderUnicodeTable(p, new QFont("Times New Roman"));
p.translate(240, 0);
renderUnicodeTable(p, new QFont("Courier New"));
p.translate(240, 0);
renderUnicodeTable(p, new QFont("Tahoma"));
p.translate(240, 0);
renderUnicodeTable(p, new QFont("Comic Sans MS"));
}
private void renderUnicodeTable(QPainter p, QFont font) {
p.setFont(font);
p.drawText(0, 0, font.family());
for (int c=0; c<8; ++c) {
for (int r=0; r<16; ++r) {
char character = (char) (0x2500 | (c << 4) | r);
p.drawText(10 + c * font.pointSize() * 2, 10 + r * font.pointSize() * 2, "" + character);
}
}
}
public static void main(String args[]) {
QApplication.initialize(args);
Unicode code = new Unicode();
code.show();
QApplication.exec();
}
}
Message 3 in thread
Hi Gunnar,
The problem was that my java source file editor on windows saved the files
in some other format than UTF8. When forced IntelliJ to save as UTF8 the
problem was solved.
Thanks a lot
-=BÃrge
"Gunnar Sletta" <gunnar@xxxxxxxxxxxxx> wrote in message
news:46FA60FC.3030502@xxxxxxxxxxxxxxxx
> BÃrge Austvold wrote:
>> Hi,
>>
>> I'm struggeling with drawing unicode text using the QPainter on Windows.
>> Drawing text like "ââââââââââââââââ" only shows ?????????? on the screen.
>>
>> Here is some observations:
>>
>> 1. I can see the lines in Notepad meaning that the Windows (Vista) has a
>> unicode font.
>> 2. I can see the lines in my IntelliJ Java text editor and they shows up
>> nice there.
>> 3. I can paint the on linux without any problems.
>> 4. Thre is no difference if I use QPainter or the AWT's Graphics object
>> to draw the text, same result....
>>
>> Any ideas ?
>
> Hi BÃrge,
>
> There are a lot of dependencies in font drawing, unfortunatly. The
> system may not have unicode fonts installed or they may be only partial,
> e.g. that certain sections of the unicode tables are left as blank
> glyphs.
>
> Based on the input it gets Qt tries its best to find a unicode font that
> can render the glyphs you specify, but this is sometimes out of our
> control. I've attached a small showcase that draws the 2500-258f table
> of the unicode in a few fonts which could be used to draw the "box"-es
> you have above.
>
> When putting unicode symbols in string litterals you need to make sure
> that you have the right data in your strings and that you have right
> encoding when compiling the source files. The default encoding used by
> javac is the system encoding which is probably norwegian or some other
> latin1 derivative. If you specify the string above directly in the
> source file it will be interpreted as characters in the range 007f-00ff.
> To properly address the unicode table 2500-258f you need to escape
> each character using "\u2500" or construct chars like:
>
> char c = 0x2500;
>
> and work with those. Alternativly, you can use some funky input method
> that can create the above string using unicode, save the file as utf-8
> and compile the source file using
>
> javac -encoding utf-8 SourceFile.java
>
> Which should also preserve your unicode characters.
>
> Hope this helps,
> Gunnar
>
>
--------------------------------------------------------------------------------
> package com.trolltech.tests;
>
> import com.trolltech.qt.gui.*;
>
> public class Unicode extends QWidget {
>
> protected void paintEvent(QPaintEvent e) {
> QPainter p = new QPainter(this);
>
> p.translate(0, 20);
>
> renderUnicodeTable(p, new QFont("Times New Roman"));
>
> p.translate(240, 0);
> renderUnicodeTable(p, new QFont("Courier New"));
>
> p.translate(240, 0);
> renderUnicodeTable(p, new QFont("Tahoma"));
>
> p.translate(240, 0);
> renderUnicodeTable(p, new QFont("Comic Sans MS"));
>
> }
>
> private void renderUnicodeTable(QPainter p, QFont font) {
> p.setFont(font);
> p.drawText(0, 0, font.family());
> for (int c=0; c<8; ++c) {
> for (int r=0; r<16; ++r) {
> char character = (char) (0x2500 | (c << 4) | r);
> p.drawText(10 + c * font.pointSize() * 2, 10 + r *
> font.pointSize() * 2, "" + character);
> }
> }
> }
>
> public static void main(String args[]) {
> QApplication.initialize(args);
>
> Unicode code = new Unicode();
> code.show();
>
> QApplication.exec();
> }
>
> }
>
>