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.

PageViewport.java 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  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.area;
  19. import java.awt.Rectangle;
  20. import java.io.IOException;
  21. import java.io.ObjectInputStream;
  22. import java.io.ObjectOutputStream;
  23. import java.util.ArrayList;
  24. import java.util.HashMap;
  25. import java.util.List;
  26. import java.util.Map;
  27. import java.util.Set;
  28. import org.apache.commons.logging.Log;
  29. import org.apache.commons.logging.LogFactory;
  30. import org.apache.fop.apps.FOPException;
  31. import org.apache.fop.fo.flow.AbstractRetrieveMarker;
  32. import org.apache.fop.fo.flow.Marker;
  33. import org.apache.fop.fo.flow.Markers;
  34. import org.apache.fop.fo.pagination.SimplePageMaster;
  35. import org.apache.fop.traits.WritingModeTraitsGetter;
  36. import static org.apache.fop.fo.Constants.FO_REGION_BODY;
  37. /**
  38. * Page viewport that specifies the viewport area and holds the page contents.
  39. * This is the top level object for a page and remains valid for the life
  40. * of the document and the area tree.
  41. * This object may be used as a key to reference a page.
  42. * This is the level that creates the page.
  43. * The page (reference area) is then rendered inside the page object
  44. */
  45. public class PageViewport extends AreaTreeObject implements Resolvable {
  46. private Page page;
  47. private Rectangle viewArea;
  48. private String simplePageMasterName;
  49. /**
  50. * Unique key to identify the page. pageNumberString and pageIndex are both no option
  51. * for this.
  52. */
  53. private String pageKey;
  54. private int pageNumber = -1;
  55. private String pageNumberString;
  56. private int pageIndex = -1; //-1 = undetermined
  57. private boolean blank;
  58. private transient PageSequence pageSequence;
  59. // set of IDs that appear first (or exclusively) on this page:
  60. private Set<String> idFirsts = new java.util.HashSet<String>();
  61. // this keeps a list of currently unresolved areas or extensions
  62. // once an idref is resolved it is removed
  63. // when this is empty the page can be rendered
  64. private Map<String, List<Resolvable>> unresolvedIDRefs
  65. = new java.util.HashMap<String, List<Resolvable>>();
  66. private Map<String, List<PageViewport>> pendingResolved;
  67. private Markers pageMarkers;
  68. /**
  69. * logging instance
  70. */
  71. protected static final Log log = LogFactory.getLog(PageViewport.class);
  72. /**
  73. * Create a page viewport.
  74. * @param spm SimplePageMaster indicating the page and region dimensions
  75. * @param pageNumber the page number
  76. * @param pageStr String representation of the page number
  77. * @param blank true if this is a blank page
  78. * @param spanAll true if the first span area spans all columns
  79. */
  80. public PageViewport(SimplePageMaster spm, int pageNumber, String pageStr,
  81. boolean blank, boolean spanAll) {
  82. this.simplePageMasterName = spm.getMasterName();
  83. setExtensionAttachments(spm.getExtensionAttachments());
  84. setForeignAttributes(spm.getForeignAttributes());
  85. this.blank = blank;
  86. int pageWidth = spm.getPageWidth().getValue();
  87. int pageHeight = spm.getPageHeight().getValue();
  88. this.pageNumber = pageNumber;
  89. this.pageNumberString = pageStr;
  90. this.viewArea = new Rectangle(0, 0, pageWidth, pageHeight);
  91. this.page = new Page(spm);
  92. createSpan(spanAll);
  93. }
  94. /**
  95. * Create a page viewport.
  96. * @param spm SimplePageMaster indicating the page and region dimensions
  97. * @param pageNumber the page number
  98. * @param pageStr String representation of the page number
  99. * @param blank true if this is a blank page
  100. */
  101. public PageViewport(SimplePageMaster spm, int pageNumber, String pageStr, boolean blank) {
  102. this(spm, pageNumber, pageStr, blank, false);
  103. }
  104. /**
  105. * Copy constructor.
  106. * @param original the original PageViewport to copy from
  107. * @throws FOPException when cloning of the page is not supported
  108. */
  109. public PageViewport(PageViewport original) throws FOPException {
  110. if (original.extensionAttachments != null) {
  111. setExtensionAttachments(original.extensionAttachments);
  112. }
  113. if (original.foreignAttributes != null) {
  114. setForeignAttributes(original.foreignAttributes);
  115. }
  116. this.pageIndex = original.pageIndex;
  117. this.pageNumber = original.pageNumber;
  118. this.pageNumberString = original.pageNumberString;
  119. try {
  120. this.page = (Page) original.page.clone();
  121. } catch (CloneNotSupportedException e) {
  122. throw new FOPException(e);
  123. }
  124. this.viewArea = new Rectangle(original.viewArea);
  125. this.simplePageMasterName = original.simplePageMasterName;
  126. this.blank = original.blank;
  127. }
  128. /**
  129. * Constructor used by the area tree parser.
  130. * @param viewArea the view area
  131. * @param pageNumber the page number
  132. * @param pageStr String representation of the page number
  133. * @param simplePageMasterName name of the original simple-page-master that generated this page
  134. * @param blank true if this is a blank page
  135. */
  136. public PageViewport(Rectangle viewArea, int pageNumber, String pageStr,
  137. String simplePageMasterName, boolean blank) {
  138. this.viewArea = viewArea;
  139. this.pageNumber = pageNumber;
  140. this.pageNumberString = pageStr;
  141. this.simplePageMasterName = simplePageMasterName;
  142. this.blank = blank;
  143. }
  144. /**
  145. * Sets the page sequence this page belongs to
  146. * @param seq the page sequence
  147. */
  148. public void setPageSequence(PageSequence seq) {
  149. this.pageSequence = seq;
  150. }
  151. /** @return the page sequence this page belongs to */
  152. public PageSequence getPageSequence() {
  153. return this.pageSequence;
  154. }
  155. /**
  156. * Get the view area rectangle of this viewport.
  157. * @return the rectangle for this viewport
  158. */
  159. public Rectangle getViewArea() {
  160. return viewArea;
  161. }
  162. /**
  163. * Get the page reference area with the contents.
  164. * @return the page reference area
  165. */
  166. public Page getPage() {
  167. return page;
  168. }
  169. /**
  170. * Sets the page object for this PageViewport.
  171. * @param page the page
  172. */
  173. public void setPage(Page page) {
  174. this.page = page;
  175. }
  176. /**
  177. * Get the page number of this page.
  178. * @return the integer value that represents this page
  179. */
  180. public int getPageNumber() {
  181. return pageNumber;
  182. }
  183. /**
  184. * Get the page number of this page.
  185. * @return the string that represents this page
  186. */
  187. public String getPageNumberString() {
  188. return pageNumberString;
  189. }
  190. /**
  191. * Sets the page index of the page in this rendering run.
  192. * (This is not the same as the page number!)
  193. * @param index the page index (zero-based), -1 if it is undetermined
  194. */
  195. public void setPageIndex(int index) {
  196. this.pageIndex = index;
  197. }
  198. /**
  199. * @return the overall page index of the page in this rendering run (zero-based,
  200. * -1 if it is undetermined).
  201. */
  202. public int getPageIndex() {
  203. return this.pageIndex;
  204. }
  205. /**
  206. * Sets the unique key for this PageViewport that will be used to reference this page.
  207. * @param key the unique key.
  208. */
  209. public void setKey(String key) {
  210. this.pageKey = key;
  211. }
  212. /**
  213. * Get the key for this page viewport.
  214. * This is used so that a serializable key can be used to
  215. * lookup the page or some other reference.
  216. *
  217. * @return a unique page viewport key for this area tree
  218. */
  219. public String getKey() {
  220. if (this.pageKey == null) {
  221. throw new IllegalStateException("No page key set on the PageViewport: " + toString());
  222. }
  223. return this.pageKey;
  224. }
  225. /**
  226. * Add an "ID-first" to this page.
  227. * This is typically called by the {@link AreaTreeHandler} when associating
  228. * an ID with a {@link PageViewport}.
  229. *
  230. * @param id the id to be registered as first appearing on this page
  231. */
  232. public void setFirstWithID(String id) {
  233. if (id != null) {
  234. idFirsts.add(id);
  235. }
  236. }
  237. /**
  238. * Check whether a certain id first appears on this page
  239. *
  240. * @param id the id to be checked
  241. * @return true if this page is the first where the id appears
  242. */
  243. public boolean isFirstWithID(String id) {
  244. return idFirsts.contains(id);
  245. }
  246. /**
  247. * Add an idref to this page.
  248. * All idrefs found for child areas of this {@link PageViewport} are added
  249. * to unresolvedIDRefs, for subsequent resolution by {@link AreaTreeHandler}
  250. * calls to this object's {@code resolveIDRef()}.
  251. *
  252. * @param idref the idref
  253. * @param res the child element of this page that needs this
  254. * idref resolved
  255. */
  256. public void addUnresolvedIDRef(String idref, Resolvable res) {
  257. if (unresolvedIDRefs == null) {
  258. unresolvedIDRefs = new HashMap<String, List<Resolvable>>();
  259. }
  260. List<Resolvable> pageViewports = unresolvedIDRefs.get(idref);
  261. if (pageViewports == null) {
  262. pageViewports = new ArrayList<Resolvable>();
  263. unresolvedIDRefs.put(idref, pageViewports);
  264. }
  265. pageViewports.add(res);
  266. }
  267. /**
  268. * Check if this page has been fully resolved.
  269. * @return true if the page is resolved and can be rendered
  270. */
  271. public boolean isResolved() {
  272. return unresolvedIDRefs == null
  273. || unresolvedIDRefs.size() == 0;
  274. }
  275. /**
  276. * Get the unresolved idrefs for this page.
  277. * @return String array of idref's that still have not been resolved
  278. */
  279. public String[] getIDRefs() {
  280. return (unresolvedIDRefs == null) ? null
  281. : unresolvedIDRefs.keySet().toArray(
  282. new String[unresolvedIDRefs.keySet().size()]);
  283. }
  284. /** {@inheritDoc} */
  285. public void resolveIDRef(String id, List<PageViewport> pages) {
  286. if (page == null) {
  287. if (pendingResolved == null) {
  288. pendingResolved = new HashMap<String, List<PageViewport>>();
  289. }
  290. pendingResolved.put(id, pages);
  291. } else {
  292. if (unresolvedIDRefs != null) {
  293. List<Resolvable> todo = unresolvedIDRefs.get(id);
  294. if (todo != null) {
  295. for (Resolvable res : todo) {
  296. res.resolveIDRef(id, pages);
  297. }
  298. }
  299. }
  300. }
  301. if (unresolvedIDRefs != null && pages != null) {
  302. unresolvedIDRefs.remove(id);
  303. if (unresolvedIDRefs.isEmpty()) {
  304. unresolvedIDRefs = null;
  305. }
  306. }
  307. }
  308. /**
  309. * Register the markers for this page.
  310. *
  311. * @param marks the map of markers to add
  312. * @param starting if the area being added is starting or ending
  313. * @param isfirst if the area being added has is-first trait
  314. * @param islast if the area being added has is-last trait
  315. */
  316. public void registerMarkers(Map<String, Marker> marks, boolean starting, boolean isfirst, boolean islast) {
  317. if (pageMarkers == null) {
  318. pageMarkers = new Markers();
  319. }
  320. pageMarkers.register(marks, starting, isfirst, islast);
  321. }
  322. /**
  323. * Resolve a marker from this page.
  324. * This will retrieve a marker with the class name
  325. * and position.
  326. *
  327. * @param name The class name of the marker to retrieve
  328. * @param pos the position to retrieve
  329. * @return Object the marker found or null
  330. */
  331. public Marker resolveMarker(AbstractRetrieveMarker rm) {
  332. if (pageMarkers == null) {
  333. return null;
  334. }
  335. return pageMarkers.resolve(rm);
  336. }
  337. /** Dumps the current marker data to the logger. */
  338. public void dumpMarkers() {
  339. if (pageMarkers != null) {
  340. pageMarkers.dump();
  341. }
  342. }
  343. /**
  344. * Save the page contents to an object stream.
  345. * The map of unresolved references are set on the page so that
  346. * the resolvers can be properly serialized and reloaded.
  347. * @param out the object output stream to write the contents
  348. * @throws IOException in case of an I/O error while serializing the page
  349. */
  350. public void savePage(ObjectOutputStream out) throws IOException {
  351. // set the unresolved references so they are serialized
  352. page.setUnresolvedReferences(unresolvedIDRefs);
  353. out.writeObject(page);
  354. page = null;
  355. }
  356. /**
  357. * Load the page contents from an object stream.
  358. * This loads the page contents from the stream and
  359. * if there are any unresolved references that were resolved
  360. * while saved they will be resolved on the page contents.
  361. * @param in the object input stream to read the page from
  362. * @throws ClassNotFoundException if a class was not found while loading the page
  363. * @throws IOException if an I/O error occurred while loading the page
  364. */
  365. public void loadPage(ObjectInputStream in) throws IOException, ClassNotFoundException {
  366. page = (Page) in.readObject();
  367. unresolvedIDRefs = page.getUnresolvedReferences();
  368. if (unresolvedIDRefs != null && pendingResolved != null) {
  369. for (String id : pendingResolved.keySet()) {
  370. resolveIDRef(id, pendingResolved.get(id));
  371. }
  372. pendingResolved = null;
  373. }
  374. }
  375. /** {@inheritDoc} */
  376. public Object clone() throws CloneNotSupportedException {
  377. PageViewport pvp = (PageViewport) super.clone();
  378. pvp.page = (Page) page.clone();
  379. pvp.viewArea = (Rectangle) viewArea.clone();
  380. return pvp;
  381. }
  382. /**
  383. * Clear the page contents to save memory.
  384. * This object is kept for the life of the area tree since
  385. * it holds id and marker information and is used as a key.
  386. */
  387. public void clear() {
  388. page = null;
  389. }
  390. /** {@inheritDoc} */
  391. @Override
  392. public String toString() {
  393. StringBuffer sb = new StringBuffer(64);
  394. sb.append("PageViewport: page=");
  395. sb.append(getPageNumberString());
  396. return sb.toString();
  397. }
  398. /** @return the name of the simple-page-master that created this page */
  399. public String getSimplePageMasterName() {
  400. return this.simplePageMasterName;
  401. }
  402. /** @return True if this is a blank page. */
  403. public boolean isBlank() {
  404. return this.blank;
  405. }
  406. /**
  407. * Convenience method to get BodyRegion of this PageViewport
  408. * @return BodyRegion object
  409. */
  410. public BodyRegion getBodyRegion() {
  411. return (BodyRegion) getPage().getRegionViewport(FO_REGION_BODY).getRegionReference();
  412. }
  413. /**
  414. * Convenience method to create a new Span for this
  415. * this PageViewport.
  416. *
  417. * @param spanAll whether this is a single-column span
  418. * @return Span object created
  419. */
  420. public Span createSpan(boolean spanAll) {
  421. return getBodyRegion().getMainReference().createSpan(spanAll);
  422. }
  423. /**
  424. * Convenience method to get the span-reference-area currently
  425. * being processed
  426. *
  427. * @return span currently being processed.
  428. */
  429. public Span getCurrentSpan() {
  430. return getBodyRegion().getMainReference().getCurrentSpan();
  431. }
  432. /**
  433. * Convenience method to get the normal-flow-reference-area
  434. * currently being processed
  435. *
  436. * @return span currently being processed.
  437. */
  438. public NormalFlow getCurrentFlow() {
  439. return getCurrentSpan().getCurrentFlow();
  440. }
  441. /**
  442. * Convenience method to increment the Span to the
  443. * next NormalFlow to be processed, and to return that flow.
  444. *
  445. * @return the next NormalFlow in the Span.
  446. */
  447. public NormalFlow moveToNextFlow() {
  448. return getCurrentSpan().moveToNextFlow();
  449. }
  450. /**
  451. * Convenience method to return a given region-reference-area,
  452. * keyed by the Constants class identifier for the corresponding
  453. * formatting object (ie. Constants.FO_REGION_BODY, FO_REGION_START,
  454. * etc.)
  455. *
  456. * @param id the Constants class identifier for the region.
  457. * @return the corresponding region-reference-area for this page.
  458. */
  459. public RegionReference getRegionReference(int id) {
  460. return getPage().getRegionViewport(id).getRegionReference();
  461. }
  462. /**
  463. * Sets the writing mode traits for the page associated with this viewport.
  464. * @param wmtg a WM traits getter
  465. */
  466. public void setWritingModeTraits(WritingModeTraitsGetter wmtg) {
  467. if (page != null) {
  468. page.setWritingModeTraits(wmtg);
  469. }
  470. }
  471. }