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 5.5KB

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