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.

AbstractBaseLayoutManager.java 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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;
  19. import java.util.List;
  20. import java.util.Stack;
  21. import org.apache.commons.logging.Log;
  22. import org.apache.commons.logging.LogFactory;
  23. import org.apache.fop.datatypes.LengthBase;
  24. import org.apache.fop.datatypes.PercentBaseContext;
  25. import org.apache.fop.fo.FObj;
  26. /**
  27. * The base class for nearly all LayoutManagers.
  28. * Provides the functionality for merging the {@link LayoutManager}
  29. * and the {@link org.apache.fop.datatypes.PercentBaseContext} interfaces
  30. * into a common base class for all higher LayoutManagers.
  31. */
  32. public abstract class AbstractBaseLayoutManager
  33. implements LayoutManager, PercentBaseContext {
  34. /** Indicator if this LM generates reference areas. */
  35. protected boolean generatesReferenceArea = false;
  36. /** Indicator if this LM generates block areas. */
  37. protected boolean generatesBlockArea = false;
  38. /** The formatting object for this LM. */
  39. protected final FObj fobj;
  40. /**
  41. * logging instance
  42. */
  43. private static final Log LOG = LogFactory.getLog(AbstractBaseLayoutManager.class);
  44. /**
  45. * Abstract base layout manager.
  46. */
  47. public AbstractBaseLayoutManager() {
  48. fobj = null;
  49. }
  50. /**
  51. * Abstract base layout manager.
  52. *
  53. * @param fo the formatting object for this layout manager
  54. */
  55. public AbstractBaseLayoutManager(FObj fo) {
  56. this.fobj = fo;
  57. setGeneratesReferenceArea(fo.generatesReferenceAreas());
  58. if (getGeneratesReferenceArea()) {
  59. setGeneratesBlockArea(true);
  60. }
  61. }
  62. // --------- Property Resolution related functions --------- //
  63. /** {@inheritDoc} */
  64. public int getBaseLength(int lengthBase, FObj fobjx) {
  65. if (fobjx == this.fobj) {
  66. switch (lengthBase) {
  67. case LengthBase.CONTAINING_BLOCK_WIDTH:
  68. return getAncestorBlockAreaIPD();
  69. case LengthBase.CONTAINING_BLOCK_HEIGHT:
  70. return getAncestorBlockAreaBPD();
  71. case LengthBase.PARENT_AREA_WIDTH:
  72. return getParentAreaIPD();
  73. case LengthBase.CONTAINING_REFAREA_WIDTH:
  74. return getReferenceAreaIPD();
  75. default:
  76. LOG.error("Unknown base type for LengthBase:" + lengthBase);
  77. return 0;
  78. }
  79. } else {
  80. LayoutManager lm = getParent();
  81. while (lm != null && fobjx != lm.getFObj()) {
  82. lm = lm.getParent();
  83. }
  84. if (lm != null) {
  85. return lm.getBaseLength(lengthBase, fobjx);
  86. }
  87. }
  88. LOG.error("Cannot find LM to handle given FO for LengthBase. ("
  89. + fobjx.getContextInfo() + ")");
  90. return 0;
  91. }
  92. /**
  93. * Find the first ancestor area that is a block area
  94. * and returns its IPD.
  95. * @return the ipd of the ancestor block area
  96. */
  97. protected int getAncestorBlockAreaIPD() {
  98. LayoutManager lm = getParent();
  99. while (lm != null) {
  100. if (lm.getGeneratesBlockArea() && !lm.getGeneratesLineArea()) {
  101. return lm.getContentAreaIPD();
  102. }
  103. lm = lm.getParent();
  104. }
  105. LOG.error("No parent LM found");
  106. return 0;
  107. }
  108. /**
  109. * Find the first ancestor area that is a block area
  110. * and returns its BPD.
  111. * @return the bpd of the ancestor block area
  112. */
  113. protected int getAncestorBlockAreaBPD() {
  114. LayoutManager lm = getParent();
  115. while (lm != null) {
  116. if (lm.getGeneratesBlockArea() && !lm.getGeneratesLineArea()) {
  117. return lm.getContentAreaBPD();
  118. }
  119. lm = lm.getParent();
  120. }
  121. LOG.error("No parent LM found");
  122. return 0;
  123. }
  124. /**
  125. * Find the parent area and returns its IPD.
  126. * @return the ipd of the parent area
  127. */
  128. protected int getParentAreaIPD() {
  129. LayoutManager lm = getParent();
  130. if (lm != null) {
  131. return lm.getContentAreaIPD();
  132. }
  133. LOG.error("No parent LM found");
  134. return 0;
  135. }
  136. /**
  137. * Find the parent area and returns its BPD.
  138. * @return the bpd of the parent area
  139. */
  140. protected int getParentAreaBPD() {
  141. LayoutManager lm = getParent();
  142. if (lm != null) {
  143. return lm.getContentAreaBPD();
  144. }
  145. LOG.error("No parent LM found");
  146. return 0;
  147. }
  148. /**
  149. * Find the first ancestor area that is a reference area
  150. * and returns its IPD.
  151. * @return the ipd of the ancestor reference area
  152. */
  153. public int getReferenceAreaIPD() {
  154. LayoutManager lm = getParent();
  155. while (lm != null) {
  156. if (lm.getGeneratesReferenceArea()) {
  157. return lm.getContentAreaIPD();
  158. }
  159. lm = lm.getParent();
  160. }
  161. LOG.error("No parent LM found");
  162. return 0;
  163. }
  164. /**
  165. * Find the first ancestor area that is a reference area
  166. * and returns its BPD.
  167. * @return the bpd of the ancestor reference area
  168. */
  169. protected int getReferenceAreaBPD() {
  170. LayoutManager lm = getParent();
  171. while (lm != null) {
  172. if (lm.getGeneratesReferenceArea()) {
  173. return lm.getContentAreaBPD();
  174. }
  175. lm = lm.getParent();
  176. }
  177. LOG.error("No parent LM found");
  178. return 0;
  179. }
  180. /**
  181. * {@inheritDoc}
  182. * <i>NOTE: Should be overridden by subclasses.
  183. * Default implementation throws an <code>UnsupportedOperationException</code>.</i>
  184. */
  185. public int getContentAreaIPD() {
  186. throw new UnsupportedOperationException(
  187. "getContentAreaIPD() called when it should have been overridden");
  188. }
  189. /**
  190. * {@inheritDoc}
  191. * <i>NOTE: Should be overridden by subclasses.
  192. * Default implementation throws an <code>UnsupportedOperationException</code>.</i>
  193. */
  194. public int getContentAreaBPD() {
  195. throw new UnsupportedOperationException(
  196. "getContentAreaBPD() called when it should have been overridden");
  197. }
  198. /** {@inheritDoc} */
  199. public boolean getGeneratesReferenceArea() {
  200. return generatesReferenceArea;
  201. }
  202. /**
  203. * Lets implementing LM set the flag indicating if they
  204. * generate reference areas.
  205. * @param generatesReferenceArea if true the areas generates by this LM are
  206. * reference areas.
  207. */
  208. protected void setGeneratesReferenceArea(boolean generatesReferenceArea) {
  209. this.generatesReferenceArea = generatesReferenceArea;
  210. }
  211. /** {@inheritDoc} */
  212. public boolean getGeneratesBlockArea() {
  213. return generatesBlockArea;
  214. }
  215. /**
  216. * Lets implementing LM set the flag indicating if they
  217. * generate block areas.
  218. * @param generatesBlockArea if true the areas generates by this LM are block areas.
  219. */
  220. protected void setGeneratesBlockArea(boolean generatesBlockArea) {
  221. this.generatesBlockArea = generatesBlockArea;
  222. }
  223. /** {@inheritDoc} */
  224. public boolean getGeneratesLineArea() {
  225. return false;
  226. }
  227. /**
  228. * {@inheritDoc}
  229. */
  230. public FObj getFObj() {
  231. return fobj;
  232. }
  233. /** {@inheritDoc} */
  234. public void reset() {
  235. throw new UnsupportedOperationException("Not implemented");
  236. }
  237. /** {@inheritDoc} */
  238. public boolean isRestartable() {
  239. return false;
  240. }
  241. /** {@inheritDoc} */
  242. public List getNextKnuthElements(LayoutContext context, int alignment, Stack lmStack,
  243. Position positionAtIPDChange, LayoutManager restartAtLM) {
  244. throw new UnsupportedOperationException("Not implemented");
  245. }
  246. public void preserveChildrenAtEndOfLayout() {
  247. }
  248. public void recreateChildrenLMs() {
  249. }
  250. }