1 29 30 package com.caucho.quercus.lib.pdf; 31 32 import com.caucho.quercus.QuercusModuleException; 33 import com.caucho.util.L10N; 34 import com.caucho.vfs.TempBuffer; 35 import com.caucho.vfs.TempStream; 36 import com.caucho.vfs.WriteStream; 37 38 import java.io.IOException ; 39 import java.util.logging.Logger ; 40 41 44 public class PDFStream { 45 private static final Logger log 46 = Logger.getLogger(PDFStream.class.getName()); 47 private static final L10N L = new L10N(PDFStream.class); 48 49 private int _id; 50 51 private TempStream _tempStream = new TempStream(); 52 private WriteStream _out = new WriteStream(); 53 54 private PDFProcSet _procSet; 55 private PDFFont _font; 56 private double _fontSize = 24; 57 58 private boolean _inText; 59 private boolean _hasFont; 60 private boolean _hasTextPos = true; 61 private double _textX = 0; 62 private double _textY = 0; 63 64 private double _x = 0; 65 private double _y = 0; 66 private boolean _hasGraphicsPos = true; 67 68 PDFStream(int id) 69 { 70 _id = id; 71 72 _tempStream = new TempStream(); 73 _tempStream.openWrite(); 74 _out.init(_tempStream); 75 76 _procSet = new PDFProcSet(); 77 _procSet.add("/PDF"); 78 79 _font = null; 80 _inText = false; 81 _hasFont = false; 82 _hasTextPos = true; 83 _hasGraphicsPos = true; 84 } 85 86 public int getId() 87 { 88 return _id; 89 } 90 91 public void setFont(PDFFont font, double size) 92 { 93 _font = font; 94 _fontSize = size; 95 _hasFont = false; 96 } 97 98 public PDFFont getFont() 99 { 100 return _font; 101 } 102 103 public double getFontSize() 104 { 105 return _fontSize; 106 } 107 108 public void setTextPos(double x, double y) 109 { 110 _textX = x; 111 _textY = y; 112 _hasTextPos = false; 113 } 114 115 public void stroke() 116 { 117 flushToGraph(); 118 119 println("S"); 120 } 121 122 public void closepath() 123 { 124 flushToGraph(); 125 126 println("h"); 127 } 128 129 public void clip() 130 { 131 flushToGraph(); 132 133 println("W"); 134 } 135 136 public void curveTo(double x1, double y1, 137 double x2, double y2, 138 double x3, double y3) 139 { 140 flushToGraph(); 141 142 if (x1 == x2 && y1 == y2) { 143 println(x1 + " " + y1 + " " + 144 x3 + " " + y3 + " y"); 145 } 146 else if (x2 == x3 && y2 == y3) { 147 println(x1 + " " + y1 + " " + 148 x2 + " " + y2 + " v"); 149 } 150 else { 151 println(x1 + " " + y1 + " " + 152 x2 + " " + y2 + " " + 153 x3 + " " + y3 + " c"); 154 } 155 156 _x = x3; 157 _y = y3; 158 _hasGraphicsPos = true; 159 } 160 161 public void endpath() 162 { 163 flushToGraph(); 164 165 println("n"); 166 } 167 168 public void closepathStroke() 169 { 170 flushToGraph(); 171 172 println("s"); 173 } 174 175 public void closepathFillStroke() 176 { 177 flushToGraph(); 178 179 println("b"); 180 } 181 182 public void fill() 183 { 184 flushToGraph(); 185 186 println("f"); 187 } 188 189 public void fillStroke() 190 { 191 flushToGraph(); 192 193 println("B"); 194 } 195 196 public void lineTo(double x, double y) 197 { 198 flushToGraph(); 199 200 if (x != _x || y != _y || ! _hasGraphicsPos) 201 println(x + " " + y + " l"); 202 203 _x = x; 204 _y = y; 205 _hasGraphicsPos = true; 206 } 207 208 public void rect(double x, double y, double w, double h) 209 { 210 flushToGraph(); 211 212 println(x + " " + y + " " + w + " " + h + " re"); 213 } 214 215 public void moveTo(double x, double y) 216 { 217 if (_x != x || _y != y) { 218 _x = x; 219 _y = y; 220 _hasGraphicsPos = false; 221 } 222 } 223 224 public static int STROKE = 1; 225 public static int FILL = 2; 226 public static int BOTH = 3; 227 228 public boolean setcolor(String fstype, String colorspace, 229 double c1, double c2, double c3, double c4) 230 { 231 flushToGraph(); 232 233 int type; 234 if ("both".equals(fstype) || "fillstroke".equals(fstype)) 235 type = BOTH; 236 else if ("fill".equals(fstype)) 237 type = FILL; 238 else if ("stroke".equals(fstype)) 239 type = STROKE; 240 else 241 return false; 242 243 if ("gray".equals(colorspace)) { 244 if ((type & STROKE) != 0) 245 println(c1 + " G"); 246 if ((type & FILL) != 0) 247 println(c1 + " g"); 248 249 return true; 250 } 251 else if ("rgb".equals(colorspace)) { 252 if ((type & STROKE) != 0) 253 println(c1 + " " + c2 + " " + c3 + " RG"); 254 if ((type & FILL) != 0) 255 println(c1 + " " + c2 + " " + c3 + " rg"); 256 257 return true; 258 } 259 else if ("cmyk".equals(colorspace)) { 260 if ((type & STROKE) != 0) 261 println(c1 + " " + c2 + " " + c3 + " " + c4 + " K"); 262 if ((type & FILL) != 0) 263 println(c1 + " " + c2 + " " + c3 + " " + c4 + " k"); 264 265 return true; 266 } 267 else { 268 270 return false; 271 } 272 } 273 274 public void setDash(double b, double w) 275 { 276 println("[" + b + " " + w + "] 0 d"); 277 } 278 279 public boolean setlinewidth(double w) 280 { 281 println(w + " w"); 282 283 return true; 284 } 285 286 289 public boolean save() 290 { 291 println("q"); 292 293 return true; 294 } 295 296 299 public boolean restore() 300 { 301 println("Q"); 302 303 return true; 304 } 305 306 public boolean concat(double a, double b, double c, 307 double d, double e, double f) 308 { 309 println(String.format("%.4f %.4f %.4f %.4f %.4f %.4f cm", 310 a, b, c, d, e, f)); 311 312 return true; 313 } 314 315 public void show(String text) 316 { 317 _procSet.add("/Text"); 318 319 if (! _inText) { 320 println("BT"); 321 _inText = true; 322 } 323 324 if (! _hasFont && _font != null) { 325 println("/" + _font.getPDFName() + " " + _fontSize + " Tf"); 326 _hasFont = true; 327 } 328 329 if (! _hasTextPos) { 330 println(_textX + " " + _textY + " Td"); 331 _hasTextPos = true; 332 } 333 334 println("(" + text + ") Tj"); 335 } 336 337 public void continue_text(String text) 338 { 339 println("(" + text + ") T*"); 340 } 341 342 public boolean fit_image(PDFImage img) 343 { 344 _procSet.add("/ImageB"); 345 _procSet.add("/ImageC"); 346 _procSet.add("/ImageI"); 347 348 println("/I" + img.getId() + " Do"); 349 350 return true; 351 } 352 353 public void flushToGraph() 354 { 355 try { 356 flush(); 357 358 if (! _hasGraphicsPos) { 359 _out.println(_x + " " + _y + " m"); 360 _hasGraphicsPos = true; 361 } 362 } catch (IOException e) { 363 throw new QuercusModuleException(e); 364 } 365 } 366 367 private void println(String s) 368 { 369 try { 370 _out.println(s); 371 } catch (IOException e) { 372 throw new QuercusModuleException(e); 373 } 374 } 375 376 public void flush() 377 { 378 try { 379 if (_inText) { 380 _out.println("ET"); 381 _inText = false; 382 } 383 384 _out.flush(); 385 } catch (IOException e) { 386 throw new QuercusModuleException(e); 387 } 388 } 389 390 public PDFProcSet getProcSet() 391 { 392 return _procSet; 393 } 394 395 public int getLength() 396 { 397 try { 398 _out.flush(); 399 400 return _tempStream.getLength(); 401 } catch (IOException e) { 402 throw new QuercusModuleException(e); 403 } 404 } 405 406 public void write(PDFWriter out) 407 throws IOException 408 { 409 out.writeStream(getId(), this); 410 } 411 412 public void writeToStream(WriteStream os) 413 throws IOException 414 { 415 for (TempBuffer head = _tempStream.getHead(); 416 head != null; 417 head = head.getNext()) { 418 os.write(head.getBuffer(), 0, head.getLength()); 419 } 420 421 TempBuffer.freeAll(_tempStream.getHead()); 422 } 423 } 424 | Popular Tags |