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.

BlockLayoutManager.java 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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.LinkedList;
  20. import java.util.List;
  21. import java.util.ListIterator;
  22. import java.util.Stack;
  23. import org.apache.commons.logging.Log;
  24. import org.apache.commons.logging.LogFactory;
  25. import org.apache.fop.area.Area;
  26. import org.apache.fop.area.Block;
  27. import org.apache.fop.area.LineArea;
  28. import org.apache.fop.datatypes.Length;
  29. import org.apache.fop.fo.properties.KeepProperty;
  30. import org.apache.fop.fonts.Font;
  31. import org.apache.fop.fonts.FontInfo;
  32. import org.apache.fop.fonts.FontTriplet;
  33. import org.apache.fop.layoutmgr.inline.InlineLevelLayoutManager;
  34. import org.apache.fop.layoutmgr.inline.LineLayoutManager;
  35. import org.apache.fop.traits.MinOptMax;
  36. import org.apache.fop.traits.SpaceVal;
  37. /**
  38. * LayoutManager for a block FO.
  39. */
  40. public class BlockLayoutManager extends BlockStackingLayoutManager
  41. implements ConditionalElementListener {
  42. /** logging instance */
  43. private static Log log = LogFactory.getLog(BlockLayoutManager.class);
  44. private Block curBlockArea;
  45. /** Iterator over the child layout managers. */
  46. protected ListIterator<LayoutManager> proxyLMiter;
  47. private int lead = 12000;
  48. private Length lineHeight;
  49. private int follow = 2000;
  50. //private int middleShift = 0;
  51. private boolean discardBorderBefore;
  52. private boolean discardBorderAfter;
  53. private boolean discardPaddingBefore;
  54. private boolean discardPaddingAfter;
  55. private MinOptMax effSpaceBefore;
  56. private MinOptMax effSpaceAfter;
  57. /**
  58. * Creates a new BlockLayoutManager.
  59. * @param inBlock the block FO object to create the layout manager for.
  60. */
  61. public BlockLayoutManager(org.apache.fop.fo.flow.Block inBlock) {
  62. super(inBlock);
  63. proxyLMiter = new ProxyLMiter();
  64. }
  65. /** {@inheritDoc} */
  66. @Override
  67. public void initialize() {
  68. super.initialize();
  69. org.apache.fop.fo.flow.Block fo = getBlockFO();
  70. FontInfo fi = fo.getFOEventHandler().getFontInfo();
  71. FontTriplet[] fontkeys = fo.getCommonFont().getFontState(fi);
  72. Font initFont = fi.getFontInstance(fontkeys[0],
  73. getBlockFO().getCommonFont().fontSize.getValue(this));
  74. lead = initFont.getAscender();
  75. follow = -initFont.getDescender();
  76. //middleShift = -fs.getXHeight() / 2;
  77. lineHeight = fo.getLineHeight().getOptimum(this).getLength();
  78. startIndent = fo.getCommonMarginBlock().startIndent.getValue(this);
  79. endIndent = fo.getCommonMarginBlock().endIndent.getValue(this);
  80. foSpaceBefore = new SpaceVal(fo.getCommonMarginBlock().spaceBefore, this).getSpace();
  81. foSpaceAfter = new SpaceVal(fo.getCommonMarginBlock().spaceAfter, this).getSpace();
  82. // use optimum space values
  83. adjustedSpaceBefore = fo.getCommonMarginBlock().spaceBefore.getSpace()
  84. .getOptimum(this).getLength().getValue(this);
  85. adjustedSpaceAfter = fo.getCommonMarginBlock().spaceAfter.getSpace()
  86. .getOptimum(this).getLength().getValue(this);
  87. }
  88. /** {@inheritDoc} */
  89. @Override
  90. public List getNextKnuthElements(LayoutContext context, int alignment) {
  91. return getNextKnuthElements(context, alignment, null, null, null);
  92. }
  93. /** {@inheritDoc} */
  94. @Override
  95. public List getNextKnuthElements(LayoutContext context, int alignment, Stack lmStack,
  96. Position restartPosition, LayoutManager restartAtLM) {
  97. resetSpaces();
  98. if (lmStack == null) {
  99. return super.getNextKnuthElements(context, alignment);
  100. } else {
  101. return super.getNextKnuthElements(context, alignment, lmStack, restartPosition,
  102. restartAtLM);
  103. }
  104. }
  105. private void resetSpaces() {
  106. this.discardBorderBefore = false;
  107. this.discardBorderAfter = false;
  108. this.discardPaddingBefore = false;
  109. this.discardPaddingAfter = false;
  110. this.effSpaceBefore = null;
  111. this.effSpaceAfter = null;
  112. }
  113. /**
  114. * Proxy iterator for Block LM.
  115. * This iterator creates and holds the complete list
  116. * of child LMs.
  117. * It uses fobjIter as its base iterator.
  118. * Block LM's createNextChildLMs uses this iterator
  119. * as its base iterator.
  120. */
  121. protected class ProxyLMiter extends LMiter {
  122. /**
  123. * Constructs a proxy iterator for Block LM.
  124. */
  125. public ProxyLMiter() {
  126. super(BlockLayoutManager.this);
  127. listLMs = new java.util.ArrayList<LayoutManager>(10);
  128. }
  129. /**
  130. * @return true if there are more child lms
  131. */
  132. public boolean hasNext() {
  133. return (curPos < listLMs.size()) || createNextChildLMs(curPos);
  134. }
  135. /**
  136. * @param pos ...
  137. * @return true if new child lms were added
  138. */
  139. protected boolean createNextChildLMs(int pos) {
  140. List<LayoutManager> newLMs = createChildLMs(pos + 1 - listLMs.size());
  141. if (newLMs != null) {
  142. listLMs.addAll(newLMs);
  143. }
  144. return pos < listLMs.size();
  145. }
  146. }
  147. /** {@inheritDoc} */
  148. @Override
  149. public boolean createNextChildLMs(int pos) {
  150. while (proxyLMiter.hasNext()) {
  151. LayoutManager lm = proxyLMiter.next();
  152. if (lm instanceof InlineLevelLayoutManager) {
  153. LineLayoutManager lineLM = createLineManager(lm);
  154. addChildLM(lineLM);
  155. } else {
  156. addChildLM(lm);
  157. }
  158. if (pos < childLMs.size()) {
  159. return true;
  160. }
  161. }
  162. return false;
  163. }
  164. /**
  165. * Create a new LineLM, and collect all consecutive
  166. * inline generating LMs as its child LMs.
  167. * @param firstlm First LM in new LineLM
  168. * @return the newly created LineLM
  169. */
  170. private LineLayoutManager createLineManager(LayoutManager firstlm) {
  171. LineLayoutManager llm;
  172. llm = new LineLayoutManager(getBlockFO(), lineHeight, lead, follow);
  173. List<LayoutManager> inlines = new java.util.ArrayList<LayoutManager>();
  174. inlines.add(firstlm);
  175. while (proxyLMiter.hasNext()) {
  176. LayoutManager lm = proxyLMiter.next();
  177. if (lm instanceof InlineLevelLayoutManager) {
  178. inlines.add(lm);
  179. } else {
  180. proxyLMiter.previous();
  181. break;
  182. }
  183. }
  184. llm.addChildLMs(inlines);
  185. return llm;
  186. }
  187. /** {@inheritDoc} */
  188. @Override
  189. public KeepProperty getKeepTogetherProperty() {
  190. return getBlockFO().getKeepTogether();
  191. }
  192. /** {@inheritDoc} */
  193. @Override
  194. public KeepProperty getKeepWithPreviousProperty() {
  195. return getBlockFO().getKeepWithPrevious();
  196. }
  197. /** {@inheritDoc} */
  198. @Override
  199. public KeepProperty getKeepWithNextProperty() {
  200. return getBlockFO().getKeepWithNext();
  201. }
  202. /** {@inheritDoc} */
  203. @Override
  204. public void addAreas(PositionIterator parentIter, LayoutContext layoutContext) {
  205. getParentArea(null);
  206. // if this will create the first block area in a page
  207. // and display-align is after or center, add space before
  208. if (layoutContext.getSpaceBefore() > 0) {
  209. addBlockSpacing(0.0, MinOptMax.getInstance(layoutContext.getSpaceBefore()));
  210. }
  211. LayoutManager childLM;
  212. LayoutManager lastLM = null;
  213. LayoutContext lc = new LayoutContext(0);
  214. lc.setSpaceAdjust(layoutContext.getSpaceAdjust());
  215. // set space after in the LayoutContext for children
  216. if (layoutContext.getSpaceAfter() > 0) {
  217. lc.setSpaceAfter(layoutContext.getSpaceAfter());
  218. }
  219. PositionIterator childPosIter;
  220. // "unwrap" the NonLeafPositions stored in parentIter
  221. // and put them in a new list;
  222. LinkedList<Position> positionList = new LinkedList<Position>();
  223. Position pos;
  224. Position firstPos = null;
  225. Position lastPos = null;
  226. while (parentIter.hasNext()) {
  227. pos = parentIter.next();
  228. //log.trace("pos = " + pos.getClass().getName() + "; " + pos);
  229. if (pos.getIndex() >= 0) {
  230. if (firstPos == null) {
  231. firstPos = pos;
  232. }
  233. lastPos = pos;
  234. }
  235. Position innerPosition = pos;
  236. if (pos instanceof NonLeafPosition) {
  237. //Not all elements are wrapped
  238. innerPosition = pos.getPosition();
  239. }
  240. if (innerPosition != null
  241. && (innerPosition.getLM() != this
  242. || innerPosition instanceof MappingPosition)) {
  243. // innerPosition was created by another LM
  244. positionList.add(innerPosition);
  245. lastLM = innerPosition.getLM();
  246. }
  247. }
  248. addId();
  249. addMarkersToPage(true, isFirst(firstPos), isLast(lastPos));
  250. // the Positions in positionList were inside the elements
  251. // created by the LineLM
  252. childPosIter = new StackingIter(positionList.listIterator());
  253. while ((childLM = childPosIter.getNextChildLM()) != null) {
  254. // set last area flag
  255. lc.setFlags(LayoutContext.LAST_AREA,
  256. (layoutContext.isLastArea() && childLM == lastLM));
  257. lc.setStackLimitBP(layoutContext.getStackLimitBP());
  258. // Add the line areas to Area
  259. childLM.addAreas(childPosIter, lc);
  260. }
  261. addMarkersToPage(false, isFirst(firstPos), isLast(lastPos));
  262. TraitSetter.addPtr(curBlockArea, getBlockFO().getPtr()); // used for accessibility
  263. TraitSetter.addSpaceBeforeAfter(curBlockArea, layoutContext.getSpaceAdjust(),
  264. effSpaceBefore, effSpaceAfter);
  265. flush();
  266. curBlockArea = null;
  267. resetSpaces();
  268. //Notify end of block layout manager to the PSLM
  269. checkEndOfLayout(lastPos);
  270. }
  271. /**
  272. * Return an Area which can contain the passed childArea. The childArea
  273. * may not yet have any content, but it has essential traits set.
  274. * In general, if the LayoutManager already has an Area it simply returns
  275. * it. Otherwise, it makes a new Area of the appropriate class.
  276. * It gets a parent area for its area by calling its parent LM.
  277. * Finally, based on the dimensions of the parent area, it initializes
  278. * its own area. This includes setting the content IPD and the maximum
  279. * BPD.
  280. * @param childArea area to get the parent area for
  281. * @return the parent area
  282. */
  283. @Override
  284. public Area getParentArea(Area childArea) {
  285. if (curBlockArea == null) {
  286. curBlockArea = new Block();
  287. curBlockArea.setIPD(super.getContentAreaIPD());
  288. TraitSetter.addBreaks(curBlockArea,
  289. getBlockFO().getBreakBefore(), getBlockFO().getBreakAfter());
  290. // Must get dimensions from parent area
  291. //Don't optimize this line away. It can have ugly side-effects.
  292. /*Area parentArea =*/ parentLayoutManager.getParentArea(curBlockArea);
  293. // set traits
  294. TraitSetter.setProducerID(curBlockArea, getBlockFO().getId());
  295. TraitSetter.addBorders(curBlockArea,
  296. getBlockFO().getCommonBorderPaddingBackground(),
  297. discardBorderBefore, discardBorderAfter, false, false, this);
  298. TraitSetter.addPadding(curBlockArea,
  299. getBlockFO().getCommonBorderPaddingBackground(),
  300. discardPaddingBefore, discardPaddingAfter, false, false, this);
  301. TraitSetter.addMargins(curBlockArea,
  302. getBlockFO().getCommonBorderPaddingBackground(),
  303. startIndent, endIndent,
  304. this);
  305. setCurrentArea(curBlockArea); // ??? for generic operations
  306. }
  307. return curBlockArea;
  308. }
  309. /** {@inheritDoc} */
  310. @Override
  311. public void addChildArea(Area childArea) {
  312. if (curBlockArea != null) {
  313. if (childArea instanceof LineArea) {
  314. curBlockArea.addLineArea((LineArea) childArea);
  315. } else {
  316. curBlockArea.addBlock((Block) childArea);
  317. }
  318. }
  319. }
  320. /**
  321. * Force current area to be added to parent area.
  322. * {@inheritDoc}
  323. */
  324. @Override
  325. protected void flush() {
  326. if (curBlockArea != null) {
  327. TraitSetter.addBackground(curBlockArea,
  328. getBlockFO().getCommonBorderPaddingBackground(),
  329. this);
  330. super.flush();
  331. }
  332. }
  333. /**
  334. * convenience method that returns the Block node
  335. * @return the block node
  336. */
  337. protected org.apache.fop.fo.flow.Block getBlockFO() {
  338. return (org.apache.fop.fo.flow.Block) fobj;
  339. }
  340. // --------- Property Resolution related functions --------- //
  341. /**
  342. * Returns the IPD of the content area
  343. * @return the IPD of the content area
  344. */
  345. @Override
  346. public int getContentAreaIPD() {
  347. if (curBlockArea != null) {
  348. return curBlockArea.getIPD();
  349. }
  350. return super.getContentAreaIPD();
  351. }
  352. /**
  353. * Returns the BPD of the content area
  354. * @return the BPD of the content area
  355. */
  356. @Override
  357. public int getContentAreaBPD() {
  358. if (curBlockArea != null) {
  359. return curBlockArea.getBPD();
  360. }
  361. return -1;
  362. }
  363. /** {@inheritDoc} */
  364. @Override
  365. public boolean getGeneratesBlockArea() {
  366. return true;
  367. }
  368. /** {@inheritDoc} */
  369. public void notifySpace(RelSide side, MinOptMax effectiveLength) {
  370. if (RelSide.BEFORE == side) {
  371. if (log.isDebugEnabled()) {
  372. log.debug(this + ": Space " + side + ", "
  373. + this.effSpaceBefore + "-> " + effectiveLength);
  374. }
  375. this.effSpaceBefore = effectiveLength;
  376. } else {
  377. if (log.isDebugEnabled()) {
  378. log.debug(this + ": Space " + side + ", "
  379. + this.effSpaceAfter + "-> " + effectiveLength);
  380. }
  381. this.effSpaceAfter = effectiveLength;
  382. }
  383. }
  384. /** {@inheritDoc} */
  385. public void notifyBorder(RelSide side, MinOptMax effectiveLength) {
  386. if (effectiveLength == null) {
  387. if (RelSide.BEFORE == side) {
  388. this.discardBorderBefore = true;
  389. } else {
  390. this.discardBorderAfter = true;
  391. }
  392. }
  393. if (log.isDebugEnabled()) {
  394. log.debug(this + ": Border " + side + " -> " + effectiveLength);
  395. }
  396. }
  397. /** {@inheritDoc} */
  398. public void notifyPadding(RelSide side, MinOptMax effectiveLength) {
  399. if (effectiveLength == null) {
  400. if (RelSide.BEFORE == side) {
  401. this.discardPaddingBefore = true;
  402. } else {
  403. this.discardPaddingAfter = true;
  404. }
  405. }
  406. if (log.isDebugEnabled()) {
  407. log.debug(this + ": Padding " + side + " -> " + effectiveLength);
  408. }
  409. }
  410. /** {@inheritDoc} */
  411. @Override
  412. public boolean isRestartable() {
  413. return true;
  414. }
  415. }