LWJGL Forum

Please login or register.

Login with username, password and session length

Author Topic: STB - Carriage return?  (Read 415 times)

Optimo

  • Newbie
  • *
  • Offline Offline
  • Posts: 15
STB - Carriage return?
« on: January 15, 2017, 03:30:32 »

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?
Logged

spasi

  • Administrator
  • Nerdus Imperius
  • *****
  • Offline Offline
  • Posts: 1783
Re: STB - Carriage return?
« Reply #1 on: January 15, 2017, 14:48:49 »

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.
Logged

Optimo

  • Newbie
  • *
  • Offline Offline
  • Posts: 15
Re: STB - Carriage return?
« Reply #2 on: January 15, 2017, 16:05:29 »

Thanks again, spasi!  ;D
Logged

Optimo

  • Newbie
  • *
  • Offline Offline
  • Posts: 15
Re: STB - Carriage return?
« Reply #3 on: January 24, 2017, 03:01:51 »

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.
Logged

spasi

  • Administrator
  • Nerdus Imperius
  • *****
  • Offline Offline
  • Posts: 1783
Re: STB - Carriage return?
« Reply #4 on: January 24, 2017, 10:26:42 »

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.
Logged