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

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