create changelog entry
[debian/openrocket] / core / src / net / sf / openrocket / gui / print / ITextHelper.java
1 /*
2  * ITextHelper.java
3  */
4 package net.sf.openrocket.gui.print;
5
6 import java.awt.Graphics2D;
7 import java.awt.image.BufferedImage;
8
9 import com.itextpdf.text.Chunk;
10 import com.itextpdf.text.Document;
11 import com.itextpdf.text.DocumentException;
12 import com.itextpdf.text.Font;
13 import com.itextpdf.text.Paragraph;
14 import com.itextpdf.text.Phrase;
15 import com.itextpdf.text.Rectangle;
16 import com.itextpdf.text.pdf.BaseFont;
17 import com.itextpdf.text.pdf.PdfContentByte;
18 import com.itextpdf.text.pdf.PdfPCell;
19 import com.itextpdf.text.pdf.PdfPTable;
20 import com.itextpdf.text.pdf.PdfWriter;
21
22 /**
23  * A bunch of helper methods for creating iText components.
24  */
25 public final class ITextHelper {
26
27         public static BaseFont getBaseFont(){
28                 try {
29                 return BaseFont.createFont("/dejavu-font/DejaVuSerif.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
30                 } catch (Exception ex ) {
31                         throw new RuntimeException(ex);
32                 }
33         }
34     /**
35      * Create a cell for an iText table.
36      *
37      * @return a cell with bottom border
38      */
39     public static PdfPCell createCell () {
40         return createCell(Rectangle.BOTTOM);
41     }
42
43     /**
44      * Create a cell for an iText table with the given border location.
45      *
46      * @param border the border location
47      *
48      * @return a cell with given border
49      */
50     public static PdfPCell createCell (int border) {
51         PdfPCell result = new PdfPCell();
52         result.setBorder(border);
53
54         return result;
55     }
56
57     /**
58      * Create a cell whose contents are a table.  No border.
59      *
60      * @param table the table to insert into the cell
61      *
62      * @return the cell containing a table
63      */
64     public static PdfPCell createCell (PdfPTable table) {
65         PdfPCell result = new PdfPCell();
66         result.setBorder(PdfPCell.NO_BORDER);
67         result.addElement(table);
68
69         return result;
70     }
71
72     /**
73      * Create a cell whose contents are the given string. No border.  Standard PrintUtilities.NORMAL font.
74      *
75      * @param v the text of the cell.
76      *
77      * @return the cell containing the text
78      */
79     public static PdfPCell createCell (String v) {
80         return createCell(v, Rectangle.NO_BORDER, PrintUtilities.NORMAL);
81     }
82
83     /**
84      * Create a cell whose contents are the given string , rendered with the given font.  No border.
85      *
86      * @param v    the text of the cell
87      * @param font the font
88      *
89      * @return the cell containing the text
90      */
91     public static PdfPCell createCell (String v, Font font) {
92         return createCell(v, Rectangle.NO_BORDER, font);
93     }
94
95     /**
96      * Create a cell whose contents are the given string with specified left and right padding (spacing).
97      *
98      * @param v        the text of the cell
99      * @param leftPad  the number of points to precede the text
100      * @param rightPad the number of points to follow the text
101      *
102      * @return the cell containing the text
103      */
104     public static PdfPCell createCell (String v, int leftPad, int rightPad) {
105         PdfPCell c = createCell(v, Rectangle.NO_BORDER, PrintUtilities.NORMAL);
106         c.setPaddingLeft(leftPad);
107         c.setPaddingRight(rightPad);
108         return c;
109     }
110
111     /**
112      * Create a cell whose contents are the given string with the given border.  Uses NORMAL font.
113      *
114      * @param v      the text of the cell
115      * @param border the border type
116      *
117      * @return the cell containing the text
118      */
119     public static PdfPCell createCell (String v, int border) {
120         return createCell(v, border, PrintUtilities.NORMAL);
121     }
122
123     /**
124      * Complete create cell - fully qualified.  Create a cell whose contents are the given string with the given border
125      * and font.
126      *
127      * @param v      the text of the cell
128      * @param border the border type
129      * @param font   the font
130      *
131      * @return the cell containing the text
132      */
133     public static PdfPCell createCell (String v, int border, Font font) {
134         PdfPCell result = new PdfPCell();
135         result.setBorder(border);
136         Chunk c = new Chunk();
137         c.setFont(font);
138         c.append(v);
139         result.addElement(c);
140         return result;
141     }
142
143     /**
144      * Create a phrase with the given text and font.
145      *
146      * @param text the text
147      * @param font the font
148      *
149      * @return an iText phrase
150      */
151     public static Phrase createPhrase (String text, Font font) {
152         Phrase p = new Phrase();
153         final Chunk chunk = new Chunk(text);
154         chunk.setFont(font);
155         p.add(chunk);
156         return p;
157     }
158
159     /**
160      * Create a phrase with the given text.
161      *
162      * @param text the text
163      *
164      * @return an iText phrase
165      */
166     public static Phrase createPhrase (String text) {
167         return createPhrase(text, PrintUtilities.NORMAL);
168     }
169
170     /**
171      * Create a paragraph with the given text and font.
172      *
173      * @param text the text
174      * @param font the font
175      *
176      * @return an iText paragraph
177      */
178     public static Paragraph createParagraph (String text, Font font) {
179         Paragraph p = new Paragraph();
180         final Chunk chunk = new Chunk(text);
181         chunk.setFont(font);
182         p.add(chunk);
183         return p;
184     }
185
186     /**
187      * Create a paragraph with the given text and using NORMAL font.
188      *
189      * @param text the text
190      *
191      * @return an iText paragraph
192      */
193     public static Paragraph createParagraph (String text) {
194         return createParagraph(text, PrintUtilities.NORMAL);
195     }
196
197     /**
198      * Break a large image up into page-size pieces and output each page in order to an iText document.  The image is
199      * overlayed with an matrix of pages running from left to right until the right side of the image is reached. Then
200      * the next 'row' of pages is output from left to right, and so on.
201      *
202      * @param pageSize a rectangle that defines the bounds of the page size
203      * @param doc      the iText document
204      * @param writer   the underlying content writer
205      * @param image    the source image
206      *
207      * @throws DocumentException thrown if the document could not be written
208      */
209     public static void renderImageAcrossPages (Rectangle pageSize, Document doc, PdfWriter writer, java.awt.Image image)
210             throws DocumentException {
211         final int margin = (int)Math.min(doc.topMargin(), PrintUnit.POINTS_PER_INCH * 0.3f);
212         float wPage = pageSize.getWidth() - 2 * margin;
213         float hPage = pageSize.getHeight() - 2 * margin;
214
215         float wImage = image.getWidth(null);
216         float hImage = image.getHeight(null);
217         java.awt.Rectangle crop = new java.awt.Rectangle(0, 0, (int) Math.min(wPage, wImage), (int) Math.min(hPage,
218                                                                                                              hImage));
219         PdfContentByte content = writer.getDirectContent();
220
221         int ymargin = 0;
222
223         while (true) {
224             BufferedImage subImage = ((BufferedImage) image).getSubimage((int) crop.getX(), (int) crop.getY(),
225                                                                          (int) crop.getWidth(), (int) crop.getHeight());
226
227             Graphics2D g2 = content.createGraphics(pageSize.getWidth(), pageSize.getHeight());
228             g2.drawImage(subImage, margin, ymargin, null);
229             g2.dispose();
230
231             // After the first page, the y-margin needs to be set.
232             ymargin = margin;
233
234             final int newX = (int) (crop.getWidth() + crop.getX());
235             if (newX < wImage) {
236                 double adjust = Math.min(wImage - newX, wPage);
237                 crop = new java.awt.Rectangle(newX, (int) crop.getY(), (int) adjust,
238                                               (int) crop.getHeight());
239             }
240             else {
241                 final int newY = (int) (crop.getHeight() + crop.getY());
242                 if (newY < hImage) {
243                     double adjust = Math.min(hImage - newY, hPage);
244                     crop = new java.awt.Rectangle(0, newY, (int) Math.min(wPage, wImage), (int) adjust);
245                 }
246                 else {
247                     break;
248                 }
249             }
250             doc.newPage();
251         }
252     }
253
254 }