Facepunch / garrysmod-requests

Feature requests for Garry's Mod
85 stars 24 forks source link

Precise alignment + sizing of text rendering #1750

Open WilliamVenner opened 4 years ago

WilliamVenner commented 4 years ago

Details

Please implement a FontData option that causes the font to be rendered with precise alignment and sizing.

image

For example, in this image I am drawing text and drawing a rectangle underneath it which is the height returned by surface.GetTextSize.

As you can see, the rectangle does not perfectly fit the text because of the current limitations of text rendering.

It would be really useful for HUDs and 3D2D UI for precise text alignment capabilities, to prevent cringe like this:

image

If implemented, the "Level 4" text shown above would start at the top of the SpawnIcon seen to the right, and surface.GetTextSize would return the actual rendered height of the text.

TiberiumFusion commented 4 years ago

@WilliamVenner If I'm not mistaken, the empty space included in the GetTextSize is technically correct, and is due to the ascent/descent/em size properties of the specific font used. A lot of fonts specify ascent/descent that are much larger than the actual visible glyphs to "bake in" a minimum amount of line-spacing. Typically, this is done in fonts with lowercase letters like gjypq that have extensive hanging bits, but some font creators set both values to be excessively large. Fonts that were not produced at natural em size stops (i.e. 1024, 2048) and have small glyphs are often are victim to this, as the ascent + descent must equal the em size (ideally). See: https://i.stack.imgur.com/LwZJF.png

I don't know which of the many redundant font properties gmod chooses to respect when rendering fonts, but the "FontData option that causes the font to be rendered with precise alignment and sizing" that you are after would probably be a way to override ascent/descent/em size on a per font basis. Or, alternatively, a new surface.GetTextSizeEx that returns a table containing the top-to-bottom height, ascent-to-descent height, and the largest glyph's height of each line. That may be more useful for setting up layouts with multiline text than mucking about with the bitmapped font itself.

In the meantime, you could modify and ship some fonts of your choosing to have larger glyphs which better fit the font's declared em size, or try changing the font's ascent/descent and hope gmod uses that instead of em size. That's the workaround I've been using with worthwhile success (specifically, increasing glyph size).

WilliamVenner commented 4 years ago

Thanks for the detailed reply :D

I'm aware this is the correct behaviour, just would be nice if we could have more precise control over it.