1 package net.sf.openrocket.gui.print;
3 import java.io.BufferedReader;
4 import java.io.FileReader;
5 import java.io.IOException;
6 import java.util.Locale;
8 import net.sf.openrocket.logging.LogHelper;
9 import net.sf.openrocket.startup.Application;
11 import com.itextpdf.text.PageSize;
12 import com.itextpdf.text.Rectangle;
14 public enum PaperSize {
15 A3("A3", PageSize.A3),
16 A4("A4", PageSize.A4),
17 A5("A5", PageSize.A5),
18 LETTER("Letter", PageSize.LETTER),
19 LEGAL("Legal", PageSize.LEGAL);
21 private final String name;
22 private final Rectangle size;
24 private PaperSize(String name, Rectangle size) {
29 public Rectangle getSize() {
34 public String toString() {
40 //////////////////////////
42 private static final LogHelper log = Application.getLogger();
43 private static PaperSize defaultSize = null;
46 * Return the default paper size for the current system.
47 * @return the default paper size
49 public static PaperSize getDefault() {
50 if (defaultSize == null) {
52 // Test environment variable "PAPERSIZE" (Unix)
53 defaultSize = getDefaultFromEnvironmentVariable();
54 if (defaultSize != null) {
55 log.info("Selecting default paper size from PAPERSIZE environment variable: " + defaultSize);
59 // Test /etc/papersize (Unix)
60 defaultSize = getDefaultFromEtcPapersize();
61 if (defaultSize != null) {
62 log.info("Selecting default paper size from /etc/papersize: " + defaultSize);
67 defaultSize = getDefaultForCountry(System.getProperty("user.country"));
68 if (defaultSize != null) {
69 log.info("Selecting default paper size based on user.country: " + defaultSize);
73 // Test locale country
74 defaultSize = getDefaultForCountry(Locale.getDefault().getCountry());
75 if (defaultSize != null) {
76 log.info("Selecting default paper size based on locale country: " + defaultSize);
82 log.info("Selecting default paper size fallback: " + defaultSize);
90 * Attempt to read the default paper size from the "PAPERSIZE" environment variable.
92 * @return the default paper size if successful, or <code>null</code> if unable to read/parse file.
94 private static PaperSize getDefaultFromEnvironmentVariable() {
95 String str = System.getenv("PAPERSIZE");
96 return getSizeFromString(str);
100 * Attempt to read the default paper size from the file defined by the environment variable
101 * PAPERCONF or from /etc/papersize.
103 * @return the default paper size if successful, or <code>null</code> if unable to read/parse file.
105 private static PaperSize getDefaultFromEtcPapersize() {
108 String file = System.getenv("PAPERCONF");
110 file = "/etc/papersize";
113 // Attempt to read the file
114 BufferedReader in = null;
118 in = new BufferedReader(new FileReader(file));
119 while ((str = in.readLine()) != null) {
120 if (str.matches("^\\s*(#.*|$)")) {
126 return getSizeFromString(str);
128 } catch (IOException e) {
130 // Could not read file
137 } catch (IOException e) {
145 * Get a paper size based on a string. The string is trimmed and case-insensitively
146 * compared to the base names of the paper sizes.
148 * @param size the size string (may be null)
149 * @return the corresponding paper size, or null if unknown
151 static PaperSize getSizeFromString(String size) {
157 for (PaperSize p : PaperSize.values()) {
158 if (p.name.equalsIgnoreCase(size)) {
167 * Get default paper size for a specific country. This method falls back to A4 for
168 * any country not known to use Letter.
170 * @param country the 2-char country code (may be null)
171 * @return the paper size, or <code>null</code> if country is not a country code
173 static PaperSize getDefaultForCountry(String country) {
175 * List is based on info from http://unicode.org/repos/cldr-tmp/trunk/diff/supplemental/territory_language_information.html
176 * OpenOffice.org agrees with this: http://wiki.services.openoffice.org/wiki/DefaultPaperSize#Summary
178 final String[] letterCountries = { "BZ", "CA", "CL", "CO", "CR", "SV", "GT", "MX", "NI", "PA", "PH", "PR", "US", "VE" };
180 if (country == null || !country.matches("^[a-zA-Z][a-zA-Z]$")) {
184 country = country.toUpperCase(Locale.ENGLISH);
185 for (String c : letterCountries) {
186 if (c.equals(country)) {