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.

StaticContentLayoutManager.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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.LinkedList;
  20. import java.util.List;
  21. import org.apache.fop.area.Area;
  22. import org.apache.fop.area.Block;
  23. import org.apache.fop.area.RegionReference;
  24. import org.apache.fop.fo.Constants;
  25. import org.apache.fop.fo.FObj;
  26. import org.apache.fop.fo.pagination.PageSequence;
  27. import org.apache.fop.fo.pagination.SideRegion;
  28. import org.apache.fop.fo.pagination.StaticContent;
  29. import org.apache.fop.layoutmgr.PageBreakingAlgorithm.PageBreakingLayoutListener;
  30. import org.apache.fop.layoutmgr.inline.TextLayoutManager;
  31. /**
  32. * LayoutManager for an fo:flow object.
  33. * Its parent LM is the PageSequenceLayoutManager.
  34. * This LM is responsible for getting columns of the appropriate size
  35. * and filling them with block-level areas generated by its children.
  36. */
  37. public class StaticContentLayoutManager extends BlockStackingLayoutManager {
  38. private RegionReference targetRegion;
  39. private Block targetBlock;
  40. private SideRegion regionFO;
  41. private int contentAreaIPD = 0;
  42. private int contentAreaBPD = -1;
  43. /**
  44. * Creates a new StaticContentLayoutManager.
  45. * @param pslm PageSequenceLayoutManager this layout manager belongs to
  46. * @param node static-content FO
  47. * @param reg side region to layout into
  48. */
  49. public StaticContentLayoutManager(PageSequenceLayoutManager pslm,
  50. StaticContent node, SideRegion reg) {
  51. super(node);
  52. setParent(pslm);
  53. regionFO = reg;
  54. targetRegion = getCurrentPV().getRegionReference(regionFO.getNameId());
  55. }
  56. /**
  57. * Creates a new StaticContentLayoutManager.
  58. * @param pslm PageSequenceLayoutManager this layout manager belongs to
  59. * @param node static-content FO
  60. * @param block the block to layout into
  61. */
  62. public StaticContentLayoutManager(PageSequenceLayoutManager pslm,
  63. StaticContent node, Block block) {
  64. super(node);
  65. setParent(pslm);
  66. targetBlock = block;
  67. }
  68. /** {@inheritDoc} */
  69. public List getNextKnuthElements(LayoutContext context, int alignment) {
  70. throw new IllegalStateException();
  71. }
  72. /**
  73. * {@inheritDoc}
  74. */
  75. public void addAreas(PositionIterator parentIter, LayoutContext layoutContext) {
  76. AreaAdditionUtil.addAreas(this, parentIter, layoutContext);
  77. flush();
  78. targetRegion = null;
  79. }
  80. /**
  81. * Add child area to a the correct container, depending on its
  82. * area class. A Flow can fill at most one area container of any class
  83. * at any one time. The actual work is done by BlockStackingLM.
  84. * {@inheritDoc}
  85. */
  86. public void addChildArea(Area childArea) {
  87. if (getStaticContentFO().getFlowName().equals("xsl-footnote-separator")) {
  88. targetBlock.addBlock((Block)childArea);
  89. } else {
  90. targetRegion.addBlock((Block)childArea);
  91. }
  92. }
  93. /**
  94. * {@inheritDoc}
  95. */
  96. public Area getParentArea(Area childArea) {
  97. if (getStaticContentFO().getFlowName().equals("xsl-footnote-separator")) {
  98. return targetBlock;
  99. } else {
  100. return targetRegion;
  101. }
  102. }
  103. /**
  104. * Does the layout for a side region. Called by PageSequenceLayoutManager.
  105. */
  106. public void doLayout() {
  107. int targetIPD = 0;
  108. int targetBPD = 0;
  109. int targetAlign = EN_AUTO;
  110. boolean autoHeight = false;
  111. StaticContentBreaker breaker;
  112. if (getStaticContentFO().getFlowName().equals("xsl-footnote-separator")) {
  113. targetIPD = targetBlock.getIPD();
  114. targetBPD = targetBlock.getBPD();
  115. if (targetBPD == 0) {
  116. autoHeight = true;
  117. }
  118. targetAlign = EN_BEFORE;
  119. } else {
  120. targetIPD = targetRegion.getIPD();
  121. targetBPD = targetRegion.getBPD();
  122. targetAlign = regionFO.getDisplayAlign();
  123. }
  124. setContentAreaIPD(targetIPD);
  125. setContentAreaBPD(targetBPD);
  126. breaker = new StaticContentBreaker(this, targetIPD, targetAlign);
  127. breaker.doLayout(targetBPD, autoHeight);
  128. if (breaker.isOverflow()) {
  129. if (!autoHeight) {
  130. String page = getPSLM().getCurrentPage().getPageViewport().getPageNumberString();
  131. BlockLevelEventProducer eventProducer = BlockLevelEventProducer.Provider.get(
  132. getStaticContentFO().getUserAgent().getEventBroadcaster());
  133. boolean canRecover = (regionFO.getOverflow() != EN_ERROR_IF_OVERFLOW);
  134. boolean needClip = (regionFO.getOverflow() == Constants.EN_HIDDEN
  135. || regionFO.getOverflow() == Constants.EN_ERROR_IF_OVERFLOW);
  136. eventProducer.regionOverflow(this, regionFO.getName(),
  137. page,
  138. breaker.getOverflowAmount(), needClip, canRecover,
  139. getStaticContentFO().getLocator());
  140. }
  141. }
  142. }
  143. /**
  144. * Convenience method that returns the Static Content node.
  145. * @return the static content node
  146. */
  147. protected StaticContent getStaticContentFO() {
  148. return (StaticContent) fobj;
  149. }
  150. private class StaticContentBreaker extends AbstractBreaker {
  151. private StaticContentLayoutManager lm;
  152. private int displayAlign;
  153. private int ipd;
  154. private int overflow = 0;
  155. public StaticContentBreaker(StaticContentLayoutManager lm, int ipd,
  156. int displayAlign) {
  157. this.lm = lm;
  158. this.ipd = ipd;
  159. this.displayAlign = displayAlign;
  160. }
  161. /** {@inheritDoc} */
  162. protected void observeElementList(List elementList) {
  163. String elementListID = getStaticContentFO().getFlowName();
  164. String pageSequenceID = ((PageSequence)lm.getParent().getFObj()).getId();
  165. if (pageSequenceID != null && pageSequenceID.length() > 0) {
  166. elementListID += "-" + pageSequenceID;
  167. }
  168. ElementListObserver.observe(elementList, "static-content", elementListID);
  169. }
  170. /** {@inheritDoc} */
  171. protected boolean isPartOverflowRecoveryActivated() {
  172. //For side regions, this must be disabled because of wanted overflow.
  173. return false;
  174. }
  175. public boolean isOverflow() {
  176. return (this.overflow != 0);
  177. }
  178. public int getOverflowAmount() {
  179. return this.overflow;
  180. }
  181. /** {@inheritDoc} */
  182. protected PageBreakingLayoutListener createLayoutListener() {
  183. return new PageBreakingLayoutListener() {
  184. public void notifyOverflow(int part, int amount, FObj obj) {
  185. if (StaticContentBreaker.this.overflow == 0) {
  186. StaticContentBreaker.this.overflow = amount;
  187. }
  188. }
  189. };
  190. }
  191. protected LayoutManager getTopLevelLM() {
  192. return lm;
  193. }
  194. protected LayoutContext createLayoutContext() {
  195. LayoutContext lc = super.createLayoutContext();
  196. lc.setRefIPD(ipd);
  197. return lc;
  198. }
  199. protected List getNextKnuthElements(LayoutContext context, int alignment) {
  200. LayoutManager curLM; // currently active LM
  201. List returnList = new LinkedList();
  202. while ((curLM = getChildLM()) != null) {
  203. LayoutContext childLC = new LayoutContext(0);
  204. childLC.setStackLimitBP(context.getStackLimitBP());
  205. childLC.setRefIPD(context.getRefIPD());
  206. childLC.setWritingMode(context.getWritingMode());
  207. List returnedList = null;
  208. //The following is a HACK! Ignore leading and trailing white space
  209. boolean ignore = curLM instanceof TextLayoutManager;
  210. if (!curLM.isFinished()) {
  211. returnedList = curLM.getNextKnuthElements(childLC, alignment);
  212. }
  213. if (returnedList != null && !ignore) {
  214. lm.wrapPositionElements(returnedList, returnList);
  215. }
  216. }
  217. SpaceResolver.resolveElementList(returnList);
  218. setFinished(true);
  219. return returnList;
  220. }
  221. protected int getCurrentDisplayAlign() {
  222. return displayAlign;
  223. }
  224. protected boolean hasMoreContent() {
  225. return !lm.isFinished();
  226. }
  227. protected void addAreas(PositionIterator posIter, LayoutContext context) {
  228. AreaAdditionUtil.addAreas(lm, posIter, context);
  229. }
  230. protected void doPhase3(PageBreakingAlgorithm alg, int partCount,
  231. BlockSequence originalList, BlockSequence effectiveList) {
  232. if (partCount > 1) {
  233. PageBreakPosition pos = (PageBreakPosition)alg.getPageBreaks().getFirst();
  234. int firstPartLength = ElementListUtils.calcContentLength(effectiveList,
  235. effectiveList.ignoreAtStart, pos.getLeafPos());
  236. overflow += alg.totalWidth - firstPartLength;
  237. }
  238. //Rendering all parts (not just the first) at once for the case where the parts that
  239. //overflow should be visible.
  240. alg.removeAllPageBreaks();
  241. //Directly add areas after finding the breaks
  242. this.addAreas(alg, 1, originalList, effectiveList);
  243. }
  244. protected void finishPart(PageBreakingAlgorithm alg, PageBreakPosition pbp) {
  245. //nop for static content
  246. }
  247. protected LayoutManager getCurrentChildLM() {
  248. return null; //TODO NYI
  249. }
  250. }
  251. /**
  252. * Returns the IPD of the content area
  253. * @return the IPD of the content area
  254. */
  255. public int getContentAreaIPD() {
  256. return contentAreaIPD;
  257. }
  258. /** {@inheritDoc} */
  259. protected void setContentAreaIPD(int contentAreaIPD) {
  260. this.contentAreaIPD = contentAreaIPD;
  261. }
  262. /**
  263. * Returns the BPD of the content area
  264. * @return the BPD of the content area
  265. */
  266. public int getContentAreaBPD() {
  267. return contentAreaBPD;
  268. }
  269. private void setContentAreaBPD(int contentAreaBPD) {
  270. this.contentAreaBPD = contentAreaBPD;
  271. }
  272. /** {@inheritDoc} */
  273. public Keep getKeepTogether() {
  274. return Keep.KEEP_AUTO;
  275. }
  276. /** {@inheritDoc} */
  277. public Keep getKeepWithNext() {
  278. return Keep.KEEP_AUTO;
  279. }
  280. /** {@inheritDoc} */
  281. public Keep getKeepWithPrevious() {
  282. return Keep.KEEP_AUTO;
  283. }
  284. }