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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
|
/*
* This file is part of the HTML rendering engine for KDE.
*
* Copyright (C) 2002 Lars Knoll (knoll@kde.org)
* (C) 2002 Dirk Mueller (mueller@kde.org)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*
*/
#include "table_layout.h"
#include "render_table.h"
#include <kglobal.h>
using namespace khtml;
// #define DEBUG_LAYOUT
/*
The text below is from the CSS 2.1 specs.
Fixed table tqlayout
------------------
With this (fast) algorithm, the horizontal tqlayout of the table does
not depend on the contents of the cells; it only depends on the
table's width, the width of the columns, and borders or cell
spacing.
The table's width may be specified explicitly with the 'width'
property. A value of 'auto' (for both 'display: table' and 'display:
inline-table') means use the automatic table tqlayout algorithm.
In the fixed table tqlayout algorithm, the width of each column is
determined as follows:
1. A column element with a value other than 'auto' for the 'width'
property sets the width for that column.
2. Otherwise, a cell in the first row with a value other than
'auto' for the 'width' property sets the width for that column. If
the cell spans more than one column, the width is divided over the
columns.
3. Any remaining columns equally divide the remaining horizontal
table space (minus borders or cell spacing).
The width of the table is then the greater of the value of the
'width' property for the table element and the sum of the column
widths (plus cell spacing or borders). If the table is wider than
the columns, the extra space should be distributed over the columns.
In this manner, the user agent can begin to lay out the table once
the entire first row has been received. Cells in subsequent rows do
not affect column widths. Any cell that has content that overflows
uses the 'overflow' property to determine whether to clip the
overflow content.
_____________________________________________________
This is not quite true when comparing to IE. IE always honors
table-tqlayout:fixed and treats a variable table width as 100%. Makes
a lot of sense, and is implemented here the same way.
*/
FixedTableLayout::FixedTableLayout( RenderTable *table )
: TableLayout ( table )
{
}
FixedTableLayout::~FixedTableLayout()
{
}
int FixedTableLayout::calcWidthArray()
{
int usedWidth = 0;
// iterate over all <col> elements
RenderObject *child = table->firstChild();
int cCol = 0;
int nEffCols = table->numEffCols();
width.resize( nEffCols );
width.fill( Length( Variable ) );
#ifdef DEBUG_LAYOUT
qDebug("FixedTableLayout::calcWidthArray()" );
qDebug(" col elements:");
#endif
Length grpWidth;
while ( child ) {
if ( child->isTableCol() ) {
RenderTableCol *col = static_cast<RenderTableCol *>(child);
int span = col->span();
if ( col->firstChild() ) {
grpWidth = col->style()->width();
} else {
Length w = col->style()->width();
if ( w.isVariable() )
w = grpWidth;
int effWidth = 0;
if ( w.isFixed() && w.value() > 0 ) {
effWidth = w.value();
effWidth = KMIN( effWidth, 32760 );
}
#ifdef DEBUG_LAYOUT
qDebug(" col element: effCol=%d, span=%d: %d w=%d type=%d",
cCol, span, effWidth, w.value(), w.type());
#endif
int usedSpan = 0;
int i = 0;
while ( usedSpan < span ) {
if( cCol + i >= nEffCols ) {
table->appendColumn( span - usedSpan );
nEffCols++;
width.resize( nEffCols );
width[nEffCols-1] = Length();
}
int eSpan = table->spanOfEffCol( cCol+i );
if ( (w.isFixed() || w.isPercent()) && w.value() > 0 ) {
width[cCol+i] = Length( w.value() * eSpan, w.type() );
usedWidth += effWidth * eSpan;
#ifdef DEBUG_LAYOUT
qDebug(" setting effCol %d (span=%d) to width %d(type=%d)",
cCol+i, eSpan, width[cCol+i].value(), width[cCol+i].type() );
#endif
}
usedSpan += eSpan;
i++;
}
cCol += i;
}
} else {
break;
}
RenderObject *next = child->firstChild();
if ( !next )
next = child->nextSibling();
if ( !next && child->parent()->isTableCol() ) {
next = child->parent()->nextSibling();
grpWidth = Length();
}
child = next;
}
#ifdef DEBUG_LAYOUT
qDebug(" first row:");
#endif
// iterate over the first row in case some are unspecified.
RenderTableSection *section = table->head;
if ( !section )
section = table->firstBody;
if ( !section )
section = table->foot;
if ( section && section->firstChild() ) {
cCol = 0;
// get the first cell in the first row
child = section->firstChild()->firstChild();
while ( child ) {
if ( child->isTableCell() ) {
RenderTableCell *cell = static_cast<RenderTableCell *>(child);
Length w = cell->styleOrColWidth();
int span = cell->colSpan();
int effWidth = 0;
if ( (w.isFixed() || w.isPercent()) && w.value() > 0 ) {
effWidth = w.value();
effWidth = kMin( effWidth, 32760 );
}
#ifdef DEBUG_LAYOUT
qDebug(" table cell: effCol=%d, span=%d: %d", cCol, span, effWidth);
#endif
int usedSpan = 0;
int i = 0;
while ( usedSpan < span ) {
Q_ASSERT( cCol + i < nEffCols );
int eSpan = table->spanOfEffCol( cCol+i );
// only set if no col element has already set it.
if ( width[cCol+i].isVariable() && !w.isVariable() ) {
width[cCol+i] = Length( w.value()*eSpan, w.type() );
usedWidth += effWidth*eSpan;
#ifdef DEBUG_LAYOUT
qDebug(" setting effCol %d (span=%d) to width %d(type=%d)",
cCol+i, eSpan, width[cCol+i].value(), width[cCol+i].type() );
#endif
}
#ifdef DEBUG_LAYOUT
else {
qDebug(" width of col %d already defined (span=%d)", cCol, table->spanOfEffCol( cCol ) );
}
#endif
usedSpan += eSpan;
i++;
}
cCol += i;
} else {
Q_ASSERT( false );
}
child = child->nextSibling();
}
}
return usedWidth;
}
void FixedTableLayout::calcMinMaxWidth()
{
// we might want to wait until we have all of the first row before
// tqlayouting for the first time.
// only need to calculate the minimum width as the sum of the
// cols/cells with a fixed width.
//
// The maximum width is kMax( minWidth, tableWidth ) if table
// width is fixed. If table width is percent, we set maxWidth to
// unlimited.
int bs = table->bordersPaddingAndSpacing();
int tableWidth = 0;
if (table->style()->width().isFixed()) {
tableWidth = table->calcBoxWidth(table->style()->width().value());
}
int mw = calcWidthArray() + bs;
table->m_minWidth = kMin( kMax( mw, tableWidth ), 0x7fff );
table->m_maxWidth = table->m_minWidth;
if ( !tableWidth ) {
bool haveNonFixed = false;
for ( unsigned int i = 0; i < width.size(); i++ ) {
if ( !width[i].isFixed() ) {
haveNonFixed = true;
break;
}
}
if ( haveNonFixed )
table->m_maxWidth = 0x7fff;
}
#ifdef DEBUG_LAYOUT
qDebug("FixedTableLayout::calcMinMaxWidth: minWidth=%d, maxWidth=%d", table->m_minWidth, table->m_maxWidth );
#endif
}
void FixedTableLayout::tqlayout()
{
int tableWidth = table->width() - table->bordersPaddingAndSpacing();
int available = tableWidth;
int nEffCols = table->numEffCols();
#ifdef DEBUG_LAYOUT
qDebug("FixedTableLayout::tqlayout: tableWidth=%d, numEffCols=%d", tableWidth, nEffCols);
#endif
TQMemArray<int> calcWidth;
calcWidth.resize( nEffCols );
calcWidth.fill( -1 );
// first assign fixed width
for ( int i = 0; i < nEffCols; i++ ) {
if ( width[i].isFixed() ) {
calcWidth[i] = width[i].value();
available -= width[i].value();
}
}
// assign percent width
if ( available > 0 ) {
int totalPercent = 0;
for ( int i = 0; i < nEffCols; i++ )
if ( width[i].isPercent() )
totalPercent += width[i].value();
// calculate how much to distribute to percent cells.
int base = tableWidth * totalPercent / 100;
if ( base > available )
base = available;
#ifdef DEBUG_LAYOUT
qDebug("FixedTableLayout::tqlayout: assigning percent width, base=%d, totalPercent=%d", base, totalPercent);
#endif
for ( int i = 0; available > 0 && i < nEffCols; i++ ) {
if ( width[i].isPercent() ) {
// totalPercent may be 0 below if all %-width specifed are 0%. (#172557)
int w = totalPercent ? base * width[i].value() / totalPercent : 0;
available -= w;
calcWidth[i] = w;
}
}
}
// assign variable width
if ( available > 0 ) {
int totalVariable = 0;
for ( int i = 0; i < nEffCols; i++ )
if ( width[i].isVariable() )
totalVariable++;
for ( int i = 0; available > 0 && i < nEffCols; i++ ) {
if ( width[i].isVariable() ) {
// totalVariable may be 0 below if all the variable widths specified are 0.
int w = totalVariable ? available / totalVariable : 0;
available -= w;
calcWidth[i] = w;
totalVariable--;
}
}
}
for ( int i = 0; i < nEffCols; i++ )
if ( calcWidth[i] < 0 )
calcWidth[i] = 0; // IE gives min 1 px...
// spread extra space over columns
if ( available > 0 ) {
int total = nEffCols;
// still have some width to spread
int i = nEffCols;
while ( i-- ) {
int w = available / total;
available -= w;
total--;
calcWidth[i] += w;
}
}
int pos = 0;
int hspacing = table->borderHSpacing();
for ( int i = 0; i < nEffCols; i++ ) {
#ifdef DEBUG_LAYOUT
qDebug("col %d: %d (width %d)", i, pos, calcWidth[i] );
#endif
table->columnPos[i] = pos;
pos += calcWidth[i] + hspacing;
}
table->columnPos[table->columnPos.size()-1] = pos;
}
// -------------------------------------------------------------------------
// -------------------------------------------------------------------------
AutoTableLayout::AutoTableLayout( RenderTable* table )
: TableLayout( table )
{
percentagesDirty = true;
effWidthDirty = true;
total_percent = 0;
hasPercent = false;
}
AutoTableLayout::~AutoTableLayout()
{
}
/* recalculates the full structure needed to do tqlayouting and minmax calculations.
This is usually calculated on the fly, but needs to be done fully when table cells change
dynamically
*/
void AutoTableLayout::recalcColumn( int effCol )
{
Layout &l = tqlayoutStruct[effCol];
RenderObject *child = table->firstChild();
// first we iterate over all rows.
RenderTableCell *fixedContributor = 0;
RenderTableCell *maxContributor = 0;
while ( child ) {
if ( child->isTableSection() ) {
RenderTableSection *section = static_cast<RenderTableSection *>(child);
int numRows = section->numRows();
RenderTableCell *last = 0;
for ( int i = 0; i < numRows; i++ ) {
RenderTableCell *cell = section->cellAt( i, effCol );
if ( cell == (RenderTableCell *)-1 )
continue;
if ( cell && cell->colSpan() == 1 ) {
// A cell originates in this column. Ensure we have
// a min/max width of at least 1px for this column now.
l.minWidth = kMax(int( l.minWidth ), 1);
l.maxWidth = kMax(int( l.maxWidth ), 1);
if ( !cell->minMaxKnown() )
cell->calcMinMaxWidth();
if ( cell->minWidth() > l.minWidth )
l.minWidth = cell->minWidth();
if ( cell->maxWidth() > l.maxWidth ) {
l.maxWidth = cell->maxWidth();
maxContributor = cell;
}
Length w = cell->styleOrColWidth();
w.l.value = kMin( 32767, kMax( 0, w.value() ) );
switch( w.type() ) {
case Fixed:
// ignore width=0
if ( w.value() > 0 && !l.width.isPercent() ) {
int wval = cell->calcBoxWidth(w.value());
if ( l.width.isFixed() ) {
// Nav/IE weirdness
if ((wval > l.width.value()) ||
((l.width.value() == wval) && (maxContributor == cell))) {
l.width.l.value = wval;
fixedContributor = cell;
}
} else {
l.width = Length( wval, Fixed );
fixedContributor = cell;
}
}
break;
case Percent:
hasPercent = true;
if ( w.value() > 0 && (!l.width.isPercent() || w.value() > l.width.value() ) )
l.width = w;
break;
case Relative:
if ( w.isVariable() || (w.isRelative() && w.value() > l.width.value() ) )
l.width = w;
default:
break;
}
} else {
if ( cell && (!effCol || section->cellAt( i, effCol-1 ) != cell) ) {
// This spanning cell originates in this column. Ensure we have
// a min/max width of at least 1px for this column now.
l.minWidth = kMax(int( l.minWidth ), 1);
l.maxWidth = kMax(int( l.maxWidth ), 1);
insertSpanCell( cell );
}
last = cell;
}
}
}
child = child->nextSibling();
}
// Nav/IE weirdness
if ( l.width.isFixed() ) {
if ( table->style()->htmlHacks()
&& (l.maxWidth > l.width.value()) && (fixedContributor != maxContributor)) {
l.width = Length();
fixedContributor = 0;
}
}
l.maxWidth = kMax(l.maxWidth, int(l.minWidth));
#ifdef DEBUG_LAYOUT
qDebug("col %d, final min=%d, max=%d, width=%d(%d)", effCol, l.minWidth, l.maxWidth, l.width.value(), l.width.type() );
#endif
// ### we need to add col elements aswell
}
void AutoTableLayout::fullRecalc()
{
percentagesDirty = true;
hasPercent = false;
effWidthDirty = true;
int nEffCols = table->numEffCols();
tqlayoutStruct.resize( nEffCols );
tqlayoutStruct.fill( Layout() );
spanCells.fill( 0 );
RenderObject *child = table->firstChild();
Length grpWidth;
int cCol = 0;
while ( child ) {
if ( child->isTableCol() ) {
RenderTableCol *col = static_cast<RenderTableCol *>(child);
int span = col->span();
if ( col->firstChild() ) {
grpWidth = col->style()->width();
} else {
Length w = col->style()->width();
if ( w.isVariable() )
w = grpWidth;
if ( (w.isFixed() && w.value() == 0) ||
(w.isPercent() && w.value() == 0) )
w = Length();
int cEffCol = table->colToEffCol( cCol );
#ifdef DEBUG_LAYOUT
qDebug(" col element %d (eff=%d): Length=%d(%d), span=%d, effColSpan=%d", cCol, cEffCol, w.value(), w.type(), span, table->spanOfEffCol(cEffCol ) );
#endif
if ( !w.isVariable() && span == 1 && cEffCol < nEffCols ) {
if ( table->spanOfEffCol( cEffCol ) == 1 ) {
tqlayoutStruct[cEffCol].width = w;
if (w.isFixed() && tqlayoutStruct[cEffCol].maxWidth < w.value())
tqlayoutStruct[cEffCol].maxWidth = w.value();
}
}
cCol += span;
}
} else {
break;
}
RenderObject *next = child->firstChild();
if ( !next )
next = child->nextSibling();
if ( !next && child->parent()->isTableCol() ) {
next = child->parent()->nextSibling();
grpWidth = Length();
}
child = next;
}
for ( int i = 0; i < nEffCols; i++ )
recalcColumn( i );
}
static bool shouldScaleColumns(RenderTable* table)
{
// A special case. If this table is not fixed width and contained inside
// a cell, then don't bloat the maxwidth by examining percentage growth.
bool scale = true;
while (table) {
Length tw = table->style()->width();
if ((tw.isVariable() || tw.isPercent()) && !table->isPositioned()) {
RenderBlock* cb = table->containingBlock();
while (cb && !cb->isCanvas() && !cb->isTableCell() &&
cb->style()->width().isVariable() && !cb->isPositioned())
cb = cb->containingBlock();
table = 0;
if (cb && cb->isTableCell() &&
(cb->style()->width().isVariable() || cb->style()->width().isPercent())) {
if (tw.isPercent())
scale = false;
else {
RenderTableCell* cell = static_cast<RenderTableCell*>(cb);
if (cell->colSpan() > 1 || cell->table()->style()->width().isVariable())
scale = false;
else
table = cell->table();
}
}
}
else
table = 0;
}
return scale;
}
void AutoTableLayout::calcMinMaxWidth()
{
#ifdef DEBUG_LAYOUT
qDebug("AutoTableLayout::calcMinMaxWidth");
#endif
fullRecalc();
int spanMaxWidth = calcEffectiveWidth();
int minWidth = 0;
int maxWidth = 0;
int maxPercent = 0;
int maxNonPercent = 0;
int remainingPercent = 100;
for ( unsigned int i = 0; i < tqlayoutStruct.size(); i++ ) {
minWidth += tqlayoutStruct[i].effMinWidth;
maxWidth += tqlayoutStruct[i].effMaxWidth;
if ( tqlayoutStruct[i].effWidth.isPercent() ) {
int percent = kMin(tqlayoutStruct[i].effWidth.value(), remainingPercent);
int pw = ( tqlayoutStruct[i].effMaxWidth * 100) / kMax(percent, 1);
remainingPercent -= percent;
maxPercent = kMax( pw, maxPercent );
} else {
maxNonPercent += tqlayoutStruct[i].effMaxWidth;
}
}
if (shouldScaleColumns(table)) {
maxNonPercent = (maxNonPercent * 100 + 50) / kMax(remainingPercent, 1);
maxWidth = kMax( maxNonPercent, maxWidth );
maxWidth = kMax( maxWidth, maxPercent );
}
maxWidth = kMax( maxWidth, spanMaxWidth );
int bs = table->bordersPaddingAndSpacing();
minWidth += bs;
maxWidth += bs;
Length tw = table->style()->width();
if ( tw.isFixed() && tw.value() > 0 ) {
int width = table->calcBoxWidth(tw.value());
minWidth = kMax( minWidth, width );
maxWidth = minWidth;
}
table->m_maxWidth = kMin(maxWidth, 0x7fff);
table->m_minWidth = kMin(minWidth, 0x7fff);
#ifdef DEBUG_LAYOUT
qDebug(" minWidth=%d, maxWidth=%d", table->m_minWidth, table->m_maxWidth );
#endif
}
/*
This method takes care of colspans.
effWidth is the same as width for cells without colspans. If we have colspans, they get modified.
*/
int AutoTableLayout::calcEffectiveWidth()
{
int tMaxWidth = 0;
unsigned int nEffCols = tqlayoutStruct.size();
int hspacing = table->borderHSpacing();
#ifdef DEBUG_LAYOUT
qDebug("AutoTableLayout::calcEffectiveWidth for %d cols", nEffCols );
#endif
for ( unsigned int i = 0; i < nEffCols; i++ ) {
tqlayoutStruct[i].effWidth = tqlayoutStruct[i].width;
tqlayoutStruct[i].effMinWidth = tqlayoutStruct[i].minWidth;
tqlayoutStruct[i].effMaxWidth = tqlayoutStruct[i].maxWidth;
}
for ( unsigned int i = 0; i < spanCells.size(); i++ ) {
RenderTableCell *cell = spanCells[i];
if ( !cell || cell == (RenderTableCell *)-1 )
break;
int span = cell->colSpan();
Length w = cell->styleOrColWidth();
if ( !w.isRelative() && w.value() == 0 )
w = Length(); // make it Variable
int col = table->colToEffCol( cell->col() );
unsigned int lastCol = col;
int cMinWidth = cell->minWidth() + hspacing;
int cMaxWidth = cell->maxWidth() + hspacing;
int totalPercent = 0;
int minWidth = 0;
int maxWidth = 0;
bool allColsArePercent = true;
bool allColsAreFixed = true;
bool haveVariable = false;
int fixedWidth = 0;
#ifdef DEBUG_LAYOUT
int cSpan = span;
#endif
while ( lastCol < nEffCols && span > 0 ) {
switch( tqlayoutStruct[lastCol].width.type() ) {
case Percent:
totalPercent += tqlayoutStruct[lastCol].width.value();
allColsAreFixed = false;
break;
case Fixed:
if (tqlayoutStruct[lastCol].width.value() > 0) {
fixedWidth += tqlayoutStruct[lastCol].width.value();
allColsArePercent = false;
// IE resets effWidth to Variable here, but this breaks the konqueror about page and seems to be some bad
// legacy behavior anyway. mozilla doesn't do this so I decided we don't either.
break;
}
// fall through
case Variable:
haveVariable = true;
// fall through
default:
// If the column is a percentage width, do not let the spanning cell overwrite the
// width value. This caused a mis-rendering on amazon.com.
// Sample snippet:
// <table border=2 width=100%><
// <tr><td>1</td><td colspan=2>2-3</tr>
// <tr><td>1</td><td colspan=2 width=100%>2-3</td></tr>
// </table>
if (!tqlayoutStruct[lastCol].effWidth.isPercent()) {
tqlayoutStruct[lastCol].effWidth = Length();
allColsArePercent = false;
}
else
totalPercent += tqlayoutStruct[lastCol].effWidth.value();
allColsAreFixed = false;
}
span -= table->spanOfEffCol( lastCol );
minWidth += tqlayoutStruct[lastCol].effMinWidth;
maxWidth += tqlayoutStruct[lastCol].effMaxWidth;
lastCol++;
cMinWidth -= hspacing;
cMaxWidth -= hspacing;
}
#ifdef DEBUG_LAYOUT
qDebug(" colspan cell %p at effCol %d, span %d, type %d, value %d cmin=%d min=%d fixedwidth=%d", cell, col, cSpan, w.type(), w.value(), cMinWidth, minWidth, fixedWidth );
#endif
// adjust table max width if needed
if ( w.isPercent() ) {
if ( totalPercent > w.value() || allColsArePercent ) {
// can't satify this condition, treat as variable
w = Length();
} else {
int spanMax = kMax( maxWidth, cMaxWidth );
#ifdef DEBUG_LAYOUT
qDebug(" adjusting tMaxWidth (%d): spanMax=%d, value=%d, totalPercent=%d", tMaxWidth, spanMax, w.value(), totalPercent );
#endif
tMaxWidth = kMax( tMaxWidth, spanMax * 100 / w.value() );
// all non percent columns in the span get percent values to sum up correctly.
int percentMissing = w.value() - totalPercent;
int totalWidth = 0;
for ( unsigned int pos = col; pos < lastCol; pos++ ) {
if ( !(tqlayoutStruct[pos].width.isPercent() ) )
totalWidth += tqlayoutStruct[pos].effMaxWidth;
}
for ( unsigned int pos = col; pos < lastCol && totalWidth > 0; pos++ ) {
if ( !(tqlayoutStruct[pos].width.isPercent() ) ) {
int percent = percentMissing * tqlayoutStruct[pos].effMaxWidth / totalWidth;
#ifdef DEBUG_LAYOUT
qDebug(" col %d: setting percent value %d effMaxWidth=%d totalWidth=%d", pos, percent, tqlayoutStruct[pos].effMaxWidth, totalWidth );
#endif
totalWidth -= tqlayoutStruct[pos].effMaxWidth;
percentMissing -= percent;
if ( percent > 0 )
tqlayoutStruct[pos].effWidth = Length( percent, Percent );
else
tqlayoutStruct[pos].effWidth = Length();
}
}
}
}
// make sure minWidth and maxWidth of the spanning cell are honoured
if ( cMinWidth > minWidth ) {
if ( allColsAreFixed ) {
#ifdef DEBUG_LAYOUT
qDebug("extending minWidth of cols %d-%d to %dpx currentMin=%d accroding to fixed sum %d", col, lastCol-1, cMinWidth, minWidth, fixedWidth );
#endif
for ( unsigned int pos = col; fixedWidth > 0 && pos < lastCol; pos++ ) {
int w = kMax( int( tqlayoutStruct[pos].effMinWidth ), cMinWidth * tqlayoutStruct[pos].width.value() / fixedWidth );
#ifdef DEBUG_LAYOUT
qDebug(" col %d: min=%d, effMin=%d, new=%d", pos, tqlayoutStruct[pos].effMinWidth, tqlayoutStruct[pos].effMinWidth, w );
#endif
fixedWidth -= tqlayoutStruct[pos].width.value();
cMinWidth -= w;
tqlayoutStruct[pos].effMinWidth = w;
}
} else if ( allColsArePercent ) {
int maxw = maxWidth;
int minw = minWidth;
int cminw = cMinWidth;
for ( unsigned int pos = col; maxw > 0 && pos < lastCol; pos++ ) {
if ( tqlayoutStruct[pos].effWidth.isPercent() && tqlayoutStruct[pos].effWidth.value()>0 && fixedWidth <= cMinWidth) {
int w = tqlayoutStruct[pos].effMinWidth;
w = kMax( w, cminw*tqlayoutStruct[pos].effWidth.value()/totalPercent );
w = kMin(tqlayoutStruct[pos].effMinWidth+(cMinWidth-minw), w);
#ifdef DEBUG_LAYOUT
qDebug(" col %d: min=%d, effMin=%d, new=%d", pos, tqlayoutStruct[pos].effMinWidth, tqlayoutStruct[pos].effMinWidth, w );
#endif
maxw -= tqlayoutStruct[pos].effMaxWidth;
minw -= tqlayoutStruct[pos].effMinWidth;
cMinWidth -= w;
tqlayoutStruct[pos].effMinWidth = w;
}
}
} else {
#ifdef DEBUG_LAYOUT
qDebug("extending minWidth of cols %d-%d to %dpx currentMin=%d", col, lastCol-1, cMinWidth, minWidth );
#endif
int maxw = maxWidth;
int minw = minWidth;
// Give min to variable first, to fixed second, and to others third.
for ( unsigned int pos = col; maxw > 0 && pos < lastCol; pos++ ) {
if ( tqlayoutStruct[pos].width.isFixed() && haveVariable && fixedWidth <= cMinWidth ) {
int w = kMax( int( tqlayoutStruct[pos].effMinWidth ), tqlayoutStruct[pos].width.value() );
fixedWidth -= tqlayoutStruct[pos].width.value();
minw -= tqlayoutStruct[pos].effMinWidth;
#ifdef DEBUG_LAYOUT
qDebug(" col %d: min=%d, effMin=%d, new=%d", pos, tqlayoutStruct[pos].effMinWidth, tqlayoutStruct[pos].effMinWidth, w );
#endif
maxw -= tqlayoutStruct[pos].effMaxWidth;
cMinWidth -= w;
tqlayoutStruct[pos].effMinWidth = w;
}
}
for ( unsigned int pos = col; maxw > 0 && pos < lastCol && minw < cMinWidth; pos++ ) {
if ( !(tqlayoutStruct[pos].width.isFixed() && haveVariable && fixedWidth <= cMinWidth) ) {
int w = kMax( int( tqlayoutStruct[pos].effMinWidth ), cMinWidth * tqlayoutStruct[pos].effMaxWidth / maxw );
w = kMin(tqlayoutStruct[pos].effMinWidth+(cMinWidth-minw), w);
#ifdef DEBUG_LAYOUT
qDebug(" col %d: min=%d, effMin=%d, new=%d", pos, tqlayoutStruct[pos].effMinWidth, tqlayoutStruct[pos].effMinWidth, w );
#endif
maxw -= tqlayoutStruct[pos].effMaxWidth;
minw -= tqlayoutStruct[pos].effMinWidth;
cMinWidth -= w;
tqlayoutStruct[pos].effMinWidth = w;
}
}
}
}
if ( !w.isPercent() ) {
if ( cMaxWidth > maxWidth ) {
#ifdef DEBUG_LAYOUT
qDebug("extending maxWidth of cols %d-%d to %dpx", col, lastCol-1, cMaxWidth );
#endif
for ( unsigned int pos = col; maxWidth > 0 && pos < lastCol; pos++ ) {
int w = kMax( int( tqlayoutStruct[pos].effMaxWidth ), cMaxWidth * tqlayoutStruct[pos].effMaxWidth / maxWidth );
#ifdef DEBUG_LAYOUT
qDebug(" col %d: max=%d, effMax=%d, new=%d", pos, tqlayoutStruct[pos].effMaxWidth, tqlayoutStruct[pos].effMaxWidth, w );
#endif
maxWidth -= tqlayoutStruct[pos].effMaxWidth;
cMaxWidth -= w;
tqlayoutStruct[pos].effMaxWidth = w;
}
}
} else {
for ( unsigned int pos = col; pos < lastCol; pos++ )
tqlayoutStruct[pos].maxWidth = kMax(tqlayoutStruct[pos].maxWidth, int(tqlayoutStruct[pos].minWidth) );
}
}
effWidthDirty = false;
// qDebug("calcEffectiveWidth: tMaxWidth=%d", tMaxWidth );
return tMaxWidth;
}
/* gets all cells that originate in a column and have a cellspan > 1
Sorts them by increasing cellspan
*/
void AutoTableLayout::insertSpanCell( RenderTableCell *cell )
{
if ( !cell || cell == (RenderTableCell *)-1 || cell->colSpan() == 1 )
return;
// qDebug("inserting span cell %p with span %d", cell, cell->colSpan() );
int size = spanCells.size();
if ( !size || spanCells[size-1] != 0 ) {
spanCells.resize( size + 10 );
for ( int i = 0; i < 10; i++ )
spanCells[size+i] = 0;
size += 10;
}
// add them in sort. This is a slow algorithm, and a binary search or a fast sorting after collection would be better
unsigned int pos = 0;
int span = cell->colSpan();
while ( pos < spanCells.size() && spanCells[pos] && span > spanCells[pos]->colSpan() )
pos++;
memmove( spanCells.data()+pos+1, spanCells.data()+pos, (size-pos-1)*sizeof( RenderTableCell * ) );
spanCells[pos] = cell;
}
void AutoTableLayout::tqlayout()
{
// table tqlayout based on the values collected in the tqlayout structure.
int tableWidth = table->width() - table->bordersPaddingAndSpacing();
int available = tableWidth;
int nEffCols = table->numEffCols();
if ( nEffCols != (int)tqlayoutStruct.size() ) {
qWarning("WARNING: nEffCols is not equal to tqlayoutstruct!" );
fullRecalc();
nEffCols = table->numEffCols();
}
#ifdef DEBUG_LAYOUT
qDebug("AutoTableLayout::tqlayout()");
#endif
if ( effWidthDirty )
calcEffectiveWidth();
#ifdef DEBUG_LAYOUT
qDebug(" tableWidth=%d, nEffCols=%d", tableWidth, nEffCols );
for ( int i = 0; i < nEffCols; i++ ) {
qDebug(" effcol %d is of type %d value %d, minWidth=%d, maxWidth=%d",
i, tqlayoutStruct[i].width.type(), tqlayoutStruct[i].width.value(),
tqlayoutStruct[i].minWidth, tqlayoutStruct[i].maxWidth );
qDebug(" effective: type %d value %d, minWidth=%d, maxWidth=%d",
tqlayoutStruct[i].effWidth.type(), tqlayoutStruct[i].effWidth.value(),
tqlayoutStruct[i].effMinWidth, tqlayoutStruct[i].effMaxWidth );
}
#endif
bool havePercent = false;
bool haveRelative = false;
int totalRelative = 0;
int numVariable = 0;
int numFixed = 0;
int totalVariable = 0;
int totalFixed = 0;
int totalPercent = 0;
int allocVariable = 0;
// fill up every cell with it's minWidth
for ( int i = 0; i < nEffCols; i++ ) {
int w = tqlayoutStruct[i].effMinWidth;
tqlayoutStruct[i].calcWidth = w;
available -= w;
Length& width = tqlayoutStruct[i].effWidth;
switch( width.type()) {
case Percent:
havePercent = true;
totalPercent += width.value();
break;
case Relative:
haveRelative = true;
totalRelative += width.value();
break;
case Fixed:
numFixed++;
totalFixed += tqlayoutStruct[i].effMaxWidth;
// fall through
break;
case Variable:
case Static:
numVariable++;
totalVariable += tqlayoutStruct[i].effMaxWidth;
allocVariable += w;
}
}
// allocate width to percent cols
if ( available > 0 && havePercent ) {
for ( int i = 0; i < nEffCols; i++ ) {
const Length &width = tqlayoutStruct[i].effWidth;
if ( width.isPercent() ) {
int w = kMax ( int( tqlayoutStruct[i].effMinWidth ), width.minWidth( tableWidth ) );
available += tqlayoutStruct[i].calcWidth - w;
tqlayoutStruct[i].calcWidth = w;
}
}
if ( totalPercent > 100 ) {
// remove overallocated space from the last columns
int excess = tableWidth*(totalPercent-100)/100;
for ( int i = nEffCols-1; i >= 0; i-- ) {
if ( tqlayoutStruct[i].effWidth.isPercent() ) {
int w = tqlayoutStruct[i].calcWidth;
int reduction = kMin( w, excess );
// the lines below might look inconsistent, but that's the way it's handled in mozilla
excess -= reduction;
int newWidth = kMax( int (tqlayoutStruct[i].effMinWidth), w - reduction );
available += w - newWidth;
tqlayoutStruct[i].calcWidth = newWidth;
//qDebug("col %d: reducing to %d px (reduction=%d)", i, newWidth, reduction );
}
}
}
}
#ifdef DEBUG_LAYOUT
qDebug("percent satisfied: available is %d", available);
#endif
// then allocate width to fixed cols
if ( available > 0 ) {
for ( int i = 0; i < nEffCols; ++i ) {
const Length &width = tqlayoutStruct[i].effWidth;
if ( width.isFixed() && width.value() > tqlayoutStruct[i].calcWidth ) {
available += tqlayoutStruct[i].calcWidth - width.value();
tqlayoutStruct[i].calcWidth = width.value();
}
}
}
#ifdef DEBUG_LAYOUT
qDebug("fixed satisfied: available is %d", available);
#endif
// now satisfy relative
if ( available > 0 ) {
for ( int i = 0; i < nEffCols; i++ ) {
const Length &width = tqlayoutStruct[i].effWidth;
if ( width.isRelative() && width.value() ) {
// width=0* gets effMinWidth.
int w = width.value()*tableWidth/totalRelative;
available += tqlayoutStruct[i].calcWidth - w;
tqlayoutStruct[i].calcWidth = w;
}
}
}
// now satisfy variable
if ( available > 0 && numVariable ) {
available += allocVariable; // this gets redistributed
//qDebug("redistributing %dpx to %d variable columns. totalVariable=%d", available, numVariable, totalVariable );
for ( int i = 0; i < nEffCols; i++ ) {
const Length &width = tqlayoutStruct[i].effWidth;
if ( width.isVariable() && totalVariable != 0 ) {
int w = kMax( int ( tqlayoutStruct[i].calcWidth ),
available * tqlayoutStruct[i].effMaxWidth / totalVariable );
available -= w;
totalVariable -= tqlayoutStruct[i].effMaxWidth;
tqlayoutStruct[i].calcWidth = w;
}
}
}
#ifdef DEBUG_LAYOUT
qDebug("variable satisfied: available is %d", available );
#endif
// spread over fixed colums
if ( available > 0 && numFixed) {
// still have some width to spread, distribute to fixed columns
for ( int i = 0; i < nEffCols; i++ ) {
const Length &width = tqlayoutStruct[i].effWidth;
if ( width.isFixed() ) {
int w = available * tqlayoutStruct[i].effMaxWidth / totalFixed;
available -= w;
totalFixed -= tqlayoutStruct[i].effMaxWidth;
tqlayoutStruct[i].calcWidth += w;
}
}
}
#ifdef DEBUG_LAYOUT
qDebug("after fixed distribution: available=%d", available );
#endif
// spread over percent colums
if ( available > 0 && hasPercent && totalPercent < 100) {
// still have some width to spread, distribute weighted to percent columns
for ( int i = 0; i < nEffCols; i++ ) {
const Length &width = tqlayoutStruct[i].effWidth;
if ( width.isPercent() ) {
int w = available * width.value() / totalPercent;
available -= w;
totalPercent -= width.value();
tqlayoutStruct[i].calcWidth += w;
if (!available || !totalPercent) break;
}
}
}
#ifdef DEBUG_LAYOUT
qDebug("after percent distribution: available=%d", available );
#endif
// spread over the rest
if ( available > 0 ) {
int total = nEffCols;
// still have some width to spread
int i = nEffCols;
while ( i-- ) {
int w = available / total;
available -= w;
total--;
tqlayoutStruct[i].calcWidth += w;
}
}
#ifdef DEBUG_LAYOUT
qDebug("after equal distribution: available=%d", available );
#endif
// if we have overallocated, reduce every cell according to the difference between desired width and minwidth
// this seems to produce to the pixel exaxt results with IE. Wonder is some of this also holds for width distributing.
if ( available < 0 ) {
// Need to reduce cells with the following prioritization:
// (1) Variable
// (2) Relative
// (3) Fixed
// (4) Percent
// This is basically the reverse of how we grew the cells.
if (available < 0) {
int mw = 0;
for ( int i = nEffCols-1; i >= 0; i-- ) {
Length &width = tqlayoutStruct[i].effWidth;
if (width.isVariable())
mw += tqlayoutStruct[i].calcWidth - tqlayoutStruct[i].effMinWidth;
}
for ( int i = nEffCols-1; i >= 0 && mw > 0; i-- ) {
Length &width = tqlayoutStruct[i].effWidth;
if (width.isVariable()) {
int minMaxDiff = tqlayoutStruct[i].calcWidth-tqlayoutStruct[i].effMinWidth;
int reduce = available * minMaxDiff / mw;
tqlayoutStruct[i].calcWidth += reduce;
available -= reduce;
mw -= minMaxDiff;
if ( available >= 0 )
break;
}
}
}
if (available < 0) {
int mw = 0;
for ( int i = nEffCols-1; i >= 0; i-- ) {
Length &width = tqlayoutStruct[i].effWidth;
if (width.isRelative())
mw += tqlayoutStruct[i].calcWidth - tqlayoutStruct[i].effMinWidth;
}
for ( int i = nEffCols-1; i >= 0 && mw > 0; i-- ) {
Length &width = tqlayoutStruct[i].effWidth;
if (width.isRelative()) {
int minMaxDiff = tqlayoutStruct[i].calcWidth-tqlayoutStruct[i].effMinWidth;
int reduce = available * minMaxDiff / mw;
tqlayoutStruct[i].calcWidth += reduce;
available -= reduce;
mw -= minMaxDiff;
if ( available >= 0 )
break;
}
}
}
if (available < 0) {
int mw = 0;
for ( int i = nEffCols-1; i >= 0; i-- ) {
Length &width = tqlayoutStruct[i].effWidth;
if (width.isFixed())
mw += tqlayoutStruct[i].calcWidth - tqlayoutStruct[i].effMinWidth;
}
for ( int i = nEffCols-1; i >= 0 && mw > 0; i-- ) {
Length &width = tqlayoutStruct[i].effWidth;
if (width.isFixed()) {
int minMaxDiff = tqlayoutStruct[i].calcWidth-tqlayoutStruct[i].effMinWidth;
int reduce = available * minMaxDiff / mw;
tqlayoutStruct[i].calcWidth += reduce;
available -= reduce;
mw -= minMaxDiff;
if ( available >= 0 )
break;
}
}
}
if (available < 0) {
int mw = 0;
for ( int i = nEffCols-1; i >= 0; i-- ) {
Length &width = tqlayoutStruct[i].effWidth;
if (width.isPercent())
mw += tqlayoutStruct[i].calcWidth - tqlayoutStruct[i].effMinWidth;
}
for ( int i = nEffCols-1; i >= 0 && mw > 0; i-- ) {
Length &width = tqlayoutStruct[i].effWidth;
if (width.isPercent()) {
int minMaxDiff = tqlayoutStruct[i].calcWidth-tqlayoutStruct[i].effMinWidth;
int reduce = available * minMaxDiff / mw;
tqlayoutStruct[i].calcWidth += reduce;
available -= reduce;
mw -= minMaxDiff;
if ( available >= 0 )
break;
}
}
}
}
//qDebug( " final available=%d", available );
int pos = 0;
for ( int i = 0; i < nEffCols; i++ ) {
#ifdef DEBUG_LAYOUT
qDebug("col %d: %d (width %d)", i, pos, tqlayoutStruct[i].calcWidth );
#endif
table->columnPos[i] = pos;
pos += tqlayoutStruct[i].calcWidth + table->borderHSpacing();
}
table->columnPos[table->columnPos.size()-1] = pos;
}
void AutoTableLayout::calcPercentages() const
{
total_percent = 0;
for ( unsigned int i = 0; i < tqlayoutStruct.size(); i++ ) {
if ( tqlayoutStruct[i].width.isPercent() )
total_percent += tqlayoutStruct[i].width.value();
}
percentagesDirty = false;
}
#undef DEBUG_LAYOUT
|