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.

InlineStackingLayoutManager.java 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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.inline;
  19. import java.util.LinkedList;
  20. import java.util.List;
  21. import java.util.ListIterator;
  22. import org.apache.fop.area.Area;
  23. import org.apache.fop.area.inline.Space;
  24. import org.apache.fop.fo.FObj;
  25. import org.apache.fop.fo.properties.SpaceProperty;
  26. import org.apache.fop.layoutmgr.AbstractLayoutManager;
  27. import org.apache.fop.layoutmgr.BreakOpportunity;
  28. import org.apache.fop.layoutmgr.BreakOpportunityHelper;
  29. import org.apache.fop.layoutmgr.KnuthElement;
  30. import org.apache.fop.layoutmgr.LayoutContext;
  31. import org.apache.fop.layoutmgr.NonLeafPosition;
  32. import org.apache.fop.layoutmgr.Position;
  33. import org.apache.fop.traits.MinOptMax;
  34. /**
  35. * Class modelling the commonalities of layoutmanagers for objects
  36. * which stack children in the inline direction, such as Inline or
  37. * Line. It should not be instantiated directly.
  38. */
  39. public abstract class InlineStackingLayoutManager extends AbstractLayoutManager implements
  40. InlineLevelLayoutManager, BreakOpportunity {
  41. /**
  42. * Size of border and padding in BPD (ie, before and after).
  43. */
  44. protected MinOptMax extraBPD;
  45. private Area currentArea; // LineArea or InlineParent
  46. /** The child layout context */
  47. protected LayoutContext childLC;
  48. /**
  49. * Create an inline stacking layout manager.
  50. * This is used for fo's that create areas that
  51. * contain inline areas.
  52. *
  53. * @param node the formatting object that creates the area
  54. */
  55. protected InlineStackingLayoutManager(FObj node) {
  56. super(node);
  57. extraBPD = MinOptMax.ZERO;
  58. }
  59. /**
  60. * Set the iterator.
  61. *
  62. * @param iter the iterator for this LM
  63. */
  64. public void setLMiter(ListIterator iter) {
  65. childLMiter = iter;
  66. }
  67. /**
  68. * Returns the extra IPD needed for any leading or trailing fences for the
  69. * current area.
  70. * @param bNotFirst true if not the first area for this layout manager
  71. * @param bNotLast true if not the last area for this layout manager
  72. * @return the extra IPD as a MinOptMax spec
  73. */
  74. protected MinOptMax getExtraIPD(boolean bNotFirst, boolean bNotLast) {
  75. return MinOptMax.ZERO;
  76. }
  77. /**
  78. * Indication if the current area has a leading fence.
  79. * @param bNotFirst true if not the first area for this layout manager
  80. * @return the leading fence flag
  81. */
  82. protected boolean hasLeadingFence(boolean bNotFirst) {
  83. return false;
  84. }
  85. /**
  86. * Indication if the current area has a trailing fence.
  87. * @param bNotLast true if not the last area for this layout manager
  88. * @return the trailing fence flag
  89. */
  90. protected boolean hasTrailingFence(boolean bNotLast) {
  91. return false;
  92. }
  93. /**
  94. * Get the space at the start of the inline area.
  95. * @return the space property describing the space
  96. */
  97. protected SpaceProperty getSpaceStart() {
  98. return null;
  99. }
  100. /**
  101. * Get the space at the end of the inline area.
  102. * @return the space property describing the space
  103. */
  104. protected SpaceProperty getSpaceEnd() {
  105. return null;
  106. }
  107. /**
  108. * Returns the current area.
  109. * @return the current area
  110. */
  111. protected Area getCurrentArea() {
  112. return currentArea;
  113. }
  114. /**
  115. * Set the current area.
  116. * @param area the current area
  117. */
  118. protected void setCurrentArea(Area area) {
  119. currentArea = area;
  120. }
  121. /**
  122. * Trait setter to be overridden by subclasses.
  123. * @param bNotFirst true if this is not the first child area added
  124. * @param bNotLast true if this is not the last child area added
  125. */
  126. protected void setTraits(boolean bNotFirst, boolean bNotLast) {
  127. }
  128. /**
  129. * Set the current child layout context
  130. * @param lc the child layout context
  131. */
  132. protected void setChildContext(LayoutContext lc) {
  133. childLC = lc;
  134. }
  135. /**
  136. * Current child layout context
  137. * @return the current child layout context
  138. */
  139. protected LayoutContext getContext() {
  140. return childLC;
  141. }
  142. /**
  143. * Adds a space to the area.
  144. *
  145. * @param parentArea the area to which to add the space
  146. * @param spaceRange the space range specifier
  147. * @param spaceAdjust the factor by which to stretch or shrink the space
  148. */
  149. protected void addSpace(Area parentArea, MinOptMax spaceRange, double spaceAdjust) {
  150. if (spaceRange != null) {
  151. int iAdjust = spaceRange.getOpt();
  152. if (spaceAdjust > 0.0) {
  153. // Stretch by factor
  154. iAdjust += (int) (spaceRange.getStretch() * spaceAdjust);
  155. } else if (spaceAdjust < 0.0) {
  156. // Shrink by factor
  157. iAdjust += (int) (spaceRange.getShrink() * spaceAdjust);
  158. }
  159. if (iAdjust != 0) {
  160. //getLogger().debug("Add leading space: " + iAdjust);
  161. Space ls = new Space();
  162. ls.setIPD(iAdjust);
  163. int level = parentArea.getBidiLevel();
  164. if ( level >= 0 ) {
  165. ls.setBidiLevel ( level );
  166. }
  167. parentArea.addChildArea(ls);
  168. }
  169. }
  170. }
  171. /** {@inheritDoc} */
  172. public List addALetterSpaceTo(List oldList) {
  173. return addALetterSpaceTo(oldList, 0);
  174. }
  175. /** {@inheritDoc} */
  176. public List addALetterSpaceTo(List oldList, int thisDepth) {
  177. // old list contains only a box, or the sequence: box penalty glue box
  178. ListIterator oldListIterator = oldList.listIterator(oldList.size());
  179. KnuthElement element = (KnuthElement) oldListIterator.previous();
  180. int depth = thisDepth + 1;
  181. // The last element may not have a layout manager (its position == null);
  182. // this may happen if it is a padding box; see bug 39571.
  183. Position pos = element.getPosition();
  184. InlineLevelLayoutManager lm = null;
  185. if (pos != null) {
  186. lm = (InlineLevelLayoutManager) pos.getLM(depth);
  187. }
  188. if (lm == null) {
  189. return oldList;
  190. }
  191. oldList = lm.addALetterSpaceTo(oldList, depth);
  192. // "wrap" the Position stored in new elements of oldList
  193. oldListIterator = oldList.listIterator();
  194. while (oldListIterator.hasNext()) {
  195. element = (KnuthElement) oldListIterator.next();
  196. pos = element.getPosition();
  197. lm = null;
  198. if (pos != null) {
  199. lm = (InlineLevelLayoutManager) pos.getLM(thisDepth);
  200. }
  201. // in old elements the position at thisDepth is a position for this LM
  202. // only wrap new elements
  203. if (lm != this) {
  204. // new element, wrap position
  205. element.setPosition(notifyPos(new NonLeafPosition(this, element.getPosition())));
  206. }
  207. }
  208. return oldList;
  209. }
  210. /** {@inheritDoc} */
  211. public String getWordChars(Position pos) {
  212. Position newPos = pos.getPosition();
  213. return ((InlineLevelLayoutManager) newPos.getLM()).getWordChars(newPos);
  214. }
  215. /** {@inheritDoc} */
  216. public void hyphenate(Position pos, HyphContext hc) {
  217. Position newPos = pos.getPosition();
  218. ((InlineLevelLayoutManager)
  219. newPos.getLM()).hyphenate(newPos, hc);
  220. }
  221. /** {@inheritDoc} */
  222. public boolean applyChanges(List oldList) {
  223. return applyChanges(oldList, 0);
  224. }
  225. /** {@inheritDoc} */
  226. public boolean applyChanges(List oldList, int depth) {
  227. ListIterator oldListIterator = oldList.listIterator();
  228. KnuthElement oldElement;
  229. depth += 1;
  230. InlineLevelLayoutManager prevLM = null;
  231. InlineLevelLayoutManager currLM;
  232. int fromIndex = 0;
  233. boolean bSomethingChanged = false;
  234. while (oldListIterator.hasNext()) {
  235. oldElement = (KnuthElement) oldListIterator.next();
  236. Position pos = oldElement.getPosition();
  237. if (pos == null) {
  238. currLM = null;
  239. } else {
  240. currLM = (InlineLevelLayoutManager) pos.getLM(depth);
  241. }
  242. // initialize prevLM
  243. if (prevLM == null) {
  244. prevLM = currLM;
  245. }
  246. if (currLM != prevLM || !oldListIterator.hasNext()) {
  247. if (prevLM == this || currLM == this) {
  248. prevLM = currLM;
  249. } else if (oldListIterator.hasNext()) {
  250. bSomethingChanged
  251. = prevLM.applyChanges(oldList.subList(fromIndex,
  252. oldListIterator.previousIndex()),
  253. depth)
  254. || bSomethingChanged;
  255. prevLM = currLM;
  256. fromIndex = oldListIterator.previousIndex();
  257. } else if (currLM == prevLM) {
  258. bSomethingChanged
  259. = (prevLM != null)
  260. && prevLM.applyChanges(oldList.subList(fromIndex,
  261. oldList.size()), depth)
  262. || bSomethingChanged;
  263. } else {
  264. bSomethingChanged
  265. = prevLM.applyChanges(oldList.subList(fromIndex,
  266. oldListIterator.previousIndex()),
  267. depth)
  268. || bSomethingChanged;
  269. if (currLM != null) {
  270. bSomethingChanged
  271. = currLM.applyChanges(oldList.subList(oldListIterator.previousIndex(),
  272. oldList.size()), depth)
  273. || bSomethingChanged;
  274. }
  275. }
  276. }
  277. }
  278. return bSomethingChanged;
  279. }
  280. /**
  281. * {@inheritDoc}
  282. */
  283. public List getChangedKnuthElements(List oldList, int alignment) {
  284. return getChangedKnuthElements(oldList, alignment, 0);
  285. }
  286. /** {@inheritDoc} */
  287. public List getChangedKnuthElements(List oldList, int alignment, int depth) {
  288. // "unwrap" the Positions stored in the elements
  289. ListIterator oldListIterator = oldList.listIterator();
  290. KnuthElement oldElement;
  291. depth += 1;
  292. KnuthElement returnedElement;
  293. LinkedList returnedList = new LinkedList();
  294. LinkedList returnList = new LinkedList();
  295. InlineLevelLayoutManager prevLM = null;
  296. InlineLevelLayoutManager currLM;
  297. int fromIndex = 0;
  298. while (oldListIterator.hasNext()) {
  299. oldElement = (KnuthElement) oldListIterator.next();
  300. Position pos = oldElement.getPosition();
  301. if (pos == null) {
  302. currLM = null;
  303. } else {
  304. currLM = (InlineLevelLayoutManager) pos.getLM(depth);
  305. }
  306. if (prevLM == null) {
  307. prevLM = currLM;
  308. }
  309. if (currLM != prevLM || !oldListIterator.hasNext()) {
  310. if (oldListIterator.hasNext()) {
  311. returnedList.addAll
  312. (prevLM.getChangedKnuthElements
  313. (oldList.subList(fromIndex, oldListIterator.previousIndex()),
  314. alignment, depth));
  315. prevLM = currLM;
  316. fromIndex = oldListIterator.previousIndex();
  317. } else if (currLM == prevLM) {
  318. returnedList.addAll
  319. (prevLM.getChangedKnuthElements
  320. (oldList.subList(fromIndex, oldList.size()),
  321. alignment, depth));
  322. } else {
  323. returnedList.addAll
  324. (prevLM.getChangedKnuthElements
  325. (oldList.subList(fromIndex, oldListIterator.previousIndex()),
  326. alignment, depth));
  327. if (currLM != null) {
  328. returnedList.addAll
  329. (currLM.getChangedKnuthElements
  330. (oldList.subList(oldListIterator.previousIndex(), oldList.size()),
  331. alignment, depth));
  332. }
  333. }
  334. }
  335. }
  336. // this is a new list
  337. // "wrap" the Position stored in each element of returnedList
  338. ListIterator listIter = returnedList.listIterator();
  339. while (listIter.hasNext()) {
  340. returnedElement = (KnuthElement) listIter.next();
  341. returnedElement.setPosition
  342. (notifyPos(new NonLeafPosition(this, returnedElement.getPosition())));
  343. returnList.add(returnedElement);
  344. }
  345. return returnList;
  346. }
  347. public int getBreakBefore() {
  348. return BreakOpportunityHelper.getBreakBefore(this);
  349. }
  350. }