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.PdfWriter;
7 import net.sf.openrocket.gui.print.AbstractPrintable;
8 import net.sf.openrocket.gui.print.ITextHelper;
9 import net.sf.openrocket.gui.print.PrintableCenteringRing;
10 import net.sf.openrocket.logging.LogHelper;
11 import net.sf.openrocket.rocketcomponent.CenteringRing;
12 import net.sf.openrocket.rocketcomponent.RocketComponent;
13 import net.sf.openrocket.startup.Application;
15 import java.awt.image.BufferedImage;
16 import java.util.List;
20 * A strategy for printing a centering ring to iText.
22 public class CenteringRingStrategy {
27 private static final LogHelper log = Application.getLogger();
32 protected Document document;
35 * The direct iText writer.
37 protected PdfWriter writer;
40 * The stages selected.
42 protected Set<Integer> stages;
45 * Strategy for fitting multiple components onto a page.
47 protected PageFitPrintStrategy pageFitPrint;
52 * @param doc The iText document
53 * @param theWriter The direct iText writer
54 * @param theStagesToVisit The stages to be visited by this strategy
56 public CenteringRingStrategy(Document doc, PdfWriter theWriter, Set<Integer> theStagesToVisit, PageFitPrintStrategy pageFit) {
59 stages = theStagesToVisit;
60 pageFitPrint = pageFit;
64 * Recurse through the given rocket component.
66 * @param root the root component; all children will be visited recursively
68 public void writeToDocument(final RocketComponent root) {
69 List<RocketComponent> rc = root.getChildren();
75 * Recurse through the given rocket component.
77 * @param theRc an array of rocket components; all children will be visited recursively
79 protected void goDeep(final List<RocketComponent> theRc) {
80 for (RocketComponent rocketComponent : theRc) {
81 if (rocketComponent instanceof CenteringRing) {
82 render((CenteringRing) rocketComponent);
84 else if (rocketComponent.getChildCount() > 0) {
85 goDeep(rocketComponent.getChildren());
91 * The core behavior of this visitor.
93 * @param component the object to extract info about; a graphical image of the centering ring shape is drawn to the
96 private void render(final CenteringRing component) {
98 AbstractPrintable pfs;
99 pfs = new PrintableCenteringRing(component);
101 java.awt.Dimension size = pfs.getSize();
102 final Dimension pageSize = getPageSize();
103 if (fitsOnOnePage(pageSize, size.getWidth(), size.getHeight())) {
104 pageFitPrint.addComponent(pfs);
107 BufferedImage image = (BufferedImage) pfs.createImage();
108 ITextHelper.renderImageAcrossPages(new Rectangle(pageSize.getWidth(), pageSize.getHeight()),
109 document, writer, image);
112 catch (DocumentException e) {
113 log.error("Could not render the centering ring.", e);
118 * Determine if the image will fit on the given page.
120 * @param pageSize the page size
121 * @param wImage the width of the thing to be printed
122 * @param hImage the height of the thing to be printed
124 * @return true if the thing to be printed will fit on a single page
126 private boolean fitsOnOnePage(Dimension pageSize, double wImage, double hImage) {
127 double wPage = pageSize.getWidth();
128 double hPage = pageSize.getHeight();
130 int wRatio = (int) Math.ceil(wImage / wPage);
131 int hRatio = (int) Math.ceil(hImage / hPage);
133 return wRatio <= 1.0d && hRatio <= 1.0d;
137 * Get the dimensions of the paper page.
139 * @return an internal Dimension
141 protected Dimension getPageSize() {
142 return new Dimension(document.getPageSize().getWidth(),
143 document.getPageSize().getHeight());
147 * Convenience class to model a dimension.
165 public Dimension(float w, float h) {
175 public float getWidth() {
184 public float getHeight() {