File: isbidi.c

package info (click to toggle)
wv 1.0.2-0.1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 6,428 kB
  • ctags: 6,733
  • sloc: ansic: 59,221; sh: 9,998; xml: 1,677; makefile: 36
file content (509 lines) | stat: -rw-r--r-- 14,158 bytes parent folder | download | duplicates (3)
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
#include <stdlib.h>
#include <stdio.h>
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "wv.h"

/*
    This file contains implementation of wvIsBidiDocument() which can
    be used to determine if the document requires any bidi
    support. Please note, that for the function to work, the
    wvParseStruct must be already initialized; basically you should
    call this function from inside of your document callback procedure.

    As there is no simple way to determine this, we crawl over all the
    formatting records (section, paragraph, character) and look for
    the fBidi attribute being set.

    The bulk of the code was ripped from decode_simple.c and
    decode_complex.c, and stripped of the unnecessary bits and pieces.
    
    Tomas <tomas@frydrych.uklinux.net>, Feb 1, 2003
*/

/* function prototypes */
int wvIsBidiDocumentSimple(wvParseStruct * ps, subdocument whichdoc);
int wvIsBidiDocumentComplex(wvParseStruct * ps);

int wvGetComplexParaBounds (wvVersion ver, PAPX_FKP * fkp, U32 * fcFirst,
							U32 * fcLim, U32 currentfc, CLX * clx, BTE * bte,
							U32 * pos, int nobte, U32 piece, wvStream * fd);

int wvGetComplexCharBounds (wvVersion ver, CHPX_FKP * fkp, U32 * fcFirst,
							U32 * fcLim, U32 currentfc, CLX * clx, BTE * bte,
							U32 * pos, int nobte, U32 piece, wvStream * fd);

void internal_wvReleasePAPX_FKP (PAPX_FKP * fkp);
void internal_wvReleaseCHPX_FKP (CHPX_FKP * fkp);

int wvIsBidiDocument(wvParseStruct * ps)
{
    if (ps->fib.fComplex)
		return wvIsBidiDocumentComplex (ps);
    else
		return wvIsBidiDocumentSimple (ps, Dmain);
}

int wvIsBidiDocumentComplex (wvParseStruct * ps)
{
	int ret = 0;
    U32 piececount = 0, i, j, spiece = 0;
    U32 beginfc, endfc;
	U32 stream_size;
    U32 begincp, endcp;
    int ichartype;
    U8  chartype;
    U32 para_fcFirst, para_fcLim = 0xffffffffL;
    U32 nextpara_fcLim = 0xffffffffL;
    U32 char_fcFirst, char_fcLim = 0xffffffffL;
    U32 section_fcFirst, section_fcLim = 0xffffffffL;
    BTE *btePapx = NULL, *bteChpx = NULL;
    U32 *posPapx = NULL, *posChpx = NULL;
    U32 para_intervals, char_intervals, section_intervals;
    int cpiece = 0, npiece = 0;
    PAPX_FKP para_fkp;
    PAP apap;
    CHPX_FKP char_fkp;
    CHP achp;
    int para_dirty = 0, char_dirty = 0, section_dirty = 0;
    SED *sed = NULL;
    SEP sep;
    U32 *posSedx = NULL;
    wvVersion ver = wvQuerySupported (&ps->fib, NULL);

	external_wvReleasePAPX_FKP ();
    external_wvReleaseCHPX_FKP ();

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

    /*
	  we will need the paragraph and character bounds table to make decisions as 
	  to where a table begins and ends
	*/
    if ((ver == WORD6)
		|| (ver == 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);

	/*get stream size for bounds checking*/
	stream_size = wvStream_size(ps->mainfd);

    /*for each piece */
    for (piececount = 0; piececount < ps->clx.nopcd; piececount++)
	{
		ichartype =
			wvGetPieceBoundsFC (&beginfc, &endfc, &ps->clx, piececount);
		if(ichartype==-1)
			break;
		chartype = (U8) ichartype;
		/*lvm007@aha.ru fix antiloop: check stream size */
		if(beginfc>stream_size || endfc>stream_size){
			wvError (
					 ("Piece Bounds out of range!, its a disaster\n"));
			continue;
		}
		wvStream_goto (ps->mainfd, beginfc);
		/*lvm007@aha.ru fix antiloop fix*/
		if(wvGetPieceBoundsCP (&begincp, &endcp, &ps->clx, piececount)==-1)
			break;
		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;
			if ((section_fcLim == 0xffffffff) || (section_fcLim == j))
			{
				section_dirty =
					wvGetSimpleSectionBounds (ver, ps,
											  &sep, &section_fcFirst,
											  &section_fcLim, i,
											  &ps->clx, sed, &spiece,
											  posSedx,
											  section_intervals,
											  &ps->stsh, ps->mainfd);
				section_dirty =
					(wvGetComplexSEP
					 (ver, &sep, spiece,
					  &ps->stsh, &ps->clx) ? 1 : section_dirty);

				if(sep.fBidi)
				{
					ret = 1;
					goto complex_return;
				}
				
			}

			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 (ver, &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 (j == para_fcFirst)
			{
				para_dirty =
					wvAssembleSimplePAP (ver, &apap, para_fcLim, &para_fkp, ps);
				para_dirty =
					(wvAssembleComplexPAP
					 (ver, &apap, cpiece,ps) ? 1 : para_dirty);
#if 0
				/* not sure what this is about, Tomas*/
				/* test section */
				wvReleasePAPX_FKP (&para_fkp);
				wvTrace (
						 ("cp and fc are %x(%d) %x\n", i, i,
						  wvConvertCPToFC (i, &ps->clx)));
				npiece =
					wvGetComplexParaBounds (ver, &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 (ver, &ps->nextpap, nextpara_fcLim,
										 &para_fkp, &ps->stsh,
										 ps->data);
					wvAssembleComplexPAP (ver, &ps->nextpap, npiece,
										  &ps->stsh, &ps->clx,
										  ps->data);
				}
				else
					wvInitPAP (&ps->nextpap);
				/* end test section */
#endif
				if(apap.fBidi)
				{
					ret = 1;
					goto complex_return;
				}
			}

			if ((char_fcLim == 0xffffffffL) || (char_fcLim == j))
			{
				wvReleaseCHPX_FKP (&char_fkp);
				/*try this without using the piece of the end char for anything */
				wvGetComplexCharBounds (ver, &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 (j == char_fcFirst)
			{
				/* a CHP's base style is in the para style */
				/* achp.istd = apap.istd; */
				wvTrace (("getting chp\n"));
				char_dirty =
					wvAssembleSimpleCHP (ver, &achp, &apap,
										 char_fcLim, &char_fkp,
										 &ps->stsh);
				wvTrace (("getting complex chp\n"));
				char_dirty =
					(wvAssembleComplexCHP
					 (ver, &achp, cpiece,
					  &ps->stsh, &ps->clx) ? 1 : char_dirty);

				if(achp.fBidi)
				{
					ret = 1;
					goto complex_return;
				}
			}
	    }
	}

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

    wvFree (posSedx);
    wvFree (sed);

    wvFree (btePapx);
    wvFree (posPapx);
    wvFree (bteChpx);
    wvFree (posChpx);
	return ret;
}

int wvIsBidiDocumentSimple(wvParseStruct * ps, subdocument whichdoc)
{
	int ret = 0;
    PAPX_FKP para_fkp;
    CHPX_FKP char_fkp;
    PAP apap;
    CHP achp;
    U32 piececount = 0, i, j = 0, spiece;
    U32 beginfc, endfc;
	U32 stream_size;
    U32 begincp, endcp;
    int ichartype;
    U8  chartype;
    U32 para_fcFirst, para_fcLim = 0xffffffff;
    U32 /*dummy,*/ nextpara_fcLim = 0xffffffff;
    U32 char_fcFirst, char_fcLim = 0xffffffff;
    U32 section_fcFirst, section_fcLim = 0xffffffff;
    BTE *btePapx = NULL, *bteChpx = NULL;
    U32 *posPapx = NULL, *posChpx = NULL;
    U32 para_intervals, char_intervals, section_intervals;
    int para_dirty = 0, char_dirty = 0, section_dirty = 0;
    SED *sed = NULL;
    SEP sep;
    U32 *posSedx = NULL;
    wvVersion ver;

    external_wvReleasePAPX_FKP ();
    external_wvReleaseCHPX_FKP ();

    ver = wvQuerySupported (&ps->fib, NULL);

    para_fcFirst = char_fcFirst = section_fcFirst = wvGetBeginFC (ps, whichdoc);
	

	/*
	  we will need the paragraph and character bounds table to make decisions as 
	  to where a para/char run begins and ends
	*/
    if ((ver == WORD6)
		|| (ver == 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			/* word 97 */
	{
		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));
	
    /*
	  The text of the file starts at fib.fcMin, but we will use the piecetable 
	  records rather than this basic seek.
	  fseek(ps->mainfd,ps->fib.fcMin,SEEK_SET);
	*/

    /*
	  If !fib.fComplex, the document text stream is represented by the text
	  beginning at fib.fcMin up to (but not including) fib.fcMac.
	*/

    if ((ver == WORD2) && !ps->fib.fComplex)
	{
		/* the formatting handler is not implemented, assume LTR
		   (there will not be many of these around anyway)*/ 
		goto simple_return;
	}

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

	/*get stream size for bounds checking*/
	stream_size = wvStream_size(ps->mainfd);

    /*for each piece */
    for (piececount = 0; piececount < ps->clx.nopcd; piececount++)
	{
		ichartype =
			wvGetPieceBoundsFC (&beginfc, &endfc, &ps->clx, piececount);
		if(ichartype==-1)
			break;
		chartype = (U8) ichartype;
		/*lvm007@aha.ru fix antiloop: check stream size */
		if(beginfc>stream_size || endfc>stream_size){
			wvError (
					 ("Piece Bounds out of range!, its a disaster\n"));
			continue;
		}

		wvStream_goto (ps->mainfd, beginfc);

		wvTrace (("SEEK %x\n", beginfc));

		/*lvm007@aha.ru fix antiloop fix*/
		if(wvGetPieceBoundsCP (&begincp, &endcp, &ps->clx, piececount)==-1)
			break;

		/*
		  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;
		wvTrace (("%d %d %d\n", begincp, endcp, ps->fib.ccpText));
		for (i = begincp, j = beginfc; (i < endcp /*&& i<ps->fib.ccpText */ );
			 i++, j += wvIncFC (chartype))
	    {
			if ((section_fcLim == 0xffffffff) || (section_fcLim == j))
			{
				wvTrace (("j i is %x %d\n", j, i));
				/* the following call inits sep */
				section_dirty =
					wvGetSimpleSectionBounds (ver, ps,
											  &sep, &section_fcFirst,
											  &section_fcLim, i,
											  &ps->clx, sed, &spiece,
											  posSedx,
											  section_intervals,
											  &ps->stsh, ps->mainfd);
				wvTrace (
						 ("section begins at %x ends %x\n",
						  section_fcFirst, section_fcLim));
				if(sep.fBidi)
				{
					ret = 1;
					goto simple_return;
				}
				
			}

			if ((para_fcLim == 0xffffffff) || (para_fcLim == j))
			{
				wvReleasePAPX_FKP (&para_fkp);
				wvGetSimpleParaBounds (ver, &para_fkp,
									   &para_fcFirst, &para_fcLim,
									   wvConvertCPToFC (i, &ps->clx),
									   btePapx, posPapx, para_intervals,
									   ps->mainfd);
				wvTrace (
						 ("Para from %x to %x, j is %x\n", para_fcFirst,
						  para_fcLim, j));

			}

			if (j == para_fcFirst)
			{
				para_dirty =
					wvAssembleSimplePAP (ver, &apap, para_fcLim, &para_fkp, ps);

				if(apap.fBidi)
				{
					ret = 1;
					goto simple_return;
				}
			}

			if ((char_fcLim == 0xffffffff) || (char_fcLim == j))
			{
				wvTrace (("j i is %x %d\n", j, i));
				wvReleaseCHPX_FKP (&char_fkp);
				wvGetSimpleCharBounds (ver, &char_fkp,
									   &char_fcFirst, &char_fcLim, i,
									   &ps->clx, bteChpx, posChpx,
									   char_intervals, ps->mainfd);
				wvTrace (
						 ("char begins at %x ends %x, j is %x\n",
						  char_fcFirst, char_fcLim, j));
			}

			if (j == char_fcFirst)
			{
				wvTrace (("assembling CHP...\n"));
				/* a CHP's base style is in the para style */
				/* achp.istd = apap.istd; */
				char_dirty =
					wvAssembleSimpleCHP (ver, &achp, &apap,
										 char_fcLim, &char_fkp,
										 &ps->stsh);
				wvTrace (("CHP assembled.\n"));

				if(achp.fBidi)
				{
					ret = 1;
					goto simple_return;
				}
			}
	    }
	}

 simple_return:
    internal_wvReleasePAPX_FKP (&para_fkp);
    internal_wvReleaseCHPX_FKP (&char_fkp);
    wvFree (posSedx);
    wvFree (sed);
#if defined(WIN32)
	wvReleasePAPX_FKP (&para_fkp);
	wvReleaseCHPX_FKP (&char_fkp);   

	external_wvReleasePAPX_FKP ();
	external_wvReleaseCHPX_FKP();
#endif

    wvFree (btePapx);
    wvFree (posPapx);
    wvFree (bteChpx);
    wvFree (posChpx);

	return ret;
}