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.

InlineArea.java 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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.inline;
  19. import java.io.Serializable;
  20. import org.apache.fop.area.Area;
  21. import org.apache.fop.area.LineArea;
  22. import org.apache.fop.area.Trait;
  23. /**
  24. * Inline Area
  25. * This area is for all inline areas that can be placed
  26. * in a line area.
  27. */
  28. public class InlineArea extends Area {
  29. private static final long serialVersionUID = -8940066479810170980L;
  30. /**
  31. * this class stores information about potential adjustments
  32. * that can be used in order to re-compute adjustments when a
  33. * page-number or a page-number-citation is resolved
  34. */
  35. protected class InlineAdjustingInfo implements Serializable {
  36. private static final long serialVersionUID = -5601387735459712149L;
  37. /** stretch of the inline area */
  38. protected int availableStretch;
  39. /** shrink of the inline area */
  40. protected int availableShrink;
  41. /** total adjustment (= ipd - width of fixed elements) */
  42. protected int adjustment;
  43. /**
  44. * Constructor
  45. *
  46. * @param stretch the available space for stretching
  47. * @param shrink the available space for shrinking
  48. * @param adj space adjustment type
  49. */
  50. protected InlineAdjustingInfo(int stretch, int shrink, int adj) {
  51. availableStretch = stretch;
  52. availableShrink = shrink;
  53. adjustment = adj;
  54. }
  55. /**
  56. * Apply the variation factor
  57. *
  58. * @param variationFactor the factor by which the adjustment is to be changed
  59. * @return the IPD increase
  60. */
  61. protected int applyVariationFactor(double variationFactor) {
  62. int oldAdjustment = adjustment;
  63. adjustment *= variationFactor;
  64. return adjustment - oldAdjustment;
  65. }
  66. }
  67. /**
  68. * offset position from before edge of parent area
  69. */
  70. protected int offset = 0;
  71. /**
  72. * parent area
  73. * it is needed in order to recompute adjust ratio and indents
  74. * when a page-number or a page-number-citation is resolved
  75. */
  76. private Area parentArea = null;
  77. /**
  78. * ipd variation of child areas: if this area has not already
  79. * been added and cannot notify its parent area, store the variation
  80. * and wait for the parent area to be set
  81. */
  82. private int storedIPDVariation = 0;
  83. /**
  84. * The adjustment information object
  85. */
  86. protected InlineAdjustingInfo adjustingInfo = null;
  87. /**
  88. * @return the adjustment information object
  89. */
  90. public InlineAdjustingInfo getAdjustingInfo() {
  91. return adjustingInfo;
  92. }
  93. /**
  94. * Create a new adjustment information object
  95. * @param stretch the available space for stretching
  96. * @param shrink the available space for shrinking
  97. * @param adjustment space adjustment type
  98. */
  99. public void setAdjustingInfo(int stretch, int shrink, int adjustment) {
  100. adjustingInfo = new InlineAdjustingInfo(stretch, shrink, adjustment);
  101. }
  102. /**
  103. * Modify the adjustment value in the adjustment information object
  104. * @param adjustment the new adjustment value
  105. */
  106. public void setAdjustment(int adjustment) {
  107. if (adjustingInfo != null) {
  108. adjustingInfo.adjustment = adjustment;
  109. }
  110. }
  111. /**
  112. * Increase the inline progression dimensions of this area.
  113. * This is used for inline parent areas that contain mulitple child areas.
  114. *
  115. * @param ipd the inline progression to increase by
  116. */
  117. public void increaseIPD(int ipd) {
  118. this.ipd += ipd;
  119. }
  120. /**
  121. * Set the offset of this inline area.
  122. * This is used to set the offset of the inline area
  123. * which is relative to the before edge of the parent area.
  124. *
  125. * @param offset the offset
  126. */
  127. public void setOffset(int offset) {
  128. this.offset = offset;
  129. }
  130. /**
  131. * Get the offset of this inline area.
  132. * This returns the offset of the inline area
  133. * which is relative to the before edge of the parent area.
  134. *
  135. * @return the offset
  136. */
  137. public int getOffset() {
  138. return offset;
  139. }
  140. /**
  141. * @param parentArea The parentArea to set.
  142. */
  143. public void setParentArea(Area parentArea) {
  144. this.parentArea = parentArea;
  145. }
  146. /**
  147. * @return Returns the parentArea.
  148. */
  149. public Area getParentArea() {
  150. return parentArea;
  151. }
  152. /**
  153. * Set the parent for the child area.
  154. *
  155. * {@inheritDoc}
  156. */
  157. public void addChildArea(Area childArea) {
  158. super.addChildArea(childArea);
  159. if (childArea instanceof InlineArea) {
  160. ((InlineArea) childArea).setParentArea(this);
  161. }
  162. }
  163. /**
  164. *@return true if the inline area is underlined.
  165. */
  166. public boolean hasUnderline() {
  167. return getTraitAsBoolean(Trait.UNDERLINE);
  168. }
  169. /** @return true if the inline area is overlined. */
  170. public boolean hasOverline() {
  171. return getTraitAsBoolean(Trait.OVERLINE);
  172. }
  173. /** @return true if the inline area has a line through. */
  174. public boolean hasLineThrough() {
  175. return getTraitAsBoolean(Trait.LINETHROUGH);
  176. }
  177. /** @return true if the inline area is blinking. */
  178. public boolean isBlinking() {
  179. return getTraitAsBoolean(Trait.BLINK);
  180. }
  181. /**
  182. * recursively apply the variation factor to all descendant areas
  183. * @param variationFactor the variation factor that must be applied to adjustments
  184. * @param lineStretch the total stretch of the line
  185. * @param lineShrink the total shrink of the line
  186. * @return true if there is an UnresolvedArea descendant
  187. */
  188. public boolean applyVariationFactor(double variationFactor,
  189. int lineStretch, int lineShrink) {
  190. // default behaviour: update the IPD and return false
  191. if (adjustingInfo != null) {
  192. setIPD(getIPD() + adjustingInfo.applyVariationFactor(variationFactor));
  193. }
  194. return false;
  195. }
  196. /**
  197. * Apply IPD variation.
  198. * @param ipdVariation the variation
  199. */
  200. public void handleIPDVariation(int ipdVariation) {
  201. increaseIPD(ipdVariation);
  202. notifyIPDVariation(ipdVariation);
  203. }
  204. /**
  205. * notify the parent area about the ipd variation of this area
  206. * or of a descendant area
  207. * @param ipdVariation the difference between new and old ipd
  208. */
  209. protected void notifyIPDVariation(int ipdVariation) {
  210. if (getParentArea() instanceof InlineArea) {
  211. ((InlineArea) getParentArea()).handleIPDVariation(ipdVariation);
  212. } else if (getParentArea() instanceof LineArea) {
  213. ((LineArea) getParentArea()).handleIPDVariation(ipdVariation);
  214. } else if (getParentArea() == null) {
  215. // parent area not yet set: store the variations
  216. storedIPDVariation += ipdVariation;
  217. }
  218. }
  219. }