-1

I recently have been trying to implement text rendering using glyphs in my game. I have been able to render them onto the screen, but I want to be able to scale them without having them move out of the current line that is being rendered.

For example it should look like this:

Expected

Not this:

Output

In other words, I want all of the glyphs to line up along a common origin. I've tried coming up with my own algorithm using Glyph Metrics to try to place the glyphs accurately and then I multiply them by the scale. Nothing that I have tried works for every character or every scale.

genpfault
  • 51,148
  • 11
  • 85
  • 139
Cyerunix
  • 71
  • 11
  • 2
    Please, note: a glyph has (usually) a base line, an ascent, and a descent. (Adding ascent and descent you get total height.) If you scale glyphs, you probably have to scale base line as well and (re-)position the glyph vertically respectively. (I recently had another conversation regarding font metrics - if I only could find them...) – Scheff's Cat Apr 22 '19 at 06:17
  • 1
    Found it: [SO: How to use freetypegl with {0, w, h, 0} ortho projection?](https://stackoverflow.com/q/53467400/7478597). Unfortunately, there is no answer but I provided some explanations and links in comments. – Scheff's Cat Apr 22 '19 at 06:29

1 Answers1

2

Thank you to @Scheff for pointing me in the right direction. Using a post I found related to the one they gave to me, I was able to develop two separate formulas - one for aligning the tops of the glyphs and one for aligning the bottoms of the glyphs. I figured I'd post them here to help anybody else struggling with this problem. Here are the two functions:

Aligning the Glyphs Along Their Tops:

TTF_Font font;
float scaleOfText;
int maxY;
int positionInput, realPositionValue; /*positionInput is where the program is told that the glyphs 
should be rendered on the y-axis and realPositionValue is the position on the y-axis where the
glyphs will be rendered once they are aligned*/
char glyph;
TTF_GlyphMetrics(font, glyph, nullptr, nullptr, nullptr, &maxY, nullptr);
    //Formula itself:
    realPositionValue = positionInput - (TTF_FontAscent(font) * scale - (maxY * scale));

This looks like this: https://i.stack.imgur.com/pyXDy.jpg

Aligning the Glyphs Along Their Bottoms:

TTF_Font font;
float scaleOfText;
int maxY;
int positionInput, realPositionValue; /*positionInput is where the program is told that the glyphs 
should be rendered on the y-axis and realPositionValue is the position on the y-axis where the
glyphs will be rendered once they are aligned*/
char glyph;
TTF_GlyphMetrics(font, glyph, nullptr, nullptr, nullptr, &maxY, nullptr);
    //Formula itself:
    realPositionValue = (positionInput + maxY * scale) - ((TTF_FontAscent(font) + maxY) * scale);

This looks like this: https://i.stack.imgur.com/AswYe.jpg

I haven't tested this with different fonts mixed together yet but I'd assume that should work just as well. I hope this helps anyone with a similar problem to the one I was facing. Thank you again everyone for helping!

Community
  • 1
  • 1
Cyerunix
  • 71
  • 11