1 26 27 package it.stefanochizzolini.clown.documents; 28 29 import it.stefanochizzolini.clown.util.NotImplementedException; 30 31 import java.awt.Dimension ; 32 import java.util.regex.Matcher ; 33 import java.util.regex.Pattern ; 34 35 42 public class PageFormat 43 { 44 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 74 public enum OrientationEnum 75 { 76 Portrait, 77 Landscape 78 } 79 81 private static final String SizeIsoSeries_A = "A"; 84 private static final String SizeIsoSeries_B = "B"; 85 private static final String SizeIsoSeries_C = "C"; 86 87 private static final Pattern IsoSeriesSizePattern = Pattern.compile( 88 "([" 89 + SizeIsoSeries_A 90 + SizeIsoSeries_B 91 + SizeIsoSeries_C 92 + "])([\\d]+)" 93 ); 94 96 public static Dimension getSize( 99 ) 100 {return getSize(SizeEnum.A4);} 101 102 public static Dimension getSize( 103 SizeEnum size 104 ) 105 {return getSize(size,OrientationEnum.Portrait);} 106 107 public static Dimension getSize( 108 SizeEnum size, 109 OrientationEnum orientation 110 ) 111 { 112 int width, height = 0; 113 114 { 116 String sizeName = size.name(); 117 Matcher matcher = IsoSeriesSizePattern.matcher(sizeName); 118 if(matcher.matches()) 120 { 121 int baseWidth, baseHeight = 0; 122 String 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 { 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 switch(orientation) 154 { 155 case Portrait: 156 return new Dimension (width,height); 157 case Landscape: 158 return new Dimension (height,width); 159 default: 160 throw new NotImplementedException("Orientation " + orientation + " not supported yet."); 161 } 162 } 163 167 private PageFormat( 170 ) 171 {} 172 } | Popular Tags |