Merge pull request #30 from rodinia814/printing

Printing fixes; added Save As file to MRU
This commit is contained in:
plaa 2013-01-02 21:40:05 -08:00
commit e31f94ed99
15 changed files with 753 additions and 968 deletions

File diff suppressed because it is too large Load Diff

View File

@ -22,11 +22,10 @@ public abstract class AbstractPrintable<T> extends PrintableComponent {
/**
* Constructor. Initialize this printable with the component to be printed.
*
* @param isDoubleBuffered a boolean, true for double-buffering
* @param transition the component to be printed
* @param component the component to be printed
*/
public AbstractPrintable(boolean isDoubleBuffered, T transition) {
init(transition);
public AbstractPrintable(T component) {
init(component);
}
/**
@ -50,8 +49,8 @@ public abstract class AbstractPrintable<T> extends PrintableComponent {
* @return an awt image of the printable component
*/
public Image createImage() {
int width = getWidth() + getOffsetX();
int height = getHeight() + getOffsetY();
int width = getWidth();
int height = getHeight();
// Create a buffered image in which to draw
BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
// Create a graphics contents on the buffered image
@ -73,8 +72,12 @@ public abstract class AbstractPrintable<T> extends PrintableComponent {
g2.setColor(Color.BLACK);
g2.setStroke(thinStroke);
g2.translate(getOffsetX(), getOffsetY());
translate(g2);
draw(g2);
}
protected void translate(final Graphics2D theG2) {
theG2.translate(getOffsetX(), getOffsetY());
}
}

View File

@ -3,9 +3,6 @@
*/
package net.sf.openrocket.gui.print;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
@ -19,24 +16,29 @@ import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
/**
* A bunch of helper methods for creating iText components.
*/
public final class ITextHelper {
public static BaseFont getBaseFont(){
try {
return BaseFont.createFont("/dejavu-font/DejaVuSerif.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
} catch (Exception ex ) {
throw new RuntimeException(ex);
}
}
public static BaseFont getBaseFont() {
try {
return BaseFont.createFont("/dejavu-font/DejaVuSerif.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
}
catch (Exception ex) {
throw new RuntimeException(ex);
}
}
/**
* Create a cell for an iText table.
*
* @return a cell with bottom border
*/
public static PdfPCell createCell () {
public static PdfPCell createCell() {
return createCell(Rectangle.BOTTOM);
}
@ -47,7 +49,7 @@ public final class ITextHelper {
*
* @return a cell with given border
*/
public static PdfPCell createCell (int border) {
public static PdfPCell createCell(int border) {
PdfPCell result = new PdfPCell();
result.setBorder(border);
@ -61,7 +63,7 @@ public final class ITextHelper {
*
* @return the cell containing a table
*/
public static PdfPCell createCell (PdfPTable table) {
public static PdfPCell createCell(PdfPTable table) {
PdfPCell result = new PdfPCell();
result.setBorder(PdfPCell.NO_BORDER);
result.addElement(table);
@ -76,7 +78,7 @@ public final class ITextHelper {
*
* @return the cell containing the text
*/
public static PdfPCell createCell (String v) {
public static PdfPCell createCell(String v) {
return createCell(v, Rectangle.NO_BORDER, PrintUtilities.NORMAL);
}
@ -88,7 +90,7 @@ public final class ITextHelper {
*
* @return the cell containing the text
*/
public static PdfPCell createCell (String v, Font font) {
public static PdfPCell createCell(String v, Font font) {
return createCell(v, Rectangle.NO_BORDER, font);
}
@ -101,7 +103,7 @@ public final class ITextHelper {
*
* @return the cell containing the text
*/
public static PdfPCell createCell (String v, int leftPad, int rightPad) {
public static PdfPCell createCell(String v, int leftPad, int rightPad) {
PdfPCell c = createCell(v, Rectangle.NO_BORDER, PrintUtilities.NORMAL);
c.setPaddingLeft(leftPad);
c.setPaddingRight(rightPad);
@ -116,13 +118,12 @@ public final class ITextHelper {
*
* @return the cell containing the text
*/
public static PdfPCell createCell (String v, int border) {
public static PdfPCell createCell(String v, int border) {
return createCell(v, border, PrintUtilities.NORMAL);
}
/**
* Complete create cell - fully qualified. Create a cell whose contents are the given string with the given border
* and font.
* Complete create cell - fully qualified. Create a cell whose contents are the given string with the given border and font.
*
* @param v the text of the cell
* @param border the border type
@ -130,7 +131,7 @@ public final class ITextHelper {
*
* @return the cell containing the text
*/
public static PdfPCell createCell (String v, int border, Font font) {
public static PdfPCell createCell(String v, int border, Font font) {
PdfPCell result = new PdfPCell();
result.setBorder(border);
Chunk c = new Chunk();
@ -148,7 +149,7 @@ public final class ITextHelper {
*
* @return an iText phrase
*/
public static Phrase createPhrase (String text, Font font) {
public static Phrase createPhrase(String text, Font font) {
Phrase p = new Phrase();
final Chunk chunk = new Chunk(text);
chunk.setFont(font);
@ -163,7 +164,7 @@ public final class ITextHelper {
*
* @return an iText phrase
*/
public static Phrase createPhrase (String text) {
public static Phrase createPhrase(String text) {
return createPhrase(text, PrintUtilities.NORMAL);
}
@ -175,7 +176,7 @@ public final class ITextHelper {
*
* @return an iText paragraph
*/
public static Paragraph createParagraph (String text, Font font) {
public static Paragraph createParagraph(String text, Font font) {
Paragraph p = new Paragraph();
final Chunk chunk = new Chunk(text);
chunk.setFont(font);
@ -190,14 +191,13 @@ public final class ITextHelper {
*
* @return an iText paragraph
*/
public static Paragraph createParagraph (String text) {
public static Paragraph createParagraph(String text) {
return createParagraph(text, PrintUtilities.NORMAL);
}
/**
* Break a large image up into page-size pieces and output each page in order to an iText document. The image is
* overlayed with an matrix of pages running from left to right until the right side of the image is reached. Then
* the next 'row' of pages is output from left to right, and so on.
* Break a large image up into page-size pieces and output each page in order to an iText document. The image is overlayed with an matrix of pages
* running from left to right until the right side of the image is reached. Then the next 'row' of pages is output from left to right, and so on.
*
* @param pageSize a rectangle that defines the bounds of the page size
* @param doc the iText document
@ -206,9 +206,9 @@ public final class ITextHelper {
*
* @throws DocumentException thrown if the document could not be written
*/
public static void renderImageAcrossPages (Rectangle pageSize, Document doc, PdfWriter writer, java.awt.Image image)
public static void renderImageAcrossPages(Rectangle pageSize, Document doc, PdfWriter writer, java.awt.Image image)
throws DocumentException {
final int margin = (int)Math.min(doc.topMargin(), PrintUnit.POINTS_PER_INCH * 0.3f);
final int margin = (int) Math.min(doc.topMargin(), PrintUnit.POINTS_PER_INCH * 0.3f);
float wPage = pageSize.getWidth() - 2 * margin;
float hPage = pageSize.getHeight() - 2 * margin;
@ -218,7 +218,7 @@ public final class ITextHelper {
hImage));
PdfContentByte content = writer.getDirectContent();
int ymargin = 0;
int ymargin = margin;
while (true) {
BufferedImage subImage = ((BufferedImage) image).getSubimage((int) crop.getX(), (int) crop.getY(),
@ -228,20 +228,21 @@ public final class ITextHelper {
g2.drawImage(subImage, margin, ymargin, null);
g2.dispose();
// After the first page, the y-margin needs to be set.
ymargin = margin;
final int newImageX = (int) (crop.getWidth() + crop.getX());
final int newX = (int) (crop.getWidth() + crop.getX());
if (newX < wImage) {
double adjust = Math.min(wImage - newX, wPage);
crop = new java.awt.Rectangle(newX, (int) crop.getY(), (int) adjust,
if (newImageX < wImage) {
//Spans multiple pages horizontally
double subImageWidth = Math.min(wImage - newImageX, wPage);
crop = new java.awt.Rectangle(newImageX, (int) crop.getY(), (int) subImageWidth,
(int) crop.getHeight());
}
else {
final int newY = (int) (crop.getHeight() + crop.getY());
if (newY < hImage) {
double adjust = Math.min(hImage - newY, hPage);
crop = new java.awt.Rectangle(0, newY, (int) Math.min(wPage, wImage), (int) adjust);
//Spans multiple pages vertically
final int newImageY = (int) (crop.getHeight() + crop.getY());
if (newImageY < hImage) {
double subImageHeight = Math.min(hImage - newImageY, hPage);
crop = new java.awt.Rectangle(0, newImageY, (int) Math.min(wPage, wImage), (int) subImageHeight);
}
else {
break;

View File

@ -89,15 +89,15 @@ public class PrintController {
break;
case TRANSITION_TEMPLATE:
final TransitionStrategy tranWriter = new TransitionStrategy(idoc, writer, stages, pageFitPrint);
if (tranWriter.writeToDocument(doc.getRocket(), false)) {
final TransitionStrategy tranWriter = new TransitionStrategy(idoc, writer, stages, pageFitPrint, false);
if (tranWriter.writeToDocument(doc.getRocket())) {
addRule = true;
}
break;
case NOSE_CONE_TEMPLATE:
final TransitionStrategy coneWriter = new TransitionStrategy(idoc, writer, stages, pageFitPrint);
if (coneWriter.writeToDocument(doc.getRocket(), true)) {
final TransitionStrategy coneWriter = new TransitionStrategy(idoc, writer, stages, pageFitPrint, true);
if (coneWriter.writeToDocument(doc.getRocket())) {
addRule = true;
}
break;
@ -124,7 +124,7 @@ public class PrintController {
}
// Write out parts that we are going to combine onto single sheets of paper
pageFitPrint.writeToDocument(doc.getRocket());
pageFitPrint.writeToDocument();
idoc.newPage();
//Stupid iText throws a really nasty exception if there is no data when close is called.

View File

@ -1,6 +1,6 @@
package net.sf.openrocket.gui.print;
import net.sf.openrocket.gui.print.visitor.CenteringRingStrategy;
import net.sf.openrocket.gui.print.visitor.Dimension;
import net.sf.openrocket.rocketcomponent.CenteringRing;
import net.sf.openrocket.rocketcomponent.ClusterConfiguration;
import net.sf.openrocket.rocketcomponent.InnerTube;
@ -34,7 +34,7 @@ public class PrintableCenteringRing extends AbstractPrintable<CenteringRing> {
/**
* A set of the inner 'holes'. At least one, but will have many if clustered.
*/
private Set<CenteringRingStrategy.Dimension> innerCenterPoints = new HashSet<CenteringRingStrategy.Dimension>();
private Set<Dimension> innerCenterPoints = new HashSet<Dimension>();
/**
* Construct a simple, non-clustered, printable centering ring, or if the motor mount represents a clustered
@ -44,12 +44,12 @@ public class PrintableCenteringRing extends AbstractPrintable<CenteringRing> {
* @param theMotorMount the motor mount if clustered, else null
*/
private PrintableCenteringRing(CenteringRing theRing, InnerTube theMotorMount) {
super(false, theRing);
super(theRing);
if (theMotorMount == null || theMotorMount.getClusterConfiguration().equals(ClusterConfiguration.SINGLE)) {
//Single motor.
final float v = (float) PrintUnit.METERS.toPoints(target.getOuterRadius());
innerCenterPoints.add(
new CenteringRingStrategy.Dimension(v, v,
new Dimension(v, v,
(float) PrintUnit.METERS.toPoints((target.getInnerRadius()))));
}
else {
@ -70,7 +70,7 @@ public class PrintableCenteringRing extends AbstractPrintable<CenteringRing> {
* @param theMotorMounts a list of the motor mount tubes that are physically supported by the centering ring
*/
private PrintableCenteringRing(CenteringRing theRing, List<InnerTube> theMotorMounts) {
super(false, theRing);
super(theRing);
List<Coordinate> points = new ArrayList<Coordinate>();
//Transform the radial positions of the tubes.
for (InnerTube it : theMotorMounts) {
@ -117,7 +117,7 @@ public class PrintableCenteringRing extends AbstractPrintable<CenteringRing> {
private void populateCenterPoints(final List<Coordinate> theCoords) {
float radius = (float) PrintUnit.METERS.toPoints(target.getOuterRadius());
for (Coordinate coordinate : theCoords) {
innerCenterPoints.add(new CenteringRingStrategy.Dimension(
innerCenterPoints.add(new Dimension(
(float) PrintUnit.METERS.toPoints(coordinate.y) + radius, //center point x
(float) PrintUnit.METERS.toPoints(coordinate.z) + radius, //center point y
(float) PrintUnit.METERS.toPoints(coordinate.x))); //radius of motor mount
@ -153,7 +153,7 @@ public class PrintableCenteringRing extends AbstractPrintable<CenteringRing> {
g2.setColor(Color.black);
g2.draw(outerCircle);
for (CenteringRingStrategy.Dimension next : innerCenterPoints) {
for (Dimension next : innerCenterPoints) {
drawInnerCircle(g2, next.getWidth(), next.getHeight(), next.getBreadth());
}
g2.setColor(original);

View File

@ -92,7 +92,6 @@ public class PrintableComponent extends JPanel implements Printable, Comparable<
return offsetY;
}
/**
* Compares this object with the specified object for order. Returns a negative integer, zero, or a positive integer
* as this object is less than, equal to, or greater than the specified object.

View File

@ -6,33 +6,29 @@ package net.sf.openrocket.gui.print;
import net.sf.openrocket.rocketcomponent.FinSet;
import net.sf.openrocket.util.Coordinate;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.geom.GeneralPath;
import java.awt.image.BufferedImage;
/**
* This class allows for a FinSet to be printable. It does so by decorating an existing finset (which will not be
* modified) and rendering it within a JPanel. The JPanel is not actually visualized on a display, but instead renders
* it to a print device.
*/
public class PrintableFinSet extends PrintableComponent {
public class PrintableFinSet extends AbstractPrintable<FinSet> {
/**
* The object that represents the shape (outline) of the fin. This gets drawn onto the Swing component.
*/
protected GeneralPath polygon = null;
protected GeneralPath polygon;
/**
* The minimum X coordinate.
*/
private int minX = 0;
private int minX;
/**
* The minimum Y coordinate.
*/
private int minY = 0;
private int minY;
/**
* Constructor.
@ -40,28 +36,23 @@ public class PrintableFinSet extends PrintableComponent {
* @param fs the finset to print
*/
public PrintableFinSet (FinSet fs) {
this(fs.getFinPointsWithTab());
}
/**
* Construct a fin set from a set of points.
*
* @param points an array of points.
*/
public PrintableFinSet (Coordinate[] points) {
init(points);
super(fs);
}
/**
* Initialize the fin set polygon and set the size of the component.
*
* @param points an array of points.
* @param component the fin set
*/
private void init (Coordinate[] points) {
protected void init (FinSet component) {
Coordinate[] points = component.getFinPointsWithTab();
polygon = new GeneralPath(GeneralPath.WIND_EVEN_ODD, points.length);
polygon.moveTo(0, 0);
minX = 0;
minY = 0;
int maxX = 0;
int maxY = 0;
@ -76,29 +67,7 @@ public class PrintableFinSet extends PrintableComponent {
}
polygon.closePath();
setSize(maxX - minX, maxY - minY);
}
/**
* Returns a generated image of the fin set. May then be used wherever AWT images can be used, or converted to
* another image/picture format and used accordingly.
*
* @return an awt image of the fin set
*/
public Image createImage () {
int width = getWidth();
int height = getHeight();
// Create a buffered image in which to draw
BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
// Create a graphics contents on the buffered image
Graphics2D g2d = bufferedImage.createGraphics();
// Draw graphics
g2d.setBackground(Color.white);
g2d.clearRect(0, 0, width, height);
paintComponent(g2d);
// Graphics context no longer needed so dispose it
g2d.dispose();
return bufferedImage;
setSize(maxX - minX + 1, maxY - minY + 1);
}
/**
@ -106,12 +75,9 @@ public class PrintableFinSet extends PrintableComponent {
* outline of the fin set coordinates to create a polygon, which is then drawn onto the graphics context.
* Through-the-wall fin tabs are supported if they are present.
*
* @param g the Java2D graphics context
* @param g2d the Java2D graphics context
*/
@Override
public void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
protected void draw(Graphics2D g2d) {
int x = 0;
int y = 0;
@ -132,4 +98,13 @@ public class PrintableFinSet extends PrintableComponent {
g2d.setPaint(TemplateProperties.getLineColor());
g2d.draw(polygon);
}
/**
* Don't let super class translate the coordinates - we'll do that ourselves in the draw method.
*
* @param theG2 the graphics context
*/
@Override
protected void translate(final Graphics2D theG2) {
}
}

View File

@ -1,5 +1,6 @@
package net.sf.openrocket.gui.print;
import net.sf.openrocket.gui.print.visitor.PageFitPrintStrategy;
import net.sf.openrocket.gui.rocketfigure.TransitionShapes;
import net.sf.openrocket.rocketcomponent.NoseCone;
import net.sf.openrocket.util.Transformation;
@ -10,50 +11,71 @@ import java.awt.Shape;
public class PrintableNoseCone extends AbstractPrintable<NoseCone> {
/**
* If the component to be drawn is a nose cone, save a reference to it.
*/
private NoseCone target;
/**
* If the component to be drawn is a nose cone, save a reference to it.
*/
private NoseCone target;
/**
* Construct a printable nose cone.
*
* @param noseCone the component to print
*/
public PrintableNoseCone(NoseCone noseCone) {
super(false, noseCone);
}
/**
* Offset for shoulder radius edge case.
*/
private int xOffset;
@Override
protected void init(NoseCone component) {
/**
* Construct a printable nose cone.
*
* @param noseCone the component to print
*/
public PrintableNoseCone(NoseCone noseCone) {
super(noseCone);
}
target = component;
double radius = target.getForeRadius();
if (radius < target.getAftRadius()) {
radius = target.getAftRadius();
}
setSize((int) PrintUnit.METERS.toPoints(2 * radius) + 4,
(int) PrintUnit.METERS.toPoints(target.getLength() + target.getAftShoulderLength()) + 4);
}
@Override
protected void init(NoseCone component) {
/**
* Draw a nose cone. Presumes that the graphics context has already had the x/y position translated based on
* where it should be drawn.
*
* @param g2 the graphics context
*/
@Override
protected void draw(Graphics2D g2) {
Shape[] shapes = TransitionShapes.getShapesSide(target, Transformation.rotate_x(0d), PrintUnit.METERS.toPoints(1));
target = component;
xOffset = 0;
double radius = target.getForeRadius();
if (radius < target.getAftRadius()) {
radius = target.getAftRadius();
}
//Really odd edge case where the shoulder radius exceeds the radius of the nose cone.
if (radius < target.getAftShoulderRadius()) {
double tmp = radius;
radius = target.getAftShoulderRadius();
xOffset = (int) PrintUnit.METERS.toPoints(radius - tmp) + PageFitPrintStrategy.MARGIN;
}
setSize((int) PrintUnit.METERS.toPoints(2 * radius) + 4,
(int) PrintUnit.METERS.toPoints(target.getLength() + target.getAftShoulderLength()) + 4);
}
if (shapes != null && shapes.length > 0) {
Rectangle r = shapes[0].getBounds();
g2.translate(r.getHeight() / 2, 0);
g2.rotate(Math.PI / 2);
for (Shape shape : shapes) {
g2.draw(shape);
}
g2.rotate(-Math.PI / 2);
}
}
/**
* Draw a nose cone. Presumes that the graphics context has already had the x/y position translated based on where it should be drawn.
*
* @param g2 the graphics context
*/
@Override
protected void draw(Graphics2D g2) {
Shape[] shapes = TransitionShapes.getShapesSide(target, Transformation.rotate_x(0d), PrintUnit.METERS.toPoints(1));
if (shapes != null && shapes.length > 0) {
Rectangle r = shapes[0].getBounds();
g2.translate(r.getHeight() / 2, 0);
g2.rotate(Math.PI / 2);
for (Shape shape : shapes) {
g2.draw(shape);
}
g2.rotate(-Math.PI / 2);
}
}
@Override
protected void translate(final Graphics2D theG2) {
if (xOffset == 0) {
super.translate(theG2);
}
else {
theG2.translate(xOffset, getOffsetY());
}
}
}

View File

@ -69,7 +69,7 @@ public class PrintableTransition extends AbstractPrintable<Transition> {
* @param transition the transition to print
*/
public PrintableTransition(Transition transition) {
super(false, transition);
super(transition);
}
/**

View File

@ -0,0 +1,137 @@
package net.sf.openrocket.gui.print.visitor;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfWriter;
import net.sf.openrocket.gui.print.AbstractPrintable;
import net.sf.openrocket.gui.print.ITextHelper;
import net.sf.openrocket.logging.LogHelper;
import net.sf.openrocket.rocketcomponent.RocketComponent;
import net.sf.openrocket.startup.Application;
import java.awt.image.BufferedImage;
import java.util.List;
import java.util.Set;
/**
* Common logic for printing strategies.
*/
public abstract class AbstractPrintStrategy<V> {
/**
* The logger.
*/
protected static final LogHelper log = Application.getLogger();
/**
* The iText document.
*/
protected Document document;
/**
* The direct iText writer.
*/
protected PdfWriter writer;
/**
* The stages selected.
*/
protected Set<Integer> stages;
/**
* Strategy for fitting multiple components onto a page.
*/
protected PageFitPrintStrategy pageFitPrint;
/**
* Constructor.
*
* @param doc the document
* @param pageFit the page fitting strategy
* @param theWriter the pdf writer
* @param theStages the set of stages in the rocket
*/
public AbstractPrintStrategy(Document doc, PageFitPrintStrategy pageFit, PdfWriter theWriter,
Set<Integer> theStages) {
document = doc;
pageFitPrint = pageFit;
writer = theWriter;
stages = theStages;
}
/**
* Recurse through the given rocket component.
*
* @param root the root component; all children will be printed recursively
*/
public V writeToDocument(final RocketComponent root) {
return goDeep(root.getChildren());
}
/**
* Recurse through the given rocket component.
*
* @param theRc an array of rocket components; all children will be printed recursively
*/
protected abstract V goDeep(List<RocketComponent> theRc);
/**
* Determine if the image will fit on the given page.
*
* @param pageSize the page size
* @param wImage the width of the thing to be printed
* @param hImage the height of the thing to be printed
*
* @return true if the thing to be printed will fit on a single page
*/
protected boolean fitsOnOnePage(Dimension pageSize, double wImage, double hImage) {
double wPage = pageSize.getWidth() - PageFitPrintStrategy.MARGIN * 2;
double hPage = pageSize.getHeight() - PageFitPrintStrategy.MARGIN * 2;
int wRatio = (int) Math.ceil(wImage / wPage);
int hRatio = (int) Math.ceil(hImage / hPage);
return wRatio <= 1.0d && hRatio <= 1.0d;
}
/**
* Get the dimensions of the paper page.
*
* @return an internal Dimension
*/
protected Dimension getPageSize() {
return new Dimension(document.getPageSize().getWidth(),
document.getPageSize().getHeight());
}
/**
* Determine if the strategy's set of stage numbers (to print) contains the specified stage.
*
* @param stageNumber a stage number
*
* @return true if the strategy contains the stage number provided
*/
public boolean shouldPrintStage(int stageNumber) {
if (stages == null || stages.isEmpty()) {
return false;
}
for (final Integer stage : stages) {
if (stage == stageNumber) {
return true;
}
}
return false;
}
void render(final AbstractPrintable thePrintable) throws DocumentException {
java.awt.Dimension size = thePrintable.getSize();
final Dimension pageSize = getPageSize();
if (fitsOnOnePage(pageSize, size.getWidth(), size.getHeight())) {
pageFitPrint.addComponent(thePrintable);
}
else {
BufferedImage image = (BufferedImage) thePrintable.createImage();
ITextHelper.renderImageAcrossPages(new Rectangle(pageSize.getWidth(), pageSize.getHeight()),
document, writer, image);
document.newPage();
}
}
}

View File

@ -2,52 +2,21 @@ package net.sf.openrocket.gui.print.visitor;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfWriter;
import net.sf.openrocket.gui.print.AbstractPrintable;
import net.sf.openrocket.gui.print.ITextHelper;
import net.sf.openrocket.gui.print.PrintUnit;
import net.sf.openrocket.gui.print.PrintableCenteringRing;
import net.sf.openrocket.logging.LogHelper;
import net.sf.openrocket.rocketcomponent.CenteringRing;
import net.sf.openrocket.rocketcomponent.InnerTube;
import net.sf.openrocket.rocketcomponent.RocketComponent;
import net.sf.openrocket.startup.Application;
import net.sf.openrocket.util.ArrayList;
import java.awt.image.BufferedImage;
import java.util.List;
import java.util.Set;
/**
* A strategy for printing a centering ring to iText.
*/
public class CenteringRingStrategy {
/**
* The logger.
*/
private static final LogHelper log = Application.getLogger();
/**
* The iText document.
*/
protected Document document;
/**
* The direct iText writer.
*/
protected PdfWriter writer;
/**
* The stages selected.
*/
protected Set<Integer> stages;
/**
* Strategy for fitting multiple components onto a page.
*/
protected PageFitPrintStrategy pageFitPrint;
public class CenteringRingStrategy extends AbstractPrintStrategy<Void> {
/**
* Constructor.
@ -57,29 +26,19 @@ public class CenteringRingStrategy {
* @param theStagesToVisit The stages to be visited by this strategy
*/
public CenteringRingStrategy(Document doc, PdfWriter theWriter, Set<Integer> theStagesToVisit, PageFitPrintStrategy pageFit) {
super(doc, pageFit, theWriter, theStagesToVisit);
document = doc;
writer = theWriter;
stages = theStagesToVisit;
pageFitPrint = pageFit;
}
/**
* Recurse through the given rocket component.
*
* @param root the root component; all children will be visited recursively
*/
public void writeToDocument(final RocketComponent root) {
List<RocketComponent> rc = root.getChildren();
goDeep(rc);
}
/**
* Recurse through the given rocket component.
*
* @param theRc an array of rocket components; all children will be visited recursively
*/
protected void goDeep(final List<RocketComponent> theRc) {
protected Void goDeep(final List<RocketComponent> theRc) {
for (RocketComponent rocketComponent : theRc) {
if (rocketComponent instanceof CenteringRing) {
render((CenteringRing) rocketComponent);
@ -88,6 +47,7 @@ public class CenteringRingStrategy {
goDeep(rocketComponent.getChildren());
}
}
return null;
}
/**
@ -141,130 +101,21 @@ public class CenteringRingStrategy {
}
/**
* The core behavior of this visitor.
* The core behavior of this strategy.
*
* @param component the object to extract info about; a graphical image of the centering ring shape is drawn to the
* document
*/
private void render(final CenteringRing component) {
try {
AbstractPrintable<CenteringRing> pfs;
AbstractPrintable pfs;
pfs = PrintableCenteringRing.create(component, findMotorMount(component));
java.awt.Dimension size = pfs.getSize();
final Dimension pageSize = getPageSize();
if (fitsOnOnePage(pageSize, size.getWidth(), size.getHeight())) {
pageFitPrint.addComponent(pfs);
}
else {
int off = (int) (PrintUnit.POINTS_PER_INCH * 0.3f);
pfs.setPrintOffset(off, off);
BufferedImage image = (BufferedImage) pfs.createImage();
ITextHelper.renderImageAcrossPages(new Rectangle(pageSize.getWidth(), pageSize.getHeight()),
document, writer, image);
document.newPage();
}
render(pfs);
}
catch (DocumentException e) {
log.error("Could not render the centering ring.", e);
}
}
/**
* Determine if the image will fit on the given page.
*
* @param pageSize the page size
* @param wImage the width of the thing to be printed
* @param hImage the height of the thing to be printed
*
* @return true if the thing to be printed will fit on a single page
*/
private boolean fitsOnOnePage(Dimension pageSize, double wImage, double hImage) {
double wPage = pageSize.getWidth();
double hPage = pageSize.getHeight();
int wRatio = (int) Math.ceil(wImage / wPage);
int hRatio = (int) Math.ceil(hImage / hPage);
return wRatio <= 1.0d && hRatio <= 1.0d;
}
/**
* Get the dimensions of the paper page.
*
* @return an internal Dimension
*/
protected Dimension getPageSize() {
return new Dimension(document.getPageSize().getWidth(),
document.getPageSize().getHeight());
}
/**
* Convenience class to model a dimension.
*/
public static class Dimension {
/**
* Width, in points.
*/
public float width;
/**
* Height, in points.
*/
public float height;
/**
* Breadth, in points.
*/
public float breadth = 0f;
/**
* Constructor.
*
* @param w width
* @param h height
*/
public Dimension(float w, float h) {
width = w;
height = h;
}
/**
* Constructor.
*
* @param w width
* @param h height
* @param b breadth; optionally used to represent radius
*/
public Dimension(float w, float h, float b) {
width = w;
height = h;
breadth = b;
}
/**
* Get the width.
*
* @return the width
*/
public float getWidth() {
return width;
}
/**
* Get the height.
*
* @return the height
*/
public float getHeight() {
return height;
}
/**
* Get the breadth.
*
* @return the breadth
*/
public float getBreadth() {
return breadth;
}
}
}

View File

@ -0,0 +1,70 @@
package net.sf.openrocket.gui.print.visitor;
/**
* Convenience class to model a dimension.
*/
public class Dimension {
/**
* Width, in points.
*/
public float width;
/**
* Height, in points.
*/
public float height;
/**
* Breadth, in points.
*/
public float breadth = 0f;
/**
* Constructor.
*
* @param w width
* @param h height
*/
public Dimension(float w, float h) {
width = w;
height = h;
}
/**
* Constructor.
*
* @param w width
* @param h height
* @param b breadth; optionally used to represent radius
*/
public Dimension(float w, float h, float b) {
width = w;
height = h;
breadth = b;
}
/**
* Get the width.
*
* @return the width
*/
public float getWidth() {
return width;
}
/**
* Get the height.
*
* @return the height
*/
public float getHeight() {
return height;
}
/**
* Get the breadth.
*
* @return the breadth
*/
public float getBreadth() {
return breadth;
}
}

View File

@ -5,49 +5,19 @@ package net.sf.openrocket.gui.print.visitor;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfWriter;
import net.sf.openrocket.gui.print.ITextHelper;
import net.sf.openrocket.gui.print.PrintUnit;
import net.sf.openrocket.gui.print.AbstractPrintable;
import net.sf.openrocket.gui.print.PrintableFinSet;
import net.sf.openrocket.logging.LogHelper;
import net.sf.openrocket.rocketcomponent.FinSet;
import net.sf.openrocket.rocketcomponent.RocketComponent;
import net.sf.openrocket.startup.Application;
import java.awt.image.BufferedImage;
import java.util.List;
import java.util.Set;
/**
* A strategy for drawing fin templates.
*/
public class FinSetPrintStrategy {
/**
* The logger.
*/
private static final LogHelper log = Application.getLogger();
/**
* The iText document.
*/
protected Document document;
/**
* The direct iText writer.
*/
protected PdfWriter writer;
/**
* The stages selected.
*/
protected Set<Integer> stages;
/**
* Strategy for fitting multiple components onto a page.
*/
protected PageFitPrintStrategy pageFitPrint;
public class FinSetPrintStrategy extends AbstractPrintStrategy<Void> {
/**
* Constructor.
@ -57,37 +27,25 @@ public class FinSetPrintStrategy {
* @param theStages The stages to be printed by this strategy
*/
public FinSetPrintStrategy(Document doc, PdfWriter theWriter, Set<Integer> theStages, PageFitPrintStrategy pageFit) {
document = doc;
writer = theWriter;
stages = theStages;
pageFitPrint = pageFit;
super(doc, pageFit, theWriter, theStages);
}
/**
* Recurse through the given rocket component.
*
* @param root the root component; all children will be printed recursively
*/
public void writeToDocument (final RocketComponent root) {
List<RocketComponent> rc = root.getChildren();
goDeep(rc);
}
/**
* Recurse through the given rocket component.
*
* @param theRc an array of rocket components; all children will be printed recursively
*/
protected void goDeep (final List<RocketComponent> theRc) {
@Override
protected Void goDeep (final List<RocketComponent> theRc) {
for (RocketComponent rocketComponent : theRc) {
if (rocketComponent instanceof FinSet) {
printFinSet((FinSet) rocketComponent);
render((FinSet) rocketComponent);
}
else if (rocketComponent.getChildCount() > 0) {
goDeep(rocketComponent.getChildren());
}
}
return null;
}
/**
@ -95,24 +53,11 @@ public class FinSetPrintStrategy {
*
* @param finSet the object to extract info about; a graphical image of the fin shape is drawn to the document
*/
private void printFinSet(final FinSet finSet) {
private void render(final FinSet finSet) {
if (shouldPrintStage(finSet.getStageNumber())) {
try {
PrintableFinSet pfs = new PrintableFinSet(finSet);
java.awt.Dimension finSize = pfs.getSize();
final Dimension pageSize = getPageSize();
if (fitsOnOnePage(pageSize, finSize.getWidth(), finSize.getHeight())) {
pageFitPrint.addComponent(pfs);
}
else {
int off = (int)(PrintUnit.POINTS_PER_INCH * 0.3f);
pfs.setPrintOffset(off, off);
BufferedImage image = (BufferedImage) pfs.createImage();
ITextHelper.renderImageAcrossPages(new Rectangle(pageSize.getWidth(), pageSize.getHeight()),
document, writer, image);
document.newPage();
}
AbstractPrintable<FinSet> pfs = new PrintableFinSet(finSet);
render(pfs);
}
catch (DocumentException e) {
log.error("Could not render fin.", e);
@ -120,91 +65,4 @@ public class FinSetPrintStrategy {
}
}
/**
* Determine if the strategy's set of stage numbers (to print) contains the specified stage.
*
* @param stageNumber a stage number
*
* @return true if the strategy contains the stage number provided
*/
public boolean shouldPrintStage(int stageNumber) {
if (stages == null || stages.isEmpty()) {
return false;
}
for (final Integer stage : stages) {
if (stage == stageNumber) {
return true;
}
}
return false;
}
/**
* Determine if the image will fit on the given page.
*
* @param pageSize the page size
* @param wImage the width of the thing to be printed
* @param hImage the height of the thing to be printed
*
* @return true if the thing to be printed will fit on a single page
*/
private boolean fitsOnOnePage (Dimension pageSize, double wImage, double hImage) {
double wPage = pageSize.getWidth();
double hPage = pageSize.getHeight();
int wRatio = (int) Math.ceil(wImage / wPage);
int hRatio = (int) Math.ceil(hImage / hPage);
return wRatio <= 1.0d && hRatio <= 1.0d;
}
/**
* Get the dimensions of the paper page.
*
* @return an internal Dimension
*/
protected Dimension getPageSize () {
return new Dimension(document.getPageSize().getWidth(),
document.getPageSize().getHeight());
}
/**
* Convenience class to model a dimension.
*/
class Dimension {
/** Width, in points. */
public float width;
/** Height, in points. */
public float height;
/**
* Constructor.
* @param w width
* @param h height
*/
public Dimension (float w, float h) {
width = w;
height = h;
}
/**
* Get the width.
*
* @return the width
*/
public float getWidth () {
return width;
}
/**
* Get the height.
*
* @return the height
*/
public float getHeight () {
return height;
}
}
}
}

View File

@ -8,7 +8,9 @@ import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfWriter;
import net.sf.openrocket.gui.print.PrintUnit;
import net.sf.openrocket.gui.print.PrintableComponent;
import net.sf.openrocket.rocketcomponent.RocketComponent;
import net.sf.openrocket.logging.LogHelper;
import net.sf.openrocket.startup.Application;
import java.awt.Graphics2D;
import java.util.ArrayList;
import java.util.Collections;
@ -22,6 +24,14 @@ import java.util.Set;
*/
public class PageFitPrintStrategy {
/** The margin. */
public final static int MARGIN = (int)(PrintUnit.POINTS_PER_INCH * 0.3f);
/**
* The logger.
*/
private static final LogHelper log = Application.getLogger();
/**
* The iText document.
*/
@ -62,10 +72,8 @@ public class PageFitPrintStrategy {
/**
* Recurse through the given rocket component.
*
* @param root the root component; all children will be printed recursively
*/
public void writeToDocument (final RocketComponent root) {
public void writeToDocument () {
fitPrintComponents();
}
@ -76,8 +84,8 @@ public class PageFitPrintStrategy {
final Dimension pageSize = getPageSize();
double wPage = pageSize.getWidth();
double hPage = pageSize.getHeight();
int marginX = (int)(PrintUnit.POINTS_PER_INCH * 0.3f);
int marginY = (int)(PrintUnit.POINTS_PER_INCH * 0.3f);
int marginX = MARGIN;
int marginY = MARGIN;
PdfContentByte cb = writer.getDirectContent();
Collections.sort(componentToPrint);
@ -86,7 +94,7 @@ public class PageFitPrintStrategy {
int pageY = marginY;
Boolean anyAddedToRow;
Graphics2D g2 = cb.createGraphics(pageSize.width, pageSize.height);
Graphics2D g2 = createGraphics((float) wPage, (float) hPage, cb);
do {
// Fill the row
@ -98,17 +106,19 @@ public class PageFitPrintStrategy {
while (entry.hasNext()) {
PrintableComponent component = entry.next();
java.awt.Dimension dim = component.getSize();
if ((rowX + dim.width + marginX < wPage) && (rowY + dim.height + marginY < hPage)) {
if ((rowX + dim.width + marginX <= wPage) && (rowY + dim.height + marginY <= hPage)) {
component.setPrintOffset(rowX, rowY);
// Separate each component horizontally by a space equal to the margin
rowX += dim.width + marginX;
if (rowY + dim.height + marginY > pageY) {
pageY = rowY + dim.height + marginY;
}
entry.remove();
entry.remove();
component.print(g2);
anyAddedToRow = true;
}
}
// Separate each component vertically by a space equal to the margin
pageY += marginY;
} while (anyAddedToRow);
@ -117,6 +127,20 @@ public class PageFitPrintStrategy {
}
}
/**
* Create a graphics context.
*
* @param theWPage the width of the physical page
* @param theHPage the height of the physical
*
* @param theCb the pdf content byte instance
*
* @return a Graphics2D instance
*/
Graphics2D createGraphics(final float theWPage, final float theHPage, final PdfContentByte theCb) {
return theCb.createGraphics(theWPage, theHPage);
}
/**
* Get the dimensions of the paper page.
*
@ -126,42 +150,4 @@ public class PageFitPrintStrategy {
return new Dimension(document.getPageSize().getWidth(),
document.getPageSize().getHeight());
}
/**
* Convenience class to model a dimension.
*/
class Dimension {
/** Width, in points. */
public float width;
/** Height, in points. */
public float height;
/**
* Constructor.
* @param w width
* @param h height
*/
public Dimension (float w, float h) {
width = w;
height = h;
}
/**
* Get the width.
*
* @return the width
*/
public float getWidth () {
return width;
}
/**
* Get the height.
*
* @return the height
*/
public float getHeight () {
return height;
}
}
}

View File

@ -2,52 +2,26 @@ package net.sf.openrocket.gui.print.visitor;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfWriter;
import net.sf.openrocket.gui.print.AbstractPrintable;
import net.sf.openrocket.gui.print.ITextHelper;
import net.sf.openrocket.gui.print.PrintUnit;
import net.sf.openrocket.gui.print.PrintableNoseCone;
import net.sf.openrocket.gui.print.PrintableTransition;
import net.sf.openrocket.logging.LogHelper;
import net.sf.openrocket.rocketcomponent.NoseCone;
import net.sf.openrocket.rocketcomponent.RocketComponent;
import net.sf.openrocket.rocketcomponent.Transition;
import net.sf.openrocket.startup.Application;
import java.awt.image.BufferedImage;
import java.util.List;
import java.util.Set;
/**
* A strategy for drawing transition/shroud/nose cone templates.
*/
public class TransitionStrategy {
public class TransitionStrategy extends AbstractPrintStrategy <Boolean> {
/**
* The logger.
* Print nose cones?
*/
private static final LogHelper log = Application.getLogger();
/**
* The iText document.
*/
protected Document document;
/**
* The direct iText writer.
*/
protected PdfWriter writer;
/**
* The stages selected.
*/
protected Set<Integer> stages;
/**
* Strategy for fitting multiple components onto a page.
*/
protected PageFitPrintStrategy pageFitPrint;
private boolean printNoseCones = false;
/**
* Constructor.
@ -55,65 +29,53 @@ public class TransitionStrategy {
* @param doc The iText document
* @param theWriter The direct iText writer
* @param theStagesToVisit The stages to be visited by this strategy
* @param pageFit the page fit strategy
* @param noseCones nose cones are a special form of a transition; if true, then print nose cones
*/
public TransitionStrategy(Document doc, PdfWriter theWriter, Set<Integer> theStagesToVisit, PageFitPrintStrategy pageFit) {
public TransitionStrategy(Document doc, PdfWriter theWriter, Set<Integer> theStagesToVisit, PageFitPrintStrategy pageFit, boolean noseCones) {
super(doc, pageFit, theWriter, theStagesToVisit);
document = doc;
writer = theWriter;
stages = theStagesToVisit;
pageFitPrint = pageFit;
printNoseCones = noseCones;
}
/**
* Recurse through the given rocket component.
*
* @param root the root component; all children will be visited recursively
* @param noseCones nose cones are a special form of a transition; if true, then print nose cones
*
* @return true if a transition/nosecone was rendered
*/
public boolean writeToDocument(final RocketComponent root, boolean noseCones) {
List<RocketComponent> rc = root.getChildren();
return goDeep(rc, noseCones);
}
/**
* Recurse through the given rocket component.
*
* @param theRc an array of rocket components; all children will be visited recursively
* @param noseCones nose cones are a special form of a transition; if true, then print nose cones
*
* @return true if a transition/nosecone was rendered
*/
protected boolean goDeep(final List<RocketComponent> theRc, boolean noseCones) {
protected Boolean goDeep(final List<RocketComponent> theRc) {
boolean result = false;
for (RocketComponent rocketComponent : theRc) {
if (rocketComponent instanceof NoseCone) {
if (noseCones) {
if (printNoseCones) {
result |= render((Transition) rocketComponent);
}
}
else if (rocketComponent instanceof Transition && !noseCones) {
else if (rocketComponent instanceof Transition && !printNoseCones) {
result |= render((Transition) rocketComponent);
}
else if (rocketComponent.getChildCount() > 0) {
result |= goDeep(rocketComponent.getChildren(), noseCones);
result |= goDeep(rocketComponent.getChildren());
}
}
return result;
}
/**
* The core behavior of this visitor.
* The core behavior of this strategy.
*
* @param component the object to extract info about; a graphical image of the transition shape is drawn to the
* document
* @param component the object to extract info about; a graphical image of the transition shape is drawn to the document
*
* @return true, always
*/
private boolean render(final Transition component) {
try {
AbstractPrintable<?> pfs;
AbstractPrintable pfs;
if (component instanceof NoseCone) {
pfs = new PrintableNoseCone((NoseCone) component);
}
@ -121,19 +83,7 @@ public class TransitionStrategy {
pfs = new PrintableTransition(component);
}
java.awt.Dimension size = pfs.getSize();
final Dimension pageSize = getPageSize();
if (fitsOnOnePage(pageSize, size.getWidth(), size.getHeight())) {
pageFitPrint.addComponent(pfs);
}
else {
int off = (int) (PrintUnit.POINTS_PER_INCH * 0.3f);
pfs.setPrintOffset(off, off);
BufferedImage image = (BufferedImage) pfs.createImage();
ITextHelper.renderImageAcrossPages(new Rectangle(pageSize.getWidth(), pageSize.getHeight()),
document, writer, image);
document.newPage();
}
render(pfs);
}
catch (DocumentException e) {
log.error("Could not render the transition.", e);
@ -141,75 +91,4 @@ public class TransitionStrategy {
return true;
}
/**
* Determine if the image will fit on the given page.
*
* @param pageSize the page size
* @param wImage the width of the thing to be printed
* @param hImage the height of the thing to be printed
*
* @return true if the thing to be printed will fit on a single page
*/
private boolean fitsOnOnePage(Dimension pageSize, double wImage, double hImage) {
double wPage = pageSize.getWidth();
double hPage = pageSize.getHeight();
int wRatio = (int) Math.ceil(wImage / wPage);
int hRatio = (int) Math.ceil(hImage / hPage);
return wRatio <= 1.0d && hRatio <= 1.0d;
}
/**
* Get the dimensions of the paper page.
*
* @return an internal Dimension
*/
protected Dimension getPageSize() {
return new Dimension(document.getPageSize().getWidth(),
document.getPageSize().getHeight());
}
/**
* Convenience class to model a dimension.
*/
class Dimension {
/**
* Width, in points.
*/
public float width;
/**
* Height, in points.
*/
public float height;
/**
* Constructor.
*
* @param w width
* @param h height
*/
public Dimension(float w, float h) {
width = w;
height = h;
}
/**
* Get the width.
*
* @return the width
*/
public float getWidth() {
return width;
}
/**
* Get the height.
*
* @return the height
*/
public float getHeight() {
return height;
}
}
}