📄 table_layout.cpp
字号:
/* * 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 layout ------------------ With this (fast) algorithm, the horizontal layout 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 layout algorithm. In the fixed table layout 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-layout: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->style()->width(); 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 // layouting 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 = table->style()->width().isFixed() ? table->style()->width().value() - bs : 0; 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::layout(){ int tableWidth = table->width() - table->bordersPaddingAndSpacing(); int available = tableWidth; int nEffCols = table->numEffCols();#ifdef DEBUG_LAYOUT qDebug("FixedTableLayout::layout: tableWidth=%d, numEffCols=%d", tableWidth, nEffCols);#endif QMemArray<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; else totalPercent = 100;#ifdef DEBUG_LAYOUT qDebug("FixedTableLayout::layout: assigning percent width, base=%d, totalPercent=%d", base, totalPercent);#endif for ( int i = 0; available > 0 && i < nEffCols; i++ ) { if ( width[i].isPercent() ) { int w = base * width[i].value() / totalPercent; 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() ) { int w = available / totalVariable; 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 layouting 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 = layoutStruct[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);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -