KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lowagie > text > ImgPostscript


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

50
51 package com.lowagie.text;
52
53 import java.io.*;
54 import java.net.*;
55 import com.lowagie.text.pdf.*;
56 import com.lowagie.text.pdf.codec.postscript.*;
57 import java.util.StringTokenizer;
58
59 /**
60  * An <CODE>ImgPostscript</CODE> is the representation of an EPS
61  * that has to be inserted into the document
62  *
63  * @see Element
64  * @see Image
65  */

66
67 public class ImgPostscript
68 extends Image
69 implements Element {
70     
71     // Constructors
72

73     ImgPostscript(Image image) {
74         super(image);
75     }
76     
77     /**
78      * Constructs an <CODE>ImgPostscript</CODE>-object, using an <VAR>url</VAR>.
79      *
80      * @param url the <CODE>URL</CODE> where the image can be found
81      * @throws BadElementException on error
82      * @throws IOException on error
83      */

84     
85     public ImgPostscript(URL url) throws BadElementException, IOException {
86         super(url);
87         processParameters();
88     }
89     
90     /**
91      * Constructs an <CODE>ImgPostscript</CODE>-object, using a <VAR>filename</VAR>.
92      *
93      * @param filename a <CODE>String</CODE>-representation of the file that contains the image.
94      * @throws BadElementException on error
95      * @throws MalformedURLException on error
96      * @throws IOException on error
97      */

98     
99     public ImgPostscript(String filename) throws BadElementException,
100     MalformedURLException, IOException {
101         this(Image.toURL(filename));
102     }
103     
104     /**
105      * Constructs an <CODE>ImgPostscript</CODE>-object from memory.
106      *
107      * @param img the memory image
108      * @throws BadElementException on error
109      * @throws IOException on error
110      */

111     
112     public ImgPostscript(byte[] img) throws BadElementException, IOException {
113         super( (URL)null);
114         rawData = img;
115         originalData = img;
116         processParameters();
117     }
118     
119     /**
120      * This method checks if the image is a valid Postscript and processes some parameters.
121      * @throws BadElementException
122      * @throws IOException
123      */

124     
125     private void processParameters() throws BadElementException, IOException {
126         type = IMGTEMPLATE;
127         originalType = ORIGINAL_PS;
128         InputStream is = null;
129         try {
130             String errorID;
131             if (rawData == null) {
132                 is = url.openStream();
133                 errorID = url.toString();
134             }
135             else {
136                 is = new java.io.ByteArrayInputStream(rawData);
137                 errorID = "Byte array";
138             }
139             String boundingbox=null;
140             Reader r = new BufferedReader(new InputStreamReader(is));
141             // StreamTokenizer st = new StreamTokenizer(r);
142
while (r.ready()) {
143                 char c;
144                 StringBuffer sb = new StringBuffer();
145                 while ( (c = ( (char) r.read())) != '\n') {
146                     sb.append(c);
147                 }
148                 //System.out.println("<<" + sb.toString() + ">>");
149
if (sb.toString().startsWith("%%BoundingBox:")) {
150                     boundingbox = sb.toString();
151                     
152                 }
153                 if (sb.toString().startsWith("%%TemplateBox:")) {
154                     boundingbox = sb.toString();
155                 }
156                 if (sb.toString().startsWith("%%EndComments")) {
157                     break;
158                 }
159                 
160             }
161             if(boundingbox==null)return;
162             StringTokenizer st=new StringTokenizer(boundingbox,": \r\n");
163             st.nextElement();
164             String xx1=st.nextToken();
165             String yy1=st.nextToken();
166             String xx2=st.nextToken();
167             String yy2=st.nextToken();
168             
169             int left = Integer.parseInt(xx1);
170             int top = Integer.parseInt(yy1);
171             int right = Integer.parseInt(xx2);
172             int bottom = Integer.parseInt(yy2);
173             int inch = 1;
174             dpiX = 72;
175             dpiY = 72;
176             scaledHeight = (float) (bottom - top) / inch *1f;
177             scaledHeight=800;
178             setTop(scaledHeight);
179             scaledWidth = (float) (right - left) / inch * 1f;
180             scaledWidth=800;
181             setRight(scaledWidth);
182         }
183         finally {
184             if (is != null) {
185                 is.close();
186             }
187             plainWidth = width();
188             plainHeight = height();
189         }
190     }
191     
192     /** Reads the Postscript into a template.
193      * @param template the template to read to
194      * @throws IOException on error
195      * @throws DocumentException on error
196      */

197     public void readPostscript(PdfTemplate template) throws IOException,
198     DocumentException {
199         setTemplateData(template);
200         template.setWidth(width());
201         template.setHeight(height());
202         InputStream is = null;
203         try {
204             if (rawData == null) {
205                 is = url.openStream();
206             }
207             else {
208                 is = new java.io.ByteArrayInputStream(rawData);
209             }
210             MetaDoPS meta = new MetaDoPS(is, template);
211             meta.readAll();
212         }
213         finally {
214             if (is != null) {
215                 is.close();
216             }
217         }
218     }
219 }
220
Popular Tags