Skip to content

Package: PrintJ2DOp

PrintJ2DOp

nameinstructionbranchcomplexitylinemethod
PrintJ2DOp()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
execute(PrintOp, EditableImage, BufferedImage)
M: 25 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 9 C: 0
0%
M: 1 C: 0
0%
lambda$execute$0(EditableImage, Graphics, PageFormat, int)
M: 62 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 11 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*
2: * *********************************************************************************************************************
3: *
4: * Mistral: open source imaging engine
5: * http://tidalwave.it/projects/mistral
6: *
7: * Copyright (C) 2003 - 2023 by Tidalwave s.a.s. (http://tidalwave.it)
8: *
9: * *********************************************************************************************************************
10: *
11: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
12: * the License. You may obtain a copy of the License at
13: *
14: * http://www.apache.org/licenses/LICENSE-2.0
15: *
16: * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
17: * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
18: * specific language governing permissions and limitations under the License.
19: *
20: * *********************************************************************************************************************
21: *
22: * git clone https://bitbucket.org/tidalwave/mistral-src
23: * git clone https://github.com/tidalwave-it/mistral-src
24: *
25: * *********************************************************************************************************************
26: */
27: package it.tidalwave.image.java2d;
28:
29: import java.awt.Graphics2D;
30: import java.awt.image.BufferedImage;
31: import java.awt.print.Printable;
32: import java.awt.print.PrinterException;
33: import it.tidalwave.image.EditableImage;
34: import it.tidalwave.image.op.OperationImplementation;
35: import it.tidalwave.image.op.PaintOp;
36: import it.tidalwave.image.op.PrintOp;
37:
38: /***********************************************************************************************************************
39: *
40: * @author Fabrizio Giudici
41: *
42: **********************************************************************************************************************/
43: public class PrintJ2DOp extends OperationImplementation<PrintOp, BufferedImage>
44: {
45: public PrintJ2DOp()
46: {
47: }
48:
49: @Override
50: protected BufferedImage execute (final PrintOp operation, final EditableImage image, final BufferedImage model)
51: {
52: final Printable printable = (graphics, pageFormat, pageIndex) ->
53: {
54:• if (pageIndex > 0)
55: {
56: return Printable.NO_SUCH_PAGE;
57: }
58:
59: final var g2d = (Graphics2D)graphics;
60: g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
61: final var xScale = pageFormat.getImageableWidth() / image.getWidth();
62: final var yScale = pageFormat.getImageableHeight() / image.getHeight();
63: final var aspectScale = Math.min(xScale, yScale);
64: final var width = (int)Math.round(image.getWidth() * aspectScale);
65: final var height = (int)Math.round(image.getHeight() * aspectScale);
66: image.executeInPlace(new PaintOp(g2d, 0, 0, width, height, null, null));
67: return Printable.PAGE_EXISTS;
68: };
69:
70: final var pj = operation.getPrinterJob();
71: pj.setPrintable(printable);
72:
73:• if (operation.confirmPrint())
74: {
75: try
76: {
77: pj.print(operation.getPrintRequestAttributeSet());
78: }
79: catch (PrinterException e)
80: {
81: throw new RuntimeException(e);
82: }
83: }
84:
85: return model;
86: }
87: }