1 /************************************************************************/
2 /*									*/
3 /*  Reading of Rtf files. (Various utility functions.)			*/
4 /*									*/
5 /************************************************************************/
6 
7 #   include	"docRtfConfig.h"
8 
9 #   include	<stdlib.h>
10 #   include	<stdio.h>
11 #   include	<ctype.h>
12 
13 #   include	<appDebugon.h>
14 
15 #   include	<appUnit.h>
16 #   include	<textOfficeCharset.h>
17 #   include	<textConverter.h>
18 #   include	<textConverterImpl.h>
19 #   include	<docShape.h>
20 
21 #   include	"docRtfReaderImpl.h"
22 
23 /************************************************************************/
24 /*									*/
25 /*  Reset the attributes in an RtfReader to the defaults.	*/
26 /*									*/
27 /************************************************************************/
28 
docRtfInitReader(RtfReader * rr)29 static void docRtfInitReader(	RtfReader *	rr )
30     {
31     rr->rrcAddParticule= (RtfAddTextParticule)0;
32     rr->rrcSis= (SimpleInputStream *)0;
33 
34     rr->rrcLevel= DOClevOUT;
35     rr->rrcState= (RtfReadingState *)0;
36     rr->rrcInIgnoredGroup= 0;
37 
38 				/****************************************/
39 				/*  The depth in the document		*/
40 				/*  == Type of BufferItem.		*/
41 				/****************************************/
42     rr->rrcComplainUnknown= 1;
43 				/****************************************/
44 				/*  Complain about unknown control.	*/
45 				/****************************************/
46     rr->rrcCharacterAhead= EOF;
47     rr->rrcAfterNoteref= 0;
48 
49     rr->rrReadFlags= 0;
50 
51     rr->rrCurrentLine= 1;
52     rr->rrDocument= (BufferDocument *)0;
53     rr->rrcTree= (DocumentTree *)0;
54     rr->rrcNode= (struct BufferItem *)0;
55 				/****************************************/
56 				/*  The current position in the input.	*/
57 				/****************************************/
58 				/****************************************/
59 				/* Attributes of the current position.	*/
60 				/****************************************/
61     docInitFrameProperties( &(rr->rrcRowFrameProperties) );
62     docInitCellProperties( &(rr->rrcCellProperties) );
63     docInitItemShading( &(rr->rrcCellShading) );
64     docInitSectionProperties( &(rr->rrcSectionProperties) );
65     rr->rrcSectionColumn= 0;
66     docInitRowProperties( &(rr->rrcRowProperties) );
67     docInitItemShading( &(rr->rrcRowShading) );
68     utilPropMaskClear( &(rr->rrcRowPropertyMask) );
69     utilPropMaskClear( &(rr->rrcCellPropertyMask) );
70 
71     docInitSelectionScope( &(rr->rrcSelectionScope) );
72 
73     docInitDocumentList( &(rr->rrcDocumentList) );
74     docInitListOverride( &(rr->rrcListOverride) );
75     docInitDocumentListLevel( &(rr->rrcDocumentListLevel) );
76     docInitListOverrideLevel( &(rr->rrcListOverrideLevel) );
77     utilInitMemoryBuffer( &(rr->rrcListLevelText) );
78     utilInitMemoryBuffer( &(rr->rrcListLevelNumbers) );
79 
80     docInitBorderProperties( &(rr->rrcBorderProperties) );
81     rr->rrDrawingShape= (struct DrawingShape *)0;
82     rr->rrShapeProperty= (struct RtfControlWord *)0;
83     utilInitMemoryBuffer( &(rr->rrShapePropertyName) );
84     utilInitMemoryBuffer( &(rr->rrShapePropertyValue) );
85     rr->rrcNextObjectVertex= 0;
86     docInitTabStop( &(rr->rrcTabStop) );
87 
88     docInitParagraphNumber( &(rr->rrcParagraphNumber) );
89     rr->rrcParagraphNumbers= (ParagraphNumber *)0;
90     rr->rrcParagraphNumberCount= 0;
91 
92     rr->rrcColor.rgb8Red= rr->rrcColor.rgb8Green= rr->rrcColor.rgb8Blue= 0;
93     rr->rrcGotComponent= 0;
94 
95     utilInvalidateTime( &(rr->rrcTm) );
96     rr->rrcInfoText= (unsigned char *)0;
97 				/****************************************/
98 				/*  Document properties.		*/
99 				/****************************************/
100     docInitDocumentFont( &(rr->rrcCurrentFont) );
101     utilInitPagedList( &(rr->rrcEncodedFontList) );
102     docRtfInitEncodedFont( &(rr->rrcCurrentEncodedFont) );
103     utilStartPagedList( &(rr->rrcEncodedFontList),
104 				sizeof(EncodedFont),
105 				(InitPagedListItem)docRtfInitEncodedFont,
106 				(CleanPagedListItem)0 );
107 
108     rr->rrcDefaultFont= -1;
109     rr->rrcDefaultFontDbch= -1;
110     rr->rrcDefaultFontLoch= -1;
111     rr->rrcDefaultFontHich= -1;
112     rr->rrcDefaultFontBi= -1;
113 
114     docInitDocumentStyle( &(rr->rrcStyle) );
115     docInitDocumentProperties( &(rr->rrcDocumentProperties) );
116     utilPropMaskClear( &(rr->rrcDocPropertyMask) );
117 				/****************************************/
118 				/*  For reading the font table.		*/
119 				/****************************************/
120     rr->rrcInsertedObjectNr= -1;
121     utilInitMemoryBuffer( &(rr->rrcBookmark) );
122     rr->rrcFieldStack= (struct RtfFieldStackLevel *)0;
123     rr->rrcLastFieldNumber= -1;
124 				/****************************************/
125 				/*  For reading 'objects' and pictures.	*/
126 				/*  For reading 'fields'.		*/
127 				/****************************************/
128     rr->rrAfterParaHeadField= 0;
129     rr->rrParagraphBreakOverride= -1;
130     rr->rrcGotDocGeometry= 0;
131 				/****************************************/
132 				/*  For coping with the way word saves	*/
133 				/*  {\pntext ... }			*/
134 				/****************************************/
135 
136     rr->rrRtfTextConverter= (struct TextConverter *)0;
137     rr->rrTextTextConverter= (struct TextConverter *)0;
138 
139 				/*
140 				 * Only used for reading Undo/Redo traces.
141 				 */
142     rr->rrcTraceReadWhat= 0;
143     rr->rrcTraceCommand= -1;
144     rr->rrcTraceSelectionPosition= -1;
145     rr->rrcTraceInProps= 0;
146 
147     docInitSelectionScope( &(rr->rrcTraceOldSelectionScope) );
148     docInitEditRange( &(rr->rrcTraceOldRange) );
149     rr->rrcTraceOldCol0= -1;
150     rr->rrcTraceOldCol1= -1;
151     rr->rrcTraceOldPage= -1;
152     rr->rrcTraceOldColumn= -1;
153 
154     docInitSelectionScope( &(rr->rrcTraceNewSelectionScope) );
155     docInitEditRange( &(rr->rrcTraceNewRange) );
156     rr->rrcTraceNewCol0= -1;
157     rr->rrcTraceNewCol1= -1;
158     rr->rrcTraceNewPage= -1;
159     rr->rrcTraceNewColumn= -1;
160 
161     docInitNoteProperties( &(rr->rrcNoteProperties) );
162     utilPropMaskClear( &(rr->rrcNotePropertyMask) );
163     }
164 
docRtfCleanReader(RtfReader * rr)165 static void docRtfCleanReader(	RtfReader *	rr )
166     {
167     while( rr->rrcState )
168 	{
169 	XDEB(rr->rrcState);
170 	docRtfPopReadingState( rr );
171 	}
172 
173     /*docCleanCellProperties( &(rr->rrcRowProperties) );*/
174     docCleanSectionProperties( &(rr->rrcSectionProperties) );
175     docCleanRowProperties( &(rr->rrcRowProperties) );
176     /*docCleanShapeProperties( &(rr->rrcShapeProperties) );*/
177 
178     if  ( rr->rrDrawingShape )
179 	{ docDeleteDrawingShape( rr->rrDocument, rr->rrDrawingShape ); }
180 
181     docCleanDocumentFont( &(rr->rrcCurrentFont) );
182     utilCleanPagedList( &(rr->rrcEncodedFontList) );
183 
184     docCleanDocumentList( &(rr->rrcDocumentList) );
185     docCleanListOverride( &(rr->rrcListOverride) );
186     docCleanDocumentListLevel( &(rr->rrcDocumentListLevel) );
187     docCleanListOverrideLevel( &(rr->rrcListOverrideLevel) );
188     utilCleanMemoryBuffer( &(rr->rrcListLevelText) );
189     utilCleanMemoryBuffer( &(rr->rrcListLevelNumbers) );
190 
191     docCleanDocumentStyle( &(rr->rrcStyle) );
192     docCleanDocumentProperties( &(rr->rrcDocumentProperties) );
193 
194     if  ( rr->rrcParagraphNumbers )
195 	{ free( rr->rrcParagraphNumbers );	}
196 
197     docCleanParagraphNumber( &(rr->rrcParagraphNumber) );
198 
199     utilCleanMemoryBuffer( &(rr->rrcBookmark) );
200 
201     docCleanNoteProperties( &(rr->rrcNoteProperties) );
202 
203     if  ( rr->rrRtfTextConverter )
204 	{
205 	textCleanTextConverter( rr->rrRtfTextConverter );
206 	free( rr->rrRtfTextConverter );
207 	}
208 
209     if  ( rr->rrTextTextConverter )
210 	{
211 	textCleanTextConverter( rr->rrTextTextConverter );
212 	free( rr->rrTextTextConverter );
213 	}
214     }
215 
docRtfPushReadingState(RtfReader * rr,RtfReadingState * rrs)216 void docRtfPushReadingState(	RtfReader *		rr,
217 				RtfReadingState *	rrs )
218     {
219     BufferDocument *		bd= rr->rrDocument;
220 
221     docPlainTextAttribute( &(rrs->rrsTextAttribute), bd );
222     rrs->rrsTextCharset= -1;
223     docInitItemShading( &(rrs->rrsTextShading) );
224     rrs->rrsTextShadingChanged= 0;
225 
226     docInitParagraphProperties( &(rrs->rrsParagraphProperties) );
227     docInitTabStopList( &(rrs->rrsTabStopList) );
228     docInitItemShading( &(rrs->rrsParagraphShading) );
229     docInitFrameProperties( &(rrs->rrsParaFrameProperties) );
230 
231     utilInitMemoryBuffer( &(rrs->rrsSavedText) );
232 
233     rrs->rrsBytesPerUnicode= 1;
234     rrs->rrsUnicodeBytesToSkip= 0;
235 
236     if  ( rr->rrcState )
237 	{
238 	const RtfReadingState *	above= rr->rrcState;
239 
240 	rrs->rrsBytesPerUnicode= above->rrsBytesPerUnicode;
241 	rrs->rrsUnicodeBytesToSkip= 0;
242 
243 	rrs->rrsTextAttribute= above->rrsTextAttribute;
244 	rrs->rrsTextShading= above->rrsTextShading;
245 	rrs->rrsTextShadingChanged= above->rrsTextShadingChanged;
246 
247 	docCopyParagraphProperties( &(rrs->rrsParagraphProperties),
248 				    &(above->rrsParagraphProperties) );
249 	docCopyTabStopList( &(rrs->rrsTabStopList), &(above->rrsTabStopList) );
250 	rrs->rrsParagraphShading= above->rrsParagraphShading;
251 	}
252 
253     rrs->rrsPrev= rr->rrcState;
254     rr->rrcState= rrs;
255 
256     return;
257     }
258 
docRtfPopReadingState(RtfReader * rr)259 void docRtfPopReadingState(	RtfReader *	rr )
260     {
261     RtfReadingState *	rrs= rr->rrcState;
262 
263     if  ( ! rrs )
264 	{ XDEB(rrs); return;	}
265 
266     docCleanParagraphProperties( &(rrs->rrsParagraphProperties) );
267     docCleanTabStopList( &(rrs->rrsTabStopList) );
268 
269     /*  Copy unused data in child to parent */
270     if  ( rrs->rrsPrev )
271 	{
272 	if  ( utilMemoryAppendBuffer( &(rrs->rrsPrev->rrsSavedText),
273 						    &(rrs->rrsSavedText) ) )
274 	    { LDEB(rrs->rrsSavedText.mbSize);	}
275 	}
276 
277     utilCleanMemoryBuffer( &(rrs->rrsSavedText) );
278 
279     /**/
280     rr->rrcState= rrs->rrsPrev;
281     if  ( rr->rrcState )
282 	{ rr->rrcState->rrsUnicodeBytesToSkip= 0;	}
283 
284     return;
285     }
286 
287 /************************************************************************/
288 /*									*/
289 /*  Open a reader.							*/
290 /*									*/
291 /************************************************************************/
292 
docRtfOpenReader(SimpleInputStream * sis,BufferDocument * bd,int flags)293 RtfReader * docRtfOpenReader(		SimpleInputStream *	sis,
294 					BufferDocument *	bd,
295 					int			flags )
296     {
297     RtfReader *		rval= (RtfReader *)0;
298     RtfReader *		rr= (RtfReader *)0;
299 
300     rr= (RtfReader *)malloc(sizeof(RtfReader) );
301     if  ( ! rr )
302 	{ PDEB(rr); goto ready;	}
303 
304     docRtfInitReader( rr );
305 
306     rr->rrRtfTextConverter= malloc( sizeof(TextConverter) );
307     if  ( ! rr->rrRtfTextConverter )
308 	{ PDEB(rr->rrRtfTextConverter); goto ready;	}
309     textInitTextConverter( rr->rrRtfTextConverter );
310 
311     rr->rrTextTextConverter= malloc( sizeof(TextConverter) );
312     if  ( ! rr->rrTextTextConverter )
313 	{ PDEB(rr->rrTextTextConverter); goto ready;	}
314     textInitTextConverter( rr->rrTextTextConverter );
315 
316     rr->rrDocument= bd;
317     rr->rrcSis= sis;
318     rr->rrReadFlags= flags;
319 
320     docRtfReadSetupTextConverters( rr );
321 
322     rval= rr; rr= (RtfReader *)0; /* steal */
323 
324   ready:
325 
326     if  ( rr )
327 	{ docRtfCloseReader( rr );	}
328 
329     return rval;
330     }
331 
332 /************************************************************************/
333 /*									*/
334 /*  Close a reader.							*/
335 /*									*/
336 /************************************************************************/
337 
docRtfCloseReader(RtfReader * rr)338 void docRtfCloseReader(		RtfReader *		rr )
339     {
340     docRtfCleanReader( rr );
341     free( rr );
342 
343     return;
344     }
345