001: /*
002: Copyright © 2007 Stefano Chizzolini. http://clown.stefanochizzolini.it
003:
004: Contributors:
005: * Stefano Chizzolini (original code developer, http://www.stefanochizzolini.it):
006: contributed code is Copyright © 2007 by Stefano Chizzolini.
007:
008: This file should be part of the source code distribution of "PDF Clown library"
009: (the Program): see the accompanying README files for more info.
010:
011: This Program is free software; you can redistribute it and/or modify it under
012: the terms of the GNU General Public License as published by the Free Software
013: Foundation; either version 2 of the License, or (at your option) any later version.
014:
015: This Program is distributed in the hope that it will be useful, but WITHOUT ANY
016: WARRANTY, either expressed or implied; without even the implied warranty of
017: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the License for more details.
018:
019: You should have received a copy of the GNU General Public License along with this
020: Program (see README files); if not, go to the GNU website (http://www.gnu.org/).
021:
022: Redistribution and use, with or without modification, are permitted provided that such
023: redistributions retain the above copyright notice, license and disclaimer, along with
024: this list of conditions.
025: */
026:
027: package it.stefanochizzolini.clown.documents;
028:
029: import it.stefanochizzolini.clown.util.NotImplementedException;
030:
031: import java.awt.Dimension;
032: import java.util.regex.Matcher;
033: import java.util.regex.Pattern;
034:
035: /**
036: Page format.
037: <p>This utility provides an easy access to the dimension of common page formats.</p>
038:
039: @author Stefano Chizzolini
040: @version 0.0.4, 07/27/07
041: @since 0.0.3
042: @see Document#setPageSize(Dimension2D)
043: @see Page#setSize(Dimension2D)
044: */
045: public class PageFormat {
046: // <class>
047: // <classes>
048: /**
049: Paper size.
050: <h3>Remarks</h3>
051: <p>References:</p>
052: <ul>
053: <li>{ 'A' digit+ }: [ISO 216] "A" series: Paper and boards, trimmed sizes.</li>
054: <li>{ 'B' digit+ }: [ISO 216] "B" series: Posters, wall charts and similar items.</li>
055: <li>{ 'C' digit+ }: [ISO 269] "C" series: Envelopes or folders suitable for A-size
056: stationery.</li>
057: <li>{ 'Ansi' letter }: [ANSI/ASME Y14.1] ANSI series: US engineering drawing series.</li>
058: <li>{ 'Arch' letter }: Architectural series.</li>
059: <li>{ "Letter", "Legal", "Executive", "Statement", "Tabloid" }: Traditional north-american
060: sizes.</li>
061: </ul>
062: */
063: public enum SizeEnum {
064: A0, A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, B0, B1, B2, B3, B4, B5, B6, B7, B8, B9, B10, C0, C1, C2, C3, C4, C5, C6, C7, C8, C9, C10, Letter, Legal, Executive, Statement, Tabloid, ArchA, ArchB, ArchC, ArchD, ArchE, AnsiA, AnsiB, AnsiC, AnsiD, AnsiE
065: };
066:
067: /**
068: Page orientation.
069: */
070: public enum OrientationEnum {
071: Portrait, Landscape
072: }
073:
074: // </classes>
075:
076: // <static>
077: // <fields>
078: private static final String IsoSeriesSize_A = "A";
079: private static final String IsoSeriesSize_B = "B";
080: private static final String IsoSeriesSize_C = "C";
081:
082: private static final Pattern IsoSeriesSizePattern = Pattern
083: .compile("([" + IsoSeriesSize_A + IsoSeriesSize_B
084: + IsoSeriesSize_C + "])([\\d]+)");
085:
086: // </fields>
087:
088: // <interface>
089: // <public>
090: /**
091: Gets the default page size.
092: <h3>Remarks</h3>
093: <p>The returned dimension corresponds to the widely-established ISO A4 standard paper format,
094: portrait orientation.</p>
095: */
096: public static Dimension getSize() {
097: return getSize(SizeEnum.A4);
098: }
099:
100: /**
101: Gets the page size of the given format, portrait orientation.
102: @param size Page size.
103: */
104: public static Dimension getSize(SizeEnum size) {
105: return getSize(size, OrientationEnum.Portrait);
106: }
107:
108: /**
109: Gets the page size of the given format and orientation.
110: @param size Page size.
111: @param orientation Page orientation.
112: */
113: public static Dimension getSize(SizeEnum size,
114: OrientationEnum orientation) {
115: int width, height = 0;
116:
117: // Size.
118: {
119: String sizeName = size.name();
120: Matcher matcher = IsoSeriesSizePattern.matcher(sizeName);
121: // Is it an ISO standard size?
122: if (matcher.matches()) {
123: int baseWidth, baseHeight = 0;
124: String isoSeriesSize = matcher.group(1);
125: if (isoSeriesSize.equals(IsoSeriesSize_A)) {
126: baseWidth = 2384;
127: baseHeight = 3370;
128: } else if (isoSeriesSize.equals(IsoSeriesSize_B)) {
129: baseWidth = 2834;
130: baseHeight = 4008;
131: } else if (isoSeriesSize.equals(IsoSeriesSize_C)) {
132: baseWidth = 2599;
133: baseHeight = 3676;
134: } else {
135: throw new NotImplementedException("Paper format "
136: + size + " not supported yet.");
137: }
138:
139: int isoSeriesSizeIndex = Integer.parseInt(matcher
140: .group(2));
141: double isoSeriesSizeFactor = 1 / Math.pow(2,
142: isoSeriesSizeIndex / 2d);
143:
144: width = (int) Math.floor(baseWidth
145: * isoSeriesSizeFactor);
146: height = (int) Math.floor(baseHeight
147: * isoSeriesSizeFactor);
148: } else // Non-ISO size.
149: {
150: switch (size) {
151: case ArchA:
152: width = 648;
153: height = 864;
154: break;
155: case ArchB:
156: width = 864;
157: height = 1296;
158: break;
159: case ArchC:
160: width = 1296;
161: height = 1728;
162: break;
163: case ArchD:
164: width = 1728;
165: height = 2592;
166: break;
167: case ArchE:
168: width = 2592;
169: height = 3456;
170: break;
171: case AnsiA:
172: case Letter:
173: width = 612;
174: height = 792;
175: break;
176: case AnsiB:
177: case Tabloid:
178: width = 792;
179: height = 1224;
180: break;
181: case AnsiC:
182: width = 1224;
183: height = 1584;
184: break;
185: case AnsiD:
186: width = 1584;
187: height = 2448;
188: break;
189: case AnsiE:
190: width = 2448;
191: height = 3168;
192: break;
193: case Legal:
194: width = 612;
195: height = 1008;
196: break;
197: case Executive:
198: width = 522;
199: height = 756;
200: break;
201: case Statement:
202: width = 396;
203: height = 612;
204: break;
205: default:
206: throw new NotImplementedException("Paper format "
207: + size + " not supported yet.");
208: }
209: }
210: }
211:
212: // Orientation.
213: switch (orientation) {
214: case Portrait:
215: return new Dimension(width, height);
216: case Landscape:
217: return new Dimension(height, width);
218: default:
219: throw new NotImplementedException("Orientation "
220: + orientation + " not supported yet.");
221: }
222: }
223:
224: // </public>
225: // </interface>
226: // </static>
227:
228: // <dynamic>
229: // <constructors>
230: private PageFormat() {
231: }
232: // </constructors>
233: // </dynamic>
234: // </class>
235: }
|