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

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