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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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.pdf;
  19. import org.apache.xmlgraphics.util.DoubleFormatUtil;
  20. /**
  21. * This class represents a simple number object. It also contains contains some
  22. * utility methods for outputting numbers to PDF.
  23. */
  24. public class PDFNumber extends PDFObject {
  25. private Number number;
  26. public PDFNumber() {
  27. this.number = 0;
  28. }
  29. public PDFNumber(Number number) {
  30. this.number = number;
  31. }
  32. /**
  33. * Returns the number.
  34. * @return the number
  35. */
  36. public Number getNumber() {
  37. return this.number;
  38. }
  39. /**
  40. * Sets the number.
  41. * @param number the number
  42. */
  43. public void setNumber(Number number) {
  44. this.number = number;
  45. }
  46. /**
  47. * Output a Double value to a string suitable for PDF.
  48. *
  49. * @param doubleDown the Double value
  50. * @return the value as a string
  51. */
  52. public static String doubleOut(Double doubleDown) {
  53. return doubleOut(doubleDown.doubleValue());
  54. }
  55. /**
  56. * Output a double value to a string suitable for PDF (6 decimal digits).
  57. *
  58. * @param doubleDown the double value
  59. * @return the value as a string
  60. */
  61. public static String doubleOut(double doubleDown) {
  62. return doubleOut(doubleDown, 6);
  63. }
  64. /**
  65. * Output a double value to a string suitable for PDF.
  66. * In this method it is possible to set the maximum
  67. * number of decimal places to output.
  68. *
  69. * @param doubleDown the Double value
  70. * @param dec the number of decimal places to output
  71. * @return the value as a string
  72. */
  73. public static String doubleOut(double doubleDown, int dec) {
  74. if (dec < 0 || dec > 16) {
  75. throw new IllegalArgumentException("Parameter dec must be between 1 and 16");
  76. }
  77. StringBuffer buf = new StringBuffer();
  78. DoubleFormatUtil.formatDouble(doubleDown, dec, dec, buf);
  79. return buf.toString();
  80. }
  81. /**
  82. * Append a double value to a string buffer suitable for PDF.
  83. * In this method it is possible to set the maximum
  84. * number of decimal places to output.
  85. *
  86. * @param doubleDown the Double value
  87. * @param dec the number of decimal places to output
  88. * @param buf the string buffer to which double is formatted (appended)
  89. * @return the string buffer
  90. */
  91. public static StringBuffer doubleOut(double doubleDown, int dec, StringBuffer buf) {
  92. if (dec < 0 || dec > 16) {
  93. throw new IllegalArgumentException("Parameter dec must be between 1 and 16");
  94. }
  95. DoubleFormatUtil.formatDouble(doubleDown, dec, dec, buf);
  96. return buf;
  97. }
  98. /** {@inheritDoc} */
  99. protected String toPDFString() {
  100. if (getNumber() == null) {
  101. throw new IllegalArgumentException(
  102. "The number of this PDFNumber must not be empty");
  103. }
  104. StringBuffer sb = new StringBuffer(64);
  105. sb.append(doubleOut(getNumber().doubleValue(), 10));
  106. return sb.toString();
  107. }
  108. }