| 1 |
/* ===================================================================== |
| 2 |
* JFreePDF : a fast, light-weight PDF library for the Java(tm) platform |
| 3 |
* ===================================================================== |
| 4 |
* |
| 5 |
* (C)opyright 2013-2022, by David Gilbert. All rights reserved. |
| 6 |
* |
| 7 |
* https://github.com/jfree/orsonpdf |
| 8 |
* |
| 9 |
* This program is free software: you can redistribute it and/or modify |
| 10 |
* it under the terms of the GNU General Public License as published by |
| 11 |
* the Free Software Foundation, either version 3 of the License, or |
| 12 |
* (at your option) any later version. |
| 13 |
* |
| 14 |
* This program is distributed in the hope that it will be useful, |
| 15 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 |
* GNU General Public License for more details. |
| 18 |
* |
| 19 |
* You should have received a copy of the GNU General Public License |
| 20 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 21 |
* |
| 22 |
* [Oracle and Java are registered trademarks of Oracle and/or its affiliates. |
| 23 |
* Other names may be trademarks of their respective owners.] |
| 24 |
* |
| 25 |
* If you do not wish to be bound by the terms of the GPL, an alternative |
| 26 |
* runtime license is available to JFree sponsors: |
| 27 |
* |
| 28 |
* https://github.com/sponsors/jfree |
| 29 |
* |
| 30 |
*/ |
| 31 |
|
| 32 |
package org.jfree.pdf.util; |
| 33 |
|
| 34 |
import java.awt.geom.AffineTransform; |
| 35 |
import java.io.UnsupportedEncodingException; |
| 36 |
import java.text.DateFormat; |
| 37 |
import java.text.SimpleDateFormat; |
| 38 |
import java.util.Calendar; |
| 39 |
import java.util.Date; |
| 40 |
|
| 41 |
/** |
| 42 |
* Various utility functions for working with the PDF format. |
| 43 |
*/ |
| 44 |
public class PDFUtils { |
| 45 |
|
| 46 |
private PDFUtils() { |
| 47 |
// no need to instantiate this |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* A utility method to convert a boolean[] to a PDF array string. |
| 52 |
* |
| 53 |
* @param b the array ({@code null} not permitted). |
| 54 |
* |
| 55 |
* @return The string. |
| 56 |
*/ |
| 57 |
public static String toPDFArray(boolean[] b) { |
| 58 |
Args.nullNotPermitted(b, "b"); |
| 59 |
StringBuilder sb = new StringBuilder("["); |
| 60 |
for (int i = 0; i < b.length; i++) { |
| 61 |
if (i != 0) { |
| 62 |
sb.append(" "); |
| 63 |
} |
| 64 |
sb.append(String.valueOf(b[i])); |
| 65 |
} |
| 66 |
return sb.append("]").toString(); |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* A utility method to convert a float[] to a PDF array string. |
| 71 |
* |
| 72 |
* @param f the array ({@code null} not permitted). |
| 73 |
* |
| 74 |
* @return The string. |
| 75 |
*/ |
| 76 |
public static String toPDFArray(float[] f) { |
| 77 |
Args.nullNotPermitted(f, "f"); |
| 78 |
StringBuilder b = new StringBuilder("["); |
| 79 |
for (int i = 0; i < f.length; i++) { |
| 80 |
if (i != 0) { |
| 81 |
b.append(" "); |
| 82 |
} |
| 83 |
b.append(String.valueOf(f[i])); |
| 84 |
} |
| 85 |
return b.append("]").toString(); |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* A utility method to convert a double[] to a PDF array string. |
| 90 |
* |
| 91 |
* @param d the array ({@code null} not permitted). |
| 92 |
* |
| 93 |
* @return The string. |
| 94 |
*/ |
| 95 |
public static String toPDFArray(double[] d) { |
| 96 |
Args.nullNotPermitted(d, "d"); |
| 97 |
StringBuilder b = new StringBuilder("["); |
| 98 |
for (int i = 0; i < d.length; i++) { |
| 99 |
if (i != 0) { |
| 100 |
b.append(" "); |
| 101 |
} |
| 102 |
b.append(String.valueOf(d[i])); |
| 103 |
} |
| 104 |
return b.append("]").toString(); |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Returns a Java2D AffineTransform in PDF matrix format. |
| 109 |
* |
| 110 |
* @param t the transform ({@code null} not permitted). |
| 111 |
* |
| 112 |
* @return A PDF matrix string. |
| 113 |
*/ |
| 114 |
public static String transformToPDF(AffineTransform t) { |
| 115 |
Args.nullNotPermitted(t, "t"); |
| 116 |
StringBuilder b = new StringBuilder("["); |
| 117 |
b.append(t.getScaleX()).append(" "); |
| 118 |
b.append(t.getShearY()).append(" "); |
| 119 |
b.append(t.getShearX()).append(" "); |
| 120 |
b.append(t.getScaleY()).append(" "); |
| 121 |
b.append(t.getTranslateX()).append(" "); |
| 122 |
b.append(t.getTranslateY()); |
| 123 |
return b.append("]").toString(); |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Returns a string in standard PDF date format representing the specified |
| 128 |
* date (in the default timezone). |
| 129 |
* |
| 130 |
* @param date the date ({@code null} not permitted). |
| 131 |
* |
| 132 |
* @return A string in standard PDF date format. |
| 133 |
*/ |
| 134 |
public static String toDateFormat(Date date) { |
| 135 |
Calendar c = Calendar.getInstance(); |
| 136 |
c.setTime(date); |
| 137 |
return toPDFDateFormat(c); |
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* Returns a string in standard PDF date format representing the date |
| 142 |
* contained by the specified calendar. |
| 143 |
* |
| 144 |
* @param calendar the date and timezone ({@code null} not permitted). |
| 145 |
* |
| 146 |
* @return A string in standard PDF date format. |
| 147 |
*/ |
| 148 |
public static String toPDFDateFormat(Calendar calendar) { |
| 149 |
Date d = calendar.getTime(); |
| 150 |
DateFormat df1 = new SimpleDateFormat("yyyyMMddHHmmss"); |
| 151 |
DateFormat df2 = new SimpleDateFormat("Z"); |
| 152 |
String part1 = df1.format(d); |
| 153 |
String part2 = df2.format(d); |
| 154 |
String tzinfo; |
| 155 |
if (part2.equals("z")) { |
| 156 |
tzinfo = "Z00'00'"; |
| 157 |
} else { |
| 158 |
tzinfo = part2.substring(0, 3) + "'" + part2.substring(4) + "'"; |
| 159 |
} |
| 160 |
return "D:" + part1 + tzinfo; |
| 161 |
} |
| 162 |
|
| 163 |
/** |
| 164 |
* A utility method to convert a string to US-ASCII byte format. |
| 165 |
* |
| 166 |
* @param s the string. |
| 167 |
* |
| 168 |
* @return The corresponding byte array. |
| 169 |
*/ |
| 170 |
public static byte[] toBytes(String s) { |
| 171 |
byte[] result = null; |
| 172 |
try { |
| 173 |
result = s.getBytes("US-ASCII"); |
| 174 |
} catch (UnsupportedEncodingException ex) { |
| 175 |
throw new RuntimeException(ex); |
| 176 |
} |
| 177 |
return result; |
| 178 |
} |
| 179 |
|
| 180 |
} |