OSDN Git Service

54fd0e2e855d5df1bf2720d463899509854d14a4
[posterdivider/PosterDivider.git] / src / jp / sourceforge / posterdivider / Lib.java
1 //
2 // Lib.java
3 // This file is part of PosterDivider.
4 //
5 package jp.sourceforge.posterdivider;
6
7 import java.io.*;
8 import java.util.Locale;
9 import java.util.ResourceBundle;
10
11 import com.itextpdf.text.Image;
12 import com.itextpdf.text.PageSize;
13 import com.itextpdf.text.Rectangle;
14 import com.itextpdf.text.Utilities;
15 //import com.lowagie.text.*;
16
17 class Lib {
18         // OS
19         public final static int OS_WIN = 0;
20         public final static int OS_MAC = 1;
21         public final static int OS_OTHER = 2;
22
23         // FileType
24         public final static int FT_NONE = 0;
25         public final static int FT_PDF = 1;
26         public final static int FT_BMP = 2;
27         public final static int FT_GIF = 3;
28         public final static int FT_JBIG2 = 4;
29         public final static int FT_JPEG = 5;
30         public final static int FT_JPEG2000 = 6;
31         public final static int FT_PNG = 7;
32         public final static int FT_TIFF = 8;
33         public final static int FT_WMF = 9;
34         public final static int FT_OTHERIMAGE = 10;
35
36         //
37         public final static double UNIT_POINT_POINT = 1.0;
38         public final static double UNIT_POINT_INCH = Utilities.pointsToInches(1);
39         public final static double UNIT_POINT_MM = Utilities.pointsToMillimeters(1);
40         public final static double UNIT_POINT_CM = UNIT_POINT_MM / 10;
41         public final static double UNIT_POINT_M = UNIT_POINT_MM / 1000;
42         public final static double UNIT_PIXEL_PIXEL = 1.0;
43
44         public static int getImageFileType(Image image) {
45                 switch (image.getOriginalType()) {
46                 case Image.ORIGINAL_BMP:
47                         return FT_BMP;
48                 case Image.ORIGINAL_GIF:
49                         return FT_GIF;
50                 case Image.ORIGINAL_JBIG2:
51                         return FT_JBIG2;
52                 case Image.ORIGINAL_JPEG:
53                         return FT_JPEG;
54                 case Image.ORIGINAL_JPEG2000:
55                         return FT_JPEG2000;
56                 case Image.ORIGINAL_PNG:
57                         return FT_PNG;
58                 case Image.ORIGINAL_TIFF:
59                         return FT_TIFF;
60                 case Image.ORIGINAL_WMF:
61                         return FT_WMF;
62                 default:
63                         return FT_OTHERIMAGE;
64                 }
65         }
66
67         public static String getFileTypeName(int fileType) {
68                 switch (fileType) {
69                 case FT_PDF:
70                         return "PDF ";
71                 case FT_BMP:
72                         return "BMP ";
73                 case FT_GIF:
74                         return "GIF";
75                 case FT_JBIG2:
76                         return "JBIG2";
77                 case FT_JPEG:
78                         return "JPEG";
79                 case FT_JPEG2000:
80                         return "JPEG 2000";
81                 case FT_PNG:
82                         return "PNG";
83                 case FT_TIFF:
84                         return "TIFF";
85                 case FT_WMF:
86                         return "WMF";
87                 case FT_OTHERIMAGE:
88                         return "IMAGE";
89                 default:
90                         return "";
91                 }
92         }
93     public static int judgeFileTypeFromExtention(File f) {
94         if(f == null) return FT_NONE;
95         String s = f.getName();
96         int p = f.getName().lastIndexOf(".");
97         if(p < 0) return FT_NONE;
98         s = s.substring(p).toLowerCase();
99         
100         if(s.equals(".pdf")) {
101             return FT_PDF;
102         } else if (s.equals(".bmp")) {
103             return FT_BMP;
104         } else if (s.equals(".gif")) {
105             return FT_GIF;
106         } else if (s.equals(".jbg") || s.equals(".jbig")) {
107             return FT_JBIG2;
108         } else if (s.equals(".jpeg") || s.equals(".jpg") || s.equals(".jpe") || s.equals(".jfif") || s.equals(".jfi") || s.equals(".jif")) {
109             return FT_JPEG;
110         } else if (s.equals(".jp2") || s.equals(".j2c")) {
111             return FT_JPEG2000;
112         } else if (s.equals(".png")) {
113             return FT_PNG;
114         } else if (s.equals(".tiff") || s.equals("tif")) {
115             return FT_TIFF;
116         } else if (s.equals(".wmf") || s.equals(".emf") || s.equals(".wmz") || s.equals(".emz")) {
117             return FT_WMF;
118         } else {
119             return FT_NONE;
120         }
121     }
122
123         private static String osName;
124         private static int os;
125         private static ResourceBundle resourceBundle;
126         static {
127                 osName = System.getProperty("os.name");
128                 String lowerOSName = osName.toLowerCase();
129                 if(lowerOSName.startsWith("win")) {
130                         os = OS_WIN;
131                 } else if(lowerOSName.startsWith("mac")) {
132                         os = OS_MAC;
133                 } else {
134                         os = OS_OTHER;
135                 }
136
137                 resourceBundle = ResourceBundle.getBundle("jp.sourceforge.posterdivider.Message", Locale.getDefault());
138         }
139
140     public static int getOS() {
141         return os;
142     }
143     public static String getOSName() {
144         return osName;
145     }
146
147     public static String getMessage(String key) {
148         try {
149             String value = resourceBundle.getString(key);
150             return new String(value.getBytes("ISO-8859-1"), "UTF-8");
151         } catch (Exception ex) {
152             return key;
153         }
154     }
155
156         public static byte[] FileReadAllBytes(File file) throws Exception {
157                 // TODO:
158                 FileInputStream is;
159                 is = new FileInputStream(file);
160                 int length = is.available();
161                 byte[] data = new byte[length];
162                 is.read(data);
163                 is.close();
164                 return data;
165         }
166 }
167
168 class PaperSize {
169         public final static PaperSize A0 = new PaperSize(PageSize.A0);
170         public final static PaperSize A1 = new PaperSize(PageSize.A1);
171         public final static PaperSize A2 = new PaperSize(PageSize.A2);
172         public final static PaperSize A3 = new PaperSize(PageSize.A3);
173         public final static PaperSize A4 = new PaperSize(PageSize.A4);
174         public final static PaperSize A5 = new PaperSize(PageSize.A5);
175         public final static PaperSize A6 = new PaperSize(PageSize.A6);
176         public final static PaperSize B0 = new PaperSize(PageSize.B0);
177         public final static PaperSize B1 = new PaperSize(PageSize.B1);
178         public final static PaperSize B2 = new PaperSize(PageSize.B2);
179         public final static PaperSize B3 = new PaperSize(PageSize.B3);
180         public final static PaperSize B4 = new PaperSize(PageSize.B4);
181         public final static PaperSize B5 = new PaperSize(PageSize.B5);
182         public final static PaperSize B6 = new PaperSize(PageSize.B6);
183         public final static PaperSize CUSTOM = new PaperSize(true, 0, 0);
184
185         private boolean custom;
186         private float width;
187         private float height;
188
189         public boolean isCustom() {
190                 return this.custom;
191         }
192
193         public float getWidth() {
194                 return this.width;
195         }
196
197         public float getHeight() {
198                 return this.height;
199         }
200
201         public PaperSize(boolean custom, float width, float height) {
202                 this.custom = custom;
203                 this.width = width;
204                 this.height = height;
205         }
206
207         private PaperSize(Rectangle rect) {
208                 this.custom = false;
209                 this.width = rect.getWidth();
210                 this.height = rect.getHeight();
211         }
212 }