1 /*
2  * $Id$
3  *
4  * Copyright 1999, 2000, 2001, 2002 Bruno Lowagie
5  *
6  * The contents of this file are subject to the Mozilla Public License Version 1.1
7  * (the "License"); you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
9  *
10  * Software distributed under the License is distributed on an "AS IS" basis,
11  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12  * for the specific language governing rights and limitations under the License.
13  *
14  * The Original Code is 'iText, a free JAVA-PDF library'.
15  *
16  * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
17  * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
18  * All Rights Reserved.
19  * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
20  * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
21  *
22  * Contributor(s): all the names of the contributors are added in the source code
23  * where applicable.
24  *
25  * Alternatively, the contents of this file may be used under the terms of the
26  * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
27  * provisions of LGPL are applicable instead of those above.  If you wish to
28  * allow use of your version of this file only under the terms of the LGPL
29  * License and not to allow others to use your version of this file under
30  * the MPL, indicate your decision by deleting the provisions above and
31  * replace them with the notice and other provisions required by the LGPL.
32  * If you do not delete the provisions above, a recipient may use your version
33  * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
34  *
35  * This library is free software; you can redistribute it and/or modify it
36  * under the terms of the MPL as stated above or under the terms of the GNU
37  * Library General Public License as published by the Free Software Foundation;
38  * either version 2 of the License, or any later version.
39  *
40  * This library is distributed in the hope that it will be useful, but WITHOUT
41  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
42  * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
43  * details.
44  *
45  * If you didn't download this code from the following link, you should check if
46  * you aren't using an obsolete version:
47  * http://www.lowagie.com/iText/
48  */
49 
50 package com.lowagie.text.pdf;
51 
52 import java.io.ByteArrayOutputStream;
53 import java.io.OutputStream;
54 import java.util.zip.Deflater;
55 import java.util.zip.DeflaterOutputStream;
56 
57 import com.lowagie.text.DocWriter;
58 import com.lowagie.text.Document;
59 import com.lowagie.text.Rectangle;
60 
61 /**
62  * <CODE>PdfContents</CODE> is a <CODE>PdfStream</CODE> containing the contents (text + graphics) of a <CODE>PdfPage</CODE>.
63  */
64 
65 class PdfContents extends PdfStream {
66 
67     static final byte SAVESTATE[] = DocWriter.getISOBytes("q\n");
68     static final byte RESTORESTATE[] = DocWriter.getISOBytes("Q\n");
69     static final byte ROTATE90[] = DocWriter.getISOBytes("0 1 -1 0 ");
70     static final byte ROTATE180[] = DocWriter.getISOBytes("-1 0 0 -1 ");
71     static final byte ROTATE270[] = DocWriter.getISOBytes("0 -1 1 0 ");
72     static final byte ROTATEFINAL[] = DocWriter.getISOBytes(" cm\n");
73     // constructor
74 
75 /**
76  * Constructs a <CODE>PdfContents</CODE>-object, containing text and general graphics.
77  *
78  * @param under the direct content that is under all others
79  * @param content the graphics in a page
80  * @param text the text in a page
81  * @param secondContent the direct content that is over all others
82  * @throws BadPdfFormatException on error
83  */
84 
PdfContents(PdfContentByte under, PdfContentByte content, PdfContentByte text, PdfContentByte secondContent, Rectangle page)85     PdfContents(PdfContentByte under, PdfContentByte content, PdfContentByte text, PdfContentByte secondContent, Rectangle page) throws BadPdfFormatException {
86         super();
87         try {
88             OutputStream out = null;
89             Deflater deflater = null;
90             streamBytes = new ByteArrayOutputStream();
91             if (Document.compress)
92             {
93                 compressed = true;
94                 compressionLevel = text.getPdfWriter().getCompressionLevel();
95                 deflater = new Deflater(compressionLevel);
96                 out = new DeflaterOutputStream(streamBytes, deflater);
97             }
98             else
99                 out = streamBytes;
100             int rotation = page.getRotation();
101             switch (rotation) {
102                 case 90:
103                     out.write(ROTATE90);
104                     out.write(DocWriter.getISOBytes(ByteBuffer.formatDouble(page.getTop())));
105                     out.write(' ');
106                     out.write('0');
107                     out.write(ROTATEFINAL);
108                     break;
109                 case 180:
110                     out.write(ROTATE180);
111                     out.write(DocWriter.getISOBytes(ByteBuffer.formatDouble(page.getRight())));
112                     out.write(' ');
113                     out.write(DocWriter.getISOBytes(ByteBuffer.formatDouble(page.getTop())));
114                     out.write(ROTATEFINAL);
115                     break;
116                 case 270:
117                     out.write(ROTATE270);
118                     out.write('0');
119                     out.write(' ');
120                     out.write(DocWriter.getISOBytes(ByteBuffer.formatDouble(page.getRight())));
121                     out.write(ROTATEFINAL);
122                     break;
123             }
124             if (under.size() > 0) {
125                 out.write(SAVESTATE);
126                 under.getInternalBuffer().writeTo(out);
127                 out.write(RESTORESTATE);
128             }
129             if (content.size() > 0) {
130                 out.write(SAVESTATE);
131                 content.getInternalBuffer().writeTo(out);
132                 out.write(RESTORESTATE);
133             }
134             if (text != null) {
135                 out.write(SAVESTATE);
136                 text.getInternalBuffer().writeTo(out);
137                 out.write(RESTORESTATE);
138             }
139             if (secondContent.size() > 0) {
140                 secondContent.getInternalBuffer().writeTo(out);
141             }
142             out.close();
143             if (deflater != null) {
144                 deflater.end();
145             }
146         }
147         catch (Exception e) {
148             throw new BadPdfFormatException(e.getMessage());
149         }
150         put(PdfName.LENGTH, new PdfNumber(streamBytes.size()));
151         if (compressed)
152             put(PdfName.FILTER, PdfName.FLATEDECODE);
153     }
154 }