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

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