RTFHandler: support charsets in plain text

Allow to switch charset in RTF plain text parts.
This commit is contained in:
Javier Amor García
2016-02-01 11:48:15 +01:00
parent 3d9466df45
commit 8e80b57d31
3 changed files with 26 additions and 29 deletions
+12 -17
View File
@@ -32,7 +32,7 @@
#define DEFAULT_CHARSET 1
#define FONTNAME_LEN_MAX 100
#define UTF8_FIRST_BYTE_LAST_CODEPOINT 0x7F
//
// Charset definitions. See http://msdn.microsoft.com/en-us/goglobal/bb964654 for all details.
//
@@ -1215,7 +1215,6 @@ inline static void parseUl(RTFHandler *self, BOOL hasArg, int arg, RTFFormatting
unsigned char c;
NSData *d;
NSString *s;
unichar uch;
stack = [[RTFStack alloc] init];
fontTable = nil;
@@ -1223,7 +1222,7 @@ inline static void parseUl(RTFHandler *self, BOOL hasArg, int arg, RTFFormatting
defaultCharset = ansicpg1252;
formattingOptions = nil;
_html = [[NSMutableData alloc] init];
_html = [[[NSMutableData alloc] init] autorelease];
[_html appendBytes: "<html><meta charset='utf-8'><body>" length: 34];
@@ -1254,7 +1253,6 @@ inline static void parseUl(RTFHandler *self, BOOL hasArg, int arg, RTFFormatting
unsigned short index;
const unsigned short * active_charset;
if (formattingOptions && formattingOptions->charset)
active_charset = formattingOptions->charset;
else
@@ -1439,18 +1437,15 @@ inline static void parseUl(RTFHandler *self, BOOL hasArg, int arg, RTFFormatting
// We avoid appending NULL bytes or endlines
if (c && (c != '\n'))
{
if (c <= UTF8_FIRST_BYTE_LAST_CODEPOINT)
{
// in this case utf8 and ascii encoding are the same
[_html appendBytes: &c length: 1];
}
else
{
uch = c;
s = [NSString stringWithCharacters: &uch length: 1];
d = [s dataUsingEncoding: NSUTF8StringEncoding];
[_html appendData: d];
}
const unsigned short * active_charset;
if (formattingOptions && formattingOptions->charset)
active_charset = formattingOptions->charset;
else
active_charset = defaultCharset;
s = [NSString stringWithCharacters: &(active_charset[c]) length: 1];
d = [s dataUsingEncoding: NSUTF8StringEncoding];
[_html appendData: d];
}
ADVANCE;
}
@@ -1459,7 +1454,7 @@ inline static void parseUl(RTFHandler *self, BOOL hasArg, int arg, RTFFormatting
[_html appendBytes: "</body></html>" length: 14];
[stack release];
return [_html autorelease];
return _html;
}
/* This method is for ease of testing and should not be used in normal operations */
File diff suppressed because one or more lines are too long
+13 -12
View File
@@ -44,26 +44,18 @@
return nil;
RTFHandler *handler = [[RTFHandler alloc] initWithData: rtf];
NSMutableData *data2 = [handler parse];
if (data2 == nil)
NSMutableData *data = [handler parse];
if (data == nil)
{
NSString *error = [NSString stringWithFormat: @"Couldn't parse RTF data:\n %s",
(char *)[rtf bytes]];
testWithMessage(NO, error);
}
html = [[NSString alloc] initWithData: data2 encoding: NSUTF8StringEncoding];
html = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
if (html == nil)
{
html = [[NSString alloc] initWithData: data2 encoding: NSASCIIStringEncoding];
}
if (html == nil)
{
html = [[NSString alloc] initWithData: data2 encoding: NSISOLatin1StringEncoding];
}
if (html == nil)
{
NSString *error = [NSString stringWithFormat: @"Couldn't convert parsed data"];
NSString *error = [NSString stringWithFormat: @"Couldn't convert parsed data to UTF8 string"];
testWithMessage(NO, error);
}
return html;
@@ -305,4 +297,13 @@
againstExpectedHTML: expected];
}
- (void) test_cyr_event_ru_editor
{
NSString *file =@"cyr_event_ru_editor.rtf";
NSString *expected=@"<html><meta charset='utf-8'><body><font face=\"Calibri\"><font face=\"Calibri Cyr\"><font color=\"#000000\">йчсмй</font></font><font color=\"#000000\"><br></font></font></body></html>";
[self checkHTMLConversionOfRTFFile: file
againstExpectedHTML: expected];
}
@end