You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ColumnSetup.java 9.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /* $Id$ */
  18. package org.apache.fop.layoutmgr.table;
  19. import java.util.Iterator;
  20. import java.util.List;
  21. import java.util.ListIterator;
  22. import org.apache.commons.logging.Log;
  23. import org.apache.commons.logging.LogFactory;
  24. import org.apache.fop.datatypes.Length;
  25. import org.apache.fop.datatypes.PercentBaseContext;
  26. import org.apache.fop.fo.FONode;
  27. import org.apache.fop.fo.expr.RelativeNumericProperty;
  28. import org.apache.fop.fo.flow.table.Table;
  29. import org.apache.fop.fo.flow.table.TableColumn;
  30. import org.apache.fop.fo.properties.TableColLength;
  31. /**
  32. * Class holding a number of columns making up the column setup of a row.
  33. */
  34. public class ColumnSetup {
  35. /** Logger **/
  36. private static Log log = LogFactory.getLog(ColumnSetup.class);
  37. private Table table;
  38. private List columns = new java.util.ArrayList();
  39. private List colWidths = new java.util.ArrayList();
  40. private int maxColIndexReferenced = 0;
  41. /**
  42. * Main Constructor.
  43. * @param table the table to construct this column setup for
  44. */
  45. public ColumnSetup(Table table) {
  46. this.table = table;
  47. prepareColumns();
  48. initializeColumnWidths();
  49. }
  50. private void prepareColumns() {
  51. List rawCols = table.getColumns();
  52. if (rawCols != null) {
  53. int colnum = 1;
  54. ListIterator iter = rawCols.listIterator();
  55. while (iter.hasNext()) {
  56. TableColumn col = (TableColumn)iter.next();
  57. if (col == null) {
  58. continue;
  59. }
  60. colnum = col.getColumnNumber();
  61. for (int i = 0; i < col.getNumberColumnsRepeated(); i++) {
  62. while (colnum > columns.size()) {
  63. columns.add(null);
  64. }
  65. columns.set(colnum - 1, col);
  66. colnum++;
  67. }
  68. }
  69. //Post-processing the list (looking for gaps)
  70. //TODO The following block could possibly be removed
  71. int pos = 1;
  72. ListIterator ppIter = columns.listIterator();
  73. while (ppIter.hasNext()) {
  74. TableColumn col = (TableColumn)ppIter.next();
  75. if (col == null) {
  76. assert false; //Gaps are filled earlier by fo.flow.table.Table.finalizeColumns()
  77. //log.error("Found a gap in the table-columns at position " + pos);
  78. }
  79. pos++;
  80. }
  81. }
  82. }
  83. /**
  84. * Returns a column. If the index of the column is bigger than the number of explicitly
  85. * defined columns the last column is returned.
  86. * @param index index of the column (1 is the first column)
  87. * @return the requested column
  88. */
  89. public TableColumn getColumn(int index) {
  90. int size = columns.size();
  91. if (index > size) {
  92. if (index > maxColIndexReferenced) {
  93. maxColIndexReferenced = index;
  94. TableColumn col = getColumn(1);
  95. if (!(size == 1 && col.isImplicitColumn())) {
  96. assert false; //TODO Seems to be removable as this is now done in the FO tree
  97. log.warn(FONode.decorateWithContextInfo(
  98. "There are fewer table-columns than are needed. "
  99. + "Column " + index + " was accessed, but only "
  100. + size + " columns have been defined. "
  101. + "The last defined column will be reused."
  102. , table));
  103. if (!table.isAutoLayout()) {
  104. log.warn("Please note that according XSL-FO 1.0 (7.26.9) says that "
  105. + "the 'column-width' property must be specified for every "
  106. + "column, unless the automatic table layout is used.");
  107. }
  108. }
  109. }
  110. return (TableColumn) columns.get(size - 1);
  111. } else {
  112. return (TableColumn) columns.get(index - 1);
  113. }
  114. }
  115. /** {@inheritDoc} */
  116. public String toString() {
  117. return columns.toString();
  118. }
  119. /** @return the number of columns in the setup. */
  120. public int getColumnCount() {
  121. if (maxColIndexReferenced > columns.size()) {
  122. return maxColIndexReferenced;
  123. } else {
  124. return columns.size();
  125. }
  126. }
  127. /** @return an Iterator over all columns */
  128. public Iterator iterator() {
  129. return this.columns.iterator();
  130. }
  131. /*
  132. private void createColumnsFromFirstRow() {
  133. //TODO Create oldColumns from first row here
  134. //--> rule 2 in "fixed table layout", see CSS2, 17.5.2
  135. //Alternative: extend oldColumns on-the-fly, but in this case we need the
  136. //new property evaluation context so proportional-column-width() works
  137. //correctly.
  138. if (columns.size() == 0) {
  139. this.columns.add(table.getDefaultColumn());
  140. }
  141. }
  142. */
  143. /**
  144. * Initializes the column's widths
  145. *
  146. */
  147. private void initializeColumnWidths() {
  148. TableColumn col;
  149. Length colWidth;
  150. for (int i = columns.size(); --i >= 0;) {
  151. if (columns.get(i) != null) {
  152. col = (TableColumn) columns.get(i);
  153. colWidth = col.getColumnWidth();
  154. colWidths.add(0, colWidth);
  155. }
  156. }
  157. colWidths.add(0, null);
  158. }
  159. /**
  160. * Works out the base unit for resolving proportional-column-width()
  161. * [p-c-w(x) = x * base_unit_ipd]
  162. *
  163. * @param tlm the TableLayoutManager
  164. * @return the computed base unit (in millipoint)
  165. */
  166. protected double computeTableUnit(TableLayoutManager tlm) {
  167. return computeTableUnit(tlm, tlm.getContentAreaIPD());
  168. }
  169. /**
  170. * Works out the base unit for resolving proportional-column-width()
  171. * [p-c-w(x) = x * base_unit_ipd]
  172. *
  173. * @param percentBaseContext the percent base context for relative values
  174. * @param contentAreaIPD the IPD of the available content area
  175. * @return the computed base unit (in millipoints)
  176. */
  177. public float computeTableUnit(PercentBaseContext percentBaseContext, int contentAreaIPD) {
  178. int sumCols = 0;
  179. float factors = 0;
  180. float unit = 0;
  181. /* calculate the total width (specified absolute/percentages),
  182. * and work out the total number of factors to use to distribute
  183. * the remaining space (if any)
  184. */
  185. for (Iterator i = colWidths.iterator(); i.hasNext();) {
  186. Length colWidth = (Length) i.next();
  187. if (colWidth != null) {
  188. sumCols += colWidth.getValue(percentBaseContext);
  189. if (colWidth instanceof RelativeNumericProperty) {
  190. factors += ((RelativeNumericProperty) colWidth).getTableUnits();
  191. } else if (colWidth instanceof TableColLength) {
  192. factors += ((TableColLength) colWidth).getTableUnits();
  193. }
  194. }
  195. }
  196. /* distribute the remaining space over the accumulated
  197. * factors (if any)
  198. */
  199. if (factors > 0) {
  200. if (sumCols < contentAreaIPD) {
  201. unit = (contentAreaIPD - sumCols) / factors;
  202. } else {
  203. log.warn("No space remaining to distribute over columns.");
  204. }
  205. }
  206. return unit;
  207. }
  208. /**
  209. * @param col column index (1 is first column)
  210. * @param context the context for percentage based calculations
  211. * @return the X offset of the requested column
  212. */
  213. public int getXOffset(int col, PercentBaseContext context) {
  214. int xoffset = 0;
  215. for (int i = col; --i >= 0;) {
  216. int effCol;
  217. if (i < colWidths.size()) {
  218. effCol = i;
  219. } else {
  220. effCol = colWidths.size() - 1;
  221. }
  222. if (colWidths.get(effCol) != null) {
  223. xoffset += ((Length) colWidths.get(effCol)).getValue(context);
  224. }
  225. }
  226. return xoffset;
  227. }
  228. /**
  229. * Calculates the sum of all column widths.
  230. * @param context the context for percentage based calculations
  231. * @return the requested sum in millipoints
  232. */
  233. public int getSumOfColumnWidths(PercentBaseContext context) {
  234. int sum = 0;
  235. for (int i = 1, c = getColumnCount(); i <= c; i++) {
  236. int effIndex = i;
  237. if (i >= colWidths.size()) {
  238. effIndex = colWidths.size() - 1;
  239. }
  240. if (colWidths.get(effIndex) != null) {
  241. sum += ((Length) colWidths.get(effIndex)).getValue(context);
  242. }
  243. }
  244. return sum;
  245. }
  246. }