Skip to content

Commit

Permalink
Fix the printing function.
Browse files Browse the repository at this point in the history
  • Loading branch information
wrandelshofer committed Sep 13, 2024
1 parent 357bffb commit f7d1d0c
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ file.loadRecent.text=Load Recent
file.removeOpenRecentEntry.buttonText=Remove entry from 'Open Recent'
file.print.accelerator=
file.print.couldntPrint.message=Couldn''t print the document.
file.print.couldntFindAPrinter.message=Couldn''t find a printer.
file.print.smallIcon=
file.print.largeIcon=
file.print.mnemonic=${file.print.mnemonic.[$os]}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ toolBars=Schaltleisten
file.print.text=Drucken...
file.print.mnemonic=${file.print.mnemonic.[$os]}
file.print.couldntPrint.message=Konnte das Dokument nicht drucken.
file.print.couldntFindAPrinter.message=Konnte keinen Drucker finden.
file.open.couldntOpen.message=Konnte Datei "{0}" nicht \u00f6ffnen.
edit.clearSelection.text=Auswahl aufheben
file.load.text=Laden...
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ file.clearRecentFiles.mnemonic=
toolBars=Outils
file.print.text=Imprimer...
file.print.mnemonic=${file.print.mnemonic.[$os]}
file.print.couldntPrint.message=Je n'ai pas pu imprimer le document.
file.print.couldntPrint.message=Ne pouvais pas imprimer le document.
file.print.couldntFindAPrinter.message=Ne pouvais pas trouver une imprimante.
file.open.couldntOpen.message=Le fichier "{0}" n'a pas pu \u00eatre ouvert.
edit.clearSelection.text=effacer la s\u00e9lection
file.load.text=Charger...
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,19 @@ protected void onActionPerformed(ActionEvent event, FileBasedActivity activity)
activity.addDisabler(workState);
PrinterJob job = PrinterJob.createPrinterJob();
if (job != null && job.showPrintDialog(activity.getNode().getScene().getWindow())) {
activity.print(job, workState).thenRun(() -> activity.removeDisabler(workState));
activity.print(job, workState).whenComplete((v, t) -> {
activity.removeDisabler(workState);
if (t != null) {
t.printStackTrace();
Alert alert = new Alert(AlertType.INFORMATION, ApplicationLabels.getResources().getTextProperty("file.print.couldntPrint.message"));
alert.setContentText(t.getMessage());
alert.getDialogPane().setMaxWidth(640.0);
alert.show();
activity.removeDisabler(workState);
}
});
} else {
Alert alert = new Alert(AlertType.INFORMATION, "Sorry, no printer found");
Alert alert = new Alert(AlertType.INFORMATION, ApplicationLabels.getResources().getTextProperty("file.print.couldntPrint.message"));
alert.getDialogPane().setMaxWidth(640.0);
alert.show();
activity.removeDisabler(workState);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public boolean isExportSlices3x() {
* @param internalPageNumber the internal page number of the page figure
* @throws IOException if writing fails
*/
protected abstract void writePage(Path file, Page page, Node node, int pageCount, int pageNumber, int internalPageNumber) throws IOException;
protected abstract void writePage(@Nullable Path file, Page page, Node node, int pageCount, int pageNumber, int internalPageNumber) throws IOException;

protected void writePages(@Nullable Path dir, String basename, Drawing drawing) throws IOException {
List<Page> pages = new ArrayList<>();
Expand All @@ -99,7 +99,6 @@ protected void writePages(@Nullable Path dir, String basename, Drawing drawing)
/**
* Writes all pages of the drawing.
*
* @param dir the output directory, null for print output
* @param basename the basename of the pages, null for print output
* @param drawing the drawing
* @param pages the pages
Expand All @@ -121,7 +120,6 @@ protected void writePages(@Nullable Path dir, String basename, Drawing drawing,
Group parentOfPageNode = new Group();
for (Page page : pages) {
for (int internalPageNumber = 0, n = page.getNumberOfSubPages(); internalPageNumber < n; internalPageNumber++) {
Path filename = (dir == null) ? null : dir.resolve(basename + "_" + (pageNumber + 1) + "." + getExtension());

hints.put(RenderContext.RENDER_PAGE, page);
hints.put(RenderContext.RENDER_NUMBER_OF_PAGES, numberOfPages);
Expand Down Expand Up @@ -150,7 +148,7 @@ protected void writePages(@Nullable Path dir, String basename, Drawing drawing,

rootNode.getChildren().setAll(parentOfDrawing, parentOfPageNode);

writePage(filename, page, rootNode, numberOfPages, pageNumber, internalPageNumber);
writePage(null, page, rootNode, numberOfPages, pageNumber, internalPageNumber);

pageNumber++;
}
Expand All @@ -167,7 +165,7 @@ protected void writePages(@Nullable Path dir, String basename, Drawing drawing,
* @return returns true if the state of the node was destroyed
* @throws IOException in case of failure
*/
protected abstract boolean writeSlice(Path file, Slice slice, Node node, double dpi) throws IOException;
protected abstract boolean writeSlice(@Nullable Path file, Slice slice, Node node, double dpi) throws IOException;

protected void writeSlices(@Nullable Path dir, Drawing drawing) throws IOException {
List<Slice> slices = new ArrayList<>();
Expand All @@ -194,7 +192,7 @@ protected void writeSlices(@Nullable Path dir, Drawing drawing) throws IOExcepti
* @param slices
* @throws IOException
*/
private void writeSlices(Path dir, Drawing drawing, List<Slice> slices, String suffix, double dpi) throws IOException {
private void writeSlices(@Nullable Path dir, Drawing drawing, List<Slice> slices, String suffix, double dpi) throws IOException {
Map<Key<?>, Object> hints = new HashMap<>();
RenderContext.RENDERING_INTENT.put(hints, RenderingIntent.EXPORT);
RenderContext.DPI.put(hints, dpi);
Expand All @@ -208,7 +206,7 @@ private void writeSlices(Path dir, Drawing drawing, List<Slice> slices, String s
}
Node node = null;
for (Slice slice : slices) {
Path filename = dir.resolve(idFactory.createId(slice, "Slice") + suffix + "." + getExtension());
Path filename = dir == null ? null : dir.resolve(idFactory.createId(slice, "Slice") + suffix + "." + getExtension());
if (node == null) {
node = toNode(drawing, Collections.singleton(drawing), hints);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.transform.Scale;
import javafx.scene.transform.Transform;
import javafx.scene.transform.Translate;
import org.jhotdraw8.css.value.CssSize;
import org.jhotdraw8.css.value.DefaultUnitConverter;
Expand All @@ -23,14 +24,22 @@
import org.jhotdraw8.draw.figure.Page;
import org.jhotdraw8.draw.figure.PageFigure;
import org.jhotdraw8.draw.figure.Slice;
import org.jhotdraw8.draw.render.RenderContext;
import org.jhotdraw8.draw.render.RenderingIntent;
import org.jhotdraw8.fxcollection.typesafekey.Key;
import org.jspecify.annotations.Nullable;

import javax.imageio.metadata.IIOInvalidTreeException;
import javax.imageio.metadata.IIOMetadata;
import javax.imageio.metadata.IIOMetadataNode;
import java.io.IOException;
import java.nio.file.Path;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import static java.lang.Math.abs;
import static org.jhotdraw8.draw.render.SimpleDrawingRenderer.toNode;

/**
* PrinterExportFormat.
Expand Down Expand Up @@ -68,7 +77,15 @@ public Paper findPaper(CssDimension2D paperSize) {
return Paper.A4;
}

private void printSlice(CssDimension2D pageSize, Figure slice, Bounds viewportBounds, Node node, double dpi) {
/**
* Prints a slice of a drawing.
*
* @param pageSize the page size
* @param worldToLocal The worldToLocal transform of the viewport
* @param viewportBounds the bounds of the viewport that we want to print
* @param node the rendered node of the slice
*/
private void printSlice(CssDimension2D pageSize, @Nullable Transform worldToLocal, Bounds viewportBounds, Node node) {
Paper paper = findPaper(pageSize);
Dimension2D psize = pageSize.getConvertedValue();
PageLayout pl = job.getPrinter().createPageLayout(paper, psize.getWidth() <= psize.getHeight() ? PageOrientation.PORTRAIT : PageOrientation.LANDSCAPE, 0, 0, 0, 0);
Expand Down Expand Up @@ -109,10 +126,9 @@ private void printSlice(CssDimension2D pageSize, Figure slice, Bounds viewportBo
new Translate(-pl.getLeftMargin(), -pl.getTopMargin()),
new Scale(scaleFactor, scaleFactor),
new Translate(-viewportBounds.getMinX(), -viewportBounds.getMinY())
// slice.getWorldToLocal()
);
if (slice.getWorldToLocal() != null) {
printParent.getTransforms().add(slice.getWorldToLocal());
if (worldToLocal != null) {
printParent.getTransforms().add(worldToLocal);
}

Group printNode = new Group();
Expand Down Expand Up @@ -151,12 +167,12 @@ protected void writePage(Path file, Page page, Node node, int pageCount, int pag
final Bounds pageBounds = page.getPageBounds(internalPageNumber);
double factor = paperWidth / pageBounds.getWidth();

printSlice(page.get(PageFigure.PAPER_SIZE), page, pageBounds, node, EXPORT_PAGES_DPI_KEY.get(getOptions()) * factor);
printSlice(page.get(PageFigure.PAPER_SIZE), page.getWorldToLocal(), pageBounds, node);
}

@Override
protected boolean writeSlice(Path file, Slice slice, Node node, double dpi) throws IOException {
printSlice(null, slice, slice.getLayoutBounds(), node, dpi);
printSlice(null, slice.getWorldToLocal(), slice.getLayoutBounds(), node);
return false;
}

Expand All @@ -165,10 +181,32 @@ protected boolean writeSlice(Path file, Slice slice, Node node, double dpi) thro
public void print(PrinterJob job, Drawing drawing) throws IOException {
this.job = job;
try {
// If we have pages, print the pages, otherwise print the entire drawing
boolean hasPages = false;
for (Figure f : drawing.preorderIterable()) {
if (f instanceof Page) {
hasPages = true;
break;
}
}
if (hasPages) {
writePages(null, null, drawing);
writeSlices(null, drawing);
} else {
writeDrawing(drawing);
}
} finally {
job = null;
}
}

private void writeDrawing(Drawing drawing) throws IOException {
Map<Key<?>, Object> hints = new HashMap<>();
Double dpi = EXPORT_PAGES_DPI_KEY.get(getOptions());
RenderContext.RENDERING_INTENT.put(hints, RenderingIntent.EXPORT);
RenderContext.DPI.put(hints, dpi);
Bounds b = drawing.getLayoutBounds();
Node node = toNode(drawing, Collections.singleton(drawing), hints);

printSlice(new CssDimension2D(b.getWidth(), b.getHeight()), null, b, node);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,10 @@ private void writeDrawingElementAttributes(Element docElement, Drawing drawing)
}

@Override
protected void writePage(Path file, Page page, Node node, int pageCount, int pageNumber, int internalPageNumber) throws IOException {
protected void writePage(@Nullable Path file, Page page, Node node, int pageCount, int pageNumber, int internalPageNumber) throws IOException {
if (file == null) {
throw new IOException("No file specified.");
}
CssSize pw = page.getNonNull(PageFigure.PAPER_WIDTH);
CssSize ph = page.getNonNull(PageFigure.PAPER_HEIGHT);
markNodesOutsideBoundsWithSkip(node, FXTransforms.transform(page.getLocalToWorld(), page.getPageBounds(internalPageNumber)));
Expand All @@ -236,8 +239,10 @@ private void writePageElementAttributes(Element docElement, Page page, int inter
}

@Override
protected boolean writeSlice(Path file, Slice slice, Node node, double dpi) throws IOException {
LOGGER.info("Writing slice " + file);
protected boolean writeSlice(@Nullable Path file, Slice slice, Node node, double dpi) throws IOException {
if (file == null) {
throw new IOException("No file specified.");
}
markNodesOutsideBoundsWithSkip(node, slice.getLayoutBounds());
Transform worldToLocal = slice.getWorldToLocal();
Point2D sliceOrigin = slice.getSliceOrigin();
Expand Down

0 comments on commit f7d1d0c

Please sign in to comment.