Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Non-graphical text wrapping

 
Rick Crawford
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all. As usual, I'm trying to do something a little weird here. And as usual, I'm stuck. That's why I'm turning to the Moose for help.
Without going into too much background detail, I need to parse a bunch of words separated by spaces, like a long paragraph, into an list. Each element in the list will be a String containing a section of the paragraph not to exceed a given length. I don't want to split any words. This processing will be done in preparation to print onto (annotate) an image file.
In other words, I need to create a non-graphical word wrapper.
I could write a class that does pretty much I've described to this point (although any sample code someone else already has working would be great.) The real kicker is that I want to be able to specify a font size and face for the method to take under consideration in calculating the contents of each String. I would need to somehow specify a maximum number of pixels(?)
as the length of each line.
This class would also need to calculate the offset between the 'sentence' Strings to print them down the page (the image.)
Here's a high-level use case of how I'd envision this to work:
TextWrapper tw = new TextWrapper();
tw.setTextToWrap("Here's the stuff we want to wrap all over the place, blah, blah, blah");
tw.setFont(new Font("Arial",Font.PLAIN,10);
tw.setMaxPixels(1000);
tw.wrapIt();
for (int i = 0; i < tw.getNumLines(); i++) {
String nextLine = (String)tw.getLine(i);
// write out each line
}
There may be a way to accomplish the end results using awt components, but I haven't been able to figure this out so far.
Thanks in advance for any assistance,
Rick <><
 
Jason Kilgrow
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rick Crawford:
Hello all. As usual, I'm trying to do something a little weird here. And as usual, I'm stuck. That's why I'm turning to the Moose for help.
...
The real kicker is that I want to be able to specify a font size and face for the method to take under consideration in calculating the contents of each String. I would need to somehow specify a maximum number of pixels(?)
as the length of each line.
...
Thanks in advance for any assistance,
Rick <><

I've actually tried something like this for a jsp application I just wrote. The one really irritating thing about font sizes is that they don't have anything to do with character width. Font sizes are all about character height. How stupid is that?
Anyway, it wasn't exactly how I wanted it but it was pretty close. What I did was calculate an average pixel width and use that number to calculate how many characters I could have on one line. It was sort of kludgy but it worked. I suppose I could have figured out how many pixels there were for each character I had to print, test what character I was printing, and count pixels as I went. Yuk.
If anybody has a better idea, I'm interested...
[This message has been edited by Jason Kilgrow (edited December 18, 2001).]
 
David Weitzman
Ranch Hand
Posts: 1365
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that java.awt.Font may help you, but I haven't actually checked if a solution is there.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try java.awt.FontMetrics (in combination with Font).
 
Jason Kilgrow
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks David and Jim!
I'm going to try them out!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic