Cascading Style Sheets (CSS) in Gmod?

Hi All – I’m looking for a way to format text in Gmod – the world, not a panel – using CSS. The formatting options in Gmod and Lua are not enough. Google is all dead ends. Anyone have any ideas? Thanks in advance.

Unless you intend to use pure html for your UI, your best bet would be to replicate the desired style with code.

You should clarify what you want to accomplish and we could point you in the right direction.

Thanks. I need to be able to control line length (# of characters per line), line height (spacing between lines), and spacing between characters, as well as the formatting I see in Lua documentation (font, size, color, etc.) I’d very much appreciate any pointers.

If you want to split based on the number of characters, something like this would probably be up your alley and be easily modable to fit your needs - it is completely untested though.


local cache = { }

local function CachedSize( font, char )
    if not cache[ font ] then
        cache[ font ] = { }
    end
    
    if not cache[ font ][ char ] then
        surface.SetFont( font )
        local r, n = surface.GetTextSize( char )
        
        cache[ font ][ char ] = { w = r, h = n }
    end
    
    return cache[ font ][ char ]
end

function draw.SpacedText( font, str, x, y, linew, color, wpad, ypad )
    wpad = tonumber( wpad ) or 0
    ypad = tonumber( ypad ) or 0
    
    local r = 0
    
    surface.SetFont( font )
    surface.SetDrawColor( color )
    for k in str:gmatch( string.rep( ".", linew ) ) do
        if wpad > 0 then
            r = x
            
            for ik in k:gmatch( "." ) do
                surface.SetTextPos( r, y )
                surface.DrawText( ik )
                c = CachedSize( font, ik )
                r = r + c.w + wpad
            end
        else
            surface.SetTextPos( x, y )
            surface.DrawText( k )
        end
        a, b = CachedSize( font, k )
        y = y + h + ypad
    end
end

--EG:    draw.SpacedText( "ChatFont", "Some really long string that I'd like broken into after N characters", xpos, ypos, N, 4 )

Might be faster to index the string directly instead of using pattern matching, but that’s how I’d go about it.

Thanks. Please take a look at this job posting and let me know if you’re interested.

https://scriptfodder.com/jobs/view/7940

The due date and price are just placeholders.