1 | package org.expeditee.io;
|
---|
2 |
|
---|
3 | import java.awt.Color;
|
---|
4 | import java.awt.Dimension;
|
---|
5 | import java.awt.Image;
|
---|
6 | import java.awt.Polygon;
|
---|
7 | import java.io.FileOutputStream;
|
---|
8 | import java.io.IOException;
|
---|
9 | import java.io.Writer;
|
---|
10 | import java.util.Collection;
|
---|
11 | import java.util.LinkedList;
|
---|
12 | import java.util.List;
|
---|
13 |
|
---|
14 | import org.expeditee.gui.Frame;
|
---|
15 | import org.expeditee.gui.FrameGraphics;
|
---|
16 | import org.expeditee.items.Circle;
|
---|
17 | import org.expeditee.items.Item;
|
---|
18 | import org.expeditee.items.Line;
|
---|
19 | import org.expeditee.items.Picture;
|
---|
20 | import org.expeditee.items.Text;
|
---|
21 | import org.expeditee.settings.UserSettings;
|
---|
22 |
|
---|
23 | import com.lowagie.text.Document;
|
---|
24 | import com.lowagie.text.DocumentException;
|
---|
25 | import com.lowagie.text.Font;
|
---|
26 | import com.lowagie.text.FontFactory;
|
---|
27 | import com.lowagie.text.Rectangle;
|
---|
28 | import com.lowagie.text.pdf.PdfContentByte;
|
---|
29 | import com.lowagie.text.pdf.PdfWriter;
|
---|
30 |
|
---|
31 | /**
|
---|
32 | *
|
---|
33 | * @author root
|
---|
34 | *
|
---|
35 | */
|
---|
36 | public class PDF2Writer extends DefaultFrameWriter {
|
---|
37 |
|
---|
38 | private Document _pdfDocument;
|
---|
39 | private Dimension _pageSize;
|
---|
40 | private PdfWriter _pdfWriter;
|
---|
41 | private float _height;
|
---|
42 |
|
---|
43 | public PDF2Writer() {
|
---|
44 | _pageSize = FrameGraphics.getMaxSize();
|
---|
45 | _pdfDocument = new Document(new Rectangle(_pageSize.width, _pageSize.height));
|
---|
46 | }
|
---|
47 |
|
---|
48 | @Override
|
---|
49 | protected void initialise(Frame start, Writer writer) throws IOException {
|
---|
50 | _format = ".pdf";
|
---|
51 | super.initialise(start, writer);
|
---|
52 | try {
|
---|
53 | _pdfWriter = PdfWriter.getInstance(_pdfDocument,
|
---|
54 | new FileOutputStream(_filename));
|
---|
55 | _pdfDocument.open();
|
---|
56 | _pdfDocument.addCreationDate();
|
---|
57 | _pdfDocument.addAuthor(UserSettings.UserName.get());
|
---|
58 | _pdfDocument.addCreator("Expeditee");
|
---|
59 | _height = _pdfWriter.getPageSize().getHeight();
|
---|
60 | // set bg color
|
---|
61 | PdfContentByte cb = _pdfWriter.getDirectContent();
|
---|
62 | cb.setColorFill(start.getPaintBackgroundColor());
|
---|
63 | System.out.println(start.getBackgroundColor());
|
---|
64 | cb.rectangle(0, 0, _pageSize.width, _pageSize.height);
|
---|
65 | cb.fillStroke();
|
---|
66 | } catch (DocumentException e) {
|
---|
67 | e.printStackTrace();
|
---|
68 | throw new IOException(e.getMessage());
|
---|
69 | }
|
---|
70 | }
|
---|
71 |
|
---|
72 | @Override
|
---|
73 | protected void writeTitle(Text toWrite, List<Item> items)
|
---|
74 | throws IOException {
|
---|
75 | _pdfDocument.addTitle(toWrite.getText());
|
---|
76 | writeText(toWrite);
|
---|
77 | }
|
---|
78 |
|
---|
79 | @Override
|
---|
80 | protected String finalise() throws IOException {
|
---|
81 | _pdfDocument.close();
|
---|
82 | return super.finalise();
|
---|
83 | }
|
---|
84 |
|
---|
85 | @Override
|
---|
86 | protected void writeText(Text text) throws IOException {
|
---|
87 | PdfContentByte cb = _pdfWriter.getDirectContent();
|
---|
88 | // we tell the ContentByte we're ready to draw text
|
---|
89 | cb.beginText();
|
---|
90 | Font font = FontFactory.getFont(
|
---|
91 | Conversion.getPdfFont(text.getFamily()), text.getSize(), text
|
---|
92 | .getPaintFont().getStyle(), text.getPaintColor());
|
---|
93 | cb.setFontAndSize(font.getBaseFont(), text.getSize());
|
---|
94 | // cb.setColorStroke(text.getPaintColor());
|
---|
95 | cb.setColorFill(text.getPaintColor());
|
---|
96 |
|
---|
97 | // we draw some text on a certain position
|
---|
98 | cb.setTextMatrix(text.getX(), _pdfWriter.getPageSize().getHeight() - text.getY());
|
---|
99 | List<String> textLines = text.getTextList();
|
---|
100 | //cb.showText(textLines.get(0));
|
---|
101 | for(int i = 0; i < textLines.size(); i++){
|
---|
102 | cb.showText(textLines.get(i));
|
---|
103 | cb.moveText(0, -text.getLineHeight());
|
---|
104 | }
|
---|
105 |
|
---|
106 | // we tell the contentByte, we've finished drawing text
|
---|
107 | cb.endText();
|
---|
108 | }
|
---|
109 |
|
---|
110 | @Override
|
---|
111 | protected void writePicture(Picture pic) throws IOException {
|
---|
112 | Image image = pic.getCroppedImage();
|
---|
113 | try {
|
---|
114 | PdfContentByte cb = _pdfWriter.getDirectContent();
|
---|
115 | com.lowagie.text.Image iTextImage = com.lowagie.text.Image.getInstance(image, null);
|
---|
116 | float angle = (float) (pic.getRotate() * Math.PI / 180);
|
---|
117 | double sin = Math.sin(angle), cos = Math.cos(angle);
|
---|
118 | int w = pic.getWidth(), h = pic.getHeight();
|
---|
119 | iTextImage.setAbsolutePosition((float) (pic.getX() + (w - Math.abs(w * cos + h * sin)) / 2),
|
---|
120 | (float) (_pdfWriter.getPageSize().getHeight() - pic.getY() - (h + Math.abs(w * sin + h * cos)) / 2));
|
---|
121 | iTextImage.scalePercent(pic.getScale() * 100);
|
---|
122 | iTextImage.setRotation(-angle);
|
---|
123 | cb.addImage(iTextImage);
|
---|
124 | } catch (DocumentException e) {
|
---|
125 | e.printStackTrace();
|
---|
126 | }
|
---|
127 | }
|
---|
128 |
|
---|
129 | @Override
|
---|
130 | protected void writeCircle(Circle circle) {
|
---|
131 | PdfContentByte cb = _pdfWriter.getDirectContent();
|
---|
132 | cb.circle(circle.getCenter().getX(), _pdfWriter.getPageSize().getHeight() - circle.getCenter().getY(), (float) circle.getRadius());
|
---|
133 | cb.setColorFill(circle.getFillColor());
|
---|
134 | if(circle.getThickness() > 0) {
|
---|
135 | cb.setLineWidth(circle.getThickness());
|
---|
136 | cb.setColorStroke(circle.getPaintColor());
|
---|
137 | cb.fillStroke();
|
---|
138 | } else {
|
---|
139 | cb.fill();
|
---|
140 | }
|
---|
141 | }
|
---|
142 |
|
---|
143 | private void fillPolygon(PdfContentByte cb, Polygon poly, Color fill, Color line, float lineThickness) {
|
---|
144 | if(poly != null) {
|
---|
145 | cb.moveTo(poly.xpoints[0], _height - poly.ypoints[0]);
|
---|
146 | for(int i = 1; i < poly.npoints; i++) {
|
---|
147 | cb.lineTo(poly.xpoints[i], _height - poly.ypoints[i]);
|
---|
148 | }
|
---|
149 | cb.closePath();
|
---|
150 | cb.setColorFill(fill);
|
---|
151 | if(lineThickness > 0) {
|
---|
152 | cb.setLineWidth(lineThickness);
|
---|
153 | cb.setColorStroke(line);
|
---|
154 | cb.fillStroke();
|
---|
155 | } else {
|
---|
156 | cb.fill();
|
---|
157 | }
|
---|
158 | }
|
---|
159 | }
|
---|
160 |
|
---|
161 | private List<Line> seenLines = new LinkedList<Line>();
|
---|
162 | @Override
|
---|
163 | protected void writeLine(Line line) throws IOException {
|
---|
164 | if(seenLines.contains(line)) {
|
---|
165 | return;
|
---|
166 | }
|
---|
167 | PdfContentByte cb = _pdfWriter.getDirectContent();
|
---|
168 | if(line.getStartItem().isEnclosed()) {
|
---|
169 | seenLines.add(line);
|
---|
170 | Line currentLine = line;
|
---|
171 | Item currentItem = line.getStartItem();
|
---|
172 | cb.moveTo(currentItem.getX(), _height - currentItem.getY());
|
---|
173 | for(;;) {
|
---|
174 | currentItem = currentLine.getOppositeEnd(currentItem);
|
---|
175 | cb.lineTo(currentItem.getX(), _height - currentItem.getY());
|
---|
176 | for(Line l : currentItem.getLines()) {
|
---|
177 | if(l.equals(currentLine)) {
|
---|
178 | continue;
|
---|
179 | }
|
---|
180 | currentLine = l;
|
---|
181 | break;
|
---|
182 | }
|
---|
183 | if(currentLine == null || currentLine.equals(line)) {
|
---|
184 | break;
|
---|
185 | }
|
---|
186 | seenLines.add(currentLine);
|
---|
187 | }
|
---|
188 | cb.closePath();
|
---|
189 | Color fill = currentItem.getFillColor();
|
---|
190 | if(fill != null) {
|
---|
191 | cb.setColorFill(fill);
|
---|
192 | if(currentItem.getThickness() > 0) {
|
---|
193 | cb.setLineWidth(currentItem.getThickness());
|
---|
194 | cb.setColorStroke(currentLine.getPaintColor());
|
---|
195 | cb.fillStroke();
|
---|
196 | } else {
|
---|
197 | cb.fill();
|
---|
198 | }
|
---|
199 | } else if(currentItem.getThickness() > 0) {
|
---|
200 | cb.setLineWidth(currentItem.getThickness());
|
---|
201 | cb.setColorStroke(currentLine.getPaintColor());
|
---|
202 | cb.stroke();
|
---|
203 | }
|
---|
204 | } else {
|
---|
205 | for(Item l : line.getStartItem().getAllConnected()) {
|
---|
206 | if(l instanceof Line && !seenLines.contains(l)) {
|
---|
207 | seenLines.add((Line) l);
|
---|
208 | Item start = ((Line) l).getStartItem();
|
---|
209 | Item end = ((Line) l).getEndItem();
|
---|
210 | cb.moveTo(start.getX(), _height - start.getY());
|
---|
211 | cb.lineTo(end.getX(), _height - end.getY());
|
---|
212 | cb.closePath();
|
---|
213 | if(l.getThickness() >= 0) {
|
---|
214 | cb.setLineWidth(l.getThickness());
|
---|
215 | cb.setColorStroke(l.getPaintColor());
|
---|
216 | cb.stroke();
|
---|
217 | }
|
---|
218 | fillPolygon(cb, ((Line) l).getStartArrow(), l.getPaintColor(), l.getPaintColor(), l.getThickness());
|
---|
219 | fillPolygon(cb, ((Line) l).getEndArrow(), l.getPaintColor(), l.getPaintColor(), l.getThickness());
|
---|
220 | }
|
---|
221 | }
|
---|
222 | }
|
---|
223 | }
|
---|
224 |
|
---|
225 | @Override
|
---|
226 | protected Collection<Item> getItemsToWrite(Frame toWrite) {
|
---|
227 | return toWrite.getVisibleItems();
|
---|
228 | }
|
---|
229 | }
|
---|