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

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