Is there a font library out there?

This is what I need in my game regarding text:

Word Wrap Support given a bounding box
Vertical and Horizontal alignment given a bounding box

Now, I’ve been reading about how to use TextLayout, and it seems possible to write all this myself but I’d prefer to think at a higher level. I just want a Label class with a signature like this:

public Label(String text, Alignment alignment, VAlignment vAlignment);

Does anyone know of an open source (non-gpl) library out there that makes text formatting simple?

Riven: Your suggestion is limited by the fact that I need to use a swing layout manager to use it. I want to be able to define my label’s bounds with absolute positioning. Maybe I can just use a layout manager with absolute positioning for this though but my guess is that I will hit a limitation with JLabel somewhere. Can it have a clear background?

JLabel can now format HTML. I think thats since version 5.

See this example. http://www.java2s.com/Code/Java/Swing-JFC/AJLabelthatusesinlineHTMLtoformatitstext.htm
Also see: http://java.sun.com/docs/books/tutorial/uiswing/components/label.html

Thus you could have multi-line text in a label or button.

Maybe you didn’t see my reply: I’m concerned about the limitations of using a JLabel in a game that doesn’t have a swing look. I need a label but I don’t want it’s L&F, it’s background, its need for a layout manager, it’s anything except for its ability to wordwrap and align text. Is it easy to get rid of the parts I don’t want?

Swing objects are pretty heavy weight. You might be better off making your own label. The main advantage to using Swing is you don’t have to worry about the look of the GUI because the layout manager adjust for this.
For Example: If you use a Font that is not on the user’s computer, The layout manager will substitute to a different font and it will make everything fit.

Have you looked at the sourcecode? No need for a LayoutManager at all! :slight_smile:


BufferedImage img = new BufferedImage(w, h, ...);

String html = "";
html += "<b>bold</b> ";
html += "<font color=\"red\">red</font> text with ";
html += " support for <div style=\"margin:32px\">paragraphs</div> and styles";

renderHTML(img, html);

now your image contains your rendered HTML. You asked for a boundingbox, and your BufferedImage acts like one. There also is no background color. If your image is transparant, the resulting image will stay transparant, until you specify it yourself, by using styles:

Could you maybe tell us what additional features you require?

ok that sounds good. Thanks for the info