Scale issue with emoji using SetDrawOrigin (repro)

Hello,

It seems there is an issue with the width/height of emoji in text with SetDrawOrigin when changing the scale or maybe drawing multiple emojis.

The issue is that the scale is often wrong and not dynamic.
You can check a video here:
Around 00:03 you can see the scale changing, you can also see that the scale of emoji does not adapt when the scale of the text changes.

Here is a simple repro:

local function drawText(intFont, strText, floatScale, intPosX, intPosY, color, boolShadow, intAlign, addWarp)
	SetTextFont(intFont)
	SetTextScale(floatScale, floatScale)
	if boolShadow then
		SetTextDropShadow(0, 0, 0, 0, 0)
	end
	SetTextColour(color[1], color[2], color[3], 255)
	SetTextJustification(intAlign or 1)
	if intAlign == 2 then
		SetTextWrap(.0, addWarp or intPosX)
	end
	BeginTextCommandDisplayText("STRING")
	AddTextComponentSubstringPlayerName(strText)
	EndTextCommandDisplayText(intPosX, intPosY)
end


local scale = 0.5
CreateThread(function()
    while true do
        Wait(400)

        if scale == 0.5 then
            scale = 0.75
        else
            scale = 0.5
        end
    end
end)

CreateThread(function()
    while true do
        Wait(0)

        local ped = PlayerPedId()
        local fwdVec, rgtVec, upVec, posVec = GetEntityMatrix(ped)
        local lastPosition = posVec + fwdVec * 2 + vec3(0, 0, 1)

        SetDrawOrigin(lastPosition.x, lastPosition.y, lastPosition.z, 0)
            drawText(0, "๐Ÿš” ~r~Some text ๐Ÿš”", scale, 0.0, 0.0, { 255, 255, 255, 255 }, false, 1)
        ClearDrawOrigin()

        SetDrawOrigin(lastPosition.x, lastPosition.y, lastPosition.z, 0)
            drawText(0, "๐Ÿš” ~r~Some text ๐Ÿš”", 0.5, 0.0, 0.2, { 255, 255, 255, 255 }, false, 1)
        ClearDrawOrigin()
    end
end)

One solution is to replace the wstring key with a struct that includes text size (see below).

However, when used in 3D contexts (where the size can rapidly change) it would go against what the function is trying to optimize: tweak: cache emoji-regex text to increase performance of text draws ยท citizenfx/fivem@d159626 ยท GitHub

template<typename S>
struct TextKey
{
    S key;
    int size;

    TextKey(const S& key_, int size_)
        : key(key_), size(size_)
    {
    }
};

// Allow transparent wstring/wstring_view comparators 

template<typename T, typename S = T>
bool operator==(const TextKey<T>& lhs, const TextKey<S>& rhs)
{
    return (lhs.key == rhs.key) && (lhs.size == rhs.size);
}

template<typename T, typename S = T>
bool operator<(const TextKey<T>& lhs, const TextKey<S>& rhs)
{
    return (lhs.key < rhs.key) ? true : lhs.size < rhs.size;
}
1 Like

It seems like making the width/height a placeholder to replace later would solve this as well - this is likely an oversight in the caching logic.

1 Like

Thank you for the fix btw.
I just discovered that the opacity is also not updating when the text color alpha/opacity is updated.
Just like the example above, the text opacity is correctly updated for the text but the emoji does not update.