1 /************************************************************************/
2 /*									*/
3 /*  Read the various document tables of an RTF text file into a		*/
4 /*  BufferDocument.							*/
5 /*									*/
6 /************************************************************************/
7 
8 #   include	"docRtfConfig.h"
9 
10 #   include	<stdio.h>
11 #   include	<ctype.h>
12 
13 #   include	<appDebugon.h>
14 
15 #   include	"docRtfReaderImpl.h"
16 
17 /************************************************************************/
18 /*									*/
19 /*  Consume the style name in the stylesheet of a document. It comes	*/
20 /*  last in the style, so we can save the style.			*/
21 /*									*/
22 /************************************************************************/
23 
docRtfStyleName(RtfReader * rrc,const char * name,int len)24 static int docRtfStyleName(	RtfReader *	rrc,
25 				const char *		name,
26 				int			len )
27     {
28     RtfReadingState *		rrs= rrc->rrcState;
29 
30     DocumentStyle *		ds;
31 
32     int				size;
33     const int			removeSemicolon= 1;
34 
35     const DocumentAttributeMap * const dam0= (const DocumentAttributeMap *)0;
36 
37     if  ( docRtfSaveDocEncodedText( rrc, name, len ) )
38 	{ LDEB(len); return -1;	}
39 
40     ds= &(rrc->rrcStyle);
41     switch( ds->dsLevel )
42 	{
43 	case DOClevSPAN:
44 	    break;
45 
46 	case 0: case -1:
47 	    LDEB(rrc->rrcStyle.dsLevel);
48 	    ds->dsStyleNumber= 0;
49 	    ds->dsLevel= DOClevPARA;
50 	    break;
51 
52 	case DOClevPARA:
53 	    ds->dsStyleNumber= rrs->rrsParagraphProperties.ppStyle;
54 	    break;
55 
56 	case DOClevROW:
57 	    /* NO! They have trowd and the sheet owns the style
58 	    ds->dsStyleNumber= rrc->rrcRowProperties.rpRowStyle;
59 	    */
60 	    break;
61 
62 	case DOClevSECT:
63 	    ds->dsStyleNumber= rrc->rrcSectionProperties.spStyle;
64 	    break;
65 
66 	default:
67 	    LDEB(rrc->rrcStyle.dsLevel); return -1;
68 	}
69 
70     if  ( docRtfStoreStyleProperties( rrc ) )
71 	{ LDEB(1);	}
72 
73     if  ( docRtfStoreSavedText( &(rrc->rrcStyle.dsName),
74 					    &size, rrc, removeSemicolon ) )
75 	{ LDEB(len); return -1;	}
76 
77     ds= docInsertStyle( &(rrc->rrDocument->bdStyleSheet),
78 					rrc->rrcStyle.dsStyleNumber,
79 					&(rrc->rrcStyle), dam0 );
80     if  ( ! ds )
81 	{ XDEB(ds); return -1;	}
82 
83     docCleanDocumentStyle( &(rrc->rrcStyle) );
84     docInitDocumentStyle( &(rrc->rrcStyle) );
85 
86     docRtfResetParagraphProperties( rrs );
87     docRtfResetTextAttribute( rrs, rrc->rrDocument );
88 
89     return 0;
90     }
91 
docRtfStyleGroup(const RtfControlWord * rcw,int arg,RtfReader * rrc)92 static int docRtfStyleGroup(		const RtfControlWord *	rcw,
93 					int			arg,
94 					RtfReader *	rrc )
95     {
96     RtfReadingState *	rrs= rrc->rrcState;
97 
98     switch( rcw->rcwID )
99 	{
100 	case DOClevSPAN:
101 	    rrc->rrcStyle.dsStyleNumber= arg;
102 	    rrc->rrcStyle.dsLevel= DOClevSPAN;
103 	    break;
104 
105 	case DOClevSECT:
106 	    rrc->rrcSectionProperties.spStyle= arg;
107 	    rrc->rrcStyle.dsStyleNumber= arg;
108 	    rrc->rrcStyle.dsLevel= DOClevSECT;
109 	    break;
110 
111 	case DOClevROW:
112 	    rrc->rrcRowProperties.rpRowStyle= arg;
113 	    rrc->rrcStyle.dsStyleNumber= arg;
114 	    rrc->rrcStyle.dsLevel= DOClevROW;
115 	    break;
116 
117 	case DOClevPARA:
118 	    rrs->rrsParagraphProperties.ppStyle= arg;
119 	    rrc->rrcStyle.dsStyleNumber= arg;
120 	    rrc->rrcStyle.dsLevel= DOClevPARA;
121 	    break;
122 
123 	default:
124 	    LDEB(rcw->rcwID);
125 	}
126 
127     if  ( docRtfReadGroup( rcw, 0, 0, rrc,
128 		    (RtfControlWord *)0, docRtfStyleName, (RtfCommitGroup)0 ) )
129 	{ SLDEB(rcw->rcwWord,arg); return -1;	}
130 
131     rrc->rrcStyle.dsLevel= DOClevANY;
132 
133     return 0;
134     }
135 
136 /************************************************************************/
137 /*									*/
138 /*  Inside a stylesheet.. These tags are handled differently.		*/
139 /*									*/
140 /************************************************************************/
141 
142 static RtfControlWord	docRtfStylesheetGroups[]=
143     {
144 	RTF_DEST_XX( "cs",	DOClevSPAN,	docRtfStyleGroup ),
145 	RTF_DEST_XX( "ds",	DOClevSECT,	docRtfStyleGroup ),
146 	RTF_DEST_XX( "ts",	DOClevROW,	docRtfStyleGroup ),
147 	RTF_DEST_XX( "tsrowd",	DOClevROW,	docRtfStyleGroup ),
148 	RTF_DEST_XX( "s",	DOClevPARA,	docRtfStyleGroup ),
149 
150 	{ (char *)0, 0, 0 }
151     };
152 
153 /************************************************************************/
154 /*									*/
155 /*  Consume a group introduced by a control word that is not a		*/
156 /*  destination.							*/
157 /*									*/
158 /************************************************************************/
159 
docRtfReadWordGroup(RtfReader * rrc,int gotArg,int arg,const char * controlWord,const RtfControlWord * groupWords,RtfAddTextParticule addParticule)160 static int docRtfReadWordGroup(	RtfReader *	rrc,
161 				int			gotArg,
162 				int			arg,
163 				const char *		controlWord,
164 				const RtfControlWord *	groupWords,
165 				RtfAddTextParticule	addParticule )
166     {
167     const RtfControlWord *	rcw;
168 
169     rcw= docRtfFindPropertyWord( controlWord );
170     if  ( rcw )
171 	{
172 	if  ( docRtfReadGroupX( rcw, rcw, gotArg, arg, rrc, groupWords,
173 					addParticule, (RtfCommitGroup)0 ) )
174 	    { SDEB(rcw->rcwWord); return -1;	}
175 	}
176     else{
177 	if  ( rrc->rrcComplainUnknown )
178 	    { LSDEB(rrc->rrCurrentLine,controlWord);	}
179 
180 	if  ( docRtfReadUnknownGroup( rrc ) )
181 	    { LSDEB(rrc->rrCurrentLine,controlWord); return -1;	}
182 	}
183 
184     return 0;
185     }
186 
187 /************************************************************************/
188 /*									*/
189 /*  Consume the stylesheet in a document.				*/
190 /*									*/
191 /*  Because <styledef> is optional in the stylesheet. (For the first	*/
192 /*  style at least.) The normal mechanism of the parser does not work	*/
193 /*  for the stylesheet. Do things by hand.				*/
194 /*									*/
195 /************************************************************************/
196 
docRtfStylesheet(const RtfControlWord * rcw,int arg,RtfReader * rrc)197 int docRtfStylesheet(		const RtfControlWord *	rcw,
198 				int			arg,
199 				RtfReader *		rrc )
200     {
201     RtfReadingState *		rrs= rrc->rrcState;
202     int				res;
203 
204     char			controlWord[TEDszRTFCONTROL+1];
205     int				gotArg;
206     int				c;
207 
208     docCleanDocumentStyle( &(rrc->rrcStyle) );
209     docInitDocumentStyle( &(rrc->rrcStyle) );
210 
211     res= docRtfFindControl( rrc, &c, controlWord, &gotArg, &arg );
212     if  ( res < 0 )
213 	{ LDEB(res); return -1;	}
214 
215     rrc->rrcStyle.dsLevel= DOClevPARA;
216     rrs->rrsParagraphProperties.ppStyle= 0;
217 
218     for (;;)
219 	{
220 	rrs= rrc->rrcState;
221 
222 	switch( res )
223 	    {
224 	    case RTFfiCLOSE:
225 		break;
226 
227 	    case RTFfiCTRLGROUP:
228 		rcw= docRtfFindWord( controlWord, docRtfStylesheetGroups );
229 		if  ( ! rcw )
230 		    {
231 		    if  ( docRtfReadWordGroup( rrc, gotArg, arg, controlWord,
232 					(RtfControlWord *)0, docRtfStyleName ) )
233 			{ SDEB(controlWord); return -1;	}
234 		    }
235 		else{
236 		  groupFound:
237 		    res= docRtfApplyControlWord( rcw, gotArg, arg, rrc );
238 		    if  ( res < 0 )
239 			{ LDEB(res); SDEB(controlWord); return -1;	}
240 		    }
241 
242 		res= docRtfFindControl( rrc, &c, controlWord, &gotArg, &arg );
243 		if  ( res < 0 )
244 		    { LDEB(res); return -1;	}
245 		continue;
246 
247 	    case RTFfiSTARGROUP:
248 		rcw= docRtfFindWord( controlWord, docRtfStylesheetGroups );
249 		if  ( rcw )
250 		    { goto groupFound; }
251 
252 		rrc->rrcInIgnoredGroup++;
253 
254 		if  ( docRtfReadUnknownGroup( rrc ) )
255 		    { LDEB(1); rrc->rrcInIgnoredGroup--; return -1;	}
256 
257 		rrc->rrcInIgnoredGroup--;
258 
259 		res= docRtfFindControl( rrc, &c, controlWord, &gotArg, &arg );
260 		if  ( res < 0 )
261 		    { LDEB(res); return -1;	}
262 		continue;
263 
264 	    case RTFfiTEXTGROUP:
265 		{
266 		const RtfControlWord *	rcwApplyFirst= (RtfControlWord *)0;
267 		int			argApplyFirst= -1;
268 		int			gotArgApplyFirst= 0;
269 
270 		if  ( docRtfReadGroupX( (const RtfControlWord *)0,
271 			rcwApplyFirst, gotArgApplyFirst, argApplyFirst,
272 			rrc, docRtfStylesheetGroups,
273 			docRtfStyleName, (RtfCommitGroup)0 ) )
274 		    { LDEB(1); return -1;	}
275 
276 		res= docRtfFindControl( rrc, &c, controlWord, &gotArg, &arg );
277 		if  ( res < 0 )
278 		    { LDEB(res); return -1;	}
279 		}
280 		continue;
281 
282 	    default:
283 		LDEB(res); return -1;
284 	    }
285 
286 	break;
287 	}
288 
289     return 0;
290     }
291 
292 /************************************************************************/
293 /*									*/
294 /*  Handle a style property when reading RTF.				*/
295 /*									*/
296 /************************************************************************/
297 
docRtfRememberStyleProperty(const RtfControlWord * rcw,int arg,RtfReader * rrc)298 int docRtfRememberStyleProperty(	const RtfControlWord *	rcw,
299 					int			arg,
300 					RtfReader *		rrc )
301     {
302     DocumentStyle *	ds= &(rrc->rrcStyle);
303 
304     /* \s \cs \ds tags but \s and \ds handeled with the para/sect style */
305     if  ( rcw->rcwID == DSpropSTYLE_NUMBER )
306 	{
307 	ds->dsStyleNumber= arg;
308 	/* Exceptional trick */
309 	ds->dsLevel= rcw->rcwEnumValue;
310 	return 0;
311 	}
312 
313     if  ( docSetStyleProperty( ds, rcw->rcwID, arg ) )
314 	{ SLDEB(rcw->rcwWord,arg); return -1;	}
315 
316     return 0;
317     }
318 
319 /************************************************************************/
320 /*									*/
321 /*  Remember accumulated properties of the reader on the current style.	*/
322 /*									*/
323 /************************************************************************/
324 
docRtfStoreStyleProperties(RtfReader * rrc)325 int docRtfStoreStyleProperties(		RtfReader *		rrc )
326     {
327     RtfReadingState *		rrs= rrc->rrcState;
328     BufferDocument *		bd= rrc->rrDocument;
329     int				mindTable= 1;
330 
331     DocumentStyle *		ds= &(rrc->rrcStyle);
332 
333     /*  SECT	*/
334     docCopySectionProperties( &(ds->dsSectProps),
335 					    &(rrc->rrcSectionProperties) );
336 
337     /*  ROW	*/
338 
339     /*  CELL	*/
340     ds->dsCellProps.cpShadingNumber= docItemShadingNumber(
341 				    rrc->rrDocument, &(rrc->rrcCellShading) );
342     if  ( ds->dsCellProps.cpShadingNumber < 0 )
343 	{ LDEB(ds->dsCellProps.cpShadingNumber);	}
344 
345     /*  PARA	*/
346     if  ( docRtfSetParaProperties( &(ds->dsParaProps), bd,
347 						    mindTable, rrs, -1 ) )
348 	{ LDEB(1);	}
349 
350     /*  SPAN	*/
351     if  ( rrs->rrsTextShadingChanged )
352 	{
353 	docRtfRefreshTextShading( rrc, rrs );
354 	PROPmaskADD( &(rrc->rrcStyle.dsTextMask), TApropSHADING );
355 	}
356 
357     ds->dsTextAttribute= rrs->rrsTextAttribute;
358 
359     return 0;
360     }
361 
362