Editing Module:ItemBox

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"


-- ============================================================================
local function charWidth(cp)
-- Name box geometry, in em of .itembox's font-size (1em = 10px at size=1).
    -- Fullwidth & Wide ranges (CJK, Hangul, etc.) → 2 units
-- Mirrors Template:ItemBox/styles.css:
    if
--    .itembox            width: 8em
        (cp >= 0x1100  and cp <= 0x115F)  or  -- Hangul Jamo
--   .itembox-name-area  height: 4.5em; padding: 0.5em 0.4em (border-box)
        (cp >= 0x2E80  and cp <= 0x303E)  or  -- CJK radicals, Kangxi
--   .itembox-name      line-height: 1.2; font-size: 1.5em (overridden inline)
        (cp >= 0x3041  and cp <= 0x33BF)  or  -- Hiragana, Katakana, CJK compat
-- so the name's content box is 8 - 2*0.4 = 7.2em wide and 4.5 - 2*0.5 = 3.5em
        (cp >= 0x33FF  and cp <= 0xA4CF)  or  -- CJK unified
-- tall. Only the ratio and the size limits are read from those figures: the
        (cp >= 0xA960  and cp <= 0xA97F)  or  -- Hangul Jamo Extended
--  size is emitted in cqw against .itembox-name-area, so the browser supplies
        (cp >= 0xAC00  and cp <= 0xD7FF)  or  -- Hangul syllables
--  the actual width. Glyph widths live in [[Module:TextFit]].
        (cp >= 0xF900  and cp <= 0xFAFF)  or  -- CJK compatibility ideographs
-- ============================================================================
        (cp >= 0xFE10  and cp <= 0xFE1F)  or  -- Vertical forms
local NAME_W = 7.2
        (cp >= 0xFE30  and cp <= 0xFE6F)  or  -- CJK compatibility forms
local NAME_H = 3.5
        (cp >= 0xFF01  and cp <= 0xFF60)  or  -- Fullwidth Latin/punctuation
local LINE_H = 1.2
        (cp >= 0xFFE0  and cp <= 0xFFE6)  or  -- Fullwidth signs
local MAX_EM = 1.5
        (cp >= 0x1B000 and cp <= 0x1B0FF) or  -- Kana supplement
local MIN_EM = 0.8
        (cp >= 0x1F300 and cp <= 0x1F9FF) or  -- Emoji block
        (cp >= 0x20000 and cp <= 0x2FFFD) or  -- CJK Extension B–F
        (cp >= 0x30000 and cp <= 0x3FFFD)    -- CJK Extension G+
    then
        return 2
 
    -- Narrow Latin specifics
    elseif cp == 0x69 or cp == 0x6C or  -- i, l
          cp == 0x49 or                -- I
          cp == 0x31 or                -- 1
          cp == 0x21 or cp == 0x7C    -- !, |
    then
        return 0.5
 
    -- Wide Latin
    elseif cp == 0x6D or cp == 0x77 or  -- m, w
          cp == 0x4D or cp == 0x57    -- M, W
    then
        return 1.5
 
    -- Cyrillic and other "ambiguous" scripts — slightly wider than Latin
    elseif cp >= 0x0400 and cp <= 0x04FF then
        -- Wide Cyrillic (1.5)
            local wide = {
                [0x0416]=true, [0x0436]=true, -- Ж ж
                [0x041C]=true, [0x043C]=true, -- М м
                [0x0424]=true, [0x0444]=true, -- Ф ф
                [0x0428]=true, [0x0448]=true, -- Ш ш
                [0x0429]=true, [0x0449]=true, -- Щ щ
                [0x042E]=true, [0x044E]=true, -- Ю ю
                [0x042B]=true, [0x044B]=true, -- Ы ы
            }
            -- Narrow Cyrillic (0.5)
            local narrow = {
                [0x0406]=true, [0x0456]=true, -- І і (Ukrainian I)
                [0x0407]=true, [0x0457]=true, -- Ї ї
            }
 
            if wide[cp] then
                return 1.5
            elseif narrow[cp] then
                return 0.5
            else
                return 1.1
            end
 
    -- Default for everything else (Arabic, Hebrew, Thai, etc.)
    else
        return 1
    end
end
 
local function wordWidth(word)
    local width = 0
    for i = 1, mw.ustring.len(word) do
    local cp = mw.ustring.codepoint(word, i, i)
    width = width + charWidth(cp)
end
    return math.ceil(width)
end


-- Returns the name font size as a cqw string, measured against
-- .itembox-name-area, which [[Template:ItemBox]] makes a query container.
function p.getFontSize(frame)
function p.getFontSize(frame)
     local name = ustring.gsub(frame.args[1] or "", "^%s*(.-)%s*$", "%1")
     local name = frame.args[1] or ""
     local max = MAX_EM / NAME_W * 100          -- 20.83cqw
     local words = {}
     if name == "" then return string.format("%.2fcqw", max) end
     for word in name:gmatch("%S+") do
        table.insert(words, word)
    end
   
    local longestLine = 0


     return string.format("%.2fcqw", textfit.fit(name, {
     if #words == 1 then
         aspect    = NAME_H / NAME_W,
        longestLine = wordWidth(words[1])
         lineHeight = LINE_H,
 
         max        = max,
    elseif #words == 2 then
         min        = MIN_EM / NAME_W * 100,   -- 11.11cqw
        longestLine = math.max(wordWidth(words[1]), wordWidth(words[2]))
    }))
 
end
    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)
    else
    -- 4+ words: take the max of all individual word widths
    for _, word in ipairs(words) do
        longestLine = math.max(longestLine, wordWidth(word))
    end
    end
 
local fullSize = 15
    local fontSize


-- 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
    fontSize = math.max(6, fontSize)


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: