Module:ItemBox

Revision as of 23:18, 18 May 2026 by Jannepg (talk | contribs) (Halfass solution for somewhat scaling the text on ItemBox)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Documentation for this module may be created at Module:ItemBox/doc

local p = {}

function p.getFontSize(frame)
    local name = frame.args[1] or ""
    local words = {}
    for word in name:gmatch("%S+") do
        table.insert(words, word)
    end
    
    local function wordWidth(word)
    	word = word:lower()
        local width = 0
        for i = 1, #word do
            local c = word:sub(i, i)
            if c == "i" or c == "l" then
                width = width + 0.5
            elseif c == "m" or c == "w" then
                width = width + 1.5
            else
                width = width + 1
            end
        end
        return math.ceil(width)
    end
    
    local longestLine

    if #words == 1 then
        longestLine = wordWidth(words[1])

    elseif #words == 2 then
        longestLine = math.max(wordWidth(words[1]), wordWidth(words[2]))

    elseif #words == 3 then
        local first = wordWidth(words[1])
        local mid   = wordWidth(words[2])
        local last  = wordWidth(words[3])

        local shorterOuter = math.min(first, last)
        local longerOuter  = math.max(first, last)

        local combined = mid + 1 + shorterOuter

        longestLine = math.max(combined, longerOuter)
    end

	local fullSize = 15
    local fontSize

    if longestLine <= 8 then
        fontSize = fullSize
    else
        fontSize = fullSize - (longestLine - 8) * 1
    end

    return fontSize
end

return p