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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677
  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.awt.geom.Rectangle2D;
  21. import java.io.IOException;
  22. import java.io.ObjectInputStream;
  23. import java.io.ObjectOutputStream;
  24. import java.util.ArrayList;
  25. import java.util.Collections;
  26. import java.util.HashMap;
  27. import java.util.Iterator;
  28. import java.util.List;
  29. import java.util.Map;
  30. import java.util.Set;
  31. import org.apache.commons.logging.Log;
  32. import org.apache.commons.logging.LogFactory;
  33. import org.apache.fop.fo.Constants;
  34. import org.apache.fop.fo.extensions.ExtensionAttachment;
  35. import org.apache.fop.fo.pagination.SimplePageMaster;
  36. /**
  37. * Page viewport that specifies the viewport area and holds the page contents.
  38. * This is the top level object for a page and remains valid for the life
  39. * of the document and the area tree.
  40. * This object may be used as a key to reference a page.
  41. * This is the level that creates the page.
  42. * The page (reference area) is then rendered inside the page object
  43. */
  44. public class PageViewport extends AreaTreeObject implements Resolvable, Cloneable {
  45. private Page page;
  46. private Rectangle2D viewArea;
  47. private String simplePageMasterName;
  48. /**
  49. * Unique key to identify the page. pageNumberString and pageIndex are both no option
  50. * for this.
  51. */
  52. private String pageKey;
  53. private int pageNumber = -1;
  54. private String pageNumberString = null;
  55. private int pageIndex = -1; //-1 = undetermined
  56. private boolean blank;
  57. private transient PageSequence pageSequence;
  58. // list of id references and the rectangle on the page
  59. //private Map idReferences = null;
  60. // set of IDs that appear first (or exclusively) on this page:
  61. private Set idFirsts = new java.util.HashSet();
  62. // this keeps a list of currently unresolved areas or extensions
  63. // once an idref is resolved it is removed
  64. // when this is empty the page can be rendered
  65. private Map unresolvedIDRefs = new java.util.HashMap();
  66. private Map pendingResolved = null;
  67. // hashmap of markers for this page
  68. // start and end are added by the fo that contains the markers
  69. private Map markerFirstStart = null;
  70. private Map markerLastStart = null;
  71. private Map markerFirstAny = null;
  72. private Map markerLastEnd = null;
  73. private Map markerLastAny = null;
  74. //Arbitrary attachments to the page from extensions that need to pass information
  75. //down to the renderers.
  76. private List extensionAttachments = null;
  77. /**
  78. * logging instance
  79. */
  80. protected static Log log = LogFactory.getLog(PageViewport.class);
  81. /**
  82. * Create a page viewport.
  83. * @param spm SimplePageMaster indicating the page and region dimensions
  84. * @param pageNumber the page number
  85. * @param pageStr String representation of the page number
  86. * @param blank true if this is a blank page
  87. */
  88. public PageViewport(SimplePageMaster spm, int pageNumber, String pageStr, boolean blank) {
  89. this.simplePageMasterName = spm.getMasterName();
  90. this.extensionAttachments = spm.getExtensionAttachments();
  91. this.blank = blank;
  92. int pageWidth = spm.getPageWidth().getValue();
  93. int pageHeight = spm.getPageHeight().getValue();
  94. this.pageNumber = pageNumber;
  95. this.pageNumberString = pageStr;
  96. this.viewArea = new Rectangle(0, 0, pageWidth, pageHeight);
  97. this.page = new Page(spm);
  98. createSpan(false);
  99. }
  100. /**
  101. * Copy constructor.
  102. * @param original the original PageViewport to copy from
  103. */
  104. public PageViewport(PageViewport original) {
  105. if (original.extensionAttachments != null) {
  106. this.extensionAttachments = new java.util.ArrayList(original.extensionAttachments);
  107. }
  108. this.pageIndex = original.pageIndex;
  109. this.pageNumber = original.pageNumber;
  110. this.pageNumberString = original.pageNumberString;
  111. this.page = (Page)original.page.clone();
  112. this.viewArea = (Rectangle2D)original.viewArea.clone();
  113. this.simplePageMasterName = original.simplePageMasterName;
  114. this.blank = original.blank;
  115. }
  116. /**
  117. * Constructor used by the area tree parser.
  118. * @param viewArea the view area
  119. * @param pageNumber the page number
  120. * @param pageStr String representation of the page number
  121. * @param simplePageMasterName name of the original simple-page-master that generated this page
  122. * @param blank true if this is a blank page
  123. */
  124. public PageViewport(Rectangle2D viewArea, int pageNumber, String pageStr,
  125. String simplePageMasterName, boolean blank) {
  126. this.viewArea = viewArea;
  127. this.pageNumber = pageNumber;
  128. this.pageNumberString = pageStr;
  129. this.simplePageMasterName = simplePageMasterName;
  130. this.blank = blank;
  131. }
  132. /**
  133. * Sets the page sequence this page belongs to
  134. * @param seq the page sequence
  135. */
  136. public void setPageSequence(PageSequence seq) {
  137. this.pageSequence = seq;
  138. }
  139. /** @return the page sequence this page belongs to */
  140. public PageSequence getPageSequence() {
  141. return this.pageSequence;
  142. }
  143. /**
  144. * Get the view area rectangle of this viewport.
  145. * @return the rectangle for this viewport
  146. */
  147. public Rectangle2D getViewArea() {
  148. return viewArea;
  149. }
  150. /**
  151. * Get the page reference area with the contents.
  152. * @return the page reference area
  153. */
  154. public Page getPage() {
  155. return page;
  156. }
  157. /**
  158. * Sets the page object for this PageViewport.
  159. * @param page the page
  160. */
  161. public void setPage(Page page) {
  162. this.page = page;
  163. }
  164. /**
  165. * Get the page number of this page.
  166. * @return the integer value that represents this page
  167. */
  168. public int getPageNumber() {
  169. return pageNumber;
  170. }
  171. /**
  172. * Get the page number of this page.
  173. * @return the string that represents this page
  174. */
  175. public String getPageNumberString() {
  176. return pageNumberString;
  177. }
  178. /**
  179. * Sets the page index of the page in this rendering run.
  180. * (This is not the same as the page number!)
  181. * @param index the page index (zero-based), -1 if it is undetermined
  182. */
  183. public void setPageIndex(int index) {
  184. this.pageIndex = index;
  185. }
  186. /**
  187. * @return the overall page index of the page in this rendering run (zero-based,
  188. * -1 if it is undetermined).
  189. */
  190. public int getPageIndex() {
  191. return this.pageIndex;
  192. }
  193. /**
  194. * Sets the unique key for this PageViewport that will be used to reference this page.
  195. * @param key the unique key.
  196. */
  197. public void setKey(String key) {
  198. this.pageKey = key;
  199. }
  200. /**
  201. * Get the key for this page viewport.
  202. * This is used so that a serializable key can be used to
  203. * lookup the page or some other reference.
  204. *
  205. * @return a unique page viewport key for this area tree
  206. */
  207. public String getKey() {
  208. if (this.pageKey == null) {
  209. throw new IllegalStateException("No page key set on the PageViewport: " + toString());
  210. }
  211. return this.pageKey;
  212. }
  213. /**
  214. * Add an "ID-first" to this page.
  215. * This is typically called by the AreaTreeHandler when associating
  216. * an ID with a PageViewport.
  217. *
  218. * @param id the id to be registered as first appearing on this page
  219. */
  220. public void setFirstWithID(String id) {
  221. if (id != null) {
  222. idFirsts.add(id);
  223. }
  224. }
  225. /**
  226. * Check whether a certain id first appears on this page
  227. *
  228. * @param id the id to be checked
  229. * @return true if this page is the first where the id appears
  230. */
  231. public boolean isFirstWithID(String id) {
  232. return idFirsts.contains(id);
  233. }
  234. /**
  235. * Add an idref to this page.
  236. * All idrefs found for child areas of this PageViewport are added
  237. * to unresolvedIDRefs, for subsequent resolution by AreaTreeHandler
  238. * calls to this object's resolveIDRef().
  239. *
  240. * @param idref the idref
  241. * @param res the child element of this page that needs this
  242. * idref resolved
  243. */
  244. public void addUnresolvedIDRef(String idref, Resolvable res) {
  245. if (unresolvedIDRefs == null) {
  246. unresolvedIDRefs = new HashMap();
  247. }
  248. List list = (List)unresolvedIDRefs.get(idref);
  249. if (list == null) {
  250. list = new ArrayList();
  251. unresolvedIDRefs.put(idref, list);
  252. }
  253. list.add(res);
  254. }
  255. /**
  256. * Check if this page has been fully resolved.
  257. * @return true if the page is resolved and can be rendered
  258. */
  259. public boolean isResolved() {
  260. return unresolvedIDRefs == null
  261. || unresolvedIDRefs.size() == 0;
  262. }
  263. /**
  264. * Get the unresolved idrefs for this page.
  265. * @return String array of idref's that still have not been resolved
  266. */
  267. public String[] getIDRefs() {
  268. return (unresolvedIDRefs == null) ? null
  269. : (String[]) unresolvedIDRefs.keySet().toArray(new String[] {});
  270. }
  271. /**
  272. * {@inheritDoc}
  273. */
  274. public void resolveIDRef(String id, List pages) {
  275. if (page == null) {
  276. if (pendingResolved == null) {
  277. pendingResolved = new HashMap();
  278. }
  279. pendingResolved.put(id, pages);
  280. } else {
  281. if (unresolvedIDRefs != null) {
  282. List todo = (List)unresolvedIDRefs.get(id);
  283. if (todo != null) {
  284. for (int count = 0; count < todo.size(); count++) {
  285. Resolvable res = (Resolvable)todo.get(count);
  286. res.resolveIDRef(id, pages);
  287. }
  288. }
  289. }
  290. }
  291. if (unresolvedIDRefs != null && pages != null) {
  292. unresolvedIDRefs.remove(id);
  293. if (unresolvedIDRefs.isEmpty()) {
  294. unresolvedIDRefs = null;
  295. }
  296. }
  297. }
  298. /**
  299. * Add the markers for this page.
  300. * Only the required markers are kept.
  301. * For "first-starting-within-page" it adds the markers
  302. * that are starting only if the marker class name is not
  303. * already added.
  304. * For "first-including-carryover" it adds any starting marker
  305. * if the marker class name is not already added.
  306. * For "last-starting-within-page" it adds all marks that
  307. * are starting, replacing earlier markers.
  308. * For "last-ending-within-page" it adds all markers that
  309. * are ending, replacing earlier markers.
  310. *
  311. * Should this logic be placed in the Page layout manager.
  312. *
  313. * @param marks the map of markers to add
  314. * @param starting if the area being added is starting or ending
  315. * @param isfirst if the area being added has is-first trait
  316. * @param islast if the area being added has is-last trait
  317. */
  318. public void addMarkers(Map marks, boolean starting,
  319. boolean isfirst, boolean islast) {
  320. if (marks == null) {
  321. return;
  322. }
  323. if (log.isDebugEnabled()) {
  324. log.debug("--" + marks.keySet() + ": "
  325. + (starting ? "starting" : "ending")
  326. + (isfirst ? ", first" : "")
  327. + (islast ? ", last" : ""));
  328. }
  329. // at the start of the area, register is-first and any areas
  330. if (starting) {
  331. if (isfirst) {
  332. if (markerFirstStart == null) {
  333. markerFirstStart = new HashMap();
  334. }
  335. if (markerFirstAny == null) {
  336. markerFirstAny = new HashMap();
  337. }
  338. // first on page: only put in new values, leave current
  339. for (Iterator iter = marks.keySet().iterator(); iter.hasNext();) {
  340. Object key = iter.next();
  341. if (!markerFirstStart.containsKey(key)) {
  342. markerFirstStart.put(key, marks.get(key));
  343. if (log.isTraceEnabled()) {
  344. log.trace("page " + pageNumberString + ": "
  345. + "Adding marker " + key + " to FirstStart");
  346. }
  347. }
  348. if (!markerFirstAny.containsKey(key)) {
  349. markerFirstAny.put(key, marks.get(key));
  350. if (log.isTraceEnabled()) {
  351. log.trace("page " + pageNumberString + ": "
  352. + "Adding marker " + key + " to FirstAny");
  353. }
  354. }
  355. }
  356. if (markerLastStart == null) {
  357. markerLastStart = new HashMap();
  358. }
  359. // last on page: replace all
  360. markerLastStart.putAll(marks);
  361. if (log.isTraceEnabled()) {
  362. log.trace("page " + pageNumberString + ": "
  363. + "Adding all markers to LastStart");
  364. }
  365. } else {
  366. if (markerFirstAny == null) {
  367. markerFirstAny = new HashMap();
  368. }
  369. // first on page: only put in new values, leave current
  370. for (Iterator iter = marks.keySet().iterator(); iter.hasNext();) {
  371. Object key = iter.next();
  372. if (!markerFirstAny.containsKey(key)) {
  373. markerFirstAny.put(key, marks.get(key));
  374. if (log.isTraceEnabled()) {
  375. log.trace("page " + pageNumberString + ": "
  376. + "Adding marker " + key + " to FirstAny");
  377. }
  378. }
  379. }
  380. }
  381. } else {
  382. // at the end of the area, register is-last and any areas
  383. if (islast) {
  384. if (markerLastEnd == null) {
  385. markerLastEnd = new HashMap();
  386. }
  387. // last on page: replace all
  388. markerLastEnd.putAll(marks);
  389. if (log.isTraceEnabled()) {
  390. log.trace("page " + pageNumberString + ": "
  391. + "Adding all markers to LastEnd");
  392. }
  393. }
  394. if (markerLastAny == null) {
  395. markerLastAny = new HashMap();
  396. }
  397. // last on page: replace all
  398. markerLastAny.putAll(marks);
  399. if (log.isTraceEnabled()) {
  400. log.trace("page " + pageNumberString + ": "
  401. + "Adding all markers to LastAny");
  402. }
  403. }
  404. }
  405. /**
  406. * Get a marker from this page.
  407. * This will retrieve a marker with the class name
  408. * and position.
  409. *
  410. * @param name The class name of the marker to retrieve
  411. * @param pos the position to retrieve
  412. * @return Object the marker found or null
  413. */
  414. public Object getMarker(String name, int pos) {
  415. Object mark = null;
  416. String posName = null;
  417. switch (pos) {
  418. case Constants.EN_FSWP:
  419. if (markerFirstStart != null) {
  420. mark = markerFirstStart.get(name);
  421. posName = "FSWP";
  422. }
  423. if (mark == null && markerFirstAny != null) {
  424. mark = markerFirstAny.get(name);
  425. posName = "FirstAny after " + posName;
  426. }
  427. break;
  428. case Constants.EN_FIC:
  429. if (markerFirstAny != null) {
  430. mark = markerFirstAny.get(name);
  431. posName = "FIC";
  432. }
  433. break;
  434. case Constants.EN_LSWP:
  435. if (markerLastStart != null) {
  436. mark = markerLastStart.get(name);
  437. posName = "LSWP";
  438. }
  439. if (mark == null && markerLastAny != null) {
  440. mark = markerLastAny.get(name);
  441. posName = "LastAny after " + posName;
  442. }
  443. break;
  444. case Constants.EN_LEWP:
  445. if (markerLastEnd != null) {
  446. mark = markerLastEnd.get(name);
  447. posName = "LEWP";
  448. }
  449. if (mark == null && markerLastAny != null) {
  450. mark = markerLastAny.get(name);
  451. posName = "LastAny after " + posName;
  452. }
  453. break;
  454. default:
  455. throw new RuntimeException();
  456. }
  457. if (log.isTraceEnabled()) {
  458. log.trace("page " + pageNumberString + ": " + "Retrieving marker " + name
  459. + " at position " + posName);
  460. }
  461. return mark;
  462. }
  463. /** Dumps the current marker data to the logger. */
  464. public void dumpMarkers() {
  465. if (log.isTraceEnabled()) {
  466. log.trace("FirstAny: " + this.markerFirstAny);
  467. log.trace("FirstStart: " + this.markerFirstStart);
  468. log.trace("LastAny: " + this.markerLastAny);
  469. log.trace("LastEnd: " + this.markerLastEnd);
  470. log.trace("LastStart: " + this.markerLastStart);
  471. }
  472. }
  473. /**
  474. * Save the page contents to an object stream.
  475. * The map of unresolved references are set on the page so that
  476. * the resolvers can be properly serialized and reloaded.
  477. * @param out the object output stream to write the contents
  478. * @throws IOException in case of an I/O error while serializing the page
  479. */
  480. public void savePage(ObjectOutputStream out) throws IOException {
  481. // set the unresolved references so they are serialized
  482. page.setUnresolvedReferences(unresolvedIDRefs);
  483. out.writeObject(page);
  484. page = null;
  485. }
  486. /**
  487. * Load the page contents from an object stream.
  488. * This loads the page contents from the stream and
  489. * if there are any unresolved references that were resolved
  490. * while saved they will be resolved on the page contents.
  491. * @param in the object input stream to read the page from
  492. * @throws ClassNotFoundException if a class was not found while loading the page
  493. * @throws IOException if an I/O error occurred while loading the page
  494. */
  495. public void loadPage(ObjectInputStream in) throws IOException, ClassNotFoundException {
  496. page = (Page) in.readObject();
  497. unresolvedIDRefs = page.getUnresolvedReferences();
  498. if (unresolvedIDRefs != null && pendingResolved != null) {
  499. for (Iterator iter = pendingResolved.keySet().iterator();
  500. iter.hasNext();) {
  501. String id = (String) iter.next();
  502. resolveIDRef(id, (List)pendingResolved.get(id));
  503. }
  504. pendingResolved = null;
  505. }
  506. }
  507. /**
  508. * Clone this page.
  509. * Used by the page master to create a copy of an original page.
  510. * @return a copy of this page and associated viewports
  511. */
  512. public Object clone() {
  513. return new PageViewport(this);
  514. }
  515. /**
  516. * Clear the page contents to save memory.
  517. * This object is kept for the life of the area tree since
  518. * it holds id and marker information and is used as a key.
  519. */
  520. public void clear() {
  521. page = null;
  522. }
  523. /**
  524. * {@inheritDoc}
  525. */
  526. public String toString() {
  527. StringBuffer sb = new StringBuffer(64);
  528. sb.append("PageViewport: page=");
  529. sb.append(getPageNumberString());
  530. return sb.toString();
  531. }
  532. /** @return the name of the simple-page-master that created this page */
  533. public String getSimplePageMasterName() {
  534. return this.simplePageMasterName;
  535. }
  536. /**
  537. * Adds a new ExtensionAttachment instance to this page.
  538. * @param attachment the ExtensionAttachment
  539. */
  540. public void addExtensionAttachment(ExtensionAttachment attachment) {
  541. if (this.extensionAttachments == null) {
  542. this.extensionAttachments = new java.util.ArrayList();
  543. }
  544. extensionAttachments.add(attachment);
  545. }
  546. /** @return the list of extension attachments for this page */
  547. public List getExtensionAttachments() {
  548. if (this.extensionAttachments == null) {
  549. return Collections.EMPTY_LIST;
  550. } else {
  551. return this.extensionAttachments;
  552. }
  553. }
  554. /** @return True if this is a blank page. */
  555. public boolean isBlank() {
  556. return this.blank;
  557. }
  558. /**
  559. * Convenience method to get BodyRegion of this PageViewport
  560. * @return BodyRegion object
  561. */
  562. public BodyRegion getBodyRegion() {
  563. return (BodyRegion) getPage().getRegionViewport(
  564. Constants.FO_REGION_BODY).getRegionReference();
  565. }
  566. /**
  567. * Convenience method to create a new Span for this
  568. * this PageViewport.
  569. *
  570. * @param spanAll whether this is a single-column span
  571. * @return Span object created
  572. */
  573. public Span createSpan(boolean spanAll) {
  574. return getBodyRegion().getMainReference().createSpan(spanAll);
  575. }
  576. /**
  577. * Convenience method to get the span-reference-area currently
  578. * being processed
  579. *
  580. * @return span currently being processed.
  581. */
  582. public Span getCurrentSpan() {
  583. return getBodyRegion().getMainReference().getCurrentSpan();
  584. }
  585. /**
  586. * Convenience method to get the normal-flow-reference-area
  587. * currently being processed
  588. *
  589. * @return span currently being processed.
  590. */
  591. public NormalFlow getCurrentFlow() {
  592. return getCurrentSpan().getCurrentFlow();
  593. }
  594. /**
  595. * Convenience method to increment the Span to the
  596. * next NormalFlow to be processed, and to return that flow.
  597. *
  598. * @return the next NormalFlow in the Span.
  599. */
  600. public NormalFlow moveToNextFlow() {
  601. return getCurrentSpan().moveToNextFlow();
  602. }
  603. /**
  604. * Convenience method to return a given region-reference-area,
  605. * keyed by the Constants class identifier for the corresponding
  606. * formatting object (ie. Constants.FO_REGION_BODY, FO_REGION_START,
  607. * etc.)
  608. *
  609. * @param id the Constants class identifier for the region.
  610. * @return the corresponding region-reference-area for this page.
  611. */
  612. public RegionReference getRegionReference(int id) {
  613. return getPage().getRegionViewport(id).getRegionReference();
  614. }
  615. /** @return whether this page viewport has any extension attachments */
  616. public boolean hasExtensionAttachments() {
  617. return this.extensionAttachments != null && !this.extensionAttachments.isEmpty();
  618. }
  619. }