001: /*
002: Copyright © 2006,2007 Stefano Chizzolini. http://clown.stefanochizzolini.it
003:
004: Contributors:
005: * Stefano Chizzolini (original code developer, http://www.stefanochizzolini.it):
006: contributed code is Copyright © 2006,2007 by Stefano Chizzolini.
007:
008: This file should be part of the source code distribution of "PDF Clown library"
009: (the Program): see the accompanying README files for more info.
010:
011: This Program is free software; you can redistribute it and/or modify it under
012: the terms of the GNU General Public License as published by the Free Software
013: Foundation; either version 2 of the License, or (at your option) any later version.
014:
015: This Program is distributed in the hope that it will be useful, but WITHOUT ANY
016: WARRANTY, either expressed or implied; without even the implied warranty of
017: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the License for more details.
018:
019: You should have received a copy of the GNU General Public License along with this
020: Program (see README files); if not, go to the GNU website (http://www.gnu.org/).
021:
022: Redistribution and use, with or without modification, are permitted provided that such
023: redistributions retain the above copyright notice, license and disclaimer, along with
024: this list of conditions.
025: */
026:
027: package it.stefanochizzolini.clown.documents.contents.xObjects;
028:
029: import it.stefanochizzolini.clown.documents.Document;
030: import it.stefanochizzolini.clown.documents.contents.Contents;
031: import it.stefanochizzolini.clown.documents.contents.IContentContext;
032: import it.stefanochizzolini.clown.documents.contents.Resources;
033: import it.stefanochizzolini.clown.documents.contents.objects.ContentObject;
034: import it.stefanochizzolini.clown.files.File;
035: import it.stefanochizzolini.clown.objects.IPdfNumber;
036: import it.stefanochizzolini.clown.objects.PdfArray;
037: import it.stefanochizzolini.clown.objects.PdfDictionary;
038: import it.stefanochizzolini.clown.objects.PdfDirectObject;
039: import it.stefanochizzolini.clown.objects.PdfName;
040: import it.stefanochizzolini.clown.objects.PdfRectangle;
041: import it.stefanochizzolini.clown.objects.PdfReference;
042: import it.stefanochizzolini.clown.util.NotImplementedException;
043:
044: import java.awt.Dimension;
045: import java.awt.geom.Dimension2D;
046:
047: /**
048: Form external object [PDF:1.6:4.9].
049: @version 0.0.5
050: */
051: public class FormXObject extends XObject implements IContentContext {
052: // <class>
053: // <dynamic>
054: // <constructors>
055: /**
056: Creates a new form within the given document context, using default resources.
057: @version 0.0.5
058: */
059: public FormXObject(Document context) {
060: this (context, null);
061: }
062:
063: /**
064: Creates a new form within the given document context, using custom resources.
065: @version 0.0.5
066: @since 0.0.5
067: */
068: public FormXObject(Document context, Resources resources) {
069: super (context);
070:
071: PdfDictionary header = getBaseDataObject().getHeader();
072: header.put(PdfName.Subtype, PdfName.Form);
073: header.put(PdfName.BBox, new PdfRectangle(0, 0, 0, 0));
074:
075: // No resources collection?
076: /* NOTE: Resources collection is mandatory. */
077: if (resources == null) {
078: resources = new Resources(context);
079: }
080: header.put(PdfName.Resources, resources.getBaseObject());
081: }
082:
083: /**
084: <h3>Remarks</h3>
085: <p>For internal use only.</p>
086: */
087: public FormXObject(PdfDirectObject baseObject) {
088: super (baseObject);
089: }
090:
091: // </constructors>
092:
093: // <interface>
094: // <public>
095: @Override
096: public FormXObject clone(Document context) {
097: throw new NotImplementedException();
098: }
099:
100: /**
101: @version 0.0.5
102: @since 0.0.5
103: */
104: @Override
105: public double[] getMatrix() {
106: /*
107: NOTE: Form-space-to-user-space matrix is identity [1 0 0 1 0 0] by default,
108: but may be adjusted by setting the Matrix entry in the form dictionary [PDF:1.6:4.9].
109: */
110: PdfArray matrix = (PdfArray) File.resolve(getBaseDataObject()
111: .getHeader().get(PdfName.Matrix));
112: if (matrix == null) {
113: return new double[] { 1, // a.
114: 0, // b.
115: 0, // c.
116: 1, // d.
117: 0, // e.
118: 0 // f.
119: };
120: } else {
121: return new double[] {
122: ((IPdfNumber) matrix.get(0)).getNumberValue(), // a.
123: ((IPdfNumber) matrix.get(1)).getNumberValue(), // b.
124: ((IPdfNumber) matrix.get(2)).getNumberValue(), // c.
125: ((IPdfNumber) matrix.get(3)).getNumberValue(), // d.
126: ((IPdfNumber) matrix.get(4)).getNumberValue(), // e.
127: ((IPdfNumber) matrix.get(5)).getNumberValue() // f.
128: };
129: }
130: }
131:
132: /**
133: Gets the form size.
134: */
135: @Override
136: public Dimension2D getSize() {
137: PdfArray box = (PdfArray) File.resolve(getBaseDataObject()
138: .getHeader().get(PdfName.BBox));
139:
140: return new Dimension((int) ((IPdfNumber) box.get(2))
141: .getNumberValue(), (int) ((IPdfNumber) box.get(3))
142: .getNumberValue());
143: }
144:
145: /**
146: Sets the resources associated to the form.
147: */
148: public void setResources(Resources value) {
149: getBaseDataObject().getHeader().put(PdfName.Resources,
150: value.getBaseObject());
151: }
152:
153: /**
154: Sets the form size.
155: */
156: @Override
157: public void setSize(Dimension2D value) {
158: PdfDirectObject box = getBaseDataObject().getHeader().get(
159: PdfName.BBox);
160:
161: PdfArray boxObject = (PdfArray) File.resolve(box);
162: ((IPdfNumber) boxObject.get(2))
163: .setNumberValue(value.getWidth());
164: ((IPdfNumber) boxObject.get(3)).setNumberValue(value
165: .getHeight());
166:
167: File.update(box);
168: }
169:
170: // <IContentContext>
171: public PdfArray getBox() {
172: return (PdfArray) File.resolve(getBaseDataObject().getHeader()
173: .get(PdfName.BBox)); // Required [PDF:1.6:4.9.1].
174: }
175:
176: /**
177: Gets the content stream associated to the form.
178: */
179: public Contents getContents() {
180: return new Contents(getBaseObject(),
181: ((PdfReference) getBaseObject()).getIndirectObject(),
182: this );
183: }
184:
185: /**
186: Gets the resources associated to the form.
187: */
188: public Resources getResources() {
189: return new Resources(getBaseDataObject().getHeader().get(
190: PdfName.Resources), ((PdfReference) getBaseObject())
191: .getIndirectObject());
192: }
193:
194: // <IContentEntity>
195: /**
196: @version 0.0.5
197: @since 0.0.5
198: */
199: public ContentObject toInlineObject(IContentContext context) {
200: throw new NotImplementedException();
201: }
202:
203: /**
204: @version 0.0.5
205: @since 0.0.5
206: */
207: public XObject toXObject(Document context) {
208: throw new NotImplementedException();
209: }
210: // </IContentEntity>
211: // </IContentContext>
212: // </public>
213: // </interface>
214: // </dynamic>
215: // </class>
216: }
|