mirror of
https://github.com/cyanfish/naps2.git
synced 2024-09-11 15:26:55 +03:00
parent
fdcec857e9
commit
91a57ea84f
@ -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)
|
||||
{
|
||||
printOp.CurrentPage = images.IndexOf(selectedImages[0]);
|
||||
}
|
||||
var printTarget = images;
|
||||
printOp.BeginPrint += (_, args) =>
|
||||
{
|
||||
if (printOp.PrintSettings.PrintPages == PrintPages.Selection)
|
||||
{
|
||||
NPages = images.Count,
|
||||
UseFullPage = true,
|
||||
HasSelection = selectedImages.Count > 1,
|
||||
SupportSelection = selectedImages.Count > 1
|
||||
};
|
||||
if (selectedImages.Count == 1)
|
||||
{
|
||||
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) =>
|
||||
|
||||
// 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
|
||||
{
|
||||
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);
|
||||
|
||||
Gdk.CairoHelper.SetSourcePixbuf(cairoCtx, image.AsPixbuf(), 0, 0);
|
||||
cairoCtx.Paint();
|
||||
}
|
||||
finally
|
||||
{
|
||||
image.Dispose();
|
||||
}
|
||||
};
|
||||
printOp.EndPrint += (_, args) =>
|
||||
image.Dispose();
|
||||
}
|
||||
};
|
||||
printOp.EndPrint += (_, args) =>
|
||||
{
|
||||
Log.Event(EventType.Print, new EventParams
|
||||
{
|
||||
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;
|
||||
});
|
||||
Name = MiscResources.Print,
|
||||
Pages = printOp.NPagesToPrint
|
||||
});
|
||||
};
|
||||
var result = printOp.Run(PrintOperationAction.PrintDialog, (Window) parentWindow.ControlObject);
|
||||
return Task.FromResult(result == PrintOperationResult.Apply);
|
||||
}
|
||||
}
|
@ -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(() =>
|
||||
{
|
||||
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;
|
||||
});
|
||||
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 Task.FromResult(true);
|
||||
}
|
||||
return Task.FromResult(false);
|
||||
}
|
||||
|
||||
private class PaginatedImageView : NSBox
|
||||
|
Loading…
Reference in New Issue
Block a user