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.

BodyRegion.java 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. import org.apache.fop.fo.pagination.RegionBody;
  21. import org.apache.fop.traits.WritingModeTraitsGetter;
  22. /**
  23. * This class is a container for the areas that may be generated by
  24. * an fo:region-body. It extends the RegionReference that is used
  25. * directly by the other region classes.
  26. * See fo:region-body definition in the XSL Rec for more information.
  27. */
  28. public class BodyRegion extends RegionReference {
  29. private static final long serialVersionUID = -1848872997724078080L;
  30. private BeforeFloat beforeFloat; // optional
  31. private MainReference mainReference; // mandatory
  32. private Footnote footnote; // optional
  33. private int columnGap;
  34. private int columnCount;
  35. /**
  36. * Constructor which can read traits directly
  37. * from an fo:region-body formatting object.
  38. * @param rb the region-body FO node
  39. * @param parent the parent region viewport
  40. */
  41. public BodyRegion(RegionBody rb, RegionViewport parent) {
  42. this(rb.getNameId(), rb.getRegionName(), parent, rb.getColumnCount(), rb.getColumnGap());
  43. }
  44. /**
  45. * Constructor which can read traits directly
  46. * from an fo:region-body formatting object.
  47. * @param regionClass the region class (as returned by Region.getNameId())
  48. * @param regionName the name of the region (as returned by Region.getRegionName())
  49. * @param parent the parent region viewport
  50. * @param columnCount the number of columns
  51. * @param columnGap the gap between columns
  52. */
  53. public BodyRegion(int regionClass, String regionName, RegionViewport parent,
  54. int columnCount, int columnGap) {
  55. super(regionClass, regionName, parent);
  56. this.columnCount = columnCount;
  57. this.columnGap = columnGap;
  58. mainReference = new MainReference(this);
  59. }
  60. /**
  61. * Get the number of columns when not spanning
  62. *
  63. * @return the number of columns
  64. */
  65. public int getColumnCount() {
  66. return this.columnCount;
  67. }
  68. /** @return the column-gap value */
  69. public int getColumnGap() {
  70. return this.columnGap;
  71. }
  72. int getContentIPD() {
  73. RegionViewport rv = getRegionViewport();
  74. return getIPD() - rv.getBorderAndPaddingWidthStart() - rv.getBorderAndPaddingWidthEnd();
  75. }
  76. public int getColumnIPD() {
  77. return (getContentIPD() - (columnCount - 1) * columnGap) / columnCount;
  78. }
  79. /**
  80. * Get the main reference area.
  81. *
  82. * @return the main reference area
  83. */
  84. public MainReference getMainReference() {
  85. return mainReference;
  86. }
  87. /**
  88. * indicates whether the main reference area has any child areas added to it
  89. *
  90. * @return whether the main reference area has any child areas added to it
  91. */
  92. public boolean isEmpty() {
  93. return (mainReference == null || mainReference.isEmpty())
  94. && (footnote == null || footnote.isEmpty())
  95. && (beforeFloat == null || beforeFloat.isEmpty());
  96. }
  97. /**
  98. * Get the before float area.
  99. *
  100. * @return the before float area
  101. */
  102. public BeforeFloat getBeforeFloat() {
  103. if (beforeFloat == null) {
  104. beforeFloat = new BeforeFloat();
  105. }
  106. return beforeFloat;
  107. }
  108. /**
  109. * Get the footnote area.
  110. *
  111. * @return the footnote area
  112. */
  113. public Footnote getFootnote() {
  114. if (footnote == null) {
  115. footnote = new Footnote();
  116. }
  117. return footnote;
  118. }
  119. /**
  120. * @return the available BPD in the main reference area after the previous span reference
  121. * areas are subtracted.
  122. */
  123. public int getRemainingBPD() {
  124. int usedBPD = 0;
  125. List<Span> spans = getMainReference().getSpans();
  126. int previousSpanCount = spans.size() - 1;
  127. for (int i = 0; i < previousSpanCount; i++) {
  128. usedBPD += spans.get(i).getHeight();
  129. }
  130. return getBPD() - usedBPD;
  131. }
  132. /**
  133. * Sets the writing mode traits for the main reference area of
  134. * this body region area.
  135. * @param wmtg a WM traits getter
  136. */
  137. public void setWritingModeTraits(WritingModeTraitsGetter wmtg) {
  138. if (getMainReference() != null) {
  139. getMainReference().setWritingModeTraits(wmtg);
  140. }
  141. }
  142. /** {@inheritDoc} */
  143. public Object clone() throws CloneNotSupportedException {
  144. BodyRegion br = (BodyRegion) super.clone();
  145. br.mainReference = new MainReference(br);
  146. br.mainReference.setSpans(mainReference.getSpans());
  147. return br;
  148. }
  149. }