Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(131)

Side by Side Diff: Source/core/layout/TableLayoutAlgorithmAuto.cpp

Issue 1111443003: Refactor the code distributing width to columns in auto layout tables. (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Updated Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « Source/core/layout/TableLayoutAlgorithmAuto.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2002 Lars Knoll (knoll@kde.org) 2 * Copyright (C) 2002 Lars Knoll (knoll@kde.org)
3 * (C) 2002 Dirk Mueller (mueller@kde.org) 3 * (C) 2002 Dirk Mueller (mueller@kde.org)
4 * Copyright (C) 2003, 2006, 2008, 2010 Apple Inc. All rights reserved. 4 * Copyright (C) 2003, 2006, 2008, 2010 Apple Inc. All rights reserved.
5 * 5 *
6 * This library is free software; you can redistribute it and/or 6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public 7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either 8 * License as published by the Free Software Foundation; either
9 * version 2 of the License. 9 * version 2 of the License.
10 * 10 *
(...skipping 576 matching lines...) Expand 10 before | Expand all | Expand 10 after
587 if (available > 0) { 587 if (available > 0) {
588 for (size_t i = 0; i < nEffCols; ++i) { 588 for (size_t i = 0; i < nEffCols; ++i) {
589 Length& logicalWidth = m_layoutStruct[i].effectiveLogicalWidth; 589 Length& logicalWidth = m_layoutStruct[i].effectiveLogicalWidth;
590 if (logicalWidth.isFixed() && logicalWidth.value() > m_layoutStruct[ i].computedLogicalWidth) { 590 if (logicalWidth.isFixed() && logicalWidth.value() > m_layoutStruct[ i].computedLogicalWidth) {
591 available += m_layoutStruct[i].computedLogicalWidth - logicalWid th.value(); 591 available += m_layoutStruct[i].computedLogicalWidth - logicalWid th.value();
592 m_layoutStruct[i].computedLogicalWidth = logicalWidth.value(); 592 m_layoutStruct[i].computedLogicalWidth = logicalWidth.value();
593 } 593 }
594 } 594 }
595 } 595 }
596 596
597 // now satisfy variable 597 // Give each auto width column its share of the available width.
598 if (available > 0 && numAuto) { 598 if (available > 0 && numAuto) {
599 available += allocAuto; // this gets redistributed 599 available += allocAuto;
600 for (size_t i = 0; i < nEffCols; ++i) { 600 distributeWidthToColumns<float, Auto, NonEmptyCells, InitialWidth, Start ToEnd>(available, totalAuto);
601 Length& logicalWidth = m_layoutStruct[i].effectiveLogicalWidth;
602 if (logicalWidth.isAuto() && totalAuto && !m_layoutStruct[i].emptyCe llsOnly) {
603 int cellLogicalWidth = std::max<int>(m_layoutStruct[i].computedL ogicalWidth, static_cast<int>(available * static_cast<float>(m_layoutStruct[i].e ffectiveMaxLogicalWidth) / totalAuto));
604 available -= cellLogicalWidth;
605 totalAuto -= m_layoutStruct[i].effectiveMaxLogicalWidth;
606 m_layoutStruct[i].computedLogicalWidth = cellLogicalWidth;
607 }
608 }
609 } 601 }
610 602
611 // spread over fixed columns 603 // Any remaining available width expands fixed width, percent width and non- empty auto width columns, in that order.
612 if (available > 0 && numFixed) { 604 if (available > 0 && numFixed)
613 for (size_t i = 0; i < nEffCols; ++i) { 605 distributeWidthToColumns<float, Fixed, AllCells, ExtraWidth, StartToEnd> (available, totalFixed);
614 Length& logicalWidth = m_layoutStruct[i].effectiveLogicalWidth;
615 if (logicalWidth.isFixed()) {
616 int cellLogicalWidth = static_cast<int>(available * static_cast< float>(m_layoutStruct[i].effectiveMaxLogicalWidth) / totalFixed);
617 available -= cellLogicalWidth;
618 totalFixed -= m_layoutStruct[i].effectiveMaxLogicalWidth;
619 m_layoutStruct[i].computedLogicalWidth += cellLogicalWidth;
620 }
621 }
622 }
623 606
624 // spread over percent colums 607 if (available > 0 && m_hasPercent && totalPercent < 100)
625 if (available > 0 && m_hasPercent && totalPercent < 100) { 608 distributeWidthToColumns<float, Percent, AllCells, ExtraWidth, StartToEn d>(available, totalPercent);
626 for (size_t i = 0; i < nEffCols; ++i) {
627 Length& logicalWidth = m_layoutStruct[i].effectiveLogicalWidth;
628 // TODO(alancutter): Make this work correctly for calc lengths.
629 if (logicalWidth.hasPercent()) {
630 int cellLogicalWidth = available * logicalWidth.percent() / tota lPercent;
631 available -= cellLogicalWidth;
632 totalPercent -= logicalWidth.percent();
633 m_layoutStruct[i].computedLogicalWidth += cellLogicalWidth;
634 if (!available || !totalPercent)
635 break;
636 }
637 }
638 }
639 609
640 // spread over the rest
641 if (available > 0 && nEffCols > numAutoEmptyCellsOnly) { 610 if (available > 0 && nEffCols > numAutoEmptyCellsOnly) {
642 unsigned total = nEffCols - numAutoEmptyCellsOnly; 611 unsigned total = nEffCols - numAutoEmptyCellsOnly;
643 // still have some width to spread 612 // Starting from the last cell is for compatibility with FF/IE - it isn' t specified anywhere.
644 for (unsigned i = nEffCols; i; ) { 613 distributeWidthToColumns<unsigned, Auto, NonEmptyCells, LeftoverWidth, E ndToStart>(available, total);
645 --i;
646 // variable columns with empty cells only don't get any width
647 if (m_layoutStruct[i].effectiveLogicalWidth.isAuto() && m_layoutStru ct[i].emptyCellsOnly)
648 continue;
649 int cellLogicalWidth = available / total;
650 available -= cellLogicalWidth;
651 total--;
652 m_layoutStruct[i].computedLogicalWidth += cellLogicalWidth;
653 }
654 } 614 }
655 615
656 // If we have overallocated, reduce every cell according to the difference b etween desired width and minwidth 616 // If we have overallocated, reduce every cell according to the difference b etween desired width and minwidth
657 // this seems to produce to the pixel exact results with IE. Wonder is some of this also holds for width distributing. 617 // this seems to produce to the pixel exact results with IE. Wonder is some of this also holds for width distributing.
658 // Need to reduce cells with the following prioritization:
659 // This is basically the reverse of how we grew the cells. 618 // This is basically the reverse of how we grew the cells.
660 if (available < 0) 619 if (available < 0)
661 shrinkCellWidth(Auto, available); 620 shrinkColumnWidth(Auto, available);
662 if (available < 0) 621 if (available < 0)
663 shrinkCellWidth(Fixed, available); 622 shrinkColumnWidth(Fixed, available);
664 if (available < 0) 623 if (available < 0)
665 shrinkCellWidth(Percent, available); 624 shrinkColumnWidth(Percent, available);
666 625
667 int pos = 0; 626 int pos = 0;
668 for (size_t i = 0; i < nEffCols; ++i) { 627 for (size_t i = 0; i < nEffCols; ++i) {
669 m_table->setColumnPosition(i, pos); 628 m_table->setColumnPosition(i, pos);
670 pos += m_layoutStruct[i].computedLogicalWidth + m_table->hBorderSpacing( ); 629 pos += m_layoutStruct[i].computedLogicalWidth + m_table->hBorderSpacing( );
671 } 630 }
672 m_table->setColumnPosition(m_table->columnPositions().size() - 1, pos); 631 m_table->setColumnPosition(m_table->columnPositions().size() - 1, pos);
673 } 632 }
674 633
675 void TableLayoutAlgorithmAuto::shrinkCellWidth(const LengthType& lengthType, int & available) 634 template<typename Total, LengthType lengthType, CellsToProcess cellsToProcess, D istributionMode distributionMode, DistributionDirection distributionDirection>
635 void TableLayoutAlgorithmAuto::distributeWidthToColumns(int& available, Total to tal)
636 {
637 // TODO(alancutter): Make this work correctly for calc lengths.
638 int nEffCols = static_cast<int>(m_table->numEffCols());
639 bool startToEnd = distributionDirection == StartToEnd;
640 for (int i = startToEnd ? 0 : nEffCols - 1; startToEnd ? i < nEffCols : i > -1; startToEnd ? ++i : --i) {
641 const Length& logicalWidth = m_layoutStruct[i].effectiveLogicalWidth;
642 if (cellsToProcess == NonEmptyCells && logicalWidth.isAuto() && m_layout Struct[i].emptyCellsOnly)
643 continue;
644 if (distributionMode != LeftoverWidth && logicalWidth.type() != lengthTy pe)
645 continue;
646
647 float factor = 1;
648 if (distributionMode != LeftoverWidth) {
649 if (lengthType == Percent)
650 factor = logicalWidth.percent();
651 else if (lengthType == Auto || lengthType == Fixed)
652 factor = m_layoutStruct[i].effectiveMaxLogicalWidth;
653 }
654
655 int newWidth = available * factor / total;
656 int cellLogicalWidth = (distributionMode == InitialWidth) ? max<int>(m_l ayoutStruct[i].computedLogicalWidth, newWidth) : newWidth;
657 available -= cellLogicalWidth;
658 total -= factor;
659 m_layoutStruct[i].computedLogicalWidth = (distributionMode == InitialWid th) ? cellLogicalWidth : m_layoutStruct[i].computedLogicalWidth + cellLogicalWid th;
660
661 // If we have run out of width to allocate we're done.
662 // Also, any overallocation will be unwound later so no point in buildin g up cells to unwind, just bail now.
663 if (available <= 0 || total <= 0)
664 return;
665 }
666 }
667
668 void TableLayoutAlgorithmAuto::shrinkColumnWidth(const LengthType& lengthType, i nt& available)
676 { 669 {
677 size_t nEffCols = m_table->numEffCols(); 670 size_t nEffCols = m_table->numEffCols();
678 int logicalWidthBeyondMin = 0; 671 int logicalWidthBeyondMin = 0;
679 for (unsigned i = nEffCols; i; ) { 672 for (unsigned i = nEffCols; i; ) {
680 --i; 673 --i;
681 Length& logicalWidth = m_layoutStruct[i].effectiveLogicalWidth; 674 Length& logicalWidth = m_layoutStruct[i].effectiveLogicalWidth;
682 if (logicalWidth.type() == lengthType) 675 if (logicalWidth.type() == lengthType)
683 logicalWidthBeyondMin += m_layoutStruct[i].computedLogicalWidth - m_ layoutStruct[i].effectiveMinLogicalWidth; 676 logicalWidthBeyondMin += m_layoutStruct[i].computedLogicalWidth - m_ layoutStruct[i].effectiveMinLogicalWidth;
684 } 677 }
685 678
686 for (unsigned i = nEffCols; i && logicalWidthBeyondMin > 0; ) { 679 for (unsigned i = nEffCols; i && logicalWidthBeyondMin > 0; ) {
687 --i; 680 --i;
688 Length& logicalWidth = m_layoutStruct[i].effectiveLogicalWidth; 681 Length& logicalWidth = m_layoutStruct[i].effectiveLogicalWidth;
689 if (logicalWidth.type() == lengthType) { 682 if (logicalWidth.type() == lengthType) {
690 int minMaxDiff = m_layoutStruct[i].computedLogicalWidth - m_layoutSt ruct[i].effectiveMinLogicalWidth; 683 int minMaxDiff = m_layoutStruct[i].computedLogicalWidth - m_layoutSt ruct[i].effectiveMinLogicalWidth;
691 int reduce = available * minMaxDiff / logicalWidthBeyondMin; 684 int reduce = available * minMaxDiff / logicalWidthBeyondMin;
692 m_layoutStruct[i].computedLogicalWidth += reduce; 685 m_layoutStruct[i].computedLogicalWidth += reduce;
693 available -= reduce; 686 available -= reduce;
694 logicalWidthBeyondMin -= minMaxDiff; 687 logicalWidthBeyondMin -= minMaxDiff;
695 if (available >= 0) 688 if (available >= 0)
696 break; 689 break;
697 } 690 }
698 } 691 }
699 } 692 }
700 } 693 }
OLDNEW
« no previous file with comments | « Source/core/layout/TableLayoutAlgorithmAuto.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698