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

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