1 47 package com.lowagie.text.pdf; 48 49 import java.io.IOException ; 50 51 import com.lowagie.text.ExceptionConverter; 52 56 public class PdfFunction { 57 58 protected PdfWriter writer; 59 60 protected PdfIndirectReference reference; 61 62 protected PdfDictionary dictionary; 63 64 65 protected PdfFunction(PdfWriter writer) { 66 this.writer = writer; 67 } 68 69 PdfIndirectReference getReference() { 70 try { 71 if (reference == null) { 72 reference = writer.addToBody(dictionary).getIndirectReference(); 73 } 74 } 75 catch (IOException ioe) { 76 throw new ExceptionConverter(ioe); 77 } 78 return reference; 79 } 80 81 public static PdfFunction type0(PdfWriter writer, float domain[], float range[], int size[], 82 int bitsPerSample, int order, float encode[], float decode[], byte stream[]) { 83 PdfFunction func = new PdfFunction(writer); 84 func.dictionary = new PdfStream(stream); 85 ((PdfStream)func.dictionary).flateCompress(); 86 func.dictionary.put(PdfName.FUNCTIONTYPE, new PdfNumber(0)); 87 func.dictionary.put(PdfName.DOMAIN, new PdfArray(domain)); 88 func.dictionary.put(PdfName.RANGE, new PdfArray(range)); 89 func.dictionary.put(PdfName.SIZE, new PdfArray(size)); 90 func.dictionary.put(PdfName.BITSPERSAMPLE, new PdfNumber(bitsPerSample)); 91 if (order != 1) 92 func.dictionary.put(PdfName.ORDER, new PdfNumber(order)); 93 if (encode != null) 94 func.dictionary.put(PdfName.ENCODE, new PdfArray(encode)); 95 if (decode != null) 96 func.dictionary.put(PdfName.DECODE, new PdfArray(decode)); 97 return func; 98 } 99 100 public static PdfFunction type2(PdfWriter writer, float domain[], float range[], float c0[], float c1[], float n) { 101 PdfFunction func = new PdfFunction(writer); 102 func.dictionary = new PdfDictionary(); 103 func.dictionary.put(PdfName.FUNCTIONTYPE, new PdfNumber(2)); 104 func.dictionary.put(PdfName.DOMAIN, new PdfArray(domain)); 105 if (range != null) 106 func.dictionary.put(PdfName.RANGE, new PdfArray(range)); 107 if (c0 != null) 108 func.dictionary.put(PdfName.C0, new PdfArray(c0)); 109 if (c1 != null) 110 func.dictionary.put(PdfName.C1, new PdfArray(c1)); 111 func.dictionary.put(PdfName.N, new PdfNumber(n)); 112 return func; 113 } 114 115 public static PdfFunction type3(PdfWriter writer, float domain[], float range[], PdfFunction functions[], float bounds[], float encode[]) { 116 PdfFunction func = new PdfFunction(writer); 117 func.dictionary = new PdfDictionary(); 118 func.dictionary.put(PdfName.FUNCTIONTYPE, new PdfNumber(3)); 119 func.dictionary.put(PdfName.DOMAIN, new PdfArray(domain)); 120 if (range != null) 121 func.dictionary.put(PdfName.RANGE, new PdfArray(range)); 122 PdfArray array = new PdfArray(); 123 for (int k = 0; k < functions.length; ++k) 124 array.add(functions[k].getReference()); 125 func.dictionary.put(PdfName.FUNCTIONS, array); 126 func.dictionary.put(PdfName.BOUNDS, new PdfArray(bounds)); 127 func.dictionary.put(PdfName.ENCODE, new PdfArray(encode)); 128 return func; 129 } 130 131 public static PdfFunction type4(PdfWriter writer, float domain[], float range[], String postscript) { 132 byte b[] = new byte[postscript.length()]; 133 for (int k = 0; k < b.length; ++k) 134 b[k] = (byte)postscript.charAt(k); 135 PdfFunction func = new PdfFunction(writer); 136 func.dictionary = new PdfStream(b); 137 ((PdfStream)func.dictionary).flateCompress(); 138 func.dictionary.put(PdfName.FUNCTIONTYPE, new PdfNumber(4)); 139 func.dictionary.put(PdfName.DOMAIN, new PdfArray(domain)); 140 func.dictionary.put(PdfName.RANGE, new PdfArray(range)); 141 return func; 142 } 143 } 144 | Popular Tags |