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 & HelpPrograms › Drawing text into an image buffer
Page Index Toggle Pages: 1
Drawing text into an image buffer (Read 843 times)
Drawing text into an image buffer
Apr 18th, 2006, 4:55am
 
In short, I want to use the shapes and areas defined by a dynamic text string as a mask, or in the future as points in a physics model. Since you can't use text() to draw into a particular PImage, the only way within Processing I can think of is drawing to the main screen, saving those pixels, and then covering the screen up again with background().

If it comes to it I can look up on using a text renderer for Java, but I'm just wondering if anyone has a simple solution. Thanks!
Re: Drawing text into an image buffer
Reply #1 - Apr 18th, 2006, 8:51am
 
try:

Code:



PGraphics drawing;

void setup() {
size(400, 300, P3D);

drawing = createGraphics(width,height,P3D,null);
drawing.defaults();
}





void draw()
{
background (255);
image(drawing, 0 ,0);

drawing.fill(mouseX);
drawing.ellipse(mouseX,mouseY,20,20);

}


Re: Drawing text into an image buffer
Reply #2 - Apr 18th, 2006, 8:59am
 
Although this is more in tune with your request, but I'm a bit unsure what's happening (as are others) with the fill on the mask.

Code:



PGraphics drawing;

void setup() {
size(400, 300, P3D);

drawing = createGraphics(width,height,P3D,null);
drawing.defaults();

PGraphics drawingMask = createGraphics(width,height,P3D,null);
drawingMask.defaults();
drawingMask.background(0);
drawing.mask(drawingMask);
}





void draw()
{
background (255);
image(drawing, 0 ,0);

drawing.fill(mouseX,0);
drawing.stroke(mouseX);
drawing.ellipse(mouseX,mouseY,20,20);

}
Re: Drawing text into an image buffer
Reply #3 - Apr 18th, 2006, 9:46am
 
Thanks for the PGraphics tip. I decided to dig into the Processing source for the first time and I think I found an answer in the form of PFont.images, which seems to contain the glyphs of each character.

But looking further, there are a bunch of methods in the PGraphics involving the placing and drawing of glyphs ... so perhaps I'll extend PGraphics and make a version of
protected void textCharScreenImpl(PImage glyph, int xx, int yy, int w0, int h0)

where it draws to a to a pixel buffer rather than the screen.
PImage.setBuffer(PImage new_buffer)  ... etc.

Going down that route, it would just beg for making it another textMode (textMode == IMAGE_BUFFER ?) such that i can simply reuse the  textLinePlacedImpl(), textCharImpl() functions ... the problem is that I don't need those features at all. Would it be helpful at all for others if I implemented it that way?
Re: Drawing text into an image buffer
Reply #4 - Apr 18th, 2006, 9:49am
 
correction: instead of a new textMode it would probably be a PGraphics-wide drawMode(BUFFER), but that's probably confusing.
Re: Drawing text into an image buffer
Reply #5 - Apr 18th, 2006, 10:45am
 
I think the best approach would be to make a new method text(PGraphics g, ...) which also takes as input a PGraphics. That would avoid having to change modes and not knowing to what buffer it's drawing.

It's the approach I used in the geomerative library.  This library might be of your interest if you are dealing with fonts.
link to geomerative

It's still alpha and the API might change, but right now I don't have the time to spend on it.
Re: Drawing text into an image buffer
Reply #6 - Apr 18th, 2006, 3:41pm
 
the PGraphics method mentioned by mark is the way you want to go. PFont.images isn't guaranteed to work in the future. the PGraphics objects subclass PImage specifically so that you can draw into them and later draw the PGraphics back to the screen as an image.
Re: Drawing text into an image buffer
Reply #7 - Apr 18th, 2006, 4:58pm
 
Thanks Ricard, Fry. Your geomerative library looks like it may be very useful to me (as a plus, we use the same naming scheme) ... I'll check it out and let you know.
Page Index Toggle Pages: 1