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

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