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

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