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.8KB

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