Skip to content

BasicFontRenderer: Fix various sizing and placement issues - #185

Merged
Johni0702 merged 2 commits into
masterfrom
fix/basicfontrenderer-spacing-and-positioning
Jul 30, 2026
Merged

BasicFontRenderer: Fix various sizing and placement issues#185
Johni0702 merged 2 commits into
masterfrom
fix/basicfontrenderer-spacing-and-positioning

Conversation

@Johni0702

Copy link
Copy Markdown
Contributor

In particular:

  • getStringHeight returned the height of the biggest character, but while g and h have similar height, the height of gh should be 1.5x that.
  • getStringWidth failed to include the width of glyphs without atlas bounds (such as the space character).
  • getStringWidth incorrectly adds between-letter spacing when the last character has no atlas bounds (e.g. space or unmapped or color code)
  • All glyphs were incorrectly offset on the y axis by 0.025em, and all their atlas coordinates on both axes by 0.5px. Together with other bugs these happened to cancel out with the Minecraft Five font, but not for any other font. Afaict these stem from an incorrect understanding of why the raw values did not match what one expected. See the docs on the newly added shrinkGlyphsByHalfAPixel method and the following point for where this confusion likely came from.
  • The provided Minecraft Five font actually has a base line height of 6 because most regular letters are actually placed at 0.5px above the baseline. This does not affect other fonts and must be an issue with the original font.
  • The string overall was rendered one higher than it should have been. Likely as a workaround to the above two points and the following pointwm. This magic offset has been removed now.
  • Glyphs were drawn at the wrong y position. The font file has its y origin at the bottom, while Minecraft has it at the top; this was taken into account in other places, but not for the y positioning. The old code happened to produce close to correct y positions when taking into account all the other bugs for most characters, but completely failed to do so for characters that aren't close to full-height, such as most punctuation.
  • The _ in Minecraft Five was manually changed in 274b2d1 to appear at the correct Y position. This was done by assigning it an incorrect position in the file, which happened to come out at roughly the correct position after all it went through all the other bugs. With the other bugs fixed, this has now been reverted.

Further issues that are not addressed by this commit:

  • The renderer almost completely ignores the x position and advance values of glyphs, hard-coding a single pixel spacing instead. This is kind of required given that the provided Minecraft Five font places glyphs in the middle of their allocated space instead of left-aligning them, and uses non-integer spacing. It does however ofc make it impossible to use a font where some characters have extra space around them.
  • The Minecraft Five font has some characters (e.g. $, @) which extend far below and/or above the regular line height. These are presently simply drawn out of bounds and may be cut off by scissor effects and similar. Elementa doesn't have any robust way to handle them (without substantially affecting layout), so there's no easy solution for them.
  • The font file format technically allows setting the y origin to be at the top. This continues to be unsupported by the renderer though.

In particular:
- `getStringHeight` returned the height of the biggest character, but
while `g` and `h` have similar height, the height of `gh` should be 1.5x
that.
- `getStringWidth` failed to include the width of glyphs without atlas
bounds (such as the space character).
- `getStringWidth` incorrectly adds between-letter spacing when the last
character has no atlas bounds (e.g. space or unmapped or color code)
- All glyphs were incorrectly offset on the y axis by 0.025em, and all
their atlas coordinates on both axes by 0.5px. Together with other bugs
these happened to cancel out with the Minecraft Five font, but not for
any other font. Afaict these stem from an incorrect understanding of why
the raw values did not match what one expected. See the docs on the
newly added `shrinkGlyphsByHalfAPixel` method and the following point
for where this confusion likely came from.
- The provided Minecraft Five font actually has a base line height of 6
because most regular letters are actually placed at 0.5px above the
baseline. This does not affect other fonts and must be an issue with the
original font.
- The string overall was rendered one higher than it should have been.
Likely as a workaround to the above two points and the following
pointwm. This magic offset has been removed now.
- Glyphs were drawn at the wrong y position. The font file has its y
origin at the bottom, while Minecraft has it at the top; this was taken
into account in other places, but not for the y positioning. The old
code happened to produce close to correct y positions when taking into
account all the other bugs for most characters, but completely failed to
do so for characters that aren't close to full-height, such as most
punctuation.
- The `_` in Minecraft Five was manually changed in 274b2d1 to appear
at the correct Y position. This was done by assigning it an incorrect
position in the file, which happened to come out at roughly the correct
position after all it went through all the other bugs. With the other
bugs fixed, this has now been reverted.

Further issues that are not addressed by this commit:
- The renderer almost completely ignores the x position and `advance`
values of glyphs, hard-coding a single pixel spacing instead. This is
kind of required given that the provided Minecraft Five font places
glyphs in the middle of their allocated space instead of left-aligning
them, and uses non-integer spacing. It does however ofc make it
impossible to use a font where some characters have extra space around
them.
- The Minecraft Five font has some characters (e.g. `$`, `@`) which
extend far below and/or above the regular line height. These are
presently simply drawn out of bounds and may be cut off by scissor
effects and similar. Elementa doesn't have any robust way to handle them
(without substantially affecting layout), so there's no easy solution
for them.
- The font file format technically allows setting the y origin to be at
the top. This continues to be unsupported by the renderer though.

@RedEpicness RedEpicness left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks

@Johni0702
Johni0702 merged commit ffc0b68 into master Jul 30, 2026
1 check passed
@Johni0702
Johni0702 deleted the fix/basicfontrenderer-spacing-and-positioning branch July 30, 2026 14:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants