Improve compatibility when reading RTF documents created on other

systems and when writing RTF documents.


git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/gui/trunk@29680 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
wlux 2010-02-19 23:53:54 +00:00
parent 11d5564384
commit 007394b21d
4 changed files with 32 additions and 21 deletions

View file

@ -1,3 +1,18 @@
2010-02-20 Wolfgang Lux <wolfgang.lux@gmail.com>
* TextConverters/RTF/RTFConsumer.h: Add encoding attribute to
RTFConsumer.
* TextConverters/RTF/RTFConsumer.m (initStringContext, readString,
-init, -parseRTF:documentAttributes:class:, GSRTFmangleText):
Convert characters to Unicode only after all hex-escapes, which
use the document's code page, have been expanded. Fixes an issue
where some non-ASCII characters in RTF documents created on Mac OS
(X) got mixed up.
* TextConverters/RTF/RTFProducer.m (-_headerString): Fix RTF
header to use code page 1252 instead of 10000. Fixes an issue
where some RTF readers would assume our encoding is Mac OS Roman
and hence mix up some non-ASCII characters in our documents.
2010-02-20 Wolfgang Lux <wolfgang.lux@gmail.com> 2010-02-20 Wolfgang Lux <wolfgang.lux@gmail.com>
* Source/NSTextView.m (-writeSelectionToPasteboard:types:): Add * Source/NSTextView.m (-writeSelectionToPasteboard:types:): Add

View file

@ -36,6 +36,7 @@
@interface RTFConsumer: NSObject <GSTextConsumer> @interface RTFConsumer: NSObject <GSTextConsumer>
{ {
@public @public
NSStringEncoding encoding;
NSMutableDictionary *documentAttributes; NSMutableDictionary *documentAttributes;
NSMutableDictionary *fonts; NSMutableDictionary *fonts;
NSMutableArray *colours; NSMutableArray *colours;

View file

@ -34,24 +34,23 @@
/* we have to satisfy the scanner with a stream reading function */ /* we have to satisfy the scanner with a stream reading function */
typedef struct { typedef struct {
NSString *string; const char *string;
int position; int position;
int length; int length;
} StringContext; } StringContext;
static void static void
initStringContext (StringContext *ctxt, NSString *string) initStringContext (StringContext *ctxt, NSData *data)
{ {
ctxt->string = string; ctxt->string = [data bytes];
ctxt->position = 0; ctxt->position = 0;
ctxt->length = [string length]; ctxt->length = [data length];
} }
static int static int
readNSString (StringContext *ctxt) readString (StringContext *ctxt)
{ {
return (ctxt->position < ctxt->length ) return ctxt->position < ctxt->length ? ctxt->string[ctxt->position++] : EOF;
? [ctxt->string characterAtIndex:ctxt->position++]: EOF;
} }
// Hold the attributs of the current run // Hold the attributs of the current run
@ -367,6 +366,7 @@ static BOOL classInheritsFromNSMutableAttributedString (Class c)
{ {
ignore = 0; ignore = 0;
result = nil; result = nil;
encoding = NSISOLatin1StringEncoding;
documentAttributes = nil; documentAttributes = nil;
fonts = nil; fonts = nil;
attrs = nil; attrs = nil;
@ -546,9 +546,7 @@ static BOOL classInheritsFromNSMutableAttributedString (Class c)
CREATE_AUTORELEASE_POOL(pool); CREATE_AUTORELEASE_POOL(pool);
RTFscannerCtxt scanner; RTFscannerCtxt scanner;
StringContext stringCtxt; StringContext stringCtxt;
NSString *rtfString;
char buffer[5]; char buffer[5];
NSStringEncoding encoding = NSASCIIStringEncoding;
// We read in the first few characters to find out which // We read in the first few characters to find out which
// encoding we have // encoding we have
@ -565,28 +563,25 @@ static BOOL classInheritsFromNSMutableAttributedString (Class c)
else if (strncmp(buffer, "pc", 2) == 0) else if (strncmp(buffer, "pc", 2) == 0)
{ {
// FIXME: Code page 437 kCFStringEncodingDOSLatinUS // FIXME: Code page 437 kCFStringEncodingDOSLatinUS
encoding = NSASCIIStringEncoding; encoding = NSISOLatin1StringEncoding;
} }
else if (strncmp(buffer, "pca", 3) == 0) else if (strncmp(buffer, "pca", 3) == 0)
{ {
// FIXME: Code page 850 kCFStringEncodingDOSLatin1 // FIXME: Code page 850 kCFStringEncodingDOSLatin1
encoding = NSASCIIStringEncoding; encoding = NSISOLatin1StringEncoding;
} }
else if (strncmp(buffer, "ansi", 2) == 0) else
{ {
encoding = NSASCIIStringEncoding; encoding = NSISOLatin1StringEncoding;
} }
rtfString = [[NSString alloc]
initWithData: rtfData
encoding: encoding];
// Reset this RFTConsumer, as it might already have been used! // Reset this RFTConsumer, as it might already have been used!
_class = class; _class = class;
[self reset]; [self reset];
initStringContext(&stringCtxt, rtfString); initStringContext(&stringCtxt, rtfData);
lexInitContext(&scanner, &stringCtxt, (int (*)(void*))readNSString); lexInitContext(&scanner, &stringCtxt, (int (*)(void*))readString);
[result beginEditing]; [result beginEditing];
NS_DURING NS_DURING
GSRTFparse((void *)self, &scanner); GSRTFparse((void *)self, &scanner);
@ -597,7 +592,6 @@ static BOOL classInheritsFromNSMutableAttributedString (Class c)
NS_ENDHANDLER NS_ENDHANDLER
[result endEditing]; [result endEditing];
RELEASE(rtfString);
RELEASE(pool); RELEASE(pool);
// document attributes // document attributes
if (dict) if (dict)
@ -677,6 +671,7 @@ static BOOL classInheritsFromNSMutableAttributedString (Class c)
#define IGNORE ((RTFConsumer *)ctxt)->ignore #define IGNORE ((RTFConsumer *)ctxt)->ignore
#define TEXTPOSITION [RESULT length] #define TEXTPOSITION [RESULT length]
#define DOCUMENTATTRIBUTES ((RTFConsumer*)ctxt)->documentAttributes #define DOCUMENTATTRIBUTES ((RTFConsumer*)ctxt)->documentAttributes
#define ENCODING ((RTFConsumer *)ctxt)->encoding
#define FILES ((RTFDConsumer*)ctxt)->files #define FILES ((RTFDConsumer*)ctxt)->files
@ -760,7 +755,7 @@ void GSRTFmangleText (void *ctxt, const char *text)
NSData *data = [[NSData alloc] initWithBytes: (void*)text NSData *data = [[NSData alloc] initWithBytes: (void*)text
length: strlen(text)]; length: strlen(text)];
NSString *str = [[NSString alloc] initWithData: data NSString *str = [[NSString alloc] initWithData: data
encoding: NSISOLatin1StringEncoding]; encoding: ENCODING];
[(RTFConsumer *)ctxt appendString: str]; [(RTFConsumer *)ctxt appendString: str];
DESTROY(str); DESTROY(str);

View file

@ -403,7 +403,7 @@
// grok paragraph spacing \saN. Should be no problem with other RTF parsers // grok paragraph spacing \saN. Should be no problem with other RTF parsers
// as this command will be ignored. So this is for compatibility with OS X. // as this command will be ignored. So this is for compatibility with OS X.
result = (NSMutableString *)[NSMutableString stringWithString: result = (NSMutableString *)[NSMutableString stringWithString:
@"{\\rtf1\\ansi\\ansicpg10000\\cocoartf102"]; @"{\\rtf1\\ansi\\ansicpg1252\\cocoartf102"];
[result appendString: [self fontTable]]; [result appendString: [self fontTable]];
[result appendString: [self colorTable]]; [result appendString: [self colorTable]];