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.

AbstractTextArea.java 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. /**
  20. * Abstract base class for both TextArea and Character.
  21. */
  22. public abstract class AbstractTextArea extends InlineParent {
  23. private static final long serialVersionUID = -1246306443569094371L;
  24. /**
  25. * this class stores information about spaces and potential adjustments
  26. * that can be used in order to re-compute adjustments when a
  27. * page-number or a page-number-citation is resolved
  28. */
  29. protected class TextAdjustingInfo extends InlineAdjustingInfo {
  30. private static final long serialVersionUID = -2412095162983479947L;
  31. /** difference between the optimal width of a space
  32. * and the default width of a space according to the font
  33. * (this is equivalent to the property word-spacing.optimum)
  34. */
  35. protected int spaceDifference;
  36. /**
  37. * Constructor
  38. *
  39. * @param stretch the available space for stretching
  40. * @param shrink the available space for shrinking
  41. * @param adj space adjustment type
  42. */
  43. protected TextAdjustingInfo(int stretch, int shrink, int adj) {
  44. super(stretch, shrink, adj);
  45. }
  46. }
  47. private int textWordSpaceAdjust;
  48. private int textLetterSpaceAdjust;
  49. private TextAdjustingInfo textAdjustingInfo;
  50. private int baselineOffset;
  51. /**
  52. * Default constructor
  53. */
  54. public AbstractTextArea() {
  55. }
  56. /**
  57. * Constructor with extra parameters:
  58. * create a TextAdjustingInfo object
  59. * @param stretch the available stretch of the text
  60. * @param shrink the available shrink of the text
  61. * @param adj the current adjustment of the area
  62. */
  63. public AbstractTextArea(int stretch, int shrink, int adj) {
  64. textAdjustingInfo = new TextAdjustingInfo(stretch, shrink, adj);
  65. }
  66. /**
  67. * Get text word space adjust.
  68. *
  69. * @return the text word space adjustment
  70. */
  71. public int getTextWordSpaceAdjust() {
  72. return textWordSpaceAdjust;
  73. }
  74. /**
  75. * Set text word space adjust.
  76. *
  77. * @param textWordSpaceAdjust the text word space adjustment
  78. */
  79. public void setTextWordSpaceAdjust(int textWordSpaceAdjust) {
  80. this.textWordSpaceAdjust = textWordSpaceAdjust;
  81. }
  82. /**
  83. * Get text letter space adjust.
  84. *
  85. * @return the text letter space adjustment
  86. */
  87. public int getTextLetterSpaceAdjust() {
  88. return textLetterSpaceAdjust;
  89. }
  90. /**
  91. * Set text letter space adjust.
  92. *
  93. * @param textLetterSpaceAdjust the text letter space adjustment
  94. */
  95. public void setTextLetterSpaceAdjust(int textLetterSpaceAdjust) {
  96. this.textLetterSpaceAdjust = textLetterSpaceAdjust;
  97. }
  98. /**
  99. * Set the difference between optimal width of a space and
  100. * default width of a space according to the font; this part
  101. * of the space adjustment is fixed and must not be
  102. * multiplied by the variation factor.
  103. * @param spaceDiff the space difference
  104. */
  105. public void setSpaceDifference(int spaceDiff) {
  106. textAdjustingInfo.spaceDifference = spaceDiff;
  107. }
  108. /**
  109. * recursively apply the variation factor to all descendant areas
  110. * @param variationFactor the variation factor that must be applied to adjustments
  111. * @param lineStretch the total stretch of the line
  112. * @param lineShrink the total shrink of the line
  113. * @return true if there is an UnresolvedArea descendant
  114. */
  115. public boolean applyVariationFactor(double variationFactor,
  116. int lineStretch, int lineShrink) {
  117. if (textAdjustingInfo != null) {
  118. // compute the new adjustments:
  119. // if the variation factor is negative, it means that before
  120. // the ipd variation the line had to stretch and now it has
  121. // to shrink (or vice versa);
  122. // in this case, if the stretch and shrink are not equally
  123. // divided among the inline areas, we must compute a
  124. // balancing factor
  125. double balancingFactor = 1.0;
  126. if (variationFactor < 0) {
  127. if (textWordSpaceAdjust < 0) {
  128. // from a negative adjustment to a positive one
  129. balancingFactor
  130. = ((double) textAdjustingInfo.availableStretch
  131. / textAdjustingInfo.availableShrink)
  132. * ((double) lineShrink / lineStretch);
  133. } else {
  134. // from a positive adjustment to a negative one
  135. balancingFactor
  136. = ((double) textAdjustingInfo.availableShrink
  137. / textAdjustingInfo.availableStretch)
  138. * ((double) lineStretch / lineShrink);
  139. }
  140. }
  141. textWordSpaceAdjust = (int) ((textWordSpaceAdjust - textAdjustingInfo.spaceDifference)
  142. * variationFactor * balancingFactor)
  143. + textAdjustingInfo.spaceDifference;
  144. textLetterSpaceAdjust *= variationFactor;
  145. // update the ipd of the area
  146. int oldAdjustment = textAdjustingInfo.adjustment;
  147. textAdjustingInfo.adjustment *= balancingFactor * variationFactor;
  148. ipd += textAdjustingInfo.adjustment - oldAdjustment;
  149. }
  150. return false;
  151. }
  152. /**
  153. * Get baseline offset, i.e. the distance from the before edge
  154. * of this area to the nominal baseline.
  155. *
  156. * @return the baseline offset
  157. */
  158. public int getBaselineOffset() {
  159. return baselineOffset;
  160. }
  161. /**
  162. * Set the baseline offset.
  163. *
  164. * @param baselineOffset the baseline offset
  165. */
  166. public void setBaselineOffset(int baselineOffset) {
  167. this.baselineOffset = baselineOffset;
  168. }
  169. @Override
  170. int getVirtualOffset() {
  171. return getBlockProgressionOffset();
  172. }
  173. @Override
  174. int getVirtualBPD() {
  175. /* Word and space areas don't have a properly set bpd; return this area's bpd instead. */
  176. return getBPD();
  177. }
  178. }