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	<psDocumentFontStyle.h>
18 #   include	"docRtfWriterImpl.h"
19 #   include	"docRtfTextConverter.h"
20 #   include	"docRtfFlags.h"
21 
22 /************************************************************************/
23 
docRtfFontFamilyStyleTag(int styleInt)24 static const char * docRtfFontFamilyStyleTag(	int styleInt )
25     {
26     switch( styleInt )
27 	{
28 	case DFstyleFNIL:	return "fnil";
29 	case DFstyleFROMAN:	return "froman";
30 	case DFstyleFSWISS:	return "fswiss";
31 	case DFstyleFMODERN:	return "fmodern";
32 	case DFstyleFSCRIPT:	return "fscript";
33 	case DFstyleFDECOR:	return "fdecor";
34 	case DFstyleFTECH:	return "ftech";
35 	case DFstyleFBIDI:	return "fbidi";
36 
37 	default:
38 	    LDEB(styleInt); return "fnil";
39 	}
40     }
41 
42 /************************************************************************/
43 /*									*/
44 /*  Write a font table.							*/
45 /*									*/
46 /************************************************************************/
47 
docRtfWriteEncodedFont(RtfWriter * rw,const DocumentFont * df,const char * suffix,int fontnum,int charset)48 static void docRtfWriteEncodedFont(	RtfWriter *		rw,
49 					const DocumentFont *	df,
50 					const char *		suffix,
51 					int			fontnum,
52 					int			charset )
53     {
54     docRtfWriteArgDestinationBegin( rw, "f", fontnum );
55 
56     docRtfWriteTag( rw, docRtfFontFamilyStyleTag( df->dfStyleInt ) );
57 
58     if  ( charset != FONTcharsetDEFAULT )
59 	{ docRtfWriteArgTag( rw, "fcharset", charset ); }
60 
61     if  ( df->dfPitch != FONTpitchDEFAULT )
62 	{ docRtfWriteArgTag( rw, "fprq", df->dfPitch ); }
63 
64     if  ( df->dfPanose[0] )
65 	{
66 	const int	addSemicolon= 0;
67 
68 	docRtfWriteDocEncodedStringDestination( rw, "*\\panose",
69 		    df->dfPanose, strlen( df->dfPanose ), addSemicolon );
70 	}
71 
72     if  ( df->dfName )
73 	{
74 	const char *	name= df->dfName;
75 
76 	if  ( ! strcmp( name, "ITC Zapf Dingbats" ) )
77 	    { name= "ZapfDingbats";	}
78 	if  ( ! strcmp( name, "ITC Zapf Chancery" ) )
79 	    { name= "ZapfChancery";	}
80 
81 	docRtfWriteDocEncodedString( rw, name, strlen( name ) );
82 
83 	if  ( suffix )
84 	    { docRtfWriteDocEncodedString( rw, suffix, strlen( suffix ) ); }
85 	}
86 
87     if  ( df->dfAltName )
88 	{
89 	const int	addSemicolon= 0;
90 
91 	docRtfWriteDocEncodedStringDestination( rw, "*\\falt",
92 		    df->dfAltName, strlen( df->dfAltName ), addSemicolon );
93 	}
94 
95     docRtfWriteSemicolon( rw );
96 
97     docRtfWriteDestinationEnd( rw );
98 
99     return;
100     }
101 
dowRtfWriteGetEncodedFont(RtfWriter * rw,const DocumentFont * df,int csIdx)102 static const EncodedFont * dowRtfWriteGetEncodedFont(
103 					RtfWriter *		rw,
104 					const DocumentFont *	df,
105 					int			csIdx )
106     {
107     const EncodedFont *		ef;
108     int				key;
109 
110     key= CHARSETidx_COUNT* df->dfDocFontNumber+ csIdx;
111     ef= (const EncodedFont *)utilPagedListGetItemByNumber(
112 					&(rw->rwcEncodedFontList), key );
113     if  ( ! ef )
114 	{ SLLLXDEB(df->dfName,df->dfDocFontNumber,csIdx,key,ef);	}
115 
116     return ef;
117     }
118 
docRtfWriteGetCharset(RtfWriter * rw,int * pCharset,const DocumentFont * df,int symbol)119 int docRtfWriteGetCharset(	RtfWriter *		rw,
120 				int *			pCharset,
121 				const DocumentFont *	df,
122 				int			symbol )
123     {
124     int			csIdx;
125     const EncodedFont *	ef;
126     int			charset= FONTcharsetDEFAULT;
127     int			fontNumber= -1;
128 
129     csIdx= utilIndexMappingGet( &(df->dfUnicodeToCharset), symbol );
130     if  ( csIdx < 0 )
131 	{ csIdx= df->dfCharsetIdxUsed[0];	}
132 
133     ef= dowRtfWriteGetEncodedFont( rw, df, csIdx );
134     if  ( ef )
135 	{
136 	charset= ef->ecCharset;
137 	fontNumber= ef->ecFileFontNumber;
138 	}
139     else{ LXXDEB(csIdx,ef,symbol);		}
140 
141     *pCharset= charset;
142     return fontNumber;
143     }
144 
docRtfWriteGetDefaultFont(RtfWriter * rw,int deff)145 int docRtfWriteGetDefaultFont(	RtfWriter *			rw,
146 				int				deff )
147     {
148     const BufferDocument *	bd= rw->rwDocument;
149     const DocumentProperties *	dp= &(bd->bdProperties);
150     const DocumentFontList *	dfl= dp->dpFontList;
151 
152     const int			cs= 0;
153 
154     const DocumentFont *	df;
155     const EncodedFont *		ef;
156 
157     if  ( rw->rwSaveFlags & RTFflagUNENCODED )
158 	{ return deff;	}
159 
160     if  ( deff < 0 || deff >= dfl->dflFontCount )
161 	{ return -1;	}
162 
163     df= docFontListGetFontByNumber( dfl, deff );
164     if  ( ! df || ! df->dfName || df->dfCharsetIdxUsedCount == 0 )
165 	{ return -1;	}
166 
167     ef= dowRtfWriteGetEncodedFont( rw, df, df->dfCharsetIdxUsed[cs] );
168     if  ( ! ef )
169 	{ LXDEB(cs,ef); return -1;	}
170 
171     return ef->ecFileFontNumber;
172     }
173 
docRtfWriteFontTable(RtfWriter * rw)174 void docRtfWriteFontTable(	RtfWriter *			rw )
175     {
176     const BufferDocument *	bd= rw->rwDocument;
177     const DocumentProperties *	dp= &(bd->bdProperties);
178     const DocumentFontList *	dfl= dp->dpFontList;
179 
180     int				fnt;
181 
182     docRtfWriteDestinationBegin( rw, "fonttbl" );
183     docRtfWriteNextLine( rw );
184 
185     for ( fnt= 0; fnt < dfl->dflFontCount; fnt++ )
186 	{
187 	const DocumentFont *	df= docFontListGetFontByNumber( dfl, fnt );
188 	int			cs;
189 
190 	if  ( ! df || ! df->dfName )
191 	    { continue;	}
192 
193 	for ( cs= 0; cs < df->dfCharsetIdxUsedCount; cs++ )
194 	    {
195 	    const OfficeCharset *	oc;
196 	    const EncodedFont *		ef;
197 	    const char *		suffix= (const char *)0;
198 
199 	    ef= dowRtfWriteGetEncodedFont( rw, df, df->dfCharsetIdxUsed[cs] );
200 	    if  ( ! ef )
201 		{ LXDEB(cs,ef); continue;	}
202 
203 	    oc= utilGetOfficeCharsetByIndex( df->dfCharsetIdxUsed[cs] );
204 	    if  ( ! oc )
205 		{ LXDEB(df->dfCharsetIdxUsed[cs],oc); continue; }
206 
207 	    if  ( oc->ocEmitSuffix )
208 		{
209 		suffix= oc->ocOfficeFontnameSuffix;
210 		}
211 
212 	    docRtfWriteEncodedFont( rw, df, suffix, ef->ecFileFontNumber,
213 							oc->ocOfficeCharset );
214 
215 	    if  ( fnt+ 1 < dfl->dflFontCount		||
216 		  cs+ 1 < df->dfCharsetIdxUsedCount	)
217 		{ docRtfWriteNextLine( rw );	}
218 	    }
219 	}
220 
221     docRtfWriteDestinationEnd( rw );
222     docRtfWriteNextLine( rw );
223 
224     return;
225     }
226 
227 /************************************************************************/
228 
docRtfWriteBuildFontAdmin(RtfWriter * rw)229 int docRtfWriteBuildFontAdmin(	RtfWriter *		rw )
230     {
231     BufferDocument *		bd= rw->rwDocument;
232     DocumentProperties *	dp= &(bd->bdProperties);
233     DocumentFontList *		dfl= dp->dpFontList;
234 
235     int				f;
236     int				fileFontNumber= 0;
237 
238     utilDocFontListClearCharsUsed( dfl );
239 
240     if  ( docGetCharsUsed( bd ) )
241 	{ LDEB(1); return -1;	}
242 
243     for ( f= 0; f < dfl->dflFontCount; f++ )
244 	{
245 	DocumentFont *	df= docFontListGetFontByNumber( dfl, f );
246 
247 	if  ( ! df )
248 	    { continue;	}
249 
250 	if  ( docFontFindLegacyEncodings( df ) )
251 	    { LSDEB(f,df->dfName); return -1;	}
252 
253 	if  ( df->dfCharsetIdxUsedCount > 0 )
254 	    {
255 	    int		cs;
256 
257 	    for ( cs= 0; cs < df->dfCharsetIdxUsedCount; cs++ )
258 		{
259 		EncodedFont		efIn;
260 		EncodedFont *		efOut;
261 		const OfficeCharset *	oc;
262 		int			key;
263 
264 		oc= utilGetOfficeCharsetByIndex( df->dfCharsetIdxUsed[cs] );
265 		if  ( ! oc )
266 		    { XDEB(oc); return -1;	}
267 
268 		docRtfInitEncodedFont( &efIn );
269 
270 		efIn.ecFileFontNumber= fileFontNumber++;
271 		efIn.ecBufFontNumber= df->dfDocFontNumber;
272 		efIn.ecCharset= oc->ocOfficeCharset;
273 		efIn.ecCharsetIdx= df->dfCharsetIdxUsed[cs];
274 
275 		key= CHARSETidx_COUNT* efIn.ecBufFontNumber+
276 						    df->dfCharsetIdxUsed[cs];
277 
278 		efOut= (EncodedFont *)utilPagedListClaimItem(
279 					    &(rw->rwcEncodedFontList), key );
280 		if  ( ! efOut )
281 		    { LXDEB(key,efOut); return -1;	}
282 		*efOut= efIn;
283 		}
284 	    }
285 	}
286 
287     return 0;
288     }
289 
290