Unify the text converter handling. Plus added a bit of implementation

for the new test block methods.


git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/gui/trunk@28713 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
Fred Kiefer 2009-09-19 14:55:57 +00:00
parent 420648751a
commit 3b51ebf3fe
7 changed files with 306 additions and 170 deletions

View file

@ -1,3 +1,15 @@
2009-09-19 Fred Kiefer <FredKiefer@gmx.de>
* Headers/Additions/GNUstepGUI/GSTextConverter.h,
* TextConverters/RTF/RTFConsumer.m,
* TextConverters/RTF/RTFProducer.h,
* TextConverters/RTF/RTFProducer.m,
* Source/NSAttributedString.m: Unify the text converter handling.
* Headers/AppKit/NSAttributedString.h,
* Source/NSAttributedString.m (-itemNumberInTextList:atIndex:,
-rangeOfTextBlock:atIndex:, -rangeOfTextList:atIndex:): Correct
interface and add implementation for these methods.
2009-09-16 Fred Kiefer <FredKiefer@gmx.de>
* Source/GSToolbarView.m (-drawRect:): Also correct the only use

View file

@ -35,8 +35,7 @@
#include <Foundation/NSAttributedString.h>
#include <Foundation/NSDictionary.h>
#include <Foundation/NSData.h>
#include <AppKit/NSFileWrapper.h>
#include <Foundation/NSError.h>
@protocol GSTextConverter
+ (Class) classForFormat: (NSString*)format
@ -45,9 +44,8 @@
@protocol GSTextProducer
+ (NSData*) produceDataFrom: (NSAttributedString*) aText
documentAttributes: (NSDictionary*)dict;
+ (NSFileWrapper*) produceFileFrom: (NSAttributedString*) aText
documentAttributes: (NSDictionary*)dict;
documentAttributes: (NSDictionary*)dict
error: (NSError **)error;
@end
/*
@ -57,10 +55,9 @@
*/
@protocol GSTextConsumer
+ (NSAttributedString*) parseData: (NSData *)aData
options: (NSDictionary *)options
documentAttributes: (NSDictionary **)dict
class: (Class)class;
+ (NSAttributedString*) parseFile: (NSFileWrapper *)aFile
documentAttributes: (NSDictionary **)dict
error: (NSError **)error
class: (Class)class;
@end

View file

@ -251,14 +251,14 @@ enum
documentAttributes: (NSDictionary **)dict
error: (NSError **)error;
- (NSRange) itemNumberInTextList: (NSTextList *)list
atIndex: (unsigned)location;
- (NSInteger) itemNumberInTextList: (NSTextList *)list
atIndex: (NSUInteger)location;
- (NSRange) rangeOfTextBlock: (NSTextBlock *)block
atIndex: (unsigned)location;
atIndex: (NSUInteger)location;
- (NSRange) rangeOfTextList: (NSTextList *)list
atIndex: (unsigned)location;
atIndex: (NSUInteger)location;
- (NSRange) rangeOfTextTable: (NSTextTable *)table
atIndex: (unsigned)location;
atIndex: (NSUInteger)location;
#endif
@end

View file

@ -247,16 +247,22 @@ create_error(int code, NSString* desc)
+ (NSArray *) textFileTypes
{
// FIXME
// FIXME: Apply service filters
return [self textUnfilteredFileTypes];
}
+ (NSArray *) textPasteboardTypes
{
// FIXME
// FIXME: Apply service filters
return [self textUnfilteredPasteboardTypes];
}
+ (NSArray *) textTypes
{
// FIXME: Apply service filters
return [self textUnfilteredTypes];
}
+ (NSArray *) textUnfilteredFileTypes
{
return [NSArray arrayWithObjects: @"txt", @"rtf", @"rtfd", @"html", nil];
@ -268,12 +274,6 @@ create_error(int code, NSString* desc)
NSRTFDPboardType, NSHTMLPboardType, nil];
}
+ (NSArray *) textTypes
{
// FIXME
return [self textUnfilteredTypes];
}
+ (NSArray *) textUnfilteredTypes
{
return [NSArray arrayWithObjects: @"public.plain-text",
@ -651,61 +651,34 @@ create_error(int code, NSString* desc)
- (id) initWithRTFDFileWrapper: (NSFileWrapper *)wrapper
documentAttributes: (NSDictionary **)dict
{
NSAttributedString *new;
if (wrapper == nil)
{
RELEASE (self);
return nil;
}
new = [converter_class(@"RTFD", NO)
parseFile: wrapper
documentAttributes: dict
class: [self class]];
// We do not return self but the newly created object
RELEASE (self);
return RETAIN (new);
return [self initWithRTFD: [wrapper serializedRepresentation]
documentAttributes: dict];
}
- (id) initWithRTFD: (NSData*)data
documentAttributes: (NSDictionary**)dict
{
NSAttributedString *new;
NSDictionary *options;
if (data == nil)
{
RELEASE (self);
return nil;
}
new = [converter_class(@"RTFD", NO)
parseData: data
documentAttributes: dict
class: [self class]];
// We do not return self but the newly created object
RELEASE (self);
return RETAIN (new);
options = [NSDictionary dictionaryWithObject: NSRTFDTextDocumentType
forKey: NSDocumentTypeDocumentOption];
return [self initWithData: data
options: options
documentAttributes: dict
error: NULL];
}
- (id) initWithRTF: (NSData *)data
documentAttributes: (NSDictionary **)dict
{
NSAttributedString *new;
NSDictionary *options;
if (data == nil)
{
RELEASE (self);
return nil;
}
new = [converter_class(@"RTF", NO)
parseData: data
documentAttributes: dict
class: [self class]];
// We do not return self but the newly created object
RELEASE (self);
return RETAIN (new);
options = [NSDictionary dictionaryWithObject: NSRTFTextDocumentType
forKey: NSDocumentTypeDocumentOption];
return [self initWithData: data
options: options
documentAttributes: dict
error: NULL];
}
- (id) initWithHTML: (NSData *)data
@ -727,7 +700,7 @@ create_error(int code, NSString* desc)
forKey: NSBaseURLDocumentOption];
return [self initWithHTML: data
options: options
options: options
documentAttributes: dict];
}
@ -735,34 +708,41 @@ create_error(int code, NSString* desc)
options: (NSDictionary *)options
documentAttributes: (NSDictionary **)dict
{
if (data == nil)
if (options == nil)
{
RELEASE (self);
return nil;
options = [NSDictionary dictionaryWithObject: NSHTMLTextDocumentType
forKey: NSDocumentTypeDocumentOption];
}
else if ([options objectForKey: NSDocumentTypeDocumentOption] == nil)
{
options = AUTORELEASE([options mutableCopy]);
[(NSMutableDictionary*)options setObject: NSHTMLTextDocumentType
forKey: NSDocumentTypeDocumentOption];
}
// FIXME: Not implemented
return self;
/*
The converter should support:
NSHTMLTextDocumentType
@"public.html"
@"html"
*/
return [self initWithData: data
options: options
documentAttributes: dict
error: NULL];
}
- (id) initWithDocFormat: (NSData *)data
documentAttributes: (NSDictionary **)dict
{
NSAttributedString *new;
NSDictionary *options;
if (data == nil)
{
RELEASE (self);
return nil;
}
new = [converter_class(@"DOC", NO)
parseData: data
documentAttributes: dict
class: [self class]];
// We do not return self but the newly created object
RELEASE (self);
return RETAIN (new);
options = [NSDictionary dictionaryWithObject: NSDocFormatTextDocumentType
forKey: NSDocumentTypeDocumentOption];
return [self initWithData: data
options: options
documentAttributes: dict
error: NULL];
}
- (id) initWithData: (NSData *)data
@ -771,6 +751,7 @@ create_error(int code, NSString* desc)
error: (NSError **)error
{
NSString *type = [options objectForKey: NSDocumentTypeDocumentOption];
Class converter;
if (data == nil)
{
@ -789,43 +770,33 @@ create_error(int code, NSString* desc)
return nil;
}
if ([type isEqualToString: NSDocFormatTextDocumentType])
converter = converter_class(type, NO);
if (converter != Nil)
{
return [self initWithDocFormat: data
documentAttributes: dict];
}
else if ([type isEqualToString: NSHTMLTextDocumentType]
|| [type isEqualToString: @"public.html"]
|| [type isEqualToString: @"html"])
{
return [self initWithHTML: data
options: options
documentAttributes: dict];
}
else if ([type isEqualToString: NSRTFDTextDocumentType]
|| [type isEqualToString: @"com.apple.rtfd"]
|| [type isEqualToString: @"rtfd"])
{
return [self initWithRTFD: data
documentAttributes: dict];
}
else if ([type isEqualToString: NSRTFTextDocumentType]
|| [type isEqualToString: @"public.rtf"]
|| [type isEqualToString: @"rtf"])
{
return [self initWithRTF: data
documentAttributes: dict];
NSAttributedString *new;
new = [converter
parseData: data
options: options
documentAttributes: dict
error: error
class: [self class]];
// We do not return self but the newly created object
RELEASE(self);
return RETAIN(new);
}
else if ([type isEqualToString: NSPlainTextDocumentType]
|| [type isEqualToString: @"public.plain-text"]
|| [type isEqualToString: @"text"])
{
// FIXME: Should we have a proper converter for this type?
NSStringEncoding encoding = [[options objectForKey: @"CharacterEncoding"]
intValue];
NSDictionary *defaultAttrs = [options objectForKey: @"DefaultAttributes"];
NSString *str = [[NSString alloc] initWithData: data
encoding: encoding];
NSString *str;
str = [[NSString alloc] initWithData: data
encoding: encoding];
self = [self initWithString: str
attributes: defaultAttrs];
RELEASE(str);
@ -862,7 +833,7 @@ create_error(int code, NSString* desc)
}
else
{
return [self initWithURL: [NSURL fileURLWithPath: path]
return [self initWithURL: [NSURL fileURLWithPath: path]
documentAttributes: dict];
}
}
@ -915,35 +886,70 @@ documentAttributes: (NSDictionary **)dict
- (NSData *) RTFFromRange: (NSRange)range
documentAttributes: (NSDictionary *)dict
{
return [converter_class(@"RTF", YES)
produceDataFrom:
[self attributedSubstringFromRange: range]
documentAttributes: dict];
if (dict == nil)
{
dict = [NSDictionary dictionaryWithObject: NSRTFTextDocumentType
forKey: NSDocumentTypeDocumentOption];
}
else if ([dict objectForKey: NSDocumentTypeDocumentOption] == nil)
{
dict = AUTORELEASE([dict mutableCopy]);
[(NSMutableDictionary*)dict setObject: NSRTFTextDocumentType
forKey: NSDocumentTypeDocumentOption];
}
return [self dataFromRange: range
documentAttributes: dict
error: NULL];
}
- (NSData *) RTFDFromRange: (NSRange)range
documentAttributes: (NSDictionary *)dict
{
return [converter_class(@"RTFD", YES)
produceDataFrom:
[self attributedSubstringFromRange: range]
documentAttributes: dict];
if (dict == nil)
{
dict = [NSDictionary dictionaryWithObject: NSRTFDTextDocumentType
forKey: NSDocumentTypeDocumentOption];
}
else if ([dict objectForKey: NSDocumentTypeDocumentOption] == nil)
{
dict = AUTORELEASE([dict mutableCopy]);
[(NSMutableDictionary*)dict setObject: NSRTFDTextDocumentType
forKey: NSDocumentTypeDocumentOption];
}
return [self dataFromRange: range
documentAttributes: dict
error: NULL];
}
- (NSFileWrapper *) RTFDFileWrapperFromRange: (NSRange)range
documentAttributes: (NSDictionary *)dict
{
return [converter_class(@"RTFD", YES)
produceFileFrom:
[self attributedSubstringFromRange: range]
documentAttributes: dict];
return AUTORELEASE([[NSFileWrapper alloc]
initWithSerializedRepresentation:
[self RTFDFromRange: range
documentAttributes: dict]]);
}
- (NSData *) docFormatFromRange: (NSRange)range
documentAttributes: (NSDictionary *)dict
{
// FIXME
return nil;
if (dict == nil)
{
dict = [NSDictionary dictionaryWithObject: NSDocFormatTextDocumentType
forKey: NSDocumentTypeDocumentOption];
}
else if ([dict objectForKey: NSDocumentTypeDocumentOption] == nil)
{
dict = AUTORELEASE([dict mutableCopy]);
[(NSMutableDictionary*)dict setObject: NSDocFormatTextDocumentType
forKey: NSDocumentTypeDocumentOption];
}
return [self dataFromRange: range
documentAttributes: dict
error: NULL];
}
- (NSData *) dataFromRange: (NSRange)range
@ -951,6 +957,7 @@ documentAttributes: (NSDictionary **)dict
error: (NSError **)error
{
NSString *type = [dict objectForKey: NSDocumentTypeDocumentOption];
Class converter;
if (type == nil)
{
@ -959,26 +966,18 @@ documentAttributes: (NSDictionary **)dict
return nil;
}
if ([type isEqualToString: NSDocFormatTextDocumentType])
converter = converter_class(type, YES);
if (converter != Nil)
{
return [self docFormatFromRange: range
documentAttributes: dict];
return [converter
produceDataFrom:
[self attributedSubstringFromRange: range]
documentAttributes: dict
error: error];
}
else if ([type isEqualToString: NSHTMLTextDocumentType])
{
// FIXME
}
else if ([type isEqualToString: NSRTFDTextDocumentType])
{
return [self RTFDFromRange: range
documentAttributes: dict];
}
else if ([type isEqualToString: NSRTFTextDocumentType])
{
return [self RTFFromRange: range
documentAttributes: dict];
}
else if ([type isEqualToString: NSPlainTextDocumentType])
else if ([type isEqualToString: NSPlainTextDocumentType]
|| [type isEqualToString: @"public.plain-text"]
|| [type isEqualToString: @"text"])
{
NSStringEncoding encoding = [[dict objectForKey: @"CharacterEncoding"]
intValue];
@ -1000,12 +999,12 @@ documentAttributes: (NSDictionary **)dict
NSFileWrapper *wrapper;
NSData *data;
// FIXME: This wont work for directory bundles.
data = [self dataFromRange: range
documentAttributes: dict
error: error];
if (data != nil)
{
// FIXME: This wont work for directory bundles.
wrapper = [[NSFileWrapper alloc] initRegularFileWithContents: data];
return AUTORELEASE(wrapper);
}
@ -1017,29 +1016,139 @@ documentAttributes: (NSDictionary **)dict
return nil;
}
- (NSRange) itemNumberInTextList: (NSTextList *)list
atIndex: (unsigned)location
- (NSInteger) itemNumberInTextList: (NSTextList *)list
atIndex: (NSUInteger)location
{
// FIXME
return NSMakeRange(NSNotFound, 0);
NSParagraphStyle *style = [self attribute: NSParagraphStyleAttributeName
atIndex: location
effectiveRange: NULL];
if (style != nil)
{
NSArray *textLists = [style textLists];
if (textLists != nil)
{
return [textLists indexOfObject: list];
}
}
return NSNotFound;
}
- (NSRange) rangeOfTextBlock: (NSTextBlock *)block
atIndex: (unsigned)location
atIndex: (NSUInteger)location
{
// FIXME
NSRange effRange;
NSParagraphStyle *style = [self attribute: NSParagraphStyleAttributeName
atIndex: location
effectiveRange: &effRange];
if (style != nil)
{
NSArray *textBlocks = [style textBlocks];
if ((textBlocks != nil) && [textBlocks containsObject: block])
{
NSRange newEffRange;
unsigned len = [self length];
while ((effRange.location > 0) && style && textBlocks)
{
style = [self attribute: NSParagraphStyleAttributeName
atIndex: effRange.location - 1
effectiveRange: &newEffRange];
if (style != nil)
{
textBlocks = [style textBlocks];
if ((textBlocks != nil) && [textBlocks containsObject: block])
{
effRange.location = newEffRange.location;
effRange.length += newEffRange.length;
}
}
}
while (NSMaxRange(effRange) < len && style && textBlocks)
{
style = [self attribute: NSParagraphStyleAttributeName
atIndex: NSMaxRange(effRange)
effectiveRange: &newEffRange];
if (style != nil)
{
textBlocks = [style textBlocks];
if ((textBlocks != nil) && [textBlocks containsObject: block])
{
effRange.length += newEffRange.length;
}
}
}
return effRange;
}
}
return NSMakeRange(NSNotFound, 0);
}
- (NSRange) rangeOfTextList: (NSTextList *)list
atIndex: (unsigned)location
atIndex: (NSUInteger)location
{
// FIXME
NSRange effRange;
NSParagraphStyle *style = [self attribute: NSParagraphStyleAttributeName
atIndex: location
effectiveRange: &effRange];
if (style != nil)
{
NSArray *textLists = [style textLists];
if ((textLists != nil) && [textLists containsObject: list])
{
NSRange newEffRange;
unsigned len = [self length];
while ((effRange.location > 0) && style && textLists)
{
style = [self attribute: NSParagraphStyleAttributeName
atIndex: effRange.location - 1
effectiveRange: &newEffRange];
if (style != nil)
{
textLists = [style textLists];
if ((textLists != nil) && [textLists containsObject: list])
{
effRange.location = newEffRange.location;
effRange.length += newEffRange.length;
}
}
}
while (NSMaxRange(effRange) < len && style && textLists)
{
style = [self attribute: NSParagraphStyleAttributeName
atIndex: NSMaxRange(effRange)
effectiveRange: &newEffRange];
if (style != nil)
{
textLists = [style textLists];
if ((textLists != nil) && [textLists containsObject: list])
{
effRange.length += newEffRange.length;
}
}
}
return effRange;
}
}
return NSMakeRange(NSNotFound, 0);
}
- (NSRange) rangeOfTextTable: (NSTextTable *)table
atIndex: (unsigned)location
atIndex: (NSUInteger)location
{
// FIXME
return NSMakeRange(NSNotFound, 0);
@ -1619,9 +1728,9 @@ static NSMutableDictionary *cachedCSets = nil;
range: (NSRange)range
{
[self setAttributes: [NSDictionary dictionaryWithObject:
[NSNumber numberWithInt: writingDirection]
forKey: @"WritingDirection"]
range: range];
[NSNumber numberWithInt: writingDirection]
forKey: @"WritingDirection"]
range: range];
}
@end

View file

@ -279,22 +279,30 @@ static BOOL classInheritsFromNSMutableAttributedString (Class c)
if (flag)
{
if ([format isEqual: @"RTFD"])
if (([format isEqual: NSRTFDTextDocumentType]) ||
([format isEqual: @"com.apple.rtfd"]) ||
([format isEqual: @"rtfd"]))
{
cClass = [RTFDProducer class];
}
else if ([format isEqual: @"RTF"])
else if (([format isEqual: NSRTFTextDocumentType]) ||
([format isEqual: @"public.rtf"]) ||
([format isEqual: @"rtf"]))
{
cClass = [RTFProducer class];
}
}
else
{
if ([format isEqual: @"RTFD"])
if (([format isEqual: NSRTFDTextDocumentType]) ||
([format isEqual: @"com.apple.rtfd"]) ||
([format isEqual: @"rtfd"]))
{
cClass = [RTFDConsumer class];
}
else if ([format isEqual: @"RTF"])
else if (([format isEqual: NSRTFTextDocumentType]) ||
([format isEqual: @"public.rtf"]) ||
([format isEqual: @"rtf"]))
{
cClass = [RTFConsumer class];
}
@ -303,7 +311,9 @@ static BOOL classInheritsFromNSMutableAttributedString (Class c)
}
+ (NSAttributedString*) parseFile: (NSFileWrapper *)wrapper
options: (NSDictionary *)options
documentAttributes: (NSDictionary **)dict
error: (NSError **)error
class: (Class)class
{
NSAttributedString *text = nil;
@ -337,7 +347,9 @@ static BOOL classInheritsFromNSMutableAttributedString (Class c)
}
+ (NSAttributedString*) parseData: (NSData *)rtfData
options: (NSDictionary *)options
documentAttributes: (NSDictionary **)dict
error: (NSError **)error
class: (Class)class
{
RTFConsumer *consumer = [RTFConsumer new];
@ -406,14 +418,20 @@ static BOOL classInheritsFromNSMutableAttributedString (Class c)
}
+ (NSAttributedString*) parseData: (NSData *)rtfData
options: (NSDictionary *)options
documentAttributes: (NSDictionary **)dict
error: (NSError **)error
class: (Class)class
{
NSAttributedString *str;
NSFileWrapper *wrapper = [[NSFileWrapper alloc]
initWithSerializedRepresentation: rtfData];
str = [self parseFile: wrapper documentAttributes: dict class: class];
str = [self parseFile: wrapper
options: options
documentAttributes: dict
error: error
class: class];
RELEASE (wrapper);
return str;

View file

@ -60,12 +60,6 @@
int unnamedAttachmentCounter; /*" Count the number of unnamed attachments so we can name them uniquely "*/
}
+ (NSData *)produceDataFrom: (NSAttributedString *)aText
documentAttributes: (NSDictionary *)dict;
+ (NSFileWrapper *)produceFileFrom: (NSAttributedString *)aText
documentAttributes: (NSDictionary *)dict;
@end
@interface RTFProducer: RTFDProducer

View file

@ -69,6 +69,7 @@
+ (NSFileWrapper *)produceFileFrom: (NSAttributedString *)aText
documentAttributes: (NSDictionary *)dict
error: (NSError **)error
{
RTFDProducer *producer;
NSData *encodedText;
@ -122,9 +123,11 @@
+ (NSData *)produceDataFrom: (NSAttributedString *)aText
documentAttributes: (NSDictionary *)dict
error: (NSError **)error
{
return [[self produceFileFrom: aText
documentAttributes: dict] serializedRepresentation];
documentAttributes: dict
error: error] serializedRepresentation];
}
- (id)init
@ -170,6 +173,7 @@
+ (NSData *)produceDataFrom: (NSAttributedString *)aText
documentAttributes: (NSDictionary *)dict
error: (NSError **)error
{
RTFProducer *producer;
NSData *data;
@ -187,10 +191,12 @@
+ (NSFileWrapper *)produceFileFrom: (NSAttributedString *)aText
documentAttributes: (NSDictionary *)dict
error: (NSError **)error
{
return AUTORELEASE([[NSFileWrapper alloc]
initRegularFileWithContents: [self produceDataFrom: aText
documentAttributes: dict]]);
documentAttributes: dict
error: error]]);
}
@end