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.

PageProvider.java 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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.List;
  20. import org.apache.commons.logging.Log;
  21. import org.apache.commons.logging.LogFactory;
  22. import org.apache.fop.area.AreaTreeHandler;
  23. import org.apache.fop.area.BodyRegion;
  24. import org.apache.fop.area.PageViewport;
  25. import org.apache.fop.fo.Constants;
  26. import org.apache.fop.fo.pagination.PageSequence;
  27. import org.apache.fop.fo.pagination.Region;
  28. import org.apache.fop.fo.pagination.RegionBody;
  29. import org.apache.fop.fo.pagination.SimplePageMaster;
  30. /**
  31. * <p>This class delivers Page instances. It also caches them as necessary.
  32. * </p>
  33. * <p>Additional functionality makes sure that surplus instances that are requested by the
  34. * page breaker are properly discarded, especially in situations where hard breaks cause
  35. * blank pages. The reason for that: The page breaker sometimes needs to preallocate
  36. * additional pages since it doesn't know exactly until the end how many pages it really needs.
  37. * </p>
  38. */
  39. public class PageProvider implements Constants {
  40. private Log log = LogFactory.getLog(PageProvider.class);
  41. /** Indices are evaluated relative to the first page in the page-sequence. */
  42. public static final int RELTO_PAGE_SEQUENCE = 0;
  43. /** Indices are evaluated relative to the first page in the current element list. */
  44. public static final int RELTO_CURRENT_ELEMENT_LIST = 1;
  45. private int startPageOfPageSequence;
  46. private int startPageOfCurrentElementList;
  47. private int startColumnOfCurrentElementList;
  48. private boolean spanAllForCurrentElementList;
  49. private List<Page> cachedPages = new java.util.ArrayList<Page>();
  50. private int lastPageIndex = -1;
  51. private int indexOfCachedLastPage = -1;
  52. //Cache to optimize getAvailableBPD() calls
  53. private int lastRequestedIndex = -1;
  54. private int lastReportedBPD = -1;
  55. /**
  56. * AreaTreeHandler which activates the PSLM and controls
  57. * the rendering of its pages.
  58. */
  59. private AreaTreeHandler areaTreeHandler;
  60. /**
  61. * fo:page-sequence formatting object being
  62. * processed by this class
  63. */
  64. private PageSequence pageSeq;
  65. protected boolean skipPagePositionOnly;
  66. /**
  67. * Main constructor.
  68. * @param ath the area tree handler
  69. * @param ps The page-sequence the provider operates on
  70. */
  71. public PageProvider(AreaTreeHandler ath, PageSequence ps) {
  72. this.areaTreeHandler = ath;
  73. this.pageSeq = ps;
  74. this.startPageOfPageSequence = ps.getStartingPageNumber();
  75. }
  76. public void initialize() {
  77. cachedPages.clear();
  78. }
  79. /**
  80. * The page breaker notifies the provider about the page number an element list starts
  81. * on so it can later retrieve PageViewports relative to this first page.
  82. * @param startPage the number of the first page for the element list.
  83. * @param startColumn the starting column number for the element list.
  84. * @param spanAll true if the current element list is for a column-spanning section
  85. */
  86. public void setStartOfNextElementList(int startPage, int startColumn, boolean spanAll) {
  87. if (log.isDebugEnabled()) {
  88. log.debug("start of the next element list is:"
  89. + " page=" + startPage + " col=" + startColumn
  90. + (spanAll ? ", column-spanning" : ""));
  91. }
  92. this.startPageOfCurrentElementList = startPage - startPageOfPageSequence + 1;
  93. this.startColumnOfCurrentElementList = startColumn;
  94. this.spanAllForCurrentElementList = spanAll;
  95. //Reset Cache
  96. this.lastRequestedIndex = -1;
  97. this.lastReportedBPD = -1;
  98. }
  99. /**
  100. * Sets the index of the last page. This is done as soon as the position of the last page
  101. * is known or assumed.
  102. * @param index the index relative to the first page in the page-sequence
  103. */
  104. public void setLastPageIndex(int index) {
  105. this.lastPageIndex = index;
  106. }
  107. /**
  108. * Returns the available BPD for the part/page indicated by the index parameter.
  109. * The index is the part/page relative to the start of the current element list.
  110. * This method takes multiple columns into account.
  111. * @param index zero-based index of the requested part/page
  112. * @return the available BPD
  113. */
  114. public int getAvailableBPD(int index) {
  115. //Special optimization: There may be many equal calls by the BreakingAlgorithm
  116. if (this.lastRequestedIndex == index) {
  117. if (log.isTraceEnabled()) {
  118. log.trace("getAvailableBPD(" + index + ") -> (cached) " + lastReportedBPD);
  119. }
  120. return this.lastReportedBPD;
  121. }
  122. int pageIndexTmp = index;
  123. int pageIndex = 0;
  124. int colIndex = startColumnOfCurrentElementList;
  125. Page page = getPage(
  126. false, pageIndex, RELTO_CURRENT_ELEMENT_LIST);
  127. while (pageIndexTmp > 0) {
  128. colIndex++;
  129. if (colIndex >= page.getPageViewport().getCurrentSpan().getColumnCount()) {
  130. colIndex = 0;
  131. pageIndex++;
  132. page = getPage(false, pageIndex, RELTO_CURRENT_ELEMENT_LIST);
  133. BodyRegion br = page.getPageViewport().getBodyRegion();
  134. if (!pageSeq.getMainFlow().getFlowName().equals(br.getRegionName())) {
  135. pageIndexTmp++;
  136. }
  137. }
  138. pageIndexTmp--;
  139. }
  140. this.lastRequestedIndex = index;
  141. this.lastReportedBPD = page.getPageViewport().getBodyRegion().getRemainingBPD();
  142. if (log.isTraceEnabled()) {
  143. log.trace("getAvailableBPD(" + index + ") -> " + lastReportedBPD);
  144. }
  145. return this.lastReportedBPD;
  146. }
  147. private static class Column {
  148. final Page page;
  149. final int pageIndex;
  150. final int colIndex;
  151. final int columnCount;
  152. Column(Page page, int pageIndex, int colIndex, int columnCount) {
  153. this.page = page;
  154. this.pageIndex = pageIndex;
  155. this.colIndex = colIndex;
  156. this.columnCount = columnCount;
  157. }
  158. }
  159. private Column getColumn(int index) {
  160. int columnCount = 0;
  161. int colIndex = startColumnOfCurrentElementList + index;
  162. int pageIndex = -1;
  163. Page page;
  164. do {
  165. colIndex -= columnCount;
  166. pageIndex++;
  167. page = getPage(false, pageIndex, RELTO_CURRENT_ELEMENT_LIST);
  168. columnCount = page.getPageViewport().getCurrentSpan().getColumnCount();
  169. } while (colIndex >= columnCount);
  170. return new Column(page, pageIndex, colIndex, columnCount);
  171. }
  172. /**
  173. * Compares the IPD of the given part with the following one.
  174. *
  175. * @param index index of the current part
  176. * @return a negative integer, zero or a positive integer as the current IPD is less
  177. * than, equal to or greater than the IPD of the following part
  178. */
  179. public int compareIPDs(int index) {
  180. Column column = getColumn(index);
  181. if (column.colIndex + 1 < column.columnCount) {
  182. // Next part is a column on same page => same IPD
  183. return 0;
  184. } else {
  185. Page nextPage = getPage(false, column.pageIndex + 1, RELTO_CURRENT_ELEMENT_LIST);
  186. return column.page.getPageViewport().getBodyRegion().getColumnIPD()
  187. - nextPage.getPageViewport().getBodyRegion().getColumnIPD();
  188. }
  189. }
  190. /**
  191. * Checks if a break at the passed index would start a new page
  192. * @param index the index of the element before the break
  193. * @return {@code true} if the break starts a new page
  194. */
  195. boolean startPage(int index) {
  196. return getColumn(index).colIndex == 0;
  197. }
  198. /**
  199. * Checks if a break at the passed index would end a page
  200. * @param index the index of the element before the break
  201. * @return {@code true} if the break ends a page
  202. */
  203. boolean endPage(int index) {
  204. Column column = getColumn(index);
  205. return column.colIndex == column.columnCount - 1;
  206. }
  207. /**
  208. * Obtain the applicable column-count for the element at the
  209. * passed index
  210. * @param index the index of the element
  211. * @return the number of columns
  212. */
  213. int getColumnCount(int index) {
  214. return getColumn(index).columnCount;
  215. }
  216. /**
  217. * Returns the part index (0&lt;x&lt;partCount) which denotes the first part on the last page
  218. * generated by the current element list.
  219. * @param partCount Number of parts determined by the breaking algorithm
  220. * @return the requested part index
  221. */
  222. public int getStartingPartIndexForLastPage(int partCount) {
  223. int lastPartIndex = partCount - 1;
  224. return lastPartIndex - getColumn(lastPartIndex).colIndex;
  225. }
  226. Page getPageFromColumnIndex(int columnIndex) {
  227. return getColumn(columnIndex).page;
  228. }
  229. /**
  230. * Returns a Page.
  231. * @param isBlank true if this page is supposed to be blank.
  232. * @param index Index of the page (see relativeTo)
  233. * @param relativeTo Defines which value the index parameter should be evaluated relative
  234. * to. (One of PageProvider.RELTO_*)
  235. * @return the requested Page
  236. */
  237. public Page getPage(boolean isBlank, int index, int relativeTo) {
  238. if (relativeTo == RELTO_PAGE_SEQUENCE) {
  239. return getPage(isBlank, index);
  240. } else if (relativeTo == RELTO_CURRENT_ELEMENT_LIST) {
  241. int effIndex = startPageOfCurrentElementList + index;
  242. effIndex += startPageOfPageSequence - 1;
  243. return getPage(isBlank, effIndex);
  244. } else {
  245. throw new IllegalArgumentException(
  246. "Illegal value for relativeTo: " + relativeTo);
  247. }
  248. }
  249. /**
  250. * Returns a Page.
  251. * @param isBlank true if the Page should be a blank one
  252. * @param index the Page's index
  253. * @return a Page instance
  254. */
  255. protected Page getPage(boolean isBlank, int index) {
  256. boolean isLastPage = (lastPageIndex >= 0) && (index == lastPageIndex);
  257. if (log.isTraceEnabled()) {
  258. log.trace("getPage(" + index + " " + (isBlank ? "blank" : "non-blank")
  259. + (isLastPage ? " <LAST>" : "") + ")");
  260. }
  261. int intIndex = index - startPageOfPageSequence;
  262. if (log.isTraceEnabled()) {
  263. if (isBlank) {
  264. log.trace("blank page requested: " + index);
  265. }
  266. if (isLastPage) {
  267. log.trace("last page requested: " + index);
  268. }
  269. }
  270. if (intIndex > cachedPages.size()) {
  271. throw new UnsupportedOperationException("Cannot handle holes in page cache");
  272. } else if (intIndex == cachedPages.size()) {
  273. if (log.isTraceEnabled()) {
  274. log.trace("Caching " + index);
  275. }
  276. cacheNextPage(index, isBlank, isLastPage, this.spanAllForCurrentElementList);
  277. }
  278. Page page = cachedPages.get(intIndex);
  279. boolean replace = false;
  280. if (page.getPageViewport().isBlank() != isBlank) {
  281. log.debug("blank condition doesn't match. Replacing PageViewport.");
  282. replace = true;
  283. }
  284. if (page.getPageViewport().getCurrentSpan().getColumnCount() == 1
  285. && !this.spanAllForCurrentElementList) {
  286. RegionBody rb = (RegionBody)page.getSimplePageMaster().getRegion(Region.FO_REGION_BODY);
  287. int colCount = rb.getColumnCount();
  288. if (colCount > 1) {
  289. log.debug("Span doesn't match. Replacing PageViewport.");
  290. replace = true;
  291. }
  292. }
  293. if ((isLastPage && indexOfCachedLastPage != intIndex)
  294. || (!isLastPage && indexOfCachedLastPage >= 0)) {
  295. log.debug("last page condition doesn't match. Replacing PageViewport.");
  296. replace = true;
  297. indexOfCachedLastPage = (isLastPage ? intIndex : -1);
  298. }
  299. if (replace) {
  300. discardCacheStartingWith(intIndex);
  301. PageViewport oldPageVP = page.getPageViewport();
  302. page = cacheNextPage(index, isBlank, isLastPage, this.spanAllForCurrentElementList);
  303. PageViewport newPageVP = page.getPageViewport();
  304. newPageVP.replace(oldPageVP);
  305. this.areaTreeHandler.getIDTracker().replacePageViewPort(oldPageVP, newPageVP);
  306. }
  307. return page;
  308. }
  309. protected void discardCacheStartingWith(int index) {
  310. while (index < cachedPages.size()) {
  311. this.cachedPages.remove(cachedPages.size() - 1);
  312. if (!pageSeq.goToPreviousSimplePageMaster()) {
  313. log.warn("goToPreviousSimplePageMaster() on the first page called!");
  314. }
  315. }
  316. }
  317. private Page cacheNextPage(int index, boolean isBlank, boolean isLastPage, boolean spanAll) {
  318. String pageNumberString = pageSeq.makeFormattedPageNumber(index);
  319. boolean isFirstPage = (startPageOfPageSequence == index);
  320. SimplePageMaster spm = pageSeq.getNextSimplePageMaster(
  321. index, isFirstPage, isLastPage, isBlank);
  322. boolean isPagePositionOnly = pageSeq.hasPagePositionOnly() && !skipPagePositionOnly;
  323. if (isPagePositionOnly) {
  324. spm = pageSeq.getNextSimplePageMaster(index, isFirstPage, true, isBlank);
  325. }
  326. Page page = new Page(spm, index, pageNumberString, isBlank, spanAll, isPagePositionOnly);
  327. //Set unique key obtained from the AreaTreeHandler
  328. page.getPageViewport().setKey(areaTreeHandler.generatePageViewportKey());
  329. page.getPageViewport().setForeignAttributes(spm.getForeignAttributes());
  330. page.getPageViewport().setWritingModeTraits(pageSeq);
  331. cachedPages.add(page);
  332. if (isLastPage) {
  333. pageSeq.getRoot().setLastSeq(pageSeq);
  334. } else if (!isFirstPage) {
  335. pageSeq.getRoot().setLastSeq(null);
  336. }
  337. return page;
  338. }
  339. public int getIndexOfCachedLastPage() {
  340. return indexOfCachedLastPage;
  341. }
  342. public int getLastPageIndex() {
  343. return lastPageIndex;
  344. }
  345. public int getLastPageIPD() {
  346. int index = this.cachedPages.size();
  347. boolean isFirstPage = (startPageOfPageSequence == index);
  348. SimplePageMaster spm = pageSeq.getLastSimplePageMaster(index, isFirstPage, false);
  349. Page page = new Page(spm, index, "", false, false, false);
  350. if (pageSeq.getRoot().getLastSeq() != null && pageSeq.getRoot().getLastSeq() != pageSeq) {
  351. return -1;
  352. }
  353. return page.getPageViewport().getBodyRegion().getColumnIPD();
  354. }
  355. public int getCurrentIPD() {
  356. return getPageFromColumnIndex(startColumnOfCurrentElementList).getPageViewport().getBodyRegion()
  357. .getColumnIPD();
  358. }
  359. /**
  360. * Indicates whether the column/page at the given index is on the first page of the page sequence.
  361. *
  362. * @return {@code true} if the given part is on the first page of the sequence
  363. */
  364. boolean isOnFirstPage(int partIndex) {
  365. Column column = getColumn(partIndex);
  366. return startPageOfCurrentElementList + column.pageIndex == startPageOfPageSequence;
  367. }
  368. }