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

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