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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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; // @SuppressFBWarnings("URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
  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.setChangeBarList(getChangeBarList());
  163. ls.setIPD(iAdjust);
  164. int level = parentArea.getBidiLevel();
  165. if (level >= 0) {
  166. ls.setBidiLevel(level);
  167. }
  168. ls.setFromFootnote(isFromFootnote());
  169. parentArea.addChildArea(ls);
  170. }
  171. }
  172. }
  173. /** {@inheritDoc} */
  174. public List addALetterSpaceTo(List oldList) {
  175. return addALetterSpaceTo(oldList, 0);
  176. }
  177. /** {@inheritDoc} */
  178. public List addALetterSpaceTo(List oldList, int thisDepth) {
  179. // old list contains only a box, or the sequence: box penalty glue box
  180. ListIterator oldListIterator = oldList.listIterator(oldList.size());
  181. KnuthElement element = (KnuthElement) oldListIterator.previous();
  182. int depth = thisDepth + 1;
  183. // The last element may not have a layout manager (its position == null);
  184. // this may happen if it is a padding box; see bug 39571.
  185. Position pos = element.getPosition();
  186. InlineLevelLayoutManager lm = null;
  187. if (pos != null) {
  188. lm = (InlineLevelLayoutManager) pos.getLM(depth);
  189. }
  190. if (lm == null) {
  191. return oldList;
  192. }
  193. oldList = lm.addALetterSpaceTo(oldList, depth);
  194. // "wrap" the Position stored in new elements of oldList
  195. oldListIterator = oldList.listIterator();
  196. while (oldListIterator.hasNext()) {
  197. element = (KnuthElement) oldListIterator.next();
  198. pos = element.getPosition();
  199. lm = null;
  200. if (pos != null) {
  201. lm = (InlineLevelLayoutManager) pos.getLM(thisDepth);
  202. }
  203. // in old elements the position at thisDepth is a position for this LM
  204. // only wrap new elements
  205. if (lm != this) {
  206. // new element, wrap position
  207. element.setPosition(notifyPos(new NonLeafPosition(this, element.getPosition())));
  208. }
  209. }
  210. return oldList;
  211. }
  212. /** {@inheritDoc} */
  213. public String getWordChars(Position pos) {
  214. Position newPos = pos.getPosition();
  215. return ((InlineLevelLayoutManager) newPos.getLM()).getWordChars(newPos);
  216. }
  217. /** {@inheritDoc} */
  218. public void hyphenate(Position pos, HyphContext hc) {
  219. Position newPos = pos.getPosition();
  220. ((InlineLevelLayoutManager)
  221. newPos.getLM()).hyphenate(newPos, hc);
  222. }
  223. /** {@inheritDoc} */
  224. public boolean applyChanges(List oldList) {
  225. return applyChanges(oldList, 0);
  226. }
  227. /** {@inheritDoc} */
  228. public boolean applyChanges(List oldList, int depth) {
  229. ListIterator oldListIterator = oldList.listIterator();
  230. KnuthElement oldElement;
  231. depth += 1;
  232. InlineLevelLayoutManager prevLM = null;
  233. InlineLevelLayoutManager currLM;
  234. int fromIndex = 0;
  235. boolean bSomethingChanged = false;
  236. while (oldListIterator.hasNext()) {
  237. oldElement = (KnuthElement) oldListIterator.next();
  238. Position pos = oldElement.getPosition();
  239. if (pos == null) {
  240. currLM = null;
  241. } else {
  242. currLM = (InlineLevelLayoutManager) pos.getLM(depth);
  243. }
  244. // initialize prevLM
  245. if (prevLM == null) {
  246. prevLM = currLM;
  247. }
  248. if (currLM != prevLM || !oldListIterator.hasNext()) {
  249. if (prevLM == this || currLM == this) {
  250. prevLM = currLM;
  251. } else if (oldListIterator.hasNext()) {
  252. bSomethingChanged
  253. = prevLM.applyChanges(oldList.subList(fromIndex,
  254. oldListIterator.previousIndex()),
  255. depth)
  256. || bSomethingChanged;
  257. prevLM = currLM;
  258. fromIndex = oldListIterator.previousIndex();
  259. } else if (currLM == prevLM) {
  260. bSomethingChanged
  261. = (prevLM != null)
  262. && prevLM.applyChanges(oldList.subList(fromIndex,
  263. oldList.size()), depth)
  264. || bSomethingChanged;
  265. } else {
  266. bSomethingChanged
  267. = prevLM.applyChanges(oldList.subList(fromIndex,
  268. oldListIterator.previousIndex()),
  269. depth)
  270. || bSomethingChanged;
  271. if (currLM != null) {
  272. bSomethingChanged
  273. = currLM.applyChanges(oldList.subList(oldListIterator.previousIndex(),
  274. oldList.size()), depth)
  275. || bSomethingChanged;
  276. }
  277. }
  278. }
  279. }
  280. return bSomethingChanged;
  281. }
  282. /**
  283. * {@inheritDoc}
  284. */
  285. public List getChangedKnuthElements(List oldList, int alignment) {
  286. return getChangedKnuthElements(oldList, alignment, 0);
  287. }
  288. /** {@inheritDoc} */
  289. public List getChangedKnuthElements(List oldList, int alignment, int depth) {
  290. // "unwrap" the Positions stored in the elements
  291. ListIterator oldListIterator = oldList.listIterator();
  292. KnuthElement oldElement;
  293. depth += 1;
  294. KnuthElement returnedElement;
  295. LinkedList returnedList = new LinkedList();
  296. LinkedList returnList = new LinkedList();
  297. InlineLevelLayoutManager prevLM = null;
  298. InlineLevelLayoutManager currLM;
  299. int fromIndex = 0;
  300. while (oldListIterator.hasNext()) {
  301. oldElement = (KnuthElement) oldListIterator.next();
  302. Position pos = oldElement.getPosition();
  303. if (pos == null) {
  304. currLM = null;
  305. } else {
  306. currLM = (InlineLevelLayoutManager) pos.getLM(depth);
  307. }
  308. if (prevLM == null) {
  309. prevLM = currLM;
  310. }
  311. if (currLM != prevLM || !oldListIterator.hasNext()) {
  312. if (oldListIterator.hasNext()) {
  313. returnedList.addAll(
  314. prevLM.getChangedKnuthElements(
  315. oldList.subList(fromIndex, oldListIterator.previousIndex()),
  316. alignment, depth));
  317. prevLM = currLM;
  318. fromIndex = oldListIterator.previousIndex();
  319. } else if (currLM == prevLM) {
  320. returnedList.addAll(
  321. prevLM.getChangedKnuthElements(
  322. oldList.subList(fromIndex, oldList.size()),
  323. alignment, depth));
  324. } else {
  325. returnedList.addAll(
  326. prevLM.getChangedKnuthElements(
  327. oldList.subList(fromIndex, oldListIterator.previousIndex()),
  328. alignment, depth));
  329. if (currLM != null) {
  330. returnedList.addAll(
  331. currLM.getChangedKnuthElements(
  332. oldList.subList(oldListIterator.previousIndex(), oldList.size()),
  333. alignment, depth));
  334. }
  335. }
  336. }
  337. }
  338. // this is a new list
  339. // "wrap" the Position stored in each element of returnedList
  340. for (Object aReturnedList : returnedList) {
  341. returnedElement = (KnuthElement) aReturnedList;
  342. returnedElement.setPosition(
  343. notifyPos(new NonLeafPosition(this, returnedElement.getPosition())));
  344. returnList.add(returnedElement);
  345. }
  346. return returnList;
  347. }
  348. public int getBreakBefore() {
  349. return BreakOpportunityHelper.getBreakBefore(this);
  350. }
  351. }