Editing Module:ItemBox

Jump to navigation Jump to search
Warning: You are not logged in. Once you make an edit, a temporary account will be created for you. Learn more. Log in or create an account to continue receiving notifications after this account expires, and to access other features.
The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then publish the changes below to finish undoing the edit.
Latest revision Your text
Line 1: Line 1:
local p = {}
local p = {}
local ustring = mw.ustring
local textfit = require "Module:TextFit"


-- ============================================================================
function p.getFontSize(frame)
--  Name box geometry, in em of .itembox's font-size (1em = 10px at size=1).
    local name = frame.args[1] or ""
--  Mirrors Template:ItemBox/styles.css:
    local words = {}
--    .itembox            width: 8em
    for word in name:gmatch("%S+") do
--    .itembox-name-area  height: 4.5em; padding: 0.5em 0.4em (border-box)
        table.insert(words, word)
--    .itembox-name      line-height: 1.2; font-size: 1.5em (overridden inline)
    end
--  so the name's content box is 8 - 2*0.4 = 7.2em wide and 4.5 - 2*0.5 = 3.5em
   
--  tall. Only the ratio and the size limits are read from those figures: the
    local function wordWidth(word)
--  size is emitted in cqw against .itembox-name-area, so the browser supplies
    word = word:lower()
--  the actual width. Glyph widths live in [[Module:TextFit]].
        local width = 0
-- ============================================================================
        for i = 1, #word do
local NAME_W = 7.2
            local c = word:sub(i, i)
local NAME_H = 3.5
            if c == "i" or c == "l" then
local LINE_H = 1.2
                width = width + 0.5
local MAX_EM = 1.5
            elseif c == "m" or c == "w" then
local MIN_EM = 0.8
                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])


-- Returns the name font size as a cqw string, measured against
        local shorterOuter = math.min(first, last)
-- .itembox-name-area, which [[Template:ItemBox]] makes a query container.
        local longerOuter  = math.max(first, last)
function p.getFontSize(frame)
 
    local name = ustring.gsub(frame.args[1] or "", "^%s*(.-)%s*$", "%1")
        local combined = mid + 1 + shorterOuter
    local max = MAX_EM / NAME_W * 100          -- 20.83cqw
 
    if name == "" then return string.format("%.2fcqw", max) end
        longestLine = math.max(combined, longerOuter)
    end


    return string.format("%.2fcqw", textfit.fit(name, {
local fullSize = 15
        aspect    = NAME_H / NAME_W,
     local fontSize
        lineHeight = LINE_H,
        max        = max,
        min        = MIN_EM / NAME_W * 100,    -- 11.11cqw
     }))
end


-- User for stable per-item texture variety
    if longestLine <= 8 then
local function hash(s)
        fontSize = fullSize
    local h = 5381
     else
     for i = 1, #s do
         fontSize = fullSize - (longestLine - 8) * 1
         h = (h * 33 + s:byte(i)) % 2147483647
     end
     end
    return h
end


function p.variants(frame)
     return fontSize
    local h = hash(frame.args[1] or "")
    local icon = (h % 3) + 1
    local wear = (math.floor(h / 3) % 4) + 1
    local tex  = (math.floor(h / 12) % 3) + 1
     return string.format("itembox-icon-v%d itembox-wear-v%d itembox-texture-v%d",
        icon, wear, tex)
end
end


return p
return p
Please note that all contributions to The Deadlock Wiki are considered to be released under the Creative Commons Attribution-NonCommercial-ShareAlike (see Deadlock:Copyrights for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource. Do not submit copyrighted work without permission!
Cancel Editing help (opens in new window)
Preview page with this template

Page included on this page: