1 package net.sf.openrocket.gui.print.visitor;
3 import com.itextpdf.text.Document;
4 import com.itextpdf.text.DocumentException;
5 import com.itextpdf.text.Rectangle;
6 import com.itextpdf.text.pdf.PdfContentByte;
7 import com.itextpdf.text.pdf.PdfWriter;
8 import net.sf.openrocket.gui.print.FinMarkingGuide;
9 import net.sf.openrocket.gui.print.ITextHelper;
10 import net.sf.openrocket.logging.LogHelper;
11 import net.sf.openrocket.rocketcomponent.Rocket;
12 import net.sf.openrocket.startup.Application;
15 import java.awt.image.BufferedImage;
18 * A strategy for drawing a fin marking guide. As currently implemented, each body tube with a finset will have
19 * a marking guide. If a tube has multiple fin sets, they are combined onto one marking guide. Launch lugs are supported
22 public class FinMarkingGuideStrategy {
27 private static final LogHelper log = Application.getLogger();
32 protected Document document;
35 * The direct iText writer.
37 protected PdfWriter writer;
42 * @param doc The iText document
43 * @param theWriter The direct iText writer
45 public FinMarkingGuideStrategy(Document doc, PdfWriter theWriter) {
51 * Recurse through the given rocket component.
53 * @param root the root component; all children will be visited recursively
55 public void writeToDocument(final Rocket root) {
61 * The core behavior of this strategy.
63 * @param rocket the rocket to render all
65 private void render(final Rocket rocket) {
67 FinMarkingGuide pfs = new FinMarkingGuide(rocket);
69 java.awt.Dimension size = pfs.getSize();
70 final Dimension pageSize = getPageSize();
71 if (fitsOnOnePage(pageSize, size.getWidth(), size.getHeight())) {
74 BufferedImage image = (BufferedImage) pfs.createImage();
75 ITextHelper.renderImageAcrossPages(new Rectangle(pageSize.getWidth(), pageSize.getHeight()),
76 document, writer, image);
78 } catch (DocumentException e) {
79 log.error("Could not render the fin marking guide.", e);
84 * Determine if the image will fit on the given page.
86 * @param pageSize the page size
87 * @param wImage the width of the thing to be printed
88 * @param hImage the height of the thing to be printed
89 * @return true if the thing to be printed will fit on a single page
91 private boolean fitsOnOnePage(Dimension pageSize, double wImage, double hImage) {
92 double wPage = pageSize.getWidth();
93 double hPage = pageSize.getHeight();
95 int wRatio = (int) Math.ceil(wImage / wPage);
96 int hRatio = (int) Math.ceil(hImage / hPage);
98 return wRatio <= 1.0d && hRatio <= 1.0d;
102 * Print the transition.
104 * @param theMarkingGuide the fin marking guide
106 private void printOnOnePage(final FinMarkingGuide theMarkingGuide) {
107 Dimension d = getPageSize();
108 PdfContentByte cb = writer.getDirectContent();
109 Graphics2D g2 = cb.createGraphics(d.width, d.height);
110 theMarkingGuide.print(g2);
116 * Get the dimensions of the paper page.
118 * @return an internal Dimension
120 protected Dimension getPageSize() {
121 return new Dimension(document.getPageSize().getWidth(),
122 document.getPageSize().getHeight());
126 * Convenience class to model a dimension.
144 public Dimension(float w, float h) {
154 public float getWidth() {
163 public float getHeight() {