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.

PDFNumber.java 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * Copyright 1999-2004 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /* $Id$ */
  17. package org.apache.fop.pdf;
  18. /**
  19. * This class represents a simple number object. It also contains contains some
  20. * utility methods for outputing numbers to PDF.
  21. */
  22. public class PDFNumber extends PDFObject {
  23. private Number number;
  24. /**
  25. * Returns the number.
  26. * @return the number
  27. */
  28. public Number getNumber() {
  29. return this.number;
  30. }
  31. /**
  32. * Sets the number.
  33. * @param number the number
  34. */
  35. public void setNumber(Number number) {
  36. this.number = number;
  37. }
  38. /**
  39. * Output a Double value to a string suitable for PDF.
  40. *
  41. * @param doubleDown the Double value
  42. * @return the value as a string
  43. */
  44. public static String doubleOut(Double doubleDown) {
  45. return doubleOut(doubleDown.doubleValue());
  46. }
  47. /**
  48. * Output a double value to a string suitable for PDF.
  49. *
  50. * @param doubleDown the double value
  51. * @return the value as a string
  52. */
  53. public static String doubleOut(double doubleDown) {
  54. StringBuffer p = new StringBuffer();
  55. if (doubleDown < 0) {
  56. doubleDown = -doubleDown;
  57. p.append("-");
  58. }
  59. double trouble = doubleDown % 1;
  60. if (trouble > 0.950) {
  61. p.append((int)doubleDown + 1);
  62. } else if (trouble < 0.050) {
  63. p.append((int)doubleDown);
  64. } else {
  65. String doubleString = new String(doubleDown + "");
  66. int decimal = doubleString.indexOf(".");
  67. if (decimal != -1) {
  68. p.append(doubleString.substring(0, decimal));
  69. if ((doubleString.length() - decimal) > 6) {
  70. p.append(doubleString.substring(decimal, decimal + 6));
  71. } else {
  72. p.append(doubleString.substring(decimal));
  73. }
  74. } else {
  75. p.append(doubleString);
  76. }
  77. }
  78. return (p.toString());
  79. }
  80. /**
  81. * Output a double value to a string suitable for PDF.
  82. * In this method it is possible to set the maximum
  83. * number of decimal places to output.
  84. *
  85. * @param doubleDown the Double value
  86. * @param dec the number of decimal places to output
  87. * @return the value as a string
  88. */
  89. public static String doubleOut(double doubleDown, int dec) {
  90. StringBuffer p = new StringBuffer();
  91. if (doubleDown < 0) {
  92. doubleDown = -doubleDown;
  93. p.append("-");
  94. }
  95. double trouble = doubleDown % 1;
  96. if (trouble > (1.0 - (5.0 / (Math.pow(10.0, dec))))) {
  97. p.append((int)doubleDown + 1);
  98. } else if (trouble < (5.0 / (Math.pow(10.0, dec)))) {
  99. p.append((int)doubleDown);
  100. } else {
  101. String doubleString = new String(doubleDown + "");
  102. int decimal = doubleString.indexOf(".");
  103. if (decimal != -1) {
  104. p.append(doubleString.substring(0, decimal));
  105. if ((doubleString.length() - decimal) > dec) {
  106. p.append(doubleString.substring(decimal, decimal + dec));
  107. } else {
  108. p.append(doubleString.substring(decimal));
  109. }
  110. } else {
  111. p.append(doubleString);
  112. }
  113. }
  114. return (p.toString());
  115. }
  116. /**
  117. * @see org.apache.fop.pdf.PDFObject#toPDFString()
  118. */
  119. protected String toPDFString() {
  120. if (getNumber() == null) {
  121. throw new IllegalArgumentException(
  122. "The number of this PDFNumber must not be empty");
  123. }
  124. StringBuffer sb = new StringBuffer(64);
  125. sb.append(getObjectID());
  126. sb.append(getNumber().toString());
  127. sb.append("\nendobj\n");
  128. return sb.toString();
  129. }
  130. }