STB - Carriage return?

Started by Optimo, January 15, 2017, 03:30:32

Previous topic - Next topic

Optimo

Pretty noob question, but does anyone know a way to have STB handle a carriage return for text? Searched and couldn't find it. I presently am able to print one line but no returns. Or am I just supposed to have STB print the individual lines myself with separate print statements per line?

spasi

Unlike stb_easy_font which handles \n, with stb_truetype only the x position is advanced automatically. Glyph kerning and line breaks must be done manually. It is a low-level solution, but it's very powerful. You have access to the full range of glyph metrics and kerning information. High quality text layout is possible, but it does take some work.

For a higher-level solution, see NanoVG. E.g. nvgTextBox supports line breaks and uses stb_truetype internally.

Optimo


Optimo

Perhaps I can get help with another part of this? Using STBTruetype, I have it such that a line with delimiters is broken into multiple strings and then that is printed as I'd wished.

I want to take a String, have it dynamically find where that String with a given font is at a width limit, and do the linebreak there. Some Java font class had the ability to do that (find a String width). Can I do this with STBTruetype? I looked through the API and couldn't quite understand if what was there is what I need.

To be clear, the linebreak is done with something other that STB. I'm hoping STB can give me this width so I can tell my linebreaker where to do its thang.

spasi

Yes, you do that, but there is no API that accepts an entire string and returns the width. You have to loop over each glyph in your string, query its metrics and accumulate the total width.

I repeat that stb_truetype is a low-level solution. E.g. it's up to you to cache glyph metrics to avoid query overhead. Also you need to take care of scaling, all metrics are in "unscaled" coordinates and you must scale them appropriately for your given font size and other transformations you may have applied.