KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lowagie > text > pdf > PdfFunction


1 /*
2  * Copyright 2002 Paulo Soares
3  *
4  * The contents of this file are subject to the Mozilla Public License Version 1.1
5  * (the "License"); you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
7  *
8  * Software distributed under the License is distributed on an "AS IS" basis,
9  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10  * for the specific language governing rights and limitations under the License.
11  *
12  * The Original Code is 'iText, a free JAVA-PDF library'.
13  *
14  * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
15  * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
16  * All Rights Reserved.
17  * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
18  * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
19  *
20  * Contributor(s): all the names of the contributors are added in the source code
21  * where applicable.
22  *
23  * Alternatively, the contents of this file may be used under the terms of the
24  * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
25  * provisions of LGPL are applicable instead of those above. If you wish to
26  * allow use of your version of this file only under the terms of the LGPL
27  * License and not to allow others to use your version of this file under
28  * the MPL, indicate your decision by deleting the provisions above and
29  * replace them with the notice and other provisions required by the LGPL.
30  * If you do not delete the provisions above, a recipient may use your version
31  * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
32  *
33  * This library is free software; you can redistribute it and/or modify it
34  * under the terms of the MPL as stated above or under the terms of the GNU
35  * Library General Public License as published by the Free Software Foundation;
36  * either version 2 of the License, or any later version.
37  *
38  * This library is distributed in the hope that it will be useful, but WITHOUT
39  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
40  * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
41  * details.
42  *
43  * If you didn't download this code from the following link, you should check if
44  * you aren't using an obsolete version:
45  * http://www.lowagie.com/iText/
46  */

47 package com.lowagie.text.pdf;
48
49 import java.io.IOException JavaDoc;
50
51 import com.lowagie.text.ExceptionConverter;
52 /** Implements PDF functions.
53  *
54  * @author Paulo Soares (psoares@consiste.pt)
55  */

56 public class PdfFunction {
57     
58     protected PdfWriter writer;
59     
60     protected PdfIndirectReference reference;
61     
62     protected PdfDictionary dictionary;
63     
64     /** Creates new PdfFunction */
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 JavaDoc 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 JavaDoc 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