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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  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. private boolean fromFootnote;
  110. /**
  111. * Returns the active change bar list.
  112. *
  113. * @return The active change bar list
  114. */
  115. public List<ChangeBar> getChangeBarList() {
  116. return changeBarList;
  117. }
  118. /**
  119. * Sets the active change bar list.
  120. *
  121. * @param changeBarList The active change bar list
  122. */
  123. public void setChangeBarList(List<ChangeBar> changeBarList) {
  124. this.changeBarList = changeBarList;
  125. }
  126. /**
  127. * Get the area class of this area.
  128. *
  129. * @return the area class
  130. */
  131. public int getAreaClass() {
  132. return this.areaClass;
  133. }
  134. /** {@inheritDoc} */
  135. public Object clone() throws CloneNotSupportedException {
  136. Area area = (Area) super.clone();
  137. if (traits != null) {
  138. area.traits = (TreeMap<Integer, Object>) traits.clone();
  139. }
  140. return area;
  141. }
  142. /**
  143. * Set the area class of this area.
  144. *
  145. * @param areaClass the area class
  146. */
  147. public void setAreaClass(int areaClass) {
  148. this.areaClass = areaClass;
  149. }
  150. /**
  151. * Set the inline progression dimension of content rectangle
  152. * for this area.
  153. *
  154. * @param ipd the new inline progression dimension
  155. * @see <a href="http://www.w3.org/TR/xsl/#inline-progression-dimension">ipd</a>
  156. */
  157. public void setIPD(int ipd) {
  158. this.ipd = ipd;
  159. }
  160. /**
  161. * Get the inline progression dimension of the content rectangle
  162. * for this area.
  163. *
  164. * @return the inline progression dimension
  165. * @see <a href="http://www.w3.org/TR/xsl/#inline-progression-dimension">ipd</a>
  166. */
  167. public int getIPD() {
  168. return ipd;
  169. }
  170. /**
  171. * Set the block progression dimension of the content rectangle
  172. * for this area.
  173. *
  174. * @param bpd the new block progression dimension
  175. * @see <a href="http://www.w3.org/TR/xsl/#block-progression-dimension">bpd</a>
  176. */
  177. public void setBPD(int bpd) {
  178. this.bpd = bpd;
  179. }
  180. /**
  181. * Get the block progression dimension of the content rectangle
  182. * for this area.
  183. *
  184. * @return the block progression dimension
  185. * @see <a href="http://www.w3.org/TR/xsl/#block-progression-dimension">bpd</a>
  186. */
  187. public int getBPD() {
  188. return bpd;
  189. }
  190. /**
  191. * Get the allocation inline progression dimension of this area.
  192. * This adds the content, borders and the padding to find the
  193. * total allocated IPD.
  194. *
  195. * @return the total IPD allocation for this area
  196. */
  197. public int getAllocIPD() {
  198. return getBorderAndPaddingWidthStart() + getIPD() + getBorderAndPaddingWidthEnd();
  199. }
  200. public int getEffectiveAllocIPD() {
  201. return getBorderAndPaddingWidthStart() + getEffectiveIPD() + getBorderAndPaddingWidthEnd();
  202. }
  203. /**
  204. * Get the allocation block progression dimension of this area.
  205. * This adds the content, borders, padding and spaces to find the
  206. * total allocated BPD.
  207. *
  208. * @return the total BPD allocation for this area
  209. */
  210. public int getAllocBPD() {
  211. return getSpaceBefore() + getBorderAndPaddingWidthBefore() + getBPD()
  212. + getBorderAndPaddingWidthAfter() + getSpaceAfter();
  213. }
  214. /**
  215. * Set the bidirectional embedding level.
  216. *
  217. * @param bidiLevel the bidirectional embedding level
  218. */
  219. public void setBidiLevel(int bidiLevel) {
  220. this.bidiLevel = bidiLevel;
  221. }
  222. /**
  223. * Reset the bidirectional embedding level to default
  224. * value (-1).
  225. */
  226. public void resetBidiLevel() {
  227. setBidiLevel(-1);
  228. }
  229. /**
  230. * Get the bidirectional embedding level.
  231. *
  232. * @return the bidirectional embedding level
  233. */
  234. public int getBidiLevel() {
  235. return bidiLevel;
  236. }
  237. /**
  238. * Return the sum of region border- and padding-before
  239. *
  240. * @return width in millipoints
  241. */
  242. public int getBorderAndPaddingWidthBefore() {
  243. int margin = 0;
  244. BorderProps bps = (BorderProps) getTrait(Trait.BORDER_BEFORE);
  245. if (bps != null) {
  246. margin = bps.width;
  247. }
  248. Integer padWidth = (Integer) getTrait(Trait.PADDING_BEFORE);
  249. if (padWidth != null) {
  250. margin += padWidth;
  251. }
  252. return margin;
  253. }
  254. /**
  255. * Return the sum of region border- and padding-after
  256. *
  257. * @return width in millipoints
  258. */
  259. public int getBorderAndPaddingWidthAfter() {
  260. int margin = 0;
  261. BorderProps bps = (BorderProps) getTrait(Trait.BORDER_AFTER);
  262. if (bps != null) {
  263. margin = bps.width;
  264. }
  265. Integer padWidth = (Integer) getTrait(Trait.PADDING_AFTER);
  266. if (padWidth != null) {
  267. margin += padWidth;
  268. }
  269. return margin;
  270. }
  271. /**
  272. * Return the sum of region border- and padding-start
  273. *
  274. * @return width in millipoints
  275. */
  276. public int getBorderAndPaddingWidthStart() {
  277. int margin = 0;
  278. BorderProps bps = (BorderProps) getTrait(Trait.BORDER_START);
  279. if (bps != null) {
  280. margin = bps.width;
  281. }
  282. Integer padWidth = (Integer) getTrait(Trait.PADDING_START);
  283. if (padWidth != null) {
  284. margin += padWidth;
  285. }
  286. return margin;
  287. }
  288. /**
  289. * Return the sum of region border- and padding-end
  290. *
  291. * @return width in millipoints
  292. */
  293. public int getBorderAndPaddingWidthEnd() {
  294. int margin = 0;
  295. BorderProps bps = (BorderProps) getTrait(Trait.BORDER_END);
  296. if (bps != null) {
  297. margin = bps.width;
  298. }
  299. Integer padWidth = (Integer) getTrait(Trait.PADDING_END);
  300. if (padWidth != null) {
  301. margin += padWidth;
  302. }
  303. return margin;
  304. }
  305. /**
  306. * Returns the space before
  307. *
  308. * @return width in millipoints
  309. */
  310. public int getSpaceBefore() {
  311. int margin = 0;
  312. Integer space = (Integer) getTrait(Trait.SPACE_BEFORE);
  313. if (space != null) {
  314. margin = space;
  315. }
  316. return margin;
  317. }
  318. /**
  319. * Returns the space after
  320. *
  321. * @return width in millipoints
  322. */
  323. public int getSpaceAfter() {
  324. int margin = 0;
  325. Integer space = (Integer) getTrait(Trait.SPACE_AFTER);
  326. if (space != null) {
  327. margin = space;
  328. }
  329. return margin;
  330. }
  331. /**
  332. * Returns the space start
  333. *
  334. * @return width in millipoints
  335. */
  336. public int getSpaceStart() {
  337. int margin = 0;
  338. Integer space = (Integer) getTrait(Trait.SPACE_START);
  339. if (space != null) {
  340. margin = space;
  341. }
  342. return margin;
  343. }
  344. /**
  345. * Returns the space end
  346. *
  347. * @return width in millipoints
  348. */
  349. public int getSpaceEnd() {
  350. int margin = 0;
  351. Integer space = (Integer) getTrait(Trait.SPACE_END);
  352. if (space != null) {
  353. margin = space;
  354. }
  355. return margin;
  356. }
  357. /**
  358. * Add a child to this area.
  359. * The default is to do nothing. Subclasses must override
  360. * to do something if they can have child areas.
  361. *
  362. * @param child the child area to add
  363. */
  364. public void addChildArea(Area child) {
  365. }
  366. /**
  367. * Add a trait to this area.
  368. *
  369. * @param traitCode the trait key
  370. * @param prop the value of the trait
  371. */
  372. public void addTrait(Integer traitCode, Object prop) {
  373. // use treemap since the typical number of traits are less than four
  374. if (traits == null) {
  375. traits = new TreeMap<Integer, Object>();
  376. }
  377. traits.put(traitCode, prop);
  378. }
  379. /**
  380. * Set traits on this area, copying from an existing traits map.
  381. *
  382. * @param traits the map of traits
  383. */
  384. public void setTraits(Map traits) {
  385. if (traits != null) {
  386. this.traits = new TreeMap<Integer, Object>(traits);
  387. } else {
  388. this.traits = null;
  389. }
  390. }
  391. /**
  392. * Get the map of all traits on this area.
  393. *
  394. * @return the map of traits
  395. */
  396. public Map<Integer, Object> getTraits() {
  397. return this.traits;
  398. }
  399. /** @return true if the area has traits */
  400. public boolean hasTraits() {
  401. return (this.traits != null);
  402. }
  403. /**
  404. * Get a trait from this area.
  405. *
  406. * @param traitCode the trait key
  407. * @return the trait value
  408. */
  409. public Object getTrait(Integer traitCode) {
  410. return (traits != null ? traits.get(traitCode) : null);
  411. }
  412. /**
  413. * Checks whether a certain trait is set on this area.
  414. * @param traitCode the trait key
  415. * @return true if the trait is set
  416. */
  417. public boolean hasTrait(Integer traitCode) {
  418. return (getTrait(traitCode) != null);
  419. }
  420. /**
  421. * Get a boolean trait from this area.
  422. * @param traitCode the trait key
  423. * @return the trait value
  424. */
  425. public boolean getTraitAsBoolean(Integer traitCode) {
  426. return Boolean.TRUE.equals(getTrait(traitCode));
  427. }
  428. /**
  429. * Get a trait from this area as an integer.
  430. *
  431. * @param traitCode the trait key
  432. * @return the trait value
  433. */
  434. public int getTraitAsInteger(Integer traitCode) {
  435. final Object obj = getTrait(traitCode);
  436. if (obj instanceof Integer) {
  437. return (Integer) obj;
  438. } else {
  439. throw new IllegalArgumentException("Trait "
  440. + traitCode.getClass().getName()
  441. + " could not be converted to an integer");
  442. }
  443. }
  444. /**
  445. * Sets the writing mode traits for this area. Default implementation
  446. * does nothing.
  447. * @param wmtg a WM traits getter
  448. */
  449. public void setWritingModeTraits(WritingModeTraitsGetter wmtg) {
  450. }
  451. /**
  452. * {@inheritDoc}
  453. * @return ipd and bpd of area
  454. */
  455. @Override
  456. public String toString() {
  457. StringBuffer sb = new StringBuffer(super.toString());
  458. sb.append(" {ipd=").append(Integer.toString(getIPD()));
  459. sb.append(", bpd=").append(Integer.toString(getBPD()));
  460. sb.append("}");
  461. return sb.toString();
  462. }
  463. public int getEffectiveIPD() {
  464. return 0;
  465. }
  466. public void activateEffectiveIPD() {
  467. if (effectiveIPD != -1) {
  468. ipd = effectiveIPD;
  469. }
  470. }
  471. public boolean isFromFootnote() {
  472. return fromFootnote;
  473. }
  474. public void setFromFootnote(boolean fromFootnote) {
  475. this.fromFootnote = fromFootnote;
  476. }
  477. }