~ubuntu-branches/ubuntu/feisty/libitext-java/feisty

« back to all changes in this revision

Viewing changes to com/lowagie/text/rtf/graphic/RtfImage.java

  • Committer: Bazaar Package Importer
  • Author(s): Gerardo Curiel
  • Date: 2006-09-21 00:08:53 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20060921000853-le9rrpcnw9fc57sm
Tags: 1.4.5-1
* New upstream release
* debian/rules modified due to a new build.xml file
* Patched Pdfgraphics2d.java to remove privative dependencie on com.sun.image.codec.jpeg.*
  (more information on
  http://developer.classpath.org/mediation/ClasspathMigration#head-d4ee9efe53a641e29ffdcd96e985bf38bbc671c1 )
* ant/.ant.properties paths fixed
* Build package with the packages provided by java-gcj-compat-dev
* Removed unused README.Debian
* Removed unused debian/patches/01libitext-java-addbuildscript.patch

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * $Id: RtfImage.java,v 1.23 2005/12/24 13:14:28 hallm Exp $
 
3
 * $Name:  $
 
4
 *
 
5
 * Copyright 2001, 2002, 2003, 2004 by Mark Hall
 
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.rtf.graphic;
 
52
 
 
53
import java.io.ByteArrayInputStream;
 
54
import java.io.ByteArrayOutputStream;
 
55
import java.io.IOException;
 
56
import java.io.InputStream;
 
57
 
 
58
import com.lowagie.text.DocumentException;
 
59
import com.lowagie.text.Element;
 
60
import com.lowagie.text.Image;
 
61
import com.lowagie.text.pdf.codec.wmf.MetaDo;
 
62
import com.lowagie.text.rtf.RtfElement;
 
63
import com.lowagie.text.rtf.document.RtfDocument;
 
64
import com.lowagie.text.rtf.style.RtfParagraphStyle;
 
65
import com.lowagie.text.rtf.text.RtfParagraph;
 
66
 
 
67
 
 
68
/**
 
69
 * The RtfImage contains one image. Supported image types are jpeg, png, wmf, bmp.
 
70
 * 
 
71
 * @version $Revision: 1.23 $
 
72
 * @author Mark Hall (mhall@edu.uni-klu.ac.at)
 
73
 * @author Paulo Soares
 
74
 */
 
75
public class RtfImage extends RtfElement {
 
76
    
 
77
    /**
 
78
     * Constant for the shape/picture group
 
79
     */
 
80
    private static final byte[] PICTURE_GROUP = "\\*\\shppict".getBytes();
 
81
    /**
 
82
     * Constant for a picture
 
83
     */
 
84
    private static final byte[] PICTURE = "\\pict".getBytes();
 
85
    /**
 
86
     * Constant for a jpeg image
 
87
     */
 
88
    private static final byte[] PICTURE_JPEG = "\\jpegblip".getBytes();
 
89
    /**
 
90
     * Constant for a png image
 
91
     */
 
92
    private static final byte[] PICTURE_PNG = "\\pngblip".getBytes();
 
93
    /**
 
94
     * Constant for a bmp image
 
95
     */
 
96
    private static final byte[] PICTURE_BMP = "\\dibitmap0".getBytes();
 
97
    /**
 
98
     * Constant for a wmf image
 
99
     */
 
100
    private static final byte[] PICTURE_WMF = "\\wmetafile8".getBytes();
 
101
    /**
 
102
     * Constant for the picture width
 
103
     */
 
104
    private static final byte[] PICTURE_WIDTH = "\\picw".getBytes();
 
105
    /**
 
106
     * Constant for the picture height
 
107
     */
 
108
    private static final byte[] PICTURE_HEIGHT = "\\pich".getBytes();
 
109
    /**
 
110
     * Constant for the picture width scale
 
111
     */
 
112
    private static final byte[] PICTURE_SCALED_WIDTH = "\\picwgoal".getBytes();
 
113
    /**
 
114
     * Constant for the picture height scale
 
115
     */
 
116
    private static final byte[] PICTURE_SCALED_HEIGHT = "\\pichgoal".getBytes();
 
117
    
 
118
    /**
 
119
     * The type of image this is.
 
120
     */
 
121
    private int imageType = Image.ORIGINAL_NONE;
 
122
    /**
 
123
     * The actual image. Already formated for direct inclusion in the rtf document
 
124
     */
 
125
    private byte[] image = new byte[0];
 
126
    /**
 
127
     * The alignment of this picture
 
128
     */
 
129
    private int alignment = Element.ALIGN_LEFT;
 
130
    /**
 
131
     * The width of this picture
 
132
     */
 
133
    private float width = 0;
 
134
    /**
 
135
     * The height of this picutre
 
136
     */
 
137
    private float height = 0;
 
138
    /**
 
139
     * The intended display width of this picture
 
140
     */
 
141
    private float plainWidth = 0;
 
142
    /**
 
143
     * The intended display height of this picture
 
144
     */
 
145
    private float plainHeight = 0;
 
146
    /**
 
147
     * Whether this RtfImage is a top level element and should
 
148
     * be an extra paragraph.
 
149
     */
 
150
    private boolean topLevelElement = false;
 
151
    
 
152
    /**
 
153
     * Constructs a RtfImage for an Image.
 
154
     * 
 
155
     * @param doc The RtfDocument this RtfImage belongs to
 
156
     * @param image The Image that this RtfImage wraps
 
157
     * @throws DocumentException If an error occured accessing the image content
 
158
     */
 
159
    public RtfImage(RtfDocument doc, Image image) throws DocumentException {
 
160
        super(doc);
 
161
        imageType = image.getOriginalType();
 
162
        if (!(imageType == Image.ORIGINAL_JPEG || imageType == Image.ORIGINAL_BMP
 
163
                || imageType == Image.ORIGINAL_PNG || imageType == Image.ORIGINAL_WMF)) {
 
164
            throw new DocumentException("Only BMP, PNG, WMF and JPEG images are supported by the RTF Writer");
 
165
        }
 
166
        alignment = image.alignment();
 
167
        width = image.width();
 
168
        height = image.height();
 
169
        plainWidth = image.plainWidth();
 
170
        plainHeight = image.plainHeight();
 
171
        this.image = getImage(image);
 
172
    }
 
173
    
 
174
    /**
 
175
     * Extracts the image data from the Image. The data is formated for direct inclusion
 
176
     * in a rtf document
 
177
     * 
 
178
     * @param image The Image for which to extract the content
 
179
     * @return The image data formated for the rtf document
 
180
     * @throws DocumentException If an error occurs accessing the image content
 
181
     */
 
182
    private byte[] getImage(Image image) throws DocumentException {
 
183
        ByteArrayOutputStream imageTemp = new ByteArrayOutputStream();
 
184
        try {
 
185
            InputStream imageIn;
 
186
            if (imageType == Image.ORIGINAL_BMP) {
 
187
                imageIn = new ByteArrayInputStream(MetaDo.wrapBMP(image));
 
188
            } else {
 
189
                if (image.getOriginalData() == null) {
 
190
                    imageIn = image.url().openStream();
 
191
                } else {
 
192
                    imageIn = new ByteArrayInputStream(image.getOriginalData());
 
193
                }
 
194
                if (imageType == Image.ORIGINAL_WMF) { //remove the placeable header
 
195
                    long skipLength = 22;
 
196
                        while(skipLength > 0) {
 
197
                            skipLength = skipLength - imageIn.skip(skipLength);
 
198
                        }
 
199
                }
 
200
            }
 
201
            int buffer = 0;
 
202
            int count = 0;
 
203
            while((buffer = imageIn.read()) != -1) {
 
204
                String helperStr = Integer.toHexString(buffer);
 
205
                if (helperStr.length() < 2) helperStr = "0" + helperStr;
 
206
                imageTemp.write(helperStr.getBytes());
 
207
                count++;
 
208
                if (count == 64) {
 
209
                    imageTemp.write((byte) '\n');
 
210
                    count = 0;
 
211
                }
 
212
            }
 
213
        } catch(IOException ioe) {
 
214
            throw new DocumentException(ioe.getMessage());
 
215
        }
 
216
        return imageTemp.toByteArray();
 
217
    }
 
218
    
 
219
    /**
 
220
     * Writes the RtfImage content
 
221
     * 
 
222
     * @return the RtfImage content
 
223
     */
 
224
    public byte[] write() {
 
225
        ByteArrayOutputStream result = new ByteArrayOutputStream();
 
226
        try {
 
227
            if(this.topLevelElement) {
 
228
                result.write(RtfParagraph.PARAGRAPH_DEFAULTS);
 
229
            }
 
230
            switch(alignment) {
 
231
                case Element.ALIGN_LEFT:
 
232
                        result.write(RtfParagraphStyle.ALIGN_LEFT);
 
233
                        break;
 
234
                case Element.ALIGN_RIGHT:
 
235
                        result.write(RtfParagraphStyle.ALIGN_RIGHT);
 
236
                        break;
 
237
                case Element.ALIGN_CENTER:
 
238
                        result.write(RtfParagraphStyle.ALIGN_CENTER);
 
239
                        break;
 
240
                case Element.ALIGN_JUSTIFIED:
 
241
                        result.write(RtfParagraphStyle.ALIGN_JUSTIFY);
 
242
                        break;
 
243
            }
 
244
            result.write(OPEN_GROUP);
 
245
            result.write(PICTURE_GROUP);
 
246
            result.write(OPEN_GROUP);
 
247
            result.write(PICTURE);
 
248
            switch(imageType) {
 
249
                case Image.ORIGINAL_JPEG:
 
250
                    result.write(PICTURE_JPEG);
 
251
                        break;
 
252
                case Image.ORIGINAL_PNG:
 
253
                    result.write(PICTURE_PNG);
 
254
                        break;
 
255
                case Image.ORIGINAL_WMF:
 
256
                case Image.ORIGINAL_BMP:
 
257
                    result.write(PICTURE_WMF);
 
258
                        break;
 
259
            }
 
260
            result.write(PICTURE_WIDTH);
 
261
            result.write(intToByteArray((int) width));
 
262
            result.write(PICTURE_HEIGHT);
 
263
            result.write(intToByteArray((int) height));
 
264
            if(width != plainWidth || this.imageType == Image.ORIGINAL_BMP) {
 
265
                result.write(PICTURE_SCALED_WIDTH);
 
266
                result.write(intToByteArray((int) (plainWidth * RtfElement.TWIPS_FACTOR)));
 
267
            }
 
268
            if(height != plainHeight || this.imageType == Image.ORIGINAL_BMP) {
 
269
                result.write(PICTURE_SCALED_HEIGHT);
 
270
                result.write(intToByteArray((int) (plainHeight * RtfElement.TWIPS_FACTOR)));
 
271
            }
 
272
            result.write(DELIMITER);
 
273
            result.write((byte) '\n');
 
274
            result.write(image);
 
275
            result.write(CLOSE_GROUP);
 
276
            result.write(CLOSE_GROUP);
 
277
            if(this.topLevelElement) {
 
278
                result.write(RtfParagraph.PARAGRAPH);
 
279
                result.write(RtfParagraph.PARAGRAPH);
 
280
            }
 
281
            result.write((byte) '\n');
 
282
        } catch(IOException ioe) {
 
283
            ioe.printStackTrace();
 
284
        }
 
285
        return result.toByteArray();
 
286
    }
 
287
    
 
288
    /**
 
289
     * Sets the alignment of this RtfImage. Uses the alignments from com.lowagie.text.Element.
 
290
     * 
 
291
     * @param alignment The alignment to use.
 
292
     */
 
293
    public void setAlignment(int alignment) {
 
294
        this.alignment = alignment;
 
295
    }
 
296
    
 
297
    /**
 
298
     * Set whether this RtfImage should behave like a top level element
 
299
     * and enclose itself in a paragraph.
 
300
     * 
 
301
     * @param topLevelElement Whether to behave like a top level element.
 
302
     */
 
303
    public void setTopLevelElement(boolean topLevelElement) {
 
304
        this.topLevelElement = topLevelElement;
 
305
    }
 
306
}