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.

FixedLength.java 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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.fo.properties;
  19. import org.apache.fop.datatypes.PercentBaseContext;
  20. /**
  21. * An absolute length quantity in XSL
  22. */
  23. public final class FixedLength extends LengthProperty {
  24. /** Describes the unit pica. */
  25. public static final String PICA = "pc";
  26. /** Describes the unit point. */
  27. public static final String POINT = "pt";
  28. /** Describes the unit millimeter. */
  29. public static final String MM = "mm";
  30. /** Describes the unit centimeter. */
  31. public static final String CM = "cm";
  32. /** Describes the unit inch. */
  33. public static final String INCH = "in";
  34. /** Describes the unit millipoint. */
  35. public static final String MPT = "mpt";
  36. /** cache holding all canonical FixedLength instances */
  37. private static final PropertyCache<FixedLength> CACHE = new PropertyCache<FixedLength>();
  38. /** canonical zero-length instance */
  39. public static final FixedLength ZERO_FIXED_LENGTH = new FixedLength(0, FixedLength.MPT, 1.0f);
  40. private int millipoints;
  41. /**
  42. * Set the length given a number of units, a unit name and
  43. * an assumed resolution (used in case the units are pixels)
  44. *
  45. * @param numUnits quantity of input units
  46. * @param units input unit specifier
  47. * @param res input/source resolution
  48. */
  49. private FixedLength(double numUnits, String units, float res) {
  50. this.millipoints = convert(numUnits, units, res);
  51. }
  52. /**
  53. * Return the cached {@link FixedLength} instance corresponding
  54. * to the computed value in base-units (millipoints).
  55. *
  56. * @param numUnits quantity of input units
  57. * @param units input unit specifier
  58. * @param sourceResolution input/source resolution (= ratio of pixels per pt)
  59. * @return the canonical FixedLength instance corresponding
  60. * to the given number of units and unit specifier
  61. * in the given resolution
  62. */
  63. public static FixedLength getInstance(double numUnits,
  64. String units,
  65. float sourceResolution) {
  66. if (numUnits == 0.0) {
  67. return ZERO_FIXED_LENGTH;
  68. } else {
  69. return CACHE.fetch(new FixedLength(numUnits, units, sourceResolution));
  70. }
  71. }
  72. /**
  73. * Return the cached {@link FixedLength} instance corresponding
  74. * to the computed value
  75. * This method assumes a source-resolution of 1 (1px = 1pt)
  76. *
  77. * @param numUnits input units
  78. * @param units unit specifier
  79. * @return the canonical FixedLength instance corresponding
  80. * to the given number of units and unit specifier
  81. */
  82. public static FixedLength getInstance(double numUnits,
  83. String units) {
  84. return getInstance(numUnits, units, 1.0f);
  85. }
  86. /**
  87. * Return the cached {@link FixedLength} instance corresponding
  88. * to the computed value.
  89. * This method assumes 'millipoints' (non-standard) as units,
  90. * and an implied source-resolution of 1 (1px = 1pt).
  91. *
  92. * @param numUnits input units
  93. * @return the canonical FixedLength instance corresponding
  94. * to the given number of units and unit specifier
  95. */
  96. public static FixedLength getInstance(double numUnits) {
  97. return getInstance(numUnits, FixedLength.MPT, 1.0f);
  98. }
  99. /**
  100. * Convert the given length to a dimensionless integer representing
  101. * a whole number of base units (milli-points).
  102. *
  103. * @param dvalue quantity of input units
  104. * @param unit input unit specifier (in, cm, etc.)
  105. * @param res the input/source resolution (in case the unit spec is "px")
  106. */
  107. private static int convert(double dvalue, String unit, float res) {
  108. // TODO: Maybe this method has a better place in org.apache.fop.util.UnitConv?.
  109. if ("px".equals(unit)) {
  110. //device-dependent units, take the resolution into account
  111. dvalue *= (res * 1000);
  112. } else {
  113. if (FixedLength.INCH.equals(unit)) {
  114. dvalue *= 72000;
  115. } else if (FixedLength.CM.equals(unit)) {
  116. dvalue *= 28346.4567;
  117. } else if (FixedLength.MM.equals(unit)) {
  118. dvalue *= 2834.64567;
  119. } else if (FixedLength.POINT.equals(unit)) {
  120. dvalue *= 1000;
  121. } else if (FixedLength.PICA.equals(unit)) {
  122. dvalue *= 12000;
  123. } else if (!FixedLength.MPT.equals(unit)) {
  124. dvalue = 0;
  125. log.error("Unknown length unit '" + unit + "'");
  126. }
  127. }
  128. return (int)dvalue;
  129. }
  130. /** {@inheritDoc} */
  131. public int getValue() {
  132. return millipoints;
  133. }
  134. /** {@inheritDoc} */
  135. public int getValue(PercentBaseContext context) {
  136. return millipoints;
  137. }
  138. /** {@inheritDoc} */
  139. public double getNumericValue() {
  140. return millipoints;
  141. }
  142. /** {@inheritDoc} */
  143. public double getNumericValue(PercentBaseContext context) {
  144. return millipoints;
  145. }
  146. /**
  147. * Return true since a FixedLength is always absolute.
  148. * {@inheritDoc}
  149. */
  150. public boolean isAbsolute() {
  151. return true;
  152. }
  153. /** {@inheritDoc} */
  154. public String toString() {
  155. return millipoints + FixedLength.MPT;
  156. }
  157. /** {@inheritDoc} */
  158. public boolean equals(Object obj) {
  159. if (this == obj) {
  160. return true;
  161. }
  162. if (obj instanceof FixedLength) {
  163. return (((FixedLength)obj).millipoints == this.millipoints);
  164. }
  165. return false;
  166. }
  167. /** {@inheritDoc} */
  168. public int hashCode() {
  169. return millipoints;
  170. }
  171. }