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.

Area.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  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.area;
  19. import java.io.Serializable;
  20. import java.util.Map;
  21. import org.apache.commons.logging.Log;
  22. import org.apache.commons.logging.LogFactory;
  23. import org.apache.fop.traits.BorderProps;
  24. // If the area appears more than once in the output
  25. // or if the area has external data it is cached
  26. // to keep track of it and to minimize rendered output
  27. // renderers can render the output once and display it
  28. // for every occurence
  29. // this should also extend to all outputs (including PDFGraphics2D)
  30. // and all types of renderers
  31. /**
  32. * Base object for all areas.
  33. */
  34. public class Area extends AreaTreeObject implements Serializable {
  35. private static final long serialVersionUID = 6342888466142626492L;
  36. // stacking directions
  37. /**
  38. * Stacking left to right
  39. */
  40. public static final int LR = 0;
  41. /**
  42. * Stacking right to left
  43. */
  44. public static final int RL = 1;
  45. /**
  46. * Stacking top to bottom
  47. */
  48. public static final int TB = 2;
  49. /**
  50. * Stacking bottom to top
  51. */
  52. public static final int BT = 3;
  53. // orientations for reference areas
  54. /**
  55. * Normal orientation
  56. */
  57. public static final int ORIENT_0 = 0;
  58. /**
  59. * Rotated 90 degrees clockwise
  60. */
  61. public static final int ORIENT_90 = 1;
  62. /**
  63. * Rotate 180 degrees
  64. */
  65. public static final int ORIENT_180 = 2;
  66. /**
  67. * Rotated 270 degrees clockwise
  68. */
  69. public static final int ORIENT_270 = 3;
  70. // area class values
  71. /**
  72. * Normal class
  73. */
  74. public static final int CLASS_NORMAL = 0;
  75. /**
  76. * Fixed position class
  77. */
  78. public static final int CLASS_FIXED = 1;
  79. /**
  80. * Absolute position class
  81. */
  82. public static final int CLASS_ABSOLUTE = 2;
  83. /**
  84. * Before float class
  85. */
  86. public static final int CLASS_BEFORE_FLOAT = 3;
  87. /**
  88. * Footnote class
  89. */
  90. public static final int CLASS_FOOTNOTE = 4;
  91. /**
  92. * Side float class
  93. */
  94. public static final int CLASS_SIDE_FLOAT = 5;
  95. // IMPORTANT: make sure this is the maximum + 1
  96. /**
  97. * Maximum class count
  98. */
  99. public static final int CLASS_MAX = CLASS_SIDE_FLOAT + 1;
  100. private int areaClass = CLASS_NORMAL;
  101. /** the area's inline-progression-dimension */
  102. protected int ipd;
  103. /** the area's block-progression-dimension */
  104. protected int bpd;
  105. /**
  106. * Traits for this area stored in a HashMap
  107. */
  108. protected Map props = null;
  109. /**
  110. * logging instance
  111. */
  112. protected static final Log log = LogFactory.getLog(Area.class);
  113. /**
  114. * Get the area class of this area.
  115. *
  116. * @return the area class
  117. */
  118. public int getAreaClass() {
  119. return this.areaClass;
  120. }
  121. /**
  122. * Set the area class of this area.
  123. *
  124. * @param areaClass the area class
  125. */
  126. public void setAreaClass(int areaClass) {
  127. this.areaClass = areaClass;
  128. }
  129. /**
  130. * Set the inline progression dimension of content rectangle
  131. * for this area.
  132. *
  133. * @param ipd the new inline progression dimension
  134. * @see <a href="http://www.w3.org/TR/xsl/#inline-progression-dimension">ipd</a>
  135. */
  136. public void setIPD(int ipd) {
  137. this.ipd = ipd;
  138. }
  139. /**
  140. * Get the inline progression dimension of the content rectangle
  141. * for this area.
  142. *
  143. * @return the inline progression dimension
  144. * @see <a href="http://www.w3.org/TR/xsl/#inline-progression-dimension">ipd</a>
  145. */
  146. public int getIPD() {
  147. return ipd;
  148. }
  149. /**
  150. * Set the block progression dimension of the content rectangle
  151. * for this area.
  152. *
  153. * @param bpd the new block progression dimension
  154. * @see <a href="http://www.w3.org/TR/xsl/#block-progression-dimension">bpd</a>
  155. */
  156. public void setBPD(int bpd) {
  157. this.bpd = bpd;
  158. }
  159. /**
  160. * Get the block progression dimension of the content rectangle
  161. * for this area.
  162. *
  163. * @return the block progression dimension
  164. * @see <a href="http://www.w3.org/TR/xsl/#block-progression-dimension">bpd</a>
  165. */
  166. public int getBPD() {
  167. return bpd;
  168. }
  169. /**
  170. * Get the allocation inline progression dimension of this area.
  171. * This adds the content, borders and the padding to find the
  172. * total allocated IPD.
  173. *
  174. * @return the total IPD allocation for this area
  175. */
  176. public int getAllocIPD() {
  177. return getBorderAndPaddingWidthStart() + getIPD() + getBorderAndPaddingWidthEnd();
  178. }
  179. /**
  180. * Get the allocation block progression dimension of this area.
  181. * This adds the content, borders, padding and spaces to find the
  182. * total allocated BPD.
  183. *
  184. * @return the total BPD allocation for this area
  185. */
  186. public int getAllocBPD() {
  187. return getSpaceBefore() + getBorderAndPaddingWidthBefore() + getBPD()
  188. + getBorderAndPaddingWidthAfter() + getSpaceAfter();
  189. }
  190. /**
  191. * Return the sum of region border- and padding-before
  192. *
  193. * @return width in millipoints
  194. */
  195. public int getBorderAndPaddingWidthBefore() {
  196. int margin = 0;
  197. BorderProps bps = (BorderProps) getTrait(Trait.BORDER_BEFORE);
  198. if (bps != null) {
  199. margin = bps.width;
  200. }
  201. Integer padWidth = (Integer) getTrait(Trait.PADDING_BEFORE);
  202. if (padWidth != null) {
  203. margin += padWidth.intValue();
  204. }
  205. return margin;
  206. }
  207. /**
  208. * Return the sum of region border- and padding-after
  209. *
  210. * @return width in millipoints
  211. */
  212. public int getBorderAndPaddingWidthAfter() {
  213. int margin = 0;
  214. BorderProps bps = (BorderProps) getTrait(Trait.BORDER_AFTER);
  215. if (bps != null) {
  216. margin = bps.width;
  217. }
  218. Integer padWidth = (Integer) getTrait(Trait.PADDING_AFTER);
  219. if (padWidth != null) {
  220. margin += padWidth.intValue();
  221. }
  222. return margin;
  223. }
  224. /**
  225. * Return the sum of region border- and padding-start
  226. *
  227. * @return width in millipoints
  228. */
  229. public int getBorderAndPaddingWidthStart() {
  230. int margin = 0;
  231. BorderProps bps = (BorderProps) getTrait(Trait.BORDER_START);
  232. if (bps != null) {
  233. margin = bps.width;
  234. }
  235. Integer padWidth = (Integer) getTrait(Trait.PADDING_START);
  236. if (padWidth != null) {
  237. margin += padWidth.intValue();
  238. }
  239. return margin;
  240. }
  241. /**
  242. * Return the sum of region border- and padding-end
  243. *
  244. * @return width in millipoints
  245. */
  246. public int getBorderAndPaddingWidthEnd() {
  247. int margin = 0;
  248. BorderProps bps = (BorderProps) getTrait(Trait.BORDER_END);
  249. if (bps != null) {
  250. margin = bps.width;
  251. }
  252. Integer padWidth = (Integer) getTrait(Trait.PADDING_END);
  253. if (padWidth != null) {
  254. margin += padWidth.intValue();
  255. }
  256. return margin;
  257. }
  258. /**
  259. * Returns the space before
  260. *
  261. * @return width in millipoints
  262. */
  263. public int getSpaceBefore() {
  264. int margin = 0;
  265. Integer space = (Integer) getTrait(Trait.SPACE_BEFORE);
  266. if (space != null) {
  267. margin = space.intValue();
  268. }
  269. return margin;
  270. }
  271. /**
  272. * Returns the space after
  273. *
  274. * @return width in millipoints
  275. */
  276. public int getSpaceAfter() {
  277. int margin = 0;
  278. Integer space = (Integer) getTrait(Trait.SPACE_AFTER);
  279. if (space != null) {
  280. margin = space.intValue();
  281. }
  282. return margin;
  283. }
  284. /**
  285. * Returns the space start
  286. *
  287. * @return width in millipoints
  288. */
  289. public int getSpaceStart() {
  290. int margin = 0;
  291. Integer space = (Integer) getTrait(Trait.SPACE_START);
  292. if (space != null) {
  293. margin = space.intValue();
  294. }
  295. return margin;
  296. }
  297. /**
  298. * Returns the space end
  299. *
  300. * @return width in millipoints
  301. */
  302. public int getSpaceEnd() {
  303. int margin = 0;
  304. Integer space = (Integer) getTrait(Trait.SPACE_END);
  305. if (space != null) {
  306. margin = space.intValue();
  307. }
  308. return margin;
  309. }
  310. /**
  311. * Add a child to this area.
  312. * The default is to do nothing. Subclasses must override
  313. * to do something if they can have child areas.
  314. *
  315. * @param child the child area to add
  316. */
  317. public void addChildArea(Area child) {
  318. }
  319. /**
  320. * Add a trait to this area.
  321. *
  322. * @param traitCode the trait key
  323. * @param prop the value of the trait
  324. */
  325. public void addTrait(Object traitCode, Object prop) {
  326. if (props == null) {
  327. props = new java.util.HashMap(20);
  328. }
  329. props.put(traitCode, prop);
  330. }
  331. /**
  332. * Get the map of all traits on this area.
  333. *
  334. * @return the map of traits
  335. */
  336. public Map getTraits() {
  337. return this.props;
  338. }
  339. /** @return true if the area has traits */
  340. public boolean hasTraits() {
  341. return (this.props != null);
  342. }
  343. /**
  344. * Get a trait from this area.
  345. *
  346. * @param oTraitCode the trait key
  347. * @return the trait value
  348. */
  349. public Object getTrait(Object oTraitCode) {
  350. return (props != null ? props.get(oTraitCode) : null);
  351. }
  352. /**
  353. * Checks whether a certain trait is set on this area.
  354. * @param oTraitCode the trait key
  355. * @return true if the trait is set
  356. */
  357. public boolean hasTrait(Object oTraitCode) {
  358. return (getTrait(oTraitCode) != null);
  359. }
  360. /**
  361. * Get a boolean trait from this area.
  362. * @param oTraitCode the trait key
  363. * @return the trait value
  364. */
  365. public boolean getTraitAsBoolean(Object oTraitCode) {
  366. return Boolean.TRUE.equals(getTrait(oTraitCode));
  367. }
  368. /**
  369. * Get a trait from this area as an integer.
  370. *
  371. * @param oTraitCode the trait key
  372. * @return the trait value
  373. */
  374. public int getTraitAsInteger(Object oTraitCode) {
  375. final Object obj = getTrait(oTraitCode);
  376. if (obj instanceof Integer) {
  377. return ((Integer)obj).intValue();
  378. } else {
  379. throw new IllegalArgumentException("Trait "
  380. + oTraitCode.getClass().getName()
  381. + " could not be converted to an integer");
  382. }
  383. }
  384. /**
  385. * {@inheritDoc}
  386. * @return ipd and bpd of area
  387. * */
  388. public String toString() {
  389. StringBuffer sb = new StringBuffer(super.toString());
  390. sb.append(" {ipd=").append(Integer.toString(getIPD()));
  391. sb.append(", bpd=").append(Integer.toString(getBPD()));
  392. sb.append("}");
  393. return sb.toString();
  394. }
  395. }