diff --git a/Source/Parsers/RTFProducer.h b/Source/Parsers/RTFProducer.h index 24bce1d2c..d0ed63e35 100644 --- a/Source/Parsers/RTFProducer.h +++ b/Source/Parsers/RTFProducer.h @@ -43,7 +43,10 @@ */ } -+ (NSData*) RTFDFromAttributedString: (NSAttributedString*) aText - documentAttributes: (NSDictionary*)dict; ++ (NSData*) produceRTF: (NSAttributedString*) aText + documentAttributes: (NSDictionary*)dict; ++ (NSFileWrapper*) produceRTFD: (NSAttributedString*) aText + documentAttributes: (NSDictionary*)dict; + @end diff --git a/Source/Parsers/RTFProducer.m b/Source/Parsers/RTFProducer.m index 584b9c4bb..8e35e32c8 100644 --- a/Source/Parsers/RTFProducer.m +++ b/Source/Parsers/RTFProducer.m @@ -95,14 +95,46 @@ @implementation RTFProducer -+ (NSData*) RTFDFromAttributedString: (NSAttributedString*) aText - documentAttributes: (NSDictionary*)dict ++ (NSFileWrapper*) produceRTFD: (NSAttributedString*) aText + documentAttributes: (NSDictionary*)dict { RTFProducer *new = [self new]; - NSData *data = [[new RTFDStringFromAttributedString: aText - documentAttributes: dict] - dataUsingEncoding: NSISOLatin1StringEncoding]; + NSData *data; + NSFileWrapper *wrapper; + data = [[new RTFDStringFromAttributedString: aText + documentAttributes: dict] + dataUsingEncoding: NSISOLatin1StringEncoding]; + + if ([aText containsAttachments]) + { + NSMutableDictionary *fileDict = [NSMutableDictionary dictionary]; + NSFileWrapper *txt = [[NSFileWrapper alloc] + initRegularFileWithContents: data]; + + [fileDict setObject: txt forKey: @"TXT.rtf"]; + RELEASE(txt); + // FIXME: We have to add the attachments to the directory file wrapper + + wrapper = [[NSFileWrapper alloc] initDirectoryWithFileWrappers: fileDict]; + } + else + wrapper = [[NSFileWrapper alloc] initRegularFileWithContents: data]; + + + RELEASE(new); + return AUTORELEASE(wrapper); +} + ++ (NSData*) produceRTF: (NSAttributedString*) aText + documentAttributes: (NSDictionary*)dict +{ + RTFProducer *new = [self new]; + NSData *data; + + data = [[new RTFDStringFromAttributedString: aText + documentAttributes: dict] + dataUsingEncoding: NSISOLatin1StringEncoding]; RELEASE(new); return data; } diff --git a/Source/Parsers/attributedStringConsumer.m b/Source/Parsers/attributedStringConsumer.m index 90f05171f..57eb23437 100644 --- a/Source/Parsers/attributedStringConsumer.m +++ b/Source/Parsers/attributedStringConsumer.m @@ -206,17 +206,10 @@ readNSString(StringContext *ctxt) @end -@interface RTFConsumer: NSObject -{ -@public - NSMutableDictionary *documentAttributes; - NSMutableDictionary *fonts; - NSMutableArray *colours; - NSMutableArray *attrs; - NSMutableAttributedString *result; - int ignore; -} +@interface RTFConsumer (Private) +- (NSAttributedString*) parseRTF: (NSData *)rtfData + documentAttributes: (NSDictionary **)dict; - (NSDictionary*) documentAttributes; - (NSAttributedString*) result; @@ -228,6 +221,45 @@ readNSString(StringContext *ctxt) @implementation RTFConsumer ++ (NSAttributedString*) parseRTFD: (NSFileWrapper *)wrapper + documentAttributes: (NSDictionary **)dict +{ + RTFConsumer *consumer = [RTFConsumer new]; + NSAttributedString *text = nil; + + if ([wrapper isRegularFile]) + text = [consumer parseRTF: [wrapper regularFileContents] + documentAttributes: dict]; + else if ([wrapper isDirectory]) + { + NSDictionary *files = [wrapper fileWrappers]; + NSFileWrapper *contents; + + //FIXME: We should store the files in the consumer + // We try to read the main file in the directory + if ((contents = [files objectForKey: @"TXT.rtf"]) != nil) + text = [consumer parseRTF: [contents regularFileContents] + documentAttributes: dict]; + } + + RELEASE(consumer); + + return text; +} + ++ (NSAttributedString*) parseRTF: (NSData *)rtfData + documentAttributes: (NSDictionary **)dict +{ + RTFConsumer *consumer = [RTFConsumer new]; + NSAttributedString *text; + + text = [consumer parseRTF: rtfData + documentAttributes: dict]; + RELEASE(consumer); + + return text; +} + - (id) init { ignore = 0; @@ -250,6 +282,10 @@ readNSString(StringContext *ctxt) [super dealloc]; } +@end + +@implementation RTFConsumer (Private) + - (NSDictionary*) documentAttributes { RETAIN(documentAttributes); @@ -777,15 +813,3 @@ void GSRTFparagraph(void *ctxt) GSRTFmangleText(ctxt, "\n"); CTXT->tabChanged = NO; } - -NSAttributedString *parseRTFintoAttributedString(NSData *rtfData, - NSDictionary **dict) -{ - RTFConsumer *consumer = [RTFConsumer new]; - NSAttributedString *result; - - result = [consumer parseRTF: rtfData documentAttributes: dict]; - RELEASE(consumer); - - return result; -} diff --git a/Source/Parsers/rtfConsumer.h b/Source/Parsers/rtfConsumer.h index d1c13900f..7a76d016c 100644 --- a/Source/Parsers/rtfConsumer.h +++ b/Source/Parsers/rtfConsumer.h @@ -27,7 +27,22 @@ #ifndef _rtfConsumer_h_INCLUDE #define _rtfConsumer_h_INCLUDE -NSAttributedString *parseRTFintoAttributedString(NSData *rtfData, - NSDictionary **dict); +@interface RTFConsumer: NSObject +{ +@public + NSMutableDictionary *documentAttributes; + NSMutableDictionary *fonts; + NSMutableArray *colours; + NSMutableArray *attrs; + NSMutableAttributedString *result; + int ignore; +} + ++ (NSAttributedString*) parseRTF: (NSData *)rtfData + documentAttributes: (NSDictionary **)dict; ++ (NSAttributedString*) parseRTFD: (NSFileWrapper *)rtfFile + documentAttributes: (NSDictionary **)dict; + +@end #endif