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 & HelpSyntax Questions › 2 questions about "text" - slow, buggy
Page Index Toggle Pages: 1
2 questions about "text" - slow, buggy? (Read 488 times)
2 questions about "text" - slow, buggy?
Oct 4th, 2007, 7:00pm
 
I have posted a small example applet online that illustrates 2 questions I have about drawing "text".
http://dxjones.com/processing/textimage/

(1) Text drawing seems to be extremely slow, relative to other drawing functions, even though I am running on Mac OSX 10.4.10, 2.5 GHz G5, Processing 0125.  Toggling text drawing on/off changes my frame rate by a huge amount.  I wondered whether calling "textFont(font)" every frame was doing some expensive setup, while "text("mesg",x,y)" might be relatively fast, but if a sketch uses more than 2 fonts, you need to switch between them, so calling textFont() seems unavoidable.

(2) As a workaround, I tried drawing the text message into an image, using createGraphics(), but there seems to be a bug in the steps I have followed to draw the text.  The bounding boxes of each letter are drawn, but not the letters themselves.  Pasting the pre-rendered text images onto the screen is extremely fast, so I feel tantalizingly close to a solution, but there is EITHER a bug in Processing's PGraphics/Text (which I hope is unlikely) OR some stupid bug in the steps I followed to *try* to render the text properly.  Maybe I am just missing some required function or parameter.

I would appreciate any help or suggestions I could get.  Just click on this link to see the applet in action:
http://dxjones.com/processing/textimage/

thanks,
 djones

Code:
PFont font;
PGraphics p;
boolean showText = true;
boolean showPic = true;

void setup() {
size(600,400);
frameRate(60);
smooth();
font = loadFont("HelveticaNeue-36.vlw");
textMode(SCREEN);
p = createTextImage("This text was drawn only once.", font, 36);
}

void draw() {
float x;

background(0);
x = min(1,frameRate / 60);
rect(0.1*width,0.8*height,x*0.8*width,0.1*height);

if (showText) {
fill(255);
textFont(font);
textAlign(LEFT,CENTER);
text("This text is drawn each frame.", 0.1*width, 0.3*height);
}
if (showPic) {
image(p,0.1*width,0.5*height);
}
}

void keyPressed() {
if (key == 't' || key == 'T') {
showText = !showText;
} else if (key == 'p' || key == 'P') {
showPic = !showPic;
} else if (key == 'f' || key == 'F') {
println("frameRate = " + frameRate);
}
}

PGraphics createTextImage(String label, PFont font, float fontSize) {
PGraphics pg;

textFont(font);
pg = createGraphics((int) textWidth(label),(int) fontSize, P2D);
pg.beginDraw();
pg.background(0);
pg.smooth();
pg.noStroke();
pg.fill(255);
pg.textFont(font,fontSize);
pg.textAlign(LEFT);
pg.text(label,0,fontSize);
pg.endDraw();
return pg;
}
Re: 2 questions about "text" - slow, bug
Reply #1 - Oct 4th, 2007, 8:21pm
 
Not sure about the speed.  I use OpenGL for the renderer for all my projects and it handles this fine.  No speed decrease at all.  Even setting it to P3D keeps the framerate at 60+.  As for your second point, I think that is a pre-existing bug.

http://processing.org/discourse/yabb_beta/YaBB.cgi?board=Syntax;action=display;num=1163180365;start=0#0

http://dev.processing.org/bugs/show_bug.cgi?id=428
Re: 2 questions about "text" - slow, bug
Reply #2 - Oct 4th, 2007, 8:25pm
 
textMode(SCREEN) with the default renderer (and opengl) is extremely slow. this is because textMode(SCREEN) manipulates the pixels directly, which the default renderer stinks at.

if you're doing 2D text with JAVA2D (the default renderer), the fastest method is to use createFont(), and/or hint(ENABLE_NATIVE_FONTS) and don't use textMode(SCREEN).
Re: 2 questions about "text" - slow, bug
Reply #3 - Oct 4th, 2007, 8:41pm
 
thanks to flight404 !!

The following single line is a workaround for bug 428:

Code:
pg.textMode(SCREEN); 



This eliminates the "letters as blocks" error.

-- djones
Re: 2 questions about "text" - slow, bug
Reply #4 - Oct 4th, 2007, 9:05pm
 
Thanks Ben.

For pre-rendering text into an image, textMode(SCREEN) seems to be a necessary workaround for bug 428.

For text animation (inside draw()), removing textMode(SCREEN) does indeed make calls to "text" much faster.

I looked under "Reference", but I can't find details on the "hint" function.  Is it intentionally undocumented?

-- djones
Page Index Toggle Pages: 1