1 17 18 19 20 package org.apache.fop.render.afp.modca; 21 22 import java.awt.Color ; 23 import java.io.IOException ; 24 import java.io.OutputStream ; 25 26 import org.apache.commons.logging.Log; 27 import org.apache.commons.logging.LogFactory; 28 import org.apache.fop.render.afp.fonts.AFPFont; 29 import org.apache.fop.render.afp.tools.StringUtils; 30 31 47 public class AFPDataStream { 48 49 52 protected static final Log log = LogFactory.getLog("org.apache.fop.render.afp.modca"); 53 54 57 private boolean _complete = false; 58 59 62 private String _producer = null; 63 64 67 private Document _document = null; 68 69 72 private PageGroup _currentPageGroup = null; 73 74 77 private PageObject _currentPageObject = null; 78 79 82 private Overlay _currentOverlay = null; 83 84 87 private AbstractPageObject _currentPage = null; 88 89 92 private int _pageCount = 0; 93 94 97 private int _pageGroupCount = 0; 98 99 102 private int _ovlCount = 0; 103 104 107 private int _portraitRotation = 0; 108 109 112 private int _landscapeRotation = 270; 113 114 117 private int _xOffset = 0; 118 119 122 private int _yOffset = 0; 123 124 127 private int _rotation; 128 129 132 private OutputStream _outputStream = null; 133 134 137 public AFPDataStream() { 138 } 139 140 144 public void startDocument(OutputStream outputStream) { 145 146 if (_document != null) { 147 String msg = "Invalid state - document already started."; 148 log.warn("startDocument():: " + msg); 149 throw new IllegalStateException (msg); 150 } 151 152 _document = new Document(); 153 _outputStream = outputStream; 154 155 } 156 157 162 public void endDocument() 163 throws IOException { 164 165 if (_complete) { 166 String msg = "Invalid state - document already ended."; 167 log.warn("endDocument():: " + msg); 168 throw new IllegalStateException (msg); 169 } 170 171 if (_currentPageObject != null) { 172 endPage(); 174 } 175 176 if (_currentPageGroup != null) { 177 endPageGroup(); 179 } 180 181 _document.endDocument(); 182 _document.writeDataStream(_outputStream); 183 _outputStream.flush(); 184 185 _complete = true; 186 187 _document = null; 188 189 _outputStream = null; 190 } 191 192 203 public void startPage(int pageWidth, int pageHeight, int pageRotation) { 204 205 String pageName = "PGN" 206 + StringUtils.lpad(String.valueOf(_pageCount++), '0', 5); 207 208 _currentPageObject = new PageObject(pageName, pageWidth, pageHeight, pageRotation); 209 _currentPage = _currentPageObject; 210 _currentOverlay = null; 211 setOffsets(0, 0, 0); 212 } 213 214 229 public void startOverlay(int overlayX, int overlayY, int overlayWidth, int overlayHeight, int overlayRotation) { 230 231 String overlayName = "OVL" 232 + StringUtils.lpad(String.valueOf(_ovlCount++), '0', 5); 233 234 _currentOverlay = new Overlay(overlayName, overlayWidth, overlayHeight, overlayRotation); 235 _currentPageObject.addOverlay(_currentOverlay); 236 _currentPageObject.createIncludePageOverlay(overlayName, overlayX, overlayY, 0); 237 _currentPage = _currentOverlay; 238 setOffsets(0, 0, 0); 239 } 240 241 244 public void endOverlay() { 245 246 _currentOverlay.endPage(); 247 _currentOverlay = null; 248 _currentPage = _currentPageObject; 249 250 } 251 252 255 public PageObject savePage() { 256 257 PageObject pageObject = _currentPageObject; 258 if (_currentPageGroup != null) { 259 _currentPageGroup.addPage(_currentPageObject); 260 } else { 261 _document.addPage(_currentPageObject); 262 } 263 _currentPageObject = null; 264 _currentPage = null; 265 return pageObject; 266 267 } 268 269 272 public void restorePage(PageObject pageObject) { 273 274 _currentPageObject = pageObject; 275 _currentPage = pageObject; 276 277 } 278 279 282 public void endPage() 283 throws IOException { 284 285 _currentPageObject.endPage(); 286 if (_currentPageGroup != null) { 287 _currentPageGroup.addPage(_currentPageObject); 288 } else { 289 _document.addPage(_currentPageObject); 290 _document.writeDataStream(_outputStream); 291 } 292 293 _currentPageObject = null; 294 _currentPage = null; 295 296 } 297 298 308 public void setOffsets(int xOffset, int yOffset, int rotation) { 309 _xOffset = xOffset; 310 _yOffset = yOffset; 311 _rotation = rotation; 312 } 313 314 326 public void createFont(byte fontReference, AFPFont font, int size) { 327 328 _currentPage.createFont(fontReference, font, size); 329 330 } 331 332 351 public void createText(int fontNumber, int x, int y, Color col, int vsci, int ica, byte[] data) { 352 353 _currentPage.createText(fontNumber, x + _xOffset, y + _yOffset, _rotation, col, vsci, ica, data); 354 355 } 356 357 369 public ImageObject getImageObject(int x, int y, int w, int h) { 370 371 int xOrigin; 372 int yOrigin; 373 int width; 374 int height; 375 switch (_rotation) { 376 case 90: 377 xOrigin = _currentPage.getWidth() - y - _yOffset; 378 yOrigin = x + _xOffset; 379 width = h; 380 height = w; 381 break; 382 case 180: 383 xOrigin = _currentPage.getWidth() - x - _xOffset; 384 yOrigin = _currentPage.getHeight() - y - _yOffset; 385 width = w; 386 height = h; 387 break; 388 case 270: 389 xOrigin = y + _yOffset; 390 yOrigin = _currentPage.getHeight() - x - _xOffset; 391 width = h; 392 height = w; 393 break; 394 default: 395 xOrigin = x + _xOffset; 396 yOrigin = y + _yOffset; 397 width = w; 398 height = h; 399 break; 400 } 401 ImageObject io = _currentPage.getImageObject(); 402 io.setImageViewport(xOrigin, yOrigin, width, height, _rotation); 403 return io; 404 405 } 406 407 423 public void createLine(int x1, int y1, int x2, int y2, int thickness, Color col) { 424 425 _currentPage.createLine(x1 + _xOffset, y1 + _yOffset, x2 + _xOffset, y2 + _yOffset, thickness, _rotation, col); 426 427 } 428 429 449 public void createShading(int x, int y, int w, int h, int red, int green, 450 int blue) { 451 452 _currentPage.createShading(x + _xOffset, y + _xOffset, w, h, red, green, blue); 453 454 } 455 456 463 public void createIncludePageOverlay(String name) { 464 465 _currentPageObject.createIncludePageOverlay(name, 0, 0, _rotation); 466 ActiveEnvironmentGroup aeg = _currentPageObject.getActiveEnvironmentGroup(); 467 aeg.createOverlay(name); 468 469 } 470 471 477 public void createInvokeMediumMap(String name) { 478 479 if (_currentPageGroup == null) { 480 startPageGroup(); 481 } 482 _currentPageGroup.createInvokeMediumMap(name); 483 484 } 485 486 496 public void createIncludePageSegment(String name, int x, int y) { 497 498 int xOrigin; 499 int yOrigin; 500 switch (_rotation) { 501 case 90: 502 xOrigin = _currentPage.getWidth() - y - _yOffset; 503 yOrigin = x + _xOffset; 504 break; 505 case 180: 506 xOrigin = _currentPage.getWidth() - x - _xOffset; 507 yOrigin = _currentPage.getHeight() - y - _yOffset; 508 break; 509 case 270: 510 xOrigin = y + _yOffset; 511 yOrigin = _currentPage.getHeight() - x - _xOffset; 512 break; 513 default: 514 xOrigin = x + _xOffset; 515 yOrigin = y + _yOffset; 516 break; 517 } 518 _currentPage.createIncludePageSegment(name, xOrigin, yOrigin); 519 520 } 521 522 528 public void createPageTagLogicalElement(TagLogicalElementBean[] attributes) { 529 530 for (int i = 0; i < attributes.length; i++) { 531 String name = (String ) attributes[i].getKey(); 532 String value = (String ) attributes[i].getValue(); 533 _currentPage.createTagLogicalElement(name, value); 534 } 535 536 } 537 538 544 public void createPageGroupTagLogicalElement( 545 TagLogicalElementBean[] attributes) { 546 547 for (int i = 0; i < attributes.length; i++) { 548 String name = (String ) attributes[i].getKey(); 549 String value = (String ) attributes[i].getValue(); 550 _currentPageGroup.createTagLogicalElement(name, value); 551 } 552 553 } 554 555 563 public void createTagLogicalElement(String name, String value) { 564 565 if (_currentPageGroup != null) { 566 _currentPageGroup.createTagLogicalElement(name, value); 567 } else { 568 _currentPage.createTagLogicalElement(name, value); 569 } 570 571 } 572 573 581 public void startPageGroup() { 582 583 String pageGroupName = "PGP" 584 + StringUtils.lpad(String.valueOf(_pageCount++), '0', 5); 585 586 _currentPageGroup = new PageGroup(pageGroupName); 587 588 } 589 590 593 public void endPageGroup() 594 throws IOException { 595 596 _currentPageGroup.endPageGroup(); 597 _document.addPageGroup(_currentPageGroup); 598 _document.writeDataStream(_outputStream); 599 _currentPageGroup = null; 600 601 } 602 603 610 public void setPortraitRotation(int rotation) { 611 612 if (rotation == 0 || rotation == 90 || rotation == 180 613 || rotation == 270) { 614 _portraitRotation = rotation; 615 } else { 616 throw new IllegalArgumentException ( 617 "The portrait rotation must be one of the values 0, 90, 180, 270"); 618 } 619 620 } 621 622 629 public void setLandscapeRotation(int rotation) { 630 631 if (rotation == 0 || rotation == 90 || rotation == 180 632 || rotation == 270) { 633 _landscapeRotation = rotation; 634 } else { 635 throw new IllegalArgumentException ( 636 "The landscape rotation must be one of the values 0, 90, 180, 270"); 637 } 638 639 } 640 641 } 642 | Popular Tags |