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

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