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.

PageSequenceLayoutManager.java 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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 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.AreaTreeHandler;
  26. import org.apache.fop.area.AreaTreeModel;
  27. import org.apache.fop.area.LineArea;
  28. import org.apache.fop.complexscripts.bidi.BidiResolver;
  29. import org.apache.fop.fo.Constants;
  30. import org.apache.fop.fo.pagination.PageSequence;
  31. import org.apache.fop.fo.pagination.PageSequenceMaster;
  32. import org.apache.fop.fo.pagination.SideRegion;
  33. import org.apache.fop.fo.pagination.StaticContent;
  34. import org.apache.fop.layoutmgr.inline.ContentLayoutManager;
  35. import org.apache.fop.traits.MinOptMax;
  36. /**
  37. * LayoutManager for a PageSequence. This class is instantiated by
  38. * area.AreaTreeHandler for each fo:page-sequence found in the
  39. * input document.
  40. */
  41. public class PageSequenceLayoutManager extends AbstractPageSequenceLayoutManager {
  42. private static Log log = LogFactory.getLog(PageSequenceLayoutManager.class);
  43. private PageProvider pageProvider;
  44. private PageBreaker pageBreaker;
  45. /** Footnotes coming from repeated table headers, to be added before any other footnote. */
  46. private List<List<KnuthElement>> tableHeaderFootnotes;
  47. /** Footnotes coming from repeated table footers, to be added after any other footnote. */
  48. private List<List<KnuthElement>> tableFooterFootnotes;
  49. private int startIntrusionAdjustment;
  50. private int endIntrusionAdjustment;
  51. /**
  52. * Constructor
  53. *
  54. * @param ath the area tree handler object
  55. * @param pseq fo:page-sequence to process
  56. */
  57. public PageSequenceLayoutManager(AreaTreeHandler ath, PageSequence pseq) {
  58. super(ath, pseq);
  59. this.pageProvider = new PageProvider(ath, pseq);
  60. }
  61. /** @return the PageProvider applicable to this page-sequence. */
  62. public PageProvider getPageProvider() {
  63. return this.pageProvider;
  64. }
  65. /**
  66. * @return the PageSequence being managed by this layout manager
  67. */
  68. protected PageSequence getPageSequence() {
  69. return (PageSequence)pageSeq;
  70. }
  71. /**
  72. * Provides access to this object
  73. * @return this PageSequenceLayoutManager instance
  74. */
  75. public PageSequenceLayoutManager getPSLM() {
  76. return this;
  77. }
  78. public FlowLayoutManager getFlowLayoutManager() {
  79. if (pageBreaker == null) {
  80. throw new IllegalStateException("This method can be called only during layout");
  81. }
  82. return pageBreaker.getCurrentChildLM();
  83. }
  84. /** {@inheritDoc} */
  85. public void activateLayout() {
  86. initialize();
  87. // perform step 5.8 of refinement process (Unicode BIDI Processing)
  88. if (areaTreeHandler.isComplexScriptFeaturesEnabled()) {
  89. BidiResolver.resolveInlineDirectionality(getPageSequence());
  90. }
  91. LineArea title = null;
  92. if (getPageSequence().getTitleFO() != null) {
  93. try {
  94. ContentLayoutManager clm = getLayoutManagerMaker()
  95. .makeContentLayoutManager(this, getPageSequence().getTitleFO());
  96. Area parentArea = clm.getParentArea(null);
  97. assert (parentArea instanceof LineArea);
  98. title = (LineArea) parentArea;
  99. } catch (IllegalStateException e) {
  100. // empty title; do nothing
  101. }
  102. }
  103. AreaTreeModel areaTreeModel = areaTreeHandler.getAreaTreeModel();
  104. org.apache.fop.area.PageSequence pageSequenceAreaObject
  105. = new org.apache.fop.area.PageSequence(title);
  106. transferExtensions(pageSequenceAreaObject);
  107. pageSequenceAreaObject.setLocale(getPageSequence().getLocale());
  108. areaTreeModel.startPageSequence(pageSequenceAreaObject);
  109. if (log.isDebugEnabled()) {
  110. log.debug("Starting layout");
  111. }
  112. boolean finished = false;
  113. while (!finished) {
  114. initialize();
  115. curPage = makeNewPage(false);
  116. pageBreaker = new PageBreaker(this);
  117. int flowBPD = getCurrentPV().getBodyRegion().getRemainingBPD();
  118. finished = pageBreaker.doLayout(flowBPD);
  119. pageProvider.skipPagePositionOnly = true;
  120. }
  121. finishPage();
  122. }
  123. public void initialize() {
  124. super.initialize();
  125. pageProvider.initialize();
  126. }
  127. /** {@inheritDoc} */
  128. public void finishPageSequence() {
  129. if (pageSeq.hasId()) {
  130. idTracker.signalIDProcessed(pageSeq.getId());
  131. }
  132. pageSeq.getRoot().notifyPageSequenceFinished(currentPageNum,
  133. (currentPageNum - startPageNum) + 1);
  134. areaTreeHandler.notifyPageSequenceFinished(pageSeq,
  135. (currentPageNum - startPageNum) + 1);
  136. getPageSequence().releasePageSequence();
  137. // If this sequence has a page sequence master so we must reset
  138. // it in preparation for the next sequence
  139. String masterReference = getPageSequence().getMasterReference();
  140. PageSequenceMaster pageSeqMaster
  141. = pageSeq.getRoot().getLayoutMasterSet().getPageSequenceMaster(masterReference);
  142. if (pageSeqMaster != null) {
  143. pageSeqMaster.reset();
  144. }
  145. if (log.isDebugEnabled()) {
  146. log.debug("Ending layout");
  147. }
  148. }
  149. /** {@inheritDoc} */
  150. protected Page createPage(int pageNumber, boolean isBlank) {
  151. return pageProvider.getPage(isBlank,
  152. pageNumber, PageProvider.RELTO_PAGE_SEQUENCE);
  153. }
  154. @Override
  155. protected Page makeNewPage(boolean isBlank) {
  156. Page newPage = super.makeNewPage(isBlank);
  157. // Empty pages (pages that have been generated from a SPM that has an un-mapped flow name)
  158. // cannot layout areas from the main flow. Blank pages can be created from empty pages.
  159. if (!isBlank) {
  160. while (!getPageSequence().getMainFlow().getFlowName()
  161. .equals(newPage.getSimplePageMaster()
  162. .getRegion(FO_REGION_BODY).getRegionName())) {
  163. newPage = super.makeNewPage(isBlank);
  164. }
  165. }
  166. return newPage;
  167. }
  168. private void layoutSideRegion(int regionID) {
  169. SideRegion reg = (SideRegion)curPage.getSimplePageMaster().getRegion(regionID);
  170. if (reg == null) {
  171. return;
  172. }
  173. StaticContent sc = getPageSequence().getStaticContent(reg.getRegionName());
  174. if (sc == null) {
  175. return;
  176. }
  177. StaticContentLayoutManager lm = getLayoutManagerMaker()
  178. .makeStaticContentLayoutManager(
  179. this, sc, reg);
  180. lm.doLayout();
  181. }
  182. /** {@inheritDoc} */
  183. protected void finishPage() {
  184. // Layout side regions
  185. layoutSideRegion(FO_REGION_BEFORE);
  186. layoutSideRegion(FO_REGION_AFTER);
  187. layoutSideRegion(FO_REGION_START);
  188. layoutSideRegion(FO_REGION_END);
  189. super.finishPage();
  190. }
  191. /**
  192. * The last page number of the sequence may be incremented, as determined by the
  193. * force-page-count formatting property semantics
  194. * @param lastPageNum number of sequence
  195. * @return the forced last page number of sequence
  196. */
  197. protected int getForcedLastPageNum(final int lastPageNum) {
  198. int forcedLastPageNum = lastPageNum;
  199. int relativeLastPage = lastPageNum - startPageNum + 1;
  200. if (getPageSequence().getForcePageCount() == Constants.EN_EVEN) {
  201. if (relativeLastPage % 2 != 0) {
  202. forcedLastPageNum++;
  203. }
  204. } else if (getPageSequence().getForcePageCount() == Constants.EN_ODD) {
  205. if (relativeLastPage % 2 == 0) {
  206. forcedLastPageNum++;
  207. }
  208. } else if (getPageSequence().getForcePageCount() == Constants.EN_END_ON_EVEN) {
  209. if (lastPageNum % 2 != 0) {
  210. forcedLastPageNum++;
  211. }
  212. } else if (getPageSequence().getForcePageCount() == Constants.EN_END_ON_ODD) {
  213. if (lastPageNum % 2 == 0) {
  214. forcedLastPageNum++;
  215. }
  216. }
  217. return forcedLastPageNum;
  218. }
  219. /**
  220. * Indicates whether the column/page at the given index is on the first page of this page sequence.
  221. *
  222. * @return {@code true} if the given part is on the first page of the sequence
  223. */
  224. boolean isOnFirstPage(int partIndex) {
  225. return pageProvider.isOnFirstPage(partIndex);
  226. }
  227. protected int getLastPageNumber() {
  228. return pageProvider.getLastPageIndex();
  229. }
  230. protected int getWidthOfCurrentPage() {
  231. if (curPage != null) {
  232. return (int) curPage.getPageViewport().getViewArea().getWidth();
  233. }
  234. return 0;
  235. }
  236. /**
  237. * Registers the given footnotes so that they can be added to the current page, before any other footnote.
  238. *
  239. * @param headerFootnotes footnotes coming from a repeated table header
  240. */
  241. public void addTableHeaderFootnotes(List<List<KnuthElement>> headerFootnotes) {
  242. if (tableHeaderFootnotes == null) {
  243. tableHeaderFootnotes = new ArrayList<List<KnuthElement>>();
  244. }
  245. tableHeaderFootnotes.addAll(headerFootnotes);
  246. }
  247. public List<List<KnuthElement>> getTableHeaderFootnotes() {
  248. return getTableFootnotes(tableHeaderFootnotes);
  249. }
  250. /**
  251. * Registers the given footnotes so that they can be added to the current page, after any other footnote.
  252. *
  253. * @param footerFootnotes footnotes coming from a repeated table footer
  254. */
  255. public void addTableFooterFootnotes(List<List<KnuthElement>> footerFootnotes) {
  256. if (tableFooterFootnotes == null) {
  257. tableFooterFootnotes = new ArrayList<List<KnuthElement>>();
  258. }
  259. tableFooterFootnotes.addAll(footerFootnotes);
  260. }
  261. public List<List<KnuthElement>> getTableFooterFootnotes() {
  262. return getTableFootnotes(tableFooterFootnotes);
  263. }
  264. private List<List<KnuthElement>> getTableFootnotes(List<List<KnuthElement>> tableFootnotes) {
  265. if (tableFootnotes == null) {
  266. List<List<KnuthElement>> emptyList = Collections.emptyList();
  267. return emptyList;
  268. } else {
  269. return tableFootnotes;
  270. }
  271. }
  272. /**
  273. * Clears the footnotes coming from repeated table headers/footers, in order to start
  274. * afresh for a new page.
  275. */
  276. public void clearTableHeadingFootnotes() {
  277. if (tableHeaderFootnotes != null) {
  278. tableHeaderFootnotes.clear();
  279. }
  280. if (tableFooterFootnotes != null) {
  281. tableFooterFootnotes.clear();
  282. }
  283. }
  284. public void setStartIntrusionAdjustment(int sia) {
  285. startIntrusionAdjustment = sia;
  286. }
  287. public void setEndIntrusionAdjustment(int eia) {
  288. endIntrusionAdjustment = eia;
  289. }
  290. public int getStartIntrusionAdjustment() {
  291. return startIntrusionAdjustment;
  292. }
  293. public int getEndIntrusionAdjustment() {
  294. return endIntrusionAdjustment;
  295. }
  296. public void recordEndOfFloat(int fHeight) {
  297. pageBreaker.handleEndOfFloat(fHeight);
  298. }
  299. public boolean handlingEndOfFloat() {
  300. return pageBreaker.handlingEndOfFloat();
  301. }
  302. public int getOffsetDueToFloat() {
  303. return pageBreaker.getOffsetDueToFloat();
  304. }
  305. public void recordStartOfFloat(int fHeight, int fYOffset) {
  306. pageBreaker.handleStartOfFloat(fHeight, fYOffset);
  307. }
  308. public boolean handlingStartOfFloat() {
  309. return pageBreaker.handlingStartOfFloat();
  310. }
  311. public int getFloatHeight() {
  312. return pageBreaker.getFloatHeight();
  313. }
  314. public int getFloatYOffset() {
  315. return pageBreaker.getFloatYOffset();
  316. }
  317. public int getCurrentColumnWidth() {
  318. int flowIPD = getCurrentPV().getCurrentSpan().getColumnWidth();
  319. flowIPD -= startIntrusionAdjustment + endIntrusionAdjustment;
  320. return flowIPD;
  321. }
  322. public void holdFootnotes(List fl, List ll, int tfl, int ifl, boolean fp, boolean nf, int fnfi, int fli,
  323. int fei, MinOptMax fsl, int pfli, int pfei) {
  324. if (fl != null && fl.size() > 0) {
  325. pageBreaker.holdFootnotes(fl, ll, tfl, ifl, fp, nf, fnfi, fli, fei, fsl, pfli, pfei);
  326. }
  327. }
  328. public void retrieveFootnotes(PageBreakingAlgorithm alg) {
  329. pageBreaker.retrieveFootones(alg);
  330. }
  331. }