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.

FloatContentLayoutManager.java 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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;
  19. import java.util.ArrayList;
  20. import java.util.Collections;
  21. import java.util.List;
  22. import java.util.ListIterator;
  23. import org.apache.fop.area.Area;
  24. import org.apache.fop.area.SideFloat;
  25. import org.apache.fop.fo.Constants;
  26. import org.apache.fop.fo.flow.Float;
  27. import org.apache.fop.fo.properties.CommonBorderPaddingBackground;
  28. import org.apache.fop.layoutmgr.inline.FloatLayoutManager;
  29. import org.apache.fop.layoutmgr.inline.KnuthInlineBox;
  30. import org.apache.fop.layoutmgr.table.TableLayoutManager;
  31. public class FloatContentLayoutManager extends SpacedBorderedPaddedBlockLayoutManager {
  32. private SideFloat floatContentArea;
  33. private int side;
  34. private int yOffset;
  35. /**
  36. * {Add info}
  37. *
  38. * @param node the {@link Float} associated with this instance
  39. */
  40. public FloatContentLayoutManager(Float node) {
  41. super(node);
  42. generatesReferenceArea = true;
  43. side = node.getFloat();
  44. }
  45. @Override
  46. public Keep getKeepTogether() {
  47. return getParentKeepTogether();
  48. }
  49. @Override
  50. public Keep getKeepWithNext() {
  51. return Keep.KEEP_AUTO;
  52. }
  53. @Override
  54. public Keep getKeepWithPrevious() {
  55. return Keep.KEEP_ALWAYS;
  56. }
  57. @Override
  58. public void addAreas(PositionIterator parentIter, LayoutContext layoutContext) {
  59. floatContentArea = new SideFloat();
  60. AreaAdditionUtil.addAreas(this, parentIter, layoutContext);
  61. flush();
  62. }
  63. @Override
  64. public void addChildArea(Area childArea) {
  65. floatContentArea.addChildArea(childArea);
  66. floatContentArea.setBPD(childArea.getAllocBPD());
  67. int effectiveContentIPD = getContentAreaIPD(childLMs, childArea);
  68. int contentIPD = childArea.getIPD();
  69. int xOffset = childArea.getBorderAndPaddingWidthStart();
  70. floatContentArea.setIPD(effectiveContentIPD);
  71. childArea.activateEffectiveIPD();
  72. if (side == Constants.EN_END || side == Constants.EN_RIGHT) {
  73. xOffset += getStartIndent();
  74. floatContentArea.setXOffset(xOffset + contentIPD - effectiveContentIPD);
  75. } else if (side == Constants.EN_START || side == Constants.EN_LEFT) {
  76. floatContentArea.setXOffset(xOffset);
  77. }
  78. LayoutManager lm = parentLayoutManager;
  79. while (!lm.getGeneratesReferenceArea()) {
  80. lm = lm.getParent();
  81. }
  82. yOffset = lm.getParentArea(floatContentArea).getBPD();
  83. lm.addChildArea(floatContentArea);
  84. if (side == Constants.EN_END || side == Constants.EN_RIGHT) {
  85. lm.getPSLM().setEndIntrusionAdjustment(effectiveContentIPD);
  86. } else if (side == Constants.EN_START || side == Constants.EN_LEFT) {
  87. lm.getPSLM().setStartIntrusionAdjustment(effectiveContentIPD);
  88. }
  89. }
  90. private int getContentAreaIPD(List<LayoutManager> childLMs, Area childArea) {
  91. int ipd = getContentAreaIPD(childLMs);
  92. if (ipd == 0) {
  93. return childArea.getEffectiveAllocIPD();
  94. }
  95. return ipd;
  96. }
  97. private int getContentAreaIPD(List<LayoutManager> childLMs) {
  98. int ipd = 0;
  99. for (LayoutManager childLM : childLMs) {
  100. if (childLM instanceof TableLayoutManager) {
  101. ipd += childLM.getContentAreaIPD();
  102. } else {
  103. ipd += getContentAreaIPD(childLM.getChildLMs());
  104. }
  105. }
  106. return ipd;
  107. }
  108. /**
  109. * {Add info}
  110. *
  111. * @param elemenList
  112. * @param startIndex
  113. * @param endIndex
  114. * @return
  115. */
  116. public static List<FloatContentLayoutManager> checkForFloats(List<ListElement> elemenList,
  117. int startIndex, int endIndex) {
  118. ListIterator<ListElement> iter = elemenList.listIterator(startIndex);
  119. List<FloatContentLayoutManager> floats = new ArrayList<FloatContentLayoutManager>();
  120. while (iter.nextIndex() <= endIndex) {
  121. ListElement element = iter.next();
  122. if (element instanceof KnuthInlineBox && ((KnuthInlineBox) element).isFloatAnchor()) {
  123. floats.add(((KnuthInlineBox) element).getFloatContentLM());
  124. } else if (element instanceof KnuthBlockBox && ((KnuthBlockBox) element).hasFloatAnchors()) {
  125. floats.addAll(((KnuthBlockBox) element).getFloatContentLMs());
  126. }
  127. }
  128. if (floats.isEmpty()) {
  129. return Collections.emptyList();
  130. } else {
  131. return floats;
  132. }
  133. }
  134. @Override
  135. protected CommonBorderPaddingBackground getCommonBorderPaddingBackground() {
  136. return null;
  137. }
  138. /**
  139. * {Add info}
  140. *
  141. * @param layoutContext
  142. */
  143. public void processAreas(LayoutContext layoutContext) {
  144. if (getParent() instanceof FloatLayoutManager) {
  145. FloatLayoutManager flm = (FloatLayoutManager) getParent();
  146. flm.processAreas(layoutContext);
  147. }
  148. }
  149. /**
  150. * @return the height of the float content area
  151. */
  152. public int getFloatHeight() {
  153. return floatContentArea.getAllocBPD();
  154. }
  155. /**
  156. * @return the y-offset of the float content
  157. */
  158. public int getFloatYOffset() {
  159. return yOffset;
  160. }
  161. private int getStartIndent() {
  162. int startIndent;
  163. LayoutManager lm = getParent();
  164. while (!(lm instanceof BlockLayoutManager)) {
  165. lm = lm.getParent();
  166. }
  167. startIndent = ((BlockLayoutManager) lm).startIndent;
  168. return startIndent;
  169. }
  170. }