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.

SpaceVal.java 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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.traits;
  19. import org.apache.fop.datatypes.PercentBaseContext;
  20. import org.apache.fop.fo.Constants;
  21. import org.apache.fop.fo.properties.Property;
  22. import org.apache.fop.fo.properties.SpaceProperty;
  23. import org.apache.fop.fonts.Font;
  24. /**
  25. * Store a single Space property value in simplified form, with all
  26. * Length values resolved. See section 4.3 in the specs.
  27. */
  28. public class SpaceVal {
  29. private final MinOptMax space;
  30. private final boolean conditional;
  31. private final boolean forcing;
  32. private final int precedence; // Numeric only, if forcing, set to 0
  33. /**
  34. * Constructor for SpaceVal objects based on Space objects.
  35. * @param spaceprop Space object to use
  36. * @param context Percentage evaluation context
  37. */
  38. public SpaceVal(SpaceProperty spaceprop, PercentBaseContext context) {
  39. space = createSpaceProperty(spaceprop, context);
  40. conditional = (spaceprop.getConditionality().getEnum() == Constants.EN_DISCARD);
  41. Property precProp = spaceprop.getPrecedence();
  42. if (precProp.getNumber() != null) {
  43. precedence = precProp.getNumber().intValue();
  44. forcing = false;
  45. } else {
  46. forcing = (precProp.getEnum() == Constants.EN_FORCE);
  47. precedence = 0;
  48. }
  49. }
  50. private static MinOptMax createSpaceProperty(SpaceProperty spaceprop,
  51. PercentBaseContext context) {
  52. int min = spaceprop.getMinimum(context).getLength().getValue(context);
  53. int opt = spaceprop.getOptimum(context).getLength().getValue(context);
  54. int max = spaceprop.getMaximum(context).getLength().getValue(context);
  55. // if a value is not set defaults to zero
  56. if (min > max && max == 0) {
  57. max = opt > min ? opt : min;
  58. }
  59. if (min > opt && opt == 0) {
  60. opt = (min + max) / 2;
  61. }
  62. if (opt > max && max == 0) {
  63. max = opt;
  64. }
  65. return MinOptMax.getInstance(min, opt, max);
  66. }
  67. /**
  68. * Constructor for SpaceVal objects based on the full set of properties.
  69. * @param space space to use
  70. * @param conditional Conditionality value
  71. * @param forcing Forcing value
  72. * @param precedence Precedence value
  73. */
  74. public SpaceVal(MinOptMax space, boolean conditional, boolean forcing, int precedence) {
  75. this.space = space;
  76. this.conditional = conditional;
  77. this.forcing = forcing;
  78. this.precedence = precedence;
  79. }
  80. /**
  81. * @param wordSpacing property
  82. * @param letterSpacing space value
  83. * @param fs font
  84. * @return space value
  85. */
  86. public static SpaceVal makeWordSpacing(Property wordSpacing, SpaceVal letterSpacing, Font fs) {
  87. if (wordSpacing.getEnum() == Constants.EN_NORMAL) {
  88. // give word spaces the possibility to shrink by a third,
  89. // and stretch by a half;
  90. int spaceCharIPD = fs.getCharWidth(' ');
  91. MinOptMax space = MinOptMax.getInstance(-spaceCharIPD / 3, 0, spaceCharIPD / 2);
  92. //TODO Adding 2 letter spaces here is not 100% correct. Spaces don't have letter spacing
  93. return new SpaceVal(space.plus(letterSpacing.getSpace().mult(2)), true, true, 0);
  94. } else {
  95. return new SpaceVal(wordSpacing.getSpace(), null);
  96. }
  97. }
  98. /**
  99. * @param letterSpacing property
  100. * @return space value
  101. */
  102. public static SpaceVal makeLetterSpacing(Property letterSpacing) {
  103. if (letterSpacing.getEnum() == Constants.EN_NORMAL) {
  104. // letter spaces are set to zero (or use different values?)
  105. return new SpaceVal(MinOptMax.ZERO, true, true, 0);
  106. } else {
  107. return new SpaceVal(letterSpacing.getSpace(), null);
  108. }
  109. }
  110. /**
  111. * Returns the Conditionality value.
  112. * @return the Conditionality value
  113. */
  114. public boolean isConditional() {
  115. return conditional;
  116. }
  117. /**
  118. * Returns the Forcing value.
  119. * @return the Forcing value
  120. */
  121. public boolean isForcing() {
  122. return forcing;
  123. }
  124. /**
  125. * Returns the Precedence value.
  126. * @return the Precedence value
  127. */
  128. public int getPrecedence() {
  129. return precedence;
  130. }
  131. /**
  132. * Returns the Space value.
  133. * @return the Space value
  134. */
  135. public MinOptMax getSpace() {
  136. return space;
  137. }
  138. /** {@inheritDoc} */
  139. public String toString() {
  140. return "SpaceVal: " + getSpace().toString();
  141. }
  142. }