Added RTFD read support

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/gui/trunk@22905 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
Nicolas Roard 2006-05-11 21:03:21 +00:00
parent 407e0c79b4
commit 2775136fd9
8 changed files with 679 additions and 537 deletions

View file

@ -1,3 +1,19 @@
2006-05-11 Nicolas Roard <nicolas@roard.com>
* TextConverters/RTF: added support for reading RTFD documents
* TextConverters/RTF/rtfScanner.c:
* TextConverters/RTF/rtfGrammer.y:
* TextConverters/RTF/rtfGrammer.tab.h:
* TextConverters/RTF/rtfGrammer.tab.c: modified the parser and
the grammar to parse \NeXTGraphic and the associated \width and \height
although \width and \height are not used to set the image size.
* TextConverters/RTF/RTFConsumerFunctions.h:
* TextConverters/RTF/RTFConsumer.h:
* TextConverters/RTF/RTFConsumer.m: modified the RTFDConsumer class so
that it holds a list of the files in the rtfd, added an appendImage:
method which wrap the files and set an attachment cell with the image.
Also added the RTFNeXTGraphic() function called by the parser.
2006-05-11 Richard Frith-Macdonald <rfm@gnu.org>
* Source/NSBundleAdditions.m: ([loadNibNamed:owner:]) add comments

View file

@ -47,6 +47,10 @@
@end
@interface RTFDConsumer: RTFConsumer
{
NSDictionary* files;
}
- (void) setFiles: (NSDictionary*) theFiles;
@end
#endif

View file

@ -305,31 +305,32 @@ static BOOL classInheritsFromNSMutableAttributedString (Class c)
documentAttributes: (NSDictionary **)dict
class: (Class)class
{
RTFConsumer *consumer = [RTFConsumer new];
NSAttributedString *text = nil;
if ([wrapper isRegularFile])
{
RTFConsumer *consumer = [RTFConsumer new];
text = [consumer parseRTF: [wrapper regularFileContents]
documentAttributes: dict
class: class];
RELEASE(consumer);
}
else if ([wrapper isDirectory])
{
NSDictionary *files = [wrapper fileWrappers];
NSFileWrapper *contents;
RTFDConsumer* consumer = [RTFDConsumer new];
//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)
{
[consumer setFiles: files];
text = [consumer parseRTF: [contents regularFileContents]
documentAttributes: dict
class: class];
}
RELEASE(consumer);
}
RELEASE(consumer);
return text;
}
@ -376,6 +377,26 @@ static BOOL classInheritsFromNSMutableAttributedString (Class c)
@implementation RTFDConsumer
- (id) init
{
self = [super init];
files = nil;
return self;
}
- (void) dealloc
{
RELEASE(files);
[super dealloc];
}
- (void) setFiles: (NSDictionary*) theFiles
{
ASSIGN (files, theFiles);
}
+ (NSAttributedString*) parseData: (NSData *)rtfData
documentAttributes: (NSDictionary **)dict
class: (Class)class
@ -390,6 +411,44 @@ static BOOL classInheritsFromNSMutableAttributedString (Class c)
return str;
}
- (void) appendImage: (NSString*)string
{
int oldPosition = [result length];
NSRange insertionRange = NSMakeRange(oldPosition,0);
if (!ignore)
{
NSString* fileName = [string stringByTrimmingCharactersInSet:
[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSFileWrapper* wrapper = [files objectForKey: fileName];
if (wrapper != nil)
{
NSImage* image = [[NSImage alloc] initWithData: [wrapper regularFileContents]];
NSTextAttachmentCell* attachedCell = [[NSTextAttachmentCell alloc] initImageCell: image];
NSTextAttachment* attachment = [[NSTextAttachment alloc] initWithFileWrapper: wrapper];
[attachment setAttachmentCell: attachedCell];
RTFAttribute* attr = [self attr];
NSMutableDictionary* attributes = [[NSMutableDictionary alloc]
initWithObjectsAndKeys:
[attr currentFont], NSFontAttributeName,
attr->paragraph, NSParagraphStyleAttributeName,
nil];
NSMutableAttributedString* str = [NSMutableAttributedString attributedStringWithAttachment: attachment];
[str addAttributes: attributes range: NSMakeRange (0, [str length])];
[result replaceCharactersInRange: insertionRange withAttributedString: str];
RELEASE(attributes);
RELEASE(attachment);
RELEASE(attachedCell);
RELEASE(image);
}
}
}
@end
@implementation RTFConsumer (Private)
@ -552,6 +611,8 @@ static BOOL classInheritsFromNSMutableAttributedString (Class c)
#define TEXTPOSITION [RESULT length]
#define DOCUMENTATTRIBUTES ((RTFConsumer*)ctxt)->documentAttributes
#define FILES ((RTFDConsumer*)ctxt)->files
#define CTXT [((RTFConsumer *)ctxt) attr]
#define CHANGED CTXT->changed
#define PARAGRAPH CTXT->paragraph
@ -1029,3 +1090,11 @@ void GSRTFparagraph (void *ctxt)
GSRTFmangleText(ctxt, "\n");
CTXT->tabChanged = NO;
}
void GSRTFNeXTGraphic (void *ctxt, const char *fileName, int width, int height)
{
NSLog (@"RTFNextGraphic called");
NSLog (@"we want to read the file: <%s> (%dx%d)", fileName, width, height);
[(RTFDConsumer *)ctxt appendImage: [NSString stringWithCString: fileName]];
}

View file

@ -134,6 +134,8 @@ void GSRTFitalic(void *ctxt, BOOL on);
void GSRTFunderline(void *ctxt, BOOL on);
/* new paragraph */
void GSRTFparagraph(void *ctxt);
/* NeXTGraphic */
void GSRTFNeXTGraphic(void *ctxt, const char *fileName, int width, int height);
#endif

File diff suppressed because it is too large Load diff

View file

@ -15,8 +15,8 @@
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA. */
Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA. */
/* As a special exception, when this file is copied by Bison into a
Bison output file, you may use that output file without restriction.
@ -68,42 +68,45 @@
RTFcolortable = 291,
RTFfont = 292,
RTFfontSize = 293,
RTFpaperWidth = 294,
RTFpaperHeight = 295,
RTFmarginLeft = 296,
RTFmarginRight = 297,
RTFmarginTop = 298,
RTFmarginButtom = 299,
RTFfirstLineIndent = 300,
RTFleftIndent = 301,
RTFrightIndent = 302,
RTFalignCenter = 303,
RTFalignJustified = 304,
RTFalignLeft = 305,
RTFalignRight = 306,
RTFlineSpace = 307,
RTFspaceAbove = 308,
RTFstyle = 309,
RTFbold = 310,
RTFitalic = 311,
RTFunderline = 312,
RTFunderlineStop = 313,
RTFunichar = 314,
RTFsubscript = 315,
RTFsuperscript = 316,
RTFtabstop = 317,
RTFfcharset = 318,
RTFfprq = 319,
RTFcpg = 320,
RTFOtherStatement = 321,
RTFfontListStart = 322,
RTFfamilyNil = 323,
RTFfamilyRoman = 324,
RTFfamilySwiss = 325,
RTFfamilyModern = 326,
RTFfamilyScript = 327,
RTFfamilyDecor = 328,
RTFfamilyTech = 329
RTFNeXTGraphic = 294,
RTFNeXTGraphicWidth = 295,
RTFNeXTGraphicHeight = 296,
RTFpaperWidth = 297,
RTFpaperHeight = 298,
RTFmarginLeft = 299,
RTFmarginRight = 300,
RTFmarginTop = 301,
RTFmarginButtom = 302,
RTFfirstLineIndent = 303,
RTFleftIndent = 304,
RTFrightIndent = 305,
RTFalignCenter = 306,
RTFalignJustified = 307,
RTFalignLeft = 308,
RTFalignRight = 309,
RTFlineSpace = 310,
RTFspaceAbove = 311,
RTFstyle = 312,
RTFbold = 313,
RTFitalic = 314,
RTFunderline = 315,
RTFunderlineStop = 316,
RTFunichar = 317,
RTFsubscript = 318,
RTFsuperscript = 319,
RTFtabstop = 320,
RTFfcharset = 321,
RTFfprq = 322,
RTFcpg = 323,
RTFOtherStatement = 324,
RTFfontListStart = 325,
RTFfamilyNil = 326,
RTFfamilyRoman = 327,
RTFfamilySwiss = 328,
RTFfamilyModern = 329,
RTFfamilyScript = 330,
RTFfamilyDecor = 331,
RTFfamilyTech = 332
};
#endif
#define RTFtext 258
@ -142,42 +145,45 @@
#define RTFcolortable 291
#define RTFfont 292
#define RTFfontSize 293
#define RTFpaperWidth 294
#define RTFpaperHeight 295
#define RTFmarginLeft 296
#define RTFmarginRight 297
#define RTFmarginTop 298
#define RTFmarginButtom 299
#define RTFfirstLineIndent 300
#define RTFleftIndent 301
#define RTFrightIndent 302
#define RTFalignCenter 303
#define RTFalignJustified 304
#define RTFalignLeft 305
#define RTFalignRight 306
#define RTFlineSpace 307
#define RTFspaceAbove 308
#define RTFstyle 309
#define RTFbold 310
#define RTFitalic 311
#define RTFunderline 312
#define RTFunderlineStop 313
#define RTFunichar 314
#define RTFsubscript 315
#define RTFsuperscript 316
#define RTFtabstop 317
#define RTFfcharset 318
#define RTFfprq 319
#define RTFcpg 320
#define RTFOtherStatement 321
#define RTFfontListStart 322
#define RTFfamilyNil 323
#define RTFfamilyRoman 324
#define RTFfamilySwiss 325
#define RTFfamilyModern 326
#define RTFfamilyScript 327
#define RTFfamilyDecor 328
#define RTFfamilyTech 329
#define RTFNeXTGraphic 294
#define RTFNeXTGraphicWidth 295
#define RTFNeXTGraphicHeight 296
#define RTFpaperWidth 297
#define RTFpaperHeight 298
#define RTFmarginLeft 299
#define RTFmarginRight 300
#define RTFmarginTop 301
#define RTFmarginButtom 302
#define RTFfirstLineIndent 303
#define RTFleftIndent 304
#define RTFrightIndent 305
#define RTFalignCenter 306
#define RTFalignJustified 307
#define RTFalignLeft 308
#define RTFalignRight 309
#define RTFlineSpace 310
#define RTFspaceAbove 311
#define RTFstyle 312
#define RTFbold 313
#define RTFitalic 314
#define RTFunderline 315
#define RTFunderlineStop 316
#define RTFunichar 317
#define RTFsubscript 318
#define RTFsuperscript 319
#define RTFtabstop 320
#define RTFfcharset 321
#define RTFfprq 322
#define RTFcpg 323
#define RTFOtherStatement 324
#define RTFfontListStart 325
#define RTFfamilyNil 326
#define RTFfamilyRoman 327
#define RTFfamilySwiss 328
#define RTFfamilyModern 329
#define RTFfamilyScript 330
#define RTFfamilyDecor 331
#define RTFfamilyTech 332
@ -189,8 +195,8 @@ typedef union {
const char *text;
RTFcmd cmd;
} yystype;
/* Line 1281 of /usr/share/bison/yacc.c. */
#line 194 "rtfGrammer.tab.h"
/* Line 1281 of /usr/local/share/bison/yacc.c. */
#line 200 "rtfGrammer.tab.h"
# define YYSTYPE yystype
#endif

View file

@ -117,6 +117,9 @@ int GSRTFlex(void *lvalp, void *lctxt);
%token <cmd> RTFcolortable
%token <cmd> RTFfont
%token <cmd> RTFfontSize
%token <cmd> RTFNeXTGraphic
%token <cmd> RTFNeXTGraphicWidth
%token <cmd> RTFNeXTGraphicHeight
%token <cmd> RTFpaperWidth
%token <cmd> RTFpaperHeight
%token <cmd> RTFmarginLeft
@ -176,6 +179,7 @@ rtfCharset: RTFansi { $$ = 1; }
;
rtfIngredients: /* empty */
| rtfIngredients rtfNeXTGraphic
| rtfIngredients rtfFontList
| rtfIngredients rtfColorDef
| rtfIngredients rtfStatement
@ -356,6 +360,15 @@ rtfStatement: RTFfont { int font;
free((void*)$1.name); }
;
/*
NeXTGraphic (images)
*/
rtfNeXTGraphic: '{' '{' RTFNeXTGraphic RTFtext RTFNeXTGraphicWidth RTFNeXTGraphicHeight '}' RTFtext '}'
{
GSRTFNeXTGraphic (CTXT, $4, $5.parameter, $6.parameter);
};
/*
Font description
*/

View file

@ -158,6 +158,7 @@ int findStringFromKeywordArray(const char *string, const LexKeyword *array,
// <!> must be sorted
LexKeyword RTFcommands[] =
{
{"NeXTGraphic",token(RTFNeXTGraphic)},
{"ansi", token(RTFansi)},
{"b", token(RTFbold)},
{"blue", token(RTFblue)},
@ -197,6 +198,7 @@ LexKeyword RTFcommands[] =
{"headerf", token(RTFheader)},
{"headerl", token(RTFheader)},
{"headerr", token(RTFheader)},
{"height", token(RTFNeXTGraphicHeight)},
{"i", token(RTFitalic)},
{"info", token(RTFinfo)},
{"ldblquote", token(RTFldblquote)},
@ -238,7 +240,8 @@ LexKeyword RTFcommands[] =
{"uldb", token(RTFunderline)},
{"ulnone", token(RTFunderlineStop)},
{"ulw", token(RTFunderline)},
{"up", token(RTFsuperscript)}
{"up", token(RTFsuperscript)},
{"width", token(RTFNeXTGraphicWidth)}
};
BOOL probeCommand (RTFscannerCtxt *lctxt)