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 de.intarsys.pdf.cos.COSArray;
033: import de.intarsys.pdf.cos.COSFixed;
034: import de.intarsys.pdf.cos.COSName;
035: import de.intarsys.pdf.cos.COSNull;
036: import de.intarsys.pdf.cos.COSNumber;
037: import de.intarsys.pdf.cos.COSObject;
038:
039: /**
040: * The explicit reference to a destination in a PDF document, consisting of a
041: * page and a definition of the rectangle to be displayed.
042: */
043: public class PDExplicitDestination extends PDDestination {
044: /**
045: * The meta class implementation
046: */
047: public static class MetaClass extends PDDestination.MetaClass {
048: protected MetaClass(Class instanceClass) {
049: super (instanceClass);
050: }
051: }
052:
053: /** The meta class instance */
054: public static final MetaClass META = new MetaClass(MetaClass.class
055: .getDeclaringClass());
056:
057: public static final COSName CN_DISPLAY_MODE_XYZ = COSName
058: .constant("XYZ");
059:
060: public static final COSName CN_DISPLAY_MODE_Fit = COSName
061: .constant("Fit");
062:
063: public static final COSName CN_DISPLAY_MODE_FitH = COSName
064: .constant("FitH");
065:
066: public static final COSName CN_DISPLAY_MODE_FitV = COSName
067: .constant("FitV");
068:
069: public static final COSName CN_DISPLAY_MODE_FitR = COSName
070: .constant("FitR");
071:
072: public static final COSName CN_DISPLAY_MODE_FitB = COSName
073: .constant("FitB");
074:
075: public static final COSName CN_DISPLAY_MODE_FitBH = COSName
076: .constant("FitBH");
077:
078: public static final COSName CN_DISPLAY_MODE_FitBV = COSName
079: .constant("FitBV");
080:
081: protected PDExplicitDestination(COSObject object) {
082: super (object);
083: }
084:
085: public COSName getDisplayMode() {
086: COSArray definition = cosGetArray();
087: if (definition.size() < 2) {
088: return null;
089: }
090: return definition.get(1).asName();
091: }
092:
093: /**
094: * The destination page. ATTENTION: it is common have dangling destinations
095: * to invalid (null) pages around!
096: *
097: * @return The destination page. Be sure to handle null return values.
098: */
099: public PDPage getPage(PDDocument doc) {
100: COSArray definition = cosGetArray();
101: COSObject page = definition.get(0);
102: if (page.asNumber() != null) {
103: int pageIndex = page.asNumber().intValue();
104: return doc.getPageTree().getPageAt(pageIndex);
105: }
106: if (page.asDictionary() != null) {
107: return (PDPage) PDPageNode.META.createFromCos(page
108: .asDictionary());
109: }
110: return null;
111: }
112:
113: public void setPage(PDPage page) {
114: COSArray definition = cosGetArray();
115: if (definition.size() == 0) {
116: definition.add(page.cosGetObject());
117: } else {
118: definition.set(0, page.cosGetObject());
119: }
120: }
121:
122: public void setDisplayMode(COSName mode) {
123: COSArray definition = cosGetArray();
124: if (definition.size() == 0) {
125: definition.add(COSNull.NULL);
126: definition.add(mode);
127: } else if (definition.size() == 1) {
128: definition.add(mode);
129: } else {
130: definition.set(1, mode);
131: }
132: }
133:
134: public float[] getParameters() {
135: COSArray definition = cosGetArray();
136: int size = definition.size() - 2;
137: if (size < 0) {
138: return new float[0];
139: }
140: float[] result = new float[size];
141: for (int i = 0; i < size; i++) {
142: COSNumber param = definition.get(i + 2).asNumber();
143: if (param == null) {
144: result[i] = 0;
145: } else {
146: result[i] = param.floatValue();
147: }
148: }
149: return result;
150: }
151:
152: public void setParameters(float[] parameters) {
153: COSArray definition = cosGetArray();
154: while (definition.size() < (2 + parameters.length)) {
155: definition.add(COSNull.NULL);
156: }
157: for (int i = 2; i < (2 + parameters.length); i++) {
158: definition.set(i, COSFixed.create(parameters[i]));
159: }
160: }
161:
162: public void setParameters(double[] parameters) {
163: COSArray definition = cosGetArray();
164: while (definition.size() < (2 + parameters.length)) {
165: definition.add(COSNull.NULL);
166: }
167: for (int i = 0; i < parameters.length; i++) {
168: definition.set(i + 2, COSFixed.create(parameters[i]));
169: }
170: }
171:
172: /*
173: * (non-Javadoc)
174: *
175: * @see de.intarsys.pdf.pd.PDDestination#getResolvedDestination(de.intarsys.pdf.pd.PDDoc)
176: */
177: public PDExplicitDestination getResolvedDestination(PDDocument doc) {
178: return this ;
179: }
180:
181: /*
182: * (non-Javadoc)
183: *
184: * @see de.intarsys.pdf.cos.COSBasedObject#initializeFromScratch()
185: */
186: protected void initializeFromScratch() {
187: // TODO Auto-generated method stub
188: super.initializeFromScratch();
189: }
190: }
|