KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > it > stefanochizzolini > clown > documents > PageFormat


1 /*
2   Copyright 2007 Stefano Chizzolini. http://clown.stefanochizzolini.it
3
4   Contributors:
5     * Stefano Chizzolini (original code developer, info@stefanochizzolini.it):
6       contributed code is Copyright 2007 by Stefano Chizzolini.
7
8   This file should be part of the source code distribution of "PDF Clown library"
9   (the Program): see the accompanying README files for more info.
10
11   This Program is free software; you can redistribute it and/or modify it under
12   the terms of the GNU General Public License as published by the Free Software
13   Foundation; either version 2 of the License, or (at your option) any later version.
14
15   This Program is distributed in the hope that it will be useful, but WITHOUT ANY
16   WARRANTY, either expressed or implied; without even the implied warranty of
17   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the License for more details.
18
19   You should have received a copy of the GNU General Public License along with this
20   Program (see README files); if not, go to the GNU website (http://www.gnu.org/).
21
22   Redistribution and use, with or without modification, are permitted provided that such
23   redistributions retain the above copyright notice, license and disclaimer, along with
24   this list of conditions.
25 */

26
27 package it.stefanochizzolini.clown.documents;
28
29 import it.stefanochizzolini.clown.util.NotImplementedException;
30
31 import java.awt.Dimension JavaDoc;
32 import java.util.regex.Matcher JavaDoc;
33 import java.util.regex.Pattern JavaDoc;
34
35 /**
36   Page format.
37
38   @author Stefano Chizzolini
39   @version 0.0.3.42, 05/05/07
40   @since 0.0.3
41 */

42 public class PageFormat
43 {
44   // <class>
45
// <classes>
46
/**
47     Paper size.
48     <h3>Remarks</h3>
49     <p>References:</p>
50     <ul>
51       <li>{ 'A' digit+ }: [ISO 216] "A" series: Paper and boards, trimmed sizes.</li>
52       <li>{ 'B' digit+ }: [ISO 216] "B" series: Posters, wall charts and similar items.</li>
53       <li>{ 'C' digit+ }: [ISO 269] "C" series: Envelopes or folders suitable for A-size
54       stationery.</li>
55       <li>{ "Letter", "Legal", "Executive", "Statement", "Tabloid" }: Traditional north-american
56       sizes.</li>
57     </ul>
58   */

59   public enum SizeEnum
60   {
61     A0,A1,A2,A3,A4,A5,A6,A7,A8,A9,A10,
62     B0,B1,B2,B3,B4,B5,B6,B7,B8,B9,B10,
63     C0,C1,C2,C3,C4,C5,C6,C7,C8,C9,C10,
64     Letter,
65     Legal,
66     Executive,
67     Statement,
68     Tabloid
69   };
70
71   /**
72     Page orientation.
73   */

74   public enum OrientationEnum
75   {
76     Portrait,
77     Landscape
78   }
79   // </classes>
80

81   // <static>
82
// <fields>
83
private static final String JavaDoc SizeIsoSeries_A = "A";
84   private static final String JavaDoc SizeIsoSeries_B = "B";
85   private static final String JavaDoc SizeIsoSeries_C = "C";
86
87   private static final Pattern JavaDoc IsoSeriesSizePattern = Pattern.compile(
88     "(["
89       + SizeIsoSeries_A
90       + SizeIsoSeries_B
91       + SizeIsoSeries_C
92       + "])([\\d]+)"
93     );
94   // </fields>
95

96   // <interface>
97
// <public>
98
public static Dimension JavaDoc getSize(
99     )
100   {return getSize(SizeEnum.A4);}
101
102   public static Dimension JavaDoc getSize(
103     SizeEnum size
104     )
105   {return getSize(size,OrientationEnum.Portrait);}
106
107   public static Dimension JavaDoc getSize(
108     SizeEnum size,
109     OrientationEnum orientation
110     )
111   {
112     int width, height = 0;
113
114     // Size.
115
{
116       String JavaDoc sizeName = size.name();
117       Matcher JavaDoc matcher = IsoSeriesSizePattern.matcher(sizeName);
118       // Is it an ISO standard size?
119
if(matcher.matches())
120       {
121         int baseWidth, baseHeight = 0;
122         String JavaDoc sizeIsoSeries = matcher.group(1);
123         if(sizeIsoSeries.equals(SizeIsoSeries_A))
124         {baseWidth = 2384; baseHeight = 3370;}
125         else if(sizeIsoSeries.equals(SizeIsoSeries_B))
126         {baseWidth = 2834; baseHeight = 4008;}
127         else if(sizeIsoSeries.equals(SizeIsoSeries_C))
128         {baseWidth = 2599; baseHeight = 3676;}
129         else
130         {throw new NotImplementedException("Paper format " + size + " not supported yet.");}
131
132         int sizeIsoIndex = Integer.parseInt(matcher.group(2));
133         double sizeIsoFactor = 1 / Math.pow(2,sizeIsoIndex/2d);
134
135         width = (int)Math.floor(baseWidth * sizeIsoFactor);
136         height = (int)Math.floor(baseHeight * sizeIsoFactor);
137       }
138       else // Non-ISO size.
139
{
140         switch(size)
141         {
142           case Letter: width = 612; height = 792; break;
143           case Legal: width = 612; height = 1008; break;
144           case Executive: width = 522; height = 756; break;
145           case Statement: width = 396; height = 612; break;
146           case Tabloid: width = 792; height = 1224; break;
147           default: throw new NotImplementedException("Paper format " + size + " not supported yet.");
148         }
149       }
150     }
151
152     // Orientation.
153
switch(orientation)
154     {
155       case Portrait:
156         return new Dimension JavaDoc(width,height);
157       case Landscape:
158         return new Dimension JavaDoc(height,width);
159       default:
160         throw new NotImplementedException("Orientation " + orientation + " not supported yet.");
161     }
162   }
163   // </public>
164
// </interface>
165
// </static>
166

167   // <dynamic>
168
// <constructors>
169
private PageFormat(
170     )
171   {}
172   // </constructors>
173
// </dynamic>
174
// </class>
175
}
Popular Tags