File: docLayoutParagraphs.c

package info (click to toggle)
ted 2.11-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 11,064 kB
  • ctags: 13,935
  • sloc: ansic: 120,446; makefile: 7,469; sh: 253
file content (573 lines) | stat: -rw-r--r-- 15,649 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
/************************************************************************/
/*									*/
/*  Layout of a document. Layout of a series of paragraphs in a common	*/
/*  parent.								*/
/*									*/
/************************************************************************/

#   include	"tedConfig.h"

#   include	<stddef.h>
#   include	<stdio.h>
#   include	<stdlib.h>

#   define	y0	math_y0
#   define	y1	math_y1
#   include	<math.h>
#   undef	y0
#   undef	y1

#   include	<appImage.h>
#   include	"docLayout.h"

#   include	<appDebugon.h>

/************************************************************************/
/*									*/
/*  Initialize the formatting of a paragraph by determining its frame	*/
/*									*/
/************************************************************************/

static int docPsLayoutStartParagraph(
				int *				pToNextPage,
				const ParagraphLayoutContext *	plc,
				BufferItem *			paraBi,
				int				bottomTwips,
				int				stripHigh,
				const BlockFrame *		bf,
				ParagraphLayoutPosition *	plp )
    {
    const DocumentProperties *	dp= &(plc->plcBd->bdProperties);
    const DocumentGeometry *	dg;
    const BufferItem *		sectBi= paraBi;

    const ScreenLayout *	sl= &(plc->plcScreenLayout);

    while( sectBi && sectBi->biLevel != DOClevSECT )
	{ sectBi= sectBi->biParent;	}

    if  ( ! sectBi )
	{ XDEB(sectBi); return -1;	}

    dg= &(sectBi->biSectDocumentGeometry);

    /*  1  */
    if  ( paraBi->biInExternalItem == DOCinBODY			&&
	  ! paraBi->biParaInTable				&&
	  paraBi->biParaStartsOnNewPage				&&
	  ! plp->plpPos.lpAtTopOfColumn				)
	{ *pToNextPage= 1; return 0; }

    docParagraphFrameTwips( &(plp->plpFormattingFrame), bf,
					bottomTwips, stripHigh, paraBi );

    if  ( sl && sl->slScreenFrame )
	{
	(*sl->slScreenFrame)( &(plp->plpFormattingFrame),
						    plc->plcAdd, paraBi );
	}

    if  ( (*plc->plcStartParagraph)( paraBi, plc->plcAdd, dp ) )
	{ LDEB(1); return -1;	}

    if  ( sl && sl->slStartParagraph					&&
	  (*sl->slStartParagraph)( paraBi, plc->plcAdd, plc->plcBd )	)
	{ LDEB(1); return -1;	}

    *pToNextPage= 0;
    return 0;
    }

/************************************************************************/
/*									*/
/*  Layout as much of a series of paragraph as fits on the current	*/
/*  page.								*/
/*									*/
/*  1)  When the paragraph starts on a new page, refuse to proceed on	*/
/*	the current one.						*/
/*  2)  When Widow/Orphan control is active and we could only place	*/
/*	one line on the page without finishing the paragraph. Force	*/
/*	the formatting of this paragraph to restart on the next page.	*/
/*  3)  When the paragraph is to be kept on one page, and it is not	*/
/*	possible to finish it on this page, Force the formatting of	*/
/*	paragraph to restart on the next page.				*/
/*  4)  When Widow/Orphan control is active and we finish the paragraph	*/
/*	by placing its last line on this page, skip back to the		*/
/*	penulimate line.						*/
/*									*/
/************************************************************************/

int docLayoutParagraphsInStrip(	ParagraphLayoutJob *		plj,
				BlockFrame *			bf,
				const ParagraphLayoutContext *	plc,
				BufferItem *			cellBi,
				int				paraUpto,
				int				bottomTwips,
				int				stripHigh )
    {
    int				changed= 0;
    ParagraphLayoutPosition *	plp= &(plj->pljPos);
    ParagraphStripPosition *	psp= &(plp->plpProgress);

    if  ( cellBi->biLevel != DOClevCELL )
	{ SDEB(docLevelStr(cellBi->biLevel)); return -1;	}

    while( psp->pspPara < paraUpto )
	{
	BufferItem *		paraBi= cellBi->biChildren[psp->pspPara];
	int			accepted;
	int			prevLine;
	LayoutPosition		lpBefore;

	lpBefore= plp->plpPos;

	if  ( psp->pspPart == 0 )
	    {
	    int		toNextPage= 0;

	    if  ( docPsLayoutStartParagraph( &toNextPage, plc, paraBi,
					bottomTwips, stripHigh, bf, plp ) )
		{ LDEB(1); return -1;	}

	    if  ( toNextPage )
		{ return 0;	}

	    paraBi->biTopPosition= plp->plpPos;
	    }

	prevLine= psp->pspLine;

	accepted= (*plc->plcLayoutLines)( plp, bf, plc, paraBi );

	if  ( accepted < 0 )
	    { LDEB(accepted); return -1;	}

	/*  2  */
	if  ( paraBi->biParaWidowControl				&&
	      ! lpBefore.lpAtTopOfColumn				&&
	      psp->pspLine == 1						&&
	      psp->pspPart+ accepted < paraBi->biParaParticuleCount	&&
	      ! paraBi->biParaLines[0].tlHasPageBreak			)
	    {
	    psp->pspLine= 0;
	    psp->pspPart= 0;
	    break;
	    }

	/*  3  */
	if  ( ( paraBi->biParaKeepOnPage	||
	        paraBi->biParaKeepWithNext	)			&&
	      ! lpBefore.lpAtTopOfColumn				&&
	      psp->pspPart+ accepted < paraBi->biParaParticuleCount	&&
	      paraBi->biParaLineCount >= psp->pspLine			&&
	      psp->pspLine > 0						&&
	      ! paraBi->biParaLines[psp->pspLine- 1].tlHasPageBreak	)
	    {
	    psp->pspLine= 0;
	    psp->pspPart= 0;
	    break;
	    }

	psp->pspPart += accepted;

	if  ( psp->pspPart < paraBi->biParaParticuleCount		&&
	      ( accepted > 0 || ! lpBefore.lpAtTopOfColumn )		)
	    { break;	}

	/*  4  */
	if  ( paraBi->biParaWidowControl				&&
	      psp->pspLine > 1						&&
	      psp->pspLine- prevLine == 1				)
	    {
	    TextLine *		tl= paraBi->biParaLines+ prevLine- 1;

	    if  ( ! tl[0].tlHasPageBreak				&&
		  ! tl[0].tlTopPosition.lpAtTopOfColumn			&&
		  tl[0].tlTopPosition.lpPage < tl[1].tlTopPosition.lpPage )
		{
		psp->pspLine= prevLine- 1;
		psp->pspPart= tl->tlFirstParticule;
		plp->plpPos= tl[1].tlTopPosition;
		continue;
		}
	    }

	(*plc->plcAdjustBottom)( &changed, paraBi, &(plp->plpPos), plc->plcAdd,
					    plc->plcChangedRectanglePixels );

	psp->pspLine= 0;
	psp->pspPart= 0;
	psp->pspPara++;
	}

    return 0;
    }

/************************************************************************/
/*									*/
/*  Find a start point for the formatter to layout lines. A certain	*/
/*  work ahead is necessary to decide whether work on line layout can	*/
/*  be used or not. docPsAdvanceParagraphLayout() is used to decide	*/
/*  what work is final and what possibly is to be redone.		*/
/*									*/
/*  docPsAdvanceParagraphLayout() is called when a column is full. In	*/
/*  this case, everything that is not final yet needs to be moved to	*/
/*  the next page.							*/
/*									*/
/*  -   Insure that the constraints comming from Widow/Orphan control	*/
/*	are been obeyed.						*/
/*  -   Insure that paragraphs that should not be divided over pages	*/
/*	are not split.							*/
/*  -   Insure that paragraphs that should be with the next one as a	*/
/*	header really are with it. (Experimenting with Word shows that	*/
/*	paragraphs that should be with the next one should be		*/
/*	completely with it.)						*/
/*									*/
/*  When any of the above is violated, find a solution that respects	*/
/*  the requirements by pushing offending paragraphs or lines down.	*/
/*									*/
/*  docPsAdvanceParagraphLayout() shifts 'plp0' to the last position	*/
/*  that is not below 'plp1' that is not influenced by the layout of	*/
/*  position on 'plp1' or below. Everything before 'plp1' is formatted	*/
/*  and plp1->plpPos is the position where formatting would continue	*/
/*  if docPsAdvanceParagraphLayout() moved 'plp0' onto 'plp1'. (As it	*/
/*  often does).							*/
/*									*/
/*  0)  Avoid superfluous work and the handling of this exception	*/
/*	below: Commit an explit page break.				*/
/*									*/
/*  1)  Find the place to begin. Anything on a previous page is final:	*/
/*	It will not be shifted to the next page. (Paragraph)		*/
/*  2)	Idem: Line. The line found here might very well be irrelevant	*/
/*	because a different paragraph to commit is found below.		*/
/*  3)	Commit all paragraphs before the current one that do not have	*/
/*	the 'Keep with Next' flag.					*/
/*	flag.								*/
/*  4)	If the paragraph to be committed is before the current one	*/
/*	commit the position we have found. This can be:			*/
/*	-   The first line on the current page (from 2)			*/
/*	    [ This case is however excluded by the exception code (0)].	*/
/*	-   The head of a series of KeepWithNext paragraphs. (from 3)	*/
/*  5)	When this paragraph is to be kept together, restart from its	*/
/*	beginning.							*/
/*  6)	When Widow/Orphan control is active.. Do not commit the first	*/
/*	line before the whole paragraph is committed.			*/
/*  7)	When Widow/Orphan control is active.. Do not commit the last	*/
/*	line that has been formatted unless the paragraph is complete.	*/
/*									*/
/*  8)	And finally when no exception applies, start from where we are	*/
/*	now.								*/
/*									*/
/************************************************************************/

static void docPsAdvanceParagraphLayout_x(
				ParagraphStripPosition *	psp0,
				const ParagraphStripPosition *	psp1,
				int				page,
				const BufferItem *		cellBi )
    {
    const BufferItem *		paraBi;
    const TextLine *		tl;

    int				para;

    int				line1;

    /*  0  */
    if  ( psp1->pspPara < cellBi->biChildCount )
	{
	paraBi= cellBi->biChildren[psp1->pspPara];
	line1= psp1->pspLine;

	if  ( line1- 1 >= 0				&&
	      line1- 1 < paraBi->biParaLineCount	)
	    {
	    const TextLine *	tl= paraBi->biParaLines+ line1- 1;

	    if  ( tl->tlHasPageBreak )
		{
		*psp0= *psp1;
		return;
		}
	    }
	}

    /*  1  */
    paraBi= cellBi->biChildren[psp0->pspPara];
    while( psp0->pspPara < psp1->pspPara )
	{
	paraBi= cellBi->biChildren[psp0->pspPara];

	if  ( paraBi->biBelowPosition.lpPage >= page )
	    { break;	}

	psp0->pspPara++;
	psp0->pspLine= 0;
	psp0->pspPart= 0;
	}

    /*  2  */
    if  ( psp0->pspPara < psp1->pspPara )
	{ line1= paraBi->biParaLineCount;	}
    else{ line1= psp1->pspLine;			}

    tl= paraBi->biParaLines+ psp0->pspLine;
    while( psp0->pspLine < line1 )
	{
	if  ( tl->tlTopPosition.lpPage >= page )
	    { break;	}

	psp0->pspLine++; tl++;
	}

    /*  3  */
    for ( para= psp0->pspPara; para < psp1->pspPara; para++ )
	{
	paraBi= cellBi->biChildren[para];

	if  ( ! paraBi->biParaKeepWithNext )
	    {
	    psp0->pspPara= para+ 1;
	    psp0->pspLine= 0;
	    psp0->pspPart= 0;
	    }
	}

    /*  4  */
    if  ( psp0->pspPara < psp1->pspPara )
	{ return;	}

    if  ( psp0->pspPara != psp1->pspPara )
	{ LLDEB(psp0->pspPara,psp1->pspPara );	}

    if  ( psp0->pspPara >= cellBi->biChildCount )
	{ return;	}

    /*  5  */
    paraBi= cellBi->biChildren[psp0->pspPara];
    if  ( paraBi->biParaKeepOnPage	||
	  paraBi->biParaKeepWithNext	)
	{
	psp0->pspLine= 0;
	psp0->pspPart= 0;

	return;
	}

    /*  6  */
    if  ( paraBi->biParaWidowControl	&&
	  psp1->pspLine == 1		&&
	  paraBi->biParaLineCount >= 1	)
	{
	tl= paraBi->biParaLines+ 0;

	if  ( tl->tlFirstParticule+ tl->tlParticuleCount <
					paraBi->biParaParticuleCount	)
	    {
	    psp0->pspLine= 0;
	    psp0->pspPart= 0;

	    return;
	    }
	}

    /*  7  */
    if  ( paraBi->biParaWidowControl	&&
	  psp1->pspLine > 0		)
	{
	tl= paraBi->biParaLines+ psp1->pspLine- 1;

	if  ( tl->tlFirstParticule+ tl->tlParticuleCount <
					paraBi->biParaParticuleCount	)
	    {
	    psp0->pspLine= psp1->pspLine- 1;
	    psp0->pspPart= tl->tlFirstParticule;

	    return;
	    }
	}

    /*  8  */
    *psp0= *psp1;

    return;
    }


void docPsAdvanceParagraphLayout(
				int *				pAdvanced,
				ParagraphStripPosition *	psp0,
				const ParagraphStripPosition *	psp00,
				const ParagraphStripPosition *	psp1,
				int				page,
				const BufferItem *		cellBi )
    {
    int		advanced= 0;

    docPsAdvanceParagraphLayout_x( psp0, psp1, page, cellBi );

    if  ( psp0->pspPara >    psp00->pspPara		||
	  ( psp0->pspPara == psp00->pspPara	&&
	    psp0->pspPart >  psp00->pspPart	)	)
	{ advanced= 1;	}

    *pAdvanced= advanced;
    }

/************************************************************************/
/*									*/
/*  Format the lines in a series of paragraphs. On the way keep an	*/
/*  administration on where to rebegin formatting at a page break.	*/
/*									*/
/************************************************************************/

int docLayoutParagraphs(	const ParagraphLayoutContext *	plc,
				BufferItem *			cellBi,
				BlockFrame *			bf,
				LayoutJob *			lj,
				ParagraphLayoutJob *		plj )
    {
    int				toNextPage= 0;

    const int			bottomTwips= -1;
    const int			stripHigh= -1;

    LayoutPosition		lpBefore;

    const ScreenLayout *	sl= &(plc->plcScreenLayout);

    lpBefore= plj->pljPos.plpPos;

    if  ( docLayoutParagraphsInStrip( plj, bf, plc, cellBi,
					    plj->pljParaUpto,
					    bottomTwips, stripHigh ) )
	{ LDEB(1); return -1;	}

    toNextPage= plj->pljPos.plpProgress.pspPara < plj->pljParaUpto;

    while( toNextPage )
	{
	int				advanced;
	ParagraphStripPosition		psp0;

	if  ( BF_HAS_FOOTNOTES( bf )					&&
	      docLayoutFootnotesForColumn( bf, &(plj->pljPos.plpPos), lj ) )
	    { LDEB(1); return -1;	}

	psp0= plj->pljPos0.plpProgress;

	docPsAdvanceParagraphLayout( &advanced,
					&psp0,
					&(plj->pljPos0.plpProgress),
					&(plj->pljPos.plpProgress),
					lpBefore.lpPage, cellBi );

	docLayoutToNextColumn( cellBi, plc->plcBd, &(plj->pljPos.plpPos), bf );

	if  ( advanced )
	    {
	    plj->pljPos0.plpProgress= psp0;
	    plj->pljPos.plpProgress= psp0;
	    }
	else{
	    plj->pljPos0.plpProgress= plj->pljPos.plpProgress;
	    }

	docParagraphStripFrame( sl, cellBi, plc->plcAdd,
				    bottomTwips, stripHigh, bf, plj );

	lpBefore= plj->pljPos.plpPos;

	if  ( docLayoutParagraphsInStrip( plj, bf, plc, cellBi,
					    plj->pljParaUpto,
					    bottomTwips, stripHigh ) )
	    { LDEB(1); return -1;	}

	toNextPage= plj->pljPos.plpProgress.pspPara < plj->pljParaUpto;
	}

    return 0;
    }

/************************************************************************/
/*									*/
/*  Calculate the formatting frame for a paragraph layout.		*/
/*									*/
/************************************************************************/

void docParagraphStripFrame(	const ScreenLayout *		sl,
				BufferItem *			cellBi,
				AppDrawingData *		add,
				int				bottomTwips,
				int				stripHigh,
				const BlockFrame *		bf,
				ParagraphLayoutJob *		plj )
					
    {
    BufferItem *		paraBi;

    paraBi= cellBi->biChildren[plj->pljPos.plpProgress.pspPara];

    docParagraphFrameTwips( &(plj->pljPos.plpFormattingFrame), bf,
					bottomTwips, stripHigh, paraBi );

    if  ( sl && sl->slScreenFrame )
	{
	(*sl->slScreenFrame)( &(plj->pljPos.plpFormattingFrame),
							    add, paraBi );
	}

    return;
    }

/************************************************************************/
/*									*/
/*  Initialize paragraph layout progress.				*/
/*									*/
/************************************************************************/

static void docPsInitParagraphLayoutPosition( ParagraphLayoutPosition *	plp )
    {
    plp->plpProgress.pspPara= 0;
    plp->plpProgress.pspLine= 0;
    plp->plpProgress.pspPart= 0;

    docInitLayoutPosition( &(plp->plpPos) );

    return;
    }

void docPsInitParagraphLayoutJob(	ParagraphLayoutJob *	plj )
    {
    plj->pljParaUpto= 0;

    docPsInitParagraphLayoutPosition( &(plj->pljPos) );
    docPsInitParagraphLayoutPosition( &(plj->pljPos0) );

    plj->pljStopWhenSame= 0;
    plj->pljPageToFinish= -1;
    plj->pljColumnToFinish= -1;

    return;
    }

void docPsBeginParagraphLayoutProgress(	ParagraphLayoutJob *	plj,
					int			para,
					int			line,
					int			part,
					int			paraUpto,
					const LayoutPosition *	lp )
    {
    docPsInitParagraphLayoutJob( plj );

    plj->pljPos.plpProgress.pspPara= para;
    plj->pljPos.plpProgress.pspLine= line;
    plj->pljPos.plpProgress.pspPart= part;

    plj->pljParaUpto= paraUpto;

    plj->pljPos.plpPos= *lp;

    return;
    }