---
interface Props {
text: string;
radius: number;
fontSize?: number;
separator?: string;
letterSpacing?: number;
wordSpacing?: number;
separatorSpacing?: number;
inside?: boolean;
reverse?: boolean;
}
const {
text,
radius,
fontSize = 16,
separator = "",
letterSpacing = 0,
wordSpacing = 0,
separatorSpacing = 16,
inside = false,
reverse = false,
} = Astro.props;
function calculateTextProperties(
text: string,
radius: number,
fontSize: number,
separator: string,
letterSpacing: number,
wordSpacing: number,
separatorSpacing: number,
) {
const circumference = 2 * Math.PI * radius;
const charSize = fontSize * 0.5 + letterSpacing;
const wordSize = text.split(" ").length > 1 ? wordSpacing : 0;
const separatorSize = separator.length * charSize + separatorSpacing;
const fullTextLength =
text.length * charSize +
wordSize * (text.split(" ").length - 1) +
separatorSize;
// Estimate how many times the text (plus separator) can fit around the circle
const repetitions = Math.floor(circumference / fullTextLength);
let finalText = "";
const halfSpacerCount = Math.round(
separatorSpacing / (2 * (fontSize * 0.5)),
);
const spacer = "\u00A0".repeat(halfSpacerCount);
for (let i = 0; i < repetitions; i++) {
if (i > 0) finalText += spacer + separator + spacer;
finalText += text;
}
// Add final separator
finalText += spacer + separator + spacer;
// Calculate the actual length of the final text
const actualTextLength = finalText.length * charSize;
// Calculate additional spacing to distribute text evenly
const additionalSpacing =
(circumference - actualTextLength) / finalText.length;
return { finalText, additionalSpacing };
}
const { finalText, additionalSpacing } = calculateTextProperties(
text.toUpperCase(),
radius,
fontSize,
separator,
letterSpacing,
wordSpacing,
separatorSpacing,
);
const svgSize = radius * 2 + fontSize;
---