File: stylesheet.c

package info (click to toggle)
abiword 0.7.7-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 20,604 kB
  • ctags: 18,358
  • sloc: cpp: 88,791; ansic: 66,296; sh: 7,777; makefile: 3,397; xml: 687; perl: 361; awk: 273; sed: 36; csh: 28
file content (436 lines) | stat: -rw-r--r-- 10,086 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
#include <stdio.h>
#include <stdlib.h>
#include "wv.h"
#include <string.h>

/*modify this to handle cbSTSHI < the current size*/
void wvGetSTSHI(STSHI *item,U16 cbSTSHI,FILE *fd)
	{
	U16 temp16;
	int i;
	U16 count=0;


	wvInitSTSHI(item);	/* zero any new fields that might not exist in the file*/

    item->cstd = read_16ubit(fd);
	count+=2;
	wvTrace(("there are %d std\n",item->cstd));
    item->cbSTDBaseInFile = read_16ubit(fd);
	count+=2;
	temp16 = read_16ubit(fd);
	count+=2;
    item->fStdStylenamesWritten = temp16 & 0x01;
    item->reserved = (temp16 & 0xfe) >> 1;
    item->stiMaxWhenSaved = read_16ubit(fd);
	count+=2;
    item->istdMaxFixedWhenSaved = read_16ubit(fd);
	count+=2;
    item->nVerBuiltInNamesWhenSaved = read_16ubit(fd);
	count+=2;
	for (i=0;i<3;i++)
		{
    	item->rgftcStandardChpStsh[i] = read_16ubit(fd);
		count+=2;
		if (count >= cbSTSHI) break;
		}

	while(count<cbSTSHI)
		{
		getc(fd);
		count++;
		}
	}

void wvInitSTSHI(STSHI *item)
	{
	int i;

    item->cstd = 0;
    item->cbSTDBaseInFile = 0;
    item->fStdStylenamesWritten = 0;
    item->reserved = 0;
    item->stiMaxWhenSaved = 0;
    item->istdMaxFixedWhenSaved = 0;
    item->nVerBuiltInNamesWhenSaved = 0;
	for (i=0;i<3;i++)
    	item->rgftcStandardChpStsh[i] = 0;
	}

void wvReleaseSTD(STD *item)
	{
	U8 i;
	if (!item)
		return;
	wvFree(item->xstzName);

	for (i=0;i<item->cupx;i++)
		{
		if (item->grupxf[i].cbUPX == 0)
			continue;

		if ((item->cupx == 1) || ((item->cupx == 2) && (i==1)))
			wvFree(item->grupxf[i].upx.chpx.grpprl);
		else if ((item->cupx == 2) && (i==0))
			wvFree(item->grupxf[i].upx.papx.grpprl);
		}

	if (item->sgc == sgcChp)
		if (item->grupe)
			wvReleaseCHPX(&(item->grupe[0].chpx));
	wvFree(item->grupxf);
	wvFree(item->grupe);
	}

void wvInitSTD(STD *item)
	{
	item->sti=0;
	item->fScratch=0;
	item->fInvalHeight=0;
	item->fHasUpe=0;
	item->fMassCopy=0;
	item->sgc=0;
	item->istdBase=istdNil;
	item->cupx=0;
	item->istdNext=0;
	item->bchUpe=0;
	item->fAutoRedef=0;
	item->fHidden=0;
	item->reserved=0;
	item->xstzName=NULL;
	item->grupxf=NULL;
	item->grupe=NULL;
	}

int wvGetSTD(STD *item,U16 baselen,U16 fixedlen,FILE *fd)
	{
	U16 temp16;
	U16 len,i,j;
	int pos;
	int ret=0;
	U16 count=0;

	wvInitSTD(item); /* zero any new fields that might not exist in the file*/

	wvTrace(("baselen set to %d fixed part len is %d\n",baselen,fixedlen));

	temp16 = read_16ubit(fd);
	count+=2;
	item->sti = temp16 & 0x0fff;
    item->fScratch = (temp16 & 0x1000) >> 12;
    item->fInvalHeight = (temp16 & 0x2000) >> 13;
    item->fHasUpe = (temp16 & 0x4000) >> 14;
    item->fMassCopy = (temp16 & 0x8000) >> 15;
	temp16 = read_16ubit(fd);
	count+=2;
    item->sgc = temp16 & 0x000f;
    item->istdBase = (temp16 & 0xfff0) >> 4;
	temp16 = read_16ubit(fd);
	count+=2;
    item->cupx = temp16 & 0x000f;
    item->istdNext = (temp16 & 0xfff0) >> 4;
    item->bchUpe = read_16ubit(fd);
	count+=2;
	if (count < baselen)		/* word 6 has only a count of 8 */
		{
		temp16 = read_16ubit(fd);
		count+=2;
		item->fAutoRedef = temp16 & 0x0001;
		item->fHidden = (temp16 & 0x0002) >> 1;
		item->reserved = (temp16 & 0xfffc) >> 2;

		while (count<baselen)	/* eat any new fields we might know about ourselves*/
			{
			getc(fd);
			count++;
			}
		}
	wvTrace(("count is %d, baselen is %d\n",count,baselen));


	pos=10;


	if (count < 10)
		{
		ret=1;
		len = getc(fd);
		pos++;
		}
	else
		{
		len = read_16ubit(fd);
		pos+=2;
		wvTrace(("%x %x %x %x\n",fixedlen,baselen,fixedlen-baselen,len));
		if (fixedlen-baselen < len)
			{
			wvWarning("The names of the styles are not stored in unicode as is usual for this version, going to 8 bit\n");
			fseek(fd,-2,SEEK_CUR);
			len = getc(fd);
			count=9;	/* to fake the later char reader code */
			pos--;
			}
		}


	wvTrace(("doing a std, str len is %d\n",len+1));
	item->xstzName = (U16 *)malloc((len+1) * sizeof(XCHAR));

	for(i=0;i<len+1;i++)
		{
		if (count < 10)
			{
			item->xstzName[i] = getc(fd);
			pos++;
			}
		else
			{
			item->xstzName[i] = read_16ubit(fd);
			pos+=2;
			}

		wvTrace(("sample letter is %c\n",item->xstzName[i]));
		}
	wvTrace(("string ended\n"));


	wvTrace(("cupx is %d\n",item->cupx));
	if (item->cupx == 0)
		{
		item->grupxf = NULL;
		item->grupe = NULL;
		return(0);
		}

	item->grupxf = (UPXF *)malloc(sizeof(UPXF) * item->cupx);
	if (item->grupxf == NULL)
		{
		wvError(("Couuldn't alloc %d bytes for UPXF\n",sizeof(UPXF) * item->cupx));
		return(0);
		}

	item->grupe = (UPE *)malloc(sizeof(UPE) * item->cupx);
	if (item->grupe == NULL)
		{
		wvError(("Couuldn't alloc %d bytes for UPE\n",sizeof(UPE) * item->cupx));
		return(0);
		}

	for (i=0;i<item->cupx;i++)
		{
		if ((pos+1)/2 != pos/2)
			{
			/*eat odd bytes*/
			fseek(fd,1,SEEK_CUR);
			pos++;
			}
		
		item->grupxf[i].cbUPX = read_16ubit(fd);
		wvTrace(("cbUPX is %d\n",item->grupxf[i].cbUPX));
		pos+=2;

		if (item->grupxf[i].cbUPX == 0)
			continue;

		if ((item->cupx == 1) || ((item->cupx == 2) && (i==1)))
			{
			item->grupxf[i].upx.chpx.grpprl = (U8 *)malloc(item->grupxf[i].cbUPX);
			for(j=0;j<item->grupxf[i].cbUPX;j++)
				{
				item->grupxf[i].upx.chpx.grpprl[j] = getc(fd);
				pos++;
				}
			}
		else if ((item->cupx == 2) && (i==0))
			{
			item->grupxf[i].upx.papx.istd = read_16ubit(fd);
			pos+=2;
			if (item->grupxf[i].cbUPX-2)
				item->grupxf[i].upx.papx.grpprl = (U8 *)malloc(item->grupxf[i].cbUPX-2);
			else
				item->grupxf[i].upx.papx.grpprl = NULL;
			for(j=0;j<item->grupxf[i].cbUPX-2;j++)
				{
				item->grupxf[i].upx.papx.grpprl[j] = getc(fd);
				pos++;
				}
			}
		else 
			{
			wvTrace(("Strange cupx option\n"));
			fseek(fd,item->grupxf[i].cbUPX,SEEK_CUR);
			pos+=item->grupxf[i].cbUPX;
			}
		}



	/*eat odd bytes*/
	if ((pos+1)/2 != pos/2)
		fseek(fd,1,SEEK_CUR);
	return(ret);
	}

void wvReleaseSTSH(STSH *item)
	{
	int i;
	for (i=0;i<item->Stshi.cstd;i++)
		{
		wvTrace(("Releaseing %d std\n",i));
		wvReleaseSTD(&(item->std[i]));
		}
	wvFree(item->std);
	}

void wvGetSTSH(STSH *item,U32 offset,U32 len,FILE *fd)
	{
	U16 cbStshi,cbStd,i,word6,j;
	U16 *chains1;
	U16 *chains2;
	if (len == 0)
		{
		item->Stshi.cstd=0;
		item->std=NULL;
		return;
		}
	wvTrace(("stsh offset len is %x %d\n",offset,len));
	fseek(fd,offset,SEEK_SET);
	cbStshi = read_16ubit(fd);
	wvGetSTSHI(&(item->Stshi),cbStshi,fd);
	
	if (item->Stshi.cstd == 0)
		{
		item->std=NULL;
		return;
		}
	chains1 = (U16 *)malloc(sizeof(U16) * item->Stshi.cstd);
	chains2 = (U16 *)malloc(sizeof(U16) * item->Stshi.cstd);

	item->std = (STD *)malloc(sizeof(STD) * item->Stshi.cstd);
	if (item->std == NULL)
		{
		wvError(("No mem for STD list, of size %d\n",sizeof(STD) * item->Stshi.cstd));
		return;
		}

	for(i=0;i<item->Stshi.cstd;i++)
		wvInitSTD(&(item->std[i]));
		
	for(i=0;i<item->Stshi.cstd;i++)
		{
		cbStd = read_16ubit(fd);
		wvTrace(("index is %d,cbStd is %d, should end on %x\n",i,cbStd,ftell(fd)+cbStd));
		if (cbStd != 0)
			{
			word6 = wvGetSTD(&(item->std[i]),item->Stshi.cbSTDBaseInFile,cbStd,fd);
			wvTrace(("istdBase is %d, type is %d, 6|8 version is %d\n",item->std[i].istdBase,item->std[i].sgc,word6));
			}
		wvTrace(("actually ended on %x\n",ftell(fd)));
		chains1[i] = item->std[i].istdBase;
		}

	
	/* 
	we will do number 10, (standard character style) first if possible, 
	some evil word docs attempt illegally to use sprmCBold etc with a 
	128 and 129 argument, which is supposedly not allowed, but happens
	anyway. In all examples so far it has been character style no 10 that
	they have attempted to access
	*/
	if (item->std[10].istdBase == istdNil)
		{
		wvTrace(("Generating istd no %d\n",i));
		wvGenerateStyle(item,10,word6);
		}
	for(i=0;i<item->Stshi.cstd;i++)
		{
		if ( (item->std[i].istdBase == istdNil) && (i!=10) )
			{
			wvTrace(("Generating istd no %d\n",i));
			wvGenerateStyle(item,i,word6);
			}
		wvTrace(("1: No %d,Base is %d\n",i,chains1[i]));
		}

	j=0;
	while(j<11)
		{
		int finished=1;
		for(i=0;i<item->Stshi.cstd;i++)
			{
			if ( (chains1[i] != istdNil) && (chains1[chains1[i]] == istdNil))
				{
				chains2[i] = istdNil;
				wvTrace(("Generating istd no %d\n",i));
				wvGenerateStyle(item,i,word6);
				finished=0;
				}
			else
				chains2[i] = chains1[i];
			wvTrace(("%d: No %d, Base is %d\n",j,i,chains2[i]));
			}
		for(i=0;i<item->Stshi.cstd;i++)
			chains1[i] = chains2[i];
		if (finished) break;
		j++;
		}

	wvFree(chains1);
	wvFree(chains2);
	}


void wvGenerateStyle(STSH *item,U16 i,U16 word6)
	{
	if (item->std[i].cupx == 0)
		{
		wvTrace(("Empty Slot %d\n",i));
		return;
		}

	switch (item->std[i].sgc)
		{
		case sgcPara:
			wvTrace(("doing paragraph, len is %d\n",item->std[i].grupxf[0].cbUPX));
			wvTrace(("doing paragraph, len is %d\n",item->std[i].grupxf[1].cbUPX));
			wvInitPAPFromIstd(&(item->std[i].grupe[0].apap),(U16)item->std[i].istdBase,item);
			if (word6)
				wvAddPAPXFromBucket6(&(item->std[i].grupe[0].apap),&(item->std[i].grupxf[0]),item);
			else
				wvAddPAPXFromBucket(&(item->std[i].grupe[0].apap),&(item->std[i].grupxf[0]),item,NULL);
				/* data is NULL because HugePAPX cannot occur in this circumstance, according to the
				docs */

			wvInitCHPFromIstd(&(item->std[i].grupe[1].achp),(U16)item->std[i].istdBase,item);

			wvTrace(("here1\n"));
			
			if (word6)
				wvAddCHPXFromBucket6(&(item->std[i].grupe[1].achp),&(item->std[i].grupxf[1]),item);
			else
				wvAddCHPXFromBucket(&(item->std[i].grupe[1].achp),&(item->std[i].grupxf[1]),item);

			wvTrace(("here2\n"));

			if (item->std[i].grupe[1].achp.istd != istdNormalChar)
				{
				wvWarning("chp should have had istd set to istdNormalChar, doing it manually\n");
				item->std[i].grupe[1].achp.istd = istdNormalChar;
				}

			break;
		case sgcChp:
			wvInitCHPXFromIstd(&(item->std[i].grupe[0].chpx),(U16)item->std[i].istdBase,item);

			if (word6)
				wvUpdateCHPXBucket(&(item->std[i].grupxf[0]));
				
			wvMergeCHPXFromBucket(&(item->std[i].grupe[0].chpx),&(item->std[i].grupxf[0]));
			/* UPE.chpx.istd is set to the style's istd */
			item->std[i].grupe[0].chpx.istd = i;	 /*?*/
			break;
		default:
			wvWarning("New document type\n");
			break;
		}
	}