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.

AbstractPageSequenceLayoutManager.java 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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.AreaTreeModel;
  24. import org.apache.fop.area.IDTracker;
  25. import org.apache.fop.area.PageViewport;
  26. import org.apache.fop.area.Resolvable;
  27. import org.apache.fop.datatypes.Numeric;
  28. import org.apache.fop.fo.Constants;
  29. import org.apache.fop.fo.flow.Marker;
  30. import org.apache.fop.fo.flow.RetrieveMarker;
  31. import org.apache.fop.fo.pagination.AbstractPageSequence;
  32. /**
  33. * Abstract base class for a page sequence layout manager.
  34. */
  35. public abstract class AbstractPageSequenceLayoutManager extends AbstractLayoutManager
  36. implements TopLevelLayoutManager {
  37. private static Log log = LogFactory.getLog(AbstractPageSequenceLayoutManager.class);
  38. /**
  39. * AreaTreeHandler which activates the PSLM and controls
  40. * the rendering of its pages.
  41. */
  42. protected AreaTreeHandler areaTreeHandler;
  43. /** ID tracker supplied by the AreaTreeHandler */
  44. protected IDTracker idTracker;
  45. /** page sequence formatting object being processed by this class */
  46. protected AbstractPageSequence pageSeq;
  47. /** Current page with page-viewport-area being filled by the PSLM. */
  48. protected Page curPage;
  49. /** the current page number */
  50. protected int currentPageNum = 0;
  51. /** The stating page number */
  52. protected int startPageNum = 0;
  53. /**
  54. * Constructor
  55. *
  56. * @param ath the area tree handler object
  57. * @param pseq fo:page-sequence to process
  58. */
  59. public AbstractPageSequenceLayoutManager(AreaTreeHandler ath, AbstractPageSequence pseq) {
  60. super(pseq);
  61. this.areaTreeHandler = ath;
  62. this.idTracker = ath.getIDTracker();
  63. this.pageSeq = pseq;
  64. }
  65. /**
  66. * @return the LayoutManagerMaker object associated to the areaTreeHandler
  67. */
  68. public LayoutManagerMaker getLayoutManagerMaker() {
  69. return areaTreeHandler.getLayoutManagerMaker();
  70. }
  71. /**
  72. * Provides access to the current page.
  73. * @return the current Page
  74. */
  75. public Page getCurrentPage() {
  76. return curPage;
  77. }
  78. /**
  79. * Provides access for setting the current page.
  80. * @param currentPage the new current Page
  81. */
  82. protected void setCurrentPage(Page currentPage) {
  83. this.curPage = currentPage;
  84. }
  85. /**
  86. * Provides access to the current page number
  87. * @return the current page number
  88. */
  89. protected int getCurrentPageNum() {
  90. return currentPageNum;
  91. }
  92. /** {@inheritDoc} */
  93. public void initialize() {
  94. startPageNum = pageSeq.getStartingPageNumber();
  95. currentPageNum = startPageNum - 1;
  96. }
  97. /**
  98. * This returns the first PageViewport that contains an id trait
  99. * matching the idref argument, or null if no such PV exists.
  100. *
  101. * @param idref the idref trait needing to be resolved
  102. * @return the first PageViewport that contains the ID trait
  103. */
  104. public PageViewport getFirstPVWithID(String idref) {
  105. List list = idTracker.getPageViewportsContainingID(idref);
  106. if (list != null && list.size() > 0) {
  107. return (PageViewport) list.get(0);
  108. }
  109. return null;
  110. }
  111. /**
  112. * This returns the last PageViewport that contains an id trait
  113. * matching the idref argument, or null if no such PV exists.
  114. *
  115. * @param idref the idref trait needing to be resolved
  116. * @return the last PageViewport that contains the ID trait
  117. */
  118. public PageViewport getLastPVWithID(String idref) {
  119. List list = idTracker.getPageViewportsContainingID(idref);
  120. if (list != null && list.size() > 0) {
  121. return (PageViewport) list.get(list.size() - 1);
  122. }
  123. return null;
  124. }
  125. /**
  126. * Add an ID reference to the current page.
  127. * When adding areas the area adds its ID reference.
  128. * For the page layout manager it adds the id reference
  129. * with the current page to the area tree.
  130. *
  131. * @param id the ID reference to add
  132. */
  133. public void addIDToPage(String id) {
  134. if (id != null && id.length() > 0) {
  135. idTracker.associateIDWithPageViewport(id, curPage.getPageViewport());
  136. }
  137. }
  138. /**
  139. * Add an id reference of the layout manager in the AreaTreeHandler,
  140. * if the id hasn't been resolved yet
  141. * @param id the id to track
  142. * @return a boolean indicating if the id has already been resolved
  143. * TODO Maybe give this a better name
  144. */
  145. public boolean associateLayoutManagerID(String id) {
  146. if (log.isDebugEnabled()) {
  147. log.debug("associateLayoutManagerID(" + id + ")");
  148. }
  149. if (!idTracker.alreadyResolvedID(id)) {
  150. idTracker.signalPendingID(id);
  151. return false;
  152. } else {
  153. return true;
  154. }
  155. }
  156. /**
  157. * Notify the areaTreeHandler that the LayoutManagers containing
  158. * idrefs have finished creating areas
  159. * @param id the id for which layout has finished
  160. */
  161. public void notifyEndOfLayout(String id) {
  162. idTracker.signalIDProcessed(id);
  163. }
  164. /**
  165. * Identify an unresolved area (one needing an idref to be
  166. * resolved, e.g. the internal-destination of an fo:basic-link)
  167. * for both the AreaTreeHandler and PageViewport object.
  168. *
  169. * The IDTracker keeps a document-wide list of idref's
  170. * and the PV's needing them to be resolved. It uses this to
  171. * send notifications to the PV's when an id has been resolved.
  172. *
  173. * The PageViewport keeps lists of id's needing resolving, along
  174. * with the child areas (page-number-citation, basic-link, etc.)
  175. * of the PV needing their resolution.
  176. *
  177. * @param id the ID reference to add
  178. * @param res the resolvable object that needs resolving
  179. */
  180. public void addUnresolvedArea(String id, Resolvable res) {
  181. curPage.getPageViewport().addUnresolvedIDRef(id, res);
  182. idTracker.addUnresolvedIDRef(id, curPage.getPageViewport());
  183. }
  184. /**
  185. * Bind the RetrieveMarker to the corresponding Marker subtree.
  186. * If the boundary is page then it will only check the
  187. * current page. For page-sequence and document it will
  188. * lookup preceding pages from the area tree and try to find
  189. * a marker.
  190. * If we retrieve a marker from a preceding page,
  191. * then the containing page does not have a qualifying area,
  192. * and all qualifying areas have ended.
  193. * Therefore we use last-ending-within-page (Constants.EN_LEWP)
  194. * as the position.
  195. *
  196. * @param rm the RetrieveMarker instance whose properties are to
  197. * used to find the matching Marker.
  198. * @return a bound RetrieveMarker instance, or null if no Marker
  199. * could be found.
  200. */
  201. public RetrieveMarker resolveRetrieveMarker(RetrieveMarker rm) {
  202. AreaTreeModel areaTreeModel = areaTreeHandler.getAreaTreeModel();
  203. String name = rm.getRetrieveClassName();
  204. int boundary = rm.getRetrieveBoundary();
  205. // get marker from the current markers on area tree
  206. Marker mark = getCurrentPV().resolveMarker(rm);
  207. if (mark == null && boundary != EN_PAGE) {
  208. // go back over pages until mark found
  209. // if document boundary then keep going
  210. boolean doc = (boundary == EN_DOCUMENT);
  211. int seq = areaTreeModel.getPageSequenceCount();
  212. int page = areaTreeModel.getPageCount(seq) - 1;
  213. while (page < 0 && doc && seq > 1) {
  214. seq--;
  215. page = areaTreeModel.getPageCount(seq) - 1;
  216. }
  217. while (page >= 0) {
  218. PageViewport pv = areaTreeModel.getPage(seq, page);
  219. int originalPosition = rm.getPosition();
  220. rm.changePositionTo(Constants.EN_LEWP);
  221. mark = (Marker) pv.resolveMarker(rm);
  222. // this is probably not necessary since the RM will not be used again, but to be safe...
  223. rm.changePositionTo(originalPosition);
  224. if (mark != null) {
  225. break;
  226. }
  227. page--;
  228. if (page < 0 && doc && seq > 1) {
  229. seq--;
  230. page = areaTreeModel.getPageCount(seq) - 1;
  231. }
  232. }
  233. }
  234. if (mark == null) {
  235. log.debug("found no marker with name: " + name);
  236. return null;
  237. } else {
  238. rm.bindMarker(mark);
  239. return rm;
  240. }
  241. }
  242. /**
  243. * Creates and returns a new page.
  244. * @param pageNumber the page number
  245. * @param isBlank true if it's a blank page
  246. * @return the newly created page
  247. */
  248. protected abstract Page createPage(int pageNumber, boolean isBlank);
  249. /**
  250. * Makes a new page
  251. *
  252. * @param isBlank whether this page is blank or not
  253. * @return a new page
  254. */
  255. protected Page makeNewPage(boolean isBlank) {
  256. if (curPage != null) {
  257. finishPage();
  258. }
  259. currentPageNum++;
  260. curPage = createPage(currentPageNum, isBlank);
  261. if (log.isDebugEnabled()) {
  262. log.debug("[" + curPage.getPageViewport().getPageNumberString()
  263. + (isBlank ? "*" : "") + "]");
  264. }
  265. addIDToPage(pageSeq.getRoot().getId());
  266. addIDToPage(pageSeq.getId());
  267. return curPage;
  268. }
  269. /**
  270. * Finishes a page in preparation for a new page.
  271. */
  272. protected void finishPage() {
  273. if (log.isTraceEnabled()) {
  274. curPage.getPageViewport().dumpMarkers();
  275. }
  276. // Try to resolve any unresolved IDs for the current page.
  277. //
  278. idTracker.tryIDResolution(curPage.getPageViewport());
  279. // Queue for ID resolution and rendering
  280. areaTreeHandler.getAreaTreeModel().addPage(curPage.getPageViewport());
  281. if (log.isDebugEnabled()) {
  282. log.debug("page finished: " + curPage.getPageViewport().getPageNumberString()
  283. + ", current num: " + currentPageNum);
  284. }
  285. curPage = null;
  286. }
  287. /** {@inheritDoc} */
  288. public void doForcePageCount(Numeric nextPageSeqInitialPageNumber) {
  289. int forcePageCount = pageSeq.getForcePageCount();
  290. // xsl-spec version 1.0 (15.oct 2001)
  291. // auto | even | odd | end-on-even | end-on-odd | no-force | inherit
  292. // auto:
  293. // Force the last page in this page-sequence to be an odd-page
  294. // if the initial-page-number of the next page-sequence is even.
  295. // Force it to be an even-page
  296. // if the initial-page-number of the next page-sequence is odd.
  297. // If there is no next page-sequence
  298. // or if the value of its initial-page-number is "auto" do not force any page.
  299. // if force-page-count is auto then set the value of forcePageCount
  300. // depending on the initial-page-number of the next page-sequence
  301. if (nextPageSeqInitialPageNumber != null && forcePageCount == Constants.EN_AUTO) {
  302. if (nextPageSeqInitialPageNumber.getEnum() != 0) {
  303. // auto | auto-odd | auto-even
  304. int nextPageSeqPageNumberType = nextPageSeqInitialPageNumber.getEnum();
  305. if (nextPageSeqPageNumberType == Constants.EN_AUTO_ODD) {
  306. forcePageCount = Constants.EN_END_ON_EVEN;
  307. } else if (nextPageSeqPageNumberType == Constants.EN_AUTO_EVEN) {
  308. forcePageCount = Constants.EN_END_ON_ODD;
  309. } else { // auto
  310. forcePageCount = Constants.EN_NO_FORCE;
  311. }
  312. } else { // <integer> for explicit page number
  313. int nextPageSeqPageStart = nextPageSeqInitialPageNumber.getValue();
  314. // spec rule
  315. nextPageSeqPageStart = (nextPageSeqPageStart > 0) ? nextPageSeqPageStart : 1;
  316. if (nextPageSeqPageStart % 2 == 0) { // explicit even startnumber
  317. forcePageCount = Constants.EN_END_ON_ODD;
  318. } else { // explicit odd startnumber
  319. forcePageCount = Constants.EN_END_ON_EVEN;
  320. }
  321. }
  322. }
  323. if (forcePageCount == Constants.EN_EVEN) {
  324. if ((currentPageNum - startPageNum + 1) % 2 != 0) { // we have an odd number of pages
  325. curPage = makeNewPage(true);
  326. }
  327. } else if (forcePageCount == Constants.EN_ODD) {
  328. if ((currentPageNum - startPageNum + 1) % 2 == 0) { // we have an even number of pages
  329. curPage = makeNewPage(true);
  330. }
  331. } else if (forcePageCount == Constants.EN_END_ON_EVEN) {
  332. if (currentPageNum % 2 != 0) { // we are now on an odd page
  333. curPage = makeNewPage(true);
  334. }
  335. } else if (forcePageCount == Constants.EN_END_ON_ODD) {
  336. if (currentPageNum % 2 == 0) { // we are now on an even page
  337. curPage = makeNewPage(true);
  338. }
  339. } else if (forcePageCount == Constants.EN_NO_FORCE) {
  340. // i hope: nothing special at all
  341. }
  342. if (curPage != null) {
  343. finishPage();
  344. }
  345. }
  346. /** {@inheritDoc} */
  347. public void reset() {
  348. throw new IllegalStateException();
  349. }
  350. }