Jump to content
  • Advertisement
Sign in to follow this  
Acharis

Block of text

This topic is 2725 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Same rectangle area (300x200 pixels lets say), now I want to fill it via nicely formatted text. Sort of box where you just pass a string as a paremeter with \n for end lines and it displays it inside. How do you approach it?

1) Create some font bitmap, then make all functions yourself.
2) Use TTF, still you have to make most string formatting functions yourself (it have only a function to return the size of string in pixels, nothing to cut text to fit an area).
3) Pongo library, well, nice one, but it requires 2MB worth of dlls. A bit of overkill if you are making just a game...

Are there other options? Which you would choose?

Share this post


Link to post
Share on other sites
Advertisement
I made my own functions for that. It's anything but easy. So I'd go for a library instead.

Share this post


Link to post
Share on other sites
Depending on your needs...

1- Simple prototyping?
Bitmapped quads supporting only ASCII-7 or ASCII-8 limited to your own codepage. Between choosing the font you want, extracting glyph metrics, preparing the texture quad etc, it could take as little as few days to a couple of weeks.

2- More long term support?
There's no good solution in this complexity range. If unsure, you can probably stay in "simple prototype". Your first step here is probably to rework the initial prototype to support at least Unicode characters for simple scripts using FreeType or Win32 font support (beware that FT2 is a bit quirky in my opinion).

3- Reliable system for generic international text.
Because of the complexities involved in generic script processing this is difficult. You need at least a Unicode layout system (HarfBuzz, or Uniscribe) and a glyph rasterizer.

I have used (1) for some years with satisfactory results. When transitioning to (2) I found out that text management was still quirky on these systems so I went to (3) directly. I also wrote my own glyph drawing routines so while I have dependancies on glyph layout, I don't have dependancies on the rasterizer. Beware, this takes easily a month (depending on how much code you can reuse). It took about two months to me. Rasterizing arbitrary glyphs is damn difficult.

Share this post


Link to post
Share on other sites
[s]If you're using Windows and GDI or DirectX, look at DrawText. There are quite a few possibilities using the DT_ formatting flags for multiline text, word breaks, etc. Both GDI DrawText and ID3DXFont.DrawText take a rectangle as an argument.

[/s]EDIT: didn't read all your needs.

Share this post


Link to post
Share on other sites
Same as szecs said. I have my own graphics functions including text with word wrapping.(what you want) The entire set was written over something like 6 months. And of course, supports only basic fonts. No special languages which these days is a drawback.

Writing your own word wrap function for the first time may be not worth the gain of a 2mb decrease in your project.

Share this post


Link to post
Share on other sites
I need just a simple text display with word wrapping. One or two fonts total of several colours. No modifications like bold/italic or anything. No international support. Only basic stuff.

Isn't there any light library for this?

Share this post


Link to post
Share on other sites
Unfortunately, it isn't "basic stuff", and a library that uses fonts cannot be "light".
Using one font is as difficult as using many; support for non-ASCII characters boils down to using adequate fonts with non brain-damaged APIs; bold and italic are built in fonts.
The simplest text layout must deal with metrics, kerning, fractional positions, hinting, altered metrics due to hinting, non-breaking spaces, and with remotely modern fonts ligatures and contextual substitutions.
I suggest you use the highest level API available, most likely getting metrics and bitmaps for a whole string rather than a character at a time, iteratively attempting different line wrapping options.

Share this post


Link to post
Share on other sites

I need just a simple text display with word wrapping. One or two fonts total of several colours. No modifications like bold/italic or anything. No international support. Only basic stuff.

Isn't there any light library for this?
Probably not, because what you're asking for is a library to deal with (1 - see above) . Supposing you are using the term "word wrapping" properly (not to be confused with "word breaking") then what you really need is a textured quad approach.
Open The GIMP, or the image editor of your choice and create an image with all the glyphs you need to support. For each glyph, write the position of its bounding box in pixels (you might want to use sub-pixels). Divide by texture dimension obtaining a set of normalized texture coordinates.
Slap this information in an array.
For each character you need to draw, lookup this array. Spawn 4 vertices with the obtained texture coordinates. Advance position according to AABB dimension. You will need special behavior for newline, tab and space.
Just compare to the maximum allowed line length for word wrapping by using "sheet space s vector".
You're done.

The above method, while plain out simple has some issues with kerning pairs: "AV" for example will look terrible. Expanding the system to handle those cases is fairly simple. Obviously, when you start having a lot of kerning pairs and a lot of fonts, you'll probably want to switch to another method.
This method is completely insulated from the world and will break horribly on everything which is not using your codepage but it's still good enough to be used for a while.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!