Skip to content

Commit

Permalink
Native printing still needs to wait for a frame of animation to avoid…
Browse files Browse the repository at this point in the history
… blanks on successive pages
  • Loading branch information
Berenz committed Apr 24, 2020
1 parent f10b866 commit b57bfb2
Showing 1 changed file with 26 additions and 19 deletions.
45 changes: 26 additions & 19 deletions src/qz/printer/action/WebApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.IntPredicate;

/**
* JavaFX container for taking HTML snapshots.
Expand Down Expand Up @@ -158,7 +159,7 @@ public void start(Stage st) throws Exception {
public static synchronized void print(final PrinterJob job, final WebAppModel model) throws Throwable {
model.setZoom(1); //vector prints do not need to use zoom

load(model, () -> {
load(model, (int frames) -> {
try {
PageLayout layout = job.getJobSettings().getPageLayout();
if (model.isScaled()) {
Expand Down Expand Up @@ -205,6 +206,8 @@ public static synchronized void print(final PrinterJob job, final WebAppModel mo
});
}
catch(Exception e) { unlatch(e); }

return true; //only runs on first frame
});

log.trace("Waiting on print..");
Expand Down Expand Up @@ -232,25 +235,20 @@ public static synchronized BufferedImage raster(final WebAppModel model) throws
model.setWebWidth(model.getWebWidth() * increase);
model.setWebHeight(model.getWebHeight() * increase);

load(model, () -> Platform.runLater(() -> new AnimationTimer() {
int frames = 0;

@Override
public void handle(long l) {
if (++frames == 2) {
log.debug("Attempting image capture");

webView.snapshot((snapshotResult) -> {
capture.set(SwingFXUtils.fromFXImage(snapshotResult.getImage(), null));
unlatch(null);
load(model, (int frames) -> {
if (frames == 2) {
log.debug("Attempting image capture");

return null;
}, null, null);
webView.snapshot((snapshotResult) -> {
capture.set(SwingFXUtils.fromFXImage(snapshotResult.getImage(), null));
unlatch(null);

stop();
}
return null;
}, null, null);
}
}.start()));

return frames >= 2;
});

log.trace("Waiting on capture..");
captureLatch.await(); //released when unlatch is called
Expand All @@ -266,7 +264,7 @@ public void handle(long l) {
* @param model The model specifying the web page parameters.
* @param action EventHandler that will be ran when the WebView completes loading.
*/
private static synchronized void load(WebAppModel model, Runnable action) {
private static synchronized void load(WebAppModel model, IntPredicate action) {
captureLatch = new CountDownLatch(1);
thrown.set(null);

Expand All @@ -293,7 +291,16 @@ private static synchronized void load(WebAppModel model, Runnable action) {

autosize(webView);

printAction = action;
printAction = () -> Platform.runLater(() -> new AnimationTimer() {
int frames = 0;

@Override
public void handle(long l) {
if (action.test(++frames)) {
stop();
}
}
}.start());

if (model.isPlainText()) {
webView.getEngine().loadContent(model.getSource(), "text/html");
Expand Down

0 comments on commit b57bfb2

Please sign in to comment.