@@ -629,8 +629,48 @@ private function buildTableRows(array $rows): TableRows
629629 foreach ($ rows [$ rowKey ] as $ column => $ cell ) {
630630 $ colspan = $ cell instanceof TableCell ? $ cell ->getColspan () : 1 ;
631631
632- if (isset ($ this ->columnMaxWidths [$ column ]) && Helper::width (Helper::removeDecoration ($ formatter , $ cell )) > $ this ->columnMaxWidths [$ column ]) {
633- $ cell = $ formatter ->formatAndWrap ($ cell , $ this ->columnMaxWidths [$ column ] * $ colspan );
632+ $ minWrappedWidth = 0 ;
633+ $ widthApplied = [];
634+ $ lengthColumnBorder = $ this ->getColumnSeparatorWidth () + Helper::width ($ this ->style ->getCellRowContentFormat ()) - 2 ;
635+ for ($ i = $ column ; $ i < ($ column + $ colspan ); ++$ i ) {
636+ if (isset ($ this ->columnMaxWidths [$ i ])) {
637+ $ minWrappedWidth += $ this ->columnMaxWidths [$ i ];
638+ $ widthApplied [] = ['type ' => 'max ' , 'column ' => $ i ];
639+ } elseif (($ this ->columnWidths [$ i ] ?? 0 ) > 0 && $ colspan > 1 ) {
640+ $ minWrappedWidth += $ this ->columnWidths [$ i ];
641+ $ widthApplied [] = ['type ' => 'min ' , 'column ' => $ i ];
642+ }
643+ }
644+ if (1 === \count ($ widthApplied )) {
645+ if ($ colspan > 1 ) {
646+ $ minWrappedWidth *= $ colspan ; // previous logic
647+ }
648+ } elseif (\count ($ widthApplied ) > 1 ) {
649+ $ minWrappedWidth += (\count ($ widthApplied ) - 1 ) * $ lengthColumnBorder ;
650+ }
651+
652+ $ cellWidth = Helper::width (Helper::removeDecoration ($ formatter , $ cell ));
653+ if ($ minWrappedWidth && $ cellWidth > $ minWrappedWidth ) {
654+ $ cell = $ formatter ->formatAndWrap ($ cell , $ minWrappedWidth );
655+ }
656+ // update minimal columnWidths for spanned columns
657+ if ($ colspan > 1 && $ minWrappedWidth > 0 ) {
658+ $ columnsMinWidthProcessed = [];
659+ $ cellWidth = min ($ cellWidth , $ minWrappedWidth );
660+ foreach ($ widthApplied as $ item ) {
661+ if ('max ' === $ item ['type ' ] && $ cellWidth >= $ this ->columnMaxWidths [$ item ['column ' ]]) {
662+ $ minWidthColumn = $ this ->columnMaxWidths [$ item ['column ' ]];
663+ $ this ->columnWidths [$ item ['column ' ]] = $ minWidthColumn ;
664+ $ columnsMinWidthProcessed [$ item ['column ' ]] = true ;
665+ $ cellWidth -= $ minWidthColumn + $ lengthColumnBorder ;
666+ }
667+ }
668+ for ($ i = $ column ; $ i < ($ column + $ colspan ); ++$ i ) {
669+ if (isset ($ columnsMinWidthProcessed [$ i ])) {
670+ continue ;
671+ }
672+ $ this ->columnWidths [$ i ] = $ cellWidth + $ lengthColumnBorder ;
673+ }
634674 }
635675 if (!str_contains ($ cell ?? '' , "\n" )) {
636676 continue ;
0 commit comments