Include GSTextConverter.h. New function converter_class() to load

the class for a given text converter format. Use this function in
the RTF methods and use new protocol there.


git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/gui/trunk@10720 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
Fred Kiefer 2001-08-18 22:03:17 +00:00
parent 0eb432b95f
commit fa8999f56a

View file

@ -28,6 +28,8 @@
59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/ */
#include <Foundation/NSString.h>
#include <Foundation/NSRange.h>
#include <AppKit/NSAttributedString.h> #include <AppKit/NSAttributedString.h>
#include <AppKit/NSParagraphStyle.h> #include <AppKit/NSParagraphStyle.h>
#include <AppKit/NSTextAttachment.h> #include <AppKit/NSTextAttachment.h>
@ -35,11 +37,8 @@
#include <AppKit/NSColor.h> #include <AppKit/NSColor.h>
// For the colour name spaces // For the colour name spaces
#include <AppKit/NSGraphics.h> #include <AppKit/NSGraphics.h>
#include <Foundation/NSString.h>
#include <Foundation/NSRange.h>
#include "Parsers/rtfConsumer.h" #include <AppKit/GSTextConverter.h>
#include "Parsers/RTFProducer.h"
/* Cache class pointers to avoid the expensive lookup by string. */ /* Cache class pointers to avoid the expensive lookup by string. */
static Class dictionaryClass = nil; static Class dictionaryClass = nil;
@ -100,6 +99,55 @@ static inline void cache_init ()
} }
} }
/*
Return a suitable converter for the text format supplied as argument.
If producer is YES a class capable of writting that format is returned,
otherwise a class able to read the format is returned.
*/
static Class converter_class(NSString *format, BOOL producer)
{
static NSMutableDictionary *p_classes = nil;
static NSMutableDictionary *c_classes = nil;
Class found;
if (producer)
{
if (p_classes == nil)
p_classes = [NSMutableDictionary new];
found = [p_classes objectForKey: format];
if (found == Nil)
{
if ([format isEqual: @"RTF"])
found = NSClassFromString(@"RTFProducer");
else if ([format isEqual: @"RTFD"])
found = NSClassFromString(@"RTFDProducer");
if (found != Nil)
[p_classes setObject: found forKey: format];
}
return found;
}
else
{
if (c_classes == nil)
c_classes = [NSMutableDictionary new];
found = [c_classes objectForKey: format];
if (found == Nil)
{
if ([format isEqual: @"RTF"])
found = NSClassFromString(@"RTFConsumer");
else if ([format isEqual: @"RTFD"])
found = NSClassFromString(@"RTFDConsumer");
if (found != Nil)
[c_classes setObject: found forKey: format];
}
return found;
}
return Nil;
}
@implementation NSAttributedString (AppKit) @implementation NSAttributedString (AppKit)
+ (NSAttributedString *) attributedStringWithAttachment: + (NSAttributedString *) attributedStringWithAttachment:
@ -476,8 +524,9 @@ documentAttributes: (NSDictionary **)dict
- (id) initWithRTFDFileWrapper: (NSFileWrapper *)wrapper - (id) initWithRTFDFileWrapper: (NSFileWrapper *)wrapper
documentAttributes: (NSDictionary **)dict documentAttributes: (NSDictionary **)dict
{ {
NSAttributedString *new = [RTFConsumer parseRTFD: wrapper NSAttributedString *new = [converter_class(@"RTFD", NO)
documentAttributes: dict]; parseFile: wrapper
documentAttributes: dict];
// We do not return self but the newly created object // We do not return self but the newly created object
RELEASE (self); RELEASE (self);
return RETAIN (new); return RETAIN (new);
@ -486,21 +535,20 @@ documentAttributes: (NSDictionary **)dict
- (id) initWithRTFD: (NSData*)data - (id) initWithRTFD: (NSData*)data
documentAttributes: (NSDictionary**)dict documentAttributes: (NSDictionary**)dict
{ {
NSFileWrapper *wrapper = [[NSFileWrapper alloc] NSAttributedString *new = [converter_class(@"RTFD", NO)
initWithSerializedRepresentation: data]; parseData: data
NSAttributedString *new = [RTFConsumer parseRTFD: wrapper documentAttributes: dict];
documentAttributes: dict];
// We do not return self but the newly created object // We do not return self but the newly created object
RELEASE (self); RELEASE (self);
RELEASE (wrapper);
return RETAIN (new); return RETAIN (new);
} }
- (id) initWithRTF: (NSData *)data - (id) initWithRTF: (NSData *)data
documentAttributes: (NSDictionary **)dict documentAttributes: (NSDictionary **)dict
{ {
NSAttributedString *new = [RTFConsumer parseRTF: data NSAttributedString *new = [converter_class(@"RTF", NO)
documentAttributes: dict]; parseData: data
documentAttributes: dict];
// We do not return self but the newly created object // We do not return self but the newly created object
RELEASE (self); RELEASE (self);
return RETAIN (new); return RETAIN (new);
@ -525,22 +573,25 @@ documentAttributes: (NSDictionary **)dict
- (NSData *) RTFFromRange: (NSRange)range - (NSData *) RTFFromRange: (NSRange)range
documentAttributes: (NSDictionary *)dict documentAttributes: (NSDictionary *)dict
{ {
return [RTFProducer produceRTF: [self attributedSubstringFromRange: range] return [converter_class(@"RTF", YES)
documentAttributes: dict]; produceDataFrom: [self attributedSubstringFromRange: range]
documentAttributes: dict];
} }
- (NSData *) RTFDFromRange: (NSRange)range - (NSData *) RTFDFromRange: (NSRange)range
documentAttributes: (NSDictionary *)dict documentAttributes: (NSDictionary *)dict
{ {
return [[RTFProducer produceRTFD: [self attributedSubstringFromRange: range] return [converter_class(@"RTFD", YES)
documentAttributes: dict] serializedRepresentation]; produceDataFrom: [self attributedSubstringFromRange: range]
documentAttributes: dict];
} }
- (NSFileWrapper *) RTFDFileWrapperFromRange: (NSRange)range - (NSFileWrapper *) RTFDFileWrapperFromRange: (NSRange)range
documentAttributes: (NSDictionary *)dict documentAttributes: (NSDictionary *)dict
{ {
return [RTFProducer produceRTFD: [self attributedSubstringFromRange: range] return [converter_class(@"RTFD", YES)
documentAttributes: dict]; produceFileFrom: [self attributedSubstringFromRange: range]
documentAttributes: dict];
} }
@end @end