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.5KB

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