Hi everyone,
I’m struggeling to write some text along an arc. It is basically working, but it looks horrible. The indvidual letters aren’t rotated correctly along the path and the text seems to kind of drift off. I can’t figure out where I’m going wrong. My code looks like this:
float startAngle = 3.76991;
float endAngle = 8.79646;
outlinePath.addArc(centre.getX() - radius , centre.getY() - radius,
2 * radius, 2 * radius,
startAngle, endAngle, true);
g.setColour (juce::Colours::white);
g.strokePath (outlinePath, juce::PathStrokeType (4.0f));
// Create a GlyphArrangement and add the text
juce::GlyphArrangement glyphs;
glyphs.addFittedText (g.getCurrentFont(), "Curved", 0, 0, 100, 20, juce::Justification::left, 1);
// Calculate the total width of the text
float totalWidth = 0.0f;
for (int i = 0; i < glyphs.getNumGlyphs(); ++i)
{
totalWidth += glyphs.getGlyph(i).getBounds().getWidth();
}
// Define the middle section of the arc
const float middleSectionStart = startAngle * 0.05f;
const float middleSectionEnd = startAngle * 0.3f;
// Manually position each glyph along the middle section of the path
float currentAngle = middleSectionStart;
for (int i = 0; i < glyphs.getNumGlyphs(); ++i)
{
auto glyph = glyphs.getGlyph(i);
auto glyphWidth = glyph.getBounds().getWidth();
auto angle = currentAngle + (glyphWidth / totalWidth) * (middleSectionEnd - middleSectionStart);
auto point = centre.getPointOnCircumference (radius + (pathDistanceToSlider * 3.5), angle);
juce::AffineTransform transform;
transform = transform.translated(point.getX() - glyphWidth / 2, point.getY());
transform = transform.rotated(angle, point.getX(), point.getY());
juce::Path glyphPath;
glyph.createPath (glyphPath);
glyphPath.applyTransform (transform);
g.fillPath (glyphPath);
currentAngle += (glyphWidth / totalWidth) * (middleSectionEnd - middleSectionStart) * 0.8f; // Adjust spacing factor here
}
middleSectionStart
and middleSectionEnd
are my attempt to make the text appear in the middle of the arc.
centre.get(X)
is 125 and centre.get(Y)
is 92.5 in this case.
Ideally I would like the text to be written neatly along the arc, with normal distance between the individual letters and not spaced apart along the entire arc.
Can anyone give me any pointers where I’m going wrong?