mirror of
https://github.com/gnustep/libs-gui.git
synced 2025-04-26 23:20:44 +00:00
Allow the caller to require the generated attributed string to be of a
specific class/subclass git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/gui/trunk@13839 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
parent
5c189130c1
commit
3550e38fea
2 changed files with 64 additions and 20 deletions
|
@ -40,6 +40,7 @@
|
||||||
NSMutableArray *colours;
|
NSMutableArray *colours;
|
||||||
NSMutableArray *attrs;
|
NSMutableArray *attrs;
|
||||||
NSMutableAttributedString *result;
|
NSMutableAttributedString *result;
|
||||||
|
Class _class;
|
||||||
int ignore;
|
int ignore;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -211,12 +211,27 @@ readNSString (StringContext *ctxt)
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|
||||||
|
static BOOL classInheritsFromNSMutableAttributedString (Class c)
|
||||||
|
{
|
||||||
|
Class mutable = [NSMutableAttributedString class];
|
||||||
|
|
||||||
|
while (c != Nil)
|
||||||
|
{
|
||||||
|
if (c == mutable)
|
||||||
|
{
|
||||||
|
return YES;
|
||||||
|
}
|
||||||
|
c = [c superclass];
|
||||||
|
}
|
||||||
|
return NO;
|
||||||
|
}
|
||||||
|
|
||||||
@interface RTFConsumer (Private)
|
@interface RTFConsumer (Private)
|
||||||
|
|
||||||
- (NSAttributedString*) parseRTF: (NSData *)rtfData
|
- (NSAttributedString*) parseRTF: (NSData *)rtfData
|
||||||
documentAttributes: (NSDictionary **)dict;
|
documentAttributes: (NSDictionary **)dict
|
||||||
|
class: (Class)class;
|
||||||
- (NSDictionary*) documentAttributes;
|
- (NSDictionary*) documentAttributes;
|
||||||
- (NSAttributedString*) result;
|
|
||||||
|
|
||||||
- (RTFAttribute*) attr;
|
- (RTFAttribute*) attr;
|
||||||
- (void) push;
|
- (void) push;
|
||||||
|
@ -258,6 +273,7 @@ readNSString (StringContext *ctxt)
|
||||||
|
|
||||||
+ (NSAttributedString*) parseFile: (NSFileWrapper *)wrapper
|
+ (NSAttributedString*) parseFile: (NSFileWrapper *)wrapper
|
||||||
documentAttributes: (NSDictionary **)dict
|
documentAttributes: (NSDictionary **)dict
|
||||||
|
class: (Class)class
|
||||||
{
|
{
|
||||||
RTFConsumer *consumer = [RTFConsumer new];
|
RTFConsumer *consumer = [RTFConsumer new];
|
||||||
NSAttributedString *text = nil;
|
NSAttributedString *text = nil;
|
||||||
|
@ -265,7 +281,8 @@ readNSString (StringContext *ctxt)
|
||||||
if ([wrapper isRegularFile])
|
if ([wrapper isRegularFile])
|
||||||
{
|
{
|
||||||
text = [consumer parseRTF: [wrapper regularFileContents]
|
text = [consumer parseRTF: [wrapper regularFileContents]
|
||||||
documentAttributes: dict];
|
documentAttributes: dict
|
||||||
|
class: class];
|
||||||
}
|
}
|
||||||
else if ([wrapper isDirectory])
|
else if ([wrapper isDirectory])
|
||||||
{
|
{
|
||||||
|
@ -277,7 +294,8 @@ readNSString (StringContext *ctxt)
|
||||||
if ((contents = [files objectForKey: @"TXT.rtf"]) != nil)
|
if ((contents = [files objectForKey: @"TXT.rtf"]) != nil)
|
||||||
{
|
{
|
||||||
text = [consumer parseRTF: [contents regularFileContents]
|
text = [consumer parseRTF: [contents regularFileContents]
|
||||||
documentAttributes: dict];
|
documentAttributes: dict
|
||||||
|
class: class];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -286,19 +304,37 @@ readNSString (StringContext *ctxt)
|
||||||
return text;
|
return text;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
+ (NSAttributedString*) parseFile: (NSFileWrapper *)wrapper
|
||||||
|
documentAttributes: (NSDictionary **)dict
|
||||||
|
{
|
||||||
|
return [self parseFile: wrapper
|
||||||
|
documentAttributes: dict
|
||||||
|
class: [NSMutableAttributedString class]];
|
||||||
|
}
|
||||||
|
|
||||||
+ (NSAttributedString*) parseData: (NSData *)rtfData
|
+ (NSAttributedString*) parseData: (NSData *)rtfData
|
||||||
documentAttributes: (NSDictionary **)dict
|
documentAttributes: (NSDictionary **)dict
|
||||||
|
class: (Class)class
|
||||||
{
|
{
|
||||||
RTFConsumer *consumer = [RTFConsumer new];
|
RTFConsumer *consumer = [RTFConsumer new];
|
||||||
NSAttributedString *text;
|
NSAttributedString *text;
|
||||||
|
|
||||||
text = [consumer parseRTF: rtfData
|
text = [consumer parseRTF: rtfData
|
||||||
documentAttributes: dict];
|
documentAttributes: dict
|
||||||
|
class: class];
|
||||||
RELEASE(consumer);
|
RELEASE(consumer);
|
||||||
|
|
||||||
return text;
|
return text;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
+ (NSAttributedString*) parseData: (NSData *)rtfData
|
||||||
|
documentAttributes: (NSDictionary **)dict
|
||||||
|
{
|
||||||
|
return [self parseData: rtfData
|
||||||
|
documentAttributes: dict
|
||||||
|
class: [NSMutableAttributedString class]];
|
||||||
|
}
|
||||||
|
|
||||||
- (id) init
|
- (id) init
|
||||||
{
|
{
|
||||||
ignore = 0;
|
ignore = 0;
|
||||||
|
@ -307,6 +343,7 @@ readNSString (StringContext *ctxt)
|
||||||
fonts = nil;
|
fonts = nil;
|
||||||
attrs = nil;
|
attrs = nil;
|
||||||
colours = nil;
|
colours = nil;
|
||||||
|
_class = Nil;
|
||||||
|
|
||||||
return self;
|
return self;
|
||||||
}
|
}
|
||||||
|
@ -325,13 +362,6 @@ readNSString (StringContext *ctxt)
|
||||||
|
|
||||||
@implementation RTFDConsumer
|
@implementation RTFDConsumer
|
||||||
|
|
||||||
+ (NSAttributedString*) parseFile: (NSFileWrapper *)wrapper
|
|
||||||
documentAttributes: (NSDictionary **)dict
|
|
||||||
{
|
|
||||||
return [super parseFile: wrapper
|
|
||||||
documentAttributes: dict];
|
|
||||||
}
|
|
||||||
|
|
||||||
+ (NSAttributedString*) parseData: (NSData *)rtfData
|
+ (NSAttributedString*) parseData: (NSData *)rtfData
|
||||||
documentAttributes: (NSDictionary **)dict
|
documentAttributes: (NSDictionary **)dict
|
||||||
{
|
{
|
||||||
|
@ -361,7 +391,15 @@ readNSString (StringContext *ctxt)
|
||||||
|
|
||||||
ignore = 0;
|
ignore = 0;
|
||||||
DESTROY(result);
|
DESTROY(result);
|
||||||
result = [[NSMutableAttributedString alloc] init];
|
|
||||||
|
if (classInheritsFromNSMutableAttributedString (_class))
|
||||||
|
{
|
||||||
|
result = [[_class alloc] init];
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
result = [[NSMutableAttributedString alloc] init];
|
||||||
|
}
|
||||||
ASSIGN(documentAttributes, [NSMutableDictionary dictionary]);
|
ASSIGN(documentAttributes, [NSMutableDictionary dictionary]);
|
||||||
ASSIGN(fonts, [NSMutableDictionary dictionary]);
|
ASSIGN(fonts, [NSMutableDictionary dictionary]);
|
||||||
ASSIGN(attrs, [NSMutableArray array]);
|
ASSIGN(attrs, [NSMutableArray array]);
|
||||||
|
@ -389,14 +427,9 @@ readNSString (StringContext *ctxt)
|
||||||
((RTFAttribute*)[attrs lastObject])->changed = YES;
|
((RTFAttribute*)[attrs lastObject])->changed = YES;
|
||||||
}
|
}
|
||||||
|
|
||||||
- (NSAttributedString*) result
|
|
||||||
{
|
|
||||||
RETAIN(result);
|
|
||||||
return AUTORELEASE(result);
|
|
||||||
}
|
|
||||||
|
|
||||||
- (NSAttributedString*) parseRTF: (NSData *)rtfData
|
- (NSAttributedString*) parseRTF: (NSData *)rtfData
|
||||||
documentAttributes: (NSDictionary **)dict
|
documentAttributes: (NSDictionary **)dict
|
||||||
|
class: (Class)class
|
||||||
{
|
{
|
||||||
CREATE_AUTORELEASE_POOL(pool);
|
CREATE_AUTORELEASE_POOL(pool);
|
||||||
RTFscannerCtxt scanner;
|
RTFscannerCtxt scanner;
|
||||||
|
@ -408,6 +441,7 @@ readNSString (StringContext *ctxt)
|
||||||
encoding: NSASCIIStringEncoding];
|
encoding: NSASCIIStringEncoding];
|
||||||
|
|
||||||
// Reset this RFTConsumer, as it might already have been used!
|
// Reset this RFTConsumer, as it might already have been used!
|
||||||
|
_class = class;
|
||||||
[self reset];
|
[self reset];
|
||||||
|
|
||||||
initStringContext(&stringCtxt, rtfString);
|
initStringContext(&stringCtxt, rtfString);
|
||||||
|
@ -428,7 +462,16 @@ readNSString (StringContext *ctxt)
|
||||||
*dict = [self documentAttributes];
|
*dict = [self documentAttributes];
|
||||||
}
|
}
|
||||||
|
|
||||||
return [self result];
|
if (classInheritsFromNSMutableAttributedString (_class))
|
||||||
|
{
|
||||||
|
RETAIN (result);
|
||||||
|
AUTORELEASE (result);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return AUTORELEASE ([[_class alloc] initWithAttributedString: result]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|
Loading…
Reference in a new issue