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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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.Collections;
  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. * TODO [GA] replace use of hungarian notation with normalized java naming
  46. */
  47. public abstract class LeafNodeLayoutManager extends AbstractLayoutManager
  48. implements InlineLevelLayoutManager {
  49. /**
  50. * logging instance
  51. */
  52. protected static final Log log = LogFactory.getLog(LeafNodeLayoutManager.class);
  53. /**
  54. * The inline area that this leafnode will add.
  55. */
  56. protected InlineArea curArea = null;
  57. /** Any border, padding and background properties applying to this area */
  58. protected CommonBorderPaddingBackground commonBorderPaddingBackground = null;
  59. /** The alignment context applying to this area */
  60. protected AlignmentContext alignmentContext = null;
  61. /** Flag to indicate if something was changed as part of the getChangeKnuthElements sequence */
  62. protected boolean isSomethingChanged = false;
  63. /** Our area info for the Knuth elements */
  64. protected AreaInfo areaInfo = null;
  65. /**
  66. * Store information about the inline area
  67. */
  68. protected class AreaInfo {
  69. /** letter space count */
  70. protected short iLScount;
  71. /** ipd of area */
  72. protected MinOptMax ipdArea;
  73. /** true if hyphenated */
  74. protected boolean bHyphenated;
  75. /** alignment context */
  76. protected AlignmentContext alignmentContext;
  77. /**
  78. * Construct an area information item.
  79. * @param iLS letter space count
  80. * @param ipd inline progression dimension
  81. * @param bHyph true if hyphenated
  82. * @param alignmentContext an alignment context
  83. */
  84. public AreaInfo(short iLS, MinOptMax ipd, boolean bHyph,
  85. AlignmentContext alignmentContext) {
  86. iLScount = iLS;
  87. ipdArea = ipd;
  88. bHyphenated = bHyph;
  89. this.alignmentContext = alignmentContext;
  90. }
  91. }
  92. /**
  93. * Create a Leaf node layout manager.
  94. * @param node the FObj to attach to this LM.
  95. */
  96. public LeafNodeLayoutManager(FObj node) {
  97. super(node);
  98. }
  99. /**
  100. * Create a Leaf node layout manager.
  101. */
  102. public LeafNodeLayoutManager() {
  103. }
  104. /**
  105. * get the inline area.
  106. * @param context the context used to create the area
  107. * @return the current inline area for this layout manager
  108. */
  109. public InlineArea get(LayoutContext context) {
  110. return curArea;
  111. }
  112. /**
  113. * Check if this inline area is resolved due to changes in
  114. * page or ipd.
  115. * Currently not used.
  116. * @return true if the area is resolved when adding
  117. */
  118. public boolean resolved() {
  119. return false;
  120. }
  121. /**
  122. * Set the current inline area.
  123. * @param ia the inline area to set for this layout manager
  124. */
  125. public void setCurrentArea(InlineArea ia) {
  126. curArea = ia;
  127. }
  128. /**
  129. * This is a leaf-node, so this method is never called.
  130. * @param childArea the childArea to add
  131. */
  132. public void addChildArea(Area childArea) {
  133. }
  134. /**
  135. * This is a leaf-node, so this method is never called.
  136. * @param childArea the childArea to get the parent for
  137. * @return the parent area
  138. */
  139. public Area getParentArea(Area childArea) {
  140. return null;
  141. }
  142. /**
  143. * Set the border and padding properties of the inline area.
  144. * @param commonBorderPaddingBackground the alignment adjust property
  145. */
  146. protected void setCommonBorderPaddingBackground(
  147. CommonBorderPaddingBackground commonBorderPaddingBackground) {
  148. this.commonBorderPaddingBackground = commonBorderPaddingBackground;
  149. }
  150. /**
  151. * Get the allocation ipd of the inline area.
  152. * This method may be overridden to handle percentage values.
  153. * @param refIPD the ipd of the parent reference area
  154. * @return the min/opt/max ipd of the inline area
  155. */
  156. protected MinOptMax getAllocationIPD(int refIPD) {
  157. return MinOptMax.getInstance(curArea.getIPD());
  158. }
  159. /**
  160. * Add the area for this layout manager.
  161. * This adds the single inline area to the parent.
  162. * @param posIter the position iterator
  163. * @param context the layout context for adding the area
  164. */
  165. public void addAreas(PositionIterator posIter, LayoutContext context) {
  166. addId();
  167. InlineArea area = getEffectiveArea();
  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() {
  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.setOffset(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. public List getNextKnuthElements(LayoutContext context, int alignment) {
  234. curArea = get(context);
  235. if (curArea == null) {
  236. setFinished(true);
  237. return null;
  238. }
  239. alignmentContext = makeAlignmentContext(context);
  240. MinOptMax ipd = getAllocationIPD(context.getRefIPD());
  241. // create the AreaInfo object to store the computed values
  242. areaInfo = new AreaInfo((short) 0, ipd, false, alignmentContext);
  243. // node is a fo:ExternalGraphic, fo:InstreamForeignObject,
  244. // fo:PageNumber or fo:PageNumberCitation
  245. KnuthSequence seq = new InlineKnuthSequence();
  246. addKnuthElementsForBorderPaddingStart(seq);
  247. seq.add(new KnuthInlineBox(areaInfo.ipdArea.getOpt(), alignmentContext,
  248. notifyPos(new LeafPosition(this, 0)), false));
  249. addKnuthElementsForBorderPaddingEnd(seq);
  250. setFinished(true);
  251. return Collections.singletonList(seq);
  252. }
  253. /** {@inheritDoc} */
  254. public List addALetterSpaceTo(List oldList) {
  255. // return the unchanged elements
  256. return oldList;
  257. }
  258. /**
  259. * Remove the word space represented by the given elements
  260. *
  261. * @param oldList the elements representing the word space
  262. */
  263. public void removeWordSpace(List oldList) {
  264. // do nothing
  265. log.warn(this.getClass().getName() + " should not receive a call to removeWordSpace(list)");
  266. }
  267. /** {@inheritDoc} */
  268. public String getWordChars(Position pos) {
  269. return "";
  270. }
  271. /** {@inheritDoc} */
  272. public void hyphenate(Position pos, HyphContext hyphContext) {
  273. }
  274. /** {@inheritDoc} */
  275. public boolean applyChanges(List oldList) {
  276. setFinished(false);
  277. return false;
  278. }
  279. /** {@inheritDoc} */
  280. public List getChangedKnuthElements(List oldList,
  281. int alignment) {
  282. if (isFinished()) {
  283. return null;
  284. }
  285. LinkedList returnList = new LinkedList();
  286. addKnuthElementsForBorderPaddingStart(returnList);
  287. // fobj is a fo:ExternalGraphic, fo:InstreamForeignObject,
  288. // fo:PageNumber or fo:PageNumberCitation
  289. returnList.add(new KnuthInlineBox(areaInfo.ipdArea.getOpt(), areaInfo.alignmentContext,
  290. notifyPos(new LeafPosition(this, 0)), true));
  291. addKnuthElementsForBorderPaddingEnd(returnList);
  292. setFinished(true);
  293. return returnList;
  294. }
  295. /**
  296. * Creates Knuth elements for start border padding and adds them to the return list.
  297. * @param returnList return list to add the additional elements to
  298. */
  299. protected void addKnuthElementsForBorderPaddingStart(List returnList) {
  300. //Border and Padding (start)
  301. if (commonBorderPaddingBackground != null) {
  302. int ipStart = commonBorderPaddingBackground.getBorderStartWidth(false)
  303. + commonBorderPaddingBackground.getPaddingStart(false, this);
  304. if (ipStart > 0) {
  305. // Add a non breakable glue
  306. returnList.add(new KnuthPenalty(0, KnuthPenalty.INFINITE,
  307. false, new LeafPosition(this, -1), true));
  308. returnList.add(new KnuthGlue(ipStart, 0, 0, new LeafPosition(this, -1), true));
  309. }
  310. }
  311. }
  312. /**
  313. * Creates Knuth elements for end border padding and adds them to the return list.
  314. * @param returnList return list to add the additional elements to
  315. */
  316. protected void addKnuthElementsForBorderPaddingEnd(List returnList) {
  317. //Border and Padding (after)
  318. if (commonBorderPaddingBackground != null) {
  319. int ipEnd = commonBorderPaddingBackground.getBorderEndWidth(false)
  320. + commonBorderPaddingBackground.getPaddingEnd(false, this);
  321. if (ipEnd > 0) {
  322. // Add a non breakable glue
  323. returnList.add(new KnuthPenalty(0, KnuthPenalty.INFINITE,
  324. false, new LeafPosition(this, -1), true));
  325. returnList.add(new KnuthGlue(ipEnd, 0, 0, new LeafPosition(this, -1), true));
  326. }
  327. }
  328. }
  329. }