2 * FinSetPrintStrategy.java
4 package net.sf.openrocket.gui.print.visitor;
6 import com.itextpdf.text.Document;
7 import com.itextpdf.text.DocumentException;
8 import com.itextpdf.text.Rectangle;
9 import com.itextpdf.text.pdf.PdfContentByte;
10 import com.itextpdf.text.pdf.PdfWriter;
11 import net.sf.openrocket.gui.print.ITextHelper;
12 import net.sf.openrocket.gui.print.PrintUnit;
13 import net.sf.openrocket.gui.print.PrintableFinSet;
14 import net.sf.openrocket.logging.LogHelper;
15 import net.sf.openrocket.rocketcomponent.FinSet;
16 import net.sf.openrocket.rocketcomponent.RocketComponent;
17 import net.sf.openrocket.startup.Application;
20 import java.awt.image.BufferedImage;
21 import java.util.ArrayList;
22 import java.util.List;
23 import java.util.ListIterator;
27 * A strategy for drawing fin templates.
29 public class FinSetPrintStrategy {
34 private static final LogHelper log = Application.getLogger();
39 protected Document document;
42 * The direct iText writer.
44 protected PdfWriter writer;
47 * The stages selected.
49 protected Set<Integer> stages;
52 * Strategy for fitting multiple components onto a page.
54 protected PageFitPrintStrategy pageFitPrint;
59 * @param doc The iText document
60 * @param theWriter The direct iText writer
61 * @param theStages The stages to be printed by this strategy
63 public FinSetPrintStrategy(Document doc, PdfWriter theWriter, Set<Integer> theStages, PageFitPrintStrategy pageFit) {
67 pageFitPrint = pageFit;
71 * Recurse through the given rocket component.
73 * @param root the root component; all children will be printed recursively
75 public void writeToDocument (final RocketComponent root) {
76 List<RocketComponent> rc = root.getChildren();
82 * Recurse through the given rocket component.
84 * @param theRc an array of rocket components; all children will be printed recursively
86 protected void goDeep (final List<RocketComponent> theRc) {
87 for (RocketComponent rocketComponent : theRc) {
88 if (rocketComponent instanceof FinSet) {
89 printFinSet((FinSet) rocketComponent);
91 else if (rocketComponent.getChildCount() > 0) {
92 goDeep(rocketComponent.getChildren());
98 * The core behavior of this strategy.
100 * @param finSet the object to extract info about; a graphical image of the fin shape is drawn to the document
102 private void printFinSet(final FinSet finSet) {
103 if (shouldPrintStage(finSet.getStageNumber())) {
105 PrintableFinSet pfs = new PrintableFinSet(finSet);
107 java.awt.Dimension finSize = pfs.getSize();
108 final Dimension pageSize = getPageSize();
109 if (fitsOnOnePage(pageSize, finSize.getWidth(), finSize.getHeight())) {
110 pageFitPrint.addComponent(pfs);
113 BufferedImage image = (BufferedImage) pfs.createImage();
114 ITextHelper.renderImageAcrossPages(new Rectangle(pageSize.getWidth(), pageSize.getHeight()),
115 document, writer, image);
119 catch (DocumentException e) {
120 log.error("Could not render fin.", e);
126 * Determine if the strategy's set of stage numbers (to print) contains the specified stage.
128 * @param stageNumber a stage number
130 * @return true if the strategy contains the stage number provided
132 public boolean shouldPrintStage(int stageNumber) {
133 if (stages == null || stages.isEmpty()) {
137 for (final Integer stage : stages) {
138 if (stage == stageNumber) {
147 * Determine if the image will fit on the given page.
149 * @param pageSize the page size
150 * @param wImage the width of the thing to be printed
151 * @param hImage the height of the thing to be printed
153 * @return true if the thing to be printed will fit on a single page
155 private boolean fitsOnOnePage (Dimension pageSize, double wImage, double hImage) {
156 double wPage = pageSize.getWidth();
157 double hPage = pageSize.getHeight();
159 int wRatio = (int) Math.ceil(wImage / wPage);
160 int hRatio = (int) Math.ceil(hImage / hPage);
162 return wRatio <= 1.0d && hRatio <= 1.0d;
166 * Get the dimensions of the paper page.
168 * @return an internal Dimension
170 protected Dimension getPageSize () {
171 return new Dimension(document.getPageSize().getWidth(),
172 document.getPageSize().getHeight());
176 * Convenience class to model a dimension.
179 /** Width, in points. */
181 /** Height, in points. */
189 public Dimension (float w, float h) {
199 public float getWidth () {
208 public float getHeight () {