Module:Abilities/card: Difference between revisions

Jump to navigation Jump to search
Size upgrade descriptions by measuring the rendered text against the box instead of counting bytes of the raw string, fixing punctuation and non-English localisations (with help from vergir-bot LLM)
Vergir (talk | contribs)
m Undo revision 104482 by ~2026-SlotReloadHex823 broke it, revert to investigate
Tag: Undo
Line 3: Line 3:
local utils = require "Module:Abilities/utils"
local utils = require "Module:Abilities/utils"
local Icon = require"Module:Abilities/icon"
local Icon = require"Module:Abilities/icon"
local textfit = require "Module:TextFit"


local p = {}
local p = {}
Line 416: Line 415:
local alt_boxes = {}
local alt_boxes = {}
     for k, prop in ipairs(props) do
     for k, prop in pairs(props) do
     -- Some props don't have values, as those come from upgrades
     -- Some props don't have values, as those come from upgrades
     -- For now, we will ignore these and only show data for the base ability
     -- For now, we will ignore these and only show data for the base ability
Line 452: Line 451:


local UPGRADE_COST_MAP = {1, 2, 5}
local UPGRADE_COST_MAP = {1, 2, 5}
-- Upgrade description sizing. These are design proportions, not copies of any
-- CSS measurement: the browser supplies the box width through the container
-- query on .ac-upgrade-descwrap, so nothing here needs updating if the card's
-- widths, margins or padding change.
local UG_DESC_ASPECT = 0.357  -- max height of the text block, as a multiple of its width
local UG_LINE_HEIGHT = 1.4    -- emitted alongside the size so the two cannot drift
local UG_MAX_CQW    = 11.9  -- ceiling, as % of the text width (~16px on a full-width card)
local UG_MIN_CQW    = 6.7    -- floor (~9px)
function get_upgrade_boxes(hero_key, ability_num)
function get_upgrade_boxes(hero_key, ability_num)
local ability = utils.get_ability_card_data(hero_key, ability_num)
local ability = utils.get_ability_card_data(hero_key, ability_num)
Line 471: Line 460:
local upgrade_boxes = {}
local upgrade_boxes = {}
for k, upgrade in ipairs(upgrades) do
for k, upgrade in pairs(upgrades) do
local description = lang.get_string(upgrade.DescKey)
local description = lang.get_string(upgrade.DescKey)
local desc_length = description and #description or 0


-- bypass some keys if they are mistakenly left in game files
-- bypass some keys if they are mistakenly left in game files
Line 492: Line 482:
end
end
-- Expand once, then size from the text the reader actually sees, rather
-- Vary the font size based on the number of characters to prevent overflow
-- than from a character count of the unexpanded string.
local fontsize = '1rem'
local rendered = frame:preprocess(description)
if desc_length > 30 and desc_length < 51 then
local cqw = textfit.fit(rendered, {
fontsize = '0.9rem'
aspect = UG_DESC_ASPECT,
elseif desc_length > 50 and desc_length < 71 then
lineHeight = UG_LINE_HEIGHT,
fontsize = '0.8rem'
max = UG_MAX_CQW,
elseif desc_length > 70 then
min = UG_MIN_CQW,
fontsize = '0.7rem'
})
end
local upgrade_scale = pick_scale(upgrade)
local upgrade_scale = pick_scale(upgrade)
Line 508: Line 498:
index = k,
index = k,
cost = UPGRADE_COST_MAP[k],
cost = UPGRADE_COST_MAP[k],
description = rendered,
description = frame:preprocess(description),
scale_value = upgrade_scale and commonutils.round_to_sig_fig(upgrade_scale.Value, 3),
scale_value = upgrade_scale and commonutils.round_to_sig_fig(upgrade_scale.Value, 3),
scale_type =  upgrade_scale and upgrade_scale.Type,
scale_type =  upgrade_scale and upgrade_scale.Type,
-- Measured against the box's own width, so the size stays correct
fontsize = fontsize
-- when the card renders below its 500px max-width.
fontsize = string.format('%.2fcqw', cqw),
lineheight = UG_LINE_HEIGHT
}
}
}
}