We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpOpenGL and 3D Libraries › UTF-8 display in OPENGL mode
Page Index Toggle Pages: 1
UTF-8 display in OPENGL mode (Read 2311 times)
UTF-8 display in OPENGL mode
Apr 16th, 2007, 11:41am
 
Hi all,

I've been trying to display some Chinese text in the OPENGL mode for days without any success. The sketch is as below, the required font and text are in the link below.

http://www.hamletbon.net/temp/fontDisplay/laodFile.zip
*Beware of the fileSize (>9MB).


import processing.opengl.*;

String tempString= "";
//byte linesByte[]= loadBytes("text.txt");
//String temp[]= loadStrings("text.txt");
byte linesByte[] = new byte[80];//loadBytes("text.txt");
String lines="";
byte[] tempByte= new byte[80];
String castedString="";

PFont sysFont= createFont("sung", 18);

void mousePressed(){
 println("loadbye");
 linesByte=loadBytes("text.txt");
 try{
   tempByte=tempString.getBytes("UTF-8");
//    println(tempByte);
   castedString= new String(tempByte, "UTF-8");
   lines=new String(linesByte, "UTF-8");
 }catch (Exception e){
   println("error");
 }
}

void setup(){
 size(screen.width/2, screen.height/10*8, P3D);
   linesByte=loadBytes("text.txt");
 try{
   tempByte=tempString.getBytes("UTF-8");
   castedString= new String(tempByte, "UTF-8");
   lines=new String(linesByte, "UTF-8");
 }catch (Exception e){
   println("error");
 }
 
 //lines of string
 String wholeString=lines;
 Vector singleLine=new Vector();
 int lineCount=0;
 StringBuffer sb=new StringBuffer();
 for (int i=0; i<wholeString.length(); i++){
   
if (wholeString.charAt(i)!='\n'){


sb.append(wholeString.charAt(i));

}else{
         singleLine.add(sb.toString());
         sb=new StringBuffer();
         lineCount++;
     }
 }
 for (int i=0; i<singleLine.size(); i++){
   println("line "+i+": "+(String)singleLine.get(i));
 }
}

void draw(){
 background(0);
 textFont(sysFont, 18);
 fill(255, 0, 0);
 text(lines, 20, 20);
}


Would that be the problem of the jogl package?... Any idea?


Re: UTF-8 display in OPENGL mode
Reply #1 - Oct 12th, 2008, 5:18pm
 
hi hamletbon,

did you find any solution to this problem? i am facing it, too.

i will probably switch to pure jogl Sad
Re: UTF-8 display in OPENGL mode
Reply #2 - Oct 12th, 2008, 5:32pm
 
Finally I found a method, below is the class I developed for displaying UTF-8/Unicode text in opengl, it may slow down the sketch a little bit but it functions well for few of my projects

class ChineseFont{
//class to create Chinese font to avoid accumlation of new internal fonts for each char.
String characters="";
int fontSize=18;
PFont refFont;
String nameOfFont="";
ChineseFont(String _nameOfFont, int _fontSize){
  this.nameOfFont=_nameOfFont;
  this.fontSize=_fontSize;
  refFont=createFont(this.nameOfFont, this.fontSize, true, new char[]{'a'});
}

PFont getFont(){
  return this.refFont;
}
void setFontSize(int _fontSize){
  this.fontSize=_fontSize;
  this.refreshPFont();
}
void addString(String _newCharToAdd){//refresh character list to be added
  String tempCharacters=this.characters;
  println("currentString Length: "+this.characters.length());
  for (int i=0; i<_newCharToAdd.length(); i++){
    boolean hit=false;
    for (int j=0; j<tempCharacters.length(); j++){
      if (_newCharToAdd.charAt(i)==(tempCharacters.charAt(j)))hit=true;
    }
    if (hit==false){
      //println("adding char to refFont: "+_newCharToAdd.charAt(i));
      tempCharacters+=_newCharToAdd.charAt(i);
    }
  }
  this.characters=tempCharacters;
  refreshPFont();
  println("[ChineseFont.addString returns]---current Characters= "+this.characters);
}

void refreshPFont(){
  this.refFont=null;
  char[] tempCharArray=new char[this.characters.length()];
  for (int i=0; i<this.characters.length(); i++){
      tempCharArray[i]=this.characters.charAt(i);
  }
    this.refFont=createFont(this.nameOfFont, this.fontSize, true, tempCharArray);  
}
}

Basically, you just need to initialize the text with the font type on the machine, and use addString method to add the string before displaying it, and call textFont(ChineseFont.getFont()) to get the reference PFont in the class.

Hope it will be helpful for all the projects in different languages~

Cheers~
Re: UTF-8 display in OPENGL mode
Reply #3 - Nov 15th, 2008, 10:46am
 
I am sorry, but I still don't know how to use it. Give me an example? Thank you.
Re: UTF-8 display in OPENGL mode
Reply #4 - Nov 15th, 2008, 4:04pm
 
I don't understand what the problem is, you shouldn't need any of that. loadStrings() already uses UTF-8 to load files, there's no need to do all that UTF-8 parsing and messing with loadBytes().

And to create a font that includes Chinese characters use Tools > Create Font and select "All Characters". Or use the createFont() method inside your code if you're packaging a .ttf/.otf file.
Re: UTF-8 display in OPENGL mode
Reply #5 - Nov 15th, 2008, 4:29pm
 
Hi Fry,

Creating all characters for Chinese font will cause java heap space error, in fact when I created Chinese font using the create font method, the whole processing froze. That's why I have to create the fonts dynamically, only for those character I'm actually using.

Btw, this problem only occurs in opengl mode. And it didn't work using the "useNativeFont" enabled either.

And I haven't tried it after version 0135.

Please let me know if there's a better way to do that, that'd be great~
Re: UTF-8 display in OPENGL mode
Reply #6 - Nov 15th, 2008, 9:52pm
 
Have you tried specifying the characters you need via the charset parameter in createFont()?
http://processing.org/reference/createFont_.html

Please don't use release 0135, it's nearly a year old and there have been many dozens of bugs fixed since then.
Re: UTF-8 display in OPENGL mode
Reply #7 - Nov 16th, 2008, 3:10am
 
That's preciously why I written the ChienseText class.

I used the charset parameter to create the font every-time when I need to use a specific character. And actually all the functions inside that class aimed at reducing the actual memory use when using huge number of Chinese Character.


Page Index Toggle Pages: 1