Hi all,

I ran into some strangeness with NSAttributedString -RTFFromRange when using text table attributes. I'm actually implementing a custom NSTextStorage and ran into this when copying to the clipboard, but have been able to boil this down to a simple example using NSMutableAttributedString.

Basically the implementation of RTFFromRange gets confused in some cases and can call attributesAtIndex with an out-of-bounds index. It could be that I am the one confusing it, because I have somehow mangled the attributes, so I'd welcome any advice.

What follows is an example that sets up some table attributes programmatically on the string - in the first case making a the table span the entire string, and in the second case making it span all but the last character. In the second case, calling RTFFromRange on the string causes an exception.

- (void) testTableBug {
NSMutableAttributedString* test = [[NSMutableAttributedString alloc] initWithString:@"Testa"];
        
        // create some 'default' attributes
        NSMutableDictionary* baseAttrs = [NSMutableDictionary dictionary];
NSParagraphStyle* baseParaStyle = [NSParagraphStyle defaultParagraphStyle]; [baseAttrs setObject:baseParaStyle forKey:NSParagraphStyleAttributeName];
        
        // create some attributes with a 1-cell table
        NSMutableDictionary* tableAttrs = [NSMutableDictionary dictionary];

        NSTextTable* table = [[NSTextTable alloc] init];
        [table setNumberOfColumns:1];
        
        NSTextTableBlock* block = [[NSTextTableBlock alloc]
                                                           initWithTable:table
                                                           startingRow:0
                                                           rowSpan:1
                                                           startingColumn:0
                                                           columnSpan:1];
        
NSMutableParagraphStyle* paraStyle = [[NSMutableParagraphStyle alloc] init];
        [paraStyle setParagraphStyle:baseParaStyle];
        [paraStyle setTextBlocks:[NSArray arrayWithObjects:block,nil]];
        [tableAttrs setObject:paraStyle forKey:NSParagraphStyleAttributeName];
        
// test 1: make the table style span the whole string, the convert to RTF (will work OK)
        NSRange everything = NSMakeRange(0, [test length]);
        [test setAttributes:tableAttrs range:everything];
        
NSData* data = [test RTFFromRange:everything documentAttributes: [NSDictionary dictionary]];
        
// test 2: make the table span all but the last character, then convert again (will break)
        NSRange tableRange = NSMakeRange(0, [test length] - 1);
        NSRange baseRange = NSMakeRange(NSMaxRange(tableRange), 1);
        
        [test setAttributes:tableAttrs range:tableRange];
        [test setAttributes:baseAttrs range:baseRange];
        
data = [test RTFFromRange:everything documentAttributes:[NSDictionary dictionary]];
}

BTW, this is on 10.5.4 (Intel) and my app is GC-only.

Cheers

..Mark..

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]

Reply via email to