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