1 58 package org.krysalis.barcode.output.eps; 59 60 import java.io.IOException ; 61 import java.io.OutputStream ; 62 import java.io.UnsupportedEncodingException ; 63 import java.io.Writer ; 64 import java.text.DecimalFormat ; 65 import java.text.DecimalFormatSymbols ; 66 import java.text.SimpleDateFormat ; 67 68 import org.apache.avalon.framework.CascadingRuntimeException; 69 import org.krysalis.barcode.BarcodeDimension; 70 import org.krysalis.barcode.output.AbstractCanvasProvider; 71 import org.krysalis.barcode.tools.UnitConv; 72 73 77 public class EPSCanvasProvider extends AbstractCanvasProvider { 78 79 private OutputStream out; 80 private Writer writer; 81 private DecimalFormat df; 82 private IOException firstError; 83 private double height; 84 85 90 public EPSCanvasProvider(OutputStream out) throws IOException { 91 super(); 92 this.out = out; 93 try { 94 this.writer = new java.io.OutputStreamWriter (out, "US-ASCII"); 95 } catch (UnsupportedEncodingException uee) { 96 throw new CascadingRuntimeException( 97 "Incompatible VM: Need US-ASCII encoding", uee); 98 } 99 } 100 101 105 protected DecimalFormat getDecimalFormat() { 106 if (this.df == null) { 107 DecimalFormatSymbols dfs = new DecimalFormatSymbols (); 108 dfs.setDecimalSeparator('.'); 109 this.df = new DecimalFormat ("0.####", dfs); 110 } 111 return this.df; 112 } 113 114 private String format(double coord) { 115 return getDecimalFormat().format(coord); 116 } 117 118 private String formatmm(double coord) { 119 return getDecimalFormat().format(UnitConv.mm2pt(coord)); 120 } 121 122 private String formatmm(double x, double y) { 123 return formatmm(x) + " " + formatmm(this.height - y); 124 } 125 126 private void writeHeader(double width, double height) throws IOException { 127 writer.write("%!PS-Adobe-3.0 EPSF-3.0\n"); 128 double widthpt = UnitConv.mm2pt(width); 129 double heightpt = UnitConv.mm2pt(height); 130 writer.write("%%BoundingBox: 0 0 " 131 + Math.round(Math.ceil(widthpt)) + " " 132 + Math.round(Math.ceil(heightpt)) + "\n"); 133 writer.write("%%HiResBoundingBox: 0 0 " 134 + format(widthpt) + " " 135 + format(heightpt) + "\n"); 136 writer.write("%%Creator: Krysalis Barcode (http://www.krysalis.org/barcode)\n"); 137 final SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd'T'HH:mm:ss"); 138 writer.write("%%CreationDate: " + sdf.format(new java.util.Date ()) + "\n"); 139 writer.write("%%LanguageLevel: 1\n"); 140 writer.write("%%EndComments\n"); 141 writer.write("%%BeginProlog\n"); 142 writer.write("%%BeginProcSet: krysalis-barcode-procset 1.0\n"); 143 writer.write("/rf {\n"); writer.write("newpath\n"); 145 writer.write("4 -2 roll moveto\n"); 146 writer.write("dup neg 0 exch rlineto\n"); 147 writer.write("exch 0 rlineto\n"); 148 writer.write("0 neg exch rlineto\n"); 149 writer.write("closepath fill\n"); 150 writer.write("} def\n"); 151 writer.write("/ct {\n"); writer.write("moveto dup stringwidth\n"); 153 writer.write("2 div neg exch 2 div neg exch\n"); 154 writer.write("rmoveto show\n"); 155 writer.write("} def\n"); 156 writer.write("/jt {\n"); writer.write("4 -1 roll dup stringwidth pop\n"); 159 writer.write("5 -2 roll 1 index sub\n"); 161 writer.write("3 -1 roll sub\n"); 163 writer.write("2 index length\n"); 165 writer.write("1 sub div\n"); 167 writer.write("0 4 -1 roll 4 -1 roll 5 -1 roll\n"); 169 writer.write("moveto ashow\n"); 170 writer.write("} def\n"); 171 writer.write("%%EndProcSet: krysalis-barcode-procset 1.0\n"); 172 writer.write("%%EndProlog\n"); 173 } 174 175 180 public void finish() throws IOException { 181 if (firstError != null) { 182 throw firstError; 183 } 184 writer.write("%%EOF\n"); 185 writer.flush(); 186 } 187 188 189 public void establishDimensions(BarcodeDimension dim) { 190 super.establishDimensions(dim); 191 if (firstError != null) { 192 return; 193 } 194 this.height = dim.getHeightPlusQuiet(); 195 try { 196 writeHeader(dim.getWidthPlusQuiet(), dim.getHeightPlusQuiet()); 197 } catch (IOException ioe) { 198 firstError = ioe; 199 } 200 } 201 202 203 public void deviceFillRect(double x, double y, double w, double h) { 204 if (firstError != null) { 205 return; 206 } 207 try { 208 212 writer.write(formatmm(x, y) + " " 213 + formatmm(w) + " " + formatmm(h) + " rf\n"); 214 } catch (IOException ioe) { 215 firstError = ioe; 216 } 217 } 218 219 220 public void deviceJustifiedText( 221 String text, 222 double x1, 223 double x2, 224 double y1, 225 String fontName, 226 double fontSize) { 227 if (firstError != null) { 228 return; 229 } 230 try { 231 writer.write("/" + fontName + " findfont " 232 + fontSize + " scalefont setfont\n"); 233 234 writer.write("(" + text + ") " 235 + formatmm(x1) + " " 236 + formatmm(x2) + " " 237 + formatmm(this.height - y1) + " jt\n"); 238 } catch (IOException ioe) { 239 firstError = ioe; 240 } 241 } 242 243 244 public void deviceCenteredText( 245 String text, 246 double x1, 247 double x2, 248 double y1, 249 String fontName, 250 double fontSize) { 251 if (firstError != null) { 252 return; 253 } 254 try { 255 writer.write("/" + fontName + " findfont " 256 + fontSize + " scalefont setfont\n"); 257 writer.write("(" + text + ") " 258 + formatmm((x1 + x2) / 2, y1) + " ct\n"); 259 } catch (IOException ioe) { 260 firstError = ioe; 261 } 262 } 263 264 } 265 | Popular Tags |