1 /************************************************************************/
2 /*									*/
3 /*  Read an RTF text file into a BufferDocument				*/
4 /*									*/
5 /************************************************************************/
6 
7 #   include	"docRtfConfig.h"
8 
9 #   include	<stdio.h>
10 #   include	<ctype.h>
11 #   include	<bitmap.h>
12 
13 #   include	<docObjectProperties.h>
14 #   include	<docParaParticules.h>
15 #   include	<docShape.h>
16 #   include	<appDebugon.h>
17 
18 #   include	"docRtfReaderImpl.h"
19 #   include	"docRtfTagEnum.h"
20 #   include	"docRtfTags.h"
21 
docRtfObjectProperty(const RtfControlWord * rcw,int arg,RtfReader * rrc)22 int docRtfObjectProperty(	const RtfControlWord *	rcw,
23 				int			arg,
24 				RtfReader *	rrc )
25     {
26     InsertedObject *	io;
27 
28     io= docGetObject( rrc->rrDocument, rrc->rrcInsertedObjectNr );
29     if  ( ! io )
30 	{
31 	LSXDEB(rrc->rrCurrentLine,rcw->rcwWord,rrc->rrcInsertedObjectNr);
32 	return 0;
33 	}
34 
35     switch( rcw->rcwID )
36 	{
37 	case IOpropRESULT_KIND:
38 	    io->ioRtfResultKind= arg;
39 	    break;
40 
41 	case IOpropEMBED_KIND:
42 	    io->ioRtfEmbedKind= arg;
43 	    break;
44 
45 	case IOpropOBJTWIPS_WIDE:
46 	    io->ioTwipsWide= arg;
47 	    break;
48 	case IOpropOBJTWIPS_HIGH:
49 	    io->ioTwipsHigh= arg;
50 	    break;
51 
52 	case IOpropOBJSCALE_X:
53 	    if  ( arg < 1 )
54 		{ LDEB(arg); arg= 1;	}
55 	    io->ioScaleXSet= arg;
56 	    io->ioScaleXUsed= io->ioScaleXSet;
57 	    break;
58 	case IOpropOBJSCALE_Y:
59 	    if  ( arg < 1 )
60 		{ LDEB(arg); arg= 1;	}
61 	    io->ioScaleYSet= arg;
62 	    io->ioScaleYUsed= io->ioScaleYSet;
63 	    break;
64 
65 	case IOpropOBJCROP_TOP:
66 	    io->ioTopCropTwips= arg;
67 	    break;
68 	case IOpropOBJCROP_BOTTOM:
69 	    io->ioBottomCropTwips= arg;
70 	    break;
71 	case IOpropOBJCROP_LEFT:
72 	    io->ioLeftCropTwips= arg;
73 	    break;
74 	case IOpropOBJCROP_RIGHT:
75 	    io->ioRightCropTwips= arg;
76 	    break;
77 
78 	case IOprop_UNSUPPORTED:
79 	    break;
80 
81 	default:
82 	    SLDEB(rcw->rcwWord,rcw->rcwID);
83 	    break;
84 	}
85 
86     return 0;
87     }
88 
docRtfPictureProperty(const RtfControlWord * rcw,int arg,RtfReader * rrc)89 int docRtfPictureProperty(	const RtfControlWord *	rcw,
90 				int			arg,
91 				RtfReader *		rrc )
92     {
93     PictureProperties *	pip= &(rrc->rrcPictureProperties);
94 
95     switch( rcw->rcwID )
96 	{
97 	case PIPpropTYPE:
98 	    /* Exception to cover \wmetafile8 */
99 	    if  ( docSetPictureProperty( pip, rcw->rcwID, rcw->rcwEnumValue ) )
100 		{ SDEB(rcw->rcwWord); return 0;	}
101 
102 	    if  ( pip->pipType == DOCokPICTWMETAFILE )
103 		{ pip->pipMapMode= arg;	}
104 	    break;
105 
106 	case PIPpropPICSCALE_X:
107 	    if  ( docSetPictureProperty( pip, rcw->rcwID, arg ) )
108 		{ SDEB(rcw->rcwWord); return 0;	}
109 
110 	    pip->pipScaleXUsed= pip->pipScaleXSet;
111 	    break;
112 	case PIPpropPICSCALE_Y:
113 	    if  ( docSetPictureProperty( pip, rcw->rcwID, arg ) )
114 		{ SDEB(rcw->rcwWord); return 0;	}
115 
116 	    pip->pipScaleYUsed= pip->pipScaleYSet;
117 	    break;
118 
119 	default:
120 	    if  ( docSetPictureProperty( pip, rcw->rcwID, arg ) )
121 		{ SDEB(rcw->rcwWord); return 0;	}
122 
123 	    break;
124 	}
125 
126     PROPmaskADD( &(rrc->rrcPicturePropMask), rcw->rcwID );
127 
128     return 0;
129     }
130 
131 /************************************************************************/
132 /*									*/
133 /*  Consume picture property tags.					*/
134 /*									*/
135 /*  In particular:							*/
136 /*	shplid:	The identification of the shape of the picture.		*/
137 /*	sp:	Various shape properties for the picture.		*/
138 /*  We make a drawing shape to receive the properties.			*/
139 /*									*/
140 /************************************************************************/
141 
142 static RtfControlWord	docRtfPicpropGroups[]=
143     {
144 	RTF_DEST_XX( "sp",	1,	docRtfShapeProperty ),
145 
146 	{ (char *)0, 0, 0 }
147     };
148 
docRtfReadPicprop(const RtfControlWord * rcw,int arg,RtfReader * rrc)149 static int docRtfReadPicprop(	const RtfControlWord *	rcw,
150 				int			arg,
151 				RtfReader *		rrc )
152     {
153     int			rval;
154     DrawingShape *	parent= rrc->rrDrawingShape;
155     DrawingShape *	ds;
156 
157     ds= docClaimDrawingShape( &(rrc->rrDocument->bdShapeList) );
158     if  ( ! ds )
159 	{ XDEB(ds); return -1;	}
160 
161     ds->dsDrawing.sdShapeType= rcw->rcwID;
162 
163     rrc->rrDrawingShape= ds;
164 
165     rval= docRtfConsumeGroup( (const RtfControlWord *)0, 0, -1,
166 				rrc, docRtfPicpropGroups, docRtfIgnoreText );
167     if  ( rval )
168 	{ SLDEB(rcw->rcwWord,rval);	}
169 
170     /* ? */
171     docDeleteDrawingShape( rrc->rrDocument, ds );
172 
173     rrc->rrDrawingShape= parent;
174     return rval;
175     }
176 
177 static RtfControlWord	docRtfPictGroups[]=
178     {
179 	RTF_DEST_XX( "picprop",	SHPtyPICPROP,	docRtfReadPicprop ),
180 
181 	{ (char *)0, 0, 0 }
182     };
183 
docRtfSaveObjectData(RtfReader * rrc,const char * text,int len)184 static int docRtfSaveObjectData(	RtfReader *		rrc,
185 					const char *		text,
186 					int			len )
187     {
188     InsertedObject *	io;
189 
190     io= docGetObject( rrc->rrDocument, rrc->rrcInsertedObjectNr );
191     if  ( ! io )
192 	{ LXDEB(rrc->rrcInsertedObjectNr,io); return -1;	}
193 
194     if  ( docObjectSetData( io, text, len ) )
195 	{ LDEB(len); return -1;	}
196 
197     return 0;
198     }
199 
docRtfSaveResultData(RtfReader * rrc,const char * text,int len)200 static int docRtfSaveResultData(	RtfReader *		rrc,
201 					const char *		text,
202 					int			len )
203     {
204     InsertedObject *	io;
205 
206     io= docGetObject( rrc->rrDocument, rrc->rrcInsertedObjectNr );
207     if  ( ! io )
208 	{ LXDEB(rrc->rrcInsertedObjectNr,io); return -1;	}
209 
210     if  ( docSetResultData( io, text, len ) )
211 	{ LDEB(len); return -1;	}
212 
213     return 0;
214     }
215 
docRtfReadPict(const RtfControlWord * rcw,int arg,RtfReader * rrc)216 int docRtfReadPict(	const RtfControlWord *	rcw,
217 			int			arg,
218 			RtfReader *		rrc )
219     {
220     int				rval= 0;
221 
222     RtfReadingState *		externRrs= rrc->rrcState;
223     RtfReadingState		internRrs;
224 
225     TextParticule *		tp;
226 
227     BufferItem *		paraNode;
228     int				ioNrSave= rrc->rrcInsertedObjectNr;
229 
230     RtfAddTextParticule		addParticule;
231 
232     paraNode= docRtfGetParaNode( rrc );
233     if  ( ! paraNode )
234 	{ XDEB(paraNode); rval= -1; goto ready; }
235 
236     utilPropMaskClear( &(rrc->rrcPicturePropMask) );
237     docInitPictureProperties( &(rrc->rrcPictureProperties) );
238 
239     if  ( rrc->rrcInIgnoredGroup )
240 	{ addParticule= docRtfIgnoreText;	}
241     else{
242 	addParticule= docRtfSaveObjectData;
243 
244 	if  ( externRrs->rrsTextShadingChanged )
245 	    { docRtfRefreshTextShading( rrc, externRrs );	}
246 
247 	tp= docAppendObject( rrc->rrDocument, paraNode,
248 					    &(externRrs->rrsTextAttribute) );
249 	if  ( ! tp )
250 	    { LDEB(paraNode->biParaParticuleCount); rval= -1; goto ready; }
251 
252 	rrc->rrcInsertedObjectNr= tp->tpObjectNumber;
253 	}
254 
255     docRtfPushReadingState( rrc, &internRrs );
256 
257     rval= docRtfConsumeGroup( (const RtfControlWord *)0, 0, -1,
258 					rrc, docRtfPictGroups, addParticule );
259 
260     if  ( rval )
261 	{ LDEB(rval);	}
262     if  ( rrc->rrcInsertedObjectNr >= 0 )
263 	{
264 	InsertedObject *	io;
265 	PictureProperties *	pip;
266 
267 	io= docGetObject( rrc->rrDocument, rrc->rrcInsertedObjectNr );
268 	if  ( ! io )
269 	    { LXDEB(rrc->rrcInsertedObjectNr,io); rval= -1; goto ready;	}
270 	pip= &(rrc->rrcPictureProperties);
271 
272 	io->ioKind= pip->pipType;
273 	io->ioPictureProperties= rrc->rrcPictureProperties;
274 
275 	io->ioTwipsWide= pip->pipTwipsWide;
276 	io->ioTwipsHigh= pip->pipTwipsHigh;
277 	io->ioScaleXSet= pip->pipScaleXSet;
278 	io->ioScaleYSet= pip->pipScaleYSet;
279 	io->ioScaleXUsed= pip->pipScaleXUsed;
280 	io->ioScaleYUsed= pip->pipScaleYUsed;
281 	}
282 
283     docRtfPopReadingState( rrc );
284 
285   ready:
286 
287     rrc->rrcInsertedObjectNr= ioNrSave;
288 
289     return rval;
290     }
291 
docRtfReadShpXPict(const RtfControlWord * rcw,int arg,RtfReader * rrc)292 int docRtfReadShpXPict(		const RtfControlWord *	rcw,
293 				int			arg,
294 				RtfReader *		rrc )
295     {
296     int			rval;
297     int			ignore= 0;
298 
299     if  ( ! docRtfGetParaNode( rrc ) )
300 	{ SDEB(rcw->rcwWord); return -1; }
301 
302     switch( rcw->rcwID )
303 	{
304 	case PICshpSHPPICT:
305 	    ignore= 0;
306 	    break;
307 	case PICshpNONSHPPICT:
308 	    ignore= 1;
309 	    break;
310 	default:
311 	    SLDEB(rcw->rcwWord,rcw->rcwID);
312 	    break;
313 	}
314 
315     rrc->rrcInIgnoredGroup += ignore;
316 
317     rval= docRtfConsumeGroup( (const RtfControlWord *)0, 0, -1,
318 			rrc, (const RtfControlWord *)0, docRtfIgnoreText );
319     if  ( rval )
320 	{ SLDEB(rcw->rcwWord,rval);	}
321 
322     rrc->rrcInIgnoredGroup -= ignore;
323 
324     return rval;
325     }
326 
docRtfReadResultPict(const RtfControlWord * rcw,int arg,RtfReader * rrc)327 static int docRtfReadResultPict(	const RtfControlWord *	rcw,
328 					int			arg,
329 					RtfReader *		rrc )
330     {
331     int				rval;
332 
333     RtfAddTextParticule		addParticule;
334 
335     if  ( ! docRtfGetParaNode( rrc ) )
336 	{ SDEB(rcw->rcwWord); return -1; }
337 
338     if  ( rrc->rrcInIgnoredGroup )
339 	{ addParticule= docRtfIgnoreText;	}
340     else{ addParticule= docRtfSaveResultData;	}
341 
342     rval= docRtfConsumeGroup( (const RtfControlWord *)0, 0, -1,
343 					rrc, docRtfPictGroups, addParticule );
344 
345     if  ( rval )
346 	{ SLDEB(rcw->rcwWord,rval);	}
347 
348     if  ( rrc->rrcInsertedObjectNr >= 0 )
349 	{
350 	InsertedObject *	io;
351 	PictureProperties *	pip;
352 
353 	io= docGetObject( rrc->rrDocument, rrc->rrcInsertedObjectNr );
354 	if  ( ! io )
355 	    { LXDEB(rrc->rrcInsertedObjectNr,io); return -1;	}
356 	pip= &(rrc->rrcPictureProperties);
357 
358 	io->ioResultKind= pip->pipType;
359 	io->ioPictureProperties= rrc->rrcPictureProperties;
360 
361 	if  ( io->ioTwipsWide < 2 )
362 	    { io->ioTwipsWide= pip->pipTwipsWide;	}
363 	if  ( io->ioTwipsHigh < 2 )
364 	    { io->ioTwipsHigh= pip->pipTwipsHigh;	}
365 	if  ( io->ioScaleXSet == 100 )
366 	    {
367 	    io->ioScaleXSet= pip->pipScaleXSet;
368 	    io->ioScaleXUsed= pip->pipScaleXUsed;
369 	    }
370 	if  ( io->ioScaleYSet == 100 )
371 	    {
372 	    io->ioScaleYSet= pip->pipScaleYSet;
373 	    io->ioScaleYUsed= pip->pipScaleYUsed;
374 	    }
375 	}
376 
377     return rval;
378     }
379 
380 static RtfControlWord	docRtfReadResultXPpictGroups[]=
381     {
382 	RTF_DEST_XX( RTFtag_pict,	PICshpPICT,	docRtfReadResultPict ),
383 
384 	{ (char *)0, 0, 0 }
385     };
386 
docRtfReadResultXPpict(const RtfControlWord * rcw,int arg,RtfReader * rrc)387 static int docRtfReadResultXPpict(	const RtfControlWord *	rcw,
388 					int			arg,
389 					RtfReader *		rrc )
390     {
391     int				rval;
392     int				ignore= 0;
393 
394     if  ( ! docRtfGetParaNode( rrc ) )
395 	{ SDEB(rcw->rcwWord); return -1; }
396 
397     switch( rcw->rcwID )
398 	{
399 	case PICshpSHPPICT:
400 	    ignore= 0;
401 	    break;
402 	case PICshpNONSHPPICT:
403 	    ignore= 1;
404 	    break;
405 	default:
406 	    SLDEB(rcw->rcwWord,rcw->rcwID);
407 	    break;
408 	}
409 
410     rrc->rrcInIgnoredGroup += ignore;
411 
412     rval= docRtfConsumeGroup( (const RtfControlWord *)0, 0, -1,
413 			rrc, docRtfReadResultXPpictGroups, docRtfIgnoreText );
414 
415     if  ( rval )
416 	{ SLDEB(rcw->rcwWord,rval);	}
417 
418     rrc->rrcInIgnoredGroup -= ignore;
419 
420     return rval;
421     }
422 
docRtfSaveObjectClass(RtfReader * rrc,const char * text,int len)423 static int docRtfSaveObjectClass(	RtfReader *	rrc,
424 					const char *		text,
425 					int			len )
426     {
427     InsertedObject *	io;
428 
429     io= docGetObject( rrc->rrDocument, rrc->rrcInsertedObjectNr );
430     if  ( ! io )
431 	{ LXDEB(rrc->rrcInsertedObjectNr,io); return -1;	}
432 
433     if  ( docSetObjectClass( io, text, len ) )
434 	{ LDEB(len); return -1;	}
435 
436     return 0;
437     }
438 
docRtfSaveObjectName(RtfReader * rrc,const char * text,int len)439 static int docRtfSaveObjectName(	RtfReader *	rrc,
440 					const char *		text,
441 					int			len )
442     {
443     InsertedObject *	io;
444 
445     io= docGetObject( rrc->rrDocument, rrc->rrcInsertedObjectNr );
446     if  ( ! io )
447 	{ LXDEB(rrc->rrcInsertedObjectNr,io); return -1;	}
448 
449     if  ( docSetObjectName( io, text, len ) )
450 	{ LDEB(len); return -1;	}
451 
452     return 0;
453     }
454 
docRtfObjectClass(const RtfControlWord * rcw,int arg,RtfReader * rrc)455 static int docRtfObjectClass(	const RtfControlWord *	rcw,
456 				int			arg,
457 				RtfReader *	rrc )
458     {
459     int				rval;
460 
461     rval= docRtfConsumeGroup( (const RtfControlWord *)0, 0, -1,
462 			rrc, (RtfControlWord *)0, docRtfSaveObjectClass );
463 
464     if  ( rval )
465 	{ SLDEB(rcw->rcwWord,rval);	}
466 
467     return rval;
468     }
469 
docRtfObjectName(const RtfControlWord * rcw,int arg,RtfReader * rrc)470 static int docRtfObjectName(	const RtfControlWord *	rcw,
471 				int			arg,
472 				RtfReader *	rrc )
473     {
474     int				rval;
475 
476     rval= docRtfConsumeGroup( (const RtfControlWord *)0, 0, -1,
477 			rrc, (RtfControlWord *)0, docRtfSaveObjectName );
478     if  ( rval )
479 	{ SLDEB(rcw->rcwWord,rval);	}
480 
481     return rval;
482     }
483 
docRtfObjectData(const RtfControlWord * rcw,int arg,RtfReader * rrc)484 static int docRtfObjectData(	const RtfControlWord *	rcw,
485 				int			arg,
486 				RtfReader *	rrc )
487     {
488     int				rval;
489 
490     rval= docRtfConsumeGroup( (const RtfControlWord *)0, 0, -1,
491 			rrc, (RtfControlWord *)0, docRtfSaveObjectData );
492     if  ( rval )
493 	{ SLDEB(rcw->rcwWord,rval);	}
494 
495     return rval;
496     }
497 
498 static RtfControlWord	docRtfResultGroups[]=
499 {
500     RTF_DEST_XX( RTFtag_pict,	PICshpPICT,	docRtfReadResultPict ),
501     RTF_DEST_XX( "shppict",	PICshpSHPPICT,	docRtfReadResultXPpict ),
502     RTF_DEST_XX( "nonshppict",	PICshpNONSHPPICT, docRtfReadResultXPpict ),
503 
504     { (char *)0, 0, 0 }
505 };
506 
docRtfObjectResult(const RtfControlWord * rcw,int arg,RtfReader * rrc)507 static int docRtfObjectResult(	const RtfControlWord *	rcw,
508 				int			arg,
509 				RtfReader *	rrc )
510     {
511     int				rval;
512 
513     rval= docRtfConsumeGroup( (const RtfControlWord *)0, 0, -1,
514 				rrc, docRtfResultGroups, docRtfIgnoreText );
515     if  ( rval )
516 	{ SLDEB(rcw->rcwWord,rval);	}
517 
518     return rval;
519     }
520 
521 static RtfControlWord	docRtfObjectGroups[]=
522     {
523 	RTF_DEST_XX( "objname",	RTFidOBJNAME,	docRtfObjectName ),
524 	RTF_DEST_XX( "objclass",RTFidOBJCLASS,	docRtfObjectClass ),
525 	RTF_DEST_XX( "objdata",	RTFidOBJDATA,	docRtfObjectData ),
526 	RTF_DEST_XX( RTFtag_result,	RTFidOBJDATA,	docRtfObjectResult ),
527 
528 	{ (char *)0, 0, 0 }
529     };
530 
docRtfReadObject(const RtfControlWord * rcw,int arg,RtfReader * rrc)531 int docRtfReadObject(	const RtfControlWord *	rcw,
532 			int			arg,
533 			RtfReader *		rrc )
534     {
535     RtfReadingState *		rrs= rrc->rrcState;
536     int				rval;
537 
538     int				ioNrSave= rrc->rrcInsertedObjectNr;
539 
540     const TextParticule *	tp;
541     InsertedObject *		io;
542 
543     if  ( ! docRtfGetParaNode( rrc ) )
544 	{ SDEB(rcw->rcwWord); return -1; }
545 
546     utilPropMaskClear( &(rrc->rrcPicturePropMask) );
547     docInitPictureProperties( &(rrc->rrcPictureProperties) );
548 
549     if  ( rrs->rrsTextShadingChanged )
550 	{ docRtfRefreshTextShading( rrc, rrs );	}
551 
552     tp= docAppendObject( rrc->rrDocument, rrc->rrcNode,
553 						&(rrs->rrsTextAttribute) );
554     if  ( ! tp )
555 	{ SPDEB(rcw->rcwWord,tp); return -1;	}
556 
557     rrc->rrcInsertedObjectNr= tp->tpObjectNumber;
558     io= docGetObject( rrc->rrDocument, rrc->rrcInsertedObjectNr );
559     if  ( ! io )
560 	{ LXDEB(rrc->rrcInsertedObjectNr,io); return -1;	}
561     io->ioKind= DOCokOLEOBJECT;
562 
563     rval= docRtfConsumeGroup( (const RtfControlWord *)0, 0, -1,
564 			rrc, docRtfObjectGroups, docRtfSaveObjectData );
565 
566     rrc->rrcInsertedObjectNr= ioNrSave;
567 
568     if  ( rval )
569 	{ SLDEB(rcw->rcwWord,rval);	}
570 
571     return rval;
572     }
573