private float getMaxFontSize(Graphics g, int width, int height) {
		if (bestFontSize > 0) {
			return bestFontSize;
		}

		float size = 12f;
		Font font = g.getFont().deriveFont(size); // reasonable default
		if (textFitsInFont(g, font, width, height)) {
			bestFontSize = size;
			return bestFontSize;
		}

		do {
			size--;
			font = g.getFont().deriveFont(size);
		}
		while (!textFitsInFont(g, font, width, height));

		bestFontSize = Math.max(1f, size);
		return bestFontSize;
	}
An icon that paints the given number
Ghidra/Framework/Docking/src/main/java/docking/resources/icons/NumberIcon.java