1 | /**
|
---|
2 | * PdfFramesetWriter.java
|
---|
3 | * Copyright (C) 2010 New Zealand Digital Library, http://expeditee.org
|
---|
4 | *
|
---|
5 | * This program is free software: you can redistribute it and/or modify
|
---|
6 | * it under the terms of the GNU General Public License as published by
|
---|
7 | * the Free Software Foundation, either version 3 of the License, or
|
---|
8 | * (at your option) any later version.
|
---|
9 | *
|
---|
10 | * This program is distributed in the hope that it will be useful,
|
---|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
13 | * GNU General Public License for more details.
|
---|
14 | *
|
---|
15 | * You should have received a copy of the GNU General Public License
|
---|
16 | * along with this program. If not, see <http://www.gnu.org/licenses/>.
|
---|
17 | */
|
---|
18 |
|
---|
19 | package org.expeditee.io;
|
---|
20 |
|
---|
21 | import java.awt.Graphics2D;
|
---|
22 | import java.io.FileOutputStream;
|
---|
23 | import java.io.IOException;
|
---|
24 | import java.io.Writer;
|
---|
25 | import java.util.Collection;
|
---|
26 | import java.util.List;
|
---|
27 |
|
---|
28 | import org.expeditee.core.Dimension;
|
---|
29 | import org.expeditee.core.Image;
|
---|
30 | import org.expeditee.gio.EcosystemManager;
|
---|
31 | import org.expeditee.gio.GraphicsManager;
|
---|
32 | import org.expeditee.gio.swing.SwingConversions;
|
---|
33 | import org.expeditee.gio.swing.SwingMiscManager;
|
---|
34 | import org.expeditee.gui.DisplayController;
|
---|
35 | import org.expeditee.gui.Frame;
|
---|
36 | import org.expeditee.gui.FrameGraphics;
|
---|
37 | import org.expeditee.items.Circle;
|
---|
38 | import org.expeditee.items.Item;
|
---|
39 | import org.expeditee.items.Line;
|
---|
40 | import org.expeditee.items.Picture;
|
---|
41 | import org.expeditee.items.Text;
|
---|
42 | import org.expeditee.items.widgets.Widget;
|
---|
43 | import org.expeditee.settings.UserSettings;
|
---|
44 |
|
---|
45 | import com.lowagie.text.Document;
|
---|
46 | import com.lowagie.text.DocumentException;
|
---|
47 | import com.lowagie.text.Font;
|
---|
48 | import com.lowagie.text.FontFactory;
|
---|
49 | import com.lowagie.text.Rectangle;
|
---|
50 | import com.lowagie.text.pdf.PdfContentByte;
|
---|
51 | import com.lowagie.text.pdf.PdfWriter;
|
---|
52 |
|
---|
53 | /**
|
---|
54 | *
|
---|
55 | * @author root
|
---|
56 | *
|
---|
57 | */
|
---|
58 | public class PdfFramesetWriter extends DefaultFramesetWriter {
|
---|
59 |
|
---|
60 | private Document _pdfDocument;
|
---|
61 |
|
---|
62 | private PdfWriter _pdfWriter;
|
---|
63 |
|
---|
64 | private boolean _showFrameNames;
|
---|
65 |
|
---|
66 | public PdfFramesetWriter(long firstFrame, long maxFrame, boolean showFrameNames)
|
---|
67 | {
|
---|
68 | super(firstFrame, maxFrame);
|
---|
69 | Dimension d = DisplayController.getFramePaintArea().getSize();
|
---|
70 | _pdfDocument = new Document(new Rectangle(d.width, d.height));
|
---|
71 | _showFrameNames = showFrameNames;
|
---|
72 | }
|
---|
73 |
|
---|
74 | public PdfFramesetWriter() {
|
---|
75 | this(1, Long.MAX_VALUE, false);
|
---|
76 | }
|
---|
77 |
|
---|
78 | @Override
|
---|
79 | protected String getFileName(Frame start) {
|
---|
80 | String fileName = start.getFramesetName();
|
---|
81 | if (_maxFrame < Long.MAX_VALUE) {
|
---|
82 | fileName += "_" + _firstFrame + "-" + _maxFrame;
|
---|
83 | }
|
---|
84 | return fileName;
|
---|
85 | }
|
---|
86 |
|
---|
87 | @Override
|
---|
88 | protected void initialise(Frame start, Writer writer) throws IOException {
|
---|
89 | _format = ".pdf";
|
---|
90 | super.initialise(start, writer);
|
---|
91 |
|
---|
92 | try {
|
---|
93 | _pdfWriter = PdfWriter.getInstance(_pdfDocument,
|
---|
94 | new FileOutputStream(_filename));
|
---|
95 | _pdfDocument.open();
|
---|
96 | _pdfDocument.addCreationDate();
|
---|
97 | _pdfDocument.addAuthor(UserSettings.UserName.get());
|
---|
98 | _pdfDocument.addCreator("Expeditee");
|
---|
99 | } catch (DocumentException e) {
|
---|
100 | e.printStackTrace();
|
---|
101 | throw new IOException(e.getMessage());
|
---|
102 | }
|
---|
103 | }
|
---|
104 |
|
---|
105 | @Override
|
---|
106 | protected void writeTitle(Text toWrite, List<Item> items)
|
---|
107 | throws IOException {
|
---|
108 | _pdfDocument.addTitle(toWrite.getText());
|
---|
109 | writeText(toWrite);
|
---|
110 | }
|
---|
111 |
|
---|
112 | protected void writeStartFrame(Frame starting) throws IOException {
|
---|
113 | // TODO color in the frame background color
|
---|
114 |
|
---|
115 | super.writeStartFrame(starting);
|
---|
116 | }
|
---|
117 |
|
---|
118 | protected void writeEndFrame(Frame ending) throws IOException {
|
---|
119 | if (_showFrameNames)
|
---|
120 | writeText((Text) ending.getNameItem());
|
---|
121 | super.writeEndFrame(ending);
|
---|
122 | // Move to the next page
|
---|
123 | _pdfDocument.newPage();
|
---|
124 | }
|
---|
125 |
|
---|
126 | @Override
|
---|
127 | protected String finalise() throws IOException {
|
---|
128 | _pdfDocument.close();
|
---|
129 | return super.finalise();
|
---|
130 | }
|
---|
131 |
|
---|
132 | @Override
|
---|
133 | protected void writeText(Text text) throws IOException {
|
---|
134 | PdfContentByte cb = _pdfWriter.getDirectContent();
|
---|
135 | // we tell the ContentByte we're ready to draw text
|
---|
136 | cb.beginText();
|
---|
137 | Font font = FontFactory.getFont(Conversion.getPdfFont(text.getFamily()),
|
---|
138 | text.getSize(),
|
---|
139 | SwingConversions.toSwingFontStyle(text.getPaintFont().getStyle()),
|
---|
140 | SwingConversions.toSwingColor(text.getPaintColor()));
|
---|
141 | cb.setFontAndSize(font.getBaseFont(), text.getSize());
|
---|
142 | // cb.setColorStroke(text.getPaintColor());
|
---|
143 | cb.setColorFill(SwingConversions.toSwingColor(text.getPaintColor()));
|
---|
144 |
|
---|
145 | // we draw some text on a certain position
|
---|
146 | cb.setTextMatrix(text.getX(), _pdfWriter.getPageSize().getHeight()
|
---|
147 | - text.getY());
|
---|
148 | List<String> textLines = text.getTextList();
|
---|
149 | // cb.showText(textLines.get(0));
|
---|
150 | for (int i = 0; i < textLines.size(); i++) {
|
---|
151 | cb.showText(textLines.get(i));
|
---|
152 | cb.moveText(0, -text.getLineHeight());
|
---|
153 | }
|
---|
154 | // we tell the contentByte, we've finished drawing text
|
---|
155 | cb.endText();
|
---|
156 | }
|
---|
157 |
|
---|
158 | @Override
|
---|
159 | protected void writePicture(Picture pic) throws IOException {
|
---|
160 | Image image = pic.getCroppedImage();
|
---|
161 | if (image == null)
|
---|
162 | return;
|
---|
163 | try {
|
---|
164 | PdfContentByte cb = _pdfWriter.getDirectContent();
|
---|
165 | com.lowagie.text.Image iTextImage = com.lowagie.text.Image
|
---|
166 | .getInstance(SwingMiscManager.getIfUsingSwingImageManager().getInternalImage(image), null);
|
---|
167 | iTextImage.setAbsolutePosition(pic.getX(), _pdfWriter.getPageSize()
|
---|
168 | .getHeight()
|
---|
169 | - pic.getY() - pic.getHeight());
|
---|
170 | cb.addImage(iTextImage);
|
---|
171 | } catch (DocumentException e) {
|
---|
172 | e.printStackTrace();
|
---|
173 | }
|
---|
174 | }
|
---|
175 |
|
---|
176 | @Override
|
---|
177 | protected void writeLine(Line lineEnd) throws IOException {
|
---|
178 | PdfContentByte cb = _pdfWriter.getDirectContent();
|
---|
179 | // Color fill = lineEnd.getFillColor();
|
---|
180 | // if (fill != null) {
|
---|
181 | // cb.setColorFill(fill);
|
---|
182 | // }
|
---|
183 | /* Graphics2D g = cb.createGraphicsShapes(
|
---|
184 | FrameGraphics.getMaxSize().width,
|
---|
185 | FrameGraphics.getMaxSize().height);*/
|
---|
186 | GraphicsManager g = EcosystemManager.getGraphicsManager();
|
---|
187 | Image temp = EcosystemManager.getImageManager().createImage(DisplayController.getFramePaintArea().getSize());
|
---|
188 | lineEnd.paint();
|
---|
189 | // if (fill != null) {
|
---|
190 | // g.setPaint(fill);
|
---|
191 | // }
|
---|
192 | // lineEnd.paintFill(g);
|
---|
193 | }
|
---|
194 |
|
---|
195 | @Override
|
---|
196 | protected Collection<Item> getItemsToWrite(Frame toWrite) {
|
---|
197 | return toWrite.getVisibleItems();
|
---|
198 | }
|
---|
199 |
|
---|
200 | @Override
|
---|
201 | protected void writeCircle(Circle toWrite) throws IOException
|
---|
202 | {
|
---|
203 | PdfContentByte cb = _pdfWriter.getDirectContent();
|
---|
204 | Graphics2D g = cb.createGraphicsShapes(DisplayController.getFramePaintArea().getWidth(), DisplayController.getFramePaintArea().getHeight());
|
---|
205 | Image temp = EcosystemManager.getImageManager().createImage(DisplayController.getFramePaintArea().getSize());
|
---|
206 | EcosystemManager.getGraphicsManager().pushDrawingSurface(temp);
|
---|
207 | toWrite.paint();
|
---|
208 | EcosystemManager.getGraphicsManager().popDrawingSurface();
|
---|
209 | g.drawImage(SwingMiscManager.getIfUsingSwingImageManager().getInternalImage(temp), 0, 0, null);
|
---|
210 | }
|
---|
211 |
|
---|
212 | @Override
|
---|
213 | protected void writeWidget(Widget toWrite) throws IOException
|
---|
214 | {
|
---|
215 | PdfContentByte cb = _pdfWriter.getDirectContent();
|
---|
216 | Graphics2D g = cb.createGraphicsShapes(DisplayController.getFramePaintArea().getWidth(), DisplayController.getFramePaintArea().getHeight());
|
---|
217 | Image temp = EcosystemManager.getImageManager().createImage(DisplayController.getFramePaintArea().getSize());
|
---|
218 | EcosystemManager.getGraphicsManager().pushDrawingSurface(temp);
|
---|
219 | toWrite.paint();
|
---|
220 | EcosystemManager.getGraphicsManager().popDrawingSurface();
|
---|
221 | g.drawImage(SwingMiscManager.getIfUsingSwingImageManager().getInternalImage(temp), 0, 0, null);
|
---|
222 | }
|
---|
223 | }
|
---|