FAQ
Cover
This is the archive Discourse for the Processing (ALPHA) software.
Please visit the new Processing forum for current information.

   Processing 1.0 _ALPHA_
   Suggestions
   Software Suggestions
(Moderator: fry)
   more text manipulation tools
« Previous topic | Next topic »

Pages: 1 
   Author  Topic: more text manipulation tools  (Read 1214 times)
kevinP

Email
more text manipulation tools
« on: Feb 2nd, 2004, 9:53am »

Hi,
 
I woke up with this on my mind...
 
Code:

BFont fontA;
void setup()
{
  fontA = loadFont("Meta-Bold.vlw.gz");
  textFont(fontA);
  textSize = 48;
}
 
loop()
{
  background();
  text("My favourite letters are ", 25, 100);
  textMode(CONCATENATE);
 
  // In concatenate mode each add'l text command
  // uses the same y coord. (or baseline?) and x
  // coord. & is set to the right side of previous
  // text command.
 
  delay(500);
  text("A");
  delay(500);
  text(" B");
  delay(500);
  text(" C");
}

 
Or maybe this is more practical...
Code:

BFont fontA;
textObj fave, a, b, c; // new
int offset;
void setup()
{
  size(200, 200);
  fontA = loadFont("Meta-Bold.vlw.gz");
  textFont(fontA);
  textSize = 48;
 
  // create a text object and assign to var
 
  fave = textObj("My favourite letters are ", 25, 100);
 
  // use the width() to get width in pixels of
  //  another text object
 
  a = textObj("A ", 25, offset + fave.width());
 
  // use the append function to utilize x- and y-
  // position of right bounding box (or baseline)
  // of another text object (kerning hints would
  // also be nice
 
  b = textObj("B ", 35, a.append() + 10);
  c = textObj("C", 45, b.append() + 10);
 
  offset = -width;
}
 
loop()
{
  background();
  text(fave);
  text(a);
  text(b);
  text(c);
  offset++;
}

 
That's the rough idea - a way to have the bounding box (at least) for a text sequence available and useable. So that one can have two different text objects that know how to line up with each other. This means there would also have to be something like: "yPos = myTextObject.baseline()".
 
Or are there already ways to do this that I've missed?
 
[Addendum: I suppose an "append" would have to be based on locale - for languages that read right-to-left and other directions. But better, would be a way to place text pieces next to each other in any way (with knowledge of bounding box, baseline, and possibly kerning).]
 
-K
« Last Edit: Feb 2nd, 2004, 1:31pm by kevinP »  

Kevin Pfeiffer
kevinP

Email
Re: more text manipulation tools
« Reply #1 on: Feb 2nd, 2004, 9:50pm »

hmmm... digging deeper into the examples I found this:
 
Code:

// returns width of text for String in word
int wordLgth = int(fontA.width(word));

 
The examples folder has a lot of rich material to mine...
 
I ended up trying something like this
Code:

// word positioning
 
size(600, 200);
background(0);
 
int fntSize = 85;
BFont fontA = loadFont("OCR-B.vlw.gz");
textFont(fontA, fntSize);
fill(255);
 
int kern = 3;
int spc = fntSize/3;  // letterspace
int left = 3;         // margin
float crsr;           // last "cursor" pos. (x)
 
String word = "Hello";
String word2 = "world";
String comma = ",";
String exclaim = "!";
 
// Draw words to the screen
text(word, left, 100);             // draw 1st word
crsr = left + fontA.width(word) - kern; // set cursor position
text(comma, crsr, 100);            // draw comma
crsr += fontA.width(comma) + spc;       // etc.
text(word2, crsr, 100);
crsr += fontA.width(word2);
text(exclaim, crsr -kern, 100);

 
Here's a fancier test...
http://timo.iu-bremen.de/~pfeiffer/processing/2004/lions_and_tigers/
 
Still, it might be nice to have a global variable that remembers the x-pos of the last char drawn to screen... ?
 
-K
 
 
 
 
 
 
« Last Edit: Feb 2nd, 2004, 9:53pm by kevinP »  

Kevin Pfeiffer
fry


WWW
Re: more text manipulation tools
« Reply #2 on: Feb 3rd, 2004, 12:18am »

on Feb 2nd, 2004, 9:50pm, pfeiffer wrote:
Still, it might be nice to have a global variable that remembers the x-pos of the last char drawn to screen...

interesting idea.. postscript does this (though for everything), i'll have to give it a bit more thought.
 
regarding fancier text placement, i've thought about this in the past and decided that it's something that needs to be done as a class. there are too many features (line spacing, tracking, alignment, size, font, width, height, justification) for it to be handled in a single function. someday we might have a default extension class for this, but in the meantime, it'd be easy to throw together and drop into your 'code' folder to use it in projects.
 
hm, though now that i'm thinking about it (as i write this) we could get away with adding textLeading() and another version of text() that includes a width and a height, and it'd cover a lot of it. hmm..
 
Pages: 1 

« Previous topic | Next topic »