Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

BlockLayoutManager.java 17KB

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