PageRenderTime 40ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/Doduo1.1.2/WebCore/rendering/FixedTableLayout.cpp

https://github.com/weissms/owb-mirror
C++ | 309 lines | 200 code | 27 blank | 82 comment | 56 complexity | 140f39abe2f0d19a1be7464e34b5f84e MD5 | raw file
  1. /*
  2. * This file is part of the HTML rendering engine for KDE.
  3. *
  4. * Copyright (C) 2002 Lars Knoll (knoll@kde.org)
  5. * (C) 2002 Dirk Mueller (mueller@kde.org)
  6. * Copyright (C) 2003, 2006 Apple Computer, Inc.
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Library General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2 of the License.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Library General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Library General Public License
  19. * along with this library; see the file COPYING.LIB. If not, write to
  20. * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  21. * Boston, MA 02110-1301, USA.
  22. */
  23. #include "config.h"
  24. #include "FixedTableLayout.h"
  25. #include "RenderTable.h"
  26. #include "RenderTableCell.h"
  27. #include "RenderTableCol.h"
  28. #include "RenderTableSection.h"
  29. /*
  30. The text below is from the CSS 2.1 specs.
  31. Fixed table layout
  32. With this (fast) algorithm, the horizontal layout of the table does
  33. not depend on the contents of the cells; it only depends on the
  34. table's width, the width of the columns, and borders or cell
  35. spacing.
  36. The table's width may be specified explicitly with the 'width'
  37. property. A value of 'auto' (for both 'display: table' and 'display:
  38. inline-table') means use the automatic table layout algorithm.
  39. In the fixed table layout algorithm, the width of each column is
  40. determined as follows:
  41. 1. A column element with a value other than 'auto' for the 'width'
  42. property sets the width for that column.
  43. 2. Otherwise, a cell in the first row with a value other than
  44. 'auto' for the 'width' property sets the width for that column. If
  45. the cell spans more than one column, the width is divided over the
  46. columns.
  47. 3. Any remaining columns equally divide the remaining horizontal
  48. table space (minus borders or cell spacing).
  49. The width of the table is then the greater of the value of the
  50. 'width' property for the table element and the sum of the column
  51. widths (plus cell spacing or borders). If the table is wider than
  52. the columns, the extra space should be distributed over the columns.
  53. In this manner, the user agent can begin to lay out the table once
  54. the entire first row has been received. Cells in subsequent rows do
  55. not affect column widths. Any cell that has content that overflows
  56. uses the 'overflow' property to determine whether to clip the
  57. overflow content.
  58. */
  59. using namespace std;
  60. namespace WebCore {
  61. FixedTableLayout::FixedTableLayout(RenderTable* table)
  62. : TableLayout(table)
  63. {
  64. }
  65. int FixedTableLayout::calcWidthArray(int tableWidth)
  66. {
  67. int usedWidth = 0;
  68. // iterate over all <col> elements
  69. RenderObject* child = m_table->firstChild();
  70. int cCol = 0;
  71. int nEffCols = m_table->numEffCols();
  72. m_width.resize(nEffCols);
  73. m_width.fill(Length(Auto));
  74. Length grpWidth;
  75. while (child) {
  76. if (child->isTableCol()) {
  77. RenderTableCol *col = static_cast<RenderTableCol *>(child);
  78. int span = col->span();
  79. if (col->firstChild())
  80. grpWidth = col->style()->width();
  81. else {
  82. Length w = col->style()->width();
  83. if (w.isAuto())
  84. w = grpWidth;
  85. int effWidth = 0;
  86. if (w.isFixed() && w.value() > 0)
  87. effWidth = w.value();
  88. int usedSpan = 0;
  89. int i = 0;
  90. while (usedSpan < span) {
  91. if(cCol + i >= nEffCols) {
  92. m_table->appendColumn(span - usedSpan);
  93. nEffCols++;
  94. m_width.resize(nEffCols);
  95. m_width[nEffCols-1] = Length();
  96. }
  97. int eSpan = m_table->spanOfEffCol(cCol+i);
  98. if ((w.isFixed() || w.isPercent()) && w.isPositive()) {
  99. m_width[cCol + i].setRawValue(w.type(), w.rawValue() * eSpan);
  100. usedWidth += effWidth * eSpan;
  101. }
  102. usedSpan += eSpan;
  103. i++;
  104. }
  105. cCol += i;
  106. }
  107. } else
  108. break;
  109. RenderObject *next = child->firstChild();
  110. if (!next)
  111. next = child->nextSibling();
  112. if (!next && child->parent()->isTableCol()) {
  113. next = child->parent()->nextSibling();
  114. grpWidth = Length();
  115. }
  116. child = next;
  117. }
  118. // Iterate over the first row in case some are unspecified.
  119. RenderTableSection* section = m_table->header();
  120. if (!section)
  121. section = m_table->firstBody();
  122. if (!section)
  123. section = m_table->footer();
  124. if (section && !section->numRows())
  125. section = m_table->sectionBelow(section, true);
  126. if (section) {
  127. cCol = 0;
  128. RenderObject* firstRow = section->firstChild();
  129. child = firstRow->firstChild();
  130. while (child) {
  131. if (child->isTableCell()) {
  132. RenderTableCell* cell = static_cast<RenderTableCell*>(child);
  133. if (cell->prefWidthsDirty())
  134. cell->calcPrefWidths();
  135. Length w = cell->styleOrColWidth();
  136. int span = cell->colSpan();
  137. int effWidth = 0;
  138. if (w.isFixed() && w.isPositive())
  139. effWidth = w.value();
  140. int usedSpan = 0;
  141. int i = 0;
  142. while (usedSpan < span) {
  143. ASSERT(cCol + i < nEffCols);
  144. int eSpan = m_table->spanOfEffCol(cCol + i);
  145. // Only set if no col element has already set it.
  146. if (m_width[cCol + i].isAuto() && w.type() != Auto) {
  147. m_width[cCol + i].setRawValue(w.type(), w.rawValue() * eSpan / span);
  148. usedWidth += effWidth * eSpan / span;
  149. }
  150. usedSpan += eSpan;
  151. i++;
  152. }
  153. cCol += i;
  154. }
  155. child = child->nextSibling();
  156. }
  157. }
  158. return usedWidth;
  159. }
  160. void FixedTableLayout::calcPrefWidths(int& minWidth, int& maxWidth)
  161. {
  162. // FIXME: This entire calculation is incorrect for both minwidth and maxwidth.
  163. // we might want to wait until we have all of the first row before
  164. // layouting for the first time.
  165. // only need to calculate the minimum width as the sum of the
  166. // cols/cells with a fixed width.
  167. //
  168. // The maximum width is max(minWidth, tableWidth).
  169. int bs = m_table->bordersPaddingAndSpacing();
  170. int tableWidth = m_table->style()->width().isFixed() ? m_table->style()->width().value() - bs : 0;
  171. int mw = calcWidthArray(tableWidth) + bs;
  172. minWidth = max(mw, tableWidth);
  173. maxWidth = minWidth;
  174. }
  175. void FixedTableLayout::layout()
  176. {
  177. int tableWidth = m_table->width() - m_table->bordersPaddingAndSpacing();
  178. int nEffCols = m_table->numEffCols();
  179. Vector<int> calcWidth(nEffCols, 0);
  180. int numAuto = 0;
  181. int autoSpan = 0;
  182. int totalFixedWidth = 0;
  183. int totalPercentWidth = 0;
  184. int totalRawPercent = 0;
  185. // Compute requirements and try to satisfy fixed and percent widths.
  186. // Percentages are of the table's width, so for example
  187. // for a table width of 100px with columns (40px, 10%), the 10% compute
  188. // to 10px here, and will scale up to 20px in the final (80px, 20px).
  189. for (int i = 0; i < nEffCols; i++) {
  190. if (m_width[i].isFixed()) {
  191. calcWidth[i] = m_width[i].value();
  192. totalFixedWidth += calcWidth[i];
  193. } else if (m_width[i].isPercent()) {
  194. calcWidth[i] = m_width[i].calcValue(tableWidth);
  195. totalPercentWidth += calcWidth[i];
  196. totalRawPercent += m_width[i].rawValue();
  197. } else if (m_width[i].isAuto()) {
  198. numAuto++;
  199. autoSpan += m_table->spanOfEffCol(i);
  200. }
  201. }
  202. int hspacing = m_table->hBorderSpacing();
  203. int totalWidth = totalFixedWidth + totalPercentWidth;
  204. if (!numAuto || totalWidth > tableWidth) {
  205. // If there are no auto columns, or if the total is too wide, take
  206. // what we have and scale it to fit as necessary.
  207. if (totalWidth != tableWidth) {
  208. // Fixed widths only scale up
  209. if (totalFixedWidth && totalWidth < tableWidth) {
  210. totalFixedWidth = 0;
  211. for (int i = 0; i < nEffCols; i++) {
  212. if (m_width[i].isFixed()) {
  213. calcWidth[i] = calcWidth[i] * tableWidth / totalWidth;
  214. totalFixedWidth += calcWidth[i];
  215. }
  216. }
  217. }
  218. if (totalRawPercent) {
  219. totalPercentWidth = 0;
  220. for (int i = 0; i < nEffCols; i++) {
  221. if (m_width[i].isPercent()) {
  222. calcWidth[i] = m_width[i].rawValue() * (tableWidth - totalFixedWidth) / totalRawPercent;
  223. totalPercentWidth += calcWidth[i];
  224. }
  225. }
  226. }
  227. totalWidth = totalFixedWidth + totalPercentWidth;
  228. }
  229. } else {
  230. // Divide the remaining width among the auto columns.
  231. int remainingWidth = tableWidth - totalFixedWidth - totalPercentWidth - hspacing * (autoSpan - numAuto);
  232. int lastAuto = 0;
  233. for (int i = 0; i < nEffCols; i++) {
  234. if (m_width[i].isAuto()) {
  235. int span = m_table->spanOfEffCol(i);
  236. int w = remainingWidth * span / autoSpan;
  237. calcWidth[i] = w + hspacing * (span - 1);
  238. remainingWidth -= w;
  239. if (!remainingWidth)
  240. break;
  241. lastAuto = i;
  242. numAuto--;
  243. autoSpan -= span;
  244. }
  245. }
  246. // Last one gets the remainder.
  247. if (remainingWidth)
  248. calcWidth[lastAuto] += remainingWidth;
  249. totalWidth = tableWidth;
  250. }
  251. if (totalWidth < tableWidth) {
  252. // Spread extra space over columns.
  253. int remainingWidth = tableWidth - totalWidth;
  254. int total = nEffCols;
  255. while (total) {
  256. int w = remainingWidth / total;
  257. remainingWidth -= w;
  258. calcWidth[--total] += w;
  259. }
  260. if (nEffCols > 0)
  261. calcWidth[nEffCols - 1] += remainingWidth;
  262. }
  263. int pos = 0;
  264. for (int i = 0; i < nEffCols; i++) {
  265. m_table->columnPositions()[i] = pos;
  266. pos += calcWidth[i] + hspacing;
  267. }
  268. int colPositionsSize = m_table->columnPositions().size();
  269. if (colPositionsSize > 0)
  270. m_table->columnPositions()[colPositionsSize - 1] = pos;
  271. }
  272. } // namespace WebCore