1 17 18 19 20 package org.apache.fop.render.afp.modca; 21 22 import java.awt.Color ; 23 import java.io.ByteArrayOutputStream ; 24 import java.io.IOException ; 25 import java.io.OutputStream ; 26 27 import org.apache.fop.render.afp.tools.BinaryUtils; 28 29 49 public class PresentationTextData extends AbstractAFPObject { 50 51 54 private static final int MAX_SIZE = 8192; 55 56 59 private ByteArrayOutputStream _baos = new ByteArrayOutputStream (1024); 60 61 64 private int _currentXCoordinate = -1; 65 66 69 private int _currentYCoordinate = -1; 70 71 74 private String _currentFont = ""; 75 76 79 private int _currentOrientation = 0; 80 81 84 private Color _currentColor = new Color (0, 0, 0); 85 86 89 private int _currentVariableSpaceCharacterIncrement = 0; 90 91 94 private int _currentInterCharacterAdjustment = 0; 95 96 99 public PresentationTextData() { 100 101 this(false); 102 103 } 104 105 113 public PresentationTextData(boolean controlInd) { 114 115 _baos.write(new byte[] { 0x5A, 0x00, 0x00, (byte) 0xD3, (byte) 0xEE, (byte) 0x9B, 0x00, 0x00, 0x00, }, 0, 9); 125 126 if (controlInd) { 127 _baos.write(new byte[] { 0x2B, (byte) 0xD3 }, 0, 2); 128 } 129 130 } 131 132 142 private void setCodedFont(byte font, ByteArrayOutputStream afpdata) { 143 144 if (String.valueOf(font).equals(_currentFont)) { 146 return; 147 } else { 148 _currentFont = String.valueOf(font); 149 } 150 151 afpdata.write(new byte[] { 0x03, (byte) 0xF1, font, }, 0, 3); 152 153 } 154 155 165 private void absoluteMoveInline(int coordinate, 166 ByteArrayOutputStream afpdata) { 167 168 byte[] b = BinaryUtils.convert(coordinate, 2); 169 170 afpdata.write(new byte[] { 0x04, (byte) 0xC7, b[0], b[1], }, 0, 4); 171 172 _currentXCoordinate = coordinate; 173 174 } 175 176 186 private void absoluteMoveBaseline(int coordinate, 187 ByteArrayOutputStream afpdata) { 188 189 byte[] b = BinaryUtils.convert(coordinate, 2); 190 191 afpdata.write(new byte[] { 0x04, (byte) 0xD3, b[0], b[1], }, 0, 4); 192 193 _currentYCoordinate = coordinate; 194 195 } 196 197 206 private void addTransparentData(byte[] data, ByteArrayOutputStream afpdata) { 207 208 int l = data.length + 2; 210 211 if (l > 255) { 212 throw new IllegalArgumentException ( 214 "Transparent data is longer than 253 bytes: " + data); 215 } 216 217 afpdata.write(new byte[] { BinaryUtils.convert(l)[0], (byte) 0xDB, }, 218 0, 2); 219 220 afpdata.write(data, 0, data.length); 221 222 } 223 224 236 private void drawBaxisRule(int length, int width, 237 ByteArrayOutputStream afpdata) { 238 239 afpdata.write(new byte[] { 0x07, (byte) 0xE7, }, 0, 2); 242 243 byte[] data1 = BinaryUtils.shortToByteArray((short) length); 245 afpdata.write(data1, 0, data1.length); 246 byte[] data2 = BinaryUtils.shortToByteArray((short) width); 248 afpdata.write(data2, 0, data2.length); 249 afpdata.write(0x00); 251 252 } 253 254 266 private void drawIaxisRule(int length, int width, 267 ByteArrayOutputStream afpdata) { 268 269 afpdata.write(new byte[] { 0x07, (byte) 0xE5, }, 0, 2); 272 273 byte[] data1 = BinaryUtils.shortToByteArray((short) length); 275 afpdata.write(data1, 0, data1.length); 276 byte[] data2 = BinaryUtils.shortToByteArray((short) width); 278 afpdata.write(data2, 0, data2.length); 279 afpdata.write(0x00); 281 282 } 283 284 305 public void createTextData(int fontNumber, int x, int y, int orientation, 306 Color col, int vsci, int ica, byte[] data) 307 throws MaximumSizeExceededException { 308 309 ByteArrayOutputStream afpdata = new ByteArrayOutputStream (); 310 311 if (_currentOrientation != orientation) { 312 setTextOrientation(orientation, afpdata); 313 _currentOrientation = orientation; 314 _currentXCoordinate = -1; 315 _currentYCoordinate = -1; 316 } 317 318 if (y != _currentYCoordinate) { 320 absoluteMoveBaseline(y, afpdata); 321 _currentXCoordinate = -1; 322 } 323 324 if (x != _currentXCoordinate) { 326 absoluteMoveInline(x, afpdata); 327 } 328 329 if (vsci != _currentVariableSpaceCharacterIncrement) { 331 setVariableSpaceCharacterIncrement(vsci, afpdata); 332 _currentVariableSpaceCharacterIncrement = vsci; 333 } 334 335 if (ica != _currentInterCharacterAdjustment) { 337 setInterCharacterAdjustment(ica, afpdata); 338 _currentInterCharacterAdjustment = ica; 339 } 340 341 if (!col.equals(_currentColor)) { 343 setExtendedTextColor(col, afpdata); 344 _currentColor = col; 345 } 346 347 setCodedFont(BinaryUtils.convert(fontNumber)[0], afpdata); 348 addTransparentData(data, afpdata); 349 _currentXCoordinate = -1; 350 351 int s = afpdata.size(); 352 353 if (_baos.size() + s > MAX_SIZE) { 354 _currentXCoordinate = -1; 355 _currentYCoordinate = -1; 356 throw new MaximumSizeExceededException(); 357 } 358 359 byte[] outputdata = afpdata.toByteArray(); 360 _baos.write(outputdata, 0, outputdata.length); 361 362 } 363 364 383 public void createLineData(int x1, int y1, int x2, int y2, int thickness, 384 int orientation, Color col) throws MaximumSizeExceededException { 385 386 ByteArrayOutputStream afpdata = new ByteArrayOutputStream (); 387 388 if (_currentOrientation != orientation) { 389 setTextOrientation(orientation, afpdata); 390 _currentOrientation = orientation; 391 } 392 393 if (y1 != _currentYCoordinate) { 395 absoluteMoveBaseline(y1, afpdata); 396 } 397 398 if (x1 != _currentXCoordinate) { 400 absoluteMoveInline(x1, afpdata); 401 } 402 403 if (!col.equals(_currentColor)) { 404 setExtendedTextColor(col, afpdata); 405 _currentColor = col; 406 } 407 408 if (y1 == y2) { 409 drawIaxisRule(x2 - x1, thickness, afpdata); 410 } else if (x1 == x2) { 411 drawBaxisRule(y2 - y1, thickness, afpdata); 412 } else { 413 return; 414 } 415 416 int s = afpdata.size(); 417 418 if (_baos.size() + s > MAX_SIZE) { 419 _currentXCoordinate = -1; 420 _currentYCoordinate = -1; 421 throw new MaximumSizeExceededException(); 422 } 423 424 byte[] outputdata = afpdata.toByteArray(); 425 _baos.write(outputdata, 0, outputdata.length); 426 427 } 428 429 443 private void setTextOrientation(int orientation, 444 ByteArrayOutputStream afpdata) { 445 446 afpdata.write(new byte[] { 0x06, (byte) 0xF7, }, 0, 2); 447 448 switch (orientation) { 449 case 90: 450 afpdata.write(0x2D); 451 afpdata.write(0x00); 452 afpdata.write(0x5A); 453 afpdata.write(0x00); 454 break; 455 case 180: 456 afpdata.write(0x5A); 457 afpdata.write(0x00); 458 afpdata.write(0x87); 459 afpdata.write(0x00); 460 break; 461 case 270: 462 afpdata.write(0x87); 463 afpdata.write(0x00); 464 afpdata.write(0x00); 465 afpdata.write(0x00); 466 break; 467 default: 468 afpdata.write(0x00); 469 afpdata.write(0x00); 470 afpdata.write(0x2D); 471 afpdata.write(0x00); 472 break; 473 } 474 475 } 476 477 488 private void setExtendedTextColor(Color col, 489 ByteArrayOutputStream afpdata) { 490 491 afpdata.write(new byte[] { 492 15 , (byte)0x81 , 0x00 , 0x01 , 0x00 , 0x00 , 0x00 , 0x00 , 8 , 8 , 8 , 0 , (byte)(col.getRed()) , (byte)(col.getGreen()) , (byte)(col.getBlue()) }, 0, 15); 508 509 } 510 511 520 private void setVariableSpaceCharacterIncrement(int incr, 521 ByteArrayOutputStream afpdata) { 522 523 byte[] b = BinaryUtils.convert(incr, 2); 524 525 afpdata.write(new byte[] { 526 4 , (byte)0xC5 , b[0] 529 , b[1] 530 }, 0, 4); 531 532 } 533 534 543 private void setInterCharacterAdjustment(int incr, 544 ByteArrayOutputStream afpdata) { 545 546 byte[] b = BinaryUtils.convert(Math.abs(incr), 2); 547 548 afpdata.write(new byte[] { 549 5 , (byte)0xC3 , b[0] 552 , b[1] 553 , (byte)(incr >= 0 ? 0 : 1) }, 0, 5); 555 556 } 557 558 564 public void writeDataStream(OutputStream os) 565 throws IOException { 566 567 byte[] data = _baos.toByteArray(); 568 byte[] size = BinaryUtils.convert(data.length - 1, 2); 569 data[1] = size[0]; 570 data[2] = size[1]; 571 572 os.write(data); 573 574 } 575 576 585 public void endControlSequence() throws MaximumSizeExceededException { 586 587 byte[] data = new byte[2]; 588 data[0] = 0x02; 589 data[1] = (byte) 0xF8; 590 591 if (data.length + _baos.size() > MAX_SIZE) { 592 throw new MaximumSizeExceededException(); 593 } 594 595 _baos.write(data, 0, data.length); 596 597 } 598 599 } | Popular Tags |