Skip to content

Commit

Permalink
Don't run print in a task on Mac/Linux
Browse files Browse the repository at this point in the history
  • Loading branch information
cyanfish committed Dec 8, 2023
1 parent fdcec85 commit 91a57ea
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 87 deletions.
111 changes: 54 additions & 57 deletions NAPS2.Lib.Gtk/ImportExport/GtkScannedImagePrinter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,76 +5,73 @@ namespace NAPS2.ImportExport;

public class GtkScannedImagePrinter : IScannedImagePrinter
{
public async Task<bool> PromptToPrint(
public Task<bool> PromptToPrint(
Eto.Forms.Window parentWindow, IList<ProcessedImage> images, IList<ProcessedImage> selectedImages)
{
if (!images.Any())
{
return false;
return Task.FromResult(false);
}
return await Task.Run(() =>
var printOp = new PrintOperation
{
var printOp = new PrintOperation
{
NPages = images.Count,
UseFullPage = true,
HasSelection = selectedImages.Count > 1,
SupportSelection = selectedImages.Count > 1
};
if (selectedImages.Count == 1)
NPages = images.Count,
UseFullPage = true,
HasSelection = selectedImages.Count > 1,
SupportSelection = selectedImages.Count > 1
};
if (selectedImages.Count == 1)
{
printOp.CurrentPage = images.IndexOf(selectedImages[0]);
}
var printTarget = images;
printOp.BeginPrint += (_, args) =>
{
if (printOp.PrintSettings.PrintPages == PrintPages.Selection)
{
printOp.CurrentPage = images.IndexOf(selectedImages[0]);
printTarget = selectedImages;
printOp.NPages = printTarget.Count;
}
var printTarget = images;
printOp.BeginPrint += (_, args) =>
};
printOp.DrawPage += (_, args) =>
{
var image = printTarget[args.PageNr].Render();
try
{
if (printOp.PrintSettings.PrintPages == PrintPages.Selection)
var ctx = args.Context;
var cairoCtx = ctx.CairoContext;

if (Math.Sign(image.Width - image.Height) != Math.Sign(ctx.Width - ctx.Height))
{
printTarget = selectedImages;
printOp.NPages = printTarget.Count;
// Flip portrait/landscape to match output
image = image.PerformTransform(new RotationTransform(90));
}
};
printOp.DrawPage += (_, args) =>
{
var image = printTarget[args.PageNr].Render();
try
{
var ctx = args.Context;
var cairoCtx = ctx.CairoContext;

if (Math.Sign(image.Width - image.Height) != Math.Sign(ctx.Width - ctx.Height))
{
// Flip portrait/landscape to match output
image = image.PerformTransform(new RotationTransform(90));
}

// Fit the image into the output rect (centered) while maintaining its aspect ratio
var heightBound = image.Width / ctx.Width < image.Height / ctx.Height;
var targetWidth = heightBound ? image.Width * ctx.Height / image.Height : ctx.Width;
var targetHeight = heightBound ? ctx.Height : image.Height * ctx.Width / image.Width;
var targetX = (ctx.Width - targetWidth) / 2;
var targetY = (ctx.Height - targetHeight) / 2;
cairoCtx.Translate(targetX, targetY);
cairoCtx.Scale(targetWidth / image.Width, targetHeight / image.Height);
// Fit the image into the output rect (centered) while maintaining its aspect ratio
var heightBound = image.Width / ctx.Width < image.Height / ctx.Height;
var targetWidth = heightBound ? image.Width * ctx.Height / image.Height : ctx.Width;
var targetHeight = heightBound ? ctx.Height : image.Height * ctx.Width / image.Width;
var targetX = (ctx.Width - targetWidth) / 2;
var targetY = (ctx.Height - targetHeight) / 2;
cairoCtx.Translate(targetX, targetY);
cairoCtx.Scale(targetWidth / image.Width, targetHeight / image.Height);

Gdk.CairoHelper.SetSourcePixbuf(cairoCtx, image.AsPixbuf(), 0, 0);
cairoCtx.Paint();
}
finally
{
image.Dispose();
}
};
printOp.EndPrint += (_, args) =>
Gdk.CairoHelper.SetSourcePixbuf(cairoCtx, image.AsPixbuf(), 0, 0);
cairoCtx.Paint();
}
finally
{
Log.Event(EventType.Print, new EventParams
{
Name = MiscResources.Print,
Pages = printOp.NPagesToPrint
});
};
var result = printOp.Run(PrintOperationAction.PrintDialog, (Window) parentWindow.ControlObject);
return result == PrintOperationResult.Apply;
});
image.Dispose();
}
};
printOp.EndPrint += (_, args) =>
{
Log.Event(EventType.Print, new EventParams
{
Name = MiscResources.Print,
Pages = printOp.NPagesToPrint
});
};
var result = printOp.Run(PrintOperationAction.PrintDialog, (Window) parentWindow.ControlObject);
return Task.FromResult(result == PrintOperationResult.Apply);
}
}
54 changes: 24 additions & 30 deletions NAPS2.Lib.Mac/ImportExport/MacScannedImagePrinter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,44 +4,38 @@ namespace NAPS2.ImportExport;

public class MacScannedImagePrinter : IScannedImagePrinter
{
public async Task<bool> PromptToPrint(
public Task<bool> PromptToPrint(
Eto.Forms.Window parentWindow, IList<ProcessedImage> images, IList<ProcessedImage> selectedImages)
{
if (!images.Any())
{
return false;
return Task.FromResult(false);
}
return await Task.Run(() =>
using var view = new PaginatedImageView(images);
var printOp = NSPrintOperation.FromView(view, new NSPrintInfo
{
return Invoker.Current.InvokeGet(() =>
LeftMargin = 0,
BottomMargin = 0,
RightMargin = 0,
TopMargin = 0,
HorizontalPagination = NSPrintingPaginationMode.Fit,
VerticalPagination = NSPrintingPaginationMode.Fit,
HorizontallyCentered = true,
VerticallyCentered = true,
Orientation = view.CurrentImage!.Width > view.CurrentImage.Height
? NSPrintingOrientation.Landscape
: NSPrintingOrientation.Portrait
});
if (printOp.RunOperation())
{
Log.Event(EventType.Print, new EventParams
{
using var view = new PaginatedImageView(images);
var printOp = NSPrintOperation.FromView(view, new NSPrintInfo
{
LeftMargin = 0,
BottomMargin = 0,
RightMargin = 0,
TopMargin = 0,
HorizontalPagination = NSPrintingPaginationMode.Fit,
VerticalPagination = NSPrintingPaginationMode.Fit,
HorizontallyCentered = true,
VerticallyCentered = true,
Orientation = view.CurrentImage!.Width > view.CurrentImage.Height
? NSPrintingOrientation.Landscape
: NSPrintingOrientation.Portrait
});
if (printOp.RunOperation())
{
Log.Event(EventType.Print, new EventParams
{
Name = MiscResources.Print,
Pages = (int) printOp.PageRange.Length
});
return true;
}
return false;
Name = MiscResources.Print,
Pages = (int) printOp.PageRange.Length
});
});
return Task.FromResult(true);
}
return Task.FromResult(false);
}

private class PaginatedImageView : NSBox
Expand Down

0 comments on commit 91a57ea

Please sign in to comment.