001: /**
002: * Copyright (c) 2006, www.pdfbox.org
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions are met:
007: *
008: * 1. Redistributions of source code must retain the above copyright notice,
009: * this list of conditions and the following disclaimer.
010: * 2. Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: * 3. Neither the name of pdfbox; nor the names of its
014: * contributors may be used to endorse or promote products derived from this
015: * software without specific prior written permission.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
018: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
019: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
020: * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
021: * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
022: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
023: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
024: * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
025: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
026: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027: *
028: * http://www.pdfbox.org
029: *
030: */package org.pdfbox.pdmodel.common.function;
031:
032: import org.pdfbox.cos.COSArray;
033: import org.pdfbox.cos.COSBase;
034: import org.pdfbox.cos.COSDictionary;
035: import org.pdfbox.cos.COSFloat;
036: import org.pdfbox.cos.COSName;
037: import org.pdfbox.pdmodel.common.PDRange;
038:
039: /**
040: * This class represents a function in a PDF document.
041: *
042: * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a>
043: * @version $Revision: 1.3 $
044: */
045: public abstract class PDDictionaryFunction extends PDFunction {
046: private COSDictionary function = null;
047:
048: /**
049: * Constructor to create a new blank function, should only be called by
050: * subclasses.
051: *
052: * @param functionType An integer describing the function type, only 0,2,3,4
053: * are defined by the PDF sepc.
054: */
055: protected PDDictionaryFunction(int functionType) {
056: function = new COSDictionary();
057: function.setInt("FunctionType", functionType);
058: }
059:
060: /**
061: * Constructor.
062: *
063: * @param functionDictionary The prepopulated function dictionary.
064: */
065: public PDDictionaryFunction(COSDictionary functionDictionary) {
066: function = functionDictionary;
067: }
068:
069: /**
070: * {@inheritDoc}
071: */
072: public COSBase getCOSObject() {
073: return function;
074: }
075:
076: /**
077: * Get the underlying cos dictionary.
078: *
079: * @return The underlying cos dictionary.
080: */
081: public COSDictionary getCOSDictionary() {
082: return function;
083: }
084:
085: private COSArray getRangeArray(String fieldName, int n) {
086: COSArray rangeArray = (COSArray) function
087: .getDictionaryObject(COSName.getPDFName("Range"));
088: if (rangeArray == null) {
089: rangeArray = new COSArray();
090: function.setItem(fieldName, rangeArray);
091: while (rangeArray.size() < n * 2) {
092: rangeArray.add(new COSFloat(0));
093: rangeArray.add(new COSFloat(0));
094: }
095: }
096: return rangeArray;
097: }
098:
099: /**
100: * {@inheritDoc}
101: */
102: public int getNumberOfOutputParameters() {
103: COSArray array = getRangeArray("Range", 0);
104: return array.size() / 2;
105: }
106:
107: /**
108: * {@inheritDoc}
109: */
110: public PDRange getRangeForOutput(int n) {
111: COSArray rangeArray = getRangeArray("Range", n);
112: return new PDRange(rangeArray, n);
113: }
114:
115: /**
116: * {@inheritDoc}
117: */
118: public void setRangeForOutput(PDRange range, int n) {
119: COSArray rangeArray = getRangeArray("Range", n);
120: rangeArray.set(n * 2, new COSFloat(range.getMin()));
121: rangeArray.set(n * 2 + 1, new COSFloat(range.getMax()));
122: }
123:
124: /**
125: * {@inheritDoc}
126: */
127: public int getNumberOfInputParameters() {
128: COSArray array = getRangeArray("Domain", 0);
129: return array.size() / 2;
130: }
131:
132: /**
133: * {@inheritDoc}
134: */
135: public PDRange getDomainForInput(int n) {
136: COSArray rangeArray = getRangeArray("Domain", n);
137: return new PDRange(rangeArray, n);
138: }
139:
140: /**
141: * {@inheritDoc}
142: */
143: public void setDomainForInput(PDRange range, int n) {
144: COSArray rangeArray = getRangeArray("Domain", n);
145: rangeArray.set(n * 2, new COSFloat(range.getMin()));
146: rangeArray.set(n * 2 + 1, new COSFloat(range.getMax()));
147: }
148: }
|