← Pixel Dreamer Studio

Text around a circle with even gaps

Give each letter an arc as wide as the letter itself, plus the gap, instead of the same angle for every letter.

Most ways of putting text around a circle, like the rune ring of a magic circle, give every letter the same angle. Letters are not the same width, so the gaps change. In our Godot 4.7 test, 27 letters of the default font at 24 px, from 6 to 23 px wide, at equal angles around a circle of 120 px radius had gaps of 7.4 to 18.4 px. Equal steps sized from the average width made the wide letters overlap, with gaps from -3.9 to 7.1 px. The script below gives each letter an arc as wide as itself plus the gap: 2.00 px between every pair, or 13.37 px all the way round with fill_circle.

The code

Add a Node2D, attach this script, and set text, radius and font_size in the Inspector. It is a @tool script, so the ring shows in the editor too. The letters stand on the circle facing outward, starting at start_degrees, where -90 is the top. Turn on fill_circle to spread them all the way round.

@tool
extends Node2D
## Text around a circle, like the rune ring of a magic circle, with the same gap between every pair of letters.
## Each letter gets an arc as wide as the letter itself, so a W and an I each take their own room.

@export var text := "AEGIS OF THE WANDERING MOON":
	set(v):
		text = v
		queue_redraw()
@export var radius := 120.0:
	set(v):
		radius = v
		queue_redraw()
@export var font: Font  ## empty = the default font
@export var font_size := 24:
	set(v):
		font_size = v
		queue_redraw()
@export var spacing := 2.0  ## pixels between letters, along the circle
@export var start_degrees := -90.0  ## where the first letter starts: -90 is the top
@export var fill_circle := false  ## spread the letters all the way round
@export var color := Color.WHITE


func _font() -> Font:
	return font if font else ThemeDB.fallback_font


## Returns [letter, angle of its centre in radians, its width] for every letter. The gap used is in gap_used.
var gap_used := 0.0


func layout() -> Array:
	var f := _font()
	var widths: Array[float] = []
	var total := 0.0
	for i in text.length():
		var w := f.get_char_size(text.unicode_at(i), font_size).x
		widths.append(w)
		total += w
	gap_used = spacing
	if fill_circle and text.length() > 0:
		gap_used = (TAU * radius - total) / text.length()
	var out := []
	var along := 0.0
	for i in text.length():
		var a := deg_to_rad(start_degrees) + (along + widths[i] * 0.5) / radius
		out.append([text[i], a, widths[i]])
		along += widths[i] + gap_used
	return out


func _draw() -> void:
	var f := _font()
	for g in layout():
		var a: float = g[1]
		draw_set_transform(Vector2(cos(a), sin(a)) * radius, a + PI * 0.5)
		draw_char(f, Vector2(-g[2] * 0.5, 0.0), g[0], font_size, color)
	draw_set_transform(Vector2.ZERO)

What we measured

The words AEGIS OF THE WANDERING MOON, 27 letters with the spaces, in Godot's default font at 24 px: 6.0 to 23.0 px wide, 393 px in all, on a circle of 120 px radius, 754 px round. We measured the gap along the circle from each letter to the next. Two runs gave the same numbers.

Why it happens

An angle on a circle is a length: the angle times the radius. Giving every letter the same angle gives every letter the same length, so a narrow I sits in a big space and a W fills its space edge to edge. Using each letter's own width from the font, Font.get_char_size(), for its arc keeps the space between letters the same. To fill the circle, share what is left of the circle equally between the gaps.

What to change

Ready-made magic circles: PD Sigil Forge Pro generates magic circles in 8 styles and exports large PNGs, SVG and spinning animations.

Tested in Godot 4.7 with the Compatibility renderer.

Ready-made: PD Sigil Forge Pro generates magic circles in 8 styles and exports large PNGs, SVG and spinning animations.

More how-tos: Auras, rings, beams and weather