views:

127

answers:

3

For an application I am building I have drawn 2 circles. One a bit bigger than the other. I want to curve text between those lines, for a circular menu I am building.

I read most stuff about curving a text that you have to split up your text in characters and draw each character on it's own with the right angle in mind (by rotating the context you are drawing on).

I just can't wrap my head around on how to get the right angles and positions for my characters.

I included a screenshot on what the menu, at the moment, look like. Only the texts I added by are loaded from an image in an UIImageView.

alt text

I hope someone can get me some starting points on how I can draw the text in the white circle, at certain points.

EDIT: Ok, I am currently at this point:

alt text

I accomplish by using the following code:

- (UIImage*) createMenuRingWithFrame:(CGRect)frame
{
    CGRect imageSize = CGRectMake(0,0,300,300);
    float perSectionDegrees = 360 / [sections count];
    float totalRotation = 90;
    char* fontName = (char*)[self.menuItemsFont.fontName cStringUsingEncoding:NSASCIIStringEncoding];

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef context = CGBitmapContextCreate(NULL, imageSize.size.width, imageSize.size.height, 8, 4 * imageSize.size.width, colorSpace, kCGImageAlphaPremultipliedFirst);

    CGContextSetTextMatrix(context, CGAffineTransformIdentity);
    CGContextSelectFont(context, fontName, 18, kCGEncodingMacRoman);

    CGContextSetRGBFillColor(context, 0, 0, 0, 1);

    CGPoint centerPoint = CGPointMake(imageSize.size.width / 2, imageSize.size.height / 2);
    double radius = (frame.size.width / 2);

    CGContextStrokeEllipseInRect(context, CGRectMake(centerPoint.x - (frame.size.width / 2), centerPoint.y - (frame.size.height / 2), frame.size.width, frame.size.height));

    for (int index = 0; index < [sections count]; index++)
    {
        NSString* menuItemText = [sections objectAtIndex:index];
        CGSize textSize = [menuItemText sizeWithFont:self.menuItemsFont];
        char* menuItemTextChar = (char*)[menuItemText cStringUsingEncoding:NSASCIIStringEncoding];

        float x = centerPoint.x + radius * cos(degreesToRadians(totalRotation));
        float y = centerPoint.y + radius * sin(degreesToRadians(totalRotation));

        CGContextSaveGState(context);

        CGContextTranslateCTM(context, x, y);
        CGContextRotateCTM(context, degreesToRadians(totalRotation - 90));
        CGContextShowTextAtPoint(context, 0 - (textSize.width / 2), 0 - (textSize.height / 2), menuItemTextChar, strlen(menuItemTextChar));

        CGContextRestoreGState(context);

        totalRotation += perSectionDegrees;
    }

    CGImageRef contextImage = CGBitmapContextCreateImage(context);

    CGContextRelease(context);
    CGColorSpaceRelease(colorSpace);

    return [UIImage imageWithCGImage:contextImage];
}

These are the variables I use in there:

NSArray* sections = [[NSArray alloc] initWithObjects:@"settings", @"test", @"stats", @"nog iets", @"woei", @"woei2", nil];
self.menuItemsFont = [UIFont fontWithName:@"VAGRounded-Bold" size:18];

The rotation of the words seem correct, the placement also. Now I need somehow figure out at which rotation the letters (and their coordinates) should be. I could use some help with that.

Edit: Fixed! Check out the following code!

- (void) drawStringAtContext:(CGContextRef) context string:(NSString*) text atAngle:(float) angle withRadius:(float) radius
{
    CGSize textSize = [text sizeWithFont:self.menuItemsFont];

    float perimeter = 2 * M_PI * radius;
    float textAngle = textSize.width / perimeter * 2 * M_PI;

    angle += textAngle / 2;

    for (int index = 0; index < [text length]; index++)
    {
        NSRange range = {index, 1};
        NSString* letter = [text substringWithRange:range];     
        char* c = (char*)[letter cStringUsingEncoding:NSASCIIStringEncoding];
        CGSize charSize = [letter sizeWithFont:self.menuItemsFont];

        NSLog(@"Char %@ with size: %f x %f", letter, charSize.width, charSize.height);

        float x = radius * cos(angle);
        float y = radius * sin(angle);

        float letterAngle = (charSize.width / perimeter * -2 * M_PI);

        CGContextSaveGState(context);
        CGContextTranslateCTM(context, x, y);
        CGContextRotateCTM(context, (angle - 0.5 * M_PI));
        CGContextShowTextAtPoint(context, 0, 0, c, strlen(c));
        CGContextRestoreGState(context);

        angle += letterAngle;
    }
}

- (UIImage*) createMenuRingWithFrame:(CGRect)frame
{
    CGPoint centerPoint = CGPointMake(frame.size.width / 2, frame.size.height / 2);
    char* fontName = (char*)[self.menuItemsFont.fontName cStringUsingEncoding:NSASCIIStringEncoding];

    CGFloat* ringColorComponents = (float*)CGColorGetComponents(ringColor.CGColor);
    CGFloat* textColorComponents = (float*)CGColorGetComponents(textColor.CGColor);

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef context = CGBitmapContextCreate(NULL, frame.size.width, frame.size.height, 8, 4 * frame.size.width, colorSpace, kCGImageAlphaPremultipliedFirst);

    CGContextSetTextMatrix(context, CGAffineTransformIdentity);

    CGContextSelectFont(context, fontName, 18, kCGEncodingMacRoman);
    CGContextSetRGBStrokeColor(context, ringColorComponents[0], ringColorComponents[1], ringColorComponents[2], ringAlpha);
    CGContextSetLineWidth(context, ringWidth);  

    CGContextStrokeEllipseInRect(context, CGRectMake(ringWidth, ringWidth, frame.size.width - (ringWidth * 2), frame.size.height - (ringWidth * 2)));
    CGContextSetRGBFillColor(context, textColorComponents[0], textColorComponents[1], textColorComponents[2], textAlpha);

    CGContextSaveGState(context);
    CGContextTranslateCTM(context, centerPoint.x, centerPoint.y);

    float angleStep = 2 * M_PI / [sections count];
    float angle = degreesToRadians(90);

    textRadius = textRadius - 12;

    for (NSString* text in sections)
    {
        [self drawStringAtContext:context string:text atAngle:angle withRadius:textRadius];
        angle -= angleStep;
    }

    CGContextRestoreGState(context);

    CGImageRef contextImage = CGBitmapContextCreateImage(context);

    CGContextRelease(context);
    CGColorSpaceRelease(colorSpace);

    [self saveImage:[UIImage imageWithCGImage:contextImage] withName:@"test.png"];
    return [UIImage imageWithCGImage:contextImage];

}
A: 

Check out this Apple sample project: CoreTextArcCocoa

Demonstrates using Core Text to draw text along an arc in a Cocoa application. As well, this sample illustrates how you can use the Cocoa font panel to receive font settings that can be used by Core Text to select the font used for drawing.

CoreText is also available in iOS so you should be able to implement something similar.

TomH
Thanks for your reply. But somehow I can not get that to work nicely. Either my text is way off posittion or it isn't appearing at all. I had it working for 4 menu items, and now I wanted to add a fifth item and then everything is borked again. I think I need some more help.
Wim Haanstra
Hmmmmm. Probably time to post some code?
TomH
I added some more code to my post. Explaining where I stand at the moment.
Wim Haanstra
+1  A: 

I tried to work it out quickly on paper, so i may be wrong :)

Convert the length of the string into units on the UnitCircle. Thus (string.lenght/ circle perimeter)*2Pi. You now have the angle in radians for the whole string. (That is the angle between start and end of the string)

For the separate letters you could do the same to get the angle (in radians) for individual letters (using letter widths)

Once you have the angle in radians you can work out the x and y position (and rotation) of the letters.

Bonus: for even spacing you could even work out the ratio between the total length of all strings and the whole perimeter. And divide the remaining space equally between the string.

Update I made a proof of concept using html5/canvas, so view it with a decent browser :) You should be able to port it. (mind you, the code isn't commented)
wtf: the code runs fine with the chrome debug console open, and fails when it is closed. (workaround: open chrome console: ctrl-shift-j and reload the page: f5); FF3.6.8 seems to do fine, but the letters 'dance'.

Dribbel
I am not that much of a math wonder. But I am missing something I believe. I divided my string in characters and I know the width+height of those, but how do I know at which angle they should be placed? I took your formula's and incorporated them in the code, but the angle isn't actually dependent on the location it seems?I suck at this ;)
Wim Haanstra
I didn't add any calculations for the orientation of the characters (yet) First orde is to get the position right (try it with dots) Then get the orientation right. The baseline of the characters is orthogonal to the angle
Dribbel
Thank you Dribbel for providing me with the solution. I will update my post shortly to provide the answer
Wim Haanstra
+1  A: 

Take the circumference of the inner circle. This is the circle you want the base of the characters to be rendered onto. We'll call this circumference totalLength.

I assume you have a list of strings to render around the circle in textItems.

Take the width of each string into a textWidths array and distribute them evenly across totalLength, perhaps like this pseudo(pythonish) code:

block = max(textWidths)
assert(block * len(textWidths) <= totalLength)
offsets = [(block * i) + ((block-width) / 2) for i, width in enumerate(textWidths)]

Although better layouts can no doubt be done in the cases where the assert would trigger, all that really matters is that we know where individual words start and end in a known area. To render on a straight line of length totalLength we simply start rendering each block of text at offsets[i].

To get it onto the circle, we'll map that straight line back onto the circumference. To do that we need to map each pixel along that line onto a position on the circle and an angle. This function converts the offset along that line into an angle (it takes values in the range 0 to totalLength)

def offsetToAngle(pixel):
    ratio = pixel / totalLength
    angle = math.pi * 2 * ratio # cool kids use radians.
    return angle

that's your angle. To get a position:

def angleToPosition(angle, characterWidth):
    xNorm = math.sin(angle + circleRotation)
    yNorm = math.cos(angle + circleRotation)

    halfCWidth = characterWidth / 2
    x = xNorm * radius + yNorm * halfCWidth # +y = tangent
    y = yNorm * radius - xNorm * halfCWidth # -x = tangent again.

    # translate to the circle centre
    x += circleCentre.x
    y += circleCentre.y

    return x,y

That's a bit more tricky. This is pretty much the crux of your issues, I'd have thought. The big deal is that you need to offset back along the tangent of the circle to work out the point to start rendering so that the middle of the character hits the radius of the circle. What constitues 'back' depends on your coordinate system. if 0,0 is in the bottom left, then the signs of the tangent components is swapped. I assumed top left.

This is important: I'm also making a big assumption that the text rotation occurs around the bottom left of the glyph. If it doesn't then things will look a bit weird. It will be more noticeable at larger font sizes. There is always a way to compensate for wherever it rotates around, and there's usually a way to tell the system where you want the rotation origin to be (that will be related to the CGContextTranslateCTM call in your code I'd imagine) you'll need to do a small experiment to get characters drawing at a single point rotating around their bottom left.

circleRotation is just an offset so you can rotate the whole circle, rather than having things always be in the same orientation. That's in radians too.

so now for each character in each block of text:

for text, offset in zip(textItems, offsets):
    pix = offset # start each block at the offset we calculated earlier.
    for c in text:
        cWidth = measureGlyph(c)
        # choose the circumference location of the middle of the character
        # this is to match with the tangent calculation of tangentToOffset
        angle = offsetToAngle(pix + cWidth / 2)
        x,y = angleToPosition(angle, cWidth)
        drawGlyph(c, x, y, angle)

        pix += cWidth # start of next character in circumference space

That's the concept, anyway.

Tom Whittock
of course if you can draw and rotate a glyph from its bottom middle then you don't need the tangent stuff.
Tom Whittock