File: decode_complex.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 (900 lines) | stat: -rw-r--r-- 28,253 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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
#include <stdlib.h>
#include <stdio.h>
#include "wv.h"

/*
To find the beginning of the paragraph containing a character in a complex
document, it's first necessary to 

1) search for the piece containing the character in the piece table. 

2) Then calculate the FC in the file that stores the character from the piece 
	table information. 
	
3) Using the FC, search the FCs FKP for the largest FC less than the character's 
	FC, call it fcTest. 
	
4) If the character at fcTest-1 is contained in the current piece, then the 
	character corresponding to that FC in the piece is the first character of 
	the paragraph. 
	
5) If that FC is before or marks the beginning of the piece, scan a piece at a 
time towards the beginning of the piece table until a piece is found that 
contains a paragraph mark. 

(This can be done by using the end of the piece FC, finding the largest FC in 
its FKP that is less than or equal to the end of piece FC, and checking to see 
if the character in front of the FKP FC (which must mark a paragraph end) is 
within the piece.)

6) When such an FKP FC is found, the FC marks the first byte of paragraph text.
*/

/*
To find the end of a paragraph for a character in a complex format file,
again 

1) it is necessary to know the piece that contains the character and the
FC assigned to the character. 

2) Using the FC of the character, first search the FKP that describes the 
character to find the smallest FC in the rgfc that is larger than the character 
FC. 

3) If the FC found in the FKP is less than or equal to the limit FC of the 
piece, the end of the paragraph that contains the character is at the FKP FC 
minus 1. 

4) If the FKP FC that was found was greater than the FC of the end of the 
piece, scan piece by piece toward the end of the document until a piece is 
found that contains a paragraph end mark. 

5) It's possible to check if a piece contains a paragraph mark by using the 
FC of the beginning of the piece to search in the FKPs for the smallest FC in 
the FKP rgfc that is greater than the FC of the beginning of the piece. 

If the FC found is less than or equal to the limit FC of the
piece, then the character that ends the paragraph is the character
immediately before the FKP FC.
*/
int wvGetComplexParaBounds(version ver,PAPX_FKP *fkp,U32 *fcFirst, U32 *fcLim, U32 currentfc,CLX *clx, BTE *bte, U32 *pos,int nobte,U32 piece,FILE *fd)
	{
	/*
	U32 currentfc;
	*/
	BTE entry;
	long currentpos;

	if (currentfc==0xffffffffL)
		{
		wvError(("Para Bounds not found !, this is ok if this is the last para, otherwise its a disaster\n"));
		return(-1);
		}

	if (0 != wvGetBTE_FromFC(&entry,currentfc, bte,pos,nobte))
		{
		wvError(("BTE not found !\n"));
		return(-1);
		}
	currentpos = ftell(fd);
	/*The pagenumber of the FKP is entry.pn */

	wvTrace(("the entry.pn is %d\n",entry.pn));
	wvGetPAPX_FKP(ver,fkp,entry.pn,fd);

	wvGetComplexParafcFirst(ver,fcFirst,currentfc,clx, bte, pos,nobte,piece,fkp,fd);

	wvReleasePAPX_FKP(fkp);
	wvTrace(("BREAK\n"));
	wvGetPAPX_FKP(ver,fkp,entry.pn,fd);

	piece = wvGetComplexParafcLim(ver,fcLim,currentfc,clx, bte, pos,nobte,piece,fkp,fd);

	fseek(fd,currentpos,SEEK_SET);
	return(piece);
	}

int wvGetComplexParafcLim(version ver,U32 *fcLim,U32 currentfc,CLX *clx, BTE *bte, U32 *pos,int nobte,U32 piece,PAPX_FKP *fkp,FILE *fd)
	{
	U32 fcTest,beginfc;
	BTE entry;
	*fcLim=0xffffffffL;
	wvTrace(("here is fcLim, currentfc is %x\n",currentfc));
	fcTest = wvSearchNextSmallestFCPAPX_FKP(fkp,currentfc);

	wvTrace(("fcTest is %x, end is %x\n",fcTest,wvGetEndFCPiece(piece,clx)));


	if (fcTest <= wvGetEndFCPiece(piece,clx))
		{
		*fcLim = fcTest;
		}
	else
		{
		/*get end fc of previous piece*/
		piece++;
		while (piece < clx->nopcd) 
			{
			wvTrace(("piece is %d\n",piece));
			beginfc = wvNormFC(clx->pcd[piece].fc,NULL);
			if (0 != wvGetBTE_FromFC(&entry,beginfc, bte,pos,nobte))
				{
				wvError(("BTE not found !\n"));
				return(-1);
				}
			wvReleasePAPX_FKP(fkp);
			wvGetPAPX_FKP(ver,fkp,entry.pn,fd);
			fcTest = wvSearchNextSmallestFCPAPX_FKP(fkp,beginfc);
			wvTrace(("fcTest(t) is %x, end is %x\n",fcTest,wvGetEndFCPiece(piece,clx)));
			if (fcTest <= wvGetEndFCPiece(piece,clx))
				{
				*fcLim = fcTest;
				break;
				}
			piece++;
			}
		}
	wvTrace(("fcLim is %x\n",*fcLim));
	if (piece == clx->nopcd)
		{
		wvTrace(("failed to find a solution to end of paragraph\n"));
		*fcLim = fcTest;
		return(clx->nopcd-1);	/* test using this */
		}
	return(piece);
	}


int wvGetComplexParafcFirst(version ver,U32 *fcFirst,U32 currentfc,CLX *clx, BTE *bte, U32 *pos,int nobte,U32 piece,PAPX_FKP *fkp,FILE *fd)
	{
	U32 fcTest,endfc;
	BTE entry;
	fcTest = wvSearchNextLargestFCPAPX_FKP(fkp,currentfc);

	wvTrace(("fcTest (s) is %x\n",fcTest));

	if (wvQuerySamePiece(fcTest-1,clx,piece))
		{
		wvTrace(("same piece\n"));
		*fcFirst = fcTest-1;
		}
	else
		{
		/*
		get end fc of previous piece ??, or use the end of the current piece
		*/
		piece--;
		while (piece != 0xffffffffL) 
			{
			wvTrace(("piece is %d\n",piece));
			endfc = wvGetEndFCPiece(piece,clx);
			wvTrace(("endfc is %x\n",endfc));
			if (0 != wvGetBTE_FromFC(&entry,endfc, bte,pos,nobte))
				{
				wvError(("BTE not found !\n"));
				return(-1);
				}
			wvReleasePAPX_FKP(fkp);
			wvGetPAPX_FKP(ver,fkp,entry.pn,fd);
			fcTest = wvSearchNextLargestFCPAPX_FKP(fkp,endfc);
			wvTrace(("fcTest(ft) is %x\n",fcTest));
			if (wvQuerySamePiece(fcTest-1,clx,piece))
				{
				*fcFirst = fcTest-1;
				break;
				}
			piece--;
			}
		
		}
	if (piece == 0xffffffffL)
		{
		wvTrace(("failed to find a solution to the beginning of the paragraph\n"));
		*fcFirst = currentfc;
		}
	wvTrace(("fcFirst is finally %x\n",*fcFirst));
	return(0);
	}


/* char properties version of the above -JB */
/* only difference is that we're using CHPX FKP pages,
 * and specifically just the Get and Release functions are
 * different between the two. We might be able to 
 * abstract the necessary functions to avoid duplicating them... */

int wvGetComplexCharBounds(version ver, CHPX_FKP *fkp, U32 *fcFirst, 
			   U32 *fcLim, U32 currentfc,CLX *clx, BTE *bte, 
			   U32 *pos, int nobte, U32 piece, FILE *fd)
	{
	BTE entry;
	long currentpos;

	wvTrace(("current fc is %x\n", currentfc));

	if (currentfc==0xffffffffL)
		{
		wvTrace(("Char Bounds not found !, this is ok if this is the last char, otherwise its a disaster\n"));
		return(-1);
		}

	if (0 != wvGetBTE_FromFC(&entry, currentfc, bte, pos, nobte))
		{
		wvError(("BTE not found !\n"));
		return(-1);
		}
	currentpos = ftell(fd);
	/*The pagenumber of the FKP is entry.pn */

	wvGetCHPX_FKP(ver, fkp, entry.pn, fd);

	wvGetComplexCharfcFirst(ver, fcFirst, currentfc, clx, bte, pos, nobte, piece, fkp, fd);
	wvTrace(("BEFORE PIECE is %d\n",piece));

	wvReleaseCHPX_FKP(fkp);
	wvGetCHPX_FKP(ver,fkp,entry.pn,fd);
	
	piece = wvGetComplexCharfcLim(ver, fcLim, currentfc, clx, bte, pos, nobte, piece, fkp, fd);
	wvTrace(("AFTER PIECE is %d\n",piece));

	fseek(fd,currentpos,SEEK_SET);
	return(piece);
	}

int wvGetComplexCharfcLim(version ver, U32 *fcLim, U32 currentfc, CLX *clx, BTE *bte, U32 *pos, int nobte, U32 piece, CHPX_FKP *fkp, FILE *fd)
	{
	U32 fcTest;
	/*
	BTE entry;
	*/
	*fcLim=0xffffffffL;
	/* this only works with the initial rgfc array, which is the
	 * same for both CHPX and PAPX FKPs */
	fcTest = wvSearchNextSmallestFCPAPX_FKP((PAPX_FKP*)fkp, currentfc);

	wvTrace(("fcTest is %x\n",fcTest));

	/*
	this single line replaces all the rest, is it conceivable that i overengineered,
	careful rereading of the spec makes no mention of repeating the para process to
	find the boundaries of the exception text runs
	*/
	*fcLim = fcTest;
	wvTrace(("fcLim is %x\n",*fcLim));
	if (piece == clx->nopcd)
		return(clx->nopcd-1);	/* test using this */
	return(piece);
	}


int wvGetComplexCharfcFirst(version ver,U32 *fcFirst,U32 currentfc,CLX *clx, BTE *bte, U32 *pos,int nobte,U32 piece,CHPX_FKP *fkp, FILE *fd)
	{
	U32 fcTest/*,endfc*/;
	/*BTE entry;*/
	/* this only works with the initial rgfc array, which is the */
	fcTest = wvSearchNextLargestFCCHPX_FKP(fkp,currentfc);

	wvTrace(("fcTest (s) is %x\n",fcTest));

	/*
	this single line replaces all the rest, is it conceivable that i overengineered,
	careful rereading of the spec makes no mention of repeating the para process to
	find the boundaries of the exception text runs
	*/
	*fcFirst = fcTest;
	return(0);
	}

/*
how this works,
we seek to the beginning of the text, we loop for a count of charaters that is stored in the fib.

the piecetable divides the text up into various sections, we keep track of our location vs
the next entry in that table, when we reach that location, we seek to the position that
the table tells us to go.

there are special cases for coming to the end of a section, and for the beginning and ends of
pages. for the purposes of headers and footers etc.
*/
void wvDecodeComplex(wvParseStruct *ps)
	{
	U32 piececount=0,i,j,spiece=0;
	U32 beginfc,endfc;
	U32 begincp,endcp;
	U8 chartype;
	U16 eachchar;
	U32 para_fcFirst,para_fcLim=0xffffffffL;
	U32 dummy,nextpara_fcLim=0xffffffffL;
	U32 char_fcFirst,char_fcLim=0xffffffffL;
	U32 section_fcFirst,section_fcLim=0xffffffffL;
	U32 comment_cpFirst=0xffffffffL,comment_cpLim=0xffffffffL;
	BTE *btePapx=NULL, *bteChpx=NULL;
	U32 *posPapx=NULL, *posChpx=NULL;
	U32 para_intervals, char_intervals,section_intervals,atrd_intervals;
	int cpiece=0,npiece=0;
	PAPX_FKP para_fkp;
	PAP apap;
	CHPX_FKP char_fkp;
	CHP achp;
	int para_pendingclose=0, comment_pendingclose=0, char_pendingclose=0,section_pendingclose=0;
	int para_dirty=0,char_dirty=0,section_dirty=0;
	SED *sed;
	SEP sep;
	U32 *posSedx;
	ATRD *atrd,*catrd=NULL;
	U32 *posAtrd;
	STTBF grpXstAtnOwners,SttbfAtnbkmk;
	BKF *bkf;
	U32 *posBKF;
	U32 bkf_intervals;
	BKL *bkl;
	U32 *posBKL;
	U32 bkl_intervals;

#if 0	
/* 
this is the versioning name information, the first 22 bytes of each sttbf entry are 
unknown, the rest is a ordinary unicode string, is the time and date and saved by
encoded into the first 22 bytes.
*/ 
	STTBF versioning;
	if (wvQuerySupported(&ps->fib,NULL) == 0) 
		{
		U16 *str;
		wvError(("into the versions\n"));
		wvGetSTTBF(&versioning,ps->fib.fcSttbfUssr,ps->fib.lcbSttbfUssr,ps->tablefd);
		str = UssrStrBegin(&versioning,0);
		wvError(("versioning text is %s\n",wvWideStrToMB(str)));
		}
#endif

	wvGetATRD_PLCF(&atrd,&posAtrd,&atrd_intervals,ps->fib.fcPlcfandRef,ps->fib.lcbPlcfandRef,ps->tablefd);
	wvGetGrpXst(&grpXstAtnOwners,ps->fib.fcGrpXstAtnOwners,ps->fib.lcbGrpXstAtnOwners,ps->tablefd);
	wvTrace(("offset is %x, len is %d\n",ps->fib.fcSttbfAtnbkmk,ps->fib.lcbSttbfAtnbkmk));
	wvGetSTTBF(&SttbfAtnbkmk,ps->fib.fcSttbfAtnbkmk,ps->fib.lcbSttbfAtnbkmk,ps->tablefd);
	wvGetBKF_PLCF(&bkf,&posBKF,&bkf_intervals,ps->fib.fcPlcfAtnbkf,ps->fib.lcbPlcfAtnbkf,ps->tablefd);
	wvGetBKL_PLCF(&bkl,&posBKL,&bkl_intervals,ps->fib.fcPlcfAtnbkl,ps->fib.lcbPlcfAtnbkl,ps->tablefd);

	/*we will need the stylesheet to do anything useful with layout and look*/
	wvGetSTSH(&ps->stsh,ps->fib.fcStshf,ps->fib.lcbStshf,ps->tablefd);

        /* get font list */
	if ( (wvQuerySupported(&ps->fib,NULL) == WORD6) || (wvQuerySupported(&ps->fib,NULL) == WORD7) )
		wvGetFFN_STTBF6(&ps->fonts, ps->fib.fcSttbfffn, ps->fib.lcbSttbfffn, ps->tablefd);
	else
		wvGetFFN_STTBF(&ps->fonts, ps->fib.fcSttbfffn, ps->fib.lcbSttbfffn, ps->tablefd);
	   
	/*we will need the table of names to answer questions like the name of the doc*/
	if ( (wvQuerySupported(&ps->fib,NULL) == WORD6) || (wvQuerySupported(&ps->fib,NULL) == WORD7) )
		{
		wvGetSTTBF6(&ps->anSttbfAssoc,ps->fib.fcSttbfAssoc,ps->fib.lcbSttbfAssoc,ps->tablefd);
		wvGetSTTBF6(&ps->Sttbfbkmk,ps->fib.fcSttbfbkmk,ps->fib.lcbSttbfbkmk,ps->tablefd);
		}
	else
		{
		wvGetSTTBF(&ps->anSttbfAssoc,ps->fib.fcSttbfAssoc,ps->fib.lcbSttbfAssoc,ps->tablefd);
		wvGetSTTBF(&ps->Sttbfbkmk,ps->fib.fcSttbfbkmk,ps->fib.lcbSttbfbkmk,ps->tablefd);
		}

	/*Extract all the list information that we will need to handle lists later on*/
    wvGetLST(&ps->lst,&ps->noofLST,ps->fib.fcPlcfLst,ps->fib.lcbPlcfLst,ps->tablefd);
    wvGetLFO_records(&ps->lfo,&ps->lfolvl,&ps->lvl,&ps->nolfo,&ps->nooflvl,ps->fib.fcPlfLfo,ps->fib.lcbPlfLfo,ps->tablefd);
    /* init the starting list number table */
    if (ps->nolfo)
        {
        ps->liststartnos = (U32 *)malloc(9 * ps->nolfo * sizeof(U32));
        ps->listnfcs = (U8 *)malloc(9 * ps->nolfo);
        ps->finallvl = (LVL *)malloc(9 * ps->nolfo * sizeof(LVL));
        for (i=0;i<9 * ps->nolfo;i++) 
			{
			ps->liststartnos[i] = 0xffffffffL;
			ps->listnfcs[i] = 0xff;
			wvInitLVL(&(ps->finallvl[i]));
			}
        }
    else
        {
        ps->liststartnos=NULL;
        ps->listnfcs=NULL;
        ps->finallvl=NULL;
        }

	 /*Extract Graphic Information*/
	wvGetFSPA_PLCF(&ps->fspa,&ps->fspapos,&ps->nooffspa,ps->fib.fcPlcspaMom,ps->fib.lcbPlcspaMom,ps->tablefd);
	wvGetFDOA_PLCF(&ps->fdoa,&ps->fdoapos,&ps->nooffdoa,ps->fib.fcPlcdoaMom,ps->fib.lcbPlcdoaMom,ps->tablefd);

	wvGetCLX(wvQuerySupported(&ps->fib,NULL),&ps->clx,ps->fib.fcClx,ps->fib.lcbClx,ps->fib.fExtChar,ps->tablefd);

	para_fcFirst = char_fcFirst = section_fcFirst = wvConvertCPToFC(0,&ps->clx);

#ifdef DEBUG
	if ((ps->fib.ccpFtn) || (ps->fib.ccpHdd))
		wvTrace(("Special ending\n"));
#endif

	/*
	we will need the paragraph and character bounds table to make decisions as 
	to where a table begins and ends
	*/
	if ( (wvQuerySupported(&ps->fib,NULL) == WORD6) || (wvQuerySupported(&ps->fib,NULL) == WORD7) )
		{
		wvGetBTE_PLCF6(&btePapx,&posPapx,&para_intervals,ps->fib.fcPlcfbtePapx,ps->fib.lcbPlcfbtePapx,ps->tablefd);
		wvGetBTE_PLCF6(&bteChpx,&posChpx,&char_intervals,ps->fib.fcPlcfbteChpx, ps->fib.lcbPlcfbteChpx,ps->tablefd);
		}
	else 
		{
		wvGetBTE_PLCF(&btePapx,&posPapx,&para_intervals,ps->fib.fcPlcfbtePapx,ps->fib.lcbPlcfbtePapx,ps->tablefd);
		wvGetBTE_PLCF(&bteChpx,&posChpx,&char_intervals,ps->fib.fcPlcfbteChpx,ps->fib.lcbPlcfbteChpx,ps->tablefd);
		}

	wvGetSED_PLCF(&sed,&posSedx,&section_intervals,ps->fib.fcPlcfsed,ps->fib.lcbPlcfsed,ps->tablefd);
	wvTrace(("section_intervals is %d\n",section_intervals));

	wvInitPAPX_FKP(&para_fkp);
	wvInitCHPX_FKP(&char_fkp);

	wvHandleDocument(ps,DOCBEGIN);

	
	/*for each piece*/
	for (piececount=0;piececount<ps->clx.nopcd;piececount++)
		{
		chartype = wvGetPieceBoundsFC(&beginfc,&endfc,&ps->clx,piececount);
		fseek(ps->mainfd,beginfc,SEEK_SET);
		wvGetPieceBoundsCP(&begincp,&endcp,&ps->clx,piececount);
		wvTrace(("piece begins at %x and ends just before %x. the char end is %x\n",beginfc,endfc,char_fcLim));

		/*
		text that is not in the same piece is not guaranteed to have the same properties as
		the rest of the exception run, so force a stop and restart of these properties.
		*/
		char_fcLim = beginfc;
		
		for (i=begincp,j=beginfc;(i<endcp /*&& i<ps->fib.ccpText*/);i++,j += wvIncFC(chartype))
			{
			ps->currentcp = i;
			/* character properties */
			if (j == char_fcLim)
				{
				wvHandleElement(ps,CHARPROPEND, (void*)&achp,char_dirty);
				char_pendingclose=0;
				}

			/* comment ending location */
			if (i == comment_cpLim)
				{
				wvHandleElement(ps,COMMENTEND, (void*)catrd,0);
				comment_pendingclose=0;
				}

			/* paragraph properties */
			if (j == para_fcLim)
				{
				wvHandleElement(ps,PARAEND, (void*)&apap,para_dirty);
				para_pendingclose=0;
				}

			/* section properties */
			if (j == section_fcLim)
                {
                wvHandleElement(ps, SECTIONEND, (void*)&sep,section_dirty);
                section_pendingclose = 0;
                }

			if ((section_fcLim == 0xffffffff) || (section_fcLim == j))
				{
                section_dirty = wvGetSimpleSectionBounds(wvQuerySupported(&ps->fib,NULL),ps,&sep,&section_fcFirst,&section_fcLim,i,&ps->clx, sed, &spiece,posSedx, section_intervals, &ps->stsh,ps->mainfd);
                section_dirty = (wvGetComplexSEP(wvQuerySupported(&ps->fib,NULL),&sep,spiece,&ps->stsh,&ps->clx) ? 1 : section_dirty);
				}

            if (j == section_fcFirst)
                {
                wvHandleElement(ps, SECTIONBEGIN, (void*)&sep,section_dirty);
                section_pendingclose = 1;
                }


			if ((para_fcLim == 0xffffffffL) || (para_fcLim == j))
				{
				wvReleasePAPX_FKP(&para_fkp);
				wvTrace(("cp and fc are %x(%d) %x\n",i,i,wvConvertCPToFC(i, &ps->clx)));
				cpiece = wvGetComplexParaBounds(wvQuerySupported(&ps->fib,NULL),&para_fkp,&para_fcFirst,&para_fcLim,wvConvertCPToFC(i, &ps->clx),&ps->clx, btePapx, posPapx, para_intervals,piececount,ps->mainfd);
				wvTrace(("para begin and end is %x %x\n",para_fcFirst,para_fcLim));

				if (0 == para_pendingclose)
					{
					/*
					if there's no paragraph open, but there should be then I believe that the fcFirst search
					has failed me, so I set it to now. I need to investigate this further. I believe it occurs
					when a the last piece ended simultaneously with the last paragraph, and that the algorithm
					for finding the beginning of a para breaks under that condition. I need more examples to
					be sure, but it happens is very large complex files so its hard to find
					*/
					if (j != para_fcFirst)
						{
						wvWarning(("There is no paragraph due to open but one should be, plugging the gap.\n"));
						para_fcFirst = j;
						}
					}

				}

			if (j == para_fcFirst)
				{
				para_dirty = wvAssembleSimplePAP(wvQuerySupported(&ps->fib,NULL),&apap,para_fcLim,&para_fkp,&ps->stsh,ps->data);
				para_dirty = (wvAssembleComplexPAP(wvQuerySupported(&ps->fib,NULL),&apap,cpiece,&ps->stsh,&ps->clx,ps->data) ? 1 : para_dirty);

				/* test section */
				wvReleasePAPX_FKP(&para_fkp);
				wvTrace(("cp and fc are %x(%d) %x\n",i,i,wvConvertCPToFC(i, &ps->clx)));
				npiece = wvGetComplexParaBounds(wvQuerySupported(&ps->fib,NULL),&para_fkp,&dummy,&nextpara_fcLim,para_fcLim,&ps->clx, btePapx, posPapx, para_intervals,piececount,ps->mainfd);
				wvTrace(("para begin and end is %x %x\n",para_fcFirst,para_fcLim));
				if (npiece > -1)
					{
					wvAssembleSimplePAP(wvQuerySupported(&ps->fib,NULL),&ps->nextpap,nextpara_fcLim,&para_fkp,&ps->stsh,ps->data);
					wvAssembleComplexPAP(wvQuerySupported(&ps->fib,NULL),&ps->nextpap,npiece,&ps->stsh,&ps->clx,ps->data);
					}
				else
					wvInitPAP(&ps->nextpap);
				/* end test section */

				if ( (apap.fInTable) && (!apap.fTtp) )
					{
					wvGetComplexFullTableInit(ps,para_intervals,btePapx,posPapx,piececount);
					wvGetComplexRowTap(ps,&apap,para_intervals,btePapx,posPapx,piececount);
					}
				else if (apap.fInTable == 0)
					ps->intable=0;

				wvHandleElement(ps,PARABEGIN, (void*)&apap,para_dirty);

				char_fcLim = j;
				para_pendingclose=1;
				}


			if ((comment_cpLim == 0xffffffffL) || (comment_cpLim == i))
				{
				wvTrace(("searching for the next comment begin cp is %d\n",i));
				catrd = wvGetCommentBounds(&comment_cpFirst,&comment_cpLim,i,atrd,posAtrd,atrd_intervals,&SttbfAtnbkmk,
				bkf,posBKF,bkf_intervals,bkl,posBKL,bkl_intervals);
				wvTrace(("begin and end are %d %d\n",comment_cpFirst,comment_cpLim));
				}

			if (i == comment_cpFirst)
				{
				wvHandleElement(ps,COMMENTBEGIN, (void*)catrd,0);
				comment_pendingclose=1;
				}


			if ((char_fcLim == 0xffffffffL) || (char_fcLim == j))
				{
				wvReleaseCHPX_FKP(&char_fkp);
				/*try this without using the piece of the end char for anything*/
				wvGetComplexCharBounds(wvQuerySupported(&ps->fib,NULL),&char_fkp,&char_fcFirst,&char_fcLim,wvConvertCPToFC(i, &ps->clx),&ps->clx, bteChpx, posChpx, char_intervals,piececount,ps->mainfd);
				wvTrace(("Bounds from %x to %x\n",char_fcFirst,char_fcLim));
				if (char_fcLim == char_fcFirst)
					wvError(("I believe that this is an error, and you might see incorrect character properties\n"));
				 if (0 == char_pendingclose)
                    {
                    /*
                    if there's no character run open, but there should be then I believe that the fcFirst search
                    has failed me, so I set it to now. I need to investigate this further.
                    */
                    if (j != char_fcFirst)
                        {
                        wvWarning(("There is no character run due to open but one should be, plugging the gap.\n"));
                        char_fcFirst = j;
                        }

                    }
				}

			if (j == char_fcFirst)
				{
				/* a CHP's base style is in the para style */
				achp.istd = apap.istd;
				wvTrace(("getting chp\n"));
				char_dirty = wvAssembleSimpleCHP(wvQuerySupported(&ps->fib, NULL),&achp,char_fcLim,&char_fkp,&ps->stsh);
				wvTrace(("getting complex chp\n"));
				char_dirty = (wvAssembleComplexCHP(wvQuerySupported(&ps->fib,NULL),&achp,cpiece,&ps->stsh,&ps->clx) ? 1 : char_dirty);
				wvHandleElement(ps,CHARPROPBEGIN, (void*)&achp,char_dirty);
				char_pendingclose=1;
				}


			eachchar = wvGetChar(ps->mainfd,chartype);

			/* previously, in place of ps there was a NULL,
			 * but it was crashing Abiword. Was it NULL for a
			 * reason? -JB */
			/* 
			nah, it was a oversight from when i didn't actually
			use ps in this function
			C.
			*/
			if ((eachchar == 0x07) && (!achp.fSpec))
				ps->endcell=1;

			wvTrace(("char pos is %x %x\n",j,eachchar));
			wvOutputTextChar(eachchar,chartype,ps,&achp);
			}

		if (j == para_fcLim)
			{
			wvHandleElement(ps,PARAEND, (void*)&apap,para_dirty);
			para_pendingclose=0;
			para_fcLim = 0xffffffffL;
			}

		if (i == comment_cpLim)
			{
			wvHandleElement(ps,COMMENTEND, (void*)catrd,0);
			comment_pendingclose=0;
			comment_cpLim = 0xffffffffL;
			}

		if (j == char_fcLim)
			{
			wvHandleElement(ps,CHARPROPEND, (void*)&achp,char_dirty);
			char_pendingclose=0;
			char_fcLim = 0xffffffffL;
			}

#if 0
		/*	
		I might have to rethink this closing tag enforcer for complex mode, have to think the
		flow out a bit more, this section one is plain wrong, im leaving it here so i won't
		forget and be tempted to put it back in :-)
		if (j == section_fcLim)
			{
        	wvHandleElement(ps, SECTIONEND, (void*)&sep,section_dirty);
			section_pendingclose=0;
			}
		*/
#endif
		}

	if (char_pendingclose)
		{
		wvInitCHP(&achp);
		wvHandleElement(ps,CHARPROPEND, (void*)&achp,char_dirty);
		}

	if (comment_pendingclose)
		wvHandleElement(ps,COMMENTEND, (void*)catrd,0);

	if (para_pendingclose)
		{
		wvInitPAP(&apap);
		wvHandleElement(ps,PARAEND, (void*)&apap,para_dirty);
		}

	if (section_pendingclose)
        wvHandleElement(ps, SECTIONEND, (void*)&sep,section_dirty);

	wvFree(ps->fspa);
	wvFree(ps->fspapos);
	wvFree(ps->fdoa);
	wvFree(ps->fdoapos);

	wvFree(posBKL);
	wvFree(bkl);
	wvFree(posBKF);
	wvFree(bkf);
	wvFree(posAtrd);
	wvFree(atrd);

	wvReleasePAPX_FKP(&para_fkp);
	wvReleaseCHPX_FKP(&char_fkp);

	wvHandleDocument(ps,DOCEND);
	wvFree(posSedx);
	wvFree(sed);

	wvFree(ps->liststartnos);
	wvFree(ps->listnfcs);
	for (i=0;i<9 * ps->nolfo;i++)
		wvReleaseLVL(&(ps->finallvl[i]));
	wvFree(ps->finallvl);

	wvReleaseLST(&ps->lst,ps->noofLST);
	wvReleaseLFO_records(&ps->lfo,&ps->lfolvl,&ps->lvl,ps->nooflvl);
	wvReleaseSTTBF(&ps->anSttbfAssoc);
	
	wvFree(btePapx);
	wvFree(posPapx);
	wvFree(bteChpx);
	wvFree(posChpx);
	wvReleaseCLX(&ps->clx);
	wvReleaseFFN_STTBF(&ps->fonts);
	wvReleaseSTSH(&ps->stsh);
	wvReleaseSTTBF(&SttbfAtnbkmk);
	wvReleaseSTTBF(&grpXstAtnOwners);
	if (ps->vmerges)
		{
		for(i=0;i<ps->norows;i++)
			wvFree(ps->vmerges[i]);
		wvFree(ps->vmerges);
		}
	wvFree(ps->cellbounds);
	tokenTreeFreeAll();
	}

/*
 The process thus far has created a SEP that describes what the section properties of 
 the section at the last full save. 

 1) Now apply any section sprms that were linked to the piece that contains the 
 section's section mark. 
 
 2) If pcd.prm.fComplex is 0, pcd.prm contains 1 sprm which should be applied to 
 the local SEP if it is a section sprm. 
 
 3) If pcd.prm.fComplex is 1, pcd.prm.igrpprl is the index of a grpprl in the CLX. 
 If that grpprl contains any section sprms, they should be applied to the local SEP
*/
int wvGetComplexSEP(version ver,SEP *sep,U32 cpiece,STSH *stsh,CLX *clx)
	{
	int ret=0;
	U16 sprm,pos=0,i=0;
	U8 *pointer;
	U16 index;
	U8 val;
	Sprm RetSprm;

	if (clx->pcd[cpiece].prm.fComplex == 0)
		{
		val = clx->pcd[cpiece].prm.para.var1.val;
		pointer = &val;
#ifdef SPRMTEST
		wvError(("singleton\n",clx->pcd[cpiece].prm.para.var1.isprm));
#endif
		RetSprm = wvApplySprmFromBucket(ver,wvGetrgsprmPrm(clx->pcd[cpiece].prm.para.var1.isprm),
		NULL,NULL, sep,stsh,pointer,&pos,NULL);
		if (RetSprm.sgc == sgcSep)	ret = 1;
		}
	else
		{
		index = clx->pcd[cpiece].prm.para.var2.igrpprl;
#ifdef SPRMTEST
		fprintf(stderr,"\n");
		while (i < clx->cbGrpprl[index])
			{
			fprintf(stderr,"%x (%d)\n",*(clx->grpprl[index]+i),*(clx->grpprl[index]+i));
			i++;
			}
		fprintf(stderr,"\n");
		i=0;
#endif
		while (i < clx->cbGrpprl[index])   
			{
			if (ver == WORD8)
				sprm = bread_16ubit(clx->grpprl[index]+i,&i);
			else
				{
				sprm = bgetc(clx->grpprl[index]+i,&i);
				sprm = wvGetrgsprmWord6(sprm);
				}
			pointer = clx->grpprl[index]+i;
			RetSprm = wvApplySprmFromBucket(ver,sprm,NULL,NULL,sep,stsh,pointer,&i,NULL);
			if (RetSprm.sgc == sgcSep)	ret = 1;
			}
		}
	return(ret);
	}
/*
The process thus far has created a PAP that describes
what the paragraph properties of the paragraph were at the last full save.

1) Now it's necessary to apply any paragraph sprms that were linked to the
piece that contains the paragraph's paragraph mark. 

2) If pcd.prm.fComplex is 0, pcd.prm contains 1 sprm which should only be 
applied to the local PAP if it is a paragraph sprm. 

3) If pcd.prm.fComplex is 1, pcd.prm.igrpprl is the index of a grpprl in the 
CLX.  If that grpprl contains any paragraph sprms, they should be applied to 
the local PAP.
*/
int wvAssembleComplexPAP(version ver,PAP *apap,U32 cpiece,STSH *stsh,CLX *clx, FILE *data)
	{
	int ret=0;
	U16 sprm,pos=0,i=0;
	U8 sprm8;
	U8 *pointer;
	U16 index;
	U8 val;
	Sprm RetSprm;

	if (clx->pcd[cpiece].prm.fComplex == 0)
		{
		val = clx->pcd[cpiece].prm.para.var1.val;
		pointer = &val;
#ifdef SPRMTEST
		wvError(("singleton\n",clx->pcd[cpiece].prm.para.var1.isprm));
#endif
		RetSprm = wvApplySprmFromBucket(ver,wvGetrgsprmPrm(clx->pcd[cpiece].prm.para.var1.isprm),
		apap,NULL, NULL,stsh,pointer,&pos,data);
		if (RetSprm.sgc == sgcPara)	ret = 1;
		}
	else
		{
		index = clx->pcd[cpiece].prm.para.var2.igrpprl;
#ifdef SPRMTEST
		wvError(("HERE-->\n"));
		fprintf(stderr,"\n");
		for (i=0;i<clx->cbGrpprl[index];i++)
			fprintf(stderr,"%x ",*(clx->grpprl[index]+i));
		fprintf(stderr,"\n");
		i=0;
#endif
		while (i < clx->cbGrpprl[index])   
			{
			if (ver == WORD8)
				sprm = bread_16ubit(clx->grpprl[index]+i,&i);
			else
				{
				sprm8 = bgetc(clx->grpprl[index]+i,&i);
				sprm = (U16)wvGetrgsprmWord6(sprm8);
				wvTrace(("sprm is %x\n",sprm));
				}
			pointer = clx->grpprl[index]+i;
			RetSprm = wvApplySprmFromBucket(ver,sprm,apap,NULL,NULL,stsh,pointer,&i,data);
			if (RetSprm.sgc == sgcPara)	ret = 1;
			}
		}
	return(ret);
	}

/* CHP version of the above. follows the same rules -JB */
int wvAssembleComplexCHP(version ver,CHP *achp,U32 cpiece,STSH *stsh,CLX *clx)
	{
	int ret=0;
	U16 sprm,pos=0,i=0;
	U8 sprm8;
	U8 *pointer;
	U16 index;
	U8 val;
	Sprm RetSprm;

	if (clx->pcd[cpiece].prm.fComplex == 0)
		{
		val = clx->pcd[cpiece].prm.para.var1.val;
		pointer = &val;
#ifdef SPRMTEST
		wvError(("singleton %d\n",clx->pcd[cpiece].prm.para.var1.isprm));
#endif
		RetSprm = wvApplySprmFromBucket(ver,wvGetrgsprmPrm(clx->pcd[cpiece].prm.para.var1.isprm),
		NULL, achp, NULL,stsh,pointer,&pos,NULL);
		if (RetSprm.sgc == sgcChp)  ret = 1;
		}
	else
		{
		index = clx->pcd[cpiece].prm.para.var2.igrpprl;
#ifdef SPRMTEST
		fprintf(stderr,"\n");
		for (i=0;i<clx->cbGrpprl[index];i++)
			fprintf(stderr,"%x ",*(clx->grpprl[index]+i));
		fprintf(stderr,"\n");
		i=0;
#endif
		while (i < clx->cbGrpprl[index])   
			{
			if (ver == WORD8)
				sprm = bread_16ubit(clx->grpprl[index]+i,&i);
			else
				{
				sprm8 = bgetc(clx->grpprl[index]+i,&i);
				sprm = (U16)wvGetrgsprmWord6(sprm8);
				}
			pointer = clx->grpprl[index]+i;
			RetSprm = wvApplySprmFromBucket(ver,sprm,NULL,achp,NULL,stsh,
				pointer,&i,NULL);
			if (RetSprm.sgc == sgcChp)  ret = 1;
			}
		}
	return(ret);
	}