001: /*
002: * Copyright (c) 2007, intarsys consulting GmbH
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * - Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * - 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: *
014: * - Neither the name of intarsys nor the names of its contributors may be used
015: * to endorse or promote products derived from this software without specific
016: * prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
020: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
021: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
022: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
023: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
024: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
025: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
026: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
027: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
028: * POSSIBILITY OF SUCH DAMAGE.
029: */
030: package de.intarsys.pdf.pd;
031:
032: import java.io.StringReader;
033: import java.util.ArrayList;
034: import java.util.List;
035: import de.intarsys.pdf.cos.COSDictionary;
036: import de.intarsys.pdf.cos.COSObject;
037: import de.intarsys.pdf.cos.COSStream;
038: import de.intarsys.pdf.encoding.PDFDocEncoding;
039: import de.intarsys.pdf.postscript.Handler;
040: import de.intarsys.pdf.postscript.ParseException;
041: import de.intarsys.pdf.postscript.Parser;
042:
043: /**
044: * A function implementation based on a "mini" postscript interpreter.
045: */
046: public class PDPostScriptFunction extends PDFunction {
047: /**
048: * The meta class implementation
049: */
050: static public class MetaClass extends PDFunction.MetaClass {
051: protected MetaClass(Class instanceClass) {
052: super (instanceClass);
053: }
054:
055: protected COSObject doCreateCOSObject() {
056: return COSStream.create(null);
057: }
058: }
059:
060: /** The meta class instance */
061: static public final MetaClass META = new MetaClass(MetaClass.class
062: .getDeclaringClass());
063:
064: private String code;
065:
066: protected PDPostScriptFunction(COSObject object) {
067: super (object);
068: }
069:
070: /*
071: * (non-Javadoc)
072: *
073: * @see de.intarsys.pdf.cos.COSBasedObject#cosGetDict()
074: */
075: public COSDictionary cosGetDict() {
076: return cosGetStream().getDict();
077: }
078:
079: public float[] evaluate(float[] values) {
080: Handler handler;
081: List argList;
082: List resultList;
083: float[] result;
084:
085: handler = new Handler();
086: argList = new ArrayList(values.length);
087: for (int index = 0; index < values.length; index++) {
088: argList.add(new Double(values[index]));
089: }
090: handler.pushArgs(argList);
091: try {
092: new Parser(new StringReader(getCode())).parse(handler);
093: } catch (ParseException ex) {
094: // TODO warning?
095: return dummyResult();
096: } catch (UnsupportedOperationException ex) {
097: // postscript is only partially implemented; do the same as when it
098: // wasn't implemented at all
099: return dummyResult();
100: }
101: resultList = handler.popResult();
102:
103: result = new float[getOutputSize()];
104: for (int index = 0; index < result.length; index++) {
105: result[index] = ((Number) resultList.get(index))
106: .floatValue();
107: }
108: return result;
109: }
110:
111: public String getCode() {
112: if (code == null) {
113: byte[] bytes;
114: String string;
115:
116: bytes = cosGetStream().getDecodedBytes();
117: string = PDFDocEncoding.UNIQUE.decode(bytes);
118: code = string.substring(string.indexOf('{') + 1, string
119: .lastIndexOf('}') - 1);
120: }
121: return code;
122: }
123:
124: public int getOutputSize() {
125: return getRange().size() / 2;
126: }
127: }
|