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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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.fo.Constants;
  24. import org.apache.fop.fo.pagination.PageSequence;
  25. import org.apache.fop.fo.pagination.Region;
  26. import org.apache.fop.fo.pagination.SimplePageMaster;
  27. /**
  28. * <p>This class delivers Page instances. It also caches them as necessary.
  29. * </p>
  30. * <p>Additional functionality makes sure that surplus instances that are requested by the
  31. * page breaker are properly discarded, especially in situations where hard breaks cause
  32. * blank pages. The reason for that: The page breaker sometimes needs to preallocate
  33. * additional pages since it doesn't know exactly until the end how many pages it really needs.
  34. * </p>
  35. */
  36. public class PageProvider implements Constants {
  37. private Log log = LogFactory.getLog(PageProvider.class);
  38. /** Indices are evaluated relative to the first page in the page-sequence. */
  39. public static final int RELTO_PAGE_SEQUENCE = 0;
  40. /** Indices are evaluated relative to the first page in the current element list. */
  41. public static final int RELTO_CURRENT_ELEMENT_LIST = 1;
  42. private int startPageOfPageSequence;
  43. private int startPageOfCurrentElementList;
  44. private int startColumnOfCurrentElementList;
  45. private boolean spanAllForCurrentElementList;
  46. private List cachedPages = new java.util.ArrayList();
  47. private int lastPageIndex = -1;
  48. private int indexOfCachedLastPage = -1;
  49. //Cache to optimize getAvailableBPD() calls
  50. private int lastRequestedIndex = -1;
  51. private int lastReportedBPD = -1;
  52. /**
  53. * AreaTreeHandler which activates the PSLM and controls
  54. * the rendering of its pages.
  55. */
  56. private AreaTreeHandler areaTreeHandler;
  57. /**
  58. * fo:page-sequence formatting object being
  59. * processed by this class
  60. */
  61. private PageSequence pageSeq;
  62. /**
  63. * Main constructor.
  64. * @param ath the area tree handler
  65. * @param ps The page-sequence the provider operates on
  66. */
  67. public PageProvider(AreaTreeHandler ath, PageSequence ps) {
  68. this.areaTreeHandler = ath;
  69. this.pageSeq = ps;
  70. this.startPageOfPageSequence = ps.getStartingPageNumber();
  71. }
  72. /**
  73. * The page breaker notifies the provider about the page number an element list starts
  74. * on so it can later retrieve PageViewports relative to this first page.
  75. * @param startPage the number of the first page for the element list.
  76. * @param startColumn the starting column number for the element list.
  77. * @param spanAll true if the current element list is for a column-spanning section
  78. */
  79. public void setStartOfNextElementList(int startPage, int startColumn, boolean spanAll) {
  80. if (log.isDebugEnabled()) {
  81. log.debug("start of the next element list is:"
  82. + " page=" + startPage + " col=" + startColumn
  83. + (spanAll ? ", column-spanning" : ""));
  84. }
  85. this.startPageOfCurrentElementList = startPage - startPageOfPageSequence + 1;
  86. this.startColumnOfCurrentElementList = startColumn;
  87. this.spanAllForCurrentElementList = spanAll;
  88. //Reset Cache
  89. this.lastRequestedIndex = -1;
  90. this.lastReportedBPD = -1;
  91. }
  92. /**
  93. * Sets the index of the last page. This is done as soon as the position of the last page
  94. * is known or assumed.
  95. * @param index the index relative to the first page in the page-sequence
  96. */
  97. public void setLastPageIndex(int index) {
  98. this.lastPageIndex = index;
  99. }
  100. /**
  101. * Returns the available BPD for the part/page indicated by the index parameter.
  102. * The index is the part/page relative to the start of the current element list.
  103. * This method takes multiple columns into account.
  104. * @param index zero-based index of the requested part/page
  105. * @return the available BPD
  106. */
  107. public int getAvailableBPD(int index) {
  108. //Special optimization: There may be many equal calls by the BreakingAlgorithm
  109. if (this.lastRequestedIndex == index) {
  110. if (log.isTraceEnabled()) {
  111. log.trace("getAvailableBPD(" + index + ") -> (cached) " + lastReportedBPD);
  112. }
  113. return this.lastReportedBPD;
  114. }
  115. int c = index;
  116. int pageIndex = 0;
  117. int colIndex = startColumnOfCurrentElementList;
  118. Page page = getPage(
  119. false, pageIndex, RELTO_CURRENT_ELEMENT_LIST);
  120. while (c > 0) {
  121. colIndex++;
  122. if (colIndex >= page.getPageViewport().getCurrentSpan().getColumnCount()) {
  123. colIndex = 0;
  124. pageIndex++;
  125. page = getPage(
  126. false, pageIndex, RELTO_CURRENT_ELEMENT_LIST);
  127. }
  128. c--;
  129. }
  130. this.lastRequestedIndex = index;
  131. this.lastReportedBPD = page.getPageViewport().getBodyRegion().getRemainingBPD();
  132. if (log.isTraceEnabled()) {
  133. log.trace("getAvailableBPD(" + index + ") -> " + lastReportedBPD);
  134. }
  135. return this.lastReportedBPD;
  136. }
  137. // Wish there were a more elegant way to do this in Java
  138. private int[] getColIndexAndColCount(int index) {
  139. int columnCount = 0;
  140. int colIndex = startColumnOfCurrentElementList + index;
  141. int pageIndex = -1;
  142. do {
  143. colIndex -= columnCount;
  144. pageIndex++;
  145. Page page = getPage(false, pageIndex, RELTO_CURRENT_ELEMENT_LIST);
  146. columnCount = page.getPageViewport().getCurrentSpan().getColumnCount();
  147. } while (colIndex >= columnCount);
  148. return new int[] {colIndex, columnCount};
  149. }
  150. /**
  151. * Compares the IPD of the given part with the following one.
  152. *
  153. * @param index index of the current part
  154. * @return a negative integer, zero or a positive integer as the current IPD is less
  155. * than, equal to or greater than the IPD of the following part
  156. */
  157. public int compareIPDs(int index) {
  158. int columnCount = 0;
  159. int colIndex = startColumnOfCurrentElementList + index;
  160. int pageIndex = -1;
  161. Page page;
  162. do {
  163. colIndex -= columnCount;
  164. pageIndex++;
  165. page = getPage(false, pageIndex, RELTO_CURRENT_ELEMENT_LIST);
  166. columnCount = page.getPageViewport().getCurrentSpan().getColumnCount();
  167. } while (colIndex >= columnCount);
  168. if (colIndex + 1 < columnCount) {
  169. // Next part is a column on same page => same IPD
  170. return 0;
  171. } else {
  172. Page nextPage = getPage(false, pageIndex + 1, RELTO_CURRENT_ELEMENT_LIST);
  173. return page.getPageViewport().getBodyRegion().getIPD()
  174. - nextPage.getPageViewport().getBodyRegion().getIPD();
  175. }
  176. }
  177. /**
  178. * Checks if a break at the passed index would start a new page
  179. * @param index the index of the element before the break
  180. * @return {@code true} if the break starts a new page
  181. */
  182. boolean startPage(int index) {
  183. return getColIndexAndColCount(index)[0] == 0;
  184. }
  185. /**
  186. * Checks if a break at the passed index would end a page
  187. * @param index the index of the element before the break
  188. * @return {@code true} if the break ends a page
  189. */
  190. boolean endPage(int index) {
  191. int[] colIndexAndColCount = getColIndexAndColCount(index);
  192. return colIndexAndColCount[0] == colIndexAndColCount[1] - 1;
  193. }
  194. /**
  195. * Obtain the applicable column-count for the element at the
  196. * passed index
  197. * @param index the index of the element
  198. * @return the number of columns
  199. */
  200. int getColumnCount(int index) {
  201. return getColIndexAndColCount(index)[1];
  202. }
  203. /**
  204. * Returns the part index (0<x<partCount) which denotes the first part on the last page
  205. * generated by the current element list.
  206. * @param partCount Number of parts determined by the breaking algorithm
  207. * @return the requested part index
  208. */
  209. public int getStartingPartIndexForLastPage(int partCount) {
  210. int result = 0;
  211. int idx = 0;
  212. int pageIndex = 0;
  213. int colIndex = startColumnOfCurrentElementList;
  214. Page page = getPage(
  215. false, pageIndex, RELTO_CURRENT_ELEMENT_LIST);
  216. while (idx < partCount) {
  217. if ((colIndex >= page.getPageViewport().getCurrentSpan().getColumnCount())) {
  218. colIndex = 0;
  219. pageIndex++;
  220. page = getPage(
  221. false, pageIndex, RELTO_CURRENT_ELEMENT_LIST);
  222. result = idx;
  223. }
  224. colIndex++;
  225. idx++;
  226. }
  227. return result;
  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. while (intIndex >= cachedPages.size()) {
  271. if (log.isTraceEnabled()) {
  272. log.trace("Caching " + index);
  273. }
  274. cacheNextPage(index, isBlank, isLastPage, this.spanAllForCurrentElementList);
  275. }
  276. Page page = (Page)cachedPages.get(intIndex);
  277. boolean replace = false;
  278. if (page.getPageViewport().isBlank() != isBlank) {
  279. log.debug("blank condition doesn't match. Replacing PageViewport.");
  280. replace = true;
  281. }
  282. if ((isLastPage && indexOfCachedLastPage != intIndex)
  283. || (!isLastPage && indexOfCachedLastPage >= 0)) {
  284. log.debug("last page condition doesn't match. Replacing PageViewport.");
  285. replace = true;
  286. indexOfCachedLastPage = (isLastPage ? intIndex : -1);
  287. }
  288. if (replace) {
  289. discardCacheStartingWith(intIndex);
  290. page = cacheNextPage(index, isBlank, isLastPage, this.spanAllForCurrentElementList);
  291. }
  292. return page;
  293. }
  294. private void discardCacheStartingWith(int index) {
  295. while (index < cachedPages.size()) {
  296. this.cachedPages.remove(cachedPages.size() - 1);
  297. if (!pageSeq.goToPreviousSimplePageMaster()) {
  298. log.warn("goToPreviousSimplePageMaster() on the first page called!");
  299. }
  300. }
  301. }
  302. private Page cacheNextPage(int index, boolean isBlank, boolean isLastPage, boolean spanAll) {
  303. String pageNumberString = pageSeq.makeFormattedPageNumber(index);
  304. boolean isFirstPage = (startPageOfPageSequence == index);
  305. SimplePageMaster spm = pageSeq.getNextSimplePageMaster(
  306. index, isFirstPage, isLastPage, isBlank);
  307. Region body = spm.getRegion(FO_REGION_BODY);
  308. if (!pageSeq.getMainFlow().getFlowName().equals(body.getRegionName())) {
  309. // this is fine by the XSL Rec (fo:flow's flow-name can be mapped to
  310. // any region), but we don't support it yet.
  311. BlockLevelEventProducer eventProducer = BlockLevelEventProducer.Provider.get(
  312. pageSeq.getUserAgent().getEventBroadcaster());
  313. eventProducer.flowNotMappingToRegionBody(this,
  314. pageSeq.getMainFlow().getFlowName(), spm.getMasterName(), spm.getLocator());
  315. }
  316. Page page = new Page(spm, index, pageNumberString, isBlank, spanAll);
  317. //Set unique key obtained from the AreaTreeHandler
  318. page.getPageViewport().setKey(areaTreeHandler.generatePageViewportKey());
  319. page.getPageViewport().setForeignAttributes(spm.getForeignAttributes());
  320. cachedPages.add(page);
  321. return page;
  322. }
  323. }