Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

PageProvider.java 15KB

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