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.

AbstractLayoutManager.java 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  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.ArrayList;
  20. import java.util.Iterator;
  21. import java.util.List;
  22. import java.util.ListIterator;
  23. import java.util.Map;
  24. import org.apache.commons.logging.Log;
  25. import org.apache.commons.logging.LogFactory;
  26. import org.apache.fop.area.Area;
  27. import org.apache.fop.area.AreaTreeObject;
  28. import org.apache.fop.area.PageViewport;
  29. import org.apache.fop.fo.Constants;
  30. import org.apache.fop.fo.FONode;
  31. import org.apache.fop.fo.FObj;
  32. import org.apache.fop.fo.flow.RetrieveMarker;
  33. /**
  34. * The base class for most LayoutManagers.
  35. */
  36. public abstract class AbstractLayoutManager extends AbstractBaseLayoutManager
  37. implements Constants {
  38. /**
  39. * logging instance
  40. */
  41. private static Log log = LogFactory.getLog(AbstractLayoutManager.class);
  42. /** Parent LayoutManager for this LayoutManager */
  43. protected LayoutManager parentLayoutManager;
  44. /** List of child LayoutManagers */
  45. protected List childLMs;
  46. /** Iterator for child LayoutManagers */
  47. protected ListIterator fobjIter;
  48. /** Marker map for markers related to this LayoutManager */
  49. private Map markers;
  50. /** True if this LayoutManager has handled all of its content. */
  51. private boolean isFinished;
  52. /** child LM during getNextKnuthElement phase */
  53. protected LayoutManager curChildLM;
  54. /** child LM iterator during getNextKnuthElement phase */
  55. protected ListIterator childLMiter;
  56. private int lastGeneratedPosition = -1;
  57. private int smallestPosNumberChecked = Integer.MAX_VALUE;
  58. /**
  59. * Abstract layout manager.
  60. */
  61. public AbstractLayoutManager() {
  62. }
  63. /**
  64. * Abstract layout manager.
  65. *
  66. * @param fo the formatting object for this layout manager
  67. */
  68. public AbstractLayoutManager(FObj fo) {
  69. super(fo);
  70. if (fo == null) {
  71. throw new IllegalStateException("Null formatting object found.");
  72. }
  73. markers = fo.getMarkers();
  74. fobjIter = fo.getChildNodes();
  75. childLMiter = new LMiter(this);
  76. }
  77. /** {@inheritDoc} */
  78. public void setParent(LayoutManager lm) {
  79. this.parentLayoutManager = lm;
  80. }
  81. /** {@inheritDoc} */
  82. public LayoutManager getParent() {
  83. return this.parentLayoutManager;
  84. }
  85. /** {@inheritDoc} */
  86. public void initialize() {
  87. // Empty
  88. }
  89. /**
  90. * Return currently active child LayoutManager or null if
  91. * all children have finished layout.
  92. * Note: child must implement LayoutManager! If it doesn't, skip it
  93. * and print a warning.
  94. * @return the current child LayoutManager
  95. */
  96. protected LayoutManager getChildLM() {
  97. if (curChildLM != null && !curChildLM.isFinished()) {
  98. return curChildLM;
  99. }
  100. if (childLMiter.hasNext()) {
  101. curChildLM = (LayoutManager) childLMiter.next();
  102. curChildLM.initialize();
  103. return curChildLM;
  104. }
  105. return null;
  106. }
  107. /**
  108. * Set currently active child layout manager.
  109. * @param childLM the child layout manager
  110. */
  111. protected void setCurrentChildLM(LayoutManager childLM) {
  112. curChildLM = childLM;
  113. childLMiter = new LMiter(this);
  114. do {
  115. curChildLM = (LayoutManager) childLMiter.next();
  116. } while (curChildLM != childLM);
  117. }
  118. /**
  119. * Return indication if getChildLM will return another LM.
  120. * @return true if another child LM is still available
  121. */
  122. protected boolean hasNextChildLM() {
  123. return childLMiter.hasNext();
  124. }
  125. /**
  126. * Tell whether this LayoutManager has handled all of its content.
  127. * @return True if there are no more break possibilities,
  128. * ie. the last one returned represents the end of the content.
  129. */
  130. public boolean isFinished() {
  131. return isFinished;
  132. }
  133. /**
  134. * Set the flag indicating the LayoutManager has handled all of its content.
  135. * @param fin the flag value to be set
  136. */
  137. public void setFinished(boolean fin) {
  138. isFinished = fin;
  139. }
  140. /** {@inheritDoc} */
  141. public void addAreas(PositionIterator posIter, LayoutContext context) {
  142. }
  143. /** {@inheritDoc} */
  144. public List getNextKnuthElements(LayoutContext context,
  145. int alignment) {
  146. log.warn("null implementation of getNextKnuthElements() called!");
  147. setFinished(true);
  148. return null;
  149. }
  150. /** {@inheritDoc} */
  151. public List getChangedKnuthElements(List oldList,
  152. int alignment) {
  153. log.warn("null implementation of getChangeKnuthElement() called!");
  154. return null;
  155. }
  156. /**
  157. * Return an Area which can contain the passed childArea. The childArea
  158. * may not yet have any content, but it has essential traits set.
  159. * In general, if the LayoutManager already has an Area it simply returns
  160. * it. Otherwise, it makes a new Area of the appropriate class.
  161. * It gets a parent area for its area by calling its parent LM.
  162. * Finally, based on the dimensions of the parent area, it initializes
  163. * its own area. This includes setting the content IPD and the maximum
  164. * BPD.
  165. * @param childArea the child area for which the parent area is wanted
  166. * @return the parent area for the given child
  167. */
  168. public Area getParentArea(Area childArea) {
  169. return null;
  170. }
  171. /**
  172. * Add a child area to the current area. If this causes the maximum
  173. * dimension of the current area to be exceeded, the parent LM is called
  174. * to add it.
  175. * @param childArea the child area to be added
  176. */
  177. public void addChildArea(Area childArea) {
  178. }
  179. /**
  180. * Create the LM instances for the children of the
  181. * formatting object being handled by this LM.
  182. * @param size the requested number of child LMs
  183. * @return the list with the preloaded child LMs
  184. */
  185. protected List createChildLMs(int size) {
  186. if (fobjIter == null) {
  187. return null;
  188. }
  189. List newLMs = new ArrayList(size);
  190. while (fobjIter.hasNext() && newLMs.size() < size ) {
  191. Object theobj = fobjIter.next();
  192. if (theobj instanceof FONode) {
  193. FONode foNode = (FONode) theobj;
  194. if (foNode instanceof RetrieveMarker) {
  195. foNode = getPSLM().resolveRetrieveMarker(
  196. (RetrieveMarker) foNode);
  197. }
  198. if (foNode != null) {
  199. getPSLM().getLayoutManagerMaker().
  200. makeLayoutManagers(foNode, newLMs);
  201. }
  202. }
  203. }
  204. return newLMs;
  205. }
  206. /** {@inheritDoc} */
  207. public PageSequenceLayoutManager getPSLM() {
  208. return parentLayoutManager.getPSLM();
  209. }
  210. /**
  211. * @see PageSequenceLayoutManager#getCurrentPage()
  212. * @return the {@link Page} instance corresponding to the current page
  213. */
  214. public Page getCurrentPage() {
  215. return getPSLM().getCurrentPage();
  216. }
  217. /** @return the current page viewport */
  218. public PageViewport getCurrentPV() {
  219. return getPSLM().getCurrentPage().getPageViewport();
  220. }
  221. /**
  222. * {@inheritDoc}
  223. */
  224. public boolean createNextChildLMs(int pos) {
  225. List newLMs = createChildLMs(pos + 1 - childLMs.size());
  226. addChildLMs(newLMs);
  227. return pos < childLMs.size();
  228. }
  229. /**
  230. * {@inheritDoc}
  231. */
  232. public List getChildLMs() {
  233. if (childLMs == null) {
  234. childLMs = new java.util.ArrayList(10);
  235. }
  236. return childLMs;
  237. }
  238. /**
  239. * {@inheritDoc}
  240. */
  241. public void addChildLM(LayoutManager lm) {
  242. if (lm == null) {
  243. return;
  244. }
  245. lm.setParent(this);
  246. if (childLMs == null) {
  247. childLMs = new java.util.ArrayList(10);
  248. }
  249. childLMs.add(lm);
  250. if (log.isTraceEnabled()) {
  251. log.trace(this.getClass().getName()
  252. + ": Adding child LM " + lm.getClass().getName());
  253. }
  254. }
  255. /**
  256. * {@inheritDoc}
  257. */
  258. public void addChildLMs(List newLMs) {
  259. if (newLMs == null || newLMs.size() == 0) {
  260. return;
  261. }
  262. ListIterator iter = newLMs.listIterator();
  263. while (iter.hasNext()) {
  264. LayoutManager lm = (LayoutManager) iter.next();
  265. addChildLM(lm);
  266. }
  267. }
  268. /**
  269. * Adds a Position to the Position participating in the first|last determination by assigning
  270. * it a unique position index.
  271. * @param pos the Position
  272. * @return the same Position but with a position index
  273. */
  274. public Position notifyPos(Position pos) {
  275. if (pos.getIndex() >= 0) {
  276. throw new IllegalStateException("Position already got its index");
  277. }
  278. lastGeneratedPosition++;
  279. pos.setIndex(lastGeneratedPosition);
  280. return pos;
  281. }
  282. private void verifyNonNullPosition(Position pos) {
  283. if (pos == null || pos.getIndex() < 0) {
  284. throw new IllegalArgumentException(
  285. "Only non-null Positions with an index can be checked");
  286. }
  287. }
  288. /**
  289. * Indicates whether the given Position is the first area-generating Position of this LM.
  290. * @param pos the Position (must be one with a position index)
  291. * @return True if it is the first Position
  292. */
  293. public boolean isFirst(Position pos) {
  294. //log.trace("isFirst() smallestPosNumberChecked=" + smallestPosNumberChecked + " " + pos);
  295. verifyNonNullPosition(pos);
  296. if (pos.getIndex() == this.smallestPosNumberChecked) {
  297. return true;
  298. } else if (pos.getIndex() < this.smallestPosNumberChecked) {
  299. this.smallestPosNumberChecked = pos.getIndex();
  300. return true;
  301. } else {
  302. return false;
  303. }
  304. }
  305. /**
  306. * Indicates whether the given Position is the last area-generating Position of this LM.
  307. * @param pos the Position (must be one with a position index)
  308. * @return True if it is the last Position
  309. */
  310. public boolean isLast(Position pos) {
  311. verifyNonNullPosition(pos);
  312. return (pos.getIndex() == this.lastGeneratedPosition
  313. && isFinished());
  314. }
  315. /**
  316. * Transfers foreign attributes from the formatting object to the area.
  317. * @param targetArea the area to set the attributes on
  318. */
  319. protected void transferForeignAttributes(AreaTreeObject targetArea) {
  320. Map atts = fobj.getForeignAttributes();
  321. targetArea.setForeignAttributes(atts);
  322. }
  323. /**
  324. * Transfers extension attachments from the formatting object to the area.
  325. * @param targetArea the area to set the extensions on
  326. */
  327. protected void transferExtensionAttachments(AreaTreeObject targetArea) {
  328. if (fobj.hasExtensionAttachments()) {
  329. targetArea.setExtensionAttachments(fobj.getExtensionAttachments());
  330. }
  331. }
  332. /**
  333. * Transfers extensions (foreign attributes and extension attachments) from
  334. * the formatting object to the area.
  335. * @param targetArea the area to set the extensions on
  336. */
  337. protected void transferExtensions(AreaTreeObject targetArea) {
  338. transferForeignAttributes(targetArea);
  339. transferExtensionAttachments(targetArea);
  340. }
  341. /**
  342. * Registers the FO's markers on the current PageViewport
  343. *
  344. * @param isStarting boolean indicating whether the markers qualify as 'starting'
  345. * @param isFirst boolean indicating whether the markers qualify as 'first'
  346. * @param isLast boolean indicating whether the markers qualify as 'last'
  347. */
  348. protected void addMarkersToPage(boolean isStarting, boolean isFirst, boolean isLast) {
  349. if (this.markers != null) {
  350. getCurrentPV().addMarkers(
  351. this.markers,
  352. isStarting,
  353. isFirst,
  354. isLast);
  355. }
  356. }
  357. /**
  358. * Registers the FO's id on the current PageViewport
  359. */
  360. protected void addId() {
  361. if (fobj != null) {
  362. getPSLM().addIDToPage(fobj.getId());
  363. }
  364. }
  365. /**
  366. * Notifies the {@link PageSequenceLayoutManager} that layout
  367. * for this LM has ended.
  368. */
  369. protected void notifyEndOfLayout() {
  370. if (fobj != null) {
  371. getPSLM().notifyEndOfLayout(fobj.getId());
  372. }
  373. }
  374. /**
  375. * Checks to see if the incoming {@link Position}
  376. * is the last one for this LM, and if so, calls
  377. * {@link #notifyEndOfLayout()} and cleans up.
  378. *
  379. * @param pos the {@link Position} to check
  380. */
  381. protected void checkEndOfLayout(Position pos) {
  382. if (pos != null
  383. && pos.getLM() == this
  384. && this.isLast(pos)) {
  385. notifyEndOfLayout();
  386. /* References to the child LMs are no longer needed
  387. */
  388. childLMs = null;
  389. curChildLM = null;
  390. childLMiter = null;
  391. /* markers that qualify have been transferred to the page
  392. */
  393. markers = null;
  394. /* References to the FO's children can be released if the
  395. * LM is a descendant of the FlowLM. For static-content
  396. * the FO may still be needed on following pages.
  397. */
  398. LayoutManager lm = this.parentLayoutManager;
  399. while (!(lm instanceof FlowLayoutManager
  400. || lm instanceof PageSequenceLayoutManager)) {
  401. lm = lm.getParent();
  402. }
  403. if (lm instanceof FlowLayoutManager) {
  404. fobj.clearChildNodes();
  405. fobjIter = null;
  406. }
  407. }
  408. }
  409. /** {@inheritDoc} */
  410. public String toString() {
  411. return (super.toString() + (fobj != null ? "[fobj=" + fobj.toString() + "]" : ""));
  412. }
  413. /** {@inheritDoc} */
  414. public void reset() {
  415. isFinished = false;
  416. curChildLM = null;
  417. childLMiter = new LMiter(this);
  418. /* Reset all the children LM that have been created so far. */
  419. for (Iterator iter = getChildLMs().iterator(); iter.hasNext();) {
  420. ((LayoutManager) iter.next()).reset();
  421. }
  422. if (fobj != null) {
  423. markers = fobj.getMarkers();
  424. }
  425. lastGeneratedPosition = -1;
  426. }
  427. }