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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /*
  2. * $Id: LeafNodeLayoutManager.java,v 1.23 2003/03/07 07:58:51 jeremias Exp $
  3. * ============================================================================
  4. * The Apache Software License, Version 1.1
  5. * ============================================================================
  6. *
  7. * Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without modifica-
  10. * tion, are permitted provided that the following conditions are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright notice,
  13. * this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. *
  19. * 3. The end-user documentation included with the redistribution, if any, must
  20. * include the following acknowledgment: "This product includes software
  21. * developed by the Apache Software Foundation (http://www.apache.org/)."
  22. * Alternately, this acknowledgment may appear in the software itself, if
  23. * and wherever such third-party acknowledgments normally appear.
  24. *
  25. * 4. The names "FOP" and "Apache Software Foundation" must not be used to
  26. * endorse or promote products derived from this software without prior
  27. * written permission. For written permission, please contact
  28. * apache@apache.org.
  29. *
  30. * 5. Products derived from this software may not be called "Apache", nor may
  31. * "Apache" appear in their name, without prior written permission of the
  32. * Apache Software Foundation.
  33. *
  34. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
  35. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  36. * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  37. * APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  38. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
  39. * DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  40. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  41. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  42. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  43. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  44. * ============================================================================
  45. *
  46. * This software consists of voluntary contributions made by many individuals
  47. * on behalf of the Apache Software Foundation and was originally created by
  48. * James Tauber <jtauber@jtauber.com>. For more information on the Apache
  49. * Software Foundation, please see <http://www.apache.org/>.
  50. */
  51. package org.apache.fop.layoutmgr;
  52. import org.apache.fop.area.Area;
  53. import org.apache.fop.area.inline.InlineArea;
  54. import org.apache.fop.fo.properties.VerticalAlign;
  55. import org.apache.fop.traits.MinOptMax;
  56. /**
  57. * Base LayoutManager for leaf-node FObj, ie: ones which have no children.
  58. * These are all inline objects. Most of them cannot be split (Text is
  59. * an exception to this rule.)
  60. * This class can be extended to handle the creation and adding of the
  61. * inline area.
  62. */
  63. public class LeafNodeLayoutManager extends AbstractLayoutManager {
  64. /**
  65. * The inline area that this leafnode will add.
  66. */
  67. protected InlineArea curArea = null;
  68. private int alignment;
  69. private int lead;
  70. private MinOptMax ipd;
  71. /**
  72. * Create a Leaf node layout mananger.
  73. */
  74. public LeafNodeLayoutManager() {
  75. }
  76. /**
  77. * get the inline area.
  78. * @param context the context used to create the area
  79. * @return the current inline area for this layout manager
  80. */
  81. public InlineArea get(LayoutContext context) {
  82. return curArea;
  83. }
  84. /**
  85. * Check if this generates inline areas.
  86. * @return true always since this is an inline area manager
  87. */
  88. public boolean generatesInlineAreas() {
  89. return true;
  90. }
  91. /**
  92. * Check if this inline area is resolved due to changes in
  93. * page or ipd.
  94. * Currently not used.
  95. * @return true if the area is resolved when adding
  96. */
  97. public boolean resolved() {
  98. return false;
  99. }
  100. /**
  101. * Set the current inline area.
  102. * @param ia the inline area to set for this layout manager
  103. */
  104. public void setCurrentArea(InlineArea ia) {
  105. curArea = ia;
  106. }
  107. /**
  108. * Set the alignment of the inline area.
  109. * @param al the vertical alignment positioning
  110. */
  111. public void setAlignment(int al) {
  112. alignment = al;
  113. }
  114. /**
  115. * Set the lead for this inline area.
  116. * The lead is the distance from the top of the object
  117. * to the baseline.
  118. * Currently not used.
  119. * @param l the lead value
  120. */
  121. public void setLead(int l) {
  122. lead = l;
  123. }
  124. /**
  125. * This is a leaf-node, so this method is never called.
  126. * @param childArea the childArea to add
  127. */
  128. public void addChild(Area childArea) {
  129. }
  130. /**
  131. * This is a leaf-node, so this method is never called.
  132. * @param childArea the childArea to get the parent for
  133. * @return the parent area
  134. */
  135. public Area getParentArea(Area childArea) {
  136. return null;
  137. }
  138. /**
  139. * Get the next break position.
  140. * Since this holds an inline area it will return a single
  141. * break position.
  142. * @param context the layout context for this inline area
  143. * @return the break poisition for adding this inline area
  144. */
  145. public BreakPoss getNextBreakPoss(LayoutContext context) {
  146. curArea = get(context);
  147. if (curArea == null) {
  148. setFinished(true);
  149. return null;
  150. }
  151. BreakPoss bp = new BreakPoss(new LeafPosition(this, 0),
  152. BreakPoss.CAN_BREAK_AFTER
  153. | BreakPoss.CAN_BREAK_BEFORE | BreakPoss.ISFIRST
  154. | BreakPoss.ISLAST);
  155. ipd = getAllocationIPD(context.getRefIPD());
  156. bp.setStackingSize(ipd);
  157. bp.setNonStackingSize(new MinOptMax(curArea.getHeight()));
  158. bp.setTrailingSpace(new SpaceSpecifier(false));
  159. int bpd = curArea.getHeight();
  160. switch (alignment) {
  161. case VerticalAlign.MIDDLE:
  162. bp.setMiddle(bpd / 2 /* - fontLead/2 */);
  163. bp.setLead(bpd / 2 /* + fontLead/2 */);
  164. break;
  165. case VerticalAlign.TOP:
  166. bp.setTotal(bpd);
  167. break;
  168. case VerticalAlign.BOTTOM:
  169. bp.setTotal(bpd);
  170. break;
  171. case VerticalAlign.BASELINE:
  172. default:
  173. bp.setLead(bpd);
  174. break;
  175. }
  176. setFinished(true);
  177. return bp;
  178. }
  179. /**
  180. * Get the allocation ipd of the inline area.
  181. * This method may be overridden to handle percentage values.
  182. * @param refIPD the ipd of the parent reference area
  183. * @return the min/opt/max ipd of the inline area
  184. */
  185. protected MinOptMax getAllocationIPD(int refIPD) {
  186. return new MinOptMax(curArea.getIPD());
  187. }
  188. /**
  189. * Reset the position.
  190. * If the reset position is null then this inline area should be
  191. * restarted.
  192. * @param resetPos the position to reset.
  193. */
  194. public void resetPosition(Position resetPos) {
  195. // only reset if setting null, start again
  196. if (resetPos == null) {
  197. setFinished(false);
  198. }
  199. }
  200. /**
  201. * Add the area for this layout manager.
  202. * This adds the single inline area to the parent.
  203. * @param posIter the position iterator
  204. * @param context the layout context for adding the area
  205. */
  206. public void addAreas(PositionIterator posIter, LayoutContext context) {
  207. parentLM.addChild(curArea);
  208. addID();
  209. offsetArea(context);
  210. widthAdjustArea(context);
  211. while (posIter.hasNext()) {
  212. posIter.next();
  213. }
  214. }
  215. /**
  216. * Offset this area.
  217. * Offset the inline area in the bpd direction when adding the
  218. * inline area.
  219. * This is used for vertical alignment.
  220. * Subclasses should override this if necessary.
  221. * @param context the layout context used for adding the area
  222. */
  223. protected void offsetArea(LayoutContext context) {
  224. int bpd = curArea.getHeight();
  225. switch (alignment) {
  226. case VerticalAlign.MIDDLE:
  227. curArea.setOffset(context.getBaseline() - bpd / 2 /* - fontLead/2 */);
  228. break;
  229. case VerticalAlign.TOP:
  230. //curArea.setOffset(0);
  231. break;
  232. case VerticalAlign.BOTTOM:
  233. curArea.setOffset(context.getLineHeight() - bpd);
  234. break;
  235. case VerticalAlign.BASELINE:
  236. default:
  237. curArea.setOffset(context.getBaseline() - bpd);
  238. break;
  239. }
  240. }
  241. /**
  242. * Adjust the width of the area when adding.
  243. * This uses the min/opt/max values to adjust the with
  244. * of the inline area by a percentage.
  245. * @param context the layout context for adding this area
  246. */
  247. protected void widthAdjustArea(LayoutContext context) {
  248. double dAdjust = context.getIPDAdjust();
  249. int width = ipd.opt;
  250. if (dAdjust < 0) {
  251. width = (int)(width + dAdjust * (ipd.opt - ipd.min));
  252. } else if (dAdjust > 0) {
  253. width = (int)(width + dAdjust * (ipd.max - ipd.opt));
  254. }
  255. curArea.setWidth(width);
  256. }
  257. /**
  258. * Check if can break before this area.
  259. * @param context the layout context to check for the break
  260. * @return true if can break before this area in the context
  261. */
  262. public boolean canBreakBefore(LayoutContext context) {
  263. return true;
  264. }
  265. }