How can I calculate text size without using DrawHUD?

Currently I am using the following code:

//if (IsCanvasValid_WarnIfNot())
{
	UFont* Font = HUDFonts[fontID];
	Canvas->TextSize(Font ? Font : GEngine->GetMediumFont(), txt, w, h, scale, scale);
}

However, it returns the wrong text size. I’ve read that this should only be accessed from the DrawHUD() function. Is it possible to instantly calculate the size outside of the DrawHUD() function? I would like to call this function from C# by posting the parameters and return the size directly.

Any ideas?

That’s a tough one. You need to take into consider things like kerning which I believe is done at render time. If you know your font details and you know they won’t change (bold, size, etc) then you could take the size of the largest letter (W in many cases) and get a ballpark of what what coefficients you need for it. You might be able to work out a reliable formula after some trial and error but I don’t think there is any easy way to do this.