« Return to Thread: Drawing strings on a line

Re: Drawing strings on a line

by David Chisnall :: Rate this Message:

Reply to Author | View in Thread


On 3 Jun 2009, at 19:19, Andreas Höschler wrote:

> Hello all,
>
> I am developing a mapping application where I need to draw labels in  
> a view. Currently I am using (as a first approach)
>
>      [_name drawAtPoint:labelPoint withAttributes:nil];
>
> This works: However, it does not exactly has the desired effect,  
> since the labels are always drawn horizontally. The labels are  
> actually street names and the streets are drawn with NSBezierPath  
> stroke. Ideally I would like the street label to be exactly drawn on  
> the NSBezierPath which is a connected list of points, but at least  
> to be drawn with the same angle as the NSBezierPath segment. There  
> is this NSBezierPath
>
> - (void)appendBezierPathWithGlyphs:(NSGlyph *)glyphs count:
> (int)count inFont:(NSFont *)fontObj

This is, without any doubts in my mind, the worst-named method in the  
whole of AppKit.  It appends a set of gylphs, but they are drawn  
horizontally at the current point, not along the path.  I've not  
tested it on GNUstep, but the easiest way of doing this on OS X is to  
do something like this:

void drawAtPointWithAngle(NSAttributedString *str, NSPoint p, CGFloat  
angle)
{
     NSAffineTransform * transform = [NSAffineTransform transform];
     [transform translateXBy: -p.x yBy: -p.y];
     [transform rotateByDegrees: angle];
     [NSGraphicsContext saveGraphicsState];
     [transform concat];
     [str drawAtPoint: NSMakePoint(0,0)];
     [NSGraphicsContext restoreGraphicsState];
}

Assuming I've not mixed up the transforms (it's late - I probably  
have), this will set a coordinate transform for a translated, rotated,  
system, draw the string horizontally at the origin in this space (i.e.  
translated and rotated) and then reset the context.

David

P.S. Prior to 10.5, this was really slow because the  
NSAttrubutedString method set up all of the text system objects and  
then destroyed them at the end.  With 10.5, there is just one instance  
of the text system objects that is shared between all  
NSAttributedString instances, so this is very fast.  If this isn't the  
case in GNUstep, it should be, because Cocoa programmers are likely to  
use this convenience method a lot more now that it's fast for them.

_______________________________________________
Discuss-gnustep mailing list
Discuss-gnustep@...
http://lists.gnu.org/mailman/listinfo/discuss-gnustep

 « Return to Thread: Drawing strings on a line