« Return to Thread: Drawing strings on a line

Re: Drawing strings on a line

by Andreas Höschler :: Rate this Message:

Reply to Author | View in Thread

Hi David,

>>
>> 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.

Thanks for your responses. Since neither core foundation (MacOSX) nor
cairo can be used I did stick to the above approach. The method that
actually works for me is

- (void)drawString:(NSString *)str atPoint:(NSPoint)point
angle:(float)angle offset:(BOOL)offset
{
    static NSDictionary *_attr = nil;
    if (!_attr) _attr = [[NSDictionary alloc]
initWithObjectsAndKeys:LABELFONT, NSFontAttributeName, nil];
    NSSize size = [str sizeWithAttributes:_attr];
    NSAffineTransform * transform = [NSAffineTransform transform];
    [transform translateXBy: point.x yBy: point.y];
    [transform rotateByDegrees:angle];
    [transform translateXBy: (offset ? - size.width / 2 : 0.0) yBy: -
size.height / 2]; // <--
    [NSGraphicsContext saveGraphicsState];
    [transform concat];
    [str drawAtPoint:NSMakePoint(0,0) withAttributes:_attr];
    [NSGraphicsContext restoreGraphicsState];
}

Thanks a lot,

   Andreas



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

 « Return to Thread: Drawing strings on a line