1 51 package org.apache.fop.pdf; 52 53 import java.util.List ; 55 import java.io.UnsupportedEncodingException ; 56 57 67 public class PDFFunction extends PDFObject { 68 70 73 protected int functionType = 0; 75 78 protected List domain = null; 79 80 83 protected List range = null; 84 85 86 88 93 protected List size = null; 94 95 98 protected int bitsPerSample = 1; 99 100 103 protected int order = 1; 104 105 111 protected List encode = null; 112 113 116 protected List decode = null; 117 118 121 122 125 protected StringBuffer functionDataStream = null; 126 127 131 protected List filter = null; 132 133 134 137 protected List cZero = null; 138 139 142 protected List cOne = null; 143 144 149 protected double interpolationExponentN = 1; 150 151 152 153 156 protected List functions = null; 157 158 165 protected List bounds = null; 166 168 169 171 227 public PDFFunction(int theNumber, int theFunctionType, List theDomain, 228 List theRange, List theSize, int theBitsPerSample, 229 int theOrder, List theEncode, List theDecode, 230 StringBuffer theFunctionDataStream, List theFilter) { 231 super(theNumber); 232 233 this.functionType = 0; this.size = theSize; 235 this.bitsPerSample = theBitsPerSample; 236 this.order = theOrder; this.encode = theEncode; this.decode = theDecode; this.functionDataStream = theFunctionDataStream; 240 this.filter = theFilter; 242 this.domain = theDomain; 246 this.range = theRange; 247 } 248 249 277 public PDFFunction(int theNumber, int theFunctionType, List theDomain, 278 List theRange, List theCZero, List theCOne, 279 double theInterpolationExponentN) { 280 super(theNumber); 281 282 this.functionType = 2; 284 this.cZero = theCZero; 285 this.cOne = theCOne; 286 this.interpolationExponentN = theInterpolationExponentN; 287 288 289 this.domain = theDomain; 290 this.range = theRange; 291 292 } 293 294 330 public PDFFunction(int theNumber, int theFunctionType, List theDomain, 331 List theRange, List theFunctions, 332 List theBounds, List theEncode) { 333 super(theNumber); 334 335 this.functionType = 3; 337 this.functions = theFunctions; 338 this.bounds = theBounds; 339 this.encode = theEncode; 340 this.domain = theDomain; 341 this.range = theRange; 342 343 } 344 345 367 public PDFFunction(int theNumber, int theFunctionType, List theDomain, 368 List theRange, StringBuffer theFunctionDataStream) { 369 super(theNumber); 370 371 this.functionType = 4; this.functionDataStream = theFunctionDataStream; 373 374 this.domain = theDomain; 375 376 this.range = theRange; 377 378 } 379 380 381 392 public byte[] toPDF() { 393 int vectorSize = 0; 394 int numberOfFunctions = 0; 395 int tempInt = 0; 396 StringBuffer p = new StringBuffer (); 397 p.append(this.number + " " + this.generation 398 + " obj\n<< \n/FunctionType " + this.functionType + " \n"); 399 400 if (this.functionType == 0) { 402 if (this.domain != null) { 403 p.append("/Domain [ "); 405 vectorSize = this.domain.size(); 406 for (tempInt = 0; tempInt < vectorSize; tempInt++) { 407 p.append(PDFNumber.doubleOut((Double )this.domain.get(tempInt)) 408 + " "); 409 } 410 411 p.append("] \n"); 412 } else { 413 p.append("/Domain [ 0 1 ] \n"); 414 } 415 416 if (this.size != null) { 418 p.append("/Size [ "); 419 vectorSize = this.size.size(); 420 for (tempInt = 0; tempInt < vectorSize; tempInt++) { 421 p.append(PDFNumber.doubleOut((Double )this.size.get(tempInt)) 422 + " "); 423 } 424 p.append("] \n"); 425 } 426 if (this.encode != null) { 428 p.append("/Encode [ "); 429 vectorSize = this.encode.size(); 430 for (tempInt = 0; tempInt < vectorSize; tempInt++) { 431 p.append(PDFNumber.doubleOut((Double )this.encode.get(tempInt)) 432 + " "); 433 } 434 p.append("] \n"); 435 } else { 436 p.append("/Encode [ "); 437 vectorSize = this.functions.size(); 438 for (tempInt = 0; tempInt < vectorSize; tempInt++) { 439 p.append("0 1 "); 440 } 441 p.append("] \n"); 442 443 } 444 445 p.append("/BitsPerSample " + this.bitsPerSample); 447 448 if (this.order == 1 || this.order == 3) { 450 p.append(" \n/Order " + this.order + " \n"); 451 } 452 453 if (this.range != null) { 455 p.append("/Range [ "); 456 vectorSize = this.range.size(); 457 for (tempInt = 0; tempInt < vectorSize; tempInt++) { 458 p.append(PDFNumber.doubleOut((Double )this.range.get(tempInt)) 459 + " "); 460 } 461 462 p.append("] \n"); 463 } 464 465 if (this.decode != null) { 467 p.append("/Decode [ "); 468 vectorSize = this.decode.size(); 469 for (tempInt = 0; tempInt < vectorSize; tempInt++) { 470 p.append(PDFNumber.doubleOut((Double )this.decode.get(tempInt)) 471 + " "); 472 } 473 474 p.append("] \n"); 475 } 476 477 if (this.functionDataStream != null) { 479 p.append("/Length " + (this.functionDataStream.length() + 1) 480 + " \n"); 481 } 482 483 if (this.filter != null) { vectorSize = this.filter.size(); 486 p.append("/Filter "); 487 if (vectorSize == 1) { 488 p.append("/" + ((String )this.filter.get(0)) 489 + " \n"); 490 } else { 491 p.append("[ "); 492 for (tempInt = 0; tempInt < vectorSize; tempInt++) { 493 p.append("/" + ((String )this.filter.get(0)) 494 + " "); 495 } 496 p.append("] \n"); 497 } 498 } 499 p.append(">> \n"); 500 501 if (this.functionDataStream != null) { 503 p.append("stream\n" + this.functionDataStream 504 + "\nendstream\n"); 505 } 506 507 p.append("endobj\n"); 508 509 } else if (this.functionType == 2) { 511 if (this.domain != null) { 513 p.append("/Domain [ "); 514 vectorSize = this.domain.size(); 515 for (tempInt = 0; tempInt < vectorSize; tempInt++) { 516 p.append(PDFNumber.doubleOut((Double )this.domain.get(tempInt)) 517 + " "); 518 } 519 520 p.append("] \n"); 521 } else { 522 p.append("/Domain [ 0 1 ] \n"); 523 } 524 525 526 if (this.range != null) { 528 p.append("/Range [ "); 529 vectorSize = this.range.size(); 530 for (tempInt = 0; tempInt < vectorSize; tempInt++) { 531 p.append(PDFNumber.doubleOut((Double )this.range.get(tempInt)) 532 + " "); 533 } 534 535 p.append("] \n"); 536 } 537 538 540 if (this.cZero != null) { 542 p.append("/C0 [ "); 543 vectorSize = this.cZero.size(); 544 for (tempInt = 0; tempInt < vectorSize; tempInt++) { 545 p.append(PDFNumber.doubleOut((Double )this.cZero.get(tempInt)) 546 + " "); 547 } 548 p.append("] \n"); 549 } 550 551 if (this.cOne != null) { 553 p.append("/C1 [ "); 554 vectorSize = this.cOne.size(); 555 for (tempInt = 0; tempInt < vectorSize; tempInt++) { 556 p.append(PDFNumber.doubleOut((Double )this.cOne.get(tempInt)) 557 + " "); 558 } 559 p.append("] \n"); 560 } 561 562 p.append("/N " 564 + PDFNumber.doubleOut(new Double (this.interpolationExponentN)) 565 + " \n"); 566 567 p.append(">> \nendobj\n"); 568 569 } else if (this.functionType 570 == 3) { if (this.domain != null) { 573 p.append("/Domain [ "); 574 vectorSize = this.domain.size(); 575 for (tempInt = 0; tempInt < vectorSize; tempInt++) { 576 p.append(PDFNumber.doubleOut((Double )this.domain.get(tempInt)) 577 + " "); 578 } 579 p.append("] \n"); 580 } else { 581 p.append("/Domain [ 0 1 ] \n"); 582 } 583 584 if (this.range != null) { 586 p.append("/Range [ "); 587 vectorSize = this.range.size(); 588 for (tempInt = 0; tempInt < vectorSize; tempInt++) { 589 p.append(PDFNumber.doubleOut((Double )this.range.get(tempInt)) 590 + " "); 591 } 592 593 p.append("] \n"); 594 } 595 596 if (this.functions != null) { 598 p.append("/Functions [ "); 599 numberOfFunctions = this.functions.size(); 600 for (tempInt = 0; tempInt < numberOfFunctions; tempInt++) { 601 p.append(((PDFFunction)this.functions.get(tempInt)).referencePDF() 602 + " "); 603 604 } 605 p.append("] \n"); 606 } 607 608 609 if (this.encode != null) { 611 p.append("/Encode [ "); 612 vectorSize = this.encode.size(); 613 for (tempInt = 0; tempInt < vectorSize; tempInt++) { 614 p.append(PDFNumber.doubleOut((Double )this.encode.get(tempInt)) 615 + " "); 616 } 617 618 p.append("] \n"); 619 } else { 620 p.append("/Encode [ "); 621 vectorSize = this.functions.size(); 622 for (tempInt = 0; tempInt < vectorSize; tempInt++) { 623 p.append("0 1 "); 624 } 625 p.append("] \n"); 626 627 } 628 629 630 p.append("/Bounds [ "); 632 if (this.bounds != null) { 633 634 vectorSize = this.bounds.size(); 635 for (tempInt = 0; tempInt < vectorSize; tempInt++) { 636 p.append(PDFNumber.doubleOut((Double )this.bounds.get(tempInt)) 637 + " "); 638 } 639 640 } else { 641 if (this.functions != null) { 642 647 String functionsFraction = PDFNumber.doubleOut(new Double (1.0 648 / ((double)numberOfFunctions))); 649 650 for (tempInt = 0; tempInt + 1 < numberOfFunctions; 651 tempInt++) { 652 653 p.append(functionsFraction + " "); 654 } 655 functionsFraction = null; 657 } 658 659 } 660 p.append("] \n"); 661 662 663 p.append(">> \nendobj\n"); 664 } else if (this.functionType 665 == 4) { if (this.domain != null) { 668 p.append("/Domain [ "); 669 vectorSize = this.domain.size(); 670 for (tempInt = 0; tempInt < vectorSize; tempInt++) { 671 p.append(PDFNumber.doubleOut((Double )this.domain.get(tempInt)) 672 + " "); 673 } 674 675 p.append("] \n"); 676 } else { 677 p.append("/Domain [ 0 1 ] \n"); 678 } 679 680 if (this.range != null) { 682 p.append("/Range [ "); 683 vectorSize = this.range.size(); 684 for (tempInt = 0; tempInt < vectorSize; tempInt++) { 685 p.append(PDFNumber.doubleOut((Double )this.range.get(tempInt)) 686 + " "); 687 } 688 689 p.append("] \n"); 690 } 691 692 if (this.functionDataStream != null) { 694 p.append("/Length " + (this.functionDataStream.length() + 1) 695 + " \n"); 696 } 697 698 p.append(">> \n"); 699 700 if (this.functionDataStream != null) { 702 p.append("stream\n{ " + this.functionDataStream 703 + " } \nendstream\n"); 704 } 705 706 p.append("endobj\n"); 707 708 } 709 710 try { 711 return p.toString().getBytes(PDFDocument.ENCODING); 712 } catch (UnsupportedEncodingException ue) { 713 return p.toString().getBytes(); 714 } 715 } 716 717 } 718 | Popular Tags |