Files

158 lines
6.9 KiB
Lua

-- Addon-local frame factories opt in. Blizzard factories/fonts are never replaced.
local originalCreateFrame = CreateFrame
local objects = setmetatable({}, { __mode = "k" })
local frames = setmetatable({}, { __mode = "k" })
local busy = setmetatable({}, { __mode = "k" })
local readers = setmetatable({}, { __mode = "k" })
local API = {}
OctoTextSize = API
local scale = 1
local function currentScale()
local value = TextSizeManager and TextSizeManager.GetScale and TextSizeManager:GetScale()
value = tonumber(value or (GetCVar and GetCVar("userFontScale"))) or 1
return math.max(0.8, value)
end
local function nativeScaled(object, element)
if not object.GetFontObject or not GetFontInfo then return false end
local font = element and object:GetFontObject(element) or object:GetFontObject()
local name = font and font.GetName and font:GetName()
local info = name and GetFontInfo(name)
return info and info.canBeUserScaled or false
end
local function measure(object, element, record)
if element then record.file, record.size, record.flags = readers[object](object, element)
else record.file, record.size, record.flags = readers[object](object) end
record.native = nativeScaled(object, element)
end
local function apply(object, element, record)
if record.native or not record.file or not record.size or record.size <= 0 then return end
if InCombatLockdown and InCombatLockdown() then return end
busy[object] = true
if element then object:SetFont(element, record.file, record.size * scale, record.flags or "")
else object:SetFont(record.file, record.size * scale, record.flags or "") end
busy[object] = nil
end
local function recordFor(object, element)
local state = objects[object]
if not state then return end
local key = element or false
if not state[key] then
state[key] = {}
measure(object, element, state[key])
end
return state[key]
end
function API.TrackFont(object)
if not object or objects[object] or not object.GetFont or not object.SetFont then return end
local kind = object.GetObjectType and object:GetObjectType()
if kind ~= "FontString" and kind ~= "EditBox" and kind ~= "ScrollingMessageFrame" and kind ~= "SimpleHTML" then return end
local html = kind == "SimpleHTML"
readers[object] = object.GetFont
objects[object] = {}
if html then
for _, element in ipairs({"p", "h1", "h2", "h3"}) do recordFor(object, element) end
else recordFor(object) end
-- Addon layout/style code copies GetFont() into SetFont(), including between
-- different labels. Keep that owned object's logical font size unscaled;
-- engine text measurement/rendering still sees the actual scaled font.
object.GetFont = function(self, element)
local key = html and string.lower(element or "p") or false
local record = objects[self] and objects[self][key]
if record and not record.native and record.file then return record.file, record.size, record.flags end
if element then return readers[self](self, element) end
return readers[self](self)
end
hooksecurefunc(object, "SetFont", function(_, first, second, third, fourth)
if busy[object] then return end
local element = html and string.lower(first) or nil
local record = recordFor(object, element)
record.file = html and second or first
record.size = html and third or second
if html then record.flags = fourth or "" else record.flags = third or "" end
record.native = false
apply(object, element, record)
end)
if object.SetFontObject then
hooksecurefunc(object, "SetFontObject", function(_, element)
if busy[object] then return end
element = html and string.lower(element) or nil
local record = recordFor(object, element)
measure(object, element, record)
apply(object, element, record)
end)
end
if not html and object.SetFontHeight then
hooksecurefunc(object, "SetFontHeight", function(_, height)
if busy[object] then return end
local record = recordFor(object)
record.size, record.native = height, false
apply(object, nil, record)
end)
end
for key, record in pairs(objects[object]) do apply(object, key or nil, record) end
end
local function regions(frame)
if frame.GetRegions then
for _, region in ipairs({frame:GetRegions()}) do API.TrackFont(region) end
end
end
function API.RegisterFrame(frame)
if not frame or frames[frame] then return frame end
frames[frame] = true
API.TrackFont(frame)
regions(frame)
if frame.CreateFontString then hooksecurefunc(frame, "CreateFontString", function() regions(frame) end) end
if frame.GetChildren then
for _, child in ipairs({frame:GetChildren()}) do API.RegisterFrame(child) end
end
return frame
end
function API.WrapFactory(factory)
return function(...) return API.RegisterFrame(factory(...)) end
end
function API.CreateFrame(...)
return API.RegisterFrame(originalCreateFrame(...))
end
function API:OnTextScaleUpdated(value)
scale = math.max(0.8, tonumber(value) or currentScale())
for object, state in pairs(objects) do
for key, record in pairs(state) do apply(object, key or nil, record) end
end
end
-- XML roots and ports loaded before the helper. Only addon-owned name prefixes;
-- walking an addon root never walks upward into UIParent or a Blizzard window.
local prefixes = {"Octo", "WoWHybrid", "TWThreat", "pfUI", "pfQuest", "pfPlayer", "pfTarget", "pfGroup", "pfRaid", "BCS", "BetterCharacterStats", "Necrosis", "aux"}
local function ownedName(name)
if type(name) ~= "string" or not name:find("^[OWTpBNa]") then return false end
for _, prefix in ipairs(prefixes) do
if name:sub(1, #prefix) == prefix then return true end
end
return false
end
-- One full _G walk costs ~50k entries; do it only for our own add-ons' ADDON_LOADED and once
-- at PLAYER_LOGIN, never for every Blizzard/third-party ADDON_LOADED (login time).
local function scanNamed()
for name, object in pairs(_G) do
if type(object) == "table" and ownedName(name) and object.GetObjectType and object.GetRegions then
API.RegisterFrame(object)
end
end
end
local subscribed
local function subscribe()
if not subscribed and TextSizeManager and TextSizeManager.RegisterObject then
subscribed = true
TextSizeManager:RegisterObject(API)
end
end
scale = currentScale()
local events = originalCreateFrame("Frame")
for _, event in ipairs({"ADDON_LOADED", "PLAYER_LOGIN", "CVAR_UPDATE", "PLAYER_REGEN_ENABLED"}) do events:RegisterEvent(event) end
events:SetScript("OnEvent", function(_, event, name)
if event == "CVAR_UPDATE" and name ~= "userFontScale" then return end
subscribe()
if event == "PLAYER_LOGIN" or (event == "ADDON_LOADED" and ownedName(name)) then scanNamed() end
API:OnTextScaleUpdated(currentScale())
end)
subscribe()