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

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