It sounds like you don’t like gdscript.
Anyway I created a control node, centred it, added a child label with some text and added this script to it:
@onready var label = $Label
func _ready() -> void:
# get bounding box of label node
var bounding_box = label.get_rect()
print("Bounding Box:", bounding_box)
It outputs:
Bounding Box:[P: (0, 0), S: (149, 36)]
Which all seems fine.
Then I thought you were asking about text_size and labels have boundaries, so I added this:
func _ready() -> void:
# get bounding box of label node
var bounding_box = label.get_rect()
print("Bounding Box:", bounding_box)
# get text size after rendering on screen
await get_tree().process_frame # Wait for initial render
var font = $Label.get_theme_font("font")
var text_size = font.get_string_size($Label.text)
print("Text size:", text_size)
Which output this:
Bounding Box:[P: (0, 0), S: (149, 36)]
Text size:(92, 23)
Which all seemed fine too.
Then I thought that you were using text_server and shaped text (which admittedly I have never used). So I tried it. Now this was a fiddly affair to say the least, especially getting the RIDs and primary canvas stuff to work, but after a bit of reading in the docs and adding a font to the theme, I have this:
extends Control
@onready var label = $Label
var draw_text = false
var shaped_text
var text_server
func _ready() -> void:
# get bounding box of label node
var bounding_box = label.get_rect()
print("Bounding Box:", bounding_box)
# get text size after rendering on screen
await get_tree().process_frame # Wait for initial render
var font = $Label.get_theme_font("font")
var text_size = font.get_string_size($Label.text)
print("Text size:", text_size)
# Do it again using TextServerManager
await get_tree().process_frame
var new_font = label.get_theme_font("font")
var new_font_size = label.get_theme_font_size("font_size")
text_server = TextServerManager.get_primary_interface()
var font_rids = new_font.get_rids()
shaped_text = text_server.create_shaped_text()
text_server.shaped_text_add_string(shaped_text, "Hello World!", [font_rids[0]], new_font_size)
text_server.shaped_text_shape(shaped_text)
var size_of_text = text_server.shaped_text_get_size(shaped_text)
print("Shaped Text size:", size_of_text)
draw_text = true
queue_redraw()
func _draw():
if draw_text:
var canvas_item = self.get_canvas_item()
var text_position = Vector2(200,200)
var color = Color(1, 1, 1)
text_server.shaped_text_draw(shaped_text, canvas_item, text_position, -1, -1, color)
Which outputs this:
Bounding Box:[P: (0, 0), S: (149, 36)]
Text size:(92, 23)
Shaped Text size:(149, 36)
So that all seems to be working fine too.
Try replacing your get_rid with get_rids and your reference to it as [font_rids[0]] perhaps. I must admit the text_server stuff was fiddly, I have no plans to start using it anytime soon, but it does work.