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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 java.text.DecimalFormat;
  20. import java.text.DecimalFormatSymbols;
  21. import java.util.Locale;
  22. /**
  23. * This class represents a simple number object. It also contains contains some
  24. * utility methods for outputing numbers to PDF.
  25. */
  26. public class PDFNumber extends PDFObject {
  27. private Number number;
  28. /**
  29. * Returns the number.
  30. * @return the number
  31. */
  32. public Number getNumber() {
  33. return this.number;
  34. }
  35. /**
  36. * Sets the number.
  37. * @param number the number
  38. */
  39. public void setNumber(Number number) {
  40. this.number = number;
  41. }
  42. /**
  43. * Output a Double value to a string suitable for PDF.
  44. *
  45. * @param doubleDown the Double value
  46. * @return the value as a string
  47. */
  48. public static String doubleOut(Double doubleDown) {
  49. return doubleOut(doubleDown.doubleValue());
  50. }
  51. /**
  52. * Output a double value to a string suitable for PDF (6 decimal digits).
  53. *
  54. * @param doubleDown the double value
  55. * @return the value as a string
  56. */
  57. public static String doubleOut(double doubleDown) {
  58. return doubleOut(doubleDown, 6);
  59. }
  60. private static final String BASE_FORMAT = "0.################";
  61. private static class DecimalFormatThreadLocal extends ThreadLocal {
  62. private int dec;
  63. public DecimalFormatThreadLocal(int dec) {
  64. this.dec = dec;
  65. }
  66. protected synchronized Object initialValue() {
  67. String s = "0";
  68. if (dec > 0) {
  69. s = BASE_FORMAT.substring(0, dec + 2);
  70. }
  71. DecimalFormat df = new DecimalFormat(s, new DecimalFormatSymbols(Locale.US));
  72. return df;
  73. }
  74. };
  75. //DecimalFormat is not thread-safe!
  76. private static final ThreadLocal[] DECIMAL_FORMAT_CACHE = new DecimalFormatThreadLocal[17];
  77. static {
  78. for (int i = 0, c = DECIMAL_FORMAT_CACHE.length; i < c; i++) {
  79. DECIMAL_FORMAT_CACHE[i] = new DecimalFormatThreadLocal(i);
  80. }
  81. }
  82. /**
  83. * Output a double value to a string suitable for PDF.
  84. * In this method it is possible to set the maximum
  85. * number of decimal places to output.
  86. *
  87. * @param doubleDown the Double value
  88. * @param dec the number of decimal places to output
  89. * @return the value as a string
  90. */
  91. public static String doubleOut(double doubleDown, int dec) {
  92. if (dec < 0 || dec >= DECIMAL_FORMAT_CACHE.length) {
  93. throw new IllegalArgumentException("Parameter dec must be between 1 and "
  94. + (DECIMAL_FORMAT_CACHE.length + 1));
  95. }
  96. DecimalFormat df = (DecimalFormat)DECIMAL_FORMAT_CACHE[dec].get();
  97. return df.format(doubleDown);
  98. }
  99. /** {@inheritDoc} */
  100. protected String toPDFString() {
  101. if (getNumber() == null) {
  102. throw new IllegalArgumentException(
  103. "The number of this PDFNumber must not be empty");
  104. }
  105. StringBuffer sb = new StringBuffer(64);
  106. if (hasObjectNumber()) {
  107. sb.append(getObjectID());
  108. }
  109. sb.append(getNumber().toString());
  110. if (hasObjectNumber()) {
  111. sb.append("\nendobj\n");
  112. }
  113. return sb.toString();
  114. }
  115. }