[4.7 Stable] - Drawing Text Centered On A Horizontal Pixel Point - Source Code Provided - Not Currently Working

Godot Version

v4.7.stable.official [5b4e0cb0f] - Linux x86_64

Question

Hi,

We are trying to draw text centered on a horizontal pixel point, but it is not working correctly.
Please look at below GDScript source code and suggest a fix, thank you!

SS

func DrawText(index, text, x, y, horizontalJustification, fontToUse, fontSize, scaleX, scaleY, rotations, red, green, blue, alpha, outlineRed, outlineGreen, outlineBlue):
if ( index > (TextCurrentIndex-1) ):
Texts.TextImage.append(RichTextLabel.new())
add_child(Texts.TextImage[index])
TextIsUsed[index] = true

var newTextDrawingOffsetY = 0

fontSize+=10
y-=8

var xValue = x

if horizontalJustification == 0:
	Texts.TextImage[index].text = text # No Justification
	Texts.TextImage[index].set_use_bbcode(false)
elif horizontalJustification == 1:
	Texts.TextImage[index].text = "[center]"+text+"[/center]" # Justify Center
	Texts.TextImage[index].set_use_bbcode(true)
elif horizontalJustification == 2: # Justify Right
	Texts.TextImage[index].text = "[right]"+text+"[/right]"
	Texts.TextImage[index].set_use_bbcode(true)
elif horizontalJustification == 4: # Center On Horizontal Pixel Point (Not working correctly?)
	Texts.TextImage[index].text = text
	Texts.TextImage[index].set_use_bbcode(false)
	var textWidth = Texts.TextImage[index].get_theme_font("normal_font").get_string_size(Texts.TextImage[index].text).x
	xValue = x - (textWidth/2)

Texts.TextImage[index].clip_contents = false
Texts.TextImage[index].add_theme_font_override("normal_font", FontTTF[fontToUse])
Texts.TextImage[index].add_theme_font_size_override("normal_font_size", fontSize)
Texts.TextImage[index].add_theme_color_override("default_color", Color(red, green, blue, alpha))
Texts.TextImage[index].add_theme_constant_override("outline_size", 20.0)
Texts.TextImage[index].add_theme_color_override("font_outline_color", Color(outlineRed, outlineGreen, outlineBlue, alpha)) 

var textHeight = Texts.TextImage[index].get_theme_font("normal_font").get_string_size(Texts.TextImage[index].text).y

Texts.TextImage[index].global_position.x = xValue#x
Texts.TextImage[index].global_position.y = (y - newTextDrawingOffsetY)
Texts.TextImage[index].set_size(Vector2(VisualsCore.ScreenWidth, VisualsCore.ScreenHeight), false)
Texts.TextImage[index].pivot_offset = Vector2((VisualsCore.ScreenWidth / 2.0), (textHeight / 2.0))
Texts.TextImage[index].scale = Vector2(scaleX, scaleY)
Texts.TextImage[index].rotation = rotations

Texts.TextIndex.append(index)
Texts.TextScreenX.append(x)
Texts.TextScreenY.append(y)
Texts.TextHorizontalJustification.append(horizontalJustification)
Texts.TextSize.append(fontSize)
Texts.TextScaleX.append(scaleX)
Texts.TextScaleY.append(scaleY)
Texts.TextRotation.append(rotations)
Texts.TextColorRed.append(red)
Texts.TextColorGreen.append(green)
Texts.TextColorBlue.append(blue)
Texts.TextColorAlpha.append(alpha)
Texts.TextOutlineRed.append(outlineRed)
Texts.TextOutlineGreen.append(outlineGreen)
Texts.TextOutlineBlue.append(outlineBlue)

if ( index > (TextCurrentIndex-1) ):
	TextCurrentIndex+=1

return(TextCurrentIndex-1)


In what way?