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;
}