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.

LeafNodeLayoutManager.java 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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 org.apache.commons.logging.Log;
  22. import org.apache.commons.logging.LogFactory;
  23. import org.apache.fop.area.Area;
  24. import org.apache.fop.area.inline.InlineArea;
  25. import org.apache.fop.fo.FObj;
  26. import org.apache.fop.fo.properties.CommonBorderPaddingBackground;
  27. import org.apache.fop.layoutmgr.AbstractLayoutManager;
  28. import org.apache.fop.layoutmgr.InlineKnuthSequence;
  29. import org.apache.fop.layoutmgr.KnuthGlue;
  30. import org.apache.fop.layoutmgr.KnuthPenalty;
  31. import org.apache.fop.layoutmgr.KnuthSequence;
  32. import org.apache.fop.layoutmgr.LayoutContext;
  33. import org.apache.fop.layoutmgr.LeafPosition;
  34. import org.apache.fop.layoutmgr.Position;
  35. import org.apache.fop.layoutmgr.PositionIterator;
  36. import org.apache.fop.layoutmgr.TraitSetter;
  37. import org.apache.fop.traits.MinOptMax;
  38. /**
  39. * Base LayoutManager for leaf-node FObj, ie: ones which have no children.
  40. * These are all inline objects. Most of them cannot be split (Text is
  41. * an exception to this rule.)
  42. * This class can be extended to handle the creation and adding of the
  43. * inline area.
  44. */
  45. public abstract class LeafNodeLayoutManager extends AbstractLayoutManager
  46. implements InlineLevelLayoutManager {
  47. /**
  48. * logging instance
  49. */
  50. protected static Log log = LogFactory.getLog(LeafNodeLayoutManager.class);
  51. /**
  52. * The inline area that this leafnode will add.
  53. */
  54. protected InlineArea curArea = null;
  55. /** Any border, padding and background properties applying to this area */
  56. protected CommonBorderPaddingBackground commonBorderPaddingBackground = null;
  57. /** The alignment context applying to this area */
  58. protected AlignmentContext alignmentContext = null;
  59. /** Flag to indicate if something was changed as part of the getChangeKnuthElements sequence */
  60. protected boolean isSomethingChanged = false;
  61. /** Our area info for the Knuth elements */
  62. protected AreaInfo areaInfo = null;
  63. /**
  64. * Store information about the inline area
  65. */
  66. protected class AreaInfo {
  67. protected short iLScount;
  68. protected MinOptMax ipdArea;
  69. protected boolean bHyphenated;
  70. protected AlignmentContext alignmentContext;
  71. public AreaInfo(short iLS, MinOptMax ipd, boolean bHyph,
  72. AlignmentContext alignmentContext) {
  73. iLScount = iLS;
  74. ipdArea = ipd;
  75. bHyphenated = bHyph;
  76. this.alignmentContext = alignmentContext;
  77. }
  78. }
  79. /**
  80. * Create a Leaf node layout mananger.
  81. * @param node the FObj to attach to this LM.
  82. */
  83. public LeafNodeLayoutManager(FObj node) {
  84. super(node);
  85. }
  86. /**
  87. * Create a Leaf node layout mananger.
  88. */
  89. public LeafNodeLayoutManager() {
  90. }
  91. /**
  92. * get the inline area.
  93. * @param context the context used to create the area
  94. * @return the current inline area for this layout manager
  95. */
  96. public InlineArea get(LayoutContext context) {
  97. return curArea;
  98. }
  99. /**
  100. * Check if this inline area is resolved due to changes in
  101. * page or ipd.
  102. * Currently not used.
  103. * @return true if the area is resolved when adding
  104. */
  105. public boolean resolved() {
  106. return false;
  107. }
  108. /**
  109. * Set the current inline area.
  110. * @param ia the inline area to set for this layout manager
  111. */
  112. public void setCurrentArea(InlineArea ia) {
  113. curArea = ia;
  114. }
  115. /**
  116. * This is a leaf-node, so this method is never called.
  117. * @param childArea the childArea to add
  118. */
  119. public void addChildArea(Area childArea) {
  120. }
  121. /**
  122. * This is a leaf-node, so this method is never called.
  123. * @param childArea the childArea to get the parent for
  124. * @return the parent area
  125. */
  126. public Area getParentArea(Area childArea) {
  127. return null;
  128. }
  129. /**
  130. * Set the border and padding properties of the inline area.
  131. * @param commonBorderPaddingBackground the alignment adjust property
  132. */
  133. protected void setCommonBorderPaddingBackground(
  134. CommonBorderPaddingBackground commonBorderPaddingBackground) {
  135. this.commonBorderPaddingBackground = commonBorderPaddingBackground;
  136. }
  137. /**
  138. * Get the allocation ipd of the inline area.
  139. * This method may be overridden to handle percentage values.
  140. * @param refIPD the ipd of the parent reference area
  141. * @return the min/opt/max ipd of the inline area
  142. */
  143. protected MinOptMax getAllocationIPD(int refIPD) {
  144. return new MinOptMax(curArea.getIPD());
  145. }
  146. /**
  147. * Add the area for this layout manager.
  148. * This adds the single inline area to the parent.
  149. * @param posIter the position iterator
  150. * @param context the layout context for adding the area
  151. */
  152. public void addAreas(PositionIterator posIter, LayoutContext context) {
  153. addId();
  154. InlineArea area = getEffectiveArea();
  155. if (area.getAllocIPD() > 0 || area.getAllocBPD() > 0) {
  156. offsetArea(area, context);
  157. widthAdjustArea(area, context);
  158. if (commonBorderPaddingBackground != null) {
  159. // Add border and padding to area
  160. TraitSetter.setBorderPaddingTraits(area,
  161. commonBorderPaddingBackground,
  162. false, false, this);
  163. TraitSetter.addBackground(area, commonBorderPaddingBackground, this);
  164. }
  165. parentLM.addChildArea(area);
  166. }
  167. while (posIter.hasNext()) {
  168. posIter.next();
  169. }
  170. }
  171. /**
  172. * @return the effective area to be added to the area tree. Normally, this is simply "curArea"
  173. * but in the case of page-number(-citation) curArea is cloned, updated and returned.
  174. */
  175. protected InlineArea getEffectiveArea() {
  176. return curArea;
  177. }
  178. /**
  179. * Offset this area.
  180. * Offset the inline area in the bpd direction when adding the
  181. * inline area.
  182. * This is used for vertical alignment.
  183. * Subclasses should override this if necessary.
  184. * @param area the inline area to be updated
  185. * @param context the layout context used for adding the area
  186. */
  187. protected void offsetArea(InlineArea area, LayoutContext context) {
  188. area.setOffset(alignmentContext.getOffset());
  189. }
  190. /**
  191. * Creates a new alignment context or returns the current
  192. * alignment context.
  193. * This is used for vertical alignment.
  194. * Subclasses should override this if necessary.
  195. * @param context the layout context used
  196. * @return the appropriate alignment context
  197. */
  198. protected AlignmentContext makeAlignmentContext(LayoutContext context) {
  199. return context.getAlignmentContext();
  200. }
  201. /**
  202. * Adjust the width of the area when adding.
  203. * This uses the min/opt/max values to adjust the with
  204. * of the inline area by a percentage.
  205. * @param area the inline area to be updated
  206. * @param context the layout context for adding this area
  207. */
  208. protected void widthAdjustArea(InlineArea area, LayoutContext context) {
  209. double dAdjust = context.getIPDAdjust();
  210. int width = areaInfo.ipdArea.opt;
  211. if (dAdjust < 0) {
  212. width = (int) (width + dAdjust * (areaInfo.ipdArea.opt
  213. - areaInfo.ipdArea.min));
  214. } else if (dAdjust > 0) {
  215. width = (int) (width + dAdjust * (areaInfo.ipdArea.max
  216. - areaInfo.ipdArea.opt));
  217. }
  218. area.setIPD(width);
  219. area.setAdjustment(width - areaInfo.ipdArea.opt);
  220. }
  221. /** {@inheritDoc} */
  222. public LinkedList getNextKnuthElements(LayoutContext context, int alignment) {
  223. curArea = get(context);
  224. if (curArea == null) {
  225. setFinished(true);
  226. return null;
  227. }
  228. alignmentContext = makeAlignmentContext(context);
  229. MinOptMax ipd = getAllocationIPD(context.getRefIPD());
  230. // create the AreaInfo object to store the computed values
  231. areaInfo = new AreaInfo((short) 0, ipd, false, alignmentContext);
  232. // node is a fo:ExternalGraphic, fo:InstreamForeignObject,
  233. // fo:PageNumber or fo:PageNumberCitation
  234. KnuthSequence seq = new InlineKnuthSequence();
  235. addKnuthElementsForBorderPaddingStart(seq);
  236. seq.add(new KnuthInlineBox(areaInfo.ipdArea.opt, alignmentContext,
  237. notifyPos(new LeafPosition(this, 0)), false));
  238. addKnuthElementsForBorderPaddingEnd(seq);
  239. LinkedList returnList = new LinkedList();
  240. returnList.add(seq);
  241. setFinished(true);
  242. return returnList;
  243. }
  244. /** {@inheritDoc} */
  245. public List addALetterSpaceTo(List oldList) {
  246. // return the unchanged elements
  247. return oldList;
  248. }
  249. /**
  250. * Remove the word space represented by the given elements
  251. *
  252. * @param oldList the elements representing the word space
  253. */
  254. public void removeWordSpace(List oldList) {
  255. // do nothing
  256. log.warn(this.getClass().getName() + " should not receive a call to removeWordSpace(list)");
  257. }
  258. /** {@inheritDoc} */
  259. public void getWordChars(StringBuffer sbChars, Position pos) {
  260. }
  261. /** {@inheritDoc} */
  262. public void hyphenate(Position pos, HyphContext hc) {
  263. }
  264. /** {@inheritDoc} */
  265. public boolean applyChanges(List oldList) {
  266. setFinished(false);
  267. return false;
  268. }
  269. /** {@inheritDoc} */
  270. public LinkedList getChangedKnuthElements(List oldList,
  271. int alignment) {
  272. if (isFinished()) {
  273. return null;
  274. }
  275. LinkedList returnList = new LinkedList();
  276. addKnuthElementsForBorderPaddingStart(returnList);
  277. // fobj is a fo:ExternalGraphic, fo:InstreamForeignObject,
  278. // fo:PageNumber or fo:PageNumberCitation
  279. returnList.add(new KnuthInlineBox(areaInfo.ipdArea.opt, areaInfo.alignmentContext,
  280. notifyPos(new LeafPosition(this, 0)), true));
  281. addKnuthElementsForBorderPaddingEnd(returnList);
  282. setFinished(true);
  283. return returnList;
  284. }
  285. /**
  286. * Creates Knuth elements for start border padding and adds them to the return list.
  287. * @param returnList return list to add the additional elements to
  288. */
  289. protected void addKnuthElementsForBorderPaddingStart(List returnList) {
  290. //Border and Padding (start)
  291. if (commonBorderPaddingBackground != null) {
  292. int ipStart = commonBorderPaddingBackground.getBorderStartWidth(false)
  293. + commonBorderPaddingBackground.getPaddingStart(false, this);
  294. if (ipStart > 0) {
  295. // Add a non breakable glue
  296. returnList.add(new KnuthPenalty(0, KnuthPenalty.INFINITE,
  297. false, new LeafPosition(this, -1), true));
  298. returnList.add(new KnuthGlue(ipStart, 0, 0, new LeafPosition(this, -1), true));
  299. }
  300. }
  301. }
  302. /**
  303. * Creates Knuth elements for end border padding and adds them to the return list.
  304. * @param returnList return list to add the additional elements to
  305. */
  306. protected void addKnuthElementsForBorderPaddingEnd(List returnList) {
  307. //Border and Padding (after)
  308. if (commonBorderPaddingBackground != null) {
  309. int ipEnd = commonBorderPaddingBackground.getBorderEndWidth(false)
  310. + commonBorderPaddingBackground.getPaddingEnd(false, this);
  311. if (ipEnd > 0) {
  312. // Add a non breakable glue
  313. returnList.add(new KnuthPenalty(0, KnuthPenalty.INFINITE,
  314. false, new LeafPosition(this, -1), true));
  315. returnList.add(new KnuthGlue(ipEnd, 0, 0, new LeafPosition(this, -1), true));
  316. }
  317. }
  318. }
  319. }