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 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 class FixedLength extends LengthProperty {
  24. /** cache holding all canonical FixedLength instances */
  25. private static final PropertyCache cache = new PropertyCache();
  26. private int millipoints;
  27. /**
  28. * Set the length given a number of units and a unit name.
  29. * @param numUnits quantity of input units
  30. * @param units input unit specifier (in, cm, etc.)
  31. */
  32. private FixedLength(double numUnits, String units) {
  33. convert(numUnits, units);
  34. }
  35. /**
  36. * Return the canonical FixedLength instance corresponding
  37. * to the computed value
  38. * @param numUnits input units
  39. * @param units unit specifier
  40. * @return the canonical FixedLength instance corresponding
  41. * to the given number of units and unit specifier
  42. */
  43. public static FixedLength getInstance(double numUnits, String units) {
  44. return (FixedLength) cache.fetch(
  45. new FixedLength(numUnits, units));
  46. }
  47. /**
  48. * @param baseUnits the length as a number of base units (millipoints)
  49. */
  50. public FixedLength(int baseUnits) {
  51. millipoints = baseUnits;
  52. }
  53. /**
  54. * Convert the given length to a dimensionless integer representing
  55. * a whole number of base units (milli-points).
  56. * @param dvalue quantity of input units
  57. * @param unit input unit specifier (in, cm, etc.)
  58. */
  59. protected void convert(double dvalue, String unit) {
  60. // TODO: the whole routine smells fishy.
  61. int assumedResolution = 1; // points/pixel = 72dpi
  62. if (unit.equals("in")) {
  63. dvalue = dvalue * 72;
  64. } else if (unit.equals("cm")) {
  65. dvalue = dvalue * 28.3464567;
  66. } else if (unit.equals("mm")) {
  67. dvalue = dvalue * 2.83464567;
  68. } else if (unit.equals("pt")) {
  69. // Do nothing.
  70. // dvalue = dvalue;
  71. } else if (unit.equals("mpt")) { //mpt is non-standard!!! mpt=millipoints
  72. // TODO: this seems to be wrong.
  73. // Do nothing.
  74. // dvalue = dvalue;
  75. } else if (unit.equals("pc")) {
  76. dvalue = dvalue * 12;
  77. /*
  78. * } else if (unit.equals("em")) {
  79. * dvalue = dvalue * fontsize;
  80. */
  81. } else if (unit.equals("px")) {
  82. // TODO: get resolution from user agent?
  83. dvalue = dvalue * assumedResolution;
  84. } else {
  85. dvalue = 0;
  86. log.error("Unknown length unit '" + unit + "'");
  87. }
  88. if (unit.equals("mpt")) {
  89. millipoints = (int)dvalue;
  90. } else {
  91. millipoints = (int)(dvalue * 1000);
  92. }
  93. }
  94. /**
  95. * {@inheritDoc}
  96. */
  97. public int getValue() {
  98. return millipoints;
  99. }
  100. /**
  101. * {@inheritDoc}
  102. */
  103. public int getValue(PercentBaseContext context) {
  104. return millipoints;
  105. }
  106. /**
  107. * {@inheritDoc}
  108. */
  109. public double getNumericValue() {
  110. return millipoints;
  111. }
  112. /**
  113. * {@inheritDoc}
  114. */
  115. public double getNumericValue(PercentBaseContext context) {
  116. return millipoints;
  117. }
  118. /**
  119. * Return true since FixedLength are always absolute.
  120. * {@inheritDoc}
  121. */
  122. public boolean isAbsolute() {
  123. return true;
  124. }
  125. /**
  126. * {@inheritDoc}
  127. */
  128. public String toString() {
  129. return millipoints + "mpt";
  130. }
  131. /**
  132. * {@inheritDoc}
  133. */
  134. public boolean equals(Object obj) {
  135. if (obj instanceof FixedLength) {
  136. return (((FixedLength)obj).millipoints == this.millipoints);
  137. } else {
  138. return false;
  139. }
  140. }
  141. /**
  142. * {@inheritDoc}
  143. */
  144. public int hashCode() {
  145. return millipoints;
  146. }
  147. }