Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
New Contributor
DerZimmermann
Posts: 2
Registered: ‎04-21-2010
My Device: 8900

output utf8 encoded text with Graphics.DrawText

Hi!

 

I have a question concerning the output of utf8 encoded text.

 

I have some sample code here which should write some russian text to the screen but instead of the russian glyphs i get ???? characters only.

 

protected void paint(Graphics g) 
{
try
{
String utf8String = new String("Самые быстрые новости. Фото и видео дня.");
String str1 = new String(utf8String .getBytes(), "UTF-8");
g.setFont(FontFamily.forName("BBSansSerif").getFont(Font.BOLD,18);
g.drawText(str1,0,80);
}catch(.....)
}

 

I also tried the following code without sucess

 

protected void paint(Graphics g) 
{
try
{
String utf8String = new String("\u65e5\u672c\u8a9e\u6587\u5b57\u5217");
String str1 = new String(utf8String .getBytes(), "UTF-8");
g.setFont(FontFamily.forName("BBSansSerif").getFont(Font.BOLD,18);
g.drawText(str1,0,80);
}catch(.....)
}

Whereas this code here works properly and displays "Hello World" so I assume that the code itself is supposed to work, but the font does not support the glyphs I want to render.

 

protected void paint(Graphics g) 
{
try
{
String utf8String = new String("\u0048\u0065\u006C\u006C\u006F World");
String str1 = new String(utf8String .getBytes(), "UTF-8");
g.setFont(FontFamily.forName("BBSansSerif").getFont(Font.BOLD,18);
g.drawText(str1,0,80);
}catch(.....)
}

 

Can anyone here show me a way of how to display chinese japanese or russian text with the Graphics.drawText method.   I know that the my device supports russian glyphs because I can set the input method to russian, allowing me to type russian glyphs.(I am using IDE 4.6)

 

Any help is appreciated!

 

Best regards,

Stephan

 

 

 

Developer
Ted_Hopp
Posts: 1,305
Registered: ‎01-21-2009
My Device: Not Specified

Re: output utf8 encoded text with Graphics.DrawText

This line:

 

 

String str1 = new String(utf8String .getBytes(), "UTF-8");

 

is messing you up. getBytes() encodes characters as bytes using ISO-8859-1. You would need to use getBytes("UTF-8") to support non-8859-1 characters. (But then, the entire line is equivalent to String str1 = utf8String, so why bother?)

 

 

In fact, your first example could be simplified to:

 

 

protected void paint(Graphics g) 
{
   String utf8String = "Самые быстрые новости. Фото и видео дня.";
   g.setFont(FontFamily.forName("BBSansSerif").getFont(Font.BOLD,18);
   g.drawText(utf8String,0,80);
}

 

(However, I'm not sure what encoding the compiler assumes for source files. You're probably better off escaping the non-Latin characters as in your second and third examples.)

 




Solved? click "Accept as solution". Helpful? give kudos by clicking on the star.
New Contributor
DerZimmermann
Posts: 2
Registered: ‎04-21-2010
My Device: 8900

Re: output utf8 encoded text with Graphics.DrawText

Hi!

 

protected void paint(Graphics g) 
{
String utf8String = "Самые быстрые новости. Фото и видео дня.";
g.setFont(FontFamily.forName("BBSansSerif").getFon t(Font.BOLD,18);
g.drawText(utf8String,0,80);
}

Using this sample code I get the same result. It shows ? characters instead of russian glyphs.

 

Any idea?

Visitor
elcondor
Posts: 1
Registered: ‎10-15-2008
My Device: Not Specified

Re: output utf8 encoded text with Graphics.DrawText

I am having the same problems. What works for me is escaping the characters. Just like \n is for new line, you can use \u044f for the cyrillic small letter 'Ya'

Developer
Ted_Hopp
Posts: 1,305
Registered: ‎01-21-2009
My Device: Not Specified

Re: output utf8 encoded text with Graphics.DrawText

[ Edited ]

Try encoding the string using escape sequences:

String utf8String = "\u0421\u0430\u043c\u044b\u0435.";

instead of:

String utf8String = "Самые.";

I'm not at all sure that the compiler understands anything outside of ISO-8859-1 in the source code.

 

(Just like elcondor suggested while I was writing this. :smileyhappy:)




Solved? click "Accept as solution". Helpful? give kudos by clicking on the star.