Added support for RTF special characters and extra paragraph attributes

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/gui/trunk@6786 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
Fred Kiefer 2000-06-24 22:10:09 +00:00
parent 856468e13b
commit 21e0c1e7b2
6 changed files with 620 additions and 406 deletions

View file

@ -57,7 +57,8 @@ readNSString(StringContext *ctxt)
{
@public
BOOL changed;
NSParagraphStyle *paragraph;
BOOL tabChanged;
NSMutableParagraphStyle *paragraph;
NSColor *fgColour;
NSColor *bgColour;
NSString *fontName;
@ -73,6 +74,7 @@ readNSString(StringContext *ctxt)
- (NSNumber*) underline;
- (void) resetParagraphStyle;
- (void) resetFont;
- (void) addTab: (float) location type: (NSTextTabType) type;
@end
@ -97,16 +99,10 @@ readNSString(StringContext *ctxt)
- (id) copyWithZone: (NSZone*)zone
{
RTFAttribute *new = [isa allocWithZone: zone];
RTFAttribute *new = (RTFAttribute *)NSCopyObject (self, 0, zone);
new->paragraph = [paragraph copyWithZone: zone];
new->fontName = [fontName copyWithZone: zone];
new->fontSize = fontSize;
new->italic = italic;
new->bold = bold;
new->underline = underline;
new->script = script;
new->changed = NO;
return new;
}
@ -124,7 +120,7 @@ readNSString(StringContext *ctxt)
}
else
{
weight = 6;
weight = 5;
traits |= NSUnboldFontMask;
}
@ -169,6 +165,7 @@ readNSString(StringContext *ctxt)
{
ASSIGN(paragraph, [NSMutableParagraphStyle defaultParagraphStyle]);
tabChanged = NO;
changed = YES;
}
@ -188,6 +185,24 @@ readNSString(StringContext *ctxt)
changed = YES;
}
- (void) addTab: (float) location type: (NSTextTabType) type
{
NSTextTab *tab = [[NSTextTab alloc] initWithType: NSLeftTabStopType
location: location];
if (!tabChanged)
{
// remove all tab stops
[paragraph setTabStops: [NSArray arrayWithObject: tab]];
tabChanged = YES;
}
else
[paragraph addTabStop: tab];
changed = YES;
RELEASE(tab);
}
@end
@ -268,6 +283,7 @@ readNSString(StringContext *ctxt)
- (void) pop
{
[attrs removeLastObject];
((RTFAttribute*)[attrs lastObject])->changed = YES;
}
- (NSAttributedString*) result
@ -284,7 +300,7 @@ readNSString(StringContext *ctxt)
StringContext stringCtxt;
NSString *rtfString = [[NSString alloc]
initWithData: rtfData
encoding: NSASCIIStringEncoding];
encoding: NSISOLatin1StringEncoding];
// Reset this RFTConsumer, as it might already have been used!
[self reset];
@ -355,6 +371,7 @@ void GSRTFgenericRTFcommand(void *ctxt, RTFcmd cmd)
//Start: we're doing some initialization
void GSRTFstart(void *ctxt)
{
NSDebugLLog(@"RTFParser", @"Start RTF parsing");
[RESULT beginEditing];
}
@ -363,6 +380,7 @@ void GSRTFstop(void *ctxt)
{
//<!> close all open bolds et al.
[RESULT endEditing];
NSDebugLLog(@"RTFParser", @"End RTF parsing");
}
void GSRTFopenBlock(void *ctxt, BOOL ignore)
@ -379,7 +397,9 @@ void GSRTFcloseBlock(void *ctxt, BOOL ignore)
if (ignore)
IGNORE--;
if (!IGNORE)
[(RTFConsumer *)ctxt pop];
{
[(RTFConsumer *)ctxt pop];
}
}
void GSRTFmangleText(void *ctxt, const char *text)
@ -544,6 +564,7 @@ void GSRTFfirstLineIndent(void *ctxt, int indent)
NSMutableParagraphStyle *para = PARAGRAPH;
float findent = twips2points(indent);
// FIXME: This should changed the left indent of the paragraph, if < 0
// for attributed strings only positiv indent is allowed
if ((findent >= 0.0) && ([para firstLineHeadIndent] != findent))
{
@ -580,16 +601,12 @@ void GSRTFrightIndent(void *ctxt, int indent)
void GSRTFtabstop(void *ctxt, int location)
{
/*
NSMutableParagraphStyle *para = PARAGRAPH;
float flocation = twips2points(location);
if (flocation >= 0.0))
if (flocation >= 0.0)
{
//[para addTab: flocation];
CHANGED = YES;
[CTXT addTab: flocation type: NSLeftTabStopType];
}
*/
}
void GSRTFalignCenter(void *ctxt)
@ -603,6 +620,17 @@ void GSRTFalignCenter(void *ctxt)
}
}
void GSRTFalignJustified(void *ctxt)
{
NSMutableParagraphStyle *para = PARAGRAPH;
if ([para alignment] != NSJustifiedTextAlignment)
{
[para setAlignment: NSJustifiedTextAlignment];
CHANGED = YES;
}
}
void GSRTFalignLeft(void *ctxt)
{
NSMutableParagraphStyle *para = PARAGRAPH;
@ -625,9 +653,39 @@ void GSRTFalignRight(void *ctxt)
}
}
void GSRTFspaceAbove(void *ctxt, int space)
{
NSMutableParagraphStyle *para = PARAGRAPH;
float fspace = twips2points(space);
if (fspace >= 0.0)
{
[para setParagraphSpacing: fspace];
}
}
void GSRTFlineSpace(void *ctxt, int space)
{
NSMutableParagraphStyle *para = PARAGRAPH;
float fspace = twips2points(space);
if (space == 1000)
{
[para setMinimumLineHeight: 0.0];
[para setMaximumLineHeight: 0.0];
}
else if (fspace < 0.0)
{
[para setMaximumLineHeight: -fspace];
}
else
{
[para setMinimumLineHeight: fspace];
}
}
void GSRTFdefaultParagraph(void *ctxt)
{
GSRTFmangleText(ctxt, "\n");
[CTXT resetParagraphStyle];
}
@ -635,6 +693,11 @@ void GSRTFstyle(void *ctxt, int style)
{
}
void GSRTFdefaultCharacterStyle(void *ctxt)
{
[CTXT resetFont];
}
void GSRTFaddColor(void *ctxt, int red, int green, int blue)
{
NSColor *colour = [NSColor colorWithCalibratedRed: red/255.0
@ -709,7 +772,11 @@ void GSRTFunderline(void *ctxt, BOOL state)
}
}
void GSRTFparagraph(void *ctxt)
{
GSRTFmangleText(ctxt, "\n");
CTXT->tabChanged = NO;
}
NSAttributedString *parseRTFintoAttributedString(NSData *rtfData,
NSDictionary **dict)

View file

@ -94,10 +94,16 @@ void GSRTFrightIndent(void *ctxt, int indent);
void GSRTFtabstop(void *ctxt, int location);
/* set center alignment */
void GSRTFalignCenter(void *ctxt);
/* set justified alignment */
void GSRTFalignJustified(void *ctxt);
/* set left alignment */
void GSRTFalignLeft(void *ctxt);
/* set right alignment */
void GSRTFalignRight(void *ctxt);
/* set space above */
void GSRTFspaceAbove(void *ctxt, int location);
/* set line space */
void GSRTFlineSpace(void *ctxt, int location);
/* set default paragraph style */
void GSRTFdefaultParagraph(void *ctxt);
/* set paragraph style */
@ -110,6 +116,8 @@ void GSRTFaddDefaultColor(void *ctxt);
void GSRTFcolorbg(void *ctxt, int color);
/* set foreground colour */
void GSRTFcolorfg(void *ctxt, int color);
/* set default character style */
void GSRTFdefaultCharacterStyle(void *ctxt);
/* set subscript in half points */
void GSRTFsubscript(void *ctxt, int script);
/* set superscript in half points */
@ -120,6 +128,8 @@ void GSRTFbold(void *ctxt, BOOL on);
void GSRTFitalic(void *ctxt, BOOL on);
/* Switch underline mode on or off */
void GSRTFunderline(void *ctxt, BOOL on);
/* new paragraph */
void GSRTFparagraph(void *ctxt);
#endif

File diff suppressed because it is too large Load diff

View file

@ -1,5 +1,5 @@
typedef union {
int number;
int number;
const char *text;
RTFcmd cmd;
} YYSTYPE;
@ -33,47 +33,62 @@ typedef
#define RTFheader 268
#define RTFfooter 269
#define RTFpict 270
#define RTFred 271
#define RTFgreen 272
#define RTFblue 273
#define RTFcolorbg 274
#define RTFcolorfg 275
#define RTFcolortable 276
#define RTFfont 277
#define RTFfontSize 278
#define RTFpaperWidth 279
#define RTFpaperHeight 280
#define RTFmarginLeft 281
#define RTFmarginRight 282
#define RTFmarginTop 283
#define RTFmarginButtom 284
#define RTFfirstLineIndent 285
#define RTFleftIndent 286
#define RTFrightIndent 287
#define RTFalignCenter 288
#define RTFalignLeft 289
#define RTFalignRight 290
#define RTFstyle 291
#define RTFbold 292
#define RTFitalic 293
#define RTFunderline 294
#define RTFunderlineStop 295
#define RTFsubscript 296
#define RTFsuperscript 297
#define RTFtabulator 298
#define RTFtabstop 299
#define RTFparagraph 300
#define RTFdefaultParagraph 301
#define RTFfcharset 302
#define RTFfprq 303
#define RTFcpg 304
#define RTFOtherStatement 305
#define RTFfontListStart 306
#define RTFfamilyNil 307
#define RTFfamilyRoman 308
#define RTFfamilySwiss 309
#define RTFfamilyModern 310
#define RTFfamilyScript 311
#define RTFfamilyDecor 312
#define RTFfamilyTech 313
#define RTFplain 271
#define RTFparagraph 272
#define RTFdefaultParagraph 273
#define RTFrow 274
#define RTFcell 275
#define RTFtabulator 276
#define RTFemdash 277
#define RTFendash 278
#define RTFemspace 279
#define RTFenspace 280
#define RTFbullet 281
#define RTFlquote 282
#define RTFrquote 283
#define RTFldblquote 284
#define RTFrdblquote 285
#define RTFred 286
#define RTFgreen 287
#define RTFblue 288
#define RTFcolorbg 289
#define RTFcolorfg 290
#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 RTFsubscript 314
#define RTFsuperscript 315
#define RTFtabstop 316
#define RTFfcharset 317
#define RTFfprq 318
#define RTFcpg 319
#define RTFOtherStatement 320
#define RTFfontListStart 321
#define RTFfamilyNil 322
#define RTFfamilyRoman 323
#define RTFfamilySwiss 324
#define RTFfamilyModern 325
#define RTFfamilyScript 326
#define RTFfamilyDecor 327
#define RTFfamilyTech 328

View file

@ -68,7 +68,7 @@ typedef void * GSRTFctxt;
%}
%union {
int number;
int number;
const char *text;
RTFcmd cmd;
}
@ -87,6 +87,21 @@ typedef void * GSRTFctxt;
%token RTFheader
%token RTFfooter
%token RTFpict
%token RTFplain
%token RTFparagraph
%token RTFdefaultParagraph
%token RTFrow
%token RTFcell
%token RTFtabulator
%token RTFemdash
%token RTFendash
%token RTFemspace
%token RTFenspace
%token RTFbullet
%token RTFlquote
%token RTFrquote
%token RTFldblquote
%token RTFrdblquote
%token <cmd> RTFred
%token <cmd> RTFgreen
%token <cmd> RTFblue
@ -105,8 +120,11 @@ typedef void * GSRTFctxt;
%token <cmd> RTFleftIndent
%token <cmd> RTFrightIndent
%token <cmd> RTFalignCenter
%token <cmd> RTFalignJustified
%token <cmd> RTFalignLeft
%token <cmd> RTFalignRight
%token <cmd> RTFlineSpace
%token <cmd> RTFspaceAbove
%token <cmd> RTFstyle
%token <cmd> RTFbold
%token <cmd> RTFitalic
@ -114,10 +132,7 @@ typedef void * GSRTFctxt;
%token <cmd> RTFunderlineStop
%token <cmd> RTFsubscript
%token <cmd> RTFsuperscript
%token <cmd> RTFtabulator
%token <cmd> RTFtabstop
%token <cmd> RTFparagraph
%token <cmd> RTFdefaultParagraph
%token <cmd> RTFfcharset
%token <cmd> RTFfprq
%token <cmd> RTFcpg
@ -259,8 +274,17 @@ rtfStatement: RTFfont { int font;
location = $1.parameter;
GSRTFtabstop(ctxt, location);}
| RTFalignCenter { GSRTFalignCenter(ctxt); }
| RTFalignJustified { GSRTFalignJustified(ctxt); }
| RTFalignLeft { GSRTFalignLeft(ctxt); }
| RTFalignRight { GSRTFalignRight(ctxt); }
| RTFspaceAbove { int space;
if ($1.isEmpty)
space = 0;
else
space = $1.parameter;
GSRTFspaceAbove(ctxt, space); }
| RTFlineSpace { GSRTFlineSpace(ctxt, $1.parameter); }
| RTFdefaultParagraph { GSRTFdefaultParagraph(ctxt); }
| RTFstyle { GSRTFstyle(ctxt, $1.parameter); }
| RTFcolorbg { int color;
@ -313,6 +337,9 @@ rtfStatement: RTFfont { int font;
on = NO;
GSRTFunderline(ctxt, on); }
| RTFunderlineStop { GSRTFunderline(ctxt, NO); }
| RTFplain { GSRTFdefaultCharacterStyle(ctxt); }
| RTFparagraph { GSRTFparagraph(ctxt); }
| RTFrow { GSRTFparagraph(ctxt); }
| RTFOtherStatement { GSRTFgenericRTFcommand(ctxt, $1); }
;
@ -355,7 +382,7 @@ rtfFontFamily:
/*
Font description end
Colour definition
*/
rtfColorDef: '{' RTFcolortable rtfColors '}'

View file

@ -144,11 +144,17 @@ LexKeyword RTFcommands[]={
"ansi", token(RTFansi),
"b", token(RTFbold),
"blue", token(RTFblue),
"bullet", token(RTFbullet),
"cb", token(RTFcolorbg),
"cell", token(RTFcell),
"cf", token(RTFcolorfg),
"colortbl", token(RTFcolortable),
"cpg", token(RTFcpg),
"dn", token(RTFsubscript),
"emdash", token(RTFemdash),
"emspace", token(RTFemspace),
"endash", token(RTFendash),
"enspace", token(RTFenspace),
"f", token(RTFfont),
"fcharset", token(RTFfcharset),
"fdecor", token(RTFfamilyDecor),
@ -156,7 +162,11 @@ LexKeyword RTFcommands[]={
"fmodern", token(RTFfamilyModern),
"fnil", token(RTFfamilyNil),
"fonttbl", token(RTFfontListStart),
/* All footers are mapped on one entry */
"footer", token(RTFfooter),
"footerf", token(RTFfooter),
"footerl", token(RTFfooter),
"footerr", token(RTFfooter),
"footnote", token(RTFfootnote),
"fprq", token(RTFfprq),
"froman", token(RTFfamilyRoman),
@ -165,10 +175,16 @@ LexKeyword RTFcommands[]={
"fswiss", token(RTFfamilySwiss),
"ftech", token(RTFfamilyTech),
"green", token(RTFgreen),
/* All headers are mapped on one entry */
"header", token(RTFheader),
"headerf", token(RTFheader),
"headerl", token(RTFheader),
"headerr", token(RTFheader),
"i", token(RTFitalic),
"info", token(RTFinfo),
"ldblquote", token(RTFldblquote),
"li", token(RTFleftIndent),
"lquote", token(RTFlquote),
"mac", token(RTFmac),
"margb", token(RTFmarginButtom),
"margl", token(RTFmarginLeft),
@ -181,18 +197,29 @@ LexKeyword RTFcommands[]={
"pc", token(RTFpc),
"pca", token(RTFpca),
"pict", token(RTFpict),
"plain", token(RTFplain),
"qc", token(RTFalignCenter),
"qj", token(RTFalignJustified),
"ql", token(RTFalignLeft),
"qr", token(RTFalignRight),
"rdblquote", token(RTFrdblquote),
"red", token(RTFred),
"ri", token(RTFrightIndent),
"row", token(RTFrow),
"rquote", token(RTFrquote),
"rtf", token(RTFstart),
"s", token(RTFstyle),
"sa", token(RTFspaceAbove),
"sl", token(RTFlineSpace),
"stylesheet", token(RTFstylesheet),
"tab", token(RTFtabulator),
"tx", token(RTFtabstop),
/* All underline are mapped on one entry */
"ul", token(RTFunderline),
"uld", token(RTFunderline),
"uldb", token(RTFunderline),
"ulnone", token(RTFunderlineStop),
"ulw", token(RTFunderline),
"up", token(RTFsuperscript)
};
@ -341,20 +368,29 @@ int GSRTFlex(YYSTYPE *lvalp, YYLTYPE *llocp, RTFscannerCtxt *lctxt) /* provide v
if (probeCommand(lctxt) == YES)
{
readCommand(lctxt, lvalp, &token);
if (token == RTFparagraph)
{
// release is up to the consumer
cv = calloc(1, 2);
cv[0] = '\n';
cv[1] = '\0';
lvalp->text = cv;
token = RTFtext;
return token;
}
else if (token == RTFtabulator)
c = '\t';
else
break;
switch (token)
{
case RTFtabulator: c = '\t';
break;
case RTFcell: c = '\t';
break;
case RTFemdash: c = '-';
break;
case RTFendash: c = '-';
break;
case RTFbullet: c = '*';
break;
case RTFlquote: c = '`';
break;
case RTFrquote: c = '\'';
break;
case RTFldblquote: c = '"';
break;
case RTFrdblquote: c = '"';
break;
default:
return token;
}
}
else
{
@ -380,33 +416,13 @@ int GSRTFlex(YYSTYPE *lvalp, YYLTYPE *llocp, RTFscannerCtxt *lctxt) /* provide v
break;
case '\n':
case '\r':
// release is up to the consumer
cv = calloc(1, 2);
cv[0] = '\n';
cv[1] = '\0';
lvalp->text = cv;
token = RTFtext;
return token;
return RTFparagraph;
case '{':
// release is up to the consumer
cv = calloc(1, 2);
cv[0] = '{';
cv[1] = '\0';
lvalp->text = cv;
token = RTFtext;
return token;
case '}':
// release is up to the consumer
cv = calloc(1, 2);
cv[0] = '}';
cv[1] = '\0';
lvalp->text = cv;
token = RTFtext;
return token;
case '\\':
// release is up to the consumer
cv = calloc(1, 2);
cv[0] = '\\';
cv[0] = c;
cv[1] = '\0';
lvalp->text = cv;
token = RTFtext;