Skip to content
This repository was archived by the owner on May 23, 2026. It is now read-only.

Latest commit

 

History

History
50 lines (32 loc) · 1.24 KB

File metadata and controls

50 lines (32 loc) · 1.24 KB

glua-VisualCharacterHeight

Or... VisualTextHeight, if you wish.

Adds a function that gets the visual height of the provided characters.

May be very helpful at positioning text by height where surface.GetTextSize happens to be inconvenient or insufficient.

Newlines are supported.

GetTextSize vs GetVisualCharacterHeight

1 2

local Font = 'DermaLarge'
local Chars = 'Lorem ipsum'
-- local Chars = 'Lorem ipsum\ndolor sit amet'

hook.Add( 'HUDPaint', 'GetVisualCharacterHeightDemo', function()

	surface.SetFont( Font )

	local w, h = surface.GetTextSize( Chars )

	local x = ScrW() * 0.5 - w - 5
	local y = ScrH() * 0.5

	surface.SetDrawColor( 255, 255, 255 )
	surface.DrawOutlinedRect( x - 1, y - 1, w + 2, h + 2 )

	surface.SetDrawColor( 130, 150, 255 )
	surface.DrawRect( x, y, w, h )

	draw.DrawText( Chars, Font, x, y, color_black )

	x = ScrW() * 0.5 + 5

	local visualheight, roofheight = surface.GetVisualCharacterHeight( Chars, Font )

	surface.SetDrawColor( 255, 255, 255 )
	surface.DrawOutlinedRect( x - 1, y - 1, w + 2, visualheight + 2 )

	surface.SetDrawColor( 150, 255, 150 )
	surface.DrawRect( x, y, w, visualheight )

	y = y - roofheight
	draw.DrawText( Chars, Font, x, y, color_black )

end )