}
}
Progress.switchToDeterminate(progress, max);
Rectangle size = new Rectangle(pageSize);
if (landscape) {
size = new Rectangle(pageSize.rotate());
}
size.setBackgroundColor(new BaseColor(Lookup.getDefault().lookup(PreviewController.class).getModel().getBackgroundColor()));
document = new Document(size);
PdfWriter pdfWriter = PdfWriter.getInstance(document, stream);
document.open();
cb = pdfWriter.getDirectContent();
cb.saveState();
//Limits
float minX = Float.POSITIVE_INFINITY;
float maxX = Float.NEGATIVE_INFINITY;
float minY = Float.POSITIVE_INFINITY;
float maxY = Float.NEGATIVE_INFINITY;
for (Node n : graph.getNodes()) {
minX = Math.min(minX, n.getPosition().getX() - n.getRadius() - n.getBorderWidth());
maxX = Math.max(maxX, n.getPosition().getX() + n.getRadius() + n.getBorderWidth());
minY = Math.min(minY, -n.getPosition().getY() - n.getRadius() - n.getBorderWidth());
maxY = Math.max(maxY, -n.getPosition().getY() + n.getRadius() + n.getBorderWidth());
}
double graphWidth = maxX - minX;
double graphHeight = maxY - minY;
double centerX = minX + graphWidth / 2.;
double centerY = minY + graphHeight / 2.;
//Transform
double pageWidth = size.getWidth() - marginLeft - marginRight;
double pageHeight = size.getHeight() - marginTop - marginBottom;
double ratioWidth = pageWidth / graphWidth;
double ratioHeight = pageHeight / graphHeight;
double scale = ratioWidth < ratioHeight ? ratioWidth : ratioHeight;
double translateX = (marginLeft + pageWidth / 2.) / scale;
double translateY = (marginBottom + pageHeight / 2.) / scale;