-
Notifications
You must be signed in to change notification settings - Fork 543
Expand file tree
/
Copy pathCssLayoutEngineTable.cs
More file actions
1053 lines (924 loc) · 39.5 KB
/
CssLayoutEngineTable.cs
File metadata and controls
1053 lines (924 loc) · 39.5 KB
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
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// "Therefore those skilled at the unorthodox
// are infinite as heaven and earth,
// inexhaustible as the great rivers.
// When they come to an end,
// they begin again,
// like the days and months;
// they die and are reborn,
// like the four seasons."
//
// - Sun Tsu,
// "The Art of War"
using System;
using System.Collections.Generic;
using TheArtOfDev.HtmlRenderer.Adapters;
using TheArtOfDev.HtmlRenderer.Adapters.Entities;
using TheArtOfDev.HtmlRenderer.Core.Entities;
using TheArtOfDev.HtmlRenderer.Core.Parse;
using TheArtOfDev.HtmlRenderer.Core.Utils;
namespace TheArtOfDev.HtmlRenderer.Core.Dom
{
/// <summary>
/// Layout engine for tables executing the complex layout of tables with rows/columns/headers/etc.
/// </summary>
internal sealed class CssLayoutEngineTable
{
#region Fields and Consts
/// <summary>
/// the main box of the table
/// </summary>
private readonly CssBox _tableBox;
/// <summary>
///
/// </summary>
private CssBox _caption;
private CssBox _headerBox;
private CssBox _footerBox;
/// <summary>
/// collection of all rows boxes
/// </summary>
private readonly List<CssBox> _bodyrows = new List<CssBox>();
/// <summary>
/// collection of all columns boxes
/// </summary>
private readonly List<CssBox> _columns = new List<CssBox>();
/// <summary>
///
/// </summary>
private readonly List<CssBox> _allRows = new List<CssBox>();
private int _columnCount;
private bool _widthSpecified;
private double[] _columnWidths;
private double[] _columnMinWidths;
#endregion
/// <summary>
/// Init.
/// </summary>
/// <param name="tableBox"></param>
private CssLayoutEngineTable(CssBox tableBox)
{
_tableBox = tableBox;
}
/// <summary>
/// Get the table cells spacing for all the cells in the table.<br/>
/// Used to calculate the spacing the table has in addition to regular padding and borders.
/// </summary>
/// <param name="tableBox">the table box to calculate the spacing for</param>
/// <returns>the calculated spacing</returns>
public static double GetTableSpacing(CssBox tableBox)
{
int count = 0;
int columns = 0;
foreach (var box in tableBox.Boxes)
{
if (box.Display == CssConstants.TableColumn)
{
columns += GetSpan(box);
}
else if (box.Display == CssConstants.TableRowGroup)
{
foreach (CssBox cr in tableBox.Boxes)
{
count++;
if (cr.Display == CssConstants.TableRow)
columns = Math.Max(columns, cr.Boxes.Count);
}
}
else if (box.Display == CssConstants.TableRow)
{
count++;
columns = Math.Max(columns, box.Boxes.Count);
}
// limit the amount of rows to process for performance
if (count > 30)
break;
}
// +1 columns because padding is between the cell and table borders
return (columns + 1) * GetHorizontalSpacing(tableBox);
}
/// <summary>
///
/// </summary>
/// <param name="g"></param>
/// <param name="tableBox"> </param>
public static void PerformLayout(RGraphics g, CssBox tableBox)
{
ArgChecker.AssertArgNotNull(g, "g");
ArgChecker.AssertArgNotNull(tableBox, "tableBox");
try
{
var table = new CssLayoutEngineTable(tableBox);
table.Layout(g);
}
catch (Exception ex)
{
tableBox.HtmlContainer.ReportError(HtmlRenderErrorType.Layout, "Failed table layout", ex);
}
}
#region Private Methods
/// <summary>
/// Analyzes the Table and assigns values to this CssTable object.
/// To be called from the constructor
/// </summary>
private void Layout(RGraphics g)
{
MeasureWords(_tableBox, g);
// get the table boxes into the proper fields
AssignBoxKinds();
// Insert EmptyBoxes for vertical cell spanning.
InsertEmptyBoxes();
// Determine Row and Column Count, and ColumnWidths
var availCellSpace = CalculateCountAndWidth();
DetermineMissingColumnWidths(availCellSpace);
// Check for minimum sizes (increment widths if necessary)
EnforceMinimumSize();
// While table width is larger than it should, and width is reducible
EnforceMaximumSize();
// Ensure there's no padding
_tableBox.PaddingLeft = _tableBox.PaddingTop = _tableBox.PaddingRight = _tableBox.PaddingBottom = "0";
//Actually layout cells!
LayoutCells(g);
}
/// <summary>
/// Get the table boxes into the proper fields.
/// </summary>
private void AssignBoxKinds()
{
foreach (var box in _tableBox.Boxes)
{
switch (box.Display)
{
case CssConstants.TableCaption:
_caption = box;
break;
case CssConstants.TableRow:
_bodyrows.Add(box);
break;
case CssConstants.TableRowGroup:
foreach (CssBox childBox in box.Boxes)
if (childBox.Display == CssConstants.TableRow)
_bodyrows.Add(childBox);
break;
case CssConstants.TableHeaderGroup:
if (_headerBox != null)
_bodyrows.Add(box);
else
_headerBox = box;
break;
case CssConstants.TableFooterGroup:
if (_footerBox != null)
_bodyrows.Add(box);
else
_footerBox = box;
break;
case CssConstants.TableColumn:
for (int i = 0; i < GetSpan(box); i++)
_columns.Add(box);
break;
case CssConstants.TableColumnGroup:
if (box.Boxes.Count == 0)
{
int gspan = GetSpan(box);
for (int i = 0; i < gspan; i++)
{
_columns.Add(box);
}
}
else
{
foreach (CssBox bb in box.Boxes)
{
int bbspan = GetSpan(bb);
for (int i = 0; i < bbspan; i++)
{
_columns.Add(bb);
}
}
}
break;
}
}
if (_headerBox != null)
_allRows.AddRange(_headerBox.Boxes);
_allRows.AddRange(_bodyrows);
if (_footerBox != null)
_allRows.AddRange(_footerBox.Boxes);
}
/// <summary>
/// Insert EmptyBoxes for vertical cell spanning.
/// </summary>
private void InsertEmptyBoxes()
{
if (!_tableBox._tableFixed)
{
int currow = 0;
List<CssBox> rows = _bodyrows;
foreach (CssBox row in rows)
{
for (int k = 0; k < row.Boxes.Count; k++)
{
CssBox cell = row.Boxes[k];
int rowspan = GetRowSpan(cell);
int realcol = GetCellRealColumnIndex(row, cell); //Real column of the cell
for (int i = currow + 1; i < currow + rowspan; i++)
{
if (rows.Count > i)
{
int colcount = 0;
for (int j = 0; j < rows[i].Boxes.Count; j++)
{
if (colcount == realcol)
{
rows[i].Boxes.Insert(colcount, new CssSpacingBox(_tableBox, ref cell, currow));
break;
}
colcount++;
realcol -= GetColSpan(rows[i].Boxes[j]) - 1;
}
}
}
}
currow++;
}
_tableBox._tableFixed = true;
}
}
/// <summary>
/// Determine Row and Column Count, and ColumnWidths
/// </summary>
/// <returns></returns>
private double CalculateCountAndWidth()
{
//Columns
if (_columns.Count > 0)
{
_columnCount = _columns.Count;
}
else
{
foreach (CssBox b in _allRows)
_columnCount = Math.Max(_columnCount, b.Boxes.Count);
}
//Initialize column widths array with NaNs
_columnWidths = new double[_columnCount];
for (int i = 0; i < _columnWidths.Length; i++)
_columnWidths[i] = double.NaN;
double availCellSpace = GetAvailableCellWidth();
if (_columns.Count > 0)
{
// Fill ColumnWidths array by scanning column widths
for (int i = 0; i < _columns.Count; i++)
{
CssLength len = new CssLength(_columns[i].Width); //Get specified width
if (len.Number > 0) //If some width specified
{
if (len.IsPercentage) //Get width as a percentage
{
_columnWidths[i] = CssValueParser.ParseNumber(_columns[i].Width, availCellSpace);
}
else if (len.Unit == CssUnit.Pixels || len.Unit == CssUnit.None)
{
_columnWidths[i] = len.Number; //Get width as an absolute-pixel value
}
}
}
}
else
{
// Fill ColumnWidths array by scanning width in table-cell definitions
foreach (CssBox row in _allRows)
{
//Check for column width in table-cell definitions
for (int i = 0; i < _columnCount; i++)
{
if (i < 20 || double.IsNaN(_columnWidths[i])) // limit column width check
{
if (i < row.Boxes.Count && row.Boxes[i].Display == CssConstants.TableCell)
{
double len = CssValueParser.ParseLength(row.Boxes[i].Width, availCellSpace, row.Boxes[i]);
if (len > 0) //If some width specified
{
int colspan = GetColSpan(row.Boxes[i]);
len /= Convert.ToSingle(colspan);
for (int j = i; j < i + colspan; j++)
{
_columnWidths[j] = double.IsNaN(_columnWidths[j]) ? len : Math.Max(_columnWidths[j], len);
}
}
}
}
}
}
}
return availCellSpace;
}
/// <summary>
///
/// </summary>
/// <param name="availCellSpace"></param>
private void DetermineMissingColumnWidths(double availCellSpace)
{
double occupedSpace = 0f;
if (_widthSpecified) //If a width was specified,
{
//Assign NaNs equally with space left after gathering not-NaNs
int numOfNans = 0;
//Calculate number of NaNs and occupied space
foreach (double colWidth in _columnWidths)
{
if (double.IsNaN(colWidth))
numOfNans++;
else
occupedSpace += colWidth;
}
var orgNumOfNans = numOfNans;
double[] orgColWidths = null;
if (numOfNans < _columnWidths.Length)
{
orgColWidths = new double[_columnWidths.Length];
for (int i = 0; i < _columnWidths.Length; i++)
orgColWidths[i] = _columnWidths[i];
}
if (numOfNans > 0)
{
// Determine the max width for each column
double[] minFullWidths, maxFullWidths;
GetColumnsMinMaxWidthByContent(true, out minFullWidths, out maxFullWidths);
// set the columns that can fulfill by the max width in a loop because it changes the nanWidth
int oldNumOfNans;
do
{
oldNumOfNans = numOfNans;
for (int i = 0; i < _columnWidths.Length; i++)
{
var nanWidth = (availCellSpace - occupedSpace) / numOfNans;
if (double.IsNaN(_columnWidths[i]) && nanWidth > maxFullWidths[i])
{
_columnWidths[i] = maxFullWidths[i];
numOfNans--;
occupedSpace += maxFullWidths[i];
}
}
} while (oldNumOfNans != numOfNans);
if (numOfNans > 0)
{
// Determine width that will be assigned to un assigned widths
double nanWidth = (availCellSpace - occupedSpace) / numOfNans;
for (int i = 0; i < _columnWidths.Length; i++)
{
if (double.IsNaN(_columnWidths[i]))
_columnWidths[i] = nanWidth;
}
}
}
if (numOfNans == 0 && occupedSpace < availCellSpace)
{
if (orgNumOfNans > 0)
{
// spread extra width between all non width specified columns
double extWidth = (availCellSpace - occupedSpace) / orgNumOfNans;
for (int i = 0; i < _columnWidths.Length; i++)
if (orgColWidths == null || double.IsNaN(orgColWidths[i]))
_columnWidths[i] += extWidth;
}
else
{
// spread extra width between all columns with respect to relative sizes
for (int i = 0; i < _columnWidths.Length; i++)
_columnWidths[i] += (availCellSpace - occupedSpace) * (_columnWidths[i] / occupedSpace);
}
}
}
else
{
//Get the minimum and maximum full length of NaN boxes
double[] minFullWidths, maxFullWidths;
GetColumnsMinMaxWidthByContent(true, out minFullWidths, out maxFullWidths);
for (int i = 0; i < _columnWidths.Length; i++)
{
if (double.IsNaN(_columnWidths[i]))
_columnWidths[i] = minFullWidths[i];
occupedSpace += _columnWidths[i];
}
// spread extra width between all columns
for (int i = 0; i < _columnWidths.Length; i++)
{
if (maxFullWidths[i] > _columnWidths[i])
{
var temp = _columnWidths[i];
_columnWidths[i] = Math.Min(_columnWidths[i] + (availCellSpace - occupedSpace) / Convert.ToSingle(_columnWidths.Length - i), maxFullWidths[i]);
occupedSpace = occupedSpace + _columnWidths[i] - temp;
}
}
}
}
/// <summary>
/// While table width is larger than it should, and width is reductable.<br/>
/// If table max width is limited by we need to lower the columns width even if it will result in clipping<br/>
/// </summary>
private void EnforceMaximumSize()
{
int curCol = 0;
var widthSum = GetWidthSum();
while (widthSum > GetAvailableTableWidth() && CanReduceWidth())
{
while (!CanReduceWidth(curCol))
curCol++;
_columnWidths[curCol] -= 1f;
curCol++;
if (curCol >= _columnWidths.Length)
curCol = 0;
}
// if table max width is limited by we need to lower the columns width even if it will result in clipping
var maxWidth = GetMaxTableWidth();
if (maxWidth < 90999)
{
widthSum = GetWidthSum();
if (maxWidth < widthSum)
{
//Get the minimum and maximum full length of NaN boxes
double[] minFullWidths, maxFullWidths;
GetColumnsMinMaxWidthByContent(false, out minFullWidths, out maxFullWidths);
// lower all the columns to the minimum
for (int i = 0; i < _columnWidths.Length; i++)
_columnWidths[i] = minFullWidths[i];
// either min for all column is not enought and we need to lower it more resulting in clipping
// or we now have extra space so we can give it to columns than need it
widthSum = GetWidthSum();
if (maxWidth < widthSum)
{
// lower the width of columns starting from the largest one until the max width is satisfied
for (int a = 0; a < 15 && maxWidth < widthSum - 0.1; a++) // limit iteration so bug won't create infinite loop
{
int nonMaxedColumns = 0;
double largeWidth = 0f, secLargeWidth = 0f;
for (int i = 0; i < _columnWidths.Length; i++)
{
if (_columnWidths[i] > largeWidth + 0.1)
{
secLargeWidth = largeWidth;
largeWidth = _columnWidths[i];
nonMaxedColumns = 1;
}
else if (_columnWidths[i] > largeWidth - 0.1)
{
nonMaxedColumns++;
}
}
double decrease = secLargeWidth > 0 ? largeWidth - secLargeWidth : (widthSum - maxWidth) / _columnWidths.Length;
if (decrease * nonMaxedColumns > widthSum - maxWidth)
decrease = (widthSum - maxWidth) / nonMaxedColumns;
for (int i = 0; i < _columnWidths.Length; i++)
if (_columnWidths[i] > largeWidth - 0.1)
_columnWidths[i] -= decrease;
widthSum = GetWidthSum();
}
}
else
{
// spread extra width to columns that didn't reached max width where trying to spread it between all columns
for (int a = 0; a < 15 && maxWidth > widthSum + 0.1; a++) // limit iteration so bug won't create infinite loop
{
int nonMaxedColumns = 0;
for (int i = 0; i < _columnWidths.Length; i++)
if (_columnWidths[i] + 1 < maxFullWidths[i])
nonMaxedColumns++;
if (nonMaxedColumns == 0)
nonMaxedColumns = _columnWidths.Length;
bool hit = false;
double minIncrement = (maxWidth - widthSum) / nonMaxedColumns;
for (int i = 0; i < _columnWidths.Length; i++)
{
if (_columnWidths[i] + 0.1 < maxFullWidths[i])
{
minIncrement = Math.Min(minIncrement, maxFullWidths[i] - _columnWidths[i]);
hit = true;
}
}
for (int i = 0; i < _columnWidths.Length; i++)
if (!hit || _columnWidths[i] + 1 < maxFullWidths[i])
_columnWidths[i] += minIncrement;
widthSum = GetWidthSum();
}
}
}
}
}
/// <summary>
/// Check for minimum sizes (increment widths if necessary)
/// </summary>
private void EnforceMinimumSize()
{
foreach (CssBox row in _allRows)
{
foreach (CssBox cell in row.Boxes)
{
int colspan = GetColSpan(cell);
int col = GetCellRealColumnIndex(row, cell);
int affectcol = col + colspan - 1;
if (_columnWidths.Length > col && _columnWidths[col] < GetColumnMinWidths()[col])
{
double diff = GetColumnMinWidths()[col] - _columnWidths[col];
_columnWidths[affectcol] = GetColumnMinWidths()[affectcol];
if (col < _columnWidths.Length - 1)
{
_columnWidths[col + 1] -= diff;
}
}
}
}
}
/// <summary>
/// Layout the cells by the calculated table layout
/// </summary>
/// <param name="g"></param>
private void LayoutCells(RGraphics g)
{
double startx = Math.Max(_tableBox.ClientLeft + GetHorizontalSpacing(), 0);
double starty = Math.Max(_tableBox.ClientTop + GetVerticalSpacing(), 0);
double cury = starty;
double maxRight = startx;
double maxBottom = 0f;
double maxHeaderBottom = 0f;
int currentrow = 0;
// change start X by if the table should align to center or right
if (_tableBox.TextAlign == CssConstants.Center || _tableBox.TextAlign == CssConstants.Right)
{
double maxRightCalc = GetWidthSum();
startx = _tableBox.TextAlign == CssConstants.Right
? GetAvailableTableWidth() - maxRightCalc
: startx + (GetAvailableTableWidth() - maxRightCalc) / 2;
_tableBox.Location = new RPoint(startx - _tableBox.ActualBorderLeftWidth - _tableBox.ActualPaddingLeft - GetHorizontalSpacing(), _tableBox.Location.Y);
}
for (int i = 0; i < _allRows.Count; i++)
{
var row = _allRows[i];
double curx = startx;
int curCol = 0;
bool breakPage = false;
for (int j = 0; j < row.Boxes.Count; j++)
{
CssBox cell = row.Boxes[j];
var isHeader = cell.HtmlTag != null && cell.HtmlTag.Name == "th";
if (curCol >= _columnWidths.Length)
break;
int rowspan = GetRowSpan(cell);
var columnIndex = GetCellRealColumnIndex(row, cell);
double width = GetCellWidth(columnIndex, cell);
cell.Location = new RPoint(curx, cury);
cell.Size = new RSize(width, 0f);
cell.PerformLayout(g); //That will automatically set the bottom of the cell
//Alter max bottom only if row is cell's row + cell's rowspan - 1
CssSpacingBox sb = cell as CssSpacingBox;
if (sb != null)
{
if (sb.EndRow == currentrow)
{
maxBottom = Math.Max(maxBottom, sb.ExtendedBox.ActualBottom);
}
}
else if (rowspan == 1)
{
maxBottom = Math.Max(maxBottom, cell.ActualBottom);
}
maxRight = Math.Max(maxRight, cell.ActualRight);
curCol++;
curx = cell.ActualRight + GetHorizontalSpacing();
if (isHeader)
{
maxHeaderBottom = maxBottom;
}
}
foreach (CssBox cell in row.Boxes)
{
CssSpacingBox spacer = cell as CssSpacingBox;
if (spacer == null && GetRowSpan(cell) == 1)
{
cell.ActualBottom = maxBottom;
CssLayoutEngine.ApplyCellVerticalAlignment(g, cell);
}
else if (spacer != null && spacer.EndRow == currentrow)
{
spacer.ExtendedBox.ActualBottom = maxBottom;
CssLayoutEngine.ApplyCellVerticalAlignment(g, spacer.ExtendedBox);
}
// If one cell crosses page borders then don't need to check other cells in the row
if (_tableBox.PageBreakInside == CssConstants.Avoid)
{
breakPage = cell.BreakPage();
if (breakPage)
{
cury = cell.Location.Y;
break;
}
}
}
if (breakPage) // go back to move the whole row to the next page
{
if (i == 1) // do not leave single row in previous page
i = -1; // Start layout from the first row on new page
else
i--;
maxBottom = 0;
continue;
}
cury = maxBottom + GetVerticalSpacing();
currentrow++;
}
maxRight = Math.Max(maxRight, _tableBox.Location.X + _tableBox.ActualWidth);
_tableBox.ActualRight = maxRight + GetHorizontalSpacing() + _tableBox.ActualBorderRightWidth;
_tableBox.ActualBottom = Math.Max(maxBottom, starty) + GetVerticalSpacing() + _tableBox.ActualBorderBottomWidth;
if (_headerBox != null)
{
_headerBox.Location = _tableBox.Location;
_headerBox.ActualRight = maxRight + GetHorizontalSpacing() + _headerBox.ActualBorderRightWidth;
_headerBox.ActualBottom = Math.Max(maxHeaderBottom, starty) + GetVerticalSpacing() + _headerBox.ActualBorderBottomWidth;
}
}
/// <summary>
/// Gets the spanned width of a cell (With of all columns it spans minus one).
/// </summary>
private double GetSpannedMinWidth(CssBox row, CssBox cell, int realcolindex, int colspan)
{
double w = 0f;
for (int i = realcolindex; i < row.Boxes.Count || i < realcolindex + colspan - 1; i++)
{
if (i < GetColumnMinWidths().Length)
w += GetColumnMinWidths()[i];
}
return w;
}
/// <summary>
/// Gets the cell column index checking its position and other cells colspans
/// </summary>
/// <param name="row"></param>
/// <param name="cell"></param>
/// <returns></returns>
private static int GetCellRealColumnIndex(CssBox row, CssBox cell)
{
int i = 0;
foreach (CssBox b in row.Boxes)
{
if (b.Equals(cell))
break;
i += GetColSpan(b);
}
return i;
}
/// <summary>
/// Gets the cells width, taking colspan and being in the specified column
/// </summary>
/// <param name="column"></param>
/// <param name="b"></param>
/// <returns></returns>
private double GetCellWidth(int column, CssBox b)
{
double colspan = Convert.ToSingle(GetColSpan(b));
double sum = 0f;
for (int i = column; i < column + colspan; i++)
{
if (column >= _columnWidths.Length)
break;
if (_columnWidths.Length <= i)
break;
sum += _columnWidths[i];
}
sum += (colspan - 1) * GetHorizontalSpacing();
return sum; // -b.ActualBorderLeftWidth - b.ActualBorderRightWidth - b.ActualPaddingRight - b.ActualPaddingLeft;
}
/// <summary>
/// Gets the colspan of the specified box
/// </summary>
/// <param name="b"></param>
private static int GetColSpan(CssBox b)
{
string att = b.GetAttribute("colspan", "1");
int colspan;
if (!int.TryParse(att, out colspan))
{
return 1;
}
return colspan;
}
/// <summary>
/// Gets the rowspan of the specified box
/// </summary>
/// <param name="b"></param>
private static int GetRowSpan(CssBox b)
{
string att = b.GetAttribute("rowspan", "1");
int rowspan;
if (!int.TryParse(att, out rowspan))
{
return 1;
}
return rowspan;
}
/// <summary>
/// Recursively measures words inside the box
/// </summary>
/// <param name="box">the box to measure</param>
/// <param name="g">Device to use</param>
private static void MeasureWords(CssBox box, RGraphics g)
{
if (box != null)
{
foreach (var childBox in box.Boxes)
{
childBox.MeasureWordsSize(g);
MeasureWords(childBox, g);
}
}
}
/// <summary>
/// Tells if the columns widths can be reduced,
/// by checking the minimum widths of all cells
/// </summary>
/// <returns></returns>
private bool CanReduceWidth()
{
for (int i = 0; i < _columnWidths.Length; i++)
{
if (CanReduceWidth(i))
{
return true;
}
}
return false;
}
/// <summary>
/// Tells if the specified column can be reduced,
/// by checking its minimum width
/// </summary>
/// <param name="columnIndex"></param>
/// <returns></returns>
private bool CanReduceWidth(int columnIndex)
{
if (_columnWidths.Length >= columnIndex || GetColumnMinWidths().Length >= columnIndex)
return false;
return _columnWidths[columnIndex] > GetColumnMinWidths()[columnIndex];
}
/// <summary>
/// Gets the available width for the whole table.
/// It also sets the value of WidthSpecified
/// </summary>
/// <returns></returns>
/// <remarks>
/// The table's width can be larger than the result of this method, because of the minimum
/// size that individual boxes.
/// </remarks>
private double GetAvailableTableWidth()
{
CssLength tblen = new CssLength(_tableBox.Width);
if (tblen.Number > 0)
{
_widthSpecified = true;
return CssValueParser.ParseLength(_tableBox.Width, _tableBox.ParentBox.AvailableWidth, _tableBox);
}
else
{
return _tableBox.ParentBox.AvailableWidth;
}
}
/// <summary>
/// Gets the available width for the whole table.
/// It also sets the value of WidthSpecified
/// </summary>
/// <returns></returns>
/// <remarks>
/// The table's width can be larger than the result of this method, because of the minimum
/// size that individual boxes.
/// </remarks>
private double GetMaxTableWidth()
{
var tblen = new CssLength(_tableBox.MaxWidth);
if (tblen.Number > 0)
{
_widthSpecified = true;
return CssValueParser.ParseLength(_tableBox.MaxWidth, _tableBox.ParentBox.AvailableWidth, _tableBox);
}
else
{
return 9999f;
}
}
/// <summary>
/// Calculate the min and max width for each column of the table by the content in all rows.<br/>
/// the min width possible without clipping content<br/>
/// the max width the cell content can take without wrapping<br/>
/// </summary>
/// <param name="onlyNans">if to measure only columns that have no calculated width</param>
/// <param name="minFullWidths">return the min width for each column - the min width possible without clipping content</param>
/// <param name="maxFullWidths">return the max width for each column - the max width the cell content can take without wrapping</param>
private void GetColumnsMinMaxWidthByContent(bool onlyNans, out double[] minFullWidths, out double[] maxFullWidths)
{
maxFullWidths = new double[_columnWidths.Length];
minFullWidths = new double[_columnWidths.Length];
foreach (CssBox row in _allRows)
{
for (int i = 0; i < row.Boxes.Count; i++)
{
int col = GetCellRealColumnIndex(row, row.Boxes[i]);
col = _columnWidths.Length > col ? col : _columnWidths.Length - 1;
if ((!onlyNans || double.IsNaN(_columnWidths[col])) && i < row.Boxes.Count)
{
double minWidth, maxWidth;
row.Boxes[i].GetMinMaxWidth(out minWidth, out maxWidth);
var colSpan = GetColSpan(row.Boxes[i]);
minWidth = minWidth / colSpan;
maxWidth = maxWidth / colSpan;
for (int j = 0; j < colSpan; j++)
{
minFullWidths[col + j] = Math.Max(minFullWidths[col + j], minWidth);
maxFullWidths[col + j] = Math.Max(maxFullWidths[col + j], maxWidth);
}
}
}
}
}
/// <summary>
/// Gets the width available for cells
/// </summary>
/// <returns></returns>
/// <remarks>
/// It takes away the cell-spacing from <see cref="GetAvailableTableWidth"/>
/// </remarks>
private double GetAvailableCellWidth()
{
return GetAvailableTableWidth() - GetHorizontalSpacing() * (_columnCount + 1) - _tableBox.ActualBorderLeftWidth - _tableBox.ActualBorderRightWidth;
}
/// <summary>
/// Gets the current sum of column widths
/// </summary>
/// <returns></returns>
private double GetWidthSum()
{
double f = 0f;
foreach (double t in _columnWidths)
{
if (double.IsNaN(t))
throw new Exception("CssTable Algorithm error: There's a NaN in column widths");
else
f += t;
}
//Take cell-spacing
f += GetHorizontalSpacing() * (_columnWidths.Length + 1);
//Take table borders
f += _tableBox.ActualBorderLeftWidth + _tableBox.ActualBorderRightWidth;
return f;
}
/// <summary>
/// Gets the span attribute of the tag of the specified box
/// </summary>
/// <param name="b"></param>
private static int GetSpan(CssBox b)
{
double f = CssValueParser.ParseNumber(b.GetAttribute("span"), 1);
return Math.Max(1, Convert.ToInt32(f));
}