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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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 java.util.ListIterator;
  22. import org.apache.commons.logging.Log;
  23. import org.apache.commons.logging.LogFactory;
  24. import org.apache.fop.area.Area;
  25. import org.apache.fop.area.Block;
  26. import org.apache.fop.area.RegionReference;
  27. import org.apache.fop.fo.Constants;
  28. import org.apache.fop.fo.FObj;
  29. import org.apache.fop.fo.pagination.PageSequence;
  30. import org.apache.fop.fo.pagination.SideRegion;
  31. import org.apache.fop.fo.pagination.StaticContent;
  32. import org.apache.fop.layoutmgr.PageBreakingAlgorithm.PageBreakingLayoutListener;
  33. import org.apache.fop.layoutmgr.inline.InlineLevelLayoutManager;
  34. import org.apache.fop.layoutmgr.inline.TextLayoutManager;
  35. import org.apache.fop.traits.MinOptMax;
  36. /**
  37. * LayoutManager for an fo:flow object.
  38. * Its parent LM is the PageSequenceLayoutManager.
  39. * This LM is responsible for getting columns of the appropriate size
  40. * and filling them with block-level areas generated by its children.
  41. */
  42. public class StaticContentLayoutManager extends BlockStackingLayoutManager {
  43. /**
  44. * logging instance
  45. */
  46. private static Log log = LogFactory.getLog(StaticContentLayoutManager.class);
  47. private RegionReference targetRegion;
  48. private Block targetBlock;
  49. private SideRegion regionFO;
  50. private int contentAreaIPD = 0;
  51. private int contentAreaBPD = -1;
  52. /**
  53. * Creates a new StaticContentLayoutManager.
  54. * @param pslm PageSequenceLayoutManager this layout manager belongs to
  55. * @param node static-content FO
  56. * @param reg side region to layout into
  57. */
  58. public StaticContentLayoutManager(PageSequenceLayoutManager pslm,
  59. StaticContent node, SideRegion reg) {
  60. super(node);
  61. setParent(pslm);
  62. regionFO = reg;
  63. targetRegion = getCurrentPV().getRegionReference(regionFO.getNameId());
  64. }
  65. /**
  66. * Creates a new StaticContentLayoutManager.
  67. * @param pslm PageSequenceLayoutManager this layout manager belongs to
  68. * @param node static-content FO
  69. * @param block the block to layout into
  70. */
  71. public StaticContentLayoutManager(PageSequenceLayoutManager pslm,
  72. StaticContent node, Block block) {
  73. super(node);
  74. setParent(pslm);
  75. targetBlock = block;
  76. }
  77. /** {@inheritDoc} */
  78. public LinkedList getNextKnuthElements(LayoutContext context, int alignment) {
  79. if (true) {
  80. throw new UnsupportedOperationException(
  81. "Shouldn't this method be emptied because it's never called at all?");
  82. }
  83. //TODO Empty this method?!?
  84. // set layout dimensions
  85. setContentAreaIPD(context.getRefIPD());
  86. setContentAreaBPD(context.getStackLimitBP().opt);
  87. //TODO Copied from elsewhere. May be worthwhile to factor out the common parts.
  88. // currently active LM
  89. BlockLevelLayoutManager curLM;
  90. BlockLevelLayoutManager prevLM = null;
  91. MinOptMax stackSize = new MinOptMax();
  92. LinkedList returnedList;
  93. LinkedList returnList = new LinkedList();
  94. while ((curLM = ((BlockLevelLayoutManager) getChildLM())) != null) {
  95. if (curLM instanceof InlineLevelLayoutManager) {
  96. log.error("inline area not allowed under flow - ignoring");
  97. curLM.setFinished(true);
  98. continue;
  99. }
  100. // Set up a LayoutContext
  101. MinOptMax bpd = context.getStackLimitBP();
  102. LayoutContext childLC = new LayoutContext(0);
  103. childLC.setStackLimitBP(MinOptMax.subtract(bpd, stackSize));
  104. childLC.setRefIPD(context.getRefIPD());
  105. // get elements from curLM
  106. returnedList = curLM.getNextKnuthElements(childLC, alignment);
  107. //log.debug("FLM.getNextKnuthElements> returnedList.size() = "
  108. // + returnedList.size());
  109. // "wrap" the Position inside each element
  110. LinkedList tempList = returnedList;
  111. KnuthElement tempElement;
  112. returnedList = new LinkedList();
  113. ListIterator listIter = tempList.listIterator();
  114. while (listIter.hasNext()) {
  115. tempElement = (KnuthElement)listIter.next();
  116. tempElement.setPosition(new NonLeafPosition(this, tempElement.getPosition()));
  117. returnedList.add(tempElement);
  118. }
  119. if (returnedList.size() == 1
  120. && ((KnuthElement)returnedList.getFirst()).isPenalty()
  121. && ((KnuthPenalty)returnedList.getFirst()).getP() == -KnuthElement.INFINITE) {
  122. // a descendant of this flow has break-before
  123. returnList.addAll(returnedList);
  124. return returnList;
  125. } else {
  126. if (returnList.size() > 0) {
  127. // there is a block before this one
  128. if (prevLM.mustKeepWithNext()
  129. || curLM.mustKeepWithPrevious()) {
  130. // add an infinite penalty to forbid a break between blocks
  131. returnList.add(new KnuthPenalty(0,
  132. KnuthElement.INFINITE, false,
  133. new Position(this), false));
  134. } else if (!((KnuthElement) returnList.getLast()).isGlue()) {
  135. // add a null penalty to allow a break between blocks
  136. returnList.add(new KnuthPenalty(0, 0, false, new Position(this), false));
  137. }
  138. }
  139. /*LF*/ if (returnedList.size() > 0) { // controllare!
  140. returnList.addAll(returnedList);
  141. if (((KnuthElement)returnedList.getLast()).isPenalty()
  142. && ((KnuthPenalty)returnedList.getLast()).getP()
  143. == -KnuthElement.INFINITE) {
  144. // a descendant of this flow has break-after
  145. /*LF*/ //log.debug("FLM - break after!!");
  146. return returnList;
  147. }
  148. /*LF*/ }
  149. }
  150. prevLM = curLM;
  151. }
  152. setFinished(true);
  153. if (returnList.size() > 0) {
  154. return returnList;
  155. } else {
  156. return null;
  157. }
  158. }
  159. /**
  160. * {@inheritDoc}
  161. */
  162. public void addAreas(PositionIterator parentIter, LayoutContext layoutContext) {
  163. AreaAdditionUtil.addAreas(this, parentIter, layoutContext);
  164. flush();
  165. targetRegion = null;
  166. }
  167. /**
  168. * Add child area to a the correct container, depending on its
  169. * area class. A Flow can fill at most one area container of any class
  170. * at any one time. The actual work is done by BlockStackingLM.
  171. * {@inheritDoc}
  172. */
  173. public void addChildArea(Area childArea) {
  174. if (getStaticContentFO().getFlowName().equals("xsl-footnote-separator")) {
  175. targetBlock.addBlock((Block)childArea);
  176. } else {
  177. targetRegion.addBlock((Block)childArea);
  178. }
  179. }
  180. /**
  181. * {@inheritDoc}
  182. */
  183. public Area getParentArea(Area childArea) {
  184. if (getStaticContentFO().getFlowName().equals("xsl-footnote-separator")) {
  185. return targetBlock;
  186. } else {
  187. return targetRegion;
  188. }
  189. }
  190. /**
  191. * Does the layout for a side region. Called by PageSequenceLayoutManager.
  192. */
  193. public void doLayout() {
  194. int targetIPD = 0;
  195. int targetBPD = 0;
  196. int targetAlign = EN_AUTO;
  197. boolean autoHeight = false;
  198. StaticContentBreaker breaker;
  199. if (getStaticContentFO().getFlowName().equals("xsl-footnote-separator")) {
  200. targetIPD = targetBlock.getIPD();
  201. targetBPD = targetBlock.getBPD();
  202. if (targetBPD == 0) {
  203. autoHeight = true;
  204. }
  205. targetAlign = EN_BEFORE;
  206. } else {
  207. targetIPD = targetRegion.getIPD();
  208. targetBPD = targetRegion.getBPD();
  209. targetAlign = regionFO.getDisplayAlign();
  210. }
  211. setContentAreaIPD(targetIPD);
  212. setContentAreaBPD(targetBPD);
  213. breaker = new StaticContentBreaker(this, targetIPD, targetAlign);
  214. breaker.doLayout(targetBPD, autoHeight);
  215. if (breaker.isOverflow()) {
  216. if (!autoHeight) {
  217. String page = getPSLM().getCurrentPage().getPageViewport().getPageNumberString();
  218. BlockLevelEventProducer eventProducer = BlockLevelEventProducer.Provider.get(
  219. getStaticContentFO().getUserAgent().getEventBroadcaster());
  220. boolean canRecover = (regionFO.getOverflow() != EN_ERROR_IF_OVERFLOW);
  221. boolean needClip = (regionFO.getOverflow() == Constants.EN_HIDDEN
  222. || regionFO.getOverflow() == Constants.EN_ERROR_IF_OVERFLOW);
  223. eventProducer.regionOverflow(this, regionFO.getName(),
  224. page,
  225. breaker.getOverflowAmount(), needClip, canRecover,
  226. getStaticContentFO().getLocator());
  227. }
  228. }
  229. }
  230. /**
  231. * Convenience method that returns the Static Content node.
  232. * @return the static content node
  233. */
  234. protected StaticContent getStaticContentFO() {
  235. return (StaticContent) fobj;
  236. }
  237. private class StaticContentBreaker extends AbstractBreaker {
  238. private StaticContentLayoutManager lm;
  239. private int displayAlign;
  240. private int ipd;
  241. private int overflow = 0;
  242. public StaticContentBreaker(StaticContentLayoutManager lm, int ipd,
  243. int displayAlign) {
  244. this.lm = lm;
  245. this.ipd = ipd;
  246. this.displayAlign = displayAlign;
  247. }
  248. /** {@inheritDoc} */
  249. protected void observeElementList(List elementList) {
  250. String elementListID = getStaticContentFO().getFlowName();
  251. String pageSequenceID = ((PageSequence)lm.getParent().getFObj()).getId();
  252. if (pageSequenceID != null && pageSequenceID.length() > 0) {
  253. elementListID += "-" + pageSequenceID;
  254. }
  255. ElementListObserver.observe(elementList, "static-content", elementListID);
  256. }
  257. /** {@inheritDoc} */
  258. protected boolean isPartOverflowRecoveryActivated() {
  259. //For side regions, this must be disabled because of wanted overflow.
  260. return false;
  261. }
  262. public boolean isOverflow() {
  263. return (this.overflow != 0);
  264. }
  265. public int getOverflowAmount() {
  266. return this.overflow;
  267. }
  268. /** {@inheritDoc} */
  269. protected PageBreakingLayoutListener createLayoutListener() {
  270. return new PageBreakingLayoutListener() {
  271. public void notifyOverflow(int part, int amount, FObj obj) {
  272. if (StaticContentBreaker.this.overflow == 0) {
  273. StaticContentBreaker.this.overflow = amount;
  274. }
  275. }
  276. };
  277. }
  278. protected LayoutManager getTopLevelLM() {
  279. return lm;
  280. }
  281. protected LayoutContext createLayoutContext() {
  282. LayoutContext lc = super.createLayoutContext();
  283. lc.setRefIPD(ipd);
  284. return lc;
  285. }
  286. protected LinkedList getNextKnuthElements(LayoutContext context, int alignment) {
  287. LayoutManager curLM; // currently active LM
  288. LinkedList returnList = new LinkedList();
  289. while ((curLM = getChildLM()) != null) {
  290. LayoutContext childLC = new LayoutContext(0);
  291. childLC.setStackLimitBP(context.getStackLimitBP());
  292. childLC.setRefIPD(context.getRefIPD());
  293. childLC.setWritingMode(context.getWritingMode());
  294. LinkedList returnedList = null;
  295. //The following is a HACK! Ignore leading and trailing white space
  296. boolean ignore = curLM instanceof TextLayoutManager;
  297. if (!curLM.isFinished()) {
  298. returnedList = curLM.getNextKnuthElements(childLC, alignment);
  299. }
  300. if (returnedList != null && !ignore) {
  301. lm.wrapPositionElements(returnedList, returnList);
  302. }
  303. }
  304. SpaceResolver.resolveElementList(returnList);
  305. setFinished(true);
  306. return returnList;
  307. }
  308. protected int getCurrentDisplayAlign() {
  309. return displayAlign;
  310. }
  311. protected boolean hasMoreContent() {
  312. return !lm.isFinished();
  313. }
  314. protected void addAreas(PositionIterator posIter, LayoutContext context) {
  315. AreaAdditionUtil.addAreas(lm, posIter, context);
  316. }
  317. protected void doPhase3(PageBreakingAlgorithm alg, int partCount,
  318. BlockSequence originalList, BlockSequence effectiveList) {
  319. //Rendering all parts (not just the first) at once for the case where the parts that
  320. //overflow should be visible.
  321. alg.removeAllPageBreaks();
  322. //Directly add areas after finding the breaks
  323. this.addAreas(alg, 1, originalList, effectiveList);
  324. }
  325. protected void finishPart(PageBreakingAlgorithm alg, PageBreakPosition pbp) {
  326. //nop for static content
  327. }
  328. protected LayoutManager getCurrentChildLM() {
  329. return null; //TODO NYI
  330. }
  331. }
  332. /**
  333. * Returns the IPD of the content area
  334. * @return the IPD of the content area
  335. */
  336. public int getContentAreaIPD() {
  337. return contentAreaIPD;
  338. }
  339. /** {@inheritDoc} */
  340. protected void setContentAreaIPD(int contentAreaIPD) {
  341. this.contentAreaIPD = contentAreaIPD;
  342. }
  343. /**
  344. * Returns the BPD of the content area
  345. * @return the BPD of the content area
  346. */
  347. public int getContentAreaBPD() {
  348. return contentAreaBPD;
  349. }
  350. private void setContentAreaBPD(int contentAreaBPD) {
  351. this.contentAreaBPD = contentAreaBPD;
  352. }
  353. /** {@inheritDoc} */
  354. public int getKeepTogetherStrength() {
  355. return KEEP_AUTO;
  356. }
  357. /** {@inheritDoc} */
  358. public int getKeepWithNextStrength() {
  359. return KEEP_AUTO;
  360. }
  361. /** {@inheritDoc} */
  362. public int getKeepWithPreviousStrength() {
  363. return KEEP_AUTO;
  364. }
  365. }