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.

Span.java 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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.area;
  19. import java.util.List;
  20. /**
  21. * The span-reference-area.
  22. * This is a block-area with 0 border and padding that is stacked
  23. * within the main-reference-area
  24. * This object holds one or more normal-flow-reference-area children
  25. * based on the column-count trait in effect for this span.
  26. * See fo:region-body definition in the XSL Rec for more information.
  27. */
  28. public class Span extends Area {
  29. private static final long serialVersionUID = -5551430053660081549L;
  30. // the list of flow reference areas in this span area
  31. private List flowAreas;
  32. private int colCount;
  33. private int colGap;
  34. private int colWidth; // width for each normal flow, calculated value
  35. private int curFlowIdx; // n-f-r-a currently being processed, zero-based
  36. /**
  37. * Create a span area with the number of columns for this span area.
  38. *
  39. * @param colCount the number of columns in the span
  40. * @param colGap the column gap between each column
  41. * @param ipd the total ipd of the span
  42. */
  43. public Span(int colCount, int colGap, int ipd) {
  44. addTrait(Trait.IS_REFERENCE_AREA, Boolean.TRUE);
  45. this.colCount = colCount;
  46. this.colGap = colGap;
  47. this.ipd = ipd;
  48. curFlowIdx = 0;
  49. createNormalFlows();
  50. }
  51. /**
  52. * Create the normal flows for this Span
  53. */
  54. private void createNormalFlows() {
  55. flowAreas = new java.util.ArrayList(colCount);
  56. colWidth = (ipd - ((colCount - 1) * colGap)) / colCount;
  57. for (int i = 0; i < colCount; i++) {
  58. NormalFlow newFlow = new NormalFlow(colWidth);
  59. flowAreas.add(newFlow);
  60. }
  61. }
  62. /**
  63. * Get the column count for this span area.
  64. *
  65. * @return the number of columns defined for this span area
  66. */
  67. public int getColumnCount() {
  68. return colCount;
  69. }
  70. /**
  71. * Get the width of a single column within this Span
  72. *
  73. * @return the width of a single column
  74. */
  75. public int getColumnWidth() {
  76. return colWidth;
  77. }
  78. /**
  79. * Get the height of this span area.
  80. *
  81. * @return the height of this span area
  82. */
  83. public int getHeight() {
  84. return getBPD();
  85. }
  86. /**
  87. * Get the normal flow area for a particular column.
  88. *
  89. * @param colRequested the zero-based column number of the flow
  90. * @return the flow area for the requested column
  91. */
  92. public NormalFlow getNormalFlow(int colRequested) {
  93. if (colRequested >= 0 && colRequested < colCount) {
  94. return (NormalFlow) flowAreas.get(colRequested);
  95. } else { // internal error
  96. throw new IllegalArgumentException("Invalid column number "
  97. + colRequested + " requested; only 0-" + (colCount - 1)
  98. + " available.");
  99. }
  100. }
  101. /**
  102. * Get the NormalFlow area currently being processed
  103. *
  104. * @return the current NormalFlow
  105. */
  106. public NormalFlow getCurrentFlow() {
  107. return getNormalFlow(curFlowIdx);
  108. }
  109. /** @return the index of the current normal flow */
  110. public int getCurrentFlowIndex() {
  111. return curFlowIdx;
  112. }
  113. /**
  114. * Indicate to the Span that the next column is being
  115. * processed.
  116. *
  117. * @return the new NormalFlow (in the next column)
  118. */
  119. public NormalFlow moveToNextFlow() {
  120. if (hasMoreFlows()) {
  121. curFlowIdx++;
  122. return getNormalFlow(curFlowIdx);
  123. } else {
  124. throw new IllegalStateException("(Internal error.) No more flows left in span.");
  125. }
  126. }
  127. /**
  128. * Indicates if the Span has unprocessed flows.
  129. *
  130. * @return true if Span can increment to the next flow,
  131. * false otherwise.
  132. */
  133. public boolean hasMoreFlows() {
  134. return (curFlowIdx < colCount - 1);
  135. }
  136. /**
  137. * Called to notify the span that all its flows have been fully generated so it can update
  138. * its own BPD extent.
  139. */
  140. public void notifyFlowsFinished() {
  141. int maxFlowBPD = Integer.MIN_VALUE;
  142. for (int i = 0; i < colCount; i++) {
  143. maxFlowBPD = Math.max(maxFlowBPD, getNormalFlow(i).getAllocBPD());
  144. }
  145. bpd = maxFlowBPD;
  146. }
  147. /**
  148. * Indicates whether any child areas have been added to this span area.
  149. *
  150. * This is achieved by looping through each flow.
  151. * @return true if no child areas have been added yet.
  152. */
  153. public boolean isEmpty() {
  154. int areaCount = 0;
  155. for (int i = 0; i < getColumnCount(); i++) {
  156. NormalFlow flow = getNormalFlow(i);
  157. if (flow != null) {
  158. if (flow.getChildAreas() != null) {
  159. areaCount += flow.getChildAreas().size();
  160. }
  161. }
  162. }
  163. return (areaCount == 0);
  164. }
  165. /** {@inheritDoc} */
  166. public String toString() {
  167. StringBuffer sb = new StringBuffer(super.toString());
  168. if (colCount > 1) {
  169. sb.append(" {colCount=").append(colCount);
  170. sb.append(", colWidth=").append(colWidth);
  171. sb.append(", curFlowIdx=").append(this.curFlowIdx);
  172. sb.append("}");
  173. }
  174. return sb.toString();
  175. }
  176. }