Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

AbstractBaseLayoutManager.java 8.3KB

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