Checking Font Weight Compatability with a Font

How would one check if a FontWeight is compatible with a certain Font using a script?

For example, Arial can’t support anything besides Regular and Bold weight. So, if I were to run a EnumItem like Enum.FontWeight.SemiBold through some type of script, how would I get a result of whether the font Arial could support SemiBold or not?

As far as I can tell, I don’t see any native support for this type of thing.

You are correct, there is no way to check natively. You would have to set up a dictionary listing the valid weights for each individual font. This is how you could do that;

-- Define a table with supported weights for specific fonts
local fontSupport = {
    Arial = { "Regular", "Bold" },
    Gotham = { "Regular", "Bold", "SemiBold", "Medium" },
    -- Add other fonts and their supported weights
}

-- Function to check if a specific font supports a given weight
local function isFontWeightSupported(fontName, fontWeight)
    -- Get the list of supported weights for the given font
    local supportedWeights = fontSupport[fontName]

    if supportedWeights then
        -- Check if the desired weight is in the list
        for _, weight in ipairs(supportedWeights) do
            if weight == fontWeight then
                return true
            end
        end
    end

    -- If not found, return false
    return false
end

-- Test examples
local fontName = "Arial"
local fontWeight = "SemiBold"

if isFontWeightSupported(fontName, fontWeight) then
    print(fontName .. " supports " .. fontWeight)
else
    print(fontName .. " does NOT support " .. fontWeight)
end

This is also worth looking into;

Yea… I was worried that would have to be the last resort…
Thanks, anyways! I should be using a limited set of Fonts anyways, so this hopefully won’t be too bad anyways.

Looks like this can be achieved with TextService:GetFamilyInfoAsync().

local TextService = game:GetService("TextService")

local function HasWeight(font: Font, weight: Enum.FontWeight)
	local success, info = pcall(function()
		return TextService:GetFamilyInfoAsync(font.Family)
	end)
	
	if not success then
		warn("Error fetching font family:", info)
		return nil
	end
	
	for _, face in info.Faces do
		if face.Weight == weight then
			return true
		end
	end
	
	return false
end

local font = Font.fromName("Arial")
print(HasWeight(font, Enum.FontWeight.Regular)) -- true
print(HasWeight(font, Enum.FontWeight.Bold))    -- true
print(HasWeight(font, Enum.FontWeight.Light))   -- false
print(HasWeight(font, Enum.FontWeight.Medium))  -- false

Oh hey. That works! Thanks mate.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.