Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

PageViewport.java 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  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. * Replace the old view port. This copies all ID related fields from the old view port
  248. * to the current one.
  249. * @param oldViewPort old view port
  250. */
  251. public void replace(PageViewport oldViewPort) {
  252. this.idFirsts.addAll(oldViewPort.idFirsts);
  253. this.unresolvedIDRefs.putAll(oldViewPort.unresolvedIDRefs);
  254. if (oldViewPort.pendingResolved != null) {
  255. this.pendingResolved.putAll(oldViewPort.pendingResolved);
  256. }
  257. }
  258. /**
  259. * Add an idref to this page.
  260. * All idrefs found for child areas of this {@link PageViewport} are added
  261. * to unresolvedIDRefs, for subsequent resolution by {@link AreaTreeHandler}
  262. * calls to this object's {@code resolveIDRef()}.
  263. *
  264. * @param idref the idref
  265. * @param res the child element of this page that needs this
  266. * idref resolved
  267. */
  268. public void addUnresolvedIDRef(String idref, Resolvable res) {
  269. if (unresolvedIDRefs == null) {
  270. unresolvedIDRefs = new HashMap<String, List<Resolvable>>();
  271. }
  272. List<Resolvable> pageViewports = unresolvedIDRefs.get(idref);
  273. if (pageViewports == null) {
  274. pageViewports = new ArrayList<Resolvable>();
  275. unresolvedIDRefs.put(idref, pageViewports);
  276. }
  277. pageViewports.add(res);
  278. }
  279. /**
  280. * Check if this page has been fully resolved.
  281. * @return true if the page is resolved and can be rendered
  282. */
  283. public boolean isResolved() {
  284. return unresolvedIDRefs == null
  285. || unresolvedIDRefs.size() == 0;
  286. }
  287. /**
  288. * Get the unresolved idrefs for this page.
  289. * @return String array of idref's that still have not been resolved
  290. */
  291. public String[] getIDRefs() {
  292. return (unresolvedIDRefs == null) ? null
  293. : unresolvedIDRefs.keySet().toArray(
  294. new String[unresolvedIDRefs.keySet().size()]);
  295. }
  296. /** {@inheritDoc} */
  297. public void resolveIDRef(String id, List<PageViewport> pages) {
  298. if (page == null) {
  299. if (pendingResolved == null) {
  300. pendingResolved = new HashMap<String, List<PageViewport>>();
  301. }
  302. pendingResolved.put(id, pages);
  303. } else {
  304. if (unresolvedIDRefs != null) {
  305. List<Resolvable> todo = unresolvedIDRefs.get(id);
  306. if (todo != null) {
  307. for (Resolvable res : todo) {
  308. res.resolveIDRef(id, pages);
  309. }
  310. }
  311. }
  312. }
  313. if (unresolvedIDRefs != null && pages != null) {
  314. unresolvedIDRefs.remove(id);
  315. if (unresolvedIDRefs.isEmpty()) {
  316. unresolvedIDRefs = null;
  317. }
  318. }
  319. }
  320. /**
  321. * Register the markers for this page.
  322. *
  323. * @param marks the map of markers to add
  324. * @param starting if the area being added is starting or ending
  325. * @param isfirst if the area being added has is-first trait
  326. * @param islast if the area being added has is-last trait
  327. */
  328. public void registerMarkers(Map<String, Marker> marks, boolean starting, boolean isfirst, boolean islast) {
  329. if (pageMarkers == null) {
  330. pageMarkers = new Markers();
  331. }
  332. pageMarkers.register(marks, starting, isfirst, islast);
  333. }
  334. /**
  335. * Resolve a marker from this page.
  336. * This will retrieve a marker with the class name
  337. * and position.
  338. *
  339. * @param rm the retrieve-marker instance
  340. * @return Object the marker found or null
  341. */
  342. public Marker resolveMarker(AbstractRetrieveMarker rm) {
  343. if (pageMarkers == null) {
  344. return null;
  345. }
  346. return pageMarkers.resolve(rm);
  347. }
  348. /** Dumps the current marker data to the logger. */
  349. public void dumpMarkers() {
  350. if (pageMarkers != null) {
  351. pageMarkers.dump();
  352. }
  353. }
  354. /**
  355. * Save the page contents to an object stream.
  356. * The map of unresolved references are set on the page so that
  357. * the resolvers can be properly serialized and reloaded.
  358. * @param out the object output stream to write the contents
  359. * @throws IOException in case of an I/O error while serializing the page
  360. */
  361. public void savePage(ObjectOutputStream out) throws IOException {
  362. // set the unresolved references so they are serialized
  363. page.setUnresolvedReferences(unresolvedIDRefs);
  364. out.writeObject(page);
  365. page = null;
  366. }
  367. /**
  368. * Load the page contents from an object stream.
  369. * This loads the page contents from the stream and
  370. * if there are any unresolved references that were resolved
  371. * while saved they will be resolved on the page contents.
  372. * @param in the object input stream to read the page from
  373. * @throws ClassNotFoundException if a class was not found while loading the page
  374. * @throws IOException if an I/O error occurred while loading the page
  375. */
  376. public void loadPage(ObjectInputStream in) throws IOException, ClassNotFoundException {
  377. page = (Page) in.readObject();
  378. unresolvedIDRefs = page.getUnresolvedReferences();
  379. if (unresolvedIDRefs != null && pendingResolved != null) {
  380. for (Map.Entry<String, List<PageViewport>> e : pendingResolved.entrySet()) {
  381. resolveIDRef(e.getKey(), e.getValue());
  382. }
  383. pendingResolved = null;
  384. }
  385. }
  386. /** {@inheritDoc} */
  387. public Object clone() throws CloneNotSupportedException {
  388. PageViewport pvp = (PageViewport) super.clone();
  389. pvp.page = (Page) page.clone();
  390. pvp.viewArea = (Rectangle) viewArea.clone();
  391. return pvp;
  392. }
  393. /**
  394. * Clear the page contents to save memory.
  395. * This object is kept for the life of the area tree since
  396. * it holds id and marker information and is used as a key.
  397. */
  398. public void clear() {
  399. page = null;
  400. }
  401. /** {@inheritDoc} */
  402. @Override
  403. public String toString() {
  404. StringBuffer sb = new StringBuffer(64);
  405. sb.append("PageViewport: page=");
  406. sb.append(getPageNumberString());
  407. return sb.toString();
  408. }
  409. /** @return the name of the simple-page-master that created this page */
  410. public String getSimplePageMasterName() {
  411. return this.simplePageMasterName;
  412. }
  413. /** @return True if this is a blank page. */
  414. public boolean isBlank() {
  415. return this.blank;
  416. }
  417. /**
  418. * Convenience method to get BodyRegion of this PageViewport
  419. * @return BodyRegion object
  420. */
  421. public BodyRegion getBodyRegion() {
  422. RegionReference regionReference = getPage().getRegionViewport(FO_REGION_BODY).getRegionReference();
  423. assert (regionReference instanceof BodyRegion);
  424. return (BodyRegion) regionReference;
  425. }
  426. /**
  427. * Convenience method to create a new Span for this
  428. * this PageViewport.
  429. *
  430. * @param spanAll whether this is a single-column span
  431. * @return Span object created
  432. */
  433. public Span createSpan(boolean spanAll) {
  434. return getBodyRegion().getMainReference().createSpan(spanAll);
  435. }
  436. /**
  437. * Convenience method to get the span-reference-area currently
  438. * being processed
  439. *
  440. * @return span currently being processed.
  441. */
  442. public Span getCurrentSpan() {
  443. return getBodyRegion().getMainReference().getCurrentSpan();
  444. }
  445. /**
  446. * Convenience method to get the normal-flow-reference-area
  447. * currently being processed
  448. *
  449. * @return span currently being processed.
  450. */
  451. public NormalFlow getCurrentFlow() {
  452. return getCurrentSpan().getCurrentFlow();
  453. }
  454. /**
  455. * Convenience method to increment the Span to the
  456. * next NormalFlow to be processed, and to return that flow.
  457. *
  458. * @return the next NormalFlow in the Span.
  459. */
  460. public NormalFlow moveToNextFlow() {
  461. return getCurrentSpan().moveToNextFlow();
  462. }
  463. /**
  464. * Convenience method to return a given region-reference-area,
  465. * keyed by the Constants class identifier for the corresponding
  466. * formatting object (ie. Constants.FO_REGION_BODY, FO_REGION_START,
  467. * etc.)
  468. *
  469. * @param id the Constants class identifier for the region.
  470. * @return the corresponding region-reference-area for this page.
  471. */
  472. public RegionReference getRegionReference(int id) {
  473. return getPage().getRegionViewport(id).getRegionReference();
  474. }
  475. /**
  476. * Sets the writing mode traits for the page associated with this viewport.
  477. * @param wmtg a WM traits getter
  478. */
  479. public void setWritingModeTraits(WritingModeTraitsGetter wmtg) {
  480. if (page != null) {
  481. page.setWritingModeTraits(wmtg);
  482. }
  483. }
  484. }