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.

LengthBase.java 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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.datatypes;
  19. import org.apache.commons.logging.Log;
  20. import org.apache.commons.logging.LogFactory;
  21. import org.apache.fop.fo.Constants;
  22. import org.apache.fop.fo.FObj;
  23. import org.apache.fop.fo.PropertyList;
  24. import org.apache.fop.fo.expr.PropertyException;
  25. import org.apache.fop.util.CompareUtil;
  26. /**
  27. * Models a length which can be used as a factor in a percentage length
  28. * calculation
  29. */
  30. public class LengthBase implements PercentBase {
  31. // Standard kinds of percent-based length
  32. /** constant for a custom percent-based length */
  33. public static final int CUSTOM_BASE = 0;
  34. /** constant for a font-size percent-based length */
  35. public static final int FONTSIZE = 1;
  36. /** constant for an inh font-size percent-based length */
  37. public static final int INH_FONTSIZE = 2;
  38. /** constant for a containing box percent-based length */
  39. public static final int PARENT_AREA_WIDTH = 3;
  40. /** constant for a containing refarea percent-based length */
  41. public static final int CONTAINING_REFAREA_WIDTH = 4;
  42. /** constant for a containing block percent-based length */
  43. public static final int CONTAINING_BLOCK_WIDTH = 5;
  44. /** constant for a containing block percent-based length */
  45. public static final int CONTAINING_BLOCK_HEIGHT = 6;
  46. /** constant for a image intrinsic percent-based length */
  47. public static final int IMAGE_INTRINSIC_WIDTH = 7;
  48. /** constant for a image intrinsic percent-based length */
  49. public static final int IMAGE_INTRINSIC_HEIGHT = 8;
  50. /** constant for a image background position horizontal percent-based length */
  51. public static final int IMAGE_BACKGROUND_POSITION_HORIZONTAL = 9;
  52. /** constant for a image background position vertical percent-based length */
  53. public static final int IMAGE_BACKGROUND_POSITION_VERTICAL = 10;
  54. /** constant for a table-unit-based length */
  55. public static final int TABLE_UNITS = 11;
  56. /** constant for a alignment adjust percent-based length */
  57. public static final int ALIGNMENT_ADJUST = 12;
  58. /**
  59. * logging instance
  60. */
  61. protected static final Log log = LogFactory.getLog(LengthBase.class);
  62. /**
  63. * The FO for which this property is to be calculated.
  64. */
  65. protected /* final */ FObj fobj;
  66. /**
  67. * One of the defined types of LengthBase
  68. */
  69. private /* final */ int baseType;
  70. /** For percentages based on other length properties */
  71. private Length baseLength;
  72. /**
  73. * Constructor
  74. * @param plist property list for this
  75. * @param baseType a constant defining the type of teh percent base
  76. * @throws PropertyException In case an problem occurs while evaluating values
  77. */
  78. public LengthBase(PropertyList plist, int baseType) throws PropertyException {
  79. this.fobj = plist.getFObj();
  80. this.baseType = baseType;
  81. switch (baseType) {
  82. case FONTSIZE:
  83. this.baseLength = plist.get(Constants.PR_FONT_SIZE).getLength();
  84. break;
  85. case INH_FONTSIZE:
  86. this.baseLength = plist.getInherited(Constants.PR_FONT_SIZE).getLength();
  87. break;
  88. default:
  89. // TODO: pacify CheckStyle
  90. // throw new RuntimeException();
  91. break;
  92. }
  93. }
  94. /**
  95. * @return the dimension of this object (always 1)
  96. */
  97. public int getDimension() {
  98. return 1;
  99. }
  100. /**
  101. * @return the base value of this object (always 1.0)
  102. */
  103. public double getBaseValue() {
  104. return 1.0;
  105. }
  106. /**
  107. * {@inheritDoc}
  108. */
  109. public int getBaseLength(PercentBaseContext context) throws PropertyException {
  110. int baseLen = 0;
  111. if (context != null) {
  112. if (baseType == FONTSIZE || baseType == INH_FONTSIZE) {
  113. return baseLength.getValue(context);
  114. }
  115. baseLen = context.getBaseLength(baseType, fobj);
  116. } else {
  117. log.error("getBaseLength called without context");
  118. }
  119. return baseLen;
  120. }
  121. /** {@inheritDoc} */
  122. public String toString() {
  123. return super.toString()
  124. + "[fo=" + fobj + ","
  125. + "baseType=" + baseType + ","
  126. + "baseLength=" + baseLength + "]";
  127. }
  128. /**@return the base length as a {@link Length} */
  129. public Length getBaseLength() {
  130. return baseLength;
  131. }
  132. @Override
  133. public int hashCode() {
  134. final int prime = 31;
  135. int result = 1;
  136. result = prime * result + CompareUtil.getHashCode(baseLength);
  137. result = prime * result + baseType;
  138. result = prime * result + CompareUtil.getHashCode(fobj);
  139. return result;
  140. }
  141. @Override
  142. public boolean equals(Object obj) {
  143. if (this == obj) {
  144. return true;
  145. }
  146. if (!(obj instanceof LengthBase)) {
  147. return false;
  148. }
  149. LengthBase other = (LengthBase) obj;
  150. return CompareUtil.equal(baseLength, other.baseLength)
  151. && baseType == other.baseType
  152. && CompareUtil.equal(fobj, other.fobj);
  153. }
  154. }