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	<string.h>
11 #   include	<stdio.h>
12 #   include	<ctype.h>
13 
14 #   include	<appDebugon.h>
15 
16 #   include	<textOfficeCharset.h>
17 #   include	"docRtfReaderImpl.h"
18 #   include	"docRtfTagEnum.h"
19 
20 /************************************************************************/
21 
docRtfRestartFont(RtfReader * rrc)22 void docRtfRestartFont(		RtfReader *	rrc )
23     {
24     docCleanDocumentFont( &(rrc->rrcCurrentFont) );
25     docInitDocumentFont( &(rrc->rrcCurrentFont) );
26     docRtfInitEncodedFont( &(rrc->rrcCurrentEncodedFont) );
27     if  ( rrc->rrDocument )
28 	{
29 	rrc->rrcCurrentEncodedFont.ecCharset=
30 			    rrc->rrDocument->bdProperties.dpDocumentCharset;
31 	}
32 
33     return ;
34     }
35 
36 /************************************************************************/
37 /*									*/
38 /*  Consume a font table.						*/
39 /*									*/
40 /************************************************************************/
41 
docRtfCommitFaltName(const RtfControlWord * rcw,RtfReader * rrc)42 static int docRtfCommitFaltName(	const RtfControlWord *	rcw,
43 					RtfReader *		rrc )
44     {
45     const int	removeSemicolon= 0;
46     int		size= 0;
47 
48     if  ( docRtfStoreSavedText( &(rrc->rrcCurrentFont.dfAltName), &size,
49 						    rrc, removeSemicolon ) )
50 	{ LDEB(1); return -1; }
51 
52     return 0;
53     }
54 
docRtfCommitFontPanoseText(const RtfControlWord * rcw,RtfReader * rrc)55 static int docRtfCommitFontPanoseText(	const RtfControlWord *	rcw,
56 					RtfReader *	rrc )
57     {
58     RtfReadingState *	rrs= rrc->rrcState;
59 
60     if  ( rrs->rrsSavedText.mbSize != FONTlenPANOSE )
61 	{
62 	LLDEB(rrs->rrsSavedText.mbSize,FONTlenPANOSE);
63 	utilEmptyMemoryBuffer( &(rrs->rrsSavedText) );
64 	return 0;
65 	}
66 
67     memcpy( rrc->rrcCurrentFont.dfPanose,
68 			rrs->rrsSavedText.mbBytes,
69 			rrs->rrsSavedText.mbSize );
70     utilEmptyMemoryBuffer( &(rrs->rrsSavedText) );
71 
72     rrc->rrcCurrentFont.dfPanose[FONTlenPANOSE]= '\0';
73 
74     return 0;
75     }
76 
docRtfAddCurrentFontToList(RtfReader * rrc)77 static int docRtfAddCurrentFontToList(	RtfReader *	rrc )
78     {
79     int		rval= 0;
80     const int	removeSemicolon= 1;
81     int		size= 0;
82 
83     if  ( docRtfStoreSavedText( &(rrc->rrcCurrentFont.dfName), &size,
84 						    rrc, removeSemicolon ) )
85 	{ LDEB(1); rval= -1; goto ready; }
86 
87     if  ( rrc->rrcCurrentFont.dfName && rrc->rrcCurrentFont.dfName[0] == '@' )
88 	{
89 	if  ( docFontSetFamilyName( &(rrc->rrcCurrentFont),
90 					    rrc->rrcCurrentFont.dfName+ 1 ) )
91 	    { LDEB(1); return -1;	}
92 	}
93 
94     if  ( rrc->rrcCurrentFont.dfName )
95 	{
96 	DocumentProperties *	dp= &(rrc->rrDocument->bdProperties);
97 	DocumentFontList *	dfl= dp->dpFontList;
98 	DocumentFont *		df;
99 
100 	EncodedFont *		efIn= &(rrc->rrcCurrentEncodedFont);
101 	EncodedFont *		efOut;
102 
103 	if  ( ! strcmp( rrc->rrcCurrentFont.dfName, "ZapfDingbats" ) )
104 	    {
105 	    if  ( docFontSetFamilyName( &(rrc->rrcCurrentFont),
106 							"ITC Zapf Dingbats" ) )
107 		{ LDEB(1); return -1;	}
108 	    }
109 	if  ( ! strcmp( rrc->rrcCurrentFont.dfName, "ZapfChancery" ) )
110 	    {
111 	    if  ( docFontSetFamilyName( &(rrc->rrcCurrentFont),
112 							"ITC Zapf Chancery" ) )
113 		{ LDEB(1); return -1;	}
114 	    }
115 
116 	utilRemoveCharsetFromFontName( &(rrc->rrcCurrentFont),
117 							    efIn->ecCharset );
118 
119 	efIn->ecBufFontNumber= docGetFontByName( dfl,
120 						rrc->rrcCurrentFont.dfName );
121 	if  ( efIn->ecBufFontNumber < 0 )
122 	    {
123 	    SLDEB(rrc->rrcCurrentFont.dfName,efIn->ecBufFontNumber);
124 	    rval= -1; goto ready;
125 	    }
126 
127 	df= docFontListGetFontByNumber( dfl, efIn->ecBufFontNumber );
128 	if  ( ! df )
129 	    { SXDEB(rrc->rrcCurrentFont.dfName,df); rval= -1; goto ready; }
130 
131 	if  ( docCopyDocumentFont( df, &(rrc->rrcCurrentFont) ) )
132 	    { LDEB(1); rval= -1; goto ready;	}
133 	df->dfDocFontNumber= efIn->ecBufFontNumber;
134 
135 	efOut= (EncodedFont *)utilPagedListClaimItem(
136 			&(rrc->rrcEncodedFontList), efIn->ecFileFontNumber );
137 	if  ( ! efOut )
138 	    { LXDEB(efIn->ecFileFontNumber,efOut); rval= -1; goto ready; }
139 	*efOut= *efIn;
140 	}
141 
142   ready:
143 
144     docRtfRestartFont( rrc );
145 
146     return rval;
147     }
148 
docRtfFontName(RtfReader * rrc,const char * name,int len)149 static int docRtfFontName(	RtfReader *		rrc,
150 				const char *		name,
151 				int			len )
152     {
153     if  ( docRtfSaveDocEncodedText( rrc, name, len ) )
154 	{ LDEB(len); return -1;	}
155 
156     if  ( rrc->rrcCurrentEncodedFont.ecFileFontNumber < 0 )
157 	{ LDEB(rrc->rrcCurrentEncodedFont.ecFileFontNumber); return 0;	}
158 
159     if  ( docRtfAddCurrentFontToList( rrc ) )
160 	{ LDEB(len); return -1;	}
161 
162     return 0;
163     }
164 
docRtfCommitCurrentFont(const RtfControlWord * rcw,RtfReader * rrc)165 static int docRtfCommitCurrentFont(	const RtfControlWord *	rcw,
166 					RtfReader *	rrc )
167     { return docRtfAddCurrentFontToList( rrc );	}
168 
docRtfFontProperty(const RtfControlWord * rcw,int arg,RtfReader * rrc)169 int docRtfFontProperty(		const RtfControlWord *	rcw,
170 				int			arg,
171 				RtfReader *		rrc )
172     {
173     switch( rcw->rcwID )
174 	{
175 	case DFpropFAMILY_STYLE:
176 	    rrc->rrcCurrentFont.dfStyleInt= arg;
177 	    break;
178 	case DFpropCHARSET:
179 	    rrc->rrcCurrentEncodedFont.ecCharset= arg;
180 	    break;
181 	case DFpropCODEPAGE:
182 	    rrc->rrcCurrentEncodedFont.ecCodepage= arg;
183 	    break;
184 	case DFpropPITCH:
185 	    rrc->rrcCurrentFont.dfPitch= arg;
186 	    break;
187 
188 	default:
189 	    SLDEB(rcw->rcwWord,arg);
190 	}
191 
192     return 0;
193     }
194 
195 static RtfControlWord	docRtfFontGroupGroups[]=
196     {
197     RTF_TEXT_GROUP( "panose",	DFpropPANOSE,	docRtfCommitFontPanoseText ),
198     RTF_TEXT_GROUP( "falt",	DFpropALT_NAME,	docRtfCommitFaltName ),
199 
200     { (char *)0, 0, 0 }
201     };
202 
docRtfFontGroup(const RtfControlWord * rcw,int arg,RtfReader * rrc)203 static int docRtfFontGroup(	const RtfControlWord *	rcw,
204 				int			arg,
205 				RtfReader *		rrc )
206     {
207     docRtfRestartFont( rrc );
208 
209     rrc->rrcCurrentEncodedFont.ecFileFontNumber= arg;
210 
211     if  ( docRtfReadGroup( rcw, 0, 0, rrc,
212 					    docRtfFontGroupGroups,
213 					    docRtfSaveDocEncodedText,
214 					    docRtfCommitCurrentFont ) )
215 	{ SLDEB(rcw->rcwWord,arg); return -1;	}
216 
217     return 0;
218     }
219 
docRtfThemeFontGroup(const RtfControlWord * rcw,int arg,RtfReader * rrc)220 static int docRtfThemeFontGroup(	const RtfControlWord *	rcw,
221 					int			arg,
222 					RtfReader *	rrc )
223     {
224     docRtfRestartFont( rrc );
225 
226     if  ( docRtfReadGroup( rcw, 0, 0, rrc,
227 					    docRtfFontGroupGroups,
228 					    docRtfSaveDocEncodedText,
229 					    docRtfCommitCurrentFont ) )
230 	{ SLDEB(rcw->rcwWord,arg); return -1;	}
231 
232     return 0;
233     }
234 
235 static RtfControlWord	docRtfFontTableGroups[]=
236     {
237 	RTF_DEST_XX( "f",	RTFidF,	docRtfFontGroup ),
238 	RTF_DEST_XX( "flomajor",RTFidF,	docRtfThemeFontGroup ),
239 	RTF_DEST_XX( "fhimajor",RTFidF,	docRtfThemeFontGroup ),
240 	RTF_DEST_XX( "fdbmajor",RTFidF,	docRtfThemeFontGroup ),
241 	RTF_DEST_XX( "fbimajor",RTFidF,	docRtfThemeFontGroup ),
242 	RTF_DEST_XX( "flominor",RTFidF,	docRtfThemeFontGroup ),
243 	RTF_DEST_XX( "fhiminor",RTFidF,	docRtfThemeFontGroup ),
244 	RTF_DEST_XX( "fdbminor",RTFidF,	docRtfThemeFontGroup ),
245 	RTF_DEST_XX( "fbiminor",RTFidF,	docRtfThemeFontGroup ),
246 
247 	{ (char *)0, 0, 0 }
248     };
249 
docRtfCommitFontTable(const RtfControlWord * rcw,RtfReader * rrc)250 static int docRtfCommitFontTable(	const RtfControlWord *	rcw,
251 					RtfReader *		rrc )
252     {
253     BufferDocument *		bd= rrc->rrDocument;
254     DocumentProperties *	dp= &(bd->bdProperties);
255 
256     int				charset;
257 
258     if  ( rrc->rrcDefaultFont >= 0 )
259 	{
260 	if  ( docRtfReadMapFont( rrc, &(dp->dpDefaultFont), &charset,
261 					    rrc->rrcDefaultFont ) < 0 )
262 	    { LDEB(rrc->rrcDefaultFont);	}
263 	}
264 
265     if  ( rrc->rrcDefaultFontDbch >= 0 )
266 	{
267 	if  ( docRtfReadMapFont( rrc, &(dp->dpDefaultFontDbch), &charset,
268 					    rrc->rrcDefaultFontDbch ) < 0 )
269 	    { LDEB(rrc->rrcDefaultFontDbch);	}
270 	}
271 
272     if  ( rrc->rrcDefaultFontLoch >= 0 )
273 	{
274 	if  ( docRtfReadMapFont( rrc, &(dp->dpDefaultFontLoch), &charset,
275 					    rrc->rrcDefaultFontLoch ) < 0 )
276 	    { LDEB(rrc->rrcDefaultFontLoch);	}
277 	}
278 
279     if  ( rrc->rrcDefaultFontHich >= 0 )
280 	{
281 	if  ( docRtfReadMapFont( rrc, &(dp->dpDefaultFontHich), &charset,
282 					    rrc->rrcDefaultFontHich ) < 0 )
283 	    { LDEB(rrc->rrcDefaultFontHich);	}
284 	}
285 
286     if  ( rrc->rrcDefaultFontBi >= 0 )
287 	{
288 	if  ( docRtfReadMapFont( rrc, &(dp->dpDefaultFontBi), &charset,
289 						rrc->rrcDefaultFontBi ) < 0 )
290 	    { LDEB(rrc->rrcDefaultFontBi);	}
291 	}
292 
293     return 0;
294     }
295 
docRtfFontTable(const RtfControlWord * rcw,int arg,RtfReader * rrc)296 int docRtfFontTable(	const RtfControlWord *	rcw,
297 			int			arg,
298 			RtfReader *		rrc )
299     {
300     if  ( docRtfReadGroup( rcw, 0, 0, rrc,
301 						docRtfFontTableGroups,
302 						docRtfFontName,
303 						docRtfCommitFontTable ) )
304 	{ SLDEB(rcw->rcwWord,arg); return -1;	}
305 
306     return 0;
307     }
308 
309 /************************************************************************/
310 
docRtfReadMapFont(const RtfReader * rrc,int * pDocFontNum,int * pCharset,int fileFontNum)311 int docRtfReadMapFont(	const RtfReader *		rrc,
312 			int *				pDocFontNum,
313 			int *				pCharset,
314 			int				fileFontNum )
315     {
316     EncodedFont *	ef;
317 
318     if  ( fileFontNum < 0 )
319 	{ LDEB(fileFontNum); return -1;	}
320 
321     ef= (EncodedFont *)utilPagedListGetItemByNumber(
322 				    &(rrc->rrcEncodedFontList), fileFontNum );
323     if  ( ! ef )
324 	{ return 1;	}
325 
326     if  ( ef->ecCodepage >= 0 )
327 	{ LDEB(ef->ecCodepage);	}
328 
329     *pDocFontNum= ef->ecBufFontNumber;
330     *pCharset= ef->ecCharset;
331 
332     return 0;
333     }
334